org.el 943 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (declare-function org-element-update-syntax "org-element" ())
  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,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\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 jira: Add a jira:ticket protocol to Org-mode" org-jira)
  624. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  625. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  626. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  627. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  628. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  629. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  630. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  631. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  632. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  633. (const :tag "C registry: A registry for Org-mode links" org-registry)
  634. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  635. (const :tag "C secretary: Team management with org-mode" org-secretary)
  636. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  637. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  638. (const :tag "C track: Keep up with Org-mode development" org-track)
  639. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  640. (const :tag "C vm: Links to VM folders/messages" org-vm)
  641. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  642. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  643. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  644. (defvar org-export--registered-backends) ; From ox.el.
  645. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  646. (declare-function org-export-backend-name "ox" (backend))
  647. (defcustom org-export-backends '(ascii html icalendar latex)
  648. "List of export back-ends that should be always available.
  649. If a description starts with <C>, the file is not part of Emacs
  650. and loading it will require that you have downloaded and properly
  651. installed the Org mode distribution.
  652. Unlike to `org-modules', libraries in this list will not be
  653. loaded along with Org, but only once the export framework is
  654. needed.
  655. This variable needs to be set before org.el is loaded. If you
  656. need to make a change while Emacs is running, use the customize
  657. interface or run the following code, where VAL stands for the new
  658. value of the variable, after updating it:
  659. \(progn
  660. \(setq org-export--registered-backends
  661. \(org-remove-if-not
  662. \(lambda (backend)
  663. \(let ((name (org-export-backend-name backend)))
  664. \(or (memq name val)
  665. \(catch 'parentp
  666. \(dolist (b val)
  667. \(and (org-export-derived-backend-p b name)
  668. \(throw 'parentp t)))))))
  669. org-export--registered-backends))
  670. \(let ((new-list (mapcar 'org-export-backend-name
  671. org-export--registered-backends)))
  672. \(dolist (backend val)
  673. \(cond
  674. \((not (load (format \"ox-%s\" backend) t t))
  675. \(message \"Problems while trying to load export back-end `%s'\"
  676. backend))
  677. \((not (memq backend new-list)) (push backend new-list))))
  678. \(set-default 'org-export-backends new-list)))
  679. Adding a back-end to this list will also pull the back-end it
  680. depends on, if any."
  681. :group 'org
  682. :group 'org-export
  683. :version "24.4"
  684. :package-version '(Org . "8.0")
  685. :initialize 'custom-initialize-set
  686. :set (lambda (var val)
  687. (if (not (featurep 'ox)) (set-default var val)
  688. ;; Any back-end not required anymore (not present in VAL and not
  689. ;; a parent of any back-end in the new value) is removed from the
  690. ;; list of registered back-ends.
  691. (setq org-export--registered-backends
  692. (org-remove-if-not
  693. (lambda (backend)
  694. (let ((name (org-export-backend-name backend)))
  695. (or (memq name val)
  696. (catch 'parentp
  697. (dolist (b val)
  698. (and (org-export-derived-backend-p b name)
  699. (throw 'parentp t)))))))
  700. org-export--registered-backends))
  701. ;; Now build NEW-LIST of both new back-ends and required
  702. ;; parents.
  703. (let ((new-list (mapcar 'org-export-backend-name
  704. org-export--registered-backends)))
  705. (dolist (backend val)
  706. (cond
  707. ((not (load (format "ox-%s" backend) t t))
  708. (message "Problems while trying to load export back-end `%s'"
  709. backend))
  710. ((not (memq backend new-list)) (push backend new-list))))
  711. ;; Set VAR to that list with fixed dependencies.
  712. (set-default var new-list))))
  713. :type '(set :greedy t
  714. (const :tag " ascii Export buffer to ASCII format" ascii)
  715. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  716. (const :tag " html Export buffer to HTML format" html)
  717. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  718. (const :tag " latex Export buffer to LaTeX format" latex)
  719. (const :tag " man Export buffer to MAN format" man)
  720. (const :tag " md Export buffer to Markdown format" md)
  721. (const :tag " odt Export buffer to ODT format" odt)
  722. (const :tag " org Export buffer to Org format" org)
  723. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  724. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  725. (const :tag "C deck Export buffer to deck.js presentations" deck)
  726. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  727. (const :tag "C groff Export buffer to Groff format" groff)
  728. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  729. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  730. (const :tag "C s5 Export buffer to s5 presentations" s5)
  731. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  732. (eval-after-load 'ox
  733. '(mapc
  734. (lambda (backend)
  735. (condition-case nil (require (intern (format "ox-%s" backend)))
  736. (error (message "Problems while trying to load export back-end `%s'"
  737. backend))))
  738. org-export-backends))
  739. (defcustom org-support-shift-select nil
  740. "Non-nil means make shift-cursor commands select text when possible.
  741. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  742. start selecting a region, or enlarge regions started in this way.
  743. In Org-mode, in special contexts, these same keys are used for
  744. other purposes, important enough to compete with shift selection.
  745. Org tries to balance these needs by supporting `shift-select-mode'
  746. outside these special contexts, under control of this variable.
  747. The default of this variable is nil, to avoid confusing behavior. Shifted
  748. cursor keys will then execute Org commands in the following contexts:
  749. - on a headline, changing TODO state (left/right) and priority (up/down)
  750. - on a time stamp, changing the time
  751. - in a plain list item, changing the bullet type
  752. - in a property definition line, switching between allowed values
  753. - in the BEGIN line of a clock table (changing the time block).
  754. Outside these contexts, the commands will throw an error.
  755. When this variable is t and the cursor is not in a special
  756. context, Org-mode will support shift-selection for making and
  757. enlarging regions. To make this more effective, the bullet
  758. cycling will no longer happen anywhere in an item line, but only
  759. if the cursor is exactly on the bullet.
  760. If you set this variable to the symbol `always', then the keys
  761. will not be special in headlines, property lines, and item lines,
  762. to make shift selection work there as well. If this is what you
  763. want, you can use the following alternative commands: `C-c C-t'
  764. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  765. can be used to switch TODO sets, `C-c -' to cycle item bullet
  766. types, and properties can be edited by hand or in column view.
  767. However, when the cursor is on a timestamp, shift-cursor commands
  768. will still edit the time stamp - this is just too good to give up.
  769. XEmacs user should have this variable set to nil, because
  770. `shift-select-mode' is in Emacs 23 or later only."
  771. :group 'org
  772. :type '(choice
  773. (const :tag "Never" nil)
  774. (const :tag "When outside special context" t)
  775. (const :tag "Everywhere except timestamps" always)))
  776. (defcustom org-loop-over-headlines-in-active-region nil
  777. "Shall some commands act upon headlines in the active region?
  778. When set to `t', some commands will be performed in all headlines
  779. within the active region.
  780. When set to `start-level', some commands will be performed in all
  781. headlines within the active region, provided that these headlines
  782. are of the same level than the first one.
  783. When set to a string, those commands will be performed on the
  784. matching headlines within the active region. Such string must be
  785. a tags/property/todo match as it is used in the agenda tags view.
  786. The list of commands is: `org-schedule', `org-deadline',
  787. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  788. `org-archive-to-archive-sibling'. The archiving commands skip
  789. already archived entries."
  790. :type '(choice (const :tag "Don't loop" nil)
  791. (const :tag "All headlines in active region" t)
  792. (const :tag "In active region, headlines at the same level than the first one" start-level)
  793. (string :tag "Tags/Property/Todo matcher"))
  794. :version "24.1"
  795. :group 'org-todo
  796. :group 'org-archive)
  797. (defgroup org-startup nil
  798. "Options concerning startup of Org-mode."
  799. :tag "Org Startup"
  800. :group 'org)
  801. (defcustom org-startup-folded t
  802. "Non-nil means entering Org-mode will switch to OVERVIEW.
  803. This can also be configured on a per-file basis by adding one of
  804. the following lines anywhere in the buffer:
  805. #+STARTUP: fold (or `overview', this is equivalent)
  806. #+STARTUP: nofold (or `showall', this is equivalent)
  807. #+STARTUP: content
  808. #+STARTUP: showeverything
  809. By default, this option is ignored when Org opens agenda files
  810. for the first time. If you want the agenda to honor the startup
  811. option, set `org-agenda-inhibit-startup' to nil."
  812. :group 'org-startup
  813. :type '(choice
  814. (const :tag "nofold: show all" nil)
  815. (const :tag "fold: overview" t)
  816. (const :tag "content: all headlines" content)
  817. (const :tag "show everything, even drawers" showeverything)))
  818. (defcustom org-startup-truncated t
  819. "Non-nil means entering Org-mode will set `truncate-lines'.
  820. This is useful since some lines containing links can be very long and
  821. uninteresting. Also tables look terrible when wrapped."
  822. :group 'org-startup
  823. :type 'boolean)
  824. (defcustom org-startup-indented nil
  825. "Non-nil means turn on `org-indent-mode' on startup.
  826. This can also be configured on a per-file basis by adding one of
  827. the following lines anywhere in the buffer:
  828. #+STARTUP: indent
  829. #+STARTUP: noindent"
  830. :group 'org-structure
  831. :type '(choice
  832. (const :tag "Not" nil)
  833. (const :tag "Globally (slow on startup in large files)" t)))
  834. (defcustom org-use-sub-superscripts t
  835. "Non-nil means interpret \"_\" and \"^\" for display.
  836. If you want to control how Org exports those characters, see
  837. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  838. used to be an alias for `org-export-with-sub-superscripts' in
  839. Org <8.0, it is not anymore.
  840. When this option is turned on, you can use TeX-like syntax for
  841. sub- and superscripts within the buffer. Several characters after
  842. \"_\" or \"^\" will be considered as a single item - so grouping
  843. with {} is normally not needed. For example, the following things
  844. will be parsed as single sub- or superscripts:
  845. 10^24 or 10^tau several digits will be considered 1 item.
  846. 10^-12 or 10^-tau a leading sign with digits or a word
  847. x^2-y^3 will be read as x^2 - y^3, because items are
  848. terminated by almost any nonword/nondigit char.
  849. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  850. Still, ambiguity is possible. So when in doubt, use {} to enclose
  851. the sub/superscript. If you set this variable to the symbol `{}',
  852. the braces are *required* in order to trigger interpretations as
  853. sub/superscript. This can be helpful in documents that need \"_\"
  854. frequently in plain text."
  855. :group 'org-startup
  856. :version "24.4"
  857. :package-version '(Org . "8.0")
  858. :type '(choice
  859. (const :tag "Always interpret" t)
  860. (const :tag "Only with braces" {})
  861. (const :tag "Never interpret" nil)))
  862. (defcustom org-startup-with-beamer-mode nil
  863. "Non-nil means turn on `org-beamer-mode' on startup.
  864. This can also be configured on a per-file basis by adding one of
  865. the following lines anywhere in the buffer:
  866. #+STARTUP: beamer"
  867. :group 'org-startup
  868. :version "24.1"
  869. :type 'boolean)
  870. (defcustom org-startup-align-all-tables nil
  871. "Non-nil means align all tables when visiting a file.
  872. This is useful when the column width in tables is forced with <N> cookies
  873. in table fields. Such tables will look correct only after the first re-align.
  874. This can also be configured on a per-file basis by adding one of
  875. the following lines anywhere in the buffer:
  876. #+STARTUP: align
  877. #+STARTUP: noalign"
  878. :group 'org-startup
  879. :type 'boolean)
  880. (defcustom org-startup-with-inline-images nil
  881. "Non-nil means show inline images when loading a new Org file.
  882. This can also be configured on a per-file basis by adding one of
  883. the following lines anywhere in the buffer:
  884. #+STARTUP: inlineimages
  885. #+STARTUP: noinlineimages"
  886. :group 'org-startup
  887. :version "24.1"
  888. :type 'boolean)
  889. (defcustom org-startup-with-latex-preview nil
  890. "Non-nil means preview LaTeX fragments when loading a new Org file.
  891. This can also be configured on a per-file basis by adding one of
  892. the following lines anywhere in the buffer:
  893. #+STARTUP: latexpreview
  894. #+STARTUP: nolatexpreview"
  895. :group 'org-startup
  896. :version "24.4"
  897. :package-version '(Org . "8.0")
  898. :type 'boolean)
  899. (defcustom org-insert-mode-line-in-empty-file nil
  900. "Non-nil means insert the first line setting Org-mode in empty files.
  901. When the function `org-mode' is called interactively in an empty file, this
  902. normally means that the file name does not automatically trigger Org-mode.
  903. To ensure that the file will always be in Org-mode in the future, a
  904. line enforcing Org-mode will be inserted into the buffer, if this option
  905. has been set."
  906. :group 'org-startup
  907. :type 'boolean)
  908. (defcustom org-replace-disputed-keys nil
  909. "Non-nil means use alternative key bindings for some keys.
  910. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  911. These keys are also used by other packages like shift-selection-mode'
  912. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  913. If you want to use Org-mode together with one of these other modes,
  914. or more generally if you would like to move some Org-mode commands to
  915. other keys, set this variable and configure the keys with the variable
  916. `org-disputed-keys'.
  917. This option is only relevant at load-time of Org-mode, and must be set
  918. *before* org.el is loaded. Changing it requires a restart of Emacs to
  919. become effective."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (defcustom org-use-extra-keys nil
  923. "Non-nil means use extra key sequence definitions for certain commands.
  924. This happens automatically if you run XEmacs or if `window-system'
  925. is nil. This variable lets you do the same manually. You must
  926. set it before loading org.
  927. Example: on Carbon Emacs 22 running graphically, with an external
  928. keyboard on a Powerbook, the default way of setting M-left might
  929. not work for either Alt or ESC. Setting this variable will make
  930. it work for ESC."
  931. :group 'org-startup
  932. :type 'boolean)
  933. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  934. (defcustom org-disputed-keys
  935. '(([(shift up)] . [(meta p)])
  936. ([(shift down)] . [(meta n)])
  937. ([(shift left)] . [(meta -)])
  938. ([(shift right)] . [(meta +)])
  939. ([(control shift right)] . [(meta shift +)])
  940. ([(control shift left)] . [(meta shift -)]))
  941. "Keys for which Org-mode and other modes compete.
  942. This is an alist, cars are the default keys, second element specifies
  943. the alternative to use when `org-replace-disputed-keys' is t.
  944. Keys can be specified in any syntax supported by `define-key'.
  945. The value of this option takes effect only at Org-mode's startup,
  946. therefore you'll have to restart Emacs to apply it after changing."
  947. :group 'org-startup
  948. :type 'alist)
  949. (defun org-key (key)
  950. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  951. Or return the original if not disputed.
  952. Also apply the translations defined in `org-xemacs-key-equivalents'."
  953. (when org-replace-disputed-keys
  954. (let* ((nkey (key-description key))
  955. (x (org-find-if (lambda (x)
  956. (equal (key-description (car x)) nkey))
  957. org-disputed-keys)))
  958. (setq key (if x (cdr x) key))))
  959. (when (featurep 'xemacs)
  960. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  961. key)
  962. (defun org-find-if (predicate seq)
  963. (catch 'exit
  964. (while seq
  965. (if (funcall predicate (car seq))
  966. (throw 'exit (car seq))
  967. (pop seq)))))
  968. (defun org-defkey (keymap key def)
  969. "Define a key, possibly translated, as returned by `org-key'."
  970. (define-key keymap (org-key key) def))
  971. (defcustom org-ellipsis nil
  972. "The ellipsis to use in the Org-mode outline.
  973. When nil, just use the standard three dots.
  974. When a string, use that string instead.
  975. When a face, use the standard 3 dots, but with the specified face.
  976. The change affects only Org-mode (which will then use its own display table).
  977. Changing this requires executing \\[org-mode] in a buffer to become
  978. effective."
  979. :group 'org-startup
  980. :type '(choice (const :tag "Default" nil)
  981. (face :tag "Face" :value org-warning)
  982. (string :tag "String" :value "...#")))
  983. (defvar org-display-table nil
  984. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  985. (defgroup org-keywords nil
  986. "Keywords in Org-mode."
  987. :tag "Org Keywords"
  988. :group 'org)
  989. (defcustom org-closed-keep-when-no-todo nil
  990. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  991. :group 'org-todo
  992. :group 'org-keywords
  993. :version "24.4"
  994. :package-version '(Org . "8.0")
  995. :type 'boolean)
  996. (defgroup org-structure nil
  997. "Options concerning the general structure of Org-mode files."
  998. :tag "Org Structure"
  999. :group 'org)
  1000. (defgroup org-reveal-location nil
  1001. "Options about how to make context of a location visible."
  1002. :tag "Org Reveal Location"
  1003. :group 'org-structure)
  1004. (defcustom org-show-context-detail '((isearch . lineage)
  1005. (bookmark-jump . lineage)
  1006. (default . ancestors))
  1007. "Alist between context and visibility span when revealing a location.
  1008. \\<org-mode-map>Some actions may move point into invisible
  1009. locations. As a consequence, Org always expose a neighborhood
  1010. around point. How much is shown depends on the initial action,
  1011. or context. Valid contexts are
  1012. agenda when exposing an entry from the agenda
  1013. org-goto when using the command `org-goto' (\\[org-goto])
  1014. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1015. tags-tree when constructing a sparse tree based on tags matches
  1016. link-search when exposing search matches associated with a link
  1017. mark-goto when exposing the jump goal of a mark
  1018. bookmark-jump when exposing a bookmark location
  1019. isearch when exiting from an incremental search
  1020. default default for all contexts not set explicitly
  1021. Allowed visibility spans are
  1022. minimal show current headline; if point is not on headline,
  1023. also show entry
  1024. local show current headline, entry and next headline
  1025. ancestors show current headline and its direct ancestors; if
  1026. point is not on headline, also show entry
  1027. lineage show current headline, its direct ancestors and all
  1028. their children; if point is not on headline, also show
  1029. entry and first child
  1030. tree show current headline, its direct ancestors and all
  1031. their children; if point is not on headline, also show
  1032. entry and all children
  1033. canonical show current headline, its direct ancestors along with
  1034. their entries and children; if point is not located on
  1035. the headline, also show current entry and all children
  1036. As special cases, a nil or t value means show all contexts in
  1037. `minimal' or `canonical' view, respectively.
  1038. Some views can make displayed information very compact, but also
  1039. make it harder to edit the location of the match. In such
  1040. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1041. more context."
  1042. :group 'org-reveal-location
  1043. :version "25.1"
  1044. :package-version '(Org . "8.3")
  1045. :type '(choice
  1046. (const :tag "Canonical" t)
  1047. (const :tag "Minimal" nil)
  1048. (repeat :greedy t :tag "Individual contexts"
  1049. (cons
  1050. (choice :tag "Context"
  1051. (const agenda)
  1052. (const org-goto)
  1053. (const occur-tree)
  1054. (const tags-tree)
  1055. (const link-search)
  1056. (const mark-goto)
  1057. (const bookmark-jump)
  1058. (const isearch)
  1059. (const default))
  1060. (choice :tag "Detail level"
  1061. (const minimal)
  1062. (const local)
  1063. (const ancestors)
  1064. (const lineage)
  1065. (const tree)
  1066. (const canonical))))))
  1067. (defcustom org-indirect-buffer-display 'other-window
  1068. "How should indirect tree buffers be displayed?
  1069. This applies to indirect buffers created with the commands
  1070. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1071. Valid values are:
  1072. current-window Display in the current window
  1073. other-window Just display in another window.
  1074. dedicated-frame Create one new frame, and re-use it each time.
  1075. new-frame Make a new frame each time. Note that in this case
  1076. previously-made indirect buffers are kept, and you need to
  1077. kill these buffers yourself."
  1078. :group 'org-structure
  1079. :group 'org-agenda-windows
  1080. :type '(choice
  1081. (const :tag "In current window" current-window)
  1082. (const :tag "In current frame, other window" other-window)
  1083. (const :tag "Each time a new frame" new-frame)
  1084. (const :tag "One dedicated frame" dedicated-frame)))
  1085. (defcustom org-use-speed-commands nil
  1086. "Non-nil means activate single letter commands at beginning of a headline.
  1087. This may also be a function to test for appropriate locations where speed
  1088. commands should be active.
  1089. For example, to activate speed commands when the point is on any
  1090. star at the beginning of the headline, you can do this:
  1091. (setq org-use-speed-commands
  1092. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1093. :group 'org-structure
  1094. :type '(choice
  1095. (const :tag "Never" nil)
  1096. (const :tag "At beginning of headline stars" t)
  1097. (function)))
  1098. (defcustom org-speed-commands-user nil
  1099. "Alist of additional speed commands.
  1100. This list will be checked before `org-speed-commands-default'
  1101. when the variable `org-use-speed-commands' is non-nil
  1102. and when the cursor is at the beginning of a headline.
  1103. The car if each entry is a string with a single letter, which must
  1104. be assigned to `self-insert-command' in the global map.
  1105. The cdr is either a command to be called interactively, a function
  1106. to be called, or a form to be evaluated.
  1107. An entry that is just a list with a single string will be interpreted
  1108. as a descriptive headline that will be added when listing the speed
  1109. commands in the Help buffer using the `?' speed command."
  1110. :group 'org-structure
  1111. :type '(repeat :value ("k" . ignore)
  1112. (choice :value ("k" . ignore)
  1113. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1114. (cons :tag "Letter and Command"
  1115. (string :tag "Command letter")
  1116. (choice
  1117. (function)
  1118. (sexp))))))
  1119. (defcustom org-bookmark-names-plist
  1120. '(:last-capture "org-capture-last-stored"
  1121. :last-refile "org-refile-last-stored"
  1122. :last-capture-marker "org-capture-last-stored-marker")
  1123. "Names for bookmarks automatically set by some Org commands.
  1124. This can provide strings as names for a number of bookmarks Org sets
  1125. automatically. The following keys are currently implemented:
  1126. :last-capture
  1127. :last-capture-marker
  1128. :last-refile
  1129. When a key does not show up in the property list, the corresponding bookmark
  1130. is not set."
  1131. :group 'org-structure
  1132. :type 'plist)
  1133. (defgroup org-cycle nil
  1134. "Options concerning visibility cycling in Org-mode."
  1135. :tag "Org Cycle"
  1136. :group 'org-structure)
  1137. (defcustom org-cycle-skip-children-state-if-no-children t
  1138. "Non-nil means skip CHILDREN state in entries that don't have any."
  1139. :group 'org-cycle
  1140. :type 'boolean)
  1141. (defcustom org-cycle-max-level nil
  1142. "Maximum level which should still be subject to visibility cycling.
  1143. Levels higher than this will, for cycling, be treated as text, not a headline.
  1144. When `org-odd-levels-only' is set, a value of N in this variable actually
  1145. means 2N-1 stars as the limiting headline.
  1146. When nil, cycle all levels.
  1147. Note that the limiting level of cycling is also influenced by
  1148. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1149. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1150. than its value."
  1151. :group 'org-cycle
  1152. :type '(choice
  1153. (const :tag "No limit" nil)
  1154. (integer :tag "Maximum level")))
  1155. (defcustom org-hide-block-startup nil
  1156. "Non-nil means entering Org-mode will fold all blocks.
  1157. This can also be set in on a per-file basis with
  1158. #+STARTUP: hideblocks
  1159. #+STARTUP: showblocks"
  1160. :group 'org-startup
  1161. :group 'org-cycle
  1162. :type 'boolean)
  1163. (defcustom org-cycle-global-at-bob nil
  1164. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1165. This makes it possible to do global cycling without having to use S-TAB or
  1166. \\[universal-argument] TAB. For this special case to work, the first line
  1167. of the buffer must not be a headline -- it may be empty or some other text.
  1168. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1169. sure the cursor stays at the beginning of the buffer. When this option is
  1170. nil, don't do anything special at the beginning of the buffer."
  1171. :group 'org-cycle
  1172. :type 'boolean)
  1173. (defcustom org-cycle-level-after-item/entry-creation t
  1174. "Non-nil means cycle entry level or item indentation in new empty entries.
  1175. When the cursor is at the end of an empty headline, i.e., with only stars
  1176. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1177. and then all possible ancestor states, before returning to the original state.
  1178. This makes data entry extremely fast: M-RET to create a new headline,
  1179. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1180. When the cursor is at the end of an empty plain list item, one TAB will
  1181. make it a subitem, two or more tabs will back up to make this an item
  1182. higher up in the item hierarchy."
  1183. :group 'org-cycle
  1184. :type 'boolean)
  1185. (defcustom org-cycle-emulate-tab t
  1186. "Where should `org-cycle' emulate TAB.
  1187. nil Never
  1188. white Only in completely white lines
  1189. whitestart Only at the beginning of lines, before the first non-white char
  1190. t Everywhere except in headlines
  1191. exc-hl-bol Everywhere except at the start of a headline
  1192. If TAB is used in a place where it does not emulate TAB, the current subtree
  1193. visibility is cycled."
  1194. :group 'org-cycle
  1195. :type '(choice (const :tag "Never" nil)
  1196. (const :tag "Only in completely white lines" white)
  1197. (const :tag "Before first char in a line" whitestart)
  1198. (const :tag "Everywhere except in headlines" t)
  1199. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1200. (defcustom org-cycle-separator-lines 2
  1201. "Number of empty lines needed to keep an empty line between collapsed trees.
  1202. If you leave an empty line between the end of a subtree and the following
  1203. headline, this empty line is hidden when the subtree is folded.
  1204. Org-mode will leave (exactly) one empty line visible if the number of
  1205. empty lines is equal or larger to the number given in this variable.
  1206. So the default 2 means at least 2 empty lines after the end of a subtree
  1207. are needed to produce free space between a collapsed subtree and the
  1208. following headline.
  1209. If the number is negative, and the number of empty lines is at least -N,
  1210. all empty lines are shown.
  1211. Special case: when 0, never leave empty lines in collapsed view."
  1212. :group 'org-cycle
  1213. :type 'integer)
  1214. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1215. (defcustom org-pre-cycle-hook nil
  1216. "Hook that is run before visibility cycling is happening.
  1217. The function(s) in this hook must accept a single argument which indicates
  1218. the new state that will be set right after running this hook. The
  1219. argument is a symbol. Before a global state change, it can have the values
  1220. `overview', `content', or `all'. Before a local state change, it can have
  1221. the values `folded', `children', or `subtree'."
  1222. :group 'org-cycle
  1223. :type 'hook)
  1224. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1225. org-cycle-hide-drawers
  1226. org-cycle-hide-inline-tasks
  1227. org-cycle-show-empty-lines
  1228. org-optimize-window-after-visibility-change)
  1229. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1230. The function(s) in this hook must accept a single argument which indicates
  1231. the new state that was set by the most recent `org-cycle' command. The
  1232. argument is a symbol. After a global state change, it can have the values
  1233. `overview', `contents', or `all'. After a local state change, it can have
  1234. the values `folded', `children', or `subtree'."
  1235. :group 'org-cycle
  1236. :type 'hook)
  1237. (defgroup org-edit-structure nil
  1238. "Options concerning structure editing in Org-mode."
  1239. :tag "Org Edit Structure"
  1240. :group 'org-structure)
  1241. (defcustom org-odd-levels-only nil
  1242. "Non-nil means skip even levels and only use odd levels for the outline.
  1243. This has the effect that two stars are being added/taken away in
  1244. promotion/demotion commands. It also influences how levels are
  1245. handled by the exporters.
  1246. Changing it requires restart of `font-lock-mode' to become effective
  1247. for fontification also in regions already fontified.
  1248. You may also set this on a per-file basis by adding one of the following
  1249. lines to the buffer:
  1250. #+STARTUP: odd
  1251. #+STARTUP: oddeven"
  1252. :group 'org-edit-structure
  1253. :group 'org-appearance
  1254. :type 'boolean)
  1255. (defcustom org-adapt-indentation t
  1256. "Non-nil means adapt indentation to outline node level.
  1257. When this variable is set, Org assumes that you write outlines by
  1258. indenting text in each node to align with the headline (after the
  1259. stars). The following issues are influenced by this variable:
  1260. - The indentation is increased by one space in a demotion
  1261. command, and decreased by one in a promotion command. However,
  1262. in the latter case, if shifting some line in the entry body
  1263. would alter document structure (e.g., insert a new headline),
  1264. indentation is not changed at all.
  1265. - Property drawers and planning information is inserted indented
  1266. when this variable is set. When nil, they will not be indented.
  1267. - TAB indents a line relative to current level. The lines below
  1268. a headline will be indented when this variable is set.
  1269. Note that this is all about true indentation, by adding and
  1270. removing space characters. See also `org-indent.el' which does
  1271. level-dependent indentation in a virtual way, i.e. at display
  1272. time in Emacs."
  1273. :group 'org-edit-structure
  1274. :type 'boolean)
  1275. (defcustom org-special-ctrl-a/e nil
  1276. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1277. When t, `C-a' will bring back the cursor to the beginning of the
  1278. headline text, i.e. after the stars and after a possible TODO
  1279. keyword. In an item, this will be the position after bullet and
  1280. check-box, if any. When the cursor is already at that position,
  1281. another `C-a' will bring it to the beginning of the line.
  1282. `C-e' will jump to the end of the headline, ignoring the presence
  1283. of tags in the headline. A second `C-e' will then jump to the
  1284. true end of the line, after any tags. This also means that, when
  1285. this variable is non-nil, `C-e' also will never jump beyond the
  1286. end of the heading of a folded section, i.e. not after the
  1287. ellipses.
  1288. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1289. normally, going to the true line boundary first. Only a directly
  1290. following, identical keypress will bring the cursor to the
  1291. special positions.
  1292. This may also be a cons cell where the behavior for `C-a' and
  1293. `C-e' is set separately."
  1294. :group 'org-edit-structure
  1295. :type '(choice
  1296. (const :tag "off" nil)
  1297. (const :tag "on: after stars/bullet and before tags first" t)
  1298. (const :tag "reversed: true line boundary first" reversed)
  1299. (cons :tag "Set C-a and C-e separately"
  1300. (choice :tag "Special C-a"
  1301. (const :tag "off" nil)
  1302. (const :tag "on: after stars/bullet first" t)
  1303. (const :tag "reversed: before stars/bullet first" reversed))
  1304. (choice :tag "Special C-e"
  1305. (const :tag "off" nil)
  1306. (const :tag "on: before tags first" t)
  1307. (const :tag "reversed: after tags first" reversed)))))
  1308. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1309. (defcustom org-special-ctrl-k nil
  1310. "Non-nil means `C-k' will behave specially in headlines.
  1311. When nil, `C-k' will call the default `kill-line' command.
  1312. When t, the following will happen while the cursor is in the headline:
  1313. - When the cursor is at the beginning of a headline, kill the entire
  1314. line and possible the folded subtree below the line.
  1315. - When in the middle of the headline text, kill the headline up to the tags.
  1316. - When after the headline text, kill the tags."
  1317. :group 'org-edit-structure
  1318. :type 'boolean)
  1319. (defcustom org-ctrl-k-protect-subtree nil
  1320. "Non-nil means, do not delete a hidden subtree with C-k.
  1321. When set to the symbol `error', simply throw an error when C-k is
  1322. used to kill (part-of) a headline that has hidden text behind it.
  1323. Any other non-nil value will result in a query to the user, if it is
  1324. OK to kill that hidden subtree. When nil, kill without remorse."
  1325. :group 'org-edit-structure
  1326. :version "24.1"
  1327. :type '(choice
  1328. (const :tag "Do not protect hidden subtrees" nil)
  1329. (const :tag "Protect hidden subtrees with a security query" t)
  1330. (const :tag "Never kill a hidden subtree with C-k" error)))
  1331. (defcustom org-special-ctrl-o t
  1332. "Non-nil means, make `C-o' insert a row in tables."
  1333. :group 'org-edit-structure
  1334. :type 'boolean)
  1335. (defcustom org-catch-invisible-edits nil
  1336. "Check if in invisible region before inserting or deleting a character.
  1337. Valid values are:
  1338. nil Do not check, so just do invisible edits.
  1339. error Throw an error and do nothing.
  1340. show Make point visible, and do the requested edit.
  1341. show-and-error Make point visible, then throw an error and abort the edit.
  1342. smart Make point visible, and do insertion/deletion if it is
  1343. adjacent to visible text and the change feels predictable.
  1344. Never delete a previously invisible character or add in the
  1345. middle or right after an invisible region. Basically, this
  1346. allows insertion and backward-delete right before ellipses.
  1347. FIXME: maybe in this case we should not even show?"
  1348. :group 'org-edit-structure
  1349. :version "24.1"
  1350. :type '(choice
  1351. (const :tag "Do not check" nil)
  1352. (const :tag "Throw error when trying to edit" error)
  1353. (const :tag "Unhide, but do not do the edit" show-and-error)
  1354. (const :tag "Show invisible part and do the edit" show)
  1355. (const :tag "Be smart and do the right thing" smart)))
  1356. (defcustom org-yank-folded-subtrees t
  1357. "Non-nil means when yanking subtrees, fold them.
  1358. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1359. it starts with a heading and all other headings in it are either children
  1360. or siblings, then fold all the subtrees. However, do this only if no
  1361. text after the yank would be swallowed into a folded tree by this action."
  1362. :group 'org-edit-structure
  1363. :type 'boolean)
  1364. (defcustom org-yank-adjusted-subtrees nil
  1365. "Non-nil means when yanking subtrees, adjust the level.
  1366. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1367. this function for details."
  1368. :group 'org-edit-structure
  1369. :type 'boolean)
  1370. (defcustom org-M-RET-may-split-line '((default . t))
  1371. "Non-nil means M-RET will split the line at the cursor position.
  1372. When nil, it will go to the end of the line before making a
  1373. new line.
  1374. You may also set this option in a different way for different
  1375. contexts. Valid contexts are:
  1376. headline when creating a new headline
  1377. item when creating a new item
  1378. table in a table field
  1379. default the value to be used for all contexts not explicitly
  1380. customized"
  1381. :group 'org-structure
  1382. :group 'org-table
  1383. :type '(choice
  1384. (const :tag "Always" t)
  1385. (const :tag "Never" nil)
  1386. (repeat :greedy t :tag "Individual contexts"
  1387. (cons
  1388. (choice :tag "Context"
  1389. (const headline)
  1390. (const item)
  1391. (const table)
  1392. (const default))
  1393. (boolean)))))
  1394. (defcustom org-insert-heading-respect-content nil
  1395. "Non-nil means insert new headings after the current subtree.
  1396. When nil, the new heading is created directly after the current line.
  1397. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1398. this variable on for the duration of the command."
  1399. :group 'org-structure
  1400. :type 'boolean)
  1401. (defcustom org-blank-before-new-entry '((heading . auto)
  1402. (plain-list-item . auto))
  1403. "Should `org-insert-heading' leave a blank line before new heading/item?
  1404. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1405. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1406. which case Org will look at the surrounding headings/items and try to
  1407. make an intelligent decision whether to insert a blank line or not.
  1408. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1409. the setting here is ignored and no empty line is inserted to avoid breaking
  1410. the list structure."
  1411. :group 'org-edit-structure
  1412. :type '(list
  1413. (cons (const heading)
  1414. (choice (const :tag "Never" nil)
  1415. (const :tag "Always" t)
  1416. (const :tag "Auto" auto)))
  1417. (cons (const plain-list-item)
  1418. (choice (const :tag "Never" nil)
  1419. (const :tag "Always" t)
  1420. (const :tag "Auto" auto)))))
  1421. (defcustom org-insert-heading-hook nil
  1422. "Hook being run after inserting a new heading."
  1423. :group 'org-edit-structure
  1424. :type 'hook)
  1425. (defcustom org-enable-fixed-width-editor t
  1426. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1427. This currently only means they are never auto-wrapped.
  1428. When nil, such lines will be treated like ordinary lines."
  1429. :group 'org-edit-structure
  1430. :type 'boolean)
  1431. (defcustom org-goto-auto-isearch t
  1432. "Non-nil means typing characters in `org-goto' starts incremental search.
  1433. When nil, you can use these keybindings to navigate the buffer:
  1434. q Quit the org-goto interface
  1435. n Go to the next visible heading
  1436. p Go to the previous visible heading
  1437. f Go one heading forward on same level
  1438. b Go one heading backward on same level
  1439. u Go one heading up"
  1440. :group 'org-edit-structure
  1441. :type 'boolean)
  1442. (defgroup org-sparse-trees nil
  1443. "Options concerning sparse trees in Org-mode."
  1444. :tag "Org Sparse Trees"
  1445. :group 'org-structure)
  1446. (defcustom org-highlight-sparse-tree-matches t
  1447. "Non-nil means highlight all matches that define a sparse tree.
  1448. The highlights will automatically disappear the next time the buffer is
  1449. changed by an edit command."
  1450. :group 'org-sparse-trees
  1451. :type 'boolean)
  1452. (defcustom org-remove-highlights-with-change t
  1453. "Non-nil means any change to the buffer will remove temporary highlights.
  1454. Such highlights are created by `org-occur' and `org-clock-display'.
  1455. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1456. The highlights created by `org-toggle-latex-fragment' always need
  1457. `C-c C-x C-l' to be removed."
  1458. :group 'org-sparse-trees
  1459. :group 'org-time
  1460. :type 'boolean)
  1461. (defcustom org-occur-hook '(org-first-headline-recenter)
  1462. "Hook that is run after `org-occur' has constructed a sparse tree.
  1463. This can be used to recenter the window to show as much of the structure
  1464. as possible."
  1465. :group 'org-sparse-trees
  1466. :type 'hook)
  1467. (defgroup org-imenu-and-speedbar nil
  1468. "Options concerning imenu and speedbar in Org-mode."
  1469. :tag "Org Imenu and Speedbar"
  1470. :group 'org-structure)
  1471. (defcustom org-imenu-depth 2
  1472. "The maximum level for Imenu access to Org-mode headlines.
  1473. This also applied for speedbar access."
  1474. :group 'org-imenu-and-speedbar
  1475. :type 'integer)
  1476. (defgroup org-table nil
  1477. "Options concerning tables in Org-mode."
  1478. :tag "Org Table"
  1479. :group 'org)
  1480. (defcustom org-enable-table-editor 'optimized
  1481. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1482. When nil, such lines will be treated like ordinary lines.
  1483. When equal to the symbol `optimized', the table editor will be optimized to
  1484. do the following:
  1485. - Automatic overwrite mode in front of whitespace in table fields.
  1486. This makes the structure of the table stay in tact as long as the edited
  1487. field does not exceed the column width.
  1488. - Minimize the number of realigns. Normally, the table is aligned each time
  1489. TAB or RET are pressed to move to another field. With optimization this
  1490. happens only if changes to a field might have changed the column width.
  1491. Optimization requires replacing the functions `self-insert-command',
  1492. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1493. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1494. very good at guessing when a re-align will be necessary, but you can always
  1495. force one with \\[org-ctrl-c-ctrl-c].
  1496. If you would like to use the optimized version in Org-mode, but the
  1497. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1498. This variable can be used to turn on and off the table editor during a session,
  1499. but in order to toggle optimization, a restart is required.
  1500. See also the variable `org-table-auto-blank-field'."
  1501. :group 'org-table
  1502. :type '(choice
  1503. (const :tag "off" nil)
  1504. (const :tag "on" t)
  1505. (const :tag "on, optimized" optimized)))
  1506. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1507. (version<= emacs-version "24.1"))
  1508. "Non-nil means cluster self-insert commands for undo when possible.
  1509. If this is set, then, like in the Emacs command loop, 20 consecutive
  1510. characters will be undone together.
  1511. This is configurable, because there is some impact on typing performance."
  1512. :group 'org-table
  1513. :type 'boolean)
  1514. (defcustom org-table-tab-recognizes-table.el t
  1515. "Non-nil means TAB will automatically notice a table.el table.
  1516. When it sees such a table, it moves point into it and - if necessary -
  1517. calls `table-recognize-table'."
  1518. :group 'org-table-editing
  1519. :type 'boolean)
  1520. (defgroup org-link nil
  1521. "Options concerning links in Org-mode."
  1522. :tag "Org Link"
  1523. :group 'org)
  1524. (defvar org-link-abbrev-alist-local nil
  1525. "Buffer-local version of `org-link-abbrev-alist', which see.
  1526. The value of this is taken from the #+LINK lines.")
  1527. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1528. (defcustom org-link-abbrev-alist nil
  1529. "Alist of link abbreviations.
  1530. The car of each element is a string, to be replaced at the start of a link.
  1531. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1532. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1533. [[linkkey:tag][description]]
  1534. The 'linkkey' must be a word word, starting with a letter, followed
  1535. by letters, numbers, '-' or '_'.
  1536. If REPLACE is a string, the tag will simply be appended to create the link.
  1537. If the string contains \"%s\", the tag will be inserted there. If the string
  1538. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1539. at that point (see the function `url-hexify-string'). If the string contains
  1540. the specifier \"%(my-function)\", then the custom function `my-function' will
  1541. be invoked: this function takes the tag as its only argument and must return
  1542. a string.
  1543. REPLACE may also be a function that will be called with the tag as the
  1544. only argument to create the link, which should be returned as a string.
  1545. See the manual for examples."
  1546. :group 'org-link
  1547. :type '(repeat
  1548. (cons
  1549. (string :tag "Protocol")
  1550. (choice
  1551. (string :tag "Format")
  1552. (function)))))
  1553. (defcustom org-descriptive-links t
  1554. "Non-nil means Org will display descriptive links.
  1555. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1556. \"Org Website\", hiding the link itself and just displaying its
  1557. description. When set to `nil', Org will display the full links
  1558. literally.
  1559. You can interactively set the value of this variable by calling
  1560. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1561. :group 'org-link
  1562. :type 'boolean)
  1563. (defcustom org-link-file-path-type 'adaptive
  1564. "How the path name in file links should be stored.
  1565. Valid values are:
  1566. relative Relative to the current directory, i.e. the directory of the file
  1567. into which the link is being inserted.
  1568. absolute Absolute path, if possible with ~ for home directory.
  1569. noabbrev Absolute path, no abbreviation of home directory.
  1570. adaptive Use relative path for files in the current directory and sub-
  1571. directories of it. For other files, use an absolute path."
  1572. :group 'org-link
  1573. :type '(choice
  1574. (const relative)
  1575. (const absolute)
  1576. (const noabbrev)
  1577. (const adaptive)))
  1578. (defvaralias 'org-activate-links 'org-highlight-links)
  1579. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1580. "Types of links that should be highlighted in Org-mode files.
  1581. This is a list of symbols, each one of them leading to the
  1582. highlighting of a certain link type.
  1583. You can still open links that are not highlighted.
  1584. In principle, it does not hurt to turn on highlighting for all
  1585. link types. There may be a small gain when turning off unused
  1586. link types. The types are:
  1587. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1588. angle Links in angular brackets that may contain whitespace like
  1589. <bbdb:Carsten Dominik>.
  1590. plain Plain links in normal text, no whitespace, like http://google.com.
  1591. radio Text that is matched by a radio target, see manual for details.
  1592. tag Tag settings in a headline (link to tag search).
  1593. date Time stamps (link to calendar).
  1594. footnote Footnote labels.
  1595. If you set this variable during an Emacs session, use `org-mode-restart'
  1596. in the Org buffer so that the change takes effect."
  1597. :group 'org-link
  1598. :group 'org-appearance
  1599. :type '(set :greedy t
  1600. (const :tag "Double bracket links" bracket)
  1601. (const :tag "Angular bracket links" angle)
  1602. (const :tag "Plain text links" plain)
  1603. (const :tag "Radio target matches" radio)
  1604. (const :tag "Tags" tag)
  1605. (const :tag "Timestamps" date)
  1606. (const :tag "Footnotes" footnote)))
  1607. (defcustom org-make-link-description-function nil
  1608. "Function to use for generating link descriptions from links.
  1609. When nil, the link location will be used. This function must take
  1610. two parameters: the first one is the link, the second one is the
  1611. description generated by `org-insert-link'. The function should
  1612. return the description to use."
  1613. :group 'org-link
  1614. :type '(choice (const nil) (function)))
  1615. (defgroup org-link-store nil
  1616. "Options concerning storing links in Org-mode."
  1617. :tag "Org Store Link"
  1618. :group 'org-link)
  1619. (defcustom org-url-hexify-p t
  1620. "When non-nil, hexify URL when creating a link."
  1621. :type 'boolean
  1622. :version "24.3"
  1623. :group 'org-link-store)
  1624. (defcustom org-email-link-description-format "Email %c: %.30s"
  1625. "Format of the description part of a link to an email or usenet message.
  1626. The following %-escapes will be replaced by corresponding information:
  1627. %F full \"From\" field
  1628. %f name, taken from \"From\" field, address if no name
  1629. %T full \"To\" field
  1630. %t first name in \"To\" field, address if no name
  1631. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1632. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1633. %s subject
  1634. %d date
  1635. %m message-id.
  1636. You may use normal field width specification between the % and the letter.
  1637. This is for example useful to limit the length of the subject.
  1638. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1639. :group 'org-link-store
  1640. :type 'string)
  1641. (defcustom org-from-is-user-regexp
  1642. (let (r1 r2)
  1643. (when (and user-mail-address (not (string= user-mail-address "")))
  1644. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1645. (when (and user-full-name (not (string= user-full-name "")))
  1646. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1647. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1648. "Regexp matched against the \"From:\" header of an email or usenet message.
  1649. It should match if the message is from the user him/herself."
  1650. :group 'org-link-store
  1651. :type 'regexp)
  1652. (defcustom org-context-in-file-links t
  1653. "Non-nil means file links from `org-store-link' contain context.
  1654. A search string will be added to the file name with :: as separator and
  1655. used to find the context when the link is activated by the command
  1656. `org-open-at-point'. When this option is t, the entire active region
  1657. will be placed in the search string of the file link. If set to a
  1658. positive integer, only the first n lines of context will be stored.
  1659. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1660. negates this setting for the duration of the command."
  1661. :group 'org-link-store
  1662. :type '(choice boolean integer))
  1663. (defcustom org-keep-stored-link-after-insertion nil
  1664. "Non-nil means keep link in list for entire session.
  1665. The command `org-store-link' adds a link pointing to the current
  1666. location to an internal list. These links accumulate during a session.
  1667. The command `org-insert-link' can be used to insert links into any
  1668. Org-mode file (offering completion for all stored links). When this
  1669. option is nil, every link which has been inserted once using \\[org-insert-link]
  1670. will be removed from the list, to make completing the unused links
  1671. more efficient."
  1672. :group 'org-link-store
  1673. :type 'boolean)
  1674. (defgroup org-link-follow nil
  1675. "Options concerning following links in Org-mode."
  1676. :tag "Org Follow Link"
  1677. :group 'org-link)
  1678. (defcustom org-link-translation-function nil
  1679. "Function to translate links with different syntax to Org syntax.
  1680. This can be used to translate links created for example by the Planner
  1681. or emacs-wiki packages to Org syntax.
  1682. The function must accept two parameters, a TYPE containing the link
  1683. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1684. which is everything after the link protocol. It should return a cons
  1685. with possibly modified values of type and path.
  1686. Org contains a function for this, so if you set this variable to
  1687. `org-translate-link-from-planner', you should be able follow many
  1688. links created by planner."
  1689. :group 'org-link-follow
  1690. :type '(choice (const nil) (function)))
  1691. (defcustom org-follow-link-hook nil
  1692. "Hook that is run after a link has been followed."
  1693. :group 'org-link-follow
  1694. :type 'hook)
  1695. (defcustom org-tab-follows-link nil
  1696. "Non-nil means on links TAB will follow the link.
  1697. Needs to be set before org.el is loaded.
  1698. This really should not be used, it does not make sense, and the
  1699. implementation is bad."
  1700. :group 'org-link-follow
  1701. :type 'boolean)
  1702. (defcustom org-return-follows-link nil
  1703. "Non-nil means on links RET will follow the link.
  1704. In tables, the special behavior of RET has precedence."
  1705. :group 'org-link-follow
  1706. :type 'boolean)
  1707. (defcustom org-mouse-1-follows-link
  1708. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1709. "Non-nil means mouse-1 on a link will follow the link.
  1710. A longer mouse click will still set point. Does not work on XEmacs.
  1711. Needs to be set before org.el is loaded."
  1712. :group 'org-link-follow
  1713. :version "24.4"
  1714. :package-version '(Org . "8.3")
  1715. :type '(choice
  1716. (const :tag "A double click follows the link" double)
  1717. (const :tag "Unconditionally follow the link with mouse-1" t)
  1718. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1719. (defcustom org-mark-ring-length 4
  1720. "Number of different positions to be recorded in the ring.
  1721. Changing this requires a restart of Emacs to work correctly."
  1722. :group 'org-link-follow
  1723. :type 'integer)
  1724. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1725. "Non-nil means internal links in Org files must exactly match a headline.
  1726. When nil, the link search tries to match a phrase with all words
  1727. in the search text."
  1728. :group 'org-link-follow
  1729. :version "24.1"
  1730. :type '(choice
  1731. (const :tag "Use fuzzy text search" nil)
  1732. (const :tag "Match only exact headline" t)
  1733. (const :tag "Match exact headline or query to create it"
  1734. query-to-create)))
  1735. (defcustom org-link-frame-setup
  1736. '((vm . vm-visit-folder-other-frame)
  1737. (vm-imap . vm-visit-imap-folder-other-frame)
  1738. (gnus . org-gnus-no-new-news)
  1739. (file . find-file-other-window)
  1740. (wl . wl-other-frame))
  1741. "Setup the frame configuration for following links.
  1742. When following a link with Emacs, it may often be useful to display
  1743. this link in another window or frame. This variable can be used to
  1744. set this up for the different types of links.
  1745. For VM, use any of
  1746. `vm-visit-folder'
  1747. `vm-visit-folder-other-window'
  1748. `vm-visit-folder-other-frame'
  1749. For Gnus, use any of
  1750. `gnus'
  1751. `gnus-other-frame'
  1752. `org-gnus-no-new-news'
  1753. For FILE, use any of
  1754. `find-file'
  1755. `find-file-other-window'
  1756. `find-file-other-frame'
  1757. For Wanderlust use any of
  1758. `wl'
  1759. `wl-other-frame'
  1760. For the calendar, use the variable `calendar-setup'.
  1761. For BBDB, it is currently only possible to display the matches in
  1762. another window."
  1763. :group 'org-link-follow
  1764. :type '(list
  1765. (cons (const vm)
  1766. (choice
  1767. (const vm-visit-folder)
  1768. (const vm-visit-folder-other-window)
  1769. (const vm-visit-folder-other-frame)))
  1770. (cons (const vm-imap)
  1771. (choice
  1772. (const vm-visit-imap-folder)
  1773. (const vm-visit-imap-folder-other-window)
  1774. (const vm-visit-imap-folder-other-frame)))
  1775. (cons (const gnus)
  1776. (choice
  1777. (const gnus)
  1778. (const gnus-other-frame)
  1779. (const org-gnus-no-new-news)))
  1780. (cons (const file)
  1781. (choice
  1782. (const find-file)
  1783. (const find-file-other-window)
  1784. (const find-file-other-frame)))
  1785. (cons (const wl)
  1786. (choice
  1787. (const wl)
  1788. (const wl-other-frame)))))
  1789. (defcustom org-display-internal-link-with-indirect-buffer nil
  1790. "Non-nil means use indirect buffer to display infile links.
  1791. Activating internal links (from one location in a file to another location
  1792. in the same file) normally just jumps to the location. When the link is
  1793. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1794. is displayed in
  1795. another window. When this option is set, the other window actually displays
  1796. an indirect buffer clone of the current buffer, to avoid any visibility
  1797. changes to the current buffer."
  1798. :group 'org-link-follow
  1799. :type 'boolean)
  1800. (defcustom org-open-non-existing-files nil
  1801. "Non-nil means `org-open-file' will open non-existing files.
  1802. When nil, an error will be generated.
  1803. This variable applies only to external applications because they
  1804. might choke on non-existing files. If the link is to a file that
  1805. will be opened in Emacs, the variable is ignored."
  1806. :group 'org-link-follow
  1807. :type 'boolean)
  1808. (defcustom org-open-directory-means-index-dot-org nil
  1809. "Non-nil means a link to a directory really means to index.org.
  1810. When nil, following a directory link will run dired or open a finder/explorer
  1811. window on that directory."
  1812. :group 'org-link-follow
  1813. :type 'boolean)
  1814. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1815. "Non-nil means ask for confirmation before executing shell links.
  1816. Shell links can be dangerous: just think about a link
  1817. [[shell:rm -rf ~/*][Google Search]]
  1818. This link would show up in your Org-mode document as \"Google Search\",
  1819. but really it would remove your entire home directory.
  1820. Therefore we advise against setting this variable to nil.
  1821. Just change it to `y-or-n-p' if you want to confirm with a
  1822. single keystroke rather than having to type \"yes\"."
  1823. :group 'org-link-follow
  1824. :type '(choice
  1825. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1826. (const :tag "with y-or-n (faster)" y-or-n-p)
  1827. (const :tag "no confirmation (dangerous)" nil)))
  1828. (put 'org-confirm-shell-link-function
  1829. 'safe-local-variable
  1830. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1831. (defcustom org-confirm-shell-link-not-regexp ""
  1832. "A regexp to skip confirmation for shell links."
  1833. :group 'org-link-follow
  1834. :version "24.1"
  1835. :type 'regexp)
  1836. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1837. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1838. Elisp links can be dangerous: just think about a link
  1839. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1840. This link would show up in your Org-mode document as \"Google Search\",
  1841. but really it would remove your entire home directory.
  1842. Therefore we advise against setting this variable to nil.
  1843. Just change it to `y-or-n-p' if you want to confirm with a
  1844. single keystroke rather than having to type \"yes\"."
  1845. :group 'org-link-follow
  1846. :type '(choice
  1847. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1848. (const :tag "with y-or-n (faster)" y-or-n-p)
  1849. (const :tag "no confirmation (dangerous)" nil)))
  1850. (put 'org-confirm-shell-link-function
  1851. 'safe-local-variable
  1852. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1853. (defcustom org-confirm-elisp-link-not-regexp ""
  1854. "A regexp to skip confirmation for Elisp links."
  1855. :group 'org-link-follow
  1856. :version "24.1"
  1857. :type 'regexp)
  1858. (defconst org-file-apps-defaults-gnu
  1859. '((remote . emacs)
  1860. (system . mailcap)
  1861. (t . mailcap))
  1862. "Default file applications on a UNIX or GNU/Linux system.
  1863. See `org-file-apps'.")
  1864. (defconst org-file-apps-defaults-macosx
  1865. '((remote . emacs)
  1866. (t . "open %s")
  1867. (system . "open %s")
  1868. ("ps.gz" . "gv %s")
  1869. ("eps.gz" . "gv %s")
  1870. ("dvi" . "xdvi %s")
  1871. ("fig" . "xfig %s"))
  1872. "Default file applications on a MacOS X system.
  1873. The system \"open\" is known as a default, but we use X11 applications
  1874. for some files for which the OS does not have a good default.
  1875. See `org-file-apps'.")
  1876. (defconst org-file-apps-defaults-windowsnt
  1877. (list
  1878. '(remote . emacs)
  1879. (cons t
  1880. (list (if (featurep 'xemacs)
  1881. 'mswindows-shell-execute
  1882. 'w32-shell-execute)
  1883. "open" 'file))
  1884. (cons 'system
  1885. (list (if (featurep 'xemacs)
  1886. 'mswindows-shell-execute
  1887. 'w32-shell-execute)
  1888. "open" 'file)))
  1889. "Default file applications on a Windows NT system.
  1890. The system \"open\" is used for most files.
  1891. See `org-file-apps'.")
  1892. (defcustom org-file-apps
  1893. '((auto-mode . emacs)
  1894. ("\\.mm\\'" . default)
  1895. ("\\.x?html?\\'" . default)
  1896. ("\\.pdf\\'" . default))
  1897. "External applications for opening `file:path' items in a document.
  1898. Org-mode uses system defaults for different file types, but
  1899. you can use this variable to set the application for a given file
  1900. extension. The entries in this list are cons cells where the car identifies
  1901. files and the cdr the corresponding command. Possible values for the
  1902. file identifier are
  1903. \"string\" A string as a file identifier can be interpreted in different
  1904. ways, depending on its contents:
  1905. - Alphanumeric characters only:
  1906. Match links with this file extension.
  1907. Example: (\"pdf\" . \"evince %s\")
  1908. to open PDFs with evince.
  1909. - Regular expression: Match links where the
  1910. filename matches the regexp. If you want to
  1911. use groups here, use shy groups.
  1912. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1913. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1914. to open *.html and *.xhtml with firefox.
  1915. - Regular expression which contains (non-shy) groups:
  1916. Match links where the whole link, including \"::\", and
  1917. anything after that, matches the regexp.
  1918. In a custom command string, %1, %2, etc. are replaced with
  1919. the parts of the link that were matched by the groups.
  1920. For backwards compatibility, if a command string is given
  1921. that does not use any of the group matches, this case is
  1922. handled identically to the second one (i.e. match against
  1923. file name only).
  1924. In a custom lisp form, you can access the group matches with
  1925. (match-string n link).
  1926. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1927. to open [[file:document.pdf::5]] with evince at page 5.
  1928. `directory' Matches a directory
  1929. `remote' Matches a remote file, accessible through tramp or efs.
  1930. Remote files most likely should be visited through Emacs
  1931. because external applications cannot handle such paths.
  1932. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1933. so all files Emacs knows how to handle. Using this with
  1934. command `emacs' will open most files in Emacs. Beware that this
  1935. will also open html files inside Emacs, unless you add
  1936. (\"html\" . default) to the list as well.
  1937. t Default for files not matched by any of the other options.
  1938. `system' The system command to open files, like `open' on Windows
  1939. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1940. that will be selected if you call `C-c C-o' with a double
  1941. \\[universal-argument] \\[universal-argument] prefix.
  1942. Possible values for the command are:
  1943. `emacs' The file will be visited by the current Emacs process.
  1944. `default' Use the default application for this file type, which is the
  1945. association for t in the list, most likely in the system-specific
  1946. part.
  1947. This can be used to overrule an unwanted setting in the
  1948. system-specific variable.
  1949. `system' Use the system command for opening files, like \"open\".
  1950. This command is specified by the entry whose car is `system'.
  1951. Most likely, the system-specific version of this variable
  1952. does define this command, but you can overrule/replace it
  1953. here.
  1954. string A command to be executed by a shell; %s will be replaced
  1955. by the path to the file.
  1956. sexp A Lisp form which will be evaluated. The file path will
  1957. be available in the Lisp variable `file'.
  1958. For more examples, see the system specific constants
  1959. `org-file-apps-defaults-macosx'
  1960. `org-file-apps-defaults-windowsnt'
  1961. `org-file-apps-defaults-gnu'."
  1962. :group 'org-link-follow
  1963. :type '(repeat
  1964. (cons (choice :value ""
  1965. (string :tag "Extension")
  1966. (const :tag "System command to open files" system)
  1967. (const :tag "Default for unrecognized files" t)
  1968. (const :tag "Remote file" remote)
  1969. (const :tag "Links to a directory" directory)
  1970. (const :tag "Any files that have Emacs modes"
  1971. auto-mode))
  1972. (choice :value ""
  1973. (const :tag "Visit with Emacs" emacs)
  1974. (const :tag "Use default" default)
  1975. (const :tag "Use the system command" system)
  1976. (string :tag "Command")
  1977. (sexp :tag "Lisp form")))))
  1978. (defcustom org-doi-server-url "http://dx.doi.org/"
  1979. "The URL of the DOI server."
  1980. :type 'string
  1981. :version "24.3"
  1982. :group 'org-link-follow)
  1983. (defgroup org-refile nil
  1984. "Options concerning refiling entries in Org-mode."
  1985. :tag "Org Refile"
  1986. :group 'org)
  1987. (defcustom org-directory "~/org"
  1988. "Directory with org files.
  1989. This is just a default location to look for Org files. There is no need
  1990. at all to put your files into this directory. It is only used in the
  1991. following situations:
  1992. 1. When a capture template specifies a target file that is not an
  1993. absolute path. The path will then be interpreted relative to
  1994. `org-directory'
  1995. 2. When a capture note is filed away in an interactive way (when exiting the
  1996. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1997. with `org-directory' as the default path."
  1998. :group 'org-refile
  1999. :group 'org-capture
  2000. :type 'directory)
  2001. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2002. "Default target for storing notes.
  2003. Used as a fall back file for org-capture.el, for templates that
  2004. do not specify a target file."
  2005. :group 'org-refile
  2006. :group 'org-capture
  2007. :type '(choice
  2008. (const :tag "Default from remember-data-file" nil)
  2009. file))
  2010. (defcustom org-goto-interface 'outline
  2011. "The default interface to be used for `org-goto'.
  2012. Allowed values are:
  2013. outline The interface shows an outline of the relevant file
  2014. and the correct heading is found by moving through
  2015. the outline or by searching with incremental search.
  2016. outline-path-completion Headlines in the current buffer are offered via
  2017. completion. This is the interface also used by
  2018. the refile command."
  2019. :group 'org-refile
  2020. :type '(choice
  2021. (const :tag "Outline" outline)
  2022. (const :tag "Outline-path-completion" outline-path-completion)))
  2023. (defcustom org-goto-max-level 5
  2024. "Maximum target level when running `org-goto' with refile interface."
  2025. :group 'org-refile
  2026. :type 'integer)
  2027. (defcustom org-reverse-note-order nil
  2028. "Non-nil means store new notes at the beginning of a file or entry.
  2029. When nil, new notes will be filed to the end of a file or entry.
  2030. This can also be a list with cons cells of regular expressions that
  2031. are matched against file names, and values."
  2032. :group 'org-capture
  2033. :group 'org-refile
  2034. :type '(choice
  2035. (const :tag "Reverse always" t)
  2036. (const :tag "Reverse never" nil)
  2037. (repeat :tag "By file name regexp"
  2038. (cons regexp boolean))))
  2039. (defcustom org-log-refile nil
  2040. "Information to record when a task is refiled.
  2041. Possible values are:
  2042. nil Don't add anything
  2043. time Add a time stamp to the task
  2044. note Prompt for a note and add it with template `org-log-note-headings'
  2045. This option can also be set with on a per-file-basis with
  2046. #+STARTUP: nologrefile
  2047. #+STARTUP: logrefile
  2048. #+STARTUP: lognoterefile
  2049. You can have local logging settings for a subtree by setting the LOGGING
  2050. property to one or more of these keywords.
  2051. When bulk-refiling from the agenda, the value `note' is forbidden and
  2052. will temporarily be changed to `time'."
  2053. :group 'org-refile
  2054. :group 'org-progress
  2055. :version "24.1"
  2056. :type '(choice
  2057. (const :tag "No logging" nil)
  2058. (const :tag "Record timestamp" time)
  2059. (const :tag "Record timestamp with note." note)))
  2060. (defcustom org-refile-targets nil
  2061. "Targets for refiling entries with \\[org-refile].
  2062. This is a list of cons cells. Each cell contains:
  2063. - a specification of the files to be considered, either a list of files,
  2064. or a symbol whose function or variable value will be used to retrieve
  2065. a file name or a list of file names. If you use `org-agenda-files' for
  2066. that, all agenda files will be scanned for targets. Nil means consider
  2067. headings in the current buffer.
  2068. - A specification of how to find candidate refile targets. This may be
  2069. any of:
  2070. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2071. This tag has to be present in all target headlines, inheritance will
  2072. not be considered.
  2073. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2074. todo keyword.
  2075. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2076. headlines that are refiling targets.
  2077. - a cons cell (:level . N). Any headline of level N is considered a target.
  2078. Note that, when `org-odd-levels-only' is set, level corresponds to
  2079. order in hierarchy, not to the number of stars.
  2080. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2081. Note that, when `org-odd-levels-only' is set, level corresponds to
  2082. order in hierarchy, not to the number of stars.
  2083. Each element of this list generates a set of possible targets.
  2084. The union of these sets is presented (with completion) to
  2085. the user by `org-refile'.
  2086. You can set the variable `org-refile-target-verify-function' to a function
  2087. to verify each headline found by the simple criteria above.
  2088. When this variable is nil, all top-level headlines in the current buffer
  2089. are used, equivalent to the value `((nil . (:level . 1))'."
  2090. :group 'org-refile
  2091. :type '(repeat
  2092. (cons
  2093. (choice :value org-agenda-files
  2094. (const :tag "All agenda files" org-agenda-files)
  2095. (const :tag "Current buffer" nil)
  2096. (function) (variable) (file))
  2097. (choice :tag "Identify target headline by"
  2098. (cons :tag "Specific tag" (const :value :tag) (string))
  2099. (cons :tag "TODO keyword" (const :value :todo) (string))
  2100. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2101. (cons :tag "Level number" (const :value :level) (integer))
  2102. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2103. (defcustom org-refile-target-verify-function nil
  2104. "Function to verify if the headline at point should be a refile target.
  2105. The function will be called without arguments, with point at the
  2106. beginning of the headline. It should return t and leave point
  2107. where it is if the headline is a valid target for refiling.
  2108. If the target should not be selected, the function must return nil.
  2109. In addition to this, it may move point to a place from where the search
  2110. should be continued. For example, the function may decide that the entire
  2111. subtree of the current entry should be excluded and move point to the end
  2112. of the subtree."
  2113. :group 'org-refile
  2114. :type '(choice
  2115. (const nil)
  2116. (function)))
  2117. (defcustom org-refile-use-cache nil
  2118. "Non-nil means cache refile targets to speed up the process.
  2119. The cache for a particular file will be updated automatically when
  2120. the buffer has been killed, or when any of the marker used for flagging
  2121. refile targets no longer points at a live buffer.
  2122. If you have added new entries to a buffer that might themselves be targets,
  2123. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2124. find that easier, `C-u C-u C-u C-c C-w'."
  2125. :group 'org-refile
  2126. :version "24.1"
  2127. :type 'boolean)
  2128. (defcustom org-refile-use-outline-path nil
  2129. "Non-nil means provide refile targets as paths.
  2130. So a level 3 headline will be available as level1/level2/level3.
  2131. When the value is `file', also include the file name (without directory)
  2132. into the path. In this case, you can also stop the completion after
  2133. the file name, to get entries inserted as top level in the file.
  2134. When `full-file-path', include the full file path."
  2135. :group 'org-refile
  2136. :type '(choice
  2137. (const :tag "Not" nil)
  2138. (const :tag "Yes" t)
  2139. (const :tag "Start with file name" file)
  2140. (const :tag "Start with full file path" full-file-path)))
  2141. (defcustom org-outline-path-complete-in-steps t
  2142. "Non-nil means complete the outline path in hierarchical steps.
  2143. When Org-mode uses the refile interface to select an outline path
  2144. \(see variable `org-refile-use-outline-path'), the completion of
  2145. the path can be done is a single go, or if can be done in steps down
  2146. the headline hierarchy. Going in steps is probably the best if you
  2147. do not use a special completion package like `ido' or `icicles'.
  2148. However, when using these packages, going in one step can be very
  2149. fast, while still showing the whole path to the entry."
  2150. :group 'org-refile
  2151. :type 'boolean)
  2152. (defcustom org-refile-allow-creating-parent-nodes nil
  2153. "Non-nil means allow to create new nodes as refile targets.
  2154. New nodes are then created by adding \"/new node name\" to the completion
  2155. of an existing node. When the value of this variable is `confirm',
  2156. new node creation must be confirmed by the user (recommended).
  2157. When nil, the completion must match an existing entry.
  2158. Note that, if the new heading is not seen by the criteria
  2159. listed in `org-refile-targets', multiple instances of the same
  2160. heading would be created by trying again to file under the new
  2161. heading."
  2162. :group 'org-refile
  2163. :type '(choice
  2164. (const :tag "Never" nil)
  2165. (const :tag "Always" t)
  2166. (const :tag "Prompt for confirmation" confirm)))
  2167. (defcustom org-refile-active-region-within-subtree nil
  2168. "Non-nil means also refile active region within a subtree.
  2169. By default `org-refile' doesn't allow refiling regions if they
  2170. don't contain a set of subtrees, but it might be convenient to
  2171. do so sometimes: in that case, the first line of the region is
  2172. converted to a headline before refiling."
  2173. :group 'org-refile
  2174. :version "24.1"
  2175. :type 'boolean)
  2176. (defgroup org-todo nil
  2177. "Options concerning TODO items in Org-mode."
  2178. :tag "Org TODO"
  2179. :group 'org)
  2180. (defgroup org-progress nil
  2181. "Options concerning Progress logging in Org-mode."
  2182. :tag "Org Progress"
  2183. :group 'org-time)
  2184. (defvar org-todo-interpretation-widgets
  2185. '((:tag "Sequence (cycling hits every state)" sequence)
  2186. (:tag "Type (cycling directly to DONE)" type))
  2187. "The available interpretation symbols for customizing `org-todo-keywords'.
  2188. Interested libraries should add to this list.")
  2189. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2190. "List of TODO entry keyword sequences and their interpretation.
  2191. \\<org-mode-map>This is a list of sequences.
  2192. Each sequence starts with a symbol, either `sequence' or `type',
  2193. indicating if the keywords should be interpreted as a sequence of
  2194. action steps, or as different types of TODO items. The first
  2195. keywords are states requiring action - these states will select a headline
  2196. for inclusion into the global TODO list Org-mode produces. If one of
  2197. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2198. signify that no further action is necessary. If \"|\" is not found,
  2199. the last keyword is treated as the only DONE state of the sequence.
  2200. The command \\[org-todo] cycles an entry through these states, and one
  2201. additional state where no keyword is present. For details about this
  2202. cycling, see the manual.
  2203. TODO keywords and interpretation can also be set on a per-file basis with
  2204. the special #+SEQ_TODO and #+TYP_TODO lines.
  2205. Each keyword can optionally specify a character for fast state selection
  2206. \(in combination with the variable `org-use-fast-todo-selection')
  2207. and specifiers for state change logging, using the same syntax that
  2208. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2209. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2210. indicates to record a time stamp each time this state is selected.
  2211. Each keyword may also specify if a timestamp or a note should be
  2212. recorded when entering or leaving the state, by adding additional
  2213. characters in the parenthesis after the keyword. This looks like this:
  2214. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2215. record only the time of the state change. With X and Y being either
  2216. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2217. Y when leaving the state if and only if the *target* state does not
  2218. define X. You may omit any of the fast-selection key or X or /Y,
  2219. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2220. For backward compatibility, this variable may also be just a list
  2221. of keywords. In this case the interpretation (sequence or type) will be
  2222. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2223. :group 'org-todo
  2224. :group 'org-keywords
  2225. :type '(choice
  2226. (repeat :tag "Old syntax, just keywords"
  2227. (string :tag "Keyword"))
  2228. (repeat :tag "New syntax"
  2229. (cons
  2230. (choice
  2231. :tag "Interpretation"
  2232. ;;Quick and dirty way to see
  2233. ;;`org-todo-interpretations'. This takes the
  2234. ;;place of item arguments
  2235. :convert-widget
  2236. (lambda (widget)
  2237. (widget-put widget
  2238. :args (mapcar
  2239. (lambda (x)
  2240. (widget-convert
  2241. (cons 'const x)))
  2242. org-todo-interpretation-widgets))
  2243. widget))
  2244. (repeat
  2245. (string :tag "Keyword"))))))
  2246. (defvar org-todo-keywords-1 nil
  2247. "All TODO and DONE keywords active in a buffer.")
  2248. (make-variable-buffer-local 'org-todo-keywords-1)
  2249. (defvar org-todo-keywords-for-agenda nil)
  2250. (defvar org-done-keywords-for-agenda nil)
  2251. (defvar org-todo-keyword-alist-for-agenda nil)
  2252. (defvar org-tag-alist-for-agenda nil
  2253. "Alist of all tags from all agenda files.")
  2254. (defvar org-tag-groups-alist-for-agenda nil
  2255. "Alist of all groups tags from all current agenda files.")
  2256. (defvar org-tag-groups-alist nil)
  2257. (make-variable-buffer-local 'org-tag-groups-alist)
  2258. (defvar org-agenda-contributing-files nil)
  2259. (defvar org-not-done-keywords nil)
  2260. (make-variable-buffer-local 'org-not-done-keywords)
  2261. (defvar org-done-keywords nil)
  2262. (make-variable-buffer-local 'org-done-keywords)
  2263. (defvar org-todo-heads nil)
  2264. (make-variable-buffer-local 'org-todo-heads)
  2265. (defvar org-todo-sets nil)
  2266. (make-variable-buffer-local 'org-todo-sets)
  2267. (defvar org-todo-log-states nil)
  2268. (make-variable-buffer-local 'org-todo-log-states)
  2269. (defvar org-todo-kwd-alist nil)
  2270. (make-variable-buffer-local 'org-todo-kwd-alist)
  2271. (defvar org-todo-key-alist nil)
  2272. (make-variable-buffer-local 'org-todo-key-alist)
  2273. (defvar org-todo-key-trigger nil)
  2274. (make-variable-buffer-local 'org-todo-key-trigger)
  2275. (defcustom org-todo-interpretation 'sequence
  2276. "Controls how TODO keywords are interpreted.
  2277. This variable is in principle obsolete and is only used for
  2278. backward compatibility, if the interpretation of todo keywords is
  2279. not given already in `org-todo-keywords'. See that variable for
  2280. more information."
  2281. :group 'org-todo
  2282. :group 'org-keywords
  2283. :type '(choice (const sequence)
  2284. (const type)))
  2285. (defcustom org-use-fast-todo-selection t
  2286. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2287. This variable describes if and under what circumstances the cycling
  2288. mechanism for TODO keywords will be replaced by a single-key, direct
  2289. selection scheme.
  2290. When nil, fast selection is never used.
  2291. When the symbol `prefix', it will be used when `org-todo' is called
  2292. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2293. `C-u t' in an agenda buffer.
  2294. When t, fast selection is used by default. In this case, the prefix
  2295. argument forces cycling instead.
  2296. In all cases, the special interface is only used if access keys have
  2297. actually been assigned by the user, i.e. if keywords in the configuration
  2298. are followed by a letter in parenthesis, like TODO(t)."
  2299. :group 'org-todo
  2300. :type '(choice
  2301. (const :tag "Never" nil)
  2302. (const :tag "By default" t)
  2303. (const :tag "Only with C-u C-c C-t" prefix)))
  2304. (defcustom org-provide-todo-statistics t
  2305. "Non-nil means update todo statistics after insert and toggle.
  2306. ALL-HEADLINES means update todo statistics by including headlines
  2307. with no TODO keyword as well, counting them as not done.
  2308. A list of TODO keywords means the same, but skip keywords that are
  2309. not in this list.
  2310. When set to a list of two lists, the first list contains keywords
  2311. to consider as TODO keywords, the second list contains keywords
  2312. to consider as DONE keywords.
  2313. When this is set, todo statistics is updated in the parent of the
  2314. current entry each time a todo state is changed."
  2315. :group 'org-todo
  2316. :type '(choice
  2317. (const :tag "Yes, only for TODO entries" t)
  2318. (const :tag "Yes, including all entries" all-headlines)
  2319. (repeat :tag "Yes, for TODOs in this list"
  2320. (string :tag "TODO keyword"))
  2321. (list :tag "Yes, for TODOs and DONEs in these lists"
  2322. (repeat (string :tag "TODO keyword"))
  2323. (repeat (string :tag "DONE keyword")))
  2324. (other :tag "No TODO statistics" nil)))
  2325. (defcustom org-hierarchical-todo-statistics t
  2326. "Non-nil means TODO statistics covers just direct children.
  2327. When nil, all entries in the subtree are considered.
  2328. This has only an effect if `org-provide-todo-statistics' is set.
  2329. To set this to nil for only a single subtree, use a COOKIE_DATA
  2330. property and include the word \"recursive\" into the value."
  2331. :group 'org-todo
  2332. :type 'boolean)
  2333. (defcustom org-after-todo-state-change-hook nil
  2334. "Hook which is run after the state of a TODO item was changed.
  2335. The new state (a string with a TODO keyword, or nil) is available in the
  2336. Lisp variable `org-state'."
  2337. :group 'org-todo
  2338. :type 'hook)
  2339. (defvar org-blocker-hook nil
  2340. "Hook for functions that are allowed to block a state change.
  2341. Functions in this hook should not modify the buffer.
  2342. Each function gets as its single argument a property list,
  2343. see `org-trigger-hook' for more information about this list.
  2344. If any of the functions in this hook returns nil, the state change
  2345. is blocked.")
  2346. (defvar org-trigger-hook nil
  2347. "Hook for functions that are triggered by a state change.
  2348. Each function gets as its single argument a property list with at
  2349. least the following elements:
  2350. (:type type-of-change :position pos-at-entry-start
  2351. :from old-state :to new-state)
  2352. Depending on the type, more properties may be present.
  2353. This mechanism is currently implemented for:
  2354. TODO state changes
  2355. ------------------
  2356. :type todo-state-change
  2357. :from previous state (keyword as a string), or nil, or a symbol
  2358. 'todo' or 'done', to indicate the general type of state.
  2359. :to new state, like in :from")
  2360. (defcustom org-enforce-todo-dependencies nil
  2361. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2362. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2363. be blocked if any prior sibling is not yet done.
  2364. Finally, if the parent is blocked because of ordered siblings of its own,
  2365. the child will also be blocked."
  2366. :set (lambda (var val)
  2367. (set var val)
  2368. (if val
  2369. (add-hook 'org-blocker-hook
  2370. 'org-block-todo-from-children-or-siblings-or-parent)
  2371. (remove-hook 'org-blocker-hook
  2372. 'org-block-todo-from-children-or-siblings-or-parent)))
  2373. :group 'org-todo
  2374. :type 'boolean)
  2375. (defcustom org-enforce-todo-checkbox-dependencies nil
  2376. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2377. When this is nil, checkboxes have no influence on switching TODO states.
  2378. When non-nil, you first need to check off all check boxes before the TODO
  2379. entry can be switched to DONE.
  2380. This variable needs to be set before org.el is loaded, and you need to
  2381. restart Emacs after a change to make the change effective. The only way
  2382. to change is while Emacs is running is through the customize interface."
  2383. :set (lambda (var val)
  2384. (set var val)
  2385. (if val
  2386. (add-hook 'org-blocker-hook
  2387. 'org-block-todo-from-checkboxes)
  2388. (remove-hook 'org-blocker-hook
  2389. 'org-block-todo-from-checkboxes)))
  2390. :group 'org-todo
  2391. :type 'boolean)
  2392. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2393. "Non-nil means inserting a TODO heading is treated as state change.
  2394. So when the command \\[org-insert-todo-heading] is used, state change
  2395. logging will apply if appropriate. When nil, the new TODO item will
  2396. be inserted directly, and no logging will take place."
  2397. :group 'org-todo
  2398. :type 'boolean)
  2399. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2400. "Non-nil means switching TODO states with S-cursor counts as state change.
  2401. This is the default behavior. However, setting this to nil allows a
  2402. convenient way to select a TODO state and bypass any logging associated
  2403. with that."
  2404. :group 'org-todo
  2405. :type 'boolean)
  2406. (defcustom org-todo-state-tags-triggers nil
  2407. "Tag changes that should be triggered by TODO state changes.
  2408. This is a list. Each entry is
  2409. (state-change (tag . flag) .......)
  2410. State-change can be a string with a state, and empty string to indicate the
  2411. state that has no TODO keyword, or it can be one of the symbols `todo'
  2412. or `done', meaning any not-done or done state, respectively."
  2413. :group 'org-todo
  2414. :group 'org-tags
  2415. :type '(repeat
  2416. (cons (choice :tag "When changing to"
  2417. (const :tag "Not-done state" todo)
  2418. (const :tag "Done state" done)
  2419. (string :tag "State"))
  2420. (repeat
  2421. (cons :tag "Tag action"
  2422. (string :tag "Tag")
  2423. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2424. (defcustom org-log-done nil
  2425. "Information to record when a task moves to the DONE state.
  2426. Possible values are:
  2427. nil Don't add anything, just change the keyword
  2428. time Add a time stamp to the task
  2429. note Prompt for a note and add it with template `org-log-note-headings'
  2430. This option can also be set with on a per-file-basis with
  2431. #+STARTUP: nologdone
  2432. #+STARTUP: logdone
  2433. #+STARTUP: lognotedone
  2434. You can have local logging settings for a subtree by setting the LOGGING
  2435. property to one or more of these keywords."
  2436. :group 'org-todo
  2437. :group 'org-progress
  2438. :type '(choice
  2439. (const :tag "No logging" nil)
  2440. (const :tag "Record CLOSED timestamp" time)
  2441. (const :tag "Record CLOSED timestamp with note." note)))
  2442. ;; Normalize old uses of org-log-done.
  2443. (cond
  2444. ((eq org-log-done t) (setq org-log-done 'time))
  2445. ((and (listp org-log-done) (memq 'done org-log-done))
  2446. (setq org-log-done 'note)))
  2447. (defcustom org-log-reschedule nil
  2448. "Information to record when the scheduling date of a tasks is modified.
  2449. Possible values are:
  2450. nil Don't add anything, just change the date
  2451. time Add a time stamp to the task
  2452. note Prompt for a note and add it with template `org-log-note-headings'
  2453. This option can also be set with on a per-file-basis with
  2454. #+STARTUP: nologreschedule
  2455. #+STARTUP: logreschedule
  2456. #+STARTUP: lognotereschedule"
  2457. :group 'org-todo
  2458. :group 'org-progress
  2459. :type '(choice
  2460. (const :tag "No logging" nil)
  2461. (const :tag "Record timestamp" time)
  2462. (const :tag "Record timestamp with note." note)))
  2463. (defcustom org-log-redeadline nil
  2464. "Information to record when the deadline date of a tasks is modified.
  2465. Possible values are:
  2466. nil Don't add anything, just change the date
  2467. time Add a time stamp to the task
  2468. note Prompt for a note and add it with template `org-log-note-headings'
  2469. This option can also be set with on a per-file-basis with
  2470. #+STARTUP: nologredeadline
  2471. #+STARTUP: logredeadline
  2472. #+STARTUP: lognoteredeadline
  2473. You can have local logging settings for a subtree by setting the LOGGING
  2474. property to one or more of these keywords."
  2475. :group 'org-todo
  2476. :group 'org-progress
  2477. :type '(choice
  2478. (const :tag "No logging" nil)
  2479. (const :tag "Record timestamp" time)
  2480. (const :tag "Record timestamp with note." note)))
  2481. (defcustom org-log-note-clock-out nil
  2482. "Non-nil means record a note when clocking out of an item.
  2483. This can also be configured on a per-file basis by adding one of
  2484. the following lines anywhere in the buffer:
  2485. #+STARTUP: lognoteclock-out
  2486. #+STARTUP: nolognoteclock-out"
  2487. :group 'org-todo
  2488. :group 'org-progress
  2489. :type 'boolean)
  2490. (defcustom org-log-done-with-time t
  2491. "Non-nil means the CLOSED time stamp will contain date and time.
  2492. When nil, only the date will be recorded."
  2493. :group 'org-progress
  2494. :type 'boolean)
  2495. (defcustom org-log-note-headings
  2496. '((done . "CLOSING NOTE %t")
  2497. (state . "State %-12s from %-12S %t")
  2498. (note . "Note taken on %t")
  2499. (reschedule . "Rescheduled from %S on %t")
  2500. (delschedule . "Not scheduled, was %S on %t")
  2501. (redeadline . "New deadline from %S on %t")
  2502. (deldeadline . "Removed deadline, was %S on %t")
  2503. (refile . "Refiled on %t")
  2504. (clock-out . ""))
  2505. "Headings for notes added to entries.
  2506. The value is an alist, with the car being a symbol indicating the note
  2507. context, and the cdr is the heading to be used. The heading may also be the
  2508. empty string.
  2509. %t in the heading will be replaced by a time stamp.
  2510. %T will be an active time stamp instead the default inactive one
  2511. %d will be replaced by a short-format time stamp.
  2512. %D will be replaced by an active short-format time stamp.
  2513. %s will be replaced by the new TODO state, in double quotes.
  2514. %S will be replaced by the old TODO state, in double quotes.
  2515. %u will be replaced by the user name.
  2516. %U will be replaced by the full user name.
  2517. In fact, it is not a good idea to change the `state' entry, because
  2518. agenda log mode depends on the format of these entries."
  2519. :group 'org-todo
  2520. :group 'org-progress
  2521. :type '(list :greedy t
  2522. (cons (const :tag "Heading when closing an item" done) string)
  2523. (cons (const :tag
  2524. "Heading when changing todo state (todo sequence only)"
  2525. state) string)
  2526. (cons (const :tag "Heading when just taking a note" note) string)
  2527. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2528. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2529. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2530. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2531. (cons (const :tag "Heading when refiling" refile) string)
  2532. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2533. (unless (assq 'note org-log-note-headings)
  2534. (push '(note . "%t") org-log-note-headings))
  2535. (defcustom org-log-into-drawer nil
  2536. "Non-nil means insert state change notes and time stamps into a drawer.
  2537. When nil, state changes notes will be inserted after the headline and
  2538. any scheduling and clock lines, but not inside a drawer.
  2539. The value of this variable should be the name of the drawer to use.
  2540. LOGBOOK is proposed as the default drawer for this purpose, you can
  2541. also set this to a string to define the drawer of your choice.
  2542. A value of t is also allowed, representing \"LOGBOOK\".
  2543. A value of t or nil can also be set with on a per-file-basis with
  2544. #+STARTUP: logdrawer
  2545. #+STARTUP: nologdrawer
  2546. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2547. will be ignored.
  2548. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2549. a subtree.
  2550. Do not check directly this variable in a Lisp program. Call
  2551. function `org-log-into-drawer' instead."
  2552. :group 'org-todo
  2553. :group 'org-progress
  2554. :type '(choice
  2555. (const :tag "Not into a drawer" nil)
  2556. (const :tag "LOGBOOK" t)
  2557. (string :tag "Other")))
  2558. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2559. (defun org-log-into-drawer ()
  2560. "Name of the log drawer, as a string, or nil.
  2561. This is the value of `org-log-into-drawer'. However, if the
  2562. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2563. be used instead of the default value."
  2564. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2565. (cond ((equal p "nil") nil)
  2566. ((equal p "t") "LOGBOOK")
  2567. ((stringp p) p)
  2568. (p "LOGBOOK")
  2569. ((stringp org-log-into-drawer) org-log-into-drawer)
  2570. (org-log-into-drawer "LOGBOOK"))))
  2571. (defcustom org-log-state-notes-insert-after-drawers nil
  2572. "Non-nil means insert state change notes after any drawers in entry.
  2573. Only the drawers that *immediately* follow the headline and the
  2574. deadline/scheduled line are skipped.
  2575. When nil, insert notes right after the heading and perhaps the line
  2576. with deadline/scheduling if present.
  2577. This variable will have no effect if `org-log-into-drawer' is
  2578. set."
  2579. :group 'org-todo
  2580. :group 'org-progress
  2581. :type 'boolean)
  2582. (defcustom org-log-states-order-reversed t
  2583. "Non-nil means the latest state note will be directly after heading.
  2584. When nil, the state change notes will be ordered according to time.
  2585. This option can also be set with on a per-file-basis with
  2586. #+STARTUP: logstatesreversed
  2587. #+STARTUP: nologstatesreversed"
  2588. :group 'org-todo
  2589. :group 'org-progress
  2590. :type 'boolean)
  2591. (defcustom org-todo-repeat-to-state nil
  2592. "The TODO state to which a repeater should return the repeating task.
  2593. By default this is the first task in a TODO sequence, or the previous state
  2594. in a TODO_TYP set. But you can specify another task here.
  2595. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2596. :group 'org-todo
  2597. :version "24.1"
  2598. :type '(choice (const :tag "Head of sequence" nil)
  2599. (string :tag "Specific state")))
  2600. (defcustom org-log-repeat 'time
  2601. "Non-nil means record moving through the DONE state when triggering repeat.
  2602. An auto-repeating task is immediately switched back to TODO when
  2603. marked DONE. If you are not logging state changes (by adding \"@\"
  2604. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2605. record a closing note, there will be no record of the task moving
  2606. through DONE. This variable forces taking a note anyway.
  2607. nil Don't force a record
  2608. time Record a time stamp
  2609. note Prompt for a note and add it with template `org-log-note-headings'
  2610. This option can also be set with on a per-file-basis with
  2611. #+STARTUP: nologrepeat
  2612. #+STARTUP: logrepeat
  2613. #+STARTUP: lognoterepeat
  2614. You can have local logging settings for a subtree by setting the LOGGING
  2615. property to one or more of these keywords."
  2616. :group 'org-todo
  2617. :group 'org-progress
  2618. :type '(choice
  2619. (const :tag "Don't force a record" nil)
  2620. (const :tag "Force recording the DONE state" time)
  2621. (const :tag "Force recording a note with the DONE state" note)))
  2622. (defgroup org-priorities nil
  2623. "Priorities in Org-mode."
  2624. :tag "Org Priorities"
  2625. :group 'org-todo)
  2626. (defcustom org-enable-priority-commands t
  2627. "Non-nil means priority commands are active.
  2628. When nil, these commands will be disabled, so that you never accidentally
  2629. set a priority."
  2630. :group 'org-priorities
  2631. :type 'boolean)
  2632. (defcustom org-highest-priority ?A
  2633. "The highest priority of TODO items. A character like ?A, ?B etc.
  2634. Must have a smaller ASCII number than `org-lowest-priority'."
  2635. :group 'org-priorities
  2636. :type 'character)
  2637. (defcustom org-lowest-priority ?C
  2638. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2639. Must have a larger ASCII number than `org-highest-priority'."
  2640. :group 'org-priorities
  2641. :type 'character)
  2642. (defcustom org-default-priority ?B
  2643. "The default priority of TODO items.
  2644. This is the priority an item gets if no explicit priority is given.
  2645. When starting to cycle on an empty priority the first step in the cycle
  2646. depends on `org-priority-start-cycle-with-default'. The resulting first
  2647. step priority must not exceed the range from `org-highest-priority' to
  2648. `org-lowest-priority' which means that `org-default-priority' has to be
  2649. in this range exclusive or inclusive the range boundaries. Else the
  2650. first step refuses to set the default and the second will fall back
  2651. to (depending on the command used) the highest or lowest priority."
  2652. :group 'org-priorities
  2653. :type 'character)
  2654. (defcustom org-priority-start-cycle-with-default t
  2655. "Non-nil means start with default priority when starting to cycle.
  2656. When this is nil, the first step in the cycle will be (depending on the
  2657. command used) one higher or lower than the default priority.
  2658. See also `org-default-priority'."
  2659. :group 'org-priorities
  2660. :type 'boolean)
  2661. (defcustom org-get-priority-function nil
  2662. "Function to extract the priority from a string.
  2663. The string is normally the headline. If this is nil Org computes the
  2664. priority from the priority cookie like [#A] in the headline. It returns
  2665. an integer, increasing by 1000 for each priority level.
  2666. The user can set a different function here, which should take a string
  2667. as an argument and return the numeric priority."
  2668. :group 'org-priorities
  2669. :version "24.1"
  2670. :type '(choice
  2671. (const nil)
  2672. (function)))
  2673. (defgroup org-time nil
  2674. "Options concerning time stamps and deadlines in Org-mode."
  2675. :tag "Org Time"
  2676. :group 'org)
  2677. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2678. "Number of minutes to round time stamps to.
  2679. These are two values, the first applies when first creating a time stamp.
  2680. The second applies when changing it with the commands `S-up' and `S-down'.
  2681. When changing the time stamp, this means that it will change in steps
  2682. of N minutes, as given by the second value.
  2683. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2684. numbers should be factors of 60, so for example 5, 10, 15.
  2685. When this is larger than 1, you can still force an exact time stamp by using
  2686. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2687. and by using a prefix arg to `S-up/down' to specify the exact number
  2688. of minutes to shift."
  2689. :group 'org-time
  2690. :get (lambda (var) ; Make sure both elements are there
  2691. (if (integerp (default-value var))
  2692. (list (default-value var) 5)
  2693. (default-value var)))
  2694. :type '(list
  2695. (integer :tag "when inserting times")
  2696. (integer :tag "when modifying times")))
  2697. ;; Normalize old customizations of this variable.
  2698. (when (integerp org-time-stamp-rounding-minutes)
  2699. (setq org-time-stamp-rounding-minutes
  2700. (list org-time-stamp-rounding-minutes
  2701. org-time-stamp-rounding-minutes)))
  2702. (defcustom org-display-custom-times nil
  2703. "Non-nil means overlay custom formats over all time stamps.
  2704. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2705. To turn this on on a per-file basis, insert anywhere in the file:
  2706. #+STARTUP: customtime"
  2707. :group 'org-time
  2708. :set 'set-default
  2709. :type 'sexp)
  2710. (make-variable-buffer-local 'org-display-custom-times)
  2711. (defcustom org-time-stamp-custom-formats
  2712. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2713. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2714. These are overlaid over the default ISO format if the variable
  2715. `org-display-custom-times' is set. Time like %H:%M should be at the
  2716. end of the second format. The custom formats are also honored by export
  2717. commands, if custom time display is turned on at the time of export."
  2718. :group 'org-time
  2719. :type 'sexp)
  2720. (defun org-time-stamp-format (&optional long inactive)
  2721. "Get the right format for a time string."
  2722. (let ((f (if long (cdr org-time-stamp-formats)
  2723. (car org-time-stamp-formats))))
  2724. (if inactive
  2725. (concat "[" (substring f 1 -1) "]")
  2726. f)))
  2727. (defcustom org-time-clocksum-format
  2728. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2729. "The format string used when creating CLOCKSUM lines.
  2730. This is also used when Org mode generates a time duration.
  2731. The value can be a single format string containing two
  2732. %-sequences, which will be filled with the number of hours and
  2733. minutes in that order.
  2734. Alternatively, the value can be a plist associating any of the
  2735. keys :years, :months, :weeks, :days, :hours or :minutes with
  2736. format strings. The time duration is formatted using only the
  2737. time components that are needed and concatenating the results.
  2738. If a time unit in absent, it falls back to the next smallest
  2739. unit.
  2740. The keys :require-years, :require-months, :require-days,
  2741. :require-weeks, :require-hours, :require-minutes are also
  2742. meaningful. A non-nil value for these keys indicates that the
  2743. corresponding time component should always be included, even if
  2744. its value is 0.
  2745. For example,
  2746. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2747. :require-minutes t)
  2748. means durations longer than a day will be expressed in days,
  2749. hours and minutes, and durations less than a day will always be
  2750. expressed in hours and minutes (even for durations less than an
  2751. hour).
  2752. The value
  2753. \(:days \"%dd\" :minutes \"%dm\")
  2754. means durations longer than a day will be expressed in days and
  2755. minutes, and durations less than a day will be expressed entirely
  2756. in minutes (even for durations longer than an hour)."
  2757. :group 'org-time
  2758. :group 'org-clock
  2759. :version "24.4"
  2760. :package-version '(Org . "8.0")
  2761. :type '(choice (string :tag "Format string")
  2762. (set :tag "Plist"
  2763. (group :inline t (const :tag "Years" :years)
  2764. (string :tag "Format string"))
  2765. (group :inline t
  2766. (const :tag "Always show years" :require-years)
  2767. (const t))
  2768. (group :inline t (const :tag "Months" :months)
  2769. (string :tag "Format string"))
  2770. (group :inline t
  2771. (const :tag "Always show months" :require-months)
  2772. (const t))
  2773. (group :inline t (const :tag "Weeks" :weeks)
  2774. (string :tag "Format string"))
  2775. (group :inline t
  2776. (const :tag "Always show weeks" :require-weeks)
  2777. (const t))
  2778. (group :inline t (const :tag "Days" :days)
  2779. (string :tag "Format string"))
  2780. (group :inline t
  2781. (const :tag "Always show days" :require-days)
  2782. (const t))
  2783. (group :inline t (const :tag "Hours" :hours)
  2784. (string :tag "Format string"))
  2785. (group :inline t
  2786. (const :tag "Always show hours" :require-hours)
  2787. (const t))
  2788. (group :inline t (const :tag "Minutes" :minutes)
  2789. (string :tag "Format string"))
  2790. (group :inline t
  2791. (const :tag "Always show minutes" :require-minutes)
  2792. (const t)))))
  2793. (defcustom org-time-clocksum-use-fractional nil
  2794. "When non-nil, \\[org-clock-display] uses fractional times.
  2795. See `org-time-clocksum-format' for more on time clock formats."
  2796. :group 'org-time
  2797. :group 'org-clock
  2798. :version "24.3"
  2799. :type 'boolean)
  2800. (defcustom org-time-clocksum-use-effort-durations nil
  2801. "When non-nil, \\[org-clock-display] uses effort durations.
  2802. E.g. by default, one day is considered to be a 8 hours effort,
  2803. so a task that has been clocked for 16 hours will be displayed
  2804. as during 2 days in the clock display or in the clocktable.
  2805. See `org-effort-durations' on how to set effort durations
  2806. and `org-time-clocksum-format' for more on time clock formats."
  2807. :group 'org-time
  2808. :group 'org-clock
  2809. :version "24.4"
  2810. :package-version '(Org . "8.0")
  2811. :type 'boolean)
  2812. (defcustom org-time-clocksum-fractional-format "%.2f"
  2813. "The format string used when creating CLOCKSUM lines,
  2814. or when Org mode generates a time duration, if
  2815. `org-time-clocksum-use-fractional' is enabled.
  2816. The value can be a single format string containing one
  2817. %-sequence, which will be filled with the number of hours as
  2818. a float.
  2819. Alternatively, the value can be a plist associating any of the
  2820. keys :years, :months, :weeks, :days, :hours or :minutes with
  2821. a format string. The time duration is formatted using the
  2822. largest time unit which gives a non-zero integer part. If all
  2823. specified formats have zero integer part, the smallest time unit
  2824. is used."
  2825. :group 'org-time
  2826. :type '(choice (string :tag "Format string")
  2827. (set (group :inline t (const :tag "Years" :years)
  2828. (string :tag "Format string"))
  2829. (group :inline t (const :tag "Months" :months)
  2830. (string :tag "Format string"))
  2831. (group :inline t (const :tag "Weeks" :weeks)
  2832. (string :tag "Format string"))
  2833. (group :inline t (const :tag "Days" :days)
  2834. (string :tag "Format string"))
  2835. (group :inline t (const :tag "Hours" :hours)
  2836. (string :tag "Format string"))
  2837. (group :inline t (const :tag "Minutes" :minutes)
  2838. (string :tag "Format string")))))
  2839. (defcustom org-deadline-warning-days 14
  2840. "Number of days before expiration during which a deadline becomes active.
  2841. This variable governs the display in sparse trees and in the agenda.
  2842. When 0 or negative, it means use this number (the absolute value of it)
  2843. even if a deadline has a different individual lead time specified.
  2844. Custom commands can set this variable in the options section."
  2845. :group 'org-time
  2846. :group 'org-agenda-daily/weekly
  2847. :type 'integer)
  2848. (defcustom org-scheduled-delay-days 0
  2849. "Number of days before a scheduled item becomes active.
  2850. This variable governs the display in sparse trees and in the agenda.
  2851. The default value (i.e. 0) means: don't delay scheduled item.
  2852. When negative, it means use this number (the absolute value of it)
  2853. even if a scheduled item has a different individual delay time
  2854. specified.
  2855. Custom commands can set this variable in the options section."
  2856. :group 'org-time
  2857. :group 'org-agenda-daily/weekly
  2858. :version "24.4"
  2859. :package-version '(Org . "8.0")
  2860. :type 'integer)
  2861. (defcustom org-read-date-prefer-future t
  2862. "Non-nil means assume future for incomplete date input from user.
  2863. This affects the following situations:
  2864. 1. The user gives a month but not a year.
  2865. For example, if it is April and you enter \"feb 2\", this will be read
  2866. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2867. 2. The user gives a day, but no month.
  2868. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2869. read this as the third of *next* month. However, if you enter \"17\",
  2870. it will be considered as *this* month.
  2871. If you set this variable to the symbol `time', then also the following
  2872. will work:
  2873. 3. If the user gives a time.
  2874. If the time is before now, it will be interpreted as tomorrow.
  2875. Currently none of this works for ISO week specifications.
  2876. When this option is nil, the current day, month and year will always be
  2877. used as defaults.
  2878. See also `org-agenda-jump-prefer-future'."
  2879. :group 'org-time
  2880. :type '(choice
  2881. (const :tag "Never" nil)
  2882. (const :tag "Check month and day" t)
  2883. (const :tag "Check month, day, and time" time)))
  2884. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2885. "Should the agenda jump command prefer the future for incomplete dates?
  2886. The default is to do the same as configured in `org-read-date-prefer-future'.
  2887. But you can also set a deviating value here.
  2888. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2889. :group 'org-agenda
  2890. :group 'org-time
  2891. :version "24.1"
  2892. :type '(choice
  2893. (const :tag "Use org-read-date-prefer-future"
  2894. org-read-date-prefer-future)
  2895. (const :tag "Never" nil)
  2896. (const :tag "Always" t)))
  2897. (defcustom org-read-date-force-compatible-dates t
  2898. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2899. Depending on the system Emacs is running on, certain dates cannot
  2900. be represented with the type used internally to represent time.
  2901. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2902. correctly. Some systems allow for earlier dates, some for later,
  2903. some for both. One way to find out it to insert any date into an
  2904. Org buffer, putting the cursor on the year and hitting S-up and
  2905. S-down to test the range.
  2906. When this variable is set to t, the date/time prompt will not let
  2907. you specify dates outside the 1970-2037 range, so it is certain that
  2908. these dates will work in whatever version of Emacs you are
  2909. running, and also that you can move a file from one Emacs implementation
  2910. to another. WHenever Org is forcing the year for you, it will display
  2911. a message and beep.
  2912. When this variable is nil, Org will check if the date is
  2913. representable in the specific Emacs implementation you are using.
  2914. If not, it will force a year, usually the current year, and beep
  2915. to remind you. Currently this setting is not recommended because
  2916. the likelihood that you will open your Org files in an Emacs that
  2917. has limited date range is not negligible.
  2918. A workaround for this problem is to use diary sexp dates for time
  2919. stamps outside of this range."
  2920. :group 'org-time
  2921. :version "24.1"
  2922. :type 'boolean)
  2923. (defcustom org-read-date-display-live t
  2924. "Non-nil means display current interpretation of date prompt live.
  2925. This display will be in an overlay, in the minibuffer."
  2926. :group 'org-time
  2927. :type 'boolean)
  2928. (defcustom org-read-date-popup-calendar t
  2929. "Non-nil means pop up a calendar when prompting for a date.
  2930. In the calendar, the date can be selected with mouse-1. However, the
  2931. minibuffer will also be active, and you can simply enter the date as well.
  2932. When nil, only the minibuffer will be available."
  2933. :group 'org-time
  2934. :type 'boolean)
  2935. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2936. 'org-read-date-popup-calendar)
  2937. (make-obsolete-variable
  2938. 'org-read-date-minibuffer-setup-hook
  2939. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2940. (defcustom org-read-date-minibuffer-setup-hook nil
  2941. "Hook to be used to set up keys for the date/time interface.
  2942. Add key definitions to `minibuffer-local-map', which will be a
  2943. temporary copy.
  2944. WARNING: This option is obsolete, you should use
  2945. `org-read-date-minibuffer-local-map' to set up keys."
  2946. :group 'org-time
  2947. :type 'hook)
  2948. (defcustom org-extend-today-until 0
  2949. "The hour when your day really ends. Must be an integer.
  2950. This has influence for the following applications:
  2951. - When switching the agenda to \"today\". It it is still earlier than
  2952. the time given here, the day recognized as TODAY is actually yesterday.
  2953. - When a date is read from the user and it is still before the time given
  2954. here, the current date and time will be assumed to be yesterday, 23:59.
  2955. Also, timestamps inserted in capture templates follow this rule.
  2956. IMPORTANT: This is a feature whose implementation is and likely will
  2957. remain incomplete. Really, it is only here because past midnight seems to
  2958. be the favorite working time of John Wiegley :-)"
  2959. :group 'org-time
  2960. :type 'integer)
  2961. (defcustom org-use-effective-time nil
  2962. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2963. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2964. \"effective time\" of any timestamps between midnight and 8am will be
  2965. 23:59 of the previous day."
  2966. :group 'org-time
  2967. :version "24.1"
  2968. :type 'boolean)
  2969. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2970. "When non-nil, use the last clock out time for `org-todo'.
  2971. Note that this option has precedence over the combined use of
  2972. `org-use-effective-time' and `org-extend-today-until'."
  2973. :group 'org-time
  2974. :version "24.4"
  2975. :package-version '(Org . "8.0")
  2976. :type 'boolean)
  2977. (defcustom org-edit-timestamp-down-means-later nil
  2978. "Non-nil means S-down will increase the time in a time stamp.
  2979. When nil, S-up will increase."
  2980. :group 'org-time
  2981. :type 'boolean)
  2982. (defcustom org-calendar-follow-timestamp-change t
  2983. "Non-nil means make the calendar window follow timestamp changes.
  2984. When a timestamp is modified and the calendar window is visible, it will be
  2985. moved to the new date."
  2986. :group 'org-time
  2987. :type 'boolean)
  2988. (defgroup org-tags nil
  2989. "Options concerning tags in Org-mode."
  2990. :tag "Org Tags"
  2991. :group 'org)
  2992. (defcustom org-tag-alist nil
  2993. "List of tags allowed in Org-mode files.
  2994. When this list is nil, Org-mode will base TAG input on what is already in the
  2995. buffer.
  2996. The value of this variable is an alist, the car of each entry must be a
  2997. keyword as a string, the cdr may be a character that is used to select
  2998. that tag through the fast-tag-selection interface.
  2999. See the manual for details."
  3000. :group 'org-tags
  3001. :type '(repeat
  3002. (choice
  3003. (cons (string :tag "Tag name")
  3004. (character :tag "Access char"))
  3005. (list :tag "Start radio group"
  3006. (const :startgroup)
  3007. (option (string :tag "Group description")))
  3008. (list :tag "Group tags delimiter"
  3009. (const :grouptags))
  3010. (list :tag "End radio group"
  3011. (const :endgroup)
  3012. (option (string :tag "Group description")))
  3013. (const :tag "New line" (:newline)))))
  3014. (defcustom org-tag-persistent-alist nil
  3015. "List of tags that will always appear in all Org-mode files.
  3016. This is in addition to any in buffer settings or customizations
  3017. of `org-tag-alist'.
  3018. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3019. The value of this variable is an alist, the car of each entry must be a
  3020. keyword as a string, the cdr may be a character that is used to select
  3021. that tag through the fast-tag-selection interface.
  3022. See the manual for details.
  3023. To disable these tags on a per-file basis, insert anywhere in the file:
  3024. #+STARTUP: noptag"
  3025. :group 'org-tags
  3026. :type '(repeat
  3027. (choice
  3028. (cons (string :tag "Tag name")
  3029. (character :tag "Access char"))
  3030. (const :tag "Start radio group" (:startgroup))
  3031. (const :tag "Group tags delimiter" (:grouptags))
  3032. (const :tag "End radio group" (:endgroup))
  3033. (const :tag "New line" (:newline)))))
  3034. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3035. "If non-nil, always offer completion for all tags of all agenda files.
  3036. Instead of customizing this variable directly, you might want to
  3037. set it locally for capture buffers, because there no list of
  3038. tags in that file can be created dynamically (there are none).
  3039. (add-hook 'org-capture-mode-hook
  3040. (lambda ()
  3041. (set (make-local-variable
  3042. 'org-complete-tags-always-offer-all-agenda-tags)
  3043. t)))"
  3044. :group 'org-tags
  3045. :version "24.1"
  3046. :type 'boolean)
  3047. (defvar org-file-tags nil
  3048. "List of tags that can be inherited by all entries in the file.
  3049. The tags will be inherited if the variable `org-use-tag-inheritance'
  3050. says they should be.
  3051. This variable is populated from #+FILETAGS lines.")
  3052. (defcustom org-use-fast-tag-selection 'auto
  3053. "Non-nil means use fast tag selection scheme.
  3054. This is a special interface to select and deselect tags with single keys.
  3055. When nil, fast selection is never used.
  3056. When the symbol `auto', fast selection is used if and only if selection
  3057. characters for tags have been configured, either through the variable
  3058. `org-tag-alist' or through a #+TAGS line in the buffer.
  3059. When t, fast selection is always used and selection keys are assigned
  3060. automatically if necessary."
  3061. :group 'org-tags
  3062. :type '(choice
  3063. (const :tag "Always" t)
  3064. (const :tag "Never" nil)
  3065. (const :tag "When selection characters are configured" auto)))
  3066. (defcustom org-fast-tag-selection-single-key nil
  3067. "Non-nil means fast tag selection exits after first change.
  3068. When nil, you have to press RET to exit it.
  3069. During fast tag selection, you can toggle this flag with `C-c'.
  3070. This variable can also have the value `expert'. In this case, the window
  3071. displaying the tags menu is not even shown, until you press C-c again."
  3072. :group 'org-tags
  3073. :type '(choice
  3074. (const :tag "No" nil)
  3075. (const :tag "Yes" t)
  3076. (const :tag "Expert" expert)))
  3077. (defvar org-fast-tag-selection-include-todo nil
  3078. "Non-nil means fast tags selection interface will also offer TODO states.
  3079. This is an undocumented feature, you should not rely on it.")
  3080. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3081. "The column to which tags should be indented in a headline.
  3082. If this number is positive, it specifies the column. If it is negative,
  3083. it means that the tags should be flushright to that column. For example,
  3084. -80 works well for a normal 80 character screen.
  3085. When 0, place tags directly after headline text, with only one space in
  3086. between."
  3087. :group 'org-tags
  3088. :type 'integer)
  3089. (defcustom org-auto-align-tags t
  3090. "Non-nil keeps tags aligned when modifying headlines.
  3091. Some operations (i.e. demoting) change the length of a headline and
  3092. therefore shift the tags around. With this option turned on, after
  3093. each such operation the tags are again aligned to `org-tags-column'."
  3094. :group 'org-tags
  3095. :type 'boolean)
  3096. (defcustom org-use-tag-inheritance t
  3097. "Non-nil means tags in levels apply also for sublevels.
  3098. When nil, only the tags directly given in a specific line apply there.
  3099. This may also be a list of tags that should be inherited, or a regexp that
  3100. matches tags that should be inherited. Additional control is possible
  3101. with the variable `org-tags-exclude-from-inheritance' which gives an
  3102. explicit list of tags to be excluded from inheritance, even if the value of
  3103. `org-use-tag-inheritance' would select it for inheritance.
  3104. If this option is t, a match early-on in a tree can lead to a large
  3105. number of matches in the subtree when constructing the agenda or creating
  3106. a sparse tree. If you only want to see the first match in a tree during
  3107. a search, check out the variable `org-tags-match-list-sublevels'."
  3108. :group 'org-tags
  3109. :type '(choice
  3110. (const :tag "Not" nil)
  3111. (const :tag "Always" t)
  3112. (repeat :tag "Specific tags" (string :tag "Tag"))
  3113. (regexp :tag "Tags matched by regexp")))
  3114. (defcustom org-tags-exclude-from-inheritance nil
  3115. "List of tags that should never be inherited.
  3116. This is a way to exclude a few tags from inheritance. For way to do
  3117. the opposite, to actively allow inheritance for selected tags,
  3118. see the variable `org-use-tag-inheritance'."
  3119. :group 'org-tags
  3120. :type '(repeat (string :tag "Tag")))
  3121. (defun org-tag-inherit-p (tag)
  3122. "Check if TAG is one that should be inherited."
  3123. (cond
  3124. ((member tag org-tags-exclude-from-inheritance) nil)
  3125. ((eq org-use-tag-inheritance t) t)
  3126. ((not org-use-tag-inheritance) nil)
  3127. ((stringp org-use-tag-inheritance)
  3128. (string-match org-use-tag-inheritance tag))
  3129. ((listp org-use-tag-inheritance)
  3130. (member tag org-use-tag-inheritance))
  3131. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3132. (defcustom org-tags-match-list-sublevels t
  3133. "Non-nil means list also sublevels of headlines matching a search.
  3134. This variable applies to tags/property searches, and also to stuck
  3135. projects because this search is based on a tags match as well.
  3136. When set to the symbol `indented', sublevels are indented with
  3137. leading dots.
  3138. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3139. the sublevels of a headline matching a tag search often also match
  3140. the same search. Listing all of them can create very long lists.
  3141. Setting this variable to nil causes subtrees of a match to be skipped.
  3142. This variable is semi-obsolete and probably should always be true. It
  3143. is better to limit inheritance to certain tags using the variables
  3144. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3145. :group 'org-tags
  3146. :type '(choice
  3147. (const :tag "No, don't list them" nil)
  3148. (const :tag "Yes, do list them" t)
  3149. (const :tag "List them, indented with leading dots" indented)))
  3150. (defcustom org-tags-sort-function nil
  3151. "When set, tags are sorted using this function as a comparator."
  3152. :group 'org-tags
  3153. :type '(choice
  3154. (const :tag "No sorting" nil)
  3155. (const :tag "Alphabetical" string<)
  3156. (const :tag "Reverse alphabetical" string>)
  3157. (function :tag "Custom function" nil)))
  3158. (defvar org-tags-history nil
  3159. "History of minibuffer reads for tags.")
  3160. (defvar org-last-tags-completion-table nil
  3161. "The last used completion table for tags.")
  3162. (defvar org-after-tags-change-hook nil
  3163. "Hook that is run after the tags in a line have changed.")
  3164. (defgroup org-properties nil
  3165. "Options concerning properties in Org-mode."
  3166. :tag "Org Properties"
  3167. :group 'org)
  3168. (defcustom org-property-format "%-10s %s"
  3169. "How property key/value pairs should be formatted by `indent-line'.
  3170. When `indent-line' hits a property definition, it will format the line
  3171. according to this format, mainly to make sure that the values are
  3172. lined-up with respect to each other."
  3173. :group 'org-properties
  3174. :type 'string)
  3175. (defcustom org-properties-postprocess-alist nil
  3176. "Alist of properties and functions to adjust inserted values.
  3177. Elements of this alist must be of the form
  3178. ([string] [function])
  3179. where [string] must be a property name and [function] must be a
  3180. lambda expression: this lambda expression must take one argument,
  3181. the value to adjust, and return the new value as a string.
  3182. For example, this element will allow the property \"Remaining\"
  3183. to be updated wrt the relation between the \"Effort\" property
  3184. and the clock summary:
  3185. ((\"Remaining\" (lambda(value)
  3186. (let ((clocksum (org-clock-sum-current-item))
  3187. (effort (org-duration-string-to-minutes
  3188. (org-entry-get (point) \"Effort\"))))
  3189. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3190. :group 'org-properties
  3191. :version "24.1"
  3192. :type '(alist :key-type (string :tag "Property")
  3193. :value-type (function :tag "Function")))
  3194. (defcustom org-use-property-inheritance nil
  3195. "Non-nil means properties apply also for sublevels.
  3196. This setting is chiefly used during property searches. Turning it on can
  3197. cause significant overhead when doing a search, which is why it is not
  3198. on by default.
  3199. When nil, only the properties directly given in the current entry count.
  3200. When t, every property is inherited. The value may also be a list of
  3201. properties that should have inheritance, or a regular expression matching
  3202. properties that should be inherited.
  3203. However, note that some special properties use inheritance under special
  3204. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3205. and the properties ending in \"_ALL\" when they are used as descriptor
  3206. for valid values of a property.
  3207. Note for programmers:
  3208. When querying an entry with `org-entry-get', you can control if inheritance
  3209. should be used. By default, `org-entry-get' looks only at the local
  3210. properties. You can request inheritance by setting the inherit argument
  3211. to t (to force inheritance) or to `selective' (to respect the setting
  3212. in this variable)."
  3213. :group 'org-properties
  3214. :type '(choice
  3215. (const :tag "Not" nil)
  3216. (const :tag "Always" t)
  3217. (repeat :tag "Specific properties" (string :tag "Property"))
  3218. (regexp :tag "Properties matched by regexp")))
  3219. (defun org-property-inherit-p (property)
  3220. "Check if PROPERTY is one that should be inherited."
  3221. (cond
  3222. ((eq org-use-property-inheritance t) t)
  3223. ((not org-use-property-inheritance) nil)
  3224. ((stringp org-use-property-inheritance)
  3225. (string-match org-use-property-inheritance property))
  3226. ((listp org-use-property-inheritance)
  3227. (member property org-use-property-inheritance))
  3228. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3229. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3230. "The default column format, if no other format has been defined.
  3231. This variable can be set on the per-file basis by inserting a line
  3232. #+COLUMNS: %25ITEM ....."
  3233. :group 'org-properties
  3234. :type 'string)
  3235. (defcustom org-columns-ellipses ".."
  3236. "The ellipses to be used when a field in column view is truncated.
  3237. When this is the empty string, as many characters as possible are shown,
  3238. but then there will be no visual indication that the field has been truncated.
  3239. When this is a string of length N, the last N characters of a truncated
  3240. field are replaced by this string. If the column is narrower than the
  3241. ellipses string, only part of the ellipses string will be shown."
  3242. :group 'org-properties
  3243. :type 'string)
  3244. (defcustom org-columns-modify-value-for-display-function nil
  3245. "Function that modifies values for display in column view.
  3246. For example, it can be used to cut out a certain part from a time stamp.
  3247. The function must take 2 arguments:
  3248. column-title The title of the column (*not* the property name)
  3249. value The value that should be modified.
  3250. The function should return the value that should be displayed,
  3251. or nil if the normal value should be used."
  3252. :group 'org-properties
  3253. :type '(choice (const nil) (function)))
  3254. (defconst org-global-properties-fixed
  3255. '(("VISIBILITY_ALL" . "folded children content all")
  3256. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3257. "List of property/value pairs that can be inherited by any entry.
  3258. These are fixed values, for the preset properties. The user variable
  3259. that can be used to add to this list is `org-global-properties'.
  3260. The entries in this list are cons cells where the car is a property
  3261. name and cdr is a string with the value. If the value represents
  3262. multiple items like an \"_ALL\" property, separate the items by
  3263. spaces.")
  3264. (defcustom org-global-properties nil
  3265. "List of property/value pairs that can be inherited by any entry.
  3266. This list will be combined with the constant `org-global-properties-fixed'.
  3267. The entries in this list are cons cells where the car is a property
  3268. name and cdr is a string with the value.
  3269. You can set buffer-local values for the same purpose in the variable
  3270. `org-file-properties' this by adding lines like
  3271. #+PROPERTY: NAME VALUE"
  3272. :group 'org-properties
  3273. :type '(repeat
  3274. (cons (string :tag "Property")
  3275. (string :tag "Value"))))
  3276. (defvar org-file-properties nil
  3277. "List of property/value pairs that can be inherited by any entry.
  3278. Valid for the current buffer.
  3279. This variable is populated from #+PROPERTY lines.")
  3280. (make-variable-buffer-local 'org-file-properties)
  3281. (defgroup org-agenda nil
  3282. "Options concerning agenda views in Org-mode."
  3283. :tag "Org Agenda"
  3284. :group 'org)
  3285. (defvar org-category nil
  3286. "Variable used by org files to set a category for agenda display.
  3287. Such files should use a file variable to set it, for example
  3288. # -*- mode: org; org-category: \"ELisp\"
  3289. or contain a special line
  3290. #+CATEGORY: ELisp
  3291. If the file does not specify a category, then file's base name
  3292. is used instead.")
  3293. (make-variable-buffer-local 'org-category)
  3294. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3295. (defcustom org-agenda-files nil
  3296. "The files to be used for agenda display.
  3297. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3298. \\[org-remove-file]. You can also use customize to edit the list.
  3299. If an entry is a directory, all files in that directory that are matched by
  3300. `org-agenda-file-regexp' will be part of the file list.
  3301. If the value of the variable is not a list but a single file name, then
  3302. the list of agenda files is actually stored and maintained in that file, one
  3303. agenda file per line. In this file paths can be given relative to
  3304. `org-directory'. Tilde expansion and environment variable substitution
  3305. are also made."
  3306. :group 'org-agenda
  3307. :type '(choice
  3308. (repeat :tag "List of files and directories" file)
  3309. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3310. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3311. "Regular expression to match files for `org-agenda-files'.
  3312. If any element in the list in that variable contains a directory instead
  3313. of a normal file, all files in that directory that are matched by this
  3314. regular expression will be included."
  3315. :group 'org-agenda
  3316. :type 'regexp)
  3317. (defcustom org-agenda-text-search-extra-files nil
  3318. "List of extra files to be searched by text search commands.
  3319. These files will be searched in addition to the agenda files by the
  3320. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3321. Note that these files will only be searched for text search commands,
  3322. not for the other agenda views like todo lists, tag searches or the weekly
  3323. agenda. This variable is intended to list notes and possibly archive files
  3324. that should also be searched by these two commands.
  3325. In fact, if the first element in the list is the symbol `agenda-archives',
  3326. then all archive files of all agenda files will be added to the search
  3327. scope."
  3328. :group 'org-agenda
  3329. :type '(set :greedy t
  3330. (const :tag "Agenda Archives" agenda-archives)
  3331. (repeat :inline t (file))))
  3332. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3333. 'org-agenda-text-search-extra-files)
  3334. (defcustom org-agenda-skip-unavailable-files nil
  3335. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3336. A nil value means to remove them, after a query, from the list."
  3337. :group 'org-agenda
  3338. :type 'boolean)
  3339. (defcustom org-calendar-to-agenda-key [?c]
  3340. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3341. The command `org-calendar-goto-agenda' will be bound to this key. The
  3342. default is the character `c' because then `c' can be used to switch back and
  3343. forth between agenda and calendar."
  3344. :group 'org-agenda
  3345. :type 'sexp)
  3346. (defcustom org-calendar-insert-diary-entry-key [?i]
  3347. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3348. This option is irrelevant until `org-agenda-diary-file' has been configured
  3349. to point to an Org-mode file. When that is the case, the command
  3350. `org-agenda-diary-entry' will be bound to the key given here, by default
  3351. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3352. if you want to continue doing this, you need to change this to a different
  3353. key."
  3354. :group 'org-agenda
  3355. :type 'sexp)
  3356. (defcustom org-agenda-diary-file 'diary-file
  3357. "File to which to add new entries with the `i' key in agenda and calendar.
  3358. When this is the symbol `diary-file', the functionality in the Emacs
  3359. calendar will be used to add entries to the `diary-file'. But when this
  3360. points to a file, `org-agenda-diary-entry' will be used instead."
  3361. :group 'org-agenda
  3362. :type '(choice
  3363. (const :tag "The standard Emacs diary file" diary-file)
  3364. (file :tag "Special Org file diary entries")))
  3365. (eval-after-load "calendar"
  3366. '(progn
  3367. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3368. 'org-calendar-goto-agenda)
  3369. (add-hook 'calendar-mode-hook
  3370. (lambda ()
  3371. (unless (eq org-agenda-diary-file 'diary-file)
  3372. (define-key calendar-mode-map
  3373. org-calendar-insert-diary-entry-key
  3374. 'org-agenda-diary-entry))))))
  3375. (defgroup org-latex nil
  3376. "Options for embedding LaTeX code into Org-mode."
  3377. :tag "Org LaTeX"
  3378. :group 'org)
  3379. (defcustom org-format-latex-options
  3380. '(:foreground default :background default :scale 1.0
  3381. :html-foreground "Black" :html-background "Transparent"
  3382. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3383. "Options for creating images from LaTeX fragments.
  3384. This is a property list with the following properties:
  3385. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3386. `default' means use the foreground of the default face.
  3387. `auto' means use the foreground from the text face.
  3388. :background the background color, or \"Transparent\".
  3389. `default' means use the background of the default face.
  3390. `auto' means use the background from the text face.
  3391. :scale a scaling factor for the size of the images, to get more pixels
  3392. :html-foreground, :html-background, :html-scale
  3393. the same numbers for HTML export.
  3394. :matchers a list indicating which matchers should be used to
  3395. find LaTeX fragments. Valid members of this list are:
  3396. \"begin\" find environments
  3397. \"$1\" find single characters surrounded by $.$
  3398. \"$\" find math expressions surrounded by $...$
  3399. \"$$\" find math expressions surrounded by $$....$$
  3400. \"\\(\" find math expressions surrounded by \\(...\\)
  3401. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3402. :group 'org-latex
  3403. :type 'plist)
  3404. (defcustom org-format-latex-signal-error t
  3405. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3406. When nil, just push out a message."
  3407. :group 'org-latex
  3408. :version "24.1"
  3409. :type 'boolean)
  3410. (defcustom org-latex-to-mathml-jar-file nil
  3411. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3412. Use this to specify additional executable file say a jar file.
  3413. When using MathToWeb as the converter, specify the full-path to
  3414. your mathtoweb.jar file."
  3415. :group 'org-latex
  3416. :version "24.1"
  3417. :type '(choice
  3418. (const :tag "None" nil)
  3419. (file :tag "JAR file" :must-match t)))
  3420. (defcustom org-latex-to-mathml-convert-command nil
  3421. "Command to convert LaTeX fragments to MathML.
  3422. Replace format-specifiers in the command as noted below and use
  3423. `shell-command' to convert LaTeX to MathML.
  3424. %j: Executable file in fully expanded form as specified by
  3425. `org-latex-to-mathml-jar-file'.
  3426. %I: Input LaTeX file in fully expanded form.
  3427. %i: The latex fragment to be converted.
  3428. %o: Output MathML file.
  3429. This command is used by `org-create-math-formula'.
  3430. When using MathToWeb as the converter, set this to
  3431. \"java -jar %j -unicode -force -df %o %I\".
  3432. When using LaTeXML set this to
  3433. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3434. :group 'org-latex
  3435. :version "24.1"
  3436. :type '(choice
  3437. (const :tag "None" nil)
  3438. (string :tag "\nShell command")))
  3439. (defcustom org-latex-create-formula-image-program 'dvipng
  3440. "Program to convert LaTeX fragments with.
  3441. dvipng Process the LaTeX fragments to dvi file, then convert
  3442. dvi files to png files using dvipng.
  3443. This will also include processing of non-math environments.
  3444. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3445. to convert pdf files to png files"
  3446. :group 'org-latex
  3447. :version "24.1"
  3448. :type '(choice
  3449. (const :tag "dvipng" dvipng)
  3450. (const :tag "imagemagick" imagemagick)))
  3451. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3452. "Path to store latex preview images.
  3453. A relative path here creates many directories relative to the
  3454. processed org files paths. An absolute path puts all preview
  3455. images at the same place."
  3456. :group 'org-latex
  3457. :version "24.3"
  3458. :type 'string)
  3459. (defun org-format-latex-mathml-available-p ()
  3460. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3461. (save-match-data
  3462. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3463. org-latex-to-mathml-convert-command)
  3464. (let ((executable (car (split-string
  3465. org-latex-to-mathml-convert-command))))
  3466. (when (executable-find executable)
  3467. (if (string-match
  3468. "%j" org-latex-to-mathml-convert-command)
  3469. (file-readable-p org-latex-to-mathml-jar-file)
  3470. t))))))
  3471. (defcustom org-format-latex-header "\\documentclass{article}
  3472. \\usepackage[usenames]{color}
  3473. \[PACKAGES]
  3474. \[DEFAULT-PACKAGES]
  3475. \\pagestyle{empty} % do not remove
  3476. % The settings below are copied from fullpage.sty
  3477. \\setlength{\\textwidth}{\\paperwidth}
  3478. \\addtolength{\\textwidth}{-3cm}
  3479. \\setlength{\\oddsidemargin}{1.5cm}
  3480. \\addtolength{\\oddsidemargin}{-2.54cm}
  3481. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3482. \\setlength{\\textheight}{\\paperheight}
  3483. \\addtolength{\\textheight}{-\\headheight}
  3484. \\addtolength{\\textheight}{-\\headsep}
  3485. \\addtolength{\\textheight}{-\\footskip}
  3486. \\addtolength{\\textheight}{-3cm}
  3487. \\setlength{\\topmargin}{1.5cm}
  3488. \\addtolength{\\topmargin}{-2.54cm}"
  3489. "The document header used for processing LaTeX fragments.
  3490. It is imperative that this header make sure that no page number
  3491. appears on the page. The package defined in the variables
  3492. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3493. will either replace the placeholder \"[PACKAGES]\" in this
  3494. header, or they will be appended."
  3495. :group 'org-latex
  3496. :type 'string)
  3497. (defun org-set-packages-alist (var val)
  3498. "Set the packages alist and make sure it has 3 elements per entry."
  3499. (set var (mapcar (lambda (x)
  3500. (if (and (consp x) (= (length x) 2))
  3501. (list (car x) (nth 1 x) t)
  3502. x))
  3503. val)))
  3504. (defun org-get-packages-alist (var)
  3505. "Get the packages alist and make sure it has 3 elements per entry."
  3506. (mapcar (lambda (x)
  3507. (if (and (consp x) (= (length x) 2))
  3508. (list (car x) (nth 1 x) t)
  3509. x))
  3510. (default-value var)))
  3511. (defcustom org-latex-default-packages-alist
  3512. '(("AUTO" "inputenc" t)
  3513. ("T1" "fontenc" t)
  3514. ("" "fixltx2e" nil)
  3515. ("" "graphicx" t)
  3516. ("" "longtable" nil)
  3517. ("" "float" nil)
  3518. ("" "wrapfig" nil)
  3519. ("" "rotating" nil)
  3520. ("normalem" "ulem" t)
  3521. ("" "amsmath" t)
  3522. ("" "textcomp" t)
  3523. ("" "marvosym" t)
  3524. ("" "wasysym" t)
  3525. ("" "amssymb" t)
  3526. ("" "capt-of" nil)
  3527. ("" "hyperref" nil)
  3528. "\\tolerance=1000")
  3529. "Alist of default packages to be inserted in the header.
  3530. Change this only if one of the packages here causes an
  3531. incompatibility with another package you are using.
  3532. The packages in this list are needed by one part or another of
  3533. Org mode to function properly:
  3534. - inputenc, fontenc: for basic font and character selection
  3535. - fixltx2e: Important patches of LaTeX itself
  3536. - graphicx: for including images
  3537. - longtable: For multipage tables
  3538. - float, 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, marvosymb, wasysym, 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 "24.1"
  3563. :type '(repeat
  3564. (choice
  3565. (list :tag "options/package pair"
  3566. (string :tag "options")
  3567. (string :tag "package")
  3568. (boolean :tag "Snippet"))
  3569. (string :tag "A line of LaTeX"))))
  3570. (defcustom org-latex-packages-alist nil
  3571. "Alist of packages to be inserted in every LaTeX header.
  3572. These will be inserted after `org-latex-default-packages-alist'.
  3573. Each element is either a cell or a string.
  3574. A cell is of the format:
  3575. \(\"options\" \"package\" SNIPPET-FLAG)
  3576. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3577. needed when turning LaTeX snippets into images for inclusion into
  3578. non-LaTeX output.
  3579. A string will be inserted as-is in the header of the document.
  3580. Make sure that you only list packages here which:
  3581. - you want in every file;
  3582. - do not conflict with the setup in `org-format-latex-header';
  3583. - do not conflict with the default packages in
  3584. `org-latex-default-packages-alist'."
  3585. :group 'org-latex
  3586. :group 'org-export-latex
  3587. :set 'org-set-packages-alist
  3588. :get 'org-get-packages-alist
  3589. :type '(repeat
  3590. (choice
  3591. (list :tag "options/package pair"
  3592. (string :tag "options")
  3593. (string :tag "package")
  3594. (boolean :tag "Snippet"))
  3595. (string :tag "A line of LaTeX"))))
  3596. (defgroup org-appearance nil
  3597. "Settings for Org-mode appearance."
  3598. :tag "Org Appearance"
  3599. :group 'org)
  3600. (defcustom org-level-color-stars-only nil
  3601. "Non-nil means fontify only the stars in each headline.
  3602. When nil, the entire headline is fontified.
  3603. Changing it requires restart of `font-lock-mode' to become effective
  3604. also in regions already fontified."
  3605. :group 'org-appearance
  3606. :type 'boolean)
  3607. (defcustom org-hide-leading-stars nil
  3608. "Non-nil means hide the first N-1 stars in a headline.
  3609. This works by using the face `org-hide' for these stars. This
  3610. face is white for a light background, and black for a dark
  3611. background. You may have to customize the face `org-hide' to
  3612. make this work.
  3613. Changing it requires restart of `font-lock-mode' to become effective
  3614. also in regions already fontified.
  3615. You may also set this on a per-file basis by adding one of the following
  3616. lines to the buffer:
  3617. #+STARTUP: hidestars
  3618. #+STARTUP: showstars"
  3619. :group 'org-appearance
  3620. :type 'boolean)
  3621. (defcustom org-hidden-keywords nil
  3622. "List of symbols corresponding to keywords to be hidden the org buffer.
  3623. For example, a value '(title) for this list will make the document's title
  3624. appear in the buffer without the initial #+TITLE: keyword."
  3625. :group 'org-appearance
  3626. :version "24.1"
  3627. :type '(set (const :tag "#+AUTHOR" author)
  3628. (const :tag "#+DATE" date)
  3629. (const :tag "#+EMAIL" email)
  3630. (const :tag "#+TITLE" title)))
  3631. (defcustom org-custom-properties nil
  3632. "List of properties (as strings) with a special meaning.
  3633. The default use of these custom properties is to let the user
  3634. hide them with `org-toggle-custom-properties-visibility'."
  3635. :group 'org-properties
  3636. :group 'org-appearance
  3637. :version "24.3"
  3638. :type '(repeat (string :tag "Property Name")))
  3639. (defcustom org-fontify-done-headline nil
  3640. "Non-nil means change the face of a headline if it is marked DONE.
  3641. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3642. When this is non-nil, the headline after the keyword is set to the
  3643. `org-headline-done' as an additional indication."
  3644. :group 'org-appearance
  3645. :type 'boolean)
  3646. (defcustom org-fontify-emphasized-text t
  3647. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3648. Changing this variable requires a restart of Emacs to take effect."
  3649. :group 'org-appearance
  3650. :type 'boolean)
  3651. (defcustom org-fontify-whole-heading-line nil
  3652. "Non-nil means fontify the whole line for headings.
  3653. This is useful when setting a background color for the
  3654. org-level-* faces."
  3655. :group 'org-appearance
  3656. :type 'boolean)
  3657. (defcustom org-highlight-latex-and-related nil
  3658. "Non-nil means highlight LaTeX related syntax in the buffer.
  3659. When non nil, the value should be a list containing any of the
  3660. following symbols:
  3661. `latex' Highlight LaTeX snippets and environments.
  3662. `script' Highlight subscript and superscript.
  3663. `entities' Highlight entities."
  3664. :group 'org-appearance
  3665. :version "24.4"
  3666. :package-version '(Org . "8.0")
  3667. :type '(choice
  3668. (const :tag "No highlighting" nil)
  3669. (set :greedy t :tag "Highlight"
  3670. (const :tag "LaTeX snippets and environments" latex)
  3671. (const :tag "Subscript and superscript" script)
  3672. (const :tag "Entities" entities))))
  3673. (defcustom org-hide-emphasis-markers nil
  3674. "Non-nil mean font-lock should hide the emphasis marker characters."
  3675. :group 'org-appearance
  3676. :type 'boolean)
  3677. (defcustom org-hide-macro-markers nil
  3678. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3679. :group 'org-appearance
  3680. :type 'boolean)
  3681. (defcustom org-pretty-entities nil
  3682. "Non-nil means show entities as UTF8 characters.
  3683. When nil, the \\name form remains in the buffer."
  3684. :group 'org-appearance
  3685. :version "24.1"
  3686. :type 'boolean)
  3687. (defcustom org-pretty-entities-include-sub-superscripts t
  3688. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3689. :group 'org-appearance
  3690. :version "24.1"
  3691. :type 'boolean)
  3692. (defvar org-emph-re nil
  3693. "Regular expression for matching emphasis.
  3694. After a match, the match groups contain these elements:
  3695. 0 The match of the full regular expression, including the characters
  3696. before and after the proper match
  3697. 1 The character before the proper match, or empty at beginning of line
  3698. 2 The proper match, including the leading and trailing markers
  3699. 3 The leading marker like * or /, indicating the type of highlighting
  3700. 4 The text between the emphasis markers, not including the markers
  3701. 5 The character after the match, empty at the end of a line")
  3702. (defvar org-verbatim-re nil
  3703. "Regular expression for matching verbatim text.")
  3704. (defvar org-emphasis-regexp-components) ; defined just below
  3705. (defvar org-emphasis-alist) ; defined just below
  3706. (defun org-set-emph-re (var val)
  3707. "Set variable and compute the emphasis regular expression."
  3708. (set var val)
  3709. (when (and (boundp 'org-emphasis-alist)
  3710. (boundp 'org-emphasis-regexp-components)
  3711. org-emphasis-alist org-emphasis-regexp-components)
  3712. (let* ((e org-emphasis-regexp-components)
  3713. (pre (car e))
  3714. (post (nth 1 e))
  3715. (border (nth 2 e))
  3716. (body (nth 3 e))
  3717. (nl (nth 4 e))
  3718. (body1 (concat body "*?"))
  3719. (markers (mapconcat 'car org-emphasis-alist ""))
  3720. (vmarkers (mapconcat
  3721. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3722. org-emphasis-alist "")))
  3723. ;; make sure special characters appear at the right position in the class
  3724. (if (string-match "\\^" markers)
  3725. (setq markers (concat (replace-match "" t t markers) "^")))
  3726. (if (string-match "-" markers)
  3727. (setq markers (concat (replace-match "" t t markers) "-")))
  3728. (if (string-match "\\^" vmarkers)
  3729. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3730. (if (string-match "-" vmarkers)
  3731. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3732. (if (> nl 0)
  3733. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3734. (int-to-string nl) "\\}")))
  3735. ;; Make the regexp
  3736. (setq org-emph-re
  3737. (concat "\\([" pre "]\\|^\\)"
  3738. "\\("
  3739. "\\([" markers "]\\)"
  3740. "\\("
  3741. "[^" border "]\\|"
  3742. "[^" border "]"
  3743. body1
  3744. "[^" border "]"
  3745. "\\)"
  3746. "\\3\\)"
  3747. "\\([" post "]\\|$\\)"))
  3748. (setq org-verbatim-re
  3749. (concat "\\([" pre "]\\|^\\)"
  3750. "\\("
  3751. "\\([" vmarkers "]\\)"
  3752. "\\("
  3753. "[^" border "]\\|"
  3754. "[^" border "]"
  3755. body1
  3756. "[^" border "]"
  3757. "\\)"
  3758. "\\3\\)"
  3759. "\\([" post "]\\|$\\)")))))
  3760. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3761. ;; set this option proved cumbersome. See this message/thread:
  3762. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3763. (defvar org-emphasis-regexp-components
  3764. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3765. "Components used to build the regular expression for emphasis.
  3766. This is a list with five entries. Terminology: In an emphasis string
  3767. like \" *strong word* \", we call the initial space PREMATCH, the final
  3768. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3769. and \"trong wor\" is the body. The different components in this variable
  3770. specify what is allowed/forbidden in each part:
  3771. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3772. post Chars allowed as postmatch. End of line will be allowed too.
  3773. border The chars *forbidden* as border characters.
  3774. body-regexp A regexp like \".\" to match a body character. Don't use
  3775. non-shy groups here, and don't allow newline here.
  3776. newline The maximum number of newlines allowed in an emphasis exp.
  3777. You need to reload Org or to restart Emacs after customizing this.")
  3778. (defcustom org-emphasis-alist
  3779. `(("*" bold)
  3780. ("/" italic)
  3781. ("_" underline)
  3782. ("=" org-verbatim verbatim)
  3783. ("~" org-code verbatim)
  3784. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3785. "Alist of characters and faces to emphasize text.
  3786. Text starting and ending with a special character will be emphasized,
  3787. for example *bold*, _underlined_ and /italic/. This variable sets the
  3788. marker characters and the face to be used by font-lock for highlighting
  3789. in Org-mode Emacs buffers.
  3790. You need to reload Org or to restart Emacs after customizing this."
  3791. :group 'org-appearance
  3792. :set 'org-set-emph-re
  3793. :version "24.4"
  3794. :package-version '(Org . "8.0")
  3795. :type '(repeat
  3796. (list
  3797. (string :tag "Marker character")
  3798. (choice
  3799. (face :tag "Font-lock-face")
  3800. (plist :tag "Face property list"))
  3801. (option (const verbatim)))))
  3802. (defvar org-protecting-blocks
  3803. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3804. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3805. This is needed for font-lock setup.")
  3806. ;;; Miscellaneous options
  3807. (defgroup org-completion nil
  3808. "Completion in Org-mode."
  3809. :tag "Org Completion"
  3810. :group 'org)
  3811. (defcustom org-completion-use-ido nil
  3812. "Non-nil means use ido completion wherever possible.
  3813. Note that `ido-mode' must be active for this variable to be relevant.
  3814. If you decide to turn this variable on, you might well want to turn off
  3815. `org-outline-path-complete-in-steps'.
  3816. See also `org-completion-use-iswitchb'."
  3817. :group 'org-completion
  3818. :type 'boolean)
  3819. (defcustom org-completion-use-iswitchb nil
  3820. "Non-nil means use iswitchb completion wherever possible.
  3821. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3822. If you decide to turn this variable on, you might well want to turn off
  3823. `org-outline-path-complete-in-steps'.
  3824. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3825. :group 'org-completion
  3826. :type 'boolean)
  3827. (defcustom org-completion-fallback-command 'hippie-expand
  3828. "The expansion command called by \\[pcomplete] in normal context.
  3829. Normal means, no org-mode-specific context."
  3830. :group 'org-completion
  3831. :type 'function)
  3832. ;;; Functions and variables from their packages
  3833. ;; Declared here to avoid compiler warnings
  3834. ;; XEmacs only
  3835. (defvar outline-mode-menu-heading)
  3836. (defvar outline-mode-menu-show)
  3837. (defvar outline-mode-menu-hide)
  3838. (defvar zmacs-regions) ; XEmacs regions
  3839. ;; Emacs only
  3840. (defvar mark-active)
  3841. ;; Various packages
  3842. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3843. (declare-function calendar-forward-day "cal-move" (arg))
  3844. (declare-function calendar-goto-date "cal-move" (date))
  3845. (declare-function calendar-goto-today "cal-move" ())
  3846. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3847. (defvar calc-embedded-close-formula)
  3848. (defvar calc-embedded-open-formula)
  3849. (declare-function cdlatex-tab "ext:cdlatex" ())
  3850. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3851. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3852. (defvar font-lock-unfontify-region-function)
  3853. (declare-function iswitchb-read-buffer "iswitchb"
  3854. (prompt &optional default require-match start matches-set))
  3855. (defvar iswitchb-temp-buflist)
  3856. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3857. (defvar org-agenda-tags-todo-honor-ignore-options)
  3858. (declare-function org-agenda-skip "org-agenda" ())
  3859. (declare-function
  3860. org-agenda-format-item "org-agenda"
  3861. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3862. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3863. (declare-function org-agenda-change-all-lines "org-agenda"
  3864. (newhead hdmarker &optional fixface just-this))
  3865. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3866. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3867. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3868. (beg end))
  3869. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3870. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3871. "org-agenda" (&optional end))
  3872. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3873. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3874. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3875. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3876. (declare-function org-indent-mode "org-indent" (&optional arg))
  3877. (declare-function parse-time-string "parse-time" (string))
  3878. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3879. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3880. (defvar remember-data-file)
  3881. (defvar texmathp-why)
  3882. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3883. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3884. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3885. ;;;###autoload
  3886. (defun turn-on-orgtbl ()
  3887. "Unconditionally turn on `orgtbl-mode'."
  3888. (require 'org-table)
  3889. (orgtbl-mode 1))
  3890. (defun org-at-table-p (&optional table-type)
  3891. "Return t if the cursor is inside an org-type table.
  3892. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3893. (if org-enable-table-editor
  3894. (save-excursion
  3895. (beginning-of-line 1)
  3896. (looking-at (if table-type org-table-any-line-regexp
  3897. org-table-line-regexp)))
  3898. nil))
  3899. (defsubst org-table-p () (org-at-table-p))
  3900. (defun org-at-table.el-p ()
  3901. "Return t if and only if we are at a table.el table."
  3902. (and (org-at-table-p 'any)
  3903. (save-excursion
  3904. (goto-char (org-table-begin 'any))
  3905. (looking-at org-table1-hline-regexp))))
  3906. (defun org-table-recognize-table.el ()
  3907. "If there is a table.el table nearby, recognize it and move into it."
  3908. (if org-table-tab-recognizes-table.el
  3909. (if (org-at-table.el-p)
  3910. (progn
  3911. (beginning-of-line 1)
  3912. (if (looking-at org-table-dataline-regexp)
  3913. nil
  3914. (if (looking-at org-table1-hline-regexp)
  3915. (progn
  3916. (beginning-of-line 2)
  3917. (if (looking-at org-table-any-border-regexp)
  3918. (beginning-of-line -1)))))
  3919. (if (re-search-forward "|" (org-table-end t) t)
  3920. (progn
  3921. (require 'table)
  3922. (if (table--at-cell-p (point))
  3923. t
  3924. (message "recognizing table.el table...")
  3925. (table-recognize-table)
  3926. (message "recognizing table.el table...done")))
  3927. (error "This should not happen"))
  3928. t)
  3929. nil)
  3930. nil))
  3931. (defun org-at-table-hline-p ()
  3932. "Return t if the cursor is inside a hline in a table."
  3933. (if org-enable-table-editor
  3934. (save-excursion
  3935. (beginning-of-line 1)
  3936. (looking-at org-table-hline-regexp))
  3937. nil))
  3938. (defun org-table-map-tables (function &optional quietly)
  3939. "Apply FUNCTION to the start of all tables in the buffer."
  3940. (save-excursion
  3941. (save-restriction
  3942. (widen)
  3943. (goto-char (point-min))
  3944. (while (re-search-forward org-table-any-line-regexp nil t)
  3945. (unless quietly
  3946. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3947. (beginning-of-line 1)
  3948. (when (and (looking-at org-table-line-regexp)
  3949. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3950. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3951. (save-excursion (funcall function))
  3952. (or (looking-at org-table-line-regexp)
  3953. (forward-char 1)))
  3954. (re-search-forward org-table-any-border-regexp nil 1))))
  3955. (unless quietly (message "Mapping tables: done")))
  3956. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3957. (declare-function org-clock-update-mode-line "org-clock" ())
  3958. (declare-function org-resolve-clocks "org-clock"
  3959. (&optional also-non-dangling-p prompt last-valid))
  3960. (defun org-at-TBLFM-p (&optional pos)
  3961. "Return t when point (or POS) is in #+TBLFM line."
  3962. (save-excursion
  3963. (let ((pos pos)))
  3964. (goto-char (or pos (point)))
  3965. (beginning-of-line 1)
  3966. (looking-at org-TBLFM-regexp)))
  3967. (defvar org-clock-start-time)
  3968. (defvar org-clock-marker (make-marker)
  3969. "Marker recording the last clock-in.")
  3970. (defvar org-clock-hd-marker (make-marker)
  3971. "Marker recording the last clock-in, but the headline position.")
  3972. (defvar org-clock-heading ""
  3973. "The heading of the current clock entry.")
  3974. (defun org-clock-is-active ()
  3975. "Return the buffer where the clock is currently running.
  3976. Return nil if no clock is running."
  3977. (marker-buffer org-clock-marker))
  3978. (defun org-check-running-clock ()
  3979. "Check if the current buffer contains the running clock.
  3980. If yes, offer to stop it and to save the buffer with the changes."
  3981. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3982. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3983. (buffer-name))))
  3984. (org-clock-out)
  3985. (when (y-or-n-p "Save changed buffer?")
  3986. (save-buffer))))
  3987. (defun org-clocktable-try-shift (dir n)
  3988. "Check if this line starts a clock table, if yes, shift the time block."
  3989. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3990. (org-clocktable-shift dir n)))
  3991. ;;;###autoload
  3992. (defun org-clock-persistence-insinuate ()
  3993. "Set up hooks for clock persistence."
  3994. (require 'org-clock)
  3995. (add-hook 'org-mode-hook 'org-clock-load)
  3996. (add-hook 'kill-emacs-hook 'org-clock-save))
  3997. (defgroup org-archive nil
  3998. "Options concerning archiving in Org-mode."
  3999. :tag "Org Archive"
  4000. :group 'org-structure)
  4001. (defcustom org-archive-location "%s_archive::"
  4002. "The location where subtrees should be archived.
  4003. The value of this variable is a string, consisting of two parts,
  4004. separated by a double-colon. The first part is a filename and
  4005. the second part is a headline.
  4006. When the filename is omitted, archiving happens in the same file.
  4007. %s in the filename will be replaced by the current file
  4008. name (without the directory part). Archiving to a different file
  4009. is useful to keep archived entries from contributing to the
  4010. Org-mode Agenda.
  4011. The archived entries will be filed as subtrees of the specified
  4012. headline. When the headline is omitted, the subtrees are simply
  4013. filed away at the end of the file, as top-level entries. Also in
  4014. the heading you can use %s to represent the file name, this can be
  4015. useful when using the same archive for a number of different files.
  4016. Here are a few examples:
  4017. \"%s_archive::\"
  4018. If the current file is Projects.org, archive in file
  4019. Projects.org_archive, as top-level trees. This is the default.
  4020. \"::* Archived Tasks\"
  4021. Archive in the current file, under the top-level headline
  4022. \"* Archived Tasks\".
  4023. \"~/org/archive.org::\"
  4024. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4025. \"~/org/archive.org::* From %s\"
  4026. Archive in file ~/org/archive.org (absolute path), under headlines
  4027. \"From FILENAME\" where file name is the current file name.
  4028. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4029. The \"datetree/\" string is special, signifying to archive
  4030. items to the datetree. Items are placed in either the CLOSED
  4031. date of the item, or the current date if there is no CLOSED date.
  4032. The heading will be a subentry to the current date. There doesn't
  4033. need to be a heading, but there always needs to be a slash after
  4034. datetree. For example, to store archived items directly in the
  4035. datetree, use \"~/org/datetree.org::datetree/\".
  4036. \"basement::** Finished Tasks\"
  4037. Archive in file ./basement (relative path), as level 3 trees
  4038. below the level 2 heading \"** Finished Tasks\".
  4039. You may set this option on a per-file basis by adding to the buffer a
  4040. line like
  4041. #+ARCHIVE: basement::** Finished Tasks
  4042. You may also define it locally for a subtree by setting an ARCHIVE property
  4043. in the entry. If such a property is found in an entry, or anywhere up
  4044. the hierarchy, it will be used."
  4045. :group 'org-archive
  4046. :type 'string)
  4047. (defcustom org-agenda-skip-archived-trees t
  4048. "Non-nil means the agenda will skip any items located in archived trees.
  4049. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4050. variable is no longer recommended, you should leave it at the value t.
  4051. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4052. :group 'org-archive
  4053. :group 'org-agenda-skip
  4054. :type 'boolean)
  4055. (defcustom org-columns-skip-archived-trees t
  4056. "Non-nil means ignore archived trees when creating column view."
  4057. :group 'org-archive
  4058. :group 'org-properties
  4059. :type 'boolean)
  4060. (defcustom org-cycle-open-archived-trees nil
  4061. "Non-nil means `org-cycle' will open archived trees.
  4062. An archived tree is a tree marked with the tag ARCHIVE.
  4063. When nil, archived trees will stay folded. You can still open them with
  4064. normal outline commands like `show-all', but not with the cycling commands."
  4065. :group 'org-archive
  4066. :group 'org-cycle
  4067. :type 'boolean)
  4068. (defcustom org-sparse-tree-open-archived-trees nil
  4069. "Non-nil means sparse tree construction shows matches in archived trees.
  4070. When nil, matches in these trees are highlighted, but the trees are kept in
  4071. collapsed state."
  4072. :group 'org-archive
  4073. :group 'org-sparse-trees
  4074. :type 'boolean)
  4075. (defcustom org-sparse-tree-default-date-type nil
  4076. "The default date type when building a sparse tree.
  4077. When this is nil, a date is a scheduled or a deadline timestamp.
  4078. Otherwise, these types are allowed:
  4079. all: all timestamps
  4080. active: only active timestamps (<...>)
  4081. inactive: only inactive timestamps ([...])
  4082. scheduled: only scheduled timestamps
  4083. deadline: only deadline timestamps"
  4084. :type '(choice (const :tag "Scheduled or deadline" nil)
  4085. (const :tag "All timestamps" all)
  4086. (const :tag "Only active timestamps" active)
  4087. (const :tag "Only inactive timestamps" inactive)
  4088. (const :tag "Only scheduled timestamps" scheduled)
  4089. (const :tag "Only deadline timestamps" deadline)
  4090. (const :tag "Only closed timestamps" closed))
  4091. :version "25.1"
  4092. :package-version '(Org . "8.3")
  4093. :group 'org-sparse-trees)
  4094. (defun org-cycle-hide-archived-subtrees (state)
  4095. "Re-hide all archived subtrees after a visibility state change."
  4096. (when (and (not org-cycle-open-archived-trees)
  4097. (not (memq state '(overview folded))))
  4098. (save-excursion
  4099. (let* ((globalp (memq state '(contents all)))
  4100. (beg (if globalp (point-min) (point)))
  4101. (end (if globalp (point-max) (org-end-of-subtree t))))
  4102. (org-hide-archived-subtrees beg end)
  4103. (goto-char beg)
  4104. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4105. (message "%s" (substitute-command-keys
  4106. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4107. (defun org-force-cycle-archived ()
  4108. "Cycle subtree even if it is archived."
  4109. (interactive)
  4110. (setq this-command 'org-cycle)
  4111. (let ((org-cycle-open-archived-trees t))
  4112. (call-interactively 'org-cycle)))
  4113. (defun org-hide-archived-subtrees (beg end)
  4114. "Re-hide all archived subtrees after a visibility state change."
  4115. (org-with-wide-buffer
  4116. (let ((case-fold-search nil)
  4117. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4118. (goto-char beg)
  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-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. (:grouptags ":")
  4562. (:newline "\\n")
  4563. (otherwise (concat (car tag)
  4564. (and (characterp (cdr tag))
  4565. (format "(%c)" (cdr tag)))))))
  4566. org-tag-alist)))
  4567. ;; Process the tags.
  4568. (org-set-local 'org-tag-groups-alist nil)
  4569. (org-set-local 'org-tag-alist nil)
  4570. (let (group-flag)
  4571. (while tags
  4572. (let ((e (car tags)))
  4573. (setq tags (cdr tags))
  4574. (cond
  4575. ((equal e "{")
  4576. (push '(:startgroup) org-tag-alist)
  4577. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4578. ((equal e "}")
  4579. (push '(:endgroup) org-tag-alist)
  4580. (setq group-flag nil))
  4581. ((equal e ":")
  4582. (push '(:grouptags) org-tag-alist)
  4583. (setq group-flag 'append))
  4584. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4585. ((string-match
  4586. (org-re "\\`\\([[:alnum:]_@#%]+\\)\\(?:(\\(.\\))\\)?\\'") e)
  4587. (let ((tag (match-string 1 e))
  4588. (key (and (match-beginning 2)
  4589. (string-to-char (match-string 2 e)))))
  4590. (cond ((eq group-flag 'append)
  4591. (setcar org-tag-groups-alist
  4592. (append (car org-tag-groups-alist) (list tag))))
  4593. (group-flag (push (list tag) org-tag-groups-alist)))
  4594. (unless (assoc tag org-tag-alist)
  4595. (push (cons tag key) org-tag-alist))))))))
  4596. (setq org-tag-alist (nreverse org-tag-alist)))
  4597. (defun org-file-contents (file &optional noerror)
  4598. "Return the contents of FILE, as a string."
  4599. (if (and file (file-readable-p file))
  4600. (with-temp-buffer
  4601. (insert-file-contents file)
  4602. (buffer-string))
  4603. (funcall (if noerror 'message 'error)
  4604. "Cannot read file \"%s\"%s"
  4605. file
  4606. (let ((from (buffer-file-name (buffer-base-buffer))))
  4607. (if from (concat " (referenced in file \"" from "\")") "")))))
  4608. (defun org-extract-log-state-settings (x)
  4609. "Extract the log state setting from a TODO keyword string.
  4610. This will extract info from a string like \"WAIT(w@/!)\"."
  4611. (let (kw key log1 log2)
  4612. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4613. (setq kw (match-string 1 x)
  4614. key (and (match-end 2) (match-string 2 x))
  4615. log1 (and (match-end 3) (match-string 3 x))
  4616. log2 (and (match-end 4) (match-string 4 x)))
  4617. (and (or log1 log2)
  4618. (list kw
  4619. (and log1 (if (equal log1 "!") 'time 'note))
  4620. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4621. (defun org-remove-keyword-keys (list)
  4622. "Remove a pair of parenthesis at the end of each string in LIST."
  4623. (mapcar (lambda (x)
  4624. (if (string-match "(.*)$" x)
  4625. (substring x 0 (match-beginning 0))
  4626. x))
  4627. list))
  4628. (defun org-assign-fast-keys (alist)
  4629. "Assign fast keys to a keyword-key alist.
  4630. Respect keys that are already there."
  4631. (let (new e (alt ?0))
  4632. (while (setq e (pop alist))
  4633. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4634. (cdr e)) ;; Key already assigned.
  4635. (push e new)
  4636. (let ((clist (string-to-list (downcase (car e))))
  4637. (used (append new alist)))
  4638. (when (= (car clist) ?@)
  4639. (pop clist))
  4640. (while (and clist (rassoc (car clist) used))
  4641. (pop clist))
  4642. (unless clist
  4643. (while (rassoc alt used)
  4644. (incf alt)))
  4645. (push (cons (car e) (or (car clist) alt)) new))))
  4646. (nreverse new)))
  4647. ;;; Some variables used in various places
  4648. (defvar org-window-configuration nil
  4649. "Used in various places to store a window configuration.")
  4650. (defvar org-selected-window nil
  4651. "Used in various places to store a window configuration.")
  4652. (defvar org-finish-function nil
  4653. "Function to be called when `C-c C-c' is used.
  4654. This is for getting out of special buffers like capture.")
  4655. ;; FIXME: Occasionally check by commenting these, to make sure
  4656. ;; no other functions uses these, forgetting to let-bind them.
  4657. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4658. (defvar org-last-state)
  4659. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4660. ;; Defined somewhere in this file, but used before definition.
  4661. (defvar org-entities) ;; defined in org-entities.el
  4662. (defvar org-struct-menu)
  4663. (defvar org-org-menu)
  4664. (defvar org-tbl-menu)
  4665. ;;;; Define the Org-mode
  4666. ;; We use a before-change function to check if a table might need
  4667. ;; an update.
  4668. (defvar org-table-may-need-update t
  4669. "Indicates that a table might need an update.
  4670. This variable is set by `org-before-change-function'.
  4671. `org-table-align' sets it back to nil.")
  4672. (defun org-before-change-function (beg end)
  4673. "Every change indicates that a table might need an update."
  4674. (setq org-table-may-need-update t))
  4675. (defvar org-mode-map)
  4676. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4677. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4678. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4679. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4680. (defvar org-table-buffer-is-an nil)
  4681. (defvar bidi-paragraph-direction)
  4682. (defvar buffer-face-mode-face)
  4683. (require 'outline)
  4684. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4685. (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"))
  4686. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4687. ;; Other stuff we need.
  4688. (require 'time-date)
  4689. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4690. (require 'easymenu)
  4691. (require 'overlay)
  4692. ;; (require 'org-macs) moved higher up in the file before it is first used
  4693. (require 'org-entities)
  4694. ;; (require 'org-compat) moved higher up in the file before it is first used
  4695. (require 'org-faces)
  4696. (require 'org-list)
  4697. (require 'org-pcomplete)
  4698. (require 'org-src)
  4699. (require 'org-footnote)
  4700. (require 'org-macro)
  4701. ;; babel
  4702. (require 'ob)
  4703. ;;;###autoload
  4704. (define-derived-mode org-mode outline-mode "Org"
  4705. "Outline-based notes management and organizer, alias
  4706. \"Carsten's outline-mode for keeping track of everything.\"
  4707. Org-mode develops organizational tasks around a NOTES file which
  4708. contains information about projects as plain text. Org-mode is
  4709. implemented on top of outline-mode, which is ideal to keep the content
  4710. of large files well structured. It supports ToDo items, deadlines and
  4711. time stamps, which magically appear in the diary listing of the Emacs
  4712. calendar. Tables are easily created with a built-in table editor.
  4713. Plain text URL-like links connect to websites, emails (VM), Usenet
  4714. messages (Gnus), BBDB entries, and any files related to the project.
  4715. For printing and sharing of notes, an Org-mode file (or a part of it)
  4716. can be exported as a structured ASCII or HTML file.
  4717. The following commands are available:
  4718. \\{org-mode-map}"
  4719. ;; Get rid of Outline menus, they are not needed
  4720. ;; Need to do this here because define-derived-mode sets up
  4721. ;; the keymap so late. Still, it is a waste to call this each time
  4722. ;; we switch another buffer into org-mode.
  4723. (if (featurep 'xemacs)
  4724. (when (boundp 'outline-mode-menu-heading)
  4725. ;; Assume this is Greg's port, it uses easymenu
  4726. (easy-menu-remove outline-mode-menu-heading)
  4727. (easy-menu-remove outline-mode-menu-show)
  4728. (easy-menu-remove outline-mode-menu-hide))
  4729. (define-key org-mode-map [menu-bar headings] 'undefined)
  4730. (define-key org-mode-map [menu-bar hide] 'undefined)
  4731. (define-key org-mode-map [menu-bar show] 'undefined))
  4732. (org-load-modules-maybe)
  4733. (easy-menu-add org-org-menu)
  4734. (easy-menu-add org-tbl-menu)
  4735. (org-install-agenda-files-menu)
  4736. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4737. (add-to-invisibility-spec '(org-cwidth))
  4738. (add-to-invisibility-spec '(org-hide-block . t))
  4739. (when (featurep 'xemacs)
  4740. (org-set-local 'line-move-ignore-invisible t))
  4741. (org-set-local 'outline-regexp org-outline-regexp)
  4742. (org-set-local 'outline-level 'org-outline-level)
  4743. (setq bidi-paragraph-direction 'left-to-right)
  4744. (when (and org-ellipsis
  4745. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4746. (fboundp 'make-glyph-code))
  4747. (unless org-display-table
  4748. (setq org-display-table (make-display-table)))
  4749. (set-display-table-slot
  4750. org-display-table 4
  4751. (vconcat (mapcar
  4752. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4753. org-ellipsis)))
  4754. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4755. (setq buffer-display-table org-display-table))
  4756. (org-set-regexps-and-options)
  4757. (org-set-font-lock-defaults)
  4758. (when (and org-tag-faces (not org-tags-special-faces-re))
  4759. ;; tag faces set outside customize.... force initialization.
  4760. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4761. ;; Calc embedded
  4762. (org-set-local 'calc-embedded-open-mode "# ")
  4763. ;; Modify a few syntax entries
  4764. (modify-syntax-entry ?@ "w")
  4765. (modify-syntax-entry ?\" "\"")
  4766. (modify-syntax-entry ?\\ "_")
  4767. (modify-syntax-entry ?~ "_")
  4768. (if org-startup-truncated (setq truncate-lines t))
  4769. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4770. (org-set-local 'font-lock-unfontify-region-function
  4771. 'org-unfontify-region)
  4772. ;; Activate before-change-function
  4773. (org-set-local 'org-table-may-need-update t)
  4774. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4775. 'local)
  4776. ;; Check for running clock before killing a buffer
  4777. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4778. ;; Initialize macros templates.
  4779. (org-macro-initialize-templates)
  4780. ;; Initialize radio targets.
  4781. (org-update-radio-target-regexp)
  4782. ;; Indentation.
  4783. (org-set-local 'indent-line-function 'org-indent-line)
  4784. (org-set-local 'indent-region-function 'org-indent-region)
  4785. ;; Filling and auto-filling.
  4786. (org-setup-filling)
  4787. ;; Comments.
  4788. (org-setup-comments-handling)
  4789. ;; Initialize cache.
  4790. (org-element-cache-reset)
  4791. ;; Beginning/end of defun
  4792. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4793. (org-set-local 'end-of-defun-function
  4794. (lambda ()
  4795. (if (not (org-at-heading-p))
  4796. (org-forward-element)
  4797. (org-forward-element)
  4798. (forward-char -1))))
  4799. ;; Next error for sparse trees
  4800. (org-set-local 'next-error-function 'org-occur-next-match)
  4801. ;; Make sure dependence stuff works reliably, even for users who set it
  4802. ;; too late :-(
  4803. (if org-enforce-todo-dependencies
  4804. (add-hook 'org-blocker-hook
  4805. 'org-block-todo-from-children-or-siblings-or-parent)
  4806. (remove-hook 'org-blocker-hook
  4807. 'org-block-todo-from-children-or-siblings-or-parent))
  4808. (if org-enforce-todo-checkbox-dependencies
  4809. (add-hook 'org-blocker-hook
  4810. 'org-block-todo-from-checkboxes)
  4811. (remove-hook 'org-blocker-hook
  4812. 'org-block-todo-from-checkboxes))
  4813. ;; Align options lines
  4814. (org-set-local
  4815. 'align-mode-rules-list
  4816. '((org-in-buffer-settings
  4817. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4818. (modes . '(org-mode)))))
  4819. ;; Imenu
  4820. (org-set-local 'imenu-create-index-function
  4821. 'org-imenu-get-tree)
  4822. ;; Make isearch reveal context
  4823. (if (or (featurep 'xemacs)
  4824. (not (boundp 'outline-isearch-open-invisible-function)))
  4825. ;; Emacs 21 and XEmacs make use of the hook
  4826. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4827. ;; Emacs 22 deals with this through a special variable
  4828. (org-set-local 'outline-isearch-open-invisible-function
  4829. (lambda (&rest ignore) (org-show-context 'isearch))))
  4830. ;; Setup the pcomplete hooks
  4831. (set (make-local-variable 'pcomplete-command-completion-function)
  4832. 'org-pcomplete-initial)
  4833. (set (make-local-variable 'pcomplete-command-name-function)
  4834. 'org-command-at-point)
  4835. (set (make-local-variable 'pcomplete-default-completion-function)
  4836. 'ignore)
  4837. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4838. 'org-parse-arguments)
  4839. (set (make-local-variable 'pcomplete-termination-string) "")
  4840. (when (>= emacs-major-version 23)
  4841. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4842. ;; If empty file that did not turn on org-mode automatically, make it to.
  4843. (if (and org-insert-mode-line-in-empty-file
  4844. (org-called-interactively-p 'any)
  4845. (= (point-min) (point-max)))
  4846. (insert "# -*- mode: org -*-\n\n"))
  4847. (unless org-inhibit-startup
  4848. (org-unmodified
  4849. (and org-startup-with-beamer-mode (org-beamer-mode))
  4850. (when org-startup-align-all-tables
  4851. (org-table-map-tables 'org-table-align 'quietly))
  4852. (when org-startup-with-inline-images
  4853. (org-display-inline-images))
  4854. (when org-startup-with-latex-preview
  4855. (org-toggle-latex-fragment))
  4856. (unless org-inhibit-startup-visibility-stuff
  4857. (org-set-startup-visibility))
  4858. (org-refresh-effort-properties)))
  4859. ;; Try to set org-hide correctly
  4860. (let ((foreground (org-find-invisible-foreground)))
  4861. (if foreground
  4862. (set-face-foreground 'org-hide foreground))))
  4863. ;; Update `customize-package-emacs-version-alist'
  4864. (add-to-list 'customize-package-emacs-version-alist
  4865. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4866. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4867. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4868. (defvar org-mode-transpose-word-syntax-table
  4869. (let ((st (make-syntax-table text-mode-syntax-table)))
  4870. (mapc (lambda(c) (modify-syntax-entry
  4871. (string-to-char (car c)) "w p" st))
  4872. org-emphasis-alist)
  4873. st))
  4874. (when (fboundp 'abbrev-table-put)
  4875. (abbrev-table-put org-mode-abbrev-table
  4876. :parents (list text-mode-abbrev-table)))
  4877. (defun org-find-invisible-foreground ()
  4878. (let ((candidates (remove
  4879. "unspecified-bg"
  4880. (nconc
  4881. (list (face-background 'default)
  4882. (face-background 'org-default))
  4883. (mapcar
  4884. (lambda (alist)
  4885. (when (boundp alist)
  4886. (cdr (assoc 'background-color (symbol-value alist)))))
  4887. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4888. (list (face-foreground 'org-hide))))))
  4889. (car (remove nil candidates))))
  4890. (defun org-current-time (&optional rounding-minutes past)
  4891. "Current time, possibly rounded to ROUNDING-MINUTES.
  4892. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4893. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4894. the rounding returns a past time."
  4895. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4896. (car org-time-stamp-rounding-minutes)))
  4897. (time (decode-time)) res)
  4898. (if (< r 1)
  4899. (current-time)
  4900. (setq res
  4901. (apply 'encode-time
  4902. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4903. (nthcdr 2 time))))
  4904. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4905. (seconds-to-time (- (org-float-time res) (* r 60)))
  4906. res))))
  4907. (defun org-today ()
  4908. "Return today date, considering `org-extend-today-until'."
  4909. (time-to-days
  4910. (time-subtract (current-time)
  4911. (list 0 (* 3600 org-extend-today-until) 0))))
  4912. ;;;; Font-Lock stuff, including the activators
  4913. (defvar org-mouse-map (make-sparse-keymap))
  4914. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4915. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4916. (when org-mouse-1-follows-link
  4917. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4918. (when org-tab-follows-link
  4919. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4920. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4921. (require 'font-lock)
  4922. (defconst org-non-link-chars "]\t\n\r<>")
  4923. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4924. "file+sys" "news" "shell" "elisp" "doi" "message"
  4925. "help"))
  4926. (defvar org-link-types-re nil
  4927. "Matches a link that has a url-like prefix like \"http:\"")
  4928. (defvar org-link-re-with-space nil
  4929. "Matches a link with spaces, optional angular brackets around it.")
  4930. (defvar org-link-re-with-space2 nil
  4931. "Matches a link with spaces, optional angular brackets around it.")
  4932. (defvar org-link-re-with-space3 nil
  4933. "Matches a link with spaces, only for internal part in bracket links.")
  4934. (defvar org-angle-link-re nil
  4935. "Matches link with angular brackets, spaces are allowed.")
  4936. (defvar org-plain-link-re nil
  4937. "Matches plain link, without spaces.")
  4938. (defvar org-bracket-link-regexp nil
  4939. "Matches a link in double brackets.")
  4940. (defvar org-bracket-link-analytic-regexp nil
  4941. "Regular expression used to analyze links.
  4942. Here is what the match groups contain after a match:
  4943. 1: http:
  4944. 2: http
  4945. 3: path
  4946. 4: [desc]
  4947. 5: desc")
  4948. (defvar org-bracket-link-analytic-regexp++ nil
  4949. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4950. (defvar org-any-link-re nil
  4951. "Regular expression matching any link.")
  4952. (defconst org-match-sexp-depth 3
  4953. "Number of stacked braces for sub/superscript matching.")
  4954. (defun org-create-multibrace-regexp (left right n)
  4955. "Create a regular expression which will match a balanced sexp.
  4956. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4957. as single character strings.
  4958. The regexp returned will match the entire expression including the
  4959. delimiters. It will also define a single group which contains the
  4960. match except for the outermost delimiters. The maximum depth of
  4961. stacked delimiters is N. Escaping delimiters is not possible."
  4962. (let* ((nothing (concat "[^" left right "]*?"))
  4963. (or "\\|")
  4964. (re nothing)
  4965. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4966. (while (> n 1)
  4967. (setq n (1- n)
  4968. re (concat re or next)
  4969. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4970. (concat left "\\(" re "\\)" right)))
  4971. (defconst org-match-substring-regexp
  4972. (concat
  4973. "\\(\\S-\\)\\([_^]\\)\\("
  4974. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4975. "\\|"
  4976. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4977. "\\|"
  4978. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4979. "The regular expression matching a sub- or superscript.")
  4980. (defconst org-match-substring-with-braces-regexp
  4981. (concat
  4982. "\\(\\S-\\)\\([_^]\\)"
  4983. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4984. "The regular expression matching a sub- or superscript, forcing braces.")
  4985. (defun org-make-link-regexps ()
  4986. "Update the link regular expressions.
  4987. This should be called after the variable `org-link-types' has changed."
  4988. (let ((types-re (regexp-opt org-link-types t)))
  4989. (setq org-link-types-re
  4990. (concat "\\`" types-re ":")
  4991. org-link-re-with-space
  4992. (concat "<?" types-re ":"
  4993. "\\([^" org-non-link-chars " ]"
  4994. "[^" org-non-link-chars "]*"
  4995. "[^" org-non-link-chars " ]\\)>?")
  4996. org-link-re-with-space2
  4997. (concat "<?" types-re ":"
  4998. "\\([^" org-non-link-chars " ]"
  4999. "[^\t\n\r]*"
  5000. "[^" org-non-link-chars " ]\\)>?")
  5001. org-link-re-with-space3
  5002. (concat "<?" types-re ":"
  5003. "\\([^" org-non-link-chars " ]"
  5004. "[^\t\n\r]*\\)")
  5005. org-angle-link-re
  5006. (concat "<" types-re ":"
  5007. "\\([^" org-non-link-chars " ]"
  5008. "[^" org-non-link-chars "]*"
  5009. "\\)>")
  5010. org-plain-link-re
  5011. (concat
  5012. "\\<" types-re ":"
  5013. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5014. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5015. org-bracket-link-regexp
  5016. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5017. org-bracket-link-analytic-regexp
  5018. (concat
  5019. "\\[\\["
  5020. "\\(" types-re ":\\)?"
  5021. "\\([^]]+\\)"
  5022. "\\]"
  5023. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5024. "\\]")
  5025. org-bracket-link-analytic-regexp++
  5026. (concat
  5027. "\\[\\["
  5028. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5029. "\\([^]]+\\)"
  5030. "\\]"
  5031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5032. "\\]")
  5033. org-any-link-re
  5034. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5035. org-angle-link-re "\\)\\|\\("
  5036. org-plain-link-re "\\)"))))
  5037. (org-make-link-regexps)
  5038. (defvar org-emph-face nil)
  5039. (defun org-do-emphasis-faces (limit)
  5040. "Run through the buffer and emphasize strings."
  5041. (let (rtn a)
  5042. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5043. (let* ((border (char-after (match-beginning 3)))
  5044. (bre (regexp-quote (char-to-string border))))
  5045. (if (and (not (= border (char-after (match-beginning 4))))
  5046. (not (save-match-data
  5047. (string-match (concat bre ".*" bre)
  5048. (replace-regexp-in-string
  5049. "\n" " "
  5050. (substring (match-string 2) 1 -1))))))
  5051. (progn
  5052. (setq rtn t)
  5053. (setq a (assoc (match-string 3) org-emphasis-alist))
  5054. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5055. 'face
  5056. (nth 1 a))
  5057. (and (nth 2 a)
  5058. (org-remove-flyspell-overlays-in
  5059. (match-beginning 0) (match-end 0)))
  5060. (add-text-properties (match-beginning 2) (match-end 2)
  5061. '(font-lock-multiline t org-emphasis t))
  5062. (when org-hide-emphasis-markers
  5063. (add-text-properties (match-end 4) (match-beginning 5)
  5064. '(invisible org-link))
  5065. (add-text-properties (match-beginning 3) (match-end 3)
  5066. '(invisible org-link))))))
  5067. (goto-char (1+ (match-beginning 0))))
  5068. rtn))
  5069. (defun org-emphasize (&optional char)
  5070. "Insert or change an emphasis, i.e. a font like bold or italic.
  5071. If there is an active region, change that region to a new emphasis.
  5072. If there is no region, just insert the marker characters and position
  5073. the cursor between them.
  5074. CHAR should be the marker character. If it is a space, it means to
  5075. remove the emphasis of the selected region.
  5076. If CHAR is not given (for example in an interactive call) it will be
  5077. prompted for."
  5078. (interactive)
  5079. (let ((erc org-emphasis-regexp-components)
  5080. (prompt "")
  5081. (string "") beg end move c s)
  5082. (if (org-region-active-p)
  5083. (setq beg (region-beginning) end (region-end)
  5084. string (buffer-substring beg end))
  5085. (setq move t))
  5086. (unless char
  5087. (message "Emphasis marker or tag: [%s]"
  5088. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5089. (setq char (read-char-exclusive)))
  5090. (if (equal char ?\ )
  5091. (setq s "" move nil)
  5092. (unless (assoc (char-to-string char) org-emphasis-alist)
  5093. (user-error "No such emphasis marker: \"%c\"" char))
  5094. (setq s (char-to-string char)))
  5095. (while (and (> (length string) 1)
  5096. (equal (substring string 0 1) (substring string -1))
  5097. (assoc (substring string 0 1) org-emphasis-alist))
  5098. (setq string (substring string 1 -1)))
  5099. (setq string (concat s string s))
  5100. (if beg (delete-region beg end))
  5101. (unless (or (bolp)
  5102. (string-match (concat "[" (nth 0 erc) "\n]")
  5103. (char-to-string (char-before (point)))))
  5104. (insert " "))
  5105. (unless (or (eobp)
  5106. (string-match (concat "[" (nth 1 erc) "\n]")
  5107. (char-to-string (char-after (point)))))
  5108. (insert " ") (backward-char 1))
  5109. (insert string)
  5110. (and move (backward-char 1))))
  5111. (defconst org-nonsticky-props
  5112. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5113. (defsubst org-rear-nonsticky-at (pos)
  5114. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5115. (defun org-activate-plain-links (limit)
  5116. "Add link properties for plain links."
  5117. (let (f hl)
  5118. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5119. (not (member 'org-tag
  5120. (get-text-property (1- (match-beginning 0)) 'face)))
  5121. (not (org-in-src-block-p)))
  5122. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5123. (setq f (get-text-property (match-beginning 0) 'face))
  5124. (setq hl (org-match-string-no-properties 0))
  5125. (if (or (eq f 'org-tag)
  5126. (and (listp f) (memq 'org-tag f)))
  5127. nil
  5128. (add-text-properties (match-beginning 0) (match-end 0)
  5129. (list 'mouse-face 'highlight
  5130. 'face 'org-link
  5131. 'htmlize-link `(:uri ,hl)
  5132. 'keymap org-mouse-map))
  5133. (org-rear-nonsticky-at (match-end 0)))
  5134. t)))
  5135. (defun org-activate-code (limit)
  5136. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5137. (progn
  5138. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5139. (remove-text-properties (match-beginning 0) (match-end 0)
  5140. '(display t invisible t intangible t))
  5141. t)))
  5142. (defcustom org-src-fontify-natively t
  5143. "When non-nil, fontify code in code blocks."
  5144. :type 'boolean
  5145. :version "24.4"
  5146. :package-version '(Org . "8.3")
  5147. :group 'org-appearance
  5148. :group 'org-babel)
  5149. (defcustom org-allow-promoting-top-level-subtree nil
  5150. "When non-nil, allow promoting a top level subtree.
  5151. The leading star of the top level headline will be replaced
  5152. by a #."
  5153. :type 'boolean
  5154. :version "24.1"
  5155. :group 'org-appearance)
  5156. (defun org-fontify-meta-lines-and-blocks (limit)
  5157. (condition-case nil
  5158. (org-fontify-meta-lines-and-blocks-1 limit)
  5159. (error (message "org-mode fontification error"))))
  5160. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5161. "Fontify #+ lines and blocks."
  5162. (let ((case-fold-search t))
  5163. (if (re-search-forward
  5164. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5165. limit t)
  5166. (let ((beg (match-beginning 0))
  5167. (block-start (match-end 0))
  5168. (block-end nil)
  5169. (lang (match-string 7))
  5170. (beg1 (line-beginning-position 2))
  5171. (dc1 (downcase (match-string 2)))
  5172. (dc3 (downcase (match-string 3)))
  5173. end end1 quoting block-type ovl)
  5174. (cond
  5175. ((and (match-end 4) (equal dc3 "+begin"))
  5176. ;; Truly a block
  5177. (setq block-type (downcase (match-string 5))
  5178. quoting (member block-type org-protecting-blocks))
  5179. (when (re-search-forward
  5180. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5181. nil t) ;; on purpose, we look further than LIMIT
  5182. (setq end (min (point-max) (match-end 0))
  5183. end1 (min (point-max) (1- (match-beginning 0))))
  5184. (setq block-end (match-beginning 0))
  5185. (when quoting
  5186. (org-remove-flyspell-overlays-in beg1 end1)
  5187. (remove-text-properties beg end
  5188. '(display t invisible t intangible t)))
  5189. (add-text-properties
  5190. beg end '(font-lock-fontified t font-lock-multiline t))
  5191. (add-text-properties beg beg1 '(face org-meta-line))
  5192. (org-remove-flyspell-overlays-in beg beg1)
  5193. (add-text-properties ; For end_src
  5194. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5195. (org-remove-flyspell-overlays-in end1 end)
  5196. (cond
  5197. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5198. (org-src-font-lock-fontify-block lang block-start block-end)
  5199. (add-text-properties beg1 block-end '(src-block t)))
  5200. (quoting
  5201. (add-text-properties beg1 (min (point-max) (1+ end1))
  5202. '(face org-block))) ; end of source block
  5203. ((not org-fontify-quote-and-verse-blocks))
  5204. ((string= block-type "quote")
  5205. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5206. ((string= block-type "verse")
  5207. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5208. (add-text-properties beg beg1 '(face org-block-begin-line))
  5209. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5210. '(face org-block-end-line))
  5211. t))
  5212. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5213. (org-remove-flyspell-overlays-in
  5214. (match-beginning 0)
  5215. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5216. (add-text-properties
  5217. beg (match-end 3)
  5218. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5219. '(font-lock-fontified t invisible t)
  5220. '(font-lock-fontified t face org-document-info-keyword)))
  5221. (add-text-properties
  5222. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5223. (if (string-equal dc1 "+title:")
  5224. '(font-lock-fontified t face org-document-title)
  5225. '(font-lock-fontified t face org-document-info))))
  5226. ((equal dc1 "+caption:")
  5227. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5228. (remove-text-properties (match-beginning 0) (match-end 0)
  5229. '(display t invisible t intangible t))
  5230. (add-text-properties (match-beginning 1) (match-end 3)
  5231. '(font-lock-fontified t face org-meta-line))
  5232. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5233. '(font-lock-fontified t face org-block))
  5234. t)
  5235. ((member dc3 '(" " ""))
  5236. (org-remove-flyspell-overlays-in beg (match-end 0))
  5237. (add-text-properties
  5238. beg (match-end 0)
  5239. '(font-lock-fontified t face font-lock-comment-face)))
  5240. (t ;; just any other in-buffer setting, but not indented
  5241. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5242. (remove-text-properties (match-beginning 0) (match-end 0)
  5243. '(display t invisible t intangible t))
  5244. (add-text-properties beg (match-end 0)
  5245. '(font-lock-fontified t face org-meta-line))
  5246. t))))))
  5247. (defun org-fontify-drawers (limit)
  5248. "Fontify drawers."
  5249. (when (re-search-forward org-drawer-regexp limit t)
  5250. (add-text-properties
  5251. (match-beginning 0) (match-end 0)
  5252. '(font-lock-fontified t face org-special-keyword))
  5253. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5254. t))
  5255. (defun org-fontify-macros (limit)
  5256. "Fontify macros."
  5257. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5258. (add-text-properties
  5259. (match-beginning 0) (match-end 0)
  5260. '(font-lock-fontified t face org-macro))
  5261. (when org-hide-macro-markers
  5262. (add-text-properties (match-end 2) (match-beginning 2)
  5263. '(invisible t))
  5264. (add-text-properties (match-beginning 1) (match-end 1)
  5265. '(invisible t)))
  5266. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5267. t))
  5268. (defun org-activate-angle-links (limit)
  5269. "Add text properties for angle links."
  5270. (if (and (re-search-forward org-angle-link-re limit t)
  5271. (not (org-in-src-block-p)))
  5272. (progn
  5273. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5274. (add-text-properties (match-beginning 0) (match-end 0)
  5275. (list 'mouse-face 'highlight
  5276. 'keymap org-mouse-map))
  5277. (org-rear-nonsticky-at (match-end 0))
  5278. t)))
  5279. (defun org-activate-footnote-links (limit)
  5280. "Add text properties for footnotes."
  5281. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5282. (when fn
  5283. (let* ((beg (nth 1 fn))
  5284. (end (nth 2 fn))
  5285. (label (car fn))
  5286. (referencep (/= (line-beginning-position) beg)))
  5287. (when (and referencep (nth 3 fn))
  5288. (save-excursion
  5289. (goto-char beg)
  5290. (search-forward (or label "fn:"))
  5291. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5292. (add-text-properties beg end
  5293. (list 'mouse-face 'highlight
  5294. 'keymap org-mouse-map
  5295. 'help-echo
  5296. (if referencep "Footnote reference"
  5297. "Footnote definition")
  5298. 'font-lock-fontified t
  5299. 'font-lock-multiline t
  5300. 'face 'org-footnote))))))
  5301. (defun org-activate-bracket-links (limit)
  5302. "Add text properties for bracketed links."
  5303. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5304. (not (org-in-src-block-p)))
  5305. (let* ((hl (org-match-string-no-properties 1))
  5306. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5307. (ip (org-maybe-intangible
  5308. (list 'invisible 'org-link
  5309. 'keymap org-mouse-map 'mouse-face 'highlight
  5310. 'font-lock-multiline t 'help-echo help
  5311. 'htmlize-link `(:uri ,hl))))
  5312. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5313. 'font-lock-multiline t 'help-echo help
  5314. 'htmlize-link `(:uri ,hl))))
  5315. ;; We need to remove the invisible property here. Table narrowing
  5316. ;; may have made some of this invisible.
  5317. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5318. (remove-text-properties (match-beginning 0) (match-end 0)
  5319. '(invisible nil))
  5320. (if (match-end 3)
  5321. (progn
  5322. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5323. (org-rear-nonsticky-at (match-beginning 3))
  5324. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5325. (org-rear-nonsticky-at (match-end 3))
  5326. (add-text-properties (match-end 3) (match-end 0) ip)
  5327. (org-rear-nonsticky-at (match-end 0)))
  5328. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5329. (org-rear-nonsticky-at (match-beginning 1))
  5330. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5331. (org-rear-nonsticky-at (match-end 1))
  5332. (add-text-properties (match-end 1) (match-end 0) ip)
  5333. (org-rear-nonsticky-at (match-end 0)))
  5334. t)))
  5335. (defun org-activate-dates (limit)
  5336. "Add text properties for dates."
  5337. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5338. (not (equal (char-before (match-beginning 0)) 91)))
  5339. (progn
  5340. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5341. (add-text-properties (match-beginning 0) (match-end 0)
  5342. (list 'mouse-face 'highlight
  5343. 'keymap org-mouse-map))
  5344. (org-rear-nonsticky-at (match-end 0))
  5345. (when org-display-custom-times
  5346. (if (match-end 3)
  5347. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5348. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5349. t)))
  5350. (defvar org-target-link-regexp nil
  5351. "Regular expression matching radio targets in plain text.")
  5352. (make-variable-buffer-local 'org-target-link-regexp)
  5353. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5354. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5355. border border border))
  5356. "Regular expression matching a link target.")
  5357. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5358. "Regular expression matching a radio target.")
  5359. (defconst org-any-target-regexp
  5360. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5361. "Regular expression matching any target.")
  5362. (defun org-activate-target-links (limit)
  5363. "Add text properties for target matches."
  5364. (when org-target-link-regexp
  5365. (let ((case-fold-search t))
  5366. (if (re-search-forward org-target-link-regexp limit t)
  5367. (progn
  5368. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5369. (add-text-properties (match-beginning 1) (match-end 1)
  5370. (list 'mouse-face 'highlight
  5371. 'keymap org-mouse-map
  5372. 'help-echo "Radio target link"
  5373. 'org-linked-text t))
  5374. (org-rear-nonsticky-at (match-end 1))
  5375. t)))))
  5376. (defun org-update-radio-target-regexp ()
  5377. "Find all radio targets in this file and update the regular expression.
  5378. Also refresh fontification if needed."
  5379. (interactive)
  5380. (let ((old-regexp org-target-link-regexp)
  5381. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5382. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5383. (targets
  5384. (org-with-wide-buffer
  5385. (goto-char (point-min))
  5386. (let (rtn)
  5387. (while (re-search-forward org-radio-target-regexp nil t)
  5388. ;; Make sure point is really within the object.
  5389. (backward-char)
  5390. (let ((obj (org-element-context)))
  5391. (when (eq (org-element-type obj) 'radio-target)
  5392. (add-to-list 'rtn (org-element-property :value obj)))))
  5393. rtn))))
  5394. (setq org-target-link-regexp
  5395. (and targets
  5396. (concat before-re
  5397. (mapconcat
  5398. (lambda (x)
  5399. (replace-regexp-in-string
  5400. " +" "\\s-+" (regexp-quote x) t t))
  5401. targets
  5402. "\\|")
  5403. after-re)))
  5404. (unless (equal old-regexp org-target-link-regexp)
  5405. ;; Clean-up cache.
  5406. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5407. ((not org-target-link-regexp) old-regexp)
  5408. (t
  5409. (concat before-re
  5410. (mapconcat
  5411. (lambda (re)
  5412. (substring re (length before-re)
  5413. (- (length after-re))))
  5414. (list old-regexp org-target-link-regexp)
  5415. "\\|")
  5416. after-re)))))
  5417. (org-with-wide-buffer
  5418. (goto-char (point-min))
  5419. (while (re-search-forward regexp nil t)
  5420. (org-element-cache-refresh (match-beginning 1)))))
  5421. ;; Re fontify buffer.
  5422. (when (memq 'radio org-highlight-links)
  5423. (org-restart-font-lock)))))
  5424. (defun org-hide-wide-columns (limit)
  5425. (let (s e)
  5426. (setq s (text-property-any (point) (or limit (point-max))
  5427. 'org-cwidth t))
  5428. (when s
  5429. (setq e (next-single-property-change s 'org-cwidth))
  5430. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5431. (goto-char e)
  5432. t)))
  5433. (defvar org-latex-and-related-regexp nil
  5434. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5435. (defun org-compute-latex-and-related-regexp ()
  5436. "Compute regular expression for LaTeX, entities and sub/superscript.
  5437. Result depends on variable `org-highlight-latex-and-related'."
  5438. (org-set-local
  5439. 'org-latex-and-related-regexp
  5440. (let* ((re-sub
  5441. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5442. ((eq org-use-sub-superscripts '{})
  5443. (list org-match-substring-with-braces-regexp))
  5444. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5445. (re-latex
  5446. (when (memq 'latex org-highlight-latex-and-related)
  5447. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5448. (delq nil
  5449. (mapcar (lambda (x)
  5450. (and (member (car x) matchers) (nth 1 x)))
  5451. org-latex-regexps)))))
  5452. (re-entities
  5453. (when (memq 'entities org-highlight-latex-and-related)
  5454. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5455. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5456. (defun org-do-latex-and-related (limit)
  5457. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5458. LIMIT bounds the search for syntax to highlight. Stop at first
  5459. highlighted object, if any. Return t if some highlighting was
  5460. done, nil otherwise."
  5461. (when (org-string-nw-p org-latex-and-related-regexp)
  5462. (catch 'found
  5463. (while (re-search-forward org-latex-and-related-regexp limit t)
  5464. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5465. 'face))
  5466. '(org-code org-verbatim underline))
  5467. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5468. '(?_ ?^))
  5469. 1
  5470. 0)))
  5471. (font-lock-prepend-text-property
  5472. (+ offset (match-beginning 0)) (match-end 0)
  5473. 'face 'org-latex-and-related)
  5474. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5475. '(font-lock-multiline t)))
  5476. (throw 'found t)))
  5477. nil)))
  5478. (defun org-restart-font-lock ()
  5479. "Restart `font-lock-mode', to force refontification."
  5480. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5481. (font-lock-mode -1)
  5482. (font-lock-mode 1)))
  5483. (defun org-activate-tags (limit)
  5484. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5485. (progn
  5486. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5487. (add-text-properties (match-beginning 1) (match-end 1)
  5488. (list 'mouse-face 'highlight
  5489. 'keymap org-mouse-map))
  5490. (org-rear-nonsticky-at (match-end 1))
  5491. t)))
  5492. (defun org-outline-level ()
  5493. "Compute the outline level of the heading at point.
  5494. If this is called at a normal headline, the level is the number
  5495. of stars. Use `org-reduced-level' to remove the effect of
  5496. `org-odd-levels'. Unlike to `org-current-level', this function
  5497. takes into consideration inlinetasks."
  5498. (org-with-wide-buffer
  5499. (end-of-line)
  5500. (if (re-search-backward org-outline-regexp-bol nil t)
  5501. (1- (- (match-end 0) (match-beginning 0)))
  5502. 0)))
  5503. (defvar org-font-lock-keywords nil)
  5504. (defsubst org-re-property (property &optional literal allow-null)
  5505. "Return a regexp matching a PROPERTY line.
  5506. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5507. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5508. non-nil, match properties even without a value.
  5509. Match group 3 is set to the value when it exists. If there is no
  5510. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5511. (concat
  5512. "^\\(?4:[ \t]*\\)"
  5513. (format "\\(?1::\\(?2:%s\\):\\)"
  5514. (if literal property (regexp-quote property)))
  5515. (if allow-null
  5516. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5517. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5518. (defconst org-property-re
  5519. (org-re-property "\\S-+" 'literal t)
  5520. "Regular expression matching a property line.
  5521. There are four matching groups:
  5522. 1: :PROPKEY: including the leading and trailing colon,
  5523. 2: PROPKEY without the leading and trailing colon,
  5524. 3: PROPVAL without leading or trailing spaces,
  5525. 4: the indentation of the current line,
  5526. 5: trailing whitespace.")
  5527. (defvar org-font-lock-hook nil
  5528. "Functions to be called for special font lock stuff.")
  5529. (defvar org-font-lock-set-keywords-hook nil
  5530. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5531. This is called after `org-font-lock-extra-keywords' is defined, but before
  5532. it is installed to be used by font lock. This can be useful if something
  5533. needs to be inserted at a specific position in the font-lock sequence.")
  5534. (defun org-font-lock-hook (limit)
  5535. "Run `org-font-lock-hook' within LIMIT."
  5536. (run-hook-with-args 'org-font-lock-hook limit))
  5537. (defun org-set-font-lock-defaults ()
  5538. "Set font lock defaults for the current buffer."
  5539. (let* ((em org-fontify-emphasized-text)
  5540. (lk org-highlight-links)
  5541. (org-font-lock-extra-keywords
  5542. (list
  5543. ;; Call the hook
  5544. '(org-font-lock-hook)
  5545. ;; Headlines
  5546. `(,(if org-fontify-whole-heading-line
  5547. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5548. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5549. (1 (org-get-level-face 1))
  5550. (2 (org-get-level-face 2))
  5551. (3 (org-get-level-face 3)))
  5552. ;; Table lines
  5553. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5554. (1 'org-table t))
  5555. ;; Table internals
  5556. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5557. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5558. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5559. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5560. ;; Drawers
  5561. '(org-fontify-drawers)
  5562. ;; Properties
  5563. (list org-property-re
  5564. '(1 'org-special-keyword t)
  5565. '(3 'org-property-value t))
  5566. ;; Links
  5567. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5568. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5569. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5570. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5571. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5572. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5573. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5574. ;; Targets.
  5575. (list org-any-target-regexp '(0 'org-target t))
  5576. ;; Diary sexps.
  5577. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5578. ;; Macro
  5579. '(org-fontify-macros)
  5580. '(org-hide-wide-columns (0 nil append))
  5581. ;; TODO keyword
  5582. (list (format org-heading-keyword-regexp-format
  5583. org-todo-regexp)
  5584. '(2 (org-get-todo-face 2) t))
  5585. ;; DONE
  5586. (if org-fontify-done-headline
  5587. (list (format org-heading-keyword-regexp-format
  5588. (concat
  5589. "\\(?:"
  5590. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5591. "\\)"))
  5592. '(2 'org-headline-done t))
  5593. nil)
  5594. ;; Priorities
  5595. '(org-font-lock-add-priority-faces)
  5596. ;; Tags
  5597. '(org-font-lock-add-tag-faces)
  5598. ;; Tags groups
  5599. (if (and org-group-tags org-tag-groups-alist)
  5600. (list (concat org-outline-regexp-bol ".+\\(:"
  5601. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5602. ":\\).*$")
  5603. '(1 'org-tag-group prepend)))
  5604. ;; Special keywords
  5605. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5606. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5607. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5608. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5609. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5610. ;; Emphasis
  5611. (if em
  5612. (if (featurep 'xemacs)
  5613. '(org-do-emphasis-faces (0 nil append))
  5614. '(org-do-emphasis-faces)))
  5615. ;; Checkboxes
  5616. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5617. 1 'org-checkbox prepend)
  5618. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5619. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5620. (0 (org-get-checkbox-statistics-face) t)))
  5621. ;; Description list items
  5622. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5623. 1 'org-list-dt prepend)
  5624. ;; ARCHIVEd headings
  5625. (list (concat
  5626. org-outline-regexp-bol
  5627. "\\(.*:" org-archive-tag ":.*\\)")
  5628. '(1 'org-archived prepend))
  5629. ;; Specials
  5630. '(org-do-latex-and-related)
  5631. '(org-fontify-entities)
  5632. '(org-raise-scripts)
  5633. ;; Code
  5634. '(org-activate-code (1 'org-code t))
  5635. ;; COMMENT
  5636. (list (format
  5637. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5638. org-todo-regexp
  5639. org-comment-string)
  5640. '(9 'org-special-keyword t))
  5641. ;; Blocks and meta lines
  5642. '(org-fontify-meta-lines-and-blocks))))
  5643. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5644. (run-hooks 'org-font-lock-set-keywords-hook)
  5645. ;; Now set the full font-lock-keywords
  5646. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5647. (org-set-local 'font-lock-defaults
  5648. '(org-font-lock-keywords t nil nil backward-paragraph))
  5649. (kill-local-variable 'font-lock-keywords) nil))
  5650. (defun org-toggle-pretty-entities ()
  5651. "Toggle the composition display of entities as UTF8 characters."
  5652. (interactive)
  5653. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5654. (org-restart-font-lock)
  5655. (if org-pretty-entities
  5656. (message "Entities are now displayed as UTF8 characters")
  5657. (save-restriction
  5658. (widen)
  5659. (org-decompose-region (point-min) (point-max))
  5660. (message "Entities are now displayed as plain text"))))
  5661. (defvar org-custom-properties-overlays nil
  5662. "List of overlays used for custom properties.")
  5663. (make-variable-buffer-local 'org-custom-properties-overlays)
  5664. (defun org-toggle-custom-properties-visibility ()
  5665. "Display or hide properties in `org-custom-properties'."
  5666. (interactive)
  5667. (if org-custom-properties-overlays
  5668. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5669. (setq org-custom-properties-overlays nil))
  5670. (when org-custom-properties
  5671. (org-with-wide-buffer
  5672. (goto-char (point-min))
  5673. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5674. (while (re-search-forward regexp nil t)
  5675. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5676. (when (and end (< (point) end))
  5677. ;; Hide first custom property in current drawer.
  5678. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5679. (overlay-put o 'invisible t)
  5680. (overlay-put o 'org-custom-property t)
  5681. (push o org-custom-properties-overlays))
  5682. ;; Hide additional custom properties in the same drawer.
  5683. (while (re-search-forward regexp end t)
  5684. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5685. (overlay-put o 'invisible t)
  5686. (overlay-put o 'org-custom-property t)
  5687. (push o org-custom-properties-overlays)))))
  5688. ;; Each entry is limited to a single property drawer.
  5689. (outline-next-heading)))))))
  5690. (defun org-fontify-entities (limit)
  5691. "Find an entity to fontify."
  5692. (let (ee)
  5693. (when org-pretty-entities
  5694. (catch 'match
  5695. (while (re-search-forward
  5696. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5697. limit t)
  5698. (if (and (not (org-at-comment-p))
  5699. (setq ee (org-entity-get (match-string 1)))
  5700. (= (length (nth 6 ee)) 1))
  5701. (let*
  5702. ((end (if (equal (match-string 2) "{}")
  5703. (match-end 2)
  5704. (match-end 1))))
  5705. (add-text-properties
  5706. (match-beginning 0) end
  5707. (list 'font-lock-fontified t))
  5708. (compose-region (match-beginning 0) end
  5709. (nth 6 ee) nil)
  5710. (backward-char 1)
  5711. (throw 'match t))))
  5712. nil))))
  5713. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5714. "Fontify string S like in Org-mode."
  5715. (with-temp-buffer
  5716. (insert s)
  5717. (let ((org-odd-levels-only odd-levels))
  5718. (org-mode)
  5719. (font-lock-ensure)
  5720. (buffer-string))))
  5721. (defvar org-m nil)
  5722. (defvar org-l nil)
  5723. (defvar org-f nil)
  5724. (defun org-get-level-face (n)
  5725. "Get the right face for match N in font-lock matching of headlines."
  5726. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5727. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5728. (if org-cycle-level-faces
  5729. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5730. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5731. (cond
  5732. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5733. ((eq n 2) org-f)
  5734. (t (if org-level-color-stars-only nil org-f))))
  5735. (defun org-get-todo-face (kwd)
  5736. "Get the right face for a TODO keyword KWD.
  5737. If KWD is a number, get the corresponding match group."
  5738. (if (numberp kwd) (setq kwd (match-string kwd)))
  5739. (or (org-face-from-face-or-color
  5740. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5741. (and (member kwd org-done-keywords) 'org-done)
  5742. 'org-todo))
  5743. (defun org-face-from-face-or-color (context inherit face-or-color)
  5744. "Create a face list that inherits INHERIT, but sets the foreground color.
  5745. When FACE-OR-COLOR is not a string, just return it."
  5746. (if (stringp face-or-color)
  5747. (list :inherit inherit
  5748. (cdr (assoc context org-faces-easy-properties))
  5749. face-or-color)
  5750. face-or-color))
  5751. (defun org-font-lock-add-tag-faces (limit)
  5752. "Add the special tag faces."
  5753. (when (and org-tag-faces org-tags-special-faces-re)
  5754. (while (re-search-forward org-tags-special-faces-re limit t)
  5755. (add-text-properties (match-beginning 1) (match-end 1)
  5756. (list 'face (org-get-tag-face 1)
  5757. 'font-lock-fontified t))
  5758. (backward-char 1))))
  5759. (defun org-font-lock-add-priority-faces (limit)
  5760. "Add the special priority faces."
  5761. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5762. (when (save-match-data (org-at-heading-p))
  5763. (add-text-properties
  5764. (match-beginning 0) (match-end 0)
  5765. (list 'face (or (org-face-from-face-or-color
  5766. 'priority 'org-priority
  5767. (cdr (assoc (char-after (match-beginning 1))
  5768. org-priority-faces)))
  5769. 'org-priority)
  5770. 'font-lock-fontified t)))))
  5771. (defun org-get-tag-face (kwd)
  5772. "Get the right face for a TODO keyword KWD.
  5773. If KWD is a number, get the corresponding match group."
  5774. (if (numberp kwd) (setq kwd (match-string kwd)))
  5775. (or (org-face-from-face-or-color
  5776. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5777. 'org-tag))
  5778. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5779. "Remove fontification and activation overlays from links."
  5780. (font-lock-default-unfontify-region beg end)
  5781. (let* ((buffer-undo-list t)
  5782. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5783. (inhibit-modification-hooks t)
  5784. deactivate-mark buffer-file-name buffer-file-truename)
  5785. (org-decompose-region beg end)
  5786. (remove-text-properties beg end
  5787. '(mouse-face t keymap t org-linked-text t
  5788. invisible t intangible t
  5789. org-emphasis t))
  5790. (org-remove-font-lock-display-properties beg end)))
  5791. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5792. ((raise 0.3) (height 0.7))
  5793. ((raise -0.5))
  5794. ((raise 0.5)))
  5795. "Display properties for showing superscripts and subscripts.")
  5796. (defun org-remove-font-lock-display-properties (beg end)
  5797. "Remove specific display properties that have been added by font lock.
  5798. The will remove the raise properties that are used to show superscripts
  5799. and subscripts."
  5800. (let (next prop)
  5801. (while (< beg end)
  5802. (setq next (next-single-property-change beg 'display nil end)
  5803. prop (get-text-property beg 'display))
  5804. (if (member prop org-script-display)
  5805. (put-text-property beg next 'display nil))
  5806. (setq beg next))))
  5807. (defun org-raise-scripts (limit)
  5808. "Add raise properties to sub/superscripts."
  5809. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5810. (if (re-search-forward
  5811. (if (eq org-use-sub-superscripts t)
  5812. org-match-substring-regexp
  5813. org-match-substring-with-braces-regexp)
  5814. limit t)
  5815. (let* ((pos (point)) table-p comment-p
  5816. (mpos (match-beginning 3))
  5817. (emph-p (get-text-property mpos 'org-emphasis))
  5818. (link-p (get-text-property mpos 'mouse-face))
  5819. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5820. (goto-char (point-at-bol))
  5821. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5822. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5823. (goto-char pos)
  5824. ;; Handle a_b^c
  5825. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5826. (if (or comment-p emph-p link-p keyw-p)
  5827. t
  5828. (put-text-property (match-beginning 3) (match-end 0)
  5829. 'display
  5830. (if (equal (char-after (match-beginning 2)) ?^)
  5831. (nth (if table-p 3 1) org-script-display)
  5832. (nth (if table-p 2 0) org-script-display)))
  5833. (add-text-properties (match-beginning 2) (match-end 2)
  5834. (list 'invisible t
  5835. 'org-dwidth t 'org-dwidth-n 1))
  5836. (if (and (eq (char-after (match-beginning 3)) ?{)
  5837. (eq (char-before (match-end 3)) ?}))
  5838. (progn
  5839. (add-text-properties
  5840. (match-beginning 3) (1+ (match-beginning 3))
  5841. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5842. (add-text-properties
  5843. (1- (match-end 3)) (match-end 3)
  5844. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5845. t)))))
  5846. ;;;; Visibility cycling, including org-goto and indirect buffer
  5847. ;;; Cycling
  5848. (defvar org-cycle-global-status nil)
  5849. (make-variable-buffer-local 'org-cycle-global-status)
  5850. (put 'org-cycle-global-status 'org-state t)
  5851. (defvar org-cycle-subtree-status nil)
  5852. (make-variable-buffer-local 'org-cycle-subtree-status)
  5853. (put 'org-cycle-subtree-status 'org-state t)
  5854. (defvar org-inlinetask-min-level)
  5855. (defun org-unlogged-message (&rest args)
  5856. "Display a message, but avoid logging it in the *Messages* buffer."
  5857. (let ((message-log-max nil))
  5858. (apply 'message args)))
  5859. ;;;###autoload
  5860. (defun org-cycle (&optional arg)
  5861. "TAB-action and visibility cycling for Org-mode.
  5862. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5863. is outline visibility cycling, but it also invokes other actions
  5864. in special contexts.
  5865. - When this function is called with a prefix argument, rotate the entire
  5866. buffer through 3 states (global cycling)
  5867. 1. OVERVIEW: Show only top-level headlines.
  5868. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5869. 3. SHOW ALL: Show everything.
  5870. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5871. determined by the variable `org-startup-folded', and by any VISIBILITY
  5872. properties in the buffer.
  5873. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5874. including any drawers.
  5875. - When inside a table, re-align the table and move to the next field.
  5876. - When point is at the beginning of a headline, rotate the subtree started
  5877. by this line through 3 different states (local cycling)
  5878. 1. FOLDED: Only the main headline is shown.
  5879. 2. CHILDREN: The main headline and the direct children are shown.
  5880. From this state, you can move to one of the children
  5881. and zoom in further.
  5882. 3. SUBTREE: Show the entire subtree, including body text.
  5883. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5884. - When point is at the beginning of an empty headline and the variable
  5885. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5886. of the headline by demoting and promoting it to likely levels. This
  5887. speeds up creation document structure by pressing TAB once or several
  5888. times right after creating a new headline.
  5889. - When there is a numeric prefix, go up to a heading with level ARG, do
  5890. a `show-subtree' and return to the previous cursor position. If ARG
  5891. is negative, go up that many levels.
  5892. - When point is not at the beginning of a headline, execute the global
  5893. binding for TAB, which is re-indenting the line. See the option
  5894. `org-cycle-emulate-tab' for details.
  5895. - Special case: if point is at the beginning of the buffer and there is
  5896. no headline in line 1, this function will act as if called with prefix arg
  5897. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5898. But only if also the variable `org-cycle-global-at-bob' is t."
  5899. (interactive "P")
  5900. (org-load-modules-maybe)
  5901. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5902. (and org-cycle-level-after-item/entry-creation
  5903. (or (org-cycle-level)
  5904. (org-cycle-item-indentation))))
  5905. (let* ((limit-level
  5906. (or org-cycle-max-level
  5907. (and (boundp 'org-inlinetask-min-level)
  5908. org-inlinetask-min-level
  5909. (1- org-inlinetask-min-level))))
  5910. (nstars (and limit-level
  5911. (if org-odd-levels-only
  5912. (and limit-level (1- (* limit-level 2)))
  5913. limit-level)))
  5914. (org-outline-regexp
  5915. (if (not (derived-mode-p 'org-mode))
  5916. outline-regexp
  5917. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5918. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5919. (not (looking-at org-outline-regexp))))
  5920. (org-cycle-hook
  5921. (if bob-special
  5922. (delq 'org-optimize-window-after-visibility-change
  5923. (copy-sequence org-cycle-hook))
  5924. org-cycle-hook))
  5925. (pos (point)))
  5926. (if (or bob-special (equal arg '(4)))
  5927. ;; special case: use global cycling
  5928. (setq arg t))
  5929. (cond
  5930. ((equal arg '(16))
  5931. (setq last-command 'dummy)
  5932. (org-set-startup-visibility)
  5933. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5934. ((equal arg '(64))
  5935. (show-all)
  5936. (org-unlogged-message "Entire buffer visible, including drawers"))
  5937. ;; Try cdlatex TAB completion
  5938. ((org-try-cdlatex-tab))
  5939. ;; Table: enter it or move to the next field.
  5940. ((org-at-table-p 'any)
  5941. (if (org-at-table.el-p)
  5942. (message "Use C-c ' to edit table.el tables")
  5943. (if arg (org-table-edit-field t)
  5944. (org-table-justify-field-maybe)
  5945. (call-interactively 'org-table-next-field))))
  5946. ((run-hook-with-args-until-success
  5947. 'org-tab-after-check-for-table-hook))
  5948. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5949. ((eq arg t) (org-cycle-internal-global))
  5950. ;; Drawers: delegate to `org-flag-drawer'.
  5951. ((save-excursion
  5952. (beginning-of-line 1)
  5953. (looking-at org-drawer-regexp))
  5954. (org-flag-drawer ; toggle block visibility
  5955. (not (get-char-property (match-end 0) 'invisible))))
  5956. ;; Show-subtree, ARG levels up from here.
  5957. ((integerp arg)
  5958. (save-excursion
  5959. (org-back-to-heading)
  5960. (outline-up-heading (if (< arg 0) (- arg)
  5961. (- (funcall outline-level) arg)))
  5962. (org-show-subtree)))
  5963. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5964. ((and (featurep 'org-inlinetask)
  5965. (org-inlinetask-at-task-p)
  5966. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5967. (org-inlinetask-toggle-visibility))
  5968. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5969. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5970. (save-excursion (beginning-of-line 1)
  5971. (looking-at org-outline-regexp)))
  5972. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5973. (org-cycle-internal-local))
  5974. ;; From there: TAB emulation and template completion.
  5975. (buffer-read-only (org-back-to-heading))
  5976. ((run-hook-with-args-until-success
  5977. 'org-tab-after-check-for-cycling-hook))
  5978. ((org-try-structure-completion))
  5979. ((run-hook-with-args-until-success
  5980. 'org-tab-before-tab-emulation-hook))
  5981. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5982. (or (not (bolp))
  5983. (not (looking-at org-outline-regexp))))
  5984. (call-interactively (global-key-binding "\t")))
  5985. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5986. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5987. (or (and (eq org-cycle-emulate-tab 'white)
  5988. (= (match-end 0) (point-at-eol)))
  5989. (and (eq org-cycle-emulate-tab 'whitestart)
  5990. (>= (match-end 0) pos))))
  5991. t
  5992. (eq org-cycle-emulate-tab t))
  5993. (call-interactively (global-key-binding "\t")))
  5994. (t (save-excursion
  5995. (org-back-to-heading)
  5996. (org-cycle)))))))
  5997. (defun org-cycle-internal-global ()
  5998. "Do the global cycling action."
  5999. ;; Hack to avoid display of messages for .org attachments in Gnus
  6000. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6001. (cond
  6002. ((and (eq last-command this-command)
  6003. (eq org-cycle-global-status 'overview))
  6004. ;; We just created the overview - now do table of contents
  6005. ;; This can be slow in very large buffers, so indicate action
  6006. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6007. (unless ga (org-unlogged-message "CONTENTS..."))
  6008. (org-content)
  6009. (unless ga (org-unlogged-message "CONTENTS...done"))
  6010. (setq org-cycle-global-status 'contents)
  6011. (run-hook-with-args 'org-cycle-hook 'contents))
  6012. ((and (eq last-command this-command)
  6013. (eq org-cycle-global-status 'contents))
  6014. ;; We just showed the table of contents - now show everything
  6015. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6016. (show-all)
  6017. (unless ga (org-unlogged-message "SHOW ALL"))
  6018. (setq org-cycle-global-status 'all)
  6019. (run-hook-with-args 'org-cycle-hook 'all))
  6020. (t
  6021. ;; Default action: go to overview
  6022. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6023. (org-overview)
  6024. (unless ga (org-unlogged-message "OVERVIEW"))
  6025. (setq org-cycle-global-status 'overview)
  6026. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6027. (defvar org-called-with-limited-levels nil
  6028. "Non-nil when `org-with-limited-levels' is currently active.")
  6029. (defun org-cycle-internal-local ()
  6030. "Do the local cycling action."
  6031. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6032. ;; First, determine end of headline (EOH), end of subtree or item
  6033. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6034. (save-excursion
  6035. (if (org-at-item-p)
  6036. (progn
  6037. (beginning-of-line)
  6038. (setq struct (org-list-struct))
  6039. (setq eoh (point-at-eol))
  6040. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6041. (setq has-children (org-list-has-child-p (point) struct)))
  6042. (org-back-to-heading)
  6043. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6044. (setq eos (save-excursion (org-end-of-subtree t t)
  6045. (when (bolp) (backward-char)) (point)))
  6046. (setq has-children
  6047. (or (save-excursion
  6048. (let ((level (funcall outline-level)))
  6049. (outline-next-heading)
  6050. (and (org-at-heading-p t)
  6051. (> (funcall outline-level) level))))
  6052. (save-excursion
  6053. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6054. ;; Determine end invisible part of buffer (EOL)
  6055. (beginning-of-line 2)
  6056. ;; XEmacs doesn't have `next-single-char-property-change'
  6057. (if (featurep 'xemacs)
  6058. (while (and (not (eobp)) ;; this is like `next-line'
  6059. (get-char-property (1- (point)) 'invisible))
  6060. (beginning-of-line 2))
  6061. (while (and (not (eobp)) ;; this is like `next-line'
  6062. (get-char-property (1- (point)) 'invisible))
  6063. (goto-char (next-single-char-property-change (point) 'invisible))
  6064. (and (eolp) (beginning-of-line 2))))
  6065. (setq eol (point)))
  6066. ;; Find out what to do next and set `this-command'
  6067. (cond
  6068. ((= eos eoh)
  6069. ;; Nothing is hidden behind this heading
  6070. (unless (org-before-first-heading-p)
  6071. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6072. (org-unlogged-message "EMPTY ENTRY")
  6073. (setq org-cycle-subtree-status nil)
  6074. (save-excursion
  6075. (goto-char eos)
  6076. (outline-next-heading)
  6077. (if (outline-invisible-p) (org-flag-heading nil))))
  6078. ((and (or (>= eol eos)
  6079. (not (string-match "\\S-" (buffer-substring eol eos))))
  6080. (or has-children
  6081. (not (setq children-skipped
  6082. org-cycle-skip-children-state-if-no-children))))
  6083. ;; Entire subtree is hidden in one line: children view
  6084. (unless (org-before-first-heading-p)
  6085. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6086. (if (org-at-item-p)
  6087. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6088. (org-show-entry)
  6089. (org-with-limited-levels (show-children))
  6090. ;; FIXME: This slows down the func way too much.
  6091. ;; How keep drawers hidden in subtree anyway?
  6092. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6093. ;; (org-cycle-hide-drawers 'subtree))
  6094. ;; Fold every list in subtree to top-level items.
  6095. (when (eq org-cycle-include-plain-lists 'integrate)
  6096. (save-excursion
  6097. (org-back-to-heading)
  6098. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6099. (beginning-of-line 1)
  6100. (let* ((struct (org-list-struct))
  6101. (prevs (org-list-prevs-alist struct))
  6102. (end (org-list-get-bottom-point struct)))
  6103. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6104. (org-list-get-all-items (point) struct prevs))
  6105. (goto-char (if (< end eos) end eos)))))))
  6106. (org-unlogged-message "CHILDREN")
  6107. (save-excursion
  6108. (goto-char eos)
  6109. (outline-next-heading)
  6110. (if (outline-invisible-p) (org-flag-heading nil)))
  6111. (setq org-cycle-subtree-status 'children)
  6112. (unless (org-before-first-heading-p)
  6113. (run-hook-with-args 'org-cycle-hook 'children)))
  6114. ((or children-skipped
  6115. (and (eq last-command this-command)
  6116. (eq org-cycle-subtree-status 'children)))
  6117. ;; We just showed the children, or no children are there,
  6118. ;; now show everything.
  6119. (unless (org-before-first-heading-p)
  6120. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6121. (outline-flag-region eoh eos nil)
  6122. (org-unlogged-message
  6123. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6124. (setq org-cycle-subtree-status 'subtree)
  6125. (unless (org-before-first-heading-p)
  6126. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6127. (t
  6128. ;; Default action: hide the subtree.
  6129. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6130. (outline-flag-region eoh eos t)
  6131. (org-unlogged-message "FOLDED")
  6132. (setq org-cycle-subtree-status 'folded)
  6133. (unless (org-before-first-heading-p)
  6134. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6135. ;;;###autoload
  6136. (defun org-global-cycle (&optional arg)
  6137. "Cycle the global visibility. For details see `org-cycle'.
  6138. With \\[universal-argument] prefix arg, switch to startup visibility.
  6139. With a numeric prefix, show all headlines up to that level."
  6140. (interactive "P")
  6141. (let ((org-cycle-include-plain-lists
  6142. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6143. (cond
  6144. ((integerp arg)
  6145. (show-all)
  6146. (hide-sublevels arg)
  6147. (setq org-cycle-global-status 'contents))
  6148. ((equal arg '(4))
  6149. (org-set-startup-visibility)
  6150. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6151. (t
  6152. (org-cycle '(4))))))
  6153. (defun org-set-startup-visibility ()
  6154. "Set the visibility required by startup options and properties."
  6155. (cond
  6156. ((eq org-startup-folded t)
  6157. (org-overview))
  6158. ((eq org-startup-folded 'content)
  6159. (org-content))
  6160. ((or (eq org-startup-folded 'showeverything)
  6161. (eq org-startup-folded nil))
  6162. (show-all)))
  6163. (unless (eq org-startup-folded 'showeverything)
  6164. (if org-hide-block-startup (org-hide-block-all))
  6165. (org-set-visibility-according-to-property 'no-cleanup)
  6166. (org-cycle-hide-archived-subtrees 'all)
  6167. (org-cycle-hide-drawers 'all)
  6168. (org-cycle-show-empty-lines t)))
  6169. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6170. "Switch subtree visibilities according to :VISIBILITY: property."
  6171. (interactive)
  6172. (let (org-show-entry-below)
  6173. (org-with-wide-buffer
  6174. (goto-char (point-min))
  6175. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6176. (if (not (org-at-property-p)) (outline-next-heading)
  6177. (let ((state (match-string 3)))
  6178. (save-excursion
  6179. (org-back-to-heading t)
  6180. (hide-subtree)
  6181. (org-reveal)
  6182. (cond
  6183. ((equal state "folded")
  6184. (hide-subtree))
  6185. ((equal state "children")
  6186. (org-show-hidden-entry)
  6187. (show-children))
  6188. ((equal state "content")
  6189. (save-excursion
  6190. (save-restriction
  6191. (org-narrow-to-subtree)
  6192. (org-content))))
  6193. ((member state '("all" "showall"))
  6194. (show-subtree)))))))
  6195. (unless no-cleanup
  6196. (org-cycle-hide-archived-subtrees 'all)
  6197. (org-cycle-hide-drawers 'all)
  6198. (org-cycle-show-empty-lines 'all)))))
  6199. ;; This function uses outline-regexp instead of the more fundamental
  6200. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6201. ;; buffers, where outline-regexp is needed.
  6202. (defun org-overview ()
  6203. "Switch to overview mode, showing only top-level headlines.
  6204. This shows all headlines with a level equal or greater than the level
  6205. of the first headline in the buffer. This is important, because if the
  6206. first headline is not level one, then (hide-sublevels 1) gives confusing
  6207. results."
  6208. (interactive)
  6209. (save-excursion
  6210. (let ((level
  6211. (save-excursion
  6212. (goto-char (point-min))
  6213. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6214. (progn
  6215. (goto-char (match-beginning 0))
  6216. (funcall outline-level))))))
  6217. (and level (hide-sublevels level)))))
  6218. (defun org-content (&optional arg)
  6219. "Show all headlines in the buffer, like a table of contents.
  6220. With numerical argument N, show content up to level N."
  6221. (interactive "P")
  6222. (org-overview)
  6223. (save-excursion
  6224. ;; Visit all headings and show their offspring
  6225. (and (integerp arg) (org-overview))
  6226. (goto-char (point-max))
  6227. (catch 'exit
  6228. (while (and (progn (condition-case nil
  6229. (outline-previous-visible-heading 1)
  6230. (error (goto-char (point-min))))
  6231. t)
  6232. (looking-at org-outline-regexp))
  6233. (if (integerp arg)
  6234. (show-children (1- arg))
  6235. (show-branches))
  6236. (if (bobp) (throw 'exit nil))))))
  6237. (defun org-optimize-window-after-visibility-change (state)
  6238. "Adjust the window after a change in outline visibility.
  6239. This function is the default value of the hook `org-cycle-hook'."
  6240. (when (get-buffer-window (current-buffer))
  6241. (cond
  6242. ((eq state 'content) nil)
  6243. ((eq state 'all) nil)
  6244. ((eq state 'folded) nil)
  6245. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6246. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6247. (defun org-remove-empty-overlays-at (pos)
  6248. "Remove outline overlays that do not contain non-white stuff."
  6249. (mapc
  6250. (lambda (o)
  6251. (and (eq 'outline (overlay-get o 'invisible))
  6252. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6253. (overlay-end o))))
  6254. (delete-overlay o)))
  6255. (overlays-at pos)))
  6256. (defun org-clean-visibility-after-subtree-move ()
  6257. "Fix visibility issues after moving a subtree."
  6258. ;; First, find a reasonable region to look at:
  6259. ;; Start two siblings above, end three below
  6260. (let* ((beg (save-excursion
  6261. (and (org-get-last-sibling)
  6262. (org-get-last-sibling))
  6263. (point)))
  6264. (end (save-excursion
  6265. (and (org-get-next-sibling)
  6266. (org-get-next-sibling)
  6267. (org-get-next-sibling))
  6268. (if (org-at-heading-p)
  6269. (point-at-eol)
  6270. (point))))
  6271. (level (looking-at "\\*+"))
  6272. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6273. (save-excursion
  6274. (save-restriction
  6275. (narrow-to-region beg end)
  6276. (when re
  6277. ;; Properly fold already folded siblings
  6278. (goto-char (point-min))
  6279. (while (re-search-forward re nil t)
  6280. (if (and (not (outline-invisible-p))
  6281. (save-excursion
  6282. (goto-char (point-at-eol)) (outline-invisible-p)))
  6283. (hide-entry))))
  6284. (org-cycle-show-empty-lines 'overview)
  6285. (org-cycle-hide-drawers 'overview)))))
  6286. (defun org-cycle-show-empty-lines (state)
  6287. "Show empty lines above all visible headlines.
  6288. The region to be covered depends on STATE when called through
  6289. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6290. entire buffer covered. Note that an empty line is only shown if there
  6291. are at least `org-cycle-separator-lines' empty lines before the headline."
  6292. (when (not (= org-cycle-separator-lines 0))
  6293. (save-excursion
  6294. (let* ((n (abs org-cycle-separator-lines))
  6295. (re (cond
  6296. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6297. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6298. (t (let ((ns (number-to-string (- n 2))))
  6299. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6300. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6301. beg end b e)
  6302. (cond
  6303. ((memq state '(overview contents t))
  6304. (setq beg (point-min) end (point-max)))
  6305. ((memq state '(children folded))
  6306. (setq beg (point) end (progn (org-end-of-subtree t t)
  6307. (beginning-of-line 2)
  6308. (point)))))
  6309. (when beg
  6310. (goto-char beg)
  6311. (while (re-search-forward re end t)
  6312. (unless (get-char-property (match-end 1) 'invisible)
  6313. (setq e (match-end 1))
  6314. (if (< org-cycle-separator-lines 0)
  6315. (setq b (save-excursion
  6316. (goto-char (match-beginning 0))
  6317. (org-back-over-empty-lines)
  6318. (if (save-excursion
  6319. (goto-char (max (point-min) (1- (point))))
  6320. (org-at-heading-p))
  6321. (1- (point))
  6322. (point))))
  6323. (setq b (match-beginning 1)))
  6324. (outline-flag-region b e nil)))))))
  6325. ;; Never hide empty lines at the end of the file.
  6326. (save-excursion
  6327. (goto-char (point-max))
  6328. (outline-previous-heading)
  6329. (outline-end-of-heading)
  6330. (if (and (looking-at "[ \t\n]+")
  6331. (= (match-end 0) (point-max)))
  6332. (outline-flag-region (point) (match-end 0) nil))))
  6333. (defun org-show-empty-lines-in-parent ()
  6334. "Move to the parent and re-show empty lines before visible headlines."
  6335. (save-excursion
  6336. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6337. (org-cycle-show-empty-lines context))))
  6338. (defun org-files-list ()
  6339. "Return `org-agenda-files' list, plus all open org-mode files.
  6340. This is useful for operations that need to scan all of a user's
  6341. open and agenda-wise Org files."
  6342. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6343. (dolist (buf (buffer-list))
  6344. (with-current-buffer buf
  6345. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6346. (let ((file (expand-file-name (buffer-file-name))))
  6347. (unless (member file files)
  6348. (push file files))))))
  6349. files))
  6350. (defsubst org-entry-beginning-position ()
  6351. "Return the beginning position of the current entry."
  6352. (save-excursion (outline-back-to-heading t) (point)))
  6353. (defsubst org-entry-end-position ()
  6354. "Return the end position of the current entry."
  6355. (save-excursion (outline-next-heading) (point)))
  6356. (defun org-cycle-hide-drawers (state &optional exceptions)
  6357. "Re-hide all drawers after a visibility state change.
  6358. When non-nil, optional argument EXCEPTIONS is a list of strings
  6359. specifying which drawers should not be hidden."
  6360. (when (and (derived-mode-p 'org-mode)
  6361. (not (memq state '(overview folded contents))))
  6362. (save-excursion
  6363. (let* ((globalp (memq state '(contents all)))
  6364. (beg (if globalp (point-min) (point)))
  6365. (end (if globalp (point-max)
  6366. (if (eq state 'children)
  6367. (save-excursion (outline-next-heading) (point))
  6368. (org-end-of-subtree t)))))
  6369. (goto-char beg)
  6370. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6371. (unless (member-ignore-case (match-string 1) exceptions)
  6372. (let ((drawer (org-element-at-point)))
  6373. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6374. (org-flag-drawer t drawer)
  6375. ;; Make sure to skip drawer entirely or we might flag
  6376. ;; it another time when matching its ending line with
  6377. ;; `org-drawer-regexp'.
  6378. (goto-char (org-element-property :end drawer))))))))))
  6379. (defun org-cycle-hide-inline-tasks (state)
  6380. "Re-hide inline tasks when switching to 'contents or 'children
  6381. visibility state."
  6382. (case state
  6383. (contents
  6384. (when (org-bound-and-true-p org-inlinetask-min-level)
  6385. (hide-sublevels (1- org-inlinetask-min-level))))
  6386. (children
  6387. (when (featurep 'org-inlinetask)
  6388. (save-excursion
  6389. (while (and (outline-next-heading)
  6390. (org-inlinetask-at-task-p))
  6391. (org-inlinetask-toggle-visibility)
  6392. (org-inlinetask-goto-end)))))))
  6393. (defun org-flag-drawer (flag &optional element)
  6394. "When FLAG is non-nil, hide the drawer we are at.
  6395. Otherwise make it visible. When optional argument ELEMENT is
  6396. a parsed drawer, as returned by `org-element-at-point', hide or
  6397. show that drawer instead."
  6398. (when (save-excursion
  6399. (beginning-of-line)
  6400. (org-looking-at-p org-drawer-regexp))
  6401. (let ((drawer (or element (org-element-at-point))))
  6402. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6403. (let ((post (org-element-property :post-affiliated drawer)))
  6404. (save-excursion
  6405. (outline-flag-region
  6406. (progn (goto-char post) (line-end-position))
  6407. (progn (goto-char (org-element-property :end drawer))
  6408. (skip-chars-backward " \r\t\n")
  6409. (line-end-position))
  6410. flag))
  6411. ;; When the drawer is hidden away, make sure point lies in
  6412. ;; a visible part of the buffer.
  6413. (when (and flag (> (line-beginning-position) post))
  6414. (goto-char post)))))))
  6415. (defun org-subtree-end-visible-p ()
  6416. "Is the end of the current subtree visible?"
  6417. (pos-visible-in-window-p
  6418. (save-excursion (org-end-of-subtree t) (point))))
  6419. (defun org-first-headline-recenter ()
  6420. "Move cursor to the first headline and recenter the headline."
  6421. (goto-char (point-min))
  6422. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6423. (set-window-start (selected-window) (point-at-bol))))
  6424. ;;; Saving and restoring visibility
  6425. (defun org-outline-overlay-data (&optional use-markers)
  6426. "Return a list of the locations of all outline overlays.
  6427. These are overlays with the `invisible' property value `outline'.
  6428. The return value is a list of cons cells, with start and stop
  6429. positions for each overlay.
  6430. If USE-MARKERS is set, return the positions as markers."
  6431. (let (beg end)
  6432. (save-excursion
  6433. (save-restriction
  6434. (widen)
  6435. (delq nil
  6436. (mapcar (lambda (o)
  6437. (when (eq (overlay-get o 'invisible) 'outline)
  6438. (setq beg (overlay-start o)
  6439. end (overlay-end o))
  6440. (and beg end (> end beg)
  6441. (if use-markers
  6442. (cons (copy-marker beg)
  6443. (copy-marker end t))
  6444. (cons beg end)))))
  6445. (overlays-in (point-min) (point-max))))))))
  6446. (defun org-set-outline-overlay-data (data)
  6447. "Create visibility overlays for all positions in DATA.
  6448. DATA should have been made by `org-outline-overlay-data'."
  6449. (let (o)
  6450. (save-excursion
  6451. (save-restriction
  6452. (widen)
  6453. (show-all)
  6454. (mapc (lambda (c)
  6455. (outline-flag-region (car c) (cdr c) t))
  6456. data)))))
  6457. ;;; Folding of blocks
  6458. (defvar org-hide-block-overlays nil
  6459. "Overlays hiding blocks.")
  6460. (make-variable-buffer-local 'org-hide-block-overlays)
  6461. (defun org-block-map (function &optional start end)
  6462. "Call FUNCTION at the head of all source blocks in the current buffer.
  6463. Optional arguments START and END can be used to limit the range."
  6464. (let ((start (or start (point-min)))
  6465. (end (or end (point-max))))
  6466. (save-excursion
  6467. (goto-char start)
  6468. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6469. (save-excursion
  6470. (save-match-data
  6471. (goto-char (match-beginning 0))
  6472. (funcall function)))))))
  6473. (defun org-hide-block-toggle-all ()
  6474. "Toggle the visibility of all blocks in the current buffer."
  6475. (org-block-map 'org-hide-block-toggle))
  6476. (defun org-hide-block-all ()
  6477. "Fold all blocks in the current buffer."
  6478. (interactive)
  6479. (org-show-block-all)
  6480. (org-block-map 'org-hide-block-toggle-maybe))
  6481. (defun org-show-block-all ()
  6482. "Unfold all blocks in the current buffer."
  6483. (interactive)
  6484. (mapc 'delete-overlay org-hide-block-overlays)
  6485. (setq org-hide-block-overlays nil))
  6486. (defun org-hide-block-toggle-maybe ()
  6487. "Toggle visibility of block at point.
  6488. Unlike to `org-hide-block-toggle', this function does not throw
  6489. an error. Return a non-nil value when toggling is successful."
  6490. (interactive)
  6491. (ignore-errors (org-hide-block-toggle)))
  6492. (defun org-hide-block-toggle (&optional force)
  6493. "Toggle the visibility of the current block.
  6494. When optional argument FORCE is `off', make block visible. If it
  6495. is non-nil, hide it unconditionally. Throw an error when not at
  6496. a block. Return a non-nil value when toggling is successful."
  6497. (interactive)
  6498. (let ((element (org-element-at-point)))
  6499. (unless (memq (org-element-type element)
  6500. '(center-block comment-block dynamic-block example-block
  6501. export-block quote-block special-block
  6502. src-block verse-block))
  6503. (user-error "Not at a block"))
  6504. (let* ((start (save-excursion
  6505. (goto-char (org-element-property :post-affiliated element))
  6506. (line-end-position)))
  6507. (end (save-excursion
  6508. (goto-char (org-element-property :end element))
  6509. (skip-chars-backward " \r\t\n")
  6510. (line-end-position)))
  6511. (overlays (overlays-at start)))
  6512. (cond
  6513. ;; Do nothing when not before or at the block opening line or
  6514. ;; at the block closing line.
  6515. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6516. ((and (not (eq force 'off))
  6517. (not (memq t (mapcar
  6518. (lambda (o)
  6519. (eq (overlay-get o 'invisible) 'org-hide-block))
  6520. overlays))))
  6521. (let ((ov (make-overlay start end)))
  6522. (overlay-put ov 'invisible 'org-hide-block)
  6523. ;; Make the block accessible to `isearch'.
  6524. (overlay-put
  6525. ov 'isearch-open-invisible
  6526. (lambda (ov)
  6527. (when (memq ov org-hide-block-overlays)
  6528. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6529. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6530. (delete-overlay ov))))
  6531. (push ov org-hide-block-overlays)
  6532. ;; When the block is hidden away, make sure point is left in
  6533. ;; a visible part of the buffer.
  6534. (when (> (line-beginning-position) start)
  6535. (goto-char start)
  6536. (beginning-of-line))
  6537. ;; Signal successful toggling.
  6538. t))
  6539. ((or (not force) (eq force 'off))
  6540. (dolist (ov overlays t)
  6541. (when (memq ov org-hide-block-overlays)
  6542. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6543. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6544. (delete-overlay ov))))))))
  6545. ;; org-tab-after-check-for-cycling-hook
  6546. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6547. ;; Remove overlays when changing major mode
  6548. (add-hook 'org-mode-hook
  6549. (lambda () (org-add-hook 'change-major-mode-hook
  6550. 'org-show-block-all 'append 'local)))
  6551. ;;; Org-goto
  6552. (defvar org-goto-window-configuration nil)
  6553. (defvar org-goto-marker nil)
  6554. (defvar org-goto-map)
  6555. (defun org-goto-map ()
  6556. "Set the keymap `org-goto'."
  6557. (setq org-goto-map
  6558. (let ((map (make-sparse-keymap)))
  6559. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6560. mouse-drag-region universal-argument org-occur))
  6561. cmd)
  6562. (while (setq cmd (pop cmds))
  6563. (substitute-key-definition cmd cmd map global-map)))
  6564. (suppress-keymap map)
  6565. (org-defkey map "\C-m" 'org-goto-ret)
  6566. (org-defkey map [(return)] 'org-goto-ret)
  6567. (org-defkey map [(left)] 'org-goto-left)
  6568. (org-defkey map [(right)] 'org-goto-right)
  6569. (org-defkey map [(control ?g)] 'org-goto-quit)
  6570. (org-defkey map "\C-i" 'org-cycle)
  6571. (org-defkey map [(tab)] 'org-cycle)
  6572. (org-defkey map [(down)] 'outline-next-visible-heading)
  6573. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6574. (if org-goto-auto-isearch
  6575. (if (fboundp 'define-key-after)
  6576. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6577. nil)
  6578. (org-defkey map "q" 'org-goto-quit)
  6579. (org-defkey map "n" 'outline-next-visible-heading)
  6580. (org-defkey map "p" 'outline-previous-visible-heading)
  6581. (org-defkey map "f" 'outline-forward-same-level)
  6582. (org-defkey map "b" 'outline-backward-same-level)
  6583. (org-defkey map "u" 'outline-up-heading))
  6584. (org-defkey map "/" 'org-occur)
  6585. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6586. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6587. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6588. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6589. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6590. map)))
  6591. (defconst org-goto-help
  6592. "Browse buffer copy, to find location or copy text.%s
  6593. RET=jump to location C-g=quit and return to previous location
  6594. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6595. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6596. (defun org-goto (&optional alternative-interface)
  6597. "Look up a different location in the current file, keeping current visibility.
  6598. When you want look-up or go to a different location in a
  6599. document, the fastest way is often to fold the entire buffer and
  6600. then dive into the tree. This method has the disadvantage, that
  6601. the previous location will be folded, which may not be what you
  6602. want.
  6603. This command works around this by showing a copy of the current
  6604. buffer in an indirect buffer, in overview mode. You can dive
  6605. into the tree in that copy, use org-occur and incremental search
  6606. to find a location. When pressing RET or `Q', the command
  6607. returns to the original buffer in which the visibility is still
  6608. unchanged. After RET it will also jump to the location selected
  6609. in the indirect buffer and expose the headline hierarchy above.
  6610. With a prefix argument, use the alternative interface: e.g. if
  6611. `org-goto-interface' is 'outline use 'outline-path-completion."
  6612. (interactive "P")
  6613. (org-goto-map)
  6614. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6615. (org-refile-use-outline-path t)
  6616. (org-refile-target-verify-function nil)
  6617. (interface
  6618. (if (not alternative-interface)
  6619. org-goto-interface
  6620. (if (eq org-goto-interface 'outline)
  6621. 'outline-path-completion
  6622. 'outline)))
  6623. (org-goto-start-pos (point))
  6624. (selected-point
  6625. (if (eq interface 'outline)
  6626. (car (org-get-location (current-buffer) org-goto-help))
  6627. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6628. (org-refile-check-position pa)
  6629. (nth 3 pa)))))
  6630. (if selected-point
  6631. (progn
  6632. (org-mark-ring-push org-goto-start-pos)
  6633. (goto-char selected-point)
  6634. (if (or (outline-invisible-p) (org-invisible-p2))
  6635. (org-show-context 'org-goto)))
  6636. (message "Quit"))))
  6637. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6638. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6639. (defvar org-goto-local-auto-isearch-map) ; defined below
  6640. (defun org-get-location (buf help)
  6641. "Let the user select a location in the Org-mode buffer BUF.
  6642. This function uses a recursive edit. It returns the selected position
  6643. or nil."
  6644. (org-no-popups
  6645. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6646. (isearch-hide-immediately nil)
  6647. (isearch-search-fun-function
  6648. (lambda () 'org-goto-local-search-headings))
  6649. (org-goto-selected-point org-goto-exit-command))
  6650. (save-excursion
  6651. (save-window-excursion
  6652. (delete-other-windows)
  6653. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6654. (org-pop-to-buffer-same-window
  6655. (condition-case nil
  6656. (make-indirect-buffer (current-buffer) "*org-goto*")
  6657. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6658. (with-output-to-temp-buffer "*Org Help*"
  6659. (princ (format help (if org-goto-auto-isearch
  6660. " Just type for auto-isearch."
  6661. " n/p/f/b/u to navigate, q to quit."))))
  6662. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6663. (setq buffer-read-only nil)
  6664. (let ((org-startup-truncated t)
  6665. (org-startup-folded nil)
  6666. (org-startup-align-all-tables nil))
  6667. (org-mode)
  6668. (org-overview))
  6669. (setq buffer-read-only t)
  6670. (if (and (boundp 'org-goto-start-pos)
  6671. (integer-or-marker-p org-goto-start-pos))
  6672. (progn (goto-char org-goto-start-pos)
  6673. (when (outline-invisible-p)
  6674. (org-show-set-visibility 'lineage)))
  6675. (goto-char (point-min)))
  6676. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6677. (message "Select location and press RET")
  6678. (use-local-map org-goto-map)
  6679. (recursive-edit)))
  6680. (kill-buffer "*org-goto*")
  6681. (cons org-goto-selected-point org-goto-exit-command))))
  6682. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6683. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6684. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6685. (if (fboundp 'isearch-other-control-char)
  6686. (progn
  6687. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6688. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6689. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6690. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6691. (define-key org-goto-local-auto-isearch-map [return] nil))
  6692. (defun org-goto-local-search-headings (string bound noerror)
  6693. "Search and make sure that any matches are in headlines."
  6694. (catch 'return
  6695. (while (if isearch-forward
  6696. (search-forward string bound noerror)
  6697. (search-backward string bound noerror))
  6698. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6699. (and (member :headline context)
  6700. (not (member :tags context))))
  6701. (throw 'return (point))))))
  6702. (defun org-goto-local-auto-isearch ()
  6703. "Start isearch."
  6704. (interactive)
  6705. (goto-char (point-min))
  6706. (let ((keys (this-command-keys)))
  6707. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6708. (isearch-mode t)
  6709. (isearch-process-search-char (string-to-char keys)))))
  6710. (defun org-goto-ret (&optional arg)
  6711. "Finish `org-goto' by going to the new location."
  6712. (interactive "P")
  6713. (setq org-goto-selected-point (point)
  6714. org-goto-exit-command 'return)
  6715. (throw 'exit nil))
  6716. (defun org-goto-left ()
  6717. "Finish `org-goto' by going to the new location."
  6718. (interactive)
  6719. (if (org-at-heading-p)
  6720. (progn
  6721. (beginning-of-line 1)
  6722. (setq org-goto-selected-point (point)
  6723. org-goto-exit-command 'left)
  6724. (throw 'exit nil))
  6725. (user-error "Not on a heading")))
  6726. (defun org-goto-right ()
  6727. "Finish `org-goto' by going to the new location."
  6728. (interactive)
  6729. (if (org-at-heading-p)
  6730. (progn
  6731. (setq org-goto-selected-point (point)
  6732. org-goto-exit-command 'right)
  6733. (throw 'exit nil))
  6734. (user-error "Not on a heading")))
  6735. (defun org-goto-quit ()
  6736. "Finish `org-goto' without cursor motion."
  6737. (interactive)
  6738. (setq org-goto-selected-point nil)
  6739. (setq org-goto-exit-command 'quit)
  6740. (throw 'exit nil))
  6741. ;;; Indirect buffer display of subtrees
  6742. (defvar org-indirect-dedicated-frame nil
  6743. "This is the frame being used for indirect tree display.")
  6744. (defvar org-last-indirect-buffer nil)
  6745. (defun org-tree-to-indirect-buffer (&optional arg)
  6746. "Create indirect buffer and narrow it to current subtree.
  6747. With a numerical prefix ARG, go up to this level and then take that tree.
  6748. If ARG is negative, go up that many levels.
  6749. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6750. indirect buffer previously made with this command, to avoid proliferation of
  6751. indirect buffers. However, when you call the command with a \
  6752. \\[universal-argument] prefix, or
  6753. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6754. is kept so that you can work with several indirect buffers at the same time.
  6755. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6756. \\[universal-argument] prefix also
  6757. requests that a new frame be made for the new buffer, so that the dedicated
  6758. frame is not changed."
  6759. (interactive "P")
  6760. (let ((cbuf (current-buffer))
  6761. (cwin (selected-window))
  6762. (pos (point))
  6763. beg end level heading ibuf)
  6764. (save-excursion
  6765. (org-back-to-heading t)
  6766. (when (numberp arg)
  6767. (setq level (org-outline-level))
  6768. (if (< arg 0) (setq arg (+ level arg)))
  6769. (while (> (setq level (org-outline-level)) arg)
  6770. (org-up-heading-safe)))
  6771. (setq beg (point)
  6772. heading (org-get-heading))
  6773. (org-end-of-subtree t t)
  6774. (if (org-at-heading-p) (backward-char 1))
  6775. (setq end (point)))
  6776. (if (and (buffer-live-p org-last-indirect-buffer)
  6777. (not (eq org-indirect-buffer-display 'new-frame))
  6778. (not arg))
  6779. (kill-buffer org-last-indirect-buffer))
  6780. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6781. org-last-indirect-buffer ibuf)
  6782. (cond
  6783. ((or (eq org-indirect-buffer-display 'new-frame)
  6784. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6785. (select-frame (make-frame))
  6786. (delete-other-windows)
  6787. (org-pop-to-buffer-same-window ibuf)
  6788. (org-set-frame-title heading))
  6789. ((eq org-indirect-buffer-display 'dedicated-frame)
  6790. (raise-frame
  6791. (select-frame (or (and org-indirect-dedicated-frame
  6792. (frame-live-p org-indirect-dedicated-frame)
  6793. org-indirect-dedicated-frame)
  6794. (setq org-indirect-dedicated-frame (make-frame)))))
  6795. (delete-other-windows)
  6796. (org-pop-to-buffer-same-window ibuf)
  6797. (org-set-frame-title (concat "Indirect: " heading)))
  6798. ((eq org-indirect-buffer-display 'current-window)
  6799. (org-pop-to-buffer-same-window ibuf))
  6800. ((eq org-indirect-buffer-display 'other-window)
  6801. (pop-to-buffer ibuf))
  6802. (t (error "Invalid value")))
  6803. (if (featurep 'xemacs)
  6804. (save-excursion (org-mode) (turn-on-font-lock)))
  6805. (narrow-to-region beg end)
  6806. (show-all)
  6807. (goto-char pos)
  6808. (run-hook-with-args 'org-cycle-hook 'all)
  6809. (and (window-live-p cwin) (select-window cwin))))
  6810. (defun org-get-indirect-buffer (&optional buffer heading)
  6811. (setq buffer (or buffer (current-buffer)))
  6812. (let ((n 1) (base (buffer-name buffer)) bname)
  6813. (while (buffer-live-p
  6814. (get-buffer
  6815. (setq bname
  6816. (concat base "-"
  6817. (if heading (concat heading "-" (number-to-string n))
  6818. (number-to-string n))))))
  6819. (setq n (1+ n)))
  6820. (condition-case nil
  6821. (make-indirect-buffer buffer bname 'clone)
  6822. (error (make-indirect-buffer buffer bname)))))
  6823. (defun org-set-frame-title (title)
  6824. "Set the title of the current frame to the string TITLE."
  6825. ;; FIXME: how to name a single frame in XEmacs???
  6826. (unless (featurep 'xemacs)
  6827. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6828. ;;;; Structure editing
  6829. ;;; Inserting headlines
  6830. (defun org-previous-line-empty-p (&optional next)
  6831. "Is the previous line a blank line?
  6832. When NEXT is non-nil, check the next line instead."
  6833. (save-excursion
  6834. (and (not (bobp))
  6835. (or (beginning-of-line (if next 2 0)) t)
  6836. (save-match-data
  6837. (looking-at "[ \t]*$")))))
  6838. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6839. "Insert a new heading or an item with the same depth at point.
  6840. If point is at the beginning of a heading or a list item, insert
  6841. a new heading or a new item above the current one. If point is
  6842. at the beginning of a normal line, turn the line into a heading.
  6843. If point is in the middle of a headline or a list item, split the
  6844. headline or the item and create a new headline/item with the text
  6845. in the current line after point \(see `org-M-RET-may-split-line'
  6846. on how to modify this behavior).
  6847. With one universal prefix argument, set the user option
  6848. `org-insert-heading-respect-content' to t for the duration of
  6849. the command. This modifies the behavior described above in this
  6850. ways: on list items and at the beginning of normal lines, force
  6851. the insertion of a heading after the current subtree.
  6852. With two universal prefix arguments, insert the heading at the
  6853. end of the grandparent subtree. For example, if point is within
  6854. a 2nd-level heading, then it will insert a 2nd-level heading at
  6855. the end of the 1st-level parent heading.
  6856. If point is at the beginning of a headline, insert a sibling
  6857. before the current headline. If point is not at the beginning,
  6858. split the line and create a new headline with the text in the
  6859. current line after point \(see `org-M-RET-may-split-line' on how
  6860. to modify this behavior).
  6861. If point is at the beginning of a normal line, turn this line
  6862. into a heading.
  6863. When INVISIBLE-OK is set, stop at invisible headlines when going
  6864. back. This is important for non-interactive uses of the
  6865. command.
  6866. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6867. heading, unconditionally."
  6868. (interactive "P")
  6869. (if (org-called-interactively-p 'any) (org-reveal))
  6870. (let ((itemp (and (not top-level) (org-in-item-p)))
  6871. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6872. (respect-content (or org-insert-heading-respect-content
  6873. (equal arg '(4))))
  6874. (initial-content "")
  6875. (adjust-empty-lines t))
  6876. (cond
  6877. ((or (= (buffer-size) 0)
  6878. (and (not (save-excursion
  6879. (and (ignore-errors (org-back-to-heading invisible-ok))
  6880. (org-at-heading-p))))
  6881. (or arg (not itemp))))
  6882. ;; At beginning of buffer or so high up that only a heading
  6883. ;; makes sense.
  6884. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6885. ((not respect-content)
  6886. (unless may-split (end-of-line))
  6887. (insert "\n* "))
  6888. ((re-search-forward org-outline-regexp-bol nil t)
  6889. (beginning-of-line)
  6890. (insert "* \n")
  6891. (backward-char))
  6892. (t (goto-char (point-max))
  6893. (insert "\n* ")))
  6894. (run-hooks 'org-insert-heading-hook))
  6895. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6896. (t
  6897. ;; Maybe move at the end of the subtree
  6898. (when (equal arg '(16))
  6899. (org-up-heading-safe)
  6900. (org-end-of-subtree t))
  6901. ;; Insert a heading
  6902. (save-restriction
  6903. (widen)
  6904. (let* ((level nil)
  6905. (on-heading (org-at-heading-p))
  6906. (empty-line-p (if on-heading
  6907. (org-previous-line-empty-p)
  6908. ;; We will decide later
  6909. nil))
  6910. ;; Get a level string to fall back on.
  6911. (fix-level
  6912. (if (org-before-first-heading-p) "*"
  6913. (save-excursion
  6914. (org-back-to-heading t)
  6915. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6916. (looking-at org-outline-regexp)
  6917. (make-string (1- (length (match-string 0))) ?*))))
  6918. (stars
  6919. (save-excursion
  6920. (condition-case nil
  6921. (if top-level "* "
  6922. (org-back-to-heading invisible-ok)
  6923. (when (and (not on-heading)
  6924. (featurep 'org-inlinetask)
  6925. (integerp org-inlinetask-min-level)
  6926. (>= (length (match-string 0))
  6927. org-inlinetask-min-level))
  6928. ;; Find a heading level before the inline
  6929. ;; task.
  6930. (while (and (setq level (org-up-heading-safe))
  6931. (>= level org-inlinetask-min-level)))
  6932. (if (org-at-heading-p)
  6933. (org-back-to-heading invisible-ok)
  6934. (error "This should not happen")))
  6935. (unless (and (save-excursion
  6936. (save-match-data
  6937. (org-backward-heading-same-level
  6938. 1 invisible-ok))
  6939. (= (point) (match-beginning 0)))
  6940. (not (org-previous-line-empty-p t)))
  6941. (setq empty-line-p (or empty-line-p
  6942. (org-previous-line-empty-p))))
  6943. (match-string 0))
  6944. (error (or fix-level "* ")))))
  6945. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6946. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6947. pos hide-previous previous-pos)
  6948. ;; If we insert after content, move there and clean up
  6949. ;; whitespace.
  6950. (when (and respect-content
  6951. (not (org-looking-at-p org-outline-regexp-bol)))
  6952. (if (not (org-before-first-heading-p))
  6953. (org-end-of-subtree nil t)
  6954. (re-search-forward org-outline-regexp-bol)
  6955. (beginning-of-line 0))
  6956. (skip-chars-backward " \r\t\n")
  6957. (and (not (looking-back "^\\*+"))
  6958. (looking-at "[ \t]+") (replace-match ""))
  6959. (unless (eobp) (forward-char 1))
  6960. (when (looking-at "^\\*")
  6961. (unless (bobp) (backward-char 1))
  6962. (insert "\n")))
  6963. ;; If we are splitting, grab the text that should be moved
  6964. ;; to the new headline.
  6965. (when may-split
  6966. (if (org-on-heading-p)
  6967. ;; This is a heading: split intelligently (keeping
  6968. ;; tags).
  6969. (let ((pos (point)))
  6970. (beginning-of-line)
  6971. (unless (looking-at org-complex-heading-regexp)
  6972. (error "This should not happen"))
  6973. (when (and (match-beginning 4)
  6974. (> pos (match-beginning 4))
  6975. (< pos (match-end 4)))
  6976. (setq initial-content (buffer-substring pos (match-end 4)))
  6977. (goto-char pos)
  6978. (delete-region (point) (match-end 4))
  6979. (if (looking-at "[ \t]*$")
  6980. (replace-match "")
  6981. (insert (make-string (length initial-content) ?\s)))
  6982. (setq initial-content (org-trim initial-content)))
  6983. (goto-char pos))
  6984. ;; A normal line.
  6985. (setq initial-content
  6986. (org-trim
  6987. (delete-and-extract-region (point) (line-end-position))))))
  6988. ;; If we are at the beginning of the line, insert before it.
  6989. ;; Otherwise, after it.
  6990. (cond
  6991. ((and (bolp) (looking-at "[ \t]*$")))
  6992. ((bolp) (open-line 1))
  6993. (t (end-of-line)
  6994. (insert "\n")))
  6995. ;; Insert the new heading
  6996. (insert stars)
  6997. (just-one-space)
  6998. (insert initial-content)
  6999. (when (and adjust-empty-lines
  7000. (not (and blank (org-previous-line-empty-p))))
  7001. (org-N-empty-lines-before-current (if blank 1 0)))
  7002. (run-hooks 'org-insert-heading-hook)))))))
  7003. (defun org-N-empty-lines-before-current (N)
  7004. "Make the number of empty lines before current exactly N.
  7005. So this will delete or add empty lines."
  7006. (save-excursion
  7007. (beginning-of-line)
  7008. (let ((p (point)))
  7009. (skip-chars-backward " \r\t\n")
  7010. (unless (bolp) (forward-line))
  7011. (delete-region (point) p))
  7012. (when (> N 0) (insert (make-string N ?\n)))))
  7013. (defun org-get-heading (&optional no-tags no-todo)
  7014. "Return the heading of the current entry, without the stars.
  7015. When NO-TAGS is non-nil, don't include tags.
  7016. When NO-TODO is non-nil, don't include TODO keywords."
  7017. (save-excursion
  7018. (org-back-to-heading t)
  7019. (cond
  7020. ((and no-tags no-todo)
  7021. (looking-at org-complex-heading-regexp)
  7022. (match-string 4))
  7023. (no-tags
  7024. (looking-at (concat org-outline-regexp
  7025. "\\(.*?\\)"
  7026. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7027. (match-string 1))
  7028. (no-todo
  7029. (looking-at org-todo-line-regexp)
  7030. (match-string 3))
  7031. (t (looking-at org-heading-regexp)
  7032. (match-string 2)))))
  7033. (defvar orgstruct-mode) ; defined below
  7034. (defun org-heading-components ()
  7035. "Return the components of the current heading.
  7036. This is a list with the following elements:
  7037. - the level as an integer
  7038. - the reduced level, different if `org-odd-levels-only' is set.
  7039. - the TODO keyword, or nil
  7040. - the priority character, like ?A, or nil if no priority is given
  7041. - the headline text itself, or the tags string if no headline text
  7042. - the tags string, or nil."
  7043. (save-excursion
  7044. (org-back-to-heading t)
  7045. (if (let (case-fold-search)
  7046. (looking-at
  7047. (if orgstruct-mode
  7048. org-heading-regexp
  7049. org-complex-heading-regexp)))
  7050. (if orgstruct-mode
  7051. (list (length (match-string 1))
  7052. (org-reduced-level (length (match-string 1)))
  7053. nil
  7054. nil
  7055. (match-string 2)
  7056. nil)
  7057. (list (length (match-string 1))
  7058. (org-reduced-level (length (match-string 1)))
  7059. (org-match-string-no-properties 2)
  7060. (and (match-end 3) (aref (match-string 3) 2))
  7061. (org-match-string-no-properties 4)
  7062. (org-match-string-no-properties 5))))))
  7063. (defun org-get-entry ()
  7064. "Get the entry text, after heading, entire subtree."
  7065. (save-excursion
  7066. (org-back-to-heading t)
  7067. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7068. (defun org-insert-heading-after-current ()
  7069. "Insert a new heading with same level as current, after current subtree."
  7070. (interactive)
  7071. (org-back-to-heading)
  7072. (org-insert-heading)
  7073. (org-move-subtree-down)
  7074. (end-of-line 1))
  7075. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7076. "Insert heading with `org-insert-heading-respect-content' set to t."
  7077. (interactive)
  7078. (org-insert-heading '(4) invisible-ok))
  7079. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7080. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7081. (interactive)
  7082. (org-insert-todo-heading force-state '(4)))
  7083. (defun org-insert-todo-heading (arg &optional force-heading)
  7084. "Insert a new heading with the same level and TODO state as current heading.
  7085. If the heading has no TODO state, or if the state is DONE, use the first
  7086. state (TODO by default). Also with one prefix arg, force first state. With
  7087. two prefix args, force inserting at the end of the parent subtree."
  7088. (interactive "P")
  7089. (when (or force-heading (not (org-insert-item 'checkbox)))
  7090. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7091. force-heading))
  7092. (save-excursion
  7093. (org-back-to-heading)
  7094. (outline-previous-heading)
  7095. (looking-at org-todo-line-regexp))
  7096. (let*
  7097. ((new-mark-x
  7098. (if (or (equal arg '(4))
  7099. (not (match-beginning 2))
  7100. (member (match-string 2) org-done-keywords))
  7101. (car org-todo-keywords-1)
  7102. (match-string 2)))
  7103. (new-mark
  7104. (or
  7105. (run-hook-with-args-until-success
  7106. 'org-todo-get-default-hook new-mark-x nil)
  7107. new-mark-x)))
  7108. (beginning-of-line 1)
  7109. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7110. (if org-treat-insert-todo-heading-as-state-change
  7111. (org-todo new-mark)
  7112. (insert new-mark " "))))
  7113. (when org-provide-todo-statistics
  7114. (org-update-parent-todo-statistics))))
  7115. (defun org-insert-subheading (arg)
  7116. "Insert a new subheading and demote it.
  7117. Works for outline headings and for plain lists alike."
  7118. (interactive "P")
  7119. (org-insert-heading arg)
  7120. (cond
  7121. ((org-at-heading-p) (org-do-demote))
  7122. ((org-at-item-p) (org-indent-item))))
  7123. (defun org-insert-todo-subheading (arg)
  7124. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7125. Works for outline headings and for plain lists alike."
  7126. (interactive "P")
  7127. (org-insert-todo-heading arg)
  7128. (cond
  7129. ((org-at-heading-p) (org-do-demote))
  7130. ((org-at-item-p) (org-indent-item))))
  7131. ;;; Promotion and Demotion
  7132. (defvar org-after-demote-entry-hook nil
  7133. "Hook run after an entry has been demoted.
  7134. The cursor will be at the beginning of the entry.
  7135. When a subtree is being demoted, the hook will be called for each node.")
  7136. (defvar org-after-promote-entry-hook nil
  7137. "Hook run after an entry has been promoted.
  7138. The cursor will be at the beginning of the entry.
  7139. When a subtree is being promoted, the hook will be called for each node.")
  7140. (defun org-promote-subtree ()
  7141. "Promote the entire subtree.
  7142. See also `org-promote'."
  7143. (interactive)
  7144. (save-excursion
  7145. (org-with-limited-levels (org-map-tree 'org-promote)))
  7146. (org-fix-position-after-promote))
  7147. (defun org-demote-subtree ()
  7148. "Demote the entire subtree. See `org-demote'.
  7149. See also `org-promote'."
  7150. (interactive)
  7151. (save-excursion
  7152. (org-with-limited-levels (org-map-tree 'org-demote)))
  7153. (org-fix-position-after-promote))
  7154. (defun org-do-promote ()
  7155. "Promote the current heading higher up the tree.
  7156. If the region is active in `transient-mark-mode', promote all headings
  7157. in the region."
  7158. (interactive)
  7159. (save-excursion
  7160. (if (org-region-active-p)
  7161. (org-map-region 'org-promote (region-beginning) (region-end))
  7162. (org-promote)))
  7163. (org-fix-position-after-promote))
  7164. (defun org-do-demote ()
  7165. "Demote the current heading lower down the tree.
  7166. If the region is active in `transient-mark-mode', demote all headings
  7167. in the region."
  7168. (interactive)
  7169. (save-excursion
  7170. (if (org-region-active-p)
  7171. (org-map-region 'org-demote (region-beginning) (region-end))
  7172. (org-demote)))
  7173. (org-fix-position-after-promote))
  7174. (defun org-fix-position-after-promote ()
  7175. "Make sure that after pro/demotion cursor position is right."
  7176. (let ((pos (point)))
  7177. (when (save-excursion
  7178. (beginning-of-line 1)
  7179. (looking-at org-todo-line-regexp)
  7180. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7181. (cond ((eobp) (insert " "))
  7182. ((eolp) (insert " "))
  7183. ((equal (char-after) ?\ ) (forward-char 1))))))
  7184. (defun org-current-level ()
  7185. "Return the level of the current entry, or nil if before the first headline.
  7186. The level is the number of stars at the beginning of the
  7187. headline. Use `org-reduced-level' to remove the effect of
  7188. `org-odd-levels'. Unlike to `org-outline-level', this function
  7189. ignores inlinetasks."
  7190. (let ((level (org-with-limited-levels (org-outline-level))))
  7191. (and (> level 0) level)))
  7192. (defun org-get-previous-line-level ()
  7193. "Return the outline depth of the last headline before the current line.
  7194. Returns 0 for the first headline in the buffer, and nil if before the
  7195. first headline."
  7196. (and (org-current-level)
  7197. (or (and (/= (line-beginning-position) (point-min))
  7198. (save-excursion (beginning-of-line 0) (org-current-level)))
  7199. 0)))
  7200. (defun org-reduced-level (l)
  7201. "Compute the effective level of a heading.
  7202. This takes into account the setting of `org-odd-levels-only'."
  7203. (cond
  7204. ((zerop l) 0)
  7205. (org-odd-levels-only (1+ (floor (/ l 2))))
  7206. (t l)))
  7207. (defun org-level-increment ()
  7208. "Return the number of stars that will be added or removed at a
  7209. time to headlines when structure editing, based on the value of
  7210. `org-odd-levels-only'."
  7211. (if org-odd-levels-only 2 1))
  7212. (defun org-get-valid-level (level &optional change)
  7213. "Rectify a level change under the influence of `org-odd-levels-only'
  7214. LEVEL is a current level, CHANGE is by how much the level should be
  7215. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7216. even level numbers will become the next higher odd number."
  7217. (if org-odd-levels-only
  7218. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7219. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7220. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7221. (max 1 (+ level (or change 0)))))
  7222. (if (boundp 'define-obsolete-function-alias)
  7223. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7224. (define-obsolete-function-alias 'org-get-legal-level
  7225. 'org-get-valid-level)
  7226. (define-obsolete-function-alias 'org-get-legal-level
  7227. 'org-get-valid-level "23.1")))
  7228. (defun org-promote ()
  7229. "Promote the current heading higher up the tree."
  7230. (org-with-wide-buffer
  7231. (org-back-to-heading t)
  7232. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7233. after-change-functions))
  7234. (level (save-match-data (funcall outline-level)))
  7235. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7236. (diff (abs (- level (length up-head) -1))))
  7237. (cond
  7238. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7239. (replace-match "# " nil t))
  7240. ((= level 1)
  7241. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7242. (t (replace-match up-head nil t)))
  7243. (unless (= level 1)
  7244. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7245. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7246. (run-hooks 'org-after-promote-entry-hook))))
  7247. (defun org-demote ()
  7248. "Demote the current heading lower down the tree."
  7249. (org-with-wide-buffer
  7250. (org-back-to-heading t)
  7251. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7252. after-change-functions))
  7253. (level (save-match-data (funcall outline-level)))
  7254. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7255. (diff (abs (- level (length down-head) -1))))
  7256. (replace-match down-head nil t)
  7257. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7258. (when org-adapt-indentation (org-fixup-indentation diff))
  7259. (run-hooks 'org-after-demote-entry-hook))))
  7260. (defun org-cycle-level ()
  7261. "Cycle the level of an empty headline through possible states.
  7262. This goes first to child, then to parent, level, then up the hierarchy.
  7263. After top level, it switches back to sibling level."
  7264. (interactive)
  7265. (let ((org-adapt-indentation nil))
  7266. (when (org-point-at-end-of-empty-headline)
  7267. (setq this-command 'org-cycle-level) ; Only needed for caching
  7268. (let ((cur-level (org-current-level))
  7269. (prev-level (org-get-previous-line-level)))
  7270. (cond
  7271. ;; If first headline in file, promote to top-level.
  7272. ((= prev-level 0)
  7273. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7274. do (org-do-promote)))
  7275. ;; If same level as prev, demote one.
  7276. ((= prev-level cur-level)
  7277. (org-do-demote))
  7278. ;; If parent is top-level, promote to top level if not already.
  7279. ((= prev-level 1)
  7280. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7281. do (org-do-promote)))
  7282. ;; If top-level, return to prev-level.
  7283. ((= cur-level 1)
  7284. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7285. do (org-do-demote)))
  7286. ;; If less than prev-level, promote one.
  7287. ((< cur-level prev-level)
  7288. (org-do-promote))
  7289. ;; If deeper than prev-level, promote until higher than
  7290. ;; prev-level.
  7291. ((> cur-level prev-level)
  7292. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7293. do (org-do-promote))))
  7294. t))))
  7295. (defun org-map-tree (fun)
  7296. "Call FUN for every heading underneath the current one."
  7297. (org-back-to-heading)
  7298. (let ((level (funcall outline-level)))
  7299. (save-excursion
  7300. (funcall fun)
  7301. (while (and (progn
  7302. (outline-next-heading)
  7303. (> (funcall outline-level) level))
  7304. (not (eobp)))
  7305. (funcall fun)))))
  7306. (defun org-map-region (fun beg end)
  7307. "Call FUN for every heading between BEG and END."
  7308. (let ((org-ignore-region t))
  7309. (save-excursion
  7310. (setq end (copy-marker end))
  7311. (goto-char beg)
  7312. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7313. (< (point) end))
  7314. (funcall fun))
  7315. (while (and (progn
  7316. (outline-next-heading)
  7317. (< (point) end))
  7318. (not (eobp)))
  7319. (funcall fun)))))
  7320. (defun org-fixup-indentation (diff)
  7321. "Change the indentation in the current entry by DIFF.
  7322. DIFF is an integer. Indentation is done according to the
  7323. following rules:
  7324. - Planning information and property drawers are always indented
  7325. according to the new level of the headline;
  7326. - Footnote definitions and their contents are ignored;
  7327. - Inlinetasks' boundaries are not shifted;
  7328. - Empty lines are ignored;
  7329. - Other lines' indentation are shifted by DIFF columns, unless
  7330. it would introduce a structural change in the document, in
  7331. which case no shifting is done at all.
  7332. Assume point is at a heading or an inlinetask beginning."
  7333. (org-with-wide-buffer
  7334. (narrow-to-region (line-beginning-position)
  7335. (save-excursion
  7336. (if (org-with-limited-levels (org-at-heading-p))
  7337. (org-with-limited-levels (outline-next-heading))
  7338. (org-inlinetask-goto-end))
  7339. (point)))
  7340. (forward-line)
  7341. ;; Indent properly planning info and property drawer.
  7342. (when (org-looking-at-p org-planning-line-re)
  7343. (org-indent-line)
  7344. (forward-line))
  7345. (when (looking-at org-property-drawer-re)
  7346. (goto-char (match-end 0))
  7347. (forward-line)
  7348. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7349. (catch 'no-shift
  7350. (when (zerop diff) (throw 'no-shift nil))
  7351. ;; If DIFF is negative, first check if a shift is possible at all
  7352. ;; (e.g., it doesn't break structure). This can only happen if
  7353. ;; some contents are not properly indented.
  7354. (let ((case-fold-search t))
  7355. (when (< diff 0)
  7356. (let ((diff (- diff))
  7357. (forbidden-re (concat org-outline-regexp
  7358. "\\|"
  7359. (substring org-footnote-definition-re 1))))
  7360. (save-excursion
  7361. (while (not (eobp))
  7362. (cond
  7363. ((org-looking-at-p "[ \t]*$") (forward-line))
  7364. ((and (org-looking-at-p org-footnote-definition-re)
  7365. (let ((e (org-element-at-point)))
  7366. (and (eq (org-element-type e) 'footnote-definition)
  7367. (goto-char (org-element-property :end e))))))
  7368. ((org-looking-at-p org-outline-regexp) (forward-line))
  7369. ;; Give up if shifting would move before column 0 or
  7370. ;; if it would introduce a headline or a footnote
  7371. ;; definition.
  7372. (t
  7373. (skip-chars-forward " \t")
  7374. (let ((ind (current-column)))
  7375. (when (or (< ind diff)
  7376. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7377. (throw 'no-shift nil)))
  7378. ;; Ignore contents of example blocks and source
  7379. ;; blocks if their indentation is meant to be
  7380. ;; preserved. Jump to block's closing line.
  7381. (beginning-of-line)
  7382. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7383. (let ((e (org-element-at-point)))
  7384. (and (memq (org-element-type e)
  7385. '(example-block src-block))
  7386. (or org-src-preserve-indentation
  7387. (org-element-property :preserve-indent e))
  7388. (goto-char (org-element-property :end e))
  7389. (progn (skip-chars-backward " \r\t\n")
  7390. (beginning-of-line)
  7391. t))))
  7392. (forward-line))))))))
  7393. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7394. ;; by DIFF. Also skip contents of source or example blocks
  7395. ;; when indentation is meant to be preserved.
  7396. (while (not (eobp))
  7397. (cond
  7398. ((and (org-looking-at-p org-footnote-definition-re)
  7399. (let ((e (org-element-at-point)))
  7400. (and (eq (org-element-type e) 'footnote-definition)
  7401. (goto-char (org-element-property :end e))))))
  7402. ((org-looking-at-p org-outline-regexp) (forward-line))
  7403. ((org-looking-at-p "[ \t]*$") (forward-line))
  7404. (t
  7405. (org-indent-line-to (+ (org-get-indentation) diff))
  7406. (beginning-of-line)
  7407. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7408. (let ((e (org-element-at-point)))
  7409. (and (memq (org-element-type e)
  7410. '(example-block src-block))
  7411. (or org-src-preserve-indentation
  7412. (org-element-property :preserve-indent e))
  7413. (goto-char (org-element-property :end e))
  7414. (progn (skip-chars-backward " \r\t\n")
  7415. (beginning-of-line)
  7416. t))))
  7417. (forward-line)))))))))
  7418. (defun org-convert-to-odd-levels ()
  7419. "Convert an org-mode file with all levels allowed to one with odd levels.
  7420. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7421. level 5 etc."
  7422. (interactive)
  7423. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7424. (let ((outline-level 'org-outline-level)
  7425. (org-odd-levels-only nil) n)
  7426. (save-excursion
  7427. (goto-char (point-min))
  7428. (while (re-search-forward "^\\*\\*+ " nil t)
  7429. (setq n (- (length (match-string 0)) 2))
  7430. (while (>= (setq n (1- n)) 0)
  7431. (org-demote))
  7432. (end-of-line 1))))))
  7433. (defun org-convert-to-oddeven-levels ()
  7434. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7435. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7436. file contains a section with an even level, conversion would
  7437. destroy the structure of the file. An error is signaled in this
  7438. case."
  7439. (interactive)
  7440. (goto-char (point-min))
  7441. ;; First check if there are no even levels
  7442. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7443. (org-show-set-visibility 'canonical)
  7444. (error "Not all levels are odd in this file. Conversion not possible"))
  7445. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7446. (let ((outline-regexp org-outline-regexp)
  7447. (outline-level 'org-outline-level)
  7448. (org-odd-levels-only nil) n)
  7449. (save-excursion
  7450. (goto-char (point-min))
  7451. (while (re-search-forward "^\\*\\*+ " nil t)
  7452. (setq n (/ (1- (length (match-string 0))) 2))
  7453. (while (>= (setq n (1- n)) 0)
  7454. (org-promote))
  7455. (end-of-line 1))))))
  7456. (defun org-tr-level (n)
  7457. "Make N odd if required."
  7458. (if org-odd-levels-only (1+ (/ n 2)) n))
  7459. ;;; Vertical tree motion, cutting and pasting of subtrees
  7460. (defun org-move-subtree-up (&optional arg)
  7461. "Move the current subtree up past ARG headlines of the same level."
  7462. (interactive "p")
  7463. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7464. (defun org-move-subtree-down (&optional arg)
  7465. "Move the current subtree down past ARG headlines of the same level."
  7466. (interactive "p")
  7467. (setq arg (prefix-numeric-value arg))
  7468. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7469. 'org-get-last-sibling))
  7470. (ins-point (make-marker))
  7471. (cnt (abs arg))
  7472. (col (current-column))
  7473. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7474. ;; Select the tree
  7475. (org-back-to-heading)
  7476. (setq beg0 (point))
  7477. (save-excursion
  7478. (setq ne-beg (org-back-over-empty-lines))
  7479. (setq beg (point)))
  7480. (save-match-data
  7481. (save-excursion (outline-end-of-heading)
  7482. (setq folded (outline-invisible-p)))
  7483. (progn (org-end-of-subtree nil t)
  7484. (unless (eobp) (backward-char))))
  7485. (outline-next-heading)
  7486. (setq ne-end (org-back-over-empty-lines))
  7487. (setq end (point))
  7488. (goto-char beg0)
  7489. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7490. ;; include less whitespace
  7491. (save-excursion
  7492. (goto-char beg)
  7493. (forward-line (- ne-beg ne-end))
  7494. (setq beg (point))))
  7495. ;; Find insertion point, with error handling
  7496. (while (> cnt 0)
  7497. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7498. (progn (goto-char beg0)
  7499. (user-error "Cannot move past superior level or buffer limit")))
  7500. (setq cnt (1- cnt)))
  7501. (if (> arg 0)
  7502. ;; Moving forward - still need to move over subtree
  7503. (progn (org-end-of-subtree t t)
  7504. (save-excursion
  7505. (org-back-over-empty-lines)
  7506. (or (bolp) (newline)))))
  7507. (setq ne-ins (org-back-over-empty-lines))
  7508. (move-marker ins-point (point))
  7509. (setq txt (buffer-substring beg end))
  7510. (org-save-markers-in-region beg end)
  7511. (delete-region beg end)
  7512. (org-remove-empty-overlays-at beg)
  7513. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7514. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7515. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7516. (let ((bbb (point)))
  7517. (insert-before-markers txt)
  7518. (org-reinstall-markers-in-region bbb)
  7519. (move-marker ins-point bbb))
  7520. (or (bolp) (insert "\n"))
  7521. (setq ins-end (point))
  7522. (goto-char ins-point)
  7523. (org-skip-whitespace)
  7524. (when (and (< arg 0)
  7525. (org-first-sibling-p)
  7526. (> ne-ins ne-beg))
  7527. ;; Move whitespace back to beginning
  7528. (save-excursion
  7529. (goto-char ins-end)
  7530. (let ((kill-whole-line t))
  7531. (kill-line (- ne-ins ne-beg)) (point)))
  7532. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7533. (move-marker ins-point nil)
  7534. (if folded
  7535. (hide-subtree)
  7536. (org-show-entry)
  7537. (show-children)
  7538. (org-cycle-hide-drawers 'children))
  7539. (org-clean-visibility-after-subtree-move)
  7540. ;; move back to the initial column we were at
  7541. (move-to-column col)))
  7542. (defvar org-subtree-clip ""
  7543. "Clipboard for cut and paste of subtrees.
  7544. This is actually only a copy of the kill, because we use the normal kill
  7545. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7546. (defvar org-subtree-clip-folded nil
  7547. "Was the last copied subtree folded?
  7548. This is used to fold the tree back after pasting.")
  7549. (defun org-cut-subtree (&optional n)
  7550. "Cut the current subtree into the clipboard.
  7551. With prefix arg N, cut this many sequential subtrees.
  7552. This is a short-hand for marking the subtree and then cutting it."
  7553. (interactive "p")
  7554. (org-copy-subtree n 'cut))
  7555. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7556. "Copy the current subtree it in the clipboard.
  7557. With prefix arg N, copy this many sequential subtrees.
  7558. This is a short-hand for marking the subtree and then copying it.
  7559. If CUT is non-nil, actually cut the subtree.
  7560. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7561. of some markers in the region, even if CUT is non-nil. This is
  7562. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7563. (interactive "p")
  7564. (let (beg end folded (beg0 (point)))
  7565. (if (org-called-interactively-p 'any)
  7566. (org-back-to-heading nil) ; take what looks like a subtree
  7567. (org-back-to-heading t)) ; take what is really there
  7568. (setq beg (point))
  7569. (skip-chars-forward " \t\r\n")
  7570. (save-match-data
  7571. (if nosubtrees
  7572. (outline-next-heading)
  7573. (save-excursion (outline-end-of-heading)
  7574. (setq folded (outline-invisible-p)))
  7575. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7576. (org-end-of-subtree t t)))
  7577. (setq end (point))
  7578. (goto-char beg0)
  7579. (when (> end beg)
  7580. (setq org-subtree-clip-folded folded)
  7581. (when (or cut force-store-markers)
  7582. (org-save-markers-in-region beg end))
  7583. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7584. (setq org-subtree-clip (current-kill 0))
  7585. (message "%s: Subtree(s) with %d characters"
  7586. (if cut "Cut" "Copied")
  7587. (length org-subtree-clip)))))
  7588. (defun org-paste-subtree (&optional level tree for-yank remove)
  7589. "Paste the clipboard as a subtree, with modification of headline level.
  7590. The entire subtree is promoted or demoted in order to match a new headline
  7591. level.
  7592. If the cursor is at the beginning of a headline, the same level as
  7593. that headline is used to paste the tree.
  7594. If not, the new level is derived from the *visible* headings
  7595. before and after the insertion point, and taken to be the inferior headline
  7596. level of the two. So if the previous visible heading is level 3 and the
  7597. next is level 4 (or vice versa), level 4 will be used for insertion.
  7598. This makes sure that the subtree remains an independent subtree and does
  7599. not swallow low level entries.
  7600. You can also force a different level, either by using a numeric prefix
  7601. argument, or by inserting the heading marker by hand. For example, if the
  7602. cursor is after \"*****\", then the tree will be shifted to level 5.
  7603. If optional TREE is given, use this text instead of the kill ring.
  7604. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7605. move back over whitespace before inserting, and move point to the end of
  7606. the inserted text when done.
  7607. When REMOVE is non-nil, remove the subtree from the clipboard."
  7608. (interactive "P")
  7609. (setq tree (or tree (and kill-ring (current-kill 0))))
  7610. (unless (org-kill-is-subtree-p tree)
  7611. (user-error "%s"
  7612. (substitute-command-keys
  7613. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7614. (org-with-limited-levels
  7615. (let* ((visp (not (outline-invisible-p)))
  7616. (txt tree)
  7617. (^re_ "\\(\\*+\\)[ \t]*")
  7618. (old-level (if (string-match org-outline-regexp-bol txt)
  7619. (- (match-end 0) (match-beginning 0) 1)
  7620. -1))
  7621. (force-level (cond (level (prefix-numeric-value level))
  7622. ((and (looking-at "[ \t]*$")
  7623. (string-match
  7624. "^\\*+$" (buffer-substring
  7625. (point-at-bol) (point))))
  7626. (- (match-end 0) (match-beginning 0)))
  7627. ((and (bolp)
  7628. (looking-at org-outline-regexp))
  7629. (- (match-end 0) (point) 1))))
  7630. (previous-level (save-excursion
  7631. (condition-case nil
  7632. (progn
  7633. (outline-previous-visible-heading 1)
  7634. (if (looking-at ^re_)
  7635. (- (match-end 0) (match-beginning 0) 1)
  7636. 1))
  7637. (error 1))))
  7638. (next-level (save-excursion
  7639. (condition-case nil
  7640. (progn
  7641. (or (looking-at org-outline-regexp)
  7642. (outline-next-visible-heading 1))
  7643. (if (looking-at ^re_)
  7644. (- (match-end 0) (match-beginning 0) 1)
  7645. 1))
  7646. (error 1))))
  7647. (new-level (or force-level (max previous-level next-level)))
  7648. (shift (if (or (= old-level -1)
  7649. (= new-level -1)
  7650. (= old-level new-level))
  7651. 0
  7652. (- new-level old-level)))
  7653. (delta (if (> shift 0) -1 1))
  7654. (func (if (> shift 0) 'org-demote 'org-promote))
  7655. (org-odd-levels-only nil)
  7656. beg end newend)
  7657. ;; Remove the forced level indicator
  7658. (if force-level
  7659. (delete-region (point-at-bol) (point)))
  7660. ;; Paste
  7661. (beginning-of-line (if (bolp) 1 2))
  7662. (setq beg (point))
  7663. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7664. (insert-before-markers txt)
  7665. (unless (string-match "\n\\'" txt) (insert "\n"))
  7666. (setq newend (point))
  7667. (org-reinstall-markers-in-region beg)
  7668. (setq end (point))
  7669. (goto-char beg)
  7670. (skip-chars-forward " \t\n\r")
  7671. (setq beg (point))
  7672. (if (and (outline-invisible-p) visp)
  7673. (save-excursion (outline-show-heading)))
  7674. ;; Shift if necessary
  7675. (unless (= shift 0)
  7676. (save-restriction
  7677. (narrow-to-region beg end)
  7678. (while (not (= shift 0))
  7679. (org-map-region func (point-min) (point-max))
  7680. (setq shift (+ delta shift)))
  7681. (goto-char (point-min))
  7682. (setq newend (point-max))))
  7683. (when (or (org-called-interactively-p 'interactive) for-yank)
  7684. (message "Clipboard pasted as level %d subtree" new-level))
  7685. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7686. kill-ring
  7687. (eq org-subtree-clip (current-kill 0))
  7688. org-subtree-clip-folded)
  7689. ;; The tree was folded before it was killed/copied
  7690. (hide-subtree))
  7691. (and for-yank (goto-char newend))
  7692. (and remove (setq kill-ring (cdr kill-ring))))))
  7693. (defun org-kill-is-subtree-p (&optional txt)
  7694. "Check if the current kill is an outline subtree, or a set of trees.
  7695. Returns nil if kill does not start with a headline, or if the first
  7696. headline level is not the largest headline level in the tree.
  7697. So this will actually accept several entries of equal levels as well,
  7698. which is OK for `org-paste-subtree'.
  7699. If optional TXT is given, check this string instead of the current kill."
  7700. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7701. (re (org-get-limited-outline-regexp))
  7702. (^re (concat "^" re))
  7703. (start-level (and kill
  7704. (string-match
  7705. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7706. kill)
  7707. (- (match-end 2) (match-beginning 2) 1)))
  7708. (start (1+ (or (match-beginning 2) -1))))
  7709. (if (not start-level)
  7710. (progn
  7711. nil) ;; does not even start with a heading
  7712. (catch 'exit
  7713. (while (setq start (string-match ^re kill (1+ start)))
  7714. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7715. (throw 'exit nil)))
  7716. t))))
  7717. (defvar org-markers-to-move nil
  7718. "Markers that should be moved with a cut-and-paste operation.
  7719. Those markers are stored together with their positions relative to
  7720. the start of the region.")
  7721. (defun org-save-markers-in-region (beg end)
  7722. "Check markers in region.
  7723. If these markers are between BEG and END, record their position relative
  7724. to BEG, so that after moving the block of text, we can put the markers back
  7725. into place.
  7726. This function gets called just before an entry or tree gets cut from the
  7727. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7728. called immediately, to move the markers with the entries."
  7729. (setq org-markers-to-move nil)
  7730. (when (featurep 'org-clock)
  7731. (org-clock-save-markers-for-cut-and-paste beg end))
  7732. (when (featurep 'org-agenda)
  7733. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7734. (defun org-check-and-save-marker (marker beg end)
  7735. "Check if MARKER is between BEG and END.
  7736. If yes, remember the marker and the distance to BEG."
  7737. (when (and (marker-buffer marker)
  7738. (equal (marker-buffer marker) (current-buffer)))
  7739. (if (and (>= marker beg) (< marker end))
  7740. (push (cons marker (- marker beg)) org-markers-to-move))))
  7741. (defun org-reinstall-markers-in-region (beg)
  7742. "Move all remembered markers to their position relative to BEG."
  7743. (mapc (lambda (x)
  7744. (move-marker (car x) (+ beg (cdr x))))
  7745. org-markers-to-move)
  7746. (setq org-markers-to-move nil))
  7747. (defun org-narrow-to-subtree ()
  7748. "Narrow buffer to the current subtree."
  7749. (interactive)
  7750. (save-excursion
  7751. (save-match-data
  7752. (org-with-limited-levels
  7753. (narrow-to-region
  7754. (progn (org-back-to-heading t) (point))
  7755. (progn (org-end-of-subtree t t)
  7756. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7757. (point)))))))
  7758. (defun org-narrow-to-block ()
  7759. "Narrow buffer to the current block."
  7760. (interactive)
  7761. (let* ((case-fold-search t)
  7762. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7763. "^[ \t]*#\\+end_.*")))
  7764. (if blockp
  7765. (narrow-to-region (car blockp) (cdr blockp))
  7766. (user-error "Not in a block"))))
  7767. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7768. "Clone the task (subtree) at point N times.
  7769. The clones will be inserted as siblings.
  7770. In interactive use, the user will be prompted for the number of
  7771. clones to be produced. If the entry has a timestamp, the user
  7772. will also be prompted for a time shift, which may be a repeater
  7773. as used in time stamps, for example `+3d'. To disable this,
  7774. you can call the function with a universal prefix argument.
  7775. When a valid repeater is given and the entry contains any time
  7776. stamps, the clones will become a sequence in time, with time
  7777. stamps in the subtree shifted for each clone produced. If SHIFT
  7778. is nil or the empty string, time stamps will be left alone. The
  7779. ID property of the original subtree is removed.
  7780. If the original subtree did contain time stamps with a repeater,
  7781. the following will happen:
  7782. - the repeater will be removed in each clone
  7783. - an additional clone will be produced, with the current, unshifted
  7784. date(s) in the entry.
  7785. - the original entry will be placed *after* all the clones, with
  7786. repeater intact.
  7787. - the start days in the repeater in the original entry will be shifted
  7788. to past the last clone.
  7789. In this way you can spell out a number of instances of a repeating task,
  7790. and still retain the repeater to cover future instances of the task."
  7791. (interactive "nNumber of clones to produce: ")
  7792. (let ((shift
  7793. (or shift
  7794. (if (and (not (equal current-prefix-arg '(4)))
  7795. (save-excursion
  7796. (re-search-forward org-ts-regexp-both
  7797. (save-excursion
  7798. (org-end-of-subtree t)
  7799. (point)) t)))
  7800. (read-from-minibuffer
  7801. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7802. ""))) ;; No time shift
  7803. (n-no-remove -1)
  7804. (drawer-re org-drawer-regexp)
  7805. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7806. beg end template task idprop
  7807. shift-n shift-what doshift nmin nmax)
  7808. (if (not (and (integerp n) (> n 0)))
  7809. (user-error "Invalid number of replications %s" n))
  7810. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7811. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7812. shift)))
  7813. (user-error "Invalid shift specification %s" shift))
  7814. (when doshift
  7815. (setq shift-n (string-to-number (match-string 1 shift))
  7816. shift-what (cdr (assoc (match-string 2 shift)
  7817. '(("d" . day) ("w" . week)
  7818. ("m" . month) ("y" . year))))))
  7819. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7820. (setq nmin 1 nmax n)
  7821. (org-back-to-heading t)
  7822. (setq beg (point))
  7823. (setq idprop (org-entry-get nil "ID"))
  7824. (org-end-of-subtree t t)
  7825. (or (bolp) (insert "\n"))
  7826. (setq end (point))
  7827. (setq template (buffer-substring beg end))
  7828. (when (and doshift
  7829. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7830. (delete-region beg end)
  7831. (setq end beg)
  7832. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7833. (goto-char end)
  7834. (loop for n from nmin to nmax do
  7835. ;; prepare clone
  7836. (with-temp-buffer
  7837. (insert template)
  7838. (org-mode)
  7839. (goto-char (point-min))
  7840. (org-show-subtree)
  7841. (and idprop (if org-clone-delete-id
  7842. (org-entry-delete nil "ID")
  7843. (org-id-get-create t)))
  7844. (unless (= n 0)
  7845. (while (re-search-forward org-clock-re nil t)
  7846. (kill-whole-line))
  7847. (goto-char (point-min))
  7848. (while (re-search-forward drawer-re nil t)
  7849. (org-remove-empty-drawer-at (point))))
  7850. (goto-char (point-min))
  7851. (when doshift
  7852. (while (re-search-forward org-ts-regexp-both nil t)
  7853. (org-timestamp-change (* n shift-n) shift-what))
  7854. (unless (= n n-no-remove)
  7855. (goto-char (point-min))
  7856. (while (re-search-forward org-ts-regexp nil t)
  7857. (save-excursion
  7858. (goto-char (match-beginning 0))
  7859. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7860. (delete-region (match-beginning 1) (match-end 1)))))))
  7861. (setq task (buffer-string)))
  7862. (insert task))
  7863. (goto-char beg)))
  7864. ;;; Outline Sorting
  7865. (defun org-sort (with-case)
  7866. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7867. Optional argument WITH-CASE means sort case-sensitively."
  7868. (interactive "P")
  7869. (cond
  7870. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7871. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7872. (t
  7873. (org-call-with-arg 'org-sort-entries with-case))))
  7874. (defun org-sort-remove-invisible (s)
  7875. "Remove invisible links from string S."
  7876. (remove-text-properties 0 (length s) org-rm-props s)
  7877. (while (string-match org-bracket-link-regexp s)
  7878. (setq s (replace-match (if (match-end 2)
  7879. (match-string 3 s)
  7880. (match-string 1 s)) t t s)))
  7881. (let ((st (format " %s " s)))
  7882. (while (string-match org-emph-re st)
  7883. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7884. (setq s (substring st 1 -1)))
  7885. s)
  7886. (defvar org-priority-regexp) ; defined later in the file
  7887. (defvar org-after-sorting-entries-or-items-hook nil
  7888. "Hook that is run after a bunch of entries or items have been sorted.
  7889. When children are sorted, the cursor is in the parent line when this
  7890. hook gets called. When a region or a plain list is sorted, the cursor
  7891. will be in the first entry of the sorted region/list.")
  7892. (defun org-sort-entries
  7893. (&optional with-case sorting-type getkey-func compare-func property)
  7894. "Sort entries on a certain level of an outline tree.
  7895. If there is an active region, the entries in the region are sorted.
  7896. Else, if the cursor is before the first entry, sort the top-level items.
  7897. Else, the children of the entry at point are sorted.
  7898. Sorting can be alphabetically, numerically, by date/time as given by
  7899. a time stamp, by a property, by priority order, or by a custom function.
  7900. The command prompts for the sorting type unless it has been given to the
  7901. function through the SORTING-TYPE argument, which needs to be a character,
  7902. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7903. the precise meaning of each character:
  7904. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7905. c By creation time, which is assumed to be the first inactive time stamp
  7906. at the beginning of a line.
  7907. d By deadline date/time.
  7908. k By clocking time.
  7909. n Numerically, by converting the beginning of the entry/item to a number.
  7910. o By order of TODO keywords.
  7911. p By priority according to the cookie.
  7912. r By the value of a property.
  7913. s By scheduled date/time.
  7914. t By date/time, either the first active time stamp in the entry, or, if
  7915. none exist, by the first inactive one.
  7916. Capital letters will reverse the sort order.
  7917. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7918. called with point at the beginning of the record. It must return either
  7919. a string or a number that should serve as the sorting key for that record.
  7920. Comparing entries ignores case by default. However, with an optional argument
  7921. WITH-CASE, the sorting considers case as well.
  7922. Sorting is done against the visible part of the headlines, it ignores hidden
  7923. links.
  7924. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7925. (interactive "P")
  7926. (let ((case-func (if with-case 'identity 'downcase))
  7927. (cmstr
  7928. ;; The clock marker is lost when using `sort-subr', let's
  7929. ;; store the clocking string.
  7930. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7931. (save-excursion
  7932. (goto-char org-clock-marker)
  7933. (looking-back "^.*") (match-string-no-properties 0))))
  7934. start beg end stars re re2
  7935. txt what tmp)
  7936. ;; Find beginning and end of region to sort
  7937. (cond
  7938. ((org-region-active-p)
  7939. ;; we will sort the region
  7940. (setq end (region-end)
  7941. what "region")
  7942. (goto-char (region-beginning))
  7943. (if (not (org-at-heading-p)) (outline-next-heading))
  7944. (setq start (point)))
  7945. ((or (org-at-heading-p)
  7946. (ignore-errors (progn (org-back-to-heading) t)))
  7947. ;; we will sort the children of the current headline
  7948. (org-back-to-heading)
  7949. (setq start (point)
  7950. end (progn (org-end-of-subtree t t)
  7951. (or (bolp) (insert "\n"))
  7952. (when (>= (org-back-over-empty-lines) 1)
  7953. (forward-line 1))
  7954. (point))
  7955. what "children")
  7956. (goto-char start)
  7957. (show-subtree)
  7958. (outline-next-heading))
  7959. (t
  7960. ;; we will sort the top-level entries in this file
  7961. (goto-char (point-min))
  7962. (or (org-at-heading-p) (outline-next-heading))
  7963. (setq start (point))
  7964. (goto-char (point-max))
  7965. (beginning-of-line 1)
  7966. (when (looking-at ".*?\\S-")
  7967. ;; File ends in a non-white line
  7968. (end-of-line 1)
  7969. (insert "\n"))
  7970. (setq end (point-max))
  7971. (setq what "top-level")
  7972. (goto-char start)
  7973. (show-all)))
  7974. (setq beg (point))
  7975. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7976. (looking-at "\\(\\*+\\)")
  7977. (setq stars (match-string 1)
  7978. re (concat "^" (regexp-quote stars) " +")
  7979. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7980. txt (buffer-substring beg end))
  7981. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7982. (if (and (not (equal stars "*")) (string-match re2 txt))
  7983. (user-error "Region to sort contains a level above the first entry"))
  7984. (unless sorting-type
  7985. (message
  7986. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7987. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7988. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7989. what)
  7990. (setq sorting-type (read-char-exclusive))
  7991. (unless getkey-func
  7992. (and (= (downcase sorting-type) ?f)
  7993. (setq getkey-func
  7994. (org-icompleting-read "Sort using function: "
  7995. obarray 'fboundp t nil nil))
  7996. (setq getkey-func (intern getkey-func))))
  7997. (and (= (downcase sorting-type) ?r)
  7998. (not property)
  7999. (setq property
  8000. (org-icompleting-read "Property: "
  8001. (mapcar 'list (org-buffer-property-keys t))
  8002. nil t))))
  8003. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8004. (message "Sorting entries...")
  8005. (save-restriction
  8006. (narrow-to-region start end)
  8007. (let ((dcst (downcase sorting-type))
  8008. (case-fold-search nil)
  8009. (now (current-time)))
  8010. (sort-subr
  8011. (/= dcst sorting-type)
  8012. ;; This function moves to the beginning character of the "record" to
  8013. ;; be sorted.
  8014. (lambda nil
  8015. (if (re-search-forward re nil t)
  8016. (goto-char (match-beginning 0))
  8017. (goto-char (point-max))))
  8018. ;; This function moves to the last character of the "record" being
  8019. ;; sorted.
  8020. (lambda nil
  8021. (save-match-data
  8022. (condition-case nil
  8023. (outline-forward-same-level 1)
  8024. (error
  8025. (goto-char (point-max))))))
  8026. ;; This function returns the value that gets sorted against.
  8027. (lambda nil
  8028. (cond
  8029. ((= dcst ?n)
  8030. (if (looking-at org-complex-heading-regexp)
  8031. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8032. nil))
  8033. ((= dcst ?a)
  8034. (if (looking-at org-complex-heading-regexp)
  8035. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8036. nil))
  8037. ((= dcst ?k)
  8038. (or (get-text-property (point) :org-clock-minutes) 0))
  8039. ((= dcst ?t)
  8040. (let ((end (save-excursion (outline-next-heading) (point))))
  8041. (if (or (re-search-forward org-ts-regexp end t)
  8042. (re-search-forward org-ts-regexp-both end t))
  8043. (org-time-string-to-seconds (match-string 0))
  8044. (org-float-time now))))
  8045. ((= dcst ?c)
  8046. (let ((end (save-excursion (outline-next-heading) (point))))
  8047. (if (re-search-forward
  8048. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8049. end t)
  8050. (org-time-string-to-seconds (match-string 0))
  8051. (org-float-time now))))
  8052. ((= dcst ?s)
  8053. (let ((end (save-excursion (outline-next-heading) (point))))
  8054. (if (re-search-forward org-scheduled-time-regexp end t)
  8055. (org-time-string-to-seconds (match-string 1))
  8056. (org-float-time now))))
  8057. ((= dcst ?d)
  8058. (let ((end (save-excursion (outline-next-heading) (point))))
  8059. (if (re-search-forward org-deadline-time-regexp end t)
  8060. (org-time-string-to-seconds (match-string 1))
  8061. (org-float-time now))))
  8062. ((= dcst ?p)
  8063. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8064. (string-to-char (match-string 2))
  8065. org-default-priority))
  8066. ((= dcst ?r)
  8067. (or (org-entry-get nil property) ""))
  8068. ((= dcst ?o)
  8069. (if (looking-at org-complex-heading-regexp)
  8070. (let* ((m (match-string 2))
  8071. (s (if (member m org-done-keywords) '- '+)))
  8072. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8073. ((= dcst ?f)
  8074. (if getkey-func
  8075. (progn
  8076. (setq tmp (funcall getkey-func))
  8077. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8078. tmp)
  8079. (error "Invalid key function `%s'" getkey-func)))
  8080. (t (error "Invalid sorting type `%c'" sorting-type))))
  8081. nil
  8082. (cond
  8083. ((= dcst ?a) 'string<)
  8084. ((= dcst ?f) compare-func)
  8085. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8086. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8087. ;; Reset the clock marker if needed
  8088. (when cmstr
  8089. (save-excursion
  8090. (goto-char start)
  8091. (search-forward cmstr nil t)
  8092. (move-marker org-clock-marker (point))))
  8093. (message "Sorting entries...done")))
  8094. ;;; The orgstruct minor mode
  8095. ;; Define a minor mode which can be used in other modes in order to
  8096. ;; integrate the org-mode structure editing commands.
  8097. ;; This is really a hack, because the org-mode structure commands use
  8098. ;; keys which normally belong to the major mode. Here is how it
  8099. ;; works: The minor mode defines all the keys necessary to operate the
  8100. ;; structure commands, but wraps the commands into a function which
  8101. ;; tests if the cursor is currently at a headline or a plain list
  8102. ;; item. If that is the case, the structure command is used,
  8103. ;; temporarily setting many Org-mode variables like regular
  8104. ;; expressions for filling etc. However, when any of those keys is
  8105. ;; used at a different location, function uses `key-binding' to look
  8106. ;; up if the key has an associated command in another currently active
  8107. ;; keymap (minor modes, major mode, global), and executes that
  8108. ;; command. There might be problems if any of the keys is otherwise
  8109. ;; used as a prefix key.
  8110. (defcustom orgstruct-heading-prefix-regexp ""
  8111. "Regexp that matches the custom prefix of Org headlines in
  8112. orgstruct(++)-mode."
  8113. :group 'org
  8114. :version "24.4"
  8115. :package-version '(Org . "8.3")
  8116. :type 'regexp)
  8117. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8118. (defcustom orgstruct-setup-hook nil
  8119. "Hook run after orgstruct-mode-map is filled."
  8120. :group 'org
  8121. :version "24.4"
  8122. :package-version '(Org . "8.0")
  8123. :type 'hook)
  8124. (defvar orgstruct-initialized nil)
  8125. (defvar org-local-vars nil
  8126. "List of local variables, for use by `orgstruct-mode'.")
  8127. ;;;###autoload
  8128. (define-minor-mode orgstruct-mode
  8129. "Toggle the minor mode `orgstruct-mode'.
  8130. This mode is for using Org-mode structure commands in other
  8131. modes. The following keys behave as if Org-mode were active, if
  8132. the cursor is on a headline, or on a plain list item (both as
  8133. defined by Org-mode)."
  8134. nil " OrgStruct" (make-sparse-keymap)
  8135. (funcall (if orgstruct-mode
  8136. 'add-to-invisibility-spec
  8137. 'remove-from-invisibility-spec)
  8138. '(outline . t))
  8139. (when orgstruct-mode
  8140. (org-load-modules-maybe)
  8141. (unless orgstruct-initialized
  8142. (orgstruct-setup)
  8143. (setq orgstruct-initialized t))))
  8144. ;;;###autoload
  8145. (defun turn-on-orgstruct ()
  8146. "Unconditionally turn on `orgstruct-mode'."
  8147. (orgstruct-mode 1))
  8148. (defvar org-fb-vars nil)
  8149. (make-variable-buffer-local 'org-fb-vars)
  8150. (defun orgstruct++-mode (&optional arg)
  8151. "Toggle `orgstruct-mode', the enhanced version of it.
  8152. In addition to setting orgstruct-mode, this also exports all
  8153. indentation and autofilling variables from org-mode into the
  8154. buffer. It will also recognize item context in multiline items."
  8155. (interactive "P")
  8156. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8157. (if (< arg 1)
  8158. (progn (orgstruct-mode -1)
  8159. (mapc (lambda(v)
  8160. (org-set-local (car v)
  8161. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8162. org-fb-vars))
  8163. (orgstruct-mode 1)
  8164. (setq org-fb-vars nil)
  8165. (unless org-local-vars
  8166. (setq org-local-vars (org-get-local-variables)))
  8167. (let (var val)
  8168. (mapc
  8169. (lambda (x)
  8170. (when (string-match
  8171. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8172. (symbol-name (car x)))
  8173. (setq var (car x) val (nth 1 x))
  8174. (push (list var `(quote ,(eval var))) org-fb-vars)
  8175. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8176. org-local-vars)
  8177. (org-set-local 'orgstruct-is-++ t))))
  8178. (defvar orgstruct-is-++ nil
  8179. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8180. (make-variable-buffer-local 'orgstruct-is-++)
  8181. ;;;###autoload
  8182. (defun turn-on-orgstruct++ ()
  8183. "Unconditionally turn on `orgstruct++-mode'."
  8184. (orgstruct++-mode 1))
  8185. (defun orgstruct-error ()
  8186. "Error when there is no default binding for a structure key."
  8187. (interactive)
  8188. (funcall (if (fboundp 'user-error)
  8189. 'user-error
  8190. 'error)
  8191. "This key has no function outside structure elements"))
  8192. (defun orgstruct-setup ()
  8193. "Setup orgstruct keymap."
  8194. (dolist (cell '((org-demote . t)
  8195. (org-metaleft . t)
  8196. (org-metaright . t)
  8197. (org-promote . t)
  8198. (org-shiftmetaleft . t)
  8199. (org-shiftmetaright . t)
  8200. org-backward-element
  8201. org-backward-heading-same-level
  8202. org-ctrl-c-ret
  8203. org-ctrl-c-minus
  8204. org-ctrl-c-star
  8205. org-cycle
  8206. org-forward-heading-same-level
  8207. org-insert-heading
  8208. org-insert-heading-respect-content
  8209. org-kill-note-or-show-branches
  8210. org-mark-subtree
  8211. org-meta-return
  8212. org-metadown
  8213. org-metaup
  8214. org-narrow-to-subtree
  8215. org-promote-subtree
  8216. org-reveal
  8217. org-shiftdown
  8218. org-shiftleft
  8219. org-shiftmetadown
  8220. org-shiftmetaup
  8221. org-shiftright
  8222. org-shifttab
  8223. org-shifttab
  8224. org-shiftup
  8225. org-show-subtree
  8226. org-sort
  8227. org-up-element
  8228. outline-demote
  8229. outline-next-visible-heading
  8230. outline-previous-visible-heading
  8231. outline-promote
  8232. outline-up-heading
  8233. show-children))
  8234. (let ((f (or (car-safe cell) cell))
  8235. (disable-when-heading-prefix (cdr-safe cell)))
  8236. (when (fboundp f)
  8237. (let ((new-bindings))
  8238. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8239. (where-is-internal f outline-mode-map)))
  8240. (push binding new-bindings)
  8241. ;; TODO use local-function-key-map
  8242. (dolist (rep '(("<tab>" . "TAB")
  8243. ("<return>" . "RET")
  8244. ("<escape>" . "ESC")
  8245. ("<delete>" . "DEL")))
  8246. (setq binding (read-kbd-macro
  8247. (let ((case-fold-search))
  8248. (replace-regexp-in-string
  8249. (regexp-quote (cdr rep))
  8250. (car rep)
  8251. (key-description binding)))))
  8252. (pushnew binding new-bindings :test 'equal)))
  8253. (dolist (binding new-bindings)
  8254. (let ((key (lookup-key orgstruct-mode-map binding)))
  8255. (when (or (not key) (numberp key))
  8256. (ignore-errors
  8257. (org-defkey orgstruct-mode-map
  8258. binding
  8259. (orgstruct-make-binding
  8260. f binding disable-when-heading-prefix))))))))))
  8261. (run-hooks 'orgstruct-setup-hook))
  8262. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8263. "Create a function for binding in the structure minor mode.
  8264. FUN is the command to call inside a table. KEY is the key that
  8265. should be checked in for a command to execute outside of tables.
  8266. Non-nil `disable-when-heading-prefix' means to disable the command
  8267. if `orgstruct-heading-prefix-regexp' is not empty."
  8268. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8269. (let ((nname name)
  8270. (i 0))
  8271. (while (fboundp (intern nname))
  8272. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8273. (setq name (intern nname)))
  8274. (eval
  8275. (let ((bindings '((org-heading-regexp
  8276. (concat "^"
  8277. orgstruct-heading-prefix-regexp
  8278. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8279. (org-outline-regexp
  8280. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8281. (org-outline-regexp-bol
  8282. (concat "^" org-outline-regexp))
  8283. (outline-regexp org-outline-regexp)
  8284. (outline-heading-end-regexp "\n")
  8285. (outline-level 'org-outline-level)
  8286. (outline-heading-alist))))
  8287. `(defun ,name (arg)
  8288. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8289. "Outside of structure, run the binding of `"
  8290. (key-description key) "'."
  8291. (when disable-when-heading-prefix
  8292. (concat
  8293. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8294. "back to the default binding due to limitations of Org's implementation of\n"
  8295. "`" (symbol-name fun) "'.")))
  8296. (interactive "p")
  8297. (let* ((disable
  8298. ,(and disable-when-heading-prefix
  8299. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8300. (fallback
  8301. (or disable
  8302. (not
  8303. (let* ,bindings
  8304. (org-context-p 'headline 'item
  8305. ,(when (memq fun
  8306. '(org-insert-heading
  8307. org-insert-heading-respect-content
  8308. org-meta-return))
  8309. '(when orgstruct-is-++
  8310. 'item-body))))))))
  8311. (if fallback
  8312. (let* ((orgstruct-mode)
  8313. (binding
  8314. (let ((key ,key))
  8315. (catch 'exit
  8316. (dolist
  8317. (rep
  8318. '(nil
  8319. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8320. ("<\\([^>]*\\)return>" . "\\1RET")
  8321. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8322. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8323. nil)
  8324. (when rep
  8325. (setq key (read-kbd-macro
  8326. (let ((case-fold-search))
  8327. (replace-regexp-in-string
  8328. (car rep)
  8329. (cdr rep)
  8330. (key-description key))))))
  8331. (when (key-binding key)
  8332. (throw 'exit (key-binding key))))))))
  8333. (if (keymapp binding)
  8334. (org-set-transient-map binding)
  8335. (let ((func (or binding
  8336. (unless disable
  8337. 'orgstruct-error))))
  8338. (when func
  8339. (call-interactively func)))))
  8340. (org-run-like-in-org-mode
  8341. (lambda ()
  8342. (interactive)
  8343. (let* ,bindings
  8344. (call-interactively ',fun)))))))))
  8345. name))
  8346. (defun org-contextualize-keys (alist contexts)
  8347. "Return valid elements in ALIST depending on CONTEXTS.
  8348. `org-agenda-custom-commands' or `org-capture-templates' are the
  8349. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8350. or `org-capture-templates-contexts' are the associated contexts
  8351. definitions."
  8352. (let ((contexts
  8353. ;; normalize contexts
  8354. (mapcar
  8355. (lambda(c) (cond ((listp (cadr c))
  8356. (list (car c) (car c) (cadr c)))
  8357. ((string= "" (cadr c))
  8358. (list (car c) (car c) (caddr c)))
  8359. (t c))) contexts))
  8360. (a alist) c r s)
  8361. ;; loop over all commands or templates
  8362. (while (setq c (pop a))
  8363. (let (vrules repl)
  8364. (cond
  8365. ((not (assoc (car c) contexts))
  8366. (push c r))
  8367. ((and (assoc (car c) contexts)
  8368. (setq vrules (org-contextualize-validate-key
  8369. (car c) contexts)))
  8370. (mapc (lambda (vr)
  8371. (when (not (equal (car vr) (cadr vr)))
  8372. (setq repl vr))) vrules)
  8373. (if (not repl) (push c r)
  8374. (push (cadr repl) s)
  8375. (push
  8376. (cons (car c)
  8377. (cdr (or (assoc (cadr repl) alist)
  8378. (error "Undefined key `%s' as contextual replacement for `%s'"
  8379. (cadr repl) (car c)))))
  8380. r))))))
  8381. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8382. (delq
  8383. nil
  8384. (delete-dups
  8385. (mapcar (lambda (x)
  8386. (let ((tpl (car x)))
  8387. (when (not (delq
  8388. nil
  8389. (mapcar (lambda(y)
  8390. (equal y tpl)) s))) x)))
  8391. (reverse r))))))
  8392. (defun org-contextualize-validate-key (key contexts)
  8393. "Check CONTEXTS for agenda or capture KEY."
  8394. (let (r rr res)
  8395. (while (setq r (pop contexts))
  8396. (mapc
  8397. (lambda (rr)
  8398. (when
  8399. (and (equal key (car r))
  8400. (if (functionp rr) (funcall rr)
  8401. (or (and (eq (car rr) 'in-file)
  8402. (buffer-file-name)
  8403. (string-match (cdr rr) (buffer-file-name)))
  8404. (and (eq (car rr) 'in-mode)
  8405. (string-match (cdr rr) (symbol-name major-mode)))
  8406. (and (eq (car rr) 'in-buffer)
  8407. (string-match (cdr rr) (buffer-name)))
  8408. (when (and (eq (car rr) 'not-in-file)
  8409. (buffer-file-name))
  8410. (not (string-match (cdr rr) (buffer-file-name))))
  8411. (when (eq (car rr) 'not-in-mode)
  8412. (not (string-match (cdr rr) (symbol-name major-mode))))
  8413. (when (eq (car rr) 'not-in-buffer)
  8414. (not (string-match (cdr rr) (buffer-name)))))))
  8415. (push r res)))
  8416. (car (last r))))
  8417. (delete-dups (delq nil res))))
  8418. (defun org-context-p (&rest contexts)
  8419. "Check if local context is any of CONTEXTS.
  8420. Possible values in the list of contexts are `table', `headline', and `item'."
  8421. (let ((pos (point)))
  8422. (goto-char (point-at-bol))
  8423. (prog1 (or (and (memq 'table contexts)
  8424. (looking-at "[ \t]*|"))
  8425. (and (memq 'headline contexts)
  8426. (looking-at org-outline-regexp))
  8427. (and (memq 'item contexts)
  8428. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8429. (and (memq 'item-body contexts)
  8430. (org-in-item-p)))
  8431. (goto-char pos))))
  8432. (defun org-get-local-variables ()
  8433. "Return a list of all local variables in an Org mode buffer."
  8434. (let (varlist)
  8435. (with-current-buffer (get-buffer-create "*Org tmp*")
  8436. (erase-buffer)
  8437. (org-mode)
  8438. (setq varlist (buffer-local-variables)))
  8439. (kill-buffer "*Org tmp*")
  8440. (delq nil
  8441. (mapcar
  8442. (lambda (x)
  8443. (setq x
  8444. (if (symbolp x)
  8445. (list x)
  8446. (list (car x) (cdr x))))
  8447. (if (and (not (get (car x) 'org-state))
  8448. (string-match
  8449. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8450. (symbol-name (car x))))
  8451. x nil))
  8452. varlist))))
  8453. (defun org-clone-local-variables (from-buffer &optional regexp)
  8454. "Clone local variables from FROM-BUFFER.
  8455. Optional argument REGEXP selects variables to clone."
  8456. (mapc
  8457. (lambda (pair)
  8458. (and (symbolp (car pair))
  8459. (or (null regexp)
  8460. (string-match regexp (symbol-name (car pair))))
  8461. (set (make-local-variable (car pair))
  8462. (cdr pair))))
  8463. (buffer-local-variables from-buffer)))
  8464. ;;;###autoload
  8465. (defun org-run-like-in-org-mode (cmd)
  8466. "Run a command, pretending that the current buffer is in Org-mode.
  8467. This will temporarily bind local variables that are typically bound in
  8468. Org-mode to the values they have in Org-mode, and then interactively
  8469. call CMD."
  8470. (org-load-modules-maybe)
  8471. (unless org-local-vars
  8472. (setq org-local-vars (org-get-local-variables)))
  8473. (let (binds)
  8474. (dolist (var org-local-vars)
  8475. (when (or (not (boundp (car var)))
  8476. (eq (symbol-value (car var))
  8477. (default-value (car var))))
  8478. (push (list (car var) `(quote ,(cadr var))) binds)))
  8479. (eval `(let ,binds
  8480. (call-interactively (quote ,cmd))))))
  8481. (defun org-get-category (&optional pos force-refresh)
  8482. "Get the category applying to position POS."
  8483. (save-match-data
  8484. (if force-refresh (org-refresh-category-properties))
  8485. (let ((pos (or pos (point))))
  8486. (or (get-text-property pos 'org-category)
  8487. (progn (org-refresh-category-properties)
  8488. (get-text-property pos 'org-category))))))
  8489. ;;; Refresh properties
  8490. (defun org-refresh-properties (dprop tprop)
  8491. "Refresh buffer text properties.
  8492. DPROP is the drawer property and TPROP is either the
  8493. corresponding text property to set, or an alist with each element
  8494. being a text property (as a symbol) and a function to apply to
  8495. the value of the drawer property."
  8496. (let ((case-fold-search t)
  8497. (inhibit-read-only t))
  8498. (org-with-silent-modifications
  8499. (save-excursion
  8500. (save-restriction
  8501. (widen)
  8502. (goto-char (point-min))
  8503. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8504. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8505. (defun org-refresh-property (tprop p)
  8506. "Refresh the buffer text property TPROP from the drawer property P.
  8507. The refresh happens only for the current tree (not subtree)."
  8508. (save-excursion
  8509. (org-back-to-heading t)
  8510. ;; tprop is a text property symbol
  8511. (if (symbolp tprop)
  8512. (put-text-property
  8513. (point) (or (outline-next-heading) (point-max)) tprop p)
  8514. ;; tprop is an alist with (properties . function) elements
  8515. (mapc (lambda(al)
  8516. (save-excursion
  8517. (put-text-property
  8518. (point-at-bol) (or (outline-next-heading) (point-max))
  8519. (car al)
  8520. (funcall (cdr al) p))))
  8521. tprop))))
  8522. (defun org-refresh-category-properties ()
  8523. "Refresh category text properties in the buffer."
  8524. (let ((case-fold-search t)
  8525. (inhibit-read-only t)
  8526. (def-cat (cond
  8527. ((null org-category)
  8528. (if buffer-file-name
  8529. (file-name-sans-extension
  8530. (file-name-nondirectory buffer-file-name))
  8531. "???"))
  8532. ((symbolp org-category) (symbol-name org-category))
  8533. (t org-category)))
  8534. beg end cat pos optionp)
  8535. (org-with-silent-modifications
  8536. (save-excursion
  8537. (save-restriction
  8538. (widen)
  8539. (goto-char (point-min))
  8540. (put-text-property (point) (point-max) 'org-category def-cat)
  8541. (while (re-search-forward
  8542. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8543. (setq pos (match-end 0)
  8544. optionp (equal (char-after (match-beginning 0)) ?#)
  8545. cat (org-trim (match-string 2)))
  8546. (if optionp
  8547. (setq beg (point-at-bol) end (point-max))
  8548. (org-back-to-heading t)
  8549. (setq beg (point) end (org-end-of-subtree t t)))
  8550. (put-text-property beg end 'org-category cat)
  8551. (goto-char pos)))))))
  8552. (defun org-refresh-stats-properties ()
  8553. "Refresh stats text properties in the buffer."
  8554. (let (stats)
  8555. (org-with-silent-modifications
  8556. (save-excursion
  8557. (save-restriction
  8558. (widen)
  8559. (goto-char (point-min))
  8560. (while (re-search-forward
  8561. (concat org-outline-regexp-bol ".*"
  8562. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8563. nil t)
  8564. (setq stats (cond ((equal (match-string 3) "0") 0)
  8565. ((match-string 2)
  8566. (/ (* (string-to-number (match-string 2)) 100)
  8567. (string-to-number (match-string 3))))
  8568. (t (string-to-number (match-string 1)))))
  8569. (org-back-to-heading t)
  8570. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8571. 'org-stats stats)))))))
  8572. (defun org-refresh-effort-properties ()
  8573. "Refresh effort properties"
  8574. (org-refresh-properties
  8575. org-effort-property
  8576. '((effort . identity)
  8577. (effort-minutes . org-duration-string-to-minutes))))
  8578. ;;;; Link Stuff
  8579. ;;; Link abbreviations
  8580. (defun org-link-expand-abbrev (link)
  8581. "Apply replacements as defined in `org-link-abbrev-alist'."
  8582. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8583. (let* ((key (match-string 1 link))
  8584. (as (or (assoc key org-link-abbrev-alist-local)
  8585. (assoc key org-link-abbrev-alist)))
  8586. (tag (and (match-end 2) (match-string 3 link)))
  8587. rpl)
  8588. (if (not as)
  8589. link
  8590. (setq rpl (cdr as))
  8591. (cond
  8592. ((symbolp rpl) (funcall rpl tag))
  8593. ((string-match "%(\\([^)]+\\))" rpl)
  8594. (replace-match
  8595. (save-match-data
  8596. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8597. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8598. ((string-match "%h" rpl)
  8599. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8600. (t (concat rpl tag)))))
  8601. link))
  8602. ;;; Storing and inserting links
  8603. (defvar org-insert-link-history nil
  8604. "Minibuffer history for links inserted with `org-insert-link'.")
  8605. (defvar org-stored-links nil
  8606. "Contains the links stored with `org-store-link'.")
  8607. (defvar org-store-link-plist nil
  8608. "Plist with info about the most recently link created with `org-store-link'.")
  8609. (defvar org-link-protocols nil
  8610. "Link protocols added to Org-mode using `org-add-link-type'.")
  8611. (defvar org-store-link-functions nil
  8612. "List of functions that are called to create and store a link.
  8613. Each function will be called in turn until one returns a non-nil
  8614. value. Each function should check if it is responsible for creating
  8615. this link (for example by looking at the major mode).
  8616. If not, it must exit and return nil.
  8617. If yes, it should return a non-nil value after a calling
  8618. `org-store-link-props' with a list of properties and values.
  8619. Special properties are:
  8620. :type The link prefix, like \"http\". This must be given.
  8621. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8622. This is obligatory as well.
  8623. :description Optional default description for the second pair
  8624. of brackets in an Org-mode link. The user can still change
  8625. this when inserting this link into an Org-mode buffer.
  8626. In addition to these, any additional properties can be specified
  8627. and then used in capture templates.")
  8628. (defun org-add-link-type (type &optional follow export)
  8629. "Add TYPE to the list of `org-link-types'.
  8630. Re-compute all regular expressions depending on `org-link-types'
  8631. FOLLOW and EXPORT are two functions.
  8632. FOLLOW should take the link path as the single argument and do whatever
  8633. is necessary to follow the link, for example find a file or display
  8634. a mail message.
  8635. EXPORT should format the link path for export to one of the export formats.
  8636. It should be a function accepting three arguments:
  8637. path the path of the link, the text after the prefix (like \"http:\")
  8638. desc the description of the link, if any
  8639. format the export format, a symbol like `html' or `latex' or `ascii'.
  8640. The function may use the FORMAT information to return different values
  8641. depending on the format. The return value will be put literally into
  8642. the exported file. If the return value is nil, this means Org should
  8643. do what it normally does with links which do not have EXPORT defined.
  8644. Org mode has a built-in default for exporting links. If you are happy with
  8645. this default, there is no need to define an export function for the link
  8646. type. For a simple example of an export function, see `org-bbdb.el'."
  8647. (add-to-list 'org-link-types type t)
  8648. (org-make-link-regexps)
  8649. (org-element-update-syntax)
  8650. (if (assoc type org-link-protocols)
  8651. (setcdr (assoc type org-link-protocols) (list follow export))
  8652. (push (list type follow export) org-link-protocols)))
  8653. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8654. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8655. ;;;###autoload
  8656. (defun org-store-link (arg)
  8657. "\\<org-mode-map>Store an org-link to the current location.
  8658. This link is added to `org-stored-links' and can later be inserted
  8659. into an org-buffer with \\[org-insert-link].
  8660. For some link types, a prefix arg is interpreted.
  8661. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8662. For file links, arg negates `org-context-in-file-links'.
  8663. A double prefix arg force skipping storing functions that are not
  8664. part of Org's core.
  8665. A triple prefix arg force storing a link for each line in the
  8666. active region."
  8667. (interactive "P")
  8668. (org-load-modules-maybe)
  8669. (if (and (equal arg '(64)) (org-region-active-p))
  8670. (save-excursion
  8671. (let ((end (region-end)))
  8672. (goto-char (region-beginning))
  8673. (set-mark (point))
  8674. (while (< (point-at-eol) end)
  8675. (move-end-of-line 1) (activate-mark)
  8676. (let (current-prefix-arg)
  8677. (call-interactively 'org-store-link))
  8678. (move-beginning-of-line 2)
  8679. (set-mark (point)))))
  8680. (org-with-limited-levels
  8681. (setq org-store-link-plist nil)
  8682. (let (link cpltxt desc description search
  8683. txt custom-id agenda-link sfuns sfunsn)
  8684. (cond
  8685. ;; Store a link using an external link type
  8686. ((and (not (equal arg '(16)))
  8687. (setq sfuns
  8688. (delq
  8689. nil (mapcar (lambda (f)
  8690. (let (fs) (if (funcall f) (push f fs))))
  8691. org-store-link-functions))
  8692. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8693. (or (and (cdr sfuns)
  8694. (funcall (intern
  8695. (completing-read
  8696. "Which function for creating the link? "
  8697. sfunsn nil t (car sfunsn)))))
  8698. (funcall (caar sfuns)))
  8699. (setq link (plist-get org-store-link-plist :link)
  8700. desc (or (plist-get org-store-link-plist
  8701. :description) link))))
  8702. ;; Store a link from a source code buffer
  8703. ((org-src-edit-buffer-p)
  8704. (let (label gc)
  8705. (while (or (not label)
  8706. (save-excursion
  8707. (save-restriction
  8708. (widen)
  8709. (goto-char (point-min))
  8710. (re-search-forward
  8711. (regexp-quote (format org-coderef-label-format label))
  8712. nil t))))
  8713. (when label (message "Label exists already") (sit-for 2))
  8714. (setq label (read-string "Code line label: " label)))
  8715. (end-of-line 1)
  8716. (setq link (format org-coderef-label-format label))
  8717. (setq gc (- 79 (length link)))
  8718. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8719. (insert link)
  8720. (setq link (concat "(" label ")") desc nil)))
  8721. ;; We are in the agenda, link to referenced location
  8722. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8723. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8724. (get-text-property (point) 'org-marker))))
  8725. (when m
  8726. (org-with-point-at m
  8727. (setq agenda-link
  8728. (if (org-called-interactively-p 'any)
  8729. (call-interactively 'org-store-link)
  8730. (org-store-link nil)))))))
  8731. ((eq major-mode 'calendar-mode)
  8732. (let ((cd (calendar-cursor-to-date)))
  8733. (setq link
  8734. (format-time-string
  8735. (car org-time-stamp-formats)
  8736. (apply 'encode-time
  8737. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8738. nil nil nil))))
  8739. (org-store-link-props :type "calendar" :date cd)))
  8740. ((eq major-mode 'help-mode)
  8741. (setq link (concat "help:" (save-excursion
  8742. (goto-char (point-min))
  8743. (looking-at "^[^ ]+")
  8744. (match-string 0))))
  8745. (org-store-link-props :type "help"))
  8746. ((eq major-mode 'w3-mode)
  8747. (setq cpltxt (if (and (buffer-name)
  8748. (not (string-match "Untitled" (buffer-name))))
  8749. (buffer-name)
  8750. (url-view-url t))
  8751. link (url-view-url t))
  8752. (org-store-link-props :type "w3" :url (url-view-url t)))
  8753. ((eq major-mode 'image-mode)
  8754. (setq cpltxt (concat "file:"
  8755. (abbreviate-file-name buffer-file-name))
  8756. link cpltxt)
  8757. (org-store-link-props :type "image" :file buffer-file-name))
  8758. ;; In dired, store a link to the file of the current line
  8759. ((derived-mode-p 'dired-mode)
  8760. (let ((file (dired-get-filename nil t)))
  8761. (setq file (if file
  8762. (abbreviate-file-name
  8763. (expand-file-name (dired-get-filename nil t)))
  8764. ;; otherwise, no file so use current directory.
  8765. default-directory))
  8766. (setq cpltxt (concat "file:" file)
  8767. link cpltxt)))
  8768. ((setq search (run-hook-with-args-until-success
  8769. 'org-create-file-search-functions))
  8770. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8771. "::" search))
  8772. (setq cpltxt (or description link)))
  8773. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8774. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8775. (cond
  8776. ;; Store a link using the target at point
  8777. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8778. (setq cpltxt
  8779. (concat "file:"
  8780. (abbreviate-file-name
  8781. (buffer-file-name (buffer-base-buffer)))
  8782. "::" (match-string 1))
  8783. link cpltxt))
  8784. ((and (featurep 'org-id)
  8785. (or (eq org-id-link-to-org-use-id t)
  8786. (and (org-called-interactively-p 'any)
  8787. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8788. (and (eq org-id-link-to-org-use-id
  8789. 'create-if-interactive-and-no-custom-id)
  8790. (not custom-id))))
  8791. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8792. ;; Store a link using the ID at point
  8793. (setq link (condition-case nil
  8794. (prog1 (org-id-store-link)
  8795. (setq desc (or (plist-get org-store-link-plist
  8796. :description)
  8797. "")))
  8798. (error
  8799. ;; Probably before first headline, link only to file
  8800. (concat "file:"
  8801. (abbreviate-file-name
  8802. (buffer-file-name (buffer-base-buffer))))))))
  8803. (t
  8804. ;; Just link to current headline
  8805. (setq cpltxt (concat "file:"
  8806. (abbreviate-file-name
  8807. (buffer-file-name (buffer-base-buffer)))))
  8808. ;; Add a context search string
  8809. (when (org-xor org-context-in-file-links arg)
  8810. (let* ((ee (org-element-at-point))
  8811. (et (org-element-type ee))
  8812. (ev (plist-get (cadr ee) :value))
  8813. (ek (plist-get (cadr ee) :key))
  8814. (eok (and (stringp ek) (string-match "name" ek))))
  8815. (setq txt (cond
  8816. ((org-at-heading-p) nil)
  8817. ((and (eq et 'keyword) eok) ev)
  8818. ((org-region-active-p)
  8819. (buffer-substring (region-beginning) (region-end)))))
  8820. (when (or (null txt) (string-match "\\S-" txt))
  8821. (setq cpltxt
  8822. (concat cpltxt "::"
  8823. (condition-case nil
  8824. (org-make-org-heading-search-string txt)
  8825. (error "")))
  8826. desc (or (and (eq et 'keyword) eok ev)
  8827. (nth 4 (ignore-errors (org-heading-components)))
  8828. "NONE")))))
  8829. (if (string-match "::\\'" cpltxt)
  8830. (setq cpltxt (substring cpltxt 0 -2)))
  8831. (setq link cpltxt))))
  8832. ((buffer-file-name (buffer-base-buffer))
  8833. ;; Just link to this file here.
  8834. (setq cpltxt (concat "file:"
  8835. (abbreviate-file-name
  8836. (buffer-file-name (buffer-base-buffer)))))
  8837. ;; Add a context string.
  8838. (when (org-xor org-context-in-file-links arg)
  8839. (setq txt (if (org-region-active-p)
  8840. (buffer-substring (region-beginning) (region-end))
  8841. (buffer-substring (point-at-bol) (point-at-eol))))
  8842. ;; Only use search option if there is some text.
  8843. (when (string-match "\\S-" txt)
  8844. (setq cpltxt
  8845. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8846. desc "NONE")))
  8847. (setq link cpltxt))
  8848. ((org-called-interactively-p 'interactive)
  8849. (user-error "No method for storing a link from this buffer"))
  8850. (t (setq link nil)))
  8851. ;; We're done setting link and desc, clean up
  8852. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8853. (setq link (or link cpltxt)
  8854. desc (or desc cpltxt))
  8855. (cond ((equal desc "NONE") (setq desc nil))
  8856. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8857. (let ((d0 (match-string 3 desc))
  8858. (p0 (match-string 5 desc)))
  8859. (setq desc
  8860. (replace-regexp-in-string
  8861. org-bracket-link-regexp
  8862. (concat (or p0 d0)
  8863. (if (equal (length (match-string 0 desc))
  8864. (length desc)) "*" "")) desc)))))
  8865. ;; Return the link
  8866. (if (not (and (or (org-called-interactively-p 'any)
  8867. executing-kbd-macro) link))
  8868. (or agenda-link (and link (org-make-link-string link desc)))
  8869. (push (list link desc) org-stored-links)
  8870. (message "Stored: %s" (or desc link))
  8871. (when custom-id
  8872. (setq link (concat "file:" (abbreviate-file-name
  8873. (buffer-file-name)) "::#" custom-id))
  8874. (push (list link desc) org-stored-links))
  8875. (car org-stored-links))))))
  8876. (defun org-store-link-props (&rest plist)
  8877. "Store link properties, extract names and addresses."
  8878. (let (x adr)
  8879. (when (setq x (plist-get plist :from))
  8880. (setq adr (mail-extract-address-components x))
  8881. (setq plist (plist-put plist :fromname (car adr)))
  8882. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8883. (when (setq x (plist-get plist :to))
  8884. (setq adr (mail-extract-address-components x))
  8885. (setq plist (plist-put plist :toname (car adr)))
  8886. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8887. (let ((from (plist-get plist :from))
  8888. (to (plist-get plist :to)))
  8889. (when (and from to org-from-is-user-regexp)
  8890. (setq plist
  8891. (plist-put plist :fromto
  8892. (if (string-match org-from-is-user-regexp from)
  8893. (concat "to %t")
  8894. (concat "from %f"))))))
  8895. (setq org-store-link-plist plist))
  8896. (defun org-add-link-props (&rest plist)
  8897. "Add these properties to the link property list."
  8898. (let (key value)
  8899. (while plist
  8900. (setq key (pop plist) value (pop plist))
  8901. (setq org-store-link-plist
  8902. (plist-put org-store-link-plist key value)))))
  8903. (defun org-email-link-description (&optional fmt)
  8904. "Return the description part of an email link.
  8905. This takes information from `org-store-link-plist' and formats it
  8906. according to FMT (default from `org-email-link-description-format')."
  8907. (setq fmt (or fmt org-email-link-description-format))
  8908. (let* ((p org-store-link-plist)
  8909. (to (plist-get p :toaddress))
  8910. (from (plist-get p :fromaddress))
  8911. (table
  8912. (list
  8913. (cons "%c" (plist-get p :fromto))
  8914. (cons "%F" (plist-get p :from))
  8915. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8916. (cons "%T" (plist-get p :to))
  8917. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8918. (cons "%s" (plist-get p :subject))
  8919. (cons "%d" (plist-get p :date))
  8920. (cons "%m" (plist-get p :message-id)))))
  8921. (when (string-match "%c" fmt)
  8922. ;; Check if the user wrote this message
  8923. (if (and org-from-is-user-regexp from to
  8924. (save-match-data (string-match org-from-is-user-regexp from)))
  8925. (setq fmt (replace-match "to %t" t t fmt))
  8926. (setq fmt (replace-match "from %f" t t fmt))))
  8927. (org-replace-escapes fmt table)))
  8928. (defun org-make-org-heading-search-string (&optional string)
  8929. "Make search string for the current headline or STRING."
  8930. (let ((s (or string
  8931. (and (derived-mode-p 'org-mode)
  8932. (save-excursion
  8933. (org-back-to-heading t)
  8934. (org-element-property :raw-value (org-element-at-point))))))
  8935. (lines org-context-in-file-links))
  8936. (or string (setq s (concat "*" s))) ; Add * for headlines
  8937. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8938. (when (and string (integerp lines) (> lines 0))
  8939. (let ((slines (org-split-string s "\n")))
  8940. (when (< lines (length slines))
  8941. (setq s (mapconcat
  8942. 'identity
  8943. (reverse (nthcdr (- (length slines) lines)
  8944. (reverse slines))) "\n")))))
  8945. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8946. (defun org-make-link-string (link &optional description)
  8947. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8948. (unless (string-match "\\S-" link)
  8949. (error "Empty link"))
  8950. (when (and description
  8951. (stringp description)
  8952. (not (string-match "\\S-" description)))
  8953. (setq description nil))
  8954. (when (stringp description)
  8955. ;; Remove brackets from the description, they are fatal.
  8956. (while (string-match "\\[" description)
  8957. (setq description (replace-match "{" t t description)))
  8958. (while (string-match "\\]" description)
  8959. (setq description (replace-match "}" t t description))))
  8960. (when (equal link description)
  8961. ;; No description needed, it is identical
  8962. (setq description nil))
  8963. (when (and (not description)
  8964. (not (string-match (org-image-file-name-regexp) link))
  8965. (not (equal link (org-link-escape link))))
  8966. (setq description (org-extract-attributes link)))
  8967. (setq link
  8968. (cond ((string-match (org-image-file-name-regexp) link) link)
  8969. ((string-match org-link-types-re link)
  8970. (concat (match-string 1 link)
  8971. (org-link-escape (substring link (match-end 1)))))
  8972. (t (org-link-escape link))))
  8973. (concat "[[" link "]"
  8974. (if description (concat "[" description "]") "")
  8975. "]"))
  8976. (defconst org-link-escape-chars
  8977. ;;%20 %5B %5D
  8978. '(?\ ?\[ ?\])
  8979. "List of characters that should be escaped in a link when stored to Org.
  8980. This is the list that is used for internal purposes.")
  8981. (defconst org-link-escape-chars-browser
  8982. ;;%20 %22
  8983. '(?\ ?\")
  8984. "List of characters to be escaped before handing over to the browser.
  8985. If you consider using this constant then you probably want to use
  8986. the function `org-link-escape-browser' instead. See there why
  8987. this constant is a candidate to be removed once Org drops support
  8988. for Emacs 24.1 and 24.2.")
  8989. (defun org-link-escape (text &optional table merge)
  8990. "Return percent escaped representation of TEXT.
  8991. TEXT is a string with the text to escape.
  8992. Optional argument TABLE is a list with characters that should be
  8993. escaped. When nil, `org-link-escape-chars' is used.
  8994. If optional argument MERGE is set, merge TABLE into
  8995. `org-link-escape-chars'."
  8996. ;; Don't escape chars in internal links
  8997. (if (string-match "^\\*[[:alnum:]]+" text)
  8998. text
  8999. (cond
  9000. ((and table merge)
  9001. (mapc (lambda (defchr)
  9002. (unless (member defchr table)
  9003. (setq table (cons defchr table))))
  9004. org-link-escape-chars))
  9005. ((null table)
  9006. (setq table org-link-escape-chars)))
  9007. (mapconcat
  9008. (lambda (char)
  9009. (if (or (member char table)
  9010. (and (or (< char 32) (= char ?\%) (> char 126))
  9011. org-url-hexify-p))
  9012. (mapconcat (lambda (sequence-element)
  9013. (format "%%%.2X" sequence-element))
  9014. (or (encode-coding-char char 'utf-8)
  9015. (error "Unable to percent escape character: %s"
  9016. (char-to-string char))) "")
  9017. (char-to-string char))) text "")))
  9018. (defun org-link-escape-browser (text)
  9019. "Escape some characters before handing over to the browser.
  9020. This function is a candidate to be removed together with the
  9021. constant `org-link-escape-chars-browser' once Org drops support
  9022. for Emacs 24.1 and 24.2. All calls to this function will have to
  9023. be replaced with `url-encode-url' which is available since Emacs
  9024. 24.3.1."
  9025. ;; Example with the Org link
  9026. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9027. ;; to open the browser with +subject:"Release 8.2" filled into the
  9028. ;; query field: In this case the variable TEXT contains the
  9029. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9030. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9031. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9032. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9033. (if (fboundp 'url-encode-url)
  9034. (url-encode-url text)
  9035. (if (org-string-match-p
  9036. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9037. text)
  9038. (org-link-escape text org-link-escape-chars-browser)
  9039. text)))
  9040. (defun org-link-unescape (str)
  9041. "Unhex hexified Unicode strings as returned from the JavaScript function
  9042. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9043. (unless (and (null str) (string= "" str))
  9044. (let ((pos 0) (case-fold-search t) unhexed)
  9045. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9046. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9047. (setq str (replace-match unhexed t t str))
  9048. (setq pos (+ pos (length unhexed))))))
  9049. str)
  9050. (defun org-link-unescape-compound (hex)
  9051. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9052. Note: this function also decodes single byte encodings like
  9053. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9054. (save-match-data
  9055. (let* ((bytes (cdr (split-string hex "%")))
  9056. (ret "")
  9057. (eat 0)
  9058. (sum 0))
  9059. (while bytes
  9060. (let* ((val (string-to-number (pop bytes) 16))
  9061. (shift-xor
  9062. (if (= 0 eat)
  9063. (cond
  9064. ((>= val 252) (cons 6 252))
  9065. ((>= val 248) (cons 5 248))
  9066. ((>= val 240) (cons 4 240))
  9067. ((>= val 224) (cons 3 224))
  9068. ((>= val 192) (cons 2 192))
  9069. (t (cons 0 0)))
  9070. (cons 6 128))))
  9071. (if (>= val 192) (setq eat (car shift-xor)))
  9072. (setq val (logxor val (cdr shift-xor)))
  9073. (setq sum (+ (lsh sum (car shift-xor)) val))
  9074. (if (> eat 0) (setq eat (- eat 1)))
  9075. (cond
  9076. ((= 0 eat) ;multi byte
  9077. (setq ret (concat ret (org-char-to-string sum)))
  9078. (setq sum 0))
  9079. ((not bytes) ; single byte(s)
  9080. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9081. ret)))
  9082. (defun org-link-unescape-single-byte-sequence (hex)
  9083. "Unhexify hex-encoded single byte character sequences."
  9084. (mapconcat (lambda (byte)
  9085. (char-to-string (string-to-number byte 16)))
  9086. (cdr (split-string hex "%")) ""))
  9087. (defun org-xor (a b)
  9088. "Exclusive or."
  9089. (if a (not b) b))
  9090. (defun org-fixup-message-id-for-http (s)
  9091. "Replace special characters in a message id, so it can be used in an http query."
  9092. (when (string-match "%" s)
  9093. (setq s (mapconcat (lambda (c)
  9094. (if (eq c ?%)
  9095. "%25"
  9096. (char-to-string c)))
  9097. s "")))
  9098. (while (string-match "<" s)
  9099. (setq s (replace-match "%3C" t t s)))
  9100. (while (string-match ">" s)
  9101. (setq s (replace-match "%3E" t t s)))
  9102. (while (string-match "@" s)
  9103. (setq s (replace-match "%40" t t s)))
  9104. s)
  9105. (defun org-link-prettify (link)
  9106. "Return a human-readable representation of LINK.
  9107. The car of LINK must be a raw link.
  9108. The cdr of LINK must be either a link description or nil."
  9109. (let ((desc (or (cadr link) "<no description>")))
  9110. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9111. "<" (car link) ">")))
  9112. ;;;###autoload
  9113. (defun org-insert-link-global ()
  9114. "Insert a link like Org-mode does.
  9115. This command can be called in any mode to insert a link in Org-mode syntax."
  9116. (interactive)
  9117. (org-load-modules-maybe)
  9118. (org-run-like-in-org-mode 'org-insert-link))
  9119. (defun org-insert-all-links (arg &optional pre post)
  9120. "Insert all links in `org-stored-links'.
  9121. When a universal prefix, do not delete the links from `org-stored-links'.
  9122. When `ARG' is a number, insert the last N link(s).
  9123. `PRE' and `POST' are optional arguments to define a string to
  9124. prepend or to append."
  9125. (interactive "P")
  9126. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9127. (links (copy-seq org-stored-links))
  9128. (pr (or pre "- "))
  9129. (po (or post "\n"))
  9130. (cnt 1) l)
  9131. (if (null org-stored-links)
  9132. (message "No link to insert")
  9133. (while (and (or (listp arg) (>= arg cnt))
  9134. (setq l (if (listp arg)
  9135. (pop links)
  9136. (pop org-stored-links))))
  9137. (setq cnt (1+ cnt))
  9138. (insert pr)
  9139. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9140. (insert po)))))
  9141. (defun org-insert-last-stored-link (arg)
  9142. "Insert the last link stored in `org-stored-links'."
  9143. (interactive "p")
  9144. (org-insert-all-links arg "" "\n"))
  9145. (defun org-link-fontify-links-to-this-file ()
  9146. "Fontify links to the current file in `org-stored-links'."
  9147. (let ((f (buffer-file-name)) a b)
  9148. (setq a (mapcar (lambda(l)
  9149. (let ((ll (car l)))
  9150. (when (and (string-match "^file:\\(.+\\)::" ll)
  9151. (equal f (expand-file-name (match-string 1 ll))))
  9152. ll)))
  9153. org-stored-links))
  9154. (when (featurep 'org-id)
  9155. (setq b (mapcar (lambda(l)
  9156. (let ((ll (car l)))
  9157. (when (and (string-match "^id:\\(.+\\)$" ll)
  9158. (equal f (expand-file-name
  9159. (or (org-id-find-id-file
  9160. (match-string 1 ll)) ""))))
  9161. ll)))
  9162. org-stored-links)))
  9163. (mapcar (lambda(l)
  9164. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9165. (delq nil (append a b)))))
  9166. (defvar org-link-links-in-this-file nil)
  9167. (defun org-insert-link (&optional complete-file link-location default-description)
  9168. "Insert a link. At the prompt, enter the link.
  9169. Completion can be used to insert any of the link protocol prefixes like
  9170. http or ftp in use.
  9171. The history can be used to select a link previously stored with
  9172. `org-store-link'. When the empty string is entered (i.e. if you just
  9173. press RET at the prompt), the link defaults to the most recently
  9174. stored link. As SPC triggers completion in the minibuffer, you need to
  9175. use M-SPC or C-q SPC to force the insertion of a space character.
  9176. You will also be prompted for a description, and if one is given, it will
  9177. be displayed in the buffer instead of the link.
  9178. If there is already a link at point, this command will allow you to edit link
  9179. and description parts.
  9180. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9181. be selected using completion. The path to the file will be relative to the
  9182. current directory if the file is in the current directory or a subdirectory.
  9183. Otherwise, the link will be the absolute path as completed in the minibuffer
  9184. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9185. option `org-link-file-path-type'.
  9186. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9187. the current directory or below.
  9188. With three \\[universal-argument] prefixes, negate the meaning of
  9189. `org-keep-stored-link-after-insertion'.
  9190. If `org-make-link-description-function' is non-nil, this function will be
  9191. called with the link target, and the result will be the default
  9192. link description.
  9193. If the LINK-LOCATION parameter is non-nil, this value will be
  9194. used as the link location instead of reading one interactively.
  9195. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9196. be used as the default description."
  9197. (interactive "P")
  9198. (let* ((wcf (current-window-configuration))
  9199. (origbuf (current-buffer))
  9200. (region (if (org-region-active-p)
  9201. (buffer-substring (region-beginning) (region-end))))
  9202. (remove (and region (list (region-beginning) (region-end))))
  9203. (desc region)
  9204. tmphist ; byte-compile incorrectly complains about this
  9205. (link link-location)
  9206. (abbrevs org-link-abbrev-alist-local)
  9207. entry file all-prefixes auto-desc)
  9208. (cond
  9209. (link-location) ; specified by arg, just use it.
  9210. ((org-in-regexp org-bracket-link-regexp 1)
  9211. ;; We do have a link at point, and we are going to edit it.
  9212. (setq remove (list (match-beginning 0) (match-end 0)))
  9213. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9214. (setq link (read-string "Link: "
  9215. (org-link-unescape
  9216. (org-match-string-no-properties 1)))))
  9217. ((or (org-in-regexp org-angle-link-re)
  9218. (org-in-regexp org-plain-link-re))
  9219. ;; Convert to bracket link
  9220. (setq remove (list (match-beginning 0) (match-end 0))
  9221. link (read-string "Link: "
  9222. (org-remove-angle-brackets (match-string 0)))))
  9223. ((member complete-file '((4) (16)))
  9224. ;; Completing read for file names.
  9225. (setq link (org-file-complete-link complete-file)))
  9226. (t
  9227. ;; Read link, with completion for stored links.
  9228. (org-link-fontify-links-to-this-file)
  9229. (org-switch-to-buffer-other-window "*Org Links*")
  9230. (with-current-buffer "*Org Links*"
  9231. (erase-buffer)
  9232. (insert "Insert a link.
  9233. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9234. (when org-stored-links
  9235. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9236. (insert (mapconcat 'org-link-prettify
  9237. (reverse org-stored-links) "\n")))
  9238. (goto-char (point-min)))
  9239. (let ((cw (selected-window)))
  9240. (select-window (get-buffer-window "*Org Links*" 'visible))
  9241. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9242. (unless (pos-visible-in-window-p (point-max))
  9243. (org-fit-window-to-buffer))
  9244. (and (window-live-p cw) (select-window cw)))
  9245. ;; Fake a link history, containing the stored links.
  9246. (setq tmphist (append (mapcar 'car org-stored-links)
  9247. org-insert-link-history))
  9248. (setq all-prefixes (append (mapcar 'car abbrevs)
  9249. (mapcar 'car org-link-abbrev-alist)
  9250. org-link-types))
  9251. (unwind-protect
  9252. (progn
  9253. (setq link
  9254. (org-completing-read
  9255. "Link: "
  9256. (append
  9257. (mapcar (lambda (x) (concat x ":"))
  9258. all-prefixes)
  9259. (mapcar 'car org-stored-links))
  9260. nil nil nil
  9261. 'tmphist
  9262. (caar org-stored-links)))
  9263. (if (not (string-match "\\S-" link))
  9264. (user-error "No link selected"))
  9265. (mapc (lambda(l)
  9266. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9267. org-stored-links)
  9268. (if (or (member link all-prefixes)
  9269. (and (equal ":" (substring link -1))
  9270. (member (substring link 0 -1) all-prefixes)
  9271. (setq link (substring link 0 -1))))
  9272. (setq link (with-current-buffer origbuf
  9273. (org-link-try-special-completion link)))))
  9274. (set-window-configuration wcf)
  9275. (kill-buffer "*Org Links*"))
  9276. (setq entry (assoc link org-stored-links))
  9277. (or entry (push link org-insert-link-history))
  9278. (setq desc (or desc (nth 1 entry)))))
  9279. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9280. (not org-keep-stored-link-after-insertion))
  9281. (setq org-stored-links (delq (assoc link org-stored-links)
  9282. org-stored-links)))
  9283. (if (and (string-match org-plain-link-re link)
  9284. (not (string-match org-ts-regexp link)))
  9285. ;; URL-like link, normalize the use of angular brackets.
  9286. (setq link (org-remove-angle-brackets link)))
  9287. ;; Check if we are linking to the current file with a search
  9288. ;; option If yes, simplify the link by using only the search
  9289. ;; option.
  9290. (when (and buffer-file-name
  9291. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9292. (let* ((path (match-string 1 link))
  9293. (case-fold-search nil)
  9294. (search (match-string 2 link)))
  9295. (save-match-data
  9296. (if (equal (file-truename buffer-file-name) (file-truename path))
  9297. ;; We are linking to this same file, with a search option
  9298. (setq link search)))))
  9299. ;; Check if we can/should use a relative path. If yes, simplify the link
  9300. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9301. (let* ((type (match-string 1 link))
  9302. (path (match-string 2 link))
  9303. (origpath path)
  9304. (case-fold-search nil))
  9305. (cond
  9306. ((or (eq org-link-file-path-type 'absolute)
  9307. (equal complete-file '(16)))
  9308. (setq path (abbreviate-file-name (expand-file-name path))))
  9309. ((eq org-link-file-path-type 'noabbrev)
  9310. (setq path (expand-file-name path)))
  9311. ((eq org-link-file-path-type 'relative)
  9312. (setq path (file-relative-name path)))
  9313. (t
  9314. (save-match-data
  9315. (if (string-match (concat "^" (regexp-quote
  9316. (expand-file-name
  9317. (file-name-as-directory
  9318. default-directory))))
  9319. (expand-file-name path))
  9320. ;; We are linking a file with relative path name.
  9321. (setq path (substring (expand-file-name path)
  9322. (match-end 0)))
  9323. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9324. (setq link (concat type path))
  9325. (if (equal desc origpath)
  9326. (setq desc path))))
  9327. (if org-make-link-description-function
  9328. (setq desc
  9329. (or (condition-case nil
  9330. (funcall org-make-link-description-function link desc)
  9331. (error (progn (message "Can't get link description from `%s'"
  9332. (symbol-name org-make-link-description-function))
  9333. (sit-for 2) nil)))
  9334. (read-string "Description: " default-description)))
  9335. (if default-description (setq desc default-description)
  9336. (setq desc (or (and auto-desc desc)
  9337. (read-string "Description: " desc)))))
  9338. (unless (string-match "\\S-" desc) (setq desc nil))
  9339. (if remove (apply 'delete-region remove))
  9340. (insert (org-make-link-string link desc))))
  9341. (defun org-link-try-special-completion (type)
  9342. "If there is completion support for link type TYPE, offer it."
  9343. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9344. (if (functionp fun)
  9345. (funcall fun)
  9346. (read-string "Link (no completion support): " (concat type ":")))))
  9347. (defun org-file-complete-link (&optional arg)
  9348. "Create a file link using completion."
  9349. (let (file link)
  9350. (setq file (org-iread-file-name "File: "))
  9351. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9352. (pwd1 (file-name-as-directory (abbreviate-file-name
  9353. (expand-file-name ".")))))
  9354. (cond
  9355. ((equal arg '(16))
  9356. (setq link (concat
  9357. "file:"
  9358. (abbreviate-file-name (expand-file-name file)))))
  9359. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9360. (setq link (concat "file:" (match-string 1 file))))
  9361. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9362. (expand-file-name file))
  9363. (setq link (concat
  9364. "file:" (match-string 1 (expand-file-name file)))))
  9365. (t (setq link (concat "file:" file)))))
  9366. link))
  9367. (defun org-iread-file-name (&rest args)
  9368. "Read-file-name using `ido-mode' speedup if available.
  9369. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9370. See `read-file-name' for a description of parameters."
  9371. (org-without-partial-completion
  9372. (if (and org-completion-use-ido
  9373. (fboundp 'ido-read-file-name)
  9374. (boundp 'ido-mode) ido-mode
  9375. (listp (second args)))
  9376. (let ((ido-enter-matching-directory nil))
  9377. (apply 'ido-read-file-name args))
  9378. (apply 'read-file-name args))))
  9379. (defun org-completing-read (&rest args)
  9380. "Completing-read with SPACE being a normal character."
  9381. (let ((enable-recursive-minibuffers t)
  9382. (minibuffer-local-completion-map
  9383. (copy-keymap minibuffer-local-completion-map)))
  9384. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9385. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9386. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9387. (apply 'org-icompleting-read args)))
  9388. (defun org-completing-read-no-i (&rest args)
  9389. (let (org-completion-use-ido org-completion-use-iswitchb)
  9390. (apply 'org-completing-read args)))
  9391. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9392. "Use iswitch as a completing-read replacement to choose from choices.
  9393. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9394. from."
  9395. (let* ((iswitchb-use-virtual-buffers nil)
  9396. (iswitchb-make-buflist-hook
  9397. (lambda ()
  9398. (setq iswitchb-temp-buflist choices))))
  9399. (iswitchb-read-buffer prompt)))
  9400. (defun org-icompleting-read (&rest args)
  9401. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9402. (org-without-partial-completion
  9403. (if (and org-completion-use-ido
  9404. (fboundp 'ido-completing-read)
  9405. (boundp 'ido-mode) ido-mode
  9406. (listp (second args)))
  9407. (let ((ido-enter-matching-directory nil))
  9408. (apply 'ido-completing-read (concat (car args))
  9409. (if (consp (car (nth 1 args)))
  9410. (mapcar 'car (nth 1 args))
  9411. (nth 1 args))
  9412. (cddr args)))
  9413. (if (and org-completion-use-iswitchb
  9414. (boundp 'iswitchb-mode) iswitchb-mode
  9415. (listp (second args)))
  9416. (apply 'org-iswitchb-completing-read (concat (car args))
  9417. (if (consp (car (nth 1 args)))
  9418. (mapcar 'car (nth 1 args))
  9419. (nth 1 args))
  9420. (cddr args))
  9421. (apply 'completing-read args)))))
  9422. (defun org-extract-attributes (s)
  9423. "Extract the attributes cookie from a string and set as text property."
  9424. (let (a attr (start 0) key value)
  9425. (save-match-data
  9426. (when (string-match "{{\\([^}]+\\)}}$" s)
  9427. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9428. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9429. (setq key (match-string 1 a) value (match-string 2 a)
  9430. start (match-end 0)
  9431. attr (plist-put attr (intern key) value))))
  9432. (org-add-props s nil 'org-attr attr))
  9433. s))
  9434. ;;; Opening/following a link
  9435. (defvar org-link-search-failed nil)
  9436. (defvar org-open-link-functions nil
  9437. "Hook for functions finding a plain text link.
  9438. These functions must take a single argument, the link content.
  9439. They will be called for links that look like [[link text][description]]
  9440. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9441. like a filename (e.g. \"./blue.png\").
  9442. These functions will be called *before* Org attempts to resolve the
  9443. link by doing text searches in the current buffer - so if you want a
  9444. link \"[[target]]\" to still find \"<<target>>\", your function should
  9445. handle this as a special case.
  9446. When the function does handle the link, it must return a non-nil value.
  9447. If it decides that it is not responsible for this link, it must return
  9448. nil to indicate that that Org-mode can continue with other options
  9449. like exact and fuzzy text search.")
  9450. (defun org-next-link (&optional search-backward)
  9451. "Move forward to the next link.
  9452. If the link is in hidden text, expose it."
  9453. (interactive "P")
  9454. (when (and org-link-search-failed (eq this-command last-command))
  9455. (goto-char (point-min))
  9456. (message "Link search wrapped back to beginning of buffer"))
  9457. (setq org-link-search-failed nil)
  9458. (let* ((pos (point))
  9459. (ct (org-context))
  9460. (a (assoc :link ct))
  9461. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9462. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9463. ((looking-at org-any-link-re)
  9464. ;; Don't stay stuck at link without an org-link face
  9465. (forward-char (if search-backward -1 1))))
  9466. (if (funcall srch-fun org-any-link-re nil t)
  9467. (progn
  9468. (goto-char (match-beginning 0))
  9469. (if (outline-invisible-p) (org-show-context)))
  9470. (goto-char pos)
  9471. (setq org-link-search-failed t)
  9472. (message "No further link found"))))
  9473. (defun org-previous-link ()
  9474. "Move backward to the previous link.
  9475. If the link is in hidden text, expose it."
  9476. (interactive)
  9477. (funcall 'org-next-link t))
  9478. (defun org-translate-link (s)
  9479. "Translate a link string if a translation function has been defined."
  9480. (if (and org-link-translation-function
  9481. (fboundp org-link-translation-function)
  9482. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9483. (progn
  9484. (setq s (funcall org-link-translation-function
  9485. (match-string 1 s) (match-string 2 s)))
  9486. (concat (car s) ":" (cdr s)))
  9487. s))
  9488. (defun org-translate-link-from-planner (type path)
  9489. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9490. This is still an experimental function, your mileage may vary."
  9491. (cond
  9492. ((member type '("http" "https" "news" "ftp"))
  9493. ;; standard Internet links are the same.
  9494. nil)
  9495. ((and (equal type "irc") (string-match "^//" path))
  9496. ;; Planner has two / at the beginning of an irc link, we have 1.
  9497. ;; We should have zero, actually....
  9498. (setq path (substring path 1)))
  9499. ((and (equal type "lisp") (string-match "^/" path))
  9500. ;; Planner has a slash, we do not.
  9501. (setq type "elisp" path (substring path 1)))
  9502. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9503. ;; A typical message link. Planner has the id after the final slash,
  9504. ;; we separate it with a hash mark
  9505. (setq path (concat (match-string 1 path) "#"
  9506. (org-remove-angle-brackets (match-string 2 path))))))
  9507. (cons type path))
  9508. (defun org-find-file-at-mouse (ev)
  9509. "Open file link or URL at mouse."
  9510. (interactive "e")
  9511. (mouse-set-point ev)
  9512. (org-open-at-point 'in-emacs))
  9513. (defun org-open-at-mouse (ev)
  9514. "Open file link or URL at mouse.
  9515. See the docstring of `org-open-file' for details."
  9516. (interactive "e")
  9517. (mouse-set-point ev)
  9518. (if (eq major-mode 'org-agenda-mode)
  9519. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9520. (org-open-at-point))
  9521. (defvar org-window-config-before-follow-link nil
  9522. "The window configuration before following a link.
  9523. This is saved in case the need arises to restore it.")
  9524. (defvar org-open-link-marker (make-marker)
  9525. "Marker pointing to the location where `org-open-at-point' was called.")
  9526. ;;;###autoload
  9527. (defun org-open-at-point-global ()
  9528. "Follow a link like Org-mode does.
  9529. This command can be called in any mode to follow a link that has
  9530. Org-mode syntax."
  9531. (interactive)
  9532. (org-run-like-in-org-mode 'org-open-at-point))
  9533. ;;;###autoload
  9534. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9535. "Open a link in the string S, as if it was in Org-mode."
  9536. (interactive "sLink: \nP")
  9537. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9538. (with-temp-buffer
  9539. (let ((org-inhibit-startup (not reference-buffer)))
  9540. (org-mode)
  9541. (insert s)
  9542. (goto-char (point-min))
  9543. (when reference-buffer
  9544. (setq org-link-abbrev-alist-local
  9545. (with-current-buffer reference-buffer
  9546. org-link-abbrev-alist-local)))
  9547. (org-open-at-point arg reference-buffer)))))
  9548. (defvar org-open-at-point-functions nil
  9549. "Hook that is run when following a link at point.
  9550. Functions in this hook must return t if they identify and follow
  9551. a link at point. If they don't find anything interesting at point,
  9552. they must return nil.")
  9553. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9554. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9555. (defun org-open-at-point (&optional arg reference-buffer)
  9556. "Open link, timestamp, footnote or tags at point.
  9557. When point is on a link, follow it. Normally, files will be
  9558. opened by an appropriate application. If the optional prefix
  9559. argument ARG is non-nil, Emacs will visit the file. With
  9560. a double prefix argument, try to open outside of Emacs, in the
  9561. application the system uses for this file type.
  9562. When point is on a timestamp, open the agenda at the day
  9563. specified.
  9564. When point is a footnote definition, move to the first reference
  9565. found. If it is on a reference, move to the associated
  9566. definition.
  9567. When point is on a headline, display a list of every link in the
  9568. entry, so it is possible to pick one, or all, of them. If point
  9569. is on a tag, call `org-tags-view' instead.
  9570. When optional argument REFERENCE-BUFFER is non-nil, it should
  9571. specify a buffer from where the link search should happen. This
  9572. is used internally by `org-open-link-from-string'.
  9573. On top of syntactically correct links, this function will open
  9574. the link at point in comments or comment blocks and the first
  9575. link in a property drawer line."
  9576. (interactive "P")
  9577. ;; On a code block, open block's results.
  9578. (unless (call-interactively 'org-babel-open-src-block-result)
  9579. (org-load-modules-maybe)
  9580. (move-marker org-open-link-marker (point))
  9581. (setq org-window-config-before-follow-link (current-window-configuration))
  9582. (org-remove-occur-highlights nil nil t)
  9583. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9584. (let* ((context
  9585. ;; Only consider supported types, even if they are not
  9586. ;; the closest one.
  9587. (org-element-lineage
  9588. (org-element-context)
  9589. '(comment comment-block footnote-definition footnote-reference
  9590. headline inlinetask link node-property timestamp)
  9591. t))
  9592. (type (org-element-type context))
  9593. (value (org-element-property :value context)))
  9594. (cond
  9595. ((not context) (user-error "No link found"))
  9596. ;; Exception: open timestamps and links in properties drawers
  9597. ;; and comments.
  9598. ((memq type '(comment comment-block node-property))
  9599. (cond ((org-in-regexp org-any-link-re)
  9600. (org-open-link-from-string (match-string-no-properties 0)))
  9601. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9602. (org-follow-timestamp-link))
  9603. (t (user-error "No link found"))))
  9604. ;; On a headline or an inlinetask, but not on a timestamp,
  9605. ;; a link, a footnote reference or on tags.
  9606. ((and (memq type '(headline inlinetask))
  9607. ;; Not on tags.
  9608. (progn (save-excursion (beginning-of-line)
  9609. (looking-at org-complex-heading-regexp))
  9610. (or (not (match-beginning 5))
  9611. (< (point) (match-beginning 5)))))
  9612. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9613. (links (car data))
  9614. (links-end (cdr data)))
  9615. (if links
  9616. (dolist (link (if (stringp links) (list links) links))
  9617. (search-forward link nil links-end)
  9618. (goto-char (match-beginning 0))
  9619. (org-open-at-point))
  9620. (require 'org-attach)
  9621. (org-attach-reveal 'if-exists))))
  9622. ;; Do nothing on white spaces after an object, unless point
  9623. ;; is right after it.
  9624. ((> (point)
  9625. (save-excursion
  9626. (goto-char (org-element-property :end context))
  9627. (skip-chars-backward " \t")
  9628. (point)))
  9629. (user-error "No link found"))
  9630. ((eq type 'timestamp) (org-follow-timestamp-link))
  9631. ;; On tags within a headline or an inlinetask.
  9632. ((and (memq type '(headline inlinetask))
  9633. (progn (save-excursion (beginning-of-line)
  9634. (looking-at org-complex-heading-regexp))
  9635. (and (match-beginning 5)
  9636. (>= (point) (match-beginning 5)))))
  9637. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9638. ((eq type 'link)
  9639. ;; When link is located within the description of another
  9640. ;; link (e.g., an inline image), always open the parent
  9641. ;; link.
  9642. (let*((link (let ((up (org-element-property :parent context)))
  9643. (if (eq (org-element-type up) 'link) up context)))
  9644. (type (org-element-property :type link))
  9645. (path (org-link-unescape (org-element-property :path link))))
  9646. ;; Switch back to REFERENCE-BUFFER needed when called in
  9647. ;; a temporary buffer through `org-open-link-from-string'.
  9648. (with-current-buffer (or reference-buffer (current-buffer))
  9649. (cond
  9650. ((equal type "file")
  9651. (if (string-match "[*?{]" (file-name-nondirectory path))
  9652. (dired path)
  9653. ;; Look into `org-link-protocols' in order to find
  9654. ;; a DEDICATED-FUNCTION to open file. The function
  9655. ;; will be applied on raw link instead of parsed
  9656. ;; link due to the limitation in `org-add-link-type'
  9657. ;; ("open" function called with a single argument).
  9658. ;; If no such function is found, fallback to
  9659. ;; `org-open-file'.
  9660. ;;
  9661. ;; Note : "file+emacs" and "file+sys" types are
  9662. ;; hard-coded in order to escape the previous
  9663. ;; limitation.
  9664. (let* ((option (org-element-property :search-option link))
  9665. (app (org-element-property :application link))
  9666. (dedicated-function
  9667. (nth 1 (assoc app org-link-protocols))))
  9668. (if dedicated-function
  9669. (funcall dedicated-function
  9670. (concat path
  9671. (and option (concat "::" option))))
  9672. (apply 'org-open-file
  9673. path
  9674. (cond (arg)
  9675. ((equal app "emacs") 'emacs)
  9676. ((equal app "sys") 'system))
  9677. (cond ((not option) nil)
  9678. ((org-string-match-p "\\`[0-9]+\\'" option)
  9679. (list (string-to-number option)))
  9680. (t (list nil
  9681. (org-link-unescape option)))))))))
  9682. ((assoc type org-link-protocols)
  9683. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9684. ((equal type "help")
  9685. (let ((f-or-v (intern path)))
  9686. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9687. ((boundp f-or-v) (describe-variable f-or-v))
  9688. (t (error "Not a known function or variable")))))
  9689. ((member type '("http" "https" "ftp" "mailto" "news"))
  9690. (browse-url (org-link-escape-browser (concat type ":" path))))
  9691. ((equal type "doi")
  9692. (browse-url
  9693. (org-link-escape-browser (concat org-doi-server-url path))))
  9694. ((equal type "message") (browse-url (concat type ":" path)))
  9695. ((equal type "shell")
  9696. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9697. (cmd path))
  9698. (if (or (and (org-string-nw-p
  9699. org-confirm-shell-link-not-regexp)
  9700. (string-match
  9701. org-confirm-shell-link-not-regexp cmd))
  9702. (not org-confirm-shell-link-function)
  9703. (funcall org-confirm-shell-link-function
  9704. (format "Execute \"%s\" in shell? "
  9705. (org-add-props cmd nil
  9706. 'face 'org-warning))))
  9707. (progn
  9708. (message "Executing %s" cmd)
  9709. (shell-command cmd buf)
  9710. (when (featurep 'midnight)
  9711. (setq clean-buffer-list-kill-buffer-names
  9712. (cons buf
  9713. clean-buffer-list-kill-buffer-names))))
  9714. (user-error "Abort"))))
  9715. ((equal type "elisp")
  9716. (let ((cmd path))
  9717. (if (or (and (org-string-nw-p
  9718. org-confirm-elisp-link-not-regexp)
  9719. (org-string-match-p
  9720. org-confirm-elisp-link-not-regexp cmd))
  9721. (not org-confirm-elisp-link-function)
  9722. (funcall org-confirm-elisp-link-function
  9723. (format "Execute \"%s\" as elisp? "
  9724. (org-add-props cmd nil
  9725. 'face 'org-warning))))
  9726. (message "%s => %s" cmd
  9727. (if (eq (string-to-char cmd) ?\()
  9728. (eval (read cmd))
  9729. (call-interactively (read cmd))))
  9730. (user-error "Abort"))))
  9731. ((equal type "id")
  9732. (require 'ord-id)
  9733. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9734. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9735. (unless (run-hook-with-args-until-success
  9736. 'org-open-link-functions path)
  9737. (if (not arg) (org-mark-ring-push)
  9738. (switch-to-buffer-other-window
  9739. (org-get-buffer-for-internal-link (current-buffer))))
  9740. (let ((cmd `(org-link-search
  9741. ,(if (member type '("custom-id" "coderef"))
  9742. (org-element-property :raw-link link)
  9743. path)
  9744. ,(cond ((equal arg '(4)) 'occur)
  9745. ((equal arg '(16)) 'org-occur))
  9746. ,(org-element-property :begin link))))
  9747. (condition-case nil
  9748. (let ((org-link-search-inhibit-query t))
  9749. (eval cmd))
  9750. (error (progn (widen) (eval cmd)))))))
  9751. (t (browse-url-at-point))))))
  9752. ;; On a footnote reference or at a footnote definition's label.
  9753. ((or (eq type 'footnote-reference)
  9754. (and (eq type 'footnote-definition)
  9755. (save-excursion
  9756. ;; Do not validate action when point is on the
  9757. ;; spaces right after the footnote label, in
  9758. ;; order to be on par with behaviour on links.
  9759. (skip-chars-forward " \t")
  9760. (let ((begin
  9761. (org-element-property :contents-begin context)))
  9762. (if begin (< (point) begin)
  9763. (= (org-element-property :post-affiliated context)
  9764. (line-beginning-position)))))))
  9765. (org-footnote-action))
  9766. (t (user-error "No link found")))))
  9767. (move-marker org-open-link-marker nil)
  9768. (run-hook-with-args 'org-follow-link-hook)))
  9769. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9770. "Offer links in the current entry and return the selected link.
  9771. If there is only one link, return it.
  9772. If NTH is an integer, return the NTH link found.
  9773. If ZERO is a string, check also this string for a link, and if
  9774. there is one, return it."
  9775. (with-current-buffer buffer
  9776. (save-excursion
  9777. (save-restriction
  9778. (widen)
  9779. (goto-char marker)
  9780. (let ((cnt ?0)
  9781. (in-emacs (if (integerp nth) nil nth))
  9782. have-zero end links link c)
  9783. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9784. (push (match-string 0 zero) links)
  9785. (setq cnt (1- cnt) have-zero t))
  9786. (save-excursion
  9787. (org-back-to-heading t)
  9788. (setq end (save-excursion (outline-next-heading) (point)))
  9789. (while (re-search-forward org-any-link-re end t)
  9790. (push (match-string 0) links))
  9791. (setq links (org-uniquify (reverse links))))
  9792. (cond
  9793. ((null links)
  9794. (message "No links"))
  9795. ((equal (length links) 1)
  9796. (setq link (car links)))
  9797. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9798. (setq link (nth (if have-zero nth (1- nth)) links)))
  9799. (t ; we have to select a link
  9800. (save-excursion
  9801. (save-window-excursion
  9802. (delete-other-windows)
  9803. (with-output-to-temp-buffer "*Select Link*"
  9804. (mapc (lambda (l)
  9805. (if (not (string-match org-bracket-link-regexp l))
  9806. (princ (format "[%c] %s\n" (incf cnt)
  9807. (org-remove-angle-brackets l)))
  9808. (if (match-end 3)
  9809. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9810. (match-string 3 l) (match-string 1 l)))
  9811. (princ (format "[%c] %s\n" (incf cnt)
  9812. (match-string 1 l))))))
  9813. links))
  9814. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9815. (message "Select link to open, RET to open all:")
  9816. (setq c (read-char-exclusive))
  9817. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9818. (when (equal c ?q) (user-error "Abort"))
  9819. (if (equal c ?\C-m)
  9820. (setq link links)
  9821. (setq nth (- c ?0))
  9822. (if have-zero (setq nth (1+ nth)))
  9823. (unless (and (integerp nth) (>= (length links) nth))
  9824. (user-error "Invalid link selection"))
  9825. (setq link (nth (1- nth) links)))))
  9826. (cons link end))))))
  9827. ;; TODO: These functions are deprecated since `org-open-at-point'
  9828. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9829. (defun org-open-file-with-system (path)
  9830. "Open file at PATH using the system way of opening it."
  9831. (org-open-file path 'system))
  9832. (defun org-open-file-with-emacs (path)
  9833. "Open file at PATH in Emacs."
  9834. (org-open-file path 'emacs))
  9835. ;;; File search
  9836. (defvar org-create-file-search-functions nil
  9837. "List of functions to construct the right search string for a file link.
  9838. These functions are called in turn with point at the location to
  9839. which the link should point.
  9840. A function in the hook should first test if it would like to
  9841. handle this file type, for example by checking the `major-mode'
  9842. or the file extension. If it decides not to handle this file, it
  9843. should just return nil to give other functions a chance. If it
  9844. does handle the file, it must return the search string to be used
  9845. when following the link. The search string will be part of the
  9846. file link, given after a double colon, and `org-open-at-point'
  9847. will automatically search for it. If special measures must be
  9848. taken to make the search successful, another function should be
  9849. added to the companion hook `org-execute-file-search-functions',
  9850. which see.
  9851. A function in this hook may also use `setq' to set the variable
  9852. `description' to provide a suggestion for the descriptive text to
  9853. be used for this link when it gets inserted into an Org-mode
  9854. buffer with \\[org-insert-link].")
  9855. (defvar org-execute-file-search-functions nil
  9856. "List of functions to execute a file search triggered by a link.
  9857. Functions added to this hook must accept a single argument, the
  9858. search string that was part of the file link, the part after the
  9859. double colon. The function must first check if it would like to
  9860. handle this search, for example by checking the `major-mode' or
  9861. the file extension. If it decides not to handle this search, it
  9862. should just return nil to give other functions a chance. If it
  9863. does handle the search, it must return a non-nil value to keep
  9864. other functions from trying.
  9865. Each function can access the current prefix argument through the
  9866. variable `current-prefix-arg'. Note that a single prefix is used
  9867. to force opening a link in Emacs, so it may be good to only use a
  9868. numeric or double prefix to guide the search function.
  9869. In case this is needed, a function in this hook can also restore
  9870. the window configuration before `org-open-at-point' was called using:
  9871. (set-window-configuration org-window-config-before-follow-link)")
  9872. (defun org-link-search (s &optional type avoid-pos stealth)
  9873. "Search for a link search option.
  9874. If S is surrounded by forward slashes, it is interpreted as a
  9875. regular expression. In org-mode files, this will create an `org-occur'
  9876. sparse tree. In ordinary files, `occur' will be used to list matches.
  9877. If the current buffer is in `dired-mode', grep will be used to search
  9878. in all files. If AVOID-POS is given, ignore matches near that position.
  9879. When optional argument STEALTH is non-nil, do not modify
  9880. visibility around point, thus ignoring `org-show-context-detail'
  9881. variable."
  9882. (let ((case-fold-search t)
  9883. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9884. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9885. (append '(("") (" ") ("\t") ("\n"))
  9886. org-emphasis-alist)
  9887. "\\|") "\\)"))
  9888. (pos (point))
  9889. words re0 re2 re4_ re4 re5 re2a re2a_ reall)
  9890. (cond
  9891. ;; First check if there are any special search functions
  9892. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9893. ;; Now try the builtin stuff
  9894. ((and (equal (string-to-char s0) ?#)
  9895. (> (length s0) 1)
  9896. (save-excursion
  9897. (goto-char (point-min))
  9898. (and
  9899. (re-search-forward
  9900. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9901. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9902. (setq type 'dedicated
  9903. pos (match-beginning 0))))
  9904. ;; There is an exact target for this
  9905. (goto-char pos)
  9906. (org-back-to-heading t)))
  9907. ((save-excursion
  9908. (goto-char (point-min))
  9909. (and
  9910. (re-search-forward
  9911. (concat "<<" (regexp-quote s0) ">>") nil t)
  9912. (setq type 'dedicated
  9913. pos (match-beginning 0))))
  9914. ;; There is an exact target for this
  9915. (goto-char pos))
  9916. ((save-excursion
  9917. (goto-char (point-min))
  9918. (and
  9919. (re-search-forward
  9920. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9921. (setq type 'dedicated pos (match-beginning 0))))
  9922. ;; Found an element with a matching #+name affiliated keyword.
  9923. (goto-char pos))
  9924. ((and (string-match "^(\\(.*\\))$" s0)
  9925. (save-excursion
  9926. (goto-char (point-min))
  9927. (and
  9928. (re-search-forward
  9929. (concat "[^[]" (regexp-quote
  9930. (format org-coderef-label-format
  9931. (match-string 1 s0))))
  9932. nil t)
  9933. (setq type 'dedicated
  9934. pos (1+ (match-beginning 0))))))
  9935. ;; There is a coderef target for this
  9936. (goto-char pos))
  9937. ((string-match "^/\\(.*\\)/$" s)
  9938. ;; A regular expression
  9939. (cond
  9940. ((derived-mode-p 'org-mode)
  9941. (org-occur (match-string 1 s)))
  9942. (t (org-do-occur (match-string 1 s)))))
  9943. ((and (derived-mode-p 'org-mode)
  9944. (or (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9945. org-link-search-must-match-exact-headline))
  9946. ;; Headline search.
  9947. (goto-char (point-min))
  9948. (cond
  9949. ((let (case-fold-search)
  9950. (re-search-forward
  9951. (let* ((wspace "[ \t]")
  9952. (wspaceopt (concat wspace "*"))
  9953. (cookie (concat "\\(?:"
  9954. wspaceopt
  9955. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9956. wspaceopt
  9957. "\\)"))
  9958. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)")))
  9959. (concat
  9960. org-outline-regexp-bol
  9961. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9962. "\\(?:\\[#.\\][ \t]+\\)?"
  9963. sep "*"
  9964. (mapconcat #'identity
  9965. (org-split-string (regexp-quote s))
  9966. (concat sep "+"))
  9967. sep "*"
  9968. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9969. "[ \t]*$"))
  9970. nil t))
  9971. ;; OK, found a match
  9972. (setq type 'dedicated)
  9973. (goto-char (match-beginning 0)))
  9974. ((and (not org-link-search-inhibit-query)
  9975. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9976. (y-or-n-p "No match - create this as a new heading? "))
  9977. (goto-char (point-max))
  9978. (unless (bolp) (newline))
  9979. (org-insert-heading nil t t)
  9980. (insert s "\n")
  9981. (beginning-of-line 0))
  9982. (t
  9983. (goto-char pos)
  9984. (error "No match"))))
  9985. (t
  9986. ;; A normal search string
  9987. (remove-text-properties
  9988. 0 (length s)
  9989. '(face nil mouse-face nil keymap nil fontified nil) s)
  9990. ;; Make a series of regular expressions to find a match
  9991. (setq words (org-split-string s "[ \n\r\t]+")
  9992. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9993. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9994. "\\)" markers)
  9995. re2a_ (concat "\\(" (mapconcat 'downcase words
  9996. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9997. re2a (concat "[ \t\r\n]" re2a_)
  9998. re4_ (concat "\\(" (mapconcat 'downcase words
  9999. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10000. re4 (concat "[^a-zA-Z_]" re4_)
  10001. re5 (concat ".*" re4)
  10002. reall (concat "\\(" re0 "\\)\\|\\(" re2 "\\)\\|\\(" re4
  10003. "\\)\\|\\(" re5 "\\)"))
  10004. (cond
  10005. ((eq type 'org-occur) (org-occur reall))
  10006. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10007. (t (goto-char (point-min))
  10008. (setq type 'fuzzy)
  10009. (if (or (and (org-search-not-self 1 re0 nil t)
  10010. (setq type 'dedicated))
  10011. (org-search-not-self 1 re2 nil t)
  10012. (org-search-not-self 1 re2a nil t)
  10013. (org-search-not-self 1 re4 nil t)
  10014. (org-search-not-self 1 re5 nil t))
  10015. (goto-char (match-beginning 1))
  10016. (goto-char pos)
  10017. (error "No match"))))))
  10018. (and (derived-mode-p 'org-mode)
  10019. (not stealth)
  10020. (org-show-context 'link-search))
  10021. type))
  10022. (defun org-search-not-self (group &rest args)
  10023. "Execute `re-search-forward', but only accept matches that do not
  10024. enclose the position of `org-open-link-marker'."
  10025. (let ((m org-open-link-marker))
  10026. (catch 'exit
  10027. (while (apply 're-search-forward args)
  10028. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10029. (goto-char (match-end group))
  10030. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10031. (> (match-beginning 0) (marker-position m))
  10032. (< (match-end 0) (marker-position m)))
  10033. (save-match-data
  10034. (or (not (org-in-regexp
  10035. org-bracket-link-analytic-regexp 1))
  10036. (not (match-end 4)) ; no description
  10037. (and (<= (match-beginning 4) (point))
  10038. (>= (match-end 4) (point))))))
  10039. (throw 'exit (point))))))))
  10040. (defun org-get-buffer-for-internal-link (buffer)
  10041. "Return a buffer to be used for displaying the link target of internal links."
  10042. (cond
  10043. ((not org-display-internal-link-with-indirect-buffer)
  10044. buffer)
  10045. ((string-match "(Clone)$" (buffer-name buffer))
  10046. (message "Buffer is already a clone, not making another one")
  10047. ;; we also do not modify visibility in this case
  10048. buffer)
  10049. (t ; make a new indirect buffer for displaying the link
  10050. (let* ((bn (buffer-name buffer))
  10051. (ibn (concat bn "(Clone)"))
  10052. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10053. (with-current-buffer ib (org-overview))
  10054. ib))))
  10055. (defun org-do-occur (regexp &optional cleanup)
  10056. "Call the Emacs command `occur'.
  10057. If CLEANUP is non-nil, remove the printout of the regular expression
  10058. in the *Occur* buffer. This is useful if the regex is long and not useful
  10059. to read."
  10060. (occur regexp)
  10061. (when cleanup
  10062. (let ((cwin (selected-window)) win beg end)
  10063. (when (setq win (get-buffer-window "*Occur*"))
  10064. (select-window win))
  10065. (goto-char (point-min))
  10066. (when (re-search-forward "match[a-z]+" nil t)
  10067. (setq beg (match-end 0))
  10068. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10069. (setq end (1- (match-beginning 0)))))
  10070. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10071. (goto-char (point-min))
  10072. (select-window cwin))))
  10073. ;;; The mark ring for links jumps
  10074. (defvar org-mark-ring nil
  10075. "Mark ring for positions before jumps in Org-mode.")
  10076. (defvar org-mark-ring-last-goto nil
  10077. "Last position in the mark ring used to go back.")
  10078. ;; Fill and close the ring
  10079. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10080. (loop for i from 1 to org-mark-ring-length do
  10081. (push (make-marker) org-mark-ring))
  10082. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10083. org-mark-ring)
  10084. (defun org-mark-ring-push (&optional pos buffer)
  10085. "Put the current position or POS into the mark ring and rotate it."
  10086. (interactive)
  10087. (setq pos (or pos (point)))
  10088. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10089. (move-marker (car org-mark-ring)
  10090. (or pos (point))
  10091. (or buffer (current-buffer)))
  10092. (message "%s"
  10093. (substitute-command-keys
  10094. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10095. (defun org-mark-ring-goto (&optional n)
  10096. "Jump to the previous position in the mark ring.
  10097. With prefix arg N, jump back that many stored positions. When
  10098. called several times in succession, walk through the entire ring.
  10099. Org-mode commands jumping to a different position in the current file,
  10100. or to another Org-mode file, automatically push the old position
  10101. onto the ring."
  10102. (interactive "p")
  10103. (let (p m)
  10104. (if (eq last-command this-command)
  10105. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10106. (setq p org-mark-ring))
  10107. (setq org-mark-ring-last-goto p)
  10108. (setq m (car p))
  10109. (org-pop-to-buffer-same-window (marker-buffer m))
  10110. (goto-char m)
  10111. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10112. (defun org-remove-angle-brackets (s)
  10113. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10114. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10115. s)
  10116. (defun org-add-angle-brackets (s)
  10117. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10118. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10119. s)
  10120. (defun org-remove-double-quotes (s)
  10121. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10122. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10123. s)
  10124. ;;; Following specific links
  10125. (defun org-follow-timestamp-link ()
  10126. "Open an agenda view for the time-stamp date/range at point."
  10127. (cond
  10128. ((org-at-date-range-p t)
  10129. (let ((org-agenda-start-on-weekday)
  10130. (t1 (match-string 1))
  10131. (t2 (match-string 2)) tt1 tt2)
  10132. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10133. tt2 (time-to-days (org-time-string-to-time t2)))
  10134. (let ((org-agenda-buffer-tmp-name
  10135. (format "*Org Agenda(a:%s)"
  10136. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10137. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10138. ((org-at-timestamp-p t)
  10139. (let ((org-agenda-buffer-tmp-name
  10140. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10141. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10142. (substring (match-string 1) 0 10)))
  10143. 1)))
  10144. (t (error "This should not happen"))))
  10145. ;;; Following file links
  10146. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10147. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10148. (declare-function mailcap-mime-info
  10149. "mailcap" (string &optional request no-decode))
  10150. (defvar org-wait nil)
  10151. (defun org-open-file (path &optional in-emacs line search)
  10152. "Open the file at PATH.
  10153. First, this expands any special file name abbreviations. Then the
  10154. configuration variable `org-file-apps' is checked if it contains an
  10155. entry for this file type, and if yes, the corresponding command is launched.
  10156. If no application is found, Emacs simply visits the file.
  10157. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10158. With a double \\[universal-argument] \\[universal-argument] \
  10159. prefix arg, Org tries to avoid opening in Emacs
  10160. and to use an external application to visit the file.
  10161. Optional LINE specifies a line to go to, optional SEARCH a string
  10162. to search for. If LINE or SEARCH is given, the file will be
  10163. opened in Emacs, unless an entry from org-file-apps that makes
  10164. use of groups in a regexp matches.
  10165. If you want to change the way frames are used when following a
  10166. link, please customize `org-link-frame-setup'.
  10167. If the file does not exist, an error is thrown."
  10168. (let* ((file (if (equal path "")
  10169. buffer-file-name
  10170. (substitute-in-file-name (expand-file-name path))))
  10171. (file-apps (append org-file-apps (org-default-apps)))
  10172. (apps (org-remove-if
  10173. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10174. (apps-dlink (org-remove-if-not
  10175. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10176. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10177. (dirp (if remp nil (file-directory-p file)))
  10178. (file (if (and dirp org-open-directory-means-index-dot-org)
  10179. (concat (file-name-as-directory file) "index.org")
  10180. file))
  10181. (a-m-a-p (assq 'auto-mode apps))
  10182. (dfile (downcase file))
  10183. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10184. (link (cond ((and (eq line nil)
  10185. (eq search nil))
  10186. file)
  10187. (line
  10188. (concat file "::" (number-to-string line)))
  10189. (search
  10190. (concat file "::" search))))
  10191. (dlink (downcase link))
  10192. (old-buffer (current-buffer))
  10193. (old-pos (point))
  10194. (old-mode major-mode)
  10195. ext cmd link-match-data)
  10196. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10197. (setq ext (match-string 1 dfile))
  10198. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10199. (setq ext (match-string 1 dfile))))
  10200. (cond
  10201. ((member in-emacs '((16) system))
  10202. (setq cmd (cdr (assoc 'system apps))))
  10203. (in-emacs (setq cmd 'emacs))
  10204. (t
  10205. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10206. (and dirp (cdr (assoc 'directory apps)))
  10207. ; first, try matching against apps-dlink
  10208. ; if we get a match here, store the match data for later
  10209. (let ((match (assoc-default dlink apps-dlink
  10210. 'string-match)))
  10211. (if match
  10212. (progn (setq link-match-data (match-data))
  10213. match)
  10214. (progn (setq in-emacs (or in-emacs line search))
  10215. nil))) ; if we have no match in apps-dlink,
  10216. ; always open the file in emacs if line or search
  10217. ; is given (for backwards compatibility)
  10218. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10219. 'string-match)
  10220. (cdr (assoc ext apps))
  10221. (cdr (assoc t apps))))))
  10222. (when (eq cmd 'system)
  10223. (setq cmd (cdr (assoc 'system apps))))
  10224. (when (eq cmd 'default)
  10225. (setq cmd (cdr (assoc t apps))))
  10226. (when (eq cmd 'mailcap)
  10227. (require 'mailcap)
  10228. (mailcap-parse-mailcaps)
  10229. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10230. (command (mailcap-mime-info mime-type)))
  10231. (if (stringp command)
  10232. (setq cmd command)
  10233. (setq cmd 'emacs))))
  10234. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10235. (not (file-exists-p file))
  10236. (not org-open-non-existing-files))
  10237. (user-error "No such file: %s" file))
  10238. (cond
  10239. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10240. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10241. (while (string-match "['\"]%s['\"]" cmd)
  10242. (setq cmd (replace-match "%s" t t cmd)))
  10243. (while (string-match "%s" cmd)
  10244. (setq cmd (replace-match
  10245. (save-match-data
  10246. (shell-quote-argument
  10247. (convert-standard-filename file)))
  10248. t t cmd)))
  10249. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10250. (save-match-data
  10251. (let ((match-index 1)
  10252. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10253. (set-match-data link-match-data)
  10254. (while (<= match-index number-of-groups)
  10255. (let ((regex (concat "%" (number-to-string match-index)))
  10256. (replace-with (match-string match-index dlink)))
  10257. (while (string-match regex cmd)
  10258. (setq cmd (replace-match replace-with t t cmd))))
  10259. (setq match-index (+ match-index 1)))))
  10260. (save-window-excursion
  10261. (message "Running %s...done" cmd)
  10262. (start-process-shell-command cmd nil cmd)
  10263. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10264. ((or (stringp cmd)
  10265. (eq cmd 'emacs))
  10266. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10267. (widen)
  10268. (if line (progn (org-goto-line line)
  10269. (if (derived-mode-p 'org-mode)
  10270. (org-reveal)))
  10271. (if search (org-link-search search))))
  10272. ((consp cmd)
  10273. (let ((file (convert-standard-filename file)))
  10274. (save-match-data
  10275. (set-match-data link-match-data)
  10276. (eval cmd))))
  10277. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10278. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10279. (or (not (equal old-buffer (current-buffer)))
  10280. (not (equal old-pos (point))))
  10281. (org-mark-ring-push old-pos old-buffer))))
  10282. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10283. "This function returns non-nil if `entry' uses a regular
  10284. expression which should be matched against the whole link by
  10285. org-open-file.
  10286. It assumes that is the case when the entry uses a regular
  10287. expression which has at least one grouping construct and the
  10288. action is either a lisp form or a command string containing
  10289. '%1', i.e. using at least one subexpression match as a
  10290. parameter."
  10291. (let ((selector (car entry))
  10292. (action (cdr entry)))
  10293. (if (stringp selector)
  10294. (and (> (regexp-opt-depth selector) 0)
  10295. (or (and (stringp action)
  10296. (string-match "%[0-9]" action))
  10297. (consp action)))
  10298. nil)))
  10299. (defun org-default-apps ()
  10300. "Return the default applications for this operating system."
  10301. (cond
  10302. ((eq system-type 'darwin)
  10303. org-file-apps-defaults-macosx)
  10304. ((eq system-type 'windows-nt)
  10305. org-file-apps-defaults-windowsnt)
  10306. (t org-file-apps-defaults-gnu)))
  10307. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10308. "Convert extensions to regular expressions in the cars of LIST.
  10309. Also, weed out any non-string entries, because the return value is used
  10310. only for regexp matching.
  10311. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10312. point to the symbol `emacs', indicating that the file should
  10313. be opened in Emacs."
  10314. (append
  10315. (delq nil
  10316. (mapcar (lambda (x)
  10317. (if (not (stringp (car x)))
  10318. nil
  10319. (if (string-match "\\W" (car x))
  10320. x
  10321. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10322. list))
  10323. (if add-auto-mode
  10324. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10325. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10326. (defun org-file-remote-p (file)
  10327. "Test whether FILE specifies a location on a remote system.
  10328. Return non-nil if the location is indeed remote.
  10329. For example, the filename \"/user@host:/foo\" specifies a location
  10330. on the system \"/user@host:\"."
  10331. (cond ((fboundp 'file-remote-p)
  10332. (file-remote-p file))
  10333. ((fboundp 'tramp-handle-file-remote-p)
  10334. (tramp-handle-file-remote-p file))
  10335. ((and (boundp 'ange-ftp-name-format)
  10336. (string-match (car ange-ftp-name-format) file))
  10337. t)))
  10338. ;;;; Refiling
  10339. (defun org-get-org-file ()
  10340. "Read a filename, with default directory `org-directory'."
  10341. (let ((default (or org-default-notes-file remember-data-file)))
  10342. (read-file-name (format "File name [%s]: " default)
  10343. (file-name-as-directory org-directory)
  10344. default)))
  10345. (defun org-notes-order-reversed-p ()
  10346. "Check if the current file should receive notes in reversed order."
  10347. (cond
  10348. ((not org-reverse-note-order) nil)
  10349. ((eq t org-reverse-note-order) t)
  10350. ((not (listp org-reverse-note-order)) nil)
  10351. (t (catch 'exit
  10352. (let ((all org-reverse-note-order)
  10353. entry)
  10354. (while (setq entry (pop all))
  10355. (if (string-match (car entry) buffer-file-name)
  10356. (throw 'exit (cdr entry))))
  10357. nil)))))
  10358. (defvar org-refile-target-table nil
  10359. "The list of refile targets, created by `org-refile'.")
  10360. (defvar org-agenda-new-buffers nil
  10361. "Buffers created to visit agenda files.")
  10362. (defvar org-refile-cache nil
  10363. "Cache for refile targets.")
  10364. (defvar org-refile-markers nil
  10365. "All the markers used for caching refile locations.")
  10366. (defun org-refile-marker (pos)
  10367. "Get a new refile marker, but only if caching is in use."
  10368. (if (not org-refile-use-cache)
  10369. pos
  10370. (let ((m (make-marker)))
  10371. (move-marker m pos)
  10372. (push m org-refile-markers)
  10373. m)))
  10374. (defun org-refile-cache-clear ()
  10375. "Clear the refile cache and disable all the markers."
  10376. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10377. (setq org-refile-markers nil)
  10378. (setq org-refile-cache nil)
  10379. (message "Refile cache has been cleared"))
  10380. (defun org-refile-cache-check-set (set)
  10381. "Check if all the markers in the cache still have live buffers."
  10382. (let (marker)
  10383. (catch 'exit
  10384. (while (and set (setq marker (nth 3 (pop set))))
  10385. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10386. (when (and marker (null (marker-buffer marker)))
  10387. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10388. (sit-for 3)
  10389. (throw 'exit nil)))
  10390. t)))
  10391. (defun org-refile-cache-put (set &rest identifiers)
  10392. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10393. (let* ((key (sha1 (prin1-to-string identifiers)))
  10394. (entry (assoc key org-refile-cache)))
  10395. (if entry
  10396. (setcdr entry set)
  10397. (push (cons key set) org-refile-cache))))
  10398. (defun org-refile-cache-get (&rest identifiers)
  10399. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10400. (cond
  10401. ((not org-refile-cache) nil)
  10402. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10403. (t
  10404. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10405. org-refile-cache))))
  10406. (and set (org-refile-cache-check-set set) set)))))
  10407. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10408. "Produce a table with refile targets."
  10409. (let ((case-fold-search nil)
  10410. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10411. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10412. targets tgs txt re files f desc descre fast-path-p level pos0)
  10413. (message "Getting targets...")
  10414. (with-current-buffer (or default-buffer (current-buffer))
  10415. (while (setq entry (pop entries))
  10416. (setq files (car entry) desc (cdr entry))
  10417. (setq fast-path-p nil)
  10418. (cond
  10419. ((null files) (setq files (list (current-buffer))))
  10420. ((eq files 'org-agenda-files)
  10421. (setq files (org-agenda-files 'unrestricted)))
  10422. ((and (symbolp files) (fboundp files))
  10423. (setq files (funcall files)))
  10424. ((and (symbolp files) (boundp files))
  10425. (setq files (symbol-value files))))
  10426. (if (stringp files) (setq files (list files)))
  10427. (cond
  10428. ((eq (car desc) :tag)
  10429. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10430. ((eq (car desc) :todo)
  10431. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10432. ((eq (car desc) :regexp)
  10433. (setq descre (cdr desc)))
  10434. ((eq (car desc) :level)
  10435. (setq descre (concat "^\\*\\{" (number-to-string
  10436. (if org-odd-levels-only
  10437. (1- (* 2 (cdr desc)))
  10438. (cdr desc)))
  10439. "\\}[ \t]")))
  10440. ((eq (car desc) :maxlevel)
  10441. (setq fast-path-p t)
  10442. (setq descre (concat "^\\*\\{1," (number-to-string
  10443. (if org-odd-levels-only
  10444. (1- (* 2 (cdr desc)))
  10445. (cdr desc)))
  10446. "\\}[ \t]")))
  10447. (t (error "Bad refiling target description %s" desc)))
  10448. (while (setq f (pop files))
  10449. (with-current-buffer
  10450. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10451. (or
  10452. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10453. (progn
  10454. (if (bufferp f) (setq f (buffer-file-name
  10455. (buffer-base-buffer f))))
  10456. (setq f (and f (expand-file-name f)))
  10457. (if (eq org-refile-use-outline-path 'file)
  10458. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10459. (save-excursion
  10460. (save-restriction
  10461. (widen)
  10462. (goto-char (point-min))
  10463. (while (re-search-forward descre nil t)
  10464. (goto-char (setq pos0 (point-at-bol)))
  10465. (catch 'next
  10466. (when org-refile-target-verify-function
  10467. (save-match-data
  10468. (or (funcall org-refile-target-verify-function)
  10469. (throw 'next t))))
  10470. (when (and (looking-at org-complex-heading-regexp)
  10471. (not (member (match-string 4) excluded-entries))
  10472. (match-string 4))
  10473. (setq level (org-reduced-level
  10474. (- (match-end 1) (match-beginning 1)))
  10475. txt (org-link-display-format (match-string 4))
  10476. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10477. re (format org-complex-heading-regexp-format
  10478. (regexp-quote (match-string 4))))
  10479. (when org-refile-use-outline-path
  10480. (setq txt (mapconcat
  10481. 'org-protect-slash
  10482. (append
  10483. (if (eq org-refile-use-outline-path
  10484. 'file)
  10485. (list (file-name-nondirectory
  10486. (buffer-file-name
  10487. (buffer-base-buffer))))
  10488. (if (eq org-refile-use-outline-path
  10489. 'full-file-path)
  10490. (list (buffer-file-name
  10491. (buffer-base-buffer)))))
  10492. (org-get-outline-path fast-path-p
  10493. level txt)
  10494. (list txt))
  10495. "/")))
  10496. (push (list txt f re (org-refile-marker (point)))
  10497. tgs)))
  10498. (when (= (point) pos0)
  10499. ;; verification function has not moved point
  10500. (goto-char (point-at-eol))))))))
  10501. (when org-refile-use-cache
  10502. (org-refile-cache-put tgs (buffer-file-name) descre))
  10503. (setq targets (append tgs targets))))))
  10504. (message "Getting targets...done")
  10505. (nreverse targets)))
  10506. (defun org-protect-slash (s)
  10507. (while (string-match "/" s)
  10508. (setq s (replace-match "\\" t t s)))
  10509. s)
  10510. (defvar org-olpa (make-vector 20 nil))
  10511. (defun org-get-outline-path (&optional fastp level heading)
  10512. "Return the outline path to the current entry, as a list.
  10513. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10514. routine which makes outline path derivations for an entire file,
  10515. avoiding backtracing. Refile target collection makes use of that."
  10516. (if fastp
  10517. (progn
  10518. (if (> level 19)
  10519. (error "Outline path failure, more than 19 levels"))
  10520. (loop for i from level upto 19 do
  10521. (aset org-olpa i nil))
  10522. (prog1
  10523. (delq nil (append org-olpa nil))
  10524. (aset org-olpa level heading)))
  10525. (let (rtn case-fold-search)
  10526. (save-excursion
  10527. (save-restriction
  10528. (widen)
  10529. (while (org-up-heading-safe)
  10530. (when (looking-at org-complex-heading-regexp)
  10531. (push (org-trim
  10532. (replace-regexp-in-string
  10533. ;; Remove statistical/checkboxes cookies
  10534. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10535. (org-match-string-no-properties 4)))
  10536. rtn)))
  10537. rtn)))))
  10538. (defun org-format-outline-path (path &optional width prefix separator)
  10539. "Format the outline path PATH for display.
  10540. WIDTH is the maximum number of characters that is available.
  10541. PREFIX is a prefix to be included in the returned string,
  10542. such as the file name.
  10543. SEPARATOR is inserted between the different parts of the path,
  10544. the default is \"/\"."
  10545. (setq width (or width 79))
  10546. (if prefix (setq width (- width (length prefix))))
  10547. (if (not path)
  10548. (or prefix "")
  10549. (let* ((nsteps (length path))
  10550. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10551. (maxwidth (if (<= total-width width)
  10552. 10000 ;; everything fits
  10553. ;; we need to shorten the level headings
  10554. (/ (- width nsteps) nsteps)))
  10555. (org-odd-levels-only nil)
  10556. (n 0)
  10557. (total (1+ (length prefix))))
  10558. (setq maxwidth (max maxwidth 10))
  10559. (concat prefix
  10560. (if prefix (or separator "/"))
  10561. (mapconcat
  10562. (lambda (h)
  10563. (setq n (1+ n))
  10564. (if (and (= n nsteps) (< maxwidth 10000))
  10565. (setq maxwidth (- total-width total)))
  10566. (if (< (length h) maxwidth)
  10567. (progn (setq total (+ total (length h) 1)) h)
  10568. (setq h (substring h 0 (- maxwidth 2))
  10569. total (+ total maxwidth 1))
  10570. (if (string-match "[ \t]+\\'" h)
  10571. (setq h (substring h 0 (match-beginning 0))))
  10572. (setq h (concat h "..")))
  10573. (org-add-props h nil 'face
  10574. (nth (% (1- n) org-n-level-faces)
  10575. org-level-faces))
  10576. h)
  10577. path (or separator "/"))))))
  10578. (defun org-display-outline-path (&optional file current separator just-return-string)
  10579. "Display the current outline path in the echo area.
  10580. If FILE is non-nil, prepend the output with the file name.
  10581. If CURRENT is non-nil, append the current heading to the output.
  10582. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10583. the different parts of the path and defaults to \"/\".
  10584. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10585. (interactive "P")
  10586. (let* (case-fold-search
  10587. (bfn (buffer-file-name (buffer-base-buffer)))
  10588. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10589. res)
  10590. (if current (setq path (append path
  10591. (save-excursion
  10592. (org-back-to-heading t)
  10593. (if (looking-at org-complex-heading-regexp)
  10594. (list (match-string 4)))))))
  10595. (setq res
  10596. (org-format-outline-path
  10597. path
  10598. (1- (frame-width))
  10599. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10600. separator))
  10601. (if just-return-string
  10602. (org-no-properties res)
  10603. (org-unlogged-message "%s" res))))
  10604. (defvar org-refile-history nil
  10605. "History for refiling operations.")
  10606. (defvar org-after-refile-insert-hook nil
  10607. "Hook run after `org-refile' has inserted its stuff at the new location.
  10608. Note that this is still *before* the stuff will be removed from
  10609. the *old* location.")
  10610. (defvar org-capture-last-stored-marker)
  10611. (defvar org-refile-keep nil
  10612. "Non-nil means `org-refile' will copy instead of refile.")
  10613. (defun org-copy ()
  10614. "Like `org-refile', but copy."
  10615. (interactive)
  10616. (let ((org-refile-keep t))
  10617. (funcall 'org-refile nil nil nil "Copy")))
  10618. (defun org-refile (&optional arg default-buffer rfloc msg)
  10619. "Move the entry or entries at point to another heading.
  10620. The list of target headings is compiled using the information in
  10621. `org-refile-targets', which see.
  10622. At the target location, the entry is filed as a subitem of the
  10623. target heading. Depending on `org-reverse-note-order', the new
  10624. subitem will either be the first or the last subitem.
  10625. If there is an active region, all entries in that region will be
  10626. refiled. However, the region must fulfill the requirement that
  10627. the first heading sets the top-level of the moved text.
  10628. With prefix arg ARG, the command will only visit the target
  10629. location and not actually move anything.
  10630. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10631. refiling operation has put the subtree.
  10632. With a numeric prefix argument of `2', refile to the running clock.
  10633. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10634. being set to `t' and copy to the target location, don't move it.
  10635. Beware that keeping refiled entries may result in duplicated ID
  10636. properties.
  10637. RFLOC can be a refile location obtained in a different way.
  10638. MSG is a string to replace \"Refile\" in the default prompt with
  10639. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10640. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10641. If you are using target caching (see `org-refile-use-cache'), you
  10642. have to clear the target cache in order to find new targets.
  10643. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10644. prefix argument (`C-u C-u C-u C-c C-w')."
  10645. (interactive "P")
  10646. (if (member arg '(0 (64)))
  10647. (org-refile-cache-clear)
  10648. (let* ((actionmsg (cond (msg msg)
  10649. ((equal arg 3) "Refile (and keep)")
  10650. (t "Refile")))
  10651. (cbuf (current-buffer))
  10652. (regionp (org-region-active-p))
  10653. (region-start (and regionp (region-beginning)))
  10654. (region-end (and regionp (region-end)))
  10655. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10656. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10657. pos it nbuf file re level reversed)
  10658. (setq last-command nil)
  10659. (when regionp
  10660. (goto-char region-start)
  10661. (or (bolp) (goto-char (point-at-bol)))
  10662. (setq region-start (point))
  10663. (unless (or (org-kill-is-subtree-p
  10664. (buffer-substring region-start region-end))
  10665. (prog1 org-refile-active-region-within-subtree
  10666. (let ((s (point-at-eol)))
  10667. (org-toggle-heading)
  10668. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10669. (user-error "The region is not a (sequence of) subtree(s)")))
  10670. (if (equal arg '(16))
  10671. (org-refile-goto-last-stored)
  10672. (when (or
  10673. (and (equal arg 2)
  10674. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10675. (prog1
  10676. (setq it (list (or org-clock-heading "running clock")
  10677. (buffer-file-name
  10678. (marker-buffer org-clock-hd-marker))
  10679. ""
  10680. (marker-position org-clock-hd-marker)))
  10681. (setq arg nil)))
  10682. (setq it (or rfloc
  10683. (let (heading-text)
  10684. (save-excursion
  10685. (unless (and arg (listp arg))
  10686. (org-back-to-heading t)
  10687. (setq heading-text
  10688. (replace-regexp-in-string
  10689. org-bracket-link-regexp
  10690. "\\3"
  10691. (nth 4 (org-heading-components)))))
  10692. (org-refile-get-location
  10693. (cond ((and arg (listp arg)) "Goto")
  10694. (regionp (concat actionmsg " region to"))
  10695. (t (concat actionmsg " subtree \""
  10696. heading-text "\" to")))
  10697. default-buffer
  10698. (and (not (equal '(4) arg))
  10699. org-refile-allow-creating-parent-nodes)
  10700. arg))))))
  10701. (setq file (nth 1 it)
  10702. re (nth 2 it)
  10703. pos (nth 3 it))
  10704. (if (and (not arg)
  10705. pos
  10706. (equal (buffer-file-name) file)
  10707. (if regionp
  10708. (and (>= pos region-start)
  10709. (<= pos region-end))
  10710. (and (>= pos (point))
  10711. (< pos (save-excursion
  10712. (org-end-of-subtree t t))))))
  10713. (error "Cannot refile to position inside the tree or region"))
  10714. (setq nbuf (or (find-buffer-visiting file)
  10715. (find-file-noselect file)))
  10716. (if (and arg (not (equal arg 3)))
  10717. (progn
  10718. (org-pop-to-buffer-same-window nbuf)
  10719. (goto-char pos)
  10720. (org-show-context 'org-goto))
  10721. (if regionp
  10722. (progn
  10723. (org-kill-new (buffer-substring region-start region-end))
  10724. (org-save-markers-in-region region-start region-end))
  10725. (org-copy-subtree 1 nil t))
  10726. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10727. (find-file-noselect file)))
  10728. (setq reversed (org-notes-order-reversed-p))
  10729. (save-excursion
  10730. (save-restriction
  10731. (widen)
  10732. (if pos
  10733. (progn
  10734. (goto-char pos)
  10735. (looking-at org-outline-regexp)
  10736. (setq level (org-get-valid-level (funcall outline-level) 1))
  10737. (goto-char
  10738. (if reversed
  10739. (or (outline-next-heading) (point-max))
  10740. (or (save-excursion (org-get-next-sibling))
  10741. (org-end-of-subtree t t)
  10742. (point-max)))))
  10743. (setq level 1)
  10744. (if (not reversed)
  10745. (goto-char (point-max))
  10746. (goto-char (point-min))
  10747. (or (outline-next-heading) (goto-char (point-max)))))
  10748. (if (not (bolp)) (newline))
  10749. (org-paste-subtree level nil nil t)
  10750. (when org-log-refile
  10751. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10752. (unless (eq org-log-refile 'note)
  10753. (save-excursion (org-add-log-note))))
  10754. (and org-auto-align-tags
  10755. (let ((org-loop-over-headlines-in-active-region nil))
  10756. (org-set-tags nil t)))
  10757. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10758. :last-refile)))
  10759. (when bookmark-name
  10760. (with-demoted-errors
  10761. (bookmark-set bookmark-name))))
  10762. ;; If we are refiling for capture, make sure that the
  10763. ;; last-capture pointers point here
  10764. (when (org-bound-and-true-p org-refile-for-capture)
  10765. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10766. :last-capture-marker)))
  10767. (when bookmark-name
  10768. (with-demoted-errors
  10769. (bookmark-set bookmark-name))))
  10770. (move-marker org-capture-last-stored-marker (point)))
  10771. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10772. (run-hooks 'org-after-refile-insert-hook))))
  10773. (unless org-refile-keep
  10774. (if regionp
  10775. (delete-region (point) (+ (point) (- region-end region-start)))
  10776. (delete-region
  10777. (and (org-back-to-heading t) (point))
  10778. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10779. (when (featurep 'org-inlinetask)
  10780. (org-inlinetask-remove-END-maybe))
  10781. (setq org-markers-to-move nil)
  10782. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10783. (defun org-refile-goto-last-stored ()
  10784. "Go to the location where the last refile was stored."
  10785. (interactive)
  10786. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10787. (message "This is the location of the last refile"))
  10788. (defun org-refile--get-location (refloc tbl)
  10789. "When user refile to REFLOC, find the associated target in TBL.
  10790. Also check `org-refile-target-table'."
  10791. (car (delq
  10792. nil
  10793. (mapcar
  10794. (lambda (r) (or (assoc r tbl)
  10795. (assoc r org-refile-target-table)))
  10796. (list (replace-regexp-in-string "/$" "" refloc)
  10797. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10798. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10799. no-exclude)
  10800. "Prompt the user for a refile location, using PROMPT.
  10801. PROMPT should not be suffixed with a colon and a space, because
  10802. this function appends the default value from
  10803. `org-refile-history' automatically, if that is not empty.
  10804. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10805. this is used for the GOTO interface."
  10806. (let ((org-refile-targets org-refile-targets)
  10807. (org-refile-use-outline-path org-refile-use-outline-path)
  10808. excluded-entries)
  10809. (when (and (derived-mode-p 'org-mode)
  10810. (not org-refile-use-cache)
  10811. (not no-exclude))
  10812. (org-map-tree
  10813. (lambda()
  10814. (setq excluded-entries
  10815. (append excluded-entries (list (org-get-heading t t)))))))
  10816. (setq org-refile-target-table
  10817. (org-refile-get-targets default-buffer excluded-entries)))
  10818. (unless org-refile-target-table
  10819. (user-error "No refile targets"))
  10820. (let* ((cbuf (current-buffer))
  10821. (partial-completion-mode nil)
  10822. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10823. (cfunc (if (and org-refile-use-outline-path
  10824. org-outline-path-complete-in-steps)
  10825. 'org-olpath-completing-read
  10826. 'org-icompleting-read))
  10827. (extra (if org-refile-use-outline-path "/" ""))
  10828. (cbnex (concat (buffer-name) extra))
  10829. (filename (and cfn (expand-file-name cfn)))
  10830. (tbl (mapcar
  10831. (lambda (x)
  10832. (if (and (not (member org-refile-use-outline-path
  10833. '(file full-file-path)))
  10834. (not (equal filename (nth 1 x))))
  10835. (cons (concat (car x) extra " ("
  10836. (file-name-nondirectory (nth 1 x)) ")")
  10837. (cdr x))
  10838. (cons (concat (car x) extra) (cdr x))))
  10839. org-refile-target-table))
  10840. (completion-ignore-case t)
  10841. cdef
  10842. (prompt (concat prompt
  10843. (or (and (car org-refile-history)
  10844. (concat " (default " (car org-refile-history) ")"))
  10845. (and (assoc cbnex tbl) (setq cdef cbnex)
  10846. (concat " (default " cbnex ")"))) ": "))
  10847. pa answ parent-target child parent old-hist)
  10848. (setq old-hist org-refile-history)
  10849. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10850. nil 'org-refile-history (or cdef (car org-refile-history))))
  10851. (if (setq pa (org-refile--get-location answ tbl))
  10852. (progn
  10853. (org-refile-check-position pa)
  10854. (when (or (not org-refile-history)
  10855. (not (eq old-hist org-refile-history))
  10856. (not (equal (car pa) (car org-refile-history))))
  10857. (setq org-refile-history
  10858. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10859. org-refile-history
  10860. (cdr org-refile-history))))
  10861. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10862. (pop org-refile-history)))
  10863. pa)
  10864. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10865. (progn
  10866. (setq parent (match-string 1 answ)
  10867. child (match-string 2 answ))
  10868. (setq parent-target (org-refile--get-location parent tbl))
  10869. (when (and parent-target
  10870. (or (eq new-nodes t)
  10871. (and (eq new-nodes 'confirm)
  10872. (y-or-n-p (format "Create new node \"%s\"? "
  10873. child)))))
  10874. (org-refile-new-child parent-target child)))
  10875. (user-error "Invalid target location")))))
  10876. (declare-function org-string-nw-p "org-macs" (s))
  10877. (defun org-refile-check-position (refile-pointer)
  10878. "Check if the refile pointer matches the headline to which it points."
  10879. (let* ((file (nth 1 refile-pointer))
  10880. (re (nth 2 refile-pointer))
  10881. (pos (nth 3 refile-pointer))
  10882. buffer)
  10883. (if (and (not (markerp pos)) (not file))
  10884. (user-error "Please indicate a target file in the refile path")
  10885. (when (org-string-nw-p re)
  10886. (setq buffer (if (markerp pos)
  10887. (marker-buffer pos)
  10888. (or (find-buffer-visiting file)
  10889. (find-file-noselect file))))
  10890. (with-current-buffer buffer
  10891. (save-excursion
  10892. (save-restriction
  10893. (widen)
  10894. (goto-char pos)
  10895. (beginning-of-line 1)
  10896. (unless (org-looking-at-p re)
  10897. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10898. (defun org-refile-new-child (parent-target child)
  10899. "Use refile target PARENT-TARGET to add new CHILD below it."
  10900. (unless parent-target
  10901. (error "Cannot find parent for new node"))
  10902. (let ((file (nth 1 parent-target))
  10903. (pos (nth 3 parent-target))
  10904. level)
  10905. (with-current-buffer (or (find-buffer-visiting file)
  10906. (find-file-noselect file))
  10907. (save-excursion
  10908. (save-restriction
  10909. (widen)
  10910. (if pos
  10911. (goto-char pos)
  10912. (goto-char (point-max))
  10913. (if (not (bolp)) (newline)))
  10914. (when (looking-at org-outline-regexp)
  10915. (setq level (funcall outline-level))
  10916. (org-end-of-subtree t t))
  10917. (org-back-over-empty-lines)
  10918. (insert "\n" (make-string
  10919. (if pos (org-get-valid-level level 1) 1) ?*)
  10920. " " child "\n")
  10921. (beginning-of-line 0)
  10922. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10923. (defun org-olpath-completing-read (prompt collection &rest args)
  10924. "Read an outline path like a file name."
  10925. (let ((thetable collection)
  10926. (org-completion-use-ido nil) ; does not work with ido.
  10927. (org-completion-use-iswitchb nil)) ; or iswitchb
  10928. (apply
  10929. 'org-icompleting-read prompt
  10930. (lambda (string predicate &optional flag)
  10931. (let (rtn r f (l (length string)))
  10932. (cond
  10933. ((eq flag nil)
  10934. ;; try completion
  10935. (try-completion string thetable))
  10936. ((eq flag t)
  10937. ;; all-completions
  10938. (setq rtn (all-completions string thetable predicate))
  10939. (mapcar
  10940. (lambda (x)
  10941. (setq r (substring x l))
  10942. (if (string-match " ([^)]*)$" x)
  10943. (setq f (match-string 0 x))
  10944. (setq f ""))
  10945. (if (string-match "/" r)
  10946. (concat string (substring r 0 (match-end 0)) f)
  10947. x))
  10948. rtn))
  10949. ((eq flag 'lambda)
  10950. ;; exact match?
  10951. (assoc string thetable)))))
  10952. args)))
  10953. ;;;; Dynamic blocks
  10954. (defun org-find-dblock (name)
  10955. "Find the first dynamic block with name NAME in the buffer.
  10956. If not found, stay at current position and return nil."
  10957. (let ((case-fold-search t) pos)
  10958. (save-excursion
  10959. (goto-char (point-min))
  10960. (setq pos (and (re-search-forward
  10961. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10962. (match-beginning 0))))
  10963. (if pos (goto-char pos))
  10964. pos))
  10965. (defun org-create-dblock (plist)
  10966. "Create a dynamic block section, with parameters taken from PLIST.
  10967. PLIST must contain a :name entry which is used as the name of the block."
  10968. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10969. (end-of-line 1)
  10970. (newline))
  10971. (let ((col (current-column))
  10972. (name (plist-get plist :name)))
  10973. (insert "#+BEGIN: " name)
  10974. (while plist
  10975. (if (eq (car plist) :name)
  10976. (setq plist (cddr plist))
  10977. (insert " " (prin1-to-string (pop plist)))))
  10978. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10979. (beginning-of-line -2)))
  10980. (defun org-prepare-dblock ()
  10981. "Prepare dynamic block for refresh.
  10982. This empties the block, puts the cursor at the insert position and returns
  10983. the property list including an extra property :name with the block name."
  10984. (unless (looking-at org-dblock-start-re)
  10985. (user-error "Not at a dynamic block"))
  10986. (let* ((begdel (1+ (match-end 0)))
  10987. (name (org-no-properties (match-string 1)))
  10988. (params (append (list :name name)
  10989. (read (concat "(" (match-string 3) ")")))))
  10990. (save-excursion
  10991. (beginning-of-line 1)
  10992. (skip-chars-forward " \t")
  10993. (setq params (plist-put params :indentation-column (current-column))))
  10994. (unless (re-search-forward org-dblock-end-re nil t)
  10995. (error "Dynamic block not terminated"))
  10996. (setq params
  10997. (append params
  10998. (list :content (buffer-substring
  10999. begdel (match-beginning 0)))))
  11000. (delete-region begdel (match-beginning 0))
  11001. (goto-char begdel)
  11002. (open-line 1)
  11003. params))
  11004. (defun org-map-dblocks (&optional command)
  11005. "Apply COMMAND to all dynamic blocks in the current buffer.
  11006. If COMMAND is not given, use `org-update-dblock'."
  11007. (let ((cmd (or command 'org-update-dblock)))
  11008. (save-excursion
  11009. (goto-char (point-min))
  11010. (while (re-search-forward org-dblock-start-re nil t)
  11011. (goto-char (match-beginning 0))
  11012. (save-excursion
  11013. (condition-case nil
  11014. (funcall cmd)
  11015. (error (message "Error during update of dynamic block"))))
  11016. (unless (re-search-forward org-dblock-end-re nil t)
  11017. (error "Dynamic block not terminated"))))))
  11018. (defun org-dblock-update (&optional arg)
  11019. "User command for updating dynamic blocks.
  11020. Update the dynamic block at point. With prefix ARG, update all dynamic
  11021. blocks in the buffer."
  11022. (interactive "P")
  11023. (if arg
  11024. (org-update-all-dblocks)
  11025. (or (looking-at org-dblock-start-re)
  11026. (org-beginning-of-dblock))
  11027. (org-update-dblock)))
  11028. (defun org-update-dblock ()
  11029. "Update the dynamic block at point.
  11030. This means to empty the block, parse for parameters and then call
  11031. the correct writing function."
  11032. (interactive)
  11033. (save-excursion
  11034. (let* ((win (selected-window))
  11035. (pos (point))
  11036. (line (org-current-line))
  11037. (params (org-prepare-dblock))
  11038. (name (plist-get params :name))
  11039. (indent (plist-get params :indentation-column))
  11040. (cmd (intern (concat "org-dblock-write:" name))))
  11041. (message "Updating dynamic block `%s' at line %d..." name line)
  11042. (funcall cmd params)
  11043. (message "Updating dynamic block `%s' at line %d...done" name line)
  11044. (goto-char pos)
  11045. (when (and indent (> indent 0))
  11046. (setq indent (make-string indent ?\ ))
  11047. (save-excursion
  11048. (select-window win)
  11049. (org-beginning-of-dblock)
  11050. (forward-line 1)
  11051. (while (not (looking-at org-dblock-end-re))
  11052. (insert indent)
  11053. (beginning-of-line 2))
  11054. (when (looking-at org-dblock-end-re)
  11055. (and (looking-at "[ \t]+")
  11056. (replace-match ""))
  11057. (insert indent)))))))
  11058. (defun org-beginning-of-dblock ()
  11059. "Find the beginning of the dynamic block at point.
  11060. Error if there is no such block at point."
  11061. (let ((pos (point))
  11062. beg)
  11063. (end-of-line 1)
  11064. (if (and (re-search-backward org-dblock-start-re nil t)
  11065. (setq beg (match-beginning 0))
  11066. (re-search-forward org-dblock-end-re nil t)
  11067. (> (match-end 0) pos))
  11068. (goto-char beg)
  11069. (goto-char pos)
  11070. (error "Not in a dynamic block"))))
  11071. (defun org-update-all-dblocks ()
  11072. "Update all dynamic blocks in the buffer.
  11073. This function can be used in a hook."
  11074. (interactive)
  11075. (when (derived-mode-p 'org-mode)
  11076. (org-map-dblocks 'org-update-dblock)))
  11077. ;;;; Completion
  11078. (declare-function org-export-backend-name "org-export" (cl-x))
  11079. (declare-function org-export-backend-options "org-export" (cl-x))
  11080. (defun org-get-export-keywords ()
  11081. "Return a list of all currently understood export keywords.
  11082. Export keywords include options, block names, attributes and
  11083. keywords relative to each registered export back-end."
  11084. (let (keywords)
  11085. (dolist (backend
  11086. (org-bound-and-true-p org-export--registered-backends)
  11087. (delq nil keywords))
  11088. ;; Back-end name (for keywords, like #+LATEX:)
  11089. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11090. (dolist (option-entry (org-export-backend-options backend))
  11091. ;; Back-end options.
  11092. (push (nth 1 option-entry) keywords)))))
  11093. (defconst org-options-keywords
  11094. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11095. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11096. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11097. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11098. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11099. (defcustom org-structure-template-alist
  11100. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11101. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11102. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11103. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11104. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11105. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11106. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11107. ("L" "#+LaTeX: ")
  11108. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11109. ("H" "#+HTML: ")
  11110. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11111. ("A" "#+ASCII: ")
  11112. ("i" "#+INDEX: ?")
  11113. ("I" "#+INCLUDE: %file ?"))
  11114. "Structure completion elements.
  11115. This is a list of abbreviation keys and values. The value gets inserted
  11116. if you type `<' followed by the key and then press the completion key,
  11117. usually `TAB'. %file will be replaced by a file name after prompting
  11118. for the file using completion. The cursor will be placed at the position
  11119. of the `?` in the template.
  11120. There are two templates for each key, the first uses the original Org syntax,
  11121. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11122. the default when the /org-mtags.el/ module has been loaded. See also the
  11123. variable `org-mtags-prefer-muse-templates'."
  11124. :group 'org-completion
  11125. :type '(repeat
  11126. (list
  11127. (string :tag "Key")
  11128. (string :tag "Template")))
  11129. :version "25.1"
  11130. :package-version '(Org . "8.3"))
  11131. (defun org-try-structure-completion ()
  11132. "Try to complete a structure template before point.
  11133. This looks for strings like \"<e\" on an otherwise empty line and
  11134. expands them."
  11135. (let ((l (buffer-substring (point-at-bol) (point)))
  11136. a)
  11137. (when (and (looking-at "[ \t]*$")
  11138. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11139. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11140. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11141. (match-beginning 1)) a)
  11142. t)))
  11143. (defun org-complete-expand-structure-template (start cell)
  11144. "Expand a structure template."
  11145. (let ((rpl (nth 1 cell))
  11146. (ind ""))
  11147. (delete-region start (point))
  11148. (when (string-match "\\`[ \t]*#\\+" rpl)
  11149. (cond
  11150. ((bolp))
  11151. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11152. (setq ind (buffer-substring (point-at-bol) (point))))
  11153. (t (newline))))
  11154. (setq start (point))
  11155. (if (string-match "%file" rpl)
  11156. (setq rpl (replace-match
  11157. (concat
  11158. "\""
  11159. (save-match-data
  11160. (abbreviate-file-name (read-file-name "Include file: ")))
  11161. "\"")
  11162. t t rpl)))
  11163. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11164. (concat "\n" ind)))
  11165. (insert rpl)
  11166. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11167. ;;;; TODO, DEADLINE, Comments
  11168. (defun org-toggle-comment ()
  11169. "Change the COMMENT state of an entry."
  11170. (interactive)
  11171. (save-excursion
  11172. (org-back-to-heading)
  11173. (looking-at org-complex-heading-regexp)
  11174. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11175. (skip-chars-forward " \t")
  11176. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11177. (if (org-in-commented-heading-p t)
  11178. (delete-region (point)
  11179. (progn (search-forward " " (line-end-position) 'move)
  11180. (skip-chars-forward " \t")
  11181. (point)))
  11182. (insert org-comment-string)
  11183. (unless (eolp) (insert " ")))))
  11184. (defvar org-last-todo-state-is-todo nil
  11185. "This is non-nil when the last TODO state change led to a TODO state.
  11186. If the last change removed the TODO tag or switched to DONE, then
  11187. this is nil.")
  11188. (defvar org-setting-tags nil) ; dynamically skipped
  11189. (defvar org-todo-setup-filter-hook nil
  11190. "Hook for functions that pre-filter todo specs.
  11191. Each function takes a todo spec and returns either nil or the spec
  11192. transformed into canonical form." )
  11193. (defvar org-todo-get-default-hook nil
  11194. "Hook for functions that get a default item for todo.
  11195. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11196. nil or a string to be used for the todo mark." )
  11197. (defvar org-agenda-headline-snapshot-before-repeat)
  11198. (defun org-current-effective-time ()
  11199. "Return current time adjusted for `org-extend-today-until' variable."
  11200. (let* ((ct (org-current-time))
  11201. (dct (decode-time ct))
  11202. (ct1
  11203. (cond
  11204. (org-use-last-clock-out-time-as-effective-time
  11205. (or (org-clock-get-last-clock-out-time) ct))
  11206. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11207. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11208. (t ct))))
  11209. ct1))
  11210. (defun org-todo-yesterday (&optional arg)
  11211. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11212. (interactive "P")
  11213. (if (eq major-mode 'org-agenda-mode)
  11214. (apply 'org-agenda-todo-yesterday arg)
  11215. (let* ((org-use-effective-time t)
  11216. (hour (third (decode-time
  11217. (org-current-time))))
  11218. (org-extend-today-until (1+ hour)))
  11219. (org-todo arg))))
  11220. (defvar org-block-entry-blocking ""
  11221. "First entry preventing the TODO state change.")
  11222. (defun org-cancel-repeater ()
  11223. "Cancel a repeater by setting its numeric value to zero."
  11224. (interactive)
  11225. (save-excursion
  11226. (org-back-to-heading t)
  11227. (let ((bound1 (point))
  11228. (bound0 (save-excursion (outline-next-heading) (point))))
  11229. (when (re-search-forward
  11230. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11231. org-deadline-time-regexp "\\)\\|\\("
  11232. org-ts-regexp "\\)")
  11233. bound0 t)
  11234. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11235. (replace-match "0" t nil nil 1))))))
  11236. (defun org-todo (&optional arg)
  11237. "Change the TODO state of an item.
  11238. The state of an item is given by a keyword at the start of the heading,
  11239. like
  11240. *** TODO Write paper
  11241. *** DONE Call mom
  11242. The different keywords are specified in the variable `org-todo-keywords'.
  11243. By default the available states are \"TODO\" and \"DONE\".
  11244. So for this example: when the item starts with TODO, it is changed to DONE.
  11245. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11246. DONE are present, add TODO at the beginning of the heading.
  11247. With \\[universal-argument] prefix arg, use completion to determine the new \
  11248. state.
  11249. With numeric prefix arg, switch to that state.
  11250. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11251. keywords (nextset).
  11252. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11253. With a numeric prefix arg of 0, inhibit note taking for the change.
  11254. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11255. When called through ELisp, arg is also interpreted in the following way:
  11256. 'none -> empty state
  11257. \"\"(empty string) -> switch to empty state
  11258. 'done -> switch to DONE
  11259. 'nextset -> switch to the next set of keywords
  11260. 'previousset -> switch to the previous set of keywords
  11261. \"WAITING\" -> switch to the specified keyword, but only if it
  11262. really is a member of `org-todo-keywords'."
  11263. (interactive "P")
  11264. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11265. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11266. 'region-start-level 'region))
  11267. org-loop-over-headlines-in-active-region)
  11268. (org-map-entries
  11269. `(org-todo ,arg)
  11270. org-loop-over-headlines-in-active-region
  11271. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11272. (if (equal arg '(16)) (setq arg 'nextset))
  11273. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11274. (let ((org-blocker-hook org-blocker-hook)
  11275. commentp
  11276. case-fold-search)
  11277. (when (equal arg '(64))
  11278. (setq arg nil org-blocker-hook nil))
  11279. (when (and org-blocker-hook
  11280. (or org-inhibit-blocking
  11281. (org-entry-get nil "NOBLOCKING")))
  11282. (setq org-blocker-hook nil))
  11283. (save-excursion
  11284. (catch 'exit
  11285. (org-back-to-heading t)
  11286. (when (org-in-commented-heading-p t)
  11287. (org-toggle-comment)
  11288. (setq commentp t))
  11289. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11290. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11291. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11292. (let* ((match-data (match-data))
  11293. (startpos (point-at-bol))
  11294. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11295. (org-log-done org-log-done)
  11296. (org-log-repeat org-log-repeat)
  11297. (org-todo-log-states org-todo-log-states)
  11298. (org-inhibit-logging
  11299. (if (equal arg 0)
  11300. (progn (setq arg nil) 'note) org-inhibit-logging))
  11301. (this (match-string 1))
  11302. (hl-pos (match-beginning 0))
  11303. (head (org-get-todo-sequence-head this))
  11304. (ass (assoc head org-todo-kwd-alist))
  11305. (interpret (nth 1 ass))
  11306. (done-word (nth 3 ass))
  11307. (final-done-word (nth 4 ass))
  11308. (org-last-state (or this ""))
  11309. (completion-ignore-case t)
  11310. (member (member this org-todo-keywords-1))
  11311. (tail (cdr member))
  11312. (org-state (cond
  11313. ((and org-todo-key-trigger
  11314. (or (and (equal arg '(4))
  11315. (eq org-use-fast-todo-selection 'prefix))
  11316. (and (not arg) org-use-fast-todo-selection
  11317. (not (eq org-use-fast-todo-selection
  11318. 'prefix)))))
  11319. ;; Use fast selection
  11320. (org-fast-todo-selection))
  11321. ((and (equal arg '(4))
  11322. (or (not org-use-fast-todo-selection)
  11323. (not org-todo-key-trigger)))
  11324. ;; Read a state with completion
  11325. (org-icompleting-read
  11326. "State: " (mapcar 'list org-todo-keywords-1)
  11327. nil t))
  11328. ((eq arg 'right)
  11329. (if this
  11330. (if tail (car tail) nil)
  11331. (car org-todo-keywords-1)))
  11332. ((eq arg 'left)
  11333. (if (equal member org-todo-keywords-1)
  11334. nil
  11335. (if this
  11336. (nth (- (length org-todo-keywords-1)
  11337. (length tail) 2)
  11338. org-todo-keywords-1)
  11339. (org-last org-todo-keywords-1))))
  11340. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11341. (setq arg nil))) ; hack to fall back to cycling
  11342. (arg
  11343. ;; user or caller requests a specific state
  11344. (cond
  11345. ((equal arg "") nil)
  11346. ((eq arg 'none) nil)
  11347. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11348. ((eq arg 'nextset)
  11349. (or (car (cdr (member head org-todo-heads)))
  11350. (car org-todo-heads)))
  11351. ((eq arg 'previousset)
  11352. (let ((org-todo-heads (reverse org-todo-heads)))
  11353. (or (car (cdr (member head org-todo-heads)))
  11354. (car org-todo-heads))))
  11355. ((car (member arg org-todo-keywords-1)))
  11356. ((stringp arg)
  11357. (user-error "State `%s' not valid in this file" arg))
  11358. ((nth (1- (prefix-numeric-value arg))
  11359. org-todo-keywords-1))))
  11360. ((null member) (or head (car org-todo-keywords-1)))
  11361. ((equal this final-done-word) nil) ;; -> make empty
  11362. ((null tail) nil) ;; -> first entry
  11363. ((memq interpret '(type priority))
  11364. (if (eq this-command last-command)
  11365. (car tail)
  11366. (if (> (length tail) 0)
  11367. (or done-word (car org-done-keywords))
  11368. nil)))
  11369. (t
  11370. (car tail))))
  11371. (org-state (or
  11372. (run-hook-with-args-until-success
  11373. 'org-todo-get-default-hook org-state org-last-state)
  11374. org-state))
  11375. (next (if org-state (concat " " org-state " ") " "))
  11376. (change-plist (list :type 'todo-state-change :from this :to org-state
  11377. :position startpos))
  11378. dolog now-done-p)
  11379. (when org-blocker-hook
  11380. (setq org-last-todo-state-is-todo
  11381. (not (member this org-done-keywords)))
  11382. (unless (save-excursion
  11383. (save-match-data
  11384. (org-with-wide-buffer
  11385. (run-hook-with-args-until-failure
  11386. 'org-blocker-hook change-plist))))
  11387. (if (org-called-interactively-p 'interactive)
  11388. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11389. this org-state org-block-entry-blocking)
  11390. ;; fail silently
  11391. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11392. this org-state org-block-entry-blocking)
  11393. (throw 'exit nil))))
  11394. (store-match-data match-data)
  11395. (replace-match next t t)
  11396. (cond ((equal this org-state)
  11397. (message "TODO state was already %s" (org-trim next)))
  11398. ((pos-visible-in-window-p hl-pos)
  11399. (message "TODO state changed to %s" (org-trim next))))
  11400. (unless head
  11401. (setq head (org-get-todo-sequence-head org-state)
  11402. ass (assoc head org-todo-kwd-alist)
  11403. interpret (nth 1 ass)
  11404. done-word (nth 3 ass)
  11405. final-done-word (nth 4 ass)))
  11406. (when (memq arg '(nextset previousset))
  11407. (message "Keyword-Set %d/%d: %s"
  11408. (- (length org-todo-sets) -1
  11409. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11410. (length org-todo-sets)
  11411. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11412. (setq org-last-todo-state-is-todo
  11413. (not (member org-state org-done-keywords)))
  11414. (setq now-done-p (and (member org-state org-done-keywords)
  11415. (not (member this org-done-keywords))))
  11416. (and logging (org-local-logging logging))
  11417. (when (and (or org-todo-log-states org-log-done)
  11418. (not (eq org-inhibit-logging t))
  11419. (not (memq arg '(nextset previousset))))
  11420. ;; we need to look at recording a time and note
  11421. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11422. (nth 2 (assoc this org-todo-log-states))))
  11423. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11424. (setq dolog 'time))
  11425. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11426. (and org-state
  11427. (member org-state org-not-done-keywords)
  11428. (not (member this org-not-done-keywords))))
  11429. ;; This is now a todo state and was not one before
  11430. ;; If there was a CLOSED time stamp, get rid of it.
  11431. (org-add-planning-info nil nil 'closed))
  11432. (when (and now-done-p org-log-done)
  11433. ;; It is now done, and it was not done before
  11434. (org-add-planning-info 'closed (org-current-effective-time))
  11435. (if (and (not dolog) (eq 'note org-log-done))
  11436. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11437. (when (and org-state dolog)
  11438. ;; This is a non-nil state, and we need to log it
  11439. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11440. ;; Fixup tag positioning
  11441. (org-todo-trigger-tag-changes org-state)
  11442. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11443. (when org-provide-todo-statistics
  11444. (org-update-parent-todo-statistics))
  11445. (run-hooks 'org-after-todo-state-change-hook)
  11446. (if (and arg (not (member org-state org-done-keywords)))
  11447. (setq head (org-get-todo-sequence-head org-state)))
  11448. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11449. ;; Do we need to trigger a repeat?
  11450. (when now-done-p
  11451. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11452. ;; This is for the agenda, take a snapshot of the headline.
  11453. (save-match-data
  11454. (setq org-agenda-headline-snapshot-before-repeat
  11455. (org-get-heading))))
  11456. (org-auto-repeat-maybe org-state))
  11457. ;; Fixup cursor location if close to the keyword
  11458. (if (and (outline-on-heading-p)
  11459. (not (bolp))
  11460. (save-excursion (beginning-of-line 1)
  11461. (looking-at org-todo-line-regexp))
  11462. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11463. (progn
  11464. (goto-char (or (match-end 2) (match-end 1)))
  11465. (and (looking-at " ") (just-one-space))))
  11466. (when org-trigger-hook
  11467. (save-excursion
  11468. (run-hook-with-args 'org-trigger-hook change-plist)))
  11469. (when commentp (org-toggle-comment))))))))
  11470. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11471. "Block turning an entry into a TODO, using the hierarchy.
  11472. This checks whether the current task should be blocked from state
  11473. changes. Such blocking occurs when:
  11474. 1. The task has children which are not all in a completed state.
  11475. 2. A task has a parent with the property :ORDERED:, and there
  11476. are siblings prior to the current task with incomplete
  11477. status.
  11478. 3. The parent of the task is blocked because it has siblings that should
  11479. be done first, or is child of a block grandparent TODO entry."
  11480. (if (not org-enforce-todo-dependencies)
  11481. t ; if locally turned off don't block
  11482. (catch 'dont-block
  11483. ;; If this is not a todo state change, or if this entry is already DONE,
  11484. ;; do not block
  11485. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11486. (member (plist-get change-plist :from)
  11487. (cons 'done org-done-keywords))
  11488. (member (plist-get change-plist :to)
  11489. (cons 'todo org-not-done-keywords))
  11490. (not (plist-get change-plist :to)))
  11491. (throw 'dont-block t))
  11492. ;; If this task has children, and any are undone, it's blocked
  11493. (save-excursion
  11494. (org-back-to-heading t)
  11495. (let ((this-level (funcall outline-level)))
  11496. (outline-next-heading)
  11497. (let ((child-level (funcall outline-level)))
  11498. (while (and (not (eobp))
  11499. (> child-level this-level))
  11500. ;; this todo has children, check whether they are all
  11501. ;; completed
  11502. (if (and (not (org-entry-is-done-p))
  11503. (org-entry-is-todo-p))
  11504. (progn (setq org-block-entry-blocking (org-get-heading))
  11505. (throw 'dont-block nil)))
  11506. (outline-next-heading)
  11507. (setq child-level (funcall outline-level))))))
  11508. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11509. ;; any previous siblings are undone, it's blocked
  11510. (save-excursion
  11511. (org-back-to-heading t)
  11512. (let* ((pos (point))
  11513. (parent-pos (and (org-up-heading-safe) (point))))
  11514. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11515. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11516. (forward-line 1)
  11517. (re-search-forward org-not-done-heading-regexp pos t))
  11518. (setq org-block-entry-blocking (match-string 0))
  11519. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11520. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11521. (while t
  11522. (goto-char parent-pos)
  11523. (if (not (looking-at org-not-done-heading-regexp))
  11524. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11525. (setq pos (point))
  11526. (setq parent-pos (and (org-up-heading-safe) (point)))
  11527. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11528. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11529. (forward-line 1)
  11530. (re-search-forward org-not-done-heading-regexp pos t)
  11531. (setq org-block-entry-blocking (org-get-heading)))
  11532. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11533. (defcustom org-track-ordered-property-with-tag nil
  11534. "Should the ORDERED property also be shown as a tag?
  11535. The ORDERED property decides if an entry should require subtasks to be
  11536. completed in sequence. Since a property is not very visible, setting
  11537. this option means that toggling the ORDERED property with the command
  11538. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11539. not relevant for the behavior, but it makes things more visible.
  11540. Note that toggling the tag with tags commands will not change the property
  11541. and therefore not influence behavior!
  11542. This can be t, meaning the tag ORDERED should be used, It can also be a
  11543. string to select a different tag for this task."
  11544. :group 'org-todo
  11545. :type '(choice
  11546. (const :tag "No tracking" nil)
  11547. (const :tag "Track with ORDERED tag" t)
  11548. (string :tag "Use other tag")))
  11549. (defun org-toggle-ordered-property ()
  11550. "Toggle the ORDERED property of the current entry.
  11551. For better visibility, you can track the value of this property with a tag.
  11552. See variable `org-track-ordered-property-with-tag'."
  11553. (interactive)
  11554. (let* ((t1 org-track-ordered-property-with-tag)
  11555. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11556. (save-excursion
  11557. (org-back-to-heading)
  11558. (if (org-entry-get nil "ORDERED")
  11559. (progn
  11560. (org-delete-property "ORDERED")
  11561. (and tag (org-toggle-tag tag 'off))
  11562. (message "Subtasks can be completed in arbitrary order"))
  11563. (org-entry-put nil "ORDERED" "t")
  11564. (and tag (org-toggle-tag tag 'on))
  11565. (message "Subtasks must be completed in sequence")))))
  11566. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11567. (defun org-block-todo-from-checkboxes (change-plist)
  11568. "Block turning an entry into a TODO, using checkboxes.
  11569. This checks whether the current task should be blocked from state
  11570. changes because there are unchecked boxes in this entry."
  11571. (if (not org-enforce-todo-checkbox-dependencies)
  11572. t ; if locally turned off don't block
  11573. (catch 'dont-block
  11574. ;; If this is not a todo state change, or if this entry is already DONE,
  11575. ;; do not block
  11576. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11577. (member (plist-get change-plist :from)
  11578. (cons 'done org-done-keywords))
  11579. (member (plist-get change-plist :to)
  11580. (cons 'todo org-not-done-keywords))
  11581. (not (plist-get change-plist :to)))
  11582. (throw 'dont-block t))
  11583. ;; If this task has checkboxes that are not checked, it's blocked
  11584. (save-excursion
  11585. (org-back-to-heading t)
  11586. (let ((beg (point)) end)
  11587. (outline-next-heading)
  11588. (setq end (point))
  11589. (goto-char beg)
  11590. (if (org-list-search-forward
  11591. (concat (org-item-beginning-re)
  11592. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11593. "\\[[- ]\\]")
  11594. end t)
  11595. (progn
  11596. (if (boundp 'org-blocked-by-checkboxes)
  11597. (setq org-blocked-by-checkboxes t))
  11598. (throw 'dont-block nil)))))
  11599. t))) ; do not block
  11600. (defun org-entry-blocked-p ()
  11601. "Is the current entry blocked?"
  11602. (org-with-silent-modifications
  11603. (if (org-entry-get nil "NOBLOCKING")
  11604. nil ;; Never block this entry
  11605. (not (run-hook-with-args-until-failure
  11606. 'org-blocker-hook
  11607. (list :type 'todo-state-change
  11608. :position (point)
  11609. :from 'todo
  11610. :to 'done))))))
  11611. (defun org-update-statistics-cookies (all)
  11612. "Update the statistics cookie, either from TODO or from checkboxes.
  11613. This should be called with the cursor in a line with a statistics cookie."
  11614. (interactive "P")
  11615. (if all
  11616. (progn
  11617. (org-update-checkbox-count 'all)
  11618. (org-map-entries 'org-update-parent-todo-statistics))
  11619. (if (not (org-at-heading-p))
  11620. (org-update-checkbox-count)
  11621. (let ((pos (point-marker))
  11622. end l1 l2)
  11623. (ignore-errors (org-back-to-heading t))
  11624. (if (not (org-at-heading-p))
  11625. (org-update-checkbox-count)
  11626. (setq l1 (org-outline-level))
  11627. (setq end (save-excursion
  11628. (outline-next-heading)
  11629. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11630. (point)))
  11631. (if (and (save-excursion
  11632. (re-search-forward
  11633. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11634. (not (save-excursion (re-search-forward
  11635. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11636. (org-update-checkbox-count)
  11637. (if (and l2 (> l2 l1))
  11638. (progn
  11639. (goto-char end)
  11640. (org-update-parent-todo-statistics))
  11641. (goto-char pos)
  11642. (beginning-of-line 1)
  11643. (while (re-search-forward
  11644. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11645. (point-at-eol) t)
  11646. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11647. (goto-char pos)
  11648. (move-marker pos nil)))))
  11649. (defvar org-entry-property-inherited-from) ;; defined below
  11650. (defun org-update-parent-todo-statistics ()
  11651. "Update any statistics cookie in the parent of the current headline.
  11652. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11653. the entire subtree and this will travel up the hierarchy and update
  11654. statistics everywhere."
  11655. (let* ((prop (save-excursion (org-up-heading-safe)
  11656. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11657. (recursive (or (not org-hierarchical-todo-statistics)
  11658. (and prop (string-match "\\<recursive\\>" prop))))
  11659. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11660. 0))
  11661. (first t)
  11662. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11663. level ltoggle l1 new ndel
  11664. (cnt-all 0) (cnt-done 0) is-percent kwd
  11665. checkbox-beg ov ovs ove cookie-present)
  11666. (catch 'exit
  11667. (save-excursion
  11668. (beginning-of-line 1)
  11669. (setq ltoggle (funcall outline-level))
  11670. ;; Three situations are to consider:
  11671. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11672. ;; to the top-level ancestor on the headline;
  11673. ;; 2. If parent has "recursive" property, repeat up to the
  11674. ;; headline setting that property, taking inheritance into
  11675. ;; account;
  11676. ;; 3. Else, move up to direct parent and proceed only once.
  11677. (while (and (setq level (org-up-heading-safe))
  11678. (or recursive first)
  11679. (>= (point) lim))
  11680. (setq first nil cookie-present nil)
  11681. (unless (and level
  11682. (not (string-match
  11683. "\\<checkbox\\>"
  11684. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11685. "")))))
  11686. (throw 'exit nil))
  11687. (while (re-search-forward box-re (point-at-eol) t)
  11688. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11689. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11690. (save-match-data
  11691. (unless (outline-next-heading) (throw 'exit nil))
  11692. (while (and (looking-at org-complex-heading-regexp)
  11693. (> (setq l1 (length (match-string 1))) level))
  11694. (setq kwd (and (or recursive (= l1 ltoggle))
  11695. (match-string 2)))
  11696. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11697. (and (eq org-provide-todo-statistics t)
  11698. (or (member kwd org-done-keywords)))
  11699. (and (listp org-provide-todo-statistics)
  11700. (stringp (car org-provide-todo-statistics))
  11701. (or (member kwd org-provide-todo-statistics)
  11702. (member kwd org-done-keywords)))
  11703. (and (listp org-provide-todo-statistics)
  11704. (listp (car org-provide-todo-statistics))
  11705. (or (member kwd (car org-provide-todo-statistics))
  11706. (and (member kwd org-done-keywords)
  11707. (member kwd (cadr org-provide-todo-statistics))))))
  11708. (setq cnt-all (1+ cnt-all))
  11709. (if (eq org-provide-todo-statistics t)
  11710. (and kwd (setq cnt-all (1+ cnt-all)))))
  11711. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11712. (member kwd org-done-keywords))
  11713. (and (listp org-provide-todo-statistics)
  11714. (listp (car org-provide-todo-statistics))
  11715. (member kwd org-done-keywords)
  11716. (member kwd (cadr org-provide-todo-statistics)))
  11717. (and (listp org-provide-todo-statistics)
  11718. (stringp (car org-provide-todo-statistics))
  11719. (member kwd org-done-keywords)))
  11720. (setq cnt-done (1+ cnt-done)))
  11721. (outline-next-heading)))
  11722. (setq new
  11723. (if is-percent
  11724. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11725. (format "[%d/%d]" cnt-done cnt-all))
  11726. ndel (- (match-end 0) checkbox-beg))
  11727. ;; handle overlays when updating cookie from column view
  11728. (when (setq ov (car (overlays-at checkbox-beg)))
  11729. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11730. (delete-overlay ov))
  11731. (goto-char checkbox-beg)
  11732. (insert new)
  11733. (delete-region (point) (+ (point) ndel))
  11734. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11735. (when ov (move-overlay ov ovs ove)))
  11736. (when cookie-present
  11737. (run-hook-with-args 'org-after-todo-statistics-hook
  11738. cnt-done (- cnt-all cnt-done))))))
  11739. (run-hooks 'org-todo-statistics-hook)))
  11740. (defvar org-after-todo-statistics-hook nil
  11741. "Hook that is called after a TODO statistics cookie has been updated.
  11742. Each function is called with two arguments: the number of not-done entries
  11743. and the number of done entries.
  11744. For example, the following function, when added to this hook, will switch
  11745. an entry to DONE when all children are done, and back to TODO when new
  11746. entries are set to a TODO status. Note that this hook is only called
  11747. when there is a statistics cookie in the headline!
  11748. (defun org-summary-todo (n-done n-not-done)
  11749. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11750. (let (org-log-done org-log-states) ; turn off logging
  11751. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11752. ")
  11753. (defvar org-todo-statistics-hook nil
  11754. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11755. This hook runs even if there is no statistics cookie present, in which case
  11756. `org-after-todo-statistics-hook' would not run.")
  11757. (defun org-todo-trigger-tag-changes (state)
  11758. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11759. (let ((l org-todo-state-tags-triggers)
  11760. changes)
  11761. (when (or (not state) (equal state ""))
  11762. (setq changes (append changes (cdr (assoc "" l)))))
  11763. (when (and (stringp state) (> (length state) 0))
  11764. (setq changes (append changes (cdr (assoc state l)))))
  11765. (when (member state org-not-done-keywords)
  11766. (setq changes (append changes (cdr (assoc 'todo l)))))
  11767. (when (member state org-done-keywords)
  11768. (setq changes (append changes (cdr (assoc 'done l)))))
  11769. (dolist (c changes)
  11770. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11771. (defun org-local-logging (value)
  11772. "Get logging settings from a property VALUE."
  11773. (let* (words w a)
  11774. ;; directly set the variables, they are already local.
  11775. (setq org-log-done nil
  11776. org-log-repeat nil
  11777. org-todo-log-states nil)
  11778. (setq words (org-split-string value))
  11779. (while (setq w (pop words))
  11780. (cond
  11781. ((setq a (assoc w org-startup-options))
  11782. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11783. (set (nth 1 a) (nth 2 a))))
  11784. ((setq a (org-extract-log-state-settings w))
  11785. (and (member (car a) org-todo-keywords-1)
  11786. (push a org-todo-log-states)))))))
  11787. (defun org-get-todo-sequence-head (kwd)
  11788. "Return the head of the TODO sequence to which KWD belongs.
  11789. If KWD is not set, check if there is a text property remembering the
  11790. right sequence."
  11791. (let (p)
  11792. (cond
  11793. ((not kwd)
  11794. (or (get-text-property (point-at-bol) 'org-todo-head)
  11795. (progn
  11796. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11797. nil (point-at-eol)))
  11798. (get-text-property p 'org-todo-head))))
  11799. ((not (member kwd org-todo-keywords-1))
  11800. (car org-todo-keywords-1))
  11801. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11802. (defun org-fast-todo-selection ()
  11803. "Fast TODO keyword selection with single keys.
  11804. Returns the new TODO keyword, or nil if no state change should occur."
  11805. (let* ((fulltable org-todo-key-alist)
  11806. (done-keywords org-done-keywords) ;; needed for the faces.
  11807. (maxlen (apply 'max (mapcar
  11808. (lambda (x)
  11809. (if (stringp (car x)) (string-width (car x)) 0))
  11810. fulltable)))
  11811. (expert nil)
  11812. (fwidth (+ maxlen 3 1 3))
  11813. (ncol (/ (- (window-width) 4) fwidth))
  11814. tg cnt e c tbl
  11815. groups ingroup)
  11816. (save-excursion
  11817. (save-window-excursion
  11818. (if expert
  11819. (set-buffer (get-buffer-create " *Org todo*"))
  11820. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11821. (erase-buffer)
  11822. (org-set-local 'org-done-keywords done-keywords)
  11823. (setq tbl fulltable cnt 0)
  11824. (while (setq e (pop tbl))
  11825. (cond
  11826. ((equal e '(:startgroup))
  11827. (push '() groups) (setq ingroup t)
  11828. (when (not (= cnt 0))
  11829. (setq cnt 0)
  11830. (insert "\n"))
  11831. (insert "{ "))
  11832. ((equal e '(:endgroup))
  11833. (setq ingroup nil cnt 0)
  11834. (insert "}\n"))
  11835. ((equal e '(:newline))
  11836. (when (not (= cnt 0))
  11837. (setq cnt 0)
  11838. (insert "\n")
  11839. (setq e (car tbl))
  11840. (while (equal (car tbl) '(:newline))
  11841. (insert "\n")
  11842. (setq tbl (cdr tbl)))))
  11843. (t
  11844. (setq tg (car e) c (cdr e))
  11845. (if ingroup (push tg (car groups)))
  11846. (setq tg (org-add-props tg nil 'face
  11847. (org-get-todo-face tg)))
  11848. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11849. (insert "[" c "] " tg (make-string
  11850. (- fwidth 4 (length tg)) ?\ ))
  11851. (when (= (setq cnt (1+ cnt)) ncol)
  11852. (insert "\n")
  11853. (if ingroup (insert " "))
  11854. (setq cnt 0)))))
  11855. (insert "\n")
  11856. (goto-char (point-min))
  11857. (if (not expert) (org-fit-window-to-buffer))
  11858. (message "[a-z..]:Set [SPC]:clear")
  11859. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11860. (cond
  11861. ((or (= c ?\C-g)
  11862. (and (= c ?q) (not (rassoc c fulltable))))
  11863. (setq quit-flag t))
  11864. ((= c ?\ ) nil)
  11865. ((setq e (rassoc c fulltable) tg (car e))
  11866. tg)
  11867. (t (setq quit-flag t)))))))
  11868. (defun org-entry-is-todo-p ()
  11869. (member (org-get-todo-state) org-not-done-keywords))
  11870. (defun org-entry-is-done-p ()
  11871. (member (org-get-todo-state) org-done-keywords))
  11872. (defun org-get-todo-state ()
  11873. "Return the TODO keyword of the current subtree."
  11874. (save-excursion
  11875. (org-back-to-heading t)
  11876. (and (looking-at org-todo-line-regexp)
  11877. (match-end 2)
  11878. (match-string 2))))
  11879. (defun org-at-date-range-p (&optional inactive-ok)
  11880. "Is the cursor inside a date range?"
  11881. (interactive)
  11882. (save-excursion
  11883. (catch 'exit
  11884. (let ((pos (point)))
  11885. (skip-chars-backward "^[<\r\n")
  11886. (skip-chars-backward "<[")
  11887. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11888. (>= (match-end 0) pos)
  11889. (throw 'exit t))
  11890. (skip-chars-backward "^<[\r\n")
  11891. (skip-chars-backward "<[")
  11892. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11893. (>= (match-end 0) pos)
  11894. (throw 'exit t)))
  11895. nil)))
  11896. (defun org-get-repeat (&optional tagline)
  11897. "Check if there is a deadline/schedule with repeater in this entry."
  11898. (save-match-data
  11899. (save-excursion
  11900. (org-back-to-heading t)
  11901. (and (re-search-forward (if tagline
  11902. (concat tagline "\\s-*" org-repeat-re)
  11903. org-repeat-re)
  11904. (org-entry-end-position) t)
  11905. (match-string-no-properties 1)))))
  11906. (defvar org-last-changed-timestamp)
  11907. (defvar org-last-inserted-timestamp)
  11908. (defvar org-log-post-message)
  11909. (defvar org-log-note-purpose)
  11910. (defvar org-log-note-how nil)
  11911. (defvar org-log-note-extra)
  11912. (defun org-auto-repeat-maybe (done-word)
  11913. "Check if the current headline contains a repeated deadline/schedule.
  11914. If yes, set TODO state back to what it was and change the base date
  11915. of repeating deadline/scheduled time stamps to new date.
  11916. This function is run automatically after each state change to a DONE state."
  11917. ;; last-state is dynamically scoped into this function
  11918. (let* ((repeat (org-get-repeat))
  11919. (aa (assoc org-last-state org-todo-kwd-alist))
  11920. (interpret (nth 1 aa))
  11921. (head (nth 2 aa))
  11922. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11923. (msg "Entry repeats: ")
  11924. (org-log-done nil)
  11925. (org-todo-log-states nil)
  11926. re type n what ts time to-state)
  11927. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11928. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11929. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11930. org-todo-repeat-to-state))
  11931. (unless (and to-state (member to-state org-todo-keywords-1))
  11932. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11933. (org-todo to-state)
  11934. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11935. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11936. (org-time-stamp-format t t))))
  11937. (when org-log-repeat
  11938. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11939. (memq 'org-add-log-note post-command-hook))
  11940. ;; OK, we are already setup for some record
  11941. (if (eq org-log-repeat 'note)
  11942. ;; make sure we take a note, not only a time stamp
  11943. (setq org-log-note-how 'note))
  11944. ;; Set up for taking a record
  11945. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11946. org-last-state
  11947. 'findpos org-log-repeat)))
  11948. (org-back-to-heading t)
  11949. (org-add-planning-info nil nil 'closed)
  11950. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11951. org-deadline-time-regexp "\\)\\|\\("
  11952. org-ts-regexp "\\)"))
  11953. (while (re-search-forward
  11954. re (save-excursion (outline-next-heading) (point)) t)
  11955. (setq type (if (match-end 1) org-scheduled-string
  11956. (if (match-end 3) org-deadline-string "Plain:"))
  11957. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11958. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11959. (setq n (string-to-number (match-string 2 ts))
  11960. what (match-string 3 ts))
  11961. (if (equal what "w") (setq n (* n 7) what "d"))
  11962. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11963. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11964. ;; Preparation, see if we need to modify the start date for the change
  11965. (when (match-end 1)
  11966. (setq time (save-match-data (org-time-string-to-time ts)))
  11967. (cond
  11968. ((equal (match-string 1 ts) ".")
  11969. ;; Shift starting date to today
  11970. (org-timestamp-change
  11971. (- (org-today) (time-to-days time))
  11972. 'day))
  11973. ((equal (match-string 1 ts) "+")
  11974. (let ((nshiftmax 10) (nshift 0))
  11975. (while (or (= nshift 0)
  11976. (<= (time-to-days time)
  11977. (time-to-days (current-time))))
  11978. (when (= (incf nshift) nshiftmax)
  11979. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11980. (user-error "Abort")))
  11981. (org-timestamp-change n (cdr (assoc what whata)))
  11982. (org-at-timestamp-p t)
  11983. (setq ts (match-string 1))
  11984. (setq time (save-match-data (org-time-string-to-time ts)))))
  11985. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11986. ;; rematch, so that we have everything in place for the real shift
  11987. (org-at-timestamp-p t)
  11988. (setq ts (match-string 1))
  11989. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11990. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11991. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11992. (setq org-log-post-message msg)
  11993. (message "%s" msg))))
  11994. (defun org-show-todo-tree (arg)
  11995. "Make a compact tree which shows all headlines marked with TODO.
  11996. The tree will show the lines where the regexp matches, and all higher
  11997. headlines above the match.
  11998. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11999. With a numeric prefix N, construct a sparse tree for the Nth element
  12000. of `org-todo-keywords-1'."
  12001. (interactive "P")
  12002. (let ((case-fold-search nil)
  12003. (kwd-re
  12004. (cond ((null arg) org-not-done-regexp)
  12005. ((equal arg '(4))
  12006. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12007. (mapcar 'list org-todo-keywords-1))))
  12008. (concat "\\("
  12009. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12010. "\\)\\>")))
  12011. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12012. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12013. org-todo-keywords-1)))
  12014. (t (user-error "Invalid prefix argument: %s" arg)))))
  12015. (message "%d TODO entries found"
  12016. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12017. (defun org-deadline (arg &optional time)
  12018. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12019. With one universal prefix argument, remove any deadline from the item.
  12020. With two universal prefix arguments, prompt for a warning delay.
  12021. With argument TIME, set the deadline at the corresponding date. TIME
  12022. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12023. (interactive "P")
  12024. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12025. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12026. 'region-start-level 'region))
  12027. org-loop-over-headlines-in-active-region)
  12028. (org-map-entries
  12029. `(org-deadline ',arg ,time)
  12030. org-loop-over-headlines-in-active-region
  12031. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12032. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12033. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12034. (repeater (and old-date
  12035. (string-match
  12036. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12037. old-date)
  12038. (match-string 1 old-date))))
  12039. (cond
  12040. ((equal arg '(4))
  12041. (when (and old-date org-log-redeadline)
  12042. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12043. org-log-redeadline))
  12044. (org-remove-timestamp-with-keyword org-deadline-string)
  12045. (message "Item no longer has a deadline."))
  12046. ((equal arg '(16))
  12047. (save-excursion
  12048. (org-back-to-heading t)
  12049. (if (re-search-forward
  12050. org-deadline-time-regexp
  12051. (save-excursion (outline-next-heading) (point)) t)
  12052. (let* ((rpl0 (match-string 1))
  12053. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12054. (replace-match
  12055. (concat org-deadline-string
  12056. " <" rpl
  12057. (format " -%dd"
  12058. (abs
  12059. (- (time-to-days
  12060. (save-match-data
  12061. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12062. (time-to-days old-date-time))))
  12063. ">") t t))
  12064. (user-error "No deadline information to update"))))
  12065. (t
  12066. (org-add-planning-info 'deadline time 'closed)
  12067. (when (and old-date org-log-redeadline
  12068. (not (equal old-date
  12069. (substring org-last-inserted-timestamp 1 -1))))
  12070. (org-add-log-setup 'redeadline nil old-date 'findpos
  12071. org-log-redeadline))
  12072. (when repeater
  12073. (save-excursion
  12074. (org-back-to-heading t)
  12075. (when (re-search-forward (concat org-deadline-string " "
  12076. org-last-inserted-timestamp)
  12077. (save-excursion
  12078. (outline-next-heading) (point)) t)
  12079. (goto-char (1- (match-end 0)))
  12080. (insert " " repeater)
  12081. (setq org-last-inserted-timestamp
  12082. (concat (substring org-last-inserted-timestamp 0 -1)
  12083. " " repeater
  12084. (substring org-last-inserted-timestamp -1))))))
  12085. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12086. (defun org-schedule (arg &optional time)
  12087. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12088. With one universal prefix argument, remove any scheduling date from the item.
  12089. With two universal prefix arguments, prompt for a delay cookie.
  12090. With argument TIME, scheduled at the corresponding date. TIME can
  12091. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12092. (interactive "P")
  12093. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12094. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12095. 'region-start-level 'region))
  12096. org-loop-over-headlines-in-active-region)
  12097. (org-map-entries
  12098. `(org-schedule ',arg ,time)
  12099. org-loop-over-headlines-in-active-region
  12100. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12101. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12102. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12103. (repeater (and old-date
  12104. (string-match
  12105. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12106. old-date)
  12107. (match-string 1 old-date))))
  12108. (cond
  12109. ((equal arg '(4))
  12110. (progn
  12111. (when (and old-date org-log-reschedule)
  12112. (org-add-log-setup 'delschedule nil old-date 'findpos
  12113. org-log-reschedule))
  12114. (org-remove-timestamp-with-keyword org-scheduled-string)
  12115. (message "Item is no longer scheduled.")))
  12116. ((equal arg '(16))
  12117. (save-excursion
  12118. (org-back-to-heading t)
  12119. (if (re-search-forward
  12120. org-scheduled-time-regexp
  12121. (save-excursion (outline-next-heading) (point)) t)
  12122. (let* ((rpl0 (match-string 1))
  12123. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12124. (replace-match
  12125. (concat org-scheduled-string
  12126. " <" rpl
  12127. (format " -%dd"
  12128. (abs
  12129. (- (time-to-days
  12130. (save-match-data
  12131. (org-read-date nil t nil "Delay until" old-date-time)))
  12132. (time-to-days old-date-time))))
  12133. ">") t t))
  12134. (user-error "No scheduled information to update"))))
  12135. (t
  12136. (org-add-planning-info 'scheduled time 'closed)
  12137. (when (and old-date org-log-reschedule
  12138. (not (equal old-date
  12139. (substring org-last-inserted-timestamp 1 -1))))
  12140. (org-add-log-setup 'reschedule nil old-date 'findpos
  12141. org-log-reschedule))
  12142. (when repeater
  12143. (save-excursion
  12144. (org-back-to-heading t)
  12145. (when (re-search-forward (concat org-scheduled-string " "
  12146. org-last-inserted-timestamp)
  12147. (save-excursion
  12148. (outline-next-heading) (point)) t)
  12149. (goto-char (1- (match-end 0)))
  12150. (insert " " repeater)
  12151. (setq org-last-inserted-timestamp
  12152. (concat (substring org-last-inserted-timestamp 0 -1)
  12153. " " repeater
  12154. (substring org-last-inserted-timestamp -1))))))
  12155. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12156. (defun org-get-scheduled-time (pom &optional inherit)
  12157. "Get the scheduled time as a time tuple, of a format suitable
  12158. for calling org-schedule with, or if there is no scheduling,
  12159. returns nil."
  12160. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12161. (when time
  12162. (apply 'encode-time (org-parse-time-string time)))))
  12163. (defun org-get-deadline-time (pom &optional inherit)
  12164. "Get the deadline as a time tuple, of a format suitable for
  12165. calling org-deadline with, or if there is no scheduling, returns
  12166. nil."
  12167. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12168. (when time
  12169. (apply 'encode-time (org-parse-time-string time)))))
  12170. (defun org-remove-timestamp-with-keyword (keyword)
  12171. "Remove all time stamps with KEYWORD in the current entry."
  12172. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12173. beg)
  12174. (save-excursion
  12175. (org-back-to-heading t)
  12176. (setq beg (point))
  12177. (outline-next-heading)
  12178. (while (re-search-backward re beg t)
  12179. (replace-match "")
  12180. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12181. (equal (char-before) ?\ ))
  12182. (backward-delete-char 1)
  12183. (if (string-match "^[ \t]*$" (buffer-substring
  12184. (point-at-bol) (point-at-eol)))
  12185. (delete-region (point-at-bol)
  12186. (min (point-max) (1+ (point-at-eol))))))))))
  12187. (defvar org-time-was-given) ; dynamically scoped parameter
  12188. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12189. (defun org-at-planning-p ()
  12190. "Non-nil when point is on a planning info line."
  12191. ;; This is as accurate and faster than `org-element-at-point' since
  12192. ;; planning info location is fixed in the section.
  12193. (org-with-wide-buffer
  12194. (beginning-of-line)
  12195. (and (org-looking-at-p org-planning-line-re)
  12196. (eq (point)
  12197. (ignore-errors
  12198. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12199. (org-back-to-heading t)
  12200. (org-with-limited-levels (org-back-to-heading t)))
  12201. (line-beginning-position 2))))))
  12202. (defun org-add-planning-info (what &optional time &rest remove)
  12203. "Insert new timestamp with keyword in the planning line.
  12204. WHAT indicates what kind of time stamp to add. It is a symbol
  12205. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12206. the time to use. If none is given, the user is prompted for
  12207. a date. REMOVE indicates what kind of entries to remove. An old
  12208. WHAT entry will also be removed."
  12209. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12210. (catch 'exit
  12211. (when (and (memq what '(scheduled deadline))
  12212. (or (not time)
  12213. (and (stringp time)
  12214. (string-match "^[-+]+[0-9]" time))))
  12215. ;; Try to get a default date/time from existing timestamp
  12216. (save-excursion
  12217. (org-back-to-heading t)
  12218. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12219. (when (re-search-forward (if (eq what 'scheduled)
  12220. org-scheduled-time-regexp
  12221. org-deadline-time-regexp)
  12222. end t)
  12223. (setq ts (match-string 1)
  12224. default-time (apply 'encode-time (org-parse-time-string ts))
  12225. default-input (and ts (org-get-compact-tod ts)))))))
  12226. (when what
  12227. (setq time
  12228. (if (stringp time)
  12229. ;; This is a string (relative or absolute), set
  12230. ;; proper date.
  12231. (apply #'encode-time
  12232. (org-read-date-analyze
  12233. time default-time (decode-time default-time)))
  12234. ;; If necessary, get the time from the user
  12235. (or time (org-read-date nil 'to-time nil nil
  12236. default-time default-input)))))
  12237. (org-with-wide-buffer
  12238. (org-back-to-heading t)
  12239. (forward-line)
  12240. (unless (bolp) (insert "\n"))
  12241. (cond ((org-looking-at-p org-planning-line-re)
  12242. ;; Move to current indentation.
  12243. (skip-chars-forward " \t")
  12244. ;; Check if we have to remove something.
  12245. (dolist (type (if what (cons what remove) remove))
  12246. (when (save-excursion
  12247. (re-search-forward
  12248. (case type
  12249. (closed org-closed-time-regexp)
  12250. (deadline org-deadline-time-regexp)
  12251. (scheduled org-scheduled-time-regexp)
  12252. (otherwise (error "Invalid planning type: %s" type)))
  12253. (line-end-position) t))
  12254. (replace-match "")
  12255. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12256. (when (and (not what) (eq type 'closed))
  12257. (save-excursion
  12258. (beginning-of-line)
  12259. (if (looking-at "[ \t]*$")
  12260. (delete-region (point) (1+ (point-at-eol)))))))
  12261. ;; Remove leading white spaces.
  12262. (when (and (not (bolp)) (looking-at "[ \t]+")) (replace-match ""))))
  12263. ((not what) (throw 'exit nil)) ; Nothing to do.
  12264. (t (insert-before-markers "\n")
  12265. (backward-char 1)
  12266. (when org-adapt-indentation
  12267. (org-indent-to-column (1+ (org-outline-level))))))
  12268. (when what
  12269. ;; Insert planning keyword.
  12270. (insert (case what
  12271. (closed org-closed-string)
  12272. (deadline org-deadline-string)
  12273. (scheduled org-scheduled-string)
  12274. (otherwise (error "Invalid planning type: %s" what)))
  12275. " ")
  12276. ;; Insert associated timestamp.
  12277. (let ((ts (org-insert-time-stamp
  12278. time
  12279. (or org-time-was-given
  12280. (and (eq what 'closed) org-log-done-with-time))
  12281. (eq what 'closed)
  12282. nil nil (list org-end-time-was-given))))
  12283. (unless (eolp) (insert " "))
  12284. ts))))))
  12285. (defvar org-log-note-marker (make-marker))
  12286. (defvar org-log-note-purpose nil)
  12287. (defvar org-log-note-state nil)
  12288. (defvar org-log-note-previous-state nil)
  12289. (defvar org-log-note-extra nil)
  12290. (defvar org-log-note-window-configuration nil)
  12291. (defvar org-log-note-return-to (make-marker))
  12292. (defvar org-log-note-effective-time nil
  12293. "Remembered current time so that dynamically scoped
  12294. `org-extend-today-until' affects tha timestamps in state change
  12295. log")
  12296. (defvar org-log-post-message nil
  12297. "Message to be displayed after a log note has been stored.
  12298. The auto-repeater uses this.")
  12299. (defun org-add-note ()
  12300. "Add a note to the current entry.
  12301. This is done in the same way as adding a state change note."
  12302. (interactive)
  12303. (org-add-log-setup 'note nil nil 'findpos nil))
  12304. (defun org-log-beginning (&optional create)
  12305. "Return expected start of log notes in current entry.
  12306. When optional argument CREATE is non-nil, the function creates
  12307. a drawer to store notes, if necessary. Returned position ignores
  12308. narrowing."
  12309. (org-with-wide-buffer
  12310. (org-end-of-meta-data)
  12311. (let ((end (if (org-at-heading-p) (point)
  12312. (save-excursion (outline-next-heading) (point))))
  12313. (drawer (org-log-into-drawer)))
  12314. (cond
  12315. (drawer
  12316. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12317. (case-fold-search t))
  12318. (catch 'exit
  12319. ;; Try to find existing drawer.
  12320. (while (re-search-forward regexp end t)
  12321. (let ((element (org-element-at-point)))
  12322. (when (eq (org-element-type element) 'drawer)
  12323. (let ((cend (org-element-property :contents-end element)))
  12324. (when (and (not org-log-states-order-reversed) cend)
  12325. (goto-char cend)))
  12326. (throw 'exit nil))))
  12327. ;; No drawer found. Create one, if permitted.
  12328. (when create
  12329. (unless (bolp) (insert "\n"))
  12330. (let ((beg (point)))
  12331. (insert ":" drawer ":\n:END:\n")
  12332. (org-indent-region beg (point)))
  12333. (end-of-line -1)))))
  12334. (org-log-state-notes-insert-after-drawers
  12335. (while (and (looking-at org-drawer-regexp)
  12336. (progn (goto-char (match-end 0))
  12337. (re-search-forward org-property-end-re end t)))
  12338. (forward-line)))))
  12339. (if (bolp) (point) (line-beginning-position 2))))
  12340. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12341. "Set up the post command hook to take a note.
  12342. If this is about to TODO state change, the new state is expected in STATE.
  12343. When FINDPOS is non-nil, find the correct position for the note in
  12344. the current entry. If not, assume that it can be inserted at point.
  12345. HOW is an indicator what kind of note should be created.
  12346. EXTRA is additional text that will be inserted into the notes buffer."
  12347. (org-with-wide-buffer
  12348. (when findpos
  12349. (goto-char (org-log-beginning t))
  12350. (unless org-log-states-order-reversed
  12351. (org-skip-over-state-notes)
  12352. (skip-chars-backward " \t\n\r")
  12353. (forward-line)))
  12354. (move-marker org-log-note-marker (point))
  12355. ;; Preserve position even if a property drawer is inserted in the
  12356. ;; process.
  12357. (set-marker-insertion-type org-log-note-marker t)
  12358. (setq org-log-note-purpose purpose
  12359. org-log-note-state state
  12360. org-log-note-previous-state prev-state
  12361. org-log-note-how how
  12362. org-log-note-extra extra
  12363. org-log-note-effective-time (org-current-effective-time))
  12364. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12365. (defun org-skip-over-state-notes ()
  12366. "Skip past the list of State notes in an entry."
  12367. (when (ignore-errors (goto-char (org-in-item-p)))
  12368. (let* ((struct (org-list-struct))
  12369. (prevs (org-list-prevs-alist struct))
  12370. (regexp
  12371. (concat "[ \t]*- +"
  12372. (replace-regexp-in-string
  12373. " +" " +"
  12374. (org-replace-escapes
  12375. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12376. `(("%d" . ,org-ts-regexp-inactive)
  12377. ("%D" . ,org-ts-regexp)
  12378. ("%s" . "\"\\S-+\"")
  12379. ("%S" . "\"\\S-+\"")
  12380. ("%t" . ,org-ts-regexp-inactive)
  12381. ("%T" . ,org-ts-regexp)
  12382. ("%u" . ".*?")
  12383. ("%U" . ".*?")))))))
  12384. (while (org-looking-at-p regexp)
  12385. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12386. (org-list-get-item-end (point) struct)))))))
  12387. (defun org-add-log-note (&optional purpose)
  12388. "Pop up a window for taking a note, and add this note later at point."
  12389. (remove-hook 'post-command-hook 'org-add-log-note)
  12390. (setq org-log-note-window-configuration (current-window-configuration))
  12391. (delete-other-windows)
  12392. (move-marker org-log-note-return-to (point))
  12393. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12394. (goto-char org-log-note-marker)
  12395. (org-switch-to-buffer-other-window "*Org Note*")
  12396. (erase-buffer)
  12397. (if (memq org-log-note-how '(time state))
  12398. (let (current-prefix-arg) (org-store-log-note))
  12399. (let ((org-inhibit-startup t)) (org-mode))
  12400. (insert (format "# Insert note for %s.
  12401. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12402. (cond
  12403. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12404. ((eq org-log-note-purpose 'done) "closed todo item")
  12405. ((eq org-log-note-purpose 'state)
  12406. (format "state change from \"%s\" to \"%s\""
  12407. (or org-log-note-previous-state "")
  12408. (or org-log-note-state "")))
  12409. ((eq org-log-note-purpose 'reschedule)
  12410. "rescheduling")
  12411. ((eq org-log-note-purpose 'delschedule)
  12412. "no longer scheduled")
  12413. ((eq org-log-note-purpose 'redeadline)
  12414. "changing deadline")
  12415. ((eq org-log-note-purpose 'deldeadline)
  12416. "removing deadline")
  12417. ((eq org-log-note-purpose 'refile)
  12418. "refiling")
  12419. ((eq org-log-note-purpose 'note)
  12420. "this entry")
  12421. (t (error "This should not happen")))))
  12422. (if org-log-note-extra (insert org-log-note-extra))
  12423. (org-set-local 'org-finish-function 'org-store-log-note)
  12424. (run-hooks 'org-log-buffer-setup-hook)))
  12425. (defvar org-note-abort nil) ; dynamically scoped
  12426. (defun org-store-log-note ()
  12427. "Finish taking a log note, and insert it to where it belongs."
  12428. (let ((txt (buffer-string)))
  12429. (kill-buffer (current-buffer))
  12430. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12431. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12432. (setq txt (replace-match "" t t txt)))
  12433. (if (string-match "\\s-+\\'" txt)
  12434. (setq txt (replace-match "" t t txt)))
  12435. (setq lines (org-split-string txt "\n"))
  12436. (when (and note (string-match "\\S-" note))
  12437. (setq note
  12438. (org-replace-escapes
  12439. note
  12440. (list (cons "%u" (user-login-name))
  12441. (cons "%U" user-full-name)
  12442. (cons "%t" (format-time-string
  12443. (org-time-stamp-format 'long 'inactive)
  12444. org-log-note-effective-time))
  12445. (cons "%T" (format-time-string
  12446. (org-time-stamp-format 'long nil)
  12447. org-log-note-effective-time))
  12448. (cons "%d" (format-time-string
  12449. (org-time-stamp-format nil 'inactive)
  12450. org-log-note-effective-time))
  12451. (cons "%D" (format-time-string
  12452. (org-time-stamp-format nil nil)
  12453. org-log-note-effective-time))
  12454. (cons "%s" (if org-log-note-state
  12455. (concat "\"" org-log-note-state "\"")
  12456. ""))
  12457. (cons "%S" (if org-log-note-previous-state
  12458. (concat "\"" org-log-note-previous-state "\"")
  12459. "\"\"")))))
  12460. (when lines (setq note (concat note " \\\\")))
  12461. (push note lines))
  12462. (when (or current-prefix-arg org-note-abort)
  12463. (when (org-log-into-drawer)
  12464. (org-remove-empty-drawer-at org-log-note-marker))
  12465. (setq lines nil))
  12466. (when lines
  12467. (with-current-buffer (marker-buffer org-log-note-marker)
  12468. (save-excursion
  12469. (goto-char org-log-note-marker)
  12470. (move-marker org-log-note-marker nil)
  12471. ;; Make sure point is at the beginning of an empty line.
  12472. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12473. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12474. ;; In an existing list, add a new item at the top level.
  12475. ;; Otherwise, indent line like a regular one.
  12476. (let ((itemp (org-in-item-p)))
  12477. (if itemp
  12478. (org-indent-line-to
  12479. (let ((struct (save-excursion
  12480. (goto-char itemp) (org-list-struct))))
  12481. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12482. (org-indent-line)))
  12483. (insert (org-list-bullet-string "-") (pop lines))
  12484. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12485. (dolist (line lines)
  12486. (insert "\n")
  12487. (org-indent-line-to ind)
  12488. (insert line)))
  12489. (message "Note stored")
  12490. (org-back-to-heading t)
  12491. (org-cycle-hide-drawers 'children))
  12492. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12493. ;; from within `org-add-log-note' because `buffer-undo-list'
  12494. ;; is then modified outside of `org-with-remote-undo'.
  12495. (when (eq this-command 'org-agenda-todo)
  12496. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12497. ;; Don't add undo information when called from `org-agenda-todo'
  12498. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12499. (set-window-configuration org-log-note-window-configuration)
  12500. (with-current-buffer (marker-buffer org-log-note-return-to)
  12501. (goto-char org-log-note-return-to))
  12502. (move-marker org-log-note-return-to nil)
  12503. (and org-log-post-message (message "%s" org-log-post-message))))
  12504. (defun org-remove-empty-drawer-at (pos)
  12505. "Remove an empty drawer at position POS.
  12506. POS may also be a marker."
  12507. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12508. (org-with-wide-buffer
  12509. (goto-char pos)
  12510. (let ((drawer (org-element-at-point)))
  12511. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12512. (not (org-element-property :contents-begin drawer)))
  12513. (delete-region (org-element-property :begin drawer)
  12514. (progn (goto-char (org-element-property :end drawer))
  12515. (skip-chars-backward " \r\t\n")
  12516. (forward-line)
  12517. (point))))))))
  12518. (defvar org-ts-type nil)
  12519. (defun org-sparse-tree (&optional arg type)
  12520. "Create a sparse tree, prompt for the details.
  12521. This command can create sparse trees. You first need to select the type
  12522. of match used to create the tree:
  12523. t Show all TODO entries.
  12524. T Show entries with a specific TODO keyword.
  12525. m Show entries selected by a tags/property match.
  12526. p Enter a property name and its value (both with completion on existing
  12527. names/values) and show entries with that property.
  12528. r Show entries matching a regular expression (`/' can be used as well).
  12529. b Show deadlines and scheduled items before a date.
  12530. a Show deadlines and scheduled items after a date.
  12531. d Show deadlines due within `org-deadline-warning-days'.
  12532. D Show deadlines and scheduled items between a date range."
  12533. (interactive "P")
  12534. (setq type (or type org-sparse-tree-default-date-type))
  12535. (setq org-ts-type type)
  12536. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12537. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12538. [c]ycle through date types: %s"
  12539. (case type
  12540. (all "all timestamps")
  12541. (scheduled "only scheduled")
  12542. (deadline "only deadline")
  12543. (active "only active timestamps")
  12544. (inactive "only inactive timestamps")
  12545. (closed "with a closed time-stamp")
  12546. (otherwise "scheduled/deadline")))
  12547. (let ((answer (read-char-exclusive)))
  12548. (case answer
  12549. (?c
  12550. (org-sparse-tree
  12551. arg
  12552. (cadr
  12553. (memq type '(nil all scheduled deadline active inactive closed)))))
  12554. (?d (call-interactively 'org-check-deadlines))
  12555. (?b (call-interactively 'org-check-before-date))
  12556. (?a (call-interactively 'org-check-after-date))
  12557. (?D (call-interactively 'org-check-dates-range))
  12558. (?t (call-interactively 'org-show-todo-tree))
  12559. (?T (org-show-todo-tree '(4)))
  12560. (?m (call-interactively 'org-match-sparse-tree))
  12561. ((?p ?P)
  12562. (let* ((kwd (org-icompleting-read
  12563. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12564. (value (org-icompleting-read
  12565. "Value: " (mapcar 'list (org-property-values kwd)))))
  12566. (unless (string-match "\\`{.*}\\'" value)
  12567. (setq value (concat "\"" value "\"")))
  12568. (org-match-sparse-tree arg (concat kwd "=" value))))
  12569. ((?r ?R ?/) (call-interactively 'org-occur))
  12570. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12571. (defvar org-occur-highlights nil
  12572. "List of overlays used for occur matches.")
  12573. (make-variable-buffer-local 'org-occur-highlights)
  12574. (defvar org-occur-parameters nil
  12575. "Parameters of the active org-occur calls.
  12576. This is a list, each call to org-occur pushes as cons cell,
  12577. containing the regular expression and the callback, onto the list.
  12578. The list can contain several entries if `org-occur' has been called
  12579. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12580. will only contain one set of parameters. When the highlights are
  12581. removed (for example with `C-c C-c', or with the next edit (depending
  12582. on `org-remove-highlights-with-change'), this variable is emptied
  12583. as well.")
  12584. (make-variable-buffer-local 'org-occur-parameters)
  12585. (defun org-occur (regexp &optional keep-previous callback)
  12586. "Make a compact tree which shows all matches of REGEXP.
  12587. The tree will show the lines where the regexp matches, and all higher
  12588. headlines above the match. It will also show the heading after the match,
  12589. to make sure editing the matching entry is easy.
  12590. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12591. call to `org-occur' will be kept, to allow stacking of calls to this
  12592. command.
  12593. If CALLBACK is non-nil, it is a function which is called to confirm
  12594. that the match should indeed be shown."
  12595. (interactive "sRegexp: \nP")
  12596. (when (equal regexp "")
  12597. (user-error "Regexp cannot be empty"))
  12598. (unless keep-previous
  12599. (org-remove-occur-highlights nil nil t))
  12600. (push (cons regexp callback) org-occur-parameters)
  12601. (let ((cnt 0))
  12602. (save-excursion
  12603. (goto-char (point-min))
  12604. (if (or (not keep-previous) ; do not want to keep
  12605. (not org-occur-highlights)) ; no previous matches
  12606. ;; hide everything
  12607. (org-overview))
  12608. (while (re-search-forward regexp nil t)
  12609. (when (or (not callback)
  12610. (save-match-data (funcall callback)))
  12611. (setq cnt (1+ cnt))
  12612. (when org-highlight-sparse-tree-matches
  12613. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12614. (org-show-context 'occur-tree))))
  12615. (when org-remove-highlights-with-change
  12616. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12617. nil 'local))
  12618. (unless org-sparse-tree-open-archived-trees
  12619. (org-hide-archived-subtrees (point-min) (point-max)))
  12620. (run-hooks 'org-occur-hook)
  12621. (if (org-called-interactively-p 'interactive)
  12622. (message "%d match(es) for regexp %s" cnt regexp))
  12623. cnt))
  12624. (defun org-occur-next-match (&optional n reset)
  12625. "Function for `next-error-function' to find sparse tree matches.
  12626. N is the number of matches to move, when negative move backwards.
  12627. RESET is entirely ignored - this function always goes back to the
  12628. starting point when no match is found."
  12629. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12630. (search-func (if (< n 0)
  12631. 'previous-single-char-property-change
  12632. 'next-single-char-property-change))
  12633. (n (abs n))
  12634. (pos (point))
  12635. p1)
  12636. (catch 'exit
  12637. (while (setq p1 (funcall search-func (point) 'org-type))
  12638. (when (equal p1 limit)
  12639. (goto-char pos)
  12640. (user-error "No more matches"))
  12641. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12642. (setq n (1- n))
  12643. (when (= n 0)
  12644. (goto-char p1)
  12645. (throw 'exit (point))))
  12646. (goto-char p1))
  12647. (goto-char p1)
  12648. (user-error "No more matches"))))
  12649. (defun org-show-context (&optional key)
  12650. "Make sure point and context are visible.
  12651. Optional argument KEY, when non-nil, is a symbol. See
  12652. `org-show-context-detail' for allowed values and how much is to
  12653. be shown."
  12654. (org-show-set-visibility
  12655. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12656. ((cdr (assq key org-show-context-detail)))
  12657. (t (cdr (assq 'default org-show-context-detail))))))
  12658. (defun org-show-set-visibility (detail)
  12659. "Set visibility around point according to DETAIL.
  12660. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12661. `tree', `canonical' or t. See `org-show-context-detail' for more
  12662. information."
  12663. (unless (org-before-first-heading-p)
  12664. ;; Show current heading and possibly its entry, following headline
  12665. ;; or all children.
  12666. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12667. (org-flag-heading nil)
  12668. (org-show-entry)
  12669. (org-with-limited-levels
  12670. (case detail
  12671. ((tree canonical t) (show-children))
  12672. ((nil minimal ancestors))
  12673. (t (save-excursion
  12674. (outline-next-heading)
  12675. (org-flag-heading nil))))))
  12676. ;; Show all siblings.
  12677. (when (eq detail 'lineage) (org-show-siblings))
  12678. ;; Show ancestors, possibly with their children.
  12679. (when (memq detail '(ancestors lineage tree canonical t))
  12680. (save-excursion
  12681. (while (org-up-heading-safe)
  12682. (org-flag-heading nil)
  12683. (when (memq detail '(canonical t)) (org-show-entry))
  12684. (when (memq detail '(tree canonical t)) (show-children)))))))
  12685. (defvar org-reveal-start-hook nil
  12686. "Hook run before revealing a location.")
  12687. (defun org-reveal (&optional siblings)
  12688. "Show current entry, hierarchy above it, and the following headline.
  12689. This can be used to show a consistent set of context around
  12690. locations exposed with `org-show-context'.
  12691. With optional argument SIBLINGS, on each level of the hierarchy all
  12692. siblings are shown. This repairs the tree structure to what it would
  12693. look like when opened with hierarchical calls to `org-cycle'.
  12694. With double optional argument \\[universal-argument] \\[universal-argument], \
  12695. go to the parent and show the
  12696. entire tree."
  12697. (interactive "P")
  12698. (run-hooks 'org-reveal-start-hook)
  12699. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12700. ((equal siblings '(16))
  12701. (save-excursion
  12702. (when (org-up-heading-safe)
  12703. (org-show-subtree)
  12704. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12705. (t (org-show-set-visibility 'lineage))))
  12706. (defun org-highlight-new-match (beg end)
  12707. "Highlight from BEG to END and mark the highlight is an occur headline."
  12708. (let ((ov (make-overlay beg end)))
  12709. (overlay-put ov 'face 'secondary-selection)
  12710. (overlay-put ov 'org-type 'org-occur)
  12711. (push ov org-occur-highlights)))
  12712. (defun org-remove-occur-highlights (&optional beg end noremove)
  12713. "Remove the occur highlights from the buffer.
  12714. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12715. from the `before-change-functions' in the current buffer."
  12716. (interactive)
  12717. (unless org-inhibit-highlight-removal
  12718. (mapc 'delete-overlay org-occur-highlights)
  12719. (setq org-occur-highlights nil)
  12720. (setq org-occur-parameters nil)
  12721. (unless noremove
  12722. (remove-hook 'before-change-functions
  12723. 'org-remove-occur-highlights 'local))))
  12724. ;;;; Priorities
  12725. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12726. "Regular expression matching the priority indicator.")
  12727. (defvar org-remove-priority-next-time nil)
  12728. (defun org-priority-up ()
  12729. "Increase the priority of the current item."
  12730. (interactive)
  12731. (org-priority 'up))
  12732. (defun org-priority-down ()
  12733. "Decrease the priority of the current item."
  12734. (interactive)
  12735. (org-priority 'down))
  12736. (defun org-priority (&optional action show)
  12737. "Change the priority of an item.
  12738. ACTION can be `set', `up', `down', or a character."
  12739. (interactive "P")
  12740. (if (equal action '(4))
  12741. (org-show-priority)
  12742. (unless org-enable-priority-commands
  12743. (user-error "Priority commands are disabled"))
  12744. (setq action (or action 'set))
  12745. (let (current new news have remove)
  12746. (save-excursion
  12747. (org-back-to-heading t)
  12748. (if (looking-at org-priority-regexp)
  12749. (setq current (string-to-char (match-string 2))
  12750. have t))
  12751. (cond
  12752. ((eq action 'remove)
  12753. (setq remove t new ?\ ))
  12754. ((or (eq action 'set)
  12755. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12756. (if (not (eq action 'set))
  12757. (setq new action)
  12758. (message "Priority %c-%c, SPC to remove: "
  12759. org-highest-priority org-lowest-priority)
  12760. (save-match-data
  12761. (setq new (read-char-exclusive))))
  12762. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12763. (= (upcase org-lowest-priority) org-lowest-priority))
  12764. (setq new (upcase new)))
  12765. (cond ((equal new ?\ ) (setq remove t))
  12766. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12767. (user-error "Priority must be between `%c' and `%c'"
  12768. org-highest-priority org-lowest-priority))))
  12769. ((eq action 'up)
  12770. (setq new (if have
  12771. (1- current) ; normal cycling
  12772. ;; last priority was empty
  12773. (if (eq last-command this-command)
  12774. org-lowest-priority ; wrap around empty to lowest
  12775. ;; default
  12776. (if org-priority-start-cycle-with-default
  12777. org-default-priority
  12778. (1- org-default-priority))))))
  12779. ((eq action 'down)
  12780. (setq new (if have
  12781. (1+ current) ; normal cycling
  12782. ;; last priority was empty
  12783. (if (eq last-command this-command)
  12784. org-highest-priority ; wrap around empty to highest
  12785. ;; default
  12786. (if org-priority-start-cycle-with-default
  12787. org-default-priority
  12788. (1+ org-default-priority))))))
  12789. (t (user-error "Invalid action")))
  12790. (if (or (< (upcase new) org-highest-priority)
  12791. (> (upcase new) org-lowest-priority))
  12792. (if (and (memq action '(up down))
  12793. (not have) (not (eq last-command this-command)))
  12794. ;; `new' is from default priority
  12795. (error
  12796. "The default can not be set, see `org-default-priority' why")
  12797. ;; normal cycling: `new' is beyond highest/lowest priority
  12798. ;; and is wrapped around to the empty priority
  12799. (setq remove t)))
  12800. (setq news (format "%c" new))
  12801. (if have
  12802. (if remove
  12803. (replace-match "" t t nil 1)
  12804. (replace-match news t t nil 2))
  12805. (if remove
  12806. (user-error "No priority cookie found in line")
  12807. (let ((case-fold-search nil))
  12808. (looking-at org-todo-line-regexp))
  12809. (if (match-end 2)
  12810. (progn
  12811. (goto-char (match-end 2))
  12812. (insert " [#" news "]"))
  12813. (goto-char (match-beginning 3))
  12814. (insert "[#" news "] "))))
  12815. (org-set-tags nil 'align))
  12816. (if remove
  12817. (message "Priority removed")
  12818. (message "Priority of current item set to %s" news)))))
  12819. (defun org-show-priority ()
  12820. "Show the priority of the current item.
  12821. This priority is composed of the main priority given with the [#A] cookies,
  12822. and by additional input from the age of a schedules or deadline entry."
  12823. (interactive)
  12824. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12825. (org-get-at-bol 'priority)
  12826. (save-excursion
  12827. (save-match-data
  12828. (beginning-of-line)
  12829. (and (looking-at org-heading-regexp)
  12830. (org-get-priority (match-string 0))))))))
  12831. (message "Priority is %d" (if pri pri -1000))))
  12832. (defun org-get-priority (s)
  12833. "Find priority cookie and return priority."
  12834. (save-match-data
  12835. (if (functionp org-get-priority-function)
  12836. (funcall org-get-priority-function)
  12837. (if (not (string-match org-priority-regexp s))
  12838. (* 1000 (- org-lowest-priority org-default-priority))
  12839. (* 1000 (- org-lowest-priority
  12840. (string-to-char (match-string 2 s))))))))
  12841. ;;;; Tags
  12842. (defvar org-agenda-archives-mode)
  12843. (defvar org-map-continue-from nil
  12844. "Position from where mapping should continue.
  12845. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12846. (defvar org-scanner-tags nil
  12847. "The current tag list while the tags scanner is running.")
  12848. (defvar org-trust-scanner-tags nil
  12849. "Should `org-get-tags-at' use the tags for the scanner.
  12850. This is for internal dynamical scoping only.
  12851. When this is non-nil, the function `org-get-tags-at' will return the value
  12852. of `org-scanner-tags' instead of building the list by itself. This
  12853. can lead to large speed-ups when the tags scanner is used in a file with
  12854. many entries, and when the list of tags is retrieved, for example to
  12855. obtain a list of properties. Building the tags list for each entry in such
  12856. a file becomes an N^2 operation - but with this variable set, it scales
  12857. as N.")
  12858. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12859. "Scan headline tags with inheritance and produce output ACTION.
  12860. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12861. or `agenda' to produce an entry list for an agenda view. It can also be
  12862. a Lisp form or a function that should be called at each matched headline, in
  12863. this case the return value is a list of all return values from these calls.
  12864. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12865. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12866. only lines with a not-done TODO keyword are included in the output.
  12867. This should be the same variable that was scoped into
  12868. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12869. START-LEVEL can be a string with asterisks, reducing the scope to
  12870. headlines matching this string."
  12871. (require 'org-agenda)
  12872. (let* ((re (concat "^"
  12873. (if start-level
  12874. ;; Get the correct level to match
  12875. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12876. org-outline-regexp)
  12877. " *\\(\\<\\("
  12878. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12879. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12880. (props (list 'face 'default
  12881. 'done-face 'org-agenda-done
  12882. 'undone-face 'default
  12883. 'mouse-face 'highlight
  12884. 'org-not-done-regexp org-not-done-regexp
  12885. 'org-todo-regexp org-todo-regexp
  12886. 'org-complex-heading-regexp org-complex-heading-regexp
  12887. 'help-echo
  12888. (format "mouse-2 or RET jump to org file %s"
  12889. (abbreviate-file-name
  12890. (or (buffer-file-name (buffer-base-buffer))
  12891. (buffer-name (buffer-base-buffer)))))))
  12892. (org-map-continue-from nil)
  12893. lspos tags tags-list
  12894. (tags-alist (list (cons 0 org-file-tags)))
  12895. (llast 0) rtn rtn1 level category i txt
  12896. todo marker entry priority
  12897. ts-date ts-date-type ts-date-pair)
  12898. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12899. (setq action (list 'lambda nil action)))
  12900. (save-excursion
  12901. (goto-char (point-min))
  12902. (when (eq action 'sparse-tree)
  12903. (org-overview)
  12904. (org-remove-occur-highlights))
  12905. (while (let (case-fold-search)
  12906. (re-search-forward re nil t))
  12907. (setq org-map-continue-from nil)
  12908. (catch :skip
  12909. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12910. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12911. (goto-char (setq lspos (match-beginning 0)))
  12912. (setq level (org-reduced-level (org-outline-level))
  12913. category (org-get-category))
  12914. (when (eq action 'agenda)
  12915. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12916. ts-date (car ts-date-pair)
  12917. ts-date-type (cdr ts-date-pair)))
  12918. (setq i llast llast level)
  12919. ;; remove tag lists from same and sublevels
  12920. (while (>= i level)
  12921. (when (setq entry (assoc i tags-alist))
  12922. (setq tags-alist (delete entry tags-alist)))
  12923. (setq i (1- i)))
  12924. ;; add the next tags
  12925. (when tags
  12926. (setq tags (org-split-string tags ":")
  12927. tags-alist
  12928. (cons (cons level tags) tags-alist)))
  12929. ;; compile tags for current headline
  12930. (setq tags-list
  12931. (if org-use-tag-inheritance
  12932. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12933. tags)
  12934. org-scanner-tags tags-list)
  12935. (when org-use-tag-inheritance
  12936. (setcdr (car tags-alist)
  12937. (mapcar (lambda (x)
  12938. (setq x (copy-sequence x))
  12939. (org-add-prop-inherited x))
  12940. (cdar tags-alist))))
  12941. (when (and tags org-use-tag-inheritance
  12942. (or (not (eq t org-use-tag-inheritance))
  12943. org-tags-exclude-from-inheritance))
  12944. ;; selective inheritance, remove uninherited ones
  12945. (setcdr (car tags-alist)
  12946. (org-remove-uninherited-tags (cdar tags-alist))))
  12947. (when (and
  12948. ;; eval matcher only when the todo condition is OK
  12949. (and (or (not todo-only) (member todo org-not-done-keywords))
  12950. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12951. (eval matcher)))
  12952. ;; Call the skipper, but return t if it does not skip,
  12953. ;; so that the `and' form continues evaluating
  12954. (progn
  12955. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12956. t)
  12957. ;; Check if timestamps are deselecting this entry
  12958. (or (not todo-only)
  12959. (and (member todo org-not-done-keywords)
  12960. (or (not org-agenda-tags-todo-honor-ignore-options)
  12961. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12962. ;; select this headline
  12963. (cond
  12964. ((eq action 'sparse-tree)
  12965. (and org-highlight-sparse-tree-matches
  12966. (org-get-heading) (match-end 0)
  12967. (org-highlight-new-match
  12968. (match-beginning 1) (match-end 1)))
  12969. (org-show-context 'tags-tree))
  12970. ((eq action 'agenda)
  12971. (setq txt (org-agenda-format-item
  12972. ""
  12973. (concat
  12974. (if (eq org-tags-match-list-sublevels 'indented)
  12975. (make-string (1- level) ?.) "")
  12976. (org-get-heading))
  12977. level category
  12978. tags-list)
  12979. priority (org-get-priority txt))
  12980. (goto-char lspos)
  12981. (setq marker (org-agenda-new-marker))
  12982. (org-add-props txt props
  12983. 'org-marker marker 'org-hd-marker marker 'org-category category
  12984. 'todo-state todo
  12985. 'ts-date ts-date
  12986. 'priority priority
  12987. 'type (concat "tagsmatch" ts-date-type))
  12988. (push txt rtn))
  12989. ((functionp action)
  12990. (setq org-map-continue-from nil)
  12991. (save-excursion
  12992. (setq rtn1 (funcall action))
  12993. (push rtn1 rtn)))
  12994. (t (user-error "Invalid action")))
  12995. ;; if we are to skip sublevels, jump to end of subtree
  12996. (unless org-tags-match-list-sublevels
  12997. (org-end-of-subtree t)
  12998. (backward-char 1))))
  12999. ;; Get the correct position from where to continue
  13000. (if org-map-continue-from
  13001. (goto-char org-map-continue-from)
  13002. (and (= (point) lspos) (end-of-line 1)))))
  13003. (when (and (eq action 'sparse-tree)
  13004. (not org-sparse-tree-open-archived-trees))
  13005. (org-hide-archived-subtrees (point-min) (point-max)))
  13006. (nreverse rtn)))
  13007. (defun org-remove-uninherited-tags (tags)
  13008. "Remove all tags that are not inherited from the list TAGS."
  13009. (cond
  13010. ((eq org-use-tag-inheritance t)
  13011. (if org-tags-exclude-from-inheritance
  13012. (org-delete-all org-tags-exclude-from-inheritance tags)
  13013. tags))
  13014. ((not org-use-tag-inheritance) nil)
  13015. ((stringp org-use-tag-inheritance)
  13016. (delq nil (mapcar
  13017. (lambda (x)
  13018. (if (and (string-match org-use-tag-inheritance x)
  13019. (not (member x org-tags-exclude-from-inheritance)))
  13020. x nil))
  13021. tags)))
  13022. ((listp org-use-tag-inheritance)
  13023. (delq nil (mapcar
  13024. (lambda (x)
  13025. (if (member x org-use-tag-inheritance) x nil))
  13026. tags)))))
  13027. (defun org-match-sparse-tree (&optional todo-only match)
  13028. "Create a sparse tree according to tags string MATCH.
  13029. MATCH can contain positive and negative selection of tags, like
  13030. \"+WORK+URGENT-WITHBOSS\".
  13031. If optional argument TODO-ONLY is non-nil, only select lines that are
  13032. also TODO lines."
  13033. (interactive "P")
  13034. (org-agenda-prepare-buffers (list (current-buffer)))
  13035. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13036. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13037. (defvar org-cached-props nil)
  13038. (defun org-cached-entry-get (pom property)
  13039. (if (or (eq t org-use-property-inheritance)
  13040. (and (stringp org-use-property-inheritance)
  13041. (let ((case-fold-search t))
  13042. (org-string-match-p org-use-property-inheritance property)))
  13043. (and (listp org-use-property-inheritance)
  13044. (member-ignore-case property org-use-property-inheritance)))
  13045. ;; Caching is not possible, check it directly.
  13046. (org-entry-get pom property 'inherit)
  13047. ;; Get all properties, so we can do complicated checks easily.
  13048. (cdr (assoc-string property
  13049. (or org-cached-props
  13050. (setq org-cached-props (org-entry-properties pom)))
  13051. t))))
  13052. (defun org-global-tags-completion-table (&optional files)
  13053. "Return the list of all tags in all agenda buffer/files.
  13054. Optional FILES argument is a list of files which can be used
  13055. instead of the agenda files."
  13056. (save-excursion
  13057. (org-uniquify
  13058. (delq nil
  13059. (apply 'append
  13060. (mapcar
  13061. (lambda (file)
  13062. (set-buffer (find-file-noselect file))
  13063. (append (org-get-buffer-tags)
  13064. (mapcar (lambda (x) (if (stringp (car-safe x))
  13065. (list (car-safe x)) nil))
  13066. org-tag-alist)))
  13067. (if (and files (car files))
  13068. files
  13069. (org-agenda-files))))))))
  13070. (defun org-make-tags-matcher (match)
  13071. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13072. The variable `todo-only' is scoped dynamically into this function.
  13073. It will be set to t if the matcher restricts matching to TODO entries,
  13074. otherwise will not be touched.
  13075. Returns a cons of the selection string MATCH and the constructed
  13076. lisp form implementing the matcher. The matcher is to be evaluated
  13077. at an Org entry, with point on the headline, and returns t if the
  13078. entry matches the selection string MATCH. The returned lisp form
  13079. references two variables with information about the entry, which
  13080. must be bound around the form's evaluation: todo, the TODO keyword
  13081. at the entry (or nil of none); and tags-list, the list of all tags
  13082. at the entry including inherited ones. Additionally, the category
  13083. of the entry (if any) must be specified as the text property
  13084. 'org-category on the headline.
  13085. See also `org-scan-tags'.
  13086. "
  13087. (declare (special todo-only))
  13088. (unless (boundp 'todo-only)
  13089. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13090. (unless match
  13091. ;; Get a new match request, with completion against the global
  13092. ;; tags table and the local tags in current buffer
  13093. (let ((org-last-tags-completion-table
  13094. (org-uniquify
  13095. (delq nil (append (org-get-buffer-tags)
  13096. (org-global-tags-completion-table))))))
  13097. (setq match (org-completing-read-no-i
  13098. "Match: " 'org-tags-completion-function nil nil nil
  13099. 'org-tags-history))))
  13100. ;; Parse the string and create a lisp form
  13101. (let ((match0 match)
  13102. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13103. minus tag mm
  13104. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13105. orterms term orlist re-p str-p level-p level-op time-p
  13106. prop-p pn pv po gv rest (start 0) (ss 0))
  13107. ;; Expand group tags
  13108. (setq match (org-tags-expand match))
  13109. ;; Check if there is a TODO part of this match, which would be the
  13110. ;; part after a "/". TO make sure that this slash is not part of
  13111. ;; a property value to be matched against, we also check that there
  13112. ;; is no " after that slash.
  13113. ;; First, find the last slash
  13114. (while (string-match "/+" match ss)
  13115. (setq start (match-beginning 0) ss (match-end 0)))
  13116. (if (and (string-match "/+" match start)
  13117. (not (save-match-data (string-match "\"" match start))))
  13118. ;; match contains also a todo-matching request
  13119. (progn
  13120. (setq tagsmatch (substring match 0 (match-beginning 0))
  13121. todomatch (substring match (match-end 0)))
  13122. (if (string-match "^!" todomatch)
  13123. (setq todo-only t todomatch (substring todomatch 1)))
  13124. (if (string-match "^\\s-*$" todomatch)
  13125. (setq todomatch nil)))
  13126. ;; only matching tags
  13127. (setq tagsmatch match todomatch nil))
  13128. ;; Make the tags matcher
  13129. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13130. (setq tagsmatcher t)
  13131. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13132. (while (setq term (pop orterms))
  13133. (while (and (equal (substring term -1) "\\") orterms)
  13134. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13135. (while (string-match re term)
  13136. (setq rest (substring term (match-end 0))
  13137. minus (and (match-end 1)
  13138. (equal (match-string 1 term) "-"))
  13139. tag (save-match-data (replace-regexp-in-string
  13140. "\\\\-" "-"
  13141. (match-string 2 term)))
  13142. re-p (equal (string-to-char tag) ?{)
  13143. level-p (match-end 4)
  13144. prop-p (match-end 5)
  13145. mm (cond
  13146. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13147. (level-p
  13148. (setq level-op (org-op-to-function (match-string 3 term)))
  13149. `(,level-op level ,(string-to-number
  13150. (match-string 4 term))))
  13151. (prop-p
  13152. (setq pn (match-string 5 term)
  13153. po (match-string 6 term)
  13154. pv (match-string 7 term)
  13155. re-p (equal (string-to-char pv) ?{)
  13156. str-p (equal (string-to-char pv) ?\")
  13157. time-p (save-match-data
  13158. (string-match "^\"[[<].*[]>]\"$" pv))
  13159. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13160. (if time-p (setq pv (org-matcher-time pv)))
  13161. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13162. (cond
  13163. ((equal pn "CATEGORY")
  13164. (setq gv '(get-text-property (point) 'org-category)))
  13165. ((equal pn "TODO")
  13166. (setq gv 'todo))
  13167. (t
  13168. (setq gv `(org-cached-entry-get nil ,pn))))
  13169. (if re-p
  13170. (if (eq po 'org<>)
  13171. `(not (string-match ,pv (or ,gv "")))
  13172. `(string-match ,pv (or ,gv "")))
  13173. (if str-p
  13174. `(,po (or ,gv "") ,pv)
  13175. `(,po (string-to-number (or ,gv ""))
  13176. ,(string-to-number pv) ))))
  13177. (t `(member ,tag tags-list)))
  13178. mm (if minus (list 'not mm) mm)
  13179. term rest)
  13180. (push mm tagsmatcher))
  13181. (push (if (> (length tagsmatcher) 1)
  13182. (cons 'and tagsmatcher)
  13183. (car tagsmatcher))
  13184. orlist)
  13185. (setq tagsmatcher nil))
  13186. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13187. (setq tagsmatcher
  13188. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13189. ;; Make the todo matcher
  13190. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13191. (setq todomatcher t)
  13192. (setq orterms (org-split-string todomatch "|") orlist nil)
  13193. (while (setq term (pop orterms))
  13194. (while (string-match re term)
  13195. (setq minus (and (match-end 1)
  13196. (equal (match-string 1 term) "-"))
  13197. kwd (match-string 2 term)
  13198. re-p (equal (string-to-char kwd) ?{)
  13199. term (substring term (match-end 0))
  13200. mm (if re-p
  13201. `(string-match ,(substring kwd 1 -1) todo)
  13202. (list 'equal 'todo kwd))
  13203. mm (if minus (list 'not mm) mm))
  13204. (push mm todomatcher))
  13205. (push (if (> (length todomatcher) 1)
  13206. (cons 'and todomatcher)
  13207. (car todomatcher))
  13208. orlist)
  13209. (setq todomatcher nil))
  13210. (setq todomatcher (if (> (length orlist) 1)
  13211. (cons 'or orlist) (car orlist))))
  13212. ;; Return the string and lisp forms of the matcher
  13213. (setq matcher (if todomatcher
  13214. (list 'and tagsmatcher todomatcher)
  13215. tagsmatcher))
  13216. (when todo-only
  13217. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13218. matcher)))
  13219. (cons match0 matcher)))
  13220. (defun org-tags-expand (match &optional single-as-list downcased)
  13221. "Expand group tags in MATCH.
  13222. This replaces every group tag in MATCH with a regexp tag search.
  13223. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13224. will be replaced like this:
  13225. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13226. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13227. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13228. Replacing by a regexp preserves the structure of the match.
  13229. E.g., this expansion
  13230. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13231. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13232. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13233. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13234. assumed to be a single group tag, and the function will return
  13235. the list of tags in this group.
  13236. When DOWNCASE is non-nil, expand downcased TAGS."
  13237. (if org-group-tags
  13238. (let* ((case-fold-search t)
  13239. (stable org-mode-syntax-table)
  13240. (tal (or org-tag-groups-alist-for-agenda
  13241. org-tag-groups-alist))
  13242. (tal (if downcased
  13243. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13244. (tml (mapcar 'car tal))
  13245. (rtnmatch match) rpl)
  13246. ;; @ and _ are allowed as word-components in tags
  13247. (modify-syntax-entry ?@ "w" stable)
  13248. (modify-syntax-entry ?_ "w" stable)
  13249. (while (and tml
  13250. (with-syntax-table stable
  13251. (string-match
  13252. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13253. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13254. (let* ((dir (match-string 1 rtnmatch))
  13255. (tag (match-string 2 rtnmatch))
  13256. (tag (if downcased (downcase tag) tag)))
  13257. (setq tml (delete tag tml))
  13258. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13259. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13260. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13261. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13262. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13263. (if single-as-list
  13264. (or (reverse rpl) (list rtnmatch))
  13265. rtnmatch))
  13266. (if single-as-list (list (if downcased (downcase match) match))
  13267. match)))
  13268. (defun org-op-to-function (op &optional stringp)
  13269. "Turn an operator into the appropriate function."
  13270. (setq op
  13271. (cond
  13272. ((equal op "<" ) '(< string< org-time<))
  13273. ((equal op ">" ) '(> org-string> org-time>))
  13274. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13275. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13276. ((member op '("=" "==")) '(= string= org-time=))
  13277. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13278. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13279. (defun org<> (a b) (not (= a b)))
  13280. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13281. (defun org-string>= (a b) (not (string< a b)))
  13282. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13283. (defun org-string<> (a b) (not (string= a b)))
  13284. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13285. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13286. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13287. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13288. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13289. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13290. (defun org-2ft (s)
  13291. "Convert S to a floating point time.
  13292. If S is already a number, just return it. If it is a string, parse
  13293. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13294. (cond
  13295. ((numberp s) s)
  13296. ((stringp s)
  13297. (condition-case nil
  13298. (float-time (apply 'encode-time (org-parse-time-string s)))
  13299. (error 0.)))
  13300. (t 0.)))
  13301. (defun org-time-today ()
  13302. "Time in seconds today at 0:00.
  13303. Returns the float number of seconds since the beginning of the
  13304. epoch to the beginning of today (00:00)."
  13305. (float-time (apply 'encode-time
  13306. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13307. (defun org-matcher-time (s)
  13308. "Interpret a time comparison value."
  13309. (save-match-data
  13310. (cond
  13311. ((string= s "<now>") (float-time))
  13312. ((string= s "<today>") (org-time-today))
  13313. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13314. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13315. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13316. (+ (org-time-today)
  13317. (* (string-to-number (match-string 1 s))
  13318. (cdr (assoc (match-string 2 s)
  13319. '(("d" . 86400.0) ("w" . 604800.0)
  13320. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13321. (t (org-2ft s)))))
  13322. (defun org-match-any-p (re list)
  13323. "Does re match any element of list?"
  13324. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13325. (delq nil list))
  13326. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13327. (defvar org-tags-overlay (make-overlay 1 1))
  13328. (org-detach-overlay org-tags-overlay)
  13329. (defun org-get-local-tags-at (&optional pos)
  13330. "Get a list of tags defined in the current headline."
  13331. (org-get-tags-at pos 'local))
  13332. (defun org-get-local-tags ()
  13333. "Get a list of tags defined in the current headline."
  13334. (org-get-tags-at nil 'local))
  13335. (defun org-get-tags-at (&optional pos local)
  13336. "Get a list of all headline tags applicable at POS.
  13337. POS defaults to point. If tags are inherited, the list contains
  13338. the targets in the same sequence as the headlines appear, i.e.
  13339. the tags of the current headline come last.
  13340. When LOCAL is non-nil, only return tags from the current headline,
  13341. ignore inherited ones."
  13342. (interactive)
  13343. (if (and org-trust-scanner-tags
  13344. (or (not pos) (equal pos (point)))
  13345. (not local))
  13346. org-scanner-tags
  13347. (let (tags ltags lastpos parent)
  13348. (save-excursion
  13349. (save-restriction
  13350. (widen)
  13351. (goto-char (or pos (point)))
  13352. (save-match-data
  13353. (catch 'done
  13354. (condition-case nil
  13355. (progn
  13356. (org-back-to-heading t)
  13357. (while (not (equal lastpos (point)))
  13358. (setq lastpos (point))
  13359. (when (looking-at
  13360. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13361. (setq ltags (org-split-string
  13362. (org-match-string-no-properties 1) ":"))
  13363. (when parent
  13364. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13365. (setq tags (append
  13366. (if parent
  13367. (org-remove-uninherited-tags ltags)
  13368. ltags)
  13369. tags)))
  13370. (or org-use-tag-inheritance (throw 'done t))
  13371. (if local (throw 'done t))
  13372. (or (org-up-heading-safe) (error nil))
  13373. (setq parent t)))
  13374. (error nil)))))
  13375. (if local
  13376. tags
  13377. (reverse (delete-dups
  13378. (reverse (append
  13379. (org-remove-uninherited-tags
  13380. org-file-tags) tags)))))))))
  13381. (defun org-add-prop-inherited (s)
  13382. (add-text-properties 0 (length s) '(inherited t) s)
  13383. s)
  13384. (defun org-toggle-tag (tag &optional onoff)
  13385. "Toggle the tag TAG for the current line.
  13386. If ONOFF is `on' or `off', don't toggle but set to this state."
  13387. (let (res current)
  13388. (save-excursion
  13389. (org-back-to-heading t)
  13390. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13391. (point-at-eol) t)
  13392. (progn
  13393. (setq current (match-string 1))
  13394. (replace-match ""))
  13395. (setq current ""))
  13396. (setq current (nreverse (org-split-string current ":")))
  13397. (cond
  13398. ((eq onoff 'on)
  13399. (setq res t)
  13400. (or (member tag current) (push tag current)))
  13401. ((eq onoff 'off)
  13402. (or (not (member tag current)) (setq current (delete tag current))))
  13403. (t (if (member tag current)
  13404. (setq current (delete tag current))
  13405. (setq res t)
  13406. (push tag current))))
  13407. (end-of-line 1)
  13408. (if current
  13409. (progn
  13410. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13411. (org-set-tags nil t))
  13412. (delete-horizontal-space))
  13413. (run-hooks 'org-after-tags-change-hook))
  13414. res))
  13415. (defun org-align-tags-here (to-col)
  13416. ;; Assumes that this is a headline
  13417. "Align tags on the current headline to TO-COL."
  13418. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13419. (beginning-of-line 1)
  13420. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13421. (< pos (match-beginning 2)))
  13422. (progn
  13423. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13424. (goto-char (match-beginning 1))
  13425. (insert " ")
  13426. (delete-region (point) (1+ (match-beginning 2)))
  13427. (setq ncol (max (current-column)
  13428. (1+ col)
  13429. (if (> to-col 0)
  13430. to-col
  13431. (- (abs to-col) tags-l))))
  13432. (setq p (point))
  13433. (insert (make-string (- ncol (current-column)) ?\ ))
  13434. (setq ncol (current-column))
  13435. (when indent-tabs-mode (tabify p (point-at-eol)))
  13436. (org-move-to-column (min ncol col)))
  13437. (goto-char pos))))
  13438. (defun org-set-tags-command (&optional arg just-align)
  13439. "Call the set-tags command for the current entry."
  13440. (interactive "P")
  13441. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13442. (org-set-tags arg just-align)
  13443. (save-excursion
  13444. (unless (and (org-region-active-p)
  13445. org-loop-over-headlines-in-active-region)
  13446. (org-back-to-heading t))
  13447. (org-set-tags arg just-align))))
  13448. (defun org-set-tags-to (data)
  13449. "Set the tags of the current entry to DATA, replacing the current tags.
  13450. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13451. If DATA is nil or the empty string, any tags will be removed."
  13452. (interactive "sTags: ")
  13453. (setq data
  13454. (cond
  13455. ((eq data nil) "")
  13456. ((equal data "") "")
  13457. ((stringp data)
  13458. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13459. ":"))
  13460. ((listp data)
  13461. (concat ":" (mapconcat 'identity data ":") ":"))))
  13462. (when data
  13463. (save-excursion
  13464. (org-back-to-heading t)
  13465. (when (looking-at org-complex-heading-regexp)
  13466. (if (match-end 5)
  13467. (progn
  13468. (goto-char (match-beginning 5))
  13469. (insert data)
  13470. (delete-region (point) (point-at-eol))
  13471. (org-set-tags nil 'align))
  13472. (goto-char (point-at-eol))
  13473. (insert " " data)
  13474. (org-set-tags nil 'align)))
  13475. (beginning-of-line 1)
  13476. (if (looking-at ".*?\\([ \t]+\\)$")
  13477. (delete-region (match-beginning 1) (match-end 1))))))
  13478. (defun org-align-all-tags ()
  13479. "Align the tags i all headings."
  13480. (interactive)
  13481. (save-excursion
  13482. (or (ignore-errors (org-back-to-heading t))
  13483. (outline-next-heading))
  13484. (if (org-at-heading-p)
  13485. (org-set-tags t)
  13486. (message "No headings"))))
  13487. (defvar org-indent-indentation-per-level)
  13488. (defun org-set-tags (&optional arg just-align)
  13489. "Set the tags for the current headline.
  13490. With prefix ARG, realign all tags in headings in the current buffer.
  13491. When JUST-ALIGN is non-nil, only align tags."
  13492. (interactive "P")
  13493. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13494. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13495. 'region-start-level 'region))
  13496. org-loop-over-headlines-in-active-region)
  13497. (org-map-entries
  13498. ;; We don't use ARG and JUST-ALIGN here because these args
  13499. ;; are not useful when looping over headlines.
  13500. `(org-set-tags)
  13501. org-loop-over-headlines-in-active-region
  13502. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13503. (let* ((re org-outline-regexp-bol)
  13504. (current (unless arg (org-get-tags-string)))
  13505. (col (current-column))
  13506. (org-setting-tags t)
  13507. table current-tags inherited-tags ; computed below when needed
  13508. tags p0 c0 c1 rpl di tc level)
  13509. (if arg
  13510. (save-excursion
  13511. (goto-char (point-min))
  13512. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13513. (while (re-search-forward re nil t)
  13514. (org-set-tags nil t)
  13515. (end-of-line 1)))
  13516. (message "All tags realigned to column %d" org-tags-column))
  13517. (if just-align
  13518. (setq tags current)
  13519. ;; Get a new set of tags from the user
  13520. (save-excursion
  13521. (setq table (append org-tag-persistent-alist
  13522. (or org-tag-alist (org-get-buffer-tags))
  13523. (and
  13524. org-complete-tags-always-offer-all-agenda-tags
  13525. (org-global-tags-completion-table
  13526. (org-agenda-files))))
  13527. org-last-tags-completion-table table
  13528. current-tags (org-split-string current ":")
  13529. inherited-tags (nreverse
  13530. (nthcdr (length current-tags)
  13531. (nreverse (org-get-tags-at))))
  13532. tags
  13533. (if (or (eq t org-use-fast-tag-selection)
  13534. (and org-use-fast-tag-selection
  13535. (delq nil (mapcar 'cdr table))))
  13536. (org-fast-tag-selection
  13537. current-tags inherited-tags table
  13538. (if org-fast-tag-selection-include-todo
  13539. org-todo-key-alist))
  13540. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13541. (org-trim
  13542. (org-icompleting-read "Tags: "
  13543. 'org-tags-completion-function
  13544. nil nil current 'org-tags-history))))))
  13545. (while (string-match "[-+&]+" tags)
  13546. ;; No boolean logic, just a list
  13547. (setq tags (replace-match ":" t t tags))))
  13548. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13549. (if org-tags-sort-function
  13550. (setq tags (mapconcat 'identity
  13551. (sort (org-split-string
  13552. tags (org-re "[^[:alnum:]_@#%]+"))
  13553. org-tags-sort-function) ":")))
  13554. (if (string-match "\\`[\t ]*\\'" tags)
  13555. (setq tags "")
  13556. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13557. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13558. ;; Insert new tags at the correct column
  13559. (beginning-of-line 1)
  13560. (setq level (or (and (looking-at org-outline-regexp)
  13561. (- (match-end 0) (point) 1))
  13562. 1))
  13563. (cond
  13564. ((and (equal current "") (equal tags "")))
  13565. ((re-search-forward
  13566. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13567. (point-at-eol) t)
  13568. (if (equal tags "")
  13569. (setq rpl "")
  13570. (goto-char (match-beginning 0))
  13571. (setq c0 (current-column)
  13572. ;; compute offset for the case of org-indent-mode active
  13573. di (if (org-bound-and-true-p org-indent-mode)
  13574. (* (1- org-indent-indentation-per-level) (1- level))
  13575. 0)
  13576. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13577. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13578. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13579. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13580. (replace-match rpl t t)
  13581. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13582. tags)
  13583. (t (error "Tags alignment failed")))
  13584. (org-move-to-column col)
  13585. (unless just-align
  13586. (run-hooks 'org-after-tags-change-hook))))))
  13587. (defun org-change-tag-in-region (beg end tag off)
  13588. "Add or remove TAG for each entry in the region.
  13589. This works in the agenda, and also in an org-mode buffer."
  13590. (interactive
  13591. (list (region-beginning) (region-end)
  13592. (let ((org-last-tags-completion-table
  13593. (if (derived-mode-p 'org-mode)
  13594. (org-uniquify
  13595. (delq nil (append (org-get-buffer-tags)
  13596. (org-global-tags-completion-table))))
  13597. (org-global-tags-completion-table))))
  13598. (org-icompleting-read
  13599. "Tag: " 'org-tags-completion-function nil nil nil
  13600. 'org-tags-history))
  13601. (progn
  13602. (message "[s]et or [r]emove? ")
  13603. (equal (read-char-exclusive) ?r))))
  13604. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13605. (let ((agendap (equal major-mode 'org-agenda-mode))
  13606. l1 l2 m buf pos newhead (cnt 0))
  13607. (goto-char end)
  13608. (setq l2 (1- (org-current-line)))
  13609. (goto-char beg)
  13610. (setq l1 (org-current-line))
  13611. (loop for l from l1 to l2 do
  13612. (org-goto-line l)
  13613. (setq m (get-text-property (point) 'org-hd-marker))
  13614. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13615. (and agendap m))
  13616. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13617. pos (if agendap m (point)))
  13618. (with-current-buffer buf
  13619. (save-excursion
  13620. (save-restriction
  13621. (goto-char pos)
  13622. (setq cnt (1+ cnt))
  13623. (org-toggle-tag tag (if off 'off 'on))
  13624. (setq newhead (org-get-heading)))))
  13625. (and agendap (org-agenda-change-all-lines newhead m))))
  13626. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13627. (defun org-tags-completion-function (string predicate &optional flag)
  13628. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13629. (confirm (lambda (x) (stringp (car x)))))
  13630. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13631. (setq s1 (match-string 1 string)
  13632. s2 (match-string 2 string))
  13633. (setq s1 "" s2 string))
  13634. (cond
  13635. ((eq flag nil)
  13636. ;; try completion
  13637. (setq rtn (try-completion s2 ctable confirm))
  13638. (if (stringp rtn)
  13639. (setq rtn
  13640. (concat s1 s2 (substring rtn (length s2))
  13641. (if (and org-add-colon-after-tag-completion
  13642. (assoc rtn ctable))
  13643. ":" ""))))
  13644. rtn)
  13645. ((eq flag t)
  13646. ;; all-completions
  13647. (all-completions s2 ctable confirm))
  13648. ((eq flag 'lambda)
  13649. ;; exact match?
  13650. (assoc s2 ctable)))))
  13651. (defun org-fast-tag-insert (kwd tags face &optional end)
  13652. "Insert KDW, and the TAGS, the latter with face FACE.
  13653. Also insert END."
  13654. (insert (format "%-12s" (concat kwd ":"))
  13655. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13656. (or end "")))
  13657. (defun org-fast-tag-show-exit (flag)
  13658. (save-excursion
  13659. (org-goto-line 3)
  13660. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13661. (replace-match ""))
  13662. (when flag
  13663. (end-of-line 1)
  13664. (org-move-to-column (- (window-width) 19) t)
  13665. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13666. (defun org-set-current-tags-overlay (current prefix)
  13667. "Add an overlay to CURRENT tag with PREFIX."
  13668. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13669. (if (featurep 'xemacs)
  13670. (org-overlay-display org-tags-overlay (concat prefix s)
  13671. 'secondary-selection)
  13672. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13673. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13674. (defvar org-last-tag-selection-key nil)
  13675. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13676. "Fast tag selection with single keys.
  13677. CURRENT is the current list of tags in the headline, INHERITED is the
  13678. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13679. possibly with grouping information. TODO-TABLE is a similar table with
  13680. TODO keywords, should these have keys assigned to them.
  13681. If the keys are nil, a-z are automatically assigned.
  13682. Returns the new tags string, or nil to not change the current settings."
  13683. (let* ((fulltable (append table todo-table))
  13684. (maxlen (apply 'max (mapcar
  13685. (lambda (x)
  13686. (if (stringp (car x)) (string-width (car x)) 0))
  13687. fulltable)))
  13688. (buf (current-buffer))
  13689. (expert (eq org-fast-tag-selection-single-key 'expert))
  13690. (buffer-tags nil)
  13691. (fwidth (+ maxlen 3 1 3))
  13692. (ncol (/ (- (window-width) 4) fwidth))
  13693. (i-face 'org-done)
  13694. (c-face 'org-todo)
  13695. tg cnt e c char c1 c2 ntable tbl rtn
  13696. ov-start ov-end ov-prefix
  13697. (exit-after-next org-fast-tag-selection-single-key)
  13698. (done-keywords org-done-keywords)
  13699. groups ingroup)
  13700. (save-excursion
  13701. (beginning-of-line 1)
  13702. (if (looking-at
  13703. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13704. (setq ov-start (match-beginning 1)
  13705. ov-end (match-end 1)
  13706. ov-prefix "")
  13707. (setq ov-start (1- (point-at-eol))
  13708. ov-end (1+ ov-start))
  13709. (skip-chars-forward "^\n\r")
  13710. (setq ov-prefix
  13711. (concat
  13712. (buffer-substring (1- (point)) (point))
  13713. (if (> (current-column) org-tags-column)
  13714. " "
  13715. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13716. (move-overlay org-tags-overlay ov-start ov-end)
  13717. (save-window-excursion
  13718. (if expert
  13719. (set-buffer (get-buffer-create " *Org tags*"))
  13720. (delete-other-windows)
  13721. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13722. (org-switch-to-buffer-other-window " *Org tags*"))
  13723. (erase-buffer)
  13724. (org-set-local 'org-done-keywords done-keywords)
  13725. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13726. (org-fast-tag-insert "Current" current c-face "\n\n")
  13727. (org-fast-tag-show-exit exit-after-next)
  13728. (org-set-current-tags-overlay current ov-prefix)
  13729. (setq tbl fulltable char ?a cnt 0)
  13730. (while (setq e (pop tbl))
  13731. (cond
  13732. ((equal (car e) :startgroup)
  13733. (push '() groups) (setq ingroup t)
  13734. (when (not (= cnt 0))
  13735. (setq cnt 0)
  13736. (insert "\n"))
  13737. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13738. ((equal (car e) :endgroup)
  13739. (setq ingroup nil cnt 0)
  13740. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13741. ((equal e '(:newline))
  13742. (when (not (= cnt 0))
  13743. (setq cnt 0)
  13744. (insert "\n")
  13745. (setq e (car tbl))
  13746. (while (equal (car tbl) '(:newline))
  13747. (insert "\n")
  13748. (setq tbl (cdr tbl)))))
  13749. ((equal e '(:grouptags)) nil)
  13750. (t
  13751. (setq tg (copy-sequence (car e)) c2 nil)
  13752. (if (cdr e)
  13753. (setq c (cdr e))
  13754. ;; automatically assign a character.
  13755. (setq c1 (string-to-char
  13756. (downcase (substring
  13757. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13758. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13759. (while (or (rassoc char ntable) (rassoc char table))
  13760. (setq char (1+ char)))
  13761. (setq c2 c1))
  13762. (setq c (or c2 char)))
  13763. (if ingroup (push tg (car groups)))
  13764. (setq tg (org-add-props tg nil 'face
  13765. (cond
  13766. ((not (assoc tg table))
  13767. (org-get-todo-face tg))
  13768. ((member tg current) c-face)
  13769. ((member tg inherited) i-face))))
  13770. (if (equal (caar tbl) :grouptags)
  13771. (org-add-props tg nil 'face 'org-tag-group))
  13772. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13773. (insert "[" c "] " tg (make-string
  13774. (- fwidth 4 (length tg)) ?\ ))
  13775. (push (cons tg c) ntable)
  13776. (when (= (setq cnt (1+ cnt)) ncol)
  13777. (insert "\n")
  13778. (if ingroup (insert " "))
  13779. (setq cnt 0)))))
  13780. (setq ntable (nreverse ntable))
  13781. (insert "\n")
  13782. (goto-char (point-min))
  13783. (if (not expert) (org-fit-window-to-buffer))
  13784. (setq rtn
  13785. (catch 'exit
  13786. (while t
  13787. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13788. (if (not groups) "no " "")
  13789. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13790. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13791. (setq org-last-tag-selection-key c)
  13792. (cond
  13793. ((= c ?\r) (throw 'exit t))
  13794. ((= c ?!)
  13795. (setq groups (not groups))
  13796. (goto-char (point-min))
  13797. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13798. ((= c ?\C-c)
  13799. (if (not expert)
  13800. (org-fast-tag-show-exit
  13801. (setq exit-after-next (not exit-after-next)))
  13802. (setq expert nil)
  13803. (delete-other-windows)
  13804. (set-window-buffer (split-window-vertically) " *Org tags*")
  13805. (org-switch-to-buffer-other-window " *Org tags*")
  13806. (org-fit-window-to-buffer)))
  13807. ((or (= c ?\C-g)
  13808. (and (= c ?q) (not (rassoc c ntable))))
  13809. (org-detach-overlay org-tags-overlay)
  13810. (setq quit-flag t))
  13811. ((= c ?\ )
  13812. (setq current nil)
  13813. (if exit-after-next (setq exit-after-next 'now)))
  13814. ((= c ?\t)
  13815. (condition-case nil
  13816. (setq tg (org-icompleting-read
  13817. "Tag: "
  13818. (or buffer-tags
  13819. (with-current-buffer buf
  13820. (org-get-buffer-tags)))))
  13821. (quit (setq tg "")))
  13822. (when (string-match "\\S-" tg)
  13823. (add-to-list 'buffer-tags (list tg))
  13824. (if (member tg current)
  13825. (setq current (delete tg current))
  13826. (push tg current)))
  13827. (if exit-after-next (setq exit-after-next 'now)))
  13828. ((setq e (rassoc c todo-table) tg (car e))
  13829. (with-current-buffer buf
  13830. (save-excursion (org-todo tg)))
  13831. (if exit-after-next (setq exit-after-next 'now)))
  13832. ((setq e (rassoc c ntable) tg (car e))
  13833. (if (member tg current)
  13834. (setq current (delete tg current))
  13835. (loop for g in groups do
  13836. (if (member tg g)
  13837. (mapc (lambda (x)
  13838. (setq current (delete x current)))
  13839. g)))
  13840. (push tg current))
  13841. (if exit-after-next (setq exit-after-next 'now))))
  13842. ;; Create a sorted list
  13843. (setq current
  13844. (sort current
  13845. (lambda (a b)
  13846. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13847. (if (eq exit-after-next 'now) (throw 'exit t))
  13848. (goto-char (point-min))
  13849. (beginning-of-line 2)
  13850. (delete-region (point) (point-at-eol))
  13851. (org-fast-tag-insert "Current" current c-face)
  13852. (org-set-current-tags-overlay current ov-prefix)
  13853. (while (re-search-forward
  13854. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13855. (setq tg (match-string 1))
  13856. (add-text-properties
  13857. (match-beginning 1) (match-end 1)
  13858. (list 'face
  13859. (cond
  13860. ((member tg current) c-face)
  13861. ((member tg inherited) i-face)
  13862. (t (get-text-property (match-beginning 1) 'face))))))
  13863. (goto-char (point-min)))))
  13864. (org-detach-overlay org-tags-overlay)
  13865. (if rtn
  13866. (mapconcat 'identity current ":")
  13867. nil))))
  13868. (defun org-get-tags-string ()
  13869. "Get the TAGS string in the current headline."
  13870. (unless (org-at-heading-p t)
  13871. (user-error "Not on a heading"))
  13872. (save-excursion
  13873. (beginning-of-line 1)
  13874. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13875. (org-match-string-no-properties 1)
  13876. "")))
  13877. (defun org-get-tags ()
  13878. "Get the list of tags specified in the current headline."
  13879. (org-split-string (org-get-tags-string) ":"))
  13880. (defun org-get-buffer-tags ()
  13881. "Get a table of all tags used in the buffer, for completion."
  13882. (org-with-wide-buffer
  13883. (goto-char (point-min))
  13884. (let ((tag-re (concat org-outline-regexp-bol
  13885. "\\(?:.*?[ \t]\\)?"
  13886. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13887. tags)
  13888. (while (re-search-forward tag-re nil t)
  13889. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13890. (push tag tags)))
  13891. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13892. ;;;; The mapping API
  13893. (defun org-map-entries (func &optional match scope &rest skip)
  13894. "Call FUNC at each headline selected by MATCH in SCOPE.
  13895. FUNC is a function or a lisp form. The function will be called without
  13896. arguments, with the cursor positioned at the beginning of the headline.
  13897. The return values of all calls to the function will be collected and
  13898. returned as a list.
  13899. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13900. does not need to preserve point. After evaluation, the cursor will be
  13901. moved to the end of the line (presumably of the headline of the
  13902. processed entry) and search continues from there. Under some
  13903. circumstances, this may not produce the wanted results. For example,
  13904. if you have removed (e.g. archived) the current (sub)tree it could
  13905. mean that the next entry will be skipped entirely. In such cases, you
  13906. can specify the position from where search should continue by making
  13907. FUNC set the variable `org-map-continue-from' to the desired buffer
  13908. position.
  13909. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13910. Only headlines that are matched by this query will be considered during
  13911. the iteration. When MATCH is nil or t, all headlines will be
  13912. visited by the iteration.
  13913. SCOPE determines the scope of this command. It can be any of:
  13914. nil The current buffer, respecting the restriction if any
  13915. tree The subtree started with the entry at point
  13916. region The entries within the active region, if any
  13917. region-start-level
  13918. The entries within the active region, but only those at
  13919. the same level than the first one.
  13920. file The current buffer, without restriction
  13921. file-with-archives
  13922. The current buffer, and any archives associated with it
  13923. agenda All agenda files
  13924. agenda-with-archives
  13925. All agenda files with any archive files associated with them
  13926. \(file1 file2 ...)
  13927. If this is a list, all files in the list will be scanned
  13928. The remaining args are treated as settings for the skipping facilities of
  13929. the scanner. The following items can be given here:
  13930. archive skip trees with the archive tag
  13931. comment skip trees with the COMMENT keyword
  13932. function or Emacs Lisp form:
  13933. will be used as value for `org-agenda-skip-function', so
  13934. whenever the function returns a position, FUNC will not be
  13935. called for that entry and search will continue from the
  13936. position returned
  13937. If your function needs to retrieve the tags including inherited tags
  13938. at the *current* entry, you can use the value of the variable
  13939. `org-scanner-tags' which will be much faster than getting the value
  13940. with `org-get-tags-at'. If your function gets properties with
  13941. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13942. to t around the call to `org-entry-properties' to get the same speedup.
  13943. Note that if your function moves around to retrieve tags and properties at
  13944. a *different* entry, you cannot use these techniques."
  13945. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13946. (not (org-region-active-p)))
  13947. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13948. (org-agenda-skip-archived-trees (memq 'archive skip))
  13949. (org-agenda-skip-comment-trees (memq 'comment skip))
  13950. (org-agenda-skip-function
  13951. (car (org-delete-all '(comment archive) skip)))
  13952. (org-tags-match-list-sublevels t)
  13953. (start-level (eq scope 'region-start-level))
  13954. matcher file res
  13955. org-todo-keywords-for-agenda
  13956. org-done-keywords-for-agenda
  13957. org-todo-keyword-alist-for-agenda
  13958. org-tag-alist-for-agenda
  13959. todo-only)
  13960. (cond
  13961. ((eq match t) (setq matcher t))
  13962. ((eq match nil) (setq matcher t))
  13963. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13964. (save-excursion
  13965. (save-restriction
  13966. (cond ((eq scope 'tree)
  13967. (org-back-to-heading t)
  13968. (org-narrow-to-subtree)
  13969. (setq scope nil))
  13970. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13971. (org-region-active-p))
  13972. ;; If needed, set start-level to a string like "2"
  13973. (when start-level
  13974. (save-excursion
  13975. (goto-char (region-beginning))
  13976. (unless (org-at-heading-p) (outline-next-heading))
  13977. (setq start-level (org-current-level))))
  13978. (narrow-to-region (region-beginning)
  13979. (save-excursion
  13980. (goto-char (region-end))
  13981. (unless (and (bolp) (org-at-heading-p))
  13982. (outline-next-heading))
  13983. (point)))
  13984. (setq scope nil)))
  13985. (if (not scope)
  13986. (progn
  13987. (org-agenda-prepare-buffers
  13988. (list (buffer-file-name (current-buffer))))
  13989. (setq res (org-scan-tags func matcher todo-only start-level)))
  13990. ;; Get the right scope
  13991. (cond
  13992. ((and scope (listp scope) (symbolp (car scope)))
  13993. (setq scope (eval scope)))
  13994. ((eq scope 'agenda)
  13995. (setq scope (org-agenda-files t)))
  13996. ((eq scope 'agenda-with-archives)
  13997. (setq scope (org-agenda-files t))
  13998. (setq scope (org-add-archive-files scope)))
  13999. ((eq scope 'file)
  14000. (setq scope (list (buffer-file-name))))
  14001. ((eq scope 'file-with-archives)
  14002. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14003. (org-agenda-prepare-buffers scope)
  14004. (while (setq file (pop scope))
  14005. (with-current-buffer (org-find-base-buffer-visiting file)
  14006. (save-excursion
  14007. (save-restriction
  14008. (widen)
  14009. (goto-char (point-min))
  14010. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14011. res)))
  14012. ;;; Properties API
  14013. (defconst org-special-properties
  14014. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14015. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14016. "The special properties valid in Org mode.
  14017. These are properties that are not defined in the property drawer,
  14018. but in some other way.")
  14019. (defconst org-default-properties
  14020. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14021. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14022. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14023. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14024. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14025. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14026. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14027. "Some properties that are used by Org mode for various purposes.
  14028. Being in this list makes sure that they are offered for completion.")
  14029. (defun org--update-property-plist (key val props)
  14030. "Associate KEY to VAL in alist PROPS.
  14031. Modifications are made by side-effect. Return new alist."
  14032. (let* ((appending (string= (substring key -1) "+"))
  14033. (key (if appending (substring key 0 -1) key))
  14034. (old (assoc-string key props t)))
  14035. (if (not old) (cons (cons key val) props)
  14036. (setcdr old (if appending (concat (cdr old) " " val) val))
  14037. props)))
  14038. (defun org-get-property-block (&optional beg force)
  14039. "Return the (beg . end) range of the body of the property drawer.
  14040. BEG is the beginning of the current subtree, or of the part
  14041. before the first headline. If it is not given, it will be found.
  14042. If the drawer does not exist, create it if FORCE is non-nil, or
  14043. return nil."
  14044. (org-with-wide-buffer
  14045. (when beg (goto-char beg))
  14046. (unless (org-before-first-heading-p)
  14047. (let ((beg (cond (beg)
  14048. ((or (not (featurep 'org-inlinetask))
  14049. (org-inlinetask-in-task-p))
  14050. (org-back-to-heading t))
  14051. (t (org-with-limited-levels (org-back-to-heading t))))))
  14052. (forward-line)
  14053. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14054. (cond ((looking-at org-property-drawer-re)
  14055. (forward-line)
  14056. (cons (point) (progn (goto-char (match-end 0))
  14057. (line-beginning-position))))
  14058. (force
  14059. (goto-char beg)
  14060. (org-insert-property-drawer)
  14061. (let ((pos (save-excursion (search-forward ":END:")
  14062. (line-beginning-position))))
  14063. (cons pos pos))))))))
  14064. (defun org-at-property-p ()
  14065. "Non-nil when point is inside a property drawer.
  14066. See `org-property-re' for match data, if applicable."
  14067. (save-excursion
  14068. (beginning-of-line)
  14069. (and (looking-at org-property-re)
  14070. (let ((property-drawer (save-match-data (org-get-property-block))))
  14071. (and property-drawer (< (point) (cdr property-drawer)))))))
  14072. (defun org-property-action ()
  14073. "Do an action on properties."
  14074. (interactive)
  14075. (unless (org-at-property-p) (user-error "Not at a property"))
  14076. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14077. (let ((c (read-char-exclusive)))
  14078. (case c
  14079. (?s (call-interactively #'org-set-property))
  14080. (?d (call-interactively #'org-delete-property))
  14081. (?D (call-interactively #'org-delete-property-globally))
  14082. (?c (call-interactively #'org-compute-property-at-point))
  14083. (otherwise (user-error "No such property action %c" c)))))
  14084. (defun org-inc-effort ()
  14085. "Increment the value of the effort property in the current entry."
  14086. (interactive)
  14087. (org-set-effort nil t))
  14088. (defvar org-clock-effort) ; Defined in org-clock.el.
  14089. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14090. (defun org-set-effort (&optional value increment)
  14091. "Set the effort property of the current entry.
  14092. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14093. 10th allowed value.
  14094. When INCREMENT is non-nil, set the property to the next allowed value."
  14095. (interactive "P")
  14096. (if (equal value 0) (setq value 10))
  14097. (let* ((completion-ignore-case t)
  14098. (prop org-effort-property)
  14099. (cur (org-entry-get nil prop))
  14100. (allowed (org-property-get-allowed-values nil prop 'table))
  14101. (existing (mapcar 'list (org-property-values prop)))
  14102. (heading (nth 4 (org-heading-components)))
  14103. rpl
  14104. (val (cond
  14105. ((stringp value) value)
  14106. ((and allowed (integerp value))
  14107. (or (car (nth (1- value) allowed))
  14108. (car (org-last allowed))))
  14109. ((and allowed increment)
  14110. (or (caadr (member (list cur) allowed))
  14111. (user-error "Allowed effort values are not set")))
  14112. (allowed
  14113. (message "Select 1-9,0, [RET%s]: %s"
  14114. (if cur (concat "=" cur) "")
  14115. (mapconcat 'car allowed " "))
  14116. (setq rpl (read-char-exclusive))
  14117. (if (equal rpl ?\r)
  14118. cur
  14119. (setq rpl (- rpl ?0))
  14120. (if (equal rpl 0) (setq rpl 10))
  14121. (if (and (> rpl 0) (<= rpl (length allowed)))
  14122. (car (nth (1- rpl) allowed))
  14123. (org-completing-read "Effort: " allowed nil))))
  14124. (t
  14125. (let (org-completion-use-ido org-completion-use-iswitchb)
  14126. (org-completing-read
  14127. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14128. (concat "[" cur "]") "")
  14129. ": ")
  14130. existing nil nil "" nil cur))))))
  14131. (unless (equal (org-entry-get nil prop) val)
  14132. (org-entry-put nil prop val))
  14133. (org-refresh-property
  14134. '((effort . identity)
  14135. (effort-minutes . org-duration-string-to-minutes))
  14136. val)
  14137. (when (string= heading org-clock-current-task)
  14138. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14139. (org-clock-update-mode-line))
  14140. (message "%s is now %s" prop val)))
  14141. (defun org-entry-properties (&optional pom which)
  14142. "Get all properties of the current entry.
  14143. When POM is a buffer position, get all properties from the entry
  14144. there instead.
  14145. This includes the TODO keyword, the tags, time strings for
  14146. deadline, scheduled, and clocking, and any additional properties
  14147. defined in the entry.
  14148. If WHICH is nil or `all', get all properties. If WHICH is
  14149. `special' or `standard', only get that subclass. If WHICH is
  14150. a string, only get that property.
  14151. Return value is an alist. Keys are properties, as upcased
  14152. strings."
  14153. (org-with-point-at pom
  14154. (when (and (derived-mode-p 'org-mode)
  14155. (ignore-errors (org-back-to-heading t)))
  14156. (catch 'exit
  14157. (let* ((beg (point))
  14158. (specific (and (stringp which) (upcase which)))
  14159. (which (cond ((not specific) which)
  14160. ((member specific org-special-properties) 'special)
  14161. (t 'standard)))
  14162. props)
  14163. ;; Get the special properties, like TODO and TAGS.
  14164. (when (memq which '(nil all special))
  14165. (when (or (not specific) (string= specific "CLOCKSUM"))
  14166. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14167. (when clocksum
  14168. (push (cons "CLOCKSUM"
  14169. (org-columns-number-to-string
  14170. (/ (float clocksum) 60.) 'add_times))
  14171. props)))
  14172. (when specific (throw 'exit props)))
  14173. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14174. (let ((clocksumt (get-text-property (point)
  14175. :org-clock-minutes-today)))
  14176. (when clocksumt
  14177. (push (cons "CLOCKSUM_T"
  14178. (org-columns-number-to-string
  14179. (/ (float clocksumt) 60.) 'add_times))
  14180. props)))
  14181. (when specific (throw 'exit props)))
  14182. (when (or (not specific) (string= specific "ITEM"))
  14183. (when (looking-at org-complex-heading-regexp)
  14184. (push (cons "ITEM"
  14185. (concat
  14186. (org-match-string-no-properties 1)
  14187. (let ((title (org-match-string-no-properties 4)))
  14188. (when (org-string-nw-p title)
  14189. (concat " " (org-remove-tabs title))))))
  14190. props))
  14191. (when specific (throw 'exit props)))
  14192. (when (or (not specific) (string= specific "TODO"))
  14193. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14194. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14195. (when specific (throw 'exit props)))
  14196. (when (or (not specific) (string= specific "PRIORITY"))
  14197. (when (looking-at org-priority-regexp)
  14198. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14199. (when specific (throw 'exit props)))
  14200. (when (or (not specific) (string= specific "FILE"))
  14201. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14202. props)
  14203. (when specific (throw 'exit props)))
  14204. (when (or (not specific) (string= specific "TAGS"))
  14205. (let ((value (org-string-nw-p (org-get-tags-string))))
  14206. (when value (push (cons "TAGS" value) props)))
  14207. (when specific (throw 'exit props)))
  14208. (when (or (not specific) (string= specific "ALLTAGS"))
  14209. (let ((value (org-get-tags-at)))
  14210. (when value
  14211. (push (cons "ALLTAGS"
  14212. (format ":%s:" (mapconcat #'identity value ":")))
  14213. props)))
  14214. (when specific (throw 'exit props)))
  14215. (when (or (not specific) (string= specific "BLOCKED"))
  14216. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14217. (when specific (throw 'exit props)))
  14218. (when (or (not specific)
  14219. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14220. (forward-line)
  14221. (when (org-looking-at-p org-planning-line-re)
  14222. (end-of-line)
  14223. (let ((bol (line-beginning-position))
  14224. ;; Backward compatibility: time keywords used to
  14225. ;; be configurable (before 8.3). Make sure we
  14226. ;; get the correct keyword.
  14227. (key-assoc `(("CLOSED" . ,org-closed-string)
  14228. ("DEADLINE" . ,org-deadline-string)
  14229. ("SCHEDULED" . ,org-scheduled-string))))
  14230. (dolist (pair (if specific (list (assoc specific key-assoc))
  14231. key-assoc))
  14232. (save-excursion
  14233. (when (search-backward (cdr pair) bol t)
  14234. (goto-char (match-end 0))
  14235. (skip-chars-forward " \t")
  14236. (and (looking-at org-ts-regexp-both)
  14237. (push (cons (car pair)
  14238. (org-match-string-no-properties 0))
  14239. props)))))))
  14240. (when specific (throw 'exit props)))
  14241. (when (or (not specific)
  14242. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14243. (let ((find-ts
  14244. (lambda (end ts)
  14245. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14246. (assoc "TIMESTAMP_IA" ts))
  14247. org-ts-regexp
  14248. org-ts-regexp-both)))
  14249. (catch 'next
  14250. (while (re-search-forward regexp end t)
  14251. (backward-char)
  14252. (let ((object (org-element-context)))
  14253. ;; Accept to match timestamps in node
  14254. ;; properties, too.
  14255. (when (memq (org-element-type object)
  14256. '(node-property timestamp))
  14257. (let ((type
  14258. (org-element-property :type object)))
  14259. (cond
  14260. ((and (memq type '(active active-range))
  14261. (not (equal specific "TIMESTAMP_IA")))
  14262. (unless (assoc "TIMESTAMP" ts)
  14263. (push (cons "TIMESTAMP"
  14264. (org-element-property
  14265. :raw-value object))
  14266. ts)
  14267. (when specific (throw 'exit ts))))
  14268. ((and (memq type '(inactive inactive-range))
  14269. (not (string= specific "TIMESTAMP")))
  14270. (unless (assoc "TIMESTAMP_IA" ts)
  14271. (push (cons "TIMESTAMP_IA"
  14272. (org-element-property
  14273. :raw-value object))
  14274. ts)
  14275. (when specific (throw 'exit ts))))))
  14276. ;; Both timestamp types are found,
  14277. ;; move to next part.
  14278. (when (= (length ts) 2) (throw 'next ts)))))
  14279. ts)))))
  14280. (goto-char beg)
  14281. ;; First look for timestamps within headline.
  14282. (let ((ts (funcall find-ts (line-end-position) nil)))
  14283. (if (= (length ts) 2) (setq props (nconc ts props))
  14284. (forward-line)
  14285. ;; Then find timestamps in the section, skipping
  14286. ;; planning line.
  14287. (when (org-looking-at-p org-planning-line-re)
  14288. (forward-line))
  14289. (let ((end (save-excursion (outline-next-heading))))
  14290. (setq props (nconc (funcall find-ts end ts) props))))))))
  14291. ;; Get the standard properties, like :PROP:.
  14292. (when (memq which '(nil all standard))
  14293. ;; If we are looking after a specific property, delegate
  14294. ;; to `org-entry-get', which is faster. However, make an
  14295. ;; exception for "CATEGORY", since it can be also set
  14296. ;; through keywords (i.e. #+CATEGORY).
  14297. (if (and specific (not (equal specific "CATEGORY")))
  14298. (let ((value (org-entry-get beg specific nil t)))
  14299. (throw 'exit (and value (list (cons specific value)))))
  14300. (let ((range (org-get-property-block beg)))
  14301. (when range
  14302. (let ((end (cdr range)) seen-base)
  14303. (goto-char (car range))
  14304. ;; Unlike to `org--update-property-plist', we
  14305. ;; handle the case where base values is found
  14306. ;; after its extension. We also forbid standard
  14307. ;; properties to be named as special properties.
  14308. (while (re-search-forward org-property-re end t)
  14309. (let* ((key (upcase (org-match-string-no-properties 2)))
  14310. (extendp (org-string-match-p "\\+\\'" key))
  14311. (key-base (if extendp (substring key 0 -1) key))
  14312. (value (org-match-string-no-properties 3)))
  14313. (cond
  14314. ((member-ignore-case key-base org-special-properties))
  14315. (extendp
  14316. (setq props
  14317. (org--update-property-plist key value props)))
  14318. ((member key seen-base))
  14319. (t (push key seen-base)
  14320. (let ((p (assoc-string key props t)))
  14321. (if p (setcdr p (concat value " " (cdr p)))
  14322. (push (cons key value) props))))))))))))
  14323. (unless (assoc "CATEGORY" props)
  14324. (push (cons "CATEGORY" (org-get-category beg)) props)
  14325. (when (string= specific "CATEGORY") (throw 'exit props)))
  14326. ;; Return value.
  14327. (append (get-text-property beg 'org-summaries) props))))))
  14328. (defun org-entry-get (pom property &optional inherit literal-nil)
  14329. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14330. If INHERIT is non-nil and the entry does not have the property,
  14331. then also check higher levels of the hierarchy. If INHERIT is
  14332. the symbol `selective', use inheritance only if the setting in
  14333. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14334. If the property is present but empty, the return value is the
  14335. empty string. If the property is not present at all, nil is
  14336. returned. In any other case, return the value as a string.
  14337. Search is case-insensitive.
  14338. If LITERAL-NIL is set, return the string value \"nil\" as
  14339. a string, do not interpret it as the list atom nil. This is used
  14340. for inheritance when a \"nil\" value can supersede a non-nil
  14341. value higher up the hierarchy."
  14342. (org-with-point-at pom
  14343. (cond
  14344. ((and inherit
  14345. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14346. (org-entry-get-with-inheritance property literal-nil))
  14347. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14348. ;; We need a special property. Use `org-entry-properties' to
  14349. ;; retrieve it, but specify the wanted property.
  14350. (cdr (assoc-string property (org-entry-properties nil property))))
  14351. (t
  14352. (let ((range (org-get-property-block)))
  14353. (when range
  14354. (let* ((case-fold-search t)
  14355. (end (cdr range))
  14356. (props
  14357. (let ((global
  14358. (or (assoc-string property org-file-properties t)
  14359. (assoc-string property org-global-properties t)
  14360. (assoc-string
  14361. property org-global-properties-fixed t))))
  14362. ;; Make sure to not re-use GLOBAL as
  14363. ;; `org--update-property-plist' would alter it by
  14364. ;; side-effect.
  14365. (and global (list (cons property (cdr global))))))
  14366. (find-value
  14367. (lambda (key)
  14368. (when (re-search-forward (org-re-property key nil t) end t)
  14369. (setq props
  14370. (org--update-property-plist
  14371. key (org-match-string-no-properties 3) props))))))
  14372. (goto-char (car range))
  14373. ;; Find base value.
  14374. (save-excursion (funcall find-value property))
  14375. ;; Find additional values.
  14376. (let ((property+ (concat property "+")))
  14377. (while (funcall find-value property+)))
  14378. ;; Return final value.
  14379. (let ((val (cdr (assoc-string property props t))))
  14380. (if literal-nil val (org-not-nil val))))))))))
  14381. (defun org-property-or-variable-value (var &optional inherit)
  14382. "Check if there is a property fixing the value of VAR.
  14383. If yes, return this value. If not, return the current value of the variable."
  14384. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14385. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14386. (read prop)
  14387. (symbol-value var))))
  14388. (defun org-entry-delete (pom property)
  14389. "Delete the property PROPERTY from entry at point-or-marker POM."
  14390. (unless (member property org-special-properties)
  14391. (org-with-point-at pom
  14392. (let ((range (org-get-property-block)))
  14393. (when range
  14394. (let ((begin (car range))
  14395. (end (copy-marker (cdr range))))
  14396. (goto-char begin)
  14397. (when (re-search-forward (org-re-property property nil t) end t)
  14398. (delete-region (match-beginning 0) (line-beginning-position 2))
  14399. ;; If drawer is empty, remove it altogether.
  14400. (when (= begin end)
  14401. (delete-region (line-beginning-position 0)
  14402. (line-beginning-position 2)))
  14403. (set-marker end nil))))))))
  14404. ;; Multi-values properties are properties that contain multiple values
  14405. ;; These values are assumed to be single words, separated by whitespace.
  14406. (defun org-entry-add-to-multivalued-property (pom property value)
  14407. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14408. (let* ((old (org-entry-get pom property))
  14409. (values (and old (org-split-string old "[ \t]"))))
  14410. (setq value (org-entry-protect-space value))
  14411. (unless (member value values)
  14412. (setq values (append values (list value)))
  14413. (org-entry-put pom property
  14414. (mapconcat 'identity values " ")))))
  14415. (defun org-entry-remove-from-multivalued-property (pom property value)
  14416. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14417. (let* ((old (org-entry-get pom property))
  14418. (values (and old (org-split-string old "[ \t]"))))
  14419. (setq value (org-entry-protect-space value))
  14420. (when (member value values)
  14421. (setq values (delete value values))
  14422. (org-entry-put pom property
  14423. (mapconcat 'identity values " ")))))
  14424. (defun org-entry-member-in-multivalued-property (pom property value)
  14425. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14426. (let* ((old (org-entry-get pom property))
  14427. (values (and old (org-split-string old "[ \t]"))))
  14428. (setq value (org-entry-protect-space value))
  14429. (member value values)))
  14430. (defun org-entry-get-multivalued-property (pom property)
  14431. "Return a list of values in a multivalued property."
  14432. (let* ((value (org-entry-get pom property))
  14433. (values (and value (org-split-string value "[ \t]"))))
  14434. (mapcar 'org-entry-restore-space values)))
  14435. (defun org-entry-put-multivalued-property (pom property &rest values)
  14436. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14437. VALUES should be a list of strings. Spaces will be protected."
  14438. (org-entry-put pom property
  14439. (mapconcat 'org-entry-protect-space values " "))
  14440. (let* ((value (org-entry-get pom property))
  14441. (values (and value (org-split-string value "[ \t]"))))
  14442. (mapcar 'org-entry-restore-space values)))
  14443. (defun org-entry-protect-space (s)
  14444. "Protect spaces and newline in string S."
  14445. (while (string-match " " s)
  14446. (setq s (replace-match "%20" t t s)))
  14447. (while (string-match "\n" s)
  14448. (setq s (replace-match "%0A" t t s)))
  14449. s)
  14450. (defun org-entry-restore-space (s)
  14451. "Restore spaces and newline in string S."
  14452. (while (string-match "%20" s)
  14453. (setq s (replace-match " " t t s)))
  14454. (while (string-match "%0A" s)
  14455. (setq s (replace-match "\n" t t s)))
  14456. s)
  14457. (defvar org-entry-property-inherited-from (make-marker)
  14458. "Marker pointing to the entry from where a property was inherited.
  14459. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14460. location of the entry where the inheritance search matched. If there was
  14461. no match, the marker will point nowhere.
  14462. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14463. is set.")
  14464. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14465. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14466. The search will stop at the first ancestor which has the property defined.
  14467. If the value found is \"nil\", return nil to show that the property
  14468. should be considered as undefined (this is the meaning of nil here).
  14469. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14470. (move-marker org-entry-property-inherited-from nil)
  14471. (let (value)
  14472. (org-with-wide-buffer
  14473. (catch 'exit
  14474. (while t
  14475. (when (setq value (org-entry-get nil property nil literal-nil))
  14476. (org-back-to-heading t)
  14477. (move-marker org-entry-property-inherited-from (point))
  14478. (throw 'exit nil))
  14479. (or (org-up-heading-safe) (throw 'exit nil)))))
  14480. (unless value
  14481. (setq value
  14482. (cdr (or (assoc-string property org-file-properties t)
  14483. (assoc-string property org-global-properties t)
  14484. (assoc-string property org-global-properties-fixed t)))))
  14485. (if literal-nil value (org-not-nil value))))
  14486. (defvar org-property-changed-functions nil
  14487. "Hook called when the value of a property has changed.
  14488. Each hook function should accept two arguments, the name of the property
  14489. and the new value.")
  14490. (defun org-entry-put (pom property value)
  14491. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14492. If the value is `nil', it is converted to the empty string. If
  14493. it is not a string, an error is raised.
  14494. PROPERTY can be any regular property (see
  14495. `org-special-properties'). It can also be \"TODO\",
  14496. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14497. For the last two properties, VALUE may have any of the special
  14498. values \"earlier\" and \"later\". The function then increases or
  14499. decreases scheduled or deadline date by one day."
  14500. (cond ((null value) (setq value ""))
  14501. ((not (stringp value)) (error "Properties values should be strings")))
  14502. (org-with-point-at pom
  14503. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14504. (org-back-to-heading t)
  14505. (org-with-limited-levels (org-back-to-heading t)))
  14506. (let ((beg (point)))
  14507. (cond
  14508. ((equal property "TODO")
  14509. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14510. ((not (member value org-todo-keywords-1))
  14511. (user-error "\"%s\" is not a valid TODO state" value)))
  14512. (org-todo value)
  14513. (org-set-tags nil 'align))
  14514. ((equal property "PRIORITY")
  14515. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14516. (org-set-tags nil 'align))
  14517. ((equal property "SCHEDULED")
  14518. (forward-line)
  14519. (if (and (org-looking-at-p org-planning-line-re)
  14520. (re-search-forward
  14521. org-scheduled-time-regexp (line-end-position) t))
  14522. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14523. ((string= value "later") (org-timestamp-change 1 'day))
  14524. ((string= value "") (org-schedule '(4)))
  14525. (t (org-schedule nil value)))
  14526. (if (member value '("earlier" "later" ""))
  14527. (call-interactively #'org-schedule)
  14528. (org-schedule nil value))))
  14529. ((equal property "DEADLINE")
  14530. (forward-line)
  14531. (if (and (org-looking-at-p org-planning-line-re)
  14532. (re-search-forward
  14533. org-deadline-time-regexp (line-end-position) t))
  14534. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14535. ((string= value "later") (org-timestamp-change 1 'day))
  14536. ((string= value "") (org-deadline '(4)))
  14537. (t (org-deadline nil value)))
  14538. (if (member value '("earlier" "later" ""))
  14539. (call-interactively #'org-deadline)
  14540. (org-deadline nil value))))
  14541. ((member property org-special-properties)
  14542. (error "The %s property cannot be set with `org-entry-put'" property))
  14543. (t
  14544. (let* ((range (org-get-property-block beg 'force))
  14545. (end (cdr range))
  14546. (case-fold-search t))
  14547. (goto-char (car range))
  14548. (if (re-search-forward (org-re-property property nil t) end t)
  14549. (progn (delete-region (match-beginning 0) (match-end 0))
  14550. (goto-char (match-beginning 0)))
  14551. (goto-char end)
  14552. (insert "\n")
  14553. (backward-char))
  14554. (insert ":" property ":")
  14555. (when value (insert " " value))
  14556. (org-indent-line)))))
  14557. (run-hook-with-args 'org-property-changed-functions property value)))
  14558. (defun org-buffer-property-keys (&optional specials defaults columns)
  14559. "Get all property keys in the current buffer.
  14560. When SPECIALS is non-nil, also list the special properties that
  14561. reflect things like tags and TODO state.
  14562. When DEFAULTS is non-nil, also include properties that has
  14563. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14564. DESCRIPTION, LOCATION, and LOGGING and others.
  14565. When COLUMNS in non-nil, also include property names given in
  14566. COLUMN formats in the current buffer."
  14567. (let ((case-fold-search t)
  14568. (props (append
  14569. (and specials org-special-properties)
  14570. (and defaults (cons org-effort-property org-default-properties))
  14571. nil)))
  14572. (org-with-wide-buffer
  14573. (goto-char (point-min))
  14574. (while (re-search-forward org-property-start-re nil t)
  14575. (let ((range (org-get-property-block)))
  14576. (catch 'skip
  14577. (unless range
  14578. (when (and (not (org-before-first-heading-p))
  14579. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14580. (line-beginning-position))))
  14581. (org-get-property-block nil t))
  14582. (throw 'skip nil))
  14583. (goto-char (car range))
  14584. (let ((begin (car range))
  14585. (end (cdr range)))
  14586. ;; Make sure that found property block is not located
  14587. ;; before current point, as it would generate an infloop.
  14588. ;; It can happen, for example, in the following
  14589. ;; situation:
  14590. ;;
  14591. ;; * Headline
  14592. ;; :PROPERTIES:
  14593. ;; ...
  14594. ;; :END:
  14595. ;; *************** Inlinetask
  14596. ;; #+BEGIN_EXAMPLE
  14597. ;; :PROPERTIES:
  14598. ;; #+END_EXAMPLE
  14599. ;;
  14600. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14601. (while (< (point) end)
  14602. (let ((p (progn (looking-at org-property-re)
  14603. (org-match-string-no-properties 2))))
  14604. ;; Only add true property name, not extension symbol.
  14605. (add-to-list 'props
  14606. (if (not (org-string-match-p "\\+\\'" p)) p
  14607. (substring p 0 -1))))
  14608. (forward-line))))
  14609. (outline-next-heading)))
  14610. (when columns
  14611. (goto-char (point-min))
  14612. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14613. (let ((element (org-element-at-point)))
  14614. (when (memq (org-element-type element) '(keyword node-property))
  14615. (let ((value (org-element-property :value element))
  14616. (start 0))
  14617. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14618. (setq start (match-end 0))
  14619. (let ((p (org-match-string-no-properties 1 value)))
  14620. (unless (member-ignore-case p org-special-properties)
  14621. (add-to-list 'props p))))))))))
  14622. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14623. (defun org-property-values (key)
  14624. "List all non-nil values of property KEY in current buffer."
  14625. (org-with-wide-buffer
  14626. (goto-char (point-min))
  14627. (let ((case-fold-search t)
  14628. (re (org-re-property key))
  14629. values)
  14630. (while (re-search-forward re nil t)
  14631. (add-to-list 'values (org-entry-get (point) key)))
  14632. values)))
  14633. (defun org-insert-property-drawer ()
  14634. "Insert a property drawer into the current entry."
  14635. (org-with-wide-buffer
  14636. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14637. (org-back-to-heading t)
  14638. (org-with-limited-levels (org-back-to-heading t)))
  14639. (forward-line)
  14640. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14641. (unless (org-looking-at-p org-property-drawer-re)
  14642. (let ((inhibit-read-only t))
  14643. (unless (bolp) (insert "\n"))
  14644. (let ((begin (point)))
  14645. (insert ":PROPERTIES:\n:END:\n")
  14646. (org-indent-region begin (point)))))))
  14647. (defun org-insert-drawer (&optional arg drawer)
  14648. "Insert a drawer at point.
  14649. When optional argument ARG is non-nil, insert a property drawer.
  14650. Optional argument DRAWER, when non-nil, is a string representing
  14651. drawer's name. Otherwise, the user is prompted for a name.
  14652. If a region is active, insert the drawer around that region
  14653. instead.
  14654. Point is left between drawer's boundaries."
  14655. (interactive "P")
  14656. (let* ((drawer (if arg "PROPERTIES"
  14657. (or drawer (read-from-minibuffer "Drawer: ")))))
  14658. (cond
  14659. ;; With C-u, fall back on `org-insert-property-drawer'
  14660. (arg (org-insert-property-drawer))
  14661. ;; Check validity of suggested drawer's name.
  14662. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14663. (user-error "Invalid drawer name"))
  14664. ;; With an active region, insert a drawer at point.
  14665. ((not (org-region-active-p))
  14666. (progn
  14667. (unless (bolp) (insert "\n"))
  14668. (insert (format ":%s:\n\n:END:\n" drawer))
  14669. (forward-line -2)))
  14670. ;; Otherwise, insert the drawer at point
  14671. (t
  14672. (let ((rbeg (region-beginning))
  14673. (rend (copy-marker (region-end))))
  14674. (unwind-protect
  14675. (progn
  14676. (goto-char rbeg)
  14677. (beginning-of-line)
  14678. (when (save-excursion
  14679. (re-search-forward org-outline-regexp-bol rend t))
  14680. (user-error "Drawers cannot contain headlines"))
  14681. ;; Position point at the beginning of the first
  14682. ;; non-blank line in region. Insert drawer's opening
  14683. ;; there, then indent it.
  14684. (org-skip-whitespace)
  14685. (beginning-of-line)
  14686. (insert ":" drawer ":\n")
  14687. (forward-line -1)
  14688. (indent-for-tab-command)
  14689. ;; Move point to the beginning of the first blank line
  14690. ;; after the last non-blank line in region. Insert
  14691. ;; drawer's closing, then indent it.
  14692. (goto-char rend)
  14693. (skip-chars-backward " \r\t\n")
  14694. (insert "\n:END:")
  14695. (deactivate-mark t)
  14696. (indent-for-tab-command)
  14697. (unless (eolp) (insert "\n")))
  14698. ;; Clear marker, whatever the outcome of insertion is.
  14699. (set-marker rend nil)))))))
  14700. (defvar org-property-set-functions-alist nil
  14701. "Property set function alist.
  14702. Each entry should have the following format:
  14703. (PROPERTY . READ-FUNCTION)
  14704. The read function will be called with the same argument as
  14705. `org-completing-read'.")
  14706. (defun org-set-property-function (property)
  14707. "Get the function that should be used to set PROPERTY.
  14708. This is computed according to `org-property-set-functions-alist'."
  14709. (or (cdr (assoc property org-property-set-functions-alist))
  14710. 'org-completing-read))
  14711. (defun org-read-property-value (property)
  14712. "Read PROPERTY value from user."
  14713. (let* ((completion-ignore-case t)
  14714. (allowed (org-property-get-allowed-values nil property 'table))
  14715. (cur (org-entry-get nil property))
  14716. (prompt (concat property " value"
  14717. (if (and cur (string-match "\\S-" cur))
  14718. (concat " [" cur "]") "") ": "))
  14719. (set-function (org-set-property-function property))
  14720. (val (if allowed
  14721. (funcall set-function prompt allowed nil
  14722. (not (get-text-property 0 'org-unrestricted
  14723. (caar allowed))))
  14724. (let (org-completion-use-ido org-completion-use-iswitchb)
  14725. (funcall set-function prompt
  14726. (mapcar 'list (org-property-values property))
  14727. nil nil "" nil cur)))))
  14728. (org-trim val)))
  14729. (defvar org-last-set-property nil)
  14730. (defvar org-last-set-property-value nil)
  14731. (defun org-read-property-name ()
  14732. "Read a property name."
  14733. (let* ((completion-ignore-case t)
  14734. (keys (org-buffer-property-keys nil t t))
  14735. (default-prop (or (save-excursion
  14736. (save-match-data
  14737. (beginning-of-line)
  14738. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14739. (null (string= (match-string 1) "END"))
  14740. (match-string 1))))
  14741. org-last-set-property))
  14742. (property (org-icompleting-read
  14743. (concat "Property"
  14744. (if default-prop (concat " [" default-prop "]") "")
  14745. ": ")
  14746. (mapcar 'list keys)
  14747. nil nil nil nil
  14748. default-prop)))
  14749. (if (member property keys)
  14750. property
  14751. (or (cdr (assoc (downcase property)
  14752. (mapcar (lambda (x) (cons (downcase x) x))
  14753. keys)))
  14754. property))))
  14755. (defun org-set-property-and-value (use-last)
  14756. "Allow to set [PROPERTY]: [value] direction from prompt.
  14757. When use-default, don't even ask, just use the last
  14758. \"[PROPERTY]: [value]\" string from the history."
  14759. (interactive "P")
  14760. (let* ((completion-ignore-case t)
  14761. (pv (or (and use-last org-last-set-property-value)
  14762. (org-completing-read
  14763. "Enter a \"[Property]: [value]\" pair: "
  14764. nil nil nil nil nil
  14765. org-last-set-property-value)))
  14766. prop val)
  14767. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14768. (setq prop (match-string 1 pv)
  14769. val (match-string 2 pv))
  14770. (org-set-property prop val))))
  14771. (defun org-set-property (property value)
  14772. "In the current entry, set PROPERTY to VALUE.
  14773. When called interactively, this will prompt for a property name, offering
  14774. completion on existing and default properties. And then it will prompt
  14775. for a value, offering completion either on allowed values (via an inherited
  14776. xxx_ALL property) or on existing values in other instances of this property
  14777. in the current file."
  14778. (interactive (list nil nil))
  14779. (let* ((property (or property (org-read-property-name)))
  14780. (value (or value (org-read-property-value property)))
  14781. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14782. (setq org-last-set-property property)
  14783. (setq org-last-set-property-value (concat property ": " value))
  14784. ;; Possibly postprocess the inserted value:
  14785. (when fn (setq value (funcall fn value)))
  14786. (unless (equal (org-entry-get nil property) value)
  14787. (org-entry-put nil property value))))
  14788. (defun org-delete-property (property)
  14789. "In the current entry, delete PROPERTY."
  14790. (interactive
  14791. (let* ((completion-ignore-case t)
  14792. (cat (org-entry-get (point) "CATEGORY"))
  14793. (props0 (org-entry-properties nil 'standard))
  14794. (props (if cat props0
  14795. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14796. (prop (if (< 1 (length props))
  14797. (org-icompleting-read "Property: " props nil t)
  14798. (caar props))))
  14799. (list prop)))
  14800. (if (not property)
  14801. (message "No property to delete in this entry")
  14802. (org-entry-delete nil property)
  14803. (message "Property \"%s\" deleted" property)))
  14804. (defun org-delete-property-globally (property)
  14805. "Remove PROPERTY globally, from all entries."
  14806. (interactive
  14807. (let* ((completion-ignore-case t)
  14808. (prop (org-icompleting-read
  14809. "Globally remove property: "
  14810. (mapcar 'list (org-buffer-property-keys)))))
  14811. (list prop)))
  14812. (save-excursion
  14813. (save-restriction
  14814. (widen)
  14815. (goto-char (point-min))
  14816. (let ((cnt 0))
  14817. (while (re-search-forward
  14818. (org-re-property property)
  14819. nil t)
  14820. (setq cnt (1+ cnt))
  14821. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14822. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14823. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14824. (defun org-compute-property-at-point ()
  14825. "Compute the property at point.
  14826. This looks for an enclosing column format, extracts the operator and
  14827. then applies it to the property in the column format's scope."
  14828. (interactive)
  14829. (unless (org-at-property-p)
  14830. (user-error "Not at a property"))
  14831. (let ((prop (org-match-string-no-properties 2)))
  14832. (org-columns-get-format-and-top-level)
  14833. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14834. (user-error "No operator defined for property %s" prop))
  14835. (org-columns-compute prop)))
  14836. (defvar org-property-allowed-value-functions nil
  14837. "Hook for functions supplying allowed values for a specific property.
  14838. The functions must take a single argument, the name of the property, and
  14839. return a flat list of allowed values. If \":ETC\" is one of
  14840. the values, this means that these values are intended as defaults for
  14841. completion, but that other values should be allowed too.
  14842. The functions must return nil if they are not responsible for this
  14843. property.")
  14844. (defun org-property-get-allowed-values (pom property &optional table)
  14845. "Get allowed values for the property PROPERTY.
  14846. When TABLE is non-nil, return an alist that can directly be used for
  14847. completion."
  14848. (let (vals)
  14849. (cond
  14850. ((equal property "TODO")
  14851. (setq vals (org-with-point-at pom
  14852. (append org-todo-keywords-1 '("")))))
  14853. ((equal property "PRIORITY")
  14854. (let ((n org-lowest-priority))
  14855. (while (>= n org-highest-priority)
  14856. (push (char-to-string n) vals)
  14857. (setq n (1- n)))))
  14858. ((equal property "CATEGORY"))
  14859. ((member property org-special-properties))
  14860. ((setq vals (run-hook-with-args-until-success
  14861. 'org-property-allowed-value-functions property)))
  14862. (t
  14863. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14864. (when (and vals (string-match "\\S-" vals))
  14865. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14866. (setq vals (mapcar (lambda (x)
  14867. (cond ((stringp x) x)
  14868. ((numberp x) (number-to-string x))
  14869. ((symbolp x) (symbol-name x))
  14870. (t "???")))
  14871. vals)))))
  14872. (when (member ":ETC" vals)
  14873. (setq vals (remove ":ETC" vals))
  14874. (org-add-props (car vals) '(org-unrestricted t)))
  14875. (if table (mapcar 'list vals) vals)))
  14876. (defun org-property-previous-allowed-value (&optional previous)
  14877. "Switch to the next allowed value for this property."
  14878. (interactive)
  14879. (org-property-next-allowed-value t))
  14880. (defun org-property-next-allowed-value (&optional previous)
  14881. "Switch to the next allowed value for this property."
  14882. (interactive)
  14883. (unless (org-at-property-p)
  14884. (user-error "Not at a property"))
  14885. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14886. (key (match-string 2))
  14887. (value (match-string 3))
  14888. (allowed (or (org-property-get-allowed-values (point) key)
  14889. (and (member value '("[ ]" "[-]" "[X]"))
  14890. '("[ ]" "[X]"))))
  14891. (heading (save-match-data (nth 4 (org-heading-components))))
  14892. nval)
  14893. (unless allowed
  14894. (user-error "Allowed values for this property have not been defined"))
  14895. (if previous (setq allowed (reverse allowed)))
  14896. (if (member value allowed)
  14897. (setq nval (car (cdr (member value allowed)))))
  14898. (setq nval (or nval (car allowed)))
  14899. (if (equal nval value)
  14900. (user-error "Only one allowed value for this property"))
  14901. (org-at-property-p)
  14902. (replace-match (concat " :" key ": " nval) t t)
  14903. (org-indent-line)
  14904. (beginning-of-line 1)
  14905. (skip-chars-forward " \t")
  14906. (when (equal prop org-effort-property)
  14907. (org-refresh-property
  14908. '((effort . identity)
  14909. (effort-minutes . org-duration-string-to-minutes))
  14910. nval)
  14911. (when (string= org-clock-current-task heading)
  14912. (setq org-clock-effort nval)
  14913. (org-clock-update-mode-line)))
  14914. (run-hook-with-args 'org-property-changed-functions key nval)))
  14915. (defun org-find-olp (path &optional this-buffer)
  14916. "Return a marker pointing to the entry at outline path OLP.
  14917. If anything goes wrong, throw an error.
  14918. You can wrap this call to catch the error like this:
  14919. (condition-case msg
  14920. (org-mobile-locate-entry (match-string 4))
  14921. (error (nth 1 msg)))
  14922. The return value will then be either a string with the error message,
  14923. or a marker if everything is OK.
  14924. If THIS-BUFFER is set, the outline path does not contain a file,
  14925. only headings."
  14926. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14927. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14928. (level 1)
  14929. (lmin 1)
  14930. (lmax 1)
  14931. limit re end found pos heading cnt flevel)
  14932. (unless buffer (error "File not found :%s" file))
  14933. (with-current-buffer buffer
  14934. (save-excursion
  14935. (save-restriction
  14936. (widen)
  14937. (setq limit (point-max))
  14938. (goto-char (point-min))
  14939. (while (setq heading (pop path))
  14940. (setq re (format org-complex-heading-regexp-format
  14941. (regexp-quote heading)))
  14942. (setq cnt 0 pos (point))
  14943. (while (re-search-forward re end t)
  14944. (setq level (- (match-end 1) (match-beginning 1)))
  14945. (if (and (>= level lmin) (<= level lmax))
  14946. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14947. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14948. lmax heading))
  14949. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14950. lmax heading))
  14951. (goto-char found)
  14952. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14953. (setq end (save-excursion (org-end-of-subtree t t))))
  14954. (when (org-at-heading-p)
  14955. (point-marker)))))))
  14956. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14957. "Find node HEADING in BUFFER.
  14958. Return a marker to the heading if it was found, or nil if not.
  14959. If POS-ONLY is set, return just the position instead of a marker.
  14960. The heading text must match exact, but it may have a TODO keyword,
  14961. a priority cookie and tags in the standard locations."
  14962. (with-current-buffer (or buffer (current-buffer))
  14963. (save-excursion
  14964. (save-restriction
  14965. (widen)
  14966. (goto-char (point-min))
  14967. (let (case-fold-search)
  14968. (if (re-search-forward
  14969. (format org-complex-heading-regexp-format
  14970. (regexp-quote heading)) nil t)
  14971. (if pos-only
  14972. (match-beginning 0)
  14973. (move-marker (make-marker) (match-beginning 0)))))))))
  14974. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14975. "Find Org node headline HEADING in all .org files in directory DIR.
  14976. When the target headline is found, return a marker to this location."
  14977. (let ((files (directory-files (or dir default-directory)
  14978. t "\\`[^.#].*\\.org\\'"))
  14979. file visiting m buffer)
  14980. (catch 'found
  14981. (while (setq file (pop files))
  14982. (message "trying %s" file)
  14983. (setq visiting (org-find-base-buffer-visiting file))
  14984. (setq buffer (or visiting (find-file-noselect file)))
  14985. (setq m (org-find-exact-headline-in-buffer
  14986. heading buffer))
  14987. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14988. (and m (throw 'found m))))))
  14989. (defun org-find-entry-with-id (ident)
  14990. "Locate the entry that contains the ID property with exact value IDENT.
  14991. IDENT can be a string, a symbol or a number, this function will search for
  14992. the string representation of it.
  14993. Return the position where this entry starts, or nil if there is no such entry."
  14994. (interactive "sID: ")
  14995. (let ((id (cond
  14996. ((stringp ident) ident)
  14997. ((symbol-name ident) (symbol-name ident))
  14998. ((numberp ident) (number-to-string ident))
  14999. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15000. (case-fold-search nil))
  15001. (save-excursion
  15002. (save-restriction
  15003. (widen)
  15004. (goto-char (point-min))
  15005. (when (re-search-forward
  15006. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15007. nil t)
  15008. (org-back-to-heading t)
  15009. (point))))))
  15010. ;;;; Timestamps
  15011. (defvar org-last-changed-timestamp nil)
  15012. (defvar org-last-inserted-timestamp nil
  15013. "The last time stamp inserted with `org-insert-time-stamp'.")
  15014. (defvar org-ts-what) ; dynamically scoped parameter
  15015. (defun org-time-stamp (arg &optional inactive)
  15016. "Prompt for a date/time and insert a time stamp.
  15017. If the user specifies a time like HH:MM or if this command is
  15018. called with at least one prefix argument, the time stamp contains
  15019. the date and the time. Otherwise, only the date is included.
  15020. All parts of a date not specified by the user are filled in from
  15021. the timestamp at point, if any, or the current date/time
  15022. otherwise.
  15023. If there is already a timestamp at the cursor, it is replaced.
  15024. With two universal prefix arguments, insert an active timestamp
  15025. with the current time without prompting the user.
  15026. When called from lisp, the timestamp is inactive if INACTIVE is
  15027. non-nil."
  15028. (interactive "P")
  15029. (let* ((ts
  15030. (cond ((org-at-date-range-p t)
  15031. (save-excursion
  15032. (goto-char (match-beginning 0))
  15033. (looking-at (if inactive org-ts-regexp-both org-ts-regexp)))
  15034. (match-string 0))
  15035. ((org-at-timestamp-p t) (match-string 0))))
  15036. ;; Default time is either the timestamp at point or today.
  15037. ;; When entering a range, only the range start is considered.
  15038. (default-time (if (not ts) (current-time)
  15039. (apply #'encode-time (org-parse-time-string ts))))
  15040. (default-input (and ts (org-get-compact-tod ts)))
  15041. (repeater (and ts
  15042. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15043. (match-string 0 ts)))
  15044. org-time-was-given
  15045. org-end-time-was-given
  15046. (time
  15047. (and (if (equal arg '(16)) (current-time)
  15048. ;; Preserve `this-command' and `last-command'.
  15049. (let ((this-command this-command)
  15050. (last-command last-command))
  15051. (org-read-date
  15052. arg 'totime nil nil default-time default-input
  15053. inactive))))))
  15054. (cond
  15055. ((and ts
  15056. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15057. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15058. (insert "--")
  15059. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15060. (ts
  15061. ;; Make sure we're on a timestamp. When in the middle of a date
  15062. ;; range, move arbitrarily to range end.
  15063. (unless (org-at-timestamp-p t)
  15064. (skip-chars-forward "-")
  15065. (org-at-timestamp-p t))
  15066. (replace-match "")
  15067. (setq org-last-changed-timestamp
  15068. (org-insert-time-stamp
  15069. time (or org-time-was-given arg)
  15070. inactive nil nil (list org-end-time-was-given)))
  15071. (when repeater
  15072. (backward-char)
  15073. (insert " " repeater)
  15074. (setq org-last-changed-timestamp
  15075. (concat (substring org-last-inserted-timestamp 0 -1)
  15076. " " repeater ">")))
  15077. (message "Timestamp updated"))
  15078. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15079. (t (org-insert-time-stamp
  15080. time (or org-time-was-given arg) inactive nil nil
  15081. (list org-end-time-was-given))))))
  15082. ;; FIXME: can we use this for something else, like computing time differences?
  15083. (defun org-get-compact-tod (s)
  15084. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15085. (let* ((t1 (match-string 1 s))
  15086. (h1 (string-to-number (match-string 2 s)))
  15087. (m1 (string-to-number (match-string 3 s)))
  15088. (t2 (and (match-end 4) (match-string 5 s)))
  15089. (h2 (and t2 (string-to-number (match-string 6 s))))
  15090. (m2 (and t2 (string-to-number (match-string 7 s))))
  15091. dh dm)
  15092. (if (not t2)
  15093. t1
  15094. (setq dh (- h2 h1) dm (- m2 m1))
  15095. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15096. (concat t1 "+" (number-to-string dh)
  15097. (and (/= 0 dm) (format ":%02d" dm)))))))
  15098. (defun org-time-stamp-inactive (&optional arg)
  15099. "Insert an inactive time stamp.
  15100. An inactive time stamp is enclosed in square brackets instead of angle
  15101. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15102. does not link to the calendar and cannot be changed with the S-cursor keys.
  15103. So these are more for recording a certain time/date."
  15104. (interactive "P")
  15105. (org-time-stamp arg 'inactive))
  15106. (defvar org-date-ovl (make-overlay 1 1))
  15107. (overlay-put org-date-ovl 'face 'org-date-selected)
  15108. (org-detach-overlay org-date-ovl)
  15109. (defvar org-ans1) ; dynamically scoped parameter
  15110. (defvar org-ans2) ; dynamically scoped parameter
  15111. (defvar org-plain-time-of-day-regexp) ; defined below
  15112. (defvar org-overriding-default-time nil) ; dynamically scoped
  15113. (defvar org-read-date-overlay nil)
  15114. (defvar org-dcst nil) ; dynamically scoped
  15115. (defvar org-read-date-history nil)
  15116. (defvar org-read-date-final-answer nil)
  15117. (defvar org-read-date-analyze-futurep nil)
  15118. (defvar org-read-date-analyze-forced-year nil)
  15119. (defvar org-read-date-inactive)
  15120. (defvar org-read-date-minibuffer-local-map
  15121. (let* ((map (make-sparse-keymap)))
  15122. (set-keymap-parent map minibuffer-local-map)
  15123. (org-defkey map (kbd ".")
  15124. (lambda () (interactive)
  15125. ;; Are we at the beginning of the prompt?
  15126. (if (looking-back "^[^:]+: ")
  15127. (org-eval-in-calendar '(calendar-goto-today))
  15128. (insert "."))))
  15129. (org-defkey map (kbd "C-.")
  15130. (lambda () (interactive)
  15131. (org-eval-in-calendar '(calendar-goto-today))))
  15132. (org-defkey map [(meta shift left)]
  15133. (lambda () (interactive)
  15134. (org-eval-in-calendar '(calendar-backward-month 1))))
  15135. (org-defkey map [(meta shift right)]
  15136. (lambda () (interactive)
  15137. (org-eval-in-calendar '(calendar-forward-month 1))))
  15138. (org-defkey map [(meta shift up)]
  15139. (lambda () (interactive)
  15140. (org-eval-in-calendar '(calendar-backward-year 1))))
  15141. (org-defkey map [(meta shift down)]
  15142. (lambda () (interactive)
  15143. (org-eval-in-calendar '(calendar-forward-year 1))))
  15144. (org-defkey map [?\e (shift left)]
  15145. (lambda () (interactive)
  15146. (org-eval-in-calendar '(calendar-backward-month 1))))
  15147. (org-defkey map [?\e (shift right)]
  15148. (lambda () (interactive)
  15149. (org-eval-in-calendar '(calendar-forward-month 1))))
  15150. (org-defkey map [?\e (shift up)]
  15151. (lambda () (interactive)
  15152. (org-eval-in-calendar '(calendar-backward-year 1))))
  15153. (org-defkey map [?\e (shift down)]
  15154. (lambda () (interactive)
  15155. (org-eval-in-calendar '(calendar-forward-year 1))))
  15156. (org-defkey map [(shift up)]
  15157. (lambda () (interactive)
  15158. (org-eval-in-calendar '(calendar-backward-week 1))))
  15159. (org-defkey map [(shift down)]
  15160. (lambda () (interactive)
  15161. (org-eval-in-calendar '(calendar-forward-week 1))))
  15162. (org-defkey map [(shift left)]
  15163. (lambda () (interactive)
  15164. (org-eval-in-calendar '(calendar-backward-day 1))))
  15165. (org-defkey map [(shift right)]
  15166. (lambda () (interactive)
  15167. (org-eval-in-calendar '(calendar-forward-day 1))))
  15168. (org-defkey map "!"
  15169. (lambda () (interactive)
  15170. (org-eval-in-calendar '(diary-view-entries))
  15171. (message "")))
  15172. (org-defkey map ">"
  15173. (lambda () (interactive)
  15174. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15175. (org-defkey map "<"
  15176. (lambda () (interactive)
  15177. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15178. (org-defkey map "\C-v"
  15179. (lambda () (interactive)
  15180. (org-eval-in-calendar
  15181. '(calendar-scroll-left-three-months 1))))
  15182. (org-defkey map "\M-v"
  15183. (lambda () (interactive)
  15184. (org-eval-in-calendar
  15185. '(calendar-scroll-right-three-months 1))))
  15186. map)
  15187. "Keymap for minibuffer commands when using `org-read-date'.")
  15188. (defvar org-def)
  15189. (defvar org-defdecode)
  15190. (defvar org-with-time)
  15191. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15192. default-time default-input inactive)
  15193. "Read a date, possibly a time, and make things smooth for the user.
  15194. The prompt will suggest to enter an ISO date, but you can also enter anything
  15195. which will at least partially be understood by `parse-time-string'.
  15196. Unrecognized parts of the date will default to the current day, month, year,
  15197. hour and minute. If this command is called to replace a timestamp at point,
  15198. or to enter the second timestamp of a range, the default time is taken
  15199. from the existing stamp. Furthermore, the command prefers the future,
  15200. so if you are giving a date where the year is not given, and the day-month
  15201. combination is already past in the current year, it will assume you
  15202. mean next year. For details, see the manual. A few examples:
  15203. 3-2-5 --> 2003-02-05
  15204. feb 15 --> currentyear-02-15
  15205. 2/15 --> currentyear-02-15
  15206. sep 12 9 --> 2009-09-12
  15207. 12:45 --> today 12:45
  15208. 22 sept 0:34 --> currentyear-09-22 0:34
  15209. 12 --> currentyear-currentmonth-12
  15210. Fri --> nearest Friday after today
  15211. -Tue --> last Tuesday
  15212. etc.
  15213. Furthermore you can specify a relative date by giving, as the *first* thing
  15214. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15215. change in days weeks, months, years.
  15216. With a single plus or minus, the date is relative to today. With a double
  15217. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15218. +4d --> four days from today
  15219. +4 --> same as above
  15220. +2w --> two weeks from today
  15221. ++5 --> five days from default date
  15222. The function understands only English month and weekday abbreviations.
  15223. While prompting, a calendar is popped up - you can also select the
  15224. date with the mouse (button 1). The calendar shows a period of three
  15225. months. To scroll it to other months, use the keys `>' and `<'.
  15226. If you don't like the calendar, turn it off with
  15227. \(setq org-read-date-popup-calendar nil)
  15228. With optional argument TO-TIME, the date will immediately be converted
  15229. to an internal time.
  15230. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15231. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15232. still enter a time, and this function will inform the calling routine
  15233. about this change. The calling routine may then choose to change the
  15234. format used to insert the time stamp into the buffer to include the time.
  15235. With optional argument FROM-STRING, read from this string instead from
  15236. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15237. the time/date that is used for everything that is not specified by the
  15238. user."
  15239. (require 'parse-time)
  15240. (let* ((org-time-stamp-rounding-minutes
  15241. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15242. (org-dcst org-display-custom-times)
  15243. (ct (org-current-time))
  15244. (org-def (or org-overriding-default-time default-time ct))
  15245. (org-defdecode (decode-time org-def))
  15246. (dummy (progn
  15247. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15248. (setcar (nthcdr 2 org-defdecode) -1)
  15249. (setcar (nthcdr 1 org-defdecode) 59)
  15250. (setq org-def (apply 'encode-time org-defdecode)
  15251. org-defdecode (decode-time org-def)))))
  15252. (cur-frame (selected-frame))
  15253. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15254. (calendar-frame-setup nil)
  15255. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15256. (calendar-move-hook nil)
  15257. (calendar-view-diary-initially-flag nil)
  15258. (calendar-view-holidays-initially-flag nil)
  15259. (timestr (format-time-string
  15260. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15261. (prompt (concat (if prompt (concat prompt " ") "")
  15262. (format "Date+time [%s]: " timestr)))
  15263. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15264. (cond
  15265. (from-string (setq ans from-string))
  15266. (org-read-date-popup-calendar
  15267. (save-excursion
  15268. (save-window-excursion
  15269. (calendar)
  15270. (when (eq calendar-setup 'calendar-only)
  15271. (setq cal-frame
  15272. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15273. (select-frame cal-frame))
  15274. (org-eval-in-calendar '(setq cursor-type nil) t)
  15275. (unwind-protect
  15276. (progn
  15277. (calendar-forward-day (- (time-to-days org-def)
  15278. (calendar-absolute-from-gregorian
  15279. (calendar-current-date))))
  15280. (org-eval-in-calendar nil t)
  15281. (let* ((old-map (current-local-map))
  15282. (map (copy-keymap calendar-mode-map))
  15283. (minibuffer-local-map
  15284. (copy-keymap org-read-date-minibuffer-local-map)))
  15285. (org-defkey map (kbd "RET") 'org-calendar-select)
  15286. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15287. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15288. (unwind-protect
  15289. (progn
  15290. (use-local-map map)
  15291. (setq org-read-date-inactive inactive)
  15292. (add-hook 'post-command-hook 'org-read-date-display)
  15293. (setq org-ans0 (read-string prompt default-input
  15294. 'org-read-date-history nil))
  15295. ;; org-ans0: from prompt
  15296. ;; org-ans1: from mouse click
  15297. ;; org-ans2: from calendar motion
  15298. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15299. (remove-hook 'post-command-hook 'org-read-date-display)
  15300. (use-local-map old-map)
  15301. (when org-read-date-overlay
  15302. (delete-overlay org-read-date-overlay)
  15303. (setq org-read-date-overlay nil)))))
  15304. (bury-buffer "*Calendar*")
  15305. (when cal-frame
  15306. (delete-frame cal-frame)
  15307. (select-frame-set-input-focus cur-frame))))))
  15308. (t ; Naked prompt only
  15309. (unwind-protect
  15310. (setq ans (read-string prompt default-input
  15311. 'org-read-date-history timestr))
  15312. (when org-read-date-overlay
  15313. (delete-overlay org-read-date-overlay)
  15314. (setq org-read-date-overlay nil)))))
  15315. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15316. (when org-read-date-analyze-forced-year
  15317. (message "Year was forced into %s"
  15318. (if org-read-date-force-compatible-dates
  15319. "compatible range (1970-2037)"
  15320. "range representable on this machine"))
  15321. (ding))
  15322. ;; One round trip to get rid of 34th of August and stuff like that....
  15323. (setq final (decode-time (apply 'encode-time final)))
  15324. (setq org-read-date-final-answer ans)
  15325. (if to-time
  15326. (apply 'encode-time final)
  15327. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15328. (format "%04d-%02d-%02d %02d:%02d"
  15329. (nth 5 final) (nth 4 final) (nth 3 final)
  15330. (nth 2 final) (nth 1 final))
  15331. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15332. (defun org-read-date-display ()
  15333. "Display the current date prompt interpretation in the minibuffer."
  15334. (when org-read-date-display-live
  15335. (when org-read-date-overlay
  15336. (delete-overlay org-read-date-overlay))
  15337. (when (minibufferp (current-buffer))
  15338. (save-excursion
  15339. (end-of-line 1)
  15340. (while (not (equal (buffer-substring
  15341. (max (point-min) (- (point) 4)) (point))
  15342. " "))
  15343. (insert " ")))
  15344. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15345. " " (or org-ans1 org-ans2)))
  15346. (org-end-time-was-given nil)
  15347. (f (org-read-date-analyze ans org-def org-defdecode))
  15348. (fmts (if org-dcst
  15349. org-time-stamp-custom-formats
  15350. org-time-stamp-formats))
  15351. (fmt (if (or org-with-time
  15352. (and (boundp 'org-time-was-given) org-time-was-given))
  15353. (cdr fmts)
  15354. (car fmts)))
  15355. (txt (format-time-string fmt (apply 'encode-time f)))
  15356. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15357. (txt (concat "=> " txt)))
  15358. (when (and org-end-time-was-given
  15359. (string-match org-plain-time-of-day-regexp txt))
  15360. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15361. org-end-time-was-given
  15362. (substring txt (match-end 0)))))
  15363. (when org-read-date-analyze-futurep
  15364. (setq txt (concat txt " (=>F)")))
  15365. (setq org-read-date-overlay
  15366. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15367. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15368. (defun org-read-date-analyze (ans org-def org-defdecode)
  15369. "Analyze the combined answer of the date prompt."
  15370. ;; FIXME: cleanup and comment
  15371. (let ((nowdecode (decode-time (current-time)))
  15372. delta deltan deltaw deltadef year month day
  15373. hour minute second wday pm h2 m2 tl wday1
  15374. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15375. (setq org-read-date-analyze-futurep nil
  15376. org-read-date-analyze-forced-year nil)
  15377. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15378. (setq ans "+0"))
  15379. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15380. (setq ans (replace-match "" t t ans)
  15381. deltan (car delta)
  15382. deltaw (nth 1 delta)
  15383. deltadef (nth 2 delta)))
  15384. ;; Check if there is an iso week date in there. If yes, store the
  15385. ;; info and postpone interpreting it until the rest of the parsing
  15386. ;; is done.
  15387. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15388. (setq iso-year (if (match-end 1)
  15389. (org-small-year-to-year
  15390. (string-to-number (match-string 1 ans))))
  15391. iso-weekday (if (match-end 3)
  15392. (string-to-number (match-string 3 ans)))
  15393. iso-week (string-to-number (match-string 2 ans)))
  15394. (setq ans (replace-match "" t t ans)))
  15395. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15396. (when (string-match
  15397. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15398. (setq year (if (match-end 2)
  15399. (string-to-number (match-string 2 ans))
  15400. (progn (setq kill-year t)
  15401. (string-to-number (format-time-string "%Y"))))
  15402. month (string-to-number (match-string 3 ans))
  15403. day (string-to-number (match-string 4 ans)))
  15404. (if (< year 100) (setq year (+ 2000 year)))
  15405. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15406. t nil ans)))
  15407. ;; Help matching dotted european dates
  15408. (when (string-match
  15409. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15410. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15411. (setq kill-year t)
  15412. (string-to-number (format-time-string "%Y")))
  15413. day (string-to-number (match-string 1 ans))
  15414. month (string-to-number (match-string 2 ans))
  15415. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15416. t nil ans)))
  15417. ;; Help matching american dates, like 5/30 or 5/30/7
  15418. (when (string-match
  15419. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15420. (setq year (if (match-end 4)
  15421. (string-to-number (match-string 4 ans))
  15422. (progn (setq kill-year t)
  15423. (string-to-number (format-time-string "%Y"))))
  15424. month (string-to-number (match-string 1 ans))
  15425. day (string-to-number (match-string 2 ans)))
  15426. (if (< year 100) (setq year (+ 2000 year)))
  15427. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15428. t nil ans)))
  15429. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15430. ;; If there is a time with am/pm, and *no* time without it, we convert
  15431. ;; so that matching will be successful.
  15432. (loop for i from 1 to 2 do ; twice, for end time as well
  15433. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15434. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15435. (setq hour (string-to-number (match-string 1 ans))
  15436. minute (if (match-end 3)
  15437. (string-to-number (match-string 3 ans))
  15438. 0)
  15439. pm (equal ?p
  15440. (string-to-char (downcase (match-string 4 ans)))))
  15441. (if (and (= hour 12) (not pm))
  15442. (setq hour 0)
  15443. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15444. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15445. t t ans))))
  15446. ;; Check if a time range is given as a duration
  15447. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15448. (setq hour (string-to-number (match-string 1 ans))
  15449. h2 (+ hour (string-to-number (match-string 3 ans)))
  15450. minute (string-to-number (match-string 2 ans))
  15451. m2 (+ minute (if (match-end 5) (string-to-number
  15452. (match-string 5 ans))0)))
  15453. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15454. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15455. t t ans)))
  15456. ;; Check if there is a time range
  15457. (when (boundp 'org-end-time-was-given)
  15458. (setq org-time-was-given nil)
  15459. (when (and (string-match org-plain-time-of-day-regexp ans)
  15460. (match-end 8))
  15461. (setq org-end-time-was-given (match-string 8 ans))
  15462. (setq ans (concat (substring ans 0 (match-beginning 7))
  15463. (substring ans (match-end 7))))))
  15464. (setq tl (parse-time-string ans)
  15465. day (or (nth 3 tl) (nth 3 org-defdecode))
  15466. month
  15467. (cond ((nth 4 tl))
  15468. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15469. ;; Day was specified. Make sure DAY+MONTH
  15470. ;; combination happens in the future.
  15471. ((nth 3 tl)
  15472. (setq futurep t)
  15473. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15474. (nth 4 nowdecode)))
  15475. (t (nth 4 org-defdecode)))
  15476. year
  15477. (cond ((and (not kill-year) (nth 5 tl)))
  15478. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15479. ;; Month was guessed in the future and is at least
  15480. ;; equal to NOWDECODE's. Fix year accordingly.
  15481. (futurep
  15482. (if (or (> month (nth 4 nowdecode))
  15483. (>= day (nth 3 nowdecode)))
  15484. (nth 5 nowdecode)
  15485. (1+ (nth 5 nowdecode))))
  15486. ;; Month was specified. Make sure MONTH+YEAR
  15487. ;; combination happens in the future.
  15488. ((nth 4 tl)
  15489. (setq futurep t)
  15490. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15491. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15492. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15493. (t (nth 5 nowdecode))))
  15494. (t (nth 5 org-defdecode)))
  15495. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15496. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15497. second (or (nth 0 tl) 0)
  15498. wday (nth 6 tl))
  15499. (when (and (eq org-read-date-prefer-future 'time)
  15500. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15501. (equal day (nth 3 nowdecode))
  15502. (equal month (nth 4 nowdecode))
  15503. (equal year (nth 5 nowdecode))
  15504. (nth 2 tl)
  15505. (or (< (nth 2 tl) (nth 2 nowdecode))
  15506. (and (= (nth 2 tl) (nth 2 nowdecode))
  15507. (nth 1 tl)
  15508. (< (nth 1 tl) (nth 1 nowdecode)))))
  15509. (setq day (1+ day)
  15510. futurep t))
  15511. ;; Special date definitions below
  15512. (cond
  15513. (iso-week
  15514. ;; There was an iso week
  15515. (require 'cal-iso)
  15516. (setq futurep nil)
  15517. (setq year (or iso-year year)
  15518. day (or iso-weekday wday 1)
  15519. wday nil ; to make sure that the trigger below does not match
  15520. iso-date (calendar-gregorian-from-absolute
  15521. (calendar-iso-to-absolute
  15522. (list iso-week day year))))
  15523. ; FIXME: Should we also push ISO weeks into the future?
  15524. ; (when (and org-read-date-prefer-future
  15525. ; (not iso-year)
  15526. ; (< (calendar-absolute-from-gregorian iso-date)
  15527. ; (time-to-days (current-time))))
  15528. ; (setq year (1+ year)
  15529. ; iso-date (calendar-gregorian-from-absolute
  15530. ; (calendar-iso-to-absolute
  15531. ; (list iso-week day year)))))
  15532. (setq month (car iso-date)
  15533. year (nth 2 iso-date)
  15534. day (nth 1 iso-date)))
  15535. (deltan
  15536. (setq futurep nil)
  15537. (unless deltadef
  15538. (let ((now (decode-time (current-time))))
  15539. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15540. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15541. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15542. ((equal deltaw "m") (setq month (+ month deltan)))
  15543. ((equal deltaw "y") (setq year (+ year deltan)))))
  15544. ((and wday (not (nth 3 tl)))
  15545. ;; Weekday was given, but no day, so pick that day in the week
  15546. ;; on or after the derived date.
  15547. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15548. (unless (equal wday wday1)
  15549. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15550. (if (and (boundp 'org-time-was-given)
  15551. (nth 2 tl))
  15552. (setq org-time-was-given t))
  15553. (if (< year 100) (setq year (+ 2000 year)))
  15554. ;; Check of the date is representable
  15555. (if org-read-date-force-compatible-dates
  15556. (progn
  15557. (if (< year 1970)
  15558. (setq year 1970 org-read-date-analyze-forced-year t))
  15559. (if (> year 2037)
  15560. (setq year 2037 org-read-date-analyze-forced-year t)))
  15561. (condition-case nil
  15562. (ignore (encode-time second minute hour day month year))
  15563. (error
  15564. (setq year (nth 5 org-defdecode))
  15565. (setq org-read-date-analyze-forced-year t))))
  15566. (setq org-read-date-analyze-futurep futurep)
  15567. (list second minute hour day month year)))
  15568. (defvar parse-time-weekdays)
  15569. (defun org-read-date-get-relative (s today default)
  15570. "Check string S for special relative date string.
  15571. TODAY and DEFAULT are internal times, for today and for a default.
  15572. Return shift list (N what def-flag)
  15573. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15574. N is the number of WHATs to shift.
  15575. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15576. the DEFAULT date rather than TODAY."
  15577. (require 'parse-time)
  15578. (when (and
  15579. (string-match
  15580. (concat
  15581. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15582. "\\([0-9]+\\)?"
  15583. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15584. "\\([ \t]\\|$\\)") s)
  15585. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15586. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15587. (string-to-char (substring (match-string 1 s) -1))
  15588. ?+))
  15589. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15590. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15591. (what (if (match-end 3) (match-string 3 s) "d"))
  15592. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15593. (date (if rel default today))
  15594. (wday (nth 6 (decode-time date)))
  15595. delta)
  15596. (if wday1
  15597. (progn
  15598. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15599. (if (= delta 0) (setq delta 7))
  15600. (if (= dir ?-)
  15601. (progn
  15602. (setq delta (- delta 7))
  15603. (if (= delta 0) (setq delta -7))))
  15604. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15605. (list delta "d" rel))
  15606. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15607. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15608. "Turn a user-specified date into the internal representation.
  15609. The internal representation needed by the calendar is (month day year).
  15610. This is a wrapper to handle the brain-dead convention in calendar that
  15611. user function argument order change dependent on argument order."
  15612. (if (boundp 'calendar-date-style)
  15613. (cond
  15614. ((eq calendar-date-style 'american)
  15615. (list arg1 arg2 arg3))
  15616. ((eq calendar-date-style 'european)
  15617. (list arg2 arg1 arg3))
  15618. ((eq calendar-date-style 'iso)
  15619. (list arg2 arg3 arg1)))
  15620. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15621. (if (org-bound-and-true-p european-calendar-style)
  15622. (list arg2 arg1 arg3)
  15623. (list arg1 arg2 arg3)))))
  15624. (defun org-eval-in-calendar (form &optional keepdate)
  15625. "Eval FORM in the calendar window and return to current window.
  15626. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15627. otherwise stick to the current value of `org-ans2'."
  15628. (let ((sf (selected-frame))
  15629. (sw (selected-window)))
  15630. (select-window (get-buffer-window "*Calendar*" t))
  15631. (eval form)
  15632. (when (and (not keepdate) (calendar-cursor-to-date))
  15633. (let* ((date (calendar-cursor-to-date))
  15634. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15635. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15636. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15637. (select-window sw)
  15638. (org-select-frame-set-input-focus sf)))
  15639. (defun org-calendar-select ()
  15640. "Return to `org-read-date' with the date currently selected.
  15641. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15642. (interactive)
  15643. (when (calendar-cursor-to-date)
  15644. (let* ((date (calendar-cursor-to-date))
  15645. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15646. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15647. (if (active-minibuffer-window) (exit-minibuffer))))
  15648. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15649. "Insert a date stamp for the date given by the internal TIME.
  15650. See `format-time-string' for the format of TIME.
  15651. WITH-HM means use the stamp format that includes the time of the day.
  15652. INACTIVE means use square brackets instead of angular ones, so that the
  15653. stamp will not contribute to the agenda.
  15654. PRE and POST are optional strings to be inserted before and after the
  15655. stamp.
  15656. The command returns the inserted time stamp."
  15657. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15658. stamp)
  15659. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15660. (insert-before-markers (or pre ""))
  15661. (when (listp extra)
  15662. (setq extra (car extra))
  15663. (if (and (stringp extra)
  15664. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15665. (setq extra (format "-%02d:%02d"
  15666. (string-to-number (match-string 1 extra))
  15667. (string-to-number (match-string 2 extra))))
  15668. (setq extra nil)))
  15669. (when extra
  15670. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15671. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15672. (insert-before-markers (or post ""))
  15673. (setq org-last-inserted-timestamp stamp)))
  15674. (defun org-toggle-time-stamp-overlays ()
  15675. "Toggle the use of custom time stamp formats."
  15676. (interactive)
  15677. (setq org-display-custom-times (not org-display-custom-times))
  15678. (unless org-display-custom-times
  15679. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15680. (while (setq p (next-single-property-change p 'display))
  15681. (if (and (get-text-property p 'display)
  15682. (eq (get-text-property p 'face) 'org-date))
  15683. (remove-text-properties
  15684. p (setq p (next-single-property-change p 'display))
  15685. '(display t))))
  15686. (set-buffer-modified-p bmp)))
  15687. (if (featurep 'xemacs)
  15688. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15689. (org-restart-font-lock)
  15690. (setq org-table-may-need-update t)
  15691. (if org-display-custom-times
  15692. (message "Time stamps are overlaid with custom format")
  15693. (message "Time stamp overlays removed")))
  15694. (defun org-display-custom-time (beg end)
  15695. "Overlay modified time stamp format over timestamp between BEG and END."
  15696. (let* ((ts (buffer-substring beg end))
  15697. t1 w1 with-hm tf time str w2 (off 0))
  15698. (save-match-data
  15699. (setq t1 (org-parse-time-string ts t))
  15700. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15701. (setq off (- (match-end 0) (match-beginning 0)))))
  15702. (setq end (- end off))
  15703. (setq w1 (- end beg)
  15704. with-hm (and (nth 1 t1) (nth 2 t1))
  15705. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15706. time (org-fix-decoded-time t1)
  15707. str (org-add-props
  15708. (format-time-string
  15709. (substring tf 1 -1) (apply 'encode-time time))
  15710. nil 'mouse-face 'highlight)
  15711. w2 (length str))
  15712. (if (not (= w2 w1))
  15713. (add-text-properties (1+ beg) (+ 2 beg)
  15714. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15715. (if (featurep 'xemacs)
  15716. (progn
  15717. (put-text-property beg end 'invisible t)
  15718. (put-text-property beg end 'end-glyph (make-glyph str)))
  15719. (put-text-property beg end 'display str))))
  15720. (defun org-fix-decoded-time (time)
  15721. "Set 0 instead of nil for the first 6 elements of time.
  15722. Don't touch the rest."
  15723. (let ((n 0))
  15724. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15725. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15726. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15727. "Difference between TIMESTAMP-STRING and now in days.
  15728. If SECONDS is non-nil, return the difference in seconds."
  15729. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15730. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15731. (funcall fdiff (current-time)))))
  15732. (defun org-deadline-close (timestamp-string &optional ndays)
  15733. "Is the time in TIMESTAMP-STRING close to the current date?"
  15734. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15735. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15736. (not (org-entry-is-done-p))))
  15737. (defun org-get-wdays (ts &optional delay zero-delay)
  15738. "Get the deadline lead time appropriate for timestring TS.
  15739. When DELAY is non-nil, get the delay time for scheduled items
  15740. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15741. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15742. don't try to find the delay cookie in the scheduled timestamp."
  15743. (let ((tv (if delay org-scheduled-delay-days
  15744. org-deadline-warning-days)))
  15745. (cond
  15746. ((or (and delay (< tv 0))
  15747. (and delay zero-delay (<= tv 0))
  15748. (and (not delay) (<= tv 0)))
  15749. ;; Enforce this value no matter what
  15750. (- tv))
  15751. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15752. ;; lead time is specified.
  15753. (floor (* (string-to-number (match-string 1 ts))
  15754. (cdr (assoc (match-string 2 ts)
  15755. '(("d" . 1) ("w" . 7)
  15756. ("m" . 30.4) ("y" . 365.25)
  15757. ("h" . 0.041667)))))))
  15758. ;; go for the default.
  15759. (t tv))))
  15760. (defun org-calendar-select-mouse (ev)
  15761. "Return to `org-read-date' with the date currently selected.
  15762. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15763. (interactive "e")
  15764. (mouse-set-point ev)
  15765. (when (calendar-cursor-to-date)
  15766. (let* ((date (calendar-cursor-to-date))
  15767. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15768. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15769. (if (active-minibuffer-window) (exit-minibuffer))))
  15770. (defun org-check-deadlines (ndays)
  15771. "Check if there are any deadlines due or past due.
  15772. A deadline is considered due if it happens within `org-deadline-warning-days'
  15773. days from today's date. If the deadline appears in an entry marked DONE,
  15774. it is not shown. The prefix arg NDAYS can be used to test that many
  15775. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15776. (interactive "P")
  15777. (let* ((org-warn-days
  15778. (cond
  15779. ((equal ndays '(4)) 100000)
  15780. (ndays (prefix-numeric-value ndays))
  15781. (t (abs org-deadline-warning-days))))
  15782. (case-fold-search nil)
  15783. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15784. (callback
  15785. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15786. (message "%d deadlines past-due or due within %d days"
  15787. (org-occur regexp nil callback)
  15788. org-warn-days)))
  15789. (defsubst org-re-timestamp (type)
  15790. "Return a regexp for timestamp TYPE.
  15791. Allowed values for TYPE are:
  15792. all: all timestamps
  15793. active: only active timestamps (<...>)
  15794. inactive: only inactive timestamps ([...])
  15795. scheduled: only scheduled timestamps
  15796. deadline: only deadline timestamps
  15797. closed: only closed time-stamps
  15798. When TYPE is nil, fall back on returning a regexp that matches
  15799. both scheduled and deadline timestamps."
  15800. (case type
  15801. (all org-ts-regexp-both)
  15802. (active org-ts-regexp)
  15803. (inactive org-ts-regexp-inactive)
  15804. (scheduled org-scheduled-time-regexp)
  15805. (deadline org-deadline-time-regexp)
  15806. (closed org-closed-time-regexp)
  15807. (otherwise
  15808. (concat "\\<"
  15809. (regexp-opt (list org-deadline-string org-scheduled-string))
  15810. " *<\\([^>]+\\)>"))))
  15811. (defun org-check-before-date (date)
  15812. "Check if there are deadlines or scheduled entries before DATE."
  15813. (interactive (list (org-read-date)))
  15814. (let ((case-fold-search nil)
  15815. (regexp (org-re-timestamp org-ts-type))
  15816. (callback
  15817. `(lambda ()
  15818. (and ,(if (memq org-ts-type '(active inactive all))
  15819. '(eq (org-element-type (org-element-context) 'timestamp))
  15820. '(org-at-planning-p))
  15821. (time-less-p
  15822. (org-time-string-to-time (match-string 1))
  15823. (org-time-string-to-time date))))))
  15824. (message "%d entries before %s"
  15825. (org-occur regexp nil callback) date)))
  15826. (defun org-check-after-date (date)
  15827. "Check if there are deadlines or scheduled entries after DATE."
  15828. (interactive (list (org-read-date)))
  15829. (let ((case-fold-search nil)
  15830. (regexp (org-re-timestamp org-ts-type))
  15831. (callback
  15832. `(lambda ()
  15833. (and ,(if (memq org-ts-type '(active inactive all))
  15834. '(eq (org-element-type (org-element-context) 'timestamp))
  15835. '(org-at-planning-p))
  15836. (not (time-less-p
  15837. (org-time-string-to-time (match-string 1))
  15838. (org-time-string-to-time date)))))))
  15839. (message "%d entries after %s"
  15840. (org-occur regexp nil callback) date)))
  15841. (defun org-check-dates-range (start-date end-date)
  15842. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15843. (interactive (list (org-read-date nil nil nil "Range starts")
  15844. (org-read-date nil nil nil "Range end")))
  15845. (let ((case-fold-search nil)
  15846. (regexp (org-re-timestamp org-ts-type))
  15847. (callback
  15848. `(lambda ()
  15849. (let ((match (match-string 1)))
  15850. (and
  15851. ,(if (memq org-ts-type '(active inactive all))
  15852. '(eq (org-element-type (org-element-context) 'timestamp))
  15853. '(org-at-planning-p))
  15854. (not (time-less-p
  15855. (org-time-string-to-time match)
  15856. (org-time-string-to-time start-date)))
  15857. (time-less-p
  15858. (org-time-string-to-time match)
  15859. (org-time-string-to-time end-date)))))))
  15860. (message "%d entries between %s and %s"
  15861. (org-occur regexp nil callback) start-date end-date)))
  15862. (defun org-evaluate-time-range (&optional to-buffer)
  15863. "Evaluate a time range by computing the difference between start and end.
  15864. Normally the result is just printed in the echo area, but with prefix arg
  15865. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15866. If the time range is actually in a table, the result is inserted into the
  15867. next column.
  15868. For time difference computation, a year is assumed to be exactly 365
  15869. days in order to avoid rounding problems."
  15870. (interactive "P")
  15871. (or
  15872. (org-clock-update-time-maybe)
  15873. (save-excursion
  15874. (unless (org-at-date-range-p t)
  15875. (goto-char (point-at-bol))
  15876. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15877. (if (not (org-at-date-range-p t))
  15878. (user-error "Not at a time-stamp range, and none found in current line")))
  15879. (let* ((ts1 (match-string 1))
  15880. (ts2 (match-string 2))
  15881. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15882. (match-end (match-end 0))
  15883. (time1 (org-time-string-to-time ts1))
  15884. (time2 (org-time-string-to-time ts2))
  15885. (t1 (org-float-time time1))
  15886. (t2 (org-float-time time2))
  15887. (diff (abs (- t2 t1)))
  15888. (negative (< (- t2 t1) 0))
  15889. ;; (ys (floor (* 365 24 60 60)))
  15890. (ds (* 24 60 60))
  15891. (hs (* 60 60))
  15892. (fy "%dy %dd %02d:%02d")
  15893. (fy1 "%dy %dd")
  15894. (fd "%dd %02d:%02d")
  15895. (fd1 "%dd")
  15896. (fh "%02d:%02d")
  15897. y d h m align)
  15898. (if havetime
  15899. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15900. y 0
  15901. d (floor (/ diff ds)) diff (mod diff ds)
  15902. h (floor (/ diff hs)) diff (mod diff hs)
  15903. m (floor (/ diff 60)))
  15904. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15905. y 0
  15906. d (floor (+ (/ diff ds) 0.5))
  15907. h 0 m 0))
  15908. (if (not to-buffer)
  15909. (message "%s" (org-make-tdiff-string y d h m))
  15910. (if (org-at-table-p)
  15911. (progn
  15912. (goto-char match-end)
  15913. (setq align t)
  15914. (and (looking-at " *|") (goto-char (match-end 0))))
  15915. (goto-char match-end))
  15916. (if (looking-at
  15917. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15918. (replace-match ""))
  15919. (if negative (insert " -"))
  15920. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15921. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15922. (insert " " (format fh h m))))
  15923. (if align (org-table-align))
  15924. (message "Time difference inserted")))))
  15925. (defun org-make-tdiff-string (y d h m)
  15926. (let ((fmt "")
  15927. (l nil))
  15928. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15929. l (push y l)))
  15930. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15931. l (push d l)))
  15932. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15933. l (push h l)))
  15934. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15935. l (push m l)))
  15936. (apply 'format fmt (nreverse l))))
  15937. (defun org-time-string-to-time (s &optional buffer pos)
  15938. "Convert a timestamp string into internal time."
  15939. (condition-case errdata
  15940. (apply 'encode-time (org-parse-time-string s))
  15941. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15942. s (if (not (and buffer pos))
  15943. ""
  15944. (format " at %d in buffer `%s'" pos buffer))
  15945. (cdr errdata)))))
  15946. (defun org-time-string-to-seconds (s)
  15947. "Convert a timestamp string to a number of seconds."
  15948. (org-float-time (org-time-string-to-time s)))
  15949. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15950. "Convert a time stamp to an absolute day number.
  15951. If there is a specifier for a cyclic time stamp, get the closest
  15952. date to DAYNR.
  15953. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15954. The variable `date' is bound by the calendar when this is called."
  15955. (cond
  15956. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15957. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15958. daynr
  15959. (+ daynr 1000)))
  15960. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15961. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15962. (time-to-days (current-time))) (match-string 0 s)
  15963. prefer show-all))
  15964. (t (time-to-days
  15965. (condition-case errdata
  15966. (apply 'encode-time (org-parse-time-string s))
  15967. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15968. s (if (not (and buffer pos))
  15969. ""
  15970. (format " at %d in buffer `%s'" pos buffer))
  15971. (cdr errdata))))))))
  15972. (defun org-days-to-iso-week (days)
  15973. "Return the iso week number."
  15974. (require 'cal-iso)
  15975. (car (calendar-iso-from-absolute days)))
  15976. (defun org-small-year-to-year (year)
  15977. "Convert 2-digit years into 4-digit years.
  15978. YEAR is expanded into one of the 30 next years, if possible, or
  15979. into a past one. Any year larger than 99 is returned unchanged."
  15980. (if (>= year 100) year
  15981. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  15982. (century (/ current 100))
  15983. (offset (- year (% current 100))))
  15984. (cond ((> offset 30) (+ (* (1- century) 100) year))
  15985. ((> offset -70) (+ (* century 100) year))
  15986. (t (+ (* (1+ century) 100) year))))))
  15987. (defun org-time-from-absolute (d)
  15988. "Return the time corresponding to date D.
  15989. D may be an absolute day number, or a calendar-type list (month day year)."
  15990. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15991. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15992. (defun org-calendar-holiday ()
  15993. "List of holidays, for Diary display in Org-mode."
  15994. (require 'holidays)
  15995. (let ((hl (funcall
  15996. (if (fboundp 'calendar-check-holidays)
  15997. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15998. (if hl (mapconcat 'identity hl "; "))))
  15999. (defun org-diary-sexp-entry (sexp entry date)
  16000. "Process a SEXP diary ENTRY for DATE."
  16001. (require 'diary-lib)
  16002. (let ((result (if calendar-debug-sexp
  16003. (let ((stack-trace-on-error t))
  16004. (eval (car (read-from-string sexp))))
  16005. (condition-case nil
  16006. (eval (car (read-from-string sexp)))
  16007. (error
  16008. (beep)
  16009. (message "Bad sexp at line %d in %s: %s"
  16010. (org-current-line)
  16011. (buffer-file-name) sexp)
  16012. (sleep-for 2))))))
  16013. (cond ((stringp result) (split-string result "; "))
  16014. ((and (consp result)
  16015. (not (consp (cdr result)))
  16016. (stringp (cdr result))) (cdr result))
  16017. ((and (consp result)
  16018. (stringp (car result))) result)
  16019. (result entry))))
  16020. (defun org-diary-to-ical-string (frombuf)
  16021. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16022. This uses the icalendar.el library."
  16023. (let* ((tmpdir (if (featurep 'xemacs)
  16024. (temp-directory)
  16025. temporary-file-directory))
  16026. (tmpfile (make-temp-name
  16027. (expand-file-name "orgics" tmpdir)))
  16028. buf rtn b e)
  16029. (with-current-buffer frombuf
  16030. (icalendar-export-region (point-min) (point-max) tmpfile)
  16031. (setq buf (find-buffer-visiting tmpfile))
  16032. (set-buffer buf)
  16033. (goto-char (point-min))
  16034. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16035. (setq b (match-beginning 0)))
  16036. (goto-char (point-max))
  16037. (if (re-search-backward "^END:VEVENT" nil t)
  16038. (setq e (match-end 0)))
  16039. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16040. (kill-buffer buf)
  16041. (delete-file tmpfile)
  16042. rtn))
  16043. (defun org-closest-date (start current change prefer show-all)
  16044. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16045. When PREFER is `past', return a date that is either CURRENT or past.
  16046. When PREFER is `future', return a date that is either CURRENT or future.
  16047. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16048. ;; Make the proper lists from the dates
  16049. (catch 'exit
  16050. (let ((a1 '(("h" . hour)
  16051. ("d" . day)
  16052. ("w" . week)
  16053. ("m" . month)
  16054. ("y" . year)))
  16055. (shour (nth 2 (org-parse-time-string start)))
  16056. dn dw sday cday n1 n2 n0
  16057. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16058. (setq start (org-date-to-gregorian start)
  16059. current (org-date-to-gregorian
  16060. (if show-all
  16061. current
  16062. (time-to-days (current-time))))
  16063. sday (calendar-absolute-from-gregorian start)
  16064. cday (calendar-absolute-from-gregorian current))
  16065. (if (<= cday sday) (throw 'exit sday))
  16066. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16067. (setq dn (string-to-number (match-string 1 change))
  16068. dw (cdr (assoc (match-string 2 change) a1)))
  16069. (user-error "Invalid change specifier: %s" change))
  16070. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16071. (cond
  16072. ((eq dw 'hour)
  16073. (let ((missing-hours
  16074. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16075. dn)))
  16076. (setq n1 (if (zerop missing-hours) cday
  16077. (- cday (1+ (floor (/ missing-hours 24)))))
  16078. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16079. ((eq dw 'day)
  16080. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16081. n2 (+ n1 dn)))
  16082. ((eq dw 'year)
  16083. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16084. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16085. (setq date1 (list m d y1)
  16086. n1 (calendar-absolute-from-gregorian date1)
  16087. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16088. n2 (calendar-absolute-from-gregorian date2)))
  16089. ((eq dw 'month)
  16090. ;; approx number of month between the two dates
  16091. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16092. ;; How often does dn fit in there?
  16093. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16094. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16095. m (+ m nm)
  16096. ny (floor (/ m 12))
  16097. y (+ y ny)
  16098. m (- m (* ny 12)))
  16099. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16100. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16101. (setq m2 (+ m dn) y2 y)
  16102. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16103. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16104. (while (<= n2 cday)
  16105. (setq n1 n2 m m2 y y2)
  16106. (setq m2 (+ m dn) y2 y)
  16107. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16108. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16109. ;; Make sure n1 is the earlier date
  16110. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16111. (if show-all
  16112. (cond
  16113. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16114. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16115. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16116. (cond
  16117. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16118. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16119. (t (if (= cday n1) n1 n2)))))))
  16120. (defun org-date-to-gregorian (date)
  16121. "Turn any specification of DATE into a Gregorian date for the calendar."
  16122. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16123. ((and (listp date) (= (length date) 3)) date)
  16124. ((stringp date)
  16125. (setq date (org-parse-time-string date))
  16126. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16127. ((listp date)
  16128. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16129. (defun org-parse-time-string (s &optional nodefault)
  16130. "Parse the standard Org-mode time string.
  16131. This should be a lot faster than the normal `parse-time-string'.
  16132. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16133. hour and minute fields will be nil if not given."
  16134. (cond ((string-match org-ts-regexp0 s)
  16135. (list 0
  16136. (if (or (match-beginning 8) (not nodefault))
  16137. (string-to-number (or (match-string 8 s) "0")))
  16138. (if (or (match-beginning 7) (not nodefault))
  16139. (string-to-number (or (match-string 7 s) "0")))
  16140. (string-to-number (match-string 4 s))
  16141. (string-to-number (match-string 3 s))
  16142. (string-to-number (match-string 2 s))
  16143. nil nil nil))
  16144. ((string-match "^<[^>]+>$" s)
  16145. (decode-time (seconds-to-time (org-matcher-time s))))
  16146. (t (error "Not a standard Org-mode time string: %s" s))))
  16147. (defun org-timestamp-up (&optional arg)
  16148. "Increase the date item at the cursor by one.
  16149. If the cursor is on the year, change the year. If it is on the month,
  16150. the day or the time, change that.
  16151. With prefix ARG, change by that many units."
  16152. (interactive "p")
  16153. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16154. (defun org-timestamp-down (&optional arg)
  16155. "Decrease the date item at the cursor by one.
  16156. If the cursor is on the year, change the year. If it is on the month,
  16157. the day or the time, change that.
  16158. With prefix ARG, change by that many units."
  16159. (interactive "p")
  16160. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16161. (defun org-timestamp-up-day (&optional arg)
  16162. "Increase the date in the time stamp by one day.
  16163. With prefix ARG, change that many days."
  16164. (interactive "p")
  16165. (if (and (not (org-at-timestamp-p t))
  16166. (org-at-heading-p))
  16167. (org-todo 'up)
  16168. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16169. (defun org-timestamp-down-day (&optional arg)
  16170. "Decrease the date in the time stamp by one day.
  16171. With prefix ARG, change that many days."
  16172. (interactive "p")
  16173. (if (and (not (org-at-timestamp-p t))
  16174. (org-at-heading-p))
  16175. (org-todo 'down)
  16176. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16177. (defun org-at-timestamp-p (&optional inactive-ok)
  16178. "Determine if the cursor is in or at a timestamp."
  16179. (interactive)
  16180. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16181. (pos (point))
  16182. (ans (or (looking-at tsr)
  16183. (save-excursion
  16184. (skip-chars-backward "^[<\n\r\t")
  16185. (if (> (point) (point-min)) (backward-char 1))
  16186. (and (looking-at tsr)
  16187. (> (- (match-end 0) pos) -1))))))
  16188. (and ans
  16189. (boundp 'org-ts-what)
  16190. (setq org-ts-what
  16191. (cond
  16192. ((= pos (match-beginning 0)) 'bracket)
  16193. ;; Point is considered to be "on the bracket" whether
  16194. ;; it's really on it or right after it.
  16195. ((= pos (1- (match-end 0))) 'bracket)
  16196. ((= pos (match-end 0)) 'after)
  16197. ((org-pos-in-match-range pos 2) 'year)
  16198. ((org-pos-in-match-range pos 3) 'month)
  16199. ((org-pos-in-match-range pos 7) 'hour)
  16200. ((org-pos-in-match-range pos 8) 'minute)
  16201. ((or (org-pos-in-match-range pos 4)
  16202. (org-pos-in-match-range pos 5)) 'day)
  16203. ((and (> pos (or (match-end 8) (match-end 5)))
  16204. (< pos (match-end 0)))
  16205. (- pos (or (match-end 8) (match-end 5))))
  16206. (t 'day))))
  16207. ans))
  16208. (defun org-toggle-timestamp-type ()
  16209. "Toggle the type (<active> or [inactive]) of a time stamp."
  16210. (interactive)
  16211. (when (org-at-timestamp-p t)
  16212. (let ((beg (match-beginning 0)) (end (match-end 0))
  16213. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16214. (save-excursion
  16215. (goto-char beg)
  16216. (while (re-search-forward "[][<>]" end t)
  16217. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16218. t t)))
  16219. (message "Timestamp is now %sactive"
  16220. (if (equal (char-after beg) ?<) "" "in")))))
  16221. (defun org-at-clock-log-p nil
  16222. "Is the cursor on the clock log line?"
  16223. (save-excursion
  16224. (move-beginning-of-line 1)
  16225. (looking-at org-clock-line-re)))
  16226. (defvar org-clock-history) ; defined in org-clock.el
  16227. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16228. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16229. "Change the date in the time stamp at point.
  16230. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16231. `year', `minute', `second'. If WHAT is not given, the cursor position
  16232. in the timestamp determines what will be changed.
  16233. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16234. (let ((origin (point)) origin-cat
  16235. with-hm inactive
  16236. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16237. org-ts-what
  16238. extra rem
  16239. ts time time0 fixnext clrgx)
  16240. (if (not (org-at-timestamp-p t))
  16241. (user-error "Not at a timestamp"))
  16242. (if (and (not what) (eq org-ts-what 'bracket))
  16243. (org-toggle-timestamp-type)
  16244. ;; Point isn't on brackets. Remember the part of the time-stamp
  16245. ;; the point was in. Indeed, size of time-stamps may change,
  16246. ;; but point must be kept in the same category nonetheless.
  16247. (setq origin-cat org-ts-what)
  16248. (if (and (not what) (not (eq org-ts-what 'day))
  16249. org-display-custom-times
  16250. (get-text-property (point) 'display)
  16251. (not (get-text-property (1- (point)) 'display)))
  16252. (setq org-ts-what 'day))
  16253. (setq org-ts-what (or what org-ts-what)
  16254. inactive (= (char-after (match-beginning 0)) ?\[)
  16255. ts (match-string 0))
  16256. (replace-match "")
  16257. (when (string-match
  16258. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16259. ts)
  16260. (setq extra (match-string 1 ts))
  16261. (if suppress-tmp-delay
  16262. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16263. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16264. (setq with-hm t))
  16265. (setq time0 (org-parse-time-string ts))
  16266. (when (and updown
  16267. (eq org-ts-what 'minute)
  16268. (not current-prefix-arg))
  16269. ;; This looks like s-up and s-down. Change by one rounding step.
  16270. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16271. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16272. (setcar (cdr time0) (+ (nth 1 time0)
  16273. (if (> n 0) (- rem) (- dm rem))))))
  16274. (setq time
  16275. (encode-time (or (car time0) 0)
  16276. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16277. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16278. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16279. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16280. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16281. (nthcdr 6 time0)))
  16282. (when (and (member org-ts-what '(hour minute))
  16283. extra
  16284. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16285. (setq extra (org-modify-ts-extra
  16286. extra
  16287. (if (eq org-ts-what 'hour) 2 5)
  16288. n dm)))
  16289. (when (integerp org-ts-what)
  16290. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16291. (if (eq what 'calendar)
  16292. (let ((cal-date (org-get-date-from-calendar)))
  16293. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16294. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16295. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16296. (setcar time0 (or (car time0) 0))
  16297. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16298. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16299. (setq time (apply 'encode-time time0))))
  16300. ;; Insert the new time-stamp, and ensure point stays in the same
  16301. ;; category as before (i.e. not after the last position in that
  16302. ;; category).
  16303. (let ((pos (point)))
  16304. ;; Stay before inserted string. `save-excursion' is of no use.
  16305. (setq org-last-changed-timestamp
  16306. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16307. (goto-char pos))
  16308. (save-match-data
  16309. (looking-at org-ts-regexp3)
  16310. (goto-char (cond
  16311. ;; `day' category ends before `hour' if any, or at
  16312. ;; the end of the day name.
  16313. ((eq origin-cat 'day)
  16314. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16315. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16316. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16317. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16318. ;; `year' and `month' have both fixed size: point
  16319. ;; couldn't have moved into another part.
  16320. (t origin))))
  16321. ;; Update clock if on a CLOCK line.
  16322. (org-clock-update-time-maybe)
  16323. ;; Maybe adjust the closest clock in `org-clock-history'
  16324. (when org-clock-adjust-closest
  16325. (if (not (and (org-at-clock-log-p)
  16326. (< 1 (length (delq nil (mapcar 'marker-position
  16327. org-clock-history))))))
  16328. (message "No clock to adjust")
  16329. (cond ((save-excursion ; fix previous clock?
  16330. (re-search-backward org-ts-regexp0 nil t)
  16331. (org-looking-back (concat org-clock-string " \\[")))
  16332. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16333. ((save-excursion ; fix next clock?
  16334. (re-search-backward org-ts-regexp0 nil t)
  16335. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16336. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16337. (save-window-excursion
  16338. ;; Find closest clock to point, adjust the previous/next one in history
  16339. (let* ((p (save-excursion (org-back-to-heading t)))
  16340. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16341. (clfixnth
  16342. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16343. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16344. (if (not clfixpos)
  16345. (message "No clock to adjust")
  16346. (save-excursion
  16347. (org-goto-marker-or-bmk clfixpos)
  16348. (org-show-subtree)
  16349. (when (re-search-forward clrgx nil t)
  16350. (goto-char (match-beginning 1))
  16351. (let (org-clock-adjust-closest)
  16352. (org-timestamp-change n org-ts-what updown))
  16353. (message "Clock adjusted in %s for heading: %s"
  16354. (file-name-nondirectory (buffer-file-name))
  16355. (org-get-heading t t)))))))))
  16356. ;; Try to recenter the calendar window, if any.
  16357. (if (and org-calendar-follow-timestamp-change
  16358. (get-buffer-window "*Calendar*" t)
  16359. (memq org-ts-what '(day month year)))
  16360. (org-recenter-calendar (time-to-days time))))))
  16361. (defun org-modify-ts-extra (s pos n dm)
  16362. "Change the different parts of the lead-time and repeat fields in timestamp."
  16363. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16364. ng h m new rem)
  16365. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16366. (cond
  16367. ((or (org-pos-in-match-range pos 2)
  16368. (org-pos-in-match-range pos 3))
  16369. (setq m (string-to-number (match-string 3 s))
  16370. h (string-to-number (match-string 2 s)))
  16371. (if (org-pos-in-match-range pos 2)
  16372. (setq h (+ h n))
  16373. (setq n (* dm (org-no-warnings (signum n))))
  16374. (when (not (= 0 (setq rem (% m dm))))
  16375. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16376. (setq m (+ m n)))
  16377. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16378. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16379. (setq h (min 24 (max 0 h)))
  16380. (setq ng 1 new (format "-%02d:%02d" h m)))
  16381. ((org-pos-in-match-range pos 6)
  16382. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16383. ((org-pos-in-match-range pos 5)
  16384. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16385. ((org-pos-in-match-range pos 9)
  16386. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16387. ((org-pos-in-match-range pos 8)
  16388. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16389. (when ng
  16390. (setq s (concat
  16391. (substring s 0 (match-beginning ng))
  16392. new
  16393. (substring s (match-end ng))))))
  16394. s))
  16395. (defun org-recenter-calendar (date)
  16396. "If the calendar is visible, recenter it to DATE."
  16397. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16398. (when cwin
  16399. (let ((calendar-move-hook nil))
  16400. (with-selected-window cwin
  16401. (calendar-goto-date (if (listp date) date
  16402. (calendar-gregorian-from-absolute date))))))))
  16403. (defun org-goto-calendar (&optional arg)
  16404. "Go to the Emacs calendar at the current date.
  16405. If there is a time stamp in the current line, go to that date.
  16406. A prefix ARG can be used to force the current date."
  16407. (interactive "P")
  16408. (let ((tsr org-ts-regexp) diff
  16409. (calendar-move-hook nil)
  16410. (calendar-view-holidays-initially-flag nil)
  16411. (calendar-view-diary-initially-flag nil))
  16412. (if (or (org-at-timestamp-p)
  16413. (save-excursion
  16414. (beginning-of-line 1)
  16415. (looking-at (concat ".*" tsr))))
  16416. (let ((d1 (time-to-days (current-time)))
  16417. (d2 (time-to-days
  16418. (org-time-string-to-time (match-string 1)))))
  16419. (setq diff (- d2 d1))))
  16420. (calendar)
  16421. (calendar-goto-today)
  16422. (if (and diff (not arg)) (calendar-forward-day diff))))
  16423. (defun org-get-date-from-calendar ()
  16424. "Return a list (month day year) of date at point in calendar."
  16425. (with-current-buffer "*Calendar*"
  16426. (save-match-data
  16427. (calendar-cursor-to-date))))
  16428. (defun org-date-from-calendar ()
  16429. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16430. If there is already a time stamp at the cursor position, update it."
  16431. (interactive)
  16432. (if (org-at-timestamp-p t)
  16433. (org-timestamp-change 0 'calendar)
  16434. (let ((cal-date (org-get-date-from-calendar)))
  16435. (org-insert-time-stamp
  16436. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16437. (defcustom org-effort-durations
  16438. `(("min" . 1)
  16439. ("h" . 60)
  16440. ("d" . ,(* 60 8))
  16441. ("w" . ,(* 60 8 5))
  16442. ("m" . ,(* 60 8 5 4))
  16443. ("y" . ,(* 60 8 5 40)))
  16444. "Conversion factor to minutes for an effort modifier.
  16445. Each entry has the form (MODIFIER . MINUTES).
  16446. In an effort string, a number followed by MODIFIER is multiplied
  16447. by the specified number of MINUTES to obtain an effort in
  16448. minutes.
  16449. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16450. effort string \"2hours\" is equivalent to 120 minutes."
  16451. :group 'org-agenda
  16452. :version "25.1"
  16453. :package-version '(Org . "8.3")
  16454. :type '(alist :key-type (string :tag "Modifier")
  16455. :value-type (number :tag "Minutes")))
  16456. (defun org-minutes-to-clocksum-string (m)
  16457. "Format number of minutes as a clocksum string.
  16458. The format is determined by `org-time-clocksum-format',
  16459. `org-time-clocksum-use-fractional' and
  16460. `org-time-clocksum-fractional-format' and
  16461. `org-time-clocksum-use-effort-durations'."
  16462. (let ((clocksum "")
  16463. (m (round m)) ; Don't allow fractions of minutes
  16464. h d w mo y fmt n)
  16465. (setq h (if org-time-clocksum-use-effort-durations
  16466. (cdr (assoc "h" org-effort-durations)) 60)
  16467. d (if org-time-clocksum-use-effort-durations
  16468. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16469. w (if org-time-clocksum-use-effort-durations
  16470. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16471. mo (if org-time-clocksum-use-effort-durations
  16472. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16473. y (if org-time-clocksum-use-effort-durations
  16474. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16475. ;; fractional format
  16476. (if org-time-clocksum-use-fractional
  16477. (cond
  16478. ;; single format string
  16479. ((stringp org-time-clocksum-fractional-format)
  16480. (format org-time-clocksum-fractional-format (/ m (float h))))
  16481. ;; choice of fractional formats for different time units
  16482. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16483. (> (/ (truncate m) (* y d h)) 0))
  16484. (format fmt (/ m (* y d (float h)))))
  16485. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16486. (> (/ (truncate m) (* mo d h)) 0))
  16487. (format fmt (/ m (* mo d (float h)))))
  16488. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16489. (> (/ (truncate m) (* w d h)) 0))
  16490. (format fmt (/ m (* w d (float h)))))
  16491. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16492. (> (/ (truncate m) (* d h)) 0))
  16493. (format fmt (/ m (* d (float h)))))
  16494. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16495. (> (/ (truncate m) h) 0))
  16496. (format fmt (/ m (float h))))
  16497. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16498. (format fmt m))
  16499. ;; fall back to smallest time unit with a format
  16500. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16501. (format fmt (/ m (float h))))
  16502. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16503. (format fmt (/ m (* d (float h)))))
  16504. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16505. (format fmt (/ m (* w d (float h)))))
  16506. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16507. (format fmt (/ m (* mo d (float h)))))
  16508. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16509. (format fmt (/ m (* y d (float h))))))
  16510. ;; standard (non-fractional) format, with single format string
  16511. (if (stringp org-time-clocksum-format)
  16512. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16513. ;; separate formats components
  16514. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16515. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16516. (plist-get org-time-clocksum-format :require-years))
  16517. (setq clocksum (concat clocksum (format fmt n))
  16518. m (- m (* n y d h))))
  16519. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16520. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16521. (plist-get org-time-clocksum-format :require-months))
  16522. (setq clocksum (concat clocksum (format fmt n))
  16523. m (- m (* n mo d h))))
  16524. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16525. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16526. (plist-get org-time-clocksum-format :require-weeks))
  16527. (setq clocksum (concat clocksum (format fmt n))
  16528. m (- m (* n w d h))))
  16529. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16530. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16531. (plist-get org-time-clocksum-format :require-days))
  16532. (setq clocksum (concat clocksum (format fmt n))
  16533. m (- m (* n d h))))
  16534. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16535. (or (> (setq n (/ (truncate m) h)) 0)
  16536. (plist-get org-time-clocksum-format :require-hours))
  16537. (setq clocksum (concat clocksum (format fmt n))
  16538. m (- m (* n h))))
  16539. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16540. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16541. (setq clocksum (concat clocksum (format fmt m))))
  16542. ;; return formatted time duration
  16543. clocksum))))
  16544. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16545. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16546. "Org mode version 8.0")
  16547. (defun org-hours-to-clocksum-string (n)
  16548. (org-minutes-to-clocksum-string (* n 60)))
  16549. (defun org-hh:mm-string-to-minutes (s)
  16550. "Convert a string H:MM to a number of minutes.
  16551. If the string is just a number, interpret it as minutes.
  16552. In fact, the first hh:mm or number in the string will be taken,
  16553. there can be extra stuff in the string.
  16554. If no number is found, the return value is 0."
  16555. (cond
  16556. ((integerp s) s)
  16557. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16558. (+ (* (string-to-number (match-string 1 s)) 60)
  16559. (string-to-number (match-string 2 s))))
  16560. ((string-match "\\([0-9]+\\)" s)
  16561. (string-to-number (match-string 1 s)))
  16562. (t 0)))
  16563. (defcustom org-image-actual-width t
  16564. "Should we use the actual width of images when inlining them?
  16565. When set to `t', always use the image width.
  16566. When set to a number, use imagemagick (when available) to set
  16567. the image's width to this value.
  16568. When set to a number in a list, try to get the width from any
  16569. #+ATTR.* keyword if it matches a width specification like
  16570. #+ATTR_HTML: :width 300px
  16571. and fall back on that number if none is found.
  16572. When set to nil, try to get the width from an #+ATTR.* keyword
  16573. and fall back on the original width if none is found.
  16574. This requires Emacs >= 24.1, build with imagemagick support."
  16575. :group 'org-appearance
  16576. :version "24.4"
  16577. :package-version '(Org . "8.0")
  16578. :type '(choice
  16579. (const :tag "Use the image width" t)
  16580. (integer :tag "Use a number of pixels")
  16581. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16582. (const :tag "Use #+ATTR* or don't resize" nil)))
  16583. (defcustom org-agenda-inhibit-startup nil
  16584. "Inhibit startup when preparing agenda buffers.
  16585. When this variable is `t', the initialization of the Org agenda
  16586. buffers is inhibited: e.g. the visibility state is not set, the
  16587. tables are not re-aligned, etc."
  16588. :type 'boolean
  16589. :version "24.3"
  16590. :group 'org-agenda)
  16591. (define-obsolete-variable-alias
  16592. 'org-agenda-ignore-drawer-properties
  16593. 'org-agenda-ignore-properties "25.1")
  16594. (defcustom org-agenda-ignore-properties nil
  16595. "Avoid updating text properties when building the agenda.
  16596. Properties are used to prepare buffers for effort estimates,
  16597. appointments, statistics and subtree-local categories.
  16598. If you don't use these in the agenda, you can add them to this
  16599. list and agenda building will be a bit faster.
  16600. The value is a list, with zero or more of the symbols `effort', `appt',
  16601. `stats' or `category'."
  16602. :type '(set :greedy t
  16603. (const effort)
  16604. (const appt)
  16605. (const stats)
  16606. (const category))
  16607. :version "25.1"
  16608. :package-version '(Org . "8.3")
  16609. :group 'org-agenda)
  16610. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16611. "Convert a duration string S to minutes.
  16612. A bare number is interpreted as minutes, modifiers can be set by
  16613. customizing `org-effort-durations' (which see).
  16614. Entries containing a colon are interpreted as H:MM by
  16615. `org-hh:mm-string-to-minutes'."
  16616. (let ((result 0)
  16617. (re (concat "\\([0-9.]+\\) *\\("
  16618. (regexp-opt (mapcar 'car org-effort-durations))
  16619. "\\)")))
  16620. (while (string-match re s)
  16621. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16622. (string-to-number (match-string 1 s))))
  16623. (setq s (replace-match "" nil t s)))
  16624. (setq result (floor result))
  16625. (incf result (org-hh:mm-string-to-minutes s))
  16626. (if output-to-string (number-to-string result) result)))
  16627. ;;;; Files
  16628. (defun org-save-all-org-buffers ()
  16629. "Save all Org-mode buffers without user confirmation."
  16630. (interactive)
  16631. (message "Saving all Org-mode buffers...")
  16632. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16633. (when (featurep 'org-id) (org-id-locations-save))
  16634. (message "Saving all Org-mode buffers... done"))
  16635. (defun org-revert-all-org-buffers ()
  16636. "Revert all Org-mode buffers.
  16637. Prompt for confirmation when there are unsaved changes.
  16638. Be sure you know what you are doing before letting this function
  16639. overwrite your changes.
  16640. This function is useful in a setup where one tracks org files
  16641. with a version control system, to revert on one machine after pulling
  16642. changes from another. I believe the procedure must be like this:
  16643. 1. M-x org-save-all-org-buffers
  16644. 2. Pull changes from the other machine, resolve conflicts
  16645. 3. M-x org-revert-all-org-buffers"
  16646. (interactive)
  16647. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16648. (user-error "Abort"))
  16649. (save-excursion
  16650. (save-window-excursion
  16651. (mapc
  16652. (lambda (b)
  16653. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16654. (with-current-buffer b buffer-file-name))
  16655. (org-pop-to-buffer-same-window b)
  16656. (revert-buffer t 'no-confirm)))
  16657. (buffer-list))
  16658. (when (and (featurep 'org-id) org-id-track-globally)
  16659. (org-id-locations-load)))))
  16660. ;;;; Agenda files
  16661. ;;;###autoload
  16662. (defun org-switchb (&optional arg)
  16663. "Switch between Org buffers.
  16664. With one prefix argument, restrict available buffers to files.
  16665. With two prefix arguments, restrict available buffers to agenda files.
  16666. Defaults to `iswitchb' for buffer name completion.
  16667. Set `org-completion-use-ido' to make it use ido instead."
  16668. (interactive "P")
  16669. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16670. ((equal arg '(16)) (org-buffer-list 'agenda))
  16671. (t (org-buffer-list))))
  16672. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16673. (org-completion-use-ido org-completion-use-ido))
  16674. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16675. (setq org-completion-use-iswitchb t))
  16676. (org-pop-to-buffer-same-window
  16677. (org-icompleting-read "Org buffer: "
  16678. (mapcar 'list (mapcar 'buffer-name blist))
  16679. nil t))))
  16680. ;;; Define some older names previously used for this functionality
  16681. ;;;###autoload
  16682. (defalias 'org-ido-switchb 'org-switchb)
  16683. ;;;###autoload
  16684. (defalias 'org-iswitchb 'org-switchb)
  16685. (defun org-buffer-list (&optional predicate exclude-tmp)
  16686. "Return a list of Org buffers.
  16687. PREDICATE can be `export', `files' or `agenda'.
  16688. export restrict the list to Export buffers.
  16689. files restrict the list to buffers visiting Org files.
  16690. agenda restrict the list to buffers visiting agenda files.
  16691. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16692. (let* ((bfn nil)
  16693. (agenda-files (and (eq predicate 'agenda)
  16694. (mapcar 'file-truename (org-agenda-files t))))
  16695. (filter
  16696. (cond
  16697. ((eq predicate 'files)
  16698. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16699. ((eq predicate 'export)
  16700. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16701. ((eq predicate 'agenda)
  16702. (lambda (b)
  16703. (with-current-buffer b
  16704. (and (derived-mode-p 'org-mode)
  16705. (setq bfn (buffer-file-name b))
  16706. (member (file-truename bfn) agenda-files)))))
  16707. (t (lambda (b) (with-current-buffer b
  16708. (or (derived-mode-p 'org-mode)
  16709. (string-match "\*Org .*Export"
  16710. (buffer-name b)))))))))
  16711. (delq nil
  16712. (mapcar
  16713. (lambda(b)
  16714. (if (and (funcall filter b)
  16715. (or (not exclude-tmp)
  16716. (not (string-match "tmp" (buffer-name b)))))
  16717. b
  16718. nil))
  16719. (buffer-list)))))
  16720. (defun org-agenda-files (&optional unrestricted archives)
  16721. "Get the list of agenda files.
  16722. Optional UNRESTRICTED means return the full list even if a restriction
  16723. is currently in place.
  16724. When ARCHIVES is t, include all archive files that are really being
  16725. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16726. `org-agenda-archives-mode' is t."
  16727. (let ((files
  16728. (cond
  16729. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16730. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16731. ((listp org-agenda-files) org-agenda-files)
  16732. (t (error "Invalid value of `org-agenda-files'")))))
  16733. (setq files (apply 'append
  16734. (mapcar (lambda (f)
  16735. (if (file-directory-p f)
  16736. (directory-files
  16737. f t org-agenda-file-regexp)
  16738. (list f)))
  16739. files)))
  16740. (when org-agenda-skip-unavailable-files
  16741. (setq files (delq nil
  16742. (mapcar (function
  16743. (lambda (file)
  16744. (and (file-readable-p file) file)))
  16745. files))))
  16746. (when (or (eq archives t)
  16747. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16748. (setq files (org-add-archive-files files)))
  16749. files))
  16750. (defun org-agenda-file-p (&optional file)
  16751. "Return non-nil, if FILE is an agenda file.
  16752. If FILE is omitted, use the file associated with the current
  16753. buffer."
  16754. (let ((fname (or file (buffer-file-name))))
  16755. (and fname
  16756. (member (file-truename fname)
  16757. (mapcar #'file-truename (org-agenda-files t))))))
  16758. (defun org-edit-agenda-file-list ()
  16759. "Edit the list of agenda files.
  16760. Depending on setup, this either uses customize to edit the variable
  16761. `org-agenda-files', or it visits the file that is holding the list. In the
  16762. latter case, the buffer is set up in a way that saving it automatically kills
  16763. the buffer and restores the previous window configuration."
  16764. (interactive)
  16765. (if (stringp org-agenda-files)
  16766. (let ((cw (current-window-configuration)))
  16767. (find-file org-agenda-files)
  16768. (org-set-local 'org-window-configuration cw)
  16769. (org-add-hook 'after-save-hook
  16770. (lambda ()
  16771. (set-window-configuration
  16772. (prog1 org-window-configuration
  16773. (kill-buffer (current-buffer))))
  16774. (org-install-agenda-files-menu)
  16775. (message "New agenda file list installed"))
  16776. nil 'local)
  16777. (message "%s" (substitute-command-keys
  16778. "Edit list and finish with \\[save-buffer]")))
  16779. (customize-variable 'org-agenda-files)))
  16780. (defun org-store-new-agenda-file-list (list)
  16781. "Set new value for the agenda file list and save it correctly."
  16782. (if (stringp org-agenda-files)
  16783. (let ((fe (org-read-agenda-file-list t)) b u)
  16784. (while (setq b (find-buffer-visiting org-agenda-files))
  16785. (kill-buffer b))
  16786. (with-temp-file org-agenda-files
  16787. (insert
  16788. (mapconcat
  16789. (lambda (f) ;; Keep un-expanded entries.
  16790. (if (setq u (assoc f fe))
  16791. (cdr u)
  16792. f))
  16793. list "\n")
  16794. "\n")))
  16795. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16796. (org-insert-mode-line-in-empty-file nil))
  16797. (setq org-agenda-files list)
  16798. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16799. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16800. "Read the list of agenda files from a file.
  16801. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16802. filenames, used by `org-store-new-agenda-file-list' to write back
  16803. un-expanded file names."
  16804. (when (file-directory-p org-agenda-files)
  16805. (error "`org-agenda-files' cannot be a single directory"))
  16806. (when (stringp org-agenda-files)
  16807. (with-temp-buffer
  16808. (insert-file-contents org-agenda-files)
  16809. (mapcar
  16810. (lambda (f)
  16811. (let ((e (expand-file-name (substitute-in-file-name f)
  16812. org-directory)))
  16813. (if pair-with-expansion
  16814. (cons e f)
  16815. e)))
  16816. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16817. ;;;###autoload
  16818. (defun org-cycle-agenda-files ()
  16819. "Cycle through the files in `org-agenda-files'.
  16820. If the current buffer visits an agenda file, find the next one in the list.
  16821. If the current buffer does not, find the first agenda file."
  16822. (interactive)
  16823. (let* ((fs (org-agenda-files t))
  16824. (files (append fs (list (car fs))))
  16825. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16826. file)
  16827. (unless files (user-error "No agenda files"))
  16828. (catch 'exit
  16829. (while (setq file (pop files))
  16830. (if (equal (file-truename file) tcf)
  16831. (when (car files)
  16832. (find-file (car files))
  16833. (throw 'exit t))))
  16834. (find-file (car fs)))
  16835. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16836. (defun org-agenda-file-to-front (&optional to-end)
  16837. "Move/add the current file to the top of the agenda file list.
  16838. If the file is not present in the list, it is added to the front. If it is
  16839. present, it is moved there. With optional argument TO-END, add/move to the
  16840. end of the list."
  16841. (interactive "P")
  16842. (let ((org-agenda-skip-unavailable-files nil)
  16843. (file-alist (mapcar (lambda (x)
  16844. (cons (file-truename x) x))
  16845. (org-agenda-files t)))
  16846. (ctf (file-truename
  16847. (or buffer-file-name
  16848. (user-error "Please save the current buffer to a file"))))
  16849. x had)
  16850. (setq x (assoc ctf file-alist) had x)
  16851. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16852. (if to-end
  16853. (setq file-alist (append (delq x file-alist) (list x)))
  16854. (setq file-alist (cons x (delq x file-alist))))
  16855. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16856. (org-install-agenda-files-menu)
  16857. (message "File %s to %s of agenda file list"
  16858. (if had "moved" "added") (if to-end "end" "front"))))
  16859. (defun org-remove-file (&optional file)
  16860. "Remove current file from the list of files in variable `org-agenda-files'.
  16861. These are the files which are being checked for agenda entries.
  16862. Optional argument FILE means use this file instead of the current."
  16863. (interactive)
  16864. (let* ((org-agenda-skip-unavailable-files nil)
  16865. (file (or file buffer-file-name
  16866. (user-error "Current buffer does not visit a file")))
  16867. (true-file (file-truename file))
  16868. (afile (abbreviate-file-name file))
  16869. (files (delq nil (mapcar
  16870. (lambda (x)
  16871. (if (equal true-file
  16872. (file-truename x))
  16873. nil x))
  16874. (org-agenda-files t)))))
  16875. (if (not (= (length files) (length (org-agenda-files t))))
  16876. (progn
  16877. (org-store-new-agenda-file-list files)
  16878. (org-install-agenda-files-menu)
  16879. (message "Removed from Org Agenda list: %s" afile))
  16880. (message "File was not in list: %s (not removed)" afile))))
  16881. (defun org-file-menu-entry (file)
  16882. (vector file (list 'find-file file) t))
  16883. (defun org-check-agenda-file (file)
  16884. "Make sure FILE exists. If not, ask user what to do."
  16885. (when (not (file-exists-p file))
  16886. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16887. (abbreviate-file-name file))
  16888. (let ((r (downcase (read-char-exclusive))))
  16889. (cond
  16890. ((equal r ?r)
  16891. (org-remove-file file)
  16892. (throw 'nextfile t))
  16893. (t (user-error "Abort"))))))
  16894. (defun org-get-agenda-file-buffer (file)
  16895. "Get an agenda buffer visiting FILE.
  16896. If the buffer needs to be created, add it to the list of buffers
  16897. which might be released later."
  16898. (let ((buf (org-find-base-buffer-visiting file)))
  16899. (if buf
  16900. buf ; just return it
  16901. ;; Make a new buffer and remember it
  16902. (setq buf (find-file-noselect file))
  16903. (if buf (push buf org-agenda-new-buffers))
  16904. buf)))
  16905. (defun org-release-buffers (blist)
  16906. "Release all buffers in list, asking the user for confirmation when needed.
  16907. When a buffer is unmodified, it is just killed. When modified, it is saved
  16908. \(if the user agrees) and then killed."
  16909. (let (buf file)
  16910. (while (setq buf (pop blist))
  16911. (setq file (buffer-file-name buf))
  16912. (when (and (buffer-modified-p buf)
  16913. file
  16914. (y-or-n-p (format "Save file %s? " file)))
  16915. (with-current-buffer buf (save-buffer)))
  16916. (kill-buffer buf))))
  16917. (defun org-agenda-prepare-buffers (files)
  16918. "Create buffers for all agenda files, protect archived trees and comments."
  16919. (interactive)
  16920. (let ((pa '(:org-archived t))
  16921. (pc '(:org-comment t))
  16922. (pall '(:org-archived t :org-comment t))
  16923. (inhibit-read-only t)
  16924. (org-inhibit-startup org-agenda-inhibit-startup)
  16925. (rea (concat ":" org-archive-tag ":"))
  16926. file re pos)
  16927. (setq org-tag-alist-for-agenda nil
  16928. org-tag-groups-alist-for-agenda nil)
  16929. (save-excursion
  16930. (save-restriction
  16931. (while (setq file (pop files))
  16932. (catch 'nextfile
  16933. (if (bufferp file)
  16934. (set-buffer file)
  16935. (org-check-agenda-file file)
  16936. (set-buffer (org-get-agenda-file-buffer file)))
  16937. (widen)
  16938. (org-set-regexps-and-options 'tags-only)
  16939. (setq pos (point))
  16940. (or (memq 'category org-agenda-ignore-properties)
  16941. (org-refresh-category-properties))
  16942. (or (memq 'stats org-agenda-ignore-properties)
  16943. (org-refresh-stats-properties))
  16944. (or (memq 'effort org-agenda-ignore-properties)
  16945. (org-refresh-effort-properties))
  16946. (or (memq 'appt org-agenda-ignore-properties)
  16947. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16948. (setq org-todo-keywords-for-agenda
  16949. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16950. (setq org-done-keywords-for-agenda
  16951. (append org-done-keywords-for-agenda org-done-keywords))
  16952. (setq org-todo-keyword-alist-for-agenda
  16953. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16954. (setq org-tag-alist-for-agenda
  16955. (org-uniquify
  16956. (append org-tag-alist-for-agenda
  16957. org-tag-alist
  16958. org-tag-persistent-alist)))
  16959. (if org-group-tags
  16960. (setq org-tag-groups-alist-for-agenda
  16961. (org-uniquify-alist
  16962. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16963. (org-with-silent-modifications
  16964. (save-excursion
  16965. (remove-text-properties (point-min) (point-max) pall)
  16966. (when org-agenda-skip-archived-trees
  16967. (goto-char (point-min))
  16968. (while (re-search-forward rea nil t)
  16969. (if (org-at-heading-p t)
  16970. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16971. (goto-char (point-min))
  16972. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16973. (while (re-search-forward re nil t)
  16974. (when (save-match-data (org-in-commented-heading-p t))
  16975. (add-text-properties
  16976. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16977. (goto-char pos)))))
  16978. (setq org-todo-keywords-for-agenda
  16979. (org-uniquify org-todo-keywords-for-agenda))
  16980. (setq org-todo-keyword-alist-for-agenda
  16981. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16982. ;;;; CDLaTeX minor mode
  16983. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16984. "Keymap for the minor `org-cdlatex-mode'.")
  16985. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16986. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16987. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16988. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16989. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16990. (defvar org-cdlatex-texmathp-advice-is-done nil
  16991. "Flag remembering if we have applied the advice to texmathp already.")
  16992. (define-minor-mode org-cdlatex-mode
  16993. "Toggle the minor `org-cdlatex-mode'.
  16994. This mode supports entering LaTeX environment and math in LaTeX fragments
  16995. in Org-mode.
  16996. \\{org-cdlatex-mode-map}"
  16997. nil " OCDL" nil
  16998. (when org-cdlatex-mode
  16999. (require 'cdlatex)
  17000. (run-hooks 'cdlatex-mode-hook)
  17001. (cdlatex-compute-tables))
  17002. (unless org-cdlatex-texmathp-advice-is-done
  17003. (setq org-cdlatex-texmathp-advice-is-done t)
  17004. (defadvice texmathp (around org-math-always-on activate)
  17005. "Always return t in org-mode buffers.
  17006. This is because we want to insert math symbols without dollars even outside
  17007. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17008. an embedded LaTeX fragment, let texmathp do its job.
  17009. \\[org-cdlatex-mode-map]"
  17010. (interactive)
  17011. (let (p)
  17012. (cond
  17013. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17014. ((eq this-command 'cdlatex-math-symbol)
  17015. (setq ad-return-value t
  17016. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17017. (t
  17018. (let ((p (org-inside-LaTeX-fragment-p)))
  17019. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17020. (setq ad-return-value t
  17021. texmathp-why '("Org-mode embedded math" . 0))
  17022. (if p ad-do-it)))))))))
  17023. (defun turn-on-org-cdlatex ()
  17024. "Unconditionally turn on `org-cdlatex-mode'."
  17025. (org-cdlatex-mode 1))
  17026. (defun org-try-cdlatex-tab ()
  17027. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17028. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17029. - inside a LaTeX fragment, or
  17030. - after the first word in a line, where an abbreviation expansion could
  17031. insert a LaTeX environment."
  17032. (when org-cdlatex-mode
  17033. (cond
  17034. ;; Before any word on the line: No expansion possible.
  17035. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17036. ;; Just after first word on the line: Expand it. Make sure it
  17037. ;; cannot happen on headlines, though.
  17038. ((save-excursion
  17039. (skip-chars-backward "a-zA-Z0-9*")
  17040. (skip-chars-backward " \t")
  17041. (and (bolp) (not (org-at-heading-p))))
  17042. (cdlatex-tab) t)
  17043. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17044. (defun org-cdlatex-underscore-caret (&optional arg)
  17045. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17046. Revert to the normal definition outside of these fragments."
  17047. (interactive "P")
  17048. (if (org-inside-LaTeX-fragment-p)
  17049. (call-interactively 'cdlatex-sub-superscript)
  17050. (let (org-cdlatex-mode)
  17051. (call-interactively (key-binding (vector last-input-event))))))
  17052. (defun org-cdlatex-math-modify (&optional arg)
  17053. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17054. Revert to the normal definition outside of these fragments."
  17055. (interactive "P")
  17056. (if (org-inside-LaTeX-fragment-p)
  17057. (call-interactively 'cdlatex-math-modify)
  17058. (let (org-cdlatex-mode)
  17059. (call-interactively (key-binding (vector last-input-event))))))
  17060. (defun org-cdlatex-environment-indent (&optional environment item)
  17061. "Execute `cdlatex-environment' and indent the inserted environment.
  17062. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17063. The inserted environment is indented to current indentation
  17064. unless point is at the beginning of the line, in which the
  17065. environment remains unintended."
  17066. (interactive)
  17067. ;; cdlatex-environment always return nil. Therefore, capture output
  17068. ;; first and determine if an environment was selected.
  17069. (let* ((beg (point-marker))
  17070. (end (copy-marker (point) t))
  17071. (inserted (progn
  17072. (ignore-errors (cdlatex-environment environment item))
  17073. (< beg end)))
  17074. ;; Figure out how many lines to move forward after the
  17075. ;; environment has been inserted.
  17076. (lines (when inserted
  17077. (save-excursion
  17078. (- (loop while (< beg (point))
  17079. with x = 0
  17080. do (forward-line -1)
  17081. (incf x)
  17082. finally return x)
  17083. (if (progn (goto-char beg)
  17084. (and (progn (skip-chars-forward " \t") (eolp))
  17085. (progn (skip-chars-backward " \t") (bolp))))
  17086. 1 0)))))
  17087. (env (org-trim (delete-and-extract-region beg end))))
  17088. (when inserted
  17089. ;; Get indentation of next line unless at column 0.
  17090. (let ((ind (if (bolp) 0
  17091. (save-excursion
  17092. (org-return-indent)
  17093. (prog1 (org-get-indentation)
  17094. (when (progn (skip-chars-forward " \t") (eolp))
  17095. (delete-region beg (point)))))))
  17096. (bol (progn (skip-chars-backward " \t") (bolp))))
  17097. ;; Insert a newline before environment unless at column zero
  17098. ;; to "escape" the current line. Insert a newline if
  17099. ;; something is one the same line as \end{ENVIRONMENT}.
  17100. (insert
  17101. (concat (unless bol "\n") env
  17102. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17103. (unless (zerop ind)
  17104. (save-excursion
  17105. (goto-char beg)
  17106. (while (< (point) end)
  17107. (unless (eolp) (org-indent-line-to ind))
  17108. (forward-line))))
  17109. (goto-char beg)
  17110. (forward-line lines)
  17111. (org-indent-line-to ind)))
  17112. (set-marker beg nil)
  17113. (set-marker end nil)))
  17114. ;;;; LaTeX fragments
  17115. (defun org-inside-LaTeX-fragment-p ()
  17116. "Test if point is inside a LaTeX fragment.
  17117. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17118. sequence appearing also before point.
  17119. Even though the matchers for math are configurable, this function assumes
  17120. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17121. delimiters are skipped when they have been removed by customization.
  17122. The return value is nil, or a cons cell with the delimiter and the
  17123. position of this delimiter.
  17124. This function does a reasonably good job, but can locally be fooled by
  17125. for example currency specifications. For example it will assume being in
  17126. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17127. fragments that are properly closed, but during editing, we have to live
  17128. with the uncertainty caused by missing closing delimiters. This function
  17129. looks only before point, not after."
  17130. (catch 'exit
  17131. (let ((pos (point))
  17132. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17133. (lim (progn
  17134. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17135. (point)))
  17136. dd-on str (start 0) m re)
  17137. (goto-char pos)
  17138. (when dodollar
  17139. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17140. re (nth 1 (assoc "$" org-latex-regexps)))
  17141. (while (string-match re str start)
  17142. (cond
  17143. ((= (match-end 0) (length str))
  17144. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17145. ((= (match-end 0) (- (length str) 5))
  17146. (throw 'exit nil))
  17147. (t (setq start (match-end 0))))))
  17148. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17149. (goto-char pos)
  17150. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17151. (and (match-beginning 2) (throw 'exit nil))
  17152. ;; count $$
  17153. (while (re-search-backward "\\$\\$" lim t)
  17154. (setq dd-on (not dd-on)))
  17155. (goto-char pos)
  17156. (if dd-on (cons "$$" m))))))
  17157. (defun org-inside-latex-macro-p ()
  17158. "Is point inside a LaTeX macro or its arguments?"
  17159. (save-match-data
  17160. (org-in-regexp
  17161. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17162. (defvar org-latex-fragment-image-overlays nil
  17163. "List of overlays carrying the images of latex fragments.")
  17164. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17165. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17166. "Remove all overlays with LaTeX fragment images in current buffer.
  17167. When optional arguments BEG and END are non-nil, remove all
  17168. overlays between them instead. Return t when some overlays were
  17169. removed, nil otherwise."
  17170. (let (removedp)
  17171. (setq org-latex-fragment-image-overlays
  17172. (let ((beg (or beg (point-min)))
  17173. (end (or end (point-max))))
  17174. (org-remove-if
  17175. (lambda (o)
  17176. (and (>= (overlay-start o) beg)
  17177. (<= (overlay-end o) end)
  17178. (progn (delete-overlay o)
  17179. (or removedp (setq removedp t)))))
  17180. org-latex-fragment-image-overlays)))
  17181. removedp))
  17182. (define-obsolete-function-alias
  17183. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17184. (defun org-toggle-latex-fragment (&optional arg)
  17185. "Preview the LaTeX fragment at point, or all locally or globally.
  17186. If the cursor is on a LaTeX fragment, create the image and overlay
  17187. it over the source code, if there is none. Remove it otherwise.
  17188. If there is no fragment at point, display all fragments in the
  17189. current section.
  17190. With prefix ARG, preview or clear image for all fragments in the
  17191. current subtree or in the whole buffer when used before the first
  17192. headline. With a double prefix ARG \\[universal-argument] \
  17193. \\[universal-argument] preview or clear images
  17194. for all fragments in the buffer."
  17195. (interactive "P")
  17196. (unless (buffer-file-name (buffer-base-buffer))
  17197. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17198. (when (display-graphic-p)
  17199. (catch 'exit
  17200. (save-excursion
  17201. (let ((window-start (window-start)) msg)
  17202. (save-restriction
  17203. (cond
  17204. ((or (equal arg '(16))
  17205. (and (equal arg '(4))
  17206. (org-with-limited-levels (org-before-first-heading-p))))
  17207. (if (org-remove-latex-fragment-image-overlays)
  17208. (progn (message "LaTeX fragments images removed from buffer")
  17209. (throw 'exit nil))
  17210. (setq msg "Creating images for buffer...")))
  17211. ((equal arg '(4))
  17212. (org-with-limited-levels (org-back-to-heading t))
  17213. (let ((beg (point))
  17214. (end (progn (org-end-of-subtree t) (point))))
  17215. (if (org-remove-latex-fragment-image-overlays beg end)
  17216. (progn
  17217. (message "LaTeX fragment images removed from subtree")
  17218. (throw 'exit nil))
  17219. (setq msg "Creating images for subtree...")
  17220. (narrow-to-region beg end))))
  17221. ((let ((datum (org-element-context)))
  17222. (when (memq (org-element-type datum)
  17223. '(latex-environment latex-fragment))
  17224. (let* ((beg (org-element-property :begin datum))
  17225. (end (org-element-property :end datum)))
  17226. (if (org-remove-latex-fragment-image-overlays beg end)
  17227. (progn (message "LaTeX fragment image removed")
  17228. (throw 'exit nil))
  17229. (narrow-to-region beg end)
  17230. (setq msg "Creating image..."))))))
  17231. (t
  17232. (org-with-limited-levels
  17233. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17234. (outline-previous-heading)
  17235. (point)))
  17236. (end (progn (outline-next-heading) (point))))
  17237. (if (org-remove-latex-fragment-image-overlays beg end)
  17238. (progn
  17239. (message "LaTeX fragment images removed from section")
  17240. (throw 'exit nil))
  17241. (setq msg "Creating images for section...")
  17242. (narrow-to-region beg end))))))
  17243. (let ((file (buffer-file-name (buffer-base-buffer))))
  17244. (org-format-latex
  17245. (concat org-latex-preview-ltxpng-directory
  17246. (file-name-sans-extension (file-name-nondirectory file)))
  17247. ;; Emacs cannot overlay images from remote hosts.
  17248. ;; Create it in `temporary-file-directory' instead.
  17249. (if (file-remote-p file) temporary-file-directory
  17250. default-directory)
  17251. 'overlays msg 'forbuffer
  17252. org-latex-create-formula-image-program)))
  17253. ;; Work around a bug that doesn't restore window's start
  17254. ;; when widening back the buffer.
  17255. (set-window-start nil window-start)
  17256. (message (concat msg "done")))))))
  17257. (defun org-format-latex
  17258. (prefix &optional dir overlays msg forbuffer processing-type)
  17259. "Replace LaTeX fragments with links to an image, and produce images.
  17260. When optional argument OVERLAYS is non-nil, display the image on
  17261. top of the fragment instead of replacing it.
  17262. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17263. Some of the options can be changed using the variable
  17264. `org-format-latex-options', which see."
  17265. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17266. (unless (eq processing-type 'verbatim)
  17267. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17268. (cnt 0)
  17269. checkdir-flag)
  17270. (goto-char (point-min))
  17271. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17272. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17273. (overlay-recenter (point-max)))
  17274. (while (re-search-forward math-regexp nil t)
  17275. (unless (and overlays
  17276. (eq (get-char-property (point) 'org-overlay-type)
  17277. 'org-latex-overlay))
  17278. (let* ((context (org-element-context))
  17279. (type (org-element-type context)))
  17280. (when (memq type '(latex-environment latex-fragment))
  17281. (let ((block-type (eq type 'latex-environment))
  17282. (value (org-element-property :value context))
  17283. (beg (org-element-property :begin context))
  17284. (end (save-excursion
  17285. (goto-char (org-element-property :end context))
  17286. (skip-chars-backward " \r\t\n")
  17287. (point))))
  17288. (case processing-type
  17289. (mathjax
  17290. ;; Prepare for MathJax processing.
  17291. (if (eq (char-after beg) ?$)
  17292. (save-excursion
  17293. (delete-region beg end)
  17294. (insert "\\(" (substring value 1 -1) "\\)"))
  17295. (goto-char end)))
  17296. ((dvipng imagemagick)
  17297. ;; Process to an image.
  17298. (incf cnt)
  17299. (goto-char beg)
  17300. (let* ((face (face-at-point))
  17301. ;; Get the colors from the face at point.
  17302. (fg
  17303. (let ((color (plist-get org-format-latex-options
  17304. :foreground)))
  17305. (if (and forbuffer (eq color 'auto))
  17306. (face-attribute face :foreground nil 'default)
  17307. color)))
  17308. (bg
  17309. (let ((color (plist-get org-format-latex-options
  17310. :background)))
  17311. (if (and forbuffer (eq color 'auto))
  17312. (face-attribute face :background nil 'default)
  17313. color)))
  17314. (hash (sha1 (prin1-to-string
  17315. (list org-format-latex-header
  17316. org-latex-default-packages-alist
  17317. org-latex-packages-alist
  17318. org-format-latex-options
  17319. forbuffer value fg bg))))
  17320. (absprefix (expand-file-name prefix dir))
  17321. (linkfile (format "%s_%s.png" prefix hash))
  17322. (movefile (format "%s_%s.png" absprefix hash))
  17323. (sep (and block-type "\n\n"))
  17324. (link (concat sep "[[file:" linkfile "]]" sep))
  17325. (options
  17326. (org-combine-plists
  17327. org-format-latex-options
  17328. `(:foreground ,fg :background ,bg))))
  17329. (when msg (message msg cnt))
  17330. (unless checkdir-flag ; Ensure the directory exists.
  17331. (setq checkdir-flag t)
  17332. (let ((todir (file-name-directory absprefix)))
  17333. (unless (file-directory-p todir)
  17334. (make-directory todir t))))
  17335. (unless (file-exists-p movefile)
  17336. (org-create-formula-image
  17337. value movefile options forbuffer processing-type))
  17338. (if overlays
  17339. (progn
  17340. (dolist (o (overlays-in beg end))
  17341. (when (eq (overlay-get o 'org-overlay-type)
  17342. 'org-latex-overlay)
  17343. (delete-overlay o)))
  17344. (let ((ov (make-overlay beg end)))
  17345. (overlay-put ov
  17346. 'org-overlay-type
  17347. 'org-latex-overlay)
  17348. (overlay-put ov 'evaporate t)
  17349. (if (featurep 'xemacs)
  17350. (progn
  17351. (overlay-put ov 'invisible t)
  17352. (overlay-put
  17353. ov 'end-glyph
  17354. (make-glyph
  17355. (vector 'png :file movefile))))
  17356. (overlay-put
  17357. ov 'display
  17358. (list 'image
  17359. :type 'png
  17360. :file movefile
  17361. :ascent 'center)))
  17362. (push ov org-latex-fragment-image-overlays))
  17363. (goto-char end))
  17364. (delete-region beg end)
  17365. (insert
  17366. (org-add-props link
  17367. (list 'org-latex-src
  17368. (replace-regexp-in-string "\"" "" value)
  17369. 'org-latex-src-embed-type
  17370. (if block-type 'paragraph 'character)))))))
  17371. (mathml
  17372. ;; Process to MathML.
  17373. (unless (org-format-latex-mathml-available-p)
  17374. (user-error "LaTeX to MathML converter not configured"))
  17375. (incf cnt)
  17376. (when msg (message msg cnt))
  17377. (goto-char beg)
  17378. (delete-region beg end)
  17379. (insert (org-format-latex-as-mathml
  17380. value block-type prefix dir)))
  17381. (otherwise
  17382. (error "Unknown conversion type %s for LaTeX fragments"
  17383. processing-type)))))))))))
  17384. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17385. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17386. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17387. sucessful, return the portion between \"<math...> </math>\"
  17388. elements otherwise return nil. When MATHML-FILE is specified,
  17389. write the results in to that file. When invoked as an
  17390. interactive command, prompt for LATEX-FRAG, with initial value
  17391. set to the current active region and echo the results for user
  17392. inspection."
  17393. (interactive (list (let ((frag (when (org-region-active-p)
  17394. (buffer-substring-no-properties
  17395. (region-beginning) (region-end)))))
  17396. (read-string "LaTeX Fragment: " frag nil frag))))
  17397. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17398. (let* ((tmp-in-file (file-relative-name
  17399. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17400. (ignore (write-region latex-frag nil tmp-in-file))
  17401. (tmp-out-file (file-relative-name
  17402. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17403. (cmd (format-spec
  17404. org-latex-to-mathml-convert-command
  17405. `((?j . ,(shell-quote-argument
  17406. (expand-file-name org-latex-to-mathml-jar-file)))
  17407. (?I . ,(shell-quote-argument tmp-in-file))
  17408. (?i . ,latex-frag)
  17409. (?o . ,(shell-quote-argument tmp-out-file)))))
  17410. mathml shell-command-output)
  17411. (when (org-called-interactively-p 'any)
  17412. (unless (org-format-latex-mathml-available-p)
  17413. (user-error "LaTeX to MathML converter not configured")))
  17414. (message "Running %s" cmd)
  17415. (setq shell-command-output (shell-command-to-string cmd))
  17416. (setq mathml
  17417. (when (file-readable-p tmp-out-file)
  17418. (with-current-buffer (find-file-noselect tmp-out-file t)
  17419. (goto-char (point-min))
  17420. (when (re-search-forward
  17421. (concat
  17422. (regexp-quote
  17423. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17424. "[^>]*?>"
  17425. "\\(.\\|\n\\)*"
  17426. "</math>")
  17427. nil t)
  17428. (prog1 (match-string 0) (kill-buffer))))))
  17429. (cond
  17430. (mathml
  17431. (setq mathml
  17432. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17433. (when mathml-file
  17434. (write-region mathml nil mathml-file))
  17435. (when (org-called-interactively-p 'any)
  17436. (message mathml)))
  17437. ((message "LaTeX to MathML conversion failed")
  17438. (message shell-command-output)))
  17439. (delete-file tmp-in-file)
  17440. (when (file-exists-p tmp-out-file)
  17441. (delete-file tmp-out-file))
  17442. mathml))
  17443. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17444. prefix &optional dir)
  17445. "Use `org-create-math-formula' but check local cache first."
  17446. (let* ((absprefix (expand-file-name prefix dir))
  17447. (print-length nil) (print-level nil)
  17448. (formula-id (concat
  17449. "formula-"
  17450. (sha1
  17451. (prin1-to-string
  17452. (list latex-frag
  17453. org-latex-to-mathml-convert-command)))))
  17454. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17455. (formula-cache-dir (file-name-directory formula-cache)))
  17456. (unless (file-directory-p formula-cache-dir)
  17457. (make-directory formula-cache-dir t))
  17458. (unless (file-exists-p formula-cache)
  17459. (org-create-math-formula latex-frag formula-cache))
  17460. (if (file-exists-p formula-cache)
  17461. ;; Successful conversion. Return the link to MathML file.
  17462. (org-add-props
  17463. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17464. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17465. 'org-latex-src-embed-type (if latex-frag-type
  17466. 'paragraph 'character)))
  17467. ;; Failed conversion. Return the LaTeX fragment verbatim
  17468. latex-frag)))
  17469. (defun org-create-formula-image (string tofile options buffer &optional type)
  17470. "Create an image from LaTeX source using dvipng or convert.
  17471. This function calls either `org-create-formula-image-with-dvipng'
  17472. or `org-create-formula-image-with-imagemagick' depending on the
  17473. value of `org-latex-create-formula-image-program' or on the value
  17474. of the optional TYPE variable.
  17475. Note: ultimately these two function should be combined as they
  17476. share a good deal of logic."
  17477. (org-check-external-command
  17478. "latex" "needed to convert LaTeX fragments to images")
  17479. (funcall
  17480. (case (or type org-latex-create-formula-image-program)
  17481. ('dvipng
  17482. (org-check-external-command
  17483. "dvipng" "needed to convert LaTeX fragments to images")
  17484. 'org-create-formula-image-with-dvipng)
  17485. ('imagemagick
  17486. (org-check-external-command
  17487. "convert" "you need to install imagemagick")
  17488. 'org-create-formula-image-with-imagemagick)
  17489. (t (error
  17490. "Invalid value of `org-latex-create-formula-image-program'")))
  17491. string tofile options buffer))
  17492. (declare-function org-export-get-backend "ox" (name))
  17493. (declare-function org-export--get-global-options "ox" (&optional backend))
  17494. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17495. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17496. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17497. (defun org-create-formula--latex-header ()
  17498. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17499. (let ((info (org-combine-plists (org-export--get-global-options
  17500. (org-export-get-backend 'latex))
  17501. (org-export--get-inbuffer-options
  17502. (org-export-get-backend 'latex)))))
  17503. (org-latex-guess-babel-language
  17504. (org-latex-guess-inputenc
  17505. (org-splice-latex-header
  17506. org-format-latex-header
  17507. org-latex-default-packages-alist
  17508. org-latex-packages-alist t
  17509. (plist-get info :latex-header)))
  17510. info)))
  17511. (defun org--get-display-dpi ()
  17512. "Get the DPI of the display.
  17513. Assumes that the display has the same pixel width in the
  17514. horizontal and vertical directions."
  17515. (if (display-graphic-p)
  17516. (round (/ (display-pixel-height)
  17517. (/ (display-mm-height) 25.4)))
  17518. (error "Attempt to calculate the dpi of a non-graphic display")))
  17519. ;; This function borrows from Ganesh Swami's latex2png.el
  17520. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17521. "This calls dvipng."
  17522. (require 'ox-latex)
  17523. (let* ((tmpdir (if (featurep 'xemacs)
  17524. (temp-directory)
  17525. temporary-file-directory))
  17526. (texfilebase (make-temp-name
  17527. (expand-file-name "orgtex" tmpdir)))
  17528. (texfile (concat texfilebase ".tex"))
  17529. (dvifile (concat texfilebase ".dvi"))
  17530. (pngfile (concat texfilebase ".png"))
  17531. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17532. ;; This assumes that the display has the same pixel width in
  17533. ;; the horizontal and vertical directions
  17534. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17535. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17536. "Black"))
  17537. (bg (or (plist-get options (if buffer :background :html-background))
  17538. "Transparent")))
  17539. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17540. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17541. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17542. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17543. (let ((latex-header (org-create-formula--latex-header)))
  17544. (with-temp-file texfile
  17545. (insert latex-header)
  17546. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17547. (let ((dir default-directory))
  17548. (ignore-errors
  17549. (cd tmpdir)
  17550. (call-process "latex" nil nil nil texfile))
  17551. (cd dir))
  17552. (if (not (file-exists-p dvifile))
  17553. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17554. (ignore-errors
  17555. (if (featurep 'xemacs)
  17556. (call-process "dvipng" nil nil nil
  17557. "-fg" fg "-bg" bg
  17558. "-T" "tight"
  17559. "-o" pngfile
  17560. dvifile)
  17561. (call-process "dvipng" nil nil nil
  17562. "-fg" fg "-bg" bg
  17563. "-D" dpi
  17564. ;;"-x" scale "-y" scale
  17565. "-T" "tight"
  17566. "-o" pngfile
  17567. dvifile)))
  17568. (if (not (file-exists-p pngfile))
  17569. (if org-format-latex-signal-error
  17570. (error "Failed to create png file from %s" texfile)
  17571. (message "Failed to create png file from %s" texfile)
  17572. nil)
  17573. ;; Use the requested file name and clean up
  17574. (copy-file pngfile tofile 'replace)
  17575. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17576. (if (file-exists-p (concat texfilebase e))
  17577. (delete-file (concat texfilebase e))))
  17578. pngfile))))
  17579. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17580. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17581. "This calls convert, which is included into imagemagick."
  17582. (require 'ox-latex)
  17583. (let* ((tmpdir (if (featurep 'xemacs)
  17584. (temp-directory)
  17585. temporary-file-directory))
  17586. (texfilebase (make-temp-name
  17587. (expand-file-name "orgtex" tmpdir)))
  17588. (texfile (concat texfilebase ".tex"))
  17589. (pdffile (concat texfilebase ".pdf"))
  17590. (pngfile (concat texfilebase ".png"))
  17591. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17592. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17593. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17594. "black"))
  17595. (bg (or (plist-get options (if buffer :background :html-background))
  17596. "white")))
  17597. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17598. (setq fg (org-latex-color-format fg)))
  17599. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17600. (setq bg (org-latex-color-format
  17601. (if (string= bg "Transparent") "white" bg))))
  17602. (let ((latex-header (org-create-formula--latex-header)))
  17603. (with-temp-file texfile
  17604. (insert latex-header)
  17605. (insert "\n\\begin{document}\n"
  17606. "\\definecolor{fg}{rgb}{" fg "}\n"
  17607. "\\definecolor{bg}{rgb}{" bg "}\n"
  17608. "\n\\pagecolor{bg}\n"
  17609. "\n{\\color{fg}\n"
  17610. string
  17611. "\n}\n"
  17612. "\n\\end{document}\n")))
  17613. (org-latex-compile texfile t)
  17614. (if (not (file-exists-p pdffile))
  17615. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17616. (ignore-errors
  17617. (if (featurep 'xemacs)
  17618. (call-process "convert" nil nil nil
  17619. "-density" "96"
  17620. "-trim"
  17621. "-antialias"
  17622. pdffile
  17623. "-quality" "100"
  17624. ;; "-sharpen" "0x1.0"
  17625. pngfile)
  17626. (call-process "convert" nil nil nil
  17627. "-density" dpi
  17628. "-trim"
  17629. "-antialias"
  17630. pdffile
  17631. "-quality" "100"
  17632. ;; "-sharpen" "0x1.0"
  17633. pngfile)))
  17634. (if (not (file-exists-p pngfile))
  17635. (if org-format-latex-signal-error
  17636. (error "Failed to create png file from %s" texfile)
  17637. (message "Failed to create png file from %s" texfile)
  17638. nil)
  17639. ;; Use the requested file name and clean up
  17640. (copy-file pngfile tofile 'replace)
  17641. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17642. (if (file-exists-p (concat texfilebase e))
  17643. (delete-file (concat texfilebase e))))
  17644. pngfile))))
  17645. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17646. "Fill a LaTeX header template TPL.
  17647. In the template, the following place holders will be recognized:
  17648. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17649. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17650. [PACKAGES] \\usepackage statements for PKG
  17651. [NO-PACKAGES] do not include PKG
  17652. [EXTRA] the string EXTRA
  17653. [NO-EXTRA] do not include EXTRA
  17654. For backward compatibility, if both the positive and the negative place
  17655. holder is missing, the positive one (without the \"NO-\") will be
  17656. assumed to be present at the end of the template.
  17657. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17658. EXTRA is a string.
  17659. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17660. (let (rpl (end ""))
  17661. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17662. (setq rpl (if (or (match-end 1) (not def-pkg))
  17663. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17664. tpl (replace-match rpl t t tpl))
  17665. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17666. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17667. (setq rpl (if (or (match-end 1) (not pkg))
  17668. "" (org-latex-packages-to-string pkg snippets-p t))
  17669. tpl (replace-match rpl t t tpl))
  17670. (if pkg (setq end
  17671. (concat end "\n"
  17672. (org-latex-packages-to-string pkg snippets-p)))))
  17673. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17674. (setq rpl (if (or (match-end 1) (not extra))
  17675. "" (concat extra "\n"))
  17676. tpl (replace-match rpl t t tpl))
  17677. (if (and extra (string-match "\\S-" extra))
  17678. (setq end (concat end "\n" extra))))
  17679. (if (string-match "\\S-" end)
  17680. (concat tpl "\n" end)
  17681. tpl)))
  17682. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17683. "Turn an alist of packages into a string with the \\usepackage macros."
  17684. (setq pkg (mapconcat (lambda(p)
  17685. (cond
  17686. ((stringp p) p)
  17687. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17688. (format "%% Package %s omitted" (cadr p)))
  17689. ((equal "" (car p))
  17690. (format "\\usepackage{%s}" (cadr p)))
  17691. (t
  17692. (format "\\usepackage[%s]{%s}"
  17693. (car p) (cadr p)))))
  17694. pkg
  17695. "\n"))
  17696. (if newline (concat pkg "\n") pkg))
  17697. (defun org-dvipng-color (attr)
  17698. "Return a RGB color specification for dvipng."
  17699. (apply 'format "rgb %s %s %s"
  17700. (mapcar 'org-normalize-color
  17701. (if (featurep 'xemacs)
  17702. (color-rgb-components
  17703. (face-property 'default
  17704. (cond ((eq attr :foreground) 'foreground)
  17705. ((eq attr :background) 'background))))
  17706. (color-values (face-attribute 'default attr nil))))))
  17707. (defun org-dvipng-color-format (color-name)
  17708. "Convert COLOR-NAME to a RGB color value for dvipng."
  17709. (apply 'format "rgb %s %s %s"
  17710. (mapcar 'org-normalize-color
  17711. (color-values color-name))))
  17712. (defun org-latex-color (attr)
  17713. "Return a RGB color for the LaTeX color package."
  17714. (apply 'format "%s,%s,%s"
  17715. (mapcar 'org-normalize-color
  17716. (if (featurep 'xemacs)
  17717. (color-rgb-components
  17718. (face-property 'default
  17719. (cond ((eq attr :foreground) 'foreground)
  17720. ((eq attr :background) 'background))))
  17721. (color-values (face-attribute 'default attr nil))))))
  17722. (defun org-latex-color-format (color-name)
  17723. "Convert COLOR-NAME to a RGB color value."
  17724. (apply 'format "%s,%s,%s"
  17725. (mapcar 'org-normalize-color
  17726. (color-values color-name))))
  17727. (defun org-normalize-color (value)
  17728. "Return string to be used as color value for an RGB component."
  17729. (format "%g" (/ value 65535.0)))
  17730. ;; Image display
  17731. (defvar org-inline-image-overlays nil)
  17732. (make-variable-buffer-local 'org-inline-image-overlays)
  17733. (defun org-toggle-inline-images (&optional include-linked)
  17734. "Toggle the display of inline images.
  17735. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17736. (interactive "P")
  17737. (if org-inline-image-overlays
  17738. (progn
  17739. (org-remove-inline-images)
  17740. (when (org-called-interactively-p 'interactive)
  17741. (message "Inline image display turned off")))
  17742. (org-display-inline-images include-linked)
  17743. (when (org-called-interactively-p 'interactive)
  17744. (message (if org-inline-image-overlays
  17745. (format "%d images displayed inline"
  17746. (length org-inline-image-overlays))
  17747. "No images to display inline")))))
  17748. (defun org-redisplay-inline-images ()
  17749. "Refresh the display of inline images."
  17750. (interactive)
  17751. (if (not org-inline-image-overlays)
  17752. (org-toggle-inline-images)
  17753. (org-toggle-inline-images)
  17754. (org-toggle-inline-images)))
  17755. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17756. "Display inline images.
  17757. An inline image is a link which follows either of these
  17758. conventions:
  17759. 1. Its path is a file with an extension matching return value
  17760. from `image-file-name-regexp' and it has no contents.
  17761. 2. Its description consists in a single link of the previous
  17762. type.
  17763. When optional argument INCLUDE-LINKED is non-nil, also links with
  17764. a text description part will be inlined. This can be nice for
  17765. a quick look at those images, but it does not reflect what
  17766. exported files will look like.
  17767. When optional argument REFRESH is non-nil, refresh existing
  17768. images between BEG and END. This will create new image displays
  17769. only if necessary. BEG and END default to the buffer
  17770. boundaries."
  17771. (interactive "P")
  17772. (when (display-graphic-p)
  17773. (unless refresh
  17774. (org-remove-inline-images)
  17775. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17776. (org-with-wide-buffer
  17777. (goto-char (or beg (point-min)))
  17778. (let ((case-fold-search t)
  17779. (file-extension-re (org-image-file-name-regexp)))
  17780. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17781. (let ((link (save-match-data (org-element-context))))
  17782. ;; Check if we're at an inline image.
  17783. (when (and (equal (org-element-property :type link) "file")
  17784. (or include-linked
  17785. (not (org-element-property :contents-begin link)))
  17786. (let ((parent (org-element-property :parent link)))
  17787. (or (not (eq (org-element-type parent) 'link))
  17788. (not (cdr (org-element-contents parent)))))
  17789. (org-string-match-p file-extension-re
  17790. (org-element-property :path link)))
  17791. (let ((file (expand-file-name (org-element-property :path link))))
  17792. (when (file-exists-p file)
  17793. (let ((width
  17794. ;; Apply `org-image-actual-width' specifications.
  17795. (cond
  17796. ((not (image-type-available-p 'imagemagick)) nil)
  17797. ((eq org-image-actual-width t) nil)
  17798. ((listp org-image-actual-width)
  17799. (or
  17800. ;; First try to find a width among
  17801. ;; attributes associated to the paragraph
  17802. ;; containing link.
  17803. (let ((paragraph
  17804. (let ((e link))
  17805. (while (and (setq e (org-element-property
  17806. :parent e))
  17807. (not (eq (org-element-type e)
  17808. 'paragraph))))
  17809. e)))
  17810. (when paragraph
  17811. (save-excursion
  17812. (goto-char (org-element-property :begin paragraph))
  17813. (when
  17814. (re-search-forward
  17815. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17816. (org-element-property
  17817. :post-affiliated paragraph)
  17818. t)
  17819. (string-to-number (match-string 1))))))
  17820. ;; Otherwise, fall-back to provided number.
  17821. (car org-image-actual-width)))
  17822. ((numberp org-image-actual-width)
  17823. org-image-actual-width)))
  17824. (old (get-char-property-and-overlay
  17825. (org-element-property :begin link)
  17826. 'org-image-overlay)))
  17827. (if (and (car-safe old) refresh)
  17828. (image-refresh (overlay-get (cdr old) 'display))
  17829. (let ((image (create-image file
  17830. (and width 'imagemagick)
  17831. nil
  17832. :width width)))
  17833. (when image
  17834. (let* ((link
  17835. ;; If inline image is the description
  17836. ;; of another link, be sure to
  17837. ;; consider the latter as the one to
  17838. ;; apply the overlay on.
  17839. (let ((parent
  17840. (org-element-property :parent link)))
  17841. (if (eq (org-element-type parent) 'link)
  17842. parent
  17843. link)))
  17844. (ov (make-overlay
  17845. (org-element-property :begin link)
  17846. (progn
  17847. (goto-char
  17848. (org-element-property :end link))
  17849. (skip-chars-backward " \t")
  17850. (point)))))
  17851. (overlay-put ov 'display image)
  17852. (overlay-put ov 'face 'default)
  17853. (overlay-put ov 'org-image-overlay t)
  17854. (overlay-put
  17855. ov 'modification-hooks
  17856. (list 'org-display-inline-remove-overlay))
  17857. (push ov org-inline-image-overlays)))))))))))))))
  17858. (define-obsolete-function-alias
  17859. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17860. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17861. "Remove inline-display overlay if a corresponding region is modified."
  17862. (let ((inhibit-modification-hooks t))
  17863. (when (and ov after)
  17864. (delete ov org-inline-image-overlays)
  17865. (delete-overlay ov))))
  17866. (defun org-remove-inline-images ()
  17867. "Remove inline display of images."
  17868. (interactive)
  17869. (mapc 'delete-overlay org-inline-image-overlays)
  17870. (setq org-inline-image-overlays nil))
  17871. ;;;; Key bindings
  17872. ;; Outline functions from `outline-mode-prefix-map'
  17873. ;; that can be remapped in Org:
  17874. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17875. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17876. (define-key org-mode-map [remap outline-forward-same-level]
  17877. 'org-forward-heading-same-level)
  17878. (define-key org-mode-map [remap outline-backward-same-level]
  17879. 'org-backward-heading-same-level)
  17880. (define-key org-mode-map [remap show-branches]
  17881. 'org-kill-note-or-show-branches)
  17882. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17883. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17884. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17885. (define-key org-mode-map [remap outline-next-visible-heading]
  17886. 'org-next-visible-heading)
  17887. (define-key org-mode-map [remap outline-previous-visible-heading]
  17888. 'org-previous-visible-heading)
  17889. ;; Outline functions from `outline-mode-prefix-map' that can not
  17890. ;; be remapped in Org:
  17891. ;; - the column "key binding" shows whether the Outline function is still
  17892. ;; available in Org mode on the same key that it has been bound to in
  17893. ;; Outline mode:
  17894. ;; - "overridden": key used for a different functionality in Org mode
  17895. ;; - else: key still bound to the same Outline function in Org mode
  17896. ;; | Outline function | key binding | Org replacement |
  17897. ;; |------------------------------------+-------------+--------------------------|
  17898. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  17899. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  17900. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17901. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17902. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17903. ;; | `show-entry' | overridden | no replacement |
  17904. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17905. ;; | `show-branches' | `C-c C-k' | still same function |
  17906. ;; | `show-subtree' | overridden | visibility cycling |
  17907. ;; | `show-all' | overridden | no replacement |
  17908. ;; | `hide-subtree' | overridden | visibility cycling |
  17909. ;; | `hide-body' | overridden | no replacement |
  17910. ;; | `hide-entry' | overridden | visibility cycling |
  17911. ;; | `hide-leaves' | overridden | no replacement |
  17912. ;; | `hide-sublevels' | overridden | no replacement |
  17913. ;; | `hide-other' | overridden | no replacement |
  17914. ;; Make `C-c C-x' a prefix key
  17915. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17916. ;; TAB key with modifiers
  17917. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17918. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17919. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17920. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17921. ;; The following line is necessary under Suse GNU/Linux
  17922. (unless (featurep 'xemacs)
  17923. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17924. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17925. (define-key org-mode-map [backtab] 'org-shifttab)
  17926. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17927. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17928. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17929. ;; Cursor keys with modifiers
  17930. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17931. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17932. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17933. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17934. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17935. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17936. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17937. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17938. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17939. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17940. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17941. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17942. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17943. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17944. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17945. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17946. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17947. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17948. ;; Babel keys
  17949. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17950. (mapc (lambda (pair)
  17951. (define-key org-babel-map (car pair) (cdr pair)))
  17952. org-babel-key-bindings)
  17953. ;;; Extra keys for tty access.
  17954. ;; We only set them when really needed because otherwise the
  17955. ;; menus don't show the simple keys
  17956. (when (or org-use-extra-keys
  17957. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17958. (not window-system))
  17959. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17960. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17961. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17962. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17963. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17964. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17965. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17966. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17967. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17968. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17969. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17970. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17971. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17972. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17973. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17974. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17975. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17976. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17977. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17978. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17979. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17980. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17981. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17982. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17983. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17984. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17985. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17986. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17987. ;; All the other keys
  17988. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17989. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17990. (if (boundp 'narrow-map)
  17991. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17992. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17993. (if (boundp 'narrow-map)
  17994. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17995. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17996. (if (boundp 'narrow-map)
  17997. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17998. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17999. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18000. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18001. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18002. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18003. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18004. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18005. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18006. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18007. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18008. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18009. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  18010. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18011. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18012. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18013. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18014. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18015. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18016. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18017. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18018. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18019. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18020. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18021. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18022. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18023. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18024. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18025. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18026. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18027. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18028. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18029. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18030. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18031. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18032. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18033. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18034. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18035. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18036. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18037. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18038. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18039. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18040. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18041. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18042. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18043. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18044. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18045. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18046. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18047. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18048. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18049. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18050. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18051. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18052. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18053. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18054. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18055. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18056. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18057. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18058. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18059. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18060. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18061. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18062. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18063. (org-defkey org-mode-map "\C-m" 'org-return)
  18064. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18065. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18066. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18067. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18068. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18069. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18070. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18071. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18072. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18073. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18074. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18075. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18076. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18077. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18078. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18079. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18080. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18081. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18082. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18083. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18084. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18085. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18086. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18087. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18088. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18089. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18090. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18091. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18092. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18093. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18094. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18095. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18096. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18097. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18098. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18099. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18100. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18101. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18102. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18103. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18104. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18105. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18106. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18107. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18108. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18109. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18110. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18111. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18112. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18113. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18114. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  18115. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18116. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18117. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18118. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18119. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18120. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18121. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18122. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18123. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18124. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18125. (when (featurep 'xemacs)
  18126. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18127. (defconst org-speed-commands-default
  18128. '(
  18129. ("Outline Navigation")
  18130. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18131. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18132. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18133. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18134. ("F" . org-next-block)
  18135. ("B" . org-previous-block)
  18136. ("u" . (org-speed-move-safe 'outline-up-heading))
  18137. ("j" . org-goto)
  18138. ("g" . (org-refile t))
  18139. ("Outline Visibility")
  18140. ("c" . org-cycle)
  18141. ("C" . org-shifttab)
  18142. (" " . org-display-outline-path)
  18143. ("s" . org-narrow-to-subtree)
  18144. ("=" . org-columns)
  18145. ("Outline Structure Editing")
  18146. ("U" . org-metaup)
  18147. ("D" . org-metadown)
  18148. ("r" . org-metaright)
  18149. ("l" . org-metaleft)
  18150. ("R" . org-shiftmetaright)
  18151. ("L" . org-shiftmetaleft)
  18152. ("i" . (progn (forward-char 1) (call-interactively
  18153. 'org-insert-heading-respect-content)))
  18154. ("^" . org-sort)
  18155. ("w" . org-refile)
  18156. ("a" . org-archive-subtree-default-with-confirmation)
  18157. ("@" . org-mark-subtree)
  18158. ("#" . org-toggle-comment)
  18159. ("Clock Commands")
  18160. ("I" . org-clock-in)
  18161. ("O" . org-clock-out)
  18162. ("Meta Data Editing")
  18163. ("t" . org-todo)
  18164. ("," . (org-priority))
  18165. ("0" . (org-priority ?\ ))
  18166. ("1" . (org-priority ?A))
  18167. ("2" . (org-priority ?B))
  18168. ("3" . (org-priority ?C))
  18169. (":" . org-set-tags-command)
  18170. ("e" . org-set-effort)
  18171. ("E" . org-inc-effort)
  18172. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18173. (org-entry-put (point) "APPT_WARNTIME" m)))
  18174. ("Agenda Views etc")
  18175. ("v" . org-agenda)
  18176. ("/" . org-sparse-tree)
  18177. ("Misc")
  18178. ("o" . org-open-at-point)
  18179. ("?" . org-speed-command-help)
  18180. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18181. (">" . (org-agenda-remove-restriction-lock))
  18182. )
  18183. "The default speed commands.")
  18184. (defun org-print-speed-command (e)
  18185. (if (> (length (car e)) 1)
  18186. (progn
  18187. (princ "\n")
  18188. (princ (car e))
  18189. (princ "\n")
  18190. (princ (make-string (length (car e)) ?-))
  18191. (princ "\n"))
  18192. (princ (car e))
  18193. (princ " ")
  18194. (if (symbolp (cdr e))
  18195. (princ (symbol-name (cdr e)))
  18196. (prin1 (cdr e)))
  18197. (princ "\n")))
  18198. (defun org-speed-command-help ()
  18199. "Show the available speed commands."
  18200. (interactive)
  18201. (if (not org-use-speed-commands)
  18202. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18203. (with-output-to-temp-buffer "*Help*"
  18204. (princ "User-defined Speed commands\n===========================\n")
  18205. (mapc 'org-print-speed-command org-speed-commands-user)
  18206. (princ "\n")
  18207. (princ "Built-in Speed commands\n=======================\n")
  18208. (mapc 'org-print-speed-command org-speed-commands-default))
  18209. (with-current-buffer "*Help*"
  18210. (setq truncate-lines t))))
  18211. (defun org-speed-move-safe (cmd)
  18212. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18213. If not, return to the original position and throw an error."
  18214. (interactive)
  18215. (let ((pos (point)))
  18216. (call-interactively cmd)
  18217. (unless (and (bolp) (org-at-heading-p))
  18218. (goto-char pos)
  18219. (error "Boundary reached while executing %s" cmd))))
  18220. (defvar org-self-insert-command-undo-counter 0)
  18221. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18222. (defvar org-speed-command nil)
  18223. (define-obsolete-function-alias
  18224. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18225. (defun org-speed-command-activate (keys)
  18226. "Hook for activating single-letter speed commands.
  18227. `org-speed-commands-default' specifies a minimal command set.
  18228. Use `org-speed-commands-user' for further customization."
  18229. (when (or (and (bolp) (looking-at org-outline-regexp))
  18230. (and (functionp org-use-speed-commands)
  18231. (funcall org-use-speed-commands)))
  18232. (cdr (assoc keys (append org-speed-commands-user
  18233. org-speed-commands-default)))))
  18234. (define-obsolete-function-alias
  18235. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18236. (defun org-babel-speed-command-activate (keys)
  18237. "Hook for activating single-letter code block commands."
  18238. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18239. (cdr (assoc keys org-babel-key-bindings))))
  18240. (defcustom org-speed-command-hook
  18241. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18242. "Hook for activating speed commands at strategic locations.
  18243. Hook functions are called in sequence until a valid handler is
  18244. found.
  18245. Each hook takes a single argument, a user-pressed command key
  18246. which is also a `self-insert-command' from the global map.
  18247. Within the hook, examine the cursor position and the command key
  18248. and return nil or a valid handler as appropriate. Handler could
  18249. be one of an interactive command, a function, or a form.
  18250. Set `org-use-speed-commands' to non-nil value to enable this
  18251. hook. The default setting is `org-speed-command-activate'."
  18252. :group 'org-structure
  18253. :version "24.1"
  18254. :type 'hook)
  18255. (defun org-self-insert-command (N)
  18256. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18257. If the cursor is in a table looking at whitespace, the whitespace is
  18258. overwritten, and the table is not marked as requiring realignment."
  18259. (interactive "p")
  18260. (org-check-before-invisible-edit 'insert)
  18261. (cond
  18262. ((and org-use-speed-commands
  18263. (let ((kv (this-command-keys-vector)))
  18264. (setq org-speed-command
  18265. (run-hook-with-args-until-success
  18266. 'org-speed-command-hook
  18267. (make-string 1 (aref kv (1- (length kv))))))))
  18268. (cond
  18269. ((commandp org-speed-command)
  18270. (setq this-command org-speed-command)
  18271. (call-interactively org-speed-command))
  18272. ((functionp org-speed-command)
  18273. (funcall org-speed-command))
  18274. ((and org-speed-command (listp org-speed-command))
  18275. (eval org-speed-command))
  18276. (t (let (org-use-speed-commands)
  18277. (call-interactively 'org-self-insert-command)))))
  18278. ((and
  18279. (org-table-p)
  18280. (progn
  18281. ;; check if we blank the field, and if that triggers align
  18282. (and (featurep 'org-table) org-table-auto-blank-field
  18283. (memq last-command
  18284. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18285. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18286. ;; got extra space, this field does not determine column width
  18287. (let (org-table-may-need-update) (org-table-blank-field))
  18288. ;; no extra space, this field may determine column width
  18289. (org-table-blank-field)))
  18290. t)
  18291. (eq N 1)
  18292. (looking-at "[^|\n]* |"))
  18293. (let (org-table-may-need-update)
  18294. (goto-char (1- (match-end 0)))
  18295. (backward-delete-char 1)
  18296. (goto-char (match-beginning 0))
  18297. (self-insert-command N)))
  18298. (t
  18299. (setq org-table-may-need-update t)
  18300. (self-insert-command N)
  18301. (org-fix-tags-on-the-fly)
  18302. (if org-self-insert-cluster-for-undo
  18303. (if (not (eq last-command 'org-self-insert-command))
  18304. (setq org-self-insert-command-undo-counter 1)
  18305. (if (>= org-self-insert-command-undo-counter 20)
  18306. (setq org-self-insert-command-undo-counter 1)
  18307. (and (> org-self-insert-command-undo-counter 0)
  18308. buffer-undo-list (listp buffer-undo-list)
  18309. (not (cadr buffer-undo-list)) ; remove nil entry
  18310. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18311. (setq org-self-insert-command-undo-counter
  18312. (1+ org-self-insert-command-undo-counter))))))))
  18313. (defun org-check-before-invisible-edit (kind)
  18314. "Check is editing if kind KIND would be dangerous with invisible text around.
  18315. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18316. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18317. (if (and org-catch-invisible-edits
  18318. (or (not (boundp 'visible-mode)) (not visible-mode))
  18319. (or (get-char-property (point) 'invisible)
  18320. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18321. ;; OK, we need to take a closer look
  18322. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18323. (invisible-before-point (if (bobp) nil (get-char-property
  18324. (1- (point)) 'invisible)))
  18325. (border-and-ok-direction
  18326. (or
  18327. ;; Check if we are acting predictably before invisible text
  18328. (and invisible-at-point (not invisible-before-point)
  18329. (memq kind '(insert delete-backward)))
  18330. ;; Check if we are acting predictably after invisible text
  18331. ;; This works not well, and I have turned it off. It seems
  18332. ;; better to always show and stop after invisible text.
  18333. ;; (and (not invisible-at-point) invisible-before-point
  18334. ;; (memq kind '(insert delete)))
  18335. )))
  18336. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18337. (memq invisible-before-point '(outline org-hide-block t)))
  18338. (if (eq org-catch-invisible-edits 'error)
  18339. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18340. (if (and org-custom-properties-overlays
  18341. (y-or-n-p "Display invisible properties in this buffer? "))
  18342. (org-toggle-custom-properties-visibility)
  18343. ;; Make the area visible
  18344. (save-excursion
  18345. (if invisible-before-point
  18346. (goto-char (previous-single-char-property-change
  18347. (point) 'invisible)))
  18348. (show-subtree))
  18349. (cond
  18350. ((eq org-catch-invisible-edits 'show)
  18351. ;; That's it, we do the edit after showing
  18352. (message
  18353. "Unfolding invisible region around point before editing")
  18354. (sit-for 1))
  18355. ((and (eq org-catch-invisible-edits 'smart)
  18356. border-and-ok-direction)
  18357. (message "Unfolding invisible region around point before editing"))
  18358. (t
  18359. ;; Don't do the edit, make the user repeat it in full visibility
  18360. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18361. (defun org-fix-tags-on-the-fly ()
  18362. (when (and (equal (char-after (point-at-bol)) ?*)
  18363. (org-at-heading-p))
  18364. (org-align-tags-here org-tags-column)))
  18365. (defun org-delete-backward-char (N)
  18366. "Like `delete-backward-char', insert whitespace at field end in tables.
  18367. When deleting backwards, in tables this function will insert whitespace in
  18368. front of the next \"|\" separator, to keep the table aligned. The table will
  18369. still be marked for re-alignment if the field did fill the entire column,
  18370. because, in this case the deletion might narrow the column."
  18371. (interactive "p")
  18372. (save-match-data
  18373. (org-check-before-invisible-edit 'delete-backward)
  18374. (if (and (org-table-p)
  18375. (eq N 1)
  18376. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18377. (looking-at ".*?|"))
  18378. (let ((pos (point))
  18379. (noalign (looking-at "[^|\n\r]* |"))
  18380. (c org-table-may-need-update))
  18381. (backward-delete-char N)
  18382. (if (not overwrite-mode)
  18383. (progn
  18384. (skip-chars-forward "^|")
  18385. (insert " ")
  18386. (goto-char (1- pos))))
  18387. ;; noalign: if there were two spaces at the end, this field
  18388. ;; does not determine the width of the column.
  18389. (if noalign (setq org-table-may-need-update c)))
  18390. (backward-delete-char N)
  18391. (org-fix-tags-on-the-fly))))
  18392. (defun org-delete-char (N)
  18393. "Like `delete-char', but insert whitespace at field end in tables.
  18394. When deleting characters, in tables this function will insert whitespace in
  18395. front of the next \"|\" separator, to keep the table aligned. The table will
  18396. still be marked for re-alignment if the field did fill the entire column,
  18397. because, in this case the deletion might narrow the column."
  18398. (interactive "p")
  18399. (save-match-data
  18400. (org-check-before-invisible-edit 'delete)
  18401. (if (and (org-table-p)
  18402. (not (bolp))
  18403. (not (= (char-after) ?|))
  18404. (eq N 1))
  18405. (if (looking-at ".*?|")
  18406. (let ((pos (point))
  18407. (noalign (looking-at "[^|\n\r]* |"))
  18408. (c org-table-may-need-update))
  18409. (replace-match
  18410. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18411. (goto-char pos)
  18412. ;; noalign: if there were two spaces at the end, this field
  18413. ;; does not determine the width of the column.
  18414. (if noalign (setq org-table-may-need-update c)))
  18415. (delete-char N))
  18416. (delete-char N)
  18417. (org-fix-tags-on-the-fly))))
  18418. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18419. (put 'org-self-insert-command 'delete-selection
  18420. (lambda ()
  18421. (not (run-hook-with-args-until-success
  18422. 'self-insert-uses-region-functions))))
  18423. (put 'orgtbl-self-insert-command 'delete-selection
  18424. (lambda ()
  18425. (not (run-hook-with-args-until-success
  18426. 'self-insert-uses-region-functions))))
  18427. (put 'org-delete-char 'delete-selection 'supersede)
  18428. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18429. (put 'org-yank 'delete-selection 'yank)
  18430. ;; Make `flyspell-mode' delay after some commands
  18431. (put 'org-self-insert-command 'flyspell-delayed t)
  18432. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18433. (put 'org-delete-char 'flyspell-delayed t)
  18434. (put 'org-delete-backward-char 'flyspell-delayed t)
  18435. ;; Make pabbrev-mode expand after org-mode commands
  18436. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18437. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18438. (defun org-remap (map &rest commands)
  18439. "In MAP, remap the functions given in COMMANDS.
  18440. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18441. (let (new old)
  18442. (while commands
  18443. (setq old (pop commands) new (pop commands))
  18444. (if (fboundp 'command-remapping)
  18445. (org-defkey map (vector 'remap old) new)
  18446. (substitute-key-definition old new map global-map)))))
  18447. (defun org-transpose-words ()
  18448. "Transpose words for Org.
  18449. This uses the `org-mode-transpose-word-syntax-table' syntax
  18450. table, which interprets characters in `org-emphasis-alist' as
  18451. word constituents."
  18452. (interactive)
  18453. (with-syntax-table org-mode-transpose-word-syntax-table
  18454. (call-interactively 'transpose-words)))
  18455. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18456. (when (eq org-enable-table-editor 'optimized)
  18457. ;; If the user wants maximum table support, we need to hijack
  18458. ;; some standard editing functions
  18459. (org-remap org-mode-map
  18460. 'self-insert-command 'org-self-insert-command
  18461. 'delete-char 'org-delete-char
  18462. 'delete-backward-char 'org-delete-backward-char)
  18463. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18464. (defvar org-ctrl-c-ctrl-c-hook nil
  18465. "Hook for functions attaching themselves to `C-c C-c'.
  18466. This can be used to add additional functionality to the C-c C-c
  18467. key which executes context-dependent commands. This hook is run
  18468. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18469. run after the last test.
  18470. Each function will be called with no arguments. The function
  18471. must check if the context is appropriate for it to act. If yes,
  18472. it should do its thing and then return a non-nil value. If the
  18473. context is wrong, just do nothing and return nil.")
  18474. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18475. "Hook for functions attaching themselves to `C-c C-c'.
  18476. This can be used to add additional functionality to the C-c C-c
  18477. key which executes context-dependent commands. This hook is run
  18478. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18479. before the first test.
  18480. Each function will be called with no arguments. The function
  18481. must check if the context is appropriate for it to act. If yes,
  18482. it should do its thing and then return a non-nil value. If the
  18483. context is wrong, just do nothing and return nil.")
  18484. (defvar org-tab-first-hook nil
  18485. "Hook for functions to attach themselves to TAB.
  18486. See `org-ctrl-c-ctrl-c-hook' for more information.
  18487. This hook runs as the first action when TAB is pressed, even before
  18488. `org-cycle' messes around with the `outline-regexp' to cater for
  18489. inline tasks and plain list item folding.
  18490. If any function in this hook returns t, any other actions that
  18491. would have been caused by TAB (such as table field motion or visibility
  18492. cycling) will not occur.")
  18493. (defvar org-tab-after-check-for-table-hook nil
  18494. "Hook for functions to attach themselves to TAB.
  18495. See `org-ctrl-c-ctrl-c-hook' for more information.
  18496. This hook runs after it has been established that the cursor is not in a
  18497. table, but before checking if the cursor is in a headline or if global cycling
  18498. should be done.
  18499. If any function in this hook returns t, not other actions like visibility
  18500. cycling will be done.")
  18501. (defvar org-tab-after-check-for-cycling-hook nil
  18502. "Hook for functions to attach themselves to TAB.
  18503. See `org-ctrl-c-ctrl-c-hook' for more information.
  18504. This hook runs after it has been established that not table field motion and
  18505. not visibility should be done because of current context. This is probably
  18506. the place where a package like yasnippets can hook in.")
  18507. (defvar org-tab-before-tab-emulation-hook nil
  18508. "Hook for functions to attach themselves to TAB.
  18509. See `org-ctrl-c-ctrl-c-hook' for more information.
  18510. This hook runs after every other options for TAB have been exhausted, but
  18511. before indentation and \t insertion takes place.")
  18512. (defvar org-metaleft-hook nil
  18513. "Hook for functions attaching themselves to `M-left'.
  18514. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18515. (defvar org-metaright-hook nil
  18516. "Hook for functions attaching themselves to `M-right'.
  18517. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18518. (defvar org-metaup-hook nil
  18519. "Hook for functions attaching themselves to `M-up'.
  18520. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18521. (defvar org-metadown-hook nil
  18522. "Hook for functions attaching themselves to `M-down'.
  18523. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18524. (defvar org-shiftmetaleft-hook nil
  18525. "Hook for functions attaching themselves to `M-S-left'.
  18526. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18527. (defvar org-shiftmetaright-hook nil
  18528. "Hook for functions attaching themselves to `M-S-right'.
  18529. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18530. (defvar org-shiftmetaup-hook nil
  18531. "Hook for functions attaching themselves to `M-S-up'.
  18532. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18533. (defvar org-shiftmetadown-hook nil
  18534. "Hook for functions attaching themselves to `M-S-down'.
  18535. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18536. (defvar org-metareturn-hook nil
  18537. "Hook for functions attaching themselves to `M-RET'.
  18538. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18539. (defvar org-shiftup-hook nil
  18540. "Hook for functions attaching themselves to `S-up'.
  18541. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18542. (defvar org-shiftup-final-hook nil
  18543. "Hook for functions attaching themselves to `S-up'.
  18544. This one runs after all other options except shift-select have been excluded.
  18545. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18546. (defvar org-shiftdown-hook nil
  18547. "Hook for functions attaching themselves to `S-down'.
  18548. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18549. (defvar org-shiftdown-final-hook nil
  18550. "Hook for functions attaching themselves to `S-down'.
  18551. This one runs after all other options except shift-select have been excluded.
  18552. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18553. (defvar org-shiftleft-hook nil
  18554. "Hook for functions attaching themselves to `S-left'.
  18555. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18556. (defvar org-shiftleft-final-hook nil
  18557. "Hook for functions attaching themselves to `S-left'.
  18558. This one runs after all other options except shift-select have been excluded.
  18559. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18560. (defvar org-shiftright-hook nil
  18561. "Hook for functions attaching themselves to `S-right'.
  18562. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18563. (defvar org-shiftright-final-hook nil
  18564. "Hook for functions attaching themselves to `S-right'.
  18565. This one runs after all other options except shift-select have been excluded.
  18566. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18567. (defun org-modifier-cursor-error ()
  18568. "Throw an error, a modified cursor command was applied in wrong context."
  18569. (user-error "This command is active in special context like tables, headlines or items"))
  18570. (defun org-shiftselect-error ()
  18571. "Throw an error because Shift-Cursor command was applied in wrong context."
  18572. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18573. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18574. (user-error "This command works only in special context like headlines or timestamps")))
  18575. (defun org-call-for-shift-select (cmd)
  18576. (let ((this-command-keys-shift-translated t))
  18577. (call-interactively cmd)))
  18578. (defun org-shifttab (&optional arg)
  18579. "Global visibility cycling or move to previous table field.
  18580. Call `org-table-previous-field' within a table.
  18581. When ARG is nil, cycle globally through visibility states.
  18582. When ARG is a numeric prefix, show contents of this level."
  18583. (interactive "P")
  18584. (cond
  18585. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18586. ((integerp arg)
  18587. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18588. (message "Content view to level: %d" arg)
  18589. (org-content (prefix-numeric-value arg2))
  18590. (org-cycle-show-empty-lines t)
  18591. (setq org-cycle-global-status 'overview)))
  18592. (t (call-interactively 'org-global-cycle))))
  18593. (defun org-shiftmetaleft ()
  18594. "Promote subtree or delete table column.
  18595. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18596. `org-table-delete-column', depending on context. See the
  18597. individual commands for more information."
  18598. (interactive)
  18599. (cond
  18600. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18601. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18602. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18603. ((if (not (org-region-active-p)) (org-at-item-p)
  18604. (save-excursion (goto-char (region-beginning))
  18605. (org-at-item-p)))
  18606. (call-interactively 'org-outdent-item-tree))
  18607. (t (org-modifier-cursor-error))))
  18608. (defun org-shiftmetaright ()
  18609. "Demote subtree or insert table column.
  18610. Calls `org-demote-subtree', `org-indent-item-tree', or
  18611. `org-table-insert-column', depending on context. See the
  18612. individual commands for more information."
  18613. (interactive)
  18614. (cond
  18615. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18616. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18617. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18618. ((if (not (org-region-active-p)) (org-at-item-p)
  18619. (save-excursion (goto-char (region-beginning))
  18620. (org-at-item-p)))
  18621. (call-interactively 'org-indent-item-tree))
  18622. (t (org-modifier-cursor-error))))
  18623. (defun org-shiftmetaup (&optional arg)
  18624. "Drag the line at point up.
  18625. In a table, kill the current row.
  18626. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18627. but also adjust the previous clocked item in the clock history.
  18628. Everywhere else, drag the line at point up."
  18629. (interactive "P")
  18630. (cond
  18631. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18632. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18633. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18634. (call-interactively 'org-timestamp-up)))
  18635. (t (call-interactively 'org-drag-line-backward))))
  18636. (defun org-shiftmetadown (&optional arg)
  18637. "Drag the line at point down.
  18638. In a table, insert an empty row at the current line.
  18639. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18640. but also adjust the previous clocked item in the clock history.
  18641. Everywhere else, drag the line at point down."
  18642. (interactive "P")
  18643. (cond
  18644. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18645. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18646. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18647. (call-interactively 'org-timestamp-down)))
  18648. (t (call-interactively 'org-drag-line-forward))))
  18649. (defsubst org-hidden-tree-error ()
  18650. (user-error
  18651. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18652. (defun org-metaleft (&optional arg)
  18653. "Promote heading or move table column to left.
  18654. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18655. With no specific context, calls the Emacs default `backward-word'.
  18656. See the individual commands for more information."
  18657. (interactive "P")
  18658. (cond
  18659. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18660. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18661. ((org-with-limited-levels
  18662. (or (org-at-heading-p)
  18663. (and (org-region-active-p)
  18664. (save-excursion
  18665. (goto-char (region-beginning))
  18666. (org-at-heading-p)))))
  18667. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18668. (call-interactively 'org-do-promote))
  18669. ;; At an inline task.
  18670. ((org-at-heading-p)
  18671. (call-interactively 'org-inlinetask-promote))
  18672. ((or (org-at-item-p)
  18673. (and (org-region-active-p)
  18674. (save-excursion
  18675. (goto-char (region-beginning))
  18676. (org-at-item-p))))
  18677. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18678. (call-interactively 'org-outdent-item))
  18679. (t (call-interactively 'backward-word))))
  18680. (defun org-metaright (&optional arg)
  18681. "Demote a subtree, a list item or move table column to right.
  18682. In front of a drawer or a block keyword, indent it correctly.
  18683. With no specific context, calls the Emacs default `forward-word'.
  18684. See the individual commands for more information."
  18685. (interactive "P")
  18686. (cond
  18687. ((run-hook-with-args-until-success 'org-metaright-hook))
  18688. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18689. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18690. ((org-at-block-p) (call-interactively 'org-indent-block))
  18691. ((org-with-limited-levels
  18692. (or (org-at-heading-p)
  18693. (and (org-region-active-p)
  18694. (save-excursion
  18695. (goto-char (region-beginning))
  18696. (org-at-heading-p)))))
  18697. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18698. (call-interactively 'org-do-demote))
  18699. ;; At an inline task.
  18700. ((org-at-heading-p)
  18701. (call-interactively 'org-inlinetask-demote))
  18702. ((or (org-at-item-p)
  18703. (and (org-region-active-p)
  18704. (save-excursion
  18705. (goto-char (region-beginning))
  18706. (org-at-item-p))))
  18707. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18708. (call-interactively 'org-indent-item))
  18709. (t (call-interactively 'forward-word))))
  18710. (defun org-check-for-hidden (what)
  18711. "Check if there are hidden headlines/items in the current visual line.
  18712. WHAT can be either `headlines' or `items'. If the current line is
  18713. an outline or item heading and it has a folded subtree below it,
  18714. this function returns t, nil otherwise."
  18715. (let ((re (cond
  18716. ((eq what 'headlines) org-outline-regexp-bol)
  18717. ((eq what 'items) (org-item-beginning-re))
  18718. (t (error "This should not happen"))))
  18719. beg end)
  18720. (save-excursion
  18721. (catch 'exit
  18722. (unless (org-region-active-p)
  18723. (setq beg (point-at-bol))
  18724. (beginning-of-line 2)
  18725. (while (and (not (eobp)) ;; this is like `next-line'
  18726. (get-char-property (1- (point)) 'invisible))
  18727. (beginning-of-line 2))
  18728. (setq end (point))
  18729. (goto-char beg)
  18730. (goto-char (point-at-eol))
  18731. (setq end (max end (point)))
  18732. (while (re-search-forward re end t)
  18733. (if (get-char-property (match-beginning 0) 'invisible)
  18734. (throw 'exit t))))
  18735. nil))))
  18736. (defun org-metaup (&optional arg)
  18737. "Move subtree up or move table row up.
  18738. Calls `org-move-subtree-up' or `org-table-move-row' or
  18739. `org-move-item-up', depending on context. See the individual commands
  18740. for more information."
  18741. (interactive "P")
  18742. (cond
  18743. ((run-hook-with-args-until-success 'org-metaup-hook))
  18744. ((org-region-active-p)
  18745. (let* ((a (min (region-beginning) (region-end)))
  18746. (b (1- (max (region-beginning) (region-end))))
  18747. (c (save-excursion (goto-char a)
  18748. (move-beginning-of-line 0)))
  18749. (d (save-excursion (goto-char a)
  18750. (move-end-of-line 0) (point))))
  18751. (transpose-regions a b c d)
  18752. (goto-char c)))
  18753. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18754. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18755. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18756. (t (org-drag-element-backward))))
  18757. (defun org-metadown (&optional arg)
  18758. "Move subtree down or move table row down.
  18759. Calls `org-move-subtree-down' or `org-table-move-row' or
  18760. `org-move-item-down', depending on context. See the individual
  18761. commands for more information."
  18762. (interactive "P")
  18763. (cond
  18764. ((run-hook-with-args-until-success 'org-metadown-hook))
  18765. ((org-region-active-p)
  18766. (let* ((a (min (region-beginning) (region-end)))
  18767. (b (max (region-beginning) (region-end)))
  18768. (c (save-excursion (goto-char b)
  18769. (move-beginning-of-line 1)))
  18770. (d (save-excursion (goto-char b)
  18771. (move-end-of-line 1) (1+ (point)))))
  18772. (transpose-regions a b c d)
  18773. (goto-char d)))
  18774. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18775. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18776. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18777. (t (org-drag-element-forward))))
  18778. (defun org-shiftup (&optional arg)
  18779. "Increase item in timestamp or increase priority of current headline.
  18780. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18781. depending on context. See the individual commands for more information."
  18782. (interactive "P")
  18783. (cond
  18784. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18785. ((and org-support-shift-select (org-region-active-p))
  18786. (org-call-for-shift-select 'previous-line))
  18787. ((org-at-timestamp-p t)
  18788. (call-interactively (if org-edit-timestamp-down-means-later
  18789. 'org-timestamp-down 'org-timestamp-up)))
  18790. ((and (not (eq org-support-shift-select 'always))
  18791. org-enable-priority-commands
  18792. (org-at-heading-p))
  18793. (call-interactively 'org-priority-up))
  18794. ((and (not org-support-shift-select) (org-at-item-p))
  18795. (call-interactively 'org-previous-item))
  18796. ((org-clocktable-try-shift 'up arg))
  18797. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18798. (org-support-shift-select
  18799. (org-call-for-shift-select 'previous-line))
  18800. (t (org-shiftselect-error))))
  18801. (defun org-shiftdown (&optional arg)
  18802. "Decrease item in timestamp or decrease priority of current headline.
  18803. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18804. depending on context. See the individual commands for more information."
  18805. (interactive "P")
  18806. (cond
  18807. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18808. ((and org-support-shift-select (org-region-active-p))
  18809. (org-call-for-shift-select 'next-line))
  18810. ((org-at-timestamp-p t)
  18811. (call-interactively (if org-edit-timestamp-down-means-later
  18812. 'org-timestamp-up 'org-timestamp-down)))
  18813. ((and (not (eq org-support-shift-select 'always))
  18814. org-enable-priority-commands
  18815. (org-at-heading-p))
  18816. (call-interactively 'org-priority-down))
  18817. ((and (not org-support-shift-select) (org-at-item-p))
  18818. (call-interactively 'org-next-item))
  18819. ((org-clocktable-try-shift 'down arg))
  18820. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18821. (org-support-shift-select
  18822. (org-call-for-shift-select 'next-line))
  18823. (t (org-shiftselect-error))))
  18824. (defun org-shiftright (&optional arg)
  18825. "Cycle the thing at point or in the current line, depending on context.
  18826. Depending on context, this does one of the following:
  18827. - switch a timestamp at point one day into the future
  18828. - on a headline, switch to the next TODO keyword.
  18829. - on an item, switch entire list to the next bullet type
  18830. - on a property line, switch to the next allowed value
  18831. - on a clocktable definition line, move time block into the future"
  18832. (interactive "P")
  18833. (cond
  18834. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18835. ((and org-support-shift-select (org-region-active-p))
  18836. (org-call-for-shift-select 'forward-char))
  18837. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18838. ((and (not (eq org-support-shift-select 'always))
  18839. (org-at-heading-p))
  18840. (let ((org-inhibit-logging
  18841. (not org-treat-S-cursor-todo-selection-as-state-change))
  18842. (org-inhibit-blocking
  18843. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18844. (org-call-with-arg 'org-todo 'right)))
  18845. ((or (and org-support-shift-select
  18846. (not (eq org-support-shift-select 'always))
  18847. (org-at-item-bullet-p))
  18848. (and (not org-support-shift-select) (org-at-item-p)))
  18849. (org-call-with-arg 'org-cycle-list-bullet nil))
  18850. ((and (not (eq org-support-shift-select 'always))
  18851. (org-at-property-p))
  18852. (call-interactively 'org-property-next-allowed-value))
  18853. ((org-clocktable-try-shift 'right arg))
  18854. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18855. (org-support-shift-select
  18856. (org-call-for-shift-select 'forward-char))
  18857. (t (org-shiftselect-error))))
  18858. (defun org-shiftleft (&optional arg)
  18859. "Cycle the thing at point or in the current line, depending on context.
  18860. Depending on context, this does one of the following:
  18861. - switch a timestamp at point one day into the past
  18862. - on a headline, switch to the previous TODO keyword.
  18863. - on an item, switch entire list to the previous bullet type
  18864. - on a property line, switch to the previous allowed value
  18865. - on a clocktable definition line, move time block into the past"
  18866. (interactive "P")
  18867. (cond
  18868. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18869. ((and org-support-shift-select (org-region-active-p))
  18870. (org-call-for-shift-select 'backward-char))
  18871. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18872. ((and (not (eq org-support-shift-select 'always))
  18873. (org-at-heading-p))
  18874. (let ((org-inhibit-logging
  18875. (not org-treat-S-cursor-todo-selection-as-state-change))
  18876. (org-inhibit-blocking
  18877. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18878. (org-call-with-arg 'org-todo 'left)))
  18879. ((or (and org-support-shift-select
  18880. (not (eq org-support-shift-select 'always))
  18881. (org-at-item-bullet-p))
  18882. (and (not org-support-shift-select) (org-at-item-p)))
  18883. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18884. ((and (not (eq org-support-shift-select 'always))
  18885. (org-at-property-p))
  18886. (call-interactively 'org-property-previous-allowed-value))
  18887. ((org-clocktable-try-shift 'left arg))
  18888. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18889. (org-support-shift-select
  18890. (org-call-for-shift-select 'backward-char))
  18891. (t (org-shiftselect-error))))
  18892. (defun org-shiftcontrolright ()
  18893. "Switch to next TODO set."
  18894. (interactive)
  18895. (cond
  18896. ((and org-support-shift-select (org-region-active-p))
  18897. (org-call-for-shift-select 'forward-word))
  18898. ((and (not (eq org-support-shift-select 'always))
  18899. (org-at-heading-p))
  18900. (org-call-with-arg 'org-todo 'nextset))
  18901. (org-support-shift-select
  18902. (org-call-for-shift-select 'forward-word))
  18903. (t (org-shiftselect-error))))
  18904. (defun org-shiftcontrolleft ()
  18905. "Switch to previous TODO set."
  18906. (interactive)
  18907. (cond
  18908. ((and org-support-shift-select (org-region-active-p))
  18909. (org-call-for-shift-select 'backward-word))
  18910. ((and (not (eq org-support-shift-select 'always))
  18911. (org-at-heading-p))
  18912. (org-call-with-arg 'org-todo 'previousset))
  18913. (org-support-shift-select
  18914. (org-call-for-shift-select 'backward-word))
  18915. (t (org-shiftselect-error))))
  18916. (defun org-shiftcontrolup (&optional n)
  18917. "Change timestamps synchronously up in CLOCK log lines.
  18918. Optional argument N tells to change by that many units."
  18919. (interactive "P")
  18920. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18921. (let (org-support-shift-select)
  18922. (org-clock-timestamps-up n))
  18923. (user-error "Not at a clock log")))
  18924. (defun org-shiftcontroldown (&optional n)
  18925. "Change timestamps synchronously down in CLOCK log lines.
  18926. Optional argument N tells to change by that many units."
  18927. (interactive "P")
  18928. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18929. (let (org-support-shift-select)
  18930. (org-clock-timestamps-down n))
  18931. (user-error "Not at a clock log")))
  18932. (defun org-increase-number-at-point (&optional inc)
  18933. "Increment the number at point.
  18934. With an optional prefix numeric argument INC, increment using
  18935. this numeric value."
  18936. (interactive "p")
  18937. (if (not (number-at-point))
  18938. (user-error "Not on a number")
  18939. (unless inc (setq inc 1))
  18940. (let ((pos (point))
  18941. (beg (skip-chars-backward "-+^/*0-9eE."))
  18942. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18943. (setq nap (buffer-substring-no-properties
  18944. (+ pos beg) (+ pos beg end)))
  18945. (delete-region (+ pos beg) (+ pos beg end))
  18946. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18947. (when (org-at-table-p)
  18948. (org-table-align)
  18949. (org-table-end-of-field 1))))
  18950. (defun org-decrease-number-at-point (&optional inc)
  18951. "Decrement the number at point.
  18952. With an optional prefix numeric argument INC, decrement using
  18953. this numeric value."
  18954. (interactive "p")
  18955. (org-increase-number-at-point (- inc)))
  18956. (defun org-ctrl-c-ret ()
  18957. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18958. (interactive)
  18959. (cond
  18960. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18961. (t (call-interactively 'org-insert-heading))))
  18962. (defun org-find-visible ()
  18963. (let ((s (point)))
  18964. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18965. (get-char-property s 'invisible)))
  18966. s))
  18967. (defun org-find-invisible ()
  18968. (let ((s (point)))
  18969. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18970. (not (get-char-property s 'invisible))))
  18971. s))
  18972. (defun org-copy-visible (beg end)
  18973. "Copy the visible parts of the region."
  18974. (interactive "r")
  18975. (let (snippets s)
  18976. (save-excursion
  18977. (save-restriction
  18978. (narrow-to-region beg end)
  18979. (setq s (goto-char (point-min)))
  18980. (while (not (= (point) (point-max)))
  18981. (goto-char (org-find-invisible))
  18982. (push (buffer-substring s (point)) snippets)
  18983. (setq s (goto-char (org-find-visible))))))
  18984. (kill-new (apply 'concat (nreverse snippets)))))
  18985. (defun org-copy-special ()
  18986. "Copy region in table or copy current subtree.
  18987. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18988. See the individual commands for more information."
  18989. (interactive)
  18990. (call-interactively
  18991. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18992. (defun org-cut-special ()
  18993. "Cut region in table or cut current subtree.
  18994. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18995. See the individual commands for more information."
  18996. (interactive)
  18997. (call-interactively
  18998. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18999. (defun org-paste-special (arg)
  19000. "Paste rectangular region into table, or past subtree relative to level.
  19001. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19002. See the individual commands for more information."
  19003. (interactive "P")
  19004. (if (org-at-table-p)
  19005. (org-table-paste-rectangle)
  19006. (org-paste-subtree arg)))
  19007. (defsubst org-in-fixed-width-region-p ()
  19008. "Is point in a fixed-width region?"
  19009. (save-match-data
  19010. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19011. (defun org-edit-special (&optional arg)
  19012. "Call a special editor for the element at point.
  19013. When at a table, call the formula editor with `org-table-edit-formulas'.
  19014. When in a source code block, call `org-edit-src-code'.
  19015. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19016. When in an export block, call `org-edit-export-block'.
  19017. When at an #+INCLUDE keyword, visit the included file.
  19018. On a link, call `ffap' to visit the link at point.
  19019. Otherwise, return a user error."
  19020. (interactive "P")
  19021. (let ((element (org-element-at-point)))
  19022. (assert (not buffer-read-only) nil
  19023. "Buffer is read-only: %s" (buffer-name))
  19024. (case (org-element-type element)
  19025. (src-block
  19026. (if (not arg) (org-edit-src-code)
  19027. (let* ((info (org-babel-get-src-block-info))
  19028. (lang (nth 0 info))
  19029. (params (nth 2 info))
  19030. (session (cdr (assq :session params))))
  19031. (if (not session) (org-edit-src-code)
  19032. ;; At a src-block with a session and function called with
  19033. ;; an ARG: switch to the buffer related to the inferior
  19034. ;; process.
  19035. (switch-to-buffer
  19036. (funcall (intern (concat "org-babel-prep-session:" lang))
  19037. session params))))))
  19038. (keyword
  19039. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19040. (org-open-link-from-string
  19041. (format "[[%s]]"
  19042. (expand-file-name
  19043. (let ((value (org-element-property :value element)))
  19044. (cond ((not (org-string-nw-p value))
  19045. (user-error "No file to edit"))
  19046. ((string-match "\\`\"\\(.*?\\)\"" value)
  19047. (match-string 1 value))
  19048. ((string-match "\\`[^ \t\"]\\S-*" value)
  19049. (match-string 0 value))
  19050. (t (user-error "No valid file specified")))))))
  19051. (user-error "No special environment to edit here")))
  19052. (table
  19053. (if (eq (org-element-property :type element) 'table.el)
  19054. (org-edit-table.el)
  19055. (call-interactively 'org-table-edit-formulas)))
  19056. ;; Only Org tables contain `table-row' type elements.
  19057. (table-row (call-interactively 'org-table-edit-formulas))
  19058. ((example-block src-block) (org-edit-src-code))
  19059. (export-block (org-edit-export-block))
  19060. (fixed-width (org-edit-fixed-width-region))
  19061. (otherwise
  19062. ;; No notable element at point. Though, we may be at a link,
  19063. ;; which is an object. Thus, scan deeper.
  19064. (if (eq (org-element-type (org-element-context element)) 'link)
  19065. (call-interactively 'ffap)
  19066. (user-error "No special environment to edit here"))))))
  19067. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19068. (defun org-ctrl-c-ctrl-c (&optional arg)
  19069. "Set tags in headline, or update according to changed information at point.
  19070. This command does many different things, depending on context:
  19071. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19072. this is what we do.
  19073. - If the cursor is on a statistics cookie, update it.
  19074. - If the cursor is in a headline, prompt for tags and insert them
  19075. into the current line, aligned to `org-tags-column'. When called
  19076. with prefix arg, realign all tags in the current buffer.
  19077. - If the cursor is in one of the special #+KEYWORD lines, this
  19078. triggers scanning the buffer for these lines and updating the
  19079. information.
  19080. - If the cursor is inside a table, realign the table. This command
  19081. works even if the automatic table editor has been turned off.
  19082. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19083. the entire table.
  19084. - If the cursor is at a footnote reference or definition, jump to
  19085. the corresponding definition or references, respectively.
  19086. - If the cursor is a the beginning of a dynamic block, update it.
  19087. - If the current buffer is a capture buffer, close note and file it.
  19088. - If the cursor is on a <<<target>>>, update radio targets and
  19089. corresponding links in this buffer.
  19090. - If the cursor is on a numbered item in a plain list, renumber the
  19091. ordered list.
  19092. - If the cursor is on a checkbox, toggle it.
  19093. - If the cursor is on a code block, evaluate it. The variable
  19094. `org-confirm-babel-evaluate' can be used to control prompting
  19095. before code block evaluation, by default every code block
  19096. evaluation requires confirmation. Code block evaluation can be
  19097. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19098. (interactive "P")
  19099. (cond
  19100. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19101. org-occur-highlights)
  19102. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19103. (org-remove-occur-highlights)
  19104. (message "Temporary highlights/overlays removed from current buffer"))
  19105. ((and (local-variable-p 'org-finish-function (current-buffer))
  19106. (fboundp org-finish-function))
  19107. (funcall org-finish-function))
  19108. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19109. (t
  19110. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19111. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19112. (user-error "C-c C-c can do nothing useful at this location"))
  19113. (let* ((context (org-element-context))
  19114. (type (org-element-type context)))
  19115. (case type
  19116. ;; When at a link, act according to the parent instead.
  19117. (link (setq context (org-element-property :parent context))
  19118. (setq type (org-element-type context)))
  19119. ;; Unsupported object types: refer to the first supported
  19120. ;; element or object containing it.
  19121. ((bold code entity export-snippet inline-babel-call inline-src-block
  19122. italic latex-fragment line-break macro strike-through subscript
  19123. superscript underline verbatim)
  19124. (setq context
  19125. (org-element-lineage
  19126. context '(radio-target paragraph verse-block table-cell)))))
  19127. ;; For convenience: at the first line of a paragraph on the
  19128. ;; same line as an item, apply function on that item instead.
  19129. (when (eq type 'paragraph)
  19130. (let ((parent (org-element-property :parent context)))
  19131. (when (and (eq (org-element-type parent) 'item)
  19132. (= (line-beginning-position)
  19133. (org-element-property :begin parent)))
  19134. (setq context parent type 'item))))
  19135. ;; Act according to type of element or object at point.
  19136. (case type
  19137. (clock (org-clock-update-time-maybe))
  19138. (dynamic-block
  19139. (save-excursion
  19140. (goto-char (org-element-property :post-affiliated context))
  19141. (org-update-dblock)))
  19142. (footnote-definition
  19143. (goto-char (org-element-property :post-affiliated context))
  19144. (call-interactively 'org-footnote-action))
  19145. (footnote-reference (call-interactively 'org-footnote-action))
  19146. ((headline inlinetask)
  19147. (save-excursion (goto-char (org-element-property :begin context))
  19148. (call-interactively 'org-set-tags)))
  19149. (item
  19150. ;; At an item: a double C-u set checkbox to "[-]"
  19151. ;; unconditionally, whereas a single one will toggle its
  19152. ;; presence. Without an universal argument, if the item
  19153. ;; has a checkbox, toggle it. Otherwise repair the list.
  19154. (let* ((box (org-element-property :checkbox context))
  19155. (struct (org-element-property :structure context))
  19156. (old-struct (copy-tree struct))
  19157. (parents (org-list-parents-alist struct))
  19158. (prevs (org-list-prevs-alist struct))
  19159. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19160. (org-list-set-checkbox
  19161. (org-element-property :begin context) struct
  19162. (cond ((equal arg '(16)) "[-]")
  19163. ((and (not box) (equal arg '(4))) "[ ]")
  19164. ((or (not box) (equal arg '(4))) nil)
  19165. ((eq box 'on) "[ ]")
  19166. (t "[X]")))
  19167. ;; Mimic `org-list-write-struct' but with grabbing
  19168. ;; a return value from `org-list-struct-fix-box'.
  19169. (org-list-struct-fix-ind struct parents 2)
  19170. (org-list-struct-fix-item-end struct)
  19171. (org-list-struct-fix-bul struct prevs)
  19172. (org-list-struct-fix-ind struct parents)
  19173. (let ((block-item
  19174. (org-list-struct-fix-box struct parents prevs orderedp)))
  19175. (if (and box (equal struct old-struct))
  19176. (if (equal arg '(16))
  19177. (message "Checkboxes already reset")
  19178. (user-error "Cannot toggle this checkbox: %s"
  19179. (if (eq box 'on)
  19180. "all subitems checked"
  19181. "unchecked subitems")))
  19182. (org-list-struct-apply-struct struct old-struct)
  19183. (org-update-checkbox-count-maybe))
  19184. (when block-item
  19185. (message "Checkboxes were removed due to empty box at line %d"
  19186. (org-current-line block-item))))))
  19187. (keyword
  19188. (let ((org-inhibit-startup-visibility-stuff t)
  19189. (org-startup-align-all-tables nil))
  19190. (when (boundp 'org-table-coordinate-overlays)
  19191. (mapc 'delete-overlay org-table-coordinate-overlays)
  19192. (setq org-table-coordinate-overlays nil))
  19193. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19194. (message "Local setup has been refreshed"))
  19195. (plain-list
  19196. ;; At a plain list, with a double C-u argument, set
  19197. ;; checkboxes of each item to "[-]", whereas a single one
  19198. ;; will toggle their presence according to the state of the
  19199. ;; first item in the list. Without an argument, repair the
  19200. ;; list.
  19201. (let* ((begin (org-element-property :contents-begin context))
  19202. (beginm (move-marker (make-marker) begin))
  19203. (struct (org-element-property :structure context))
  19204. (old-struct (copy-tree struct))
  19205. (first-box (save-excursion
  19206. (goto-char begin)
  19207. (looking-at org-list-full-item-re)
  19208. (match-string-no-properties 3)))
  19209. (new-box (cond ((equal arg '(16)) "[-]")
  19210. ((equal arg '(4)) (unless first-box "[ ]"))
  19211. ((equal first-box "[X]") "[ ]")
  19212. (t "[X]"))))
  19213. (cond
  19214. (arg
  19215. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19216. (org-list-get-all-items
  19217. begin struct (org-list-prevs-alist struct))))
  19218. ((and first-box (eq (point) begin))
  19219. ;; For convenience, when point is at bol on the first
  19220. ;; item of the list and no argument is provided, simply
  19221. ;; toggle checkbox of that item, if any.
  19222. (org-list-set-checkbox begin struct new-box)))
  19223. (org-list-write-struct
  19224. struct (org-list-parents-alist struct) old-struct)
  19225. (org-update-checkbox-count-maybe)
  19226. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19227. ((property-drawer node-property)
  19228. (call-interactively 'org-property-action))
  19229. ((radio-target target)
  19230. (call-interactively 'org-update-radio-target-regexp))
  19231. (statistics-cookie
  19232. (call-interactively 'org-update-statistics-cookies))
  19233. ((table table-cell table-row)
  19234. ;; At a table, recalculate every field and align it. Also
  19235. ;; send the table if necessary. If the table has
  19236. ;; a `table.el' type, just give up. At a table row or
  19237. ;; cell, maybe recalculate line but always align table.
  19238. (if (eq (org-element-property :type context) 'table.el)
  19239. (message "Use C-c ' to edit table.el tables")
  19240. (let ((org-enable-table-editor t))
  19241. (if (or (eq type 'table)
  19242. ;; Check if point is at a TBLFM line.
  19243. (and (eq type 'table-row)
  19244. (= (point) (org-element-property :end context))))
  19245. (save-excursion
  19246. (if (org-at-TBLFM-p)
  19247. (progn (require 'org-table)
  19248. (org-table-calc-current-TBLFM))
  19249. (goto-char (org-element-property :contents-begin context))
  19250. (org-call-with-arg 'org-table-recalculate (or arg t))
  19251. (orgtbl-send-table 'maybe)))
  19252. (org-table-maybe-eval-formula)
  19253. (cond (arg (call-interactively 'org-table-recalculate))
  19254. ((org-table-maybe-recalculate-line))
  19255. (t (org-table-align)))))))
  19256. (timestamp (org-timestamp-change 0 'day))
  19257. (otherwise
  19258. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19259. (user-error
  19260. "C-c C-c can do nothing useful at this location")))))))))
  19261. (defun org-mode-restart ()
  19262. (interactive)
  19263. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19264. (funcall major-mode)
  19265. (hack-local-variables)
  19266. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19267. (org-indent-mode -1)))
  19268. (message "%s restarted" major-mode))
  19269. (defun org-kill-note-or-show-branches ()
  19270. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19271. (interactive)
  19272. (if (not org-finish-function)
  19273. (progn
  19274. (hide-subtree)
  19275. (call-interactively 'show-branches))
  19276. (let ((org-note-abort t))
  19277. (funcall org-finish-function))))
  19278. (defun org-open-line (n)
  19279. "Insert a new row in tables, call `open-line' elsewhere.
  19280. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19281. (interactive "*p")
  19282. (cond
  19283. ((not org-special-ctrl-o)
  19284. (open-line n))
  19285. ((org-at-table-p)
  19286. (org-table-insert-row))
  19287. (t
  19288. (open-line n))))
  19289. (defun org-return (&optional indent)
  19290. "Goto next table row or insert a newline.
  19291. Calls `org-table-next-row' or `newline', depending on context.
  19292. When optional INDENT argument is non-nil, call
  19293. `newline-and-indent' instead of `newline'.
  19294. When `org-return-follows-link' is non-nil and point is on
  19295. a timestamp or a link, call `org-open-at-point'. However, it
  19296. will not happen if point is in a table or on a \"dead\"
  19297. object (e.g., within a comment). In these case, you need to use
  19298. `org-open-at-point' directly."
  19299. (interactive)
  19300. (if (and (save-excursion
  19301. (beginning-of-line)
  19302. (looking-at org-todo-line-regexp))
  19303. (match-beginning 3)
  19304. (>= (point) (match-beginning 3)))
  19305. ;; Point is on headline tags. Do not break them: add a newline
  19306. ;; after the headline instead.
  19307. (progn (org-show-entry)
  19308. (end-of-line)
  19309. (if indent (newline-and-indent) (newline)))
  19310. (let* ((context (if org-return-follows-link (org-element-context)
  19311. (org-element-at-point)))
  19312. (type (org-element-type context)))
  19313. (cond
  19314. ;; In a table, call `org-table-next-row'.
  19315. ((or (and (eq type 'table)
  19316. (>= (point) (org-element-property :contents-begin context))
  19317. (< (point) (org-element-property :contents-end context)))
  19318. (org-element-lineage context '(table-row table-cell) t))
  19319. (org-table-justify-field-maybe)
  19320. (call-interactively #'org-table-next-row))
  19321. ;; On a link or a timestamp but not on white spaces after it,
  19322. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19323. ((and org-return-follows-link
  19324. (memq type '(link timestamp))
  19325. (< (point)
  19326. (save-excursion (goto-char (org-element-property :end context))
  19327. (skip-chars-backward " \t")
  19328. (point))))
  19329. (call-interactively #'org-open-at-point))
  19330. ;; In a list, make sure indenting keeps trailing text within.
  19331. ((and indent
  19332. (not (eolp))
  19333. (org-element-lineage context '(item)))
  19334. (let ((trailing-data
  19335. (delete-and-extract-region (point) (line-end-position))))
  19336. (newline-and-indent)
  19337. (save-excursion (insert trailing-data))))
  19338. (t (if indent (newline-and-indent) (newline)))))))
  19339. (defun org-return-indent ()
  19340. "Goto next table row or insert a newline and indent.
  19341. Calls `org-table-next-row' or `newline-and-indent', depending on
  19342. context. See the individual commands for more information."
  19343. (interactive)
  19344. (org-return t))
  19345. (defun org-ctrl-c-star ()
  19346. "Compute table, or change heading status of lines.
  19347. Calls `org-table-recalculate' or `org-toggle-heading',
  19348. depending on context."
  19349. (interactive)
  19350. (cond
  19351. ((org-at-table-p)
  19352. (call-interactively 'org-table-recalculate))
  19353. (t
  19354. ;; Convert all lines in region to list items
  19355. (call-interactively 'org-toggle-heading))))
  19356. (defun org-ctrl-c-minus ()
  19357. "Insert separator line in table or modify bullet status of line.
  19358. Also turns a plain line or a region of lines into list items.
  19359. Calls `org-table-insert-hline', `org-toggle-item', or
  19360. `org-cycle-list-bullet', depending on context."
  19361. (interactive)
  19362. (cond
  19363. ((org-at-table-p)
  19364. (call-interactively 'org-table-insert-hline))
  19365. ((org-region-active-p)
  19366. (call-interactively 'org-toggle-item))
  19367. ((org-in-item-p)
  19368. (call-interactively 'org-cycle-list-bullet))
  19369. (t
  19370. (call-interactively 'org-toggle-item))))
  19371. (defun org-toggle-item (arg)
  19372. "Convert headings or normal lines to items, items to normal lines.
  19373. If there is no active region, only the current line is considered.
  19374. If the first non blank line in the region is a headline, convert
  19375. all headlines to items, shifting text accordingly.
  19376. If it is an item, convert all items to normal lines.
  19377. If it is normal text, change region into a list of items.
  19378. With a prefix argument ARG, change the region in a single item."
  19379. (interactive "P")
  19380. (let ((shift-text
  19381. (function
  19382. ;; Shift text in current section to IND, from point to END.
  19383. ;; The function leaves point to END line.
  19384. (lambda (ind end)
  19385. (let ((min-i 1000) (end (copy-marker end)))
  19386. ;; First determine the minimum indentation (MIN-I) of
  19387. ;; the text.
  19388. (save-excursion
  19389. (catch 'exit
  19390. (while (< (point) end)
  19391. (let ((i (org-get-indentation)))
  19392. (cond
  19393. ;; Skip blank lines and inline tasks.
  19394. ((looking-at "^[ \t]*$"))
  19395. ((looking-at org-outline-regexp-bol))
  19396. ;; We can't find less than 0 indentation.
  19397. ((zerop i) (throw 'exit (setq min-i 0)))
  19398. ((< i min-i) (setq min-i i))))
  19399. (forward-line))))
  19400. ;; Then indent each line so that a line indented to
  19401. ;; MIN-I becomes indented to IND. Ignore blank lines
  19402. ;; and inline tasks in the process.
  19403. (let ((delta (- ind min-i)))
  19404. (while (< (point) end)
  19405. (unless (or (looking-at "^[ \t]*$")
  19406. (looking-at org-outline-regexp-bol))
  19407. (org-indent-line-to (+ (org-get-indentation) delta)))
  19408. (forward-line)))))))
  19409. (skip-blanks
  19410. (function
  19411. ;; Return beginning of first non-blank line, starting from
  19412. ;; line at POS.
  19413. (lambda (pos)
  19414. (save-excursion
  19415. (goto-char pos)
  19416. (skip-chars-forward " \r\t\n")
  19417. (point-at-bol)))))
  19418. beg end)
  19419. ;; Determine boundaries of changes.
  19420. (if (org-region-active-p)
  19421. (setq beg (funcall skip-blanks (region-beginning))
  19422. end (copy-marker (region-end)))
  19423. (setq beg (funcall skip-blanks (point-at-bol))
  19424. end (copy-marker (point-at-eol))))
  19425. ;; Depending on the starting line, choose an action on the text
  19426. ;; between BEG and END.
  19427. (org-with-limited-levels
  19428. (save-excursion
  19429. (goto-char beg)
  19430. (cond
  19431. ;; Case 1. Start at an item: de-itemize. Note that it only
  19432. ;; happens when a region is active: `org-ctrl-c-minus'
  19433. ;; would call `org-cycle-list-bullet' otherwise.
  19434. ((org-at-item-p)
  19435. (while (< (point) end)
  19436. (when (org-at-item-p)
  19437. (skip-chars-forward " \t")
  19438. (delete-region (point) (match-end 0)))
  19439. (forward-line)))
  19440. ;; Case 2. Start at an heading: convert to items.
  19441. ((org-at-heading-p)
  19442. (let* ((bul (org-list-bullet-string "-"))
  19443. (bul-len (length bul))
  19444. (done (org-entry-is-done-p))
  19445. (todo (org-entry-is-todo-p))
  19446. ;; Indentation of the first heading. It should be
  19447. ;; relative to the indentation of its parent, if any.
  19448. (start-ind (save-excursion
  19449. (cond
  19450. ((not org-adapt-indentation) 0)
  19451. ((not (outline-previous-heading)) 0)
  19452. (t (length (match-string 0))))))
  19453. ;; Level of first heading. Further headings will be
  19454. ;; compared to it to determine hierarchy in the list.
  19455. (ref-level (org-reduced-level (org-outline-level))))
  19456. (when (or done todo) (org-todo ""))
  19457. (while (< (point) end)
  19458. (let* ((level (org-reduced-level (org-outline-level)))
  19459. (delta (max 0 (- level ref-level))))
  19460. ;; If current headline is less indented than the first
  19461. ;; one, set it as reference, in order to preserve
  19462. ;; subtrees.
  19463. (when (< level ref-level) (setq ref-level level))
  19464. (replace-match bul t t)
  19465. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19466. (when (or done todo)
  19467. (let* ((struct (org-list-struct))
  19468. (old (copy-tree struct)))
  19469. (org-list-set-checkbox (line-beginning-position)
  19470. struct
  19471. (if done "[X]" "[ ]"))
  19472. (org-list-write-struct struct
  19473. (org-list-parents-alist struct)
  19474. old)))
  19475. ;; Ensure all text down to END (or SECTION-END) belongs
  19476. ;; to the newly created item.
  19477. (let ((section-end (save-excursion
  19478. (or (outline-next-heading) (point)))))
  19479. (forward-line)
  19480. (funcall shift-text
  19481. (+ start-ind (* (1+ delta) bul-len))
  19482. (min end section-end)))))))
  19483. ;; Case 3. Normal line with ARG: make the first line of region
  19484. ;; an item, and shift indentation of others lines to
  19485. ;; set them as item's body.
  19486. (arg (let* ((bul (org-list-bullet-string "-"))
  19487. (bul-len (length bul))
  19488. (ref-ind (org-get-indentation)))
  19489. (skip-chars-forward " \t")
  19490. (insert bul)
  19491. (forward-line)
  19492. (while (< (point) end)
  19493. ;; Ensure that lines less indented than first one
  19494. ;; still get included in item body.
  19495. (funcall shift-text
  19496. (+ ref-ind bul-len)
  19497. (min end (save-excursion (or (outline-next-heading)
  19498. (point)))))
  19499. (forward-line))))
  19500. ;; Case 4. Normal line without ARG: turn each non-item line
  19501. ;; into an item.
  19502. (t
  19503. (while (< (point) end)
  19504. (unless (or (org-at-heading-p) (org-at-item-p))
  19505. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19506. (replace-match
  19507. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19508. (forward-line))))))))
  19509. (defun org-toggle-heading (&optional nstars)
  19510. "Convert headings to normal text, or items or text to headings.
  19511. If there is no active region, only convert the current line.
  19512. With a \\[universal-argument] prefix, convert the whole list at
  19513. point into heading.
  19514. In a region:
  19515. - If the first non blank line is a headline, remove the stars
  19516. from all headlines in the region.
  19517. - If it is a normal line, turn each and every normal line (i.e.,
  19518. not an heading or an item) in the region into headings. If you
  19519. want to convert only the first line of this region, use one
  19520. universal prefix argument.
  19521. - If it is a plain list item, turn all plain list items into headings.
  19522. When converting a line into a heading, the number of stars is chosen
  19523. such that the lines become children of the current entry. However,
  19524. when a numeric prefix argument is given, its value determines the
  19525. number of stars to add."
  19526. (interactive "P")
  19527. (let ((skip-blanks
  19528. (function
  19529. ;; Return beginning of first non-blank line, starting from
  19530. ;; line at POS.
  19531. (lambda (pos)
  19532. (save-excursion
  19533. (goto-char pos)
  19534. (while (org-at-comment-p) (forward-line))
  19535. (skip-chars-forward " \r\t\n")
  19536. (point-at-bol)))))
  19537. beg end toggled)
  19538. ;; Determine boundaries of changes. If a universal prefix has
  19539. ;; been given, put the list in a region. If region ends at a bol,
  19540. ;; do not consider the last line to be in the region.
  19541. (when (and current-prefix-arg (org-at-item-p))
  19542. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19543. (org-mark-element))
  19544. (if (org-region-active-p)
  19545. (setq beg (funcall skip-blanks (region-beginning))
  19546. end (copy-marker (save-excursion
  19547. (goto-char (region-end))
  19548. (if (bolp) (point) (point-at-eol)))))
  19549. (setq beg (funcall skip-blanks (point-at-bol))
  19550. end (copy-marker (point-at-eol))))
  19551. ;; Ensure inline tasks don't count as headings.
  19552. (org-with-limited-levels
  19553. (save-excursion
  19554. (goto-char beg)
  19555. (cond
  19556. ;; Case 1. Started at an heading: de-star headings.
  19557. ((org-at-heading-p)
  19558. (while (< (point) end)
  19559. (when (org-at-heading-p t)
  19560. (looking-at org-outline-regexp) (replace-match "")
  19561. (setq toggled t))
  19562. (forward-line)))
  19563. ;; Case 2. Started at an item: change items into headlines.
  19564. ;; One star will be added by `org-list-to-subtree'.
  19565. ((org-at-item-p)
  19566. (while (< (point) end)
  19567. (when (org-at-item-p)
  19568. ;; Pay attention to cases when region ends before list.
  19569. (let* ((struct (org-list-struct))
  19570. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19571. (save-restriction
  19572. (narrow-to-region (point) list-end)
  19573. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19574. (setq toggled t))
  19575. (forward-line)))
  19576. ;; Case 3. Started at normal text: make every line an heading,
  19577. ;; skipping headlines and items.
  19578. (t (let* ((stars
  19579. (make-string
  19580. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19581. (add-stars
  19582. (cond (nstars "") ; stars from prefix only
  19583. ((equal stars "") "*") ; before first heading
  19584. (org-odd-levels-only "**") ; inside heading, odd
  19585. (t "*"))) ; inside heading, oddeven
  19586. (rpl (concat stars add-stars " "))
  19587. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19588. (while (< (point) (if (equal nstars '(4)) lend end))
  19589. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19590. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19591. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19592. (forward-line)))))))
  19593. (unless toggled (message "Cannot toggle heading from here"))))
  19594. (defun org-meta-return (&optional arg)
  19595. "Insert a new heading or wrap a region in a table.
  19596. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19597. on context. See the individual commands for more information."
  19598. (interactive "P")
  19599. (org-check-before-invisible-edit 'insert)
  19600. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19601. (let* ((element (org-element-at-point))
  19602. (type (org-element-type element)))
  19603. (when (eq type 'table-row)
  19604. (setq element (org-element-property :parent element))
  19605. (setq type 'table))
  19606. (if (and (eq type 'table)
  19607. (eq (org-element-property :type element) 'org)
  19608. (>= (point) (org-element-property :contents-begin element))
  19609. (< (point) (org-element-property :contents-end element)))
  19610. (call-interactively 'org-table-wrap-region)
  19611. (call-interactively 'org-insert-heading)))))
  19612. ;;; Menu entries
  19613. (defsubst org-in-subtree-not-table-p ()
  19614. "Are we in a subtree and not in a table?"
  19615. (and (not (org-before-first-heading-p))
  19616. (not (org-at-table-p))))
  19617. ;; Define the Org-mode menus
  19618. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19619. '("Tbl"
  19620. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19621. ["Next Field" org-cycle (org-at-table-p)]
  19622. ["Previous Field" org-shifttab (org-at-table-p)]
  19623. ["Next Row" org-return (org-at-table-p)]
  19624. "--"
  19625. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19626. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19627. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19628. "--"
  19629. ("Column"
  19630. ["Move Column Left" org-metaleft (org-at-table-p)]
  19631. ["Move Column Right" org-metaright (org-at-table-p)]
  19632. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19633. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19634. ("Row"
  19635. ["Move Row Up" org-metaup (org-at-table-p)]
  19636. ["Move Row Down" org-metadown (org-at-table-p)]
  19637. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19638. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19639. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19640. "--"
  19641. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19642. ("Rectangle"
  19643. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19644. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19645. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19646. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19647. "--"
  19648. ("Calculate"
  19649. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19650. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19651. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19652. "--"
  19653. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19654. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19655. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19656. "--"
  19657. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19658. "--"
  19659. ["Sum Column/Rectangle" org-table-sum
  19660. (or (org-at-table-p) (org-region-active-p))]
  19661. ["Which Column?" org-table-current-column (org-at-table-p)])
  19662. ["Debug Formulas"
  19663. org-table-toggle-formula-debugger
  19664. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19665. ["Show Col/Row Numbers"
  19666. org-table-toggle-coordinate-overlays
  19667. :style toggle
  19668. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19669. "--"
  19670. ["Create" org-table-create (and (not (org-at-table-p))
  19671. org-enable-table-editor)]
  19672. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19673. ["Import from File" org-table-import (not (org-at-table-p))]
  19674. ["Export to File" org-table-export (org-at-table-p)]
  19675. "--"
  19676. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19677. "--"
  19678. ("Plot"
  19679. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19680. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19681. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19682. '("Org"
  19683. ("Show/Hide"
  19684. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19685. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19686. ["Sparse Tree..." org-sparse-tree t]
  19687. ["Reveal Context" org-reveal t]
  19688. ["Show All" show-all t]
  19689. "--"
  19690. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19691. "--"
  19692. ["New Heading" org-insert-heading t]
  19693. ("Navigate Headings"
  19694. ["Up" outline-up-heading t]
  19695. ["Next" outline-next-visible-heading t]
  19696. ["Previous" outline-previous-visible-heading t]
  19697. ["Next Same Level" outline-forward-same-level t]
  19698. ["Previous Same Level" outline-backward-same-level t]
  19699. "--"
  19700. ["Jump" org-goto t])
  19701. ("Edit Structure"
  19702. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19703. "--"
  19704. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19705. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19706. "--"
  19707. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19708. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19709. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19710. "--"
  19711. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19712. "--"
  19713. ["Copy visible text" org-copy-visible t]
  19714. "--"
  19715. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19716. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19717. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19718. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19719. "--"
  19720. ["Sort Region/Children" org-sort t]
  19721. "--"
  19722. ["Convert to odd levels" org-convert-to-odd-levels t]
  19723. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19724. ("Editing"
  19725. ["Emphasis..." org-emphasize t]
  19726. ["Edit Source Example" org-edit-special t]
  19727. "--"
  19728. ["Footnote new/jump" org-footnote-action t]
  19729. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19730. ("Archive"
  19731. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19732. "--"
  19733. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19734. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19735. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19736. )
  19737. "--"
  19738. ("Hyperlinks"
  19739. ["Store Link (Global)" org-store-link t]
  19740. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19741. ["Insert Link" org-insert-link t]
  19742. ["Follow Link" org-open-at-point t]
  19743. "--"
  19744. ["Next link" org-next-link t]
  19745. ["Previous link" org-previous-link t]
  19746. "--"
  19747. ["Descriptive Links"
  19748. org-toggle-link-display
  19749. :style radio
  19750. :selected org-descriptive-links
  19751. ]
  19752. ["Literal Links"
  19753. org-toggle-link-display
  19754. :style radio
  19755. :selected (not org-descriptive-links)])
  19756. "--"
  19757. ("TODO Lists"
  19758. ["TODO/DONE/-" org-todo t]
  19759. ("Select keyword"
  19760. ["Next keyword" org-shiftright (org-at-heading-p)]
  19761. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19762. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19763. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19764. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19765. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19766. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19767. "--"
  19768. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19769. :selected org-enforce-todo-dependencies :style toggle :active t]
  19770. "Settings for tree at point"
  19771. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19772. :selected (org-entry-get nil "ORDERED")
  19773. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19774. ["Do Children parallel" org-toggle-ordered-property :style radio
  19775. :selected (not (org-entry-get nil "ORDERED"))
  19776. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19777. "--"
  19778. ["Set Priority" org-priority t]
  19779. ["Priority Up" org-shiftup t]
  19780. ["Priority Down" org-shiftdown t]
  19781. "--"
  19782. ["Get news from all feeds" org-feed-update-all t]
  19783. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19784. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19785. ("TAGS and Properties"
  19786. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19787. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19788. "--"
  19789. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19790. ["Column view of properties" org-columns t]
  19791. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19792. ("Dates and Scheduling"
  19793. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19794. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19795. ("Change Date"
  19796. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19797. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19798. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19799. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19800. ["Compute Time Range" org-evaluate-time-range t]
  19801. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19802. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19803. "--"
  19804. ["Custom time format" org-toggle-time-stamp-overlays
  19805. :style radio :selected org-display-custom-times]
  19806. "--"
  19807. ["Goto Calendar" org-goto-calendar t]
  19808. ["Date from Calendar" org-date-from-calendar t]
  19809. "--"
  19810. ["Start/Restart Timer" org-timer-start t]
  19811. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19812. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19813. ["Insert Timer String" org-timer t]
  19814. ["Insert Timer Item" org-timer-item t])
  19815. ("Logging work"
  19816. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19817. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19818. ["Clock out" org-clock-out t]
  19819. ["Clock cancel" org-clock-cancel t]
  19820. "--"
  19821. ["Mark as default task" org-clock-mark-default-task t]
  19822. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19823. ["Goto running clock" org-clock-goto t]
  19824. "--"
  19825. ["Display times" org-clock-display t]
  19826. ["Create clock table" org-clock-report t]
  19827. "--"
  19828. ["Record DONE time"
  19829. (progn (setq org-log-done (not org-log-done))
  19830. (message "Switching to %s will %s record a timestamp"
  19831. (car org-done-keywords)
  19832. (if org-log-done "automatically" "not")))
  19833. :style toggle :selected org-log-done])
  19834. "--"
  19835. ["Agenda Command..." org-agenda t]
  19836. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19837. ("File List for Agenda")
  19838. ("Special views current file"
  19839. ["TODO Tree" org-show-todo-tree t]
  19840. ["Check Deadlines" org-check-deadlines t]
  19841. ["Timeline" org-timeline t]
  19842. ["Tags/Property tree" org-match-sparse-tree t])
  19843. "--"
  19844. ["Export/Publish..." org-export-dispatch t]
  19845. ("LaTeX"
  19846. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19847. :selected org-cdlatex-mode]
  19848. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19849. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19850. ["Modify math symbol" org-cdlatex-math-modify
  19851. (org-inside-LaTeX-fragment-p)]
  19852. ["Insert citation" org-reftex-citation t]
  19853. "--"
  19854. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19855. "--"
  19856. ("MobileOrg"
  19857. ["Push Files and Views" org-mobile-push t]
  19858. ["Get Captured and Flagged" org-mobile-pull t]
  19859. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19860. "--"
  19861. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19862. "--"
  19863. ("Documentation"
  19864. ["Show Version" org-version t]
  19865. ["Info Documentation" org-info t])
  19866. ("Customize"
  19867. ["Browse Org Group" org-customize t]
  19868. "--"
  19869. ["Expand This Menu" org-create-customize-menu
  19870. (fboundp 'customize-menu-create)])
  19871. ["Send bug report" org-submit-bug-report t]
  19872. "--"
  19873. ("Refresh/Reload"
  19874. ["Refresh setup current buffer" org-mode-restart t]
  19875. ["Reload Org (after update)" org-reload t]
  19876. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19877. ))
  19878. (defun org-info (&optional node)
  19879. "Read documentation for Org-mode in the info system.
  19880. With optional NODE, go directly to that node."
  19881. (interactive)
  19882. (info (format "(org)%s" (or node ""))))
  19883. ;;;###autoload
  19884. (defun org-submit-bug-report ()
  19885. "Submit a bug report on Org-mode via mail.
  19886. Don't hesitate to report any problems or inaccurate documentation.
  19887. If you don't have setup sending mail from (X)Emacs, please copy the
  19888. output buffer into your mail program, as it gives us important
  19889. information about your Org-mode version and configuration."
  19890. (interactive)
  19891. (require 'reporter)
  19892. (org-load-modules-maybe)
  19893. (org-require-autoloaded-modules)
  19894. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19895. (reporter-submit-bug-report
  19896. "emacs-orgmode@gnu.org"
  19897. (org-version nil 'full)
  19898. (let (list)
  19899. (save-window-excursion
  19900. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19901. (delete-other-windows)
  19902. (erase-buffer)
  19903. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19904. We would like to add your full Org-mode and Outline configuration to the
  19905. bug report. This greatly simplifies the work of the maintainer and
  19906. other experts on the mailing list.
  19907. HOWEVER, some variables you have customized may contain private
  19908. information. The names of customers, colleagues, or friends, might
  19909. appear in the form of file names, tags, todo states, or search strings.
  19910. If you answer yes to the prompt, you might want to check and remove
  19911. such private information before sending the email.")
  19912. (add-text-properties (point-min) (point-max) '(face org-warning))
  19913. (when (yes-or-no-p "Include your Org-mode configuration ")
  19914. (mapatoms
  19915. (lambda (v)
  19916. (and (boundp v)
  19917. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19918. (or (and (symbol-value v)
  19919. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19920. (and
  19921. (get v 'custom-type) (get v 'standard-value)
  19922. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19923. (push v list)))))
  19924. (kill-buffer (get-buffer "*Warn about privacy*"))
  19925. list))
  19926. nil nil
  19927. "Remember to cover the basics, that is, what you expected to happen and
  19928. what in fact did happen. You don't know how to make a good report? See
  19929. http://orgmode.org/manual/Feedback.html#Feedback
  19930. Your bug report will be posted to the Org-mode mailing list.
  19931. ------------------------------------------------------------------------")
  19932. (save-excursion
  19933. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19934. (replace-match "\\1Bug: \\3 [\\2]")))))
  19935. (defun org-install-agenda-files-menu ()
  19936. (let ((bl (buffer-list)))
  19937. (save-excursion
  19938. (while bl
  19939. (set-buffer (pop bl))
  19940. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19941. (when (derived-mode-p 'org-mode)
  19942. (easy-menu-change
  19943. '("Org") "File List for Agenda"
  19944. (append
  19945. (list
  19946. ["Edit File List" (org-edit-agenda-file-list) t]
  19947. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19948. ["Remove Current File from List" org-remove-file t]
  19949. ["Cycle through agenda files" org-cycle-agenda-files t]
  19950. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19951. "--")
  19952. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19953. ;;;; Documentation
  19954. (defun org-require-autoloaded-modules ()
  19955. (interactive)
  19956. (mapc 'require
  19957. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19958. org-table org-timer)))
  19959. ;;;###autoload
  19960. (defun org-reload (&optional uncompiled)
  19961. "Reload all org lisp files.
  19962. With prefix arg UNCOMPILED, load the uncompiled versions."
  19963. (interactive "P")
  19964. (require 'loadhist)
  19965. (let* ((org-dir (org-find-library-dir "org"))
  19966. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19967. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19968. (remove-re (mapconcat 'identity
  19969. (mapcar (lambda (f) (concat "^" f "$"))
  19970. (list (if (featurep 'xemacs)
  19971. "org-colview"
  19972. "org-colview-xemacs")
  19973. "org" "org-loaddefs" "org-version"))
  19974. "\\|"))
  19975. (feats (delete-dups
  19976. (mapcar 'file-name-sans-extension
  19977. (mapcar 'file-name-nondirectory
  19978. (delq nil
  19979. (mapcar 'feature-file
  19980. features))))))
  19981. (lfeat (append
  19982. (sort
  19983. (setq feats
  19984. (delq nil (mapcar
  19985. (lambda (f)
  19986. (if (and (string-match feature-re f)
  19987. (not (string-match remove-re f)))
  19988. f nil))
  19989. feats)))
  19990. 'string-lessp)
  19991. (list "org-version" "org")))
  19992. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19993. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19994. load-uncore load-misses)
  19995. (setq load-misses
  19996. (delq 't
  19997. (mapcar (lambda (f)
  19998. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19999. (and (string= org-dir contrib-dir)
  20000. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20001. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20002. (add-to-list 'load-uncore f 'append)
  20003. 't)
  20004. f))
  20005. lfeat)))
  20006. (if load-uncore
  20007. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20008. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20009. (if load-misses
  20010. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20011. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20012. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20013. ;;;###autoload
  20014. (defun org-customize ()
  20015. "Call the customize function with org as argument."
  20016. (interactive)
  20017. (org-load-modules-maybe)
  20018. (org-require-autoloaded-modules)
  20019. (customize-browse 'org))
  20020. (defun org-create-customize-menu ()
  20021. "Create a full customization menu for Org-mode, insert it into the menu."
  20022. (interactive)
  20023. (org-load-modules-maybe)
  20024. (org-require-autoloaded-modules)
  20025. (if (fboundp 'customize-menu-create)
  20026. (progn
  20027. (easy-menu-change
  20028. '("Org") "Customize"
  20029. `(["Browse Org group" org-customize t]
  20030. "--"
  20031. ,(customize-menu-create 'org)
  20032. ["Set" Custom-set t]
  20033. ["Save" Custom-save t]
  20034. ["Reset to Current" Custom-reset-current t]
  20035. ["Reset to Saved" Custom-reset-saved t]
  20036. ["Reset to Standard Settings" Custom-reset-standard t]))
  20037. (message "\"Org\"-menu now contains full customization menu"))
  20038. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20039. ;;;; Miscellaneous stuff
  20040. ;;; Generally useful functions
  20041. (defsubst org-get-at-eol (property n)
  20042. "Get text property PROPERTY at the end of line less N characters."
  20043. (get-text-property (- (point-at-eol) n) property))
  20044. (defun org-find-text-property-in-string (prop s)
  20045. "Return the first non-nil value of property PROP in string S."
  20046. (or (get-text-property 0 prop s)
  20047. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20048. prop s)))
  20049. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20050. "Display the given MESSAGE as a warning."
  20051. (if (fboundp 'display-warning)
  20052. (display-warning 'org message
  20053. (if (featurep 'xemacs) 'warning :warning))
  20054. (let ((buf (get-buffer-create "*Org warnings*")))
  20055. (with-current-buffer buf
  20056. (goto-char (point-max))
  20057. (insert "Warning (Org): " message)
  20058. (unless (bolp)
  20059. (newline)))
  20060. (display-buffer buf)
  20061. (sit-for 0))))
  20062. (defun org-eval (form)
  20063. "Eval FORM and return result."
  20064. (condition-case error
  20065. (eval form)
  20066. (error (format "%%![Error: %s]" error))))
  20067. (defun org-in-clocktable-p ()
  20068. "Check if the cursor is in a clocktable."
  20069. (let ((pos (point)) start)
  20070. (save-excursion
  20071. (end-of-line 1)
  20072. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20073. (setq start (match-beginning 0))
  20074. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20075. (>= (match-end 0) pos)
  20076. start))))
  20077. (defun org-in-verbatim-emphasis ()
  20078. (save-match-data
  20079. (and (org-in-regexp org-emph-re 2)
  20080. (>= (point) (match-beginning 3))
  20081. (<= (point) (match-end 4))
  20082. (member (match-string 3) '("=" "~")))))
  20083. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20084. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20085. (if (and marker (marker-buffer marker)
  20086. (buffer-live-p (marker-buffer marker)))
  20087. (progn
  20088. (org-pop-to-buffer-same-window (marker-buffer marker))
  20089. (if (or (> marker (point-max)) (< marker (point-min)))
  20090. (widen))
  20091. (goto-char marker)
  20092. (org-show-context 'org-goto))
  20093. (if bookmark
  20094. (bookmark-jump bookmark)
  20095. (error "Cannot find location"))))
  20096. (defun org-quote-csv-field (s)
  20097. "Quote field for inclusion in CSV material."
  20098. (if (string-match "[\",]" s)
  20099. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20100. s))
  20101. (defun org-force-self-insert (N)
  20102. "Needed to enforce self-insert under remapping."
  20103. (interactive "p")
  20104. (self-insert-command N))
  20105. (defun org-string-width (s)
  20106. "Compute width of string, ignoring invisible characters.
  20107. This ignores character with invisibility property `org-link', and also
  20108. characters with property `org-cwidth', because these will become invisible
  20109. upon the next fontification round."
  20110. (let (b l)
  20111. (when (or (eq t buffer-invisibility-spec)
  20112. (assq 'org-link buffer-invisibility-spec))
  20113. (while (setq b (text-property-any 0 (length s)
  20114. 'invisible 'org-link s))
  20115. (setq s (concat (substring s 0 b)
  20116. (substring s (or (next-single-property-change
  20117. b 'invisible s) (length s)))))))
  20118. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20119. (setq s (concat (substring s 0 b)
  20120. (substring s (or (next-single-property-change
  20121. b 'org-cwidth s) (length s))))))
  20122. (setq l (string-width s) b -1)
  20123. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20124. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20125. l))
  20126. (defun org-shorten-string (s maxlength)
  20127. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20128. If the string is shorter or has length MAXLENGTH, just return the
  20129. original string. If it is longer, the functions finds a space in the
  20130. string, breaks this string off at that locations and adds three dots
  20131. as ellipsis. Including the ellipsis, the string will not be longer
  20132. than MAXLENGTH. If finding a good breaking point in the string does
  20133. not work, the string is just chopped off in the middle of a word
  20134. if necessary."
  20135. (if (<= (length s) maxlength)
  20136. s
  20137. (let* ((n (max (- maxlength 4) 1))
  20138. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20139. (if (string-match re s)
  20140. (concat (match-string 1 s) "...")
  20141. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20142. (defun org-get-indentation (&optional line)
  20143. "Get the indentation of the current line, interpreting tabs.
  20144. When LINE is given, assume it represents a line and compute its indentation."
  20145. (if line
  20146. (if (string-match "^ *" (org-remove-tabs line))
  20147. (match-end 0))
  20148. (save-excursion
  20149. (beginning-of-line 1)
  20150. (skip-chars-forward " \t")
  20151. (current-column))))
  20152. (defun org-get-string-indentation (s)
  20153. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20154. (let ((n -1) (i 0) (w tab-width) c)
  20155. (catch 'exit
  20156. (while (< (setq n (1+ n)) (length s))
  20157. (setq c (aref s n))
  20158. (cond ((= c ?\ ) (setq i (1+ i)))
  20159. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20160. (t (throw 'exit t)))))
  20161. i))
  20162. (defun org-remove-tabs (s &optional width)
  20163. "Replace tabulators in S with spaces.
  20164. Assumes that s is a single line, starting in column 0."
  20165. (setq width (or width tab-width))
  20166. (while (string-match "\t" s)
  20167. (setq s (replace-match
  20168. (make-string
  20169. (- (* width (/ (+ (match-beginning 0) width) width))
  20170. (match-beginning 0)) ?\ )
  20171. t t s)))
  20172. s)
  20173. (defun org-fix-indentation (line ind)
  20174. "Fix indentation in LINE.
  20175. IND is a cons cell with target and minimum indentation.
  20176. If the current indentation in LINE is smaller than the minimum,
  20177. leave it alone. If it is larger than ind, set it to the target."
  20178. (let* ((l (org-remove-tabs line))
  20179. (i (org-get-indentation l))
  20180. (i1 (car ind)) (i2 (cdr ind)))
  20181. (if (>= i i2) (setq l (substring line i2)))
  20182. (if (> i1 0)
  20183. (concat (make-string i1 ?\ ) l)
  20184. l)))
  20185. (defun org-remove-indentation (code &optional n)
  20186. "Remove the maximum common indentation from the lines in CODE.
  20187. N may optionally be the number of spaces to remove."
  20188. (with-temp-buffer
  20189. (insert code)
  20190. (org-do-remove-indentation n)
  20191. (buffer-string)))
  20192. (defun org-do-remove-indentation (&optional n)
  20193. "Remove the maximum common indentation from the buffer."
  20194. (untabify (point-min) (point-max))
  20195. (let ((min 10000) re)
  20196. (if n
  20197. (setq min n)
  20198. (goto-char (point-min))
  20199. (while (re-search-forward "^ *[^ \n]" nil t)
  20200. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20201. (unless (or (= min 0) (= min 10000))
  20202. (setq re (format "^ \\{%d\\}" min))
  20203. (goto-char (point-min))
  20204. (while (re-search-forward re nil t)
  20205. (replace-match "")
  20206. (end-of-line 1))
  20207. min)))
  20208. (defun org-fill-template (template alist)
  20209. "Find each %key of ALIST in TEMPLATE and replace it."
  20210. (let ((case-fold-search nil)
  20211. entry key value)
  20212. (setq alist (sort (copy-sequence alist)
  20213. (lambda (a b) (< (length (car a)) (length (car b))))))
  20214. (while (setq entry (pop alist))
  20215. (setq template
  20216. (replace-regexp-in-string
  20217. (concat "%" (regexp-quote (car entry)))
  20218. (or (cdr entry) "") template t t)))
  20219. template))
  20220. (defun org-base-buffer (buffer)
  20221. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20222. (if (not buffer)
  20223. buffer
  20224. (or (buffer-base-buffer buffer)
  20225. buffer)))
  20226. (defun org-wrap (string &optional width lines)
  20227. "Wrap string to either a number of lines, or a width in characters.
  20228. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20229. that costs. If there is a word longer than WIDTH, the text is actually
  20230. wrapped to the length of that word.
  20231. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20232. many lines, whatever width that takes.
  20233. The return value is a list of lines, without newlines at the end."
  20234. (let* ((words (org-split-string string "[ \t\n]+"))
  20235. (maxword (apply 'max (mapcar 'org-string-width words)))
  20236. w ll)
  20237. (cond (width
  20238. (org-do-wrap words (max maxword width)))
  20239. (lines
  20240. (setq w maxword)
  20241. (setq ll (org-do-wrap words maxword))
  20242. (if (<= (length ll) lines)
  20243. ll
  20244. (setq ll words)
  20245. (while (> (length ll) lines)
  20246. (setq w (1+ w))
  20247. (setq ll (org-do-wrap words w)))
  20248. ll))
  20249. (t (error "Cannot wrap this")))))
  20250. (defun org-do-wrap (words width)
  20251. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20252. (let (lines line)
  20253. (while words
  20254. (setq line (pop words))
  20255. (while (and words (< (+ (length line) (length (car words))) width))
  20256. (setq line (concat line " " (pop words))))
  20257. (setq lines (push line lines)))
  20258. (nreverse lines)))
  20259. (defun org-split-string (string &optional separators)
  20260. "Splits STRING into substrings at SEPARATORS.
  20261. No empty strings are returned if there are matches at the beginning
  20262. and end of string."
  20263. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20264. (start 0)
  20265. notfirst
  20266. (list nil))
  20267. (while (and (string-match rexp string
  20268. (if (and notfirst
  20269. (= start (match-beginning 0))
  20270. (< start (length string)))
  20271. (1+ start) start))
  20272. (< (match-beginning 0) (length string)))
  20273. (setq notfirst t)
  20274. (or (eq (match-beginning 0) 0)
  20275. (and (eq (match-beginning 0) (match-end 0))
  20276. (eq (match-beginning 0) start))
  20277. (setq list
  20278. (cons (substring string start (match-beginning 0))
  20279. list)))
  20280. (setq start (match-end 0)))
  20281. (or (eq start (length string))
  20282. (setq list
  20283. (cons (substring string start)
  20284. list)))
  20285. (nreverse list)))
  20286. (defun org-quote-vert (s)
  20287. "Replace \"|\" with \"\\vert\"."
  20288. (while (string-match "|" s)
  20289. (setq s (replace-match "\\vert" t t s)))
  20290. s)
  20291. (defun org-uuidgen-p (s)
  20292. "Is S an ID created by UUIDGEN?"
  20293. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20294. (defun org-in-src-block-p (&optional inside)
  20295. "Whether point is in a code source block.
  20296. When INSIDE is non-nil, don't consider we are within a src block
  20297. when point is at #+BEGIN_SRC or #+END_SRC."
  20298. (let ((case-fold-search t) ov)
  20299. (or (and (eq (get-char-property (point) 'src-block) t))
  20300. (and (not inside)
  20301. (save-match-data
  20302. (save-excursion
  20303. (beginning-of-line)
  20304. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20305. (defun org-context ()
  20306. "Return a list of contexts of the current cursor position.
  20307. If several contexts apply, all are returned.
  20308. Each context entry is a list with a symbol naming the context, and
  20309. two positions indicating start and end of the context. Possible
  20310. contexts are:
  20311. :headline anywhere in a headline
  20312. :headline-stars on the leading stars in a headline
  20313. :todo-keyword on a TODO keyword (including DONE) in a headline
  20314. :tags on the TAGS in a headline
  20315. :priority on the priority cookie in a headline
  20316. :item on the first line of a plain list item
  20317. :item-bullet on the bullet/number of a plain list item
  20318. :checkbox on the checkbox in a plain list item
  20319. :table in an org-mode table
  20320. :table-special on a special filed in a table
  20321. :table-table in a table.el table
  20322. :clocktable in a clocktable
  20323. :src-block in a source block
  20324. :link on a hyperlink
  20325. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20326. :target on a <<target>>
  20327. :radio-target on a <<<radio-target>>>
  20328. :latex-fragment on a LaTeX fragment
  20329. :latex-preview on a LaTeX fragment with overlaid preview image
  20330. This function expects the position to be visible because it uses font-lock
  20331. faces as a help to recognize the following contexts: :table-special, :link,
  20332. and :keyword."
  20333. (let* ((f (get-text-property (point) 'face))
  20334. (faces (if (listp f) f (list f)))
  20335. (case-fold-search t)
  20336. (p (point)) clist o)
  20337. ;; First the large context
  20338. (cond
  20339. ((org-at-heading-p t)
  20340. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20341. (when (progn
  20342. (beginning-of-line 1)
  20343. (looking-at org-todo-line-tags-regexp))
  20344. (push (org-point-in-group p 1 :headline-stars) clist)
  20345. (push (org-point-in-group p 2 :todo-keyword) clist)
  20346. (push (org-point-in-group p 4 :tags) clist))
  20347. (goto-char p)
  20348. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20349. (if (looking-at "\\[#[A-Z0-9]\\]")
  20350. (push (org-point-in-group p 0 :priority) clist)))
  20351. ((org-at-item-p)
  20352. (push (org-point-in-group p 2 :item-bullet) clist)
  20353. (push (list :item (point-at-bol)
  20354. (save-excursion (org-end-of-item) (point)))
  20355. clist)
  20356. (and (org-at-item-checkbox-p)
  20357. (push (org-point-in-group p 0 :checkbox) clist)))
  20358. ((org-at-table-p)
  20359. (push (list :table (org-table-begin) (org-table-end)) clist)
  20360. (if (memq 'org-formula faces)
  20361. (push (list :table-special
  20362. (previous-single-property-change p 'face)
  20363. (next-single-property-change p 'face)) clist)))
  20364. ((org-at-table-p 'any)
  20365. (push (list :table-table) clist)))
  20366. (goto-char p)
  20367. (let ((case-fold-search t))
  20368. ;; New the "medium" contexts: clocktables, source blocks
  20369. (cond ((org-in-clocktable-p)
  20370. (push (list :clocktable
  20371. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20372. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20373. (match-beginning 1))
  20374. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20375. (match-end 0))) clist))
  20376. ((org-in-src-block-p)
  20377. (push (list :src-block
  20378. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20379. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20380. (match-beginning 1))
  20381. (and (search-forward "#+END_SRC" nil t)
  20382. (match-beginning 0))) clist))))
  20383. (goto-char p)
  20384. ;; Now the small context
  20385. (cond
  20386. ((org-at-timestamp-p)
  20387. (push (org-point-in-group p 0 :timestamp) clist))
  20388. ((memq 'org-link faces)
  20389. (push (list :link
  20390. (previous-single-property-change p 'face)
  20391. (next-single-property-change p 'face)) clist))
  20392. ((memq 'org-special-keyword faces)
  20393. (push (list :keyword
  20394. (previous-single-property-change p 'face)
  20395. (next-single-property-change p 'face)) clist))
  20396. ((org-at-target-p)
  20397. (push (org-point-in-group p 0 :target) clist)
  20398. (goto-char (1- (match-beginning 0)))
  20399. (if (looking-at org-radio-target-regexp)
  20400. (push (org-point-in-group p 0 :radio-target) clist))
  20401. (goto-char p))
  20402. ((setq o (car (delq nil
  20403. (mapcar
  20404. (lambda (x)
  20405. (if (memq x org-latex-fragment-image-overlays) x))
  20406. (overlays-at (point))))))
  20407. (push (list :latex-fragment
  20408. (overlay-start o) (overlay-end o)) clist)
  20409. (push (list :latex-preview
  20410. (overlay-start o) (overlay-end o)) clist))
  20411. ((org-inside-LaTeX-fragment-p)
  20412. ;; FIXME: positions wrong.
  20413. (push (list :latex-fragment (point) (point)) clist)))
  20414. (setq clist (nreverse (delq nil clist)))
  20415. clist))
  20416. (defun org-in-regexp (re &optional nlines visually)
  20417. "Check if point is inside a match of RE.
  20418. Normally only the current line is checked, but you can include
  20419. NLINES extra lines after point into the search. If VISUALLY is
  20420. set, require that the cursor is not after the match but really
  20421. on, so that the block visually is on the match."
  20422. (catch 'exit
  20423. (let ((pos (point))
  20424. (eol (point-at-eol (+ 1 (or nlines 0))))
  20425. (inc (if visually 1 0)))
  20426. (save-excursion
  20427. (beginning-of-line (- 1 (or nlines 0)))
  20428. (while (re-search-forward re eol t)
  20429. (if (and (<= (match-beginning 0) pos)
  20430. (>= (+ inc (match-end 0)) pos))
  20431. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20432. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20433. "Org mode 8.3")
  20434. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20435. "Non-nil when point is between matches of START-RE and END-RE.
  20436. Also return a non-nil value when point is on one of the matches.
  20437. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20438. buffer positions. Default values are the positions of headlines
  20439. surrounding the point.
  20440. The functions returns a cons cell whose car (resp. cdr) is the
  20441. position before START-RE (resp. after END-RE)."
  20442. (save-match-data
  20443. (let ((pos (point))
  20444. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20445. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20446. beg end)
  20447. (save-excursion
  20448. ;; Point is on a block when on START-RE or if START-RE can be
  20449. ;; found before it...
  20450. (and (or (org-in-regexp start-re)
  20451. (re-search-backward start-re limit-up t))
  20452. (setq beg (match-beginning 0))
  20453. ;; ... and END-RE after it...
  20454. (goto-char (match-end 0))
  20455. (re-search-forward end-re limit-down t)
  20456. (> (setq end (match-end 0)) pos)
  20457. ;; ... without another START-RE in-between.
  20458. (goto-char (match-beginning 0))
  20459. (not (re-search-backward start-re (1+ beg) t))
  20460. ;; Return value.
  20461. (cons beg end))))))
  20462. (defun org-in-block-p (names)
  20463. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20464. NAMES is a list of strings containing names of blocks.
  20465. Return first block name matched, or nil. Beware that in case of
  20466. nested blocks, the returned name may not belong to the closest
  20467. block from point."
  20468. (save-match-data
  20469. (catch 'exit
  20470. (let ((case-fold-search t)
  20471. (lim-up (save-excursion (outline-previous-heading)))
  20472. (lim-down (save-excursion (outline-next-heading))))
  20473. (mapc (lambda (name)
  20474. (let ((n (regexp-quote name)))
  20475. (when (org-between-regexps-p
  20476. (concat "^[ \t]*#\\+begin_" n)
  20477. (concat "^[ \t]*#\\+end_" n)
  20478. lim-up lim-down)
  20479. (throw 'exit n))))
  20480. names))
  20481. nil)))
  20482. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20483. "Call `multi-occur' with buffers for all agenda files."
  20484. (interactive "sOrg-files matching: \np")
  20485. (let* ((files (org-agenda-files))
  20486. (tnames (mapcar 'file-truename files))
  20487. (extra org-agenda-text-search-extra-files)
  20488. f)
  20489. (when (eq (car extra) 'agenda-archives)
  20490. (setq extra (cdr extra))
  20491. (setq files (org-add-archive-files files)))
  20492. (while (setq f (pop extra))
  20493. (unless (member (file-truename f) tnames)
  20494. (add-to-list 'files f 'append)
  20495. (add-to-list 'tnames (file-truename f) 'append)))
  20496. (multi-occur
  20497. (mapcar (lambda (x)
  20498. (with-current-buffer
  20499. (or (get-file-buffer x) (find-file-noselect x))
  20500. (widen)
  20501. (current-buffer)))
  20502. files)
  20503. regexp)))
  20504. (if (boundp 'occur-mode-find-occurrence-hook)
  20505. ;; Emacs 23
  20506. (add-hook 'occur-mode-find-occurrence-hook
  20507. (lambda ()
  20508. (when (derived-mode-p 'org-mode)
  20509. (org-reveal))))
  20510. ;; Emacs 22
  20511. (defadvice occur-mode-goto-occurrence
  20512. (after org-occur-reveal activate)
  20513. (and (derived-mode-p 'org-mode) (org-reveal)))
  20514. (defadvice occur-mode-goto-occurrence-other-window
  20515. (after org-occur-reveal activate)
  20516. (and (derived-mode-p 'org-mode) (org-reveal)))
  20517. (defadvice occur-mode-display-occurrence
  20518. (after org-occur-reveal activate)
  20519. (when (derived-mode-p 'org-mode)
  20520. (let ((pos (occur-mode-find-occurrence)))
  20521. (with-current-buffer (marker-buffer pos)
  20522. (save-excursion
  20523. (goto-char pos)
  20524. (org-reveal)))))))
  20525. (defun org-occur-link-in-agenda-files ()
  20526. "Create a link and search for it in the agendas.
  20527. The link is not stored in `org-stored-links', it is just created
  20528. for the search purpose."
  20529. (interactive)
  20530. (let ((link (condition-case nil
  20531. (org-store-link nil)
  20532. (error "Unable to create a link to here"))))
  20533. (org-occur-in-agenda-files (regexp-quote link))))
  20534. (defun org-reverse-string (string)
  20535. "Return the reverse of STRING."
  20536. (apply 'string (reverse (string-to-list string))))
  20537. ;; defsubst org-uniquify must be defined before first use
  20538. (defun org-uniquify-alist (alist)
  20539. "Merge elements of ALIST with the same key.
  20540. For example, in this alist:
  20541. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20542. => '((a 1 3) (b 2))
  20543. merge (a 1) and (a 3) into (a 1 3).
  20544. The function returns the new ALIST."
  20545. (let (rtn)
  20546. (mapc
  20547. (lambda (e)
  20548. (let (n)
  20549. (if (not (assoc (car e) rtn))
  20550. (push e rtn)
  20551. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20552. (setq rtn (assq-delete-all (car e) rtn))
  20553. (push n rtn))))
  20554. alist)
  20555. rtn))
  20556. (defun org-delete-all (elts list)
  20557. "Remove all elements in ELTS from LIST."
  20558. (while elts
  20559. (setq list (delete (pop elts) list)))
  20560. list)
  20561. (defun org-count (cl-item cl-seq)
  20562. "Count the number of occurrences of ITEM in SEQ.
  20563. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20564. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20565. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20566. (while (< cl-start cl-end)
  20567. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20568. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20569. (setq cl-start (1+ cl-start)))
  20570. cl-count))
  20571. (defun org-remove-if (predicate seq)
  20572. "Remove everything from SEQ that fulfills PREDICATE."
  20573. (let (res e)
  20574. (while seq
  20575. (setq e (pop seq))
  20576. (if (not (funcall predicate e)) (push e res)))
  20577. (nreverse res)))
  20578. (defun org-remove-if-not (predicate seq)
  20579. "Remove everything from SEQ that does not fulfill PREDICATE."
  20580. (let (res e)
  20581. (while seq
  20582. (setq e (pop seq))
  20583. (if (funcall predicate e) (push e res)))
  20584. (nreverse res)))
  20585. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20586. "Reduce two-argument FUNCTION across SEQ.
  20587. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20588. \":initial-value\" removed."
  20589. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20590. (cadr (memq :initial-value cl-keys)))
  20591. (cl-seq (pop cl-seq))
  20592. (t (funcall cl-func)))))
  20593. (while cl-seq
  20594. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20595. cl-accum))
  20596. (defun org-every (pred seq)
  20597. "Return true if PREDICATE is true of every element of SEQ.
  20598. Adapted from `every' in cl.el."
  20599. (catch 'org-every
  20600. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20601. t))
  20602. (defun org-some (pred seq)
  20603. "Return true if PREDICATE is true of any element of SEQ.
  20604. Adapted from `some' in cl.el."
  20605. (catch 'org-some
  20606. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20607. nil))
  20608. (defun org-back-over-empty-lines ()
  20609. "Move backwards over whitespace, to the beginning of the first empty line.
  20610. Returns the number of empty lines passed."
  20611. (let ((pos (point)))
  20612. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20613. (skip-chars-backward " \t\n\r")
  20614. (unless (eobp)
  20615. (forward-line -1)))
  20616. (beginning-of-line 2)
  20617. (goto-char (min (point) pos))
  20618. (count-lines (point) pos)))
  20619. (defun org-skip-whitespace ()
  20620. (skip-chars-forward " \t\n\r"))
  20621. (defun org-point-in-group (point group &optional context)
  20622. "Check if POINT is in match-group GROUP.
  20623. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20624. match. If the match group does not exist or point is not inside it,
  20625. return nil."
  20626. (and (match-beginning group)
  20627. (>= point (match-beginning group))
  20628. (<= point (match-end group))
  20629. (if context
  20630. (list context (match-beginning group) (match-end group))
  20631. t)))
  20632. (defun org-switch-to-buffer-other-window (&rest args)
  20633. "Switch to buffer in a second window on the current frame.
  20634. In particular, do not allow pop-up frames.
  20635. Returns the newly created buffer."
  20636. (org-no-popups
  20637. (apply 'switch-to-buffer-other-window args)))
  20638. (defun org-combine-plists (&rest plists)
  20639. "Create a single property list from all plists in PLISTS.
  20640. The process starts by copying the first list, and then setting properties
  20641. from the other lists. Settings in the last list are the most significant
  20642. ones and overrule settings in the other lists."
  20643. (let ((rtn (copy-sequence (pop plists)))
  20644. p v ls)
  20645. (while plists
  20646. (setq ls (pop plists))
  20647. (while ls
  20648. (setq p (pop ls) v (pop ls))
  20649. (setq rtn (plist-put rtn p v))))
  20650. rtn))
  20651. (defun org-replace-escapes (string table)
  20652. "Replace %-escapes in STRING with values in TABLE.
  20653. TABLE is an association list with keys like \"%a\" and string values.
  20654. The sequences in STRING may contain normal field width and padding information,
  20655. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20656. so values can contain further %-escapes if they are define later in TABLE."
  20657. (let ((tbl (copy-alist table))
  20658. (case-fold-search nil)
  20659. (pchg 0)
  20660. e re rpl)
  20661. (while (setq e (pop tbl))
  20662. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20663. (when (and (cdr e) (string-match re (cdr e)))
  20664. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20665. (safe "SREF"))
  20666. (add-text-properties 0 3 (list 'sref sref) safe)
  20667. (setcdr e (replace-match safe t t (cdr e)))))
  20668. (while (string-match re string)
  20669. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20670. (cdr e)))
  20671. (setq string (replace-match rpl t t string))))
  20672. (while (setq pchg (next-property-change pchg string))
  20673. (let ((sref (get-text-property pchg 'sref string)))
  20674. (when (and sref (string-match "SREF" string pchg))
  20675. (setq string (replace-match sref t t string)))))
  20676. string))
  20677. (defun org-sublist (list start end)
  20678. "Return a section of LIST, from START to END.
  20679. Counting starts at 1."
  20680. (let (rtn (c start))
  20681. (setq list (nthcdr (1- start) list))
  20682. (while (and list (<= c end))
  20683. (push (pop list) rtn)
  20684. (setq c (1+ c)))
  20685. (nreverse rtn)))
  20686. (defun org-find-base-buffer-visiting (file)
  20687. "Like `find-buffer-visiting' but always return the base buffer and
  20688. not an indirect buffer."
  20689. (let ((buf (or (get-file-buffer file)
  20690. (find-buffer-visiting file))))
  20691. (if buf
  20692. (or (buffer-base-buffer buf) buf)
  20693. nil)))
  20694. (defun org-image-file-name-regexp (&optional extensions)
  20695. "Return regexp matching the file names of images.
  20696. If EXTENSIONS is given, only match these."
  20697. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20698. (image-file-name-regexp)
  20699. (let ((image-file-name-extensions
  20700. (or extensions
  20701. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20702. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20703. (concat "\\."
  20704. (regexp-opt (nconc (mapcar 'upcase
  20705. image-file-name-extensions)
  20706. image-file-name-extensions)
  20707. t)
  20708. "\\'"))))
  20709. (defun org-file-image-p (file &optional extensions)
  20710. "Return non-nil if FILE is an image."
  20711. (save-match-data
  20712. (string-match (org-image-file-name-regexp extensions) file)))
  20713. (defun org-get-cursor-date (&optional with-time)
  20714. "Return the date at cursor in as a time.
  20715. This works in the calendar and in the agenda, anywhere else it just
  20716. returns the current time.
  20717. If WITH-TIME is non-nil, returns the time of the event at point (in
  20718. the agenda) or the current time of the day."
  20719. (let (date day defd tp tm hod mod)
  20720. (when with-time
  20721. (setq tp (get-text-property (point) 'time))
  20722. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20723. (setq hod (string-to-number (match-string 1 tp))
  20724. mod (string-to-number (match-string 2 tp))))
  20725. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20726. mod (nth 1 (decode-time (current-time))))))
  20727. (cond
  20728. ((eq major-mode 'calendar-mode)
  20729. (setq date (calendar-cursor-to-date)
  20730. defd (encode-time 0 (or mod 0) (or hod 0)
  20731. (nth 1 date) (nth 0 date) (nth 2 date))))
  20732. ((eq major-mode 'org-agenda-mode)
  20733. (setq day (get-text-property (point) 'day))
  20734. (if day
  20735. (setq date (calendar-gregorian-from-absolute day)
  20736. defd (encode-time 0 (or mod 0) (or hod 0)
  20737. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20738. (or defd (current-time))))
  20739. (defun org-mark-subtree (&optional up)
  20740. "Mark the current subtree.
  20741. This puts point at the start of the current subtree, and mark at
  20742. the end. If a numeric prefix UP is given, move up into the
  20743. hierarchy of headlines by UP levels before marking the subtree."
  20744. (interactive "P")
  20745. (org-with-limited-levels
  20746. (cond ((org-at-heading-p) (beginning-of-line))
  20747. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20748. (t (outline-previous-visible-heading 1))))
  20749. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20750. (if (org-called-interactively-p 'any)
  20751. (call-interactively 'org-mark-element)
  20752. (org-mark-element)))
  20753. ;;; Indentation
  20754. (defun org--get-expected-indentation (element contentsp)
  20755. "Expected indentation column for current line, according to ELEMENT.
  20756. ELEMENT is an element containing point. CONTENTSP is non-nil
  20757. when indentation is to be computed according to contents of
  20758. ELEMENT."
  20759. (let ((type (org-element-type element))
  20760. (start (org-element-property :begin element)))
  20761. (org-with-wide-buffer
  20762. (cond
  20763. (contentsp
  20764. (case type
  20765. (footnote-definition 0)
  20766. ((headline inlinetask nil)
  20767. (if (not org-adapt-indentation) 0
  20768. (let ((level (org-current-level)))
  20769. (if level (1+ level) 0))))
  20770. (item
  20771. (org-list-item-body-column
  20772. (org-element-property :post-affiliated element)))
  20773. (plain-list
  20774. (save-excursion
  20775. (goto-char (org-element-property :post-affiliated element))
  20776. (org-get-indentation)))
  20777. (otherwise
  20778. (goto-char start)
  20779. (org-get-indentation))))
  20780. ((memq type '(headline inlinetask nil))
  20781. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20782. (org--get-expected-indentation element t)
  20783. 0))
  20784. ((eq type 'footnote-definition) 0)
  20785. ;; First paragraph of a footnote definition or an item.
  20786. ;; Indent like parent.
  20787. ((< (line-beginning-position) start)
  20788. (org--get-expected-indentation
  20789. (org-element-property :parent element) t))
  20790. ;; At first line: indent according to previous sibling, if any,
  20791. ;; ignoring footnote definitions and inline tasks, or parent's
  20792. ;; contents.
  20793. ((= (line-beginning-position) start)
  20794. (catch 'exit
  20795. (while t
  20796. (if (= (point-min) start) (throw 'exit 0)
  20797. (goto-char (1- start))
  20798. (let* ((previous (org-element-at-point))
  20799. (parent previous))
  20800. (while (and parent (<= (org-element-property :end parent) start))
  20801. (setq previous parent
  20802. parent (org-element-property :parent parent)))
  20803. (cond
  20804. ((not previous) (throw 'exit 0))
  20805. ((> (org-element-property :end previous) start)
  20806. (throw 'exit (org--get-expected-indentation previous t)))
  20807. ((memq (org-element-type previous)
  20808. '(footnote-definition inlinetask))
  20809. (setq start (org-element-property :begin previous)))
  20810. (t (goto-char (org-element-property :begin previous))
  20811. (throw 'exit
  20812. (if (bolp) (org-get-indentation)
  20813. ;; At first paragraph in an item or
  20814. ;; a footnote definition.
  20815. (org--get-expected-indentation
  20816. (org-element-property :parent previous) t))))))))))
  20817. ;; Otherwise, move to the first non-blank line above.
  20818. (t
  20819. (beginning-of-line)
  20820. (let ((pos (point)))
  20821. (skip-chars-backward " \r\t\n")
  20822. (cond
  20823. ;; Two blank lines end a footnote definition or a plain
  20824. ;; list. When we indent an empty line after them, the
  20825. ;; containing list or footnote definition is over, so it
  20826. ;; qualifies as a previous sibling. Therefore, we indent
  20827. ;; like its first line.
  20828. ((and (memq type '(footnote-definition plain-list))
  20829. (> (count-lines (point) pos) 2))
  20830. (goto-char start)
  20831. (org-get-indentation))
  20832. ;; Line above is the first one of a paragraph at the
  20833. ;; beginning of an item or a footnote definition. Indent
  20834. ;; like parent.
  20835. ((< (line-beginning-position) start)
  20836. (org--get-expected-indentation
  20837. (org-element-property :parent element) t))
  20838. ;; POS is after contents in a greater element. Indent like
  20839. ;; the beginning of the element.
  20840. ;;
  20841. ;; As a special case, if point is at the end of a footnote
  20842. ;; definition or an item, indent like the very last element
  20843. ;; within.
  20844. ((and (not (eq type 'paragraph))
  20845. (let ((cend (org-element-property :contents-end element)))
  20846. (and cend (<= cend pos))))
  20847. (if (memq type '(footnote-definition item plain-list))
  20848. (org--get-expected-indentation (org-element-at-point) nil)
  20849. (goto-char start)
  20850. (org-get-indentation)))
  20851. ;; In any other case, indent like the current line.
  20852. (t (org-get-indentation)))))))))
  20853. (defun org--align-node-property ()
  20854. "Align node property at point.
  20855. Alignment is done according to `org-property-format', which see."
  20856. (when (save-excursion
  20857. (beginning-of-line)
  20858. (looking-at org-property-re))
  20859. (replace-match
  20860. (concat (match-string 4)
  20861. (org-trim
  20862. (format org-property-format (match-string 1) (match-string 3))))
  20863. t t)))
  20864. (defun org-indent-line ()
  20865. "Indent line depending on context.
  20866. Indentation is done according to the following rules:
  20867. - Footnote definitions, headlines and inline tasks have to
  20868. start at column 0.
  20869. - On the very first line of an element, consider, in order, the
  20870. next rules until one matches:
  20871. 1. If there's a sibling element before, ignoring footnote
  20872. definitions and inline tasks, indent like its first line.
  20873. 2. If element has a parent, indent like its contents. More
  20874. precisely, if parent is an item, indent after the
  20875. description part, if any, or the bullet (see
  20876. `org-list-description-max-indent'). Else, indent like
  20877. parent's first line.
  20878. 3. Otherwise, indent relatively to current level, if
  20879. `org-adapt-indentation' is non-nil, or to left margin.
  20880. - On a blank line at the end of an element, indent according to
  20881. the type of the element. More precisely
  20882. 1. If element is a plain list, an item, or a footnote
  20883. definition, indent like the very last element within.
  20884. 2. If element is a paragraph, indent like its last non blank
  20885. line.
  20886. 3. Otherwise, indent like its very first line.
  20887. - In the code part of a source block, use language major mode
  20888. to indent current line if `org-src-tab-acts-natively' is
  20889. non-nil. If it is nil, do nothing.
  20890. - Otherwise, indent like the first non-blank line above.
  20891. The function doesn't indent an item as it could break the whole
  20892. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20893. \\[org-shiftmetaright].
  20894. Also align node properties according to `org-property-format'."
  20895. (interactive)
  20896. (cond
  20897. (orgstruct-is-++
  20898. (let ((indent-line-function
  20899. (cadadr (assq 'indent-line-function org-fb-vars))))
  20900. (indent-according-to-mode)))
  20901. ((org-at-heading-p) 'noindent)
  20902. (t
  20903. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20904. (type (org-element-type element)))
  20905. (cond ((and (memq type '(plain-list item))
  20906. (= (line-beginning-position)
  20907. (org-element-property :post-affiliated element)))
  20908. 'noindent)
  20909. ((and (eq type 'src-block)
  20910. org-src-tab-acts-natively
  20911. (> (line-beginning-position)
  20912. (org-element-property :post-affiliated element))
  20913. (< (line-beginning-position)
  20914. (org-with-wide-buffer
  20915. (goto-char (org-element-property :end element))
  20916. (skip-chars-backward " \r\t\n")
  20917. (line-beginning-position))))
  20918. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  20919. (t
  20920. (let ((column (org--get-expected-indentation element nil)))
  20921. ;; Preserve current column.
  20922. (if (<= (current-column) (current-indentation))
  20923. (org-indent-line-to column)
  20924. (save-excursion (org-indent-line-to column))))
  20925. ;; Align node property. Also preserve current column.
  20926. (when (eq type 'node-property)
  20927. (let ((column (current-column)))
  20928. (org--align-node-property)
  20929. (org-move-to-column column)))))))))
  20930. (defun org-indent-region (start end)
  20931. "Indent each non-blank line in the region.
  20932. Called from a program, START and END specify the region to
  20933. indent. The function will not indent contents of example blocks,
  20934. verse blocks and export blocks as leading white spaces are
  20935. assumed to be significant there."
  20936. (interactive "r")
  20937. (save-excursion
  20938. (goto-char start)
  20939. (skip-chars-forward " \r\t\n")
  20940. (unless (eobp) (beginning-of-line))
  20941. (let ((indent-to
  20942. (lambda (ind pos)
  20943. ;; Set IND as indentation for all lines between point and
  20944. ;; POS or END, whichever comes first. Blank lines are
  20945. ;; ignored. Leave point after POS once done.
  20946. (let ((limit (copy-marker (min end pos))))
  20947. (while (< (point) limit)
  20948. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20949. (forward-line))
  20950. (set-marker limit nil))))
  20951. (end (copy-marker end)))
  20952. (while (< (point) end)
  20953. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20954. (let* ((element (org-element-at-point))
  20955. (type (org-element-type element))
  20956. (element-end (copy-marker (org-element-property :end element)))
  20957. (ind (org--get-expected-indentation element nil)))
  20958. (cond
  20959. ((or (memq type '(paragraph table table-row))
  20960. (not (or (org-element-property :contents-begin element)
  20961. (memq type
  20962. '(example-block export-block src-block)))))
  20963. ;; Elements here are indented as a single block. Also
  20964. ;; align node properties.
  20965. (when (eq type 'node-property)
  20966. (org--align-node-property)
  20967. (beginning-of-line))
  20968. (funcall indent-to ind element-end))
  20969. (t
  20970. ;; Elements in this category consist of three parts:
  20971. ;; before the contents, the contents, and after the
  20972. ;; contents. The contents are treated specially,
  20973. ;; according to the element type, or not indented at
  20974. ;; all. Other parts are indented as a single block.
  20975. (let* ((post (copy-marker
  20976. (org-element-property :post-affiliated element)))
  20977. (cbeg
  20978. (copy-marker
  20979. (cond
  20980. ((not (org-element-property :contents-begin element))
  20981. ;; Fake contents for source blocks.
  20982. (org-with-wide-buffer
  20983. (goto-char post)
  20984. (forward-line)
  20985. (point)))
  20986. ((memq type '(footnote-definition item plain-list))
  20987. ;; Contents in these elements could start on
  20988. ;; the same line as the beginning of the
  20989. ;; element. Make sure we start indenting
  20990. ;; from the second line.
  20991. (org-with-wide-buffer
  20992. (goto-char post)
  20993. (end-of-line)
  20994. (skip-chars-forward " \r\t\n")
  20995. (if (eobp) (point) (line-beginning-position))))
  20996. (t (org-element-property :contents-begin element)))))
  20997. (cend (copy-marker
  20998. (or (org-element-property :contents-end element)
  20999. ;; Fake contents for source blocks.
  21000. (org-with-wide-buffer
  21001. (goto-char element-end)
  21002. (skip-chars-backward " \r\t\n")
  21003. (line-beginning-position)))
  21004. t)))
  21005. ;; Do not change items indentation individually as it
  21006. ;; might break the list as a whole. On the other
  21007. ;; hand, when at a plain list, indent it as a whole.
  21008. (cond ((eq type 'plain-list)
  21009. (let ((offset (- ind (org-get-indentation))))
  21010. (unless (zerop offset)
  21011. (indent-rigidly (org-element-property :begin element)
  21012. (org-element-property :end element)
  21013. offset))
  21014. (goto-char cbeg)))
  21015. ((eq type 'item) (goto-char cbeg))
  21016. (t (funcall indent-to ind cbeg)))
  21017. (when (< (point) end)
  21018. (case type
  21019. ((example-block export-block verse-block))
  21020. (src-block
  21021. ;; In a source block, indent source code
  21022. ;; according to language major mode, but only if
  21023. ;; `org-src-tab-acts-natively' is non-nil.
  21024. (when (and (< (point) end) org-src-tab-acts-natively)
  21025. (ignore-errors
  21026. (org-babel-do-in-edit-buffer
  21027. (indent-region (point-min) (point-max))))))
  21028. (t (org-indent-region (point) (min cend end))))
  21029. (goto-char (min cend end))
  21030. (when (< (point) end) (funcall indent-to ind element-end)))
  21031. (set-marker post nil)
  21032. (set-marker cbeg nil)
  21033. (set-marker cend nil))))
  21034. (set-marker element-end nil))))
  21035. (set-marker end nil))))
  21036. (defun org-indent-drawer ()
  21037. "Indent the drawer at point."
  21038. (interactive)
  21039. (unless (save-excursion
  21040. (beginning-of-line)
  21041. (org-looking-at-p org-drawer-regexp))
  21042. (user-error "Not at a drawer"))
  21043. (let ((element (org-element-at-point)))
  21044. (unless (memq (org-element-type element) '(drawer property-drawer))
  21045. (user-error "Not at a drawer"))
  21046. (org-with-wide-buffer
  21047. (org-indent-region (org-element-property :begin element)
  21048. (org-element-property :end element))))
  21049. (message "Drawer at point indented"))
  21050. (defun org-indent-block ()
  21051. "Indent the block at point."
  21052. (interactive)
  21053. (unless (save-excursion
  21054. (beginning-of-line)
  21055. (let ((case-fold-search t))
  21056. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21057. (user-error "Not at a block"))
  21058. (let ((element (org-element-at-point)))
  21059. (unless (memq (org-element-type element)
  21060. '(comment-block center-block dynamic-block example-block
  21061. export-block quote-block special-block
  21062. src-block verse-block))
  21063. (user-error "Not at a block"))
  21064. (org-with-wide-buffer
  21065. (org-indent-region (org-element-property :begin element)
  21066. (org-element-property :end element))))
  21067. (message "Block at point indented"))
  21068. ;;; Filling
  21069. ;; We use our own fill-paragraph and auto-fill functions.
  21070. ;; `org-fill-paragraph' relies on adaptive filling and context
  21071. ;; checking. Appropriate `fill-prefix' is computed with
  21072. ;; `org-adaptive-fill-function'.
  21073. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21074. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21075. ;; `org-adaptive-fill-function' value. Internally,
  21076. ;; `org-comment-line-break-function' breaks the line.
  21077. ;; `org-setup-filling' installs filling and auto-filling related
  21078. ;; variables during `org-mode' initialization.
  21079. (defvar org-element-paragraph-separate) ; org-element.el
  21080. (defun org-setup-filling ()
  21081. (require 'org-element)
  21082. ;; Prevent auto-fill from inserting unwanted new items.
  21083. (when (boundp 'fill-nobreak-predicate)
  21084. (org-set-local
  21085. 'fill-nobreak-predicate
  21086. (org-uniquify
  21087. (append fill-nobreak-predicate
  21088. '(org-fill-line-break-nobreak-p
  21089. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21090. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21091. (org-set-local 'paragraph-start paragraph-ending)
  21092. (org-set-local 'paragraph-separate paragraph-ending))
  21093. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21094. (org-set-local 'auto-fill-inhibit-regexp nil)
  21095. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21096. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21097. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21098. (defun org-fill-line-break-nobreak-p ()
  21099. "Non-nil when a new line at point would create an Org line break."
  21100. (save-excursion
  21101. (skip-chars-backward "[ \t]")
  21102. (skip-chars-backward "\\\\")
  21103. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21104. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21105. "Non-nil when a new line at point would split a timestamp."
  21106. (and (org-at-timestamp-p t)
  21107. (not (looking-at org-ts-regexp-both))))
  21108. (declare-function message-in-body-p "message" ())
  21109. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21110. (defun org-adaptive-fill-function ()
  21111. "Compute a fill prefix for the current line.
  21112. Return fill prefix, as a string, or nil if current line isn't
  21113. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21114. matches in paragraphs or comments, use it."
  21115. (catch 'exit
  21116. (when (derived-mode-p 'message-mode)
  21117. (save-excursion
  21118. (beginning-of-line)
  21119. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21120. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21121. ((looking-at message-cite-prefix-regexp)
  21122. (throw 'exit (match-string-no-properties 0)))
  21123. ((looking-at org-outline-regexp)
  21124. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21125. (org-with-wide-buffer
  21126. (unless (org-at-heading-p)
  21127. (let* ((p (line-beginning-position))
  21128. (element (save-excursion
  21129. (beginning-of-line)
  21130. (org-element-at-point)))
  21131. (type (org-element-type element))
  21132. (post-affiliated (org-element-property :post-affiliated element)))
  21133. (unless (< p post-affiliated)
  21134. (case type
  21135. (comment
  21136. (save-excursion
  21137. (beginning-of-line)
  21138. (looking-at "[ \t]*")
  21139. (concat (match-string 0) "# ")))
  21140. (footnote-definition "")
  21141. ((item plain-list)
  21142. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21143. (paragraph
  21144. ;; Fill prefix is usually the same as the current line,
  21145. ;; unless the paragraph is at the beginning of an item.
  21146. (let ((parent (org-element-property :parent element)))
  21147. (save-excursion
  21148. (beginning-of-line)
  21149. (cond ((eq (org-element-type parent) 'item)
  21150. (make-string (org-list-item-body-column
  21151. (org-element-property :begin parent))
  21152. ?\s))
  21153. ((and adaptive-fill-regexp
  21154. ;; Locally disable
  21155. ;; `adaptive-fill-function' to let
  21156. ;; `fill-context-prefix' handle
  21157. ;; `adaptive-fill-regexp' variable.
  21158. (let (adaptive-fill-function)
  21159. (fill-context-prefix
  21160. post-affiliated
  21161. (org-element-property :end element)))))
  21162. ((looking-at "[ \t]+") (match-string 0))
  21163. (t "")))))
  21164. (comment-block
  21165. ;; Only fill contents if P is within block boundaries.
  21166. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21167. (forward-line)
  21168. (point)))
  21169. (cend (save-excursion
  21170. (goto-char (org-element-property :end element))
  21171. (skip-chars-backward " \r\t\n")
  21172. (line-beginning-position))))
  21173. (when (and (>= p cbeg) (< p cend))
  21174. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21175. (match-string 0)
  21176. "")))))))))))
  21177. (declare-function message-goto-body "message" ())
  21178. (defvar message-cite-prefix-regexp) ; From message.el
  21179. (defun org-fill-paragraph (&optional justify)
  21180. "Fill element at point, when applicable.
  21181. This function only applies to comment blocks, comments, example
  21182. blocks and paragraphs. Also, as a special case, re-align table
  21183. when point is at one.
  21184. If JUSTIFY is non-nil (interactively, with prefix argument),
  21185. justify as well. If `sentence-end-double-space' is non-nil, then
  21186. period followed by one space does not end a sentence, so don't
  21187. break a line there. The variable `fill-column' controls the
  21188. width for filling.
  21189. For convenience, when point is at a plain list, an item or
  21190. a footnote definition, try to fill the first paragraph within."
  21191. (interactive)
  21192. (if (and (derived-mode-p 'message-mode)
  21193. (or (not (message-in-body-p))
  21194. (save-excursion (move-beginning-of-line 1)
  21195. (looking-at message-cite-prefix-regexp))))
  21196. ;; First ensure filling is correct in message-mode.
  21197. (let ((fill-paragraph-function
  21198. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21199. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21200. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21201. (paragraph-separate
  21202. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21203. (fill-paragraph nil))
  21204. (with-syntax-table org-mode-transpose-word-syntax-table
  21205. ;; Move to end of line in order to get the first paragraph
  21206. ;; within a plain list or a footnote definition.
  21207. (let ((element (save-excursion
  21208. (end-of-line)
  21209. (or (ignore-errors (org-element-at-point))
  21210. (user-error "An element cannot be parsed line %d"
  21211. (line-number-at-pos (point)))))))
  21212. ;; First check if point is in a blank line at the beginning of
  21213. ;; the buffer. In that case, ignore filling.
  21214. (case (org-element-type element)
  21215. ;; Use major mode filling function is src blocks.
  21216. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21217. ;; Align Org tables, leave table.el tables as-is.
  21218. (table-row (org-table-align) t)
  21219. (table
  21220. (when (eq (org-element-property :type element) 'org)
  21221. (save-excursion
  21222. (goto-char (org-element-property :post-affiliated element))
  21223. (org-table-align)))
  21224. t)
  21225. (paragraph
  21226. ;; Paragraphs may contain `line-break' type objects.
  21227. (let ((beg (max (point-min)
  21228. (org-element-property :contents-begin element)))
  21229. (end (min (point-max)
  21230. (org-element-property :contents-end element))))
  21231. ;; Do nothing if point is at an affiliated keyword.
  21232. (if (< (line-end-position) beg) t
  21233. (when (derived-mode-p 'message-mode)
  21234. ;; In `message-mode', do not fill following citation
  21235. ;; in current paragraph nor text before message body.
  21236. (let ((body-start (save-excursion (message-goto-body))))
  21237. (when body-start (setq beg (max body-start beg))))
  21238. (when (save-excursion
  21239. (re-search-forward
  21240. (concat "^" message-cite-prefix-regexp) end t))
  21241. (setq end (match-beginning 0))))
  21242. ;; Fill paragraph, taking line breaks into account.
  21243. (save-excursion
  21244. (goto-char beg)
  21245. (let ((cuts (list beg)))
  21246. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21247. (when (eq 'line-break
  21248. (org-element-type
  21249. (save-excursion (backward-char)
  21250. (org-element-context))))
  21251. (push (point) cuts)))
  21252. (dolist (c (delq end cuts))
  21253. (fill-region-as-paragraph c end justify)
  21254. (setq end c))))
  21255. t)))
  21256. ;; Contents of `comment-block' type elements should be
  21257. ;; filled as plain text, but only if point is within block
  21258. ;; markers.
  21259. (comment-block
  21260. (let* ((case-fold-search t)
  21261. (beg (save-excursion
  21262. (goto-char (org-element-property :begin element))
  21263. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21264. (forward-line)
  21265. (point)))
  21266. (end (save-excursion
  21267. (goto-char (org-element-property :end element))
  21268. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21269. (line-beginning-position))))
  21270. (if (or (< (point) beg) (> (point) end)) t
  21271. (fill-region-as-paragraph
  21272. (save-excursion (end-of-line)
  21273. (re-search-backward "^[ \t]*$" beg 'move)
  21274. (line-beginning-position))
  21275. (save-excursion (beginning-of-line)
  21276. (re-search-forward "^[ \t]*$" end 'move)
  21277. (line-beginning-position))
  21278. justify))))
  21279. ;; Fill comments.
  21280. (comment
  21281. (let ((begin (org-element-property :post-affiliated element))
  21282. (end (org-element-property :end element)))
  21283. (when (and (>= (point) begin) (<= (point) end))
  21284. (let ((begin (save-excursion
  21285. (end-of-line)
  21286. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21287. (progn (forward-line) (point))
  21288. begin)))
  21289. (end (save-excursion
  21290. (end-of-line)
  21291. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21292. (1- (line-beginning-position))
  21293. (skip-chars-backward " \r\t\n")
  21294. (line-end-position)))))
  21295. ;; Do not fill comments when at a blank line.
  21296. (when (> end begin)
  21297. (let ((fill-prefix
  21298. (save-excursion
  21299. (beginning-of-line)
  21300. (looking-at "[ \t]*#")
  21301. (let ((comment-prefix (match-string 0)))
  21302. (goto-char (match-end 0))
  21303. (if (looking-at adaptive-fill-regexp)
  21304. (concat comment-prefix (match-string 0))
  21305. (concat comment-prefix " "))))))
  21306. (save-excursion
  21307. (fill-region-as-paragraph begin end justify))))))
  21308. t))
  21309. ;; Ignore every other element.
  21310. (otherwise t))))))
  21311. (defun org-auto-fill-function ()
  21312. "Auto-fill function."
  21313. ;; Check if auto-filling is meaningful.
  21314. (let ((fc (current-fill-column)))
  21315. (when (and fc (> (current-column) fc))
  21316. (let* ((fill-prefix (org-adaptive-fill-function))
  21317. ;; Enforce empty fill prefix, if required. Otherwise, it
  21318. ;; will be computed again.
  21319. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21320. (when fill-prefix (do-auto-fill))))))
  21321. (defun org-comment-line-break-function (&optional soft)
  21322. "Break line at point and indent, continuing comment if within one.
  21323. The inserted newline is marked hard if variable
  21324. `use-hard-newlines' is true, unless optional argument SOFT is
  21325. non-nil."
  21326. (if soft (insert-and-inherit ?\n) (newline 1))
  21327. (save-excursion (forward-char -1) (delete-horizontal-space))
  21328. (delete-horizontal-space)
  21329. (indent-to-left-margin)
  21330. (insert-before-markers-and-inherit fill-prefix))
  21331. ;;; Fixed Width Areas
  21332. (defun org-toggle-fixed-width ()
  21333. "Toggle fixed-width markup.
  21334. Add or remove fixed-width markup on current line, whenever it
  21335. makes sense. Return an error otherwise.
  21336. If a region is active and if it contains only fixed-width areas
  21337. or blank lines, remove all fixed-width markup in it. If the
  21338. region contains anything else, convert all non-fixed-width lines
  21339. to fixed-width ones.
  21340. Blank lines at the end of the region are ignored unless the
  21341. region only contains such lines."
  21342. (interactive)
  21343. (if (not (org-region-active-p))
  21344. ;; No region:
  21345. ;;
  21346. ;; Remove fixed width marker only in a fixed-with element.
  21347. ;;
  21348. ;; Add fixed width maker in paragraphs, in blank lines after
  21349. ;; elements or at the beginning of a headline or an inlinetask,
  21350. ;; and before any one-line elements (e.g., a clock).
  21351. (progn
  21352. (beginning-of-line)
  21353. (let* ((element (org-element-at-point))
  21354. (type (org-element-type element)))
  21355. (cond
  21356. ((and (eq type 'fixed-width)
  21357. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21358. (replace-match
  21359. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21360. ((and (memq type '(babel-call clock comment diary-sexp headline
  21361. horizontal-rule keyword paragraph
  21362. planning))
  21363. (<= (org-element-property :post-affiliated element) (point)))
  21364. (skip-chars-forward " \t")
  21365. (insert ": "))
  21366. ((and (org-looking-at-p "[ \t]*$")
  21367. (or (eq type 'inlinetask)
  21368. (save-excursion
  21369. (skip-chars-forward " \r\t\n")
  21370. (<= (org-element-property :end element) (point)))))
  21371. (delete-region (point) (line-end-position))
  21372. (org-indent-line)
  21373. (insert ": "))
  21374. (t (user-error "Cannot insert a fixed-width line here")))))
  21375. ;; Region active.
  21376. (let* ((begin (save-excursion
  21377. (goto-char (region-beginning))
  21378. (line-beginning-position)))
  21379. (end (copy-marker
  21380. (save-excursion
  21381. (goto-char (region-end))
  21382. (unless (eolp) (beginning-of-line))
  21383. (if (save-excursion (re-search-backward "\\S-" begin t))
  21384. (progn (skip-chars-backward " \r\t\n") (point))
  21385. (point)))))
  21386. (all-fixed-width-p
  21387. (catch 'not-all-p
  21388. (save-excursion
  21389. (goto-char begin)
  21390. (skip-chars-forward " \r\t\n")
  21391. (when (eobp) (throw 'not-all-p nil))
  21392. (while (< (point) end)
  21393. (let ((element (org-element-at-point)))
  21394. (if (eq (org-element-type element) 'fixed-width)
  21395. (goto-char (org-element-property :end element))
  21396. (throw 'not-all-p nil))))
  21397. t))))
  21398. (if all-fixed-width-p
  21399. (save-excursion
  21400. (goto-char begin)
  21401. (while (< (point) end)
  21402. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21403. (replace-match
  21404. "" nil nil nil
  21405. (if (= (line-end-position) (match-end 0)) 0 1)))
  21406. (forward-line)))
  21407. (let ((min-ind (point-max)))
  21408. ;; Find minimum indentation across all lines.
  21409. (save-excursion
  21410. (goto-char begin)
  21411. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21412. (setq min-ind 0)
  21413. (catch 'zerop
  21414. (while (< (point) end)
  21415. (unless (org-looking-at-p "[ \t]*$")
  21416. (let ((ind (org-get-indentation)))
  21417. (setq min-ind (min min-ind ind))
  21418. (when (zerop ind) (throw 'zerop t))))
  21419. (forward-line)))))
  21420. ;; Loop over all lines and add fixed-width markup everywhere
  21421. ;; but in fixed-width lines.
  21422. (save-excursion
  21423. (goto-char begin)
  21424. (while (< (point) end)
  21425. (cond
  21426. ((org-at-heading-p)
  21427. (insert ": ")
  21428. (forward-line)
  21429. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21430. (insert ":")
  21431. (forward-line)))
  21432. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21433. (let* ((element (org-element-at-point))
  21434. (element-end (org-element-property :end element)))
  21435. (if (eq (org-element-type element) 'fixed-width)
  21436. (progn (goto-char element-end)
  21437. (skip-chars-backward " \r\t\n")
  21438. (forward-line))
  21439. (let ((limit (min end element-end)))
  21440. (while (< (point) limit)
  21441. (org-move-to-column min-ind t)
  21442. (insert ": ")
  21443. (forward-line))))))
  21444. (t
  21445. (org-move-to-column min-ind t)
  21446. (insert ": ")
  21447. (forward-line)))))))
  21448. (set-marker end nil))))
  21449. ;;; Comments
  21450. ;; Org comments syntax is quite complex. It requires the entire line
  21451. ;; to be just a comment. Also, even with the right syntax at the
  21452. ;; beginning of line, some some elements (i.e. verse-block or
  21453. ;; example-block) don't accept comments. Usual Emacs comment commands
  21454. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21455. ;; Org still relies on `comment-dwim', but cannot trust
  21456. ;; `comment-only-p'. So, `comment-region-function' and
  21457. ;; `uncomment-region-function' both point
  21458. ;; to`org-comment-or-uncomment-region'. Eventually,
  21459. ;; `org-insert-comment' takes care of insertion of comments at the
  21460. ;; beginning of line.
  21461. ;; `org-setup-comments-handling' install comments related variables
  21462. ;; during `org-mode' initialization.
  21463. (defun org-setup-comments-handling ()
  21464. (interactive)
  21465. (org-set-local 'comment-use-syntax nil)
  21466. (org-set-local 'comment-start "# ")
  21467. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21468. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21469. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21470. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21471. (defun org-insert-comment ()
  21472. "Insert an empty comment above current line.
  21473. If the line is empty, insert comment at its beginning. When
  21474. point is within a source block, comment according to the related
  21475. major mode."
  21476. (if (let ((element (org-element-at-point)))
  21477. (and (eq (org-element-type element) 'src-block)
  21478. (< (save-excursion
  21479. (goto-char (org-element-property :post-affiliated element))
  21480. (line-end-position))
  21481. (point))
  21482. (> (save-excursion
  21483. (goto-char (org-element-property :end element))
  21484. (skip-chars-backward " \r\t\n")
  21485. (line-beginning-position))
  21486. (point))))
  21487. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21488. (beginning-of-line)
  21489. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21490. (open-line 1))
  21491. (org-indent-line)
  21492. (insert "# ")))
  21493. (defvar comment-empty-lines) ; From newcomment.el.
  21494. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21495. "Comment or uncomment each non-blank line in the region.
  21496. Uncomment each non-blank line between BEG and END if it only
  21497. contains commented lines. Otherwise, comment them. If region is
  21498. strictly within a source block, use appropriate comment syntax."
  21499. (if (let ((element (org-element-at-point)))
  21500. (and (eq (org-element-type element) 'src-block)
  21501. (< (save-excursion
  21502. (goto-char (org-element-property :post-affiliated element))
  21503. (line-end-position))
  21504. beg)
  21505. (>= (save-excursion
  21506. (goto-char (org-element-property :end element))
  21507. (skip-chars-backward " \r\t\n")
  21508. (line-beginning-position))
  21509. end)))
  21510. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21511. (save-restriction
  21512. ;; Restrict region
  21513. (narrow-to-region (save-excursion (goto-char beg)
  21514. (skip-chars-forward " \r\t\n" end)
  21515. (line-beginning-position))
  21516. (save-excursion (goto-char end)
  21517. (skip-chars-backward " \r\t\n" beg)
  21518. (line-end-position)))
  21519. (let ((uncommentp
  21520. ;; UNCOMMENTP is non-nil when every non blank line between
  21521. ;; BEG and END is a comment.
  21522. (save-excursion
  21523. (goto-char (point-min))
  21524. (while (and (not (eobp))
  21525. (let ((element (org-element-at-point)))
  21526. (and (eq (org-element-type element) 'comment)
  21527. (goto-char (min (point-max)
  21528. (org-element-property
  21529. :end element)))))))
  21530. (eobp))))
  21531. (if uncommentp
  21532. ;; Only blank lines and comments in region: uncomment it.
  21533. (save-excursion
  21534. (goto-char (point-min))
  21535. (while (not (eobp))
  21536. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21537. (replace-match "" nil nil nil 1))
  21538. (forward-line)))
  21539. ;; Comment each line in region.
  21540. (let ((min-indent (point-max)))
  21541. ;; First find the minimum indentation across all lines.
  21542. (save-excursion
  21543. (goto-char (point-min))
  21544. (while (and (not (eobp)) (not (zerop min-indent)))
  21545. (unless (looking-at "[ \t]*$")
  21546. (setq min-indent (min min-indent (current-indentation))))
  21547. (forward-line)))
  21548. ;; Then loop over all lines.
  21549. (save-excursion
  21550. (goto-char (point-min))
  21551. (while (not (eobp))
  21552. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21553. ;; Don't get fooled by invisible text (e.g. link path)
  21554. ;; when moving to column MIN-INDENT.
  21555. (let ((buffer-invisibility-spec nil))
  21556. (org-move-to-column min-indent t))
  21557. (insert comment-start))
  21558. (forward-line)))))))))
  21559. (defun org-comment-dwim (arg)
  21560. "Call `comment-dwim' within a source edit buffer if needed."
  21561. (interactive "*P")
  21562. (if (org-in-src-block-p)
  21563. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21564. (call-interactively 'comment-dwim)))
  21565. ;;; Timestamps API
  21566. ;; This section contains tools to operate on timestamp objects, as
  21567. ;; returned by, e.g. `org-element-context'.
  21568. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21569. "Encode TIMESTAMP object into Emacs internal time.
  21570. Use end of date range or time range when END is non-nil."
  21571. (apply #'encode-time
  21572. (cons 0
  21573. (mapcar
  21574. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21575. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21576. '(:minute-start :hour-start :day-start :month-start
  21577. :year-start))))))
  21578. (defun org-timestamp-has-time-p (timestamp)
  21579. "Non-nil when TIMESTAMP has a time specified."
  21580. (org-element-property :hour-start timestamp))
  21581. (defun org-timestamp-format (timestamp format &optional end utc)
  21582. "Format a TIMESTAMP object into a string.
  21583. FORMAT is a format specifier to be passed to
  21584. `format-time-string'.
  21585. When optional argument END is non-nil, use end of date-range or
  21586. time-range, if possible.
  21587. When optional argument UTC is non-nil, time will be expressed as
  21588. Universal Time."
  21589. (format-time-string
  21590. format (org-timestamp--to-internal-time timestamp end) utc))
  21591. (defun org-timestamp-split-range (timestamp &optional end)
  21592. "Extract a TIMESTAMP object from a date or time range.
  21593. END, when non-nil, means extract the end of the range.
  21594. Otherwise, extract its start.
  21595. Return a new timestamp object."
  21596. (let ((type (org-element-property :type timestamp)))
  21597. (if (memq type '(active inactive diary)) timestamp
  21598. (let ((split-ts (org-element-copy timestamp)))
  21599. ;; Set new type.
  21600. (org-element-put-property
  21601. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21602. ;; Copy start properties over end properties if END is
  21603. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21604. (let ((p-alist '((:minute-start . :minute-end)
  21605. (:hour-start . :hour-end)
  21606. (:day-start . :day-end)
  21607. (:month-start . :month-end)
  21608. (:year-start . :year-end))))
  21609. (dolist (p-cell p-alist)
  21610. (org-element-put-property
  21611. split-ts
  21612. (funcall (if end #'car #'cdr) p-cell)
  21613. (org-element-property
  21614. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21615. ;; Eventually refresh `:raw-value'.
  21616. (org-element-put-property split-ts :raw-value nil)
  21617. (org-element-put-property
  21618. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21619. (defun org-timestamp-translate (timestamp &optional boundary)
  21620. "Translate TIMESTAMP object to custom format.
  21621. Format string is defined in `org-time-stamp-custom-formats',
  21622. which see.
  21623. When optional argument BOUNDARY is non-nil, it is either the
  21624. symbol `start' or `end'. In this case, only translate the
  21625. starting or ending part of TIMESTAMP if it is a date or time
  21626. range. Otherwise, translate both parts.
  21627. Return timestamp as-is if `org-display-custom-times' is nil or if
  21628. it has a `diary' type."
  21629. (let ((type (org-element-property :type timestamp)))
  21630. (if (or (not org-display-custom-times) (eq type 'diary))
  21631. (org-element-interpret-data timestamp)
  21632. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21633. org-time-stamp-custom-formats)))
  21634. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21635. (concat (org-timestamp-format timestamp fmt)
  21636. "--"
  21637. (org-timestamp-format timestamp fmt t))
  21638. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21639. ;;; Other stuff.
  21640. (defun org-reftex-citation ()
  21641. "Use reftex-citation to insert a citation into the buffer.
  21642. This looks for a line like
  21643. #+BIBLIOGRAPHY: foo plain option:-d
  21644. and derives from it that foo.bib is the bibliography file relevant
  21645. for this document. It then installs the necessary environment for RefTeX
  21646. to work in this buffer and calls `reftex-citation' to insert a citation
  21647. into the buffer.
  21648. Export of such citations to both LaTeX and HTML is handled by the contributed
  21649. package ox-bibtex by Taru Karttunen."
  21650. (interactive)
  21651. (let ((reftex-docstruct-symbol 'rds)
  21652. (reftex-cite-format "\\cite{%l}")
  21653. rds bib)
  21654. (save-excursion
  21655. (save-restriction
  21656. (widen)
  21657. (let ((case-fold-search t)
  21658. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21659. (if (not (save-excursion
  21660. (or (re-search-forward re nil t)
  21661. (re-search-backward re nil t))))
  21662. (user-error "No bibliography defined in file")
  21663. (setq bib (concat (match-string 1) ".bib")
  21664. rds (list (list 'bib bib)))))))
  21665. (call-interactively 'reftex-citation)))
  21666. ;;;; Functions extending outline functionality
  21667. (defun org-beginning-of-line (&optional arg)
  21668. "Go to the beginning of the current line. If that is invisible, continue
  21669. to a visible line beginning. This makes the function of C-a more intuitive.
  21670. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21671. first attempt, and only move to after the tags when the cursor is already
  21672. beyond the end of the headline."
  21673. (interactive "P")
  21674. (let ((pos (point))
  21675. (special (if (consp org-special-ctrl-a/e)
  21676. (car org-special-ctrl-a/e)
  21677. org-special-ctrl-a/e))
  21678. deactivate-mark refpos)
  21679. (if (org-bound-and-true-p visual-line-mode)
  21680. (beginning-of-visual-line 1)
  21681. (beginning-of-line 1))
  21682. (if (and arg (fboundp 'move-beginning-of-line))
  21683. (call-interactively 'move-beginning-of-line)
  21684. (if (bobp)
  21685. nil
  21686. (backward-char 1)
  21687. (if (org-truely-invisible-p)
  21688. (while (and (not (bobp)) (org-truely-invisible-p))
  21689. (backward-char 1)
  21690. (beginning-of-line 1))
  21691. (forward-char 1))))
  21692. (when special
  21693. (cond
  21694. ((and (looking-at org-complex-heading-regexp)
  21695. (= (char-after (match-end 1)) ?\ ))
  21696. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21697. (point-at-eol)))
  21698. (goto-char
  21699. (if (eq special t)
  21700. (cond ((> pos refpos) refpos)
  21701. ((= pos (point)) refpos)
  21702. (t (point)))
  21703. (cond ((> pos (point)) (point))
  21704. ((not (eq last-command this-command)) (point))
  21705. (t refpos)))))
  21706. ((org-at-item-p)
  21707. ;; Being at an item and not looking at an the item means point
  21708. ;; was previously moved to beginning of a visual line, which
  21709. ;; doesn't contain the item. Therefore, do nothing special,
  21710. ;; just stay here.
  21711. (when (looking-at org-list-full-item-re)
  21712. ;; Set special position at first white space character after
  21713. ;; bullet, and check-box, if any.
  21714. (let ((after-bullet
  21715. (let ((box (match-end 3)))
  21716. (if (not box) (match-end 1)
  21717. (let ((after (char-after box)))
  21718. (if (and after (= after ? )) (1+ box) box))))))
  21719. ;; Special case: Move point to special position when
  21720. ;; currently after it or at beginning of line.
  21721. (if (eq special t)
  21722. (when (or (> pos after-bullet) (= (point) pos))
  21723. (goto-char after-bullet))
  21724. ;; Reversed case: Move point to special position when
  21725. ;; point was already at beginning of line and command is
  21726. ;; repeated.
  21727. (when (and (= (point) pos) (eq last-command this-command))
  21728. (goto-char after-bullet))))))))
  21729. (org-no-warnings
  21730. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21731. (setq disable-point-adjustment
  21732. (or (not (invisible-p (point)))
  21733. (not (invisible-p (max (point-min) (1- (point))))))))
  21734. (defun org-end-of-line (&optional arg)
  21735. "Go to the end of the line.
  21736. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21737. tags on the first attempt, and only move to after the tags when
  21738. the cursor is already beyond the end of the headline."
  21739. (interactive "P")
  21740. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21741. org-special-ctrl-a/e))
  21742. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21743. 'end-of-visual-line)
  21744. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21745. (t 'end-of-line)))
  21746. deactivate-mark)
  21747. (if (or (not special) arg) (call-interactively move-fun)
  21748. (let* ((element (save-excursion (beginning-of-line)
  21749. (org-element-at-point)))
  21750. (type (org-element-type element)))
  21751. (cond
  21752. ((memq type '(headline inlinetask))
  21753. (let ((pos (point)))
  21754. (beginning-of-line 1)
  21755. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21756. (if (eq special t)
  21757. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21758. (goto-char (match-beginning 1))
  21759. (goto-char (match-end 0)))
  21760. (if (or (< pos (match-end 0))
  21761. (not (eq this-command last-command)))
  21762. (goto-char (match-end 0))
  21763. (goto-char (match-beginning 1))))
  21764. (call-interactively move-fun))))
  21765. ((outline-invisible-p (line-end-position))
  21766. ;; If element is hidden, `move-end-of-line' would put point
  21767. ;; after it. Use `end-of-line' to stay on current line.
  21768. (call-interactively 'end-of-line))
  21769. (t (call-interactively move-fun)))))
  21770. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21771. (setq disable-point-adjustment
  21772. (or (not (invisible-p (point)))
  21773. (not (invisible-p (max (point-min) (1- (point))))))))
  21774. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21775. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21776. (defun org-backward-sentence (&optional arg)
  21777. "Go to beginning of sentence, or beginning of table field.
  21778. This will call `backward-sentence' or `org-table-beginning-of-field',
  21779. depending on context."
  21780. (interactive "P")
  21781. (cond
  21782. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21783. (t (call-interactively 'backward-sentence))))
  21784. (defun org-forward-sentence (&optional arg)
  21785. "Go to end of sentence, or end of table field.
  21786. This will call `forward-sentence' or `org-table-end-of-field',
  21787. depending on context."
  21788. (interactive "P")
  21789. (cond
  21790. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21791. (t (call-interactively 'forward-sentence))))
  21792. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21793. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21794. (defun org-kill-line (&optional arg)
  21795. "Kill line, to tags or end of line."
  21796. (interactive "P")
  21797. (cond
  21798. ((or (not org-special-ctrl-k)
  21799. (bolp)
  21800. (not (org-at-heading-p)))
  21801. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21802. org-ctrl-k-protect-subtree)
  21803. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21804. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21805. (user-error "C-k aborted as it would kill a hidden subtree")))
  21806. (call-interactively
  21807. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21808. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21809. (kill-region (point) (match-beginning 1))
  21810. (org-set-tags nil t))
  21811. (t (kill-region (point) (point-at-eol)))))
  21812. (define-key org-mode-map "\C-k" 'org-kill-line)
  21813. (defun org-yank (&optional arg)
  21814. "Yank. If the kill is a subtree, treat it specially.
  21815. This command will look at the current kill and check if is a single
  21816. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21817. cursor is at the beginning of a line or after the stars of a currently
  21818. empty headline, then the yank is handled specially. How exactly depends
  21819. on the value of the following variables, both set by default.
  21820. `org-yank-folded-subtrees'
  21821. When set, the subtree(s) will be folded after insertion, but only
  21822. if doing so would now swallow text after the yanked text.
  21823. `org-yank-adjusted-subtrees'
  21824. When set, the subtree will be promoted or demoted in order to
  21825. fit into the local outline tree structure, which means that the
  21826. level will be adjusted so that it becomes the smaller one of the
  21827. two *visible* surrounding headings.
  21828. Any prefix to this command will cause `yank' to be called directly with
  21829. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21830. will just
  21831. plainly yank the text as it is.
  21832. \[1] The test checks if the first non-white line is a heading
  21833. and if there are no other headings with fewer stars."
  21834. (interactive "P")
  21835. (org-yank-generic 'yank arg))
  21836. (defun org-yank-generic (command arg)
  21837. "Perform some yank-like command.
  21838. This function implements the behavior described in the `org-yank'
  21839. documentation. However, it has been generalized to work for any
  21840. interactive command with similar behavior."
  21841. ;; pretend to be command COMMAND
  21842. (setq this-command command)
  21843. (if arg
  21844. (call-interactively command)
  21845. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21846. (and (org-kill-is-subtree-p)
  21847. (or (bolp)
  21848. (and (looking-at "[ \t]*$")
  21849. (string-match
  21850. "\\`\\*+\\'"
  21851. (buffer-substring (point-at-bol) (point)))))))
  21852. swallowp)
  21853. (cond
  21854. ((and subtreep org-yank-folded-subtrees)
  21855. (let ((beg (point))
  21856. end)
  21857. (if (and subtreep org-yank-adjusted-subtrees)
  21858. (org-paste-subtree nil nil 'for-yank)
  21859. (call-interactively command))
  21860. (setq end (point))
  21861. (goto-char beg)
  21862. (when (and (bolp) subtreep
  21863. (not (setq swallowp
  21864. (org-yank-folding-would-swallow-text beg end))))
  21865. (org-with-limited-levels
  21866. (or (looking-at org-outline-regexp)
  21867. (re-search-forward org-outline-regexp-bol end t))
  21868. (while (and (< (point) end) (looking-at org-outline-regexp))
  21869. (hide-subtree)
  21870. (org-cycle-show-empty-lines 'folded)
  21871. (condition-case nil
  21872. (outline-forward-same-level 1)
  21873. (error (goto-char end))))))
  21874. (when swallowp
  21875. (message
  21876. "Inserted text not folded because that would swallow text"))
  21877. (goto-char end)
  21878. (skip-chars-forward " \t\n\r")
  21879. (beginning-of-line 1)
  21880. (push-mark beg 'nomsg)))
  21881. ((and subtreep org-yank-adjusted-subtrees)
  21882. (let ((beg (point-at-bol)))
  21883. (org-paste-subtree nil nil 'for-yank)
  21884. (push-mark beg 'nomsg)))
  21885. (t
  21886. (call-interactively command))))))
  21887. (defun org-yank-folding-would-swallow-text (beg end)
  21888. "Would hide-subtree at BEG swallow any text after END?"
  21889. (let (level)
  21890. (org-with-limited-levels
  21891. (save-excursion
  21892. (goto-char beg)
  21893. (when (or (looking-at org-outline-regexp)
  21894. (re-search-forward org-outline-regexp-bol end t))
  21895. (setq level (org-outline-level)))
  21896. (goto-char end)
  21897. (skip-chars-forward " \t\r\n\v\f")
  21898. (if (or (eobp)
  21899. (and (bolp) (looking-at org-outline-regexp)
  21900. (<= (org-outline-level) level)))
  21901. nil ; Nothing would be swallowed
  21902. t))))) ; something would swallow
  21903. (define-key org-mode-map "\C-y" 'org-yank)
  21904. (defun org-truely-invisible-p ()
  21905. "Check if point is at a character currently not visible.
  21906. This version does not only check the character property, but also
  21907. `visible-mode'."
  21908. ;; Early versions of noutline don't have `outline-invisible-p'.
  21909. (if (org-bound-and-true-p visible-mode)
  21910. nil
  21911. (outline-invisible-p)))
  21912. (defun org-invisible-p2 ()
  21913. "Check if point is at a character currently not visible."
  21914. (save-excursion
  21915. (if (and (eolp) (not (bobp))) (backward-char 1))
  21916. ;; Early versions of noutline don't have `outline-invisible-p'.
  21917. (outline-invisible-p)))
  21918. (defun org-back-to-heading (&optional invisible-ok)
  21919. "Call `outline-back-to-heading', but provide a better error message."
  21920. (condition-case nil
  21921. (outline-back-to-heading invisible-ok)
  21922. (error (error "Before first headline at position %d in buffer %s"
  21923. (point) (current-buffer)))))
  21924. (defun org-before-first-heading-p ()
  21925. "Before first heading?"
  21926. (save-excursion
  21927. (end-of-line)
  21928. (null (re-search-backward org-outline-regexp-bol nil t))))
  21929. (defun org-at-heading-p (&optional ignored)
  21930. (outline-on-heading-p t))
  21931. ;; Compatibility alias with Org versions < 7.8.03
  21932. (defalias 'org-on-heading-p 'org-at-heading-p)
  21933. (defun org-in-commented-heading-p (&optional no-inheritance)
  21934. "Non-nil if point is under a commented heading.
  21935. This function also checks ancestors of the current headline,
  21936. unless optional argument NO-INHERITANCE is non-nil."
  21937. (cond
  21938. ((org-before-first-heading-p) nil)
  21939. ((let ((headline (nth 4 (org-heading-components))))
  21940. (and headline
  21941. (let ((case-fold-search nil))
  21942. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21943. headline)))))
  21944. (no-inheritance nil)
  21945. (t
  21946. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21947. (defun org-at-comment-p nil
  21948. "Is cursor in a commented line?"
  21949. (save-excursion
  21950. (save-match-data
  21951. (beginning-of-line)
  21952. (looking-at "^[ \t]*# "))))
  21953. (defun org-at-drawer-p nil
  21954. "Is cursor at a drawer keyword?"
  21955. (save-excursion
  21956. (move-beginning-of-line 1)
  21957. (looking-at org-drawer-regexp)))
  21958. (defun org-at-block-p nil
  21959. "Is cursor at a block keyword?"
  21960. (save-excursion
  21961. (move-beginning-of-line 1)
  21962. (looking-at org-block-regexp)))
  21963. (defun org-point-at-end-of-empty-headline ()
  21964. "If point is at the end of an empty headline, return t, else nil.
  21965. If the heading only contains a TODO keyword, it is still still considered
  21966. empty."
  21967. (and (looking-at "[ \t]*$")
  21968. (when org-todo-line-regexp
  21969. (save-excursion
  21970. (beginning-of-line 1)
  21971. (let ((case-fold-search nil))
  21972. (looking-at org-todo-line-regexp)
  21973. (string= (match-string 3) ""))))))
  21974. (defun org-at-heading-or-item-p ()
  21975. (or (org-at-heading-p) (org-at-item-p)))
  21976. (defun org-at-target-p ()
  21977. (or (org-in-regexp org-radio-target-regexp)
  21978. (org-in-regexp org-target-regexp)))
  21979. ;; Compatibility alias with Org versions < 7.8.03
  21980. (defalias 'org-on-target-p 'org-at-target-p)
  21981. (defun org-up-heading-all (arg)
  21982. "Move to the heading line of which the present line is a subheading.
  21983. This function considers both visible and invisible heading lines.
  21984. With argument, move up ARG levels."
  21985. (if (fboundp 'outline-up-heading-all)
  21986. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21987. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21988. (defun org-up-heading-safe ()
  21989. "Move to the heading line of which the present line is a subheading.
  21990. This version will not throw an error. It will return the level of the
  21991. headline found, or nil if no higher level is found.
  21992. Also, this function will be a lot faster than `outline-up-heading',
  21993. because it relies on stars being the outline starters. This can really
  21994. make a significant difference in outlines with very many siblings."
  21995. (when (ignore-errors (org-back-to-heading t))
  21996. (let ((level-up (1- (funcall outline-level))))
  21997. (and (> level-up 0)
  21998. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21999. (funcall outline-level)))))
  22000. (defun org-first-sibling-p ()
  22001. "Is this heading the first child of its parents?"
  22002. (interactive)
  22003. (let ((re org-outline-regexp-bol)
  22004. level l)
  22005. (unless (org-at-heading-p t)
  22006. (user-error "Not at a heading"))
  22007. (setq level (funcall outline-level))
  22008. (save-excursion
  22009. (if (not (re-search-backward re nil t))
  22010. t
  22011. (setq l (funcall outline-level))
  22012. (< l level)))))
  22013. (defun org-goto-sibling (&optional previous)
  22014. "Goto the next sibling, even if it is invisible.
  22015. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22016. when a sibling was found. When none is found, return nil and don't
  22017. move point."
  22018. (let ((fun (if previous 're-search-backward 're-search-forward))
  22019. (pos (point))
  22020. (re org-outline-regexp-bol)
  22021. level l)
  22022. (when (ignore-errors (org-back-to-heading t))
  22023. (setq level (funcall outline-level))
  22024. (catch 'exit
  22025. (or previous (forward-char 1))
  22026. (while (funcall fun re nil t)
  22027. (setq l (funcall outline-level))
  22028. (when (< l level) (goto-char pos) (throw 'exit nil))
  22029. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22030. (goto-char pos)
  22031. nil))))
  22032. (defun org-show-siblings ()
  22033. "Show all siblings of the current headline."
  22034. (save-excursion
  22035. (while (org-goto-sibling) (org-flag-heading nil)))
  22036. (save-excursion
  22037. (while (org-goto-sibling 'previous)
  22038. (org-flag-heading nil))))
  22039. (defun org-goto-first-child ()
  22040. "Goto the first child, even if it is invisible.
  22041. Return t when a child was found. Otherwise don't move point and
  22042. return nil."
  22043. (let (level (pos (point)) (re org-outline-regexp-bol))
  22044. (when (ignore-errors (org-back-to-heading t))
  22045. (setq level (outline-level))
  22046. (forward-char 1)
  22047. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22048. (progn (goto-char (match-beginning 0)) t)
  22049. (goto-char pos) nil))))
  22050. (defun org-show-hidden-entry ()
  22051. "Show an entry where even the heading is hidden."
  22052. (save-excursion
  22053. (org-show-entry)))
  22054. (defun org-flag-heading (flag &optional entry)
  22055. "Flag the current heading. FLAG non-nil means make invisible.
  22056. When ENTRY is non-nil, show the entire entry."
  22057. (save-excursion
  22058. (org-back-to-heading t)
  22059. ;; Check if we should show the entire entry
  22060. (if entry
  22061. (progn
  22062. (org-show-entry)
  22063. (save-excursion
  22064. (and (outline-next-heading)
  22065. (org-flag-heading nil))))
  22066. (outline-flag-region (max (point-min) (1- (point)))
  22067. (save-excursion (outline-end-of-heading) (point))
  22068. flag))))
  22069. (defun org-get-next-sibling ()
  22070. "Move to next heading of the same level, and return point.
  22071. If there is no such heading, return nil.
  22072. This is like outline-next-sibling, but invisible headings are ok."
  22073. (let ((level (funcall outline-level)))
  22074. (outline-next-heading)
  22075. (while (and (not (eobp)) (> (funcall outline-level) level))
  22076. (outline-next-heading))
  22077. (if (or (eobp) (< (funcall outline-level) level))
  22078. nil
  22079. (point))))
  22080. (defun org-get-last-sibling ()
  22081. "Move to previous heading of the same level, and return point.
  22082. If there is no such heading, return nil."
  22083. (let ((opoint (point))
  22084. (level (funcall outline-level)))
  22085. (outline-previous-heading)
  22086. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22087. (while (and (> (funcall outline-level) level)
  22088. (not (bobp)))
  22089. (outline-previous-heading))
  22090. (if (< (funcall outline-level) level)
  22091. nil
  22092. (point)))))
  22093. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22094. "Goto to the end of a subtree."
  22095. ;; This contains an exact copy of the original function, but it uses
  22096. ;; `org-back-to-heading', to make it work also in invisible
  22097. ;; trees. And is uses an invisible-ok argument.
  22098. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22099. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22100. ;; with many children and grandchildren etc, this can be much faster
  22101. ;; than the outline version.
  22102. (org-back-to-heading invisible-ok)
  22103. (let ((first t)
  22104. (level (funcall outline-level)))
  22105. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22106. ;; A true heading (not a plain list item), in Org-mode
  22107. ;; This means we can easily find the end by looking
  22108. ;; only for the right number of stars. Using a regexp to do
  22109. ;; this is so much faster than using a Lisp loop.
  22110. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22111. (forward-char 1)
  22112. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22113. ;; something else, do it the slow way
  22114. (while (and (not (eobp))
  22115. (or first (> (funcall outline-level) level)))
  22116. (setq first nil)
  22117. (outline-next-heading)))
  22118. (unless to-heading
  22119. (if (memq (preceding-char) '(?\n ?\^M))
  22120. (progn
  22121. ;; Go to end of line before heading
  22122. (forward-char -1)
  22123. (if (memq (preceding-char) '(?\n ?\^M))
  22124. ;; leave blank line before heading
  22125. (forward-char -1))))))
  22126. (point))
  22127. (defun org-end-of-meta-data (&optional full)
  22128. "Skip planning line and properties drawer in current entry.
  22129. When optional argument FULL is non-nil, also skip empty lines,
  22130. clocking lines and regular drawers at the beginning of the
  22131. entry."
  22132. (org-back-to-heading t)
  22133. (forward-line)
  22134. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22135. (when (looking-at org-property-drawer-re)
  22136. (goto-char (match-end 0))
  22137. (forward-line))
  22138. (when (and full (not (org-at-heading-p)))
  22139. (catch 'exit
  22140. (let ((end (save-excursion (outline-next-heading) (point)))
  22141. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22142. (while (not (eobp))
  22143. (cond ((org-looking-at-p org-drawer-regexp)
  22144. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22145. (forward-line)
  22146. (throw 'exit t)))
  22147. ((org-looking-at-p re) (forward-line))
  22148. (t (throw 'exit t))))))))
  22149. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22150. "Move forward to the ARG'th subheading at same level as this one.
  22151. Stop at the first and last subheadings of a superior heading.
  22152. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22153. non-nil it will also look at invisible ones."
  22154. (interactive "p")
  22155. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22156. (if (and arg (< arg 0))
  22157. (goto-char (point-min))
  22158. (outline-next-heading))
  22159. (org-at-heading-p)
  22160. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22161. (f (if (and arg (< arg 0))
  22162. 're-search-backward
  22163. 're-search-forward))
  22164. (count (if arg (abs arg) 1))
  22165. (result (point)))
  22166. (while (and (prog1 (> count 0)
  22167. (forward-char (if (and arg (< arg 0)) -1 1)))
  22168. (funcall f org-outline-regexp-bol nil 'move))
  22169. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22170. (cond ((< l level) (setq count 0))
  22171. ((and (= l level)
  22172. (or invisible-ok
  22173. (progn
  22174. (goto-char (line-beginning-position))
  22175. (not (outline-invisible-p)))))
  22176. (setq count (1- count))
  22177. (when (eq l level)
  22178. (setq result (point)))))))
  22179. (goto-char result))
  22180. (beginning-of-line 1)))
  22181. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22182. "Move backward to the ARG'th subheading at same level as this one.
  22183. Stop at the first and last subheadings of a superior heading."
  22184. (interactive "p")
  22185. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22186. (defun org-next-visible-heading (arg)
  22187. "Move to the next visible heading.
  22188. This function wraps `outline-next-visible-heading' with
  22189. `org-with-limited-levels' in order to skip over inline tasks and
  22190. respect customization of `org-odd-levels-only'."
  22191. (interactive "p")
  22192. (org-with-limited-levels
  22193. (outline-next-visible-heading arg)))
  22194. (defun org-previous-visible-heading (arg)
  22195. "Move to the next visible heading.
  22196. This function wraps `outline-previous-visible-heading' with
  22197. `org-with-limited-levels' in order to skip over inline tasks and
  22198. respect customization of `org-odd-levels-only'."
  22199. (interactive "p")
  22200. (org-with-limited-levels
  22201. (outline-previous-visible-heading arg)))
  22202. (defun org-next-block (arg &optional backward block-regexp)
  22203. "Jump to the next block.
  22204. With a prefix argument ARG, jump forward ARG many source blocks.
  22205. When BACKWARD is non-nil, jump to the previous block.
  22206. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22207. (interactive "p")
  22208. (let ((re (or block-regexp org-block-regexp))
  22209. (re-search-fn (or (and backward 're-search-backward)
  22210. 're-search-forward)))
  22211. (if (looking-at re) (forward-char 1))
  22212. (condition-case nil
  22213. (funcall re-search-fn re nil nil arg)
  22214. (error (user-error "No %s code blocks"
  22215. (if backward "previous" "further" ))))
  22216. (goto-char (match-beginning 0)) (org-show-context)))
  22217. (defun org-previous-block (arg &optional block-regexp)
  22218. "Jump to the previous block.
  22219. With a prefix argument ARG, jump backward ARG many source blocks.
  22220. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22221. (interactive "p")
  22222. (org-next-block arg t block-regexp))
  22223. (defun org-forward-paragraph ()
  22224. "Move forward to beginning of next paragraph or equivalent.
  22225. The function moves point to the beginning of the next visible
  22226. structural element, which can be a paragraph, a table, a list
  22227. item, etc. It also provides some special moves for convenience:
  22228. - On an affiliated keyword, jump to the beginning of the
  22229. relative element.
  22230. - On an item or a footnote definition, move to the second
  22231. element inside, if any.
  22232. - On a table or a property drawer, jump after it.
  22233. - On a verse or source block, stop after blank lines."
  22234. (interactive)
  22235. (when (eobp) (user-error "Cannot move further down"))
  22236. (let* ((deactivate-mark nil)
  22237. (element (org-element-at-point))
  22238. (type (org-element-type element))
  22239. (post-affiliated (org-element-property :post-affiliated element))
  22240. (contents-begin (org-element-property :contents-begin element))
  22241. (contents-end (org-element-property :contents-end element))
  22242. (end (let ((end (org-element-property :end element)) (parent element))
  22243. (while (and (setq parent (org-element-property :parent parent))
  22244. (= (org-element-property :contents-end parent) end))
  22245. (setq end (org-element-property :end parent)))
  22246. end)))
  22247. (cond ((not element)
  22248. (skip-chars-forward " \r\t\n")
  22249. (or (eobp) (beginning-of-line)))
  22250. ;; On affiliated keywords, move to element's beginning.
  22251. ((< (point) post-affiliated)
  22252. (goto-char post-affiliated))
  22253. ;; At a table row, move to the end of the table. Similarly,
  22254. ;; at a node property, move to the end of the property
  22255. ;; drawer.
  22256. ((memq type '(node-property table-row))
  22257. (goto-char (org-element-property
  22258. :end (org-element-property :parent element))))
  22259. ((memq type '(property-drawer table)) (goto-char end))
  22260. ;; Consider blank lines as separators in verse and source
  22261. ;; blocks to ease editing.
  22262. ((memq type '(src-block verse-block))
  22263. (when (eq type 'src-block)
  22264. (setq contents-end
  22265. (save-excursion (goto-char end)
  22266. (skip-chars-backward " \r\t\n")
  22267. (line-beginning-position))))
  22268. (beginning-of-line)
  22269. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22270. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22271. (goto-char end)
  22272. (skip-chars-forward " \r\t\n")
  22273. (if (= (point) contents-end) (goto-char end)
  22274. (beginning-of-line))))
  22275. ;; With no contents, just skip element.
  22276. ((not contents-begin) (goto-char end))
  22277. ;; If contents are invisible, skip the element altogether.
  22278. ((outline-invisible-p (line-end-position))
  22279. (case type
  22280. (headline
  22281. (org-with-limited-levels (outline-next-visible-heading 1)))
  22282. ;; At a plain list, make sure we move to the next item
  22283. ;; instead of skipping the whole list.
  22284. (plain-list (forward-char)
  22285. (org-forward-paragraph))
  22286. (otherwise (goto-char end))))
  22287. ((>= (point) contents-end) (goto-char end))
  22288. ((>= (point) contents-begin)
  22289. ;; This can only happen on paragraphs and plain lists.
  22290. (case type
  22291. (paragraph (goto-char end))
  22292. ;; At a plain list, try to move to second element in
  22293. ;; first item, if possible.
  22294. (plain-list (end-of-line)
  22295. (org-forward-paragraph))))
  22296. ;; When contents start on the middle of a line (e.g. in
  22297. ;; items and footnote definitions), try to reach first
  22298. ;; element starting after current line.
  22299. ((> (line-end-position) contents-begin)
  22300. (end-of-line)
  22301. (org-forward-paragraph))
  22302. (t (goto-char contents-begin)))))
  22303. (defun org-backward-paragraph ()
  22304. "Move backward to start of previous paragraph or equivalent.
  22305. The function moves point to the beginning of the current
  22306. structural element, which can be a paragraph, a table, a list
  22307. item, etc., or to the beginning of the previous visible one if
  22308. point is already there. It also provides some special moves for
  22309. convenience:
  22310. - On an affiliated keyword, jump to the first one.
  22311. - On a table or a property drawer, move to its beginning.
  22312. - On a verse or source block, stop before blank lines."
  22313. (interactive)
  22314. (when (bobp) (user-error "Cannot move further up"))
  22315. (let* ((deactivate-mark nil)
  22316. (element (org-element-at-point))
  22317. (type (org-element-type element))
  22318. (contents-begin (org-element-property :contents-begin element))
  22319. (contents-end (org-element-property :contents-end element))
  22320. (post-affiliated (org-element-property :post-affiliated element))
  22321. (begin (org-element-property :begin element)))
  22322. (cond
  22323. ((not element) (goto-char (point-min)))
  22324. ((= (point) begin)
  22325. (backward-char)
  22326. (org-backward-paragraph))
  22327. ((<= (point) post-affiliated) (goto-char begin))
  22328. ((memq type '(node-property table-row))
  22329. (goto-char (org-element-property
  22330. :post-affiliated (org-element-property :parent element))))
  22331. ((memq type '(property-drawer table)) (goto-char begin))
  22332. ((memq type '(src-block verse-block))
  22333. (when (eq type 'src-block)
  22334. (setq contents-begin
  22335. (save-excursion (goto-char begin) (forward-line) (point))))
  22336. (if (= (point) contents-begin) (goto-char post-affiliated)
  22337. ;; Inside a verse block, see blank lines as paragraph
  22338. ;; separators.
  22339. (let ((origin (point)))
  22340. (skip-chars-backward " \r\t\n" contents-begin)
  22341. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22342. (skip-chars-forward " \r\t\n" origin)
  22343. (if (= (point) origin) (goto-char contents-begin)
  22344. (beginning-of-line))))))
  22345. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22346. ((eq type 'paragraph)
  22347. (goto-char contents-begin)
  22348. ;; When at first paragraph in an item or a footnote definition,
  22349. ;; move directly to beginning of line.
  22350. (let ((parent-contents
  22351. (org-element-property
  22352. :contents-begin (org-element-property :parent element))))
  22353. (when (and parent-contents (= parent-contents contents-begin))
  22354. (beginning-of-line))))
  22355. ;; At the end of a greater element, move to the beginning of the
  22356. ;; last element within.
  22357. ((>= (point) contents-end)
  22358. (goto-char (1- contents-end))
  22359. (org-backward-paragraph))
  22360. (t (goto-char (or post-affiliated begin))))
  22361. ;; Ensure we never leave point invisible.
  22362. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22363. (defun org-forward-element ()
  22364. "Move forward by one element.
  22365. Move to the next element at the same level, when possible."
  22366. (interactive)
  22367. (cond ((eobp) (user-error "Cannot move further down"))
  22368. ((org-with-limited-levels (org-at-heading-p))
  22369. (let ((origin (point)))
  22370. (goto-char (org-end-of-subtree nil t))
  22371. (unless (org-with-limited-levels (org-at-heading-p))
  22372. (goto-char origin)
  22373. (user-error "Cannot move further down"))))
  22374. (t
  22375. (let* ((elem (org-element-at-point))
  22376. (end (org-element-property :end elem))
  22377. (parent (org-element-property :parent elem)))
  22378. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22379. (goto-char (org-element-property :end parent)))
  22380. ((integer-or-marker-p end) (goto-char end))
  22381. (t (message "No element at point")))))))
  22382. (defun org-backward-element ()
  22383. "Move backward by one element.
  22384. Move to the previous element at the same level, when possible."
  22385. (interactive)
  22386. (cond ((bobp) (user-error "Cannot move further up"))
  22387. ((org-with-limited-levels (org-at-heading-p))
  22388. ;; At a headline, move to the previous one, if any, or stay
  22389. ;; here.
  22390. (let ((origin (point)))
  22391. (org-with-limited-levels (org-backward-heading-same-level 1))
  22392. ;; When current headline has no sibling above, move to its
  22393. ;; parent.
  22394. (when (= (point) origin)
  22395. (or (org-with-limited-levels (org-up-heading-safe))
  22396. (progn (goto-char origin)
  22397. (user-error "Cannot move further up"))))))
  22398. (t
  22399. (let* ((elem (org-element-at-point))
  22400. (beg (org-element-property :begin elem)))
  22401. (cond
  22402. ;; Move to beginning of current element if point isn't
  22403. ;; there already.
  22404. ((null beg) (message "No element at point"))
  22405. ((/= (point) beg) (goto-char beg))
  22406. (t (goto-char beg)
  22407. (skip-chars-backward " \r\t\n")
  22408. (unless (bobp)
  22409. (let ((prev (org-element-at-point)))
  22410. (goto-char (org-element-property :begin prev))
  22411. (while (and (setq prev (org-element-property :parent prev))
  22412. (<= (org-element-property :end prev) beg))
  22413. (goto-char (org-element-property :begin prev)))))))))))
  22414. (defun org-up-element ()
  22415. "Move to upper element."
  22416. (interactive)
  22417. (if (org-with-limited-levels (org-at-heading-p))
  22418. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22419. (let* ((elem (org-element-at-point))
  22420. (parent (org-element-property :parent elem)))
  22421. (if parent (goto-char (org-element-property :begin parent))
  22422. (if (org-with-limited-levels (org-before-first-heading-p))
  22423. (user-error "No surrounding element")
  22424. (org-with-limited-levels (org-back-to-heading)))))))
  22425. (defvar org-element-greater-elements)
  22426. (defun org-down-element ()
  22427. "Move to inner element."
  22428. (interactive)
  22429. (let ((element (org-element-at-point)))
  22430. (cond
  22431. ((memq (org-element-type element) '(plain-list table))
  22432. (goto-char (org-element-property :contents-begin element))
  22433. (forward-char))
  22434. ((memq (org-element-type element) org-element-greater-elements)
  22435. ;; If contents are hidden, first disclose them.
  22436. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22437. (goto-char (or (org-element-property :contents-begin element)
  22438. (user-error "No content for this element"))))
  22439. (t (user-error "No inner element")))))
  22440. (defun org-drag-element-backward ()
  22441. "Move backward element at point."
  22442. (interactive)
  22443. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22444. (let* ((elem (org-element-at-point))
  22445. (prev-elem
  22446. (save-excursion
  22447. (goto-char (org-element-property :begin elem))
  22448. (skip-chars-backward " \r\t\n")
  22449. (unless (bobp)
  22450. (let* ((beg (org-element-property :begin elem))
  22451. (prev (org-element-at-point))
  22452. (up prev))
  22453. (while (and (setq up (org-element-property :parent up))
  22454. (<= (org-element-property :end up) beg))
  22455. (setq prev up))
  22456. prev)))))
  22457. ;; Error out if no previous element or previous element is
  22458. ;; a parent of the current one.
  22459. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22460. (user-error "Cannot drag element backward")
  22461. (let ((pos (point)))
  22462. (org-element-swap-A-B prev-elem elem)
  22463. (goto-char (+ (org-element-property :begin prev-elem)
  22464. (- pos (org-element-property :begin elem)))))))))
  22465. (defun org-drag-element-forward ()
  22466. "Move forward element at point."
  22467. (interactive)
  22468. (let* ((pos (point))
  22469. (elem (org-element-at-point)))
  22470. (when (= (point-max) (org-element-property :end elem))
  22471. (user-error "Cannot drag element forward"))
  22472. (goto-char (org-element-property :end elem))
  22473. (let ((next-elem (org-element-at-point)))
  22474. (when (or (org-element-nested-p elem next-elem)
  22475. (and (eq (org-element-type next-elem) 'headline)
  22476. (not (eq (org-element-type elem) 'headline))))
  22477. (goto-char pos)
  22478. (user-error "Cannot drag element forward"))
  22479. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22480. ;; body's length (without final blanks) and by the length of
  22481. ;; blanks between ELEM and NEXT-ELEM.
  22482. (let ((size-next (- (save-excursion
  22483. (goto-char (org-element-property :end next-elem))
  22484. (skip-chars-backward " \r\t\n")
  22485. (forward-line)
  22486. ;; Small correction if buffer doesn't end
  22487. ;; with a newline character.
  22488. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22489. (org-element-property :begin next-elem)))
  22490. (size-blank (- (org-element-property :end elem)
  22491. (save-excursion
  22492. (goto-char (org-element-property :end elem))
  22493. (skip-chars-backward " \r\t\n")
  22494. (forward-line)
  22495. (point)))))
  22496. (org-element-swap-A-B elem next-elem)
  22497. (goto-char (+ pos size-next size-blank))))))
  22498. (defun org-drag-line-forward (arg)
  22499. "Drag the line at point ARG lines forward."
  22500. (interactive "p")
  22501. (dotimes (n (abs arg))
  22502. (let ((c (current-column)))
  22503. (if (< 0 arg)
  22504. (progn
  22505. (beginning-of-line 2)
  22506. (transpose-lines 1)
  22507. (beginning-of-line 0))
  22508. (transpose-lines 1)
  22509. (beginning-of-line -1))
  22510. (org-move-to-column c))))
  22511. (defun org-drag-line-backward (arg)
  22512. "Drag the line at point ARG lines backward."
  22513. (interactive "p")
  22514. (org-drag-line-forward (- arg)))
  22515. (defun org-mark-element ()
  22516. "Put point at beginning of this element, mark at end.
  22517. Interactively, if this command is repeated or (in Transient Mark
  22518. mode) if the mark is active, it marks the next element after the
  22519. ones already marked."
  22520. (interactive)
  22521. (let (deactivate-mark)
  22522. (if (and (org-called-interactively-p 'any)
  22523. (or (and (eq last-command this-command) (mark t))
  22524. (and transient-mark-mode mark-active)))
  22525. (set-mark
  22526. (save-excursion
  22527. (goto-char (mark))
  22528. (goto-char (org-element-property :end (org-element-at-point)))))
  22529. (let ((element (org-element-at-point)))
  22530. (end-of-line)
  22531. (push-mark (org-element-property :end element) t t)
  22532. (goto-char (org-element-property :begin element))))))
  22533. (defun org-narrow-to-element ()
  22534. "Narrow buffer to current element."
  22535. (interactive)
  22536. (let ((elem (org-element-at-point)))
  22537. (cond
  22538. ((eq (car elem) 'headline)
  22539. (narrow-to-region
  22540. (org-element-property :begin elem)
  22541. (org-element-property :end elem)))
  22542. ((memq (car elem) org-element-greater-elements)
  22543. (narrow-to-region
  22544. (org-element-property :contents-begin elem)
  22545. (org-element-property :contents-end elem)))
  22546. (t
  22547. (narrow-to-region
  22548. (org-element-property :begin elem)
  22549. (org-element-property :end elem))))))
  22550. (defun org-transpose-element ()
  22551. "Transpose current and previous elements, keeping blank lines between.
  22552. Point is moved after both elements."
  22553. (interactive)
  22554. (org-skip-whitespace)
  22555. (let ((end (org-element-property :end (org-element-at-point))))
  22556. (org-drag-element-backward)
  22557. (goto-char end)))
  22558. (defun org-unindent-buffer ()
  22559. "Un-indent the visible part of the buffer.
  22560. Relative indentation (between items, inside blocks, etc.) isn't
  22561. modified."
  22562. (interactive)
  22563. (unless (eq major-mode 'org-mode)
  22564. (user-error "Cannot un-indent a buffer not in Org mode"))
  22565. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22566. unindent-tree ; For byte-compiler.
  22567. (unindent-tree
  22568. (function
  22569. (lambda (contents)
  22570. (mapc
  22571. (lambda (element)
  22572. (if (memq (org-element-type element) '(headline section))
  22573. (funcall unindent-tree (org-element-contents element))
  22574. (save-excursion
  22575. (save-restriction
  22576. (narrow-to-region
  22577. (org-element-property :begin element)
  22578. (org-element-property :end element))
  22579. (org-do-remove-indentation)))))
  22580. (reverse contents))))))
  22581. (funcall unindent-tree (org-element-contents parse-tree))))
  22582. (defun org-show-subtree ()
  22583. "Show everything after this heading at deeper levels."
  22584. (interactive)
  22585. (outline-flag-region
  22586. (point)
  22587. (save-excursion
  22588. (org-end-of-subtree t t))
  22589. nil))
  22590. (defun org-show-entry ()
  22591. "Show the body directly following this heading.
  22592. Show the heading too, if it is currently invisible."
  22593. (interactive)
  22594. (save-excursion
  22595. (ignore-errors
  22596. (org-back-to-heading t)
  22597. (outline-flag-region
  22598. (max (point-min) (1- (point)))
  22599. (save-excursion
  22600. (if (re-search-forward
  22601. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22602. (match-beginning 1)
  22603. (point-max)))
  22604. nil)
  22605. (org-cycle-hide-drawers 'children))))
  22606. (defun org-make-options-regexp (kwds &optional extra)
  22607. "Make a regular expression for keyword lines.
  22608. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22609. when non-nil, is a regexp matching keywords names."
  22610. (concat "^[ \t]*#\\+\\("
  22611. (regexp-opt kwds)
  22612. (and extra (concat (and kwds "\\|") extra))
  22613. "\\):[ \t]*\\(.*\\)"))
  22614. ;; Make isearch reveal the necessary context
  22615. (defun org-isearch-end ()
  22616. "Reveal context after isearch exits."
  22617. (when isearch-success ; only if search was successful
  22618. (if (featurep 'xemacs)
  22619. ;; Under XEmacs, the hook is run in the correct place,
  22620. ;; we directly show the context.
  22621. (org-show-context 'isearch)
  22622. ;; In Emacs the hook runs *before* restoring the overlays.
  22623. ;; So we have to use a one-time post-command-hook to do this.
  22624. ;; (Emacs 22 has a special variable, see function `org-mode')
  22625. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22626. isearch-mode-end-hook-quit)
  22627. ;; Only when the isearch was not quitted.
  22628. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22629. 'append 'local)))))
  22630. (defun org-isearch-post-command ()
  22631. "Remove self from hook, and show context."
  22632. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22633. (org-show-context 'isearch))
  22634. ;;;; Integration with and fixes for other packages
  22635. ;;; Imenu support
  22636. (defvar org-imenu-markers nil
  22637. "All markers currently used by Imenu.")
  22638. (make-variable-buffer-local 'org-imenu-markers)
  22639. (defun org-imenu-new-marker (&optional pos)
  22640. "Return a new marker for use by Imenu, and remember the marker."
  22641. (let ((m (make-marker)))
  22642. (move-marker m (or pos (point)))
  22643. (push m org-imenu-markers)
  22644. m))
  22645. (defun org-imenu-get-tree ()
  22646. "Produce the index for Imenu."
  22647. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22648. (setq org-imenu-markers nil)
  22649. (let* ((n org-imenu-depth)
  22650. (re (concat "^" (org-get-limited-outline-regexp)))
  22651. (subs (make-vector (1+ n) nil))
  22652. (last-level 0)
  22653. m level head0 head)
  22654. (save-excursion
  22655. (save-restriction
  22656. (widen)
  22657. (goto-char (point-max))
  22658. (while (re-search-backward re nil t)
  22659. (setq level (org-reduced-level (funcall outline-level)))
  22660. (when (and (<= level n)
  22661. (looking-at org-complex-heading-regexp)
  22662. (setq head0 (org-match-string-no-properties 4)))
  22663. (setq head (org-link-display-format head0)
  22664. m (org-imenu-new-marker))
  22665. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22666. (if (>= level last-level)
  22667. (push (cons head m) (aref subs level))
  22668. (push (cons head (aref subs (1+ level))) (aref subs level))
  22669. (loop for i from (1+ level) to n do (aset subs i nil)))
  22670. (setq last-level level)))))
  22671. (aref subs 1)))
  22672. (eval-after-load "imenu"
  22673. '(progn
  22674. (add-hook 'imenu-after-jump-hook
  22675. (lambda ()
  22676. (if (derived-mode-p 'org-mode)
  22677. (org-show-context 'org-goto))))))
  22678. (defun org-link-display-format (link)
  22679. "Replace a link with its the description.
  22680. If there is no description, use the link target."
  22681. (save-match-data
  22682. (if (string-match org-bracket-link-analytic-regexp link)
  22683. (replace-match (if (match-end 5)
  22684. (match-string 5 link)
  22685. (concat (match-string 1 link)
  22686. (match-string 3 link)))
  22687. nil t link)
  22688. link)))
  22689. (defun org-toggle-link-display ()
  22690. "Toggle the literal or descriptive display of links."
  22691. (interactive)
  22692. (if org-descriptive-links
  22693. (progn (org-remove-from-invisibility-spec '(org-link))
  22694. (org-restart-font-lock)
  22695. (setq org-descriptive-links nil))
  22696. (progn (add-to-invisibility-spec '(org-link))
  22697. (org-restart-font-lock)
  22698. (setq org-descriptive-links t))))
  22699. ;; Speedbar support
  22700. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22701. "Overlay marking the agenda restriction line in speedbar.")
  22702. (overlay-put org-speedbar-restriction-lock-overlay
  22703. 'face 'org-agenda-restriction-lock)
  22704. (overlay-put org-speedbar-restriction-lock-overlay
  22705. 'help-echo "Agendas are currently limited to this item.")
  22706. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22707. (defun org-speedbar-set-agenda-restriction ()
  22708. "Restrict future agenda commands to the location at point in speedbar.
  22709. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22710. (interactive)
  22711. (require 'org-agenda)
  22712. (let (p m tp np dir txt)
  22713. (cond
  22714. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22715. 'org-imenu t))
  22716. (setq m (get-text-property p 'org-imenu-marker))
  22717. (with-current-buffer (marker-buffer m)
  22718. (goto-char m)
  22719. (org-agenda-set-restriction-lock 'subtree)))
  22720. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22721. 'speedbar-function 'speedbar-find-file))
  22722. (setq tp (previous-single-property-change
  22723. (1+ p) 'speedbar-function)
  22724. np (next-single-property-change
  22725. tp 'speedbar-function)
  22726. dir (speedbar-line-directory)
  22727. txt (buffer-substring-no-properties (or tp (point-min))
  22728. (or np (point-max))))
  22729. (with-current-buffer (find-file-noselect
  22730. (let ((default-directory dir))
  22731. (expand-file-name txt)))
  22732. (unless (derived-mode-p 'org-mode)
  22733. (user-error "Cannot restrict to non-Org-mode file"))
  22734. (org-agenda-set-restriction-lock 'file)))
  22735. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22736. (move-overlay org-speedbar-restriction-lock-overlay
  22737. (point-at-bol) (point-at-eol))
  22738. (setq current-prefix-arg nil)
  22739. (org-agenda-maybe-redo)))
  22740. (defvar speedbar-file-key-map)
  22741. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22742. (eval-after-load "speedbar"
  22743. '(progn
  22744. (speedbar-add-supported-extension ".org")
  22745. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22746. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22747. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22748. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22749. (add-hook 'speedbar-visiting-tag-hook
  22750. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22751. ;;; Fixes and Hacks for problems with other packages
  22752. (defun org--flyspell-object-check-p (element)
  22753. "Non-nil when Flyspell can check object at point.
  22754. ELEMENT is the element at point."
  22755. (let ((object (save-excursion
  22756. (when (org-looking-at-p "\\>") (backward-char))
  22757. (org-element-context element))))
  22758. (case (org-element-type object)
  22759. ;; Prevent checks in links due to keybinding conflict with
  22760. ;; Flyspell.
  22761. ((code entity export-snippet inline-babel-call
  22762. inline-src-block line-break latex-fragment link macro
  22763. statistics-cookie target timestamp verbatim)
  22764. nil)
  22765. (footnote-reference
  22766. ;; Only in inline footnotes, within the definition.
  22767. (and (eq (org-element-property :type object) 'inline)
  22768. (< (save-excursion
  22769. (goto-char (org-element-property :begin object))
  22770. (search-forward ":" nil t 2))
  22771. (point))))
  22772. (otherwise t))))
  22773. (defun org-mode-flyspell-verify ()
  22774. "Function used for `flyspell-generic-check-word-predicate'."
  22775. (if (org-at-heading-p)
  22776. ;; At a headline or an inlinetask, check title only. This is
  22777. ;; faster than relying on `org-element-at-point'.
  22778. (and (save-excursion (beginning-of-line)
  22779. (and (let ((case-fold-search t))
  22780. (not (looking-at "\\*+ END[ \t]*$")))
  22781. (looking-at org-complex-heading-regexp)))
  22782. (match-beginning 4)
  22783. (>= (point) (match-beginning 4))
  22784. (or (not (match-beginning 5))
  22785. (< (point) (match-beginning 5))))
  22786. (let* ((element (org-element-at-point))
  22787. (post-affiliated (org-element-property :post-affiliated element)))
  22788. (cond
  22789. ;; Ignore checks in all affiliated keywords but captions.
  22790. ((< (point) post-affiliated)
  22791. (and (save-excursion
  22792. (beginning-of-line)
  22793. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22794. (> (point) (match-end 0))
  22795. (org--flyspell-object-check-p element)))
  22796. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22797. ((let ((log (org-log-into-drawer)))
  22798. (and log
  22799. (let ((drawer (org-element-lineage element '(drawer))))
  22800. (and drawer
  22801. (eq (compare-strings
  22802. log nil nil
  22803. (org-element-property :drawer-name drawer) nil nil t)
  22804. t)))))
  22805. nil)
  22806. (t
  22807. (case (org-element-type element)
  22808. ((comment quote-section) t)
  22809. (comment-block
  22810. ;; Allow checks between block markers, not on them.
  22811. (and (> (line-beginning-position) post-affiliated)
  22812. (save-excursion
  22813. (end-of-line)
  22814. (skip-chars-forward " \r\t\n")
  22815. (< (point) (org-element-property :end element)))))
  22816. ;; Arbitrary list of keywords where checks are meaningful.
  22817. ;; Make sure point is on the value part of the element.
  22818. (keyword
  22819. (and (member (org-element-property :key element)
  22820. '("DESCRIPTION" "TITLE"))
  22821. (save-excursion
  22822. (search-backward ":" (line-beginning-position) t))))
  22823. ;; Check is globally allowed in paragraphs verse blocks and
  22824. ;; table rows (after affiliated keywords) but some objects
  22825. ;; must not be affected.
  22826. ((paragraph table-row verse-block)
  22827. (let ((cbeg (org-element-property :contents-begin element))
  22828. (cend (org-element-property :contents-end element)))
  22829. (and cbeg (>= (point) cbeg) (< (point) cend)
  22830. (org--flyspell-object-check-p element))))))))))
  22831. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22832. (defun org-remove-flyspell-overlays-in (beg end)
  22833. "Remove flyspell overlays in region."
  22834. (and (org-bound-and-true-p flyspell-mode)
  22835. (fboundp 'flyspell-delete-region-overlays)
  22836. (flyspell-delete-region-overlays beg end)))
  22837. (defvar flyspell-delayed-commands)
  22838. (eval-after-load "flyspell"
  22839. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22840. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22841. (eval-after-load "bookmark"
  22842. '(if (boundp 'bookmark-after-jump-hook)
  22843. ;; We can use the hook
  22844. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22845. ;; Hook not available, use advice
  22846. (defadvice bookmark-jump (after org-make-visible activate)
  22847. "Make the position visible."
  22848. (org-bookmark-jump-unhide))))
  22849. ;; Make sure saveplace shows the location if it was hidden
  22850. (eval-after-load "saveplace"
  22851. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22852. "Make the position visible."
  22853. (org-bookmark-jump-unhide)))
  22854. ;; Make sure ecb shows the location if it was hidden
  22855. (eval-after-load "ecb"
  22856. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22857. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22858. (if (derived-mode-p 'org-mode)
  22859. (org-show-context))))
  22860. (defun org-bookmark-jump-unhide ()
  22861. "Unhide the current position, to show the bookmark location."
  22862. (and (derived-mode-p 'org-mode)
  22863. (or (outline-invisible-p)
  22864. (save-excursion (goto-char (max (point-min) (1- (point))))
  22865. (outline-invisible-p)))
  22866. (org-show-context 'bookmark-jump)))
  22867. (defun org-mark-jump-unhide ()
  22868. "Make the point visible with `org-show-context' after jumping to the mark."
  22869. (when (and (derived-mode-p 'org-mode)
  22870. (outline-invisible-p))
  22871. (org-show-context 'mark-goto)))
  22872. (eval-after-load "simple"
  22873. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22874. "Make the point visible with `org-show-context'."
  22875. (org-mark-jump-unhide)))
  22876. (eval-after-load "simple"
  22877. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22878. "Make the point visible with `org-show-context'."
  22879. (org-mark-jump-unhide)))
  22880. (eval-after-load "simple"
  22881. '(defadvice pop-global-mark (after org-make-visible activate)
  22882. "Make the point visible with `org-show-context'."
  22883. (org-mark-jump-unhide)))
  22884. ;; Make session.el ignore our circular variable
  22885. (defvar session-globals-exclude)
  22886. (eval-after-load "session"
  22887. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22888. ;;;; Finish up
  22889. (provide 'org)
  22890. (run-hooks 'org-load-hook)
  22891. ;;; org.el ends here