org.el 891 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu 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. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-agenda-redo "org-agenda" (&optional all))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. ;;;###autoload
  163. (defun org-babel-load-file (file &optional compile)
  164. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  165. This function exports the source code using `org-babel-tangle'
  166. and then loads the resulting file using `load-file'. With prefix
  167. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  168. file to byte-code before it is loaded."
  169. (interactive "fFile to load: \nP")
  170. (require 'ob-core)
  171. (let* ((age (lambda (file)
  172. (float-time
  173. (time-subtract (current-time)
  174. (nth 5 (or (file-attributes (file-truename file))
  175. (file-attributes file)))))))
  176. (base-name (file-name-sans-extension file))
  177. (exported-file (concat base-name ".el")))
  178. ;; tangle if the org-mode file is newer than the elisp file
  179. (unless (and (file-exists-p exported-file)
  180. (> (funcall age file) (funcall age exported-file)))
  181. (org-babel-tangle-file file exported-file "emacs-lisp"))
  182. (message "%s %s"
  183. (if compile
  184. (progn (byte-compile-file exported-file 'load)
  185. "Compiled and loaded")
  186. (progn (load-file exported-file) "Loaded"))
  187. exported-file)))
  188. (defcustom org-babel-load-languages '((emacs-lisp . t))
  189. "Languages which can be evaluated in Org-mode buffers.
  190. This list can be used to load support for any of the languages
  191. below, note that each language will depend on a different set of
  192. system executables and/or Emacs modes. When a language is
  193. \"loaded\", then code blocks in that language can be evaluated
  194. with `org-babel-execute-src-block' bound by default to C-c
  195. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  196. be set to remove code block evaluation from the C-c C-c
  197. keybinding. By default only Emacs Lisp (which has no
  198. requirements) is loaded."
  199. :group 'org-babel
  200. :set 'org-babel-do-load-languages
  201. :version "24.1"
  202. :type '(alist :tag "Babel Languages"
  203. :key-type
  204. (choice
  205. (const :tag "Awk" awk)
  206. (const :tag "C" C)
  207. (const :tag "R" R)
  208. (const :tag "Asymptote" asymptote)
  209. (const :tag "Calc" calc)
  210. (const :tag "Clojure" clojure)
  211. (const :tag "CSS" css)
  212. (const :tag "Ditaa" ditaa)
  213. (const :tag "Dot" dot)
  214. (const :tag "Emacs Lisp" emacs-lisp)
  215. (const :tag "Fortran" fortran)
  216. (const :tag "Gnuplot" gnuplot)
  217. (const :tag "Haskell" haskell)
  218. (const :tag "IO" io)
  219. (const :tag "Java" java)
  220. (const :tag "Javascript" js)
  221. (const :tag "LaTeX" latex)
  222. (const :tag "Ledger" ledger)
  223. (const :tag "Lilypond" lilypond)
  224. (const :tag "Lisp" lisp)
  225. (const :tag "Makefile" makefile)
  226. (const :tag "Maxima" maxima)
  227. (const :tag "Matlab" matlab)
  228. (const :tag "Mscgen" mscgen)
  229. (const :tag "Ocaml" ocaml)
  230. (const :tag "Octave" octave)
  231. (const :tag "Org" org)
  232. (const :tag "Perl" perl)
  233. (const :tag "Pico Lisp" picolisp)
  234. (const :tag "PlantUML" plantuml)
  235. (const :tag "Python" python)
  236. (const :tag "Ruby" ruby)
  237. (const :tag "Sass" sass)
  238. (const :tag "Scala" scala)
  239. (const :tag "Scheme" scheme)
  240. (const :tag "Screen" screen)
  241. (const :tag "Shell Script" sh)
  242. (const :tag "Shen" shen)
  243. (const :tag "Sql" sql)
  244. (const :tag "Sqlite" sqlite))
  245. :value-type (boolean :tag "Activate" :value t)))
  246. ;;;; Customization variables
  247. (defcustom org-clone-delete-id nil
  248. "Remove ID property of clones of a subtree.
  249. When non-nil, clones of a subtree don't inherit the ID property.
  250. Otherwise they inherit the ID property with a new unique
  251. identifier."
  252. :type 'boolean
  253. :version "24.1"
  254. :group 'org-id)
  255. ;;; Version
  256. (org-check-version)
  257. ;;;###autoload
  258. (defun org-version (&optional here full message)
  259. "Show the org-mode version in the echo area.
  260. With prefix argument HERE, insert it at point.
  261. When FULL is non-nil, use a verbose version string.
  262. When MESSAGE is non-nil, display a message with the version."
  263. (interactive "P")
  264. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  265. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  266. (load-suffixes (list ".el"))
  267. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  268. (org-trash (or
  269. (and (fboundp 'org-release) (fboundp 'org-git-version))
  270. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  271. (load-suffixes save-load-suffixes)
  272. (org-version (org-release))
  273. (git-version (org-git-version))
  274. (version (format "Org-mode version %s (%s @ %s)"
  275. org-version
  276. git-version
  277. (if org-install-dir
  278. (if (string= org-dir org-install-dir)
  279. org-install-dir
  280. (concat "mixed installation! " org-install-dir " and " org-dir))
  281. "org-loaddefs.el can not be found!")))
  282. (_version (if full version org-version)))
  283. (if (org-called-interactively-p 'interactive)
  284. (if here
  285. (insert version)
  286. (message version))
  287. (if message (message _version))
  288. _version)))
  289. (defconst org-version (org-version))
  290. ;;; Compatibility constants
  291. ;;; The custom variables
  292. (defgroup org nil
  293. "Outline-based notes management and organizer."
  294. :tag "Org"
  295. :group 'outlines
  296. :group 'calendar)
  297. (defcustom org-mode-hook nil
  298. "Mode hook for Org-mode, run after the mode was turned on."
  299. :group 'org
  300. :type 'hook)
  301. (defcustom org-load-hook nil
  302. "Hook that is run after org.el has been loaded."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-log-buffer-setup-hook nil
  306. "Hook that is run after an Org log buffer is created."
  307. :group 'org
  308. :version "24.1"
  309. :type 'hook)
  310. (defvar org-modules) ; defined below
  311. (defvar org-modules-loaded nil
  312. "Have the modules been loaded already?")
  313. (defun org-load-modules-maybe (&optional force)
  314. "Load all extensions listed in `org-modules'."
  315. (when (or force (not org-modules-loaded))
  316. (mapc (lambda (ext)
  317. (condition-case nil (require ext)
  318. (error (message "Problems while trying to load feature `%s'" ext))))
  319. org-modules)
  320. (setq org-modules-loaded t)))
  321. (defun org-set-modules (var value)
  322. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  323. (set var value)
  324. (when (featurep 'org)
  325. (org-load-modules-maybe 'force)))
  326. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  327. "Modules that should always be loaded together with org.el.
  328. If a description starts with <C>, the file is not part of Emacs
  329. and loading it will require that you have downloaded and properly
  330. installed the Org mode distribution.
  331. You can also use this system to load external packages (i.e. neither Org
  332. core modules, nor modules from the CONTRIB directory). Just add symbols
  333. to the end of the list. If the package is called org-xyz.el, then you need
  334. to add the symbol `xyz', and the package must have a call to:
  335. \(provide 'org-xyz)
  336. For export specific modules, see also `org-export-backends'."
  337. :group 'org
  338. :set 'org-set-modules
  339. :version "24.4"
  340. :package-version '(Org . "8.0")
  341. :type
  342. '(set :greedy t
  343. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  344. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  345. (const :tag " crypt: Encryption of subtrees" org-crypt)
  346. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  347. (const :tag " docview: Links to doc-view buffers" org-docview)
  348. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  349. (const :tag " habit: Track your consistency with habits" org-habit)
  350. (const :tag " id: Global IDs for identifying entries" org-id)
  351. (const :tag " info: Links to Info nodes" org-info)
  352. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  353. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  354. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  355. (const :tag " mouse: Additional mouse support" org-mouse)
  356. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  357. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  358. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  359. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  360. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  361. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  362. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  363. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  364. (const :tag "C collector: Collect properties into tables" org-collector)
  365. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  366. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  367. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  368. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  369. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  370. (const :tag "C eval: Include command output as text" org-eval)
  371. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  372. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  373. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  374. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  375. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  376. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  377. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  378. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  379. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  380. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  381. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  382. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  383. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  384. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  385. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  386. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  387. (const :tag "C registry: A registry for Org-mode links" org-registry)
  388. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  389. (const :tag "C secretary: Team management with org-mode" org-secretary)
  390. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  391. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  392. (const :tag "C track: Keep up with Org-mode development" org-track)
  393. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  394. (const :tag "C vm: Links to VM folders/messages" org-vm)
  395. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  396. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  397. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  398. (defvar org-export-registered-backends) ; From ox.el
  399. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  400. (defcustom org-export-backends '(ascii html icalendar latex)
  401. "List of export back-ends that should be always available.
  402. If a description starts with <C>, the file is not part of Emacs
  403. and loading it will require that you have downloaded and properly
  404. installed the Org mode distribution.
  405. Unlike to `org-modules', libraries in this list will not be
  406. loaded along with Org, but only once the export framework is
  407. needed.
  408. This variable needs to be set before org.el is loaded. If you
  409. need to make a change while Emacs is running, use the customize
  410. interface or run the following code, where VALUE stands for the
  411. new value of the variable, after updating it:
  412. \(progn
  413. \(setq org-export-registered-backends
  414. \(org-remove-if-not
  415. \(lambda (backend)
  416. \(or (memq backend val)
  417. \(catch 'parentp
  418. \(mapc
  419. \(lambda (b)
  420. \(and (org-export-derived-backend-p b (car backend))
  421. \(throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  426. \(dolist (backend val)
  427. \(cond
  428. \((not (load (format \"ox-%s\" backend) t t))
  429. \(message \"Problems while trying to load export back-end `%s'\"
  430. backend))
  431. \((not (memq backend new-list)) (push backend new-list))))
  432. \(set-default var new-list)))
  433. Adding a back-end to this list will also pull the back-end it
  434. depends on, if any."
  435. :group 'org
  436. :group 'org-export
  437. :version "24.4"
  438. :package-version '(Org . "8.0")
  439. :initialize 'custom-initialize-set
  440. :set (lambda (var val)
  441. (if (not (featurep 'ox)) (set-default var val)
  442. ;; Any back-end not required anymore (not present in VAL and not
  443. ;; a parent of any back-end in the new value) is removed from the
  444. ;; list of registered back-ends.
  445. (setq org-export-registered-backends
  446. (org-remove-if-not
  447. (lambda (backend)
  448. (or (memq backend val)
  449. (catch 'parentp
  450. (mapc
  451. (lambda (b)
  452. (and (org-export-derived-backend-p b (car backend))
  453. (throw 'parentp t)))
  454. val)
  455. nil)))
  456. org-export-registered-backends))
  457. ;; Now build NEW-LIST of both new back-ends and required
  458. ;; parents.
  459. (let ((new-list (mapcar 'car org-export-registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type 'function)
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type 'function)
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const gnus)
  1561. (choice
  1562. (const gnus)
  1563. (const gnus-other-frame)
  1564. (const org-gnus-no-new-news)))
  1565. (cons (const file)
  1566. (choice
  1567. (const find-file)
  1568. (const find-file-other-window)
  1569. (const find-file-other-frame)))
  1570. (cons (const wl)
  1571. (choice
  1572. (const wl)
  1573. (const wl-other-frame)))))
  1574. (defcustom org-display-internal-link-with-indirect-buffer nil
  1575. "Non-nil means use indirect buffer to display infile links.
  1576. Activating internal links (from one location in a file to another location
  1577. in the same file) normally just jumps to the location. When the link is
  1578. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1579. is displayed in
  1580. another window. When this option is set, the other window actually displays
  1581. an indirect buffer clone of the current buffer, to avoid any visibility
  1582. changes to the current buffer."
  1583. :group 'org-link-follow
  1584. :type 'boolean)
  1585. (defcustom org-open-non-existing-files nil
  1586. "Non-nil means `org-open-file' will open non-existing files.
  1587. When nil, an error will be generated.
  1588. This variable applies only to external applications because they
  1589. might choke on non-existing files. If the link is to a file that
  1590. will be opened in Emacs, the variable is ignored."
  1591. :group 'org-link-follow
  1592. :type 'boolean)
  1593. (defcustom org-open-directory-means-index-dot-org nil
  1594. "Non-nil means a link to a directory really means to index.org.
  1595. When nil, following a directory link will run dired or open a finder/explorer
  1596. window on that directory."
  1597. :group 'org-link-follow
  1598. :type 'boolean)
  1599. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1600. "Function and arguments to call for following mailto links.
  1601. This is a list with the first element being a Lisp function, and the
  1602. remaining elements being arguments to the function. In string arguments,
  1603. %a will be replaced by the address, and %s will be replaced by the subject
  1604. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1605. :group 'org-link-follow
  1606. :type '(choice
  1607. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1608. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1609. (const :tag "message-mail" (message-mail "%a" "%s"))
  1610. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1611. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1612. "Non-nil means ask for confirmation before executing shell links.
  1613. Shell links can be dangerous: just think about a link
  1614. [[shell:rm -rf ~/*][Google Search]]
  1615. This link would show up in your Org-mode document as \"Google Search\",
  1616. but really it would remove your entire home directory.
  1617. Therefore we advise against setting this variable to nil.
  1618. Just change it to `y-or-n-p' if you want to confirm with a
  1619. single keystroke rather than having to type \"yes\"."
  1620. :group 'org-link-follow
  1621. :type '(choice
  1622. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1623. (const :tag "with y-or-n (faster)" y-or-n-p)
  1624. (const :tag "no confirmation (dangerous)" nil)))
  1625. (put 'org-confirm-shell-link-function
  1626. 'safe-local-variable
  1627. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1628. (defcustom org-confirm-shell-link-not-regexp ""
  1629. "A regexp to skip confirmation for shell links."
  1630. :group 'org-link-follow
  1631. :version "24.1"
  1632. :type 'regexp)
  1633. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1634. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1635. Elisp links can be dangerous: just think about a link
  1636. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1637. This link would show up in your Org-mode document as \"Google Search\",
  1638. but really it would remove your entire home directory.
  1639. Therefore we advise against setting this variable to nil.
  1640. Just change it to `y-or-n-p' if you want to confirm with a
  1641. single keystroke rather than having to type \"yes\"."
  1642. :group 'org-link-follow
  1643. :type '(choice
  1644. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1645. (const :tag "with y-or-n (faster)" y-or-n-p)
  1646. (const :tag "no confirmation (dangerous)" nil)))
  1647. (put 'org-confirm-shell-link-function
  1648. 'safe-local-variable
  1649. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1650. (defcustom org-confirm-elisp-link-not-regexp ""
  1651. "A regexp to skip confirmation for Elisp links."
  1652. :group 'org-link-follow
  1653. :version "24.1"
  1654. :type 'regexp)
  1655. (defconst org-file-apps-defaults-gnu
  1656. '((remote . emacs)
  1657. (system . mailcap)
  1658. (t . mailcap))
  1659. "Default file applications on a UNIX or GNU/Linux system.
  1660. See `org-file-apps'.")
  1661. (defconst org-file-apps-defaults-macosx
  1662. '((remote . emacs)
  1663. (t . "open %s")
  1664. (system . "open %s")
  1665. ("ps.gz" . "gv %s")
  1666. ("eps.gz" . "gv %s")
  1667. ("dvi" . "xdvi %s")
  1668. ("fig" . "xfig %s"))
  1669. "Default file applications on a MacOS X system.
  1670. The system \"open\" is known as a default, but we use X11 applications
  1671. for some files for which the OS does not have a good default.
  1672. See `org-file-apps'.")
  1673. (defconst org-file-apps-defaults-windowsnt
  1674. (list
  1675. '(remote . emacs)
  1676. (cons t
  1677. (list (if (featurep 'xemacs)
  1678. 'mswindows-shell-execute
  1679. 'w32-shell-execute)
  1680. "open" 'file))
  1681. (cons 'system
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file)))
  1686. "Default file applications on a Windows NT system.
  1687. The system \"open\" is used for most files.
  1688. See `org-file-apps'.")
  1689. (defcustom org-file-apps
  1690. '((auto-mode . emacs)
  1691. ("\\.mm\\'" . default)
  1692. ("\\.x?html?\\'" . default)
  1693. ("\\.pdf\\'" . default))
  1694. "External applications for opening `file:path' items in a document.
  1695. Org-mode uses system defaults for different file types, but
  1696. you can use this variable to set the application for a given file
  1697. extension. The entries in this list are cons cells where the car identifies
  1698. files and the cdr the corresponding command. Possible values for the
  1699. file identifier are
  1700. \"string\" A string as a file identifier can be interpreted in different
  1701. ways, depending on its contents:
  1702. - Alphanumeric characters only:
  1703. Match links with this file extension.
  1704. Example: (\"pdf\" . \"evince %s\")
  1705. to open PDFs with evince.
  1706. - Regular expression: Match links where the
  1707. filename matches the regexp. If you want to
  1708. use groups here, use shy groups.
  1709. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1710. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1711. to open *.html and *.xhtml with firefox.
  1712. - Regular expression which contains (non-shy) groups:
  1713. Match links where the whole link, including \"::\", and
  1714. anything after that, matches the regexp.
  1715. In a custom command string, %1, %2, etc. are replaced with
  1716. the parts of the link that were matched by the groups.
  1717. For backwards compatibility, if a command string is given
  1718. that does not use any of the group matches, this case is
  1719. handled identically to the second one (i.e. match against
  1720. file name only).
  1721. In a custom lisp form, you can access the group matches with
  1722. (match-string n link).
  1723. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1724. to open [[file:document.pdf::5]] with evince at page 5.
  1725. `directory' Matches a directory
  1726. `remote' Matches a remote file, accessible through tramp or efs.
  1727. Remote files most likely should be visited through Emacs
  1728. because external applications cannot handle such paths.
  1729. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1730. so all files Emacs knows how to handle. Using this with
  1731. command `emacs' will open most files in Emacs. Beware that this
  1732. will also open html files inside Emacs, unless you add
  1733. (\"html\" . default) to the list as well.
  1734. t Default for files not matched by any of the other options.
  1735. `system' The system command to open files, like `open' on Windows
  1736. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1737. that will be selected if you call `C-c C-o' with a double
  1738. \\[universal-argument] \\[universal-argument] prefix.
  1739. Possible values for the command are:
  1740. `emacs' The file will be visited by the current Emacs process.
  1741. `default' Use the default application for this file type, which is the
  1742. association for t in the list, most likely in the system-specific
  1743. part.
  1744. This can be used to overrule an unwanted setting in the
  1745. system-specific variable.
  1746. `system' Use the system command for opening files, like \"open\".
  1747. This command is specified by the entry whose car is `system'.
  1748. Most likely, the system-specific version of this variable
  1749. does define this command, but you can overrule/replace it
  1750. here.
  1751. string A command to be executed by a shell; %s will be replaced
  1752. by the path to the file.
  1753. sexp A Lisp form which will be evaluated. The file path will
  1754. be available in the Lisp variable `file'.
  1755. For more examples, see the system specific constants
  1756. `org-file-apps-defaults-macosx'
  1757. `org-file-apps-defaults-windowsnt'
  1758. `org-file-apps-defaults-gnu'."
  1759. :group 'org-link-follow
  1760. :type '(repeat
  1761. (cons (choice :value ""
  1762. (string :tag "Extension")
  1763. (const :tag "System command to open files" system)
  1764. (const :tag "Default for unrecognized files" t)
  1765. (const :tag "Remote file" remote)
  1766. (const :tag "Links to a directory" directory)
  1767. (const :tag "Any files that have Emacs modes"
  1768. auto-mode))
  1769. (choice :value ""
  1770. (const :tag "Visit with Emacs" emacs)
  1771. (const :tag "Use default" default)
  1772. (const :tag "Use the system command" system)
  1773. (string :tag "Command")
  1774. (sexp :tag "Lisp form")))))
  1775. (defcustom org-doi-server-url "http://dx.doi.org/"
  1776. "The URL of the DOI server."
  1777. :type 'string
  1778. :version "24.3"
  1779. :group 'org-link-follow)
  1780. (defgroup org-refile nil
  1781. "Options concerning refiling entries in Org-mode."
  1782. :tag "Org Refile"
  1783. :group 'org)
  1784. (defcustom org-directory "~/org"
  1785. "Directory with org files.
  1786. This is just a default location to look for Org files. There is no need
  1787. at all to put your files into this directory. It is only used in the
  1788. following situations:
  1789. 1. When a capture template specifies a target file that is not an
  1790. absolute path. The path will then be interpreted relative to
  1791. `org-directory'
  1792. 2. When a capture note is filed away in an interactive way (when exiting the
  1793. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1794. with `org-directory' as the default path."
  1795. :group 'org-refile
  1796. :group 'org-remember
  1797. :group 'org-capture
  1798. :type 'directory)
  1799. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1800. "Default target for storing notes.
  1801. Used as a fall back file for org-remember.el and org-capture.el, for
  1802. templates that do not specify a target file."
  1803. :group 'org-refile
  1804. :group 'org-remember
  1805. :group 'org-capture
  1806. :type '(choice
  1807. (const :tag "Default from remember-data-file" nil)
  1808. file))
  1809. (defcustom org-goto-interface 'outline
  1810. "The default interface to be used for `org-goto'.
  1811. Allowed values are:
  1812. outline The interface shows an outline of the relevant file
  1813. and the correct heading is found by moving through
  1814. the outline or by searching with incremental search.
  1815. outline-path-completion Headlines in the current buffer are offered via
  1816. completion. This is the interface also used by
  1817. the refile command."
  1818. :group 'org-refile
  1819. :type '(choice
  1820. (const :tag "Outline" outline)
  1821. (const :tag "Outline-path-completion" outline-path-completion)))
  1822. (defcustom org-goto-max-level 5
  1823. "Maximum target level when running `org-goto' with refile interface."
  1824. :group 'org-refile
  1825. :type 'integer)
  1826. (defcustom org-reverse-note-order nil
  1827. "Non-nil means store new notes at the beginning of a file or entry.
  1828. When nil, new notes will be filed to the end of a file or entry.
  1829. This can also be a list with cons cells of regular expressions that
  1830. are matched against file names, and values."
  1831. :group 'org-remember
  1832. :group 'org-capture
  1833. :group 'org-refile
  1834. :type '(choice
  1835. (const :tag "Reverse always" t)
  1836. (const :tag "Reverse never" nil)
  1837. (repeat :tag "By file name regexp"
  1838. (cons regexp boolean))))
  1839. (defcustom org-log-refile nil
  1840. "Information to record when a task is refiled.
  1841. Possible values are:
  1842. nil Don't add anything
  1843. time Add a time stamp to the task
  1844. note Prompt for a note and add it with template `org-log-note-headings'
  1845. This option can also be set with on a per-file-basis with
  1846. #+STARTUP: nologrefile
  1847. #+STARTUP: logrefile
  1848. #+STARTUP: lognoterefile
  1849. You can have local logging settings for a subtree by setting the LOGGING
  1850. property to one or more of these keywords.
  1851. When bulk-refiling from the agenda, the value `note' is forbidden and
  1852. will temporarily be changed to `time'."
  1853. :group 'org-refile
  1854. :group 'org-progress
  1855. :version "24.1"
  1856. :type '(choice
  1857. (const :tag "No logging" nil)
  1858. (const :tag "Record timestamp" time)
  1859. (const :tag "Record timestamp with note." note)))
  1860. (defcustom org-refile-targets nil
  1861. "Targets for refiling entries with \\[org-refile].
  1862. This is a list of cons cells. Each cell contains:
  1863. - a specification of the files to be considered, either a list of files,
  1864. or a symbol whose function or variable value will be used to retrieve
  1865. a file name or a list of file names. If you use `org-agenda-files' for
  1866. that, all agenda files will be scanned for targets. Nil means consider
  1867. headings in the current buffer.
  1868. - A specification of how to find candidate refile targets. This may be
  1869. any of:
  1870. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1871. This tag has to be present in all target headlines, inheritance will
  1872. not be considered.
  1873. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1874. todo keyword.
  1875. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1876. headlines that are refiling targets.
  1877. - a cons cell (:level . N). Any headline of level N is considered a target.
  1878. Note that, when `org-odd-levels-only' is set, level corresponds to
  1879. order in hierarchy, not to the number of stars.
  1880. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1881. Note that, when `org-odd-levels-only' is set, level corresponds to
  1882. order in hierarchy, not to the number of stars.
  1883. Each element of this list generates a set of possible targets.
  1884. The union of these sets is presented (with completion) to
  1885. the user by `org-refile'.
  1886. You can set the variable `org-refile-target-verify-function' to a function
  1887. to verify each headline found by the simple criteria above.
  1888. When this variable is nil, all top-level headlines in the current buffer
  1889. are used, equivalent to the value `((nil . (:level . 1))'."
  1890. :group 'org-refile
  1891. :type '(repeat
  1892. (cons
  1893. (choice :value org-agenda-files
  1894. (const :tag "All agenda files" org-agenda-files)
  1895. (const :tag "Current buffer" nil)
  1896. (function) (variable) (file))
  1897. (choice :tag "Identify target headline by"
  1898. (cons :tag "Specific tag" (const :value :tag) (string))
  1899. (cons :tag "TODO keyword" (const :value :todo) (string))
  1900. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1901. (cons :tag "Level number" (const :value :level) (integer))
  1902. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1903. (defcustom org-refile-target-verify-function nil
  1904. "Function to verify if the headline at point should be a refile target.
  1905. The function will be called without arguments, with point at the
  1906. beginning of the headline. It should return t and leave point
  1907. where it is if the headline is a valid target for refiling.
  1908. If the target should not be selected, the function must return nil.
  1909. In addition to this, it may move point to a place from where the search
  1910. should be continued. For example, the function may decide that the entire
  1911. subtree of the current entry should be excluded and move point to the end
  1912. of the subtree."
  1913. :group 'org-refile
  1914. :type 'function)
  1915. (defcustom org-refile-use-cache nil
  1916. "Non-nil means cache refile targets to speed up the process.
  1917. The cache for a particular file will be updated automatically when
  1918. the buffer has been killed, or when any of the marker used for flagging
  1919. refile targets no longer points at a live buffer.
  1920. If you have added new entries to a buffer that might themselves be targets,
  1921. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1922. find that easier, `C-u C-u C-u C-c C-w'."
  1923. :group 'org-refile
  1924. :version "24.1"
  1925. :type 'boolean)
  1926. (defcustom org-refile-use-outline-path nil
  1927. "Non-nil means provide refile targets as paths.
  1928. So a level 3 headline will be available as level1/level2/level3.
  1929. When the value is `file', also include the file name (without directory)
  1930. into the path. In this case, you can also stop the completion after
  1931. the file name, to get entries inserted as top level in the file.
  1932. When `full-file-path', include the full file path."
  1933. :group 'org-refile
  1934. :type '(choice
  1935. (const :tag "Not" nil)
  1936. (const :tag "Yes" t)
  1937. (const :tag "Start with file name" file)
  1938. (const :tag "Start with full file path" full-file-path)))
  1939. (defcustom org-outline-path-complete-in-steps t
  1940. "Non-nil means complete the outline path in hierarchical steps.
  1941. When Org-mode uses the refile interface to select an outline path
  1942. \(see variable `org-refile-use-outline-path'), the completion of
  1943. the path can be done is a single go, or if can be done in steps down
  1944. the headline hierarchy. Going in steps is probably the best if you
  1945. do not use a special completion package like `ido' or `icicles'.
  1946. However, when using these packages, going in one step can be very
  1947. fast, while still showing the whole path to the entry."
  1948. :group 'org-refile
  1949. :type 'boolean)
  1950. (defcustom org-refile-allow-creating-parent-nodes nil
  1951. "Non-nil means allow to create new nodes as refile targets.
  1952. New nodes are then created by adding \"/new node name\" to the completion
  1953. of an existing node. When the value of this variable is `confirm',
  1954. new node creation must be confirmed by the user (recommended)
  1955. When nil, the completion must match an existing entry.
  1956. Note that, if the new heading is not seen by the criteria
  1957. listed in `org-refile-targets', multiple instances of the same
  1958. heading would be created by trying again to file under the new
  1959. heading."
  1960. :group 'org-refile
  1961. :type '(choice
  1962. (const :tag "Never" nil)
  1963. (const :tag "Always" t)
  1964. (const :tag "Prompt for confirmation" confirm)))
  1965. (defcustom org-refile-active-region-within-subtree nil
  1966. "Non-nil means also refile active region within a subtree.
  1967. By default `org-refile' doesn't allow refiling regions if they
  1968. don't contain a set of subtrees, but it might be convenient to
  1969. do so sometimes: in that case, the first line of the region is
  1970. converted to a headline before refiling."
  1971. :group 'org-refile
  1972. :version "24.1"
  1973. :type 'boolean)
  1974. (defgroup org-todo nil
  1975. "Options concerning TODO items in Org-mode."
  1976. :tag "Org TODO"
  1977. :group 'org)
  1978. (defgroup org-progress nil
  1979. "Options concerning Progress logging in Org-mode."
  1980. :tag "Org Progress"
  1981. :group 'org-time)
  1982. (defvar org-todo-interpretation-widgets
  1983. '((:tag "Sequence (cycling hits every state)" sequence)
  1984. (:tag "Type (cycling directly to DONE)" type))
  1985. "The available interpretation symbols for customizing `org-todo-keywords'.
  1986. Interested libraries should add to this list.")
  1987. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1988. "List of TODO entry keyword sequences and their interpretation.
  1989. \\<org-mode-map>This is a list of sequences.
  1990. Each sequence starts with a symbol, either `sequence' or `type',
  1991. indicating if the keywords should be interpreted as a sequence of
  1992. action steps, or as different types of TODO items. The first
  1993. keywords are states requiring action - these states will select a headline
  1994. for inclusion into the global TODO list Org-mode produces. If one of
  1995. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1996. signify that no further action is necessary. If \"|\" is not found,
  1997. the last keyword is treated as the only DONE state of the sequence.
  1998. The command \\[org-todo] cycles an entry through these states, and one
  1999. additional state where no keyword is present. For details about this
  2000. cycling, see the manual.
  2001. TODO keywords and interpretation can also be set on a per-file basis with
  2002. the special #+SEQ_TODO and #+TYP_TODO lines.
  2003. Each keyword can optionally specify a character for fast state selection
  2004. \(in combination with the variable `org-use-fast-todo-selection')
  2005. and specifiers for state change logging, using the same syntax that
  2006. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2007. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2008. indicates to record a time stamp each time this state is selected.
  2009. Each keyword may also specify if a timestamp or a note should be
  2010. recorded when entering or leaving the state, by adding additional
  2011. characters in the parenthesis after the keyword. This looks like this:
  2012. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2013. record only the time of the state change. With X and Y being either
  2014. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2015. Y when leaving the state if and only if the *target* state does not
  2016. define X. You may omit any of the fast-selection key or X or /Y,
  2017. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2018. For backward compatibility, this variable may also be just a list
  2019. of keywords. In this case the interpretation (sequence or type) will be
  2020. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2021. :group 'org-todo
  2022. :group 'org-keywords
  2023. :type '(choice
  2024. (repeat :tag "Old syntax, just keywords"
  2025. (string :tag "Keyword"))
  2026. (repeat :tag "New syntax"
  2027. (cons
  2028. (choice
  2029. :tag "Interpretation"
  2030. ;;Quick and dirty way to see
  2031. ;;`org-todo-interpretations'. This takes the
  2032. ;;place of item arguments
  2033. :convert-widget
  2034. (lambda (widget)
  2035. (widget-put widget
  2036. :args (mapcar
  2037. #'(lambda (x)
  2038. (widget-convert
  2039. (cons 'const x)))
  2040. org-todo-interpretation-widgets))
  2041. widget))
  2042. (repeat
  2043. (string :tag "Keyword"))))))
  2044. (defvar org-todo-keywords-1 nil
  2045. "All TODO and DONE keywords active in a buffer.")
  2046. (make-variable-buffer-local 'org-todo-keywords-1)
  2047. (defvar org-todo-keywords-for-agenda nil)
  2048. (defvar org-done-keywords-for-agenda nil)
  2049. (defvar org-drawers-for-agenda nil)
  2050. (defvar org-todo-keyword-alist-for-agenda nil)
  2051. (defvar org-tag-alist-for-agenda nil
  2052. "Alist of all tags from all agenda files.")
  2053. (defvar org-tag-groups-alist-for-agenda nil
  2054. "Alist of all groups tags from all current agenda files.")
  2055. (defvar org-tag-groups-alist nil)
  2056. (make-variable-buffer-local 'org-tag-groups-alist)
  2057. (defvar org-agenda-contributing-files nil)
  2058. (defvar org-not-done-keywords nil)
  2059. (make-variable-buffer-local 'org-not-done-keywords)
  2060. (defvar org-done-keywords nil)
  2061. (make-variable-buffer-local 'org-done-keywords)
  2062. (defvar org-todo-heads nil)
  2063. (make-variable-buffer-local 'org-todo-heads)
  2064. (defvar org-todo-sets nil)
  2065. (make-variable-buffer-local 'org-todo-sets)
  2066. (defvar org-todo-log-states nil)
  2067. (make-variable-buffer-local 'org-todo-log-states)
  2068. (defvar org-todo-kwd-alist nil)
  2069. (make-variable-buffer-local 'org-todo-kwd-alist)
  2070. (defvar org-todo-key-alist nil)
  2071. (make-variable-buffer-local 'org-todo-key-alist)
  2072. (defvar org-todo-key-trigger nil)
  2073. (make-variable-buffer-local 'org-todo-key-trigger)
  2074. (defcustom org-todo-interpretation 'sequence
  2075. "Controls how TODO keywords are interpreted.
  2076. This variable is in principle obsolete and is only used for
  2077. backward compatibility, if the interpretation of todo keywords is
  2078. not given already in `org-todo-keywords'. See that variable for
  2079. more information."
  2080. :group 'org-todo
  2081. :group 'org-keywords
  2082. :type '(choice (const sequence)
  2083. (const type)))
  2084. (defcustom org-use-fast-todo-selection t
  2085. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2086. This variable describes if and under what circumstances the cycling
  2087. mechanism for TODO keywords will be replaced by a single-key, direct
  2088. selection scheme.
  2089. When nil, fast selection is never used.
  2090. When the symbol `prefix', it will be used when `org-todo' is called
  2091. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2092. `C-u t' in an agenda buffer.
  2093. When t, fast selection is used by default. In this case, the prefix
  2094. argument forces cycling instead.
  2095. In all cases, the special interface is only used if access keys have
  2096. actually been assigned by the user, i.e. if keywords in the configuration
  2097. are followed by a letter in parenthesis, like TODO(t)."
  2098. :group 'org-todo
  2099. :type '(choice
  2100. (const :tag "Never" nil)
  2101. (const :tag "By default" t)
  2102. (const :tag "Only with C-u C-c C-t" prefix)))
  2103. (defcustom org-provide-todo-statistics t
  2104. "Non-nil means update todo statistics after insert and toggle.
  2105. ALL-HEADLINES means update todo statistics by including headlines
  2106. with no TODO keyword as well, counting them as not done.
  2107. A list of TODO keywords means the same, but skip keywords that are
  2108. not in this list.
  2109. When this is set, todo statistics is updated in the parent of the
  2110. current entry each time a todo state is changed."
  2111. :group 'org-todo
  2112. :type '(choice
  2113. (const :tag "Yes, only for TODO entries" t)
  2114. (const :tag "Yes, including all entries" 'all-headlines)
  2115. (repeat :tag "Yes, for TODOs in this list"
  2116. (string :tag "TODO keyword"))
  2117. (other :tag "No TODO statistics" nil)))
  2118. (defcustom org-hierarchical-todo-statistics t
  2119. "Non-nil means TODO statistics covers just direct children.
  2120. When nil, all entries in the subtree are considered.
  2121. This has only an effect if `org-provide-todo-statistics' is set.
  2122. To set this to nil for only a single subtree, use a COOKIE_DATA
  2123. property and include the word \"recursive\" into the value."
  2124. :group 'org-todo
  2125. :type 'boolean)
  2126. (defcustom org-after-todo-state-change-hook nil
  2127. "Hook which is run after the state of a TODO item was changed.
  2128. The new state (a string with a TODO keyword, or nil) is available in the
  2129. Lisp variable `org-state'."
  2130. :group 'org-todo
  2131. :type 'hook)
  2132. (defvar org-blocker-hook nil
  2133. "Hook for functions that are allowed to block a state change.
  2134. Functions in this hook should not modify the buffer.
  2135. Each function gets as its single argument a property list,
  2136. see `org-trigger-hook' for more information about this list.
  2137. If any of the functions in this hook returns nil, the state change
  2138. is blocked.")
  2139. (defvar org-trigger-hook nil
  2140. "Hook for functions that are triggered by a state change.
  2141. Each function gets as its single argument a property list with at
  2142. least the following elements:
  2143. (:type type-of-change :position pos-at-entry-start
  2144. :from old-state :to new-state)
  2145. Depending on the type, more properties may be present.
  2146. This mechanism is currently implemented for:
  2147. TODO state changes
  2148. ------------------
  2149. :type todo-state-change
  2150. :from previous state (keyword as a string), or nil, or a symbol
  2151. 'todo' or 'done', to indicate the general type of state.
  2152. :to new state, like in :from")
  2153. (defcustom org-enforce-todo-dependencies nil
  2154. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2155. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2156. be blocked if any prior sibling is not yet done.
  2157. Finally, if the parent is blocked because of ordered siblings of its own,
  2158. the child will also be blocked."
  2159. :set (lambda (var val)
  2160. (set var val)
  2161. (if val
  2162. (add-hook 'org-blocker-hook
  2163. 'org-block-todo-from-children-or-siblings-or-parent)
  2164. (remove-hook 'org-blocker-hook
  2165. 'org-block-todo-from-children-or-siblings-or-parent)))
  2166. :group 'org-todo
  2167. :type 'boolean)
  2168. (defcustom org-enforce-todo-checkbox-dependencies nil
  2169. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2170. When this is nil, checkboxes have no influence on switching TODO states.
  2171. When non-nil, you first need to check off all check boxes before the TODO
  2172. entry can be switched to DONE.
  2173. This variable needs to be set before org.el is loaded, and you need to
  2174. restart Emacs after a change to make the change effective. The only way
  2175. to change is while Emacs is running is through the customize interface."
  2176. :set (lambda (var val)
  2177. (set var val)
  2178. (if val
  2179. (add-hook 'org-blocker-hook
  2180. 'org-block-todo-from-checkboxes)
  2181. (remove-hook 'org-blocker-hook
  2182. 'org-block-todo-from-checkboxes)))
  2183. :group 'org-todo
  2184. :type 'boolean)
  2185. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2186. "Non-nil means inserting a TODO heading is treated as state change.
  2187. So when the command \\[org-insert-todo-heading] is used, state change
  2188. logging will apply if appropriate. When nil, the new TODO item will
  2189. be inserted directly, and no logging will take place."
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2193. "Non-nil means switching TODO states with S-cursor counts as state change.
  2194. This is the default behavior. However, setting this to nil allows a
  2195. convenient way to select a TODO state and bypass any logging associated
  2196. with that."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-todo-state-tags-triggers nil
  2200. "Tag changes that should be triggered by TODO state changes.
  2201. This is a list. Each entry is
  2202. (state-change (tag . flag) .......)
  2203. State-change can be a string with a state, and empty string to indicate the
  2204. state that has no TODO keyword, or it can be one of the symbols `todo'
  2205. or `done', meaning any not-done or done state, respectively."
  2206. :group 'org-todo
  2207. :group 'org-tags
  2208. :type '(repeat
  2209. (cons (choice :tag "When changing to"
  2210. (const :tag "Not-done state" todo)
  2211. (const :tag "Done state" done)
  2212. (string :tag "State"))
  2213. (repeat
  2214. (cons :tag "Tag action"
  2215. (string :tag "Tag")
  2216. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2217. (defcustom org-log-done nil
  2218. "Information to record when a task moves to the DONE state.
  2219. Possible values are:
  2220. nil Don't add anything, just change the keyword
  2221. time Add a time stamp to the task
  2222. note Prompt for a note and add it with template `org-log-note-headings'
  2223. This option can also be set with on a per-file-basis with
  2224. #+STARTUP: nologdone
  2225. #+STARTUP: logdone
  2226. #+STARTUP: lognotedone
  2227. You can have local logging settings for a subtree by setting the LOGGING
  2228. property to one or more of these keywords."
  2229. :group 'org-todo
  2230. :group 'org-progress
  2231. :type '(choice
  2232. (const :tag "No logging" nil)
  2233. (const :tag "Record CLOSED timestamp" time)
  2234. (const :tag "Record CLOSED timestamp with note." note)))
  2235. ;; Normalize old uses of org-log-done.
  2236. (cond
  2237. ((eq org-log-done t) (setq org-log-done 'time))
  2238. ((and (listp org-log-done) (memq 'done org-log-done))
  2239. (setq org-log-done 'note)))
  2240. (defcustom org-log-reschedule nil
  2241. "Information to record when the scheduling date of a tasks is modified.
  2242. Possible values are:
  2243. nil Don't add anything, just change the date
  2244. time Add a time stamp to the task
  2245. note Prompt for a note and add it with template `org-log-note-headings'
  2246. This option can also be set with on a per-file-basis with
  2247. #+STARTUP: nologreschedule
  2248. #+STARTUP: logreschedule
  2249. #+STARTUP: lognotereschedule"
  2250. :group 'org-todo
  2251. :group 'org-progress
  2252. :type '(choice
  2253. (const :tag "No logging" nil)
  2254. (const :tag "Record timestamp" time)
  2255. (const :tag "Record timestamp with note." note)))
  2256. (defcustom org-log-redeadline nil
  2257. "Information to record when the deadline date of a tasks is modified.
  2258. Possible values are:
  2259. nil Don't add anything, just change the date
  2260. time Add a time stamp to the task
  2261. note Prompt for a note and add it with template `org-log-note-headings'
  2262. This option can also be set with on a per-file-basis with
  2263. #+STARTUP: nologredeadline
  2264. #+STARTUP: logredeadline
  2265. #+STARTUP: lognoteredeadline
  2266. You can have local logging settings for a subtree by setting the LOGGING
  2267. property to one or more of these keywords."
  2268. :group 'org-todo
  2269. :group 'org-progress
  2270. :type '(choice
  2271. (const :tag "No logging" nil)
  2272. (const :tag "Record timestamp" time)
  2273. (const :tag "Record timestamp with note." note)))
  2274. (defcustom org-log-note-clock-out nil
  2275. "Non-nil means record a note when clocking out of an item.
  2276. This can also be configured on a per-file basis by adding one of
  2277. the following lines anywhere in the buffer:
  2278. #+STARTUP: lognoteclock-out
  2279. #+STARTUP: nolognoteclock-out"
  2280. :group 'org-todo
  2281. :group 'org-progress
  2282. :type 'boolean)
  2283. (defcustom org-log-done-with-time t
  2284. "Non-nil means the CLOSED time stamp will contain date and time.
  2285. When nil, only the date will be recorded."
  2286. :group 'org-progress
  2287. :type 'boolean)
  2288. (defcustom org-log-note-headings
  2289. '((done . "CLOSING NOTE %t")
  2290. (state . "State %-12s from %-12S %t")
  2291. (note . "Note taken on %t")
  2292. (reschedule . "Rescheduled from %S on %t")
  2293. (delschedule . "Not scheduled, was %S on %t")
  2294. (redeadline . "New deadline from %S on %t")
  2295. (deldeadline . "Removed deadline, was %S on %t")
  2296. (refile . "Refiled on %t")
  2297. (clock-out . ""))
  2298. "Headings for notes added to entries.
  2299. The value is an alist, with the car being a symbol indicating the note
  2300. context, and the cdr is the heading to be used. The heading may also be the
  2301. empty string.
  2302. %t in the heading will be replaced by a time stamp.
  2303. %T will be an active time stamp instead the default inactive one
  2304. %d will be replaced by a short-format time stamp.
  2305. %D will be replaced by an active short-format time stamp.
  2306. %s will be replaced by the new TODO state, in double quotes.
  2307. %S will be replaced by the old TODO state, in double quotes.
  2308. %u will be replaced by the user name.
  2309. %U will be replaced by the full user name.
  2310. In fact, it is not a good idea to change the `state' entry, because
  2311. agenda log mode depends on the format of these entries."
  2312. :group 'org-todo
  2313. :group 'org-progress
  2314. :type '(list :greedy t
  2315. (cons (const :tag "Heading when closing an item" done) string)
  2316. (cons (const :tag
  2317. "Heading when changing todo state (todo sequence only)"
  2318. state) string)
  2319. (cons (const :tag "Heading when just taking a note" note) string)
  2320. (cons (const :tag "Heading when clocking out" clock-out) string)
  2321. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2322. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2323. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2324. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2325. (cons (const :tag "Heading when refiling" refile) string)))
  2326. (unless (assq 'note org-log-note-headings)
  2327. (push '(note . "%t") org-log-note-headings))
  2328. (defcustom org-log-into-drawer nil
  2329. "Non-nil means insert state change notes and time stamps into a drawer.
  2330. When nil, state changes notes will be inserted after the headline and
  2331. any scheduling and clock lines, but not inside a drawer.
  2332. The value of this variable should be the name of the drawer to use.
  2333. LOGBOOK is proposed as the default drawer for this purpose, you can
  2334. also set this to a string to define the drawer of your choice.
  2335. A value of t is also allowed, representing \"LOGBOOK\".
  2336. A value of t or nil can also be set with on a per-file-basis with
  2337. #+STARTUP: logdrawer
  2338. #+STARTUP: nologdrawer
  2339. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2340. will be ignored.
  2341. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2342. a subtree."
  2343. :group 'org-todo
  2344. :group 'org-progress
  2345. :type '(choice
  2346. (const :tag "Not into a drawer" nil)
  2347. (const :tag "LOGBOOK" t)
  2348. (string :tag "Other")))
  2349. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2350. (defun org-log-into-drawer ()
  2351. "Return the value of `org-log-into-drawer', but let properties overrule.
  2352. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2353. used instead of the default value."
  2354. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2355. (cond
  2356. ((not p) org-log-into-drawer)
  2357. ((equal p "nil") nil)
  2358. ((equal p "t") "LOGBOOK")
  2359. (t p))))
  2360. (defcustom org-log-state-notes-insert-after-drawers nil
  2361. "Non-nil means insert state change notes after any drawers in entry.
  2362. Only the drawers that *immediately* follow the headline and the
  2363. deadline/scheduled line are skipped.
  2364. When nil, insert notes right after the heading and perhaps the line
  2365. with deadline/scheduling if present.
  2366. This variable will have no effect if `org-log-into-drawer' is
  2367. set."
  2368. :group 'org-todo
  2369. :group 'org-progress
  2370. :type 'boolean)
  2371. (defcustom org-log-states-order-reversed t
  2372. "Non-nil means the latest state note will be directly after heading.
  2373. When nil, the state change notes will be ordered according to time.
  2374. This option can also be set with on a per-file-basis with
  2375. #+STARTUP: logstatesreversed
  2376. #+STARTUP: nologstatesreversed"
  2377. :group 'org-todo
  2378. :group 'org-progress
  2379. :type 'boolean)
  2380. (defcustom org-todo-repeat-to-state nil
  2381. "The TODO state to which a repeater should return the repeating task.
  2382. By default this is the first task in a TODO sequence, or the previous state
  2383. in a TODO_TYP set. But you can specify another task here.
  2384. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2385. :group 'org-todo
  2386. :version "24.1"
  2387. :type '(choice (const :tag "Head of sequence" nil)
  2388. (string :tag "Specific state")))
  2389. (defcustom org-log-repeat 'time
  2390. "Non-nil means record moving through the DONE state when triggering repeat.
  2391. An auto-repeating task is immediately switched back to TODO when
  2392. marked DONE. If you are not logging state changes (by adding \"@\"
  2393. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2394. record a closing note, there will be no record of the task moving
  2395. through DONE. This variable forces taking a note anyway.
  2396. nil Don't force a record
  2397. time Record a time stamp
  2398. note Prompt for a note and add it with template `org-log-note-headings'
  2399. This option can also be set with on a per-file-basis with
  2400. #+STARTUP: nologrepeat
  2401. #+STARTUP: logrepeat
  2402. #+STARTUP: lognoterepeat
  2403. You can have local logging settings for a subtree by setting the LOGGING
  2404. property to one or more of these keywords."
  2405. :group 'org-todo
  2406. :group 'org-progress
  2407. :type '(choice
  2408. (const :tag "Don't force a record" nil)
  2409. (const :tag "Force recording the DONE state" time)
  2410. (const :tag "Force recording a note with the DONE state" note)))
  2411. (defgroup org-priorities nil
  2412. "Priorities in Org-mode."
  2413. :tag "Org Priorities"
  2414. :group 'org-todo)
  2415. (defcustom org-enable-priority-commands t
  2416. "Non-nil means priority commands are active.
  2417. When nil, these commands will be disabled, so that you never accidentally
  2418. set a priority."
  2419. :group 'org-priorities
  2420. :type 'boolean)
  2421. (defcustom org-highest-priority ?A
  2422. "The highest priority of TODO items. A character like ?A, ?B etc.
  2423. Must have a smaller ASCII number than `org-lowest-priority'."
  2424. :group 'org-priorities
  2425. :type 'character)
  2426. (defcustom org-lowest-priority ?C
  2427. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2428. Must have a larger ASCII number than `org-highest-priority'."
  2429. :group 'org-priorities
  2430. :type 'character)
  2431. (defcustom org-default-priority ?B
  2432. "The default priority of TODO items.
  2433. This is the priority an item gets if no explicit priority is given.
  2434. When starting to cycle on an empty priority the first step in the cycle
  2435. depends on `org-priority-start-cycle-with-default'. The resulting first
  2436. step priority must not exceed the range from `org-highest-priority' to
  2437. `org-lowest-priority' which means that `org-default-priority' has to be
  2438. in this range exclusive or inclusive the range boundaries. Else the
  2439. first step refuses to set the default and the second will fall back
  2440. to (depending on the command used) the highest or lowest priority."
  2441. :group 'org-priorities
  2442. :type 'character)
  2443. (defcustom org-priority-start-cycle-with-default t
  2444. "Non-nil means start with default priority when starting to cycle.
  2445. When this is nil, the first step in the cycle will be (depending on the
  2446. command used) one higher or lower than the default priority.
  2447. See also `org-default-priority'."
  2448. :group 'org-priorities
  2449. :type 'boolean)
  2450. (defcustom org-get-priority-function nil
  2451. "Function to extract the priority from a string.
  2452. The string is normally the headline. If this is nil Org computes the
  2453. priority from the priority cookie like [#A] in the headline. It returns
  2454. an integer, increasing by 1000 for each priority level.
  2455. The user can set a different function here, which should take a string
  2456. as an argument and return the numeric priority."
  2457. :group 'org-priorities
  2458. :version "24.1"
  2459. :type 'function)
  2460. (defgroup org-time nil
  2461. "Options concerning time stamps and deadlines in Org-mode."
  2462. :tag "Org Time"
  2463. :group 'org)
  2464. (defcustom org-insert-labeled-timestamps-at-point nil
  2465. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2466. When nil, these labeled time stamps are forces into the second line of an
  2467. entry, just after the headline. When scheduling from the global TODO list,
  2468. the time stamp will always be forced into the second line."
  2469. :group 'org-time
  2470. :type 'boolean)
  2471. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2472. "Formats for `format-time-string' which are used for time stamps.
  2473. It is not recommended to change this constant.")
  2474. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2475. "Number of minutes to round time stamps to.
  2476. These are two values, the first applies when first creating a time stamp.
  2477. The second applies when changing it with the commands `S-up' and `S-down'.
  2478. When changing the time stamp, this means that it will change in steps
  2479. of N minutes, as given by the second value.
  2480. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2481. numbers should be factors of 60, so for example 5, 10, 15.
  2482. When this is larger than 1, you can still force an exact time stamp by using
  2483. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2484. and by using a prefix arg to `S-up/down' to specify the exact number
  2485. of minutes to shift."
  2486. :group 'org-time
  2487. :get #'(lambda (var) ; Make sure both elements are there
  2488. (if (integerp (default-value var))
  2489. (list (default-value var) 5)
  2490. (default-value var)))
  2491. :type '(list
  2492. (integer :tag "when inserting times")
  2493. (integer :tag "when modifying times")))
  2494. ;; Normalize old customizations of this variable.
  2495. (when (integerp org-time-stamp-rounding-minutes)
  2496. (setq org-time-stamp-rounding-minutes
  2497. (list org-time-stamp-rounding-minutes
  2498. org-time-stamp-rounding-minutes)))
  2499. (defcustom org-display-custom-times nil
  2500. "Non-nil means overlay custom formats over all time stamps.
  2501. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2502. To turn this on on a per-file basis, insert anywhere in the file:
  2503. #+STARTUP: customtime"
  2504. :group 'org-time
  2505. :set 'set-default
  2506. :type 'sexp)
  2507. (make-variable-buffer-local 'org-display-custom-times)
  2508. (defcustom org-time-stamp-custom-formats
  2509. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2510. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2511. These are overlaid over the default ISO format if the variable
  2512. `org-display-custom-times' is set. Time like %H:%M should be at the
  2513. end of the second format. The custom formats are also honored by export
  2514. commands, if custom time display is turned on at the time of export."
  2515. :group 'org-time
  2516. :type 'sexp)
  2517. (defun org-time-stamp-format (&optional long inactive)
  2518. "Get the right format for a time string."
  2519. (let ((f (if long (cdr org-time-stamp-formats)
  2520. (car org-time-stamp-formats))))
  2521. (if inactive
  2522. (concat "[" (substring f 1 -1) "]")
  2523. f)))
  2524. (defcustom org-time-clocksum-format
  2525. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2526. "The format string used when creating CLOCKSUM lines.
  2527. This is also used when Org mode generates a time duration.
  2528. The value can be a single format string containing two
  2529. %-sequences, which will be filled with the number of hours and
  2530. minutes in that order.
  2531. Alternatively, the value can be a plist associating any of the
  2532. keys :years, :months, :weeks, :days, :hours or :minutes with
  2533. format strings. The time duration is formatted using only the
  2534. time components that are needed and concatenating the results.
  2535. If a time unit in absent, it falls back to the next smallest
  2536. unit.
  2537. The keys :require-years, :require-months, :require-days,
  2538. :require-weeks, :require-hours, :require-minutes are also
  2539. meaningful. A non-nil value for these keys indicates that the
  2540. corresponding time component should always be included, even if
  2541. its value is 0.
  2542. For example,
  2543. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2544. :require-minutes t)
  2545. means durations longer than a day will be expressed in days,
  2546. hours and minutes, and durations less than a day will always be
  2547. expressed in hours and minutes (even for durations less than an
  2548. hour).
  2549. The value
  2550. \(:days \"%dd\" :minutes \"%dm\")
  2551. means durations longer than a day will be expressed in days and
  2552. minutes, and durations less than a day will be expressed entirely
  2553. in minutes (even for durations longer than an hour)."
  2554. :group 'org-time
  2555. :group 'org-clock
  2556. :version "24.4"
  2557. :package-version '(Org . "8.0")
  2558. :type '(choice (string :tag "Format string")
  2559. (set :tag "Plist"
  2560. (group :inline t (const :tag "Years" :years)
  2561. (string :tag "Format string"))
  2562. (group :inline t
  2563. (const :tag "Always show years" :require-years)
  2564. (const t))
  2565. (group :inline t (const :tag "Months" :months)
  2566. (string :tag "Format string"))
  2567. (group :inline t
  2568. (const :tag "Always show months" :require-months)
  2569. (const t))
  2570. (group :inline t (const :tag "Weeks" :weeks)
  2571. (string :tag "Format string"))
  2572. (group :inline t
  2573. (const :tag "Always show weeks" :require-weeks)
  2574. (const t))
  2575. (group :inline t (const :tag "Days" :days)
  2576. (string :tag "Format string"))
  2577. (group :inline t
  2578. (const :tag "Always show days" :require-days)
  2579. (const t))
  2580. (group :inline t (const :tag "Hours" :hours)
  2581. (string :tag "Format string"))
  2582. (group :inline t
  2583. (const :tag "Always show hours" :require-hours)
  2584. (const t))
  2585. (group :inline t (const :tag "Minutes" :minutes)
  2586. (string :tag "Format string"))
  2587. (group :inline t
  2588. (const :tag "Always show minutes" :require-minutes)
  2589. (const t)))))
  2590. (defcustom org-time-clocksum-use-fractional nil
  2591. "When non-nil, \\[org-clock-display] uses fractional times.
  2592. See `org-time-clocksum-format' for more on time clock formats."
  2593. :group 'org-time
  2594. :group 'org-clock
  2595. :version "24.3"
  2596. :type 'boolean)
  2597. (defcustom org-time-clocksum-use-effort-durations nil
  2598. "When non-nil, \\[org-clock-display] uses effort durations.
  2599. E.g. by default, one day is considered to be a 8 hours effort,
  2600. so a task that has been clocked for 16 hours will be displayed
  2601. as during 2 days in the clock display or in the clocktable.
  2602. See `org-effort-durations' on how to set effort durations
  2603. and `org-time-clocksum-format' for more on time clock formats."
  2604. :group 'org-time
  2605. :group 'org-clock
  2606. :version "24.4"
  2607. :package-version '(Org . "8.0")
  2608. :type 'boolean)
  2609. (defcustom org-time-clocksum-fractional-format "%.2f"
  2610. "The format string used when creating CLOCKSUM lines,
  2611. or when Org mode generates a time duration, if
  2612. `org-time-clocksum-use-fractional' is enabled.
  2613. The value can be a single format string containing one
  2614. %-sequence, which will be filled with the number of hours as
  2615. a float.
  2616. Alternatively, the value can be a plist associating any of the
  2617. keys :years, :months, :weeks, :days, :hours or :minutes with
  2618. a format string. The time duration is formatted using the
  2619. largest time unit which gives a non-zero integer part. If all
  2620. specified formats have zero integer part, the smallest time unit
  2621. is used."
  2622. :group 'org-time
  2623. :type '(choice (string :tag "Format string")
  2624. (set (group :inline t (const :tag "Years" :years)
  2625. (string :tag "Format string"))
  2626. (group :inline t (const :tag "Months" :months)
  2627. (string :tag "Format string"))
  2628. (group :inline t (const :tag "Weeks" :weeks)
  2629. (string :tag "Format string"))
  2630. (group :inline t (const :tag "Days" :days)
  2631. (string :tag "Format string"))
  2632. (group :inline t (const :tag "Hours" :hours)
  2633. (string :tag "Format string"))
  2634. (group :inline t (const :tag "Minutes" :minutes)
  2635. (string :tag "Format string")))))
  2636. (defcustom org-deadline-warning-days 14
  2637. "Number of days before expiration during which a deadline becomes active.
  2638. This variable governs the display in sparse trees and in the agenda.
  2639. When 0 or negative, it means use this number (the absolute value of it)
  2640. even if a deadline has a different individual lead time specified.
  2641. Custom commands can set this variable in the options section."
  2642. :group 'org-time
  2643. :group 'org-agenda-daily/weekly
  2644. :type 'integer)
  2645. (defcustom org-scheduled-delay-days 0
  2646. "Number of days before a scheduled item becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. The default value (i.e. 0) means: don't delay scheduled item.
  2649. When negative, it means use this number (the absolute value of it)
  2650. even if a scheduled item has a different individual delay time
  2651. specified.
  2652. Custom commands can set this variable in the options section."
  2653. :group 'org-time
  2654. :group 'org-agenda-daily/weekly
  2655. :version "24.4"
  2656. :package-version '(Org . "8.0")
  2657. :type 'integer)
  2658. (defcustom org-read-date-prefer-future t
  2659. "Non-nil means assume future for incomplete date input from user.
  2660. This affects the following situations:
  2661. 1. The user gives a month but not a year.
  2662. For example, if it is April and you enter \"feb 2\", this will be read
  2663. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2664. 2. The user gives a day, but no month.
  2665. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2666. read this as the third of *next* month. However, if you enter \"17\",
  2667. it will be considered as *this* month.
  2668. If you set this variable to the symbol `time', then also the following
  2669. will work:
  2670. 3. If the user gives a time.
  2671. If the time is before now, it will be interpreted as tomorrow.
  2672. Currently none of this works for ISO week specifications.
  2673. When this option is nil, the current day, month and year will always be
  2674. used as defaults.
  2675. See also `org-agenda-jump-prefer-future'."
  2676. :group 'org-time
  2677. :type '(choice
  2678. (const :tag "Never" nil)
  2679. (const :tag "Check month and day" t)
  2680. (const :tag "Check month, day, and time" time)))
  2681. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2682. "Should the agenda jump command prefer the future for incomplete dates?
  2683. The default is to do the same as configured in `org-read-date-prefer-future'.
  2684. But you can also set a deviating value here.
  2685. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2686. :group 'org-agenda
  2687. :group 'org-time
  2688. :version "24.1"
  2689. :type '(choice
  2690. (const :tag "Use org-read-date-prefer-future"
  2691. org-read-date-prefer-future)
  2692. (const :tag "Never" nil)
  2693. (const :tag "Always" t)))
  2694. (defcustom org-read-date-force-compatible-dates t
  2695. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2696. Depending on the system Emacs is running on, certain dates cannot
  2697. be represented with the type used internally to represent time.
  2698. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2699. correctly. Some systems allow for earlier dates, some for later,
  2700. some for both. One way to find out it to insert any date into an
  2701. Org buffer, putting the cursor on the year and hitting S-up and
  2702. S-down to test the range.
  2703. When this variable is set to t, the date/time prompt will not let
  2704. you specify dates outside the 1970-2037 range, so it is certain that
  2705. these dates will work in whatever version of Emacs you are
  2706. running, and also that you can move a file from one Emacs implementation
  2707. to another. WHenever Org is forcing the year for you, it will display
  2708. a message and beep.
  2709. When this variable is nil, Org will check if the date is
  2710. representable in the specific Emacs implementation you are using.
  2711. If not, it will force a year, usually the current year, and beep
  2712. to remind you. Currently this setting is not recommended because
  2713. the likelihood that you will open your Org files in an Emacs that
  2714. has limited date range is not negligible.
  2715. A workaround for this problem is to use diary sexp dates for time
  2716. stamps outside of this range."
  2717. :group 'org-time
  2718. :version "24.1"
  2719. :type 'boolean)
  2720. (defcustom org-read-date-display-live t
  2721. "Non-nil means display current interpretation of date prompt live.
  2722. This display will be in an overlay, in the minibuffer."
  2723. :group 'org-time
  2724. :type 'boolean)
  2725. (defcustom org-read-date-popup-calendar t
  2726. "Non-nil means pop up a calendar when prompting for a date.
  2727. In the calendar, the date can be selected with mouse-1. However, the
  2728. minibuffer will also be active, and you can simply enter the date as well.
  2729. When nil, only the minibuffer will be available."
  2730. :group 'org-time
  2731. :type 'boolean)
  2732. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2733. 'org-read-date-popup-calendar)
  2734. (make-obsolete-variable
  2735. 'org-read-date-minibuffer-setup-hook
  2736. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2737. (defcustom org-read-date-minibuffer-setup-hook nil
  2738. "Hook to be used to set up keys for the date/time interface.
  2739. Add key definitions to `minibuffer-local-map', which will be a
  2740. temporary copy.
  2741. WARNING: This option is obsolete, you should use
  2742. `org-read-date-minibuffer-local-map' to set up keys."
  2743. :group 'org-time
  2744. :type 'hook)
  2745. (defcustom org-extend-today-until 0
  2746. "The hour when your day really ends. Must be an integer.
  2747. This has influence for the following applications:
  2748. - When switching the agenda to \"today\". It it is still earlier than
  2749. the time given here, the day recognized as TODAY is actually yesterday.
  2750. - When a date is read from the user and it is still before the time given
  2751. here, the current date and time will be assumed to be yesterday, 23:59.
  2752. Also, timestamps inserted in capture templates follow this rule.
  2753. IMPORTANT: This is a feature whose implementation is and likely will
  2754. remain incomplete. Really, it is only here because past midnight seems to
  2755. be the favorite working time of John Wiegley :-)"
  2756. :group 'org-time
  2757. :type 'integer)
  2758. (defcustom org-use-effective-time nil
  2759. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2760. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2761. \"effective time\" of any timestamps between midnight and 8am will be
  2762. 23:59 of the previous day."
  2763. :group 'org-time
  2764. :version "24.1"
  2765. :type 'boolean)
  2766. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2767. "When non-nil, use the last clock out time for `org-todo'.
  2768. Note that this option has precedence over the combined use of
  2769. `org-use-effective-time' and `org-extend-today-until'."
  2770. :group 'org-time
  2771. :version "24.4"
  2772. :package-version '(Org . "8.0")
  2773. :type 'boolean)
  2774. (defcustom org-edit-timestamp-down-means-later nil
  2775. "Non-nil means S-down will increase the time in a time stamp.
  2776. When nil, S-up will increase."
  2777. :group 'org-time
  2778. :type 'boolean)
  2779. (defcustom org-calendar-follow-timestamp-change t
  2780. "Non-nil means make the calendar window follow timestamp changes.
  2781. When a timestamp is modified and the calendar window is visible, it will be
  2782. moved to the new date."
  2783. :group 'org-time
  2784. :type 'boolean)
  2785. (defgroup org-tags nil
  2786. "Options concerning tags in Org-mode."
  2787. :tag "Org Tags"
  2788. :group 'org)
  2789. (defcustom org-tag-alist nil
  2790. "List of tags allowed in Org-mode files.
  2791. When this list is nil, Org-mode will base TAG input on what is already in the
  2792. buffer.
  2793. The value of this variable is an alist, the car of each entry must be a
  2794. keyword as a string, the cdr may be a character that is used to select
  2795. that tag through the fast-tag-selection interface.
  2796. See the manual for details."
  2797. :group 'org-tags
  2798. :type '(repeat
  2799. (choice
  2800. (cons (string :tag "Tag name")
  2801. (character :tag "Access char"))
  2802. (list :tag "Start radio group"
  2803. (const :startgroup)
  2804. (option (string :tag "Group description")))
  2805. (list :tag "Group tags delimiter"
  2806. (const :grouptags))
  2807. (list :tag "End radio group"
  2808. (const :endgroup)
  2809. (option (string :tag "Group description")))
  2810. (const :tag "New line" (:newline)))))
  2811. (defcustom org-tag-persistent-alist nil
  2812. "List of tags that will always appear in all Org-mode files.
  2813. This is in addition to any in buffer settings or customizations
  2814. of `org-tag-alist'.
  2815. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2816. The value of this variable is an alist, the car of each entry must be a
  2817. keyword as a string, the cdr may be a character that is used to select
  2818. that tag through the fast-tag-selection interface.
  2819. See the manual for details.
  2820. To disable these tags on a per-file basis, insert anywhere in the file:
  2821. #+STARTUP: noptag"
  2822. :group 'org-tags
  2823. :type '(repeat
  2824. (choice
  2825. (cons (string :tag "Tag name")
  2826. (character :tag "Access char"))
  2827. (const :tag "Start radio group" (:startgroup))
  2828. (const :tag "Group tags delimiter" (:grouptags))
  2829. (const :tag "End radio group" (:endgroup))
  2830. (const :tag "New line" (:newline)))))
  2831. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2832. "If non-nil, always offer completion for all tags of all agenda files.
  2833. Instead of customizing this variable directly, you might want to
  2834. set it locally for capture buffers, because there no list of
  2835. tags in that file can be created dynamically (there are none).
  2836. (add-hook 'org-capture-mode-hook
  2837. (lambda ()
  2838. (set (make-local-variable
  2839. 'org-complete-tags-always-offer-all-agenda-tags)
  2840. t)))"
  2841. :group 'org-tags
  2842. :version "24.1"
  2843. :type 'boolean)
  2844. (defvar org-file-tags nil
  2845. "List of tags that can be inherited by all entries in the file.
  2846. The tags will be inherited if the variable `org-use-tag-inheritance'
  2847. says they should be.
  2848. This variable is populated from #+FILETAGS lines.")
  2849. (defcustom org-use-fast-tag-selection 'auto
  2850. "Non-nil means use fast tag selection scheme.
  2851. This is a special interface to select and deselect tags with single keys.
  2852. When nil, fast selection is never used.
  2853. When the symbol `auto', fast selection is used if and only if selection
  2854. characters for tags have been configured, either through the variable
  2855. `org-tag-alist' or through a #+TAGS line in the buffer.
  2856. When t, fast selection is always used and selection keys are assigned
  2857. automatically if necessary."
  2858. :group 'org-tags
  2859. :type '(choice
  2860. (const :tag "Always" t)
  2861. (const :tag "Never" nil)
  2862. (const :tag "When selection characters are configured" 'auto)))
  2863. (defcustom org-fast-tag-selection-single-key nil
  2864. "Non-nil means fast tag selection exits after first change.
  2865. When nil, you have to press RET to exit it.
  2866. During fast tag selection, you can toggle this flag with `C-c'.
  2867. This variable can also have the value `expert'. In this case, the window
  2868. displaying the tags menu is not even shown, until you press C-c again."
  2869. :group 'org-tags
  2870. :type '(choice
  2871. (const :tag "No" nil)
  2872. (const :tag "Yes" t)
  2873. (const :tag "Expert" expert)))
  2874. (defvar org-fast-tag-selection-include-todo nil
  2875. "Non-nil means fast tags selection interface will also offer TODO states.
  2876. This is an undocumented feature, you should not rely on it.")
  2877. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2878. "The column to which tags should be indented in a headline.
  2879. If this number is positive, it specifies the column. If it is negative,
  2880. it means that the tags should be flushright to that column. For example,
  2881. -80 works well for a normal 80 character screen.
  2882. When 0, place tags directly after headline text, with only one space in
  2883. between."
  2884. :group 'org-tags
  2885. :type 'integer)
  2886. (defcustom org-auto-align-tags t
  2887. "Non-nil keeps tags aligned when modifying headlines.
  2888. Some operations (i.e. demoting) change the length of a headline and
  2889. therefore shift the tags around. With this option turned on, after
  2890. each such operation the tags are again aligned to `org-tags-column'."
  2891. :group 'org-tags
  2892. :type 'boolean)
  2893. (defcustom org-use-tag-inheritance t
  2894. "Non-nil means tags in levels apply also for sublevels.
  2895. When nil, only the tags directly given in a specific line apply there.
  2896. This may also be a list of tags that should be inherited, or a regexp that
  2897. matches tags that should be inherited. Additional control is possible
  2898. with the variable `org-tags-exclude-from-inheritance' which gives an
  2899. explicit list of tags to be excluded from inheritance, even if the value of
  2900. `org-use-tag-inheritance' would select it for inheritance.
  2901. If this option is t, a match early-on in a tree can lead to a large
  2902. number of matches in the subtree when constructing the agenda or creating
  2903. a sparse tree. If you only want to see the first match in a tree during
  2904. a search, check out the variable `org-tags-match-list-sublevels'."
  2905. :group 'org-tags
  2906. :type '(choice
  2907. (const :tag "Not" nil)
  2908. (const :tag "Always" t)
  2909. (repeat :tag "Specific tags" (string :tag "Tag"))
  2910. (regexp :tag "Tags matched by regexp")))
  2911. (defcustom org-tags-exclude-from-inheritance nil
  2912. "List of tags that should never be inherited.
  2913. This is a way to exclude a few tags from inheritance. For way to do
  2914. the opposite, to actively allow inheritance for selected tags,
  2915. see the variable `org-use-tag-inheritance'."
  2916. :group 'org-tags
  2917. :type '(repeat (string :tag "Tag")))
  2918. (defun org-tag-inherit-p (tag)
  2919. "Check if TAG is one that should be inherited."
  2920. (cond
  2921. ((member tag org-tags-exclude-from-inheritance) nil)
  2922. ((eq org-use-tag-inheritance t) t)
  2923. ((not org-use-tag-inheritance) nil)
  2924. ((stringp org-use-tag-inheritance)
  2925. (string-match org-use-tag-inheritance tag))
  2926. ((listp org-use-tag-inheritance)
  2927. (member tag org-use-tag-inheritance))
  2928. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2929. (defcustom org-tags-match-list-sublevels t
  2930. "Non-nil means list also sublevels of headlines matching a search.
  2931. This variable applies to tags/property searches, and also to stuck
  2932. projects because this search is based on a tags match as well.
  2933. When set to the symbol `indented', sublevels are indented with
  2934. leading dots.
  2935. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2936. the sublevels of a headline matching a tag search often also match
  2937. the same search. Listing all of them can create very long lists.
  2938. Setting this variable to nil causes subtrees of a match to be skipped.
  2939. This variable is semi-obsolete and probably should always be true. It
  2940. is better to limit inheritance to certain tags using the variables
  2941. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2942. :group 'org-tags
  2943. :type '(choice
  2944. (const :tag "No, don't list them" nil)
  2945. (const :tag "Yes, do list them" t)
  2946. (const :tag "List them, indented with leading dots" indented)))
  2947. (defcustom org-tags-sort-function nil
  2948. "When set, tags are sorted using this function as a comparator."
  2949. :group 'org-tags
  2950. :type '(choice
  2951. (const :tag "No sorting" nil)
  2952. (const :tag "Alphabetical" string<)
  2953. (const :tag "Reverse alphabetical" string>)
  2954. (function :tag "Custom function" nil)))
  2955. (defvar org-tags-history nil
  2956. "History of minibuffer reads for tags.")
  2957. (defvar org-last-tags-completion-table nil
  2958. "The last used completion table for tags.")
  2959. (defvar org-after-tags-change-hook nil
  2960. "Hook that is run after the tags in a line have changed.")
  2961. (defgroup org-properties nil
  2962. "Options concerning properties in Org-mode."
  2963. :tag "Org Properties"
  2964. :group 'org)
  2965. (defcustom org-property-format "%-10s %s"
  2966. "How property key/value pairs should be formatted by `indent-line'.
  2967. When `indent-line' hits a property definition, it will format the line
  2968. according to this format, mainly to make sure that the values are
  2969. lined-up with respect to each other."
  2970. :group 'org-properties
  2971. :type 'string)
  2972. (defcustom org-properties-postprocess-alist nil
  2973. "Alist of properties and functions to adjust inserted values.
  2974. Elements of this alist must be of the form
  2975. ([string] [function])
  2976. where [string] must be a property name and [function] must be a
  2977. lambda expression: this lambda expression must take one argument,
  2978. the value to adjust, and return the new value as a string.
  2979. For example, this element will allow the property \"Remaining\"
  2980. to be updated wrt the relation between the \"Effort\" property
  2981. and the clock summary:
  2982. ((\"Remaining\" (lambda(value)
  2983. (let ((clocksum (org-clock-sum-current-item))
  2984. (effort (org-duration-string-to-minutes
  2985. (org-entry-get (point) \"Effort\"))))
  2986. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2987. :group 'org-properties
  2988. :version "24.1"
  2989. :type '(alist :key-type (string :tag "Property")
  2990. :value-type (function :tag "Function")))
  2991. (defcustom org-use-property-inheritance nil
  2992. "Non-nil means properties apply also for sublevels.
  2993. This setting is chiefly used during property searches. Turning it on can
  2994. cause significant overhead when doing a search, which is why it is not
  2995. on by default.
  2996. When nil, only the properties directly given in the current entry count.
  2997. When t, every property is inherited. The value may also be a list of
  2998. properties that should have inheritance, or a regular expression matching
  2999. properties that should be inherited.
  3000. However, note that some special properties use inheritance under special
  3001. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3002. and the properties ending in \"_ALL\" when they are used as descriptor
  3003. for valid values of a property.
  3004. Note for programmers:
  3005. When querying an entry with `org-entry-get', you can control if inheritance
  3006. should be used. By default, `org-entry-get' looks only at the local
  3007. properties. You can request inheritance by setting the inherit argument
  3008. to t (to force inheritance) or to `selective' (to respect the setting
  3009. in this variable)."
  3010. :group 'org-properties
  3011. :type '(choice
  3012. (const :tag "Not" nil)
  3013. (const :tag "Always" t)
  3014. (repeat :tag "Specific properties" (string :tag "Property"))
  3015. (regexp :tag "Properties matched by regexp")))
  3016. (defun org-property-inherit-p (property)
  3017. "Check if PROPERTY is one that should be inherited."
  3018. (cond
  3019. ((eq org-use-property-inheritance t) t)
  3020. ((not org-use-property-inheritance) nil)
  3021. ((stringp org-use-property-inheritance)
  3022. (string-match org-use-property-inheritance property))
  3023. ((listp org-use-property-inheritance)
  3024. (member property org-use-property-inheritance))
  3025. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3026. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3027. "The default column format, if no other format has been defined.
  3028. This variable can be set on the per-file basis by inserting a line
  3029. #+COLUMNS: %25ITEM ....."
  3030. :group 'org-properties
  3031. :type 'string)
  3032. (defcustom org-columns-ellipses ".."
  3033. "The ellipses to be used when a field in column view is truncated.
  3034. When this is the empty string, as many characters as possible are shown,
  3035. but then there will be no visual indication that the field has been truncated.
  3036. When this is a string of length N, the last N characters of a truncated
  3037. field are replaced by this string. If the column is narrower than the
  3038. ellipses string, only part of the ellipses string will be shown."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-modify-value-for-display-function nil
  3042. "Function that modifies values for display in column view.
  3043. For example, it can be used to cut out a certain part from a time stamp.
  3044. The function must take 2 arguments:
  3045. column-title The title of the column (*not* the property name)
  3046. value The value that should be modified.
  3047. The function should return the value that should be displayed,
  3048. or nil if the normal value should be used."
  3049. :group 'org-properties
  3050. :type 'function)
  3051. (defcustom org-effort-property "Effort"
  3052. "The property that is being used to keep track of effort estimates.
  3053. Effort estimates given in this property need to have the format H:MM."
  3054. :group 'org-properties
  3055. :group 'org-progress
  3056. :type '(string :tag "Property"))
  3057. (defconst org-global-properties-fixed
  3058. '(("VISIBILITY_ALL" . "folded children content all")
  3059. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3060. "List of property/value pairs that can be inherited by any entry.
  3061. These are fixed values, for the preset properties. The user variable
  3062. that can be used to add to this list is `org-global-properties'.
  3063. The entries in this list are cons cells where the car is a property
  3064. name and cdr is a string with the value. If the value represents
  3065. multiple items like an \"_ALL\" property, separate the items by
  3066. spaces.")
  3067. (defcustom org-global-properties nil
  3068. "List of property/value pairs that can be inherited by any entry.
  3069. This list will be combined with the constant `org-global-properties-fixed'.
  3070. The entries in this list are cons cells where the car is a property
  3071. name and cdr is a string with the value.
  3072. You can set buffer-local values for the same purpose in the variable
  3073. `org-file-properties' this by adding lines like
  3074. #+PROPERTY: NAME VALUE"
  3075. :group 'org-properties
  3076. :type '(repeat
  3077. (cons (string :tag "Property")
  3078. (string :tag "Value"))))
  3079. (defvar org-file-properties nil
  3080. "List of property/value pairs that can be inherited by any entry.
  3081. Valid for the current buffer.
  3082. This variable is populated from #+PROPERTY lines.")
  3083. (make-variable-buffer-local 'org-file-properties)
  3084. (defgroup org-agenda nil
  3085. "Options concerning agenda views in Org-mode."
  3086. :tag "Org Agenda"
  3087. :group 'org)
  3088. (defvar org-category nil
  3089. "Variable used by org files to set a category for agenda display.
  3090. Such files should use a file variable to set it, for example
  3091. # -*- mode: org; org-category: \"ELisp\"
  3092. or contain a special line
  3093. #+CATEGORY: ELisp
  3094. If the file does not specify a category, then file's base name
  3095. is used instead.")
  3096. (make-variable-buffer-local 'org-category)
  3097. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3098. (defcustom org-agenda-files nil
  3099. "The files to be used for agenda display.
  3100. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3101. \\[org-remove-file]. You can also use customize to edit the list.
  3102. If an entry is a directory, all files in that directory that are matched by
  3103. `org-agenda-file-regexp' will be part of the file list.
  3104. If the value of the variable is not a list but a single file name, then
  3105. the list of agenda files is actually stored and maintained in that file, one
  3106. agenda file per line. In this file paths can be given relative to
  3107. `org-directory'. Tilde expansion and environment variable substitution
  3108. are also made."
  3109. :group 'org-agenda
  3110. :type '(choice
  3111. (repeat :tag "List of files and directories" file)
  3112. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3113. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3114. "Regular expression to match files for `org-agenda-files'.
  3115. If any element in the list in that variable contains a directory instead
  3116. of a normal file, all files in that directory that are matched by this
  3117. regular expression will be included."
  3118. :group 'org-agenda
  3119. :type 'regexp)
  3120. (defcustom org-agenda-text-search-extra-files nil
  3121. "List of extra files to be searched by text search commands.
  3122. These files will be search in addition to the agenda files by the
  3123. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3124. Note that these files will only be searched for text search commands,
  3125. not for the other agenda views like todo lists, tag searches or the weekly
  3126. agenda. This variable is intended to list notes and possibly archive files
  3127. that should also be searched by these two commands.
  3128. In fact, if the first element in the list is the symbol `agenda-archives',
  3129. than all archive files of all agenda files will be added to the search
  3130. scope."
  3131. :group 'org-agenda
  3132. :type '(set :greedy t
  3133. (const :tag "Agenda Archives" agenda-archives)
  3134. (repeat :inline t (file))))
  3135. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3136. 'org-agenda-text-search-extra-files)
  3137. (defcustom org-agenda-skip-unavailable-files nil
  3138. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3139. A nil value means to remove them, after a query, from the list."
  3140. :group 'org-agenda
  3141. :type 'boolean)
  3142. (defcustom org-calendar-to-agenda-key [?c]
  3143. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3144. The command `org-calendar-goto-agenda' will be bound to this key. The
  3145. default is the character `c' because then `c' can be used to switch back and
  3146. forth between agenda and calendar."
  3147. :group 'org-agenda
  3148. :type 'sexp)
  3149. (defcustom org-calendar-insert-diary-entry-key [?i]
  3150. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3151. This option is irrelevant until `org-agenda-diary-file' has been configured
  3152. to point to an Org-mode file. When that is the case, the command
  3153. `org-agenda-diary-entry' will be bound to the key given here, by default
  3154. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3155. if you want to continue doing this, you need to change this to a different
  3156. key."
  3157. :group 'org-agenda
  3158. :type 'sexp)
  3159. (defcustom org-agenda-diary-file 'diary-file
  3160. "File to which to add new entries with the `i' key in agenda and calendar.
  3161. When this is the symbol `diary-file', the functionality in the Emacs
  3162. calendar will be used to add entries to the `diary-file'. But when this
  3163. points to a file, `org-agenda-diary-entry' will be used instead."
  3164. :group 'org-agenda
  3165. :type '(choice
  3166. (const :tag "The standard Emacs diary file" diary-file)
  3167. (file :tag "Special Org file diary entries")))
  3168. (eval-after-load "calendar"
  3169. '(progn
  3170. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3171. 'org-calendar-goto-agenda)
  3172. (add-hook 'calendar-mode-hook
  3173. (lambda ()
  3174. (unless (eq org-agenda-diary-file 'diary-file)
  3175. (define-key calendar-mode-map
  3176. org-calendar-insert-diary-entry-key
  3177. 'org-agenda-diary-entry))))))
  3178. (defgroup org-latex nil
  3179. "Options for embedding LaTeX code into Org-mode."
  3180. :tag "Org LaTeX"
  3181. :group 'org)
  3182. (defcustom org-format-latex-options
  3183. '(:foreground default :background default :scale 1.0
  3184. :html-foreground "Black" :html-background "Transparent"
  3185. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3186. "Options for creating images from LaTeX fragments.
  3187. This is a property list with the following properties:
  3188. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3189. `default' means use the foreground of the default face.
  3190. `auto' means use the foreground from the text face.
  3191. :background the background color, or \"Transparent\".
  3192. `default' means use the background of the default face.
  3193. `auto' means use the background from the text face.
  3194. :scale a scaling factor for the size of the images, to get more pixels
  3195. :html-foreground, :html-background, :html-scale
  3196. the same numbers for HTML export.
  3197. :matchers a list indicating which matchers should be used to
  3198. find LaTeX fragments. Valid members of this list are:
  3199. \"begin\" find environments
  3200. \"$1\" find single characters surrounded by $.$
  3201. \"$\" find math expressions surrounded by $...$
  3202. \"$$\" find math expressions surrounded by $$....$$
  3203. \"\\(\" find math expressions surrounded by \\(...\\)
  3204. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3205. :group 'org-latex
  3206. :type 'plist)
  3207. (defcustom org-format-latex-signal-error t
  3208. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3209. When nil, just push out a message."
  3210. :group 'org-latex
  3211. :version "24.1"
  3212. :type 'boolean)
  3213. (defcustom org-latex-to-mathml-jar-file nil
  3214. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3215. Use this to specify additional executable file say a jar file.
  3216. When using MathToWeb as the converter, specify the full-path to
  3217. your mathtoweb.jar file."
  3218. :group 'org-latex
  3219. :version "24.1"
  3220. :type '(choice
  3221. (const :tag "None" nil)
  3222. (file :tag "JAR file" :must-match t)))
  3223. (defcustom org-latex-to-mathml-convert-command nil
  3224. "Command to convert LaTeX fragments to MathML.
  3225. Replace format-specifiers in the command as noted below and use
  3226. `shell-command' to convert LaTeX to MathML.
  3227. %j: Executable file in fully expanded form as specified by
  3228. `org-latex-to-mathml-jar-file'.
  3229. %I: Input LaTeX file in fully expanded form
  3230. %o: Output MathML file
  3231. This command is used by `org-create-math-formula'.
  3232. When using MathToWeb as the converter, set this to
  3233. \"java -jar %j -unicode -force -df %o %I\"."
  3234. :group 'org-latex
  3235. :version "24.1"
  3236. :type '(choice
  3237. (const :tag "None" nil)
  3238. (string :tag "\nShell command")))
  3239. (defcustom org-latex-create-formula-image-program 'dvipng
  3240. "Program to convert LaTeX fragments with.
  3241. dvipng Process the LaTeX fragments to dvi file, then convert
  3242. dvi files to png files using dvipng.
  3243. This will also include processing of non-math environments.
  3244. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3245. to convert pdf files to png files"
  3246. :group 'org-latex
  3247. :version "24.1"
  3248. :type '(choice
  3249. (const :tag "dvipng" dvipng)
  3250. (const :tag "imagemagick" imagemagick)))
  3251. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3252. "Path to store latex preview images.
  3253. A relative path here creates many directories relative to the
  3254. processed org files paths. An absolute path puts all preview
  3255. images at the same place."
  3256. :group 'org-latex
  3257. :version "24.3"
  3258. :type 'string)
  3259. (defun org-format-latex-mathml-available-p ()
  3260. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3261. (save-match-data
  3262. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3263. org-latex-to-mathml-convert-command)
  3264. (let ((executable (car (split-string
  3265. org-latex-to-mathml-convert-command))))
  3266. (when (executable-find executable)
  3267. (if (string-match
  3268. "%j" org-latex-to-mathml-convert-command)
  3269. (file-readable-p org-latex-to-mathml-jar-file)
  3270. t))))))
  3271. (defcustom org-format-latex-header "\\documentclass{article}
  3272. \\usepackage[usenames]{color}
  3273. \\usepackage{amsmath}
  3274. \\usepackage[mathscr]{eucal}
  3275. \\pagestyle{empty} % do not remove
  3276. \[PACKAGES]
  3277. \[DEFAULT-PACKAGES]
  3278. % The settings below are copied from fullpage.sty
  3279. \\setlength{\\textwidth}{\\paperwidth}
  3280. \\addtolength{\\textwidth}{-3cm}
  3281. \\setlength{\\oddsidemargin}{1.5cm}
  3282. \\addtolength{\\oddsidemargin}{-2.54cm}
  3283. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3284. \\setlength{\\textheight}{\\paperheight}
  3285. \\addtolength{\\textheight}{-\\headheight}
  3286. \\addtolength{\\textheight}{-\\headsep}
  3287. \\addtolength{\\textheight}{-\\footskip}
  3288. \\addtolength{\\textheight}{-3cm}
  3289. \\setlength{\\topmargin}{1.5cm}
  3290. \\addtolength{\\topmargin}{-2.54cm}"
  3291. "The document header used for processing LaTeX fragments.
  3292. It is imperative that this header make sure that no page number
  3293. appears on the page. The package defined in the variables
  3294. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3295. will either replace the placeholder \"[PACKAGES]\" in this
  3296. header, or they will be appended."
  3297. :group 'org-latex
  3298. :type 'string)
  3299. (defun org-set-packages-alist (var val)
  3300. "Set the packages alist and make sure it has 3 elements per entry."
  3301. (set var (mapcar (lambda (x)
  3302. (if (and (consp x) (= (length x) 2))
  3303. (list (car x) (nth 1 x) t)
  3304. x))
  3305. val)))
  3306. (defun org-get-packages-alist (var)
  3307. "Get the packages alist and make sure it has 3 elements per entry."
  3308. (mapcar (lambda (x)
  3309. (if (and (consp x) (= (length x) 2))
  3310. (list (car x) (nth 1 x) t)
  3311. x))
  3312. (default-value var)))
  3313. (defcustom org-latex-default-packages-alist
  3314. '(("AUTO" "inputenc" t)
  3315. ("T1" "fontenc" t)
  3316. ("" "fixltx2e" nil)
  3317. ("" "graphicx" t)
  3318. ("" "longtable" nil)
  3319. ("" "float" nil)
  3320. ("" "wrapfig" nil)
  3321. ("" "soul" t)
  3322. ("" "textcomp" t)
  3323. ("" "marvosym" t)
  3324. ("" "wasysym" t)
  3325. ("" "latexsym" t)
  3326. ("" "amssymb" t)
  3327. ("" "hyperref" nil)
  3328. "\\tolerance=1000")
  3329. "Alist of default packages to be inserted in the header.
  3330. Change this only if one of the packages here causes an
  3331. incompatibility with another package you are using.
  3332. The packages in this list are needed by one part or another of
  3333. Org mode to function properly:
  3334. - inputenc, fontenc: for basic font and character selection
  3335. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3336. symbols used for interpreting the entities in `org-entities'.
  3337. You can skip some of these packages if you don't use any of the
  3338. symbols in it.
  3339. - graphicx: for including images
  3340. - float, wrapfig: for figure placement
  3341. - longtable: for long tables
  3342. - hyperref: for cross references
  3343. Therefore you should not modify this variable unless you know
  3344. what you are doing. The one reason to change it anyway is that
  3345. you might be loading some other package that conflicts with one
  3346. of the default packages. Each cell is of the format
  3347. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3348. the package also needs to be included when compiling LaTeX
  3349. snippets into images for inclusion into non-LaTeX output."
  3350. :group 'org-latex
  3351. :group 'org-export-latex
  3352. :set 'org-set-packages-alist
  3353. :get 'org-get-packages-alist
  3354. :version "24.1"
  3355. :type '(repeat
  3356. (choice
  3357. (list :tag "options/package pair"
  3358. (string :tag "options")
  3359. (string :tag "package")
  3360. (boolean :tag "Snippet"))
  3361. (string :tag "A line of LaTeX"))))
  3362. (defcustom org-latex-packages-alist nil
  3363. "Alist of packages to be inserted in every LaTeX header.
  3364. These will be inserted after `org-latex-default-packages-alist'.
  3365. Each cell is of the format:
  3366. \(\"options\" \"package\" snippet-flag)
  3367. SNIPPET-FLAG, when t, indicates that this package is also needed
  3368. when turning LaTeX snippets into images for inclusion into
  3369. non-LaTeX output.
  3370. Make sure that you only list packages here which:
  3371. - you want in every file
  3372. - do not conflict with the setup in `org-format-latex-header'.
  3373. - do not conflict with the default packages in
  3374. `org-latex-default-packages-alist'."
  3375. :group 'org-latex
  3376. :group 'org-export-latex
  3377. :set 'org-set-packages-alist
  3378. :get 'org-get-packages-alist
  3379. :type '(repeat
  3380. (choice
  3381. (list :tag "options/package pair"
  3382. (string :tag "options")
  3383. (string :tag "package")
  3384. (boolean :tag "Snippet"))
  3385. (string :tag "A line of LaTeX"))))
  3386. (defgroup org-appearance nil
  3387. "Settings for Org-mode appearance."
  3388. :tag "Org Appearance"
  3389. :group 'org)
  3390. (defcustom org-level-color-stars-only nil
  3391. "Non-nil means fontify only the stars in each headline.
  3392. When nil, the entire headline is fontified.
  3393. Changing it requires restart of `font-lock-mode' to become effective
  3394. also in regions already fontified."
  3395. :group 'org-appearance
  3396. :type 'boolean)
  3397. (defcustom org-hide-leading-stars nil
  3398. "Non-nil means hide the first N-1 stars in a headline.
  3399. This works by using the face `org-hide' for these stars. This
  3400. face is white for a light background, and black for a dark
  3401. background. You may have to customize the face `org-hide' to
  3402. make this work.
  3403. Changing it requires restart of `font-lock-mode' to become effective
  3404. also in regions already fontified.
  3405. You may also set this on a per-file basis by adding one of the following
  3406. lines to the buffer:
  3407. #+STARTUP: hidestars
  3408. #+STARTUP: showstars"
  3409. :group 'org-appearance
  3410. :type 'boolean)
  3411. (defcustom org-hidden-keywords nil
  3412. "List of symbols corresponding to keywords to be hidden the org buffer.
  3413. For example, a value '(title) for this list will make the document's title
  3414. appear in the buffer without the initial #+TITLE: keyword."
  3415. :group 'org-appearance
  3416. :version "24.1"
  3417. :type '(set (const :tag "#+AUTHOR" author)
  3418. (const :tag "#+DATE" date)
  3419. (const :tag "#+EMAIL" email)
  3420. (const :tag "#+TITLE" title)))
  3421. (defcustom org-custom-properties nil
  3422. "List of properties (as strings) with a special meaning.
  3423. The default use of these custom properties is to let the user
  3424. hide them with `org-toggle-custom-properties-visibility'."
  3425. :group 'org-properties
  3426. :group 'org-appearance
  3427. :version "24.3"
  3428. :type '(repeat (string :tag "Property Name")))
  3429. (defcustom org-fontify-done-headline nil
  3430. "Non-nil means change the face of a headline if it is marked DONE.
  3431. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3432. When this is non-nil, the headline after the keyword is set to the
  3433. `org-headline-done' as an additional indication."
  3434. :group 'org-appearance
  3435. :type 'boolean)
  3436. (defcustom org-fontify-emphasized-text t
  3437. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3438. Changing this variable requires a restart of Emacs to take effect."
  3439. :group 'org-appearance
  3440. :type 'boolean)
  3441. (defcustom org-fontify-whole-heading-line nil
  3442. "Non-nil means fontify the whole line for headings.
  3443. This is useful when setting a background color for the
  3444. org-level-* faces."
  3445. :group 'org-appearance
  3446. :type 'boolean)
  3447. (defcustom org-highlight-latex-and-related nil
  3448. "Non-nil means highlight LaTeX related syntax in the buffer.
  3449. When non nil, the value should be a list containing any of the
  3450. following symbols:
  3451. `latex' Highlight LaTeX snippets and environments.
  3452. `script' Highlight subscript and superscript.
  3453. `entities' Highlight entities."
  3454. :group 'org-appearance
  3455. :version "24.4"
  3456. :package-version '(Org . "8.0")
  3457. :type '(choice
  3458. (const :tag "No highlighting" nil)
  3459. (set :greedy t :tag "Highlight"
  3460. (const :tag "LaTeX snippets and environments" latex)
  3461. (const :tag "Subscript and superscript" script)
  3462. (const :tag "Entities" entities))))
  3463. (defcustom org-hide-emphasis-markers nil
  3464. "Non-nil mean font-lock should hide the emphasis marker characters."
  3465. :group 'org-appearance
  3466. :type 'boolean)
  3467. (defcustom org-pretty-entities nil
  3468. "Non-nil means show entities as UTF8 characters.
  3469. When nil, the \\name form remains in the buffer."
  3470. :group 'org-appearance
  3471. :version "24.1"
  3472. :type 'boolean)
  3473. (defcustom org-pretty-entities-include-sub-superscripts t
  3474. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3475. :group 'org-appearance
  3476. :version "24.1"
  3477. :type 'boolean)
  3478. (defvar org-emph-re nil
  3479. "Regular expression for matching emphasis.
  3480. After a match, the match groups contain these elements:
  3481. 0 The match of the full regular expression, including the characters
  3482. before and after the proper match
  3483. 1 The character before the proper match, or empty at beginning of line
  3484. 2 The proper match, including the leading and trailing markers
  3485. 3 The leading marker like * or /, indicating the type of highlighting
  3486. 4 The text between the emphasis markers, not including the markers
  3487. 5 The character after the match, empty at the end of a line")
  3488. (defvar org-verbatim-re nil
  3489. "Regular expression for matching verbatim text.")
  3490. (defvar org-emphasis-regexp-components) ; defined just below
  3491. (defvar org-emphasis-alist) ; defined just below
  3492. (defun org-set-emph-re (var val)
  3493. "Set variable and compute the emphasis regular expression."
  3494. (set var val)
  3495. (when (and (boundp 'org-emphasis-alist)
  3496. (boundp 'org-emphasis-regexp-components)
  3497. org-emphasis-alist org-emphasis-regexp-components)
  3498. (let* ((e org-emphasis-regexp-components)
  3499. (pre (car e))
  3500. (post (nth 1 e))
  3501. (border (nth 2 e))
  3502. (body (nth 3 e))
  3503. (nl (nth 4 e))
  3504. (body1 (concat body "*?"))
  3505. (markers (mapconcat 'car org-emphasis-alist ""))
  3506. (vmarkers (mapconcat
  3507. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3508. org-emphasis-alist "")))
  3509. ;; make sure special characters appear at the right position in the class
  3510. (if (string-match "\\^" markers)
  3511. (setq markers (concat (replace-match "" t t markers) "^")))
  3512. (if (string-match "-" markers)
  3513. (setq markers (concat (replace-match "" t t markers) "-")))
  3514. (if (string-match "\\^" vmarkers)
  3515. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3516. (if (string-match "-" vmarkers)
  3517. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3518. (if (> nl 0)
  3519. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3520. (int-to-string nl) "\\}")))
  3521. ;; Make the regexp
  3522. (setq org-emph-re
  3523. (concat "\\([" pre "]\\|^\\)"
  3524. "\\("
  3525. "\\([" markers "]\\)"
  3526. "\\("
  3527. "[^" border "]\\|"
  3528. "[^" border "]"
  3529. body1
  3530. "[^" border "]"
  3531. "\\)"
  3532. "\\3\\)"
  3533. "\\([" post "]\\|$\\)"))
  3534. (setq org-verbatim-re
  3535. (concat "\\([" pre "]\\|^\\)"
  3536. "\\("
  3537. "\\([" vmarkers "]\\)"
  3538. "\\("
  3539. "[^" border "]\\|"
  3540. "[^" border "]"
  3541. body1
  3542. "[^" border "]"
  3543. "\\)"
  3544. "\\3\\)"
  3545. "\\([" post "]\\|$\\)")))))
  3546. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3547. ;; set this option proved cumbersome. See this message/thread:
  3548. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3549. (defvar org-emphasis-regexp-components
  3550. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3551. "Components used to build the regular expression for emphasis.
  3552. This is a list with five entries. Terminology: In an emphasis string
  3553. like \" *strong word* \", we call the initial space PREMATCH, the final
  3554. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3555. and \"trong wor\" is the body. The different components in this variable
  3556. specify what is allowed/forbidden in each part:
  3557. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3558. post Chars allowed as postmatch. End of line will be allowed too.
  3559. border The chars *forbidden* as border characters.
  3560. body-regexp A regexp like \".\" to match a body character. Don't use
  3561. non-shy groups here, and don't allow newline here.
  3562. newline The maximum number of newlines allowed in an emphasis exp.
  3563. You need to reload Org or to restart Emacs after customizing this.")
  3564. (defcustom org-emphasis-alist
  3565. `(("*" bold)
  3566. ("/" italic)
  3567. ("_" underline)
  3568. ("=" org-code verbatim)
  3569. ("~" org-verbatim verbatim)
  3570. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3571. "Alist of characters and faces to emphasize text.
  3572. Text starting and ending with a special character will be emphasized,
  3573. for example *bold*, _underlined_ and /italic/. This variable sets the
  3574. marker characters and the face to be used by font-lock for highlighting
  3575. in Org-mode Emacs buffers.
  3576. You need to reload Org or to restart Emacs after customizing this."
  3577. :group 'org-appearance
  3578. :set 'org-set-emph-re
  3579. :version "24.4"
  3580. :package-version '(Org . "8.0")
  3581. :type '(repeat
  3582. (list
  3583. (string :tag "Marker character")
  3584. (choice
  3585. (face :tag "Font-lock-face")
  3586. (plist :tag "Face property list"))
  3587. (option (const verbatim)))))
  3588. (defvar org-protecting-blocks
  3589. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3590. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3591. This is needed for font-lock setup.")
  3592. ;;; Miscellaneous options
  3593. (defgroup org-completion nil
  3594. "Completion in Org-mode."
  3595. :tag "Org Completion"
  3596. :group 'org)
  3597. (defcustom org-completion-use-ido nil
  3598. "Non-nil means use ido completion wherever possible.
  3599. Note that `ido-mode' must be active for this variable to be relevant.
  3600. If you decide to turn this variable on, you might well want to turn off
  3601. `org-outline-path-complete-in-steps'.
  3602. See also `org-completion-use-iswitchb'."
  3603. :group 'org-completion
  3604. :type 'boolean)
  3605. (defcustom org-completion-use-iswitchb nil
  3606. "Non-nil means use iswitchb completion wherever possible.
  3607. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3608. If you decide to turn this variable on, you might well want to turn off
  3609. `org-outline-path-complete-in-steps'.
  3610. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3611. :group 'org-completion
  3612. :type 'boolean)
  3613. (defcustom org-completion-fallback-command 'hippie-expand
  3614. "The expansion command called by \\[pcomplete] in normal context.
  3615. Normal means, no org-mode-specific context."
  3616. :group 'org-completion
  3617. :type 'function)
  3618. ;;; Functions and variables from their packages
  3619. ;; Declared here to avoid compiler warnings
  3620. ;; XEmacs only
  3621. (defvar outline-mode-menu-heading)
  3622. (defvar outline-mode-menu-show)
  3623. (defvar outline-mode-menu-hide)
  3624. (defvar zmacs-regions) ; XEmacs regions
  3625. ;; Emacs only
  3626. (defvar mark-active)
  3627. ;; Various packages
  3628. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3629. (declare-function calendar-forward-day "cal-move" (arg))
  3630. (declare-function calendar-goto-date "cal-move" (date))
  3631. (declare-function calendar-goto-today "cal-move" ())
  3632. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3633. (defvar calc-embedded-close-formula)
  3634. (defvar calc-embedded-open-formula)
  3635. (declare-function cdlatex-tab "ext:cdlatex" ())
  3636. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3637. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3638. (defvar font-lock-unfontify-region-function)
  3639. (declare-function iswitchb-read-buffer "iswitchb"
  3640. (prompt &optional default require-match start matches-set))
  3641. (defvar iswitchb-temp-buflist)
  3642. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3643. (defvar org-agenda-tags-todo-honor-ignore-options)
  3644. (declare-function org-agenda-skip "org-agenda" ())
  3645. (declare-function
  3646. org-agenda-format-item "org-agenda"
  3647. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3648. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3649. (declare-function org-agenda-change-all-lines "org-agenda"
  3650. (newhead hdmarker &optional fixface just-this))
  3651. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3652. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3653. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3654. (beg end))
  3655. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3656. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3657. "org-agenda" (&optional end))
  3658. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3659. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3660. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3661. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3662. (declare-function org-indent-mode "org-indent" (&optional arg))
  3663. (declare-function parse-time-string "parse-time" (string))
  3664. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3665. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3666. (defvar remember-data-file)
  3667. (defvar texmathp-why)
  3668. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3669. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3670. (defvar org-latex-regexps)
  3671. ;;; Autoload and prepare some org modules
  3672. ;; Some table stuff that needs to be defined here, because it is used
  3673. ;; by the functions setting up org-mode or checking for table context.
  3674. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3675. "Detect an org-type or table-type table.")
  3676. (defconst org-table-line-regexp "^[ \t]*|"
  3677. "Detect an org-type table line.")
  3678. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3679. "Detect an org-type table line.")
  3680. (defconst org-table-hline-regexp "^[ \t]*|-"
  3681. "Detect an org-type table hline.")
  3682. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3683. "Detect a table-type table hline.")
  3684. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3685. "Detect the first line outside a table when searching from within it.
  3686. This works for both table types.")
  3687. ;; Autoload the functions in org-table.el that are needed by functions here.
  3688. (eval-and-compile
  3689. (org-autoload "org-table"
  3690. '(org-table-begin org-table-blank-field org-table-end)))
  3691. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3692. "Detect a #+TBLFM line.")
  3693. ;;;###autoload
  3694. (defun turn-on-orgtbl ()
  3695. "Unconditionally turn on `orgtbl-mode'."
  3696. (require 'org-table)
  3697. (orgtbl-mode 1))
  3698. (defun org-at-table-p (&optional table-type)
  3699. "Return t if the cursor is inside an org-type table.
  3700. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3701. (if org-enable-table-editor
  3702. (save-excursion
  3703. (beginning-of-line 1)
  3704. (looking-at (if table-type org-table-any-line-regexp
  3705. org-table-line-regexp)))
  3706. nil))
  3707. (defsubst org-table-p () (org-at-table-p))
  3708. (defun org-at-table.el-p ()
  3709. "Return t if and only if we are at a table.el table."
  3710. (and (org-at-table-p 'any)
  3711. (save-excursion
  3712. (goto-char (org-table-begin 'any))
  3713. (looking-at org-table1-hline-regexp))))
  3714. (defun org-table-recognize-table.el ()
  3715. "If there is a table.el table nearby, recognize it and move into it."
  3716. (if org-table-tab-recognizes-table.el
  3717. (if (org-at-table.el-p)
  3718. (progn
  3719. (beginning-of-line 1)
  3720. (if (looking-at org-table-dataline-regexp)
  3721. nil
  3722. (if (looking-at org-table1-hline-regexp)
  3723. (progn
  3724. (beginning-of-line 2)
  3725. (if (looking-at org-table-any-border-regexp)
  3726. (beginning-of-line -1)))))
  3727. (if (re-search-forward "|" (org-table-end t) t)
  3728. (progn
  3729. (require 'table)
  3730. (if (table--at-cell-p (point))
  3731. t
  3732. (message "recognizing table.el table...")
  3733. (table-recognize-table)
  3734. (message "recognizing table.el table...done")))
  3735. (error "This should not happen"))
  3736. t)
  3737. nil)
  3738. nil))
  3739. (defun org-at-table-hline-p ()
  3740. "Return t if the cursor is inside a hline in a table."
  3741. (if org-enable-table-editor
  3742. (save-excursion
  3743. (beginning-of-line 1)
  3744. (looking-at org-table-hline-regexp))
  3745. nil))
  3746. (defvar org-table-clean-did-remove-column nil)
  3747. (defun org-table-map-tables (function &optional quietly)
  3748. "Apply FUNCTION to the start of all tables in the buffer."
  3749. (save-excursion
  3750. (save-restriction
  3751. (widen)
  3752. (goto-char (point-min))
  3753. (while (re-search-forward org-table-any-line-regexp nil t)
  3754. (unless quietly
  3755. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3756. (beginning-of-line 1)
  3757. (when (and (looking-at org-table-line-regexp)
  3758. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3759. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3760. (save-excursion (funcall function))
  3761. (or (looking-at org-table-line-regexp)
  3762. (forward-char 1)))
  3763. (re-search-forward org-table-any-border-regexp nil 1))))
  3764. (unless quietly (message "Mapping tables: done")))
  3765. ;; Declare and autoload functions from ox.el and al.
  3766. (declare-function org-export-get-environment "ox"
  3767. (&optional backend subtreep ext-plist))
  3768. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3769. ;; Declare and autoload functions from org-agenda.el
  3770. (eval-and-compile
  3771. (org-autoload "org-agenda"
  3772. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3773. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3774. (declare-function org-clock-update-mode-line "org-clock" ())
  3775. (declare-function org-resolve-clocks "org-clock"
  3776. (&optional also-non-dangling-p prompt last-valid))
  3777. (defun org-at-TBLFM-p (&optional pos)
  3778. "Return t when point (or POS) is in #+TBLFM line."
  3779. (save-excursion
  3780. (let ((pos pos)))
  3781. (goto-char (or pos (point)))
  3782. (beginning-of-line 1)
  3783. (looking-at org-TBLFM-regexp)))
  3784. (defvar org-clock-start-time)
  3785. (defvar org-clock-marker (make-marker)
  3786. "Marker recording the last clock-in.")
  3787. (defvar org-clock-hd-marker (make-marker)
  3788. "Marker recording the last clock-in, but the headline position.")
  3789. (defvar org-clock-heading ""
  3790. "The heading of the current clock entry.")
  3791. (defun org-clock-is-active ()
  3792. "Return non-nil if clock is currently running.
  3793. The return value is actually the clock marker."
  3794. (marker-buffer org-clock-marker))
  3795. (eval-and-compile
  3796. (org-autoload "org-clock" '(org-clock-remove-overlays
  3797. org-clock-update-time-maybe
  3798. org-clocktable-shift)))
  3799. (defun org-check-running-clock ()
  3800. "Check if the current buffer contains the running clock.
  3801. If yes, offer to stop it and to save the buffer with the changes."
  3802. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3803. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3804. (buffer-name))))
  3805. (org-clock-out)
  3806. (when (y-or-n-p "Save changed buffer?")
  3807. (save-buffer))))
  3808. (defun org-clocktable-try-shift (dir n)
  3809. "Check if this line starts a clock table, if yes, shift the time block."
  3810. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3811. (org-clocktable-shift dir n)))
  3812. ;;;###autoload
  3813. (defun org-clock-persistence-insinuate ()
  3814. "Set up hooks for clock persistence."
  3815. (require 'org-clock)
  3816. (add-hook 'org-mode-hook 'org-clock-load)
  3817. (add-hook 'kill-emacs-hook 'org-clock-save))
  3818. ;; Define the variable already here, to make sure we have it.
  3819. (defvar org-indent-mode nil
  3820. "Non-nil if Org-Indent mode is enabled.
  3821. Use the command `org-indent-mode' to change this variable.")
  3822. ;; Autoload archiving code
  3823. ;; The stuff that is needed for cycling and tags has to be defined here.
  3824. (defgroup org-archive nil
  3825. "Options concerning archiving in Org-mode."
  3826. :tag "Org Archive"
  3827. :group 'org-structure)
  3828. (defcustom org-archive-location "%s_archive::"
  3829. "The location where subtrees should be archived.
  3830. The value of this variable is a string, consisting of two parts,
  3831. separated by a double-colon. The first part is a filename and
  3832. the second part is a headline.
  3833. When the filename is omitted, archiving happens in the same file.
  3834. %s in the filename will be replaced by the current file
  3835. name (without the directory part). Archiving to a different file
  3836. is useful to keep archived entries from contributing to the
  3837. Org-mode Agenda.
  3838. The archived entries will be filed as subtrees of the specified
  3839. headline. When the headline is omitted, the subtrees are simply
  3840. filed away at the end of the file, as top-level entries. Also in
  3841. the heading you can use %s to represent the file name, this can be
  3842. useful when using the same archive for a number of different files.
  3843. Here are a few examples:
  3844. \"%s_archive::\"
  3845. If the current file is Projects.org, archive in file
  3846. Projects.org_archive, as top-level trees. This is the default.
  3847. \"::* Archived Tasks\"
  3848. Archive in the current file, under the top-level headline
  3849. \"* Archived Tasks\".
  3850. \"~/org/archive.org::\"
  3851. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3852. \"~/org/archive.org::* From %s\"
  3853. Archive in file ~/org/archive.org (absolute path), under headlines
  3854. \"From FILENAME\" where file name is the current file name.
  3855. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3856. The \"datetree/\" string is special, signifying to archive
  3857. items to the datetree. Items are placed in either the CLOSED
  3858. date of the item, or the current date if there is no CLOSED date.
  3859. The heading will be a subentry to the current date. There doesn't
  3860. need to be a heading, but there always needs to be a slash after
  3861. datetree. For example, to store archived items directly in the
  3862. datetree, use \"~/org/datetree.org::datetree/\".
  3863. \"basement::** Finished Tasks\"
  3864. Archive in file ./basement (relative path), as level 3 trees
  3865. below the level 2 heading \"** Finished Tasks\".
  3866. You may set this option on a per-file basis by adding to the buffer a
  3867. line like
  3868. #+ARCHIVE: basement::** Finished Tasks
  3869. You may also define it locally for a subtree by setting an ARCHIVE property
  3870. in the entry. If such a property is found in an entry, or anywhere up
  3871. the hierarchy, it will be used."
  3872. :group 'org-archive
  3873. :type 'string)
  3874. (defcustom org-archive-tag "ARCHIVE"
  3875. "The tag that marks a subtree as archived.
  3876. An archived subtree does not open during visibility cycling, and does
  3877. not contribute to the agenda listings.
  3878. After changing this, font-lock must be restarted in the relevant buffers to
  3879. get the proper fontification."
  3880. :group 'org-archive
  3881. :group 'org-keywords
  3882. :type 'string)
  3883. (defcustom org-agenda-skip-archived-trees t
  3884. "Non-nil means the agenda will skip any items located in archived trees.
  3885. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3886. variable is no longer recommended, you should leave it at the value t.
  3887. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3888. :group 'org-archive
  3889. :group 'org-agenda-skip
  3890. :type 'boolean)
  3891. (defcustom org-columns-skip-archived-trees t
  3892. "Non-nil means ignore archived trees when creating column view."
  3893. :group 'org-archive
  3894. :group 'org-properties
  3895. :type 'boolean)
  3896. (defcustom org-cycle-open-archived-trees nil
  3897. "Non-nil means `org-cycle' will open archived trees.
  3898. An archived tree is a tree marked with the tag ARCHIVE.
  3899. When nil, archived trees will stay folded. You can still open them with
  3900. normal outline commands like `show-all', but not with the cycling commands."
  3901. :group 'org-archive
  3902. :group 'org-cycle
  3903. :type 'boolean)
  3904. (defcustom org-sparse-tree-open-archived-trees nil
  3905. "Non-nil means sparse tree construction shows matches in archived trees.
  3906. When nil, matches in these trees are highlighted, but the trees are kept in
  3907. collapsed state."
  3908. :group 'org-archive
  3909. :group 'org-sparse-trees
  3910. :type 'boolean)
  3911. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3912. "The default date type when building a sparse tree.
  3913. When this is nil, a date is a scheduled or a deadline timestamp.
  3914. Otherwise, these types are allowed:
  3915. all: all timestamps
  3916. active: only active timestamps (<...>)
  3917. inactive: only inactive timestamps (<...)
  3918. scheduled: only scheduled timestamps
  3919. deadline: only deadline timestamps"
  3920. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3921. (const :tag "All timestamps" all)
  3922. (const :tag "Only active timestamps" active)
  3923. (const :tag "Only inactive timestamps" inactive)
  3924. (const :tag "Only scheduled timestamps" scheduled)
  3925. (const :tag "Only deadline timestamps" deadline)
  3926. (const :tag "Only closed timestamps" closed))
  3927. :version "24.3"
  3928. :group 'org-sparse-trees)
  3929. (defun org-cycle-hide-archived-subtrees (state)
  3930. "Re-hide all archived subtrees after a visibility state change."
  3931. (when (and (not org-cycle-open-archived-trees)
  3932. (not (memq state '(overview folded))))
  3933. (save-excursion
  3934. (let* ((globalp (memq state '(contents all)))
  3935. (beg (if globalp (point-min) (point)))
  3936. (end (if globalp (point-max) (org-end-of-subtree t))))
  3937. (org-hide-archived-subtrees beg end)
  3938. (goto-char beg)
  3939. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3940. (message "%s" (substitute-command-keys
  3941. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3942. (defun org-force-cycle-archived ()
  3943. "Cycle subtree even if it is archived."
  3944. (interactive)
  3945. (setq this-command 'org-cycle)
  3946. (let ((org-cycle-open-archived-trees t))
  3947. (call-interactively 'org-cycle)))
  3948. (defun org-hide-archived-subtrees (beg end)
  3949. "Re-hide all archived subtrees after a visibility state change."
  3950. (save-excursion
  3951. (let* ((re (concat ":" org-archive-tag ":")))
  3952. (goto-char beg)
  3953. (while (re-search-forward re end t)
  3954. (when (org-at-heading-p)
  3955. (org-flag-subtree t)
  3956. (org-end-of-subtree t))))))
  3957. (declare-function outline-end-of-heading "outline" ())
  3958. (declare-function outline-flag-region "outline" (from to flag))
  3959. (defun org-flag-subtree (flag)
  3960. (save-excursion
  3961. (org-back-to-heading t)
  3962. (outline-end-of-heading)
  3963. (outline-flag-region (point)
  3964. (progn (org-end-of-subtree t) (point))
  3965. flag)))
  3966. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3967. (eval-and-compile
  3968. (org-autoload "org-archive"
  3969. '(org-add-archive-files)))
  3970. ;; Autoload Column View Code
  3971. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3972. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3973. (declare-function org-columns-compute "org-colview" (property))
  3974. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3975. '(org-columns-number-to-string
  3976. org-columns-get-format-and-top-level
  3977. org-columns-compute
  3978. org-columns-remove-overlays))
  3979. ;; Autoload ID code
  3980. (declare-function org-id-store-link "org-id")
  3981. (declare-function org-id-locations-load "org-id")
  3982. (declare-function org-id-locations-save "org-id")
  3983. (defvar org-id-track-globally)
  3984. (org-autoload "org-id"
  3985. '(org-id-new
  3986. org-id-copy
  3987. org-id-get-with-outline-path-completion
  3988. org-id-get-with-outline-drilling))
  3989. ;;; Variables for pre-computed regular expressions, all buffer local
  3990. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3991. "Matches first line of a hidden block.")
  3992. (make-variable-buffer-local 'org-drawer-regexp)
  3993. (defvar org-todo-regexp nil
  3994. "Matches any of the TODO state keywords.")
  3995. (make-variable-buffer-local 'org-todo-regexp)
  3996. (defvar org-not-done-regexp nil
  3997. "Matches any of the TODO state keywords except the last one.")
  3998. (make-variable-buffer-local 'org-not-done-regexp)
  3999. (defvar org-not-done-heading-regexp nil
  4000. "Matches a TODO headline that is not done.")
  4001. (make-variable-buffer-local 'org-not-done-regexp)
  4002. (defvar org-todo-line-regexp nil
  4003. "Matches a headline and puts TODO state into group 2 if present.")
  4004. (make-variable-buffer-local 'org-todo-line-regexp)
  4005. (defvar org-complex-heading-regexp nil
  4006. "Matches a headline and puts everything into groups:
  4007. group 1: the stars
  4008. group 2: The todo keyword, maybe
  4009. group 3: Priority cookie
  4010. group 4: True headline
  4011. group 5: Tags")
  4012. (make-variable-buffer-local 'org-complex-heading-regexp)
  4013. (defvar org-complex-heading-regexp-format nil
  4014. "Printf format to make regexp to match an exact headline.
  4015. This regexp will match the headline of any node which has the
  4016. exact headline text that is put into the format, but may have any
  4017. TODO state, priority and tags.")
  4018. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4019. (defvar org-todo-line-tags-regexp nil
  4020. "Matches a headline and puts TODO state into group 2 if present.
  4021. Also put tags into group 4 if tags are present.")
  4022. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4023. (defvar org-ds-keyword-length 12
  4024. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4025. (make-variable-buffer-local 'org-ds-keyword-length)
  4026. (defvar org-deadline-regexp nil
  4027. "Matches the DEADLINE keyword.")
  4028. (make-variable-buffer-local 'org-deadline-regexp)
  4029. (defvar org-deadline-time-regexp nil
  4030. "Matches the DEADLINE keyword together with a time stamp.")
  4031. (make-variable-buffer-local 'org-deadline-time-regexp)
  4032. (defvar org-deadline-time-hour-regexp nil
  4033. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4034. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4035. (defvar org-deadline-line-regexp nil
  4036. "Matches the DEADLINE keyword and the rest of the line.")
  4037. (make-variable-buffer-local 'org-deadline-line-regexp)
  4038. (defvar org-scheduled-regexp nil
  4039. "Matches the SCHEDULED keyword.")
  4040. (make-variable-buffer-local 'org-scheduled-regexp)
  4041. (defvar org-scheduled-time-regexp nil
  4042. "Matches the SCHEDULED keyword together with a time stamp.")
  4043. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4044. (defvar org-scheduled-time-hour-regexp nil
  4045. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4046. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4047. (defvar org-closed-time-regexp nil
  4048. "Matches the CLOSED keyword together with a time stamp.")
  4049. (make-variable-buffer-local 'org-closed-time-regexp)
  4050. (defvar org-keyword-time-regexp nil
  4051. "Matches any of the 4 keywords, together with the time stamp.")
  4052. (make-variable-buffer-local 'org-keyword-time-regexp)
  4053. (defvar org-keyword-time-not-clock-regexp nil
  4054. "Matches any of the 3 keywords, together with the time stamp.")
  4055. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4056. (defvar org-maybe-keyword-time-regexp nil
  4057. "Matches a timestamp, possibly preceded by a keyword.")
  4058. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4059. (defvar org-all-time-keywords nil
  4060. "List of time keywords.")
  4061. (make-variable-buffer-local 'org-all-time-keywords)
  4062. (defconst org-plain-time-of-day-regexp
  4063. (concat
  4064. "\\(\\<[012]?[0-9]"
  4065. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4066. "\\(--?"
  4067. "\\(\\<[012]?[0-9]"
  4068. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4069. "\\)?")
  4070. "Regular expression to match a plain time or time range.
  4071. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4072. groups carry important information:
  4073. 0 the full match
  4074. 1 the first time, range or not
  4075. 8 the second time, if it is a range.")
  4076. (defconst org-plain-time-extension-regexp
  4077. (concat
  4078. "\\(\\<[012]?[0-9]"
  4079. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4080. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4081. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4082. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4083. groups carry important information:
  4084. 0 the full match
  4085. 7 hours of duration
  4086. 9 minutes of duration")
  4087. (defconst org-stamp-time-of-day-regexp
  4088. (concat
  4089. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4090. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4091. "\\(--?"
  4092. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4093. "Regular expression to match a timestamp time or time range.
  4094. After a match, the following groups carry important information:
  4095. 0 the full match
  4096. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4097. 2 the first time, range or not
  4098. 4 the second time, if it is a range.")
  4099. (defconst org-startup-options
  4100. '(("fold" org-startup-folded t)
  4101. ("overview" org-startup-folded t)
  4102. ("nofold" org-startup-folded nil)
  4103. ("showall" org-startup-folded nil)
  4104. ("showeverything" org-startup-folded showeverything)
  4105. ("content" org-startup-folded content)
  4106. ("indent" org-startup-indented t)
  4107. ("noindent" org-startup-indented nil)
  4108. ("hidestars" org-hide-leading-stars t)
  4109. ("showstars" org-hide-leading-stars nil)
  4110. ("odd" org-odd-levels-only t)
  4111. ("oddeven" org-odd-levels-only nil)
  4112. ("align" org-startup-align-all-tables t)
  4113. ("noalign" org-startup-align-all-tables nil)
  4114. ("inlineimages" org-startup-with-inline-images t)
  4115. ("noinlineimages" org-startup-with-inline-images nil)
  4116. ("latexpreview" org-startup-with-latex-preview t)
  4117. ("nolatexpreview" org-startup-with-latex-preview nil)
  4118. ("customtime" org-display-custom-times t)
  4119. ("logdone" org-log-done time)
  4120. ("lognotedone" org-log-done note)
  4121. ("nologdone" org-log-done nil)
  4122. ("lognoteclock-out" org-log-note-clock-out t)
  4123. ("nolognoteclock-out" org-log-note-clock-out nil)
  4124. ("logrepeat" org-log-repeat state)
  4125. ("lognoterepeat" org-log-repeat note)
  4126. ("logdrawer" org-log-into-drawer t)
  4127. ("nologdrawer" org-log-into-drawer nil)
  4128. ("logstatesreversed" org-log-states-order-reversed t)
  4129. ("nologstatesreversed" org-log-states-order-reversed nil)
  4130. ("nologrepeat" org-log-repeat nil)
  4131. ("logreschedule" org-log-reschedule time)
  4132. ("lognotereschedule" org-log-reschedule note)
  4133. ("nologreschedule" org-log-reschedule nil)
  4134. ("logredeadline" org-log-redeadline time)
  4135. ("lognoteredeadline" org-log-redeadline note)
  4136. ("nologredeadline" org-log-redeadline nil)
  4137. ("logrefile" org-log-refile time)
  4138. ("lognoterefile" org-log-refile note)
  4139. ("nologrefile" org-log-refile nil)
  4140. ("fninline" org-footnote-define-inline t)
  4141. ("nofninline" org-footnote-define-inline nil)
  4142. ("fnlocal" org-footnote-section nil)
  4143. ("fnauto" org-footnote-auto-label t)
  4144. ("fnprompt" org-footnote-auto-label nil)
  4145. ("fnconfirm" org-footnote-auto-label confirm)
  4146. ("fnplain" org-footnote-auto-label plain)
  4147. ("fnadjust" org-footnote-auto-adjust t)
  4148. ("nofnadjust" org-footnote-auto-adjust nil)
  4149. ("constcgs" constants-unit-system cgs)
  4150. ("constSI" constants-unit-system SI)
  4151. ("noptag" org-tag-persistent-alist nil)
  4152. ("hideblocks" org-hide-block-startup t)
  4153. ("nohideblocks" org-hide-block-startup nil)
  4154. ("beamer" org-startup-with-beamer-mode t)
  4155. ("entitiespretty" org-pretty-entities t)
  4156. ("entitiesplain" org-pretty-entities nil))
  4157. "Variable associated with STARTUP options for org-mode.
  4158. Each element is a list of three items: the startup options (as written
  4159. in the #+STARTUP line), the corresponding variable, and the value to set
  4160. this variable to if the option is found. An optional forth element PUSH
  4161. means to push this value onto the list in the variable.")
  4162. (defun org-update-property-plist (key val props)
  4163. "Update PROPS with KEY and VAL."
  4164. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4165. (key (if appending (substring key 0 (- (length key) 1)) key))
  4166. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4167. (previous (cdr (assoc key props))))
  4168. (if appending
  4169. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4170. (cons (cons key val) remainder))))
  4171. (defconst org-block-regexp
  4172. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4173. "Regular expression for hiding blocks.")
  4174. (defconst org-heading-keyword-regexp-format
  4175. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4176. "Printf format for a regexp matching a headline with some keyword.
  4177. This regexp will match the headline of any node which has the
  4178. exact keyword that is put into the format. The keyword isn't in
  4179. any group by default, but the stars and the body are.")
  4180. (defconst org-heading-keyword-maybe-regexp-format
  4181. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4182. "Printf format for a regexp matching a headline, possibly with some keyword.
  4183. This regexp can match any headline with the specified keyword, or
  4184. without a keyword. The keyword isn't in any group by default,
  4185. but the stars and the body are.")
  4186. (defcustom org-group-tags t
  4187. "When non-nil (the default), use group tags.
  4188. This can be turned on/off through `org-toggle-tags-groups'."
  4189. :group 'org-tags
  4190. :group 'org-startup
  4191. :type 'boolean)
  4192. (defun org-toggle-tags-groups ()
  4193. "Toggle support for group tags.
  4194. Support for group tags is controlled by the option
  4195. `org-group-tags', which is non-nil by default."
  4196. (interactive)
  4197. (setq org-group-tags (not org-group-tags))
  4198. (cond ((and (derived-mode-p 'org-agenda-mode)
  4199. org-group-tags)
  4200. (org-agenda-redo))
  4201. ((derived-mode-p 'org-mode)
  4202. (let ((org-inhibit-startup t)) (org-mode))))
  4203. (message "Groups tags support has been turned %s"
  4204. (if org-group-tags "on" "off")))
  4205. (defun org-set-regexps-and-options-for-tags ()
  4206. "Precompute regular expressions used for tags in the current buffer."
  4207. (when (derived-mode-p 'org-mode)
  4208. (org-set-local 'org-file-tags nil)
  4209. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4210. (splitre "[ \t]+")
  4211. (start 0)
  4212. tags ftags key value)
  4213. (save-excursion
  4214. (save-restriction
  4215. (widen)
  4216. (goto-char (point-min))
  4217. (while (re-search-forward re nil t)
  4218. (setq key (upcase (org-match-string-no-properties 1))
  4219. value (org-match-string-no-properties 2))
  4220. (if (stringp value) (setq value (org-trim value)))
  4221. (cond
  4222. ((equal key "TAGS")
  4223. (setq tags (append tags (if tags '("\\n") nil)
  4224. (org-split-string value splitre))))
  4225. ((equal key "FILETAGS")
  4226. (when (string-match "\\S-" value)
  4227. (setq ftags
  4228. (append
  4229. ftags
  4230. (apply 'append
  4231. (mapcar (lambda (x) (org-split-string x ":"))
  4232. (org-split-string value)))))))))))
  4233. ;; Process the file tags.
  4234. (and ftags (org-set-local 'org-file-tags
  4235. (mapcar 'org-add-prop-inherited ftags)))
  4236. (org-set-local 'org-tag-groups-alist nil)
  4237. ;; Process the tags.
  4238. ;; FIXME
  4239. (when tags
  4240. (let (e tgs g)
  4241. (while (setq e (pop tags))
  4242. (cond
  4243. ((equal e "{")
  4244. (progn (push '(:startgroup) tgs)
  4245. (when (equal (nth 1 tags) ":")
  4246. (push (list (replace-regexp-in-string
  4247. "(.+)$" "" (nth 0 tags)))
  4248. org-tag-groups-alist)
  4249. (setq g 0))))
  4250. ((equal e ":") (push '(:grouptags) tgs))
  4251. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4252. ((equal e "\\n") (push '(:newline) tgs))
  4253. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4254. (push (cons (match-string 1 e)
  4255. (string-to-char (match-string 2 e))) tgs)
  4256. (if (and g (> g 0))
  4257. (setcar org-tag-groups-alist
  4258. (append (car org-tag-groups-alist)
  4259. (list (match-string 1 e)))))
  4260. (if g (setq g (1+ g))))
  4261. (t (push (list e) tgs)
  4262. (if (and g (> g 0))
  4263. (setcar org-tag-groups-alist
  4264. (append (car org-tag-groups-alist) (list e))))
  4265. (if g (setq g (1+ g))))))
  4266. (org-set-local 'org-tag-alist nil)
  4267. (while (setq e (pop tgs))
  4268. (or (and (stringp (car e))
  4269. (assoc (car e) org-tag-alist))
  4270. (push e org-tag-alist))))))))
  4271. (defun org-set-regexps-and-options ()
  4272. "Precompute regular expressions used in the current buffer."
  4273. (when (derived-mode-p 'org-mode)
  4274. (org-set-local 'org-todo-kwd-alist nil)
  4275. (org-set-local 'org-todo-key-alist nil)
  4276. (org-set-local 'org-todo-key-trigger nil)
  4277. (org-set-local 'org-todo-keywords-1 nil)
  4278. (org-set-local 'org-done-keywords nil)
  4279. (org-set-local 'org-todo-heads nil)
  4280. (org-set-local 'org-todo-sets nil)
  4281. (org-set-local 'org-todo-log-states nil)
  4282. (org-set-local 'org-file-properties nil)
  4283. (let ((re (org-make-options-regexp
  4284. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4285. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4286. "SETUPFILE" "OPTIONS")
  4287. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4288. (splitre "[ \t]+")
  4289. (scripts org-use-sub-superscripts)
  4290. kwds kws0 kwsa key log value cat arch const links hw dws
  4291. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4292. (start 0))
  4293. (save-excursion
  4294. (save-restriction
  4295. (widen)
  4296. (goto-char (point-min))
  4297. (while (or (and ext-setup-or-nil
  4298. (let (ret)
  4299. (with-temp-buffer
  4300. (insert ext-setup-or-nil)
  4301. (let ((major-mode 'org-mode))
  4302. (org-set-regexps-and-options-for-tags)
  4303. (setq ret (list org-file-tags org-tag-alist
  4304. org-tag-groups-alist))))
  4305. (setq org-file-tags (nth 0 ret)
  4306. org-tag-alist (nth 1 ret)
  4307. org-tag-groups-alist (nth 2 ret))))
  4308. (and ext-setup-or-nil
  4309. (string-match re ext-setup-or-nil start)
  4310. (setq start (match-end 0)))
  4311. (and (setq ext-setup-or-nil nil start 0)
  4312. (re-search-forward re nil t)))
  4313. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4314. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4315. (if (stringp value) (setq value (org-trim value)))
  4316. (cond
  4317. ((equal key "CATEGORY")
  4318. (setq cat value))
  4319. ((member key '("SEQ_TODO" "TODO"))
  4320. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4321. ((equal key "TYP_TODO")
  4322. (push (cons 'type (org-split-string value splitre)) kwds))
  4323. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4324. ;; general TODO-like setup
  4325. (push (cons (intern (downcase (match-string 1 key)))
  4326. (org-split-string value splitre)) kwds))
  4327. ((equal key "COLUMNS")
  4328. (org-set-local 'org-columns-default-format value))
  4329. ((equal key "LINK")
  4330. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4331. (push (cons (match-string 1 value)
  4332. (org-trim (match-string 2 value)))
  4333. links)))
  4334. ((equal key "PRIORITIES")
  4335. (setq prio (org-split-string value " +")))
  4336. ((equal key "PROPERTY")
  4337. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4338. (setq props (org-update-property-plist (match-string 1 value)
  4339. (match-string 2 value)
  4340. props))))
  4341. ((equal key "DRAWERS")
  4342. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4343. ((equal key "CONSTANTS")
  4344. (org-table-set-constants))
  4345. ((equal key "STARTUP")
  4346. (let ((opts (org-split-string value splitre))
  4347. l var val)
  4348. (while (setq l (pop opts))
  4349. (when (setq l (assoc l org-startup-options))
  4350. (setq var (nth 1 l) val (nth 2 l))
  4351. (if (not (nth 3 l))
  4352. (set (make-local-variable var) val)
  4353. (if (not (listp (symbol-value var)))
  4354. (set (make-local-variable var) nil))
  4355. (set (make-local-variable var) (symbol-value var))
  4356. (add-to-list var val))))))
  4357. ((equal key "ARCHIVE")
  4358. (setq arch value)
  4359. (remove-text-properties 0 (length arch)
  4360. '(face t fontified t) arch))
  4361. ((equal key "OPTIONS")
  4362. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4363. (setq scripts (read (match-string 2 value)))))
  4364. ((and (equal key "SETUPFILE")
  4365. ;; Prevent checking in Gnus messages
  4366. (not buffer-read-only))
  4367. (setq setup-contents (org-file-contents
  4368. (expand-file-name
  4369. (org-remove-double-quotes value))
  4370. 'noerror))
  4371. (if (not ext-setup-or-nil)
  4372. (setq ext-setup-or-nil setup-contents start 0)
  4373. (setq ext-setup-or-nil
  4374. (concat (substring ext-setup-or-nil 0 start)
  4375. "\n" setup-contents "\n"
  4376. (substring ext-setup-or-nil start)))))))
  4377. ;; search for property blocks
  4378. (goto-char (point-min))
  4379. (while (re-search-forward org-block-regexp nil t)
  4380. (when (equal "PROPERTY" (upcase (match-string 1)))
  4381. (setq value (replace-regexp-in-string
  4382. "[\n\r]" " " (match-string 4)))
  4383. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4384. (setq props (org-update-property-plist (match-string 1 value)
  4385. (match-string 2 value)
  4386. props)))))))
  4387. (org-set-local 'org-use-sub-superscripts scripts)
  4388. (when cat
  4389. (org-set-local 'org-category (intern cat))
  4390. (push (cons "CATEGORY" cat) props))
  4391. (when prio
  4392. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4393. (setq prio (mapcar 'string-to-char prio))
  4394. (org-set-local 'org-highest-priority (nth 0 prio))
  4395. (org-set-local 'org-lowest-priority (nth 1 prio))
  4396. (org-set-local 'org-default-priority (nth 2 prio)))
  4397. (and props (org-set-local 'org-file-properties (nreverse props)))
  4398. (and drawers (org-set-local 'org-drawers drawers))
  4399. (and arch (org-set-local 'org-archive-location arch))
  4400. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4401. ;; Process the TODO keywords
  4402. (unless kwds
  4403. ;; Use the global values as if they had been given locally.
  4404. (setq kwds (default-value 'org-todo-keywords))
  4405. (if (stringp (car kwds))
  4406. (setq kwds (list (cons org-todo-interpretation
  4407. (default-value 'org-todo-keywords)))))
  4408. (setq kwds (reverse kwds)))
  4409. (setq kwds (nreverse kwds))
  4410. (let (inter kws kw)
  4411. (while (setq kws (pop kwds))
  4412. (let ((kws (or
  4413. (run-hook-with-args-until-success
  4414. 'org-todo-setup-filter-hook kws)
  4415. kws)))
  4416. (setq inter (pop kws) sep (member "|" kws)
  4417. kws0 (delete "|" (copy-sequence kws))
  4418. kwsa nil
  4419. kws1 (mapcar
  4420. (lambda (x)
  4421. ;; 1 2
  4422. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4423. (progn
  4424. (setq kw (match-string 1 x)
  4425. key (and (match-end 2) (match-string 2 x))
  4426. log (org-extract-log-state-settings x))
  4427. (push (cons kw (and key (string-to-char key))) kwsa)
  4428. (and log (push log org-todo-log-states))
  4429. kw)
  4430. (error "Invalid TODO keyword %s" x)))
  4431. kws0)
  4432. kwsa (if kwsa (append '((:startgroup))
  4433. (nreverse kwsa)
  4434. '((:endgroup))))
  4435. hw (car kws1)
  4436. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4437. tail (list inter hw (car dws) (org-last dws))))
  4438. (add-to-list 'org-todo-heads hw 'append)
  4439. (push kws1 org-todo-sets)
  4440. (setq org-done-keywords (append org-done-keywords dws nil))
  4441. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4442. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4443. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4444. (setq org-todo-sets (nreverse org-todo-sets)
  4445. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4446. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4447. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4448. ;; Compute the regular expressions and other local variables.
  4449. ;; Using `org-outline-regexp-bol' would complicate them much,
  4450. ;; because of the fixed white space at the end of that string.
  4451. (if (not org-done-keywords)
  4452. (setq org-done-keywords (and org-todo-keywords-1
  4453. (list (org-last org-todo-keywords-1)))))
  4454. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4455. (length org-scheduled-string)
  4456. (length org-clock-string)
  4457. (length org-closed-string)))
  4458. org-drawer-regexp
  4459. (concat "^[ \t]*:\\("
  4460. (mapconcat 'regexp-quote org-drawers "\\|")
  4461. "\\):[ \t]*$")
  4462. org-not-done-keywords
  4463. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4464. org-todo-regexp
  4465. (concat "\\("
  4466. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4467. "\\)")
  4468. org-not-done-regexp
  4469. (concat "\\("
  4470. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4471. "\\)")
  4472. org-not-done-heading-regexp
  4473. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4474. org-todo-line-regexp
  4475. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4476. org-complex-heading-regexp
  4477. (concat "^\\(\\*+\\)"
  4478. "\\(?: +" org-todo-regexp "\\)?"
  4479. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4480. "\\(?: +\\(.*?\\)\\)??"
  4481. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4482. "[ \t]*$")
  4483. org-complex-heading-regexp-format
  4484. (concat "^\\(\\*+\\)"
  4485. "\\(?: +" org-todo-regexp "\\)?"
  4486. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4487. "\\(?: +"
  4488. ;; Stats cookies can be stuck to body.
  4489. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4490. "\\(%s\\)"
  4491. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4492. "\\)"
  4493. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4494. "[ \t]*$")
  4495. org-todo-line-tags-regexp
  4496. (concat "^\\(\\*+\\)"
  4497. "\\(?: +" org-todo-regexp "\\)?"
  4498. "\\(?: +\\(.*?\\)\\)??"
  4499. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4500. "[ \t]*$")
  4501. org-deadline-regexp (concat "\\<" org-deadline-string)
  4502. org-deadline-time-regexp
  4503. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4504. org-deadline-time-hour-regexp
  4505. (concat "\\<" org-deadline-string
  4506. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4507. org-deadline-line-regexp
  4508. (concat "\\<\\(" org-deadline-string "\\).*")
  4509. org-scheduled-regexp
  4510. (concat "\\<" org-scheduled-string)
  4511. org-scheduled-time-regexp
  4512. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4513. org-scheduled-time-hour-regexp
  4514. (concat "\\<" org-scheduled-string
  4515. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4516. org-closed-time-regexp
  4517. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4518. org-keyword-time-regexp
  4519. (concat "\\<\\(" org-scheduled-string
  4520. "\\|" org-deadline-string
  4521. "\\|" org-closed-string
  4522. "\\|" org-clock-string "\\)"
  4523. " *[[<]\\([^]>]+\\)[]>]")
  4524. org-keyword-time-not-clock-regexp
  4525. (concat "\\<\\(" org-scheduled-string
  4526. "\\|" org-deadline-string
  4527. "\\|" org-closed-string
  4528. "\\)"
  4529. " *[[<]\\([^]>]+\\)[]>]")
  4530. org-maybe-keyword-time-regexp
  4531. (concat "\\(\\<\\(" org-scheduled-string
  4532. "\\|" org-deadline-string
  4533. "\\|" org-closed-string
  4534. "\\|" org-clock-string "\\)\\)?"
  4535. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4536. org-all-time-keywords
  4537. (mapcar (lambda (w) (substring w 0 -1))
  4538. (list org-scheduled-string org-deadline-string
  4539. org-clock-string org-closed-string)))
  4540. (org-compute-latex-and-related-regexp)
  4541. (org-set-font-lock-defaults))))
  4542. (defun org-file-contents (file &optional noerror)
  4543. "Return the contents of FILE, as a string."
  4544. (if (or (not file)
  4545. (not (file-readable-p file)))
  4546. (if noerror
  4547. (message "Cannot read file \"%s\"" file)
  4548. (error "Cannot read file \"%s\"" file))
  4549. (with-temp-buffer
  4550. (insert-file-contents file)
  4551. (buffer-string))))
  4552. (defun org-extract-log-state-settings (x)
  4553. "Extract the log state setting from a TODO keyword string.
  4554. This will extract info from a string like \"WAIT(w@/!)\"."
  4555. (let (kw key log1 log2)
  4556. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4557. (setq kw (match-string 1 x)
  4558. key (and (match-end 2) (match-string 2 x))
  4559. log1 (and (match-end 3) (match-string 3 x))
  4560. log2 (and (match-end 4) (match-string 4 x)))
  4561. (and (or log1 log2)
  4562. (list kw
  4563. (and log1 (if (equal log1 "!") 'time 'note))
  4564. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4565. (defun org-remove-keyword-keys (list)
  4566. "Remove a pair of parenthesis at the end of each string in LIST."
  4567. (mapcar (lambda (x)
  4568. (if (string-match "(.*)$" x)
  4569. (substring x 0 (match-beginning 0))
  4570. x))
  4571. list))
  4572. (defun org-assign-fast-keys (alist)
  4573. "Assign fast keys to a keyword-key alist.
  4574. Respect keys that are already there."
  4575. (let (new e (alt ?0))
  4576. (while (setq e (pop alist))
  4577. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4578. (cdr e)) ;; Key already assigned.
  4579. (push e new)
  4580. (let ((clist (string-to-list (downcase (car e))))
  4581. (used (append new alist)))
  4582. (when (= (car clist) ?@)
  4583. (pop clist))
  4584. (while (and clist (rassoc (car clist) used))
  4585. (pop clist))
  4586. (unless clist
  4587. (while (rassoc alt used)
  4588. (incf alt)))
  4589. (push (cons (car e) (or (car clist) alt)) new))))
  4590. (nreverse new)))
  4591. ;;; Some variables used in various places
  4592. (defvar org-window-configuration nil
  4593. "Used in various places to store a window configuration.")
  4594. (defvar org-selected-window nil
  4595. "Used in various places to store a window configuration.")
  4596. (defvar org-finish-function nil
  4597. "Function to be called when `C-c C-c' is used.
  4598. This is for getting out of special buffers like capture.")
  4599. ;; FIXME: Occasionally check by commenting these, to make sure
  4600. ;; no other functions uses these, forgetting to let-bind them.
  4601. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4602. (defvar org-last-state)
  4603. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4604. ;; Defined somewhere in this file, but used before definition.
  4605. (defvar org-entities) ;; defined in org-entities.el
  4606. (defvar org-struct-menu)
  4607. (defvar org-org-menu)
  4608. (defvar org-tbl-menu)
  4609. ;;;; Define the Org-mode
  4610. ;; We use a before-change function to check if a table might need
  4611. ;; an update.
  4612. (defvar org-table-may-need-update t
  4613. "Indicates that a table might need an update.
  4614. This variable is set by `org-before-change-function'.
  4615. `org-table-align' sets it back to nil.")
  4616. (defun org-before-change-function (beg end)
  4617. "Every change indicates that a table might need an update."
  4618. (setq org-table-may-need-update t))
  4619. (defvar org-mode-map)
  4620. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4621. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4622. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4623. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4624. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4625. (defvar org-table-buffer-is-an nil)
  4626. (defvar bidi-paragraph-direction)
  4627. (defvar buffer-face-mode-face)
  4628. (require 'outline)
  4629. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4630. (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"))
  4631. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4632. ;; Other stuff we need.
  4633. (require 'time-date)
  4634. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4635. (require 'easymenu)
  4636. (require 'overlay)
  4637. ;; (require 'org-macs) moved higher up in the file before it is first used
  4638. (require 'org-entities)
  4639. ;; (require 'org-compat) moved higher up in the file before it is first used
  4640. (require 'org-faces)
  4641. (require 'org-list)
  4642. (require 'org-pcomplete)
  4643. (require 'org-src)
  4644. (require 'org-footnote)
  4645. (require 'org-macro)
  4646. ;; babel
  4647. (require 'ob)
  4648. ;;;###autoload
  4649. (define-derived-mode org-mode outline-mode "Org"
  4650. "Outline-based notes management and organizer, alias
  4651. \"Carsten's outline-mode for keeping track of everything.\"
  4652. Org-mode develops organizational tasks around a NOTES file which
  4653. contains information about projects as plain text. Org-mode is
  4654. implemented on top of outline-mode, which is ideal to keep the content
  4655. of large files well structured. It supports ToDo items, deadlines and
  4656. time stamps, which magically appear in the diary listing of the Emacs
  4657. calendar. Tables are easily created with a built-in table editor.
  4658. Plain text URL-like links connect to websites, emails (VM), Usenet
  4659. messages (Gnus), BBDB entries, and any files related to the project.
  4660. For printing and sharing of notes, an Org-mode file (or a part of it)
  4661. can be exported as a structured ASCII or HTML file.
  4662. The following commands are available:
  4663. \\{org-mode-map}"
  4664. ;; Get rid of Outline menus, they are not needed
  4665. ;; Need to do this here because define-derived-mode sets up
  4666. ;; the keymap so late. Still, it is a waste to call this each time
  4667. ;; we switch another buffer into org-mode.
  4668. (if (featurep 'xemacs)
  4669. (when (boundp 'outline-mode-menu-heading)
  4670. ;; Assume this is Greg's port, it uses easymenu
  4671. (easy-menu-remove outline-mode-menu-heading)
  4672. (easy-menu-remove outline-mode-menu-show)
  4673. (easy-menu-remove outline-mode-menu-hide))
  4674. (define-key org-mode-map [menu-bar headings] 'undefined)
  4675. (define-key org-mode-map [menu-bar hide] 'undefined)
  4676. (define-key org-mode-map [menu-bar show] 'undefined))
  4677. (org-load-modules-maybe)
  4678. (easy-menu-add org-org-menu)
  4679. (easy-menu-add org-tbl-menu)
  4680. (org-install-agenda-files-menu)
  4681. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4682. (add-to-invisibility-spec '(org-cwidth))
  4683. (add-to-invisibility-spec '(org-hide-block . t))
  4684. (when (featurep 'xemacs)
  4685. (org-set-local 'line-move-ignore-invisible t))
  4686. (org-set-local 'outline-regexp org-outline-regexp)
  4687. (org-set-local 'outline-level 'org-outline-level)
  4688. (setq bidi-paragraph-direction 'left-to-right)
  4689. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4690. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4691. (when (and org-ellipsis
  4692. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4693. (fboundp 'make-glyph-code))
  4694. (unless org-display-table
  4695. (setq org-display-table (make-display-table)))
  4696. (set-display-table-slot
  4697. org-display-table 4
  4698. (vconcat (mapcar
  4699. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4700. org-ellipsis)))
  4701. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4702. (setq buffer-display-table org-display-table))
  4703. (org-set-regexps-and-options-for-tags)
  4704. (org-set-regexps-and-options)
  4705. (when (and org-tag-faces (not org-tags-special-faces-re))
  4706. ;; tag faces set outside customize.... force initialization.
  4707. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4708. ;; Calc embedded
  4709. (org-set-local 'calc-embedded-open-mode "# ")
  4710. ;; Modify a few syntax entries
  4711. (modify-syntax-entry ?@ "w")
  4712. (modify-syntax-entry ?\" "\"")
  4713. (if org-startup-truncated (setq truncate-lines t))
  4714. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4715. (org-set-local 'font-lock-unfontify-region-function
  4716. 'org-unfontify-region)
  4717. ;; Activate before-change-function
  4718. (org-set-local 'org-table-may-need-update t)
  4719. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4720. 'local)
  4721. ;; Check for running clock before killing a buffer
  4722. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4723. ;; Initialize macros templates.
  4724. (org-macro-initialize-templates)
  4725. ;; Initialize radio targets.
  4726. (org-update-radio-target-regexp)
  4727. ;; Indentation.
  4728. (org-set-local 'indent-line-function 'org-indent-line)
  4729. (org-set-local 'indent-region-function 'org-indent-region)
  4730. ;; Filling and auto-filling.
  4731. (org-setup-filling)
  4732. ;; Comments.
  4733. (org-setup-comments-handling)
  4734. ;; Beginning/end of defun
  4735. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4736. (org-set-local 'end-of-defun-function 'org-forward-element)
  4737. ;; Next error for sparse trees
  4738. (org-set-local 'next-error-function 'org-occur-next-match)
  4739. ;; Make sure dependence stuff works reliably, even for users who set it
  4740. ;; too late :-(
  4741. (if org-enforce-todo-dependencies
  4742. (add-hook 'org-blocker-hook
  4743. 'org-block-todo-from-children-or-siblings-or-parent)
  4744. (remove-hook 'org-blocker-hook
  4745. 'org-block-todo-from-children-or-siblings-or-parent))
  4746. (if org-enforce-todo-checkbox-dependencies
  4747. (add-hook 'org-blocker-hook
  4748. 'org-block-todo-from-checkboxes)
  4749. (remove-hook 'org-blocker-hook
  4750. 'org-block-todo-from-checkboxes))
  4751. ;; Align options lines
  4752. (org-set-local
  4753. 'align-mode-rules-list
  4754. '((org-in-buffer-settings
  4755. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4756. (modes . '(org-mode)))))
  4757. ;; Imenu
  4758. (org-set-local 'imenu-create-index-function
  4759. 'org-imenu-get-tree)
  4760. ;; Make isearch reveal context
  4761. (if (or (featurep 'xemacs)
  4762. (not (boundp 'outline-isearch-open-invisible-function)))
  4763. ;; Emacs 21 and XEmacs make use of the hook
  4764. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4765. ;; Emacs 22 deals with this through a special variable
  4766. (org-set-local 'outline-isearch-open-invisible-function
  4767. (lambda (&rest ignore) (org-show-context 'isearch)))
  4768. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4769. ;; Setup the pcomplete hooks
  4770. (set (make-local-variable 'pcomplete-command-completion-function)
  4771. 'org-pcomplete-initial)
  4772. (set (make-local-variable 'pcomplete-command-name-function)
  4773. 'org-command-at-point)
  4774. (set (make-local-variable 'pcomplete-default-completion-function)
  4775. 'ignore)
  4776. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4777. 'org-parse-arguments)
  4778. (set (make-local-variable 'pcomplete-termination-string) "")
  4779. (when (>= emacs-major-version 23)
  4780. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4781. ;; If empty file that did not turn on org-mode automatically, make it to.
  4782. (if (and org-insert-mode-line-in-empty-file
  4783. (org-called-interactively-p 'any)
  4784. (= (point-min) (point-max)))
  4785. (insert "# -*- mode: org -*-\n\n"))
  4786. (unless org-inhibit-startup
  4787. (org-unmodified
  4788. (and org-startup-with-beamer-mode (org-beamer-mode))
  4789. (when org-startup-align-all-tables
  4790. (org-table-map-tables 'org-table-align 'quietly))
  4791. (when org-startup-with-inline-images
  4792. (org-display-inline-images))
  4793. (when org-startup-with-latex-preview
  4794. (org-preview-latex-fragment))
  4795. (unless org-inhibit-startup-visibility-stuff
  4796. (org-set-startup-visibility))))
  4797. ;; Try to set org-hide correctly
  4798. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4799. ;; Update `customize-package-emacs-version-alist'
  4800. (add-to-list 'customize-package-emacs-version-alist
  4801. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4802. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4803. ("8.0" . "24.4")))
  4804. (defvar org-mode-transpose-word-syntax-table
  4805. (let ((st (make-syntax-table)))
  4806. (mapc (lambda(c) (modify-syntax-entry
  4807. (string-to-char (car c)) "w p" st))
  4808. org-emphasis-alist)
  4809. st))
  4810. (when (fboundp 'abbrev-table-put)
  4811. (abbrev-table-put org-mode-abbrev-table
  4812. :parents (list text-mode-abbrev-table)))
  4813. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4814. (defsubst org-fix-ellipsis-at-bol ()
  4815. (save-excursion (goto-char (window-start)) (recenter 0)))
  4816. (defun org-find-invisible-foreground ()
  4817. (let ((candidates (remove
  4818. "unspecified-bg"
  4819. (nconc
  4820. (list (face-background 'default)
  4821. (face-background 'org-default))
  4822. (mapcar
  4823. (lambda (alist)
  4824. (when (boundp alist)
  4825. (cdr (assoc 'background-color (symbol-value alist)))))
  4826. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4827. (list (face-foreground 'org-hide))))))
  4828. (car (remove nil candidates))))
  4829. (defun org-current-time (&optional rounding-minutes past)
  4830. "Current time, possibly rounded to ROUNDING-MINUTES.
  4831. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4832. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4833. the rounding returns a past time."
  4834. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4835. (car org-time-stamp-rounding-minutes)))
  4836. (time (decode-time)) res)
  4837. (if (< r 1)
  4838. (current-time)
  4839. (setq res
  4840. (apply 'encode-time
  4841. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4842. (nthcdr 2 time))))
  4843. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4844. (seconds-to-time (- (org-float-time res) (* r 60)))
  4845. res))))
  4846. (defun org-today ()
  4847. "Return today date, considering `org-extend-today-until'."
  4848. (time-to-days
  4849. (time-subtract (current-time)
  4850. (list 0 (* 3600 org-extend-today-until) 0))))
  4851. ;;;; Font-Lock stuff, including the activators
  4852. (defvar org-mouse-map (make-sparse-keymap))
  4853. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4854. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4855. (when org-mouse-1-follows-link
  4856. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4857. (when org-tab-follows-link
  4858. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4859. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4860. (require 'font-lock)
  4861. (defconst org-non-link-chars "]\t\n\r<>")
  4862. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4863. "shell" "elisp" "doi" "message"))
  4864. (defvar org-link-types-re nil
  4865. "Matches a link that has a url-like prefix like \"http:\"")
  4866. (defvar org-link-re-with-space nil
  4867. "Matches a link with spaces, optional angular brackets around it.")
  4868. (defvar org-link-re-with-space2 nil
  4869. "Matches a link with spaces, optional angular brackets around it.")
  4870. (defvar org-link-re-with-space3 nil
  4871. "Matches a link with spaces, only for internal part in bracket links.")
  4872. (defvar org-angle-link-re nil
  4873. "Matches link with angular brackets, spaces are allowed.")
  4874. (defvar org-plain-link-re nil
  4875. "Matches plain link, without spaces.")
  4876. (defvar org-bracket-link-regexp nil
  4877. "Matches a link in double brackets.")
  4878. (defvar org-bracket-link-analytic-regexp nil
  4879. "Regular expression used to analyze links.
  4880. Here is what the match groups contain after a match:
  4881. 1: http:
  4882. 2: http
  4883. 3: path
  4884. 4: [desc]
  4885. 5: desc")
  4886. (defvar org-bracket-link-analytic-regexp++ nil
  4887. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4888. (defvar org-any-link-re nil
  4889. "Regular expression matching any link.")
  4890. (defconst org-match-sexp-depth 3
  4891. "Number of stacked braces for sub/superscript matching.")
  4892. (defun org-create-multibrace-regexp (left right n)
  4893. "Create a regular expression which will match a balanced sexp.
  4894. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4895. as single character strings.
  4896. The regexp returned will match the entire expression including the
  4897. delimiters. It will also define a single group which contains the
  4898. match except for the outermost delimiters. The maximum depth of
  4899. stacked delimiters is N. Escaping delimiters is not possible."
  4900. (let* ((nothing (concat "[^" left right "]*?"))
  4901. (or "\\|")
  4902. (re nothing)
  4903. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4904. (while (> n 1)
  4905. (setq n (1- n)
  4906. re (concat re or next)
  4907. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4908. (concat left "\\(" re "\\)" right)))
  4909. (defvar org-match-substring-regexp
  4910. (concat
  4911. "\\(\\S-\\)\\([_^]\\)\\("
  4912. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4913. "\\|"
  4914. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4915. "\\|"
  4916. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4917. "The regular expression matching a sub- or superscript.")
  4918. (defvar org-match-substring-with-braces-regexp
  4919. (concat
  4920. "\\(\\S-\\)\\([_^]\\)\\("
  4921. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4922. "\\)")
  4923. "The regular expression matching a sub- or superscript, forcing braces.")
  4924. (defun org-make-link-regexps ()
  4925. "Update the link regular expressions.
  4926. This should be called after the variable `org-link-types' has changed."
  4927. (setq org-link-types-re
  4928. (concat
  4929. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4930. org-link-re-with-space
  4931. (concat
  4932. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4933. "\\([^" org-non-link-chars " ]"
  4934. "[^" org-non-link-chars "]*"
  4935. "[^" org-non-link-chars " ]\\)>?")
  4936. org-link-re-with-space2
  4937. (concat
  4938. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4939. "\\([^" org-non-link-chars " ]"
  4940. "[^\t\n\r]*"
  4941. "[^" org-non-link-chars " ]\\)>?")
  4942. org-link-re-with-space3
  4943. (concat
  4944. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4945. "\\([^" org-non-link-chars " ]"
  4946. "[^\t\n\r]*\\)")
  4947. org-angle-link-re
  4948. (concat
  4949. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4950. "\\([^" org-non-link-chars " ]"
  4951. "[^" org-non-link-chars "]*"
  4952. "\\)>")
  4953. org-plain-link-re
  4954. (concat
  4955. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4956. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4957. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4958. org-bracket-link-regexp
  4959. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4960. org-bracket-link-analytic-regexp
  4961. (concat
  4962. "\\[\\["
  4963. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4964. "\\([^]]+\\)"
  4965. "\\]"
  4966. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4967. "\\]")
  4968. org-bracket-link-analytic-regexp++
  4969. (concat
  4970. "\\[\\["
  4971. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4972. "\\([^]]+\\)"
  4973. "\\]"
  4974. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4975. "\\]")
  4976. org-any-link-re
  4977. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4978. org-angle-link-re "\\)\\|\\("
  4979. org-plain-link-re "\\)")))
  4980. (org-make-link-regexps)
  4981. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4982. "Regular expression for fast time stamp matching.")
  4983. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4984. "Regular expression for fast time stamp matching.")
  4985. (defconst org-ts-regexp0
  4986. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4987. "Regular expression matching time strings for analysis.
  4988. This one does not require the space after the date, so it can be used
  4989. on a string that terminates immediately after the date.")
  4990. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4991. "Regular expression matching time strings for analysis.")
  4992. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4993. "Regular expression matching time stamps, with groups.")
  4994. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4995. "Regular expression matching time stamps (also [..]), with groups.")
  4996. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4997. "Regular expression matching a time stamp range.")
  4998. (defconst org-tr-regexp-both
  4999. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5000. "Regular expression matching a time stamp range.")
  5001. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5002. org-ts-regexp "\\)?")
  5003. "Regular expression matching a time stamp or time stamp range.")
  5004. (defconst org-tsr-regexp-both
  5005. (concat org-ts-regexp-both "\\(--?-?"
  5006. org-ts-regexp-both "\\)?")
  5007. "Regular expression matching a time stamp or time stamp range.
  5008. The time stamps may be either active or inactive.")
  5009. (defvar org-emph-face nil)
  5010. (defun org-do-emphasis-faces (limit)
  5011. "Run through the buffer and add overlays to emphasized strings."
  5012. (let (rtn a)
  5013. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5014. (if (not (= (char-after (match-beginning 3))
  5015. (char-after (match-beginning 4))))
  5016. (progn
  5017. (setq rtn t)
  5018. (setq a (assoc (match-string 3) org-emphasis-alist))
  5019. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5020. 'face
  5021. (nth 1 a))
  5022. (and (nth 4 a)
  5023. (org-remove-flyspell-overlays-in
  5024. (match-beginning 0) (match-end 0)))
  5025. (add-text-properties (match-beginning 2) (match-end 2)
  5026. '(font-lock-multiline t org-emphasis t))
  5027. (when org-hide-emphasis-markers
  5028. (add-text-properties (match-end 4) (match-beginning 5)
  5029. '(invisible org-link))
  5030. (add-text-properties (match-beginning 3) (match-end 3)
  5031. '(invisible org-link)))))
  5032. (backward-char 1))
  5033. rtn))
  5034. (defun org-emphasize (&optional char)
  5035. "Insert or change an emphasis, i.e. a font like bold or italic.
  5036. If there is an active region, change that region to a new emphasis.
  5037. If there is no region, just insert the marker characters and position
  5038. the cursor between them.
  5039. CHAR should be the marker character. If it is a space, it means to
  5040. remove the emphasis of the selected region.
  5041. If CHAR is not given (for example in an interactive call) it will be
  5042. prompted for."
  5043. (interactive)
  5044. (let ((erc org-emphasis-regexp-components)
  5045. (prompt "")
  5046. (string "") beg end move c s)
  5047. (if (org-region-active-p)
  5048. (setq beg (region-beginning) end (region-end)
  5049. string (buffer-substring beg end))
  5050. (setq move t))
  5051. (unless char
  5052. (message "Emphasis marker or tag: [%s]"
  5053. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5054. (setq char (read-char-exclusive)))
  5055. (if (equal char ?\ )
  5056. (setq s "" move nil)
  5057. (unless (assoc (char-to-string char) org-emphasis-alist)
  5058. (user-error "No such emphasis marker: \"%c\"" char))
  5059. (setq s (char-to-string char)))
  5060. (while (and (> (length string) 1)
  5061. (equal (substring string 0 1) (substring string -1))
  5062. (assoc (substring string 0 1) org-emphasis-alist))
  5063. (setq string (substring string 1 -1)))
  5064. (setq string (concat s string s))
  5065. (if beg (delete-region beg end))
  5066. (unless (or (bolp)
  5067. (string-match (concat "[" (nth 0 erc) "\n]")
  5068. (char-to-string (char-before (point)))))
  5069. (insert " "))
  5070. (unless (or (eobp)
  5071. (string-match (concat "[" (nth 1 erc) "\n]")
  5072. (char-to-string (char-after (point)))))
  5073. (insert " ") (backward-char 1))
  5074. (insert string)
  5075. (and move (backward-char 1))))
  5076. (defconst org-nonsticky-props
  5077. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5078. (defsubst org-rear-nonsticky-at (pos)
  5079. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5080. (defun org-activate-plain-links (limit)
  5081. "Run through the buffer and add overlays to links."
  5082. (let (f hl)
  5083. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5084. (not (org-in-src-block-p)))
  5085. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5086. (setq f (get-text-property (match-beginning 0) 'face))
  5087. (setq hl (org-match-string-no-properties 0))
  5088. (if (or (eq f 'org-tag)
  5089. (and (listp f) (memq 'org-tag f)))
  5090. nil
  5091. (add-text-properties (match-beginning 0) (match-end 0)
  5092. (list 'mouse-face 'highlight
  5093. 'face 'org-link
  5094. 'htmlize-link `(:uri ,hl)
  5095. 'keymap org-mouse-map))
  5096. (org-rear-nonsticky-at (match-end 0)))
  5097. t)))
  5098. (defun org-activate-code (limit)
  5099. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5100. (progn
  5101. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5102. (remove-text-properties (match-beginning 0) (match-end 0)
  5103. '(display t invisible t intangible t))
  5104. t)))
  5105. (defcustom org-src-fontify-natively nil
  5106. "When non-nil, fontify code in code blocks."
  5107. :type 'boolean
  5108. :version "24.1"
  5109. :group 'org-appearance
  5110. :group 'org-babel)
  5111. (defcustom org-allow-promoting-top-level-subtree nil
  5112. "When non-nil, allow promoting a top level subtree.
  5113. The leading star of the top level headline will be replaced
  5114. by a #."
  5115. :type 'boolean
  5116. :version "24.1"
  5117. :group 'org-appearance)
  5118. (defun org-fontify-meta-lines-and-blocks (limit)
  5119. (condition-case nil
  5120. (org-fontify-meta-lines-and-blocks-1 limit)
  5121. (error (message "org-mode fontification error"))))
  5122. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5123. "Fontify #+ lines and blocks."
  5124. (let ((case-fold-search t))
  5125. (if (re-search-forward
  5126. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5127. limit t)
  5128. (let ((beg (match-beginning 0))
  5129. (block-start (match-end 0))
  5130. (block-end nil)
  5131. (lang (match-string 7))
  5132. (beg1 (line-beginning-position 2))
  5133. (dc1 (downcase (match-string 2)))
  5134. (dc3 (downcase (match-string 3)))
  5135. end end1 quoting block-type ovl)
  5136. (cond
  5137. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5138. ;; a single line of backend-specific content
  5139. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5140. (remove-text-properties (match-beginning 0) (match-end 0)
  5141. '(display t invisible t intangible t))
  5142. (add-text-properties (match-beginning 1) (match-end 3)
  5143. '(font-lock-fontified t face org-meta-line))
  5144. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5145. '(font-lock-fontified t face org-block))
  5146. ; for backend-specific code
  5147. t)
  5148. ((and (match-end 4) (equal dc3 "+begin"))
  5149. ;; Truly a block
  5150. (setq block-type (downcase (match-string 5))
  5151. quoting (member block-type org-protecting-blocks))
  5152. (when (re-search-forward
  5153. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5154. nil t) ;; on purpose, we look further than LIMIT
  5155. (setq end (min (point-max) (match-end 0))
  5156. end1 (min (point-max) (1- (match-beginning 0))))
  5157. (setq block-end (match-beginning 0))
  5158. (when quoting
  5159. (remove-text-properties beg end
  5160. '(display t invisible t intangible t)))
  5161. (add-text-properties
  5162. beg end
  5163. '(font-lock-fontified t font-lock-multiline t))
  5164. (add-text-properties beg beg1 '(face org-meta-line))
  5165. (add-text-properties end1 (min (point-max) (1+ end))
  5166. '(face org-meta-line)) ; for end_src
  5167. (cond
  5168. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5169. (org-src-font-lock-fontify-block lang block-start block-end)
  5170. ;; remove old background overlays
  5171. (mapc (lambda (ov)
  5172. (if (eq (overlay-get ov 'face) 'org-block-background)
  5173. (delete-overlay ov)))
  5174. (overlays-at (/ (+ beg1 block-end) 2)))
  5175. ;; add a background overlay
  5176. (setq ovl (make-overlay beg1 block-end))
  5177. (overlay-put ovl 'face 'org-block-background)
  5178. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5179. (quoting
  5180. (add-text-properties beg1 (min (point-max) (1+ end1))
  5181. '(face org-block))) ; end of source block
  5182. ((not org-fontify-quote-and-verse-blocks))
  5183. ((string= block-type "quote")
  5184. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5185. ((string= block-type "verse")
  5186. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5187. (add-text-properties beg beg1 '(face org-block-begin-line))
  5188. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5189. '(face org-block-end-line))
  5190. t))
  5191. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5192. (add-text-properties
  5193. beg (match-end 3)
  5194. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5195. '(font-lock-fontified t invisible t)
  5196. '(font-lock-fontified t face org-document-info-keyword)))
  5197. (add-text-properties
  5198. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5199. (if (string-equal dc1 "+title:")
  5200. '(font-lock-fontified t face org-document-title)
  5201. '(font-lock-fontified t face org-document-info))))
  5202. ((or (equal dc1 "+results")
  5203. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5204. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5205. "+call:" "+header:" "+headers:" "+name:"))
  5206. (and (match-end 4) (equal dc3 "+attr")))
  5207. (add-text-properties
  5208. beg (match-end 0)
  5209. '(font-lock-fontified t face org-meta-line))
  5210. t)
  5211. ((member dc3 '(" " ""))
  5212. (add-text-properties
  5213. beg (match-end 0)
  5214. '(font-lock-fontified t face font-lock-comment-face)))
  5215. ((not (member (char-after beg) '(?\ ?\t)))
  5216. ;; just any other in-buffer setting, but not indented
  5217. (add-text-properties
  5218. beg (match-end 0)
  5219. '(font-lock-fontified t face org-meta-line))
  5220. t)
  5221. (t nil))))))
  5222. (defun org-activate-angle-links (limit)
  5223. "Run through the buffer and add overlays to links."
  5224. (if (and (re-search-forward org-angle-link-re limit t)
  5225. (not (org-in-src-block-p)))
  5226. (progn
  5227. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5228. (add-text-properties (match-beginning 0) (match-end 0)
  5229. (list 'mouse-face 'highlight
  5230. 'keymap org-mouse-map))
  5231. (org-rear-nonsticky-at (match-end 0))
  5232. t)))
  5233. (defun org-activate-footnote-links (limit)
  5234. "Run through the buffer and add overlays to footnotes."
  5235. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5236. (when fn
  5237. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5238. (org-remove-flyspell-overlays-in beg end)
  5239. (add-text-properties beg end
  5240. (list 'mouse-face 'highlight
  5241. 'keymap org-mouse-map
  5242. 'help-echo
  5243. (if (= (point-at-bol) beg)
  5244. "Footnote definition"
  5245. "Footnote reference")
  5246. 'font-lock-fontified t
  5247. 'font-lock-multiline t
  5248. 'face 'org-footnote))))))
  5249. (defun org-activate-bracket-links (limit)
  5250. "Run through the buffer and add overlays to bracketed links."
  5251. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5252. (not (org-in-src-block-p)))
  5253. (let* ((hl (org-match-string-no-properties 1))
  5254. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5255. (ip (org-maybe-intangible
  5256. (list 'invisible 'org-link
  5257. 'keymap org-mouse-map 'mouse-face 'highlight
  5258. 'font-lock-multiline t 'help-echo help
  5259. 'htmlize-link `(:uri ,hl))))
  5260. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5261. 'font-lock-multiline t 'help-echo help
  5262. 'htmlize-link `(:uri ,hl))))
  5263. ;; We need to remove the invisible property here. Table narrowing
  5264. ;; may have made some of this invisible.
  5265. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5266. (remove-text-properties (match-beginning 0) (match-end 0)
  5267. '(invisible nil))
  5268. (if (match-end 3)
  5269. (progn
  5270. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5271. (org-rear-nonsticky-at (match-beginning 3))
  5272. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5273. (org-rear-nonsticky-at (match-end 3))
  5274. (add-text-properties (match-end 3) (match-end 0) ip)
  5275. (org-rear-nonsticky-at (match-end 0)))
  5276. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5277. (org-rear-nonsticky-at (match-beginning 1))
  5278. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5279. (org-rear-nonsticky-at (match-end 1))
  5280. (add-text-properties (match-end 1) (match-end 0) ip)
  5281. (org-rear-nonsticky-at (match-end 0)))
  5282. t)))
  5283. (defun org-activate-dates (limit)
  5284. "Run through the buffer and add overlays to dates."
  5285. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5286. (not (equal (char-before (match-beginning 0)) 91)))
  5287. (progn
  5288. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5289. (add-text-properties (match-beginning 0) (match-end 0)
  5290. (list 'mouse-face 'highlight
  5291. 'keymap org-mouse-map))
  5292. (org-rear-nonsticky-at (match-end 0))
  5293. (when org-display-custom-times
  5294. (if (match-end 3)
  5295. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5296. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5297. t)))
  5298. (defvar org-target-link-regexp nil
  5299. "Regular expression matching radio targets in plain text.")
  5300. (make-variable-buffer-local 'org-target-link-regexp)
  5301. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5302. "Regular expression matching a link target.")
  5303. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5304. "Regular expression matching a radio target.")
  5305. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5306. "Regular expression matching any target.")
  5307. (defun org-activate-target-links (limit)
  5308. "Run through the buffer and add overlays to target matches."
  5309. (when org-target-link-regexp
  5310. (let ((case-fold-search t))
  5311. (if (re-search-forward org-target-link-regexp limit t)
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map
  5317. 'help-echo "Radio target link"
  5318. 'org-linked-text t))
  5319. (org-rear-nonsticky-at (match-end 0))
  5320. t)))))
  5321. (defun org-update-radio-target-regexp ()
  5322. "Find all radio targets in this file and update the regular expression."
  5323. (interactive)
  5324. (when (memq 'radio org-activate-links)
  5325. (setq org-target-link-regexp
  5326. (org-make-target-link-regexp (org-all-targets 'radio)))
  5327. (org-restart-font-lock)))
  5328. (defun org-hide-wide-columns (limit)
  5329. (let (s e)
  5330. (setq s (text-property-any (point) (or limit (point-max))
  5331. 'org-cwidth t))
  5332. (when s
  5333. (setq e (next-single-property-change s 'org-cwidth))
  5334. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5335. (goto-char e)
  5336. t)))
  5337. (defvar org-latex-and-related-regexp nil
  5338. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5339. (defvar org-match-substring-regexp)
  5340. (defvar org-match-substring-with-braces-regexp)
  5341. (defun org-compute-latex-and-related-regexp ()
  5342. "Compute regular expression for LaTeX, entities and sub/superscript.
  5343. Result depends on variable `org-highlight-latex-and-related'."
  5344. (org-set-local
  5345. 'org-latex-and-related-regexp
  5346. (let* ((re-sub
  5347. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5348. ((eq org-use-sub-superscripts '{})
  5349. (list org-match-substring-with-braces-regexp))
  5350. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5351. (re-latex
  5352. (when (memq 'latex org-highlight-latex-and-related)
  5353. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5354. (delq nil
  5355. (mapcar (lambda (x)
  5356. (and (member (car x) matchers) (nth 1 x)))
  5357. org-latex-regexps)))))
  5358. (re-entities
  5359. (when (memq 'entities org-highlight-latex-and-related)
  5360. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5361. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5362. (defun org-do-latex-and-related (limit)
  5363. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5364. LIMIT bounds the search for syntax to highlight. Stop at first
  5365. highlighted object, if any. Return t if some highlighting was
  5366. done, nil otherwise."
  5367. (when (org-string-nw-p org-latex-and-related-regexp)
  5368. (catch 'found
  5369. (while (re-search-forward org-latex-and-related-regexp limit t)
  5370. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5371. 'face))
  5372. '(org-code org-verbatim underline))
  5373. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5374. '(?_ ?^))
  5375. 1
  5376. 0)))
  5377. (font-lock-prepend-text-property
  5378. (+ offset (match-beginning 0)) (match-end 0)
  5379. 'face 'org-latex-and-related)
  5380. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5381. '(font-lock-multiline t)))
  5382. (throw 'found t)))
  5383. nil)))
  5384. (defun org-restart-font-lock ()
  5385. "Restart `font-lock-mode', to force refontification."
  5386. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5387. (font-lock-mode -1)
  5388. (font-lock-mode 1)))
  5389. (defun org-all-targets (&optional radio)
  5390. "Return a list of all targets in this file.
  5391. When optional argument RADIO is non-nil, only find radio
  5392. targets."
  5393. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5394. (save-excursion
  5395. (goto-char (point-min))
  5396. (while (re-search-forward re nil t)
  5397. ;; Make sure point is really within the object.
  5398. (backward-char)
  5399. (let ((obj (org-element-context)))
  5400. (when (memq (org-element-type obj) '(radio-target target))
  5401. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5402. rtn)))
  5403. (defun org-make-target-link-regexp (targets)
  5404. "Make regular expression matching all strings in TARGETS.
  5405. The regular expression finds the targets also if there is a line break
  5406. between words."
  5407. (and targets
  5408. (concat
  5409. "\\<\\("
  5410. (mapconcat
  5411. (lambda (x)
  5412. (setq x (regexp-quote x))
  5413. (while (string-match " +" x)
  5414. (setq x (replace-match "\\s-+" t t x)))
  5415. x)
  5416. targets
  5417. "\\|")
  5418. "\\)\\>")))
  5419. (defun org-activate-tags (limit)
  5420. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5421. (progn
  5422. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5423. (add-text-properties (match-beginning 1) (match-end 1)
  5424. (list 'mouse-face 'highlight
  5425. 'keymap org-mouse-map))
  5426. (org-rear-nonsticky-at (match-end 1))
  5427. t)))
  5428. (defun org-outline-level ()
  5429. "Compute the outline level of the heading at point.
  5430. If this is called at a normal headline, the level is the number of stars.
  5431. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5432. (save-excursion
  5433. (if (not (condition-case nil
  5434. (org-back-to-heading t)
  5435. (error nil)))
  5436. 0
  5437. (looking-at org-outline-regexp)
  5438. (1- (- (match-end 0) (match-beginning 0))))))
  5439. (defvar org-font-lock-keywords nil)
  5440. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5441. "Regular expression matching a property line.")
  5442. (defvar org-font-lock-hook nil
  5443. "Functions to be called for special font lock stuff.")
  5444. (defvar org-font-lock-set-keywords-hook nil
  5445. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5446. This is called after `org-font-lock-extra-keywords' is defined, but before
  5447. it is installed to be used by font lock. This can be useful if something
  5448. needs to be inserted at a specific position in the font-lock sequence.")
  5449. (defun org-font-lock-hook (limit)
  5450. "Run `org-font-lock-hook' within LIMIT."
  5451. (run-hook-with-args 'org-font-lock-hook limit))
  5452. (defun org-set-font-lock-defaults ()
  5453. "Set font lock defaults for the current buffer."
  5454. (let* ((em org-fontify-emphasized-text)
  5455. (lk org-activate-links)
  5456. (org-font-lock-extra-keywords
  5457. (list
  5458. ;; Call the hook
  5459. '(org-font-lock-hook)
  5460. ;; Headlines
  5461. `(,(if org-fontify-whole-heading-line
  5462. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5463. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5464. (1 (org-get-level-face 1))
  5465. (2 (org-get-level-face 2))
  5466. (3 (org-get-level-face 3)))
  5467. ;; Table lines
  5468. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5469. (1 'org-table t))
  5470. ;; Table internals
  5471. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5472. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5473. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5474. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5475. ;; Drawers
  5476. (list org-drawer-regexp '(0 'org-special-keyword t))
  5477. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5478. ;; Properties
  5479. (list org-property-re
  5480. '(1 'org-special-keyword t)
  5481. '(3 'org-property-value t))
  5482. ;; Links
  5483. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5484. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5485. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5486. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5487. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5488. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5489. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5490. ;; Targets.
  5491. (list org-any-target-regexp '(0 'org-target t))
  5492. ;; Diary sexps.
  5493. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5494. ;; Macro
  5495. '("{{{.+}}}" (0 'org-macro t))
  5496. '(org-hide-wide-columns (0 nil append))
  5497. ;; TODO keyword
  5498. (list (format org-heading-keyword-regexp-format
  5499. org-todo-regexp)
  5500. '(2 (org-get-todo-face 2) t))
  5501. ;; DONE
  5502. (if org-fontify-done-headline
  5503. (list (format org-heading-keyword-regexp-format
  5504. (concat
  5505. "\\(?:"
  5506. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5507. "\\)"))
  5508. '(2 'org-headline-done t))
  5509. nil)
  5510. ;; Priorities
  5511. '(org-font-lock-add-priority-faces)
  5512. ;; Tags
  5513. '(org-font-lock-add-tag-faces)
  5514. ;; Tags groups
  5515. (if (and org-group-tags org-tag-groups-alist)
  5516. (list (concat org-outline-regexp-bol ".+\\(:"
  5517. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5518. ":\\).*$")
  5519. '(1 'org-tag-group prepend)))
  5520. ;; Special keywords
  5521. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5522. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5523. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5524. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5525. ;; Emphasis
  5526. (if em
  5527. (if (featurep 'xemacs)
  5528. '(org-do-emphasis-faces (0 nil append))
  5529. '(org-do-emphasis-faces)))
  5530. ;; Checkboxes
  5531. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5532. 1 'org-checkbox prepend)
  5533. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5534. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5535. (0 (org-get-checkbox-statistics-face) t)))
  5536. ;; Description list items
  5537. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5538. 1 'org-list-dt prepend)
  5539. ;; ARCHIVEd headings
  5540. (list (concat
  5541. org-outline-regexp-bol
  5542. "\\(.*:" org-archive-tag ":.*\\)")
  5543. '(1 'org-archived prepend))
  5544. ;; Specials
  5545. '(org-do-latex-and-related)
  5546. '(org-fontify-entities)
  5547. '(org-raise-scripts)
  5548. ;; Code
  5549. '(org-activate-code (1 'org-code t))
  5550. ;; COMMENT
  5551. (list (format org-heading-keyword-regexp-format
  5552. (concat "\\("
  5553. org-comment-string "\\|" org-quote-string
  5554. "\\)"))
  5555. '(2 'org-special-keyword t))
  5556. ;; Blocks and meta lines
  5557. '(org-fontify-meta-lines-and-blocks))))
  5558. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5559. (run-hooks 'org-font-lock-set-keywords-hook)
  5560. ;; Now set the full font-lock-keywords
  5561. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5562. (org-set-local 'font-lock-defaults
  5563. '(org-font-lock-keywords t nil nil backward-paragraph))
  5564. (kill-local-variable 'font-lock-keywords) nil))
  5565. (defun org-toggle-pretty-entities ()
  5566. "Toggle the composition display of entities as UTF8 characters."
  5567. (interactive)
  5568. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5569. (org-restart-font-lock)
  5570. (if org-pretty-entities
  5571. (message "Entities are now displayed as UTF8 characters")
  5572. (save-restriction
  5573. (widen)
  5574. (org-decompose-region (point-min) (point-max))
  5575. (message "Entities are now displayed as plain text"))))
  5576. (defvar org-custom-properties-overlays nil
  5577. "List of overlays used for custom properties.")
  5578. (make-variable-buffer-local 'org-custom-properties-overlays)
  5579. (defun org-toggle-custom-properties-visibility ()
  5580. "Display or hide properties in `org-custom-properties'."
  5581. (interactive)
  5582. (if org-custom-properties-overlays
  5583. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5584. (setq org-custom-properties-overlays nil))
  5585. (unless (not org-custom-properties)
  5586. (save-excursion
  5587. (save-restriction
  5588. (widen)
  5589. (goto-char (point-min))
  5590. (while (re-search-forward org-property-re nil t)
  5591. (mapc (lambda(p)
  5592. (when (equal p (substring (match-string 1) 1 -1))
  5593. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5594. (overlay-put o 'invisible t)
  5595. (overlay-put o 'org-custom-property t)
  5596. (push o org-custom-properties-overlays))))
  5597. org-custom-properties)))))))
  5598. (defun org-fontify-entities (limit)
  5599. "Find an entity to fontify."
  5600. (let (ee)
  5601. (when org-pretty-entities
  5602. (catch 'match
  5603. (while (re-search-forward
  5604. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5605. limit t)
  5606. (if (and (not (org-in-indented-comment-line))
  5607. (setq ee (org-entity-get (match-string 1)))
  5608. (= (length (nth 6 ee)) 1))
  5609. (let*
  5610. ((end (if (equal (match-string 2) "{}")
  5611. (match-end 2)
  5612. (match-end 1))))
  5613. (add-text-properties
  5614. (match-beginning 0) end
  5615. (list 'font-lock-fontified t))
  5616. (compose-region (match-beginning 0) end
  5617. (nth 6 ee) nil)
  5618. (backward-char 1)
  5619. (throw 'match t))))
  5620. nil))))
  5621. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5622. "Fontify string S like in Org-mode."
  5623. (with-temp-buffer
  5624. (insert s)
  5625. (let ((org-odd-levels-only odd-levels))
  5626. (org-mode)
  5627. (font-lock-fontify-buffer)
  5628. (buffer-string))))
  5629. (defvar org-m nil)
  5630. (defvar org-l nil)
  5631. (defvar org-f nil)
  5632. (defun org-get-level-face (n)
  5633. "Get the right face for match N in font-lock matching of headlines."
  5634. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5635. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5636. (if org-cycle-level-faces
  5637. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5638. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5639. (cond
  5640. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5641. ((eq n 2) org-f)
  5642. (t (if org-level-color-stars-only nil org-f))))
  5643. (defun org-get-todo-face (kwd)
  5644. "Get the right face for a TODO keyword KWD.
  5645. If KWD is a number, get the corresponding match group."
  5646. (if (numberp kwd) (setq kwd (match-string kwd)))
  5647. (or (org-face-from-face-or-color
  5648. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5649. (and (member kwd org-done-keywords) 'org-done)
  5650. 'org-todo))
  5651. (defun org-face-from-face-or-color (context inherit face-or-color)
  5652. "Create a face list that inherits INHERIT, but sets the foreground color.
  5653. When FACE-OR-COLOR is not a string, just return it."
  5654. (if (stringp face-or-color)
  5655. (list :inherit inherit
  5656. (cdr (assoc context org-faces-easy-properties))
  5657. face-or-color)
  5658. face-or-color))
  5659. (defun org-font-lock-add-tag-faces (limit)
  5660. "Add the special tag faces."
  5661. (when (and org-tag-faces org-tags-special-faces-re)
  5662. (while (re-search-forward org-tags-special-faces-re limit t)
  5663. (add-text-properties (match-beginning 1) (match-end 1)
  5664. (list 'face (org-get-tag-face 1)
  5665. 'font-lock-fontified t))
  5666. (backward-char 1))))
  5667. (defun org-font-lock-add-priority-faces (limit)
  5668. "Add the special priority faces."
  5669. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5670. (when (save-match-data (org-at-heading-p))
  5671. (add-text-properties
  5672. (match-beginning 0) (match-end 0)
  5673. (list 'face (or (org-face-from-face-or-color
  5674. 'priority 'org-priority
  5675. (cdr (assoc (char-after (match-beginning 1))
  5676. org-priority-faces)))
  5677. 'org-priority)
  5678. 'font-lock-fontified t)))))
  5679. (defun org-get-tag-face (kwd)
  5680. "Get the right face for a TODO keyword KWD.
  5681. If KWD is a number, get the corresponding match group."
  5682. (if (numberp kwd) (setq kwd (match-string kwd)))
  5683. (or (org-face-from-face-or-color
  5684. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5685. 'org-tag))
  5686. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5687. "Remove fontification and activation overlays from links."
  5688. (font-lock-default-unfontify-region beg end)
  5689. (let* ((buffer-undo-list t)
  5690. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5691. (inhibit-modification-hooks t)
  5692. deactivate-mark buffer-file-name buffer-file-truename)
  5693. (org-decompose-region beg end)
  5694. (remove-text-properties beg end
  5695. '(mouse-face t keymap t org-linked-text t
  5696. invisible t intangible t
  5697. org-no-flyspell t org-emphasis t))
  5698. (org-remove-font-lock-display-properties beg end)))
  5699. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5700. ((raise 0.3) (height 0.7))
  5701. ((raise -0.5))
  5702. ((raise 0.5)))
  5703. "Display properties for showing superscripts and subscripts.")
  5704. (defun org-remove-font-lock-display-properties (beg end)
  5705. "Remove specific display properties that have been added by font lock.
  5706. The will remove the raise properties that are used to show superscripts
  5707. and subscripts."
  5708. (let (next prop)
  5709. (while (< beg end)
  5710. (setq next (next-single-property-change beg 'display nil end)
  5711. prop (get-text-property beg 'display))
  5712. (if (member prop org-script-display)
  5713. (put-text-property beg next 'display nil))
  5714. (setq beg next))))
  5715. (defun org-raise-scripts (limit)
  5716. "Add raise properties to sub/superscripts."
  5717. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5718. (if (re-search-forward
  5719. (if (eq org-use-sub-superscripts t)
  5720. org-match-substring-regexp
  5721. org-match-substring-with-braces-regexp)
  5722. limit t)
  5723. (let* ((pos (point)) table-p comment-p
  5724. (mpos (match-beginning 3))
  5725. (emph-p (get-text-property mpos 'org-emphasis))
  5726. (link-p (get-text-property mpos 'mouse-face))
  5727. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5728. (goto-char (point-at-bol))
  5729. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5730. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5731. (goto-char pos)
  5732. ;; Handle a_b^c
  5733. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5734. (if (or comment-p emph-p link-p keyw-p)
  5735. t
  5736. (put-text-property (match-beginning 3) (match-end 0)
  5737. 'display
  5738. (if (equal (char-after (match-beginning 2)) ?^)
  5739. (nth (if table-p 3 1) org-script-display)
  5740. (nth (if table-p 2 0) org-script-display)))
  5741. (add-text-properties (match-beginning 2) (match-end 2)
  5742. (list 'invisible t
  5743. 'org-dwidth t 'org-dwidth-n 1))
  5744. (if (and (eq (char-after (match-beginning 3)) ?{)
  5745. (eq (char-before (match-end 3)) ?}))
  5746. (progn
  5747. (add-text-properties
  5748. (match-beginning 3) (1+ (match-beginning 3))
  5749. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5750. (add-text-properties
  5751. (1- (match-end 3)) (match-end 3)
  5752. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5753. t)))))
  5754. ;;;; Visibility cycling, including org-goto and indirect buffer
  5755. ;;; Cycling
  5756. (defvar org-cycle-global-status nil)
  5757. (make-variable-buffer-local 'org-cycle-global-status)
  5758. (put 'org-cycle-global-status 'org-state t)
  5759. (defvar org-cycle-subtree-status nil)
  5760. (make-variable-buffer-local 'org-cycle-subtree-status)
  5761. (put 'org-cycle-subtree-status 'org-state t)
  5762. (defvar org-inlinetask-min-level)
  5763. ;;;###autoload
  5764. (defun org-cycle (&optional arg)
  5765. "TAB-action and visibility cycling for Org-mode.
  5766. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5767. is outline visibility cycling, but it also invokes other actions
  5768. in special contexts.
  5769. - When this function is called with a prefix argument, rotate the entire
  5770. buffer through 3 states (global cycling)
  5771. 1. OVERVIEW: Show only top-level headlines.
  5772. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5773. 3. SHOW ALL: Show everything.
  5774. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5775. determined by the variable `org-startup-folded', and by any VISIBILITY
  5776. properties in the buffer.
  5777. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5778. including any drawers.
  5779. - When inside a table, re-align the table and move to the next field.
  5780. - When point is at the beginning of a headline, rotate the subtree started
  5781. by this line through 3 different states (local cycling)
  5782. 1. FOLDED: Only the main headline is shown.
  5783. 2. CHILDREN: The main headline and the direct children are shown.
  5784. From this state, you can move to one of the children
  5785. and zoom in further.
  5786. 3. SUBTREE: Show the entire subtree, including body text.
  5787. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5788. - When point is at the beginning of an empty headline and the variable
  5789. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5790. of the headline by demoting and promoting it to likely levels. This
  5791. speeds up creation document structure by pressing TAB once or several
  5792. times right after creating a new headline.
  5793. - When there is a numeric prefix, go up to a heading with level ARG, do
  5794. a `show-subtree' and return to the previous cursor position. If ARG
  5795. is negative, go up that many levels.
  5796. - When point is not at the beginning of a headline, execute the global
  5797. binding for TAB, which is re-indenting the line. See the option
  5798. `org-cycle-emulate-tab' for details.
  5799. - Special case: if point is at the beginning of the buffer and there is
  5800. no headline in line 1, this function will act as if called with prefix arg
  5801. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5802. But only if also the variable `org-cycle-global-at-bob' is t."
  5803. (interactive "P")
  5804. (org-load-modules-maybe)
  5805. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5806. (and org-cycle-level-after-item/entry-creation
  5807. (or (org-cycle-level)
  5808. (org-cycle-item-indentation))))
  5809. (let* (message-log-max ; Don't populate the *Messages* buffer
  5810. (limit-level
  5811. (or org-cycle-max-level
  5812. (and (boundp 'org-inlinetask-min-level)
  5813. org-inlinetask-min-level
  5814. (1- org-inlinetask-min-level))))
  5815. (nstars (and limit-level
  5816. (if org-odd-levels-only
  5817. (and limit-level (1- (* limit-level 2)))
  5818. limit-level)))
  5819. (org-outline-regexp
  5820. (if (not (derived-mode-p 'org-mode))
  5821. outline-regexp
  5822. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5823. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5824. (not (looking-at org-outline-regexp))))
  5825. (org-cycle-hook
  5826. (if bob-special
  5827. (delq 'org-optimize-window-after-visibility-change
  5828. (copy-sequence org-cycle-hook))
  5829. org-cycle-hook))
  5830. (pos (point)))
  5831. (if (or bob-special (equal arg '(4)))
  5832. ;; special case: use global cycling
  5833. (setq arg t))
  5834. (cond
  5835. ((equal arg '(16))
  5836. (setq last-command 'dummy)
  5837. (org-set-startup-visibility)
  5838. (message "Startup visibility, plus VISIBILITY properties"))
  5839. ((equal arg '(64))
  5840. (show-all)
  5841. (message "Entire buffer visible, including drawers"))
  5842. ;; Table: enter it or move to the next field.
  5843. ((org-at-table-p 'any)
  5844. (if (org-at-table.el-p)
  5845. (message "Use C-c ' to edit table.el tables")
  5846. (if arg (org-table-edit-field t)
  5847. (org-table-justify-field-maybe)
  5848. (call-interactively 'org-table-next-field))))
  5849. ((run-hook-with-args-until-success
  5850. 'org-tab-after-check-for-table-hook))
  5851. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5852. ((eq arg t) (org-cycle-internal-global))
  5853. ;; Drawers: delegate to `org-flag-drawer'.
  5854. ((and org-drawers org-drawer-regexp
  5855. (save-excursion
  5856. (beginning-of-line 1)
  5857. (looking-at org-drawer-regexp)))
  5858. (org-flag-drawer ; toggle block visibility
  5859. (not (get-char-property (match-end 0) 'invisible))))
  5860. ;; Show-subtree, ARG levels up from here.
  5861. ((integerp arg)
  5862. (save-excursion
  5863. (org-back-to-heading)
  5864. (outline-up-heading (if (< arg 0) (- arg)
  5865. (- (funcall outline-level) arg)))
  5866. (org-show-subtree)))
  5867. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5868. ((and (featurep 'org-inlinetask)
  5869. (org-inlinetask-at-task-p)
  5870. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5871. (org-inlinetask-toggle-visibility))
  5872. ((org-try-cdlatex-tab))
  5873. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5874. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5875. (save-excursion (beginning-of-line 1)
  5876. (looking-at org-outline-regexp)))
  5877. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5878. (org-cycle-internal-local))
  5879. ;; From there: TAB emulation and template completion.
  5880. (buffer-read-only (org-back-to-heading))
  5881. ((run-hook-with-args-until-success
  5882. 'org-tab-after-check-for-cycling-hook))
  5883. ((org-try-structure-completion))
  5884. ((run-hook-with-args-until-success
  5885. 'org-tab-before-tab-emulation-hook))
  5886. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5887. (or (not (bolp))
  5888. (not (looking-at org-outline-regexp))))
  5889. (call-interactively (global-key-binding "\t")))
  5890. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5891. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5892. (or (and (eq org-cycle-emulate-tab 'white)
  5893. (= (match-end 0) (point-at-eol)))
  5894. (and (eq org-cycle-emulate-tab 'whitestart)
  5895. (>= (match-end 0) pos))))
  5896. t
  5897. (eq org-cycle-emulate-tab t))
  5898. (call-interactively (global-key-binding "\t")))
  5899. (t (save-excursion
  5900. (org-back-to-heading)
  5901. (org-cycle)))))))
  5902. (defun org-cycle-internal-global ()
  5903. "Do the global cycling action."
  5904. ;; Hack to avoid display of messages for .org attachments in Gnus
  5905. (let (message-log-max ; Don't populate the *Messages* buffer
  5906. (ga (string-match "\\*fontification" (buffer-name))))
  5907. (cond
  5908. ((and (eq last-command this-command)
  5909. (eq org-cycle-global-status 'overview))
  5910. ;; We just created the overview - now do table of contents
  5911. ;; This can be slow in very large buffers, so indicate action
  5912. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5913. (unless ga (message "CONTENTS..."))
  5914. (org-content)
  5915. (unless ga (message "CONTENTS...done"))
  5916. (setq org-cycle-global-status 'contents)
  5917. (run-hook-with-args 'org-cycle-hook 'contents))
  5918. ((and (eq last-command this-command)
  5919. (eq org-cycle-global-status 'contents))
  5920. ;; We just showed the table of contents - now show everything
  5921. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5922. (show-all)
  5923. (unless ga (message "SHOW ALL"))
  5924. (setq org-cycle-global-status 'all)
  5925. (run-hook-with-args 'org-cycle-hook 'all))
  5926. (t
  5927. ;; Default action: go to overview
  5928. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5929. (org-overview)
  5930. (unless ga (message "OVERVIEW"))
  5931. (setq org-cycle-global-status 'overview)
  5932. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5933. (defun org-cycle-internal-local ()
  5934. "Do the local cycling action."
  5935. (let (message-log-max ; Don't populate the *Messages* buffer
  5936. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5937. ;; First, determine end of headline (EOH), end of subtree or item
  5938. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5939. (save-excursion
  5940. (if (org-at-item-p)
  5941. (progn
  5942. (beginning-of-line)
  5943. (setq struct (org-list-struct))
  5944. (setq eoh (point-at-eol))
  5945. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5946. (setq has-children (org-list-has-child-p (point) struct)))
  5947. (org-back-to-heading)
  5948. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5949. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5950. (setq has-children
  5951. (or (save-excursion
  5952. (let ((level (funcall outline-level)))
  5953. (outline-next-heading)
  5954. (and (org-at-heading-p t)
  5955. (> (funcall outline-level) level))))
  5956. (save-excursion
  5957. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5958. ;; Determine end invisible part of buffer (EOL)
  5959. (beginning-of-line 2)
  5960. ;; XEmacs doesn't have `next-single-char-property-change'
  5961. (if (featurep 'xemacs)
  5962. (while (and (not (eobp)) ;; this is like `next-line'
  5963. (get-char-property (1- (point)) 'invisible))
  5964. (beginning-of-line 2))
  5965. (while (and (not (eobp)) ;; this is like `next-line'
  5966. (get-char-property (1- (point)) 'invisible))
  5967. (goto-char (next-single-char-property-change (point) 'invisible))
  5968. (and (eolp) (beginning-of-line 2))))
  5969. (setq eol (point)))
  5970. ;; Find out what to do next and set `this-command'
  5971. (cond
  5972. ((= eos eoh)
  5973. ;; Nothing is hidden behind this heading
  5974. (unless (org-before-first-heading-p)
  5975. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5976. (message "EMPTY ENTRY")
  5977. (setq org-cycle-subtree-status nil)
  5978. (save-excursion
  5979. (goto-char eos)
  5980. (outline-next-heading)
  5981. (if (outline-invisible-p) (org-flag-heading nil))))
  5982. ((and (or (>= eol eos)
  5983. (not (string-match "\\S-" (buffer-substring eol eos))))
  5984. (or has-children
  5985. (not (setq children-skipped
  5986. org-cycle-skip-children-state-if-no-children))))
  5987. ;; Entire subtree is hidden in one line: children view
  5988. (unless (org-before-first-heading-p)
  5989. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5990. (if (org-at-item-p)
  5991. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5992. (org-show-entry)
  5993. (org-with-limited-levels (show-children))
  5994. ;; FIXME: This slows down the func way too much.
  5995. ;; How keep drawers hidden in subtree anyway?
  5996. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5997. ;; (org-cycle-hide-drawers 'subtree))
  5998. ;; Fold every list in subtree to top-level items.
  5999. (when (eq org-cycle-include-plain-lists 'integrate)
  6000. (save-excursion
  6001. (org-back-to-heading)
  6002. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6003. (beginning-of-line 1)
  6004. (let* ((struct (org-list-struct))
  6005. (prevs (org-list-prevs-alist struct))
  6006. (end (org-list-get-bottom-point struct)))
  6007. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6008. (org-list-get-all-items (point) struct prevs))
  6009. (goto-char (if (< end eos) end eos)))))))
  6010. (message "CHILDREN")
  6011. (save-excursion
  6012. (goto-char eos)
  6013. (outline-next-heading)
  6014. (if (outline-invisible-p) (org-flag-heading nil)))
  6015. (setq org-cycle-subtree-status 'children)
  6016. (unless (org-before-first-heading-p)
  6017. (run-hook-with-args 'org-cycle-hook 'children)))
  6018. ((or children-skipped
  6019. (and (eq last-command this-command)
  6020. (eq org-cycle-subtree-status 'children)))
  6021. ;; We just showed the children, or no children are there,
  6022. ;; now show everything.
  6023. (unless (org-before-first-heading-p)
  6024. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6025. (outline-flag-region eoh eos nil)
  6026. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6027. (setq org-cycle-subtree-status 'subtree)
  6028. (unless (org-before-first-heading-p)
  6029. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6030. (t
  6031. ;; Default action: hide the subtree.
  6032. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6033. (outline-flag-region eoh eos t)
  6034. (message "FOLDED")
  6035. (setq org-cycle-subtree-status 'folded)
  6036. (unless (org-before-first-heading-p)
  6037. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6038. ;;;###autoload
  6039. (defun org-global-cycle (&optional arg)
  6040. "Cycle the global visibility. For details see `org-cycle'.
  6041. With \\[universal-argument] prefix arg, switch to startup visibility.
  6042. With a numeric prefix, show all headlines up to that level."
  6043. (interactive "P")
  6044. (let ((org-cycle-include-plain-lists
  6045. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6046. (cond
  6047. ((integerp arg)
  6048. (show-all)
  6049. (hide-sublevels arg)
  6050. (setq org-cycle-global-status 'contents))
  6051. ((equal arg '(4))
  6052. (org-set-startup-visibility)
  6053. (message "Startup visibility, plus VISIBILITY properties."))
  6054. (t
  6055. (org-cycle '(4))))))
  6056. (defun org-set-startup-visibility ()
  6057. "Set the visibility required by startup options and properties."
  6058. (cond
  6059. ((eq org-startup-folded t)
  6060. (org-cycle '(4)))
  6061. ((eq org-startup-folded 'content)
  6062. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6063. (org-cycle '(4)) (org-cycle '(4)))))
  6064. (unless (eq org-startup-folded 'showeverything)
  6065. (if org-hide-block-startup (org-hide-block-all))
  6066. (org-set-visibility-according-to-property 'no-cleanup)
  6067. (org-cycle-hide-archived-subtrees 'all)
  6068. (org-cycle-hide-drawers 'all)
  6069. (org-cycle-show-empty-lines t)))
  6070. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6071. "Switch subtree visibilities according to :VISIBILITY: property."
  6072. (interactive)
  6073. (let (org-show-entry-below state)
  6074. (save-excursion
  6075. (goto-char (point-min))
  6076. (while (re-search-forward
  6077. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6078. nil t)
  6079. (setq state (match-string 1))
  6080. (save-excursion
  6081. (org-back-to-heading t)
  6082. (hide-subtree)
  6083. (org-reveal)
  6084. (cond
  6085. ((equal state '("fold" "folded"))
  6086. (hide-subtree))
  6087. ((equal state "children")
  6088. (org-show-hidden-entry)
  6089. (show-children))
  6090. ((equal state "content")
  6091. (save-excursion
  6092. (save-restriction
  6093. (org-narrow-to-subtree)
  6094. (org-content))))
  6095. ((member state '("all" "showall"))
  6096. (show-subtree)))))
  6097. (unless no-cleanup
  6098. (org-cycle-hide-archived-subtrees 'all)
  6099. (org-cycle-hide-drawers 'all)
  6100. (org-cycle-show-empty-lines 'all)))))
  6101. ;; This function uses outline-regexp instead of the more fundamental
  6102. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6103. ;; buffers, where outline-regexp is needed.
  6104. (defun org-overview ()
  6105. "Switch to overview mode, showing only top-level headlines.
  6106. Really, this shows all headlines with level equal or greater than the level
  6107. of the first headline in the buffer. This is important, because if the
  6108. first headline is not level one, then (hide-sublevels 1) gives confusing
  6109. results."
  6110. (interactive)
  6111. (let ((l (org-current-line))
  6112. (level (save-excursion
  6113. (goto-char (point-min))
  6114. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6115. (progn
  6116. (goto-char (match-beginning 0))
  6117. (funcall outline-level))))))
  6118. (and level (hide-sublevels level))
  6119. (recenter '(4))
  6120. (org-goto-line l)))
  6121. (defun org-content (&optional arg)
  6122. "Show all headlines in the buffer, like a table of contents.
  6123. With numerical argument N, show content up to level N."
  6124. (interactive "P")
  6125. (save-excursion
  6126. ;; Visit all headings and show their offspring
  6127. (and (integerp arg) (org-overview))
  6128. (goto-char (point-max))
  6129. (catch 'exit
  6130. (while (and (progn (condition-case nil
  6131. (outline-previous-visible-heading 1)
  6132. (error (goto-char (point-min))))
  6133. t)
  6134. (looking-at org-outline-regexp))
  6135. (if (integerp arg)
  6136. (show-children (1- arg))
  6137. (show-branches))
  6138. (if (bobp) (throw 'exit nil))))))
  6139. (defun org-optimize-window-after-visibility-change (state)
  6140. "Adjust the window after a change in outline visibility.
  6141. This function is the default value of the hook `org-cycle-hook'."
  6142. (when (get-buffer-window (current-buffer))
  6143. (cond
  6144. ((eq state 'content) nil)
  6145. ((eq state 'all) nil)
  6146. ((eq state 'folded) nil)
  6147. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6148. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6149. (defun org-remove-empty-overlays-at (pos)
  6150. "Remove outline overlays that do not contain non-white stuff."
  6151. (mapc
  6152. (lambda (o)
  6153. (and (eq 'outline (overlay-get o 'invisible))
  6154. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6155. (overlay-end o))))
  6156. (delete-overlay o)))
  6157. (overlays-at pos)))
  6158. (defun org-clean-visibility-after-subtree-move ()
  6159. "Fix visibility issues after moving a subtree."
  6160. ;; First, find a reasonable region to look at:
  6161. ;; Start two siblings above, end three below
  6162. (let* ((beg (save-excursion
  6163. (and (org-get-last-sibling)
  6164. (org-get-last-sibling))
  6165. (point)))
  6166. (end (save-excursion
  6167. (and (org-get-next-sibling)
  6168. (org-get-next-sibling)
  6169. (org-get-next-sibling))
  6170. (if (org-at-heading-p)
  6171. (point-at-eol)
  6172. (point))))
  6173. (level (looking-at "\\*+"))
  6174. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6175. (save-excursion
  6176. (save-restriction
  6177. (narrow-to-region beg end)
  6178. (when re
  6179. ;; Properly fold already folded siblings
  6180. (goto-char (point-min))
  6181. (while (re-search-forward re nil t)
  6182. (if (and (not (outline-invisible-p))
  6183. (save-excursion
  6184. (goto-char (point-at-eol)) (outline-invisible-p)))
  6185. (hide-entry))))
  6186. (org-cycle-show-empty-lines 'overview)
  6187. (org-cycle-hide-drawers 'overview)))))
  6188. (defun org-cycle-show-empty-lines (state)
  6189. "Show empty lines above all visible headlines.
  6190. The region to be covered depends on STATE when called through
  6191. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6192. entire buffer covered. Note that an empty line is only shown if there
  6193. are at least `org-cycle-separator-lines' empty lines before the headline."
  6194. (when (not (= org-cycle-separator-lines 0))
  6195. (save-excursion
  6196. (let* ((n (abs org-cycle-separator-lines))
  6197. (re (cond
  6198. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6199. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6200. (t (let ((ns (number-to-string (- n 2))))
  6201. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6202. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6203. beg end b e)
  6204. (cond
  6205. ((memq state '(overview contents t))
  6206. (setq beg (point-min) end (point-max)))
  6207. ((memq state '(children folded))
  6208. (setq beg (point) end (progn (org-end-of-subtree t t)
  6209. (beginning-of-line 2)
  6210. (point)))))
  6211. (when beg
  6212. (goto-char beg)
  6213. (while (re-search-forward re end t)
  6214. (unless (get-char-property (match-end 1) 'invisible)
  6215. (setq e (match-end 1))
  6216. (if (< org-cycle-separator-lines 0)
  6217. (setq b (save-excursion
  6218. (goto-char (match-beginning 0))
  6219. (org-back-over-empty-lines)
  6220. (if (save-excursion
  6221. (goto-char (max (point-min) (1- (point))))
  6222. (org-at-heading-p))
  6223. (1- (point))
  6224. (point))))
  6225. (setq b (match-beginning 1)))
  6226. (outline-flag-region b e nil)))))))
  6227. ;; Never hide empty lines at the end of the file.
  6228. (save-excursion
  6229. (goto-char (point-max))
  6230. (outline-previous-heading)
  6231. (outline-end-of-heading)
  6232. (if (and (looking-at "[ \t\n]+")
  6233. (= (match-end 0) (point-max)))
  6234. (outline-flag-region (point) (match-end 0) nil))))
  6235. (defun org-show-empty-lines-in-parent ()
  6236. "Move to the parent and re-show empty lines before visible headlines."
  6237. (save-excursion
  6238. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6239. (org-cycle-show-empty-lines context))))
  6240. (defun org-files-list ()
  6241. "Return `org-agenda-files' list, plus all open org-mode files.
  6242. This is useful for operations that need to scan all of a user's
  6243. open and agenda-wise Org files."
  6244. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6245. (dolist (buf (buffer-list))
  6246. (with-current-buffer buf
  6247. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6248. (let ((file (expand-file-name (buffer-file-name))))
  6249. (unless (member file files)
  6250. (push file files))))))
  6251. files))
  6252. (defsubst org-entry-beginning-position ()
  6253. "Return the beginning position of the current entry."
  6254. (save-excursion (outline-back-to-heading t) (point)))
  6255. (defsubst org-entry-end-position ()
  6256. "Return the end position of the current entry."
  6257. (save-excursion (outline-next-heading) (point)))
  6258. (defun org-cycle-hide-drawers (state)
  6259. "Re-hide all drawers after a visibility state change."
  6260. (when (and (derived-mode-p 'org-mode)
  6261. (not (memq state '(overview folded contents))))
  6262. (save-excursion
  6263. (let* ((globalp (memq state '(contents all)))
  6264. (beg (if globalp (point-min) (point)))
  6265. (end (if globalp (point-max)
  6266. (if (eq state 'children)
  6267. (save-excursion (outline-next-heading) (point))
  6268. (org-end-of-subtree t)))))
  6269. (goto-char beg)
  6270. (while (re-search-forward org-drawer-regexp end t)
  6271. (org-flag-drawer t))))))
  6272. (defun org-cycle-hide-inline-tasks (state)
  6273. "Re-hide inline task when switching to 'contents visibility state."
  6274. (when (and (eq state 'contents)
  6275. (boundp 'org-inlinetask-min-level)
  6276. org-inlinetask-min-level)
  6277. (hide-sublevels (1- org-inlinetask-min-level))))
  6278. (defun org-flag-drawer (flag)
  6279. "When FLAG is non-nil, hide the drawer we are within.
  6280. Otherwise make it visible."
  6281. (save-excursion
  6282. (beginning-of-line 1)
  6283. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6284. (let ((b (match-end 0)))
  6285. (if (re-search-forward
  6286. "^[ \t]*:END:"
  6287. (save-excursion (outline-next-heading) (point)) t)
  6288. (outline-flag-region b (point-at-eol) flag)
  6289. (user-error ":END: line missing at position %s" b))))))
  6290. (defun org-subtree-end-visible-p ()
  6291. "Is the end of the current subtree visible?"
  6292. (pos-visible-in-window-p
  6293. (save-excursion (org-end-of-subtree t) (point))))
  6294. (defun org-first-headline-recenter (&optional N)
  6295. "Move cursor to the first headline and recenter the headline.
  6296. Optional argument N means put the headline into the Nth line of the window."
  6297. (goto-char (point-min))
  6298. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6299. (beginning-of-line)
  6300. (recenter (prefix-numeric-value N))))
  6301. ;;; Saving and restoring visibility
  6302. (defun org-outline-overlay-data (&optional use-markers)
  6303. "Return a list of the locations of all outline overlays.
  6304. These are overlays with the `invisible' property value `outline'.
  6305. The return value is a list of cons cells, with start and stop
  6306. positions for each overlay.
  6307. If USE-MARKERS is set, return the positions as markers."
  6308. (let (beg end)
  6309. (save-excursion
  6310. (save-restriction
  6311. (widen)
  6312. (delq nil
  6313. (mapcar (lambda (o)
  6314. (when (eq (overlay-get o 'invisible) 'outline)
  6315. (setq beg (overlay-start o)
  6316. end (overlay-end o))
  6317. (and beg end (> end beg)
  6318. (if use-markers
  6319. (cons (move-marker (make-marker) beg)
  6320. (move-marker (make-marker) end))
  6321. (cons beg end)))))
  6322. (overlays-in (point-min) (point-max))))))))
  6323. (defun org-set-outline-overlay-data (data)
  6324. "Create visibility overlays for all positions in DATA.
  6325. DATA should have been made by `org-outline-overlay-data'."
  6326. (let (o)
  6327. (save-excursion
  6328. (save-restriction
  6329. (widen)
  6330. (show-all)
  6331. (mapc (lambda (c)
  6332. (outline-flag-region (car c) (cdr c) t))
  6333. data)))))
  6334. ;;; Folding of blocks
  6335. (defvar org-hide-block-overlays nil
  6336. "Overlays hiding blocks.")
  6337. (make-variable-buffer-local 'org-hide-block-overlays)
  6338. (defun org-block-map (function &optional start end)
  6339. "Call FUNCTION at the head of all source blocks in the current buffer.
  6340. Optional arguments START and END can be used to limit the range."
  6341. (let ((start (or start (point-min)))
  6342. (end (or end (point-max))))
  6343. (save-excursion
  6344. (goto-char start)
  6345. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6346. (save-excursion
  6347. (save-match-data
  6348. (goto-char (match-beginning 0))
  6349. (funcall function)))))))
  6350. (defun org-hide-block-toggle-all ()
  6351. "Toggle the visibility of all blocks in the current buffer."
  6352. (org-block-map #'org-hide-block-toggle))
  6353. (defun org-hide-block-all ()
  6354. "Fold all blocks in the current buffer."
  6355. (interactive)
  6356. (org-show-block-all)
  6357. (org-block-map #'org-hide-block-toggle-maybe))
  6358. (defun org-show-block-all ()
  6359. "Unfold all blocks in the current buffer."
  6360. (interactive)
  6361. (mapc 'delete-overlay org-hide-block-overlays)
  6362. (setq org-hide-block-overlays nil))
  6363. (defun org-hide-block-toggle-maybe ()
  6364. "Toggle visibility of block at point."
  6365. (interactive)
  6366. (let ((case-fold-search t))
  6367. (if (save-excursion
  6368. (beginning-of-line 1)
  6369. (looking-at org-block-regexp))
  6370. (progn (org-hide-block-toggle)
  6371. t) ;; to signal that we took action
  6372. nil))) ;; to signal that we did not
  6373. (defun org-hide-block-toggle (&optional force)
  6374. "Toggle the visibility of the current block."
  6375. (interactive)
  6376. (save-excursion
  6377. (beginning-of-line)
  6378. (if (re-search-forward org-block-regexp nil t)
  6379. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6380. (end (match-end 0)) ;; end of entire body
  6381. ov)
  6382. (if (memq t (mapcar (lambda (overlay)
  6383. (eq (overlay-get overlay 'invisible)
  6384. 'org-hide-block))
  6385. (overlays-at start)))
  6386. (if (or (not force) (eq force 'off))
  6387. (mapc (lambda (ov)
  6388. (when (member ov org-hide-block-overlays)
  6389. (setq org-hide-block-overlays
  6390. (delq ov org-hide-block-overlays)))
  6391. (when (eq (overlay-get ov 'invisible)
  6392. 'org-hide-block)
  6393. (delete-overlay ov)))
  6394. (overlays-at start)))
  6395. (setq ov (make-overlay start end))
  6396. (overlay-put ov 'invisible 'org-hide-block)
  6397. ;; make the block accessible to isearch
  6398. (overlay-put
  6399. ov 'isearch-open-invisible
  6400. (lambda (ov)
  6401. (when (member ov org-hide-block-overlays)
  6402. (setq org-hide-block-overlays
  6403. (delq ov org-hide-block-overlays)))
  6404. (when (eq (overlay-get ov 'invisible)
  6405. 'org-hide-block)
  6406. (delete-overlay ov))))
  6407. (push ov org-hide-block-overlays)))
  6408. (user-error "Not looking at a source block"))))
  6409. ;; org-tab-after-check-for-cycling-hook
  6410. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6411. ;; Remove overlays when changing major mode
  6412. (add-hook 'org-mode-hook
  6413. (lambda () (org-add-hook 'change-major-mode-hook
  6414. 'org-show-block-all 'append 'local)))
  6415. ;;; Org-goto
  6416. (defvar org-goto-window-configuration nil)
  6417. (defvar org-goto-marker nil)
  6418. (defvar org-goto-map)
  6419. (defun org-goto-map ()
  6420. "Set the keymap `org-goto'."
  6421. (setq org-goto-map
  6422. (let ((map (make-sparse-keymap)))
  6423. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6424. mouse-drag-region universal-argument org-occur))
  6425. cmd)
  6426. (while (setq cmd (pop cmds))
  6427. (substitute-key-definition cmd cmd map global-map)))
  6428. (suppress-keymap map)
  6429. (org-defkey map "\C-m" 'org-goto-ret)
  6430. (org-defkey map [(return)] 'org-goto-ret)
  6431. (org-defkey map [(left)] 'org-goto-left)
  6432. (org-defkey map [(right)] 'org-goto-right)
  6433. (org-defkey map [(control ?g)] 'org-goto-quit)
  6434. (org-defkey map "\C-i" 'org-cycle)
  6435. (org-defkey map [(tab)] 'org-cycle)
  6436. (org-defkey map [(down)] 'outline-next-visible-heading)
  6437. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6438. (if org-goto-auto-isearch
  6439. (if (fboundp 'define-key-after)
  6440. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6441. nil)
  6442. (org-defkey map "q" 'org-goto-quit)
  6443. (org-defkey map "n" 'outline-next-visible-heading)
  6444. (org-defkey map "p" 'outline-previous-visible-heading)
  6445. (org-defkey map "f" 'outline-forward-same-level)
  6446. (org-defkey map "b" 'outline-backward-same-level)
  6447. (org-defkey map "u" 'outline-up-heading))
  6448. (org-defkey map "/" 'org-occur)
  6449. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6450. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6451. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6452. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6453. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6454. map)))
  6455. (defconst org-goto-help
  6456. "Browse buffer copy, to find location or copy text.%s
  6457. RET=jump to location C-g=quit and return to previous location
  6458. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6459. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6460. (defun org-goto (&optional alternative-interface)
  6461. "Look up a different location in the current file, keeping current visibility.
  6462. When you want look-up or go to a different location in a
  6463. document, the fastest way is often to fold the entire buffer and
  6464. then dive into the tree. This method has the disadvantage, that
  6465. the previous location will be folded, which may not be what you
  6466. want.
  6467. This command works around this by showing a copy of the current
  6468. buffer in an indirect buffer, in overview mode. You can dive
  6469. into the tree in that copy, use org-occur and incremental search
  6470. to find a location. When pressing RET or `Q', the command
  6471. returns to the original buffer in which the visibility is still
  6472. unchanged. After RET it will also jump to the location selected
  6473. in the indirect buffer and expose the headline hierarchy above.
  6474. With a prefix argument, use the alternative interface: e.g. if
  6475. `org-goto-interface' is 'outline use 'outline-path-completion."
  6476. (interactive "P")
  6477. (org-goto-map)
  6478. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6479. (org-refile-use-outline-path t)
  6480. (org-refile-target-verify-function nil)
  6481. (interface
  6482. (if (not alternative-interface)
  6483. org-goto-interface
  6484. (if (eq org-goto-interface 'outline)
  6485. 'outline-path-completion
  6486. 'outline)))
  6487. (org-goto-start-pos (point))
  6488. (selected-point
  6489. (if (eq interface 'outline)
  6490. (car (org-get-location (current-buffer) org-goto-help))
  6491. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6492. (org-refile-check-position pa)
  6493. (nth 3 pa)))))
  6494. (if selected-point
  6495. (progn
  6496. (org-mark-ring-push org-goto-start-pos)
  6497. (goto-char selected-point)
  6498. (if (or (outline-invisible-p) (org-invisible-p2))
  6499. (org-show-context 'org-goto)))
  6500. (message "Quit"))))
  6501. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6502. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6503. (defvar org-goto-local-auto-isearch-map) ; defined below
  6504. (defun org-get-location (buf help)
  6505. "Let the user select a location in the Org-mode buffer BUF.
  6506. This function uses a recursive edit. It returns the selected position
  6507. or nil."
  6508. (org-no-popups
  6509. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6510. (isearch-hide-immediately nil)
  6511. (isearch-search-fun-function
  6512. (lambda () 'org-goto-local-search-headings))
  6513. (org-goto-selected-point org-goto-exit-command))
  6514. (save-excursion
  6515. (save-window-excursion
  6516. (delete-other-windows)
  6517. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6518. (org-pop-to-buffer-same-window
  6519. (condition-case nil
  6520. (make-indirect-buffer (current-buffer) "*org-goto*")
  6521. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6522. (with-output-to-temp-buffer "*Org Help*"
  6523. (princ (format help (if org-goto-auto-isearch
  6524. " Just type for auto-isearch."
  6525. " n/p/f/b/u to navigate, q to quit."))))
  6526. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6527. (setq buffer-read-only nil)
  6528. (let ((org-startup-truncated t)
  6529. (org-startup-folded nil)
  6530. (org-startup-align-all-tables nil))
  6531. (org-mode)
  6532. (org-overview))
  6533. (setq buffer-read-only t)
  6534. (if (and (boundp 'org-goto-start-pos)
  6535. (integer-or-marker-p org-goto-start-pos))
  6536. (let ((org-show-hierarchy-above t)
  6537. (org-show-siblings t)
  6538. (org-show-following-heading t))
  6539. (goto-char org-goto-start-pos)
  6540. (and (outline-invisible-p) (org-show-context)))
  6541. (goto-char (point-min)))
  6542. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6543. (message "Select location and press RET")
  6544. (use-local-map org-goto-map)
  6545. (recursive-edit)))
  6546. (kill-buffer "*org-goto*")
  6547. (cons org-goto-selected-point org-goto-exit-command))))
  6548. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6549. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6550. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6551. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6552. (defun org-goto-local-search-headings (string bound noerror)
  6553. "Search and make sure that any matches are in headlines."
  6554. (catch 'return
  6555. (while (if isearch-forward
  6556. (search-forward string bound noerror)
  6557. (search-backward string bound noerror))
  6558. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6559. (and (member :headline context)
  6560. (not (member :tags context))))
  6561. (throw 'return (point))))))
  6562. (defun org-goto-local-auto-isearch ()
  6563. "Start isearch."
  6564. (interactive)
  6565. (goto-char (point-min))
  6566. (let ((keys (this-command-keys)))
  6567. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6568. (isearch-mode t)
  6569. (isearch-process-search-char (string-to-char keys)))))
  6570. (defun org-goto-ret (&optional arg)
  6571. "Finish `org-goto' by going to the new location."
  6572. (interactive "P")
  6573. (setq org-goto-selected-point (point)
  6574. org-goto-exit-command 'return)
  6575. (throw 'exit nil))
  6576. (defun org-goto-left ()
  6577. "Finish `org-goto' by going to the new location."
  6578. (interactive)
  6579. (if (org-at-heading-p)
  6580. (progn
  6581. (beginning-of-line 1)
  6582. (setq org-goto-selected-point (point)
  6583. org-goto-exit-command 'left)
  6584. (throw 'exit nil))
  6585. (user-error "Not on a heading")))
  6586. (defun org-goto-right ()
  6587. "Finish `org-goto' by going to the new location."
  6588. (interactive)
  6589. (if (org-at-heading-p)
  6590. (progn
  6591. (setq org-goto-selected-point (point)
  6592. org-goto-exit-command 'right)
  6593. (throw 'exit nil))
  6594. (user-error "Not on a heading")))
  6595. (defun org-goto-quit ()
  6596. "Finish `org-goto' without cursor motion."
  6597. (interactive)
  6598. (setq org-goto-selected-point nil)
  6599. (setq org-goto-exit-command 'quit)
  6600. (throw 'exit nil))
  6601. ;;; Indirect buffer display of subtrees
  6602. (defvar org-indirect-dedicated-frame nil
  6603. "This is the frame being used for indirect tree display.")
  6604. (defvar org-last-indirect-buffer nil)
  6605. (defun org-tree-to-indirect-buffer (&optional arg)
  6606. "Create indirect buffer and narrow it to current subtree.
  6607. With a numerical prefix ARG, go up to this level and then take that tree.
  6608. If ARG is negative, go up that many levels.
  6609. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6610. indirect buffer previously made with this command, to avoid proliferation of
  6611. indirect buffers. However, when you call the command with a \
  6612. \\[universal-argument] prefix, or
  6613. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6614. is kept so that you can work with several indirect buffers at the same time.
  6615. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6616. \\[universal-argument] prefix also
  6617. requests that a new frame be made for the new buffer, so that the dedicated
  6618. frame is not changed."
  6619. (interactive "P")
  6620. (let ((cbuf (current-buffer))
  6621. (cwin (selected-window))
  6622. (pos (point))
  6623. beg end level heading ibuf)
  6624. (save-excursion
  6625. (org-back-to-heading t)
  6626. (when (numberp arg)
  6627. (setq level (org-outline-level))
  6628. (if (< arg 0) (setq arg (+ level arg)))
  6629. (while (> (setq level (org-outline-level)) arg)
  6630. (org-up-heading-safe)))
  6631. (setq beg (point)
  6632. heading (org-get-heading))
  6633. (org-end-of-subtree t t)
  6634. (if (org-at-heading-p) (backward-char 1))
  6635. (setq end (point)))
  6636. (if (and (buffer-live-p org-last-indirect-buffer)
  6637. (not (eq org-indirect-buffer-display 'new-frame))
  6638. (not arg))
  6639. (kill-buffer org-last-indirect-buffer))
  6640. (setq ibuf (org-get-indirect-buffer cbuf)
  6641. org-last-indirect-buffer ibuf)
  6642. (cond
  6643. ((or (eq org-indirect-buffer-display 'new-frame)
  6644. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6645. (select-frame (make-frame))
  6646. (delete-other-windows)
  6647. (org-pop-to-buffer-same-window ibuf)
  6648. (org-set-frame-title heading))
  6649. ((eq org-indirect-buffer-display 'dedicated-frame)
  6650. (raise-frame
  6651. (select-frame (or (and org-indirect-dedicated-frame
  6652. (frame-live-p org-indirect-dedicated-frame)
  6653. org-indirect-dedicated-frame)
  6654. (setq org-indirect-dedicated-frame (make-frame)))))
  6655. (delete-other-windows)
  6656. (org-pop-to-buffer-same-window ibuf)
  6657. (org-set-frame-title (concat "Indirect: " heading)))
  6658. ((eq org-indirect-buffer-display 'current-window)
  6659. (org-pop-to-buffer-same-window ibuf))
  6660. ((eq org-indirect-buffer-display 'other-window)
  6661. (pop-to-buffer ibuf))
  6662. (t (error "Invalid value")))
  6663. (if (featurep 'xemacs)
  6664. (save-excursion (org-mode) (turn-on-font-lock)))
  6665. (narrow-to-region beg end)
  6666. (show-all)
  6667. (goto-char pos)
  6668. (run-hook-with-args 'org-cycle-hook 'all)
  6669. (and (window-live-p cwin) (select-window cwin))))
  6670. (defun org-get-indirect-buffer (&optional buffer)
  6671. (setq buffer (or buffer (current-buffer)))
  6672. (let ((n 1) (base (buffer-name buffer)) bname)
  6673. (while (buffer-live-p
  6674. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6675. (setq n (1+ n)))
  6676. (condition-case nil
  6677. (make-indirect-buffer buffer bname 'clone)
  6678. (error (make-indirect-buffer buffer bname)))))
  6679. (defun org-set-frame-title (title)
  6680. "Set the title of the current frame to the string TITLE."
  6681. ;; FIXME: how to name a single frame in XEmacs???
  6682. (unless (featurep 'xemacs)
  6683. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6684. ;;;; Structure editing
  6685. ;;; Inserting headlines
  6686. (defun org-previous-line-empty-p (&optional next)
  6687. "Is the previous line a blank line?
  6688. When NEXT is non-nil, check the next line instead."
  6689. (save-excursion
  6690. (and (not (bobp))
  6691. (or (beginning-of-line (if next 2 0)) t)
  6692. (save-match-data
  6693. (looking-at "[ \t]*$")))))
  6694. (defun org-insert-heading (&optional arg invisible-ok)
  6695. "Insert a new heading or item with same depth at point.
  6696. If point is in a plain list and ARG is nil, create a new list item.
  6697. With one universal prefix argument, insert a heading even in lists.
  6698. With two universal prefix arguments, insert the heading at the end
  6699. of the parent subtree.
  6700. If point is at the beginning of a headline, insert a sibling before
  6701. the current headline. If point is not at the beginning, split the line
  6702. and create a new headline with the text in the current line after point
  6703. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6704. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6705. This is important for non-interactive uses of the command."
  6706. (interactive "P")
  6707. (cond
  6708. ((or (= (buffer-size) 0)
  6709. (and (not (save-excursion
  6710. (and (ignore-errors (org-back-to-heading invisible-ok))
  6711. (org-at-heading-p))))
  6712. (or arg (not (org-in-item-p)))))
  6713. (insert
  6714. (if (org-previous-line-empty-p) "" "\n")
  6715. (if (org-in-src-block-p) ",* " "* "))
  6716. (run-hooks 'org-insert-heading-hook))
  6717. ((or arg
  6718. (and (not (org-in-item-p)) org-insert-heading-respect-content)
  6719. (not (org-insert-item
  6720. (save-excursion
  6721. (beginning-of-line)
  6722. (looking-at org-list-full-item-re)
  6723. (match-string 3)))))
  6724. (let (begn endn)
  6725. (when (org-buffer-narrowed-p)
  6726. (setq begn (point-min) endn (point-max))
  6727. (widen))
  6728. (let* ((empty-line-p nil)
  6729. (eops (equal arg '(16))) ; insert at end of parent subtree
  6730. (org-insert-heading-respect-content
  6731. (or (not (null arg)) org-insert-heading-respect-content))
  6732. (level nil)
  6733. (on-heading (org-at-heading-p))
  6734. (on-empty-line
  6735. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6736. (head (save-excursion
  6737. (condition-case nil
  6738. (progn
  6739. (org-back-to-heading invisible-ok)
  6740. (when (and (not on-heading)
  6741. (featurep 'org-inlinetask)
  6742. (integerp org-inlinetask-min-level)
  6743. (>= (length (match-string 0))
  6744. org-inlinetask-min-level))
  6745. ;; Find a heading level before the inline task
  6746. (while (and (setq level (org-up-heading-safe))
  6747. (>= level org-inlinetask-min-level)))
  6748. (if (org-at-heading-p)
  6749. (org-back-to-heading invisible-ok)
  6750. (error "This should not happen")))
  6751. (unless (and (save-excursion
  6752. (save-match-data
  6753. (org-backward-heading-same-level 1 invisible-ok))
  6754. (= (point) (match-beginning 0)))
  6755. (not (org-previous-line-empty-p t)))
  6756. (setq empty-line-p (org-previous-line-empty-p)))
  6757. (match-string 0))
  6758. (error "* "))))
  6759. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6760. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6761. pos hide-previous previous-pos)
  6762. (if ;; At the beginning of a heading, open a new line for insertiong
  6763. (and (bolp) (org-at-heading-p)
  6764. (not eops)
  6765. (or (bobp)
  6766. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6767. (open-line (if blank 2 1))
  6768. (save-excursion
  6769. (setq previous-pos (point-at-bol))
  6770. (end-of-line)
  6771. (setq hide-previous (outline-invisible-p)))
  6772. (and org-insert-heading-respect-content
  6773. (save-excursion
  6774. (while (outline-invisible-p)
  6775. (org-show-subtree)
  6776. (org-up-heading-safe))))
  6777. (let ((split
  6778. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6779. (save-excursion
  6780. (let ((p (point)))
  6781. (goto-char (point-at-bol))
  6782. (and (looking-at org-complex-heading-regexp)
  6783. (match-beginning 4)
  6784. (> p (match-beginning 4)))))))
  6785. tags pos)
  6786. (cond
  6787. ;; Insert a new line, possibly at end of parent subtree
  6788. ((and (not arg) (not on-heading) (not on-empty-line)
  6789. (not (save-excursion
  6790. (beginning-of-line 1)
  6791. (looking-at org-list-full-item-re))))
  6792. (beginning-of-line 1))
  6793. (org-insert-heading-respect-content
  6794. (if (not eops)
  6795. (progn
  6796. (org-end-of-subtree nil t)
  6797. (and (looking-at "^\\*") (backward-char 1))
  6798. (while (and (not (bobp))
  6799. ;; Don't delete spaces in empty headlines
  6800. (not (looking-back org-outline-regexp))
  6801. (member (char-before) '(?\ ?\t ?\n)))
  6802. (backward-delete-char 1)))
  6803. (let ((p (point)))
  6804. (org-up-heading-safe)
  6805. (if (= p (point))
  6806. (goto-char (point-max))
  6807. (org-end-of-subtree nil t))))
  6808. (when (featurep 'org-inlinetask)
  6809. (while (and (not (eobp))
  6810. (looking-at "\\(\\*+\\)[ \t]+")
  6811. (>= (length (match-string 1))
  6812. org-inlinetask-min-level))
  6813. (org-end-of-subtree nil t)))
  6814. (or (bolp) (newline))
  6815. (or (org-previous-line-empty-p)
  6816. (and blank (newline)))
  6817. (if (or empty-line-p eops) (open-line 1)))
  6818. ;; Insert a headling containing text after point
  6819. ((org-at-heading-p)
  6820. (when hide-previous
  6821. (show-children)
  6822. (org-show-entry))
  6823. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6824. (setq tags (and (match-end 2) (match-string 2)))
  6825. (and (match-end 1)
  6826. (delete-region (match-beginning 1) (match-end 1)))
  6827. (setq pos (point-at-bol))
  6828. (or split (end-of-line 1))
  6829. (delete-horizontal-space)
  6830. (if (string-match "\\`\\*+\\'"
  6831. (buffer-substring (point-at-bol) (point)))
  6832. (insert " "))
  6833. (newline (if blank 2 1))
  6834. (when tags
  6835. (save-excursion
  6836. (goto-char pos)
  6837. (end-of-line 1)
  6838. (insert " " tags)
  6839. (org-set-tags nil 'align))))
  6840. (t
  6841. (or split (end-of-line 1))
  6842. (newline (cond ((and blank (not on-empty-line)) 2)
  6843. (blank 1)
  6844. (on-empty-line 0) (t 1)))))))
  6845. (insert head) (just-one-space)
  6846. (setq pos (point))
  6847. (end-of-line 1)
  6848. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6849. (when (and org-insert-heading-respect-content hide-previous)
  6850. (save-excursion
  6851. (goto-char previous-pos)
  6852. (hide-subtree)))
  6853. (when (and begn endn)
  6854. (narrow-to-region (min (point) begn) (max (point) endn)))
  6855. (run-hooks 'org-insert-heading-hook))))))
  6856. (defun org-get-heading (&optional no-tags no-todo)
  6857. "Return the heading of the current entry, without the stars.
  6858. When NO-TAGS is non-nil, don't include tags.
  6859. When NO-TODO is non-nil, don't include TODO keywords."
  6860. (save-excursion
  6861. (org-back-to-heading t)
  6862. (cond
  6863. ((and no-tags no-todo)
  6864. (looking-at org-complex-heading-regexp)
  6865. (match-string 4))
  6866. (no-tags
  6867. (looking-at (concat org-outline-regexp
  6868. "\\(.*?\\)"
  6869. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6870. (match-string 1))
  6871. (no-todo
  6872. (looking-at org-todo-line-regexp)
  6873. (match-string 3))
  6874. (t (looking-at org-heading-regexp)
  6875. (match-string 2)))))
  6876. (defvar orgstruct-mode) ; defined below
  6877. (defun org-heading-components ()
  6878. "Return the components of the current heading.
  6879. This is a list with the following elements:
  6880. - the level as an integer
  6881. - the reduced level, different if `org-odd-levels-only' is set.
  6882. - the TODO keyword, or nil
  6883. - the priority character, like ?A, or nil if no priority is given
  6884. - the headline text itself, or the tags string if no headline text
  6885. - the tags string, or nil."
  6886. (save-excursion
  6887. (org-back-to-heading t)
  6888. (if (let (case-fold-search)
  6889. (looking-at
  6890. (if orgstruct-mode
  6891. org-heading-regexp
  6892. org-complex-heading-regexp)))
  6893. (if orgstruct-mode
  6894. (list (length (match-string 1))
  6895. (org-reduced-level (length (match-string 1)))
  6896. nil
  6897. nil
  6898. (match-string 2)
  6899. nil)
  6900. (list (length (match-string 1))
  6901. (org-reduced-level (length (match-string 1)))
  6902. (org-match-string-no-properties 2)
  6903. (and (match-end 3) (aref (match-string 3) 2))
  6904. (org-match-string-no-properties 4)
  6905. (org-match-string-no-properties 5))))))
  6906. (defun org-get-entry ()
  6907. "Get the entry text, after heading, entire subtree."
  6908. (save-excursion
  6909. (org-back-to-heading t)
  6910. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6911. (defun org-insert-heading-after-current ()
  6912. "Insert a new heading with same level as current, after current subtree."
  6913. (interactive)
  6914. (org-back-to-heading)
  6915. (org-insert-heading)
  6916. (org-move-subtree-down)
  6917. (end-of-line 1))
  6918. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6919. "Insert heading with `org-insert-heading-respect-content' set to t."
  6920. (interactive "P")
  6921. (let ((org-insert-heading-respect-content t))
  6922. (org-insert-heading arg invisible-ok)))
  6923. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6924. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6925. (interactive "P")
  6926. (let ((org-insert-heading-respect-content t))
  6927. (org-insert-todo-heading force-state t)))
  6928. (defun org-insert-todo-heading (arg &optional force-heading)
  6929. "Insert a new heading with the same level and TODO state as current heading.
  6930. If the heading has no TODO state, or if the state is DONE, use the first
  6931. state (TODO by default). Also one prefix arg, force first state. With two
  6932. prefix args, force inserting at the end of the parent subtree."
  6933. (interactive "P")
  6934. (when (or force-heading (not (org-insert-item 'checkbox)))
  6935. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6936. force-heading))
  6937. (save-excursion
  6938. (org-back-to-heading)
  6939. (outline-previous-heading)
  6940. (looking-at org-todo-line-regexp))
  6941. (let*
  6942. ((new-mark-x
  6943. (if (or arg
  6944. (not (match-beginning 2))
  6945. (member (match-string 2) org-done-keywords))
  6946. (car org-todo-keywords-1)
  6947. (match-string 2)))
  6948. (new-mark
  6949. (or
  6950. (run-hook-with-args-until-success
  6951. 'org-todo-get-default-hook new-mark-x nil)
  6952. new-mark-x)))
  6953. (beginning-of-line 1)
  6954. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6955. (if org-treat-insert-todo-heading-as-state-change
  6956. (org-todo new-mark)
  6957. (insert new-mark " "))))
  6958. (when org-provide-todo-statistics
  6959. (org-update-parent-todo-statistics))))
  6960. (defun org-insert-subheading (arg)
  6961. "Insert a new subheading and demote it.
  6962. Works for outline headings and for plain lists alike."
  6963. (interactive "P")
  6964. (org-insert-heading arg)
  6965. (cond
  6966. ((org-at-heading-p) (org-do-demote))
  6967. ((org-at-item-p) (org-indent-item))))
  6968. (defun org-insert-todo-subheading (arg)
  6969. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6970. Works for outline headings and for plain lists alike."
  6971. (interactive "P")
  6972. (org-insert-todo-heading arg)
  6973. (cond
  6974. ((org-at-heading-p) (org-do-demote))
  6975. ((org-at-item-p) (org-indent-item))))
  6976. ;;; Promotion and Demotion
  6977. (defvar org-after-demote-entry-hook nil
  6978. "Hook run after an entry has been demoted.
  6979. The cursor will be at the beginning of the entry.
  6980. When a subtree is being demoted, the hook will be called for each node.")
  6981. (defvar org-after-promote-entry-hook nil
  6982. "Hook run after an entry has been promoted.
  6983. The cursor will be at the beginning of the entry.
  6984. When a subtree is being promoted, the hook will be called for each node.")
  6985. (defun org-promote-subtree ()
  6986. "Promote the entire subtree.
  6987. See also `org-promote'."
  6988. (interactive)
  6989. (save-excursion
  6990. (org-with-limited-levels (org-map-tree 'org-promote)))
  6991. (org-fix-position-after-promote))
  6992. (defun org-demote-subtree ()
  6993. "Demote the entire subtree. See `org-demote'.
  6994. See also `org-promote'."
  6995. (interactive)
  6996. (save-excursion
  6997. (org-with-limited-levels (org-map-tree 'org-demote)))
  6998. (org-fix-position-after-promote))
  6999. (defun org-do-promote ()
  7000. "Promote the current heading higher up the tree.
  7001. If the region is active in `transient-mark-mode', promote all headings
  7002. in the region."
  7003. (interactive)
  7004. (save-excursion
  7005. (if (org-region-active-p)
  7006. (org-map-region 'org-promote (region-beginning) (region-end))
  7007. (org-promote)))
  7008. (org-fix-position-after-promote))
  7009. (defun org-do-demote ()
  7010. "Demote the current heading lower down the tree.
  7011. If the region is active in `transient-mark-mode', demote all headings
  7012. in the region."
  7013. (interactive)
  7014. (save-excursion
  7015. (if (org-region-active-p)
  7016. (org-map-region 'org-demote (region-beginning) (region-end))
  7017. (org-demote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-fix-position-after-promote ()
  7020. "Make sure that after pro/demotion cursor position is right."
  7021. (let ((pos (point)))
  7022. (when (save-excursion
  7023. (beginning-of-line 1)
  7024. (looking-at org-todo-line-regexp)
  7025. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7026. (cond ((eobp) (insert " "))
  7027. ((eolp) (insert " "))
  7028. ((equal (char-after) ?\ ) (forward-char 1))))))
  7029. (defun org-current-level ()
  7030. "Return the level of the current entry, or nil if before the first headline.
  7031. The level is the number of stars at the beginning of the headline."
  7032. (save-excursion
  7033. (org-with-limited-levels
  7034. (if (ignore-errors (org-back-to-heading t))
  7035. (funcall outline-level)))))
  7036. (defun org-get-previous-line-level ()
  7037. "Return the outline depth of the last headline before the current line.
  7038. Returns 0 for the first headline in the buffer, and nil if before the
  7039. first headline."
  7040. (let ((current-level (org-current-level))
  7041. (prev-level (when (> (line-number-at-pos) 1)
  7042. (save-excursion
  7043. (beginning-of-line 0)
  7044. (org-current-level)))))
  7045. (cond ((null current-level) nil) ; Before first headline
  7046. ((null prev-level) 0) ; At first headline
  7047. (prev-level))))
  7048. (defun org-reduced-level (l)
  7049. "Compute the effective level of a heading.
  7050. This takes into account the setting of `org-odd-levels-only'."
  7051. (cond
  7052. ((zerop l) 0)
  7053. (org-odd-levels-only (1+ (floor (/ l 2))))
  7054. (t l)))
  7055. (defun org-level-increment ()
  7056. "Return the number of stars that will be added or removed at a
  7057. time to headlines when structure editing, based on the value of
  7058. `org-odd-levels-only'."
  7059. (if org-odd-levels-only 2 1))
  7060. (defun org-get-valid-level (level &optional change)
  7061. "Rectify a level change under the influence of `org-odd-levels-only'
  7062. LEVEL is a current level, CHANGE is by how much the level should be
  7063. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7064. even level numbers will become the next higher odd number."
  7065. (if org-odd-levels-only
  7066. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7067. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7068. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7069. (max 1 (+ level (or change 0)))))
  7070. (if (boundp 'define-obsolete-function-alias)
  7071. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7072. (define-obsolete-function-alias 'org-get-legal-level
  7073. 'org-get-valid-level)
  7074. (define-obsolete-function-alias 'org-get-legal-level
  7075. 'org-get-valid-level "23.1")))
  7076. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7077. ;; ̀org-with-limited-levels'
  7078. (defun org-promote ()
  7079. "Promote the current heading higher up the tree.
  7080. If the region is active in `transient-mark-mode', promote all headings
  7081. in the region."
  7082. (org-back-to-heading t)
  7083. (let* ((level (save-match-data (funcall outline-level)))
  7084. (after-change-functions (remove 'flyspell-after-change-function
  7085. after-change-functions))
  7086. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7087. (diff (abs (- level (length up-head) -1))))
  7088. (cond ((and (= level 1) org-called-with-limited-levels
  7089. org-allow-promoting-top-level-subtree)
  7090. (replace-match "# " nil t))
  7091. ((= level 1)
  7092. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7093. (t (replace-match up-head nil t)))
  7094. ;; Fixup tag positioning
  7095. (unless (= level 1)
  7096. (and org-auto-align-tags (org-set-tags nil t))
  7097. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7098. (run-hooks 'org-after-promote-entry-hook)))
  7099. (defun org-demote ()
  7100. "Demote the current heading lower down the tree.
  7101. If the region is active in `transient-mark-mode', demote all headings
  7102. in the region."
  7103. (org-back-to-heading t)
  7104. (let* ((level (save-match-data (funcall outline-level)))
  7105. (after-change-functions (remove 'flyspell-after-change-function
  7106. after-change-functions))
  7107. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7108. (diff (abs (- level (length down-head) -1))))
  7109. (replace-match down-head nil t)
  7110. ;; Fixup tag positioning
  7111. (and org-auto-align-tags (org-set-tags nil t))
  7112. (if org-adapt-indentation (org-fixup-indentation diff))
  7113. (run-hooks 'org-after-demote-entry-hook)))
  7114. (defun org-cycle-level ()
  7115. "Cycle the level of an empty headline through possible states.
  7116. This goes first to child, then to parent, level, then up the hierarchy.
  7117. After top level, it switches back to sibling level."
  7118. (interactive)
  7119. (let ((org-adapt-indentation nil))
  7120. (when (org-point-at-end-of-empty-headline)
  7121. (setq this-command 'org-cycle-level) ; Only needed for caching
  7122. (let ((cur-level (org-current-level))
  7123. (prev-level (org-get-previous-line-level)))
  7124. (cond
  7125. ;; If first headline in file, promote to top-level.
  7126. ((= prev-level 0)
  7127. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7128. do (org-do-promote)))
  7129. ;; If same level as prev, demote one.
  7130. ((= prev-level cur-level)
  7131. (org-do-demote))
  7132. ;; If parent is top-level, promote to top level if not already.
  7133. ((= prev-level 1)
  7134. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7135. do (org-do-promote)))
  7136. ;; If top-level, return to prev-level.
  7137. ((= cur-level 1)
  7138. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7139. do (org-do-demote)))
  7140. ;; If less than prev-level, promote one.
  7141. ((< cur-level prev-level)
  7142. (org-do-promote))
  7143. ;; If deeper than prev-level, promote until higher than
  7144. ;; prev-level.
  7145. ((> cur-level prev-level)
  7146. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7147. do (org-do-promote))))
  7148. t))))
  7149. (defun org-map-tree (fun)
  7150. "Call FUN for every heading underneath the current one."
  7151. (org-back-to-heading)
  7152. (let ((level (funcall outline-level)))
  7153. (save-excursion
  7154. (funcall fun)
  7155. (while (and (progn
  7156. (outline-next-heading)
  7157. (> (funcall outline-level) level))
  7158. (not (eobp)))
  7159. (funcall fun)))))
  7160. (defun org-map-region (fun beg end)
  7161. "Call FUN for every heading between BEG and END."
  7162. (let ((org-ignore-region t))
  7163. (save-excursion
  7164. (setq end (copy-marker end))
  7165. (goto-char beg)
  7166. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7167. (< (point) end))
  7168. (funcall fun))
  7169. (while (and (progn
  7170. (outline-next-heading)
  7171. (< (point) end))
  7172. (not (eobp)))
  7173. (funcall fun)))))
  7174. (defvar org-property-end-re) ; silence byte-compiler
  7175. (defun org-fixup-indentation (diff)
  7176. "Change the indentation in the current entry by DIFF.
  7177. However, if any line in the current entry has no indentation, or if it
  7178. would end up with no indentation after the change, nothing at all is done."
  7179. (save-excursion
  7180. (let ((end (save-excursion (outline-next-heading)
  7181. (point-marker)))
  7182. (prohibit (if (> diff 0)
  7183. "^\\S-"
  7184. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7185. col)
  7186. (unless (save-excursion (end-of-line 1)
  7187. (re-search-forward prohibit end t))
  7188. (while (and (< (point) end)
  7189. (re-search-forward "^[ \t]+" end t))
  7190. (goto-char (match-end 0))
  7191. (setq col (current-column))
  7192. (if (< diff 0) (replace-match ""))
  7193. (org-indent-to-column (+ diff col))))
  7194. (move-marker end nil))))
  7195. (defun org-convert-to-odd-levels ()
  7196. "Convert an org-mode file with all levels allowed to one with odd levels.
  7197. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7198. level 5 etc."
  7199. (interactive)
  7200. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7201. (let ((outline-level 'org-outline-level)
  7202. (org-odd-levels-only nil) n)
  7203. (save-excursion
  7204. (goto-char (point-min))
  7205. (while (re-search-forward "^\\*\\*+ " nil t)
  7206. (setq n (- (length (match-string 0)) 2))
  7207. (while (>= (setq n (1- n)) 0)
  7208. (org-demote))
  7209. (end-of-line 1))))))
  7210. (defun org-convert-to-oddeven-levels ()
  7211. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7212. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7213. file contains a section with an even level, conversion would
  7214. destroy the structure of the file. An error is signaled in this
  7215. case."
  7216. (interactive)
  7217. (goto-char (point-min))
  7218. ;; First check if there are no even levels
  7219. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7220. (org-show-context t)
  7221. (error "Not all levels are odd in this file. Conversion not possible"))
  7222. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7223. (let ((outline-regexp org-outline-regexp)
  7224. (outline-level 'org-outline-level)
  7225. (org-odd-levels-only nil) n)
  7226. (save-excursion
  7227. (goto-char (point-min))
  7228. (while (re-search-forward "^\\*\\*+ " nil t)
  7229. (setq n (/ (1- (length (match-string 0))) 2))
  7230. (while (>= (setq n (1- n)) 0)
  7231. (org-promote))
  7232. (end-of-line 1))))))
  7233. (defun org-tr-level (n)
  7234. "Make N odd if required."
  7235. (if org-odd-levels-only (1+ (/ n 2)) n))
  7236. ;;; Vertical tree motion, cutting and pasting of subtrees
  7237. (defun org-move-subtree-up (&optional arg)
  7238. "Move the current subtree up past ARG headlines of the same level."
  7239. (interactive "p")
  7240. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7241. (defun org-move-subtree-down (&optional arg)
  7242. "Move the current subtree down past ARG headlines of the same level."
  7243. (interactive "p")
  7244. (setq arg (prefix-numeric-value arg))
  7245. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7246. 'org-get-last-sibling))
  7247. (ins-point (make-marker))
  7248. (cnt (abs arg))
  7249. (col (current-column))
  7250. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7251. ;; Select the tree
  7252. (org-back-to-heading)
  7253. (setq beg0 (point))
  7254. (save-excursion
  7255. (setq ne-beg (org-back-over-empty-lines))
  7256. (setq beg (point)))
  7257. (save-match-data
  7258. (save-excursion (outline-end-of-heading)
  7259. (setq folded (outline-invisible-p)))
  7260. (outline-end-of-subtree))
  7261. (outline-next-heading)
  7262. (setq ne-end (org-back-over-empty-lines))
  7263. (setq end (point))
  7264. (goto-char beg0)
  7265. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7266. ;; include less whitespace
  7267. (save-excursion
  7268. (goto-char beg)
  7269. (forward-line (- ne-beg ne-end))
  7270. (setq beg (point))))
  7271. ;; Find insertion point, with error handling
  7272. (while (> cnt 0)
  7273. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7274. (progn (goto-char beg0)
  7275. (user-error "Cannot move past superior level or buffer limit")))
  7276. (setq cnt (1- cnt)))
  7277. (if (> arg 0)
  7278. ;; Moving forward - still need to move over subtree
  7279. (progn (org-end-of-subtree t t)
  7280. (save-excursion
  7281. (org-back-over-empty-lines)
  7282. (or (bolp) (newline)))))
  7283. (setq ne-ins (org-back-over-empty-lines))
  7284. (move-marker ins-point (point))
  7285. (setq txt (buffer-substring beg end))
  7286. (org-save-markers-in-region beg end)
  7287. (delete-region beg end)
  7288. (org-remove-empty-overlays-at beg)
  7289. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7290. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7291. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7292. (let ((bbb (point)))
  7293. (insert-before-markers txt)
  7294. (org-reinstall-markers-in-region bbb)
  7295. (move-marker ins-point bbb))
  7296. (or (bolp) (insert "\n"))
  7297. (setq ins-end (point))
  7298. (goto-char ins-point)
  7299. (org-skip-whitespace)
  7300. (when (and (< arg 0)
  7301. (org-first-sibling-p)
  7302. (> ne-ins ne-beg))
  7303. ;; Move whitespace back to beginning
  7304. (save-excursion
  7305. (goto-char ins-end)
  7306. (let ((kill-whole-line t))
  7307. (kill-line (- ne-ins ne-beg)) (point)))
  7308. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7309. (move-marker ins-point nil)
  7310. (if folded
  7311. (hide-subtree)
  7312. (org-show-entry)
  7313. (show-children)
  7314. (org-cycle-hide-drawers 'children))
  7315. (org-clean-visibility-after-subtree-move)
  7316. ;; move back to the initial column we were at
  7317. (move-to-column col)))
  7318. (defvar org-subtree-clip ""
  7319. "Clipboard for cut and paste of subtrees.
  7320. This is actually only a copy of the kill, because we use the normal kill
  7321. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7322. (defvar org-subtree-clip-folded nil
  7323. "Was the last copied subtree folded?
  7324. This is used to fold the tree back after pasting.")
  7325. (defun org-cut-subtree (&optional n)
  7326. "Cut the current subtree into the clipboard.
  7327. With prefix arg N, cut this many sequential subtrees.
  7328. This is a short-hand for marking the subtree and then cutting it."
  7329. (interactive "p")
  7330. (org-copy-subtree n 'cut))
  7331. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7332. "Cut the current subtree into the clipboard.
  7333. With prefix arg N, cut this many sequential subtrees.
  7334. This is a short-hand for marking the subtree and then copying it.
  7335. If CUT is non-nil, actually cut the subtree.
  7336. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7337. of some markers in the region, even if CUT is non-nil. This is
  7338. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7339. (interactive "p")
  7340. (let (beg end folded (beg0 (point)))
  7341. (if (org-called-interactively-p 'any)
  7342. (org-back-to-heading nil) ; take what looks like a subtree
  7343. (org-back-to-heading t)) ; take what is really there
  7344. (setq beg (point))
  7345. (skip-chars-forward " \t\r\n")
  7346. (save-match-data
  7347. (if nosubtrees
  7348. (outline-next-heading)
  7349. (save-excursion (outline-end-of-heading)
  7350. (setq folded (outline-invisible-p)))
  7351. (condition-case nil
  7352. (org-forward-heading-same-level (1- n) t)
  7353. (error nil))
  7354. (org-end-of-subtree t t)))
  7355. (setq end (point))
  7356. (goto-char beg0)
  7357. (when (> end beg)
  7358. (setq org-subtree-clip-folded folded)
  7359. (when (or cut force-store-markers)
  7360. (org-save-markers-in-region beg end))
  7361. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7362. (setq org-subtree-clip (current-kill 0))
  7363. (message "%s: Subtree(s) with %d characters"
  7364. (if cut "Cut" "Copied")
  7365. (length org-subtree-clip)))))
  7366. (defun org-paste-subtree (&optional level tree for-yank)
  7367. "Paste the clipboard as a subtree, with modification of headline level.
  7368. The entire subtree is promoted or demoted in order to match a new headline
  7369. level.
  7370. If the cursor is at the beginning of a headline, the same level as
  7371. that headline is used to paste the tree.
  7372. If not, the new level is derived from the *visible* headings
  7373. before and after the insertion point, and taken to be the inferior headline
  7374. level of the two. So if the previous visible heading is level 3 and the
  7375. next is level 4 (or vice versa), level 4 will be used for insertion.
  7376. This makes sure that the subtree remains an independent subtree and does
  7377. not swallow low level entries.
  7378. You can also force a different level, either by using a numeric prefix
  7379. argument, or by inserting the heading marker by hand. For example, if the
  7380. cursor is after \"*****\", then the tree will be shifted to level 5.
  7381. If optional TREE is given, use this text instead of the kill ring.
  7382. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7383. move back over whitespace before inserting, and move point to the end of
  7384. the inserted text when done."
  7385. (interactive "P")
  7386. (setq tree (or tree (and kill-ring (current-kill 0))))
  7387. (unless (org-kill-is-subtree-p tree)
  7388. (user-error "%s"
  7389. (substitute-command-keys
  7390. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7391. (org-with-limited-levels
  7392. (let* ((visp (not (outline-invisible-p)))
  7393. (txt tree)
  7394. (^re_ "\\(\\*+\\)[ \t]*")
  7395. (old-level (if (string-match org-outline-regexp-bol txt)
  7396. (- (match-end 0) (match-beginning 0) 1)
  7397. -1))
  7398. (force-level (cond (level (prefix-numeric-value level))
  7399. ((and (looking-at "[ \t]*$")
  7400. (string-match
  7401. "^\\*+$" (buffer-substring
  7402. (point-at-bol) (point))))
  7403. (- (match-end 1) (match-beginning 1)))
  7404. ((and (bolp)
  7405. (looking-at org-outline-regexp))
  7406. (- (match-end 0) (point) 1))))
  7407. (previous-level (save-excursion
  7408. (condition-case nil
  7409. (progn
  7410. (outline-previous-visible-heading 1)
  7411. (if (looking-at ^re_)
  7412. (- (match-end 0) (match-beginning 0) 1)
  7413. 1))
  7414. (error 1))))
  7415. (next-level (save-excursion
  7416. (condition-case nil
  7417. (progn
  7418. (or (looking-at org-outline-regexp)
  7419. (outline-next-visible-heading 1))
  7420. (if (looking-at ^re_)
  7421. (- (match-end 0) (match-beginning 0) 1)
  7422. 1))
  7423. (error 1))))
  7424. (new-level (or force-level (max previous-level next-level)))
  7425. (shift (if (or (= old-level -1)
  7426. (= new-level -1)
  7427. (= old-level new-level))
  7428. 0
  7429. (- new-level old-level)))
  7430. (delta (if (> shift 0) -1 1))
  7431. (func (if (> shift 0) 'org-demote 'org-promote))
  7432. (org-odd-levels-only nil)
  7433. beg end newend)
  7434. ;; Remove the forced level indicator
  7435. (if force-level
  7436. (delete-region (point-at-bol) (point)))
  7437. ;; Paste
  7438. (beginning-of-line (if (bolp) 1 2))
  7439. (setq beg (point))
  7440. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7441. (insert-before-markers txt)
  7442. (unless (string-match "\n\\'" txt) (insert "\n"))
  7443. (setq newend (point))
  7444. (org-reinstall-markers-in-region beg)
  7445. (setq end (point))
  7446. (goto-char beg)
  7447. (skip-chars-forward " \t\n\r")
  7448. (setq beg (point))
  7449. (if (and (outline-invisible-p) visp)
  7450. (save-excursion (outline-show-heading)))
  7451. ;; Shift if necessary
  7452. (unless (= shift 0)
  7453. (save-restriction
  7454. (narrow-to-region beg end)
  7455. (while (not (= shift 0))
  7456. (org-map-region func (point-min) (point-max))
  7457. (setq shift (+ delta shift)))
  7458. (goto-char (point-min))
  7459. (setq newend (point-max))))
  7460. (when (or (org-called-interactively-p 'interactive) for-yank)
  7461. (message "Clipboard pasted as level %d subtree" new-level))
  7462. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7463. kill-ring
  7464. (eq org-subtree-clip (current-kill 0))
  7465. org-subtree-clip-folded)
  7466. ;; The tree was folded before it was killed/copied
  7467. (hide-subtree))
  7468. (and for-yank (goto-char newend)))))
  7469. (defun org-kill-is-subtree-p (&optional txt)
  7470. "Check if the current kill is an outline subtree, or a set of trees.
  7471. Returns nil if kill does not start with a headline, or if the first
  7472. headline level is not the largest headline level in the tree.
  7473. So this will actually accept several entries of equal levels as well,
  7474. which is OK for `org-paste-subtree'.
  7475. If optional TXT is given, check this string instead of the current kill."
  7476. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7477. (re (org-get-limited-outline-regexp))
  7478. (^re (concat "^" re))
  7479. (start-level (and kill
  7480. (string-match
  7481. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7482. kill)
  7483. (- (match-end 2) (match-beginning 2) 1)))
  7484. (start (1+ (or (match-beginning 2) -1))))
  7485. (if (not start-level)
  7486. (progn
  7487. nil) ;; does not even start with a heading
  7488. (catch 'exit
  7489. (while (setq start (string-match ^re kill (1+ start)))
  7490. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7491. (throw 'exit nil)))
  7492. t))))
  7493. (defvar org-markers-to-move nil
  7494. "Markers that should be moved with a cut-and-paste operation.
  7495. Those markers are stored together with their positions relative to
  7496. the start of the region.")
  7497. (defun org-save-markers-in-region (beg end)
  7498. "Check markers in region.
  7499. If these markers are between BEG and END, record their position relative
  7500. to BEG, so that after moving the block of text, we can put the markers back
  7501. into place.
  7502. This function gets called just before an entry or tree gets cut from the
  7503. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7504. called immediately, to move the markers with the entries."
  7505. (setq org-markers-to-move nil)
  7506. (when (featurep 'org-clock)
  7507. (org-clock-save-markers-for-cut-and-paste beg end))
  7508. (when (featurep 'org-agenda)
  7509. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7510. (defun org-check-and-save-marker (marker beg end)
  7511. "Check if MARKER is between BEG and END.
  7512. If yes, remember the marker and the distance to BEG."
  7513. (when (and (marker-buffer marker)
  7514. (equal (marker-buffer marker) (current-buffer)))
  7515. (if (and (>= marker beg) (< marker end))
  7516. (push (cons marker (- marker beg)) org-markers-to-move))))
  7517. (defun org-reinstall-markers-in-region (beg)
  7518. "Move all remembered markers to their position relative to BEG."
  7519. (mapc (lambda (x)
  7520. (move-marker (car x) (+ beg (cdr x))))
  7521. org-markers-to-move)
  7522. (setq org-markers-to-move nil))
  7523. (defun org-narrow-to-subtree ()
  7524. "Narrow buffer to the current subtree."
  7525. (interactive)
  7526. (save-excursion
  7527. (save-match-data
  7528. (org-with-limited-levels
  7529. (narrow-to-region
  7530. (progn (org-back-to-heading t) (point))
  7531. (progn (org-end-of-subtree t t)
  7532. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7533. (point)))))))
  7534. (defun org-narrow-to-block ()
  7535. "Narrow buffer to the current block."
  7536. (interactive)
  7537. (let* ((case-fold-search t)
  7538. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7539. "^[ \t]*#\\+end_.*")))
  7540. (if blockp
  7541. (narrow-to-region (car blockp) (cdr blockp))
  7542. (user-error "Not in a block"))))
  7543. (eval-when-compile
  7544. (defvar org-property-drawer-re))
  7545. (defvar org-property-start-re) ;; defined below
  7546. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7547. "Clone the task (subtree) at point N times.
  7548. The clones will be inserted as siblings.
  7549. In interactive use, the user will be prompted for the number of
  7550. clones to be produced. When called with a universal prefix argument
  7551. and if the entry has a timestamp, the user will also be prompted for
  7552. a time shift, which may be a repeater as used in time stamps, for
  7553. example `+3d'.
  7554. When a valid repeater is given and the entry contains any time
  7555. stamps, the clones will become a sequence in time, with time
  7556. stamps in the subtree shifted for each clone produced. If SHIFT
  7557. is nil or the empty string, time stamps will be left alone. The
  7558. ID property of the original subtree is removed.
  7559. If the original subtree did contain time stamps with a repeater,
  7560. the following will happen:
  7561. - the repeater will be removed in each clone
  7562. - an additional clone will be produced, with the current, unshifted
  7563. date(s) in the entry.
  7564. - the original entry will be placed *after* all the clones, with
  7565. repeater intact.
  7566. - the start days in the repeater in the original entry will be shifted
  7567. to past the last clone.
  7568. In this way you can spell out a number of instances of a repeating task,
  7569. and still retain the repeater to cover future instances of the task."
  7570. (interactive "nNumber of clones to produce: ")
  7571. (let ((shift
  7572. (or shift
  7573. (if (and (equal current-prefix-arg '(4))
  7574. (save-excursion
  7575. (re-search-forward org-ts-regexp-both
  7576. (save-excursion
  7577. (org-end-of-subtree t)
  7578. (point)) t)))
  7579. (read-from-minibuffer
  7580. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7581. ""))) ;; No time shift
  7582. (n-no-remove -1)
  7583. (drawer-re org-drawer-regexp)
  7584. beg end template task idprop
  7585. shift-n shift-what doshift nmin nmax)
  7586. (if (not (and (integerp n) (> n 0)))
  7587. (error "Invalid number of replications %s" n))
  7588. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7589. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7590. shift)))
  7591. (error "Invalid shift specification %s" shift))
  7592. (when doshift
  7593. (setq shift-n (string-to-number (match-string 1 shift))
  7594. shift-what (cdr (assoc (match-string 2 shift)
  7595. '(("d" . day) ("w" . week)
  7596. ("m" . month) ("y" . year))))))
  7597. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7598. (setq nmin 1 nmax n)
  7599. (org-back-to-heading t)
  7600. (setq beg (point))
  7601. (setq idprop (org-entry-get nil "ID"))
  7602. (org-end-of-subtree t t)
  7603. (or (bolp) (insert "\n"))
  7604. (setq end (point))
  7605. (setq template (buffer-substring beg end))
  7606. (when (and doshift
  7607. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7608. (delete-region beg end)
  7609. (setq end beg)
  7610. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7611. (goto-char end)
  7612. (loop for n from nmin to nmax do
  7613. ;; prepare clone
  7614. (with-temp-buffer
  7615. (insert template)
  7616. (org-mode)
  7617. (goto-char (point-min))
  7618. (org-show-subtree)
  7619. (and idprop (if org-clone-delete-id
  7620. (org-entry-delete nil "ID")
  7621. (org-id-get-create t)))
  7622. (unless (= n 0)
  7623. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7624. (kill-whole-line))
  7625. (goto-char (point-min))
  7626. (while (re-search-forward drawer-re nil t)
  7627. (mapc (lambda (d)
  7628. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7629. (goto-char (point-min))
  7630. (when doshift
  7631. (while (re-search-forward org-ts-regexp-both nil t)
  7632. (org-timestamp-change (* n shift-n) shift-what))
  7633. (unless (= n n-no-remove)
  7634. (goto-char (point-min))
  7635. (while (re-search-forward org-ts-regexp nil t)
  7636. (save-excursion
  7637. (goto-char (match-beginning 0))
  7638. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7639. (delete-region (match-beginning 1) (match-end 1)))))))
  7640. (setq task (buffer-string)))
  7641. (insert task))
  7642. (goto-char beg)))
  7643. ;;; Outline Sorting
  7644. (defun org-sort (with-case)
  7645. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7646. Optional argument WITH-CASE means sort case-sensitively."
  7647. (interactive "P")
  7648. (cond
  7649. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7650. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7651. (t
  7652. (org-call-with-arg 'org-sort-entries with-case))))
  7653. (defun org-sort-remove-invisible (s)
  7654. "Remove invisible links from string S."
  7655. (remove-text-properties 0 (length s) org-rm-props s)
  7656. (while (string-match org-bracket-link-regexp s)
  7657. (setq s (replace-match (if (match-end 2)
  7658. (match-string 3 s)
  7659. (match-string 1 s)) t t s)))
  7660. (let ((st (format " %s " s)))
  7661. (while (string-match org-emph-re st)
  7662. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7663. (setq s (substring st 1 -1)))
  7664. s)
  7665. (defvar org-priority-regexp) ; defined later in the file
  7666. (defvar org-after-sorting-entries-or-items-hook nil
  7667. "Hook that is run after a bunch of entries or items have been sorted.
  7668. When children are sorted, the cursor is in the parent line when this
  7669. hook gets called. When a region or a plain list is sorted, the cursor
  7670. will be in the first entry of the sorted region/list.")
  7671. (defun org-sort-entries
  7672. (&optional with-case sorting-type getkey-func compare-func property)
  7673. "Sort entries on a certain level of an outline tree.
  7674. If there is an active region, the entries in the region are sorted.
  7675. Else, if the cursor is before the first entry, sort the top-level items.
  7676. Else, the children of the entry at point are sorted.
  7677. Sorting can be alphabetically, numerically, by date/time as given by
  7678. a time stamp, by a property, by priority order, or by a custom function.
  7679. The command prompts for the sorting type unless it has been given to the
  7680. function through the SORTING-TYPE argument, which needs to be a character,
  7681. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7682. precise meaning of each character:
  7683. n Numerically, by converting the beginning of the entry/item to a number.
  7684. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7685. o By order of TODO keywords.
  7686. t By date/time, either the first active time stamp in the entry, or, if
  7687. none exist, by the first inactive one.
  7688. s By the scheduled date/time.
  7689. d By deadline date/time.
  7690. c By creation time, which is assumed to be the first inactive time stamp
  7691. at the beginning of a line.
  7692. p By priority according to the cookie.
  7693. r By the value of a property.
  7694. Capital letters will reverse the sort order.
  7695. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7696. called with point at the beginning of the record. It must return either
  7697. a string or a number that should serve as the sorting key for that record.
  7698. Comparing entries ignores case by default. However, with an optional argument
  7699. WITH-CASE, the sorting considers case as well.
  7700. Sorting is done against the visible part of the headlines, it ignores hidden
  7701. links."
  7702. (interactive "P")
  7703. (let ((case-func (if with-case 'identity 'downcase))
  7704. (cmstr
  7705. ;; The clock marker is lost when using `sort-subr', let's
  7706. ;; store the clocking string.
  7707. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7708. (save-excursion
  7709. (goto-char org-clock-marker)
  7710. (looking-back "^.*") (match-string-no-properties 0))))
  7711. start beg end stars re re2
  7712. txt what tmp)
  7713. ;; Find beginning and end of region to sort
  7714. (cond
  7715. ((org-region-active-p)
  7716. ;; we will sort the region
  7717. (setq end (region-end)
  7718. what "region")
  7719. (goto-char (region-beginning))
  7720. (if (not (org-at-heading-p)) (outline-next-heading))
  7721. (setq start (point)))
  7722. ((or (org-at-heading-p)
  7723. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7724. ;; we will sort the children of the current headline
  7725. (org-back-to-heading)
  7726. (setq start (point)
  7727. end (progn (org-end-of-subtree t t)
  7728. (or (bolp) (insert "\n"))
  7729. (org-back-over-empty-lines)
  7730. (point))
  7731. what "children")
  7732. (goto-char start)
  7733. (show-subtree)
  7734. (outline-next-heading))
  7735. (t
  7736. ;; we will sort the top-level entries in this file
  7737. (goto-char (point-min))
  7738. (or (org-at-heading-p) (outline-next-heading))
  7739. (setq start (point))
  7740. (goto-char (point-max))
  7741. (beginning-of-line 1)
  7742. (when (looking-at ".*?\\S-")
  7743. ;; File ends in a non-white line
  7744. (end-of-line 1)
  7745. (insert "\n"))
  7746. (setq end (point-max))
  7747. (setq what "top-level")
  7748. (goto-char start)
  7749. (show-all)))
  7750. (setq beg (point))
  7751. (if (>= beg end) (user-error "Nothing to sort"))
  7752. (looking-at "\\(\\*+\\)")
  7753. (setq stars (match-string 1)
  7754. re (concat "^" (regexp-quote stars) " +")
  7755. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7756. txt (buffer-substring beg end))
  7757. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7758. (if (and (not (equal stars "*")) (string-match re2 txt))
  7759. (user-error "Region to sort contains a level above the first entry"))
  7760. (unless sorting-type
  7761. (message
  7762. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7763. [t]ime [s]cheduled [d]eadline [c]reated
  7764. A/N/P/R/O/F/T/S/D/C means reversed:"
  7765. what)
  7766. (setq sorting-type (read-char-exclusive))
  7767. (unless getkey-func
  7768. (and (= (downcase sorting-type) ?f)
  7769. (setq getkey-func
  7770. (org-icompleting-read "Sort using function: "
  7771. obarray 'fboundp t nil nil))
  7772. (setq getkey-func (intern getkey-func))))
  7773. (and (= (downcase sorting-type) ?r)
  7774. (not property)
  7775. (setq property
  7776. (org-icompleting-read "Property: "
  7777. (mapcar 'list (org-buffer-property-keys t))
  7778. nil t))))
  7779. (message "Sorting entries...")
  7780. (save-restriction
  7781. (narrow-to-region start end)
  7782. (let ((dcst (downcase sorting-type))
  7783. (case-fold-search nil)
  7784. (now (current-time)))
  7785. (sort-subr
  7786. (/= dcst sorting-type)
  7787. ;; This function moves to the beginning character of the "record" to
  7788. ;; be sorted.
  7789. (lambda nil
  7790. (if (re-search-forward re nil t)
  7791. (goto-char (match-beginning 0))
  7792. (goto-char (point-max))))
  7793. ;; This function moves to the last character of the "record" being
  7794. ;; sorted.
  7795. (lambda nil
  7796. (save-match-data
  7797. (condition-case nil
  7798. (outline-forward-same-level 1)
  7799. (error
  7800. (goto-char (point-max))))))
  7801. ;; This function returns the value that gets sorted against.
  7802. (lambda nil
  7803. (cond
  7804. ((= dcst ?n)
  7805. (if (looking-at org-complex-heading-regexp)
  7806. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7807. nil))
  7808. ((= dcst ?a)
  7809. (if (looking-at org-complex-heading-regexp)
  7810. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7811. nil))
  7812. ((= dcst ?t)
  7813. (let ((end (save-excursion (outline-next-heading) (point))))
  7814. (if (or (re-search-forward org-ts-regexp end t)
  7815. (re-search-forward org-ts-regexp-both end t))
  7816. (org-time-string-to-seconds (match-string 0))
  7817. (org-float-time now))))
  7818. ((= dcst ?c)
  7819. (let ((end (save-excursion (outline-next-heading) (point))))
  7820. (if (re-search-forward
  7821. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7822. end t)
  7823. (org-time-string-to-seconds (match-string 0))
  7824. (org-float-time now))))
  7825. ((= dcst ?s)
  7826. (let ((end (save-excursion (outline-next-heading) (point))))
  7827. (if (re-search-forward org-scheduled-time-regexp end t)
  7828. (org-time-string-to-seconds (match-string 1))
  7829. (org-float-time now))))
  7830. ((= dcst ?d)
  7831. (let ((end (save-excursion (outline-next-heading) (point))))
  7832. (if (re-search-forward org-deadline-time-regexp end t)
  7833. (org-time-string-to-seconds (match-string 1))
  7834. (org-float-time now))))
  7835. ((= dcst ?p)
  7836. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7837. (string-to-char (match-string 2))
  7838. org-default-priority))
  7839. ((= dcst ?r)
  7840. (or (org-entry-get nil property) ""))
  7841. ((= dcst ?o)
  7842. (if (looking-at org-complex-heading-regexp)
  7843. (- 9999 (length (member (match-string 2)
  7844. org-todo-keywords-1)))))
  7845. ((= dcst ?f)
  7846. (if getkey-func
  7847. (progn
  7848. (setq tmp (funcall getkey-func))
  7849. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7850. tmp)
  7851. (error "Invalid key function `%s'" getkey-func)))
  7852. (t (error "Invalid sorting type `%c'" sorting-type))))
  7853. nil
  7854. (cond
  7855. ((= dcst ?a) 'string<)
  7856. ((= dcst ?f) compare-func)
  7857. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7858. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7859. ;; Reset the clock marker if needed
  7860. (when cmstr
  7861. (save-excursion
  7862. (goto-char start)
  7863. (search-forward cmstr nil t)
  7864. (move-marker org-clock-marker (point))))
  7865. (message "Sorting entries...done")))
  7866. (defun org-do-sort (table what &optional with-case sorting-type)
  7867. "Sort TABLE of WHAT according to SORTING-TYPE.
  7868. The user will be prompted for the SORTING-TYPE if the call to this
  7869. function does not specify it. WHAT is only for the prompt, to indicate
  7870. what is being sorted. The sorting key will be extracted from
  7871. the car of the elements of the table.
  7872. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7873. (unless sorting-type
  7874. (message
  7875. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7876. what)
  7877. (setq sorting-type (read-char-exclusive)))
  7878. (let ((dcst (downcase sorting-type))
  7879. extractfun comparefun)
  7880. ;; Define the appropriate functions
  7881. (cond
  7882. ((= dcst ?n)
  7883. (setq extractfun 'string-to-number
  7884. comparefun (if (= dcst sorting-type) '< '>)))
  7885. ((= dcst ?a)
  7886. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7887. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7888. comparefun (if (= dcst sorting-type)
  7889. 'string<
  7890. (lambda (a b) (and (not (string< a b))
  7891. (not (string= a b)))))))
  7892. ((= dcst ?t)
  7893. (setq extractfun
  7894. (lambda (x)
  7895. (if (or (string-match org-ts-regexp x)
  7896. (string-match org-ts-regexp-both x))
  7897. (org-float-time
  7898. (org-time-string-to-time (match-string 0 x)))
  7899. 0))
  7900. comparefun (if (= dcst sorting-type) '< '>)))
  7901. (t (error "Invalid sorting type `%c'" sorting-type)))
  7902. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7903. table)
  7904. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7905. ;;; The orgstruct minor mode
  7906. ;; Define a minor mode which can be used in other modes in order to
  7907. ;; integrate the org-mode structure editing commands.
  7908. ;; This is really a hack, because the org-mode structure commands use
  7909. ;; keys which normally belong to the major mode. Here is how it
  7910. ;; works: The minor mode defines all the keys necessary to operate the
  7911. ;; structure commands, but wraps the commands into a function which
  7912. ;; tests if the cursor is currently at a headline or a plain list
  7913. ;; item. If that is the case, the structure command is used,
  7914. ;; temporarily setting many Org-mode variables like regular
  7915. ;; expressions for filling etc. However, when any of those keys is
  7916. ;; used at a different location, function uses `key-binding' to look
  7917. ;; up if the key has an associated command in another currently active
  7918. ;; keymap (minor modes, major mode, global), and executes that
  7919. ;; command. There might be problems if any of the keys is otherwise
  7920. ;; used as a prefix key.
  7921. (defcustom orgstruct-heading-prefix-regexp nil
  7922. "Regexp that matches the custom prefix of Org headlines in
  7923. orgstruct(++)-mode."
  7924. :group 'org
  7925. :version "24.4"
  7926. :package-version '(Org . "8.0")
  7927. :type 'string)
  7928. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7929. (defcustom orgstruct-setup-hook nil
  7930. "Hook run after orgstruct-mode-map is filled."
  7931. :group 'org
  7932. :version "24.4"
  7933. :package-version '(Org . "8.0")
  7934. :type 'hook)
  7935. (defvar orgstruct-initialized nil)
  7936. (defvar org-local-vars nil
  7937. "List of local variables, for use by `orgstruct-mode'.")
  7938. ;;;###autoload
  7939. (define-minor-mode orgstruct-mode
  7940. "Toggle the minor mode `orgstruct-mode'.
  7941. This mode is for using Org-mode structure commands in other
  7942. modes. The following keys behave as if Org-mode were active, if
  7943. the cursor is on a headline, or on a plain list item (both as
  7944. defined by Org-mode)."
  7945. nil " OrgStruct" (make-sparse-keymap)
  7946. (funcall (if orgstruct-mode
  7947. 'add-to-invisibility-spec
  7948. 'remove-from-invisibility-spec)
  7949. '(outline . t))
  7950. (when orgstruct-mode
  7951. (org-load-modules-maybe)
  7952. (unless orgstruct-initialized
  7953. (orgstruct-setup)
  7954. (setq orgstruct-initialized t))))
  7955. ;;;###autoload
  7956. (defun turn-on-orgstruct ()
  7957. "Unconditionally turn on `orgstruct-mode'."
  7958. (orgstruct-mode 1))
  7959. (defvar org-fb-vars nil)
  7960. (make-variable-buffer-local 'org-fb-vars)
  7961. (defun orgstruct++-mode (&optional arg)
  7962. "Toggle `orgstruct-mode', the enhanced version of it.
  7963. In addition to setting orgstruct-mode, this also exports all
  7964. indentation and autofilling variables from org-mode into the
  7965. buffer. It will also recognize item context in multiline items."
  7966. (interactive "P")
  7967. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7968. (if (< arg 1)
  7969. (progn (orgstruct-mode -1)
  7970. (mapc (lambda(v)
  7971. (org-set-local (car v)
  7972. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7973. org-fb-vars))
  7974. (orgstruct-mode 1)
  7975. (setq org-fb-vars nil)
  7976. (let (var val)
  7977. (mapc
  7978. (lambda (x)
  7979. (when (string-match
  7980. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7981. (symbol-name (car x)))
  7982. (setq var (car x) val (nth 1 x))
  7983. (push (list var `(quote ,(eval var))) org-fb-vars)
  7984. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7985. org-local-vars)
  7986. (org-set-local 'orgstruct-is-++ t))))
  7987. (defvar orgstruct-is-++ nil
  7988. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7989. (make-variable-buffer-local 'orgstruct-is-++)
  7990. ;;;###autoload
  7991. (defun turn-on-orgstruct++ ()
  7992. "Unconditionally turn on `orgstruct++-mode'."
  7993. (orgstruct++-mode 1))
  7994. (defun orgstruct-error ()
  7995. "Error when there is no default binding for a structure key."
  7996. (interactive)
  7997. (funcall (if (fboundp 'user-error)
  7998. 'user-error
  7999. 'error)
  8000. "This key has no function outside structure elements"))
  8001. (defun orgstruct-setup ()
  8002. "Setup orgstruct keymap."
  8003. (dolist (cell '((org-demote . t)
  8004. (org-metaleft . t)
  8005. (org-metaright . t)
  8006. (org-promote . t)
  8007. (org-shiftmetaleft . t)
  8008. (org-shiftmetaright . t)
  8009. org-backward-element
  8010. org-backward-heading-same-level
  8011. org-ctrl-c-ret
  8012. org-ctrl-c-minus
  8013. org-ctrl-c-star
  8014. org-cycle
  8015. org-forward-heading-same-level
  8016. org-insert-heading
  8017. org-insert-heading-respect-content
  8018. org-kill-note-or-show-branches
  8019. org-mark-subtree
  8020. org-meta-return
  8021. org-metadown
  8022. org-metaup
  8023. org-narrow-to-subtree
  8024. org-promote-subtree
  8025. org-reveal
  8026. org-shiftdown
  8027. org-shiftleft
  8028. org-shiftmetadown
  8029. org-shiftmetaup
  8030. org-shiftright
  8031. org-shifttab
  8032. org-shifttab
  8033. org-shiftup
  8034. org-show-subtree
  8035. org-sort
  8036. org-up-element
  8037. outline-demote
  8038. outline-next-visible-heading
  8039. outline-previous-visible-heading
  8040. outline-promote
  8041. outline-up-heading
  8042. show-children))
  8043. (let ((f (or (car-safe cell) cell))
  8044. (disable-when-heading-prefix (cdr-safe cell)))
  8045. (when (fboundp f)
  8046. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8047. (where-is-internal f outline-mode-map)))
  8048. ;; TODO use local-function-key-map
  8049. (dolist (rep '(("<tab>" . "TAB")
  8050. ("<return>" . "RET")
  8051. ("<escape>" . "ESC")
  8052. ("<delete>" . "DEL")))
  8053. (setq binding (read-kbd-macro
  8054. (let ((case-fold-search))
  8055. (replace-regexp-in-string
  8056. (regexp-quote (cdr rep))
  8057. (car rep)
  8058. (key-description binding))))))
  8059. (let ((key (lookup-key orgstruct-mode-map binding)))
  8060. (when (or (not key) (numberp key))
  8061. (condition-case nil
  8062. (org-defkey orgstruct-mode-map
  8063. binding
  8064. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8065. (error nil))))))))
  8066. (run-hooks 'orgstruct-setup-hook))
  8067. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8068. "Create a function for binding in the structure minor mode.
  8069. FUN is the command to call inside a table. KEY is the key that
  8070. should be checked in for a command to execute outside of tables.
  8071. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8072. if `orgstruct-heading-prefix-regexp' is non-nil."
  8073. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8074. (let ((nname name)
  8075. (i 0))
  8076. (while (fboundp (intern nname))
  8077. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8078. (setq name (intern nname)))
  8079. (eval
  8080. (let ((bindings '((org-heading-regexp
  8081. (concat "^"
  8082. orgstruct-heading-prefix-regexp
  8083. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8084. (org-outline-regexp
  8085. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8086. (org-outline-regexp-bol
  8087. (concat "^" org-outline-regexp))
  8088. (outline-regexp org-outline-regexp)
  8089. (outline-heading-end-regexp "\n")
  8090. (outline-level 'org-outline-level)
  8091. (outline-heading-alist))))
  8092. `(defun ,name (arg)
  8093. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8094. "Outside of structure, run the binding of `"
  8095. (key-description key) "'."
  8096. (when disable-when-heading-prefix
  8097. (concat
  8098. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8099. "back to the default binding due to limitations of Org's implementation of\n"
  8100. "`" (symbol-name fun) "'.")))
  8101. (interactive "p")
  8102. (let* ((disable
  8103. ,(when disable-when-heading-prefix
  8104. '(and orgstruct-heading-prefix-regexp
  8105. (not (string= orgstruct-heading-prefix-regexp "")))))
  8106. (fallback
  8107. (or disable
  8108. (not
  8109. (let* ,bindings
  8110. (org-context-p 'headline 'item
  8111. ,(when (memq fun '(org-insert-heading))
  8112. '(when orgstruct-is-++
  8113. 'item-body))))))))
  8114. (if fallback
  8115. (let* ((orgstruct-mode)
  8116. (binding
  8117. (loop with key = ,key
  8118. for rep in
  8119. '(nil
  8120. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8121. ("<\\([^>]*\\)return>" . "\\1RET")
  8122. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8123. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8124. do
  8125. (when rep
  8126. (setq key (read-kbd-macro
  8127. (let ((case-fold-search))
  8128. (replace-regexp-in-string
  8129. (car rep)
  8130. (cdr rep)
  8131. (key-description key))))))
  8132. thereis (key-binding key))))
  8133. (if (keymapp binding)
  8134. (set-temporary-overlay-map binding)
  8135. (let ((func (or binding
  8136. (unless disable
  8137. 'orgstruct-error))))
  8138. (when func
  8139. (call-interactively func)))))
  8140. (org-run-like-in-org-mode
  8141. (lambda ()
  8142. (interactive)
  8143. (let* ,bindings
  8144. (call-interactively ',fun)))))))))
  8145. name))
  8146. (defun org-contextualize-keys (alist contexts)
  8147. "Return valid elements in ALIST depending on CONTEXTS.
  8148. `org-agenda-custom-commands' or `org-capture-templates' are the
  8149. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8150. or `org-capture-templates-contexts' are the associated contexts
  8151. definitions."
  8152. (let ((contexts
  8153. ;; normalize contexts
  8154. (mapcar
  8155. (lambda(c) (cond ((listp (cadr c))
  8156. (list (car c) (car c) (cadr c)))
  8157. ((string= "" (cadr c))
  8158. (list (car c) (car c) (caddr c)))
  8159. (t c))) contexts))
  8160. (a alist) c r s)
  8161. ;; loop over all commands or templates
  8162. (while (setq c (pop a))
  8163. (let (vrules repl)
  8164. (cond
  8165. ((not (assoc (car c) contexts))
  8166. (push c r))
  8167. ((and (assoc (car c) contexts)
  8168. (setq vrules (org-contextualize-validate-key
  8169. (car c) contexts)))
  8170. (mapc (lambda (vr)
  8171. (when (not (equal (car vr) (cadr vr)))
  8172. (setq repl vr))) vrules)
  8173. (if (not repl) (push c r)
  8174. (push (cadr repl) s)
  8175. (push
  8176. (cons (car c)
  8177. (cdr (or (assoc (cadr repl) alist)
  8178. (error "Undefined key `%s' as contextual replacement for `%s'"
  8179. (cadr repl) (car c)))))
  8180. r))))))
  8181. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8182. (delq
  8183. nil
  8184. (delete-dups
  8185. (mapcar (lambda (x)
  8186. (let ((tpl (car x)))
  8187. (when (not (delq
  8188. nil
  8189. (mapcar (lambda(y)
  8190. (equal y tpl)) s))) x)))
  8191. (reverse r))))))
  8192. (defun org-contextualize-validate-key (key contexts)
  8193. "Check CONTEXTS for agenda or capture KEY."
  8194. (let (r rr res)
  8195. (while (setq r (pop contexts))
  8196. (mapc
  8197. (lambda (rr)
  8198. (when
  8199. (and (equal key (car r))
  8200. (if (functionp rr) (funcall rr)
  8201. (or (and (eq (car rr) 'in-file)
  8202. (buffer-file-name)
  8203. (string-match (cdr rr) (buffer-file-name)))
  8204. (and (eq (car rr) 'in-mode)
  8205. (string-match (cdr rr) (symbol-name major-mode)))
  8206. (and (eq (car rr) 'in-buffer)
  8207. (string-match (cdr rr) (buffer-name)))
  8208. (when (and (eq (car rr) 'not-in-file)
  8209. (buffer-file-name))
  8210. (not (string-match (cdr rr) (buffer-file-name))))
  8211. (when (eq (car rr) 'not-in-mode)
  8212. (not (string-match (cdr rr) (symbol-name major-mode))))
  8213. (when (eq (car rr) 'not-in-buffer)
  8214. (not (string-match (cdr rr) (buffer-name)))))))
  8215. (push r res)))
  8216. (car (last r))))
  8217. (delete-dups (delq nil res))))
  8218. (defun org-context-p (&rest contexts)
  8219. "Check if local context is any of CONTEXTS.
  8220. Possible values in the list of contexts are `table', `headline', and `item'."
  8221. (let ((pos (point)))
  8222. (goto-char (point-at-bol))
  8223. (prog1 (or (and (memq 'table contexts)
  8224. (looking-at "[ \t]*|"))
  8225. (and (memq 'headline contexts)
  8226. (looking-at org-outline-regexp))
  8227. (and (memq 'item contexts)
  8228. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8229. (and (memq 'item-body contexts)
  8230. (org-in-item-p)))
  8231. (goto-char pos))))
  8232. (defun org-get-local-variables ()
  8233. "Return a list of all local variables in an Org mode buffer."
  8234. (let (varlist)
  8235. (with-current-buffer (get-buffer-create "*Org tmp*")
  8236. (erase-buffer)
  8237. (org-mode)
  8238. (setq varlist (buffer-local-variables)))
  8239. (kill-buffer "*Org tmp*")
  8240. (delq nil
  8241. (mapcar
  8242. (lambda (x)
  8243. (setq x
  8244. (if (symbolp x)
  8245. (list x)
  8246. (list (car x) (cdr x))))
  8247. (if (and (not (get (car x) 'org-state))
  8248. (string-match
  8249. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8250. (symbol-name (car x))))
  8251. x nil))
  8252. varlist))))
  8253. (defun org-clone-local-variables (from-buffer &optional regexp)
  8254. "Clone local variables from FROM-BUFFER.
  8255. Optional argument REGEXP selects variables to clone."
  8256. (mapc
  8257. (lambda (pair)
  8258. (and (symbolp (car pair))
  8259. (or (null regexp)
  8260. (string-match regexp (symbol-name (car pair))))
  8261. (set (make-local-variable (car pair))
  8262. (cdr pair))))
  8263. (buffer-local-variables from-buffer)))
  8264. ;;;###autoload
  8265. (defun org-run-like-in-org-mode (cmd)
  8266. "Run a command, pretending that the current buffer is in Org-mode.
  8267. This will temporarily bind local variables that are typically bound in
  8268. Org-mode to the values they have in Org-mode, and then interactively
  8269. call CMD."
  8270. (org-load-modules-maybe)
  8271. (unless org-local-vars
  8272. (setq org-local-vars (org-get-local-variables)))
  8273. (let (binds)
  8274. (dolist (var org-local-vars)
  8275. (when (or (not (boundp (car var)))
  8276. (eq (symbol-value (car var))
  8277. (default-value (car var))))
  8278. (push (list (car var) `(quote ,(cadr var))) binds)))
  8279. (eval `(let ,binds
  8280. (call-interactively (quote ,cmd))))))
  8281. ;;;; Archiving
  8282. (defun org-get-category (&optional pos force-refresh)
  8283. "Get the category applying to position POS."
  8284. (save-match-data
  8285. (if force-refresh (org-refresh-category-properties))
  8286. (let ((pos (or pos (point))))
  8287. (or (get-text-property pos 'org-category)
  8288. (progn (org-refresh-category-properties)
  8289. (get-text-property pos 'org-category))))))
  8290. (defun org-refresh-category-properties ()
  8291. "Refresh category text properties in the buffer."
  8292. (let ((case-fold-search t)
  8293. (inhibit-read-only t)
  8294. (def-cat (cond
  8295. ((null org-category)
  8296. (if buffer-file-name
  8297. (file-name-sans-extension
  8298. (file-name-nondirectory buffer-file-name))
  8299. "???"))
  8300. ((symbolp org-category) (symbol-name org-category))
  8301. (t org-category)))
  8302. beg end cat pos optionp)
  8303. (org-with-silent-modifications
  8304. (save-excursion
  8305. (save-restriction
  8306. (widen)
  8307. (goto-char (point-min))
  8308. (put-text-property (point) (point-max) 'org-category def-cat)
  8309. (while (re-search-forward
  8310. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8311. (setq pos (match-end 0)
  8312. optionp (equal (char-after (match-beginning 0)) ?#)
  8313. cat (org-trim (match-string 2)))
  8314. (if optionp
  8315. (setq beg (point-at-bol) end (point-max))
  8316. (org-back-to-heading t)
  8317. (setq beg (point) end (org-end-of-subtree t t)))
  8318. (put-text-property beg end 'org-category cat)
  8319. (put-text-property beg end 'org-category-position beg)
  8320. (goto-char pos)))))))
  8321. (defun org-refresh-properties (dprop tprop)
  8322. "Refresh buffer text properties.
  8323. DPROP is the drawer property and TPROP is the corresponding text
  8324. property to set."
  8325. (let ((case-fold-search t)
  8326. (inhibit-read-only t) p)
  8327. (org-with-silent-modifications
  8328. (save-excursion
  8329. (save-restriction
  8330. (widen)
  8331. (goto-char (point-min))
  8332. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8333. (setq p (org-match-string-no-properties 1))
  8334. (save-excursion
  8335. (org-back-to-heading t)
  8336. (put-text-property
  8337. (point-at-bol) (point-at-eol) tprop p))))))))
  8338. ;;;; Link Stuff
  8339. ;;; Link abbreviations
  8340. (defun org-link-expand-abbrev (link)
  8341. "Apply replacements as defined in `org-link-abbrev-alist'."
  8342. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8343. (let* ((key (match-string 1 link))
  8344. (as (or (assoc key org-link-abbrev-alist-local)
  8345. (assoc key org-link-abbrev-alist)))
  8346. (tag (and (match-end 2) (match-string 3 link)))
  8347. rpl)
  8348. (if (not as)
  8349. link
  8350. (setq rpl (cdr as))
  8351. (cond
  8352. ((symbolp rpl) (funcall rpl tag))
  8353. ((string-match "%(\\([^)]+\\))" rpl)
  8354. (replace-match
  8355. (save-match-data
  8356. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8357. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8358. ((string-match "%h" rpl)
  8359. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8360. (t (concat rpl tag)))))
  8361. link))
  8362. ;;; Storing and inserting links
  8363. (defvar org-insert-link-history nil
  8364. "Minibuffer history for links inserted with `org-insert-link'.")
  8365. (defvar org-stored-links nil
  8366. "Contains the links stored with `org-store-link'.")
  8367. (defvar org-store-link-plist nil
  8368. "Plist with info about the most recently link created with `org-store-link'.")
  8369. (defvar org-link-protocols nil
  8370. "Link protocols added to Org-mode using `org-add-link-type'.")
  8371. (defvar org-store-link-functions nil
  8372. "List of functions that are called to create and store a link.
  8373. Each function will be called in turn until one returns a non-nil
  8374. value. Each function should check if it is responsible for creating
  8375. this link (for example by looking at the major mode).
  8376. If not, it must exit and return nil.
  8377. If yes, it should return a non-nil value after a calling
  8378. `org-store-link-props' with a list of properties and values.
  8379. Special properties are:
  8380. :type The link prefix, like \"http\". This must be given.
  8381. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8382. This is obligatory as well.
  8383. :description Optional default description for the second pair
  8384. of brackets in an Org-mode link. The user can still change
  8385. this when inserting this link into an Org-mode buffer.
  8386. In addition to these, any additional properties can be specified
  8387. and then used in capture templates.")
  8388. (defun org-add-link-type (type &optional follow export)
  8389. "Add TYPE to the list of `org-link-types'.
  8390. Re-compute all regular expressions depending on `org-link-types'
  8391. FOLLOW and EXPORT are two functions.
  8392. FOLLOW should take the link path as the single argument and do whatever
  8393. is necessary to follow the link, for example find a file or display
  8394. a mail message.
  8395. EXPORT should format the link path for export to one of the export formats.
  8396. It should be a function accepting three arguments:
  8397. path the path of the link, the text after the prefix (like \"http:\")
  8398. desc the description of the link, if any, or a description added by
  8399. org-export-normalize-links if there is none
  8400. format the export format, a symbol like `html' or `latex' or `ascii'..
  8401. The function may use the FORMAT information to return different values
  8402. depending on the format. The return value will be put literally into
  8403. the exported file. If the return value is nil, this means Org should
  8404. do what it normally does with links which do not have EXPORT defined.
  8405. Org-mode has a built-in default for exporting links. If you are happy with
  8406. this default, there is no need to define an export function for the link
  8407. type. For a simple example of an export function, see `org-bbdb.el'."
  8408. (add-to-list 'org-link-types type t)
  8409. (org-make-link-regexps)
  8410. (if (assoc type org-link-protocols)
  8411. (setcdr (assoc type org-link-protocols) (list follow export))
  8412. (push (list type follow export) org-link-protocols)))
  8413. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8414. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8415. ;;;###autoload
  8416. (defun org-store-link (arg)
  8417. "\\<org-mode-map>Store an org-link to the current location.
  8418. This link is added to `org-stored-links' and can later be inserted
  8419. into an org-buffer with \\[org-insert-link].
  8420. For some link types, a prefix arg is interpreted.
  8421. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8422. For file links, arg negates `org-context-in-file-links'.
  8423. A double prefix arg force skipping storing functions that are not
  8424. part of Org's core.
  8425. A triple prefix arg force storing a link for each line in the
  8426. active region."
  8427. (interactive "P")
  8428. (org-load-modules-maybe)
  8429. (if (and (equal arg '(64)) (org-region-active-p))
  8430. (save-excursion
  8431. (let ((end (region-end)))
  8432. (goto-char (region-beginning))
  8433. (set-mark (point))
  8434. (while (< (point-at-eol) end)
  8435. (move-end-of-line 1) (activate-mark)
  8436. (let (current-prefix-arg)
  8437. (call-interactively 'org-store-link))
  8438. (move-beginning-of-line 2)
  8439. (set-mark (point)))))
  8440. (org-with-limited-levels
  8441. (setq org-store-link-plist nil)
  8442. (let (link cpltxt desc description search
  8443. txt custom-id agenda-link sfuns sfunsn)
  8444. (cond
  8445. ;; Store a link using an external link type
  8446. ((and (not (equal arg '(16)))
  8447. (setq sfuns
  8448. (delq
  8449. nil (mapcar (lambda (f)
  8450. (let (fs) (if (funcall f) (push f fs))))
  8451. org-store-link-functions))
  8452. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8453. (or (and (cdr sfuns)
  8454. (funcall (intern
  8455. (completing-read
  8456. "Which function for creating the link? "
  8457. sfunsn t (car sfunsn)))))
  8458. (funcall (caar sfuns)))
  8459. (setq link (plist-get org-store-link-plist :link)
  8460. desc (or (plist-get org-store-link-plist
  8461. :description) link))))
  8462. ;; Store a link from a source code buffer
  8463. ((org-src-edit-buffer-p)
  8464. (let (label gc)
  8465. (while (or (not label)
  8466. (save-excursion
  8467. (save-restriction
  8468. (widen)
  8469. (goto-char (point-min))
  8470. (re-search-forward
  8471. (regexp-quote (format org-coderef-label-format label))
  8472. nil t))))
  8473. (when label (message "Label exists already") (sit-for 2))
  8474. (setq label (read-string "Code line label: " label)))
  8475. (end-of-line 1)
  8476. (setq link (format org-coderef-label-format label))
  8477. (setq gc (- 79 (length link)))
  8478. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8479. (insert link)
  8480. (setq link (concat "(" label ")") desc nil)))
  8481. ;; We are in the agenda, link to referenced location
  8482. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8483. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8484. (get-text-property (point) 'org-marker))))
  8485. (when m
  8486. (org-with-point-at m
  8487. (setq agenda-link
  8488. (if (org-called-interactively-p 'any)
  8489. (call-interactively 'org-store-link)
  8490. (org-store-link nil)))))))
  8491. ((eq major-mode 'calendar-mode)
  8492. (let ((cd (calendar-cursor-to-date)))
  8493. (setq link
  8494. (format-time-string
  8495. (car org-time-stamp-formats)
  8496. (apply 'encode-time
  8497. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8498. nil nil nil))))
  8499. (org-store-link-props :type "calendar" :date cd)))
  8500. ((eq major-mode 'help-mode)
  8501. (setq link (concat "help:" (save-excursion
  8502. (goto-char (point-min))
  8503. (looking-at "^[^ ]+")
  8504. (match-string 0))))
  8505. (org-store-link-props :type "help"))
  8506. ((eq major-mode 'w3-mode)
  8507. (setq cpltxt (if (and (buffer-name)
  8508. (not (string-match "Untitled" (buffer-name))))
  8509. (buffer-name)
  8510. (url-view-url t))
  8511. link (url-view-url t))
  8512. (org-store-link-props :type "w3" :url (url-view-url t)))
  8513. ((eq major-mode 'image-mode)
  8514. (setq cpltxt (concat "file:"
  8515. (abbreviate-file-name buffer-file-name))
  8516. link cpltxt)
  8517. (org-store-link-props :type "image" :file buffer-file-name))
  8518. ;; In dired, store a link to the file of the current line
  8519. ((eq major-mode 'dired-mode)
  8520. (let ((file (dired-get-filename nil t)))
  8521. (setq file (if file
  8522. (abbreviate-file-name
  8523. (expand-file-name (dired-get-filename nil t)))
  8524. ;; otherwise, no file so use current directory.
  8525. default-directory))
  8526. (setq cpltxt (concat "file:" file)
  8527. link cpltxt)))
  8528. ((setq search (run-hook-with-args-until-success
  8529. 'org-create-file-search-functions))
  8530. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8531. "::" search))
  8532. (setq cpltxt (or description link)))
  8533. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8534. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8535. (cond
  8536. ;; Store a link using the radio target at point
  8537. ((org-in-regexp "<<\\(.*?\\)>>")
  8538. (setq cpltxt
  8539. (concat "file:"
  8540. (abbreviate-file-name
  8541. (buffer-file-name (buffer-base-buffer)))
  8542. "::" (match-string 1))
  8543. link cpltxt))
  8544. ((and (featurep 'org-id)
  8545. (or (eq org-id-link-to-org-use-id t)
  8546. (and (org-called-interactively-p 'any)
  8547. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8548. (and (eq org-id-link-to-org-use-id
  8549. 'create-if-interactive-and-no-custom-id)
  8550. (not custom-id))))
  8551. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8552. ;; Store a link using the ID at point
  8553. (setq link (condition-case nil
  8554. (prog1 (org-id-store-link)
  8555. (setq desc (plist-get org-store-link-plist
  8556. :description)))
  8557. (error
  8558. ;; Probably before first headline, link only to file
  8559. (concat "file:"
  8560. (abbreviate-file-name
  8561. (buffer-file-name (buffer-base-buffer))))))))
  8562. (t
  8563. ;; Just link to current headline
  8564. (setq cpltxt (concat "file:"
  8565. (abbreviate-file-name
  8566. (buffer-file-name (buffer-base-buffer)))))
  8567. ;; Add a context search string
  8568. (when (org-xor org-context-in-file-links arg)
  8569. (let* ((ee (org-element-at-point))
  8570. (et (org-element-type ee))
  8571. (ev (plist-get (cadr ee) :value))
  8572. (ek (plist-get (cadr ee) :key))
  8573. (eok (and (stringp ek) (string-match "name" ek))))
  8574. (setq txt (cond
  8575. ((org-at-heading-p) nil)
  8576. ((and (eq et 'keyword) eok) ev)
  8577. ((org-region-active-p)
  8578. (buffer-substring (region-beginning) (region-end)))))
  8579. (when (or (null txt) (string-match "\\S-" txt))
  8580. (setq cpltxt
  8581. (concat cpltxt "::"
  8582. (condition-case nil
  8583. (org-make-org-heading-search-string txt)
  8584. (error "")))
  8585. desc (or (and (eq et 'keyword) eok ev)
  8586. (nth 4 (ignore-errors (org-heading-components)))
  8587. "NONE")))))
  8588. (if (string-match "::\\'" cpltxt)
  8589. (setq cpltxt (substring cpltxt 0 -2)))
  8590. (setq link cpltxt))))
  8591. ((buffer-file-name (buffer-base-buffer))
  8592. ;; Just link to this file here.
  8593. (setq cpltxt (concat "file:"
  8594. (abbreviate-file-name
  8595. (buffer-file-name (buffer-base-buffer)))))
  8596. ;; Add a context string.
  8597. (when (org-xor org-context-in-file-links arg)
  8598. (setq txt (if (org-region-active-p)
  8599. (buffer-substring (region-beginning) (region-end))
  8600. (buffer-substring (point-at-bol) (point-at-eol))))
  8601. ;; Only use search option if there is some text.
  8602. (when (string-match "\\S-" txt)
  8603. (setq cpltxt
  8604. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8605. desc "NONE")))
  8606. (setq link cpltxt))
  8607. ((org-called-interactively-p 'interactive)
  8608. (user-error "No method for storing a link from this buffer"))
  8609. (t (setq link nil)))
  8610. ;; We're done setting link and desc, clean up
  8611. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8612. (setq link (or link cpltxt)
  8613. desc (or desc cpltxt))
  8614. (cond ((equal desc "NONE") (setq desc nil))
  8615. ((string-match org-bracket-link-analytic-regexp desc)
  8616. (let ((d0 (match-string 3 desc))
  8617. (p0 (match-string 5 desc)))
  8618. (setq desc
  8619. (replace-regexp-in-string
  8620. org-bracket-link-regexp
  8621. (concat (or p0 d0)
  8622. (if (equal (length (match-string 0 desc))
  8623. (length desc)) "*" "")) desc)))))
  8624. ;; Return the link
  8625. (if (not (and (or (org-called-interactively-p 'any)
  8626. executing-kbd-macro) link))
  8627. (or agenda-link (and link (org-make-link-string link desc)))
  8628. (push (list link desc) org-stored-links)
  8629. (message "Stored: %s" (or desc link))
  8630. (when custom-id
  8631. (setq link (concat "file:" (abbreviate-file-name
  8632. (buffer-file-name)) "::#" custom-id))
  8633. (push (list link desc) org-stored-links)))))))
  8634. (defun org-store-link-props (&rest plist)
  8635. "Store link properties, extract names and addresses."
  8636. (let (x adr)
  8637. (when (setq x (plist-get plist :from))
  8638. (setq adr (mail-extract-address-components x))
  8639. (setq plist (plist-put plist :fromname (car adr)))
  8640. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8641. (when (setq x (plist-get plist :to))
  8642. (setq adr (mail-extract-address-components x))
  8643. (setq plist (plist-put plist :toname (car adr)))
  8644. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8645. (let ((from (plist-get plist :from))
  8646. (to (plist-get plist :to)))
  8647. (when (and from to org-from-is-user-regexp)
  8648. (setq plist
  8649. (plist-put plist :fromto
  8650. (if (string-match org-from-is-user-regexp from)
  8651. (concat "to %t")
  8652. (concat "from %f"))))))
  8653. (setq org-store-link-plist plist))
  8654. (defun org-add-link-props (&rest plist)
  8655. "Add these properties to the link property list."
  8656. (let (key value)
  8657. (while plist
  8658. (setq key (pop plist) value (pop plist))
  8659. (setq org-store-link-plist
  8660. (plist-put org-store-link-plist key value)))))
  8661. (defun org-email-link-description (&optional fmt)
  8662. "Return the description part of an email link.
  8663. This takes information from `org-store-link-plist' and formats it
  8664. according to FMT (default from `org-email-link-description-format')."
  8665. (setq fmt (or fmt org-email-link-description-format))
  8666. (let* ((p org-store-link-plist)
  8667. (to (plist-get p :toaddress))
  8668. (from (plist-get p :fromaddress))
  8669. (table
  8670. (list
  8671. (cons "%c" (plist-get p :fromto))
  8672. (cons "%F" (plist-get p :from))
  8673. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8674. (cons "%T" (plist-get p :to))
  8675. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8676. (cons "%s" (plist-get p :subject))
  8677. (cons "%d" (plist-get p :date))
  8678. (cons "%m" (plist-get p :message-id)))))
  8679. (when (string-match "%c" fmt)
  8680. ;; Check if the user wrote this message
  8681. (if (and org-from-is-user-regexp from to
  8682. (save-match-data (string-match org-from-is-user-regexp from)))
  8683. (setq fmt (replace-match "to %t" t t fmt))
  8684. (setq fmt (replace-match "from %f" t t fmt))))
  8685. (org-replace-escapes fmt table)))
  8686. (defun org-make-org-heading-search-string (&optional string)
  8687. "Make search string for the current headline or STRING."
  8688. (let ((s (or string
  8689. (and (derived-mode-p 'org-mode)
  8690. (save-excursion
  8691. (org-back-to-heading t)
  8692. (org-element-property :raw-value (org-element-at-point))))))
  8693. (lines org-context-in-file-links))
  8694. (or string (setq s (concat "*" s))) ; Add * for headlines
  8695. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8696. (when (and string (integerp lines) (> lines 0))
  8697. (let ((slines (org-split-string s "\n")))
  8698. (when (< lines (length slines))
  8699. (setq s (mapconcat
  8700. 'identity
  8701. (reverse (nthcdr (- (length slines) lines)
  8702. (reverse slines))) "\n")))))
  8703. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8704. (defun org-make-link-string (link &optional description)
  8705. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8706. (unless (string-match "\\S-" link)
  8707. (error "Empty link"))
  8708. (when (and description
  8709. (stringp description)
  8710. (not (string-match "\\S-" description)))
  8711. (setq description nil))
  8712. (when (stringp description)
  8713. ;; Remove brackets from the description, they are fatal.
  8714. (while (string-match "\\[" description)
  8715. (setq description (replace-match "{" t t description)))
  8716. (while (string-match "\\]" description)
  8717. (setq description (replace-match "}" t t description))))
  8718. (when (equal link description)
  8719. ;; No description needed, it is identical
  8720. (setq description nil))
  8721. (when (and (not description)
  8722. (not (string-match (org-image-file-name-regexp) link))
  8723. (not (equal link (org-link-escape link))))
  8724. (setq description (org-extract-attributes link)))
  8725. (setq link
  8726. (cond ((string-match (org-image-file-name-regexp) link) link)
  8727. ((string-match org-link-types-re link)
  8728. (concat (match-string 1 link)
  8729. (org-link-escape (substring link (match-end 1)))))
  8730. (t (org-link-escape link))))
  8731. (concat "[[" link "]"
  8732. (if description (concat "[" description "]") "")
  8733. "]"))
  8734. (defconst org-link-escape-chars
  8735. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8736. "List of characters that should be escaped in link.
  8737. This is the list that is used for internal purposes.")
  8738. (defconst org-link-escape-chars-browser
  8739. '(?\ )
  8740. "List of escapes for characters that are problematic in links.
  8741. This is the list that is used before handing over to the browser.")
  8742. (defun org-link-escape (text &optional table merge)
  8743. "Return percent escaped representation of TEXT.
  8744. TEXT is a string with the text to escape.
  8745. Optional argument TABLE is a list with characters that should be
  8746. escaped. When nil, `org-link-escape-chars' is used.
  8747. If optional argument MERGE is set, merge TABLE into
  8748. `org-link-escape-chars'."
  8749. (cond
  8750. ((and table merge)
  8751. (mapc (lambda (defchr)
  8752. (unless (member defchr table)
  8753. (setq table (cons defchr table)))) org-link-escape-chars))
  8754. ((null table)
  8755. (setq table org-link-escape-chars)))
  8756. (mapconcat
  8757. (lambda (char)
  8758. (if (or (member char table)
  8759. (and (or (< char 32) (= char 37) (> char 126))
  8760. org-url-hexify-p))
  8761. (mapconcat (lambda (sequence-element)
  8762. (format "%%%.2X" sequence-element))
  8763. (or (encode-coding-char char 'utf-8)
  8764. (error "Unable to percent escape character: %s"
  8765. (char-to-string char))) "")
  8766. (char-to-string char))) text ""))
  8767. (defun org-link-unescape (str)
  8768. "Unhex hexified Unicode strings as returned from the JavaScript function
  8769. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8770. (unless (and (null str) (string= "" str))
  8771. (let ((pos 0) (case-fold-search t) unhexed)
  8772. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8773. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8774. (setq str (replace-match unhexed t t str))
  8775. (setq pos (+ pos (length unhexed))))))
  8776. str)
  8777. (defun org-link-unescape-compound (hex)
  8778. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8779. Note: this function also decodes single byte encodings like
  8780. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8781. (save-match-data
  8782. (let* ((bytes (cdr (split-string hex "%")))
  8783. (ret "")
  8784. (eat 0)
  8785. (sum 0))
  8786. (while bytes
  8787. (let* ((val (string-to-number (pop bytes) 16))
  8788. (shift-xor
  8789. (if (= 0 eat)
  8790. (cond
  8791. ((>= val 252) (cons 6 252))
  8792. ((>= val 248) (cons 5 248))
  8793. ((>= val 240) (cons 4 240))
  8794. ((>= val 224) (cons 3 224))
  8795. ((>= val 192) (cons 2 192))
  8796. (t (cons 0 0)))
  8797. (cons 6 128))))
  8798. (if (>= val 192) (setq eat (car shift-xor)))
  8799. (setq val (logxor val (cdr shift-xor)))
  8800. (setq sum (+ (lsh sum (car shift-xor)) val))
  8801. (if (> eat 0) (setq eat (- eat 1)))
  8802. (cond
  8803. ((= 0 eat) ;multi byte
  8804. (setq ret (concat ret (org-char-to-string sum)))
  8805. (setq sum 0))
  8806. ((not bytes) ; single byte(s)
  8807. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8808. )) ;; end (while bytes
  8809. ret )))
  8810. (defun org-link-unescape-single-byte-sequence (hex)
  8811. "Unhexify hex-encoded single byte character sequences."
  8812. (mapconcat (lambda (byte)
  8813. (char-to-string (string-to-number byte 16)))
  8814. (cdr (split-string hex "%")) ""))
  8815. (defun org-xor (a b)
  8816. "Exclusive or."
  8817. (if a (not b) b))
  8818. (defun org-fixup-message-id-for-http (s)
  8819. "Replace special characters in a message id, so it can be used in an http query."
  8820. (when (string-match "%" s)
  8821. (setq s (mapconcat (lambda (c)
  8822. (if (eq c ?%)
  8823. "%25"
  8824. (char-to-string c)))
  8825. s "")))
  8826. (while (string-match "<" s)
  8827. (setq s (replace-match "%3C" t t s)))
  8828. (while (string-match ">" s)
  8829. (setq s (replace-match "%3E" t t s)))
  8830. (while (string-match "@" s)
  8831. (setq s (replace-match "%40" t t s)))
  8832. s)
  8833. (defun org-link-prettify (link)
  8834. "Return a human-readable representation of LINK.
  8835. The car of LINK must be a raw link the cdr of LINK must be either
  8836. a link description or nil."
  8837. (let ((desc (or (cadr link) "<no description>")))
  8838. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8839. "<" (car link) ">")))
  8840. ;;;###autoload
  8841. (defun org-insert-link-global ()
  8842. "Insert a link like Org-mode does.
  8843. This command can be called in any mode to insert a link in Org-mode syntax."
  8844. (interactive)
  8845. (org-load-modules-maybe)
  8846. (org-run-like-in-org-mode 'org-insert-link))
  8847. (defun org-insert-all-links (&optional keep)
  8848. "Insert all links in `org-stored-links'."
  8849. (interactive "P")
  8850. (let ((links (copy-sequence org-stored-links)) l)
  8851. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8852. (insert "- ")
  8853. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8854. (insert "\n"))))
  8855. (defun org-link-fontify-links-to-this-file ()
  8856. "Fontify links to the current file in `org-stored-links'."
  8857. (let ((f (buffer-file-name)) a b)
  8858. (setq a (mapcar (lambda(l)
  8859. (let ((ll (car l)))
  8860. (when (and (string-match "^file:\\(.+\\)::" ll)
  8861. (equal f (expand-file-name (match-string 1 ll))))
  8862. ll)))
  8863. org-stored-links))
  8864. (when (featurep 'org-id)
  8865. (setq b (mapcar (lambda(l)
  8866. (let ((ll (car l)))
  8867. (when (and (string-match "^id:\\(.+\\)$" ll)
  8868. (equal f (expand-file-name
  8869. (or (org-id-find-id-file
  8870. (match-string 1 ll)) ""))))
  8871. ll)))
  8872. org-stored-links)))
  8873. (mapcar (lambda(l)
  8874. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8875. (delq nil (append a b)))))
  8876. (defvar org-link-links-in-this-file nil)
  8877. (defun org-insert-link (&optional complete-file link-location default-description)
  8878. "Insert a link. At the prompt, enter the link.
  8879. Completion can be used to insert any of the link protocol prefixes like
  8880. http or ftp in use.
  8881. The history can be used to select a link previously stored with
  8882. `org-store-link'. When the empty string is entered (i.e. if you just
  8883. press RET at the prompt), the link defaults to the most recently
  8884. stored link. As SPC triggers completion in the minibuffer, you need to
  8885. use M-SPC or C-q SPC to force the insertion of a space character.
  8886. You will also be prompted for a description, and if one is given, it will
  8887. be displayed in the buffer instead of the link.
  8888. If there is already a link at point, this command will allow you to edit link
  8889. and description parts.
  8890. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8891. be selected using completion. The path to the file will be relative to the
  8892. current directory if the file is in the current directory or a subdirectory.
  8893. Otherwise, the link will be the absolute path as completed in the minibuffer
  8894. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8895. option `org-link-file-path-type'.
  8896. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8897. the current directory or below.
  8898. With three \\[universal-argument] prefixes, negate the meaning of
  8899. `org-keep-stored-link-after-insertion'.
  8900. If `org-make-link-description-function' is non-nil, this function will be
  8901. called with the link target, and the result will be the default
  8902. link description.
  8903. If the LINK-LOCATION parameter is non-nil, this value will be
  8904. used as the link location instead of reading one interactively.
  8905. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8906. be used as the default description."
  8907. (interactive "P")
  8908. (let* ((wcf (current-window-configuration))
  8909. (origbuf (current-buffer))
  8910. (region (if (org-region-active-p)
  8911. (buffer-substring (region-beginning) (region-end))))
  8912. (remove (and region (list (region-beginning) (region-end))))
  8913. (desc region)
  8914. tmphist ; byte-compile incorrectly complains about this
  8915. (link link-location)
  8916. (abbrevs org-link-abbrev-alist-local)
  8917. entry file all-prefixes auto-desc)
  8918. (cond
  8919. (link-location) ; specified by arg, just use it.
  8920. ((org-in-regexp org-bracket-link-regexp 1)
  8921. ;; We do have a link at point, and we are going to edit it.
  8922. (setq remove (list (match-beginning 0) (match-end 0)))
  8923. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8924. (setq link (read-string "Link: "
  8925. (org-link-unescape
  8926. (org-match-string-no-properties 1)))))
  8927. ((or (org-in-regexp org-angle-link-re)
  8928. (org-in-regexp org-plain-link-re))
  8929. ;; Convert to bracket link
  8930. (setq remove (list (match-beginning 0) (match-end 0))
  8931. link (read-string "Link: "
  8932. (org-remove-angle-brackets (match-string 0)))))
  8933. ((member complete-file '((4) (16)))
  8934. ;; Completing read for file names.
  8935. (setq link (org-file-complete-link complete-file)))
  8936. (t
  8937. ;; Read link, with completion for stored links.
  8938. (org-link-fontify-links-to-this-file)
  8939. (org-switch-to-buffer-other-window "*Org Links*")
  8940. (with-current-buffer "*Org Links*"
  8941. (erase-buffer)
  8942. (insert "Insert a link.
  8943. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8944. (when org-stored-links
  8945. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8946. (insert (mapconcat 'org-link-prettify
  8947. (reverse org-stored-links) "\n")))
  8948. (goto-char (point-min)))
  8949. (let ((cw (selected-window)))
  8950. (select-window (get-buffer-window "*Org Links*" 'visible))
  8951. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8952. (unless (pos-visible-in-window-p (point-max))
  8953. (org-fit-window-to-buffer))
  8954. (and (window-live-p cw) (select-window cw)))
  8955. ;; Fake a link history, containing the stored links.
  8956. (setq tmphist (append (mapcar 'car org-stored-links)
  8957. org-insert-link-history))
  8958. (setq all-prefixes (append (mapcar 'car abbrevs)
  8959. (mapcar 'car org-link-abbrev-alist)
  8960. org-link-types))
  8961. (unwind-protect
  8962. (progn
  8963. (setq link
  8964. (org-completing-read
  8965. "Link: "
  8966. (append
  8967. (mapcar (lambda (x) (concat x ":"))
  8968. all-prefixes)
  8969. (mapcar 'car org-stored-links))
  8970. nil nil nil
  8971. 'tmphist
  8972. (caar org-stored-links)))
  8973. (if (not (string-match "\\S-" link))
  8974. (user-error "No link selected"))
  8975. (mapc (lambda(l)
  8976. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8977. org-stored-links)
  8978. (if (or (member link all-prefixes)
  8979. (and (equal ":" (substring link -1))
  8980. (member (substring link 0 -1) all-prefixes)
  8981. (setq link (substring link 0 -1))))
  8982. (setq link (with-current-buffer origbuf
  8983. (org-link-try-special-completion link)))))
  8984. (set-window-configuration wcf)
  8985. (kill-buffer "*Org Links*"))
  8986. (setq entry (assoc link org-stored-links))
  8987. (or entry (push link org-insert-link-history))
  8988. (setq desc (or desc (nth 1 entry)))))
  8989. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8990. (not org-keep-stored-link-after-insertion))
  8991. (setq org-stored-links (delq (assoc link org-stored-links)
  8992. org-stored-links)))
  8993. (if (string-match org-plain-link-re link)
  8994. ;; URL-like link, normalize the use of angular brackets.
  8995. (setq link (org-remove-angle-brackets link)))
  8996. ;; Check if we are linking to the current file with a search
  8997. ;; option If yes, simplify the link by using only the search
  8998. ;; option.
  8999. (when (and buffer-file-name
  9000. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9001. (let* ((path (match-string 1 link))
  9002. (case-fold-search nil)
  9003. (search (match-string 2 link)))
  9004. (save-match-data
  9005. (if (equal (file-truename buffer-file-name) (file-truename path))
  9006. ;; We are linking to this same file, with a search option
  9007. (setq link search)))))
  9008. ;; Check if we can/should use a relative path. If yes, simplify the link
  9009. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9010. (let* ((type (match-string 1 link))
  9011. (path (match-string 2 link))
  9012. (origpath path)
  9013. (case-fold-search nil))
  9014. (cond
  9015. ((or (eq org-link-file-path-type 'absolute)
  9016. (equal complete-file '(16)))
  9017. (setq path (abbreviate-file-name (expand-file-name path))))
  9018. ((eq org-link-file-path-type 'noabbrev)
  9019. (setq path (expand-file-name path)))
  9020. ((eq org-link-file-path-type 'relative)
  9021. (setq path (file-relative-name path)))
  9022. (t
  9023. (save-match-data
  9024. (if (string-match (concat "^" (regexp-quote
  9025. (expand-file-name
  9026. (file-name-as-directory
  9027. default-directory))))
  9028. (expand-file-name path))
  9029. ;; We are linking a file with relative path name.
  9030. (setq path (substring (expand-file-name path)
  9031. (match-end 0)))
  9032. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9033. (setq link (concat type path))
  9034. (if (equal desc origpath)
  9035. (setq desc path))))
  9036. (if org-make-link-description-function
  9037. (setq desc
  9038. (or (condition-case nil
  9039. (funcall org-make-link-description-function link desc)
  9040. (error (progn (message "Can't get link description from `%s'"
  9041. (symbol-name org-make-link-description-function))
  9042. (sit-for 2) nil)))
  9043. (read-string "Description: " default-description)))
  9044. (if default-description (setq desc default-description)
  9045. (setq desc (or (and auto-desc desc)
  9046. (read-string "Description: " desc)))))
  9047. (unless (string-match "\\S-" desc) (setq desc nil))
  9048. (if remove (apply 'delete-region remove))
  9049. (insert (org-make-link-string link desc))))
  9050. (defun org-link-try-special-completion (type)
  9051. "If there is completion support for link type TYPE, offer it."
  9052. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9053. (if (functionp fun)
  9054. (funcall fun)
  9055. (read-string "Link (no completion support): " (concat type ":")))))
  9056. (defun org-file-complete-link (&optional arg)
  9057. "Create a file link using completion."
  9058. (let (file link)
  9059. (setq file (org-iread-file-name "File: "))
  9060. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9061. (pwd1 (file-name-as-directory (abbreviate-file-name
  9062. (expand-file-name ".")))))
  9063. (cond
  9064. ((equal arg '(16))
  9065. (setq link (concat
  9066. "file:"
  9067. (abbreviate-file-name (expand-file-name file)))))
  9068. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9069. (setq link (concat "file:" (match-string 1 file))))
  9070. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9071. (expand-file-name file))
  9072. (setq link (concat
  9073. "file:" (match-string 1 (expand-file-name file)))))
  9074. (t (setq link (concat "file:" file)))))
  9075. link))
  9076. (defun org-iread-file-name (&rest args)
  9077. "Read-file-name using `ido-mode' speedup if available.
  9078. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9079. See `read-file-name' for a description of parameters."
  9080. (org-without-partial-completion
  9081. (if (and org-completion-use-ido
  9082. (fboundp 'ido-read-file-name)
  9083. (boundp 'ido-mode) ido-mode
  9084. (listp (second args)))
  9085. (let ((ido-enter-matching-directory nil))
  9086. (apply 'ido-read-file-name args))
  9087. (apply 'read-file-name args))))
  9088. (defun org-completing-read (&rest args)
  9089. "Completing-read with SPACE being a normal character."
  9090. (let ((enable-recursive-minibuffers t)
  9091. (minibuffer-local-completion-map
  9092. (copy-keymap minibuffer-local-completion-map)))
  9093. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9094. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9095. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9096. (apply 'org-icompleting-read args)))
  9097. (defun org-completing-read-no-i (&rest args)
  9098. (let (org-completion-use-ido org-completion-use-iswitchb)
  9099. (apply 'org-completing-read args)))
  9100. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9101. "Use iswitch as a completing-read replacement to choose from choices.
  9102. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9103. from."
  9104. (let* ((iswitchb-use-virtual-buffers nil)
  9105. (iswitchb-make-buflist-hook
  9106. (lambda ()
  9107. (setq iswitchb-temp-buflist choices))))
  9108. (iswitchb-read-buffer prompt)))
  9109. (defun org-icompleting-read (&rest args)
  9110. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9111. (org-without-partial-completion
  9112. (if (and org-completion-use-ido
  9113. (fboundp 'ido-completing-read)
  9114. (boundp 'ido-mode) ido-mode
  9115. (listp (second args)))
  9116. (let ((ido-enter-matching-directory nil))
  9117. (apply 'ido-completing-read (concat (car args))
  9118. (if (consp (car (nth 1 args)))
  9119. (mapcar 'car (nth 1 args))
  9120. (nth 1 args))
  9121. (cddr args)))
  9122. (if (and org-completion-use-iswitchb
  9123. (boundp 'iswitchb-mode) iswitchb-mode
  9124. (listp (second args)))
  9125. (apply 'org-iswitchb-completing-read (concat (car args))
  9126. (if (consp (car (nth 1 args)))
  9127. (mapcar 'car (nth 1 args))
  9128. (nth 1 args))
  9129. (cddr args))
  9130. (apply 'completing-read args)))))
  9131. (defun org-extract-attributes (s)
  9132. "Extract the attributes cookie from a string and set as text property."
  9133. (let (a attr (start 0) key value)
  9134. (save-match-data
  9135. (when (string-match "{{\\([^}]+\\)}}$" s)
  9136. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9137. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9138. (setq key (match-string 1 a) value (match-string 2 a)
  9139. start (match-end 0)
  9140. attr (plist-put attr (intern key) value))))
  9141. (org-add-props s nil 'org-attr attr))
  9142. s))
  9143. ;;; Opening/following a link
  9144. (defvar org-link-search-failed nil)
  9145. (defvar org-open-link-functions nil
  9146. "Hook for functions finding a plain text link.
  9147. These functions must take a single argument, the link content.
  9148. They will be called for links that look like [[link text][description]]
  9149. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9150. like a filename (e.g. \"./blue.png\").
  9151. These functions will be called *before* Org attempts to resolve the
  9152. link by doing text searches in the current buffer - so if you want a
  9153. link \"[[target]]\" to still find \"<<target>>\", your function should
  9154. handle this as a special case.
  9155. When the function does handle the link, it must return a non-nil value.
  9156. If it decides that it is not responsible for this link, it must return
  9157. nil to indicate that that Org-mode can continue with other options
  9158. like exact and fuzzy text search.")
  9159. (defun org-next-link (&optional search-backward)
  9160. "Move forward to the next link.
  9161. If the link is in hidden text, expose it."
  9162. (interactive "P")
  9163. (when (and org-link-search-failed (eq this-command last-command))
  9164. (goto-char (point-min))
  9165. (message "Link search wrapped back to beginning of buffer"))
  9166. (setq org-link-search-failed nil)
  9167. (let* ((pos (point))
  9168. (ct (org-context))
  9169. (a (assoc :link ct))
  9170. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9171. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9172. ((looking-at org-any-link-re)
  9173. ;; Don't stay stuck at link without an org-link face
  9174. (forward-char (if search-backward -1 1))))
  9175. (if (funcall srch-fun org-any-link-re nil t)
  9176. (progn
  9177. (goto-char (match-beginning 0))
  9178. (if (outline-invisible-p) (org-show-context)))
  9179. (goto-char pos)
  9180. (setq org-link-search-failed t)
  9181. (message "No further link found"))))
  9182. (defun org-previous-link ()
  9183. "Move backward to the previous link.
  9184. If the link is in hidden text, expose it."
  9185. (interactive)
  9186. (funcall 'org-next-link t))
  9187. (defun org-translate-link (s)
  9188. "Translate a link string if a translation function has been defined."
  9189. (if (and org-link-translation-function
  9190. (fboundp org-link-translation-function)
  9191. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9192. (progn
  9193. (setq s (funcall org-link-translation-function
  9194. (match-string 1 s) (match-string 2 s)))
  9195. (concat (car s) ":" (cdr s)))
  9196. s))
  9197. (defun org-translate-link-from-planner (type path)
  9198. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9199. This is still an experimental function, your mileage may vary."
  9200. (cond
  9201. ((member type '("http" "https" "news" "ftp"))
  9202. ;; standard Internet links are the same.
  9203. nil)
  9204. ((and (equal type "irc") (string-match "^//" path))
  9205. ;; Planner has two / at the beginning of an irc link, we have 1.
  9206. ;; We should have zero, actually....
  9207. (setq path (substring path 1)))
  9208. ((and (equal type "lisp") (string-match "^/" path))
  9209. ;; Planner has a slash, we do not.
  9210. (setq type "elisp" path (substring path 1)))
  9211. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9212. ;; A typical message link. Planner has the id after the final slash,
  9213. ;; we separate it with a hash mark
  9214. (setq path (concat (match-string 1 path) "#"
  9215. (org-remove-angle-brackets (match-string 2 path))))))
  9216. (cons type path))
  9217. (defun org-find-file-at-mouse (ev)
  9218. "Open file link or URL at mouse."
  9219. (interactive "e")
  9220. (mouse-set-point ev)
  9221. (org-open-at-point 'in-emacs))
  9222. (defun org-open-at-mouse (ev)
  9223. "Open file link or URL at mouse.
  9224. See the docstring of `org-open-file' for details."
  9225. (interactive "e")
  9226. (mouse-set-point ev)
  9227. (if (eq major-mode 'org-agenda-mode)
  9228. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9229. (org-open-at-point))
  9230. (defvar org-window-config-before-follow-link nil
  9231. "The window configuration before following a link.
  9232. This is saved in case the need arises to restore it.")
  9233. (defvar org-open-link-marker (make-marker)
  9234. "Marker pointing to the location where `org-open-at-point; was called.")
  9235. ;;;###autoload
  9236. (defun org-open-at-point-global ()
  9237. "Follow a link like Org-mode does.
  9238. This command can be called in any mode to follow a link that has
  9239. Org-mode syntax."
  9240. (interactive)
  9241. (org-run-like-in-org-mode 'org-open-at-point))
  9242. ;;;###autoload
  9243. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9244. "Open a link in the string S, as if it was in Org-mode."
  9245. (interactive "sLink: \nP")
  9246. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9247. (with-temp-buffer
  9248. (let ((org-inhibit-startup (not reference-buffer)))
  9249. (org-mode)
  9250. (insert s)
  9251. (goto-char (point-min))
  9252. (when reference-buffer
  9253. (setq org-link-abbrev-alist-local
  9254. (with-current-buffer reference-buffer
  9255. org-link-abbrev-alist-local)))
  9256. (org-open-at-point arg reference-buffer)))))
  9257. (defvar org-open-at-point-functions nil
  9258. "Hook that is run when following a link at point.
  9259. Functions in this hook must return t if they identify and follow
  9260. a link at point. If they don't find anything interesting at point,
  9261. they must return nil.")
  9262. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9263. (defun org-open-at-point (&optional arg reference-buffer)
  9264. "Open link at or after point.
  9265. If there is no link at point, this function will search forward up to
  9266. the end of the current line.
  9267. Normally, files will be opened by an appropriate application. If the
  9268. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9269. With a double prefix argument, try to open outside of Emacs, in the
  9270. application the system uses for this file type."
  9271. (interactive "P")
  9272. ;; if in a code block, then open the block's results
  9273. (unless (call-interactively #'org-babel-open-src-block-result)
  9274. (org-load-modules-maybe)
  9275. (move-marker org-open-link-marker (point))
  9276. (setq org-window-config-before-follow-link (current-window-configuration))
  9277. (org-remove-occur-highlights nil nil t)
  9278. (cond
  9279. ((and (org-at-heading-p)
  9280. (not (org-at-timestamp-p t))
  9281. (not (org-in-regexp
  9282. (concat org-plain-link-re "\\|"
  9283. org-bracket-link-regexp "\\|"
  9284. org-angle-link-re "\\|"
  9285. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9286. (not (get-text-property (point) 'org-linked-text)))
  9287. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9288. (lk0 (car lkall))
  9289. (lk (if (stringp lk0) (list lk0) lk0))
  9290. (lkend (cdr lkall)))
  9291. (mapcar (lambda(l)
  9292. (search-forward l nil lkend)
  9293. (goto-char (match-beginning 0))
  9294. (org-open-at-point))
  9295. lk))
  9296. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9297. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9298. ((and (org-at-timestamp-p t)
  9299. (not (org-in-regexp org-bracket-link-regexp)))
  9300. (org-follow-timestamp-link))
  9301. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9302. (not (org-in-regexp org-any-link-re)))
  9303. (org-footnote-action))
  9304. (t
  9305. (let (type path link line search (pos (point)))
  9306. (catch 'match
  9307. (save-excursion
  9308. (skip-chars-forward "^]\n\r")
  9309. (when (org-in-regexp org-bracket-link-regexp 1)
  9310. (setq link (org-extract-attributes
  9311. (org-link-unescape (org-match-string-no-properties 1))))
  9312. (while (string-match " *\n *" link)
  9313. (setq link (replace-match " " t t link)))
  9314. (setq link (org-link-expand-abbrev link))
  9315. (cond
  9316. ((or (file-name-absolute-p link)
  9317. (string-match "^\\.\\.?/" link))
  9318. (setq type "file" path link))
  9319. ((string-match org-link-re-with-space3 link)
  9320. (setq type (match-string 1 link) path (match-string 2 link)))
  9321. ((string-match "^help:+\\(.+\\)" link)
  9322. (setq type "help" path (match-string 1 link)))
  9323. (t (setq type "thisfile" path link)))
  9324. (throw 'match t)))
  9325. (when (get-text-property (point) 'org-linked-text)
  9326. (setq type "thisfile"
  9327. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9328. (1+ (point)) (point))
  9329. path (buffer-substring
  9330. (or (previous-single-property-change pos 'org-linked-text)
  9331. (point-min))
  9332. (or (next-single-property-change pos 'org-linked-text)
  9333. (point-max))))
  9334. (throw 'match t))
  9335. (save-excursion
  9336. (when (or (org-in-regexp org-angle-link-re)
  9337. (let ((match (org-in-regexp org-plain-link-re)))
  9338. ;; Check a plain link is not within a bracket link
  9339. (and match
  9340. (save-excursion
  9341. (progn
  9342. (goto-char (car match))
  9343. (not (org-in-regexp org-bracket-link-regexp))))))
  9344. (let ((line_ending (save-excursion (end-of-line) (point))))
  9345. ;; We are in a line before a plain or bracket link
  9346. (or (re-search-forward org-plain-link-re line_ending t)
  9347. (re-search-forward org-bracket-link-regexp line_ending t))))
  9348. (setq type (match-string 1)
  9349. path (org-link-unescape (match-string 2)))
  9350. (throw 'match t)))
  9351. (save-excursion
  9352. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9353. (setq type "tags"
  9354. path (match-string 1))
  9355. (while (string-match ":" path)
  9356. (setq path (replace-match "+" t t path)))
  9357. (throw 'match t)))
  9358. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9359. (setq type "tree-match"
  9360. path (match-string 1))
  9361. (throw 'match t)))
  9362. (unless path
  9363. (user-error "No link found"))
  9364. ;; switch back to reference buffer
  9365. ;; needed when if called in a temporary buffer through
  9366. ;; org-open-link-from-string
  9367. (with-current-buffer (or reference-buffer (current-buffer))
  9368. ;; Remove any trailing spaces in path
  9369. (if (string-match " +\\'" path)
  9370. (setq path (replace-match "" t t path)))
  9371. (if (and org-link-translation-function
  9372. (fboundp org-link-translation-function))
  9373. ;; Check if we need to translate the link
  9374. (let ((tmp (funcall org-link-translation-function type path)))
  9375. (setq type (car tmp) path (cdr tmp))))
  9376. (cond
  9377. ((assoc type org-link-protocols)
  9378. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9379. ((equal type "help")
  9380. (let ((f-or-v (intern path)))
  9381. (cond ((fboundp f-or-v)
  9382. (describe-function f-or-v))
  9383. ((boundp f-or-v)
  9384. (describe-variable f-or-v))
  9385. (t (error "Not a known function or variable")))))
  9386. ((equal type "mailto")
  9387. (let ((cmd (car org-link-mailto-program))
  9388. (args (cdr org-link-mailto-program)) args1
  9389. (address path) (subject "") a)
  9390. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9391. (setq address (match-string 1 path)
  9392. subject (org-link-escape (match-string 2 path))))
  9393. (while args
  9394. (cond
  9395. ((not (stringp (car args))) (push (pop args) args1))
  9396. (t (setq a (pop args))
  9397. (if (string-match "%a" a)
  9398. (setq a (replace-match address t t a)))
  9399. (if (string-match "%s" a)
  9400. (setq a (replace-match subject t t a)))
  9401. (push a args1))))
  9402. (apply cmd (nreverse args1))))
  9403. ((member type '("http" "https" "ftp" "news"))
  9404. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9405. (org-link-escape
  9406. path org-link-escape-chars-browser)
  9407. path))))
  9408. ((string= type "doi")
  9409. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9410. (org-link-escape
  9411. path org-link-escape-chars-browser)
  9412. path))))
  9413. ((member type '("message"))
  9414. (browse-url (concat type ":" path)))
  9415. ((string= type "tags")
  9416. (org-tags-view arg path))
  9417. ((string= type "tree-match")
  9418. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9419. ((string= type "file")
  9420. (if (string-match "::\\([0-9]+\\)\\'" path)
  9421. (setq line (string-to-number (match-string 1 path))
  9422. path (substring path 0 (match-beginning 0)))
  9423. (if (string-match "::\\(.+\\)\\'" path)
  9424. (setq search (match-string 1 path)
  9425. path (substring path 0 (match-beginning 0)))))
  9426. (if (string-match "[*?{]" (file-name-nondirectory path))
  9427. (dired path)
  9428. (org-open-file path arg line search)))
  9429. ((string= type "shell")
  9430. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9431. (cmd path))
  9432. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9433. (string-match org-confirm-shell-link-not-regexp cmd))
  9434. (not org-confirm-shell-link-function)
  9435. (funcall org-confirm-shell-link-function
  9436. (format "Execute \"%s\" in shell? "
  9437. (org-add-props cmd nil
  9438. 'face 'org-warning))))
  9439. (progn
  9440. (message "Executing %s" cmd)
  9441. (shell-command cmd buf)
  9442. (if (featurep 'midnight)
  9443. (setq clean-buffer-list-kill-buffer-names
  9444. (cons buf clean-buffer-list-kill-buffer-names))))
  9445. (error "Abort"))))
  9446. ((string= type "elisp")
  9447. (let ((cmd path))
  9448. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9449. (string-match org-confirm-elisp-link-not-regexp cmd))
  9450. (not org-confirm-elisp-link-function)
  9451. (funcall org-confirm-elisp-link-function
  9452. (format "Execute \"%s\" as elisp? "
  9453. (org-add-props cmd nil
  9454. 'face 'org-warning))))
  9455. (message "%s => %s" cmd
  9456. (if (equal (string-to-char cmd) ?\()
  9457. (eval (read cmd))
  9458. (call-interactively (read cmd))))
  9459. (error "Abort"))))
  9460. ((and (string= type "thisfile")
  9461. (run-hook-with-args-until-success
  9462. 'org-open-link-functions path)))
  9463. ((string= type "thisfile")
  9464. (if arg
  9465. (switch-to-buffer-other-window
  9466. (org-get-buffer-for-internal-link (current-buffer)))
  9467. (org-mark-ring-push))
  9468. (let ((cmd `(org-link-search
  9469. ,path
  9470. ,(cond ((equal arg '(4)) ''occur)
  9471. ((equal arg '(16)) ''org-occur))
  9472. ,pos)))
  9473. (condition-case nil (let ((org-link-search-inhibit-query t))
  9474. (eval cmd))
  9475. (error (progn (widen) (eval cmd))))))
  9476. (t (browse-url-at-point)))))))
  9477. (move-marker org-open-link-marker nil)
  9478. (run-hook-with-args 'org-follow-link-hook)))
  9479. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9480. "Offer links in the current entry and return the selected link.
  9481. If there is only one link, return it.
  9482. If NTH is an integer, return the NTH link found.
  9483. If ZERO is a string, check also this string for a link, and if
  9484. there is one, return it."
  9485. (with-current-buffer buffer
  9486. (save-excursion
  9487. (save-restriction
  9488. (widen)
  9489. (goto-char marker)
  9490. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9491. "\\(" org-angle-link-re "\\)\\|"
  9492. "\\(" org-plain-link-re "\\)"))
  9493. (cnt ?0)
  9494. (in-emacs (if (integerp nth) nil nth))
  9495. have-zero end links link c)
  9496. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9497. (push (match-string 0 zero) links)
  9498. (setq cnt (1- cnt) have-zero t))
  9499. (save-excursion
  9500. (org-back-to-heading t)
  9501. (setq end (save-excursion (outline-next-heading) (point)))
  9502. (while (re-search-forward re end t)
  9503. (push (match-string 0) links))
  9504. (setq links (org-uniquify (reverse links))))
  9505. (cond
  9506. ((null links)
  9507. (message "No links"))
  9508. ((equal (length links) 1)
  9509. (setq link (car links)))
  9510. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9511. (setq link (nth (if have-zero nth (1- nth)) links)))
  9512. (t ; we have to select a link
  9513. (save-excursion
  9514. (save-window-excursion
  9515. (delete-other-windows)
  9516. (with-output-to-temp-buffer "*Select Link*"
  9517. (mapc (lambda (l)
  9518. (if (not (string-match org-bracket-link-regexp l))
  9519. (princ (format "[%c] %s\n" (incf cnt)
  9520. (org-remove-angle-brackets l)))
  9521. (if (match-end 3)
  9522. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9523. (match-string 3 l) (match-string 1 l)))
  9524. (princ (format "[%c] %s\n" (incf cnt)
  9525. (match-string 1 l))))))
  9526. links))
  9527. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9528. (message "Select link to open, RET to open all:")
  9529. (setq c (read-char-exclusive))
  9530. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9531. (when (equal c ?q) (error "Abort"))
  9532. (if (equal c ?\C-m)
  9533. (setq link links)
  9534. (setq nth (- c ?0))
  9535. (if have-zero (setq nth (1+ nth)))
  9536. (unless (and (integerp nth) (>= (length links) nth))
  9537. (user-error "Invalid link selection"))
  9538. (setq link (nth (1- nth) links)))))
  9539. (cons link end))))))
  9540. ;; Add special file links that specify the way of opening
  9541. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9542. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9543. (defun org-open-file-with-system (path)
  9544. "Open file at PATH using the system way of opening it."
  9545. (org-open-file path 'system))
  9546. (defun org-open-file-with-emacs (path)
  9547. "Open file at PATH in Emacs."
  9548. (org-open-file path 'emacs))
  9549. ;;; File search
  9550. (defvar org-create-file-search-functions nil
  9551. "List of functions to construct the right search string for a file link.
  9552. These functions are called in turn with point at the location to
  9553. which the link should point.
  9554. A function in the hook should first test if it would like to
  9555. handle this file type, for example by checking the `major-mode'
  9556. or the file extension. If it decides not to handle this file, it
  9557. should just return nil to give other functions a chance. If it
  9558. does handle the file, it must return the search string to be used
  9559. when following the link. The search string will be part of the
  9560. file link, given after a double colon, and `org-open-at-point'
  9561. will automatically search for it. If special measures must be
  9562. taken to make the search successful, another function should be
  9563. added to the companion hook `org-execute-file-search-functions',
  9564. which see.
  9565. A function in this hook may also use `setq' to set the variable
  9566. `description' to provide a suggestion for the descriptive text to
  9567. be used for this link when it gets inserted into an Org-mode
  9568. buffer with \\[org-insert-link].")
  9569. (defvar org-execute-file-search-functions nil
  9570. "List of functions to execute a file search triggered by a link.
  9571. Functions added to this hook must accept a single argument, the
  9572. search string that was part of the file link, the part after the
  9573. double colon. The function must first check if it would like to
  9574. handle this search, for example by checking the `major-mode' or
  9575. the file extension. If it decides not to handle this search, it
  9576. should just return nil to give other functions a chance. If it
  9577. does handle the search, it must return a non-nil value to keep
  9578. other functions from trying.
  9579. Each function can access the current prefix argument through the
  9580. variable `current-prefix-arg'. Note that a single prefix is used
  9581. to force opening a link in Emacs, so it may be good to only use a
  9582. numeric or double prefix to guide the search function.
  9583. In case this is needed, a function in this hook can also restore
  9584. the window configuration before `org-open-at-point' was called using:
  9585. (set-window-configuration org-window-config-before-follow-link)")
  9586. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9587. (defun org-link-search (s &optional type avoid-pos stealth)
  9588. "Search for a link search option.
  9589. If S is surrounded by forward slashes, it is interpreted as a
  9590. regular expression. In org-mode files, this will create an `org-occur'
  9591. sparse tree. In ordinary files, `occur' will be used to list matches.
  9592. If the current buffer is in `dired-mode', grep will be used to search
  9593. in all files. If AVOID-POS is given, ignore matches near that position.
  9594. When optional argument STEALTH is non-nil, do not modify
  9595. visibility around point, thus ignoring
  9596. `org-show-hierarchy-above', `org-show-following-heading' and
  9597. `org-show-siblings' variables."
  9598. (let ((case-fold-search t)
  9599. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9600. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9601. (append '(("") (" ") ("\t") ("\n"))
  9602. org-emphasis-alist)
  9603. "\\|") "\\)"))
  9604. (pos (point))
  9605. (pre nil) (post nil)
  9606. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9607. (cond
  9608. ;; First check if there are any special search functions
  9609. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9610. ;; Now try the builtin stuff
  9611. ((and (equal (string-to-char s0) ?#)
  9612. (> (length s0) 1)
  9613. (save-excursion
  9614. (goto-char (point-min))
  9615. (and
  9616. (re-search-forward
  9617. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9618. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9619. (setq type 'dedicated
  9620. pos (match-beginning 0))))
  9621. ;; There is an exact target for this
  9622. (goto-char pos)
  9623. (org-back-to-heading t)))
  9624. ((save-excursion
  9625. (goto-char (point-min))
  9626. (and
  9627. (re-search-forward
  9628. (concat "<<" (regexp-quote s0) ">>") nil t)
  9629. (setq type 'dedicated
  9630. pos (match-beginning 0))))
  9631. ;; There is an exact target for this
  9632. (goto-char pos))
  9633. ((save-excursion
  9634. (goto-char (point-min))
  9635. (and
  9636. (re-search-forward
  9637. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9638. (setq type 'dedicated pos (match-beginning 0))))
  9639. ;; Found an element with a matching #+name affiliated keyword.
  9640. (goto-char pos))
  9641. ((and (string-match "^(\\(.*\\))$" s0)
  9642. (save-excursion
  9643. (goto-char (point-min))
  9644. (and
  9645. (re-search-forward
  9646. (concat "[^[]" (regexp-quote
  9647. (format org-coderef-label-format
  9648. (match-string 1 s0))))
  9649. nil t)
  9650. (setq type 'dedicated
  9651. pos (1+ (match-beginning 0))))))
  9652. ;; There is a coderef target for this
  9653. (goto-char pos))
  9654. ((string-match "^/\\(.*\\)/$" s)
  9655. ;; A regular expression
  9656. (cond
  9657. ((derived-mode-p 'org-mode)
  9658. (org-occur (match-string 1 s)))
  9659. (t (org-do-occur (match-string 1 s)))))
  9660. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9661. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9662. (goto-char (point-min))
  9663. (cond
  9664. ((let (case-fold-search)
  9665. (re-search-forward (format org-complex-heading-regexp-format
  9666. (regexp-quote s))
  9667. nil t))
  9668. ;; OK, found a match
  9669. (setq type 'dedicated)
  9670. (goto-char (match-beginning 0)))
  9671. ((and (not org-link-search-inhibit-query)
  9672. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9673. (y-or-n-p "No match - create this as a new heading? "))
  9674. (goto-char (point-max))
  9675. (or (bolp) (newline))
  9676. (insert "* " s "\n")
  9677. (beginning-of-line 0))
  9678. (t
  9679. (goto-char pos)
  9680. (error "No match"))))
  9681. (t
  9682. ;; A normal search string
  9683. (when (equal (string-to-char s) ?*)
  9684. ;; Anchor on headlines, post may include tags.
  9685. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9686. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9687. s (substring s 1)))
  9688. (remove-text-properties
  9689. 0 (length s)
  9690. '(face nil mouse-face nil keymap nil fontified nil) s)
  9691. ;; Make a series of regular expressions to find a match
  9692. (setq words (org-split-string s "[ \n\r\t]+")
  9693. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9694. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9695. "\\)" markers)
  9696. re2a_ (concat "\\(" (mapconcat 'downcase words
  9697. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9698. re2a (concat "[ \t\r\n]" re2a_)
  9699. re4_ (concat "\\(" (mapconcat 'downcase words
  9700. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9701. re4 (concat "[^a-zA-Z_]" re4_)
  9702. re1 (concat pre re2 post)
  9703. re3 (concat pre (if pre re4_ re4) post)
  9704. re5 (concat pre ".*" re4)
  9705. re2 (concat pre re2)
  9706. re2a (concat pre (if pre re2a_ re2a))
  9707. re4 (concat pre (if pre re4_ re4))
  9708. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9709. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9710. re5 "\\)"))
  9711. (cond
  9712. ((eq type 'org-occur) (org-occur reall))
  9713. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9714. (t (goto-char (point-min))
  9715. (setq type 'fuzzy)
  9716. (if (or (and (org-search-not-self 1 re0 nil t)
  9717. (setq type 'dedicated))
  9718. (org-search-not-self 1 re1 nil t)
  9719. (org-search-not-self 1 re2 nil t)
  9720. (org-search-not-self 1 re2a nil t)
  9721. (org-search-not-self 1 re3 nil t)
  9722. (org-search-not-self 1 re4 nil t)
  9723. (org-search-not-self 1 re5 nil t))
  9724. (goto-char (match-beginning 1))
  9725. (goto-char pos)
  9726. (error "No match"))))))
  9727. (and (derived-mode-p 'org-mode)
  9728. (not stealth)
  9729. (org-show-context 'link-search))
  9730. type))
  9731. (defun org-search-not-self (group &rest args)
  9732. "Execute `re-search-forward', but only accept matches that do not
  9733. enclose the position of `org-open-link-marker'."
  9734. (let ((m org-open-link-marker))
  9735. (catch 'exit
  9736. (while (apply 're-search-forward args)
  9737. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9738. (goto-char (match-end group))
  9739. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9740. (> (match-beginning 0) (marker-position m))
  9741. (< (match-end 0) (marker-position m)))
  9742. (save-match-data
  9743. (or (not (org-in-regexp
  9744. org-bracket-link-analytic-regexp 1))
  9745. (not (match-end 4)) ; no description
  9746. (and (<= (match-beginning 4) (point))
  9747. (>= (match-end 4) (point))))))
  9748. (throw 'exit (point))))))))
  9749. (defun org-get-buffer-for-internal-link (buffer)
  9750. "Return a buffer to be used for displaying the link target of internal links."
  9751. (cond
  9752. ((not org-display-internal-link-with-indirect-buffer)
  9753. buffer)
  9754. ((string-match "(Clone)$" (buffer-name buffer))
  9755. (message "Buffer is already a clone, not making another one")
  9756. ;; we also do not modify visibility in this case
  9757. buffer)
  9758. (t ; make a new indirect buffer for displaying the link
  9759. (let* ((bn (buffer-name buffer))
  9760. (ibn (concat bn "(Clone)"))
  9761. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9762. (with-current-buffer ib (org-overview))
  9763. ib))))
  9764. (defun org-do-occur (regexp &optional cleanup)
  9765. "Call the Emacs command `occur'.
  9766. If CLEANUP is non-nil, remove the printout of the regular expression
  9767. in the *Occur* buffer. This is useful if the regex is long and not useful
  9768. to read."
  9769. (occur regexp)
  9770. (when cleanup
  9771. (let ((cwin (selected-window)) win beg end)
  9772. (when (setq win (get-buffer-window "*Occur*"))
  9773. (select-window win))
  9774. (goto-char (point-min))
  9775. (when (re-search-forward "match[a-z]+" nil t)
  9776. (setq beg (match-end 0))
  9777. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9778. (setq end (1- (match-beginning 0)))))
  9779. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9780. (goto-char (point-min))
  9781. (select-window cwin))))
  9782. ;;; The mark ring for links jumps
  9783. (defvar org-mark-ring nil
  9784. "Mark ring for positions before jumps in Org-mode.")
  9785. (defvar org-mark-ring-last-goto nil
  9786. "Last position in the mark ring used to go back.")
  9787. ;; Fill and close the ring
  9788. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9789. (loop for i from 1 to org-mark-ring-length do
  9790. (push (make-marker) org-mark-ring))
  9791. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9792. org-mark-ring)
  9793. (defun org-mark-ring-push (&optional pos buffer)
  9794. "Put the current position or POS into the mark ring and rotate it."
  9795. (interactive)
  9796. (setq pos (or pos (point)))
  9797. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9798. (move-marker (car org-mark-ring)
  9799. (or pos (point))
  9800. (or buffer (current-buffer)))
  9801. (message "%s"
  9802. (substitute-command-keys
  9803. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9804. (defun org-mark-ring-goto (&optional n)
  9805. "Jump to the previous position in the mark ring.
  9806. With prefix arg N, jump back that many stored positions. When
  9807. called several times in succession, walk through the entire ring.
  9808. Org-mode commands jumping to a different position in the current file,
  9809. or to another Org-mode file, automatically push the old position
  9810. onto the ring."
  9811. (interactive "p")
  9812. (let (p m)
  9813. (if (eq last-command this-command)
  9814. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9815. (setq p org-mark-ring))
  9816. (setq org-mark-ring-last-goto p)
  9817. (setq m (car p))
  9818. (org-pop-to-buffer-same-window (marker-buffer m))
  9819. (goto-char m)
  9820. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9821. (defun org-remove-angle-brackets (s)
  9822. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9823. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9824. s)
  9825. (defun org-add-angle-brackets (s)
  9826. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9827. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9828. s)
  9829. (defun org-remove-double-quotes (s)
  9830. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9831. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9832. s)
  9833. ;;; Following specific links
  9834. (defun org-follow-timestamp-link ()
  9835. "Open an agenda view for the time-stamp date/range at point."
  9836. (cond
  9837. ((org-at-date-range-p t)
  9838. (let ((org-agenda-start-on-weekday)
  9839. (t1 (match-string 1))
  9840. (t2 (match-string 2)) tt1 tt2)
  9841. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9842. tt2 (time-to-days (org-time-string-to-time t2)))
  9843. (let ((org-agenda-buffer-tmp-name
  9844. (format "*Org Agenda(a:%s)"
  9845. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9846. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9847. ((org-at-timestamp-p t)
  9848. (let ((org-agenda-buffer-tmp-name
  9849. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9850. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9851. (substring (match-string 1) 0 10)))
  9852. 1)))
  9853. (t (error "This should not happen"))))
  9854. ;;; Following file links
  9855. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9856. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9857. (declare-function mailcap-mime-info
  9858. "mailcap" (string &optional request no-decode))
  9859. (defvar org-wait nil)
  9860. (defun org-open-file (path &optional in-emacs line search)
  9861. "Open the file at PATH.
  9862. First, this expands any special file name abbreviations. Then the
  9863. configuration variable `org-file-apps' is checked if it contains an
  9864. entry for this file type, and if yes, the corresponding command is launched.
  9865. If no application is found, Emacs simply visits the file.
  9866. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9867. With a double \\[universal-argument] \\[universal-argument] \
  9868. prefix arg, Org tries to avoid opening in Emacs
  9869. and to use an external application to visit the file.
  9870. Optional LINE specifies a line to go to, optional SEARCH a string
  9871. to search for. If LINE or SEARCH is given, the file will be
  9872. opened in Emacs, unless an entry from org-file-apps that makes
  9873. use of groups in a regexp matches.
  9874. If you want to change the way frames are used when following a
  9875. link, please customize `org-link-frame-setup'.
  9876. If the file does not exist, an error is thrown."
  9877. (let* ((file (if (equal path "")
  9878. buffer-file-name
  9879. (substitute-in-file-name (expand-file-name path))))
  9880. (file-apps (append org-file-apps (org-default-apps)))
  9881. (apps (org-remove-if
  9882. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9883. (apps-dlink (org-remove-if-not
  9884. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9885. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9886. (dirp (if remp nil (file-directory-p file)))
  9887. (file (if (and dirp org-open-directory-means-index-dot-org)
  9888. (concat (file-name-as-directory file) "index.org")
  9889. file))
  9890. (a-m-a-p (assq 'auto-mode apps))
  9891. (dfile (downcase file))
  9892. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9893. (link (cond ((and (eq line nil)
  9894. (eq search nil))
  9895. file)
  9896. (line
  9897. (concat file "::" (number-to-string line)))
  9898. (search
  9899. (concat file "::" search))))
  9900. (dlink (downcase link))
  9901. (old-buffer (current-buffer))
  9902. (old-pos (point))
  9903. (old-mode major-mode)
  9904. ext cmd link-match-data)
  9905. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9906. (setq ext (match-string 1 dfile))
  9907. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9908. (setq ext (match-string 1 dfile))))
  9909. (cond
  9910. ((member in-emacs '((16) system))
  9911. (setq cmd (cdr (assoc 'system apps))))
  9912. (in-emacs (setq cmd 'emacs))
  9913. (t
  9914. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9915. (and dirp (cdr (assoc 'directory apps)))
  9916. ; first, try matching against apps-dlink
  9917. ; if we get a match here, store the match data for later
  9918. (let ((match (assoc-default dlink apps-dlink
  9919. 'string-match)))
  9920. (if match
  9921. (progn (setq link-match-data (match-data))
  9922. match)
  9923. (progn (setq in-emacs (or in-emacs line search))
  9924. nil))) ; if we have no match in apps-dlink,
  9925. ; always open the file in emacs if line or search
  9926. ; is given (for backwards compatibility)
  9927. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9928. 'string-match)
  9929. (cdr (assoc ext apps))
  9930. (cdr (assoc t apps))))))
  9931. (when (eq cmd 'system)
  9932. (setq cmd (cdr (assoc 'system apps))))
  9933. (when (eq cmd 'default)
  9934. (setq cmd (cdr (assoc t apps))))
  9935. (when (eq cmd 'mailcap)
  9936. (require 'mailcap)
  9937. (mailcap-parse-mailcaps)
  9938. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9939. (command (mailcap-mime-info mime-type)))
  9940. (if (stringp command)
  9941. (setq cmd command)
  9942. (setq cmd 'emacs))))
  9943. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9944. (not (file-exists-p file))
  9945. (not org-open-non-existing-files))
  9946. (user-error "No such file: %s" file))
  9947. (cond
  9948. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9949. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9950. (while (string-match "['\"]%s['\"]" cmd)
  9951. (setq cmd (replace-match "%s" t t cmd)))
  9952. (while (string-match "%s" cmd)
  9953. (setq cmd (replace-match
  9954. (save-match-data
  9955. (shell-quote-argument
  9956. (convert-standard-filename file)))
  9957. t t cmd)))
  9958. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9959. (save-match-data
  9960. (let ((match-index 1)
  9961. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9962. (set-match-data link-match-data)
  9963. (while (<= match-index number-of-groups)
  9964. (let ((regex (concat "%" (number-to-string match-index)))
  9965. (replace-with (match-string match-index dlink)))
  9966. (while (string-match regex cmd)
  9967. (setq cmd (replace-match replace-with t t cmd))))
  9968. (setq match-index (+ match-index 1)))))
  9969. (save-window-excursion
  9970. (message "Running %s...done" cmd)
  9971. (start-process-shell-command cmd nil cmd)
  9972. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9973. ((or (stringp cmd)
  9974. (eq cmd 'emacs))
  9975. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9976. (widen)
  9977. (if line (org-goto-line line)
  9978. (if search (org-link-search search))))
  9979. ((consp cmd)
  9980. (let ((file (convert-standard-filename file)))
  9981. (save-match-data
  9982. (set-match-data link-match-data)
  9983. (eval cmd))))
  9984. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9985. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9986. (or (not (equal old-buffer (current-buffer)))
  9987. (not (equal old-pos (point))))
  9988. (org-mark-ring-push old-pos old-buffer))))
  9989. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9990. "This function returns non-nil if `entry' uses a regular
  9991. expression which should be matched against the whole link by
  9992. org-open-file.
  9993. It assumes that is the case when the entry uses a regular
  9994. expression which has at least one grouping construct and the
  9995. action is either a lisp form or a command string containing
  9996. '%1', i.e. using at least one subexpression match as a
  9997. parameter."
  9998. (let ((selector (car entry))
  9999. (action (cdr entry)))
  10000. (if (stringp selector)
  10001. (and (> (regexp-opt-depth selector) 0)
  10002. (or (and (stringp action)
  10003. (string-match "%[0-9]" action))
  10004. (consp action)))
  10005. nil)))
  10006. (defun org-default-apps ()
  10007. "Return the default applications for this operating system."
  10008. (cond
  10009. ((eq system-type 'darwin)
  10010. org-file-apps-defaults-macosx)
  10011. ((eq system-type 'windows-nt)
  10012. org-file-apps-defaults-windowsnt)
  10013. (t org-file-apps-defaults-gnu)))
  10014. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10015. "Convert extensions to regular expressions in the cars of LIST.
  10016. Also, weed out any non-string entries, because the return value is used
  10017. only for regexp matching.
  10018. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10019. point to the symbol `emacs', indicating that the file should
  10020. be opened in Emacs."
  10021. (append
  10022. (delq nil
  10023. (mapcar (lambda (x)
  10024. (if (not (stringp (car x)))
  10025. nil
  10026. (if (string-match "\\W" (car x))
  10027. x
  10028. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10029. list))
  10030. (if add-auto-mode
  10031. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10032. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10033. (defun org-file-remote-p (file)
  10034. "Test whether FILE specifies a location on a remote system.
  10035. Return non-nil if the location is indeed remote.
  10036. For example, the filename \"/user@host:/foo\" specifies a location
  10037. on the system \"/user@host:\"."
  10038. (cond ((fboundp 'file-remote-p)
  10039. (file-remote-p file))
  10040. ((fboundp 'tramp-handle-file-remote-p)
  10041. (tramp-handle-file-remote-p file))
  10042. ((and (boundp 'ange-ftp-name-format)
  10043. (string-match (car ange-ftp-name-format) file))
  10044. t)))
  10045. ;;;; Refiling
  10046. (defun org-get-org-file ()
  10047. "Read a filename, with default directory `org-directory'."
  10048. (let ((default (or org-default-notes-file remember-data-file)))
  10049. (read-file-name (format "File name [%s]: " default)
  10050. (file-name-as-directory org-directory)
  10051. default)))
  10052. (defun org-notes-order-reversed-p ()
  10053. "Check if the current file should receive notes in reversed order."
  10054. (cond
  10055. ((not org-reverse-note-order) nil)
  10056. ((eq t org-reverse-note-order) t)
  10057. ((not (listp org-reverse-note-order)) nil)
  10058. (t (catch 'exit
  10059. (let ((all org-reverse-note-order)
  10060. entry)
  10061. (while (setq entry (pop all))
  10062. (if (string-match (car entry) buffer-file-name)
  10063. (throw 'exit (cdr entry))))
  10064. nil)))))
  10065. (defvar org-refile-target-table nil
  10066. "The list of refile targets, created by `org-refile'.")
  10067. (defvar org-agenda-new-buffers nil
  10068. "Buffers created to visit agenda files.")
  10069. (defvar org-refile-cache nil
  10070. "Cache for refile targets.")
  10071. (defvar org-refile-markers nil
  10072. "All the markers used for caching refile locations.")
  10073. (defun org-refile-marker (pos)
  10074. "Get a new refile marker, but only if caching is in use."
  10075. (if (not org-refile-use-cache)
  10076. pos
  10077. (let ((m (make-marker)))
  10078. (move-marker m pos)
  10079. (push m org-refile-markers)
  10080. m)))
  10081. (defun org-refile-cache-clear ()
  10082. "Clear the refile cache and disable all the markers."
  10083. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10084. (setq org-refile-markers nil)
  10085. (setq org-refile-cache nil)
  10086. (message "Refile cache has been cleared"))
  10087. (defun org-refile-cache-check-set (set)
  10088. "Check if all the markers in the cache still have live buffers."
  10089. (let (marker)
  10090. (catch 'exit
  10091. (while (and set (setq marker (nth 3 (pop set))))
  10092. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10093. (when (and marker (null (marker-buffer marker)))
  10094. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10095. (sit-for 3)
  10096. (throw 'exit nil)))
  10097. t)))
  10098. (defun org-refile-cache-put (set &rest identifiers)
  10099. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10100. (let* ((key (sha1 (prin1-to-string identifiers)))
  10101. (entry (assoc key org-refile-cache)))
  10102. (if entry
  10103. (setcdr entry set)
  10104. (push (cons key set) org-refile-cache))))
  10105. (defun org-refile-cache-get (&rest identifiers)
  10106. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10107. (cond
  10108. ((not org-refile-cache) nil)
  10109. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10110. (t
  10111. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10112. org-refile-cache))))
  10113. (and set (org-refile-cache-check-set set) set)))))
  10114. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10115. "Produce a table with refile targets."
  10116. (let ((case-fold-search nil)
  10117. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10118. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10119. targets tgs txt re files f desc descre fast-path-p level pos0)
  10120. (message "Getting targets...")
  10121. (with-current-buffer (or default-buffer (current-buffer))
  10122. (while (setq entry (pop entries))
  10123. (setq files (car entry) desc (cdr entry))
  10124. (setq fast-path-p nil)
  10125. (cond
  10126. ((null files) (setq files (list (current-buffer))))
  10127. ((eq files 'org-agenda-files)
  10128. (setq files (org-agenda-files 'unrestricted)))
  10129. ((and (symbolp files) (fboundp files))
  10130. (setq files (funcall files)))
  10131. ((and (symbolp files) (boundp files))
  10132. (setq files (symbol-value files))))
  10133. (if (stringp files) (setq files (list files)))
  10134. (cond
  10135. ((eq (car desc) :tag)
  10136. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10137. ((eq (car desc) :todo)
  10138. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10139. ((eq (car desc) :regexp)
  10140. (setq descre (cdr desc)))
  10141. ((eq (car desc) :level)
  10142. (setq descre (concat "^\\*\\{" (number-to-string
  10143. (if org-odd-levels-only
  10144. (1- (* 2 (cdr desc)))
  10145. (cdr desc)))
  10146. "\\}[ \t]")))
  10147. ((eq (car desc) :maxlevel)
  10148. (setq fast-path-p t)
  10149. (setq descre (concat "^\\*\\{1," (number-to-string
  10150. (if org-odd-levels-only
  10151. (1- (* 2 (cdr desc)))
  10152. (cdr desc)))
  10153. "\\}[ \t]")))
  10154. (t (error "Bad refiling target description %s" desc)))
  10155. (while (setq f (pop files))
  10156. (with-current-buffer
  10157. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10158. (or
  10159. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10160. (progn
  10161. (if (bufferp f) (setq f (buffer-file-name
  10162. (buffer-base-buffer f))))
  10163. (setq f (and f (expand-file-name f)))
  10164. (if (eq org-refile-use-outline-path 'file)
  10165. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10166. (save-excursion
  10167. (save-restriction
  10168. (widen)
  10169. (goto-char (point-min))
  10170. (while (re-search-forward descre nil t)
  10171. (goto-char (setq pos0 (point-at-bol)))
  10172. (catch 'next
  10173. (when org-refile-target-verify-function
  10174. (save-match-data
  10175. (or (funcall org-refile-target-verify-function)
  10176. (throw 'next t))))
  10177. (when (and (looking-at org-complex-heading-regexp)
  10178. (not (member (match-string 4) excluded-entries))
  10179. (match-string 4))
  10180. (setq level (org-reduced-level
  10181. (- (match-end 1) (match-beginning 1)))
  10182. txt (org-link-display-format (match-string 4))
  10183. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10184. re (format org-complex-heading-regexp-format
  10185. (regexp-quote (match-string 4))))
  10186. (when org-refile-use-outline-path
  10187. (setq txt (mapconcat
  10188. 'org-protect-slash
  10189. (append
  10190. (if (eq org-refile-use-outline-path
  10191. 'file)
  10192. (list (file-name-nondirectory
  10193. (buffer-file-name
  10194. (buffer-base-buffer))))
  10195. (if (eq org-refile-use-outline-path
  10196. 'full-file-path)
  10197. (list (buffer-file-name
  10198. (buffer-base-buffer)))))
  10199. (org-get-outline-path fast-path-p
  10200. level txt)
  10201. (list txt))
  10202. "/")))
  10203. (push (list txt f re (org-refile-marker (point)))
  10204. tgs)))
  10205. (when (= (point) pos0)
  10206. ;; verification function has not moved point
  10207. (goto-char (point-at-eol))))))))
  10208. (when org-refile-use-cache
  10209. (org-refile-cache-put tgs (buffer-file-name) descre))
  10210. (setq targets (append tgs targets))))))
  10211. (message "Getting targets...done")
  10212. (nreverse targets)))
  10213. (defun org-protect-slash (s)
  10214. (while (string-match "/" s)
  10215. (setq s (replace-match "\\" t t s)))
  10216. s)
  10217. (defvar org-olpa (make-vector 20 nil))
  10218. (defun org-get-outline-path (&optional fastp level heading)
  10219. "Return the outline path to the current entry, as a list.
  10220. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10221. routine which makes outline path derivations for an entire file,
  10222. avoiding backtracing. Refile target collection makes use of that."
  10223. (if fastp
  10224. (progn
  10225. (if (> level 19)
  10226. (error "Outline path failure, more than 19 levels"))
  10227. (loop for i from level upto 19 do
  10228. (aset org-olpa i nil))
  10229. (prog1
  10230. (delq nil (append org-olpa nil))
  10231. (aset org-olpa level heading)))
  10232. (let (rtn case-fold-search)
  10233. (save-excursion
  10234. (save-restriction
  10235. (widen)
  10236. (while (org-up-heading-safe)
  10237. (when (looking-at org-complex-heading-regexp)
  10238. (push (org-match-string-no-properties 4) rtn)))
  10239. rtn)))))
  10240. (defun org-format-outline-path (path &optional width prefix separator)
  10241. "Format the outline path PATH for display.
  10242. WIDTH is the maximum number of characters that is available.
  10243. PREFIX is a prefix to be included in the returned string,
  10244. such as the file name.
  10245. SEPARATOR is inserted between the different parts of the path,
  10246. the default is \"/\"."
  10247. (setq width (or width 79))
  10248. (if prefix (setq width (- width (length prefix))))
  10249. (if (not path)
  10250. (or prefix "")
  10251. (let* ((nsteps (length path))
  10252. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10253. (maxwidth (if (<= total-width width)
  10254. 10000 ;; everything fits
  10255. ;; we need to shorten the level headings
  10256. (/ (- width nsteps) nsteps)))
  10257. (org-odd-levels-only nil)
  10258. (n 0)
  10259. (total (1+ (length prefix))))
  10260. (setq maxwidth (max maxwidth 10))
  10261. (concat prefix
  10262. (if prefix (or separator "/"))
  10263. (mapconcat
  10264. (lambda (h)
  10265. (setq n (1+ n))
  10266. (if (and (= n nsteps) (< maxwidth 10000))
  10267. (setq maxwidth (- total-width total)))
  10268. (if (< (length h) maxwidth)
  10269. (progn (setq total (+ total (length h) 1)) h)
  10270. (setq h (substring h 0 (- maxwidth 2))
  10271. total (+ total maxwidth 1))
  10272. (if (string-match "[ \t]+\\'" h)
  10273. (setq h (substring h 0 (match-beginning 0))))
  10274. (setq h (concat h "..")))
  10275. (org-add-props h nil 'face
  10276. (nth (% (1- n) org-n-level-faces)
  10277. org-level-faces))
  10278. h)
  10279. path (or separator "/"))))))
  10280. (defun org-display-outline-path (&optional file current separator just-return-string)
  10281. "Display the current outline path in the echo area.
  10282. If FILE is non-nil, prepend the output with the file name.
  10283. If CURRENT is non-nil, append the current heading to the output.
  10284. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10285. the different parts of the path and defaults to \"/\".
  10286. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10287. (interactive "P")
  10288. (let* (case-fold-search
  10289. message-log-max ; Don't populate the *Messages* buffer
  10290. (bfn (buffer-file-name (buffer-base-buffer)))
  10291. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10292. res)
  10293. (if current (setq path (append path
  10294. (save-excursion
  10295. (org-back-to-heading t)
  10296. (if (looking-at org-complex-heading-regexp)
  10297. (list (match-string 4)))))))
  10298. (setq res
  10299. (org-format-outline-path
  10300. path
  10301. (1- (frame-width))
  10302. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10303. separator))
  10304. (if just-return-string
  10305. (org-no-properties res)
  10306. (message "%s" res))))
  10307. (defvar org-refile-history nil
  10308. "History for refiling operations.")
  10309. (defvar org-after-refile-insert-hook nil
  10310. "Hook run after `org-refile' has inserted its stuff at the new location.
  10311. Note that this is still *before* the stuff will be removed from
  10312. the *old* location.")
  10313. (defvar org-capture-last-stored-marker)
  10314. (defvar org-refile-keep nil
  10315. "Non-nil means `org-refile' will copy instead of refile.")
  10316. (defun org-copy ()
  10317. "Like `org-refile', but copy."
  10318. (interactive)
  10319. (let ((org-refile-keep t))
  10320. (funcall 'org-refile nil nil nil "Copy")))
  10321. (defun org-refile (&optional goto default-buffer rfloc msg)
  10322. "Move the entry or entries at point to another heading.
  10323. The list of target headings is compiled using the information in
  10324. `org-refile-targets', which see.
  10325. At the target location, the entry is filed as a subitem of the target
  10326. heading. Depending on `org-reverse-note-order', the new subitem will
  10327. either be the first or the last subitem.
  10328. If there is an active region, all entries in that region will be moved.
  10329. However, the region must fulfill the requirement that the first heading
  10330. is the first one sets the top-level of the moved text - at most siblings
  10331. below it are allowed.
  10332. With prefix arg GOTO, the command will only visit the target location
  10333. and not actually move anything.
  10334. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10335. go to the location where the last refiling operation has put the subtree.
  10336. With a prefix argument of `2', refile to the running clock.
  10337. RFLOC can be a refile location obtained in a different way.
  10338. MSG is a string to replace \"Refile\" in the default prompt with
  10339. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10340. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10341. If you are using target caching (see `org-refile-use-cache'),
  10342. you have to clear the target cache in order to find new targets.
  10343. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10344. prefix argument (`C-u C-u C-u C-c C-w')."
  10345. (interactive "P")
  10346. (if (member goto '(0 (64)))
  10347. (org-refile-cache-clear)
  10348. (let* ((actionmsg (or msg "Refile"))
  10349. (cbuf (current-buffer))
  10350. (regionp (org-region-active-p))
  10351. (region-start (and regionp (region-beginning)))
  10352. (region-end (and regionp (region-end)))
  10353. (region-length (and regionp (- region-end region-start)))
  10354. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10355. pos it nbuf file re level reversed)
  10356. (setq last-command nil)
  10357. (when regionp
  10358. (goto-char region-start)
  10359. (or (bolp) (goto-char (point-at-bol)))
  10360. (setq region-start (point))
  10361. (unless (or (org-kill-is-subtree-p
  10362. (buffer-substring region-start region-end))
  10363. (prog1 org-refile-active-region-within-subtree
  10364. (org-toggle-heading)))
  10365. (user-error "The region is not a (sequence of) subtree(s)")))
  10366. (if (equal goto '(16))
  10367. (org-refile-goto-last-stored)
  10368. (when (or
  10369. (and (equal goto 2)
  10370. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10371. (prog1
  10372. (setq it (list (or org-clock-heading "running clock")
  10373. (buffer-file-name
  10374. (marker-buffer org-clock-hd-marker))
  10375. ""
  10376. (marker-position org-clock-hd-marker)))
  10377. (setq goto nil)))
  10378. (setq it (or rfloc
  10379. (let (heading-text)
  10380. (save-excursion
  10381. (unless goto
  10382. (org-back-to-heading t)
  10383. (setq heading-text
  10384. (nth 4 (org-heading-components))))
  10385. (org-refile-get-location
  10386. (cond (goto "Goto")
  10387. (regionp (concat actionmsg " region to"))
  10388. (t (concat actionmsg " subtree \""
  10389. heading-text "\" to")))
  10390. default-buffer
  10391. (and (not (equal '(4) goto))
  10392. org-refile-allow-creating-parent-nodes)
  10393. goto))))))
  10394. (setq file (nth 1 it)
  10395. re (nth 2 it)
  10396. pos (nth 3 it))
  10397. (if (and (not goto)
  10398. pos
  10399. (equal (buffer-file-name) file)
  10400. (if regionp
  10401. (and (>= pos region-start)
  10402. (<= pos region-end))
  10403. (and (>= pos (point))
  10404. (< pos (save-excursion
  10405. (org-end-of-subtree t t))))))
  10406. (error "Cannot refile to position inside the tree or region"))
  10407. (setq nbuf (or (find-buffer-visiting file)
  10408. (find-file-noselect file)))
  10409. (if goto
  10410. (progn
  10411. (org-pop-to-buffer-same-window nbuf)
  10412. (goto-char pos)
  10413. (org-show-context 'org-goto))
  10414. (if regionp
  10415. (progn
  10416. (org-kill-new (buffer-substring region-start region-end))
  10417. (org-save-markers-in-region region-start region-end))
  10418. (org-copy-subtree 1 nil t))
  10419. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10420. (find-file-noselect file)))
  10421. (setq reversed (org-notes-order-reversed-p))
  10422. (save-excursion
  10423. (save-restriction
  10424. (widen)
  10425. (if pos
  10426. (progn
  10427. (goto-char pos)
  10428. (looking-at org-outline-regexp)
  10429. (setq level (org-get-valid-level (funcall outline-level) 1))
  10430. (goto-char
  10431. (if reversed
  10432. (or (outline-next-heading) (point-max))
  10433. (or (save-excursion (org-get-next-sibling))
  10434. (org-end-of-subtree t t)
  10435. (point-max)))))
  10436. (setq level 1)
  10437. (if (not reversed)
  10438. (goto-char (point-max))
  10439. (goto-char (point-min))
  10440. (or (outline-next-heading) (goto-char (point-max)))))
  10441. (if (not (bolp)) (newline))
  10442. (org-paste-subtree level)
  10443. (when org-log-refile
  10444. (org-add-log-setup 'refile nil nil 'findpos
  10445. org-log-refile)
  10446. (unless (eq org-log-refile 'note)
  10447. (save-excursion (org-add-log-note))))
  10448. (and org-auto-align-tags
  10449. (let ((org-loop-over-headlines-in-active-region nil))
  10450. (org-set-tags nil t)))
  10451. (with-demoted-errors
  10452. (bookmark-set "org-refile-last-stored"))
  10453. ;; If we are refiling for capture, make sure that the
  10454. ;; last-capture pointers point here
  10455. (when (org-bound-and-true-p org-refile-for-capture)
  10456. (with-demoted-errors
  10457. (bookmark-set "org-capture-last-stored-marker"))
  10458. (move-marker org-capture-last-stored-marker (point)))
  10459. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10460. (run-hooks 'org-after-refile-insert-hook))))
  10461. (unless org-refile-keep
  10462. (if regionp
  10463. (delete-region (point) (+ (point) region-length))
  10464. (org-cut-subtree)))
  10465. (when (featurep 'org-inlinetask)
  10466. (org-inlinetask-remove-END-maybe))
  10467. (setq org-markers-to-move nil)
  10468. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10469. (defun org-refile-goto-last-stored ()
  10470. "Go to the location where the last refile was stored."
  10471. (interactive)
  10472. (bookmark-jump "org-refile-last-stored")
  10473. (message "This is the location of the last refile"))
  10474. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10475. no-exclude)
  10476. "Prompt the user for a refile location, using PROMPT.
  10477. PROMPT should not be suffixed with a colon and a space, because
  10478. this function appends the default value from
  10479. `org-refile-history' automatically, if that is not empty.
  10480. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10481. this is used for the GOTO interface."
  10482. (let ((org-refile-targets org-refile-targets)
  10483. (org-refile-use-outline-path org-refile-use-outline-path)
  10484. excluded-entries)
  10485. (when (and (derived-mode-p 'org-mode)
  10486. (not org-refile-use-cache)
  10487. (not no-exclude))
  10488. (org-map-tree
  10489. (lambda()
  10490. (setq excluded-entries
  10491. (append excluded-entries (list (org-get-heading t t)))))))
  10492. (setq org-refile-target-table
  10493. (org-refile-get-targets default-buffer excluded-entries)))
  10494. (unless org-refile-target-table
  10495. (user-error "No refile targets"))
  10496. (let* ((cbuf (current-buffer))
  10497. (partial-completion-mode nil)
  10498. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10499. (cfunc (if (and org-refile-use-outline-path
  10500. org-outline-path-complete-in-steps)
  10501. 'org-olpath-completing-read
  10502. 'org-icompleting-read))
  10503. (extra (if org-refile-use-outline-path "/" ""))
  10504. (cbnex (concat (buffer-name) extra))
  10505. (filename (and cfn (expand-file-name cfn)))
  10506. (tbl (mapcar
  10507. (lambda (x)
  10508. (if (and (not (member org-refile-use-outline-path
  10509. '(file full-file-path)))
  10510. (not (equal filename (nth 1 x))))
  10511. (cons (concat (car x) extra " ("
  10512. (file-name-nondirectory (nth 1 x)) ")")
  10513. (cdr x))
  10514. (cons (concat (car x) extra) (cdr x))))
  10515. org-refile-target-table))
  10516. (completion-ignore-case t)
  10517. cdef
  10518. (prompt (concat prompt
  10519. (or (and (car org-refile-history)
  10520. (concat " (default " (car org-refile-history) ")"))
  10521. (and (assoc cbnex tbl) (setq cdef cbnex)
  10522. (concat " (default " cbnex ")"))) ": "))
  10523. pa answ parent-target child parent old-hist)
  10524. (setq old-hist org-refile-history)
  10525. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10526. nil 'org-refile-history (or cdef (car org-refile-history))))
  10527. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10528. (org-refile-check-position pa)
  10529. (if pa
  10530. (progn
  10531. (when (or (not org-refile-history)
  10532. (not (eq old-hist org-refile-history))
  10533. (not (equal (car pa) (car org-refile-history))))
  10534. (setq org-refile-history
  10535. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10536. org-refile-history
  10537. (cdr org-refile-history))))
  10538. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10539. (pop org-refile-history)))
  10540. pa)
  10541. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10542. (progn
  10543. (setq parent (match-string 1 answ)
  10544. child (match-string 2 answ))
  10545. (setq parent-target (or (assoc parent tbl)
  10546. (assoc (concat parent "/") tbl)))
  10547. (when (and parent-target
  10548. (or (eq new-nodes t)
  10549. (and (eq new-nodes 'confirm)
  10550. (y-or-n-p (format "Create new node \"%s\"? "
  10551. child)))))
  10552. (org-refile-new-child parent-target child)))
  10553. (user-error "Invalid target location")))))
  10554. (declare-function org-string-nw-p "org-macs" (s))
  10555. (defun org-refile-check-position (refile-pointer)
  10556. "Check if the refile pointer matches the headline to which it points."
  10557. (let* ((file (nth 1 refile-pointer))
  10558. (re (nth 2 refile-pointer))
  10559. (pos (nth 3 refile-pointer))
  10560. buffer)
  10561. (if (and (not (markerp pos)) (not file))
  10562. (user-error "Please save the buffer to a file before refiling")
  10563. (when (org-string-nw-p re)
  10564. (setq buffer (if (markerp pos)
  10565. (marker-buffer pos)
  10566. (or (find-buffer-visiting file)
  10567. (find-file-noselect file))))
  10568. (with-current-buffer buffer
  10569. (save-excursion
  10570. (save-restriction
  10571. (widen)
  10572. (goto-char pos)
  10573. (beginning-of-line 1)
  10574. (unless (org-looking-at-p re)
  10575. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10576. (defun org-refile-new-child (parent-target child)
  10577. "Use refile target PARENT-TARGET to add new CHILD below it."
  10578. (unless parent-target
  10579. (error "Cannot find parent for new node"))
  10580. (let ((file (nth 1 parent-target))
  10581. (pos (nth 3 parent-target))
  10582. level)
  10583. (with-current-buffer (or (find-buffer-visiting file)
  10584. (find-file-noselect file))
  10585. (save-excursion
  10586. (save-restriction
  10587. (widen)
  10588. (if pos
  10589. (goto-char pos)
  10590. (goto-char (point-max))
  10591. (if (not (bolp)) (newline)))
  10592. (when (looking-at org-outline-regexp)
  10593. (setq level (funcall outline-level))
  10594. (org-end-of-subtree t t))
  10595. (org-back-over-empty-lines)
  10596. (insert "\n" (make-string
  10597. (if pos (org-get-valid-level level 1) 1) ?*)
  10598. " " child "\n")
  10599. (beginning-of-line 0)
  10600. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10601. (defun org-olpath-completing-read (prompt collection &rest args)
  10602. "Read an outline path like a file name."
  10603. (let ((thetable collection)
  10604. (org-completion-use-ido nil) ; does not work with ido.
  10605. (org-completion-use-iswitchb nil)) ; or iswitchb
  10606. (apply
  10607. 'org-icompleting-read prompt
  10608. (lambda (string predicate &optional flag)
  10609. (let (rtn r f (l (length string)))
  10610. (cond
  10611. ((eq flag nil)
  10612. ;; try completion
  10613. (try-completion string thetable))
  10614. ((eq flag t)
  10615. ;; all-completions
  10616. (setq rtn (all-completions string thetable predicate))
  10617. (mapcar
  10618. (lambda (x)
  10619. (setq r (substring x l))
  10620. (if (string-match " ([^)]*)$" x)
  10621. (setq f (match-string 0 x))
  10622. (setq f ""))
  10623. (if (string-match "/" r)
  10624. (concat string (substring r 0 (match-end 0)) f)
  10625. x))
  10626. rtn))
  10627. ((eq flag 'lambda)
  10628. ;; exact match?
  10629. (assoc string thetable)))))
  10630. args)))
  10631. ;;;; Dynamic blocks
  10632. (defun org-find-dblock (name)
  10633. "Find the first dynamic block with name NAME in the buffer.
  10634. If not found, stay at current position and return nil."
  10635. (let ((case-fold-search t) pos)
  10636. (save-excursion
  10637. (goto-char (point-min))
  10638. (setq pos (and (re-search-forward
  10639. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10640. (match-beginning 0))))
  10641. (if pos (goto-char pos))
  10642. pos))
  10643. (defconst org-dblock-start-re
  10644. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10645. "Matches the start line of a dynamic block, with parameters.")
  10646. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10647. "Matches the end of a dynamic block.")
  10648. (defun org-create-dblock (plist)
  10649. "Create a dynamic block section, with parameters taken from PLIST.
  10650. PLIST must contain a :name entry which is used as name of the block."
  10651. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10652. (end-of-line 1)
  10653. (newline))
  10654. (let ((col (current-column))
  10655. (name (plist-get plist :name)))
  10656. (insert "#+BEGIN: " name)
  10657. (while plist
  10658. (if (eq (car plist) :name)
  10659. (setq plist (cddr plist))
  10660. (insert " " (prin1-to-string (pop plist)))))
  10661. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10662. (beginning-of-line -2)))
  10663. (defun org-prepare-dblock ()
  10664. "Prepare dynamic block for refresh.
  10665. This empties the block, puts the cursor at the insert position and returns
  10666. the property list including an extra property :name with the block name."
  10667. (unless (looking-at org-dblock-start-re)
  10668. (user-error "Not at a dynamic block"))
  10669. (let* ((begdel (1+ (match-end 0)))
  10670. (name (org-no-properties (match-string 1)))
  10671. (params (append (list :name name)
  10672. (read (concat "(" (match-string 3) ")")))))
  10673. (save-excursion
  10674. (beginning-of-line 1)
  10675. (skip-chars-forward " \t")
  10676. (setq params (plist-put params :indentation-column (current-column))))
  10677. (unless (re-search-forward org-dblock-end-re nil t)
  10678. (error "Dynamic block not terminated"))
  10679. (setq params
  10680. (append params
  10681. (list :content (buffer-substring
  10682. begdel (match-beginning 0)))))
  10683. (delete-region begdel (match-beginning 0))
  10684. (goto-char begdel)
  10685. (open-line 1)
  10686. params))
  10687. (defun org-map-dblocks (&optional command)
  10688. "Apply COMMAND to all dynamic blocks in the current buffer.
  10689. If COMMAND is not given, use `org-update-dblock'."
  10690. (let ((cmd (or command 'org-update-dblock)))
  10691. (save-excursion
  10692. (goto-char (point-min))
  10693. (while (re-search-forward org-dblock-start-re nil t)
  10694. (goto-char (match-beginning 0))
  10695. (save-excursion
  10696. (condition-case nil
  10697. (funcall cmd)
  10698. (error (message "Error during update of dynamic block"))))
  10699. (unless (re-search-forward org-dblock-end-re nil t)
  10700. (error "Dynamic block not terminated"))))))
  10701. (defun org-dblock-update (&optional arg)
  10702. "User command for updating dynamic blocks.
  10703. Update the dynamic block at point. With prefix ARG, update all dynamic
  10704. blocks in the buffer."
  10705. (interactive "P")
  10706. (if arg
  10707. (org-update-all-dblocks)
  10708. (or (looking-at org-dblock-start-re)
  10709. (org-beginning-of-dblock))
  10710. (org-update-dblock)))
  10711. (defun org-update-dblock ()
  10712. "Update the dynamic block at point.
  10713. This means to empty the block, parse for parameters and then call
  10714. the correct writing function."
  10715. (interactive)
  10716. (save-window-excursion
  10717. (let* ((pos (point))
  10718. (line (org-current-line))
  10719. (params (org-prepare-dblock))
  10720. (name (plist-get params :name))
  10721. (indent (plist-get params :indentation-column))
  10722. (cmd (intern (concat "org-dblock-write:" name))))
  10723. (message "Updating dynamic block `%s' at line %d..." name line)
  10724. (funcall cmd params)
  10725. (message "Updating dynamic block `%s' at line %d...done" name line)
  10726. (goto-char pos)
  10727. (when (and indent (> indent 0))
  10728. (setq indent (make-string indent ?\ ))
  10729. (save-excursion
  10730. (org-beginning-of-dblock)
  10731. (forward-line 1)
  10732. (while (not (looking-at org-dblock-end-re))
  10733. (insert indent)
  10734. (beginning-of-line 2))
  10735. (when (looking-at org-dblock-end-re)
  10736. (and (looking-at "[ \t]+")
  10737. (replace-match ""))
  10738. (insert indent)))))))
  10739. (defun org-beginning-of-dblock ()
  10740. "Find the beginning of the dynamic block at point.
  10741. Error if there is no such block at point."
  10742. (let ((pos (point))
  10743. beg)
  10744. (end-of-line 1)
  10745. (if (and (re-search-backward org-dblock-start-re nil t)
  10746. (setq beg (match-beginning 0))
  10747. (re-search-forward org-dblock-end-re nil t)
  10748. (> (match-end 0) pos))
  10749. (goto-char beg)
  10750. (goto-char pos)
  10751. (error "Not in a dynamic block"))))
  10752. (defun org-update-all-dblocks ()
  10753. "Update all dynamic blocks in the buffer.
  10754. This function can be used in a hook."
  10755. (interactive)
  10756. (when (derived-mode-p 'org-mode)
  10757. (org-map-dblocks 'org-update-dblock)))
  10758. ;;;; Completion
  10759. (defun org-get-export-keywords ()
  10760. "Return a list of all currently understood export keywords.
  10761. Export keywords include options, block names, attributes and
  10762. keywords relative to each registered export back-end."
  10763. (delq nil
  10764. (let (keywords)
  10765. (mapc
  10766. (lambda (back-end)
  10767. (let ((props (cdr back-end)))
  10768. ;; Back-end name (for keywords, like #+LATEX:)
  10769. (push (upcase (symbol-name (car back-end))) keywords)
  10770. ;; Back-end options.
  10771. (mapc (lambda (option) (push (cadr option) keywords))
  10772. (plist-get (cdr back-end) :options-alist))))
  10773. (org-bound-and-true-p org-export-registered-backends))
  10774. keywords)))
  10775. (defconst org-options-keywords
  10776. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10777. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10778. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10779. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10780. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10781. (defcustom org-structure-template-alist
  10782. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10783. "<src lang=\"?\">\n\n</src>")
  10784. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10785. "<example>\n?\n</example>")
  10786. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10787. "<quote>\n?\n</quote>")
  10788. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10789. "<verse>\n?\n</verse>")
  10790. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10791. "<verbatim>\n?\n</verbatim>")
  10792. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10793. "<center>\n?\n</center>")
  10794. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10795. "<literal style=\"latex\">\n?\n</literal>")
  10796. ("L" "#+LaTeX: "
  10797. "<literal style=\"latex\">?</literal>")
  10798. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10799. "<literal style=\"html\">\n?\n</literal>")
  10800. ("H" "#+HTML: "
  10801. "<literal style=\"html\">?</literal>")
  10802. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10803. ("A" "#+ASCII: ")
  10804. ("i" "#+INDEX: ?"
  10805. "#+INDEX: ?")
  10806. ("I" "#+INCLUDE: %file ?"
  10807. "<include file=%file markup=\"?\">"))
  10808. "Structure completion elements.
  10809. This is a list of abbreviation keys and values. The value gets inserted
  10810. if you type `<' followed by the key and then press the completion key,
  10811. usually `M-TAB'. %file will be replaced by a file name after prompting
  10812. for the file using completion. The cursor will be placed at the position
  10813. of the `?` in the template.
  10814. There are two templates for each key, the first uses the original Org syntax,
  10815. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10816. the default when the /org-mtags.el/ module has been loaded. See also the
  10817. variable `org-mtags-prefer-muse-templates'."
  10818. :group 'org-completion
  10819. :type '(repeat
  10820. (string :tag "Key")
  10821. (string :tag "Template")
  10822. (string :tag "Muse Template")))
  10823. (defun org-try-structure-completion ()
  10824. "Try to complete a structure template before point.
  10825. This looks for strings like \"<e\" on an otherwise empty line and
  10826. expands them."
  10827. (let ((l (buffer-substring (point-at-bol) (point)))
  10828. a)
  10829. (when (and (looking-at "[ \t]*$")
  10830. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10831. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10832. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10833. (match-beginning 1)) a)
  10834. t)))
  10835. (defun org-complete-expand-structure-template (start cell)
  10836. "Expand a structure template."
  10837. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10838. (rpl (nth (if musep 2 1) cell))
  10839. (ind ""))
  10840. (delete-region start (point))
  10841. (when (string-match "\\`#\\+" rpl)
  10842. (cond
  10843. ((bolp))
  10844. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10845. (setq ind (buffer-substring (point-at-bol) (point))))
  10846. (t (newline))))
  10847. (setq start (point))
  10848. (if (string-match "%file" rpl)
  10849. (setq rpl (replace-match
  10850. (concat
  10851. "\""
  10852. (save-match-data
  10853. (abbreviate-file-name (read-file-name "Include file: ")))
  10854. "\"")
  10855. t t rpl)))
  10856. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10857. (concat "\n" ind)))
  10858. (insert rpl)
  10859. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10860. ;;;; TODO, DEADLINE, Comments
  10861. (defun org-toggle-comment ()
  10862. "Change the COMMENT state of an entry."
  10863. (interactive)
  10864. (save-excursion
  10865. (org-back-to-heading)
  10866. (let (case-fold-search)
  10867. (cond
  10868. ((looking-at (format org-heading-keyword-regexp-format
  10869. org-comment-string))
  10870. (goto-char (match-end 1))
  10871. (looking-at (concat " +" org-comment-string))
  10872. (replace-match "" t t)
  10873. (when (eolp) (insert " ")))
  10874. ((looking-at org-outline-regexp)
  10875. (goto-char (match-end 0))
  10876. (insert org-comment-string " "))))))
  10877. (defvar org-last-todo-state-is-todo nil
  10878. "This is non-nil when the last TODO state change led to a TODO state.
  10879. If the last change removed the TODO tag or switched to DONE, then
  10880. this is nil.")
  10881. (defvar org-setting-tags nil) ; dynamically skipped
  10882. (defvar org-todo-setup-filter-hook nil
  10883. "Hook for functions that pre-filter todo specs.
  10884. Each function takes a todo spec and returns either nil or the spec
  10885. transformed into canonical form." )
  10886. (defvar org-todo-get-default-hook nil
  10887. "Hook for functions that get a default item for todo.
  10888. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10889. nil or a string to be used for the todo mark." )
  10890. (defvar org-agenda-headline-snapshot-before-repeat)
  10891. (defun org-current-effective-time ()
  10892. "Return current time adjusted for `org-extend-today-until' variable."
  10893. (let* ((ct (org-current-time))
  10894. (dct (decode-time ct))
  10895. (ct1
  10896. (cond
  10897. (org-use-last-clock-out-time-as-effective-time
  10898. (or (org-clock-get-last-clock-out-time) ct))
  10899. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10900. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10901. (t ct))))
  10902. ct1))
  10903. (defun org-todo-yesterday (&optional arg)
  10904. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10905. (interactive "P")
  10906. (if (eq major-mode 'org-agenda-mode)
  10907. (apply 'org-agenda-todo-yesterday arg)
  10908. (let* ((hour (third (decode-time
  10909. (org-current-time))))
  10910. (org-extend-today-until (1+ hour)))
  10911. (org-todo arg))))
  10912. (defvar org-block-entry-blocking ""
  10913. "First entry preventing the TODO state change.")
  10914. (defun org-todo (&optional arg)
  10915. "Change the TODO state of an item.
  10916. The state of an item is given by a keyword at the start of the heading,
  10917. like
  10918. *** TODO Write paper
  10919. *** DONE Call mom
  10920. The different keywords are specified in the variable `org-todo-keywords'.
  10921. By default the available states are \"TODO\" and \"DONE\".
  10922. So for this example: when the item starts with TODO, it is changed to DONE.
  10923. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10924. DONE are present, add TODO at the beginning of the heading.
  10925. With \\[universal-argument] prefix arg, use completion to determine the new \
  10926. state.
  10927. With numeric prefix arg, switch to that state.
  10928. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10929. keywords (nextset).
  10930. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10931. With a numeric prefix arg of 0, inhibit note taking for the change.
  10932. For calling through lisp, arg is also interpreted in the following way:
  10933. 'none -> empty state
  10934. \"\"(empty string) -> switch to empty state
  10935. 'done -> switch to DONE
  10936. 'nextset -> switch to the next set of keywords
  10937. 'previousset -> switch to the previous set of keywords
  10938. \"WAITING\" -> switch to the specified keyword, but only if it
  10939. really is a member of `org-todo-keywords'."
  10940. (interactive "P")
  10941. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10942. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10943. 'region-start-level 'region))
  10944. org-loop-over-headlines-in-active-region)
  10945. (org-map-entries
  10946. `(org-todo ,arg)
  10947. org-loop-over-headlines-in-active-region
  10948. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10949. (if (equal arg '(16)) (setq arg 'nextset))
  10950. (let ((org-blocker-hook org-blocker-hook)
  10951. commentp
  10952. case-fold-search)
  10953. (when (equal arg '(64))
  10954. (setq arg nil org-blocker-hook nil))
  10955. (when (and org-blocker-hook
  10956. (or org-inhibit-blocking
  10957. (org-entry-get nil "NOBLOCKING")))
  10958. (setq org-blocker-hook nil))
  10959. (save-excursion
  10960. (catch 'exit
  10961. (org-back-to-heading t)
  10962. (when (looking-at (concat "^\\*+ " org-comment-string))
  10963. (org-toggle-comment)
  10964. (setq commentp t))
  10965. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10966. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10967. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10968. (let* ((match-data (match-data))
  10969. (startpos (point-at-bol))
  10970. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10971. (org-log-done org-log-done)
  10972. (org-log-repeat org-log-repeat)
  10973. (org-todo-log-states org-todo-log-states)
  10974. (org-inhibit-logging
  10975. (if (equal arg 0)
  10976. (progn (setq arg nil) 'note) org-inhibit-logging))
  10977. (this (match-string 1))
  10978. (hl-pos (match-beginning 0))
  10979. (head (org-get-todo-sequence-head this))
  10980. (ass (assoc head org-todo-kwd-alist))
  10981. (interpret (nth 1 ass))
  10982. (done-word (nth 3 ass))
  10983. (final-done-word (nth 4 ass))
  10984. (org-last-state (or this ""))
  10985. (completion-ignore-case t)
  10986. (member (member this org-todo-keywords-1))
  10987. (tail (cdr member))
  10988. (org-state (cond
  10989. ((and org-todo-key-trigger
  10990. (or (and (equal arg '(4))
  10991. (eq org-use-fast-todo-selection 'prefix))
  10992. (and (not arg) org-use-fast-todo-selection
  10993. (not (eq org-use-fast-todo-selection
  10994. 'prefix)))))
  10995. ;; Use fast selection
  10996. (org-fast-todo-selection))
  10997. ((and (equal arg '(4))
  10998. (or (not org-use-fast-todo-selection)
  10999. (not org-todo-key-trigger)))
  11000. ;; Read a state with completion
  11001. (org-icompleting-read
  11002. "State: " (mapcar 'list org-todo-keywords-1)
  11003. nil t))
  11004. ((eq arg 'right)
  11005. (if this
  11006. (if tail (car tail) nil)
  11007. (car org-todo-keywords-1)))
  11008. ((eq arg 'left)
  11009. (if (equal member org-todo-keywords-1)
  11010. nil
  11011. (if this
  11012. (nth (- (length org-todo-keywords-1)
  11013. (length tail) 2)
  11014. org-todo-keywords-1)
  11015. (org-last org-todo-keywords-1))))
  11016. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11017. (setq arg nil))) ; hack to fall back to cycling
  11018. (arg
  11019. ;; user or caller requests a specific state
  11020. (cond
  11021. ((equal arg "") nil)
  11022. ((eq arg 'none) nil)
  11023. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11024. ((eq arg 'nextset)
  11025. (or (car (cdr (member head org-todo-heads)))
  11026. (car org-todo-heads)))
  11027. ((eq arg 'previousset)
  11028. (let ((org-todo-heads (reverse org-todo-heads)))
  11029. (or (car (cdr (member head org-todo-heads)))
  11030. (car org-todo-heads))))
  11031. ((car (member arg org-todo-keywords-1)))
  11032. ((stringp arg)
  11033. (user-error "State `%s' not valid in this file" arg))
  11034. ((nth (1- (prefix-numeric-value arg))
  11035. org-todo-keywords-1))))
  11036. ((null member) (or head (car org-todo-keywords-1)))
  11037. ((equal this final-done-word) nil) ;; -> make empty
  11038. ((null tail) nil) ;; -> first entry
  11039. ((memq interpret '(type priority))
  11040. (if (eq this-command last-command)
  11041. (car tail)
  11042. (if (> (length tail) 0)
  11043. (or done-word (car org-done-keywords))
  11044. nil)))
  11045. (t
  11046. (car tail))))
  11047. (org-state (or
  11048. (run-hook-with-args-until-success
  11049. 'org-todo-get-default-hook org-state org-last-state)
  11050. org-state))
  11051. (next (if org-state (concat " " org-state " ") " "))
  11052. (change-plist (list :type 'todo-state-change :from this :to org-state
  11053. :position startpos))
  11054. dolog now-done-p)
  11055. (when org-blocker-hook
  11056. (setq org-last-todo-state-is-todo
  11057. (not (member this org-done-keywords)))
  11058. (unless (save-excursion
  11059. (save-match-data
  11060. (org-with-wide-buffer
  11061. (run-hook-with-args-until-failure
  11062. 'org-blocker-hook change-plist))))
  11063. (if (org-called-interactively-p 'interactive)
  11064. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11065. this org-state org-block-entry-blocking)
  11066. ;; fail silently
  11067. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11068. this org-state org-block-entry-blocking)
  11069. (throw 'exit nil))))
  11070. (store-match-data match-data)
  11071. (replace-match next t t)
  11072. (unless (pos-visible-in-window-p hl-pos)
  11073. (message "TODO state changed to %s" (org-trim next)))
  11074. (unless head
  11075. (setq head (org-get-todo-sequence-head org-state)
  11076. ass (assoc head org-todo-kwd-alist)
  11077. interpret (nth 1 ass)
  11078. done-word (nth 3 ass)
  11079. final-done-word (nth 4 ass)))
  11080. (when (memq arg '(nextset previousset))
  11081. (message "Keyword-Set %d/%d: %s"
  11082. (- (length org-todo-sets) -1
  11083. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11084. (length org-todo-sets)
  11085. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11086. (setq org-last-todo-state-is-todo
  11087. (not (member org-state org-done-keywords)))
  11088. (setq now-done-p (and (member org-state org-done-keywords)
  11089. (not (member this org-done-keywords))))
  11090. (and logging (org-local-logging logging))
  11091. (when (and (or org-todo-log-states org-log-done)
  11092. (not (eq org-inhibit-logging t))
  11093. (not (memq arg '(nextset previousset))))
  11094. ;; we need to look at recording a time and note
  11095. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11096. (nth 2 (assoc this org-todo-log-states))))
  11097. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11098. (setq dolog 'time))
  11099. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11100. (and org-state
  11101. (member org-state org-not-done-keywords)
  11102. (not (member this org-not-done-keywords))))
  11103. ;; This is now a todo state and was not one before
  11104. ;; If there was a CLOSED time stamp, get rid of it.
  11105. (org-add-planning-info nil nil 'closed))
  11106. (when (and now-done-p org-log-done)
  11107. ;; It is now done, and it was not done before
  11108. (org-add-planning-info 'closed (org-current-effective-time))
  11109. (if (and (not dolog) (eq 'note org-log-done))
  11110. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11111. (when (and org-state dolog)
  11112. ;; This is a non-nil state, and we need to log it
  11113. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11114. ;; Fixup tag positioning
  11115. (org-todo-trigger-tag-changes org-state)
  11116. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11117. (when org-provide-todo-statistics
  11118. (org-update-parent-todo-statistics))
  11119. (run-hooks 'org-after-todo-state-change-hook)
  11120. (if (and arg (not (member org-state org-done-keywords)))
  11121. (setq head (org-get-todo-sequence-head org-state)))
  11122. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11123. ;; Do we need to trigger a repeat?
  11124. (when now-done-p
  11125. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11126. ;; This is for the agenda, take a snapshot of the headline.
  11127. (save-match-data
  11128. (setq org-agenda-headline-snapshot-before-repeat
  11129. (org-get-heading))))
  11130. (org-auto-repeat-maybe org-state))
  11131. ;; Fixup cursor location if close to the keyword
  11132. (if (and (outline-on-heading-p)
  11133. (not (bolp))
  11134. (save-excursion (beginning-of-line 1)
  11135. (looking-at org-todo-line-regexp))
  11136. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11137. (progn
  11138. (goto-char (or (match-end 2) (match-end 1)))
  11139. (and (looking-at " ") (just-one-space))))
  11140. (when org-trigger-hook
  11141. (save-excursion
  11142. (run-hook-with-args 'org-trigger-hook change-plist)))
  11143. (when commentp (org-toggle-comment))))))))
  11144. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11145. "Block turning an entry into a TODO, using the hierarchy.
  11146. This checks whether the current task should be blocked from state
  11147. changes. Such blocking occurs when:
  11148. 1. The task has children which are not all in a completed state.
  11149. 2. A task has a parent with the property :ORDERED:, and there
  11150. are siblings prior to the current task with incomplete
  11151. status.
  11152. 3. The parent of the task is blocked because it has siblings that should
  11153. be done first, or is child of a block grandparent TODO entry."
  11154. (if (not org-enforce-todo-dependencies)
  11155. t ; if locally turned off don't block
  11156. (catch 'dont-block
  11157. ;; If this is not a todo state change, or if this entry is already DONE,
  11158. ;; do not block
  11159. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11160. (member (plist-get change-plist :from)
  11161. (cons 'done org-done-keywords))
  11162. (member (plist-get change-plist :to)
  11163. (cons 'todo org-not-done-keywords))
  11164. (not (plist-get change-plist :to)))
  11165. (throw 'dont-block t))
  11166. ;; If this task has children, and any are undone, it's blocked
  11167. (save-excursion
  11168. (org-back-to-heading t)
  11169. (let ((this-level (funcall outline-level)))
  11170. (outline-next-heading)
  11171. (let ((child-level (funcall outline-level)))
  11172. (while (and (not (eobp))
  11173. (> child-level this-level))
  11174. ;; this todo has children, check whether they are all
  11175. ;; completed
  11176. (if (and (not (org-entry-is-done-p))
  11177. (org-entry-is-todo-p))
  11178. (progn (setq org-block-entry-blocking (org-get-heading))
  11179. (throw 'dont-block nil)))
  11180. (outline-next-heading)
  11181. (setq child-level (funcall outline-level))))))
  11182. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11183. ;; any previous siblings are undone, it's blocked
  11184. (save-excursion
  11185. (org-back-to-heading t)
  11186. (let* ((pos (point))
  11187. (parent-pos (and (org-up-heading-safe) (point))))
  11188. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11189. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11190. (forward-line 1)
  11191. (re-search-forward org-not-done-heading-regexp pos t))
  11192. (setq org-block-entry-blocking (match-string 0))
  11193. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11194. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11195. (while t
  11196. (goto-char parent-pos)
  11197. (if (not (looking-at org-not-done-heading-regexp))
  11198. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11199. (setq pos (point))
  11200. (setq parent-pos (and (org-up-heading-safe) (point)))
  11201. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11202. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11203. (forward-line 1)
  11204. (re-search-forward org-not-done-heading-regexp pos t)
  11205. (setq org-block-entry-blocking (org-get-heading)))
  11206. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11207. (defcustom org-track-ordered-property-with-tag nil
  11208. "Should the ORDERED property also be shown as a tag?
  11209. The ORDERED property decides if an entry should require subtasks to be
  11210. completed in sequence. Since a property is not very visible, setting
  11211. this option means that toggling the ORDERED property with the command
  11212. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11213. not relevant for the behavior, but it makes things more visible.
  11214. Note that toggling the tag with tags commands will not change the property
  11215. and therefore not influence behavior!
  11216. This can be t, meaning the tag ORDERED should be used, It can also be a
  11217. string to select a different tag for this task."
  11218. :group 'org-todo
  11219. :type '(choice
  11220. (const :tag "No tracking" nil)
  11221. (const :tag "Track with ORDERED tag" t)
  11222. (string :tag "Use other tag")))
  11223. (defun org-toggle-ordered-property ()
  11224. "Toggle the ORDERED property of the current entry.
  11225. For better visibility, you can track the value of this property with a tag.
  11226. See variable `org-track-ordered-property-with-tag'."
  11227. (interactive)
  11228. (let* ((t1 org-track-ordered-property-with-tag)
  11229. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11230. (save-excursion
  11231. (org-back-to-heading)
  11232. (if (org-entry-get nil "ORDERED")
  11233. (progn
  11234. (org-delete-property "ORDERED" "PROPERTIES")
  11235. (and tag (org-toggle-tag tag 'off))
  11236. (message "Subtasks can be completed in arbitrary order"))
  11237. (org-entry-put nil "ORDERED" "t")
  11238. (and tag (org-toggle-tag tag 'on))
  11239. (message "Subtasks must be completed in sequence")))))
  11240. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11241. (defun org-block-todo-from-checkboxes (change-plist)
  11242. "Block turning an entry into a TODO, using checkboxes.
  11243. This checks whether the current task should be blocked from state
  11244. changes because there are unchecked boxes in this entry."
  11245. (if (not org-enforce-todo-checkbox-dependencies)
  11246. t ; if locally turned off don't block
  11247. (catch 'dont-block
  11248. ;; If this is not a todo state change, or if this entry is already DONE,
  11249. ;; do not block
  11250. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11251. (member (plist-get change-plist :from)
  11252. (cons 'done org-done-keywords))
  11253. (member (plist-get change-plist :to)
  11254. (cons 'todo org-not-done-keywords))
  11255. (not (plist-get change-plist :to)))
  11256. (throw 'dont-block t))
  11257. ;; If this task has checkboxes that are not checked, it's blocked
  11258. (save-excursion
  11259. (org-back-to-heading t)
  11260. (let ((beg (point)) end)
  11261. (outline-next-heading)
  11262. (setq end (point))
  11263. (goto-char beg)
  11264. (if (org-list-search-forward
  11265. (concat (org-item-beginning-re)
  11266. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11267. "\\[[- ]\\]")
  11268. end t)
  11269. (progn
  11270. (if (boundp 'org-blocked-by-checkboxes)
  11271. (setq org-blocked-by-checkboxes t))
  11272. (throw 'dont-block nil)))))
  11273. t))) ; do not block
  11274. (defun org-entry-blocked-p ()
  11275. "Is the current entry blocked?"
  11276. (org-with-silent-modifications
  11277. (if (org-entry-get nil "NOBLOCKING")
  11278. nil ;; Never block this entry
  11279. (not (run-hook-with-args-until-failure
  11280. 'org-blocker-hook
  11281. (list :type 'todo-state-change
  11282. :position (point)
  11283. :from 'todo
  11284. :to 'done))))))
  11285. (defun org-update-statistics-cookies (all)
  11286. "Update the statistics cookie, either from TODO or from checkboxes.
  11287. This should be called with the cursor in a line with a statistics cookie."
  11288. (interactive "P")
  11289. (if all
  11290. (progn
  11291. (org-update-checkbox-count 'all)
  11292. (org-map-entries 'org-update-parent-todo-statistics))
  11293. (if (not (org-at-heading-p))
  11294. (org-update-checkbox-count)
  11295. (let ((pos (point-marker))
  11296. end l1 l2)
  11297. (ignore-errors (org-back-to-heading t))
  11298. (if (not (org-at-heading-p))
  11299. (org-update-checkbox-count)
  11300. (setq l1 (org-outline-level))
  11301. (setq end (save-excursion
  11302. (outline-next-heading)
  11303. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11304. (point)))
  11305. (if (and (save-excursion
  11306. (re-search-forward
  11307. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11308. (not (save-excursion (re-search-forward
  11309. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11310. (org-update-checkbox-count)
  11311. (if (and l2 (> l2 l1))
  11312. (progn
  11313. (goto-char end)
  11314. (org-update-parent-todo-statistics))
  11315. (goto-char pos)
  11316. (beginning-of-line 1)
  11317. (while (re-search-forward
  11318. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11319. (point-at-eol) t)
  11320. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11321. (goto-char pos)
  11322. (move-marker pos nil)))))
  11323. (defvar org-entry-property-inherited-from) ;; defined below
  11324. (defun org-update-parent-todo-statistics ()
  11325. "Update any statistics cookie in the parent of the current headline.
  11326. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11327. the entire subtree and this will travel up the hierarchy and update
  11328. statistics everywhere."
  11329. (let* ((prop (save-excursion (org-up-heading-safe)
  11330. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11331. (recursive (or (not org-hierarchical-todo-statistics)
  11332. (and prop (string-match "\\<recursive\\>" prop))))
  11333. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11334. 0))
  11335. (first t)
  11336. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11337. level ltoggle l1 new ndel
  11338. (cnt-all 0) (cnt-done 0) is-percent kwd
  11339. checkbox-beg ov ovs ove cookie-present)
  11340. (catch 'exit
  11341. (save-excursion
  11342. (beginning-of-line 1)
  11343. (setq ltoggle (funcall outline-level))
  11344. ;; Three situations are to consider:
  11345. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11346. ;; to the top-level ancestor on the headline;
  11347. ;; 2. If parent has "recursive" property, repeat up to the
  11348. ;; headline setting that property, taking inheritance into
  11349. ;; account;
  11350. ;; 3. Else, move up to direct parent and proceed only once.
  11351. (while (and (setq level (org-up-heading-safe))
  11352. (or recursive first)
  11353. (>= (point) lim))
  11354. (setq first nil cookie-present nil)
  11355. (unless (and level
  11356. (not (string-match
  11357. "\\<checkbox\\>"
  11358. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11359. "")))))
  11360. (throw 'exit nil))
  11361. (while (re-search-forward box-re (point-at-eol) t)
  11362. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11363. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11364. (save-match-data
  11365. (unless (outline-next-heading) (throw 'exit nil))
  11366. (while (and (looking-at org-complex-heading-regexp)
  11367. (> (setq l1 (length (match-string 1))) level))
  11368. (setq kwd (and (or recursive (= l1 ltoggle))
  11369. (match-string 2)))
  11370. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11371. (and (listp org-provide-todo-statistics)
  11372. (or (member kwd org-provide-todo-statistics)
  11373. (member kwd org-done-keywords))))
  11374. (setq cnt-all (1+ cnt-all))
  11375. (if (eq org-provide-todo-statistics t)
  11376. (and kwd (setq cnt-all (1+ cnt-all)))))
  11377. (and (member kwd org-done-keywords)
  11378. (setq cnt-done (1+ cnt-done)))
  11379. (outline-next-heading)))
  11380. (setq new
  11381. (if is-percent
  11382. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11383. (format "[%d/%d]" cnt-done cnt-all))
  11384. ndel (- (match-end 0) checkbox-beg))
  11385. ;; handle overlays when updating cookie from column view
  11386. (when (setq ov (car (overlays-at checkbox-beg)))
  11387. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11388. (delete-overlay ov))
  11389. (goto-char checkbox-beg)
  11390. (insert new)
  11391. (delete-region (point) (+ (point) ndel))
  11392. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11393. (when ov (move-overlay ov ovs ove)))
  11394. (when cookie-present
  11395. (run-hook-with-args 'org-after-todo-statistics-hook
  11396. cnt-done (- cnt-all cnt-done))))))
  11397. (run-hooks 'org-todo-statistics-hook)))
  11398. (defvar org-after-todo-statistics-hook nil
  11399. "Hook that is called after a TODO statistics cookie has been updated.
  11400. Each function is called with two arguments: the number of not-done entries
  11401. and the number of done entries.
  11402. For example, the following function, when added to this hook, will switch
  11403. an entry to DONE when all children are done, and back to TODO when new
  11404. entries are set to a TODO status. Note that this hook is only called
  11405. when there is a statistics cookie in the headline!
  11406. (defun org-summary-todo (n-done n-not-done)
  11407. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11408. (let (org-log-done org-log-states) ; turn off logging
  11409. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11410. ")
  11411. (defvar org-todo-statistics-hook nil
  11412. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11413. This hook runs even if there is no statistics cookie present, in which case
  11414. `org-after-todo-statistics-hook' would not run.")
  11415. (defun org-todo-trigger-tag-changes (state)
  11416. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11417. (let ((l org-todo-state-tags-triggers)
  11418. changes)
  11419. (when (or (not state) (equal state ""))
  11420. (setq changes (append changes (cdr (assoc "" l)))))
  11421. (when (and (stringp state) (> (length state) 0))
  11422. (setq changes (append changes (cdr (assoc state l)))))
  11423. (when (member state org-not-done-keywords)
  11424. (setq changes (append changes (cdr (assoc 'todo l)))))
  11425. (when (member state org-done-keywords)
  11426. (setq changes (append changes (cdr (assoc 'done l)))))
  11427. (dolist (c changes)
  11428. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11429. (defun org-local-logging (value)
  11430. "Get logging settings from a property VALUE."
  11431. (let* (words w a)
  11432. ;; directly set the variables, they are already local.
  11433. (setq org-log-done nil
  11434. org-log-repeat nil
  11435. org-todo-log-states nil)
  11436. (setq words (org-split-string value))
  11437. (while (setq w (pop words))
  11438. (cond
  11439. ((setq a (assoc w org-startup-options))
  11440. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11441. (set (nth 1 a) (nth 2 a))))
  11442. ((setq a (org-extract-log-state-settings w))
  11443. (and (member (car a) org-todo-keywords-1)
  11444. (push a org-todo-log-states)))))))
  11445. (defun org-get-todo-sequence-head (kwd)
  11446. "Return the head of the TODO sequence to which KWD belongs.
  11447. If KWD is not set, check if there is a text property remembering the
  11448. right sequence."
  11449. (let (p)
  11450. (cond
  11451. ((not kwd)
  11452. (or (get-text-property (point-at-bol) 'org-todo-head)
  11453. (progn
  11454. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11455. nil (point-at-eol)))
  11456. (get-text-property p 'org-todo-head))))
  11457. ((not (member kwd org-todo-keywords-1))
  11458. (car org-todo-keywords-1))
  11459. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11460. (defun org-fast-todo-selection ()
  11461. "Fast TODO keyword selection with single keys.
  11462. Returns the new TODO keyword, or nil if no state change should occur."
  11463. (let* ((fulltable org-todo-key-alist)
  11464. (done-keywords org-done-keywords) ;; needed for the faces.
  11465. (maxlen (apply 'max (mapcar
  11466. (lambda (x)
  11467. (if (stringp (car x)) (string-width (car x)) 0))
  11468. fulltable)))
  11469. (expert nil)
  11470. (fwidth (+ maxlen 3 1 3))
  11471. (ncol (/ (- (window-width) 4) fwidth))
  11472. tg cnt e c tbl
  11473. groups ingroup)
  11474. (save-excursion
  11475. (save-window-excursion
  11476. (if expert
  11477. (set-buffer (get-buffer-create " *Org todo*"))
  11478. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11479. (erase-buffer)
  11480. (org-set-local 'org-done-keywords done-keywords)
  11481. (setq tbl fulltable cnt 0)
  11482. (while (setq e (pop tbl))
  11483. (cond
  11484. ((equal e '(:startgroup))
  11485. (push '() groups) (setq ingroup t)
  11486. (when (not (= cnt 0))
  11487. (setq cnt 0)
  11488. (insert "\n"))
  11489. (insert "{ "))
  11490. ((equal e '(:endgroup))
  11491. (setq ingroup nil cnt 0)
  11492. (insert "}\n"))
  11493. ((equal e '(:newline))
  11494. (when (not (= cnt 0))
  11495. (setq cnt 0)
  11496. (insert "\n")
  11497. (setq e (car tbl))
  11498. (while (equal (car tbl) '(:newline))
  11499. (insert "\n")
  11500. (setq tbl (cdr tbl)))))
  11501. (t
  11502. (setq tg (car e) c (cdr e))
  11503. (if ingroup (push tg (car groups)))
  11504. (setq tg (org-add-props tg nil 'face
  11505. (org-get-todo-face tg)))
  11506. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11507. (insert "[" c "] " tg (make-string
  11508. (- fwidth 4 (length tg)) ?\ ))
  11509. (when (= (setq cnt (1+ cnt)) ncol)
  11510. (insert "\n")
  11511. (if ingroup (insert " "))
  11512. (setq cnt 0)))))
  11513. (insert "\n")
  11514. (goto-char (point-min))
  11515. (if (not expert) (org-fit-window-to-buffer))
  11516. (message "[a-z..]:Set [SPC]:clear")
  11517. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11518. (cond
  11519. ((or (= c ?\C-g)
  11520. (and (= c ?q) (not (rassoc c fulltable))))
  11521. (setq quit-flag t))
  11522. ((= c ?\ ) nil)
  11523. ((setq e (rassoc c fulltable) tg (car e))
  11524. tg)
  11525. (t (setq quit-flag t)))))))
  11526. (defun org-entry-is-todo-p ()
  11527. (member (org-get-todo-state) org-not-done-keywords))
  11528. (defun org-entry-is-done-p ()
  11529. (member (org-get-todo-state) org-done-keywords))
  11530. (defun org-get-todo-state ()
  11531. "Return the TODO keyword of the current subtree."
  11532. (save-excursion
  11533. (org-back-to-heading t)
  11534. (and (looking-at org-todo-line-regexp)
  11535. (match-end 2)
  11536. (match-string 2))))
  11537. (defun org-at-date-range-p (&optional inactive-ok)
  11538. "Is the cursor inside a date range?"
  11539. (interactive)
  11540. (save-excursion
  11541. (catch 'exit
  11542. (let ((pos (point)))
  11543. (skip-chars-backward "^[<\r\n")
  11544. (skip-chars-backward "<[")
  11545. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11546. (>= (match-end 0) pos)
  11547. (throw 'exit t))
  11548. (skip-chars-backward "^<[\r\n")
  11549. (skip-chars-backward "<[")
  11550. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11551. (>= (match-end 0) pos)
  11552. (throw 'exit t)))
  11553. nil)))
  11554. (defun org-get-repeat (&optional tagline)
  11555. "Check if there is a deadline/schedule with repeater in this entry."
  11556. (save-match-data
  11557. (save-excursion
  11558. (org-back-to-heading t)
  11559. (and (re-search-forward (if tagline
  11560. (concat tagline "\\s-*" org-repeat-re)
  11561. org-repeat-re)
  11562. (org-entry-end-position) t)
  11563. (match-string-no-properties 1)))))
  11564. (defvar org-last-changed-timestamp)
  11565. (defvar org-last-inserted-timestamp)
  11566. (defvar org-log-post-message)
  11567. (defvar org-log-note-purpose)
  11568. (defvar org-log-note-how)
  11569. (defvar org-log-note-extra)
  11570. (defun org-auto-repeat-maybe (done-word)
  11571. "Check if the current headline contains a repeated deadline/schedule.
  11572. If yes, set TODO state back to what it was and change the base date
  11573. of repeating deadline/scheduled time stamps to new date.
  11574. This function is run automatically after each state change to a DONE state."
  11575. ;; last-state is dynamically scoped into this function
  11576. (let* ((repeat (org-get-repeat))
  11577. (aa (assoc org-last-state org-todo-kwd-alist))
  11578. (interpret (nth 1 aa))
  11579. (head (nth 2 aa))
  11580. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11581. (msg "Entry repeats: ")
  11582. (org-log-done nil)
  11583. (org-todo-log-states nil)
  11584. re type n what ts time to-state)
  11585. (when repeat
  11586. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11587. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11588. org-todo-repeat-to-state))
  11589. (unless (and to-state (member to-state org-todo-keywords-1))
  11590. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11591. (org-todo to-state)
  11592. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11593. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11594. (org-time-stamp-format t t))))
  11595. (when org-log-repeat
  11596. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11597. (memq 'org-add-log-note post-command-hook))
  11598. ;; OK, we are already setup for some record
  11599. (if (eq org-log-repeat 'note)
  11600. ;; make sure we take a note, not only a time stamp
  11601. (setq org-log-note-how 'note))
  11602. ;; Set up for taking a record
  11603. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11604. org-last-state
  11605. 'findpos org-log-repeat)))
  11606. (org-back-to-heading t)
  11607. (org-add-planning-info nil nil 'closed)
  11608. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11609. org-deadline-time-regexp "\\)\\|\\("
  11610. org-ts-regexp "\\)"))
  11611. (while (re-search-forward
  11612. re (save-excursion (outline-next-heading) (point)) t)
  11613. (setq type (if (match-end 1) org-scheduled-string
  11614. (if (match-end 3) org-deadline-string "Plain:"))
  11615. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11616. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11617. (setq n (string-to-number (match-string 2 ts))
  11618. what (match-string 3 ts))
  11619. (if (equal what "w") (setq n (* n 7) what "d"))
  11620. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11621. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11622. ;; Preparation, see if we need to modify the start date for the change
  11623. (when (match-end 1)
  11624. (setq time (save-match-data (org-time-string-to-time ts)))
  11625. (cond
  11626. ((equal (match-string 1 ts) ".")
  11627. ;; Shift starting date to today
  11628. (org-timestamp-change
  11629. (- (org-today) (time-to-days time))
  11630. 'day))
  11631. ((equal (match-string 1 ts) "+")
  11632. (let ((nshiftmax 10) (nshift 0))
  11633. (while (or (= nshift 0)
  11634. (<= (time-to-days time)
  11635. (time-to-days (current-time))))
  11636. (when (= (incf nshift) nshiftmax)
  11637. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11638. (error "Abort")))
  11639. (org-timestamp-change n (cdr (assoc what whata)))
  11640. (org-at-timestamp-p t)
  11641. (setq ts (match-string 1))
  11642. (setq time (save-match-data (org-time-string-to-time ts)))))
  11643. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11644. ;; rematch, so that we have everything in place for the real shift
  11645. (org-at-timestamp-p t)
  11646. (setq ts (match-string 1))
  11647. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11648. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11649. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11650. (setq org-log-post-message msg)
  11651. (message "%s" msg))))
  11652. (defun org-show-todo-tree (arg)
  11653. "Make a compact tree which shows all headlines marked with TODO.
  11654. The tree will show the lines where the regexp matches, and all higher
  11655. headlines above the match.
  11656. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11657. With a numeric prefix N, construct a sparse tree for the Nth element
  11658. of `org-todo-keywords-1'."
  11659. (interactive "P")
  11660. (let ((case-fold-search nil)
  11661. (kwd-re
  11662. (cond ((null arg) org-not-done-regexp)
  11663. ((equal arg '(4))
  11664. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11665. (mapcar 'list org-todo-keywords-1))))
  11666. (concat "\\("
  11667. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11668. "\\)\\>")))
  11669. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11670. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11671. org-todo-keywords-1)))
  11672. (t (user-error "Invalid prefix argument: %s" arg)))))
  11673. (message "%d TODO entries found"
  11674. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11675. (defun org-deadline (arg &optional time)
  11676. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11677. With one universal prefix argument, remove any deadline from the item.
  11678. With two universal prefix arguments, prompt for a warning delay.
  11679. With argument TIME, set the deadline at the corresponding date. TIME
  11680. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11681. (interactive "P")
  11682. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11683. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11684. 'region-start-level 'region))
  11685. org-loop-over-headlines-in-active-region)
  11686. (org-map-entries
  11687. `(org-deadline ',arg ,time)
  11688. org-loop-over-headlines-in-active-region
  11689. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11690. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11691. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11692. (repeater (and old-date
  11693. (string-match
  11694. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11695. old-date)
  11696. (match-string 1 old-date))))
  11697. (cond
  11698. ((equal arg '(4))
  11699. (when (and old-date org-log-redeadline)
  11700. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11701. org-log-redeadline))
  11702. (org-remove-timestamp-with-keyword org-deadline-string)
  11703. (message "Item no longer has a deadline."))
  11704. ((equal arg '(16))
  11705. (save-excursion
  11706. (if (re-search-forward
  11707. org-deadline-time-regexp
  11708. (save-excursion (outline-next-heading) (point)) t)
  11709. (let* ((rpl0 (match-string 1))
  11710. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11711. (replace-match
  11712. (concat org-deadline-string
  11713. " <" rpl
  11714. (format " -%dd"
  11715. (abs
  11716. (- (time-to-days
  11717. (save-match-data
  11718. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11719. (time-to-days old-date-time))))
  11720. ">") t t))
  11721. (user-error "No deadline information to update"))))
  11722. (t
  11723. (org-add-planning-info 'deadline time 'closed)
  11724. (when (and old-date org-log-redeadline
  11725. (not (equal old-date
  11726. (substring org-last-inserted-timestamp 1 -1))))
  11727. (org-add-log-setup 'redeadline nil old-date 'findpos
  11728. org-log-redeadline))
  11729. (when repeater
  11730. (save-excursion
  11731. (org-back-to-heading t)
  11732. (when (re-search-forward (concat org-deadline-string " "
  11733. org-last-inserted-timestamp)
  11734. (save-excursion
  11735. (outline-next-heading) (point)) t)
  11736. (goto-char (1- (match-end 0)))
  11737. (insert " " repeater)
  11738. (setq org-last-inserted-timestamp
  11739. (concat (substring org-last-inserted-timestamp 0 -1)
  11740. " " repeater
  11741. (substring org-last-inserted-timestamp -1))))))
  11742. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11743. (defun org-schedule (arg &optional time)
  11744. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11745. With one universal prefix argument, remove any scheduling date from the item.
  11746. With two universal prefix arguments, prompt for a delay cookie.
  11747. With argument TIME, scheduled at the corresponding date. TIME can
  11748. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11749. (interactive "P")
  11750. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11751. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11752. 'region-start-level 'region))
  11753. org-loop-over-headlines-in-active-region)
  11754. (org-map-entries
  11755. `(org-schedule ',arg ,time)
  11756. org-loop-over-headlines-in-active-region
  11757. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11758. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11759. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11760. (repeater (and old-date
  11761. (string-match
  11762. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11763. old-date)
  11764. (match-string 1 old-date))))
  11765. (cond
  11766. ((equal arg '(4))
  11767. (progn
  11768. (when (and old-date org-log-reschedule)
  11769. (org-add-log-setup 'delschedule nil old-date 'findpos
  11770. org-log-reschedule))
  11771. (org-remove-timestamp-with-keyword org-scheduled-string)
  11772. (message "Item is no longer scheduled.")))
  11773. ((equal arg '(16))
  11774. (save-excursion
  11775. (if (re-search-forward
  11776. org-scheduled-time-regexp
  11777. (save-excursion (outline-next-heading) (point)) t)
  11778. (let* ((rpl0 (match-string 1))
  11779. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11780. (replace-match
  11781. (concat org-scheduled-string
  11782. " <" rpl
  11783. (format " -%dd"
  11784. (abs
  11785. (- (time-to-days
  11786. (save-match-data
  11787. (org-read-date nil t nil "Delay until" old-date-time)))
  11788. (time-to-days old-date-time))))
  11789. ">") t t))
  11790. (user-error "No scheduled information to update"))))
  11791. (t
  11792. (org-add-planning-info 'scheduled time 'closed)
  11793. (when (and old-date org-log-reschedule
  11794. (not (equal old-date
  11795. (substring org-last-inserted-timestamp 1 -1))))
  11796. (org-add-log-setup 'reschedule nil old-date 'findpos
  11797. org-log-reschedule))
  11798. (when repeater
  11799. (save-excursion
  11800. (org-back-to-heading t)
  11801. (when (re-search-forward (concat org-scheduled-string " "
  11802. org-last-inserted-timestamp)
  11803. (save-excursion
  11804. (outline-next-heading) (point)) t)
  11805. (goto-char (1- (match-end 0)))
  11806. (insert " " repeater)
  11807. (setq org-last-inserted-timestamp
  11808. (concat (substring org-last-inserted-timestamp 0 -1)
  11809. " " repeater
  11810. (substring org-last-inserted-timestamp -1))))))
  11811. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11812. (defun org-get-scheduled-time (pom &optional inherit)
  11813. "Get the scheduled time as a time tuple, of a format suitable
  11814. for calling org-schedule with, or if there is no scheduling,
  11815. returns nil."
  11816. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11817. (when time
  11818. (apply 'encode-time (org-parse-time-string time)))))
  11819. (defun org-get-deadline-time (pom &optional inherit)
  11820. "Get the deadline as a time tuple, of a format suitable for
  11821. calling org-deadline with, or if there is no scheduling, returns
  11822. nil."
  11823. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11824. (when time
  11825. (apply 'encode-time (org-parse-time-string time)))))
  11826. (defun org-remove-timestamp-with-keyword (keyword)
  11827. "Remove all time stamps with KEYWORD in the current entry."
  11828. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11829. beg)
  11830. (save-excursion
  11831. (org-back-to-heading t)
  11832. (setq beg (point))
  11833. (outline-next-heading)
  11834. (while (re-search-backward re beg t)
  11835. (replace-match "")
  11836. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11837. (equal (char-before) ?\ ))
  11838. (backward-delete-char 1)
  11839. (if (string-match "^[ \t]*$" (buffer-substring
  11840. (point-at-bol) (point-at-eol)))
  11841. (delete-region (point-at-bol)
  11842. (min (point-max) (1+ (point-at-eol))))))))))
  11843. (defun org-add-planning-info (what &optional time &rest remove)
  11844. "Insert new timestamp with keyword in the line directly after the headline.
  11845. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11846. If non is given, the user is prompted for a date.
  11847. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11848. be removed."
  11849. (interactive)
  11850. (let (org-time-was-given org-end-time-was-given ts
  11851. end default-time default-input)
  11852. (catch 'exit
  11853. (when (and (memq what '(scheduled deadline))
  11854. (or (not time)
  11855. (and (stringp time)
  11856. (string-match "^[-+]+[0-9]" time))))
  11857. ;; Try to get a default date/time from existing timestamp
  11858. (save-excursion
  11859. (org-back-to-heading t)
  11860. (setq end (save-excursion (outline-next-heading) (point)))
  11861. (when (re-search-forward (if (eq what 'scheduled)
  11862. org-scheduled-time-regexp
  11863. org-deadline-time-regexp)
  11864. end t)
  11865. (setq ts (match-string 1)
  11866. default-time
  11867. (apply 'encode-time (org-parse-time-string ts))
  11868. default-input (and ts (org-get-compact-tod ts))))))
  11869. (when what
  11870. (setq time
  11871. (if (stringp time)
  11872. ;; This is a string (relative or absolute), set proper date
  11873. (apply 'encode-time
  11874. (org-read-date-analyze
  11875. time default-time (decode-time default-time)))
  11876. ;; If necessary, get the time from the user
  11877. (or time (org-read-date nil 'to-time nil nil
  11878. default-time default-input)))))
  11879. (when (and org-insert-labeled-timestamps-at-point
  11880. (member what '(scheduled deadline)))
  11881. (insert
  11882. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11883. (org-insert-time-stamp time org-time-was-given
  11884. nil nil nil (list org-end-time-was-given))
  11885. (setq what nil))
  11886. (save-excursion
  11887. (save-restriction
  11888. (let (col list elt ts buffer-invisibility-spec)
  11889. (org-back-to-heading t)
  11890. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11891. (goto-char (match-end 1))
  11892. (setq col (current-column))
  11893. (goto-char (match-end 0))
  11894. (if (eobp) (insert "\n") (forward-char 1))
  11895. (when (and (not what)
  11896. (not (looking-at
  11897. (concat "[ \t]*"
  11898. org-keyword-time-not-clock-regexp))))
  11899. ;; Nothing to add, nothing to remove...... :-)
  11900. (throw 'exit nil))
  11901. (if (and (not (looking-at org-outline-regexp))
  11902. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11903. "[^\r\n]*"))
  11904. (not (equal (match-string 1) org-clock-string)))
  11905. (narrow-to-region (match-beginning 0) (match-end 0))
  11906. (insert-before-markers "\n")
  11907. (backward-char 1)
  11908. (narrow-to-region (point) (point))
  11909. (and org-adapt-indentation (org-indent-to-column col)))
  11910. ;; Check if we have to remove something.
  11911. (setq list (cons what remove))
  11912. (while list
  11913. (setq elt (pop list))
  11914. (when (or (and (eq elt 'scheduled)
  11915. (re-search-forward org-scheduled-time-regexp nil t))
  11916. (and (eq elt 'deadline)
  11917. (re-search-forward org-deadline-time-regexp nil t))
  11918. (and (eq elt 'closed)
  11919. (re-search-forward org-closed-time-regexp nil t)))
  11920. (replace-match "")
  11921. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11922. (and (looking-at "[ \t]+") (replace-match ""))
  11923. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11924. (when what
  11925. (insert
  11926. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11927. (cond ((eq what 'scheduled) org-scheduled-string)
  11928. ((eq what 'deadline) org-deadline-string)
  11929. ((eq what 'closed) org-closed-string))
  11930. " ")
  11931. (setq ts (org-insert-time-stamp
  11932. time
  11933. (or org-time-was-given
  11934. (and (eq what 'closed) org-log-done-with-time))
  11935. (eq what 'closed)
  11936. nil nil (list org-end-time-was-given)))
  11937. (insert
  11938. (if (not (or (bolp) (eq (char-before) ?\ )
  11939. (memq (char-after) '(32 10))
  11940. (eobp))) " " ""))
  11941. (end-of-line 1))
  11942. (goto-char (point-min))
  11943. (widen)
  11944. (if (and (looking-at "[ \t]*\n")
  11945. (equal (char-before) ?\n))
  11946. (delete-region (1- (point)) (point-at-eol)))
  11947. ts))))))
  11948. (defvar org-log-note-marker (make-marker))
  11949. (defvar org-log-note-purpose nil)
  11950. (defvar org-log-note-state nil)
  11951. (defvar org-log-note-previous-state nil)
  11952. (defvar org-log-note-how nil)
  11953. (defvar org-log-note-extra nil)
  11954. (defvar org-log-note-window-configuration nil)
  11955. (defvar org-log-note-return-to (make-marker))
  11956. (defvar org-log-note-effective-time nil
  11957. "Remembered current time so that dynamically scoped
  11958. `org-extend-today-until' affects tha timestamps in state change
  11959. log")
  11960. (defvar org-log-post-message nil
  11961. "Message to be displayed after a log note has been stored.
  11962. The auto-repeater uses this.")
  11963. (defun org-add-note ()
  11964. "Add a note to the current entry.
  11965. This is done in the same way as adding a state change note."
  11966. (interactive)
  11967. (org-add-log-setup 'note nil nil 'findpos nil))
  11968. (defvar org-property-end-re)
  11969. (defun org-add-log-setup (&optional purpose state prev-state
  11970. findpos how extra)
  11971. "Set up the post command hook to take a note.
  11972. If this is about to TODO state change, the new state is expected in STATE.
  11973. When FINDPOS is non-nil, find the correct position for the note in
  11974. the current entry. If not, assume that it can be inserted at point.
  11975. HOW is an indicator what kind of note should be created.
  11976. EXTRA is additional text that will be inserted into the notes buffer."
  11977. (let* ((org-log-into-drawer (org-log-into-drawer))
  11978. (drawer (cond ((stringp org-log-into-drawer)
  11979. org-log-into-drawer)
  11980. (org-log-into-drawer "LOGBOOK"))))
  11981. (save-restriction
  11982. (save-excursion
  11983. (when findpos
  11984. (org-back-to-heading t)
  11985. (narrow-to-region (point) (save-excursion
  11986. (outline-next-heading) (point)))
  11987. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11988. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11989. "[^\r\n]*\\)?"))
  11990. (goto-char (match-end 0))
  11991. (cond
  11992. (drawer
  11993. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11994. nil t)
  11995. (progn
  11996. (goto-char (match-end 0))
  11997. (or org-log-states-order-reversed
  11998. (and (re-search-forward org-property-end-re nil t)
  11999. (goto-char (1- (match-beginning 0))))))
  12000. (insert "\n:" drawer ":\n:END:")
  12001. (beginning-of-line 0)
  12002. (org-indent-line)
  12003. (beginning-of-line 2)
  12004. (org-indent-line)
  12005. (end-of-line 0)))
  12006. ((and org-log-state-notes-insert-after-drawers
  12007. (save-excursion
  12008. (forward-line) (looking-at org-drawer-regexp)))
  12009. (forward-line)
  12010. (while (looking-at org-drawer-regexp)
  12011. (goto-char (match-end 0))
  12012. (re-search-forward org-property-end-re (point-max) t)
  12013. (forward-line))
  12014. (forward-line -1)))
  12015. (unless org-log-states-order-reversed
  12016. (and (= (char-after) ?\n) (forward-char 1))
  12017. (org-skip-over-state-notes)
  12018. (skip-chars-backward " \t\n\r")))
  12019. (move-marker org-log-note-marker (point))
  12020. (setq org-log-note-purpose purpose
  12021. org-log-note-state state
  12022. org-log-note-previous-state prev-state
  12023. org-log-note-how how
  12024. org-log-note-extra extra
  12025. org-log-note-effective-time (org-current-effective-time))
  12026. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12027. (defun org-skip-over-state-notes ()
  12028. "Skip past the list of State notes in an entry."
  12029. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12030. (when (ignore-errors (goto-char (org-in-item-p)))
  12031. (let* ((struct (org-list-struct))
  12032. (prevs (org-list-prevs-alist struct)))
  12033. (while (looking-at "[ \t]*- State")
  12034. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12035. (org-list-get-item-end (point) struct)))))))
  12036. (defun org-add-log-note (&optional purpose)
  12037. "Pop up a window for taking a note, and add this note later at point."
  12038. (remove-hook 'post-command-hook 'org-add-log-note)
  12039. (setq org-log-note-window-configuration (current-window-configuration))
  12040. (delete-other-windows)
  12041. (move-marker org-log-note-return-to (point))
  12042. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12043. (goto-char org-log-note-marker)
  12044. (org-switch-to-buffer-other-window "*Org Note*")
  12045. (erase-buffer)
  12046. (if (memq org-log-note-how '(time state))
  12047. (let (current-prefix-arg) (org-store-log-note))
  12048. (let ((org-inhibit-startup t)) (org-mode))
  12049. (insert (format "# Insert note for %s.
  12050. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12051. (cond
  12052. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12053. ((eq org-log-note-purpose 'done) "closed todo item")
  12054. ((eq org-log-note-purpose 'state)
  12055. (format "state change from \"%s\" to \"%s\""
  12056. (or org-log-note-previous-state "")
  12057. (or org-log-note-state "")))
  12058. ((eq org-log-note-purpose 'reschedule)
  12059. "rescheduling")
  12060. ((eq org-log-note-purpose 'delschedule)
  12061. "no longer scheduled")
  12062. ((eq org-log-note-purpose 'redeadline)
  12063. "changing deadline")
  12064. ((eq org-log-note-purpose 'deldeadline)
  12065. "removing deadline")
  12066. ((eq org-log-note-purpose 'refile)
  12067. "refiling")
  12068. ((eq org-log-note-purpose 'note)
  12069. "this entry")
  12070. (t (error "This should not happen")))))
  12071. (if org-log-note-extra (insert org-log-note-extra))
  12072. (org-set-local 'org-finish-function 'org-store-log-note)
  12073. (run-hooks 'org-log-buffer-setup-hook)))
  12074. (defvar org-note-abort nil) ; dynamically scoped
  12075. (defun org-store-log-note ()
  12076. "Finish taking a log note, and insert it to where it belongs."
  12077. (let ((txt (buffer-string)))
  12078. (kill-buffer (current-buffer))
  12079. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12080. lines ind bul)
  12081. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12082. (setq txt (replace-match "" t t txt)))
  12083. (if (string-match "\\s-+\\'" txt)
  12084. (setq txt (replace-match "" t t txt)))
  12085. (setq lines (org-split-string txt "\n"))
  12086. (when (and note (string-match "\\S-" note))
  12087. (setq note
  12088. (org-replace-escapes
  12089. note
  12090. (list (cons "%u" (user-login-name))
  12091. (cons "%U" user-full-name)
  12092. (cons "%t" (format-time-string
  12093. (org-time-stamp-format 'long 'inactive)
  12094. org-log-note-effective-time))
  12095. (cons "%T" (format-time-string
  12096. (org-time-stamp-format 'long nil)
  12097. org-log-note-effective-time))
  12098. (cons "%d" (format-time-string
  12099. (org-time-stamp-format nil 'inactive)
  12100. org-log-note-effective-time))
  12101. (cons "%D" (format-time-string
  12102. (org-time-stamp-format nil nil)
  12103. org-log-note-effective-time))
  12104. (cons "%s" (if org-log-note-state
  12105. (concat "\"" org-log-note-state "\"")
  12106. ""))
  12107. (cons "%S" (if org-log-note-previous-state
  12108. (concat "\"" org-log-note-previous-state "\"")
  12109. "\"\"")))))
  12110. (if lines (setq note (concat note " \\\\")))
  12111. (push note lines))
  12112. (when (or current-prefix-arg org-note-abort)
  12113. (when org-log-into-drawer
  12114. (org-remove-empty-drawer-at
  12115. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12116. org-log-note-marker))
  12117. (setq lines nil))
  12118. (when lines
  12119. (with-current-buffer (marker-buffer org-log-note-marker)
  12120. (save-excursion
  12121. (goto-char org-log-note-marker)
  12122. (move-marker org-log-note-marker nil)
  12123. (end-of-line 1)
  12124. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12125. (setq ind (save-excursion
  12126. (if (ignore-errors (goto-char (org-in-item-p)))
  12127. (let ((struct (org-list-struct)))
  12128. (org-list-get-ind
  12129. (org-list-get-top-point struct) struct))
  12130. (skip-chars-backward " \r\t\n")
  12131. (cond
  12132. ((and (org-at-heading-p)
  12133. org-adapt-indentation)
  12134. (1+ (org-current-level)))
  12135. ((org-at-heading-p) 0)
  12136. (t (org-get-indentation))))))
  12137. (setq bul (org-list-bullet-string "-"))
  12138. (org-indent-line-to ind)
  12139. (insert bul (pop lines))
  12140. (let ((ind-body (+ (length bul) ind)))
  12141. (while lines
  12142. (insert "\n")
  12143. (org-indent-line-to ind-body)
  12144. (insert (pop lines))))
  12145. (message "Note stored")
  12146. (org-back-to-heading t)
  12147. (org-cycle-hide-drawers 'children))
  12148. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12149. ;; from within `org-add-log-note' because `buffer-undo-list'
  12150. ;; is then modified outside of `org-with-remote-undo'.
  12151. (when (eq this-command 'org-agenda-todo)
  12152. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12153. ;; Don't add undo information when called from `org-agenda-todo'
  12154. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12155. (set-window-configuration org-log-note-window-configuration)
  12156. (with-current-buffer (marker-buffer org-log-note-return-to)
  12157. (goto-char org-log-note-return-to))
  12158. (move-marker org-log-note-return-to nil)
  12159. (and org-log-post-message (message "%s" org-log-post-message))))
  12160. (defun org-remove-empty-drawer-at (drawer pos)
  12161. "Remove an empty drawer DRAWER at position POS.
  12162. POS may also be a marker."
  12163. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12164. (save-excursion
  12165. (save-restriction
  12166. (widen)
  12167. (goto-char pos)
  12168. (if (org-in-regexp
  12169. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12170. (replace-match ""))))))
  12171. (defvar org-ts-type nil)
  12172. (defun org-sparse-tree (&optional arg type)
  12173. "Create a sparse tree, prompt for the details.
  12174. This command can create sparse trees. You first need to select the type
  12175. of match used to create the tree:
  12176. t Show all TODO entries.
  12177. T Show entries with a specific TODO keyword.
  12178. m Show entries selected by a tags/property match.
  12179. p Enter a property name and its value (both with completion on existing
  12180. names/values) and show entries with that property.
  12181. r Show entries matching a regular expression (`/' can be used as well).
  12182. b Show deadlines and scheduled items before a date.
  12183. a Show deadlines and scheduled items after a date.
  12184. d Show deadlines due within `org-deadline-warning-days'.
  12185. D Show deadlines and scheduled items between a date range."
  12186. (interactive "P")
  12187. (let (ans kwd value ts-type)
  12188. (setq type (or type org-sparse-tree-default-date-type))
  12189. (setq org-ts-type type)
  12190. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  12191. (cond ((eq type 'all) "all timestamps")
  12192. ((eq type 'scheduled) "only scheduled")
  12193. ((eq type 'deadline) "only deadline")
  12194. ((eq type 'active) "only active timestamps")
  12195. ((eq type 'inactive) "only inactive timestamps")
  12196. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12197. ((eq type 'closed) "with a closed time-stamp")
  12198. (t "scheduled/deadline")))
  12199. (setq ans (read-char-exclusive))
  12200. (cond
  12201. ((equal ans ?c)
  12202. (org-sparse-tree
  12203. arg (cadr (member type '(scheduled-or-deadline
  12204. all scheduled deadline active inactive closed)))))
  12205. ((equal ans ?d)
  12206. (call-interactively 'org-check-deadlines))
  12207. ((equal ans ?b)
  12208. (call-interactively 'org-check-before-date))
  12209. ((equal ans ?a)
  12210. (call-interactively 'org-check-after-date))
  12211. ((equal ans ?D)
  12212. (call-interactively 'org-check-dates-range))
  12213. ((equal ans ?t)
  12214. (call-interactively 'org-show-todo-tree))
  12215. ((equal ans ?T)
  12216. (org-show-todo-tree '(4)))
  12217. ((member ans '(?T ?m))
  12218. (call-interactively 'org-match-sparse-tree))
  12219. ((member ans '(?p ?P))
  12220. (setq kwd (org-icompleting-read "Property: "
  12221. (mapcar 'list (org-buffer-property-keys))))
  12222. (setq value (org-icompleting-read "Value: "
  12223. (mapcar 'list (org-property-values kwd))))
  12224. (unless (string-match "\\`{.*}\\'" value)
  12225. (setq value (concat "\"" value "\"")))
  12226. (org-match-sparse-tree arg (concat kwd "=" value)))
  12227. ((member ans '(?r ?R ?/))
  12228. (call-interactively 'org-occur))
  12229. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12230. (defvar org-occur-highlights nil
  12231. "List of overlays used for occur matches.")
  12232. (make-variable-buffer-local 'org-occur-highlights)
  12233. (defvar org-occur-parameters nil
  12234. "Parameters of the active org-occur calls.
  12235. This is a list, each call to org-occur pushes as cons cell,
  12236. containing the regular expression and the callback, onto the list.
  12237. The list can contain several entries if `org-occur' has been called
  12238. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12239. will only contain one set of parameters. When the highlights are
  12240. removed (for example with `C-c C-c', or with the next edit (depending
  12241. on `org-remove-highlights-with-change'), this variable is emptied
  12242. as well.")
  12243. (make-variable-buffer-local 'org-occur-parameters)
  12244. (defun org-occur (regexp &optional keep-previous callback)
  12245. "Make a compact tree which shows all matches of REGEXP.
  12246. The tree will show the lines where the regexp matches, and all higher
  12247. headlines above the match. It will also show the heading after the match,
  12248. to make sure editing the matching entry is easy.
  12249. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12250. call to `org-occur' will be kept, to allow stacking of calls to this
  12251. command.
  12252. If CALLBACK is non-nil, it is a function which is called to confirm
  12253. that the match should indeed be shown."
  12254. (interactive "sRegexp: \nP")
  12255. (when (equal regexp "")
  12256. (user-error "Regexp cannot be empty"))
  12257. (unless keep-previous
  12258. (org-remove-occur-highlights nil nil t))
  12259. (push (cons regexp callback) org-occur-parameters)
  12260. (let ((cnt 0))
  12261. (save-excursion
  12262. (goto-char (point-min))
  12263. (if (or (not keep-previous) ; do not want to keep
  12264. (not org-occur-highlights)) ; no previous matches
  12265. ;; hide everything
  12266. (org-overview))
  12267. (while (re-search-forward regexp nil t)
  12268. (when (or (not callback)
  12269. (save-match-data (funcall callback)))
  12270. (setq cnt (1+ cnt))
  12271. (when org-highlight-sparse-tree-matches
  12272. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12273. (org-show-context 'occur-tree))))
  12274. (when org-remove-highlights-with-change
  12275. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12276. nil 'local))
  12277. (unless org-sparse-tree-open-archived-trees
  12278. (org-hide-archived-subtrees (point-min) (point-max)))
  12279. (run-hooks 'org-occur-hook)
  12280. (if (org-called-interactively-p 'interactive)
  12281. (message "%d match(es) for regexp %s" cnt regexp))
  12282. cnt))
  12283. (defun org-occur-next-match (&optional n reset)
  12284. "Function for `next-error-function' to find sparse tree matches.
  12285. N is the number of matches to move, when negative move backwards.
  12286. RESET is entirely ignored - this function always goes back to the
  12287. starting point when no match is found."
  12288. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12289. (search-func (if (< n 0)
  12290. 'previous-single-char-property-change
  12291. 'next-single-char-property-change))
  12292. (n (abs n))
  12293. (pos (point))
  12294. p1)
  12295. (catch 'exit
  12296. (while (setq p1 (funcall search-func (point) 'org-type))
  12297. (when (equal p1 limit)
  12298. (goto-char pos)
  12299. (error "No more matches"))
  12300. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12301. (setq n (1- n))
  12302. (when (= n 0)
  12303. (goto-char p1)
  12304. (throw 'exit (point))))
  12305. (goto-char p1))
  12306. (goto-char p1)
  12307. (error "No more matches"))))
  12308. (defun org-show-context (&optional key)
  12309. "Make sure point and context are visible.
  12310. How much context is shown depends upon the variables
  12311. `org-show-hierarchy-above', `org-show-following-heading',
  12312. `org-show-entry-below' and `org-show-siblings'."
  12313. (let ((heading-p (org-at-heading-p t))
  12314. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12315. (following-p (org-get-alist-option org-show-following-heading key))
  12316. (entry-p (org-get-alist-option org-show-entry-below key))
  12317. (siblings-p (org-get-alist-option org-show-siblings key)))
  12318. ;; Show heading or entry text
  12319. (if (and heading-p (not entry-p))
  12320. (org-flag-heading nil) ; only show the heading
  12321. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12322. (org-show-hidden-entry))) ; show entire entry
  12323. (when following-p
  12324. ;; Show next sibling, or heading below text
  12325. (save-excursion
  12326. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12327. (org-flag-heading nil))))
  12328. (when siblings-p (org-show-siblings))
  12329. (when hierarchy-p
  12330. ;; show all higher headings, possibly with siblings
  12331. (save-excursion
  12332. (while (and (condition-case nil
  12333. (progn (org-up-heading-all 1) t)
  12334. (error nil))
  12335. (not (bobp)))
  12336. (org-flag-heading nil)
  12337. (when siblings-p (org-show-siblings)))))
  12338. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12339. (defvar org-reveal-start-hook nil
  12340. "Hook run before revealing a location.")
  12341. (defun org-reveal (&optional siblings)
  12342. "Show current entry, hierarchy above it, and the following headline.
  12343. This can be used to show a consistent set of context around locations
  12344. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12345. not t for the search context.
  12346. With optional argument SIBLINGS, on each level of the hierarchy all
  12347. siblings are shown. This repairs the tree structure to what it would
  12348. look like when opened with hierarchical calls to `org-cycle'.
  12349. With double optional argument \\[universal-argument] \\[universal-argument], \
  12350. go to the parent and show the
  12351. entire tree."
  12352. (interactive "P")
  12353. (run-hooks 'org-reveal-start-hook)
  12354. (let ((org-show-hierarchy-above t)
  12355. (org-show-following-heading t)
  12356. (org-show-siblings (if siblings t org-show-siblings)))
  12357. (org-show-context nil))
  12358. (when (equal siblings '(16))
  12359. (save-excursion
  12360. (when (org-up-heading-safe)
  12361. (org-show-subtree)
  12362. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12363. (defun org-highlight-new-match (beg end)
  12364. "Highlight from BEG to END and mark the highlight is an occur headline."
  12365. (let ((ov (make-overlay beg end)))
  12366. (overlay-put ov 'face 'secondary-selection)
  12367. (overlay-put ov 'org-type 'org-occur)
  12368. (push ov org-occur-highlights)))
  12369. (defun org-remove-occur-highlights (&optional beg end noremove)
  12370. "Remove the occur highlights from the buffer.
  12371. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12372. from the `before-change-functions' in the current buffer."
  12373. (interactive)
  12374. (unless org-inhibit-highlight-removal
  12375. (mapc 'delete-overlay org-occur-highlights)
  12376. (setq org-occur-highlights nil)
  12377. (setq org-occur-parameters nil)
  12378. (unless noremove
  12379. (remove-hook 'before-change-functions
  12380. 'org-remove-occur-highlights 'local))))
  12381. ;;;; Priorities
  12382. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12383. "Regular expression matching the priority indicator.")
  12384. (defvar org-remove-priority-next-time nil)
  12385. (defun org-priority-up ()
  12386. "Increase the priority of the current item."
  12387. (interactive)
  12388. (org-priority 'up))
  12389. (defun org-priority-down ()
  12390. "Decrease the priority of the current item."
  12391. (interactive)
  12392. (org-priority 'down))
  12393. (defun org-priority (&optional action show)
  12394. "Change the priority of an item.
  12395. ACTION can be `set', `up', `down', or a character."
  12396. (interactive "P")
  12397. (if (equal action '(4))
  12398. (org-show-priority)
  12399. (unless org-enable-priority-commands
  12400. (user-error "Priority commands are disabled"))
  12401. (setq action (or action 'set))
  12402. (let (current new news have remove)
  12403. (save-excursion
  12404. (org-back-to-heading t)
  12405. (if (looking-at org-priority-regexp)
  12406. (setq current (string-to-char (match-string 2))
  12407. have t))
  12408. (cond
  12409. ((eq action 'remove)
  12410. (setq remove t new ?\ ))
  12411. ((or (eq action 'set)
  12412. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12413. (if (not (eq action 'set))
  12414. (setq new action)
  12415. (message "Priority %c-%c, SPC to remove: "
  12416. org-highest-priority org-lowest-priority)
  12417. (save-match-data
  12418. (setq new (read-char-exclusive))))
  12419. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12420. (= (upcase org-lowest-priority) org-lowest-priority))
  12421. (setq new (upcase new)))
  12422. (cond ((equal new ?\ ) (setq remove t))
  12423. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12424. (user-error "Priority must be between `%c' and `%c'"
  12425. org-highest-priority org-lowest-priority))))
  12426. ((eq action 'up)
  12427. (setq new (if have
  12428. (1- current) ; normal cycling
  12429. ;; last priority was empty
  12430. (if (eq last-command this-command)
  12431. org-lowest-priority ; wrap around empty to lowest
  12432. ;; default
  12433. (if org-priority-start-cycle-with-default
  12434. org-default-priority
  12435. (1- org-default-priority))))))
  12436. ((eq action 'down)
  12437. (setq new (if have
  12438. (1+ current) ; normal cycling
  12439. ;; last priority was empty
  12440. (if (eq last-command this-command)
  12441. org-highest-priority ; wrap around empty to highest
  12442. ;; default
  12443. (if org-priority-start-cycle-with-default
  12444. org-default-priority
  12445. (1+ org-default-priority))))))
  12446. (t (user-error "Invalid action")))
  12447. (if (or (< (upcase new) org-highest-priority)
  12448. (> (upcase new) org-lowest-priority))
  12449. (if (and (memq action '(up down))
  12450. (not have) (not (eq last-command this-command)))
  12451. ;; `new' is from default priority
  12452. (error
  12453. "The default can not be set, see `org-default-priority' why")
  12454. ;; normal cycling: `new' is beyond highest/lowest priority
  12455. ;; and is wrapped around to the empty priority
  12456. (setq remove t)))
  12457. (setq news (format "%c" new))
  12458. (if have
  12459. (if remove
  12460. (replace-match "" t t nil 1)
  12461. (replace-match news t t nil 2))
  12462. (if remove
  12463. (user-error "No priority cookie found in line")
  12464. (let ((case-fold-search nil))
  12465. (looking-at org-todo-line-regexp))
  12466. (if (match-end 2)
  12467. (progn
  12468. (goto-char (match-end 2))
  12469. (insert " [#" news "]"))
  12470. (goto-char (match-beginning 3))
  12471. (insert "[#" news "] "))))
  12472. (org-preserve-lc (org-set-tags nil 'align)))
  12473. (if remove
  12474. (message "Priority removed")
  12475. (message "Priority of current item set to %s" news)))))
  12476. (defun org-show-priority ()
  12477. "Show the priority of the current item.
  12478. This priority is composed of the main priority given with the [#A] cookies,
  12479. and by additional input from the age of a schedules or deadline entry."
  12480. (interactive)
  12481. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12482. (org-get-at-bol 'priority)
  12483. (save-excursion
  12484. (save-match-data
  12485. (beginning-of-line)
  12486. (and (looking-at org-heading-regexp)
  12487. (org-get-priority (match-string 0))))))))
  12488. (message "Priority is %d" (if pri pri -1000))))
  12489. (defun org-get-priority (s)
  12490. "Find priority cookie and return priority."
  12491. (save-match-data
  12492. (if (functionp org-get-priority-function)
  12493. (funcall org-get-priority-function)
  12494. (if (not (string-match org-priority-regexp s))
  12495. (* 1000 (- org-lowest-priority org-default-priority))
  12496. (* 1000 (- org-lowest-priority
  12497. (string-to-char (match-string 2 s))))))))
  12498. ;;;; Tags
  12499. (defvar org-agenda-archives-mode)
  12500. (defvar org-map-continue-from nil
  12501. "Position from where mapping should continue.
  12502. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12503. (defvar org-scanner-tags nil
  12504. "The current tag list while the tags scanner is running.")
  12505. (defvar org-trust-scanner-tags nil
  12506. "Should `org-get-tags-at' use the tags for the scanner.
  12507. This is for internal dynamical scoping only.
  12508. When this is non-nil, the function `org-get-tags-at' will return the value
  12509. of `org-scanner-tags' instead of building the list by itself. This
  12510. can lead to large speed-ups when the tags scanner is used in a file with
  12511. many entries, and when the list of tags is retrieved, for example to
  12512. obtain a list of properties. Building the tags list for each entry in such
  12513. a file becomes an N^2 operation - but with this variable set, it scales
  12514. as N.")
  12515. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12516. "Sca headline tags with inheritance and produce output ACTION.
  12517. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12518. or `agenda' to produce an entry list for an agenda view. It can also be
  12519. a Lisp form or a function that should be called at each matched headline, in
  12520. this case the return value is a list of all return values from these calls.
  12521. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12522. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12523. only lines with a not-done TODO keyword are included in the output.
  12524. This should be the same variable that was scoped into
  12525. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12526. START-LEVEL can be a string with asterisks, reducing the scope to
  12527. headlines matching this string."
  12528. (require 'org-agenda)
  12529. (let* ((re (concat "^"
  12530. (if start-level
  12531. ;; Get the correct level to match
  12532. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12533. org-outline-regexp)
  12534. " *\\(\\<\\("
  12535. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12536. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12537. (props (list 'face 'default
  12538. 'done-face 'org-agenda-done
  12539. 'undone-face 'default
  12540. 'mouse-face 'highlight
  12541. 'org-not-done-regexp org-not-done-regexp
  12542. 'org-todo-regexp org-todo-regexp
  12543. 'org-complex-heading-regexp org-complex-heading-regexp
  12544. 'help-echo
  12545. (format "mouse-2 or RET jump to org file %s"
  12546. (abbreviate-file-name
  12547. (or (buffer-file-name (buffer-base-buffer))
  12548. (buffer-name (buffer-base-buffer)))))))
  12549. (case-fold-search nil)
  12550. (org-map-continue-from nil)
  12551. lspos tags tags-list
  12552. (tags-alist (list (cons 0 org-file-tags)))
  12553. (llast 0) rtn rtn1 level category i txt
  12554. todo marker entry priority)
  12555. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12556. (setq action (list 'lambda nil action)))
  12557. (save-excursion
  12558. (goto-char (point-min))
  12559. (when (eq action 'sparse-tree)
  12560. (org-overview)
  12561. (org-remove-occur-highlights))
  12562. (while (re-search-forward re nil t)
  12563. (setq org-map-continue-from nil)
  12564. (catch :skip
  12565. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12566. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12567. (goto-char (setq lspos (match-beginning 0)))
  12568. (setq level (org-reduced-level (org-outline-level))
  12569. category (org-get-category))
  12570. (setq i llast llast level)
  12571. ;; remove tag lists from same and sublevels
  12572. (while (>= i level)
  12573. (when (setq entry (assoc i tags-alist))
  12574. (setq tags-alist (delete entry tags-alist)))
  12575. (setq i (1- i)))
  12576. ;; add the next tags
  12577. (when tags
  12578. (setq tags (org-split-string tags ":")
  12579. tags-alist
  12580. (cons (cons level tags) tags-alist)))
  12581. ;; compile tags for current headline
  12582. (setq tags-list
  12583. (if org-use-tag-inheritance
  12584. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12585. tags)
  12586. org-scanner-tags tags-list)
  12587. (when org-use-tag-inheritance
  12588. (setcdr (car tags-alist)
  12589. (mapcar (lambda (x)
  12590. (setq x (copy-sequence x))
  12591. (org-add-prop-inherited x))
  12592. (cdar tags-alist))))
  12593. (when (and tags org-use-tag-inheritance
  12594. (or (not (eq t org-use-tag-inheritance))
  12595. org-tags-exclude-from-inheritance))
  12596. ;; selective inheritance, remove uninherited ones
  12597. (setcdr (car tags-alist)
  12598. (org-remove-uninherited-tags (cdar tags-alist))))
  12599. (when (and
  12600. ;; eval matcher only when the todo condition is OK
  12601. (and (or (not todo-only) (member todo org-not-done-keywords))
  12602. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12603. (eval matcher)))
  12604. ;; Call the skipper, but return t if it does not skip,
  12605. ;; so that the `and' form continues evaluating
  12606. (progn
  12607. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12608. t)
  12609. ;; Check if timestamps are deselecting this entry
  12610. (or (not todo-only)
  12611. (and (member todo org-not-done-keywords)
  12612. (or (not org-agenda-tags-todo-honor-ignore-options)
  12613. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12614. ;; select this headline
  12615. (cond
  12616. ((eq action 'sparse-tree)
  12617. (and org-highlight-sparse-tree-matches
  12618. (org-get-heading) (match-end 0)
  12619. (org-highlight-new-match
  12620. (match-beginning 1) (match-end 1)))
  12621. (org-show-context 'tags-tree))
  12622. ((eq action 'agenda)
  12623. (setq txt (org-agenda-format-item
  12624. ""
  12625. (concat
  12626. (if (eq org-tags-match-list-sublevels 'indented)
  12627. (make-string (1- level) ?.) "")
  12628. (org-get-heading))
  12629. level category
  12630. tags-list)
  12631. priority (org-get-priority txt))
  12632. (goto-char lspos)
  12633. (setq marker (org-agenda-new-marker))
  12634. (org-add-props txt props
  12635. 'org-marker marker 'org-hd-marker marker 'org-category category
  12636. 'todo-state todo
  12637. 'priority priority 'type "tagsmatch")
  12638. (push txt rtn))
  12639. ((functionp action)
  12640. (setq org-map-continue-from nil)
  12641. (save-excursion
  12642. (setq rtn1 (funcall action))
  12643. (push rtn1 rtn)))
  12644. (t (user-error "Invalid action")))
  12645. ;; if we are to skip sublevels, jump to end of subtree
  12646. (unless org-tags-match-list-sublevels
  12647. (org-end-of-subtree t)
  12648. (backward-char 1))))
  12649. ;; Get the correct position from where to continue
  12650. (if org-map-continue-from
  12651. (goto-char org-map-continue-from)
  12652. (and (= (point) lspos) (end-of-line 1)))))
  12653. (when (and (eq action 'sparse-tree)
  12654. (not org-sparse-tree-open-archived-trees))
  12655. (org-hide-archived-subtrees (point-min) (point-max)))
  12656. (nreverse rtn)))
  12657. (defun org-remove-uninherited-tags (tags)
  12658. "Remove all tags that are not inherited from the list TAGS."
  12659. (cond
  12660. ((eq org-use-tag-inheritance t)
  12661. (if org-tags-exclude-from-inheritance
  12662. (org-delete-all org-tags-exclude-from-inheritance tags)
  12663. tags))
  12664. ((not org-use-tag-inheritance) nil)
  12665. ((stringp org-use-tag-inheritance)
  12666. (delq nil (mapcar
  12667. (lambda (x)
  12668. (if (and (string-match org-use-tag-inheritance x)
  12669. (not (member x org-tags-exclude-from-inheritance)))
  12670. x nil))
  12671. tags)))
  12672. ((listp org-use-tag-inheritance)
  12673. (delq nil (mapcar
  12674. (lambda (x)
  12675. (if (member x org-use-tag-inheritance) x nil))
  12676. tags)))))
  12677. (defun org-match-sparse-tree (&optional todo-only match)
  12678. "Create a sparse tree according to tags string MATCH.
  12679. MATCH can contain positive and negative selection of tags, like
  12680. \"+WORK+URGENT-WITHBOSS\".
  12681. If optional argument TODO-ONLY is non-nil, only select lines that are
  12682. also TODO lines."
  12683. (interactive "P")
  12684. (org-agenda-prepare-buffers (list (current-buffer)))
  12685. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12686. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12687. (defvar org-cached-props nil)
  12688. (defun org-cached-entry-get (pom property)
  12689. (if (or (eq t org-use-property-inheritance)
  12690. (and (stringp org-use-property-inheritance)
  12691. (string-match org-use-property-inheritance property))
  12692. (and (listp org-use-property-inheritance)
  12693. (member property org-use-property-inheritance)))
  12694. ;; Caching is not possible, check it directly
  12695. (org-entry-get pom property 'inherit)
  12696. ;; Get all properties, so that we can do complicated checks easily
  12697. (cdr (assoc property (or org-cached-props
  12698. (setq org-cached-props
  12699. (org-entry-properties pom)))))))
  12700. (defun org-global-tags-completion-table (&optional files)
  12701. "Return the list of all tags in all agenda buffer/files.
  12702. Optional FILES argument is a list of files which can be used
  12703. instead of the agenda files."
  12704. (save-excursion
  12705. (org-uniquify
  12706. (delq nil
  12707. (apply 'append
  12708. (mapcar
  12709. (lambda (file)
  12710. (set-buffer (find-file-noselect file))
  12711. (append (org-get-buffer-tags)
  12712. (mapcar (lambda (x) (if (stringp (car-safe x))
  12713. (list (car-safe x)) nil))
  12714. org-tag-alist)))
  12715. (if (and files (car files))
  12716. files
  12717. (org-agenda-files))))))))
  12718. (defun org-make-tags-matcher (match)
  12719. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12720. The variable `todo-only' is scoped dynamically into this function.
  12721. It will be set to t if the matcher restricts matching to TODO entries,
  12722. otherwise will not be touched.
  12723. Returns a cons of the selection string MATCH and the constructed
  12724. lisp form implementing the matcher. The matcher is to be evaluated
  12725. at an Org entry, with point on the headline, and returns t if the
  12726. entry matches the selection string MATCH. The returned lisp form
  12727. references two variables with information about the entry, which
  12728. must be bound around the form's evaluation: todo, the TODO keyword
  12729. at the entry (or nil of none); and tags-list, the list of all tags
  12730. at the entry including inherited ones. Additionally, the category
  12731. of the entry (if any) must be specified as the text property
  12732. 'org-category on the headline.
  12733. See also `org-scan-tags'.
  12734. "
  12735. (declare (special todo-only))
  12736. (unless (boundp 'todo-only)
  12737. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12738. (unless match
  12739. ;; Get a new match request, with completion against the global
  12740. ;; tags table and the local tags in current buffer
  12741. (let ((org-last-tags-completion-table
  12742. (org-uniquify
  12743. (delq nil (append (org-get-buffer-tags)
  12744. (org-global-tags-completion-table))))))
  12745. (setq match (org-completing-read-no-i
  12746. "Match: " 'org-tags-completion-function nil nil nil
  12747. 'org-tags-history))))
  12748. ;; Parse the string and create a lisp form
  12749. (let ((match0 match)
  12750. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12751. minus tag mm
  12752. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12753. orterms term orlist re-p str-p level-p level-op time-p
  12754. prop-p pn pv po gv rest)
  12755. ;; Expand group tags
  12756. (setq match (org-tags-expand match))
  12757. (if (string-match "/+" match)
  12758. ;; match contains also a todo-matching request
  12759. (progn
  12760. (setq tagsmatch (substring match 0 (match-beginning 0))
  12761. todomatch (substring match (match-end 0)))
  12762. (if (string-match "^!" todomatch)
  12763. (setq todo-only t todomatch (substring todomatch 1)))
  12764. (if (string-match "^\\s-*$" todomatch)
  12765. (setq todomatch nil)))
  12766. ;; only matching tags
  12767. (setq tagsmatch match todomatch nil))
  12768. ;; Make the tags matcher
  12769. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12770. (setq tagsmatcher t)
  12771. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12772. (while (setq term (pop orterms))
  12773. (while (and (equal (substring term -1) "\\") orterms)
  12774. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12775. (while (string-match re term)
  12776. (setq rest (substring term (match-end 0))
  12777. minus (and (match-end 1)
  12778. (equal (match-string 1 term) "-"))
  12779. tag (save-match-data (replace-regexp-in-string
  12780. "\\\\-" "-"
  12781. (match-string 2 term)))
  12782. re-p (equal (string-to-char tag) ?{)
  12783. level-p (match-end 4)
  12784. prop-p (match-end 5)
  12785. mm (cond
  12786. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12787. (level-p
  12788. (setq level-op (org-op-to-function (match-string 3 term)))
  12789. `(,level-op level ,(string-to-number
  12790. (match-string 4 term))))
  12791. (prop-p
  12792. (setq pn (match-string 5 term)
  12793. po (match-string 6 term)
  12794. pv (match-string 7 term)
  12795. re-p (equal (string-to-char pv) ?{)
  12796. str-p (equal (string-to-char pv) ?\")
  12797. time-p (save-match-data
  12798. (string-match "^\"[[<].*[]>]\"$" pv))
  12799. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12800. (if time-p (setq pv (org-matcher-time pv)))
  12801. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12802. (cond
  12803. ((equal pn "CATEGORY")
  12804. (setq gv '(get-text-property (point) 'org-category)))
  12805. ((equal pn "TODO")
  12806. (setq gv 'todo))
  12807. (t
  12808. (setq gv `(org-cached-entry-get nil ,pn))))
  12809. (if re-p
  12810. (if (eq po 'org<>)
  12811. `(not (string-match ,pv (or ,gv "")))
  12812. `(string-match ,pv (or ,gv "")))
  12813. (if str-p
  12814. `(,po (or ,gv "") ,pv)
  12815. `(,po (string-to-number (or ,gv ""))
  12816. ,(string-to-number pv) ))))
  12817. (t `(member ,tag tags-list)))
  12818. mm (if minus (list 'not mm) mm)
  12819. term rest)
  12820. (push mm tagsmatcher))
  12821. (push (if (> (length tagsmatcher) 1)
  12822. (cons 'and tagsmatcher)
  12823. (car tagsmatcher))
  12824. orlist)
  12825. (setq tagsmatcher nil))
  12826. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12827. (setq tagsmatcher
  12828. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12829. ;; Make the todo matcher
  12830. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12831. (setq todomatcher t)
  12832. (setq orterms (org-split-string todomatch "|") orlist nil)
  12833. (while (setq term (pop orterms))
  12834. (while (string-match re term)
  12835. (setq minus (and (match-end 1)
  12836. (equal (match-string 1 term) "-"))
  12837. kwd (match-string 2 term)
  12838. re-p (equal (string-to-char kwd) ?{)
  12839. term (substring term (match-end 0))
  12840. mm (if re-p
  12841. `(string-match ,(substring kwd 1 -1) todo)
  12842. (list 'equal 'todo kwd))
  12843. mm (if minus (list 'not mm) mm))
  12844. (push mm todomatcher))
  12845. (push (if (> (length todomatcher) 1)
  12846. (cons 'and todomatcher)
  12847. (car todomatcher))
  12848. orlist)
  12849. (setq todomatcher nil))
  12850. (setq todomatcher (if (> (length orlist) 1)
  12851. (cons 'or orlist) (car orlist))))
  12852. ;; Return the string and lisp forms of the matcher
  12853. (setq matcher (if todomatcher
  12854. (list 'and tagsmatcher todomatcher)
  12855. tagsmatcher))
  12856. (when todo-only
  12857. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12858. matcher)))
  12859. (cons match0 matcher)))
  12860. (defun org-tags-expand (match &optional single-as-list downcased)
  12861. "Expand group tags in MATCH.
  12862. This replaces every group tag in MATCH with a regexp tag search.
  12863. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12864. will be replaced like this:
  12865. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12866. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12867. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12868. Replacing by a regexp preserves the structure of the match.
  12869. E.g., this expansion
  12870. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12871. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12872. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12873. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12874. assumed to be a single group tag, and the function will return
  12875. the list of tags in this group.
  12876. When DOWNCASE is non-nil, expand downcased TAGS."
  12877. (if org-group-tags
  12878. (let* ((case-fold-search t)
  12879. (stable org-mode-syntax-table)
  12880. (tal (or org-tag-groups-alist-for-agenda
  12881. org-tag-groups-alist))
  12882. (tal (if downcased
  12883. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12884. (tml (mapcar 'car tal))
  12885. (rtnmatch match) rpl)
  12886. ;; @ and _ are allowed as word-components in tags
  12887. (modify-syntax-entry ?@ "w" stable)
  12888. (modify-syntax-entry ?_ "w" stable)
  12889. (while (and tml
  12890. (string-match
  12891. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12892. (regexp-opt tml) "\\>\\)") rtnmatch))
  12893. (let* ((dir (match-string 1 rtnmatch))
  12894. (tag (match-string 2 rtnmatch))
  12895. (tag (if downcased (downcase tag) tag)))
  12896. (setq tml (delete tag tml))
  12897. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12898. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12899. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12900. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12901. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12902. (if single-as-list
  12903. (or (reverse rpl) (list rtnmatch))
  12904. rtnmatch))
  12905. (if single-as-list (list (if downcased (downcase match) match))
  12906. match)))
  12907. (defun org-op-to-function (op &optional stringp)
  12908. "Turn an operator into the appropriate function."
  12909. (setq op
  12910. (cond
  12911. ((equal op "<" ) '(< string< org-time<))
  12912. ((equal op ">" ) '(> org-string> org-time>))
  12913. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12914. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12915. ((member op '("=" "==")) '(= string= org-time=))
  12916. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12917. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12918. (defun org<> (a b) (not (= a b)))
  12919. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12920. (defun org-string>= (a b) (not (string< a b)))
  12921. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12922. (defun org-string<> (a b) (not (string= a b)))
  12923. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12924. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12925. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12926. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12927. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12928. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12929. (defun org-2ft (s)
  12930. "Convert S to a floating point time.
  12931. If S is already a number, just return it. If it is a string, parse
  12932. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12933. (cond
  12934. ((numberp s) s)
  12935. ((stringp s)
  12936. (condition-case nil
  12937. (float-time (apply 'encode-time (org-parse-time-string s)))
  12938. (error 0.)))
  12939. (t 0.)))
  12940. (defun org-time-today ()
  12941. "Time in seconds today at 0:00.
  12942. Returns the float number of seconds since the beginning of the
  12943. epoch to the beginning of today (00:00)."
  12944. (float-time (apply 'encode-time
  12945. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12946. (defun org-matcher-time (s)
  12947. "Interpret a time comparison value."
  12948. (save-match-data
  12949. (cond
  12950. ((string= s "<now>") (float-time))
  12951. ((string= s "<today>") (org-time-today))
  12952. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12953. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12954. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12955. (+ (org-time-today)
  12956. (* (string-to-number (match-string 1 s))
  12957. (cdr (assoc (match-string 2 s)
  12958. '(("d" . 86400.0) ("w" . 604800.0)
  12959. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12960. (t (org-2ft s)))))
  12961. (defun org-match-any-p (re list)
  12962. "Does re match any element of list?"
  12963. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12964. (delq nil list))
  12965. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12966. (defvar org-tags-overlay (make-overlay 1 1))
  12967. (org-detach-overlay org-tags-overlay)
  12968. (defun org-get-local-tags-at (&optional pos)
  12969. "Get a list of tags defined in the current headline."
  12970. (org-get-tags-at pos 'local))
  12971. (defun org-get-local-tags ()
  12972. "Get a list of tags defined in the current headline."
  12973. (org-get-tags-at nil 'local))
  12974. (defun org-get-tags-at (&optional pos local)
  12975. "Get a list of all headline tags applicable at POS.
  12976. POS defaults to point. If tags are inherited, the list contains
  12977. the targets in the same sequence as the headlines appear, i.e.
  12978. the tags of the current headline come last.
  12979. When LOCAL is non-nil, only return tags from the current headline,
  12980. ignore inherited ones."
  12981. (interactive)
  12982. (if (and org-trust-scanner-tags
  12983. (or (not pos) (equal pos (point)))
  12984. (not local))
  12985. org-scanner-tags
  12986. (let (tags ltags lastpos parent)
  12987. (save-excursion
  12988. (save-restriction
  12989. (widen)
  12990. (goto-char (or pos (point)))
  12991. (save-match-data
  12992. (catch 'done
  12993. (condition-case nil
  12994. (progn
  12995. (org-back-to-heading t)
  12996. (while (not (equal lastpos (point)))
  12997. (setq lastpos (point))
  12998. (when (looking-at
  12999. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13000. (setq ltags (org-split-string
  13001. (org-match-string-no-properties 1) ":"))
  13002. (when parent
  13003. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13004. (setq tags (append
  13005. (if parent
  13006. (org-remove-uninherited-tags ltags)
  13007. ltags)
  13008. tags)))
  13009. (or org-use-tag-inheritance (throw 'done t))
  13010. (if local (throw 'done t))
  13011. (or (org-up-heading-safe) (error nil))
  13012. (setq parent t)))
  13013. (error nil)))))
  13014. (if local
  13015. tags
  13016. (reverse (delete-dups
  13017. (reverse (append
  13018. (org-remove-uninherited-tags
  13019. org-file-tags) tags)))))))))
  13020. (defun org-add-prop-inherited (s)
  13021. (add-text-properties 0 (length s) '(inherited t) s)
  13022. s)
  13023. (defun org-toggle-tag (tag &optional onoff)
  13024. "Toggle the tag TAG for the current line.
  13025. If ONOFF is `on' or `off', don't toggle but set to this state."
  13026. (let (res current)
  13027. (save-excursion
  13028. (org-back-to-heading t)
  13029. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13030. (point-at-eol) t)
  13031. (progn
  13032. (setq current (match-string 1))
  13033. (replace-match ""))
  13034. (setq current ""))
  13035. (setq current (nreverse (org-split-string current ":")))
  13036. (cond
  13037. ((eq onoff 'on)
  13038. (setq res t)
  13039. (or (member tag current) (push tag current)))
  13040. ((eq onoff 'off)
  13041. (or (not (member tag current)) (setq current (delete tag current))))
  13042. (t (if (member tag current)
  13043. (setq current (delete tag current))
  13044. (setq res t)
  13045. (push tag current))))
  13046. (end-of-line 1)
  13047. (if current
  13048. (progn
  13049. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13050. (org-set-tags nil t))
  13051. (delete-horizontal-space))
  13052. (run-hooks 'org-after-tags-change-hook))
  13053. res))
  13054. (defun org-align-tags-here (to-col)
  13055. ;; Assumes that this is a headline
  13056. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13057. (beginning-of-line 1)
  13058. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13059. (< pos (match-beginning 2)))
  13060. (progn
  13061. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13062. (goto-char (match-beginning 1))
  13063. (insert " ")
  13064. (delete-region (point) (1+ (match-beginning 2)))
  13065. (setq ncol (max (current-column)
  13066. (1+ col)
  13067. (if (> to-col 0)
  13068. to-col
  13069. (- (abs to-col) tags-l))))
  13070. (setq p (point))
  13071. (insert (make-string (- ncol (current-column)) ?\ ))
  13072. (setq ncol (current-column))
  13073. (when indent-tabs-mode (tabify p (point-at-eol)))
  13074. (org-move-to-column (min ncol col) t))
  13075. (goto-char pos))))
  13076. (defun org-set-tags-command (&optional arg just-align)
  13077. "Call the set-tags command for the current entry."
  13078. (interactive "P")
  13079. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13080. (org-set-tags arg just-align)
  13081. (save-excursion
  13082. (unless (and (org-region-active-p)
  13083. org-loop-over-headlines-in-active-region)
  13084. (org-back-to-heading t))
  13085. (org-set-tags arg just-align))))
  13086. (defun org-set-tags-to (data)
  13087. "Set the tags of the current entry to DATA, replacing the current tags.
  13088. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13089. If DATA is nil or the empty string, any tags will be removed."
  13090. (interactive "sTags: ")
  13091. (setq data
  13092. (cond
  13093. ((eq data nil) "")
  13094. ((equal data "") "")
  13095. ((stringp data)
  13096. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13097. ":"))
  13098. ((listp data)
  13099. (concat ":" (mapconcat 'identity data ":") ":"))))
  13100. (when data
  13101. (save-excursion
  13102. (org-back-to-heading t)
  13103. (when (looking-at org-complex-heading-regexp)
  13104. (if (match-end 5)
  13105. (progn
  13106. (goto-char (match-beginning 5))
  13107. (insert data)
  13108. (delete-region (point) (point-at-eol))
  13109. (org-set-tags nil 'align))
  13110. (goto-char (point-at-eol))
  13111. (insert " " data)
  13112. (org-set-tags nil 'align)))
  13113. (beginning-of-line 1)
  13114. (if (looking-at ".*?\\([ \t]+\\)$")
  13115. (delete-region (match-beginning 1) (match-end 1))))))
  13116. (defun org-align-all-tags ()
  13117. "Align the tags i all headings."
  13118. (interactive)
  13119. (save-excursion
  13120. (or (ignore-errors (org-back-to-heading t))
  13121. (outline-next-heading))
  13122. (if (org-at-heading-p)
  13123. (org-set-tags t)
  13124. (message "No headings"))))
  13125. (defvar org-indent-indentation-per-level)
  13126. (defun org-set-tags (&optional arg just-align)
  13127. "Set the tags for the current headline.
  13128. With prefix ARG, realign all tags in headings in the current buffer."
  13129. (interactive "P")
  13130. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13131. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13132. 'region-start-level 'region))
  13133. org-loop-over-headlines-in-active-region)
  13134. (org-map-entries
  13135. ;; We don't use ARG and JUST-ALIGN here these args are not
  13136. ;; useful when looping over headlines
  13137. `(org-set-tags)
  13138. org-loop-over-headlines-in-active-region
  13139. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13140. (let* ((re org-outline-regexp-bol)
  13141. (current (unless arg (org-get-tags-string)))
  13142. (col (current-column))
  13143. (org-setting-tags t)
  13144. table current-tags inherited-tags ; computed below when needed
  13145. tags p0 c0 c1 rpl di tc level)
  13146. (if arg
  13147. (save-excursion
  13148. (goto-char (point-min))
  13149. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13150. (while (re-search-forward re nil t)
  13151. (org-set-tags nil t)
  13152. (end-of-line 1)))
  13153. (message "All tags realigned to column %d" org-tags-column))
  13154. (if just-align
  13155. (setq tags current)
  13156. ;; Get a new set of tags from the user
  13157. (save-excursion
  13158. (setq table (append org-tag-persistent-alist
  13159. (or org-tag-alist (org-get-buffer-tags))
  13160. (and
  13161. org-complete-tags-always-offer-all-agenda-tags
  13162. (org-global-tags-completion-table
  13163. (org-agenda-files))))
  13164. org-last-tags-completion-table table
  13165. current-tags (org-split-string current ":")
  13166. inherited-tags (nreverse
  13167. (nthcdr (length current-tags)
  13168. (nreverse (org-get-tags-at))))
  13169. tags
  13170. (if (or (eq t org-use-fast-tag-selection)
  13171. (and org-use-fast-tag-selection
  13172. (delq nil (mapcar 'cdr table))))
  13173. (org-fast-tag-selection
  13174. current-tags inherited-tags table
  13175. (if org-fast-tag-selection-include-todo
  13176. org-todo-key-alist))
  13177. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13178. (org-trim
  13179. (org-icompleting-read "Tags: "
  13180. 'org-tags-completion-function
  13181. nil nil current 'org-tags-history))))))
  13182. (while (string-match "[-+&]+" tags)
  13183. ;; No boolean logic, just a list
  13184. (setq tags (replace-match ":" t t tags))))
  13185. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13186. (if org-tags-sort-function
  13187. (setq tags (mapconcat 'identity
  13188. (sort (org-split-string
  13189. tags (org-re "[^[:alnum:]_@#%]+"))
  13190. org-tags-sort-function) ":")))
  13191. (if (string-match "\\`[\t ]*\\'" tags)
  13192. (setq tags "")
  13193. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13194. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13195. ;; Insert new tags at the correct column
  13196. (beginning-of-line 1)
  13197. (setq level (or (and (looking-at org-outline-regexp)
  13198. (- (match-end 0) (point) 1))
  13199. 1))
  13200. (cond
  13201. ((and (equal current "") (equal tags "")))
  13202. ((re-search-forward
  13203. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13204. (point-at-eol) t)
  13205. (if (equal tags "")
  13206. (setq rpl "")
  13207. (goto-char (match-beginning 0))
  13208. (setq c0 (current-column)
  13209. ;; compute offset for the case of org-indent-mode active
  13210. di (if org-indent-mode
  13211. (* (1- org-indent-indentation-per-level) (1- level))
  13212. 0)
  13213. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13214. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13215. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13216. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13217. (replace-match rpl t t)
  13218. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13219. tags)
  13220. (t (error "Tags alignment failed")))
  13221. (org-move-to-column col)
  13222. (unless just-align
  13223. (run-hooks 'org-after-tags-change-hook))))))
  13224. (defun org-change-tag-in-region (beg end tag off)
  13225. "Add or remove TAG for each entry in the region.
  13226. This works in the agenda, and also in an org-mode buffer."
  13227. (interactive
  13228. (list (region-beginning) (region-end)
  13229. (let ((org-last-tags-completion-table
  13230. (if (derived-mode-p 'org-mode)
  13231. (org-uniquify
  13232. (delq nil (append (org-get-buffer-tags)
  13233. (org-global-tags-completion-table))))
  13234. (org-global-tags-completion-table))))
  13235. (org-icompleting-read
  13236. "Tag: " 'org-tags-completion-function nil nil nil
  13237. 'org-tags-history))
  13238. (progn
  13239. (message "[s]et or [r]emove? ")
  13240. (equal (read-char-exclusive) ?r))))
  13241. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13242. (let ((agendap (equal major-mode 'org-agenda-mode))
  13243. l1 l2 m buf pos newhead (cnt 0))
  13244. (goto-char end)
  13245. (setq l2 (1- (org-current-line)))
  13246. (goto-char beg)
  13247. (setq l1 (org-current-line))
  13248. (loop for l from l1 to l2 do
  13249. (org-goto-line l)
  13250. (setq m (get-text-property (point) 'org-hd-marker))
  13251. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13252. (and agendap m))
  13253. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13254. pos (if agendap m (point)))
  13255. (with-current-buffer buf
  13256. (save-excursion
  13257. (save-restriction
  13258. (goto-char pos)
  13259. (setq cnt (1+ cnt))
  13260. (org-toggle-tag tag (if off 'off 'on))
  13261. (setq newhead (org-get-heading)))))
  13262. (and agendap (org-agenda-change-all-lines newhead m))))
  13263. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13264. (defun org-tags-completion-function (string predicate &optional flag)
  13265. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13266. (confirm (lambda (x) (stringp (car x)))))
  13267. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13268. (setq s1 (match-string 1 string)
  13269. s2 (match-string 2 string))
  13270. (setq s1 "" s2 string))
  13271. (cond
  13272. ((eq flag nil)
  13273. ;; try completion
  13274. (setq rtn (try-completion s2 ctable confirm))
  13275. (if (stringp rtn)
  13276. (setq rtn
  13277. (concat s1 s2 (substring rtn (length s2))
  13278. (if (and org-add-colon-after-tag-completion
  13279. (assoc rtn ctable))
  13280. ":" ""))))
  13281. rtn)
  13282. ((eq flag t)
  13283. ;; all-completions
  13284. (all-completions s2 ctable confirm))
  13285. ((eq flag 'lambda)
  13286. ;; exact match?
  13287. (assoc s2 ctable)))))
  13288. (defun org-fast-tag-insert (kwd tags face &optional end)
  13289. "Insert KDW, and the TAGS, the latter with face FACE.
  13290. Also insert END."
  13291. (insert (format "%-12s" (concat kwd ":"))
  13292. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13293. (or end "")))
  13294. (defun org-fast-tag-show-exit (flag)
  13295. (save-excursion
  13296. (org-goto-line 3)
  13297. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13298. (replace-match ""))
  13299. (when flag
  13300. (end-of-line 1)
  13301. (org-move-to-column (- (window-width) 19) t)
  13302. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13303. (defun org-set-current-tags-overlay (current prefix)
  13304. "Add an overlay to CURRENT tag with PREFIX."
  13305. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13306. (if (featurep 'xemacs)
  13307. (org-overlay-display org-tags-overlay (concat prefix s)
  13308. 'secondary-selection)
  13309. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13310. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13311. (defvar org-last-tag-selection-key nil)
  13312. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13313. "Fast tag selection with single keys.
  13314. CURRENT is the current list of tags in the headline, INHERITED is the
  13315. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13316. possibly with grouping information. TODO-TABLE is a similar table with
  13317. TODO keywords, should these have keys assigned to them.
  13318. If the keys are nil, a-z are automatically assigned.
  13319. Returns the new tags string, or nil to not change the current settings."
  13320. (let* ((fulltable (append table todo-table))
  13321. (maxlen (apply 'max (mapcar
  13322. (lambda (x)
  13323. (if (stringp (car x)) (string-width (car x)) 0))
  13324. fulltable)))
  13325. (buf (current-buffer))
  13326. (expert (eq org-fast-tag-selection-single-key 'expert))
  13327. (buffer-tags nil)
  13328. (fwidth (+ maxlen 3 1 3))
  13329. (ncol (/ (- (window-width) 4) fwidth))
  13330. (i-face 'org-done)
  13331. (c-face 'org-todo)
  13332. tg cnt e c char c1 c2 ntable tbl rtn
  13333. ov-start ov-end ov-prefix
  13334. (exit-after-next org-fast-tag-selection-single-key)
  13335. (done-keywords org-done-keywords)
  13336. groups ingroup)
  13337. (save-excursion
  13338. (beginning-of-line 1)
  13339. (if (looking-at
  13340. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13341. (setq ov-start (match-beginning 1)
  13342. ov-end (match-end 1)
  13343. ov-prefix "")
  13344. (setq ov-start (1- (point-at-eol))
  13345. ov-end (1+ ov-start))
  13346. (skip-chars-forward "^\n\r")
  13347. (setq ov-prefix
  13348. (concat
  13349. (buffer-substring (1- (point)) (point))
  13350. (if (> (current-column) org-tags-column)
  13351. " "
  13352. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13353. (move-overlay org-tags-overlay ov-start ov-end)
  13354. (save-window-excursion
  13355. (if expert
  13356. (set-buffer (get-buffer-create " *Org tags*"))
  13357. (delete-other-windows)
  13358. (split-window-vertically)
  13359. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13360. (erase-buffer)
  13361. (org-set-local 'org-done-keywords done-keywords)
  13362. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13363. (org-fast-tag-insert "Current" current c-face "\n\n")
  13364. (org-fast-tag-show-exit exit-after-next)
  13365. (org-set-current-tags-overlay current ov-prefix)
  13366. (setq tbl fulltable char ?a cnt 0)
  13367. (while (setq e (pop tbl))
  13368. (cond
  13369. ((equal (car e) :startgroup)
  13370. (push '() groups) (setq ingroup t)
  13371. (when (not (= cnt 0))
  13372. (setq cnt 0)
  13373. (insert "\n"))
  13374. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13375. ((equal (car e) :endgroup)
  13376. (setq ingroup nil cnt 0)
  13377. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13378. ((equal e '(:newline))
  13379. (when (not (= cnt 0))
  13380. (setq cnt 0)
  13381. (insert "\n")
  13382. (setq e (car tbl))
  13383. (while (equal (car tbl) '(:newline))
  13384. (insert "\n")
  13385. (setq tbl (cdr tbl)))))
  13386. ((equal e '(:grouptags)) nil)
  13387. (t
  13388. (setq tg (copy-sequence (car e)) c2 nil)
  13389. (if (cdr e)
  13390. (setq c (cdr e))
  13391. ;; automatically assign a character.
  13392. (setq c1 (string-to-char
  13393. (downcase (substring
  13394. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13395. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13396. (while (or (rassoc char ntable) (rassoc char table))
  13397. (setq char (1+ char)))
  13398. (setq c2 c1))
  13399. (setq c (or c2 char)))
  13400. (if ingroup (push tg (car groups)))
  13401. (setq tg (org-add-props tg nil 'face
  13402. (cond
  13403. ((not (assoc tg table))
  13404. (org-get-todo-face tg))
  13405. ((member tg current) c-face)
  13406. ((member tg inherited) i-face))))
  13407. (if (equal (caar tbl) :grouptags)
  13408. (org-add-props tg nil 'face 'org-tag-group))
  13409. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13410. (insert "[" c "] " tg (make-string
  13411. (- fwidth 4 (length tg)) ?\ ))
  13412. (push (cons tg c) ntable)
  13413. (when (= (setq cnt (1+ cnt)) ncol)
  13414. (insert "\n")
  13415. (if ingroup (insert " "))
  13416. (setq cnt 0)))))
  13417. (setq ntable (nreverse ntable))
  13418. (insert "\n")
  13419. (goto-char (point-min))
  13420. (if (not expert) (org-fit-window-to-buffer))
  13421. (setq rtn
  13422. (catch 'exit
  13423. (while t
  13424. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13425. (if (not groups) "no " "")
  13426. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13427. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13428. (setq org-last-tag-selection-key c)
  13429. (cond
  13430. ((= c ?\r) (throw 'exit t))
  13431. ((= c ?!)
  13432. (setq groups (not groups))
  13433. (goto-char (point-min))
  13434. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13435. ((= c ?\C-c)
  13436. (if (not expert)
  13437. (org-fast-tag-show-exit
  13438. (setq exit-after-next (not exit-after-next)))
  13439. (setq expert nil)
  13440. (delete-other-windows)
  13441. (set-window-buffer (split-window-vertically) " *Org tags*")
  13442. (org-switch-to-buffer-other-window " *Org tags*")
  13443. (org-fit-window-to-buffer)))
  13444. ((or (= c ?\C-g)
  13445. (and (= c ?q) (not (rassoc c ntable))))
  13446. (org-detach-overlay org-tags-overlay)
  13447. (setq quit-flag t))
  13448. ((= c ?\ )
  13449. (setq current nil)
  13450. (if exit-after-next (setq exit-after-next 'now)))
  13451. ((= c ?\t)
  13452. (condition-case nil
  13453. (setq tg (org-icompleting-read
  13454. "Tag: "
  13455. (or buffer-tags
  13456. (with-current-buffer buf
  13457. (org-get-buffer-tags)))))
  13458. (quit (setq tg "")))
  13459. (when (string-match "\\S-" tg)
  13460. (add-to-list 'buffer-tags (list tg))
  13461. (if (member tg current)
  13462. (setq current (delete tg current))
  13463. (push tg current)))
  13464. (if exit-after-next (setq exit-after-next 'now)))
  13465. ((setq e (rassoc c todo-table) tg (car e))
  13466. (with-current-buffer buf
  13467. (save-excursion (org-todo tg)))
  13468. (if exit-after-next (setq exit-after-next 'now)))
  13469. ((setq e (rassoc c ntable) tg (car e))
  13470. (if (member tg current)
  13471. (setq current (delete tg current))
  13472. (loop for g in groups do
  13473. (if (member tg g)
  13474. (mapc (lambda (x)
  13475. (setq current (delete x current)))
  13476. g)))
  13477. (push tg current))
  13478. (if exit-after-next (setq exit-after-next 'now))))
  13479. ;; Create a sorted list
  13480. (setq current
  13481. (sort current
  13482. (lambda (a b)
  13483. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13484. (if (eq exit-after-next 'now) (throw 'exit t))
  13485. (goto-char (point-min))
  13486. (beginning-of-line 2)
  13487. (delete-region (point) (point-at-eol))
  13488. (org-fast-tag-insert "Current" current c-face)
  13489. (org-set-current-tags-overlay current ov-prefix)
  13490. (while (re-search-forward
  13491. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13492. (setq tg (match-string 1))
  13493. (add-text-properties
  13494. (match-beginning 1) (match-end 1)
  13495. (list 'face
  13496. (cond
  13497. ((member tg current) c-face)
  13498. ((member tg inherited) i-face)
  13499. (t (get-text-property (match-beginning 1) 'face))))))
  13500. (goto-char (point-min)))))
  13501. (org-detach-overlay org-tags-overlay)
  13502. (if rtn
  13503. (mapconcat 'identity current ":")
  13504. nil))))
  13505. (defun org-get-tags-string ()
  13506. "Get the TAGS string in the current headline."
  13507. (unless (org-at-heading-p t)
  13508. (user-error "Not on a heading"))
  13509. (save-excursion
  13510. (beginning-of-line 1)
  13511. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13512. (org-match-string-no-properties 1)
  13513. "")))
  13514. (defun org-get-tags ()
  13515. "Get the list of tags specified in the current headline."
  13516. (org-split-string (org-get-tags-string) ":"))
  13517. (defun org-get-buffer-tags ()
  13518. "Get a table of all tags used in the buffer, for completion."
  13519. (let (tags)
  13520. (save-excursion
  13521. (goto-char (point-min))
  13522. (while (re-search-forward
  13523. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13524. (when (equal (char-after (point-at-bol 0)) ?*)
  13525. (mapc (lambda (x) (add-to-list 'tags x))
  13526. (org-split-string (org-match-string-no-properties 1) ":")))))
  13527. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13528. (mapcar 'list tags)))
  13529. ;;;; The mapping API
  13530. (defun org-map-entries (func &optional match scope &rest skip)
  13531. "Call FUNC at each headline selected by MATCH in SCOPE.
  13532. FUNC is a function or a lisp form. The function will be called without
  13533. arguments, with the cursor positioned at the beginning of the headline.
  13534. The return values of all calls to the function will be collected and
  13535. returned as a list.
  13536. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13537. does not need to preserve point. After evaluation, the cursor will be
  13538. moved to the end of the line (presumably of the headline of the
  13539. processed entry) and search continues from there. Under some
  13540. circumstances, this may not produce the wanted results. For example,
  13541. if you have removed (e.g. archived) the current (sub)tree it could
  13542. mean that the next entry will be skipped entirely. In such cases, you
  13543. can specify the position from where search should continue by making
  13544. FUNC set the variable `org-map-continue-from' to the desired buffer
  13545. position.
  13546. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13547. Only headlines that are matched by this query will be considered during
  13548. the iteration. When MATCH is nil or t, all headlines will be
  13549. visited by the iteration.
  13550. SCOPE determines the scope of this command. It can be any of:
  13551. nil The current buffer, respecting the restriction if any
  13552. tree The subtree started with the entry at point
  13553. region The entries within the active region, if any
  13554. region-start-level
  13555. The entries within the active region, but only those at
  13556. the same level than the first one.
  13557. file The current buffer, without restriction
  13558. file-with-archives
  13559. The current buffer, and any archives associated with it
  13560. agenda All agenda files
  13561. agenda-with-archives
  13562. All agenda files with any archive files associated with them
  13563. \(file1 file2 ...)
  13564. If this is a list, all files in the list will be scanned
  13565. The remaining args are treated as settings for the skipping facilities of
  13566. the scanner. The following items can be given here:
  13567. archive skip trees with the archive tag
  13568. comment skip trees with the COMMENT keyword
  13569. function or Emacs Lisp form:
  13570. will be used as value for `org-agenda-skip-function', so
  13571. whenever the function returns a position, FUNC will not be
  13572. called for that entry and search will continue from the
  13573. position returned
  13574. If your function needs to retrieve the tags including inherited tags
  13575. at the *current* entry, you can use the value of the variable
  13576. `org-scanner-tags' which will be much faster than getting the value
  13577. with `org-get-tags-at'. If your function gets properties with
  13578. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13579. to t around the call to `org-entry-properties' to get the same speedup.
  13580. Note that if your function moves around to retrieve tags and properties at
  13581. a *different* entry, you cannot use these techniques."
  13582. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13583. (not (org-region-active-p)))
  13584. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13585. (org-agenda-skip-archived-trees (memq 'archive skip))
  13586. (org-agenda-skip-comment-trees (memq 'comment skip))
  13587. (org-agenda-skip-function
  13588. (car (org-delete-all '(comment archive) skip)))
  13589. (org-tags-match-list-sublevels t)
  13590. (start-level (eq scope 'region-start-level))
  13591. matcher file res
  13592. org-todo-keywords-for-agenda
  13593. org-done-keywords-for-agenda
  13594. org-todo-keyword-alist-for-agenda
  13595. org-drawers-for-agenda
  13596. org-tag-alist-for-agenda
  13597. todo-only)
  13598. (cond
  13599. ((eq match t) (setq matcher t))
  13600. ((eq match nil) (setq matcher t))
  13601. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13602. (save-window-excursion
  13603. (save-restriction
  13604. (cond ((eq scope 'tree)
  13605. (org-back-to-heading t)
  13606. (org-narrow-to-subtree)
  13607. (setq scope nil))
  13608. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13609. (org-region-active-p))
  13610. ;; If needed, set start-level to a string like "2"
  13611. (when start-level
  13612. (save-excursion
  13613. (goto-char (region-beginning))
  13614. (unless (org-at-heading-p) (outline-next-heading))
  13615. (setq start-level (org-current-level))))
  13616. (narrow-to-region (region-beginning)
  13617. (save-excursion
  13618. (goto-char (region-end))
  13619. (unless (and (bolp) (org-at-heading-p))
  13620. (outline-next-heading))
  13621. (point)))
  13622. (setq scope nil)))
  13623. (if (not scope)
  13624. (progn
  13625. (org-agenda-prepare-buffers
  13626. (list (buffer-file-name (current-buffer))))
  13627. (setq res (org-scan-tags func matcher todo-only start-level)))
  13628. ;; Get the right scope
  13629. (cond
  13630. ((and scope (listp scope) (symbolp (car scope)))
  13631. (setq scope (eval scope)))
  13632. ((eq scope 'agenda)
  13633. (setq scope (org-agenda-files t)))
  13634. ((eq scope 'agenda-with-archives)
  13635. (setq scope (org-agenda-files t))
  13636. (setq scope (org-add-archive-files scope)))
  13637. ((eq scope 'file)
  13638. (setq scope (list (buffer-file-name))))
  13639. ((eq scope 'file-with-archives)
  13640. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13641. (org-agenda-prepare-buffers scope)
  13642. (while (setq file (pop scope))
  13643. (with-current-buffer (org-find-base-buffer-visiting file)
  13644. (save-excursion
  13645. (save-restriction
  13646. (widen)
  13647. (goto-char (point-min))
  13648. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13649. res)))
  13650. ;;;; Properties
  13651. ;;; Setting and retrieving properties
  13652. (defconst org-special-properties
  13653. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13654. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13655. "The special properties valid in Org-mode.
  13656. These are properties that are not defined in the property drawer,
  13657. but in some other way.")
  13658. (defconst org-default-properties
  13659. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13660. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13661. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13662. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13663. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13664. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13665. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13666. "Some properties that are used by Org-mode for various purposes.
  13667. Being in this list makes sure that they are offered for completion.")
  13668. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13669. "Regular expression matching the first line of a property drawer.")
  13670. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13671. "Regular expression matching the last line of a property drawer.")
  13672. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13673. "Regular expression matching the first line of a property drawer.")
  13674. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13675. "Regular expression matching the first line of a property drawer.")
  13676. (defconst org-property-drawer-re
  13677. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13678. org-property-end-re "\\)\n?")
  13679. "Matches an entire property drawer.")
  13680. (defconst org-clock-drawer-re
  13681. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13682. org-property-end-re "\\)\n?")
  13683. "Matches an entire clock drawer.")
  13684. (defsubst org-re-property (property)
  13685. "Return a regexp matching a PROPERTY line.
  13686. Match group 1 will be set to the value."
  13687. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13688. (defsubst org-re-property-keyword (property)
  13689. "Return a regexp matching a PROPERTY line, possibly with no
  13690. value for the property."
  13691. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13692. (defun org-property-action ()
  13693. "Do an action on properties."
  13694. (interactive)
  13695. (let (c)
  13696. (org-at-property-p)
  13697. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13698. (setq c (read-char-exclusive))
  13699. (cond
  13700. ((equal c ?s)
  13701. (call-interactively 'org-set-property))
  13702. ((equal c ?d)
  13703. (call-interactively 'org-delete-property))
  13704. ((equal c ?D)
  13705. (call-interactively 'org-delete-property-globally))
  13706. ((equal c ?c)
  13707. (call-interactively 'org-compute-property-at-point))
  13708. (t (user-error "No such property action %c" c)))))
  13709. (defun org-inc-effort ()
  13710. "Increment the value of the effort property in the current entry."
  13711. (interactive)
  13712. (org-set-effort nil t))
  13713. (defvar org-clock-effort) ;; Defined in org-clock.el
  13714. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13715. (defun org-set-effort (&optional value increment)
  13716. "Set the effort property of the current entry.
  13717. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13718. 10th allowed value.
  13719. When INCREMENT is non-nil, set the property to the next allowed value."
  13720. (interactive "P")
  13721. (if (equal value 0) (setq value 10))
  13722. (let* ((completion-ignore-case t)
  13723. (prop org-effort-property)
  13724. (cur (org-entry-get nil prop))
  13725. (allowed (org-property-get-allowed-values nil prop 'table))
  13726. (existing (mapcar 'list (org-property-values prop)))
  13727. (heading (nth 4 (org-heading-components)))
  13728. rpl
  13729. (val (cond
  13730. ((stringp value) value)
  13731. ((and allowed (integerp value))
  13732. (or (car (nth (1- value) allowed))
  13733. (car (org-last allowed))))
  13734. ((and allowed increment)
  13735. (or (caadr (member (list cur) allowed))
  13736. (user-error "Allowed effort values are not set")))
  13737. (allowed
  13738. (message "Select 1-9,0, [RET%s]: %s"
  13739. (if cur (concat "=" cur) "")
  13740. (mapconcat 'car allowed " "))
  13741. (setq rpl (read-char-exclusive))
  13742. (if (equal rpl ?\r)
  13743. cur
  13744. (setq rpl (- rpl ?0))
  13745. (if (equal rpl 0) (setq rpl 10))
  13746. (if (and (> rpl 0) (<= rpl (length allowed)))
  13747. (car (nth (1- rpl) allowed))
  13748. (org-completing-read "Effort: " allowed nil))))
  13749. (t
  13750. (let (org-completion-use-ido org-completion-use-iswitchb)
  13751. (org-completing-read
  13752. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13753. (concat "[" cur "]") "")
  13754. ": ")
  13755. existing nil nil "" nil cur))))))
  13756. (unless (equal (org-entry-get nil prop) val)
  13757. (org-entry-put nil prop val))
  13758. (save-excursion
  13759. (org-back-to-heading t)
  13760. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13761. (when (string= heading org-clock-current-task)
  13762. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13763. (org-clock-update-mode-line))
  13764. (message "%s is now %s" prop val)))
  13765. (defun org-at-property-p ()
  13766. "Is cursor inside a property drawer?"
  13767. (save-excursion
  13768. (beginning-of-line 1)
  13769. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13770. (save-match-data ;; Used by calling procedures
  13771. (let ((p (point))
  13772. (range (unless (org-before-first-heading-p)
  13773. (org-get-property-block))))
  13774. (and range (<= (car range) p) (< p (cdr range))))))))
  13775. (defun org-get-property-block (&optional beg end force)
  13776. "Return the (beg . end) range of the body of the property drawer.
  13777. BEG and END are the beginning and end of the current subtree, or of
  13778. the part before the first headline. If they are not given, they will
  13779. be found. If the drawer does not exist and FORCE is non-nil, create
  13780. the drawer."
  13781. (catch 'exit
  13782. (save-excursion
  13783. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13784. (progn (org-back-to-heading t) (point))))
  13785. (end (or end (and (not (outline-next-heading)) (point-max))
  13786. (point))))
  13787. (goto-char beg)
  13788. (if (re-search-forward org-property-start-re end t)
  13789. (setq beg (1+ (match-end 0)))
  13790. (if force
  13791. (save-excursion
  13792. (org-insert-property-drawer)
  13793. (setq end (progn (outline-next-heading) (point))))
  13794. (throw 'exit nil))
  13795. (goto-char beg)
  13796. (if (re-search-forward org-property-start-re end t)
  13797. (setq beg (1+ (match-end 0)))))
  13798. (if (re-search-forward org-property-end-re end t)
  13799. (setq end (match-beginning 0))
  13800. (or force (throw 'exit nil))
  13801. (goto-char beg)
  13802. (setq end beg)
  13803. (org-indent-line)
  13804. (insert ":END:\n"))
  13805. (cons beg end)))))
  13806. (defun org-entry-properties (&optional pom which specific)
  13807. "Get all properties of the entry at point-or-marker POM.
  13808. This includes the TODO keyword, the tags, time strings for deadline,
  13809. scheduled, and clocking, and any additional properties defined in the
  13810. entry. The return value is an alist, keys may occur multiple times
  13811. if the property key was used several times.
  13812. POM may also be nil, in which case the current entry is used.
  13813. If WHICH is nil or `all', get all properties. If WHICH is
  13814. `special' or `standard', only get that subclass. If WHICH
  13815. is a string only get exactly this property. SPECIFIC can be a string, the
  13816. specific property we are interested in. Specifying it can speed
  13817. things up because then unnecessary parsing is avoided."
  13818. (setq which (or which 'all))
  13819. (org-with-point-at pom
  13820. (let ((clockstr (substring org-clock-string 0 -1))
  13821. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13822. (case-fold-search nil)
  13823. beg end range props sum-props key key1 value string clocksum clocksumt)
  13824. (save-excursion
  13825. (when (condition-case nil
  13826. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13827. (error nil))
  13828. (setq beg (point))
  13829. (setq sum-props (get-text-property (point) 'org-summaries))
  13830. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13831. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13832. (outline-next-heading)
  13833. (setq end (point))
  13834. (when (memq which '(all special))
  13835. ;; Get the special properties, like TODO and tags
  13836. (goto-char beg)
  13837. (when (and (or (not specific) (string= specific "TODO"))
  13838. (looking-at org-todo-line-regexp) (match-end 2))
  13839. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13840. (when (and (or (not specific) (string= specific "PRIORITY"))
  13841. (looking-at org-priority-regexp))
  13842. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13843. (when (or (not specific) (string= specific "FILE"))
  13844. (push (cons "FILE" buffer-file-name) props))
  13845. (when (and (or (not specific) (string= specific "TAGS"))
  13846. (setq value (org-get-tags-string))
  13847. (string-match "\\S-" value))
  13848. (push (cons "TAGS" value) props))
  13849. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13850. (setq value (org-get-tags-at)))
  13851. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13852. ":"))
  13853. props))
  13854. (when (or (not specific) (string= specific "BLOCKED"))
  13855. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13856. (when (or (not specific)
  13857. (member specific
  13858. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13859. "TIMESTAMP" "TIMESTAMP_IA")))
  13860. (catch 'match
  13861. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13862. (setq key (if (match-end 1)
  13863. (substring (org-match-string-no-properties 1)
  13864. 0 -1))
  13865. string (if (equal key clockstr)
  13866. (org-trim
  13867. (buffer-substring-no-properties
  13868. (match-beginning 3) (goto-char
  13869. (point-at-eol))))
  13870. (substring (org-match-string-no-properties 3)
  13871. 1 -1)))
  13872. ;; Get the correct property name from the key. This is
  13873. ;; necessary if the user has configured time keywords.
  13874. (setq key1 (concat key ":"))
  13875. (cond
  13876. ((not key)
  13877. (setq key
  13878. (if (= (char-after (match-beginning 3)) ?\[)
  13879. "TIMESTAMP_IA" "TIMESTAMP")))
  13880. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13881. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13882. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13883. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13884. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13885. (progn
  13886. (push (cons key string) props)
  13887. ;; no need to search further if match is found
  13888. (throw 'match t))
  13889. (when (or (equal key "CLOCK") (not (assoc key props)))
  13890. (push (cons key string) props)))))))
  13891. (when (memq which '(all standard))
  13892. ;; Get the standard properties, like :PROP: ...
  13893. (setq range (org-get-property-block beg end))
  13894. (when range
  13895. (goto-char (car range))
  13896. (while (re-search-forward
  13897. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13898. (cdr range) t)
  13899. (setq key (org-match-string-no-properties 1)
  13900. value (org-trim (or (org-match-string-no-properties 2) "")))
  13901. (unless (member key excluded)
  13902. (push (cons key (or value "")) props)))))
  13903. (if clocksum
  13904. (push (cons "CLOCKSUM"
  13905. (org-columns-number-to-string (/ (float clocksum) 60.)
  13906. 'add_times))
  13907. props))
  13908. (if clocksumt
  13909. (push (cons "CLOCKSUM_T"
  13910. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13911. 'add_times))
  13912. props))
  13913. (unless (assoc "CATEGORY" props)
  13914. (push (cons "CATEGORY" (org-get-category)) props))
  13915. (append sum-props (nreverse props)))))))
  13916. (defun org-entry-get (pom property &optional inherit literal-nil)
  13917. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13918. If INHERIT is non-nil and the entry does not have the property,
  13919. then also check higher levels of the hierarchy.
  13920. If INHERIT is the symbol `selective', use inheritance only if the setting
  13921. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13922. If the property is present but empty, the return value is the empty string.
  13923. If the property is not present at all, nil is returned.
  13924. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13925. do not interpret it as the list atom nil. This is used for inheritance
  13926. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13927. (org-with-point-at pom
  13928. (if (and inherit (if (eq inherit 'selective)
  13929. (org-property-inherit-p property)
  13930. t))
  13931. (org-entry-get-with-inheritance property literal-nil)
  13932. (if (member property org-special-properties)
  13933. ;; We need a special property. Use `org-entry-properties' to
  13934. ;; retrieve it, but specify the wanted property
  13935. (cdr (assoc property (org-entry-properties nil 'special property)))
  13936. (let ((range (org-get-property-block)))
  13937. (when (and range (not (eq (car range) (cdr range))))
  13938. (let* ((props (list (or (assoc property org-file-properties)
  13939. (assoc property org-global-properties)
  13940. (assoc property org-global-properties-fixed))))
  13941. (ap (lambda (key)
  13942. (when (re-search-forward
  13943. (org-re-property key) (cdr range) t)
  13944. (setq props
  13945. (org-update-property-plist
  13946. key
  13947. (if (match-end 1)
  13948. (org-match-string-no-properties 1) "")
  13949. props)))))
  13950. val)
  13951. (goto-char (car range))
  13952. (funcall ap property)
  13953. (goto-char (car range))
  13954. (while (funcall ap (concat property "+")))
  13955. (setq val (cdr (assoc property props)))
  13956. (when val (if literal-nil val (org-not-nil val))))))))))
  13957. (defun org-property-or-variable-value (var &optional inherit)
  13958. "Check if there is a property fixing the value of VAR.
  13959. If yes, return this value. If not, return the current value of the variable."
  13960. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13961. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13962. (read prop)
  13963. (symbol-value var))))
  13964. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13965. "Delete the property PROPERTY from entry at point-or-marker POM.
  13966. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13967. an empty drawer to delete."
  13968. (org-with-point-at pom
  13969. (if (member property org-special-properties)
  13970. nil ; cannot delete these properties.
  13971. (let ((range (org-get-property-block)))
  13972. (if (and range
  13973. (goto-char (car range))
  13974. (re-search-forward
  13975. (org-re-property property)
  13976. (cdr range) t))
  13977. (progn
  13978. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13979. (and delete-empty-drawer
  13980. (org-remove-empty-drawer-at
  13981. delete-empty-drawer (car range)))
  13982. t)
  13983. nil)))))
  13984. ;; Multi-values properties are properties that contain multiple values
  13985. ;; These values are assumed to be single words, separated by whitespace.
  13986. (defun org-entry-add-to-multivalued-property (pom property value)
  13987. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13988. (let* ((old (org-entry-get pom property))
  13989. (values (and old (org-split-string old "[ \t]"))))
  13990. (setq value (org-entry-protect-space value))
  13991. (unless (member value values)
  13992. (setq values (append values (list value)))
  13993. (org-entry-put pom property
  13994. (mapconcat 'identity values " ")))))
  13995. (defun org-entry-remove-from-multivalued-property (pom property value)
  13996. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13997. (let* ((old (org-entry-get pom property))
  13998. (values (and old (org-split-string old "[ \t]"))))
  13999. (setq value (org-entry-protect-space value))
  14000. (when (member value values)
  14001. (setq values (delete value values))
  14002. (org-entry-put pom property
  14003. (mapconcat 'identity values " ")))))
  14004. (defun org-entry-member-in-multivalued-property (pom property value)
  14005. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14006. (let* ((old (org-entry-get pom property))
  14007. (values (and old (org-split-string old "[ \t]"))))
  14008. (setq value (org-entry-protect-space value))
  14009. (member value values)))
  14010. (defun org-entry-get-multivalued-property (pom property)
  14011. "Return a list of values in a multivalued property."
  14012. (let* ((value (org-entry-get pom property))
  14013. (values (and value (org-split-string value "[ \t]"))))
  14014. (mapcar 'org-entry-restore-space values)))
  14015. (defun org-entry-put-multivalued-property (pom property &rest values)
  14016. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14017. VALUES should be a list of strings. Spaces will be protected."
  14018. (org-entry-put pom property
  14019. (mapconcat 'org-entry-protect-space values " "))
  14020. (let* ((value (org-entry-get pom property))
  14021. (values (and value (org-split-string value "[ \t]"))))
  14022. (mapcar 'org-entry-restore-space values)))
  14023. (defun org-entry-protect-space (s)
  14024. "Protect spaces and newline in string S."
  14025. (while (string-match " " s)
  14026. (setq s (replace-match "%20" t t s)))
  14027. (while (string-match "\n" s)
  14028. (setq s (replace-match "%0A" t t s)))
  14029. s)
  14030. (defun org-entry-restore-space (s)
  14031. "Restore spaces and newline in string S."
  14032. (while (string-match "%20" s)
  14033. (setq s (replace-match " " t t s)))
  14034. (while (string-match "%0A" s)
  14035. (setq s (replace-match "\n" t t s)))
  14036. s)
  14037. (defvar org-entry-property-inherited-from (make-marker)
  14038. "Marker pointing to the entry from where a property was inherited.
  14039. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14040. location of the entry where the inheritance search matched. If there was
  14041. no match, the marker will point nowhere.
  14042. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14043. is set.")
  14044. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14045. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14046. The search will stop at the first ancestor which has the property defined.
  14047. If the value found is \"nil\", return nil to show that the property
  14048. should be considered as undefined (this is the meaning of nil here).
  14049. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14050. (move-marker org-entry-property-inherited-from nil)
  14051. (let (tmp)
  14052. (save-excursion
  14053. (save-restriction
  14054. (widen)
  14055. (catch 'ex
  14056. (while t
  14057. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14058. (or (ignore-errors (org-back-to-heading t))
  14059. (goto-char (point-min)))
  14060. (move-marker org-entry-property-inherited-from (point))
  14061. (throw 'ex tmp))
  14062. (or (ignore-errors (org-up-heading-safe))
  14063. (throw 'ex nil))))))
  14064. (setq tmp (or tmp
  14065. (cdr (assoc property org-file-properties))
  14066. (cdr (assoc property org-global-properties))
  14067. (cdr (assoc property org-global-properties-fixed))))
  14068. (if literal-nil tmp (org-not-nil tmp))))
  14069. (defvar org-property-changed-functions nil
  14070. "Hook called when the value of a property has changed.
  14071. Each hook function should accept two arguments, the name of the property
  14072. and the new value.")
  14073. (defun org-entry-put (pom property value)
  14074. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14075. (org-with-point-at pom
  14076. (org-back-to-heading t)
  14077. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14078. range)
  14079. (cond
  14080. ((equal property "TODO")
  14081. (when (and (stringp value) (string-match "\\S-" value)
  14082. (not (member value org-todo-keywords-1)))
  14083. (user-error "\"%s\" is not a valid TODO state" value))
  14084. (if (or (not value)
  14085. (not (string-match "\\S-" value)))
  14086. (setq value 'none))
  14087. (org-todo value)
  14088. (org-set-tags nil 'align))
  14089. ((equal property "PRIORITY")
  14090. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14091. (string-to-char value) ?\ ))
  14092. (org-set-tags nil 'align))
  14093. ((equal property "CLOCKSUM")
  14094. (if (not (re-search-forward
  14095. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14096. (error "Cannot find a clock log")
  14097. (goto-char (- (match-end 1) 2))
  14098. (cond
  14099. ((eq value 'earlier) (org-timestamp-down))
  14100. ((eq value 'later) (org-timestamp-up)))
  14101. (org-clock-sum-current-item)))
  14102. ((equal property "SCHEDULED")
  14103. (if (re-search-forward org-scheduled-time-regexp end t)
  14104. (cond
  14105. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14106. ((eq value 'later) (org-timestamp-change 1 'day))
  14107. (t (call-interactively 'org-schedule)))
  14108. (call-interactively 'org-schedule)))
  14109. ((equal property "DEADLINE")
  14110. (if (re-search-forward org-deadline-time-regexp end t)
  14111. (cond
  14112. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14113. ((eq value 'later) (org-timestamp-change 1 'day))
  14114. (t (call-interactively 'org-deadline)))
  14115. (call-interactively 'org-deadline)))
  14116. ((member property org-special-properties)
  14117. (error "The %s property can not yet be set with `org-entry-put'"
  14118. property))
  14119. (t ; a non-special property
  14120. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14121. (setq range (org-get-property-block beg end 'force))
  14122. (goto-char (car range))
  14123. (if (re-search-forward
  14124. (org-re-property-keyword property) (cdr range) t)
  14125. (progn
  14126. (delete-region (match-beginning 0) (match-end 0))
  14127. (goto-char (match-beginning 0)))
  14128. (goto-char (cdr range))
  14129. (insert "\n")
  14130. (backward-char 1)
  14131. (org-indent-line))
  14132. (insert ":" property ":")
  14133. (and value (insert " " value))
  14134. (org-indent-line)))))
  14135. (run-hook-with-args 'org-property-changed-functions property value)))
  14136. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14137. "Get all property keys in the current buffer.
  14138. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14139. like tags and TODO state.
  14140. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14141. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14142. and others.
  14143. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14144. formats in the current buffer."
  14145. (let (rtn range cfmt s p)
  14146. (save-excursion
  14147. (save-restriction
  14148. (widen)
  14149. (goto-char (point-min))
  14150. (while (re-search-forward org-property-start-re nil t)
  14151. (setq range (org-get-property-block))
  14152. (goto-char (car range))
  14153. (while (re-search-forward
  14154. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14155. (cdr range) t)
  14156. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14157. (outline-next-heading))))
  14158. (when include-specials
  14159. (setq rtn (append org-special-properties rtn)))
  14160. (when include-defaults
  14161. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14162. (add-to-list 'rtn org-effort-property))
  14163. (when include-columns
  14164. (save-excursion
  14165. (save-restriction
  14166. (widen)
  14167. (goto-char (point-min))
  14168. (while (re-search-forward
  14169. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14170. nil t)
  14171. (setq cfmt (match-string 2) s 0)
  14172. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14173. cfmt s)
  14174. (setq s (match-end 0)
  14175. p (match-string 1 cfmt))
  14176. (unless (or (equal p "ITEM")
  14177. (member p org-special-properties))
  14178. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14179. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14180. (defun org-property-values (key)
  14181. "Return a list of all values of property KEY in the current buffer."
  14182. (save-excursion
  14183. (save-restriction
  14184. (widen)
  14185. (goto-char (point-min))
  14186. (let ((re (org-re-property key))
  14187. values)
  14188. (while (re-search-forward re nil t)
  14189. (add-to-list 'values (org-trim (match-string 1))))
  14190. (delete "" values)))))
  14191. (defun org-insert-property-drawer ()
  14192. "Insert a property drawer into the current entry."
  14193. (org-back-to-heading t)
  14194. (looking-at org-outline-regexp)
  14195. (let ((indent (if org-adapt-indentation
  14196. (- (match-end 0) (match-beginning 0))
  14197. 0))
  14198. (beg (point))
  14199. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14200. end hiddenp)
  14201. (outline-next-heading)
  14202. (setq end (point))
  14203. (goto-char beg)
  14204. (while (re-search-forward re end t))
  14205. (setq hiddenp (outline-invisible-p))
  14206. (end-of-line 1)
  14207. (and (equal (char-after) ?\n) (forward-char 1))
  14208. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14209. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14210. ;; just skip this line
  14211. (beginning-of-line 2)
  14212. ;; Drawer start, find the end
  14213. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14214. (beginning-of-line 1)))
  14215. (org-skip-over-state-notes)
  14216. (skip-chars-backward " \t\n\r")
  14217. (if (eq (char-before) ?*) (forward-char 1))
  14218. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14219. (beginning-of-line 0)
  14220. (org-indent-to-column indent)
  14221. (beginning-of-line 2)
  14222. (org-indent-to-column indent)
  14223. (beginning-of-line 0)
  14224. (if hiddenp
  14225. (save-excursion
  14226. (org-back-to-heading t)
  14227. (hide-entry))
  14228. (org-flag-drawer t))))
  14229. (defun org-insert-drawer (&optional arg drawer)
  14230. "Insert a drawer at point.
  14231. Optional argument DRAWER, when non-nil, is a string representing
  14232. drawer's name. Otherwise, the user is prompted for a name.
  14233. If a region is active, insert the drawer around that region
  14234. instead.
  14235. Point is left between drawer's boundaries."
  14236. (interactive "P")
  14237. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14238. "LOGBOOK"))
  14239. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14240. ;; internally by Org. They are meant to be inserted
  14241. ;; automatically.
  14242. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14243. ;; Remove system drawers from list. Note: For some reason,
  14244. ;; `org-completing-read' ignores the predicate while
  14245. ;; `completing-read' handles it fine.
  14246. (drawer (if arg "PROPERTIES"
  14247. (or drawer
  14248. (completing-read
  14249. "Drawer: " org-drawers
  14250. (lambda (d) (not (member d system-drawers))))))))
  14251. (cond
  14252. ;; With C-u, fall back on `org-insert-property-drawer'
  14253. (arg (org-insert-property-drawer))
  14254. ;; With an active region, insert a drawer at point.
  14255. ((not (org-region-active-p))
  14256. (progn
  14257. (unless (bolp) (insert "\n"))
  14258. (insert (format ":%s:\n\n:END:\n" drawer))
  14259. (forward-line -2)))
  14260. ;; Otherwise, insert the drawer at point
  14261. (t
  14262. (let ((rbeg (region-beginning))
  14263. (rend (copy-marker (region-end))))
  14264. (unwind-protect
  14265. (progn
  14266. (goto-char rbeg)
  14267. (beginning-of-line)
  14268. (when (save-excursion
  14269. (re-search-forward org-outline-regexp-bol rend t))
  14270. (user-error "Drawers cannot contain headlines"))
  14271. ;; Position point at the beginning of the first
  14272. ;; non-blank line in region. Insert drawer's opening
  14273. ;; there, then indent it.
  14274. (org-skip-whitespace)
  14275. (beginning-of-line)
  14276. (insert ":" drawer ":\n")
  14277. (forward-line -1)
  14278. (indent-for-tab-command)
  14279. ;; Move point to the beginning of the first blank line
  14280. ;; after the last non-blank line in region. Insert
  14281. ;; drawer's closing, then indent it.
  14282. (goto-char rend)
  14283. (skip-chars-backward " \r\t\n")
  14284. (insert "\n:END:")
  14285. (deactivate-mark t)
  14286. (indent-for-tab-command)
  14287. (unless (eolp) (insert "\n")))
  14288. ;; Clear marker, whatever the outcome of insertion is.
  14289. (set-marker rend nil)))))))
  14290. (defvar org-property-set-functions-alist nil
  14291. "Property set function alist.
  14292. Each entry should have the following format:
  14293. (PROPERTY . READ-FUNCTION)
  14294. The read function will be called with the same argument as
  14295. `org-completing-read'.")
  14296. (defun org-set-property-function (property)
  14297. "Get the function that should be used to set PROPERTY.
  14298. This is computed according to `org-property-set-functions-alist'."
  14299. (or (cdr (assoc property org-property-set-functions-alist))
  14300. 'org-completing-read))
  14301. (defun org-read-property-value (property)
  14302. "Read PROPERTY value from user."
  14303. (let* ((completion-ignore-case t)
  14304. (allowed (org-property-get-allowed-values nil property 'table))
  14305. (cur (org-entry-get nil property))
  14306. (prompt (concat property " value"
  14307. (if (and cur (string-match "\\S-" cur))
  14308. (concat " [" cur "]") "") ": "))
  14309. (set-function (org-set-property-function property))
  14310. (val (if allowed
  14311. (funcall set-function prompt allowed nil
  14312. (not (get-text-property 0 'org-unrestricted
  14313. (caar allowed))))
  14314. (let (org-completion-use-ido org-completion-use-iswitchb)
  14315. (funcall set-function prompt
  14316. (mapcar 'list (org-property-values property))
  14317. nil nil "" nil cur)))))
  14318. (if (equal val "")
  14319. cur
  14320. val)))
  14321. (defvar org-last-set-property nil)
  14322. (defvar org-last-set-property-value nil)
  14323. (defun org-read-property-name ()
  14324. "Read a property name."
  14325. (let* ((completion-ignore-case t)
  14326. (keys (org-buffer-property-keys nil t t))
  14327. (default-prop (or (save-excursion
  14328. (save-match-data
  14329. (beginning-of-line)
  14330. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14331. (null (string= (match-string 1) "END"))
  14332. (match-string 1))))
  14333. org-last-set-property))
  14334. (property (org-icompleting-read
  14335. (concat "Property"
  14336. (if default-prop (concat " [" default-prop "]") "")
  14337. ": ")
  14338. (mapcar 'list keys)
  14339. nil nil nil nil
  14340. default-prop)))
  14341. (if (member property keys)
  14342. property
  14343. (or (cdr (assoc (downcase property)
  14344. (mapcar (lambda (x) (cons (downcase x) x))
  14345. keys)))
  14346. property))))
  14347. (defun org-set-property-and-value (use-last)
  14348. "Allow to set [PROPERTY]: [value] direction from prompt.
  14349. When use-default, don't even ask, just use the last
  14350. \"[PROPERTY]: [value]\" string from the history."
  14351. (interactive "P")
  14352. (let* ((completion-ignore-case t)
  14353. (pv (or (and use-last org-last-set-property-value)
  14354. (org-completing-read
  14355. "Enter a \"[Property]: [value]\" pair: "
  14356. nil nil nil nil nil
  14357. org-last-set-property-value)))
  14358. prop val)
  14359. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14360. (setq prop (match-string 1 pv)
  14361. val (match-string 2 pv))
  14362. (org-set-property prop val))))
  14363. (defun org-set-property (property value)
  14364. "In the current entry, set PROPERTY to VALUE.
  14365. When called interactively, this will prompt for a property name, offering
  14366. completion on existing and default properties. And then it will prompt
  14367. for a value, offering completion either on allowed values (via an inherited
  14368. xxx_ALL property) or on existing values in other instances of this property
  14369. in the current file."
  14370. (interactive (list nil nil))
  14371. (let* ((property (or property (org-read-property-name)))
  14372. (value (or value (org-read-property-value property)))
  14373. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14374. (setq org-last-set-property property)
  14375. (setq org-last-set-property-value (concat property ": " value))
  14376. ;; Possibly postprocess the inserted value:
  14377. (when fn (setq value (funcall fn value)))
  14378. (unless (equal (org-entry-get nil property) value)
  14379. (org-entry-put nil property value))))
  14380. (defun org-delete-property (property &optional delete-empty-drawer)
  14381. "In the current entry, delete PROPERTY.
  14382. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14383. an empty drawer to delete."
  14384. (interactive
  14385. (let* ((completion-ignore-case t)
  14386. (prop (org-icompleting-read "Property: "
  14387. (org-entry-properties nil 'standard))))
  14388. (list prop)))
  14389. (message "Property %s %s" property
  14390. (if (org-entry-delete nil property delete-empty-drawer)
  14391. "deleted"
  14392. "was not present in the entry")))
  14393. (defun org-delete-property-globally (property)
  14394. "Remove PROPERTY globally, from all entries."
  14395. (interactive
  14396. (let* ((completion-ignore-case t)
  14397. (prop (org-icompleting-read
  14398. "Globally remove property: "
  14399. (mapcar 'list (org-buffer-property-keys)))))
  14400. (list prop)))
  14401. (save-excursion
  14402. (save-restriction
  14403. (widen)
  14404. (goto-char (point-min))
  14405. (let ((cnt 0))
  14406. (while (re-search-forward
  14407. (org-re-property property)
  14408. nil t)
  14409. (setq cnt (1+ cnt))
  14410. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14411. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14412. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14413. (defun org-compute-property-at-point ()
  14414. "Compute the property at point.
  14415. This looks for an enclosing column format, extracts the operator and
  14416. then applies it to the property in the column format's scope."
  14417. (interactive)
  14418. (unless (org-at-property-p)
  14419. (user-error "Not at a property"))
  14420. (let ((prop (org-match-string-no-properties 2)))
  14421. (org-columns-get-format-and-top-level)
  14422. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14423. (user-error "No operator defined for property %s" prop))
  14424. (org-columns-compute prop)))
  14425. (defvar org-property-allowed-value-functions nil
  14426. "Hook for functions supplying allowed values for a specific property.
  14427. The functions must take a single argument, the name of the property, and
  14428. return a flat list of allowed values. If \":ETC\" is one of
  14429. the values, this means that these values are intended as defaults for
  14430. completion, but that other values should be allowed too.
  14431. The functions must return nil if they are not responsible for this
  14432. property.")
  14433. (defun org-property-get-allowed-values (pom property &optional table)
  14434. "Get allowed values for the property PROPERTY.
  14435. When TABLE is non-nil, return an alist that can directly be used for
  14436. completion."
  14437. (let (vals)
  14438. (cond
  14439. ((equal property "TODO")
  14440. (setq vals (org-with-point-at pom
  14441. (append org-todo-keywords-1 '("")))))
  14442. ((equal property "PRIORITY")
  14443. (let ((n org-lowest-priority))
  14444. (while (>= n org-highest-priority)
  14445. (push (char-to-string n) vals)
  14446. (setq n (1- n)))))
  14447. ((member property org-special-properties))
  14448. ((setq vals (run-hook-with-args-until-success
  14449. 'org-property-allowed-value-functions property)))
  14450. (t
  14451. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14452. (when (and vals (string-match "\\S-" vals))
  14453. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14454. (setq vals (mapcar (lambda (x)
  14455. (cond ((stringp x) x)
  14456. ((numberp x) (number-to-string x))
  14457. ((symbolp x) (symbol-name x))
  14458. (t "???")))
  14459. vals)))))
  14460. (when (member ":ETC" vals)
  14461. (setq vals (remove ":ETC" vals))
  14462. (org-add-props (car vals) '(org-unrestricted t)))
  14463. (if table (mapcar 'list vals) vals)))
  14464. (defun org-property-previous-allowed-value (&optional previous)
  14465. "Switch to the next allowed value for this property."
  14466. (interactive)
  14467. (org-property-next-allowed-value t))
  14468. (defun org-property-next-allowed-value (&optional previous)
  14469. "Switch to the next allowed value for this property."
  14470. (interactive)
  14471. (unless (org-at-property-p)
  14472. (user-error "Not at a property"))
  14473. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14474. (key (match-string 2))
  14475. (value (match-string 3))
  14476. (allowed (or (org-property-get-allowed-values (point) key)
  14477. (and (member value '("[ ]" "[-]" "[X]"))
  14478. '("[ ]" "[X]"))))
  14479. (heading (save-match-data (nth 4 (org-heading-components))))
  14480. nval)
  14481. (unless allowed
  14482. (user-error "Allowed values for this property have not been defined"))
  14483. (if previous (setq allowed (reverse allowed)))
  14484. (if (member value allowed)
  14485. (setq nval (car (cdr (member value allowed)))))
  14486. (setq nval (or nval (car allowed)))
  14487. (if (equal nval value)
  14488. (user-error "Only one allowed value for this property"))
  14489. (org-at-property-p)
  14490. (replace-match (concat " :" key ": " nval) t t)
  14491. (org-indent-line)
  14492. (beginning-of-line 1)
  14493. (skip-chars-forward " \t")
  14494. (when (equal prop org-effort-property)
  14495. (save-excursion
  14496. (org-back-to-heading t)
  14497. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14498. (when (string= org-clock-current-task heading)
  14499. (setq org-clock-effort nval)
  14500. (org-clock-update-mode-line)))
  14501. (run-hook-with-args 'org-property-changed-functions key nval)))
  14502. (defun org-find-olp (path &optional this-buffer)
  14503. "Return a marker pointing to the entry at outline path OLP.
  14504. If anything goes wrong, throw an error.
  14505. You can wrap this call to catch the error like this:
  14506. (condition-case msg
  14507. (org-mobile-locate-entry (match-string 4))
  14508. (error (nth 1 msg)))
  14509. The return value will then be either a string with the error message,
  14510. or a marker if everything is OK.
  14511. If THIS-BUFFER is set, the outline path does not contain a file,
  14512. only headings."
  14513. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14514. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14515. (level 1)
  14516. (lmin 1)
  14517. (lmax 1)
  14518. limit re end found pos heading cnt flevel)
  14519. (unless buffer (error "File not found :%s" file))
  14520. (with-current-buffer buffer
  14521. (save-excursion
  14522. (save-restriction
  14523. (widen)
  14524. (setq limit (point-max))
  14525. (goto-char (point-min))
  14526. (while (setq heading (pop path))
  14527. (setq re (format org-complex-heading-regexp-format
  14528. (regexp-quote heading)))
  14529. (setq cnt 0 pos (point))
  14530. (while (re-search-forward re end t)
  14531. (setq level (- (match-end 1) (match-beginning 1)))
  14532. (if (and (>= level lmin) (<= level lmax))
  14533. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14534. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14535. lmax heading))
  14536. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14537. lmax heading))
  14538. (goto-char found)
  14539. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14540. (setq end (save-excursion (org-end-of-subtree t t))))
  14541. (when (org-at-heading-p)
  14542. (point-marker)))))))
  14543. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14544. "Find node HEADING in BUFFER.
  14545. Return a marker to the heading if it was found, or nil if not.
  14546. If POS-ONLY is set, return just the position instead of a marker.
  14547. The heading text must match exact, but it may have a TODO keyword,
  14548. a priority cookie and tags in the standard locations."
  14549. (with-current-buffer (or buffer (current-buffer))
  14550. (save-excursion
  14551. (save-restriction
  14552. (widen)
  14553. (goto-char (point-min))
  14554. (let (case-fold-search)
  14555. (if (re-search-forward
  14556. (format org-complex-heading-regexp-format
  14557. (regexp-quote heading)) nil t)
  14558. (if pos-only
  14559. (match-beginning 0)
  14560. (move-marker (make-marker) (match-beginning 0)))))))))
  14561. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14562. "Find Org node headline HEADING in all .org files in directory DIR.
  14563. When the target headline is found, return a marker to this location."
  14564. (let ((files (directory-files (or dir default-directory)
  14565. nil "\\`[^.#].*\\.org\\'"))
  14566. file visiting m buffer)
  14567. (catch 'found
  14568. (while (setq file (pop files))
  14569. (message "trying %s" file)
  14570. (setq visiting (org-find-base-buffer-visiting file))
  14571. (setq buffer (or visiting (find-file-noselect file)))
  14572. (setq m (org-find-exact-headline-in-buffer
  14573. heading buffer))
  14574. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14575. (and m (throw 'found m))))))
  14576. (defun org-find-entry-with-id (ident)
  14577. "Locate the entry that contains the ID property with exact value IDENT.
  14578. IDENT can be a string, a symbol or a number, this function will search for
  14579. the string representation of it.
  14580. Return the position where this entry starts, or nil if there is no such entry."
  14581. (interactive "sID: ")
  14582. (let ((id (cond
  14583. ((stringp ident) ident)
  14584. ((symbol-name ident) (symbol-name ident))
  14585. ((numberp ident) (number-to-string ident))
  14586. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14587. (case-fold-search nil))
  14588. (save-excursion
  14589. (save-restriction
  14590. (widen)
  14591. (goto-char (point-min))
  14592. (when (re-search-forward
  14593. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14594. nil t)
  14595. (org-back-to-heading t)
  14596. (point))))))
  14597. ;;;; Timestamps
  14598. (defvar org-last-changed-timestamp nil)
  14599. (defvar org-last-inserted-timestamp nil
  14600. "The last time stamp inserted with `org-insert-time-stamp'.")
  14601. (defvar org-time-was-given) ; dynamically scoped parameter
  14602. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14603. (defvar org-ts-what) ; dynamically scoped parameter
  14604. (defun org-time-stamp (arg &optional inactive)
  14605. "Prompt for a date/time and insert a time stamp.
  14606. If the user specifies a time like HH:MM or if this command is
  14607. called with at least one prefix argument, the time stamp contains
  14608. the date and the time. Otherwise, only the date is be included.
  14609. All parts of a date not specified by the user is filled in from
  14610. the current date/time. So if you just press return without
  14611. typing anything, the time stamp will represent the current
  14612. date/time.
  14613. If there is already a timestamp at the cursor, it will be
  14614. modified.
  14615. With two universal prefix arguments, insert an active timestamp
  14616. with the current time without prompting the user."
  14617. (interactive "P")
  14618. (let* ((ts nil)
  14619. (default-time
  14620. ;; Default time is either today, or, when entering a range,
  14621. ;; the range start.
  14622. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14623. (save-excursion
  14624. (re-search-backward
  14625. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14626. (- (point) 20) t)))
  14627. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14628. (current-time)))
  14629. (default-input (and ts (org-get-compact-tod ts)))
  14630. (repeater (save-excursion
  14631. (save-match-data
  14632. (beginning-of-line)
  14633. (when (re-search-forward
  14634. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14635. (save-excursion (progn (end-of-line) (point))) t)
  14636. (match-string 0)))))
  14637. org-time-was-given org-end-time-was-given time)
  14638. (cond
  14639. ((and (org-at-timestamp-p t)
  14640. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14641. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14642. (insert "--")
  14643. (setq time (let ((this-command this-command))
  14644. (org-read-date arg 'totime nil nil
  14645. default-time default-input inactive)))
  14646. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14647. ((org-at-timestamp-p t)
  14648. (setq time (let ((this-command this-command))
  14649. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14650. (when (org-at-timestamp-p t) ; just to get the match data
  14651. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14652. (replace-match "")
  14653. (setq org-last-changed-timestamp
  14654. (org-insert-time-stamp
  14655. time (or org-time-was-given arg)
  14656. inactive nil nil (list org-end-time-was-given)))
  14657. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14658. (setq org-last-changed-timestamp
  14659. (concat (substring org-last-inserted-timestamp 0 -1)
  14660. " " repeater ">"))))
  14661. (message "Timestamp updated"))
  14662. ((equal arg '(16))
  14663. (org-insert-time-stamp (current-time) t))
  14664. (t
  14665. (setq time (let ((this-command this-command))
  14666. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14667. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14668. nil nil (list org-end-time-was-given))))))
  14669. ;; FIXME: can we use this for something else, like computing time differences?
  14670. (defun org-get-compact-tod (s)
  14671. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14672. (let* ((t1 (match-string 1 s))
  14673. (h1 (string-to-number (match-string 2 s)))
  14674. (m1 (string-to-number (match-string 3 s)))
  14675. (t2 (and (match-end 4) (match-string 5 s)))
  14676. (h2 (and t2 (string-to-number (match-string 6 s))))
  14677. (m2 (and t2 (string-to-number (match-string 7 s))))
  14678. dh dm)
  14679. (if (not t2)
  14680. t1
  14681. (setq dh (- h2 h1) dm (- m2 m1))
  14682. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14683. (concat t1 "+" (number-to-string dh)
  14684. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14685. (defun org-time-stamp-inactive (&optional arg)
  14686. "Insert an inactive time stamp.
  14687. An inactive time stamp is enclosed in square brackets instead of angle
  14688. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14689. does not link to the calendar and cannot be changed with the S-cursor keys.
  14690. So these are more for recording a certain time/date."
  14691. (interactive "P")
  14692. (org-time-stamp arg 'inactive))
  14693. (defvar org-date-ovl (make-overlay 1 1))
  14694. (overlay-put org-date-ovl 'face 'org-date-selected)
  14695. (org-detach-overlay org-date-ovl)
  14696. (defvar org-ans1) ; dynamically scoped parameter
  14697. (defvar org-ans2) ; dynamically scoped parameter
  14698. (defvar org-plain-time-of-day-regexp) ; defined below
  14699. (defvar org-overriding-default-time nil) ; dynamically scoped
  14700. (defvar org-read-date-overlay nil)
  14701. (defvar org-dcst nil) ; dynamically scoped
  14702. (defvar org-read-date-history nil)
  14703. (defvar org-read-date-final-answer nil)
  14704. (defvar org-read-date-analyze-futurep nil)
  14705. (defvar org-read-date-analyze-forced-year nil)
  14706. (defvar org-read-date-inactive)
  14707. (defvar org-read-date-minibuffer-local-map
  14708. (let ((map (make-sparse-keymap)))
  14709. (set-keymap-parent map minibuffer-local-map)
  14710. (org-defkey map (kbd ".")
  14711. (lambda () (interactive)
  14712. (org-eval-in-calendar '(calendar-goto-today))))
  14713. (org-defkey map [(meta shift left)]
  14714. (lambda () (interactive)
  14715. (org-eval-in-calendar '(calendar-backward-month 1))))
  14716. (org-defkey map [(meta shift right)]
  14717. (lambda () (interactive)
  14718. (org-eval-in-calendar '(calendar-forward-month 1))))
  14719. (org-defkey map [(meta shift up)]
  14720. (lambda () (interactive)
  14721. (org-eval-in-calendar '(calendar-backward-year 1))))
  14722. (org-defkey map [(meta shift down)]
  14723. (lambda () (interactive)
  14724. (org-eval-in-calendar '(calendar-forward-year 1))))
  14725. (org-defkey map [?\e (shift left)]
  14726. (lambda () (interactive)
  14727. (org-eval-in-calendar '(calendar-backward-month 1))))
  14728. (org-defkey map [?\e (shift right)]
  14729. (lambda () (interactive)
  14730. (org-eval-in-calendar '(calendar-forward-month 1))))
  14731. (org-defkey map [?\e (shift up)]
  14732. (lambda () (interactive)
  14733. (org-eval-in-calendar '(calendar-backward-year 1))))
  14734. (org-defkey map [?\e (shift down)]
  14735. (lambda () (interactive)
  14736. (org-eval-in-calendar '(calendar-forward-year 1))))
  14737. (org-defkey map [(shift up)]
  14738. (lambda () (interactive)
  14739. (org-eval-in-calendar '(calendar-backward-week 1))))
  14740. (org-defkey map [(shift down)]
  14741. (lambda () (interactive)
  14742. (org-eval-in-calendar '(calendar-forward-week 1))))
  14743. (org-defkey map [(shift left)]
  14744. (lambda () (interactive)
  14745. (org-eval-in-calendar '(calendar-backward-day 1))))
  14746. (org-defkey map [(shift right)]
  14747. (lambda () (interactive)
  14748. (org-eval-in-calendar '(calendar-forward-day 1))))
  14749. (org-defkey map "!"
  14750. (lambda () (interactive)
  14751. (org-eval-in-calendar '(diary-view-entries))
  14752. (message "")))
  14753. (org-defkey map ">"
  14754. (lambda () (interactive)
  14755. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14756. (org-defkey map "<"
  14757. (lambda () (interactive)
  14758. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14759. (org-defkey map "\C-v"
  14760. (lambda () (interactive)
  14761. (org-eval-in-calendar
  14762. '(calendar-scroll-left-three-months 1))))
  14763. (org-defkey map "\M-v"
  14764. (lambda () (interactive)
  14765. (org-eval-in-calendar
  14766. '(calendar-scroll-right-three-months 1))))
  14767. map)
  14768. "Keymap for minibuffer commands when using `org-read-date'.")
  14769. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14770. default-time default-input inactive)
  14771. "Read a date, possibly a time, and make things smooth for the user.
  14772. The prompt will suggest to enter an ISO date, but you can also enter anything
  14773. which will at least partially be understood by `parse-time-string'.
  14774. Unrecognized parts of the date will default to the current day, month, year,
  14775. hour and minute. If this command is called to replace a timestamp at point,
  14776. or to enter the second timestamp of a range, the default time is taken
  14777. from the existing stamp. Furthermore, the command prefers the future,
  14778. so if you are giving a date where the year is not given, and the day-month
  14779. combination is already past in the current year, it will assume you
  14780. mean next year. For details, see the manual. A few examples:
  14781. 3-2-5 --> 2003-02-05
  14782. feb 15 --> currentyear-02-15
  14783. 2/15 --> currentyear-02-15
  14784. sep 12 9 --> 2009-09-12
  14785. 12:45 --> today 12:45
  14786. 22 sept 0:34 --> currentyear-09-22 0:34
  14787. 12 --> currentyear-currentmonth-12
  14788. Fri --> nearest Friday after today
  14789. -Tue --> last Tuesday
  14790. etc.
  14791. Furthermore you can specify a relative date by giving, as the *first* thing
  14792. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14793. change in days weeks, months, years.
  14794. With a single plus or minus, the date is relative to today. With a double
  14795. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14796. +4d --> four days from today
  14797. +4 --> same as above
  14798. +2w --> two weeks from today
  14799. ++5 --> five days from default date
  14800. The function understands only English month and weekday abbreviations.
  14801. While prompting, a calendar is popped up - you can also select the
  14802. date with the mouse (button 1). The calendar shows a period of three
  14803. months. To scroll it to other months, use the keys `>' and `<'.
  14804. If you don't like the calendar, turn it off with
  14805. \(setq org-read-date-popup-calendar nil)
  14806. With optional argument TO-TIME, the date will immediately be converted
  14807. to an internal time.
  14808. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14809. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14810. still enter a time, and this function will inform the calling routine
  14811. about this change. The calling routine may then choose to change the
  14812. format used to insert the time stamp into the buffer to include the time.
  14813. With optional argument FROM-STRING, read from this string instead from
  14814. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14815. the time/date that is used for everything that is not specified by the
  14816. user."
  14817. (require 'parse-time)
  14818. (let* ((org-time-stamp-rounding-minutes
  14819. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14820. (org-dcst org-display-custom-times)
  14821. (ct (org-current-time))
  14822. (org-def (or org-overriding-default-time default-time ct))
  14823. (org-defdecode (decode-time org-def))
  14824. (dummy (progn
  14825. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14826. (setcar (nthcdr 2 org-defdecode) -1)
  14827. (setcar (nthcdr 1 org-defdecode) 59)
  14828. (setq org-def (apply 'encode-time org-defdecode)
  14829. org-defdecode (decode-time org-def)))))
  14830. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14831. (calendar-frame-setup nil)
  14832. (calendar-setup nil)
  14833. (calendar-move-hook nil)
  14834. (calendar-view-diary-initially-flag nil)
  14835. (calendar-view-holidays-initially-flag nil)
  14836. (timestr (format-time-string
  14837. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14838. (prompt (concat (if prompt (concat prompt " ") "")
  14839. (format "Date+time [%s]: " timestr)))
  14840. ans (org-ans0 "") org-ans1 org-ans2 final)
  14841. (cond
  14842. (from-string (setq ans from-string))
  14843. (org-read-date-popup-calendar
  14844. (save-excursion
  14845. (save-window-excursion
  14846. (calendar)
  14847. (org-eval-in-calendar '(setq cursor-type nil) t)
  14848. (unwind-protect
  14849. (progn
  14850. (calendar-forward-day (- (time-to-days org-def)
  14851. (calendar-absolute-from-gregorian
  14852. (calendar-current-date))))
  14853. (org-eval-in-calendar nil t)
  14854. (let* ((old-map (current-local-map))
  14855. (map (copy-keymap calendar-mode-map))
  14856. (minibuffer-local-map
  14857. (copy-keymap org-read-date-minibuffer-local-map)))
  14858. (org-defkey map (kbd "RET") 'org-calendar-select)
  14859. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14860. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14861. (unwind-protect
  14862. (progn
  14863. (use-local-map map)
  14864. (setq org-read-date-inactive inactive)
  14865. (add-hook 'post-command-hook 'org-read-date-display)
  14866. (setq org-ans0 (read-string prompt default-input
  14867. 'org-read-date-history nil))
  14868. ;; org-ans0: from prompt
  14869. ;; org-ans1: from mouse click
  14870. ;; org-ans2: from calendar motion
  14871. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14872. (remove-hook 'post-command-hook 'org-read-date-display)
  14873. (use-local-map old-map)
  14874. (when org-read-date-overlay
  14875. (delete-overlay org-read-date-overlay)
  14876. (setq org-read-date-overlay nil)))))
  14877. (bury-buffer "*Calendar*")))))
  14878. (t ; Naked prompt only
  14879. (unwind-protect
  14880. (setq ans (read-string prompt default-input
  14881. 'org-read-date-history timestr))
  14882. (when org-read-date-overlay
  14883. (delete-overlay org-read-date-overlay)
  14884. (setq org-read-date-overlay nil)))))
  14885. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14886. (when org-read-date-analyze-forced-year
  14887. (message "Year was forced into %s"
  14888. (if org-read-date-force-compatible-dates
  14889. "compatible range (1970-2037)"
  14890. "range representable on this machine"))
  14891. (ding))
  14892. ;; One round trip to get rid of 34th of August and stuff like that....
  14893. (setq final (decode-time (apply 'encode-time final)))
  14894. (setq org-read-date-final-answer ans)
  14895. (if to-time
  14896. (apply 'encode-time final)
  14897. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14898. (format "%04d-%02d-%02d %02d:%02d"
  14899. (nth 5 final) (nth 4 final) (nth 3 final)
  14900. (nth 2 final) (nth 1 final))
  14901. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14902. (defvar org-def)
  14903. (defvar org-defdecode)
  14904. (defvar org-with-time)
  14905. (defun org-read-date-display ()
  14906. "Display the current date prompt interpretation in the minibuffer."
  14907. (when org-read-date-display-live
  14908. (when org-read-date-overlay
  14909. (delete-overlay org-read-date-overlay))
  14910. (when (minibufferp (current-buffer))
  14911. (save-excursion
  14912. (end-of-line 1)
  14913. (while (not (equal (buffer-substring
  14914. (max (point-min) (- (point) 4)) (point))
  14915. " "))
  14916. (insert " ")))
  14917. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14918. " " (or org-ans1 org-ans2)))
  14919. (org-end-time-was-given nil)
  14920. (f (org-read-date-analyze ans org-def org-defdecode))
  14921. (fmts (if org-dcst
  14922. org-time-stamp-custom-formats
  14923. org-time-stamp-formats))
  14924. (fmt (if (or org-with-time
  14925. (and (boundp 'org-time-was-given) org-time-was-given))
  14926. (cdr fmts)
  14927. (car fmts)))
  14928. (txt (format-time-string fmt (apply 'encode-time f)))
  14929. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14930. (txt (concat "=> " txt)))
  14931. (when (and org-end-time-was-given
  14932. (string-match org-plain-time-of-day-regexp txt))
  14933. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14934. org-end-time-was-given
  14935. (substring txt (match-end 0)))))
  14936. (when org-read-date-analyze-futurep
  14937. (setq txt (concat txt " (=>F)")))
  14938. (setq org-read-date-overlay
  14939. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14940. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14941. (defun org-read-date-analyze (ans org-def org-defdecode)
  14942. "Analyze the combined answer of the date prompt."
  14943. ;; FIXME: cleanup and comment
  14944. (let ((nowdecode (decode-time (current-time)))
  14945. delta deltan deltaw deltadef year month day
  14946. hour minute second wday pm h2 m2 tl wday1
  14947. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14948. (setq org-read-date-analyze-futurep nil
  14949. org-read-date-analyze-forced-year nil)
  14950. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14951. (setq ans "+0"))
  14952. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14953. (setq ans (replace-match "" t t ans)
  14954. deltan (car delta)
  14955. deltaw (nth 1 delta)
  14956. deltadef (nth 2 delta)))
  14957. ;; Check if there is an iso week date in there. If yes, store the
  14958. ;; info and postpone interpreting it until the rest of the parsing
  14959. ;; is done.
  14960. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14961. (setq iso-year (if (match-end 1)
  14962. (org-small-year-to-year
  14963. (string-to-number (match-string 1 ans))))
  14964. iso-weekday (if (match-end 3)
  14965. (string-to-number (match-string 3 ans)))
  14966. iso-week (string-to-number (match-string 2 ans)))
  14967. (setq ans (replace-match "" t t ans)))
  14968. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14969. (when (string-match
  14970. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14971. (setq year (if (match-end 2)
  14972. (string-to-number (match-string 2 ans))
  14973. (progn (setq kill-year t)
  14974. (string-to-number (format-time-string "%Y"))))
  14975. month (string-to-number (match-string 3 ans))
  14976. day (string-to-number (match-string 4 ans)))
  14977. (if (< year 100) (setq year (+ 2000 year)))
  14978. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14979. t nil ans)))
  14980. ;; Help matching dotted european dates
  14981. (when (string-match
  14982. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14983. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14984. (setq kill-year t)
  14985. (string-to-number (format-time-string "%Y")))
  14986. day (string-to-number (match-string 1 ans))
  14987. month (string-to-number (match-string 2 ans))
  14988. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14989. t nil ans)))
  14990. ;; Help matching american dates, like 5/30 or 5/30/7
  14991. (when (string-match
  14992. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14993. (setq year (if (match-end 4)
  14994. (string-to-number (match-string 4 ans))
  14995. (progn (setq kill-year t)
  14996. (string-to-number (format-time-string "%Y"))))
  14997. month (string-to-number (match-string 1 ans))
  14998. day (string-to-number (match-string 2 ans)))
  14999. (if (< year 100) (setq year (+ 2000 year)))
  15000. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15001. t nil ans)))
  15002. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15003. ;; If there is a time with am/pm, and *no* time without it, we convert
  15004. ;; so that matching will be successful.
  15005. (loop for i from 1 to 2 do ; twice, for end time as well
  15006. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15007. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15008. (setq hour (string-to-number (match-string 1 ans))
  15009. minute (if (match-end 3)
  15010. (string-to-number (match-string 3 ans))
  15011. 0)
  15012. pm (equal ?p
  15013. (string-to-char (downcase (match-string 4 ans)))))
  15014. (if (and (= hour 12) (not pm))
  15015. (setq hour 0)
  15016. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15017. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15018. t t ans))))
  15019. ;; Check if a time range is given as a duration
  15020. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15021. (setq hour (string-to-number (match-string 1 ans))
  15022. h2 (+ hour (string-to-number (match-string 3 ans)))
  15023. minute (string-to-number (match-string 2 ans))
  15024. m2 (+ minute (if (match-end 5) (string-to-number
  15025. (match-string 5 ans))0)))
  15026. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15027. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15028. t t ans)))
  15029. ;; Check if there is a time range
  15030. (when (boundp 'org-end-time-was-given)
  15031. (setq org-time-was-given nil)
  15032. (when (and (string-match org-plain-time-of-day-regexp ans)
  15033. (match-end 8))
  15034. (setq org-end-time-was-given (match-string 8 ans))
  15035. (setq ans (concat (substring ans 0 (match-beginning 7))
  15036. (substring ans (match-end 7))))))
  15037. (setq tl (parse-time-string ans)
  15038. day (or (nth 3 tl) (nth 3 org-defdecode))
  15039. month (or (nth 4 tl)
  15040. (if (and org-read-date-prefer-future
  15041. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15042. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15043. (nth 4 org-defdecode)))
  15044. year (or (and (not kill-year) (nth 5 tl))
  15045. (if (and org-read-date-prefer-future
  15046. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15047. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15048. (nth 5 org-defdecode)))
  15049. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15050. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15051. second (or (nth 0 tl) 0)
  15052. wday (nth 6 tl))
  15053. (when (and (eq org-read-date-prefer-future 'time)
  15054. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15055. (equal day (nth 3 nowdecode))
  15056. (equal month (nth 4 nowdecode))
  15057. (equal year (nth 5 nowdecode))
  15058. (nth 2 tl)
  15059. (or (< (nth 2 tl) (nth 2 nowdecode))
  15060. (and (= (nth 2 tl) (nth 2 nowdecode))
  15061. (nth 1 tl)
  15062. (< (nth 1 tl) (nth 1 nowdecode)))))
  15063. (setq day (1+ day)
  15064. futurep t))
  15065. ;; Special date definitions below
  15066. (cond
  15067. (iso-week
  15068. ;; There was an iso week
  15069. (require 'cal-iso)
  15070. (setq futurep nil)
  15071. (setq year (or iso-year year)
  15072. day (or iso-weekday wday 1)
  15073. wday nil ; to make sure that the trigger below does not match
  15074. iso-date (calendar-gregorian-from-absolute
  15075. (calendar-absolute-from-iso
  15076. (list iso-week day year))))
  15077. ; FIXME: Should we also push ISO weeks into the future?
  15078. ; (when (and org-read-date-prefer-future
  15079. ; (not iso-year)
  15080. ; (< (calendar-absolute-from-gregorian iso-date)
  15081. ; (time-to-days (current-time))))
  15082. ; (setq year (1+ year)
  15083. ; iso-date (calendar-gregorian-from-absolute
  15084. ; (calendar-absolute-from-iso
  15085. ; (list iso-week day year)))))
  15086. (setq month (car iso-date)
  15087. year (nth 2 iso-date)
  15088. day (nth 1 iso-date)))
  15089. (deltan
  15090. (setq futurep nil)
  15091. (unless deltadef
  15092. (let ((now (decode-time (current-time))))
  15093. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15094. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15095. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15096. ((equal deltaw "m") (setq month (+ month deltan)))
  15097. ((equal deltaw "y") (setq year (+ year deltan)))))
  15098. ((and wday (not (nth 3 tl)))
  15099. ;; Weekday was given, but no day, so pick that day in the week
  15100. ;; on or after the derived date.
  15101. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15102. (unless (equal wday wday1)
  15103. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15104. (if (and (boundp 'org-time-was-given)
  15105. (nth 2 tl))
  15106. (setq org-time-was-given t))
  15107. (if (< year 100) (setq year (+ 2000 year)))
  15108. ;; Check of the date is representable
  15109. (if org-read-date-force-compatible-dates
  15110. (progn
  15111. (if (< year 1970)
  15112. (setq year 1970 org-read-date-analyze-forced-year t))
  15113. (if (> year 2037)
  15114. (setq year 2037 org-read-date-analyze-forced-year t)))
  15115. (condition-case nil
  15116. (ignore (encode-time second minute hour day month year))
  15117. (error
  15118. (setq year (nth 5 org-defdecode))
  15119. (setq org-read-date-analyze-forced-year t))))
  15120. (setq org-read-date-analyze-futurep futurep)
  15121. (list second minute hour day month year)))
  15122. (defvar parse-time-weekdays)
  15123. (defun org-read-date-get-relative (s today default)
  15124. "Check string S for special relative date string.
  15125. TODAY and DEFAULT are internal times, for today and for a default.
  15126. Return shift list (N what def-flag)
  15127. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15128. N is the number of WHATs to shift.
  15129. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15130. the DEFAULT date rather than TODAY."
  15131. (require 'parse-time)
  15132. (when (and
  15133. (string-match
  15134. (concat
  15135. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15136. "\\([0-9]+\\)?"
  15137. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15138. "\\([ \t]\\|$\\)") s)
  15139. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15140. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15141. (string-to-char (substring (match-string 1 s) -1))
  15142. ?+))
  15143. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15144. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15145. (what (if (match-end 3) (match-string 3 s) "d"))
  15146. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15147. (date (if rel default today))
  15148. (wday (nth 6 (decode-time date)))
  15149. delta)
  15150. (if wday1
  15151. (progn
  15152. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15153. (if (= delta 0) (setq delta 7))
  15154. (if (= dir ?-)
  15155. (progn
  15156. (setq delta (- delta 7))
  15157. (if (= delta 0) (setq delta -7))))
  15158. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15159. (list delta "d" rel))
  15160. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15161. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15162. "Turn a user-specified date into the internal representation.
  15163. The internal representation needed by the calendar is (month day year).
  15164. This is a wrapper to handle the brain-dead convention in calendar that
  15165. user function argument order change dependent on argument order."
  15166. (if (boundp 'calendar-date-style)
  15167. (cond
  15168. ((eq calendar-date-style 'american)
  15169. (list arg1 arg2 arg3))
  15170. ((eq calendar-date-style 'european)
  15171. (list arg2 arg1 arg3))
  15172. ((eq calendar-date-style 'iso)
  15173. (list arg2 arg3 arg1)))
  15174. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15175. (if (org-bound-and-true-p european-calendar-style)
  15176. (list arg2 arg1 arg3)
  15177. (list arg1 arg2 arg3)))))
  15178. (defun org-eval-in-calendar (form &optional keepdate)
  15179. "Eval FORM in the calendar window and return to current window.
  15180. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15181. otherwise stick to the current value of `org-ans2'."
  15182. (let ((sf (selected-frame))
  15183. (sw (selected-window)))
  15184. (select-window (get-buffer-window "*Calendar*" t))
  15185. (eval form)
  15186. (when (and (not keepdate) (calendar-cursor-to-date))
  15187. (let* ((date (calendar-cursor-to-date))
  15188. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15189. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15190. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15191. (select-window sw)
  15192. (org-select-frame-set-input-focus sf)))
  15193. (defun org-calendar-select ()
  15194. "Return to `org-read-date' with the date currently selected.
  15195. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15196. (interactive)
  15197. (when (calendar-cursor-to-date)
  15198. (let* ((date (calendar-cursor-to-date))
  15199. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15200. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15201. (if (active-minibuffer-window) (exit-minibuffer))))
  15202. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15203. "Insert a date stamp for the date given by the internal TIME.
  15204. WITH-HM means use the stamp format that includes the time of the day.
  15205. INACTIVE means use square brackets instead of angular ones, so that the
  15206. stamp will not contribute to the agenda.
  15207. PRE and POST are optional strings to be inserted before and after the
  15208. stamp.
  15209. The command returns the inserted time stamp."
  15210. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15211. stamp)
  15212. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15213. (insert-before-markers (or pre ""))
  15214. (when (listp extra)
  15215. (setq extra (car extra))
  15216. (if (and (stringp extra)
  15217. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15218. (setq extra (format "-%02d:%02d"
  15219. (string-to-number (match-string 1 extra))
  15220. (string-to-number (match-string 2 extra))))
  15221. (setq extra nil)))
  15222. (when extra
  15223. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15224. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15225. (insert-before-markers (or post ""))
  15226. (setq org-last-inserted-timestamp stamp)))
  15227. (defun org-toggle-time-stamp-overlays ()
  15228. "Toggle the use of custom time stamp formats."
  15229. (interactive)
  15230. (setq org-display-custom-times (not org-display-custom-times))
  15231. (unless org-display-custom-times
  15232. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15233. (while (setq p (next-single-property-change p 'display))
  15234. (if (and (get-text-property p 'display)
  15235. (eq (get-text-property p 'face) 'org-date))
  15236. (remove-text-properties
  15237. p (setq p (next-single-property-change p 'display))
  15238. '(display t))))
  15239. (set-buffer-modified-p bmp)))
  15240. (if (featurep 'xemacs)
  15241. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15242. (org-restart-font-lock)
  15243. (setq org-table-may-need-update t)
  15244. (if org-display-custom-times
  15245. (message "Time stamps are overlaid with custom format")
  15246. (message "Time stamp overlays removed")))
  15247. (defun org-display-custom-time (beg end)
  15248. "Overlay modified time stamp format over timestamp between BEG and END."
  15249. (let* ((ts (buffer-substring beg end))
  15250. t1 w1 with-hm tf time str w2 (off 0))
  15251. (save-match-data
  15252. (setq t1 (org-parse-time-string ts t))
  15253. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15254. (setq off (- (match-end 0) (match-beginning 0)))))
  15255. (setq end (- end off))
  15256. (setq w1 (- end beg)
  15257. with-hm (and (nth 1 t1) (nth 2 t1))
  15258. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15259. time (org-fix-decoded-time t1)
  15260. str (org-add-props
  15261. (format-time-string
  15262. (substring tf 1 -1) (apply 'encode-time time))
  15263. nil 'mouse-face 'highlight)
  15264. w2 (length str))
  15265. (if (not (= w2 w1))
  15266. (add-text-properties (1+ beg) (+ 2 beg)
  15267. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15268. (if (featurep 'xemacs)
  15269. (progn
  15270. (put-text-property beg end 'invisible t)
  15271. (put-text-property beg end 'end-glyph (make-glyph str)))
  15272. (put-text-property beg end 'display str))))
  15273. (defun org-translate-time (string)
  15274. "Translate all timestamps in STRING to custom format.
  15275. But do this only if the variable `org-display-custom-times' is set."
  15276. (when org-display-custom-times
  15277. (save-match-data
  15278. (let* ((start 0)
  15279. (re org-ts-regexp-both)
  15280. t1 with-hm inactive tf time str beg end)
  15281. (while (setq start (string-match re string start))
  15282. (setq beg (match-beginning 0)
  15283. end (match-end 0)
  15284. t1 (save-match-data
  15285. (org-parse-time-string (substring string beg end) t))
  15286. with-hm (and (nth 1 t1) (nth 2 t1))
  15287. inactive (equal (substring string beg (1+ beg)) "[")
  15288. tf (funcall (if with-hm 'cdr 'car)
  15289. org-time-stamp-custom-formats)
  15290. time (org-fix-decoded-time t1)
  15291. str (format-time-string
  15292. (concat
  15293. (if inactive "[" "<") (substring tf 1 -1)
  15294. (if inactive "]" ">"))
  15295. (apply 'encode-time time))
  15296. string (replace-match str t t string)
  15297. start (+ start (length str)))))))
  15298. string)
  15299. (defun org-fix-decoded-time (time)
  15300. "Set 0 instead of nil for the first 6 elements of time.
  15301. Don't touch the rest."
  15302. (let ((n 0))
  15303. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15304. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15305. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15306. "Difference between TIMESTAMP-STRING and now in days.
  15307. If SECONDS is non-nil, return the difference in seconds."
  15308. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15309. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15310. (funcall fdiff (current-time)))))
  15311. (defun org-deadline-close (timestamp-string &optional ndays)
  15312. "Is the time in TIMESTAMP-STRING close to the current date?"
  15313. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15314. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15315. (not (org-entry-is-done-p))))
  15316. (defun org-get-wdays (ts &optional delay zero-delay)
  15317. "Get the deadline lead time appropriate for timestring TS.
  15318. When DELAY is non-nil, get the delay time for scheduled items
  15319. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15320. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15321. don't try to find the delay cookie in the scheduled timestamp."
  15322. (let ((tv (if delay org-scheduled-delay-days
  15323. org-deadline-warning-days)))
  15324. (cond
  15325. ((or (and delay (< tv 0))
  15326. (and delay zero-delay (<= tv 0))
  15327. (and (not delay) (<= tv 0)))
  15328. ;; Enforce this value no matter what
  15329. (- tv))
  15330. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15331. ;; lead time is specified.
  15332. (floor (* (string-to-number (match-string 1 ts))
  15333. (cdr (assoc (match-string 2 ts)
  15334. '(("d" . 1) ("w" . 7)
  15335. ("m" . 30.4) ("y" . 365.25)
  15336. ("h" . 0.041667)))))))
  15337. ;; go for the default.
  15338. (t tv))))
  15339. (defun org-calendar-select-mouse (ev)
  15340. "Return to `org-read-date' with the date currently selected.
  15341. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15342. (interactive "e")
  15343. (mouse-set-point ev)
  15344. (when (calendar-cursor-to-date)
  15345. (let* ((date (calendar-cursor-to-date))
  15346. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15347. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15348. (if (active-minibuffer-window) (exit-minibuffer))))
  15349. (defun org-check-deadlines (ndays)
  15350. "Check if there are any deadlines due or past due.
  15351. A deadline is considered due if it happens within `org-deadline-warning-days'
  15352. days from today's date. If the deadline appears in an entry marked DONE,
  15353. it is not shown. The prefix arg NDAYS can be used to test that many
  15354. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15355. (interactive "P")
  15356. (let* ((org-warn-days
  15357. (cond
  15358. ((equal ndays '(4)) 100000)
  15359. (ndays (prefix-numeric-value ndays))
  15360. (t (abs org-deadline-warning-days))))
  15361. (case-fold-search nil)
  15362. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15363. (callback
  15364. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15365. (message "%d deadlines past-due or due within %d days"
  15366. (org-occur regexp nil callback)
  15367. org-warn-days)))
  15368. (defsubst org-re-timestamp (type)
  15369. "Return a regexp for timestamp TYPE.
  15370. Allowed values for TYPE are:
  15371. all: all timestamps
  15372. active: only active timestamps (<...>)
  15373. inactive: only inactive timestamps ([...])
  15374. scheduled: only scheduled timestamps
  15375. deadline: only deadline timestamps
  15376. closed: only closed time-stamps
  15377. When TYPE is nil, fall back on returning a regexp that matches
  15378. both scheduled and deadline timestamps."
  15379. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15380. ((eq type 'active) org-ts-regexp)
  15381. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15382. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15383. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15384. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15385. ((eq type 'scheduled-or-deadline)
  15386. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15387. (defun org-check-before-date (date)
  15388. "Check if there are deadlines or scheduled entries before DATE."
  15389. (interactive (list (org-read-date)))
  15390. (let ((case-fold-search nil)
  15391. (regexp (org-re-timestamp org-ts-type))
  15392. (callback
  15393. (lambda () (time-less-p
  15394. (org-time-string-to-time (match-string 1))
  15395. (org-time-string-to-time date)))))
  15396. (message "%d entries before %s"
  15397. (org-occur regexp nil callback) date)))
  15398. (defun org-check-after-date (date)
  15399. "Check if there are deadlines or scheduled entries after DATE."
  15400. (interactive (list (org-read-date)))
  15401. (let ((case-fold-search nil)
  15402. (regexp (org-re-timestamp org-ts-type))
  15403. (callback
  15404. (lambda () (not
  15405. (time-less-p
  15406. (org-time-string-to-time (match-string 1))
  15407. (org-time-string-to-time date))))))
  15408. (message "%d entries after %s"
  15409. (org-occur regexp nil callback) date)))
  15410. (defun org-check-dates-range (start-date end-date)
  15411. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15412. (interactive (list (org-read-date nil nil nil "Range starts")
  15413. (org-read-date nil nil nil "Range end")))
  15414. (let ((case-fold-search nil)
  15415. (regexp (org-re-timestamp org-ts-type))
  15416. (callback
  15417. (lambda ()
  15418. (let ((match (match-string 1)))
  15419. (and
  15420. (not (time-less-p
  15421. (org-time-string-to-time match)
  15422. (org-time-string-to-time start-date)))
  15423. (time-less-p
  15424. (org-time-string-to-time match)
  15425. (org-time-string-to-time end-date)))))))
  15426. (message "%d entries between %s and %s"
  15427. (org-occur regexp nil callback) start-date end-date)))
  15428. (defun org-evaluate-time-range (&optional to-buffer)
  15429. "Evaluate a time range by computing the difference between start and end.
  15430. Normally the result is just printed in the echo area, but with prefix arg
  15431. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15432. If the time range is actually in a table, the result is inserted into the
  15433. next column.
  15434. For time difference computation, a year is assumed to be exactly 365
  15435. days in order to avoid rounding problems."
  15436. (interactive "P")
  15437. (or
  15438. (org-clock-update-time-maybe)
  15439. (save-excursion
  15440. (unless (org-at-date-range-p t)
  15441. (goto-char (point-at-bol))
  15442. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15443. (if (not (org-at-date-range-p t))
  15444. (user-error "Not at a time-stamp range, and none found in current line")))
  15445. (let* ((ts1 (match-string 1))
  15446. (ts2 (match-string 2))
  15447. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15448. (match-end (match-end 0))
  15449. (time1 (org-time-string-to-time ts1))
  15450. (time2 (org-time-string-to-time ts2))
  15451. (t1 (org-float-time time1))
  15452. (t2 (org-float-time time2))
  15453. (diff (abs (- t2 t1)))
  15454. (negative (< (- t2 t1) 0))
  15455. ;; (ys (floor (* 365 24 60 60)))
  15456. (ds (* 24 60 60))
  15457. (hs (* 60 60))
  15458. (fy "%dy %dd %02d:%02d")
  15459. (fy1 "%dy %dd")
  15460. (fd "%dd %02d:%02d")
  15461. (fd1 "%dd")
  15462. (fh "%02d:%02d")
  15463. y d h m align)
  15464. (if havetime
  15465. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15466. y 0
  15467. d (floor (/ diff ds)) diff (mod diff ds)
  15468. h (floor (/ diff hs)) diff (mod diff hs)
  15469. m (floor (/ diff 60)))
  15470. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15471. y 0
  15472. d (floor (+ (/ diff ds) 0.5))
  15473. h 0 m 0))
  15474. (if (not to-buffer)
  15475. (message "%s" (org-make-tdiff-string y d h m))
  15476. (if (org-at-table-p)
  15477. (progn
  15478. (goto-char match-end)
  15479. (setq align t)
  15480. (and (looking-at " *|") (goto-char (match-end 0))))
  15481. (goto-char match-end))
  15482. (if (looking-at
  15483. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15484. (replace-match ""))
  15485. (if negative (insert " -"))
  15486. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15487. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15488. (insert " " (format fh h m))))
  15489. (if align (org-table-align))
  15490. (message "Time difference inserted")))))
  15491. (defun org-make-tdiff-string (y d h m)
  15492. (let ((fmt "")
  15493. (l nil))
  15494. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15495. l (push y l)))
  15496. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15497. l (push d l)))
  15498. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15499. l (push h l)))
  15500. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15501. l (push m l)))
  15502. (apply 'format fmt (nreverse l))))
  15503. (defun org-time-string-to-time (s &optional buffer pos)
  15504. "Convert a timestamp string into internal time."
  15505. (condition-case errdata
  15506. (apply 'encode-time (org-parse-time-string s))
  15507. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15508. s (if (not (and buffer pos))
  15509. ""
  15510. (format " at %d in buffer `%s'" pos buffer))
  15511. (cdr errdata)))))
  15512. (defun org-time-string-to-seconds (s)
  15513. "Convert a timestamp string to a number of seconds."
  15514. (org-float-time (org-time-string-to-time s)))
  15515. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15516. "Convert a time stamp to an absolute day number.
  15517. If there is a specifier for a cyclic time stamp, get the closest
  15518. date to DAYNR.
  15519. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15520. The variable `date' is bound by the calendar when this is called."
  15521. (cond
  15522. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15523. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15524. daynr
  15525. (+ daynr 1000)))
  15526. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15527. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15528. (time-to-days (current-time))) (match-string 0 s)
  15529. prefer show-all))
  15530. (t (time-to-days
  15531. (condition-case errdata
  15532. (apply 'encode-time (org-parse-time-string s))
  15533. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15534. s (if (not (and buffer pos))
  15535. ""
  15536. (format " at %d in buffer `%s'" pos buffer))
  15537. (cdr errdata))))))))
  15538. (defun org-days-to-iso-week (days)
  15539. "Return the iso week number."
  15540. (require 'cal-iso)
  15541. (car (calendar-iso-from-absolute days)))
  15542. (defun org-small-year-to-year (year)
  15543. "Convert 2-digit years into 4-digit years.
  15544. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15545. The year 2000 cannot be abbreviated. Any year larger than 99
  15546. is returned unchanged."
  15547. (if (< year 38)
  15548. (setq year (+ 2000 year))
  15549. (if (< year 100)
  15550. (setq year (+ 1900 year))))
  15551. year)
  15552. (defun org-time-from-absolute (d)
  15553. "Return the time corresponding to date D.
  15554. D may be an absolute day number, or a calendar-type list (month day year)."
  15555. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15556. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15557. (defun org-calendar-holiday ()
  15558. "List of holidays, for Diary display in Org-mode."
  15559. (require 'holidays)
  15560. (let ((hl (funcall
  15561. (if (fboundp 'calendar-check-holidays)
  15562. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15563. (if hl (mapconcat 'identity hl "; "))))
  15564. (defun org-diary-sexp-entry (sexp entry date)
  15565. "Process a SEXP diary ENTRY for DATE."
  15566. (require 'diary-lib)
  15567. (let ((result (if calendar-debug-sexp
  15568. (let ((stack-trace-on-error t))
  15569. (eval (car (read-from-string sexp))))
  15570. (condition-case nil
  15571. (eval (car (read-from-string sexp)))
  15572. (error
  15573. (beep)
  15574. (message "Bad sexp at line %d in %s: %s"
  15575. (org-current-line)
  15576. (buffer-file-name) sexp)
  15577. (sleep-for 2))))))
  15578. (cond ((stringp result) (split-string result "; "))
  15579. ((and (consp result)
  15580. (not (consp (cdr result)))
  15581. (stringp (cdr result))) (cdr result))
  15582. ((and (consp result)
  15583. (stringp (car result))) result)
  15584. (result entry))))
  15585. (defun org-diary-to-ical-string (frombuf)
  15586. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15587. This uses the icalendar.el library."
  15588. (let* ((tmpdir (if (featurep 'xemacs)
  15589. (temp-directory)
  15590. temporary-file-directory))
  15591. (tmpfile (make-temp-name
  15592. (expand-file-name "orgics" tmpdir)))
  15593. buf rtn b e)
  15594. (with-current-buffer frombuf
  15595. (icalendar-export-region (point-min) (point-max) tmpfile)
  15596. (setq buf (find-buffer-visiting tmpfile))
  15597. (set-buffer buf)
  15598. (goto-char (point-min))
  15599. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15600. (setq b (match-beginning 0)))
  15601. (goto-char (point-max))
  15602. (if (re-search-backward "^END:VEVENT" nil t)
  15603. (setq e (match-end 0)))
  15604. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15605. (kill-buffer buf)
  15606. (delete-file tmpfile)
  15607. rtn))
  15608. (defun org-closest-date (start current change prefer show-all)
  15609. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15610. When PREFER is `past', return a date that is either CURRENT or past.
  15611. When PREFER is `future', return a date that is either CURRENT or future.
  15612. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15613. ;; Make the proper lists from the dates
  15614. (catch 'exit
  15615. (let ((a1 '(("h" . hour)
  15616. ("d" . day)
  15617. ("w" . week)
  15618. ("m" . month)
  15619. ("y" . year)))
  15620. (shour (nth 2 (org-parse-time-string start)))
  15621. dn dw sday cday n1 n2 n0
  15622. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15623. (setq start (org-date-to-gregorian start)
  15624. current (org-date-to-gregorian
  15625. (if show-all
  15626. current
  15627. (time-to-days (current-time))))
  15628. sday (calendar-absolute-from-gregorian start)
  15629. cday (calendar-absolute-from-gregorian current))
  15630. (if (<= cday sday) (throw 'exit sday))
  15631. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15632. (setq dn (string-to-number (match-string 1 change))
  15633. dw (cdr (assoc (match-string 2 change) a1)))
  15634. (user-error "Invalid change specifier: %s" change))
  15635. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15636. (cond
  15637. ((eq dw 'hour)
  15638. (let ((missing-hours
  15639. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15640. dn)))
  15641. (setq n1 (if (zerop missing-hours) cday
  15642. (- cday (1+ (floor (/ missing-hours 24)))))
  15643. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15644. ((eq dw 'day)
  15645. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15646. n2 (+ n1 dn)))
  15647. ((eq dw 'year)
  15648. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15649. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15650. (setq date1 (list m d y1)
  15651. n1 (calendar-absolute-from-gregorian date1)
  15652. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15653. n2 (calendar-absolute-from-gregorian date2)))
  15654. ((eq dw 'month)
  15655. ;; approx number of month between the two dates
  15656. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15657. ;; How often does dn fit in there?
  15658. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15659. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15660. m (+ m nm)
  15661. ny (floor (/ m 12))
  15662. y (+ y ny)
  15663. m (- m (* ny 12)))
  15664. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15665. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15666. (setq m2 (+ m dn) y2 y)
  15667. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15668. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15669. (while (<= n2 cday)
  15670. (setq n1 n2 m m2 y y2)
  15671. (setq m2 (+ m dn) y2 y)
  15672. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15673. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15674. ;; Make sure n1 is the earlier date
  15675. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15676. (if show-all
  15677. (cond
  15678. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15679. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15680. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15681. (cond
  15682. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15683. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15684. (t (if (= cday n1) n1 n2)))))))
  15685. (defun org-date-to-gregorian (date)
  15686. "Turn any specification of DATE into a Gregorian date for the calendar."
  15687. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15688. ((and (listp date) (= (length date) 3)) date)
  15689. ((stringp date)
  15690. (setq date (org-parse-time-string date))
  15691. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15692. ((listp date)
  15693. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15694. (defun org-parse-time-string (s &optional nodefault)
  15695. "Parse the standard Org-mode time string.
  15696. This should be a lot faster than the normal `parse-time-string'.
  15697. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15698. hour and minute fields will be nil if not given."
  15699. (cond ((string-match org-ts-regexp0 s)
  15700. (list 0
  15701. (if (or (match-beginning 8) (not nodefault))
  15702. (string-to-number (or (match-string 8 s) "0")))
  15703. (if (or (match-beginning 7) (not nodefault))
  15704. (string-to-number (or (match-string 7 s) "0")))
  15705. (string-to-number (match-string 4 s))
  15706. (string-to-number (match-string 3 s))
  15707. (string-to-number (match-string 2 s))
  15708. nil nil nil))
  15709. ((string-match "^<[^>]+>$" s)
  15710. (decode-time (seconds-to-time (org-matcher-time s))))
  15711. (t (error "Not a standard Org-mode time string: %s" s))))
  15712. (defun org-timestamp-up (&optional arg)
  15713. "Increase the date item at the cursor by one.
  15714. If the cursor is on the year, change the year. If it is on the month,
  15715. the day or the time, change that.
  15716. With prefix ARG, change by that many units."
  15717. (interactive "p")
  15718. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15719. (defun org-timestamp-down (&optional arg)
  15720. "Decrease the date item at the cursor by one.
  15721. If the cursor is on the year, change the year. If it is on the month,
  15722. the day or the time, change that.
  15723. With prefix ARG, change by that many units."
  15724. (interactive "p")
  15725. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15726. (defun org-timestamp-up-day (&optional arg)
  15727. "Increase the date in the time stamp by one day.
  15728. With prefix ARG, change that many days."
  15729. (interactive "p")
  15730. (if (and (not (org-at-timestamp-p t))
  15731. (org-at-heading-p))
  15732. (org-todo 'up)
  15733. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15734. (defun org-timestamp-down-day (&optional arg)
  15735. "Decrease the date in the time stamp by one day.
  15736. With prefix ARG, change that many days."
  15737. (interactive "p")
  15738. (if (and (not (org-at-timestamp-p t))
  15739. (org-at-heading-p))
  15740. (org-todo 'down)
  15741. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15742. (defun org-at-timestamp-p (&optional inactive-ok)
  15743. "Determine if the cursor is in or at a timestamp."
  15744. (interactive)
  15745. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15746. (pos (point))
  15747. (ans (or (looking-at tsr)
  15748. (save-excursion
  15749. (skip-chars-backward "^[<\n\r\t")
  15750. (if (> (point) (point-min)) (backward-char 1))
  15751. (and (looking-at tsr)
  15752. (> (- (match-end 0) pos) -1))))))
  15753. (and ans
  15754. (boundp 'org-ts-what)
  15755. (setq org-ts-what
  15756. (cond
  15757. ((= pos (match-beginning 0)) 'bracket)
  15758. ;; Point is considered to be "on the bracket" whether
  15759. ;; it's really on it or right after it.
  15760. ((= pos (1- (match-end 0))) 'bracket)
  15761. ((= pos (match-end 0)) 'after)
  15762. ((org-pos-in-match-range pos 2) 'year)
  15763. ((org-pos-in-match-range pos 3) 'month)
  15764. ((org-pos-in-match-range pos 7) 'hour)
  15765. ((org-pos-in-match-range pos 8) 'minute)
  15766. ((or (org-pos-in-match-range pos 4)
  15767. (org-pos-in-match-range pos 5)) 'day)
  15768. ((and (> pos (or (match-end 8) (match-end 5)))
  15769. (< pos (match-end 0)))
  15770. (- pos (or (match-end 8) (match-end 5))))
  15771. (t 'day))))
  15772. ans))
  15773. (defun org-toggle-timestamp-type ()
  15774. "Toggle the type (<active> or [inactive]) of a time stamp."
  15775. (interactive)
  15776. (when (org-at-timestamp-p t)
  15777. (let ((beg (match-beginning 0)) (end (match-end 0))
  15778. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15779. (save-excursion
  15780. (goto-char beg)
  15781. (while (re-search-forward "[][<>]" end t)
  15782. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15783. t t)))
  15784. (message "Timestamp is now %sactive"
  15785. (if (equal (char-after beg) ?<) "" "in")))))
  15786. (defun org-at-clock-log-p nil
  15787. "Is the cursor on the clock log line?"
  15788. (save-excursion
  15789. (move-beginning-of-line 1)
  15790. (looking-at "^[ \t]*CLOCK:")))
  15791. (defvar org-clock-history) ; defined in org-clock.el
  15792. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15793. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15794. "Change the date in the time stamp at point.
  15795. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15796. `year', `minute', `second'. If WHAT is not given, the cursor position
  15797. in the timestamp determines what will be changed.
  15798. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15799. (let ((origin (point)) origin-cat
  15800. with-hm inactive
  15801. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15802. org-ts-what
  15803. extra rem
  15804. ts time time0 fixnext clrgx)
  15805. (if (not (org-at-timestamp-p t))
  15806. (user-error "Not at a timestamp"))
  15807. (if (and (not what) (eq org-ts-what 'bracket))
  15808. (org-toggle-timestamp-type)
  15809. ;; Point isn't on brackets. Remember the part of the time-stamp
  15810. ;; the point was in. Indeed, size of time-stamps may change,
  15811. ;; but point must be kept in the same category nonetheless.
  15812. (setq origin-cat org-ts-what)
  15813. (if (and (not what) (not (eq org-ts-what 'day))
  15814. org-display-custom-times
  15815. (get-text-property (point) 'display)
  15816. (not (get-text-property (1- (point)) 'display)))
  15817. (setq org-ts-what 'day))
  15818. (setq org-ts-what (or what org-ts-what)
  15819. inactive (= (char-after (match-beginning 0)) ?\[)
  15820. ts (match-string 0))
  15821. (replace-match "")
  15822. (when (string-match
  15823. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15824. ts)
  15825. (setq extra (match-string 1 ts))
  15826. (if suppress-tmp-delay
  15827. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15828. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15829. (setq with-hm t))
  15830. (setq time0 (org-parse-time-string ts))
  15831. (when (and updown
  15832. (eq org-ts-what 'minute)
  15833. (not current-prefix-arg))
  15834. ;; This looks like s-up and s-down. Change by one rounding step.
  15835. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15836. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15837. (setcar (cdr time0) (+ (nth 1 time0)
  15838. (if (> n 0) (- rem) (- dm rem))))))
  15839. (setq time
  15840. (encode-time (or (car time0) 0)
  15841. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15842. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15843. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15844. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15845. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15846. (nthcdr 6 time0)))
  15847. (when (and (member org-ts-what '(hour minute))
  15848. extra
  15849. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15850. (setq extra (org-modify-ts-extra
  15851. extra
  15852. (if (eq org-ts-what 'hour) 2 5)
  15853. n dm)))
  15854. (when (integerp org-ts-what)
  15855. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15856. (if (eq what 'calendar)
  15857. (let ((cal-date (org-get-date-from-calendar)))
  15858. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15859. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15860. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15861. (setcar time0 (or (car time0) 0))
  15862. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15863. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15864. (setq time (apply 'encode-time time0))))
  15865. ;; Insert the new time-stamp, and ensure point stays in the same
  15866. ;; category as before (i.e. not after the last position in that
  15867. ;; category).
  15868. (let ((pos (point)))
  15869. ;; Stay before inserted string. `save-excursion' is of no use.
  15870. (setq org-last-changed-timestamp
  15871. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15872. (goto-char pos))
  15873. (save-match-data
  15874. (looking-at org-ts-regexp3)
  15875. (goto-char (cond
  15876. ;; `day' category ends before `hour' if any, or at
  15877. ;; the end of the day name.
  15878. ((eq origin-cat 'day)
  15879. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15880. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15881. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15882. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15883. ;; `year' and `month' have both fixed size: point
  15884. ;; couldn't have moved into another part.
  15885. (t origin))))
  15886. ;; Update clock if on a CLOCK line.
  15887. (org-clock-update-time-maybe)
  15888. ;; Maybe adjust the closest clock in `org-clock-history'
  15889. (when org-clock-adjust-closest
  15890. (if (not (and (org-at-clock-log-p)
  15891. (< 1 (length (delq nil (mapcar 'marker-position
  15892. org-clock-history))))))
  15893. (message "No clock to adjust")
  15894. (cond ((save-excursion ; fix previous clock?
  15895. (re-search-backward org-ts-regexp0 nil t)
  15896. (org-looking-back (concat org-clock-string " \\[")))
  15897. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15898. ((save-excursion ; fix next clock?
  15899. (re-search-backward org-ts-regexp0 nil t)
  15900. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15901. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15902. (save-window-excursion
  15903. ;; Find closest clock to point, adjust the previous/next one in history
  15904. (let* ((p (save-excursion (org-back-to-heading t)))
  15905. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15906. (clfixnth
  15907. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15908. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15909. (if (not clfixpos)
  15910. (message "No clock to adjust")
  15911. (save-excursion
  15912. (org-goto-marker-or-bmk clfixpos)
  15913. (org-show-subtree)
  15914. (when (re-search-forward clrgx nil t)
  15915. (goto-char (match-beginning 1))
  15916. (let (org-clock-adjust-closest)
  15917. (org-timestamp-change n org-ts-what updown))
  15918. (message "Clock adjusted in %s for heading: %s"
  15919. (file-name-nondirectory (buffer-file-name))
  15920. (org-get-heading t t)))))))))
  15921. ;; Try to recenter the calendar window, if any.
  15922. (if (and org-calendar-follow-timestamp-change
  15923. (get-buffer-window "*Calendar*" t)
  15924. (memq org-ts-what '(day month year)))
  15925. (org-recenter-calendar (time-to-days time))))))
  15926. (defun org-modify-ts-extra (s pos n dm)
  15927. "Change the different parts of the lead-time and repeat fields in timestamp."
  15928. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15929. ng h m new rem)
  15930. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15931. (cond
  15932. ((or (org-pos-in-match-range pos 2)
  15933. (org-pos-in-match-range pos 3))
  15934. (setq m (string-to-number (match-string 3 s))
  15935. h (string-to-number (match-string 2 s)))
  15936. (if (org-pos-in-match-range pos 2)
  15937. (setq h (+ h n))
  15938. (setq n (* dm (org-no-warnings (signum n))))
  15939. (when (not (= 0 (setq rem (% m dm))))
  15940. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15941. (setq m (+ m n)))
  15942. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15943. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15944. (setq h (min 24 (max 0 h)))
  15945. (setq ng 1 new (format "-%02d:%02d" h m)))
  15946. ((org-pos-in-match-range pos 6)
  15947. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15948. ((org-pos-in-match-range pos 5)
  15949. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15950. ((org-pos-in-match-range pos 9)
  15951. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15952. ((org-pos-in-match-range pos 8)
  15953. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15954. (when ng
  15955. (setq s (concat
  15956. (substring s 0 (match-beginning ng))
  15957. new
  15958. (substring s (match-end ng))))))
  15959. s))
  15960. (defun org-recenter-calendar (date)
  15961. "If the calendar is visible, recenter it to DATE."
  15962. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15963. (when cwin
  15964. (let ((calendar-move-hook nil))
  15965. (with-selected-window cwin
  15966. (calendar-goto-date (if (listp date) date
  15967. (calendar-gregorian-from-absolute date))))))))
  15968. (defun org-goto-calendar (&optional arg)
  15969. "Go to the Emacs calendar at the current date.
  15970. If there is a time stamp in the current line, go to that date.
  15971. A prefix ARG can be used to force the current date."
  15972. (interactive "P")
  15973. (let ((tsr org-ts-regexp) diff
  15974. (calendar-move-hook nil)
  15975. (calendar-view-holidays-initially-flag nil)
  15976. (calendar-view-diary-initially-flag nil))
  15977. (if (or (org-at-timestamp-p)
  15978. (save-excursion
  15979. (beginning-of-line 1)
  15980. (looking-at (concat ".*" tsr))))
  15981. (let ((d1 (time-to-days (current-time)))
  15982. (d2 (time-to-days
  15983. (org-time-string-to-time (match-string 1)))))
  15984. (setq diff (- d2 d1))))
  15985. (calendar)
  15986. (calendar-goto-today)
  15987. (if (and diff (not arg)) (calendar-forward-day diff))))
  15988. (defun org-get-date-from-calendar ()
  15989. "Return a list (month day year) of date at point in calendar."
  15990. (with-current-buffer "*Calendar*"
  15991. (save-match-data
  15992. (calendar-cursor-to-date))))
  15993. (defun org-date-from-calendar ()
  15994. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15995. If there is already a time stamp at the cursor position, update it."
  15996. (interactive)
  15997. (if (org-at-timestamp-p t)
  15998. (org-timestamp-change 0 'calendar)
  15999. (let ((cal-date (org-get-date-from-calendar)))
  16000. (org-insert-time-stamp
  16001. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16002. (defcustom org-effort-durations
  16003. `(("h" . 60)
  16004. ("d" . ,(* 60 8))
  16005. ("w" . ,(* 60 8 5))
  16006. ("m" . ,(* 60 8 5 4))
  16007. ("y" . ,(* 60 8 5 40)))
  16008. "Conversion factor to minutes for an effort modifier.
  16009. Each entry has the form (MODIFIER . MINUTES).
  16010. In an effort string, a number followed by MODIFIER is multiplied
  16011. by the specified number of MINUTES to obtain an effort in
  16012. minutes.
  16013. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16014. effort string \"2hours\" is equivalent to 120 minutes."
  16015. :group 'org-agenda
  16016. :version "24.1"
  16017. :type '(alist :key-type (string :tag "Modifier")
  16018. :value-type (number :tag "Minutes")))
  16019. (defun org-minutes-to-clocksum-string (m)
  16020. "Format number of minutes as a clocksum string.
  16021. The format is determined by `org-time-clocksum-format',
  16022. `org-time-clocksum-use-fractional' and
  16023. `org-time-clocksum-fractional-format' and
  16024. `org-time-clocksum-use-effort-durations'."
  16025. (let ((clocksum "")
  16026. (m (round m)) ; Don't allow fractions of minutes
  16027. h d w mo y fmt n)
  16028. (setq h (if org-time-clocksum-use-effort-durations
  16029. (cdr (assoc "h" org-effort-durations)) 60)
  16030. d (if org-time-clocksum-use-effort-durations
  16031. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16032. w (if org-time-clocksum-use-effort-durations
  16033. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16034. mo (if org-time-clocksum-use-effort-durations
  16035. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16036. y (if org-time-clocksum-use-effort-durations
  16037. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16038. ;; fractional format
  16039. (if org-time-clocksum-use-fractional
  16040. (cond
  16041. ;; single format string
  16042. ((stringp org-time-clocksum-fractional-format)
  16043. (format org-time-clocksum-fractional-format (/ m (float h))))
  16044. ;; choice of fractional formats for different time units
  16045. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16046. (> (/ (truncate m) (* y d h)) 0))
  16047. (format fmt (/ m (* y d (float h)))))
  16048. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16049. (> (/ (truncate m) (* mo d h)) 0))
  16050. (format fmt (/ m (* mo d (float h)))))
  16051. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16052. (> (/ (truncate m) (* w d h)) 0))
  16053. (format fmt (/ m (* w d (float h)))))
  16054. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16055. (> (/ (truncate m) (* d h)) 0))
  16056. (format fmt (/ m (* d (float h)))))
  16057. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16058. (> (/ (truncate m) h) 0))
  16059. (format fmt (/ m (float h))))
  16060. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16061. (format fmt m))
  16062. ;; fall back to smallest time unit with a format
  16063. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16064. (format fmt (/ m (float h))))
  16065. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16066. (format fmt (/ m (* d (float h)))))
  16067. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16068. (format fmt (/ m (* w d (float h)))))
  16069. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16070. (format fmt (/ m (* mo d (float h)))))
  16071. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16072. (format fmt (/ m (* y d (float h))))))
  16073. ;; standard (non-fractional) format, with single format string
  16074. (if (stringp org-time-clocksum-format)
  16075. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16076. ;; separate formats components
  16077. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16078. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16079. (plist-get org-time-clocksum-format :require-years))
  16080. (setq clocksum (concat clocksum (format fmt n))
  16081. m (- m (* n y d h))))
  16082. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16083. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16084. (plist-get org-time-clocksum-format :require-months))
  16085. (setq clocksum (concat clocksum (format fmt n))
  16086. m (- m (* n mo d h))))
  16087. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16088. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16089. (plist-get org-time-clocksum-format :require-weeks))
  16090. (setq clocksum (concat clocksum (format fmt n))
  16091. m (- m (* n w d h))))
  16092. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16093. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16094. (plist-get org-time-clocksum-format :require-days))
  16095. (setq clocksum (concat clocksum (format fmt n))
  16096. m (- m (* n d h))))
  16097. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16098. (or (> (setq n (/ (truncate m) h)) 0)
  16099. (plist-get org-time-clocksum-format :require-hours))
  16100. (setq clocksum (concat clocksum (format fmt n))
  16101. m (- m (* n h))))
  16102. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16103. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16104. (setq clocksum (concat clocksum (format fmt m))))
  16105. ;; return formatted time duration
  16106. clocksum))))
  16107. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16108. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16109. "Org mode version 8.0")
  16110. (defun org-hours-to-clocksum-string (n)
  16111. (org-minutes-to-clocksum-string (* n 60)))
  16112. (defun org-hh:mm-string-to-minutes (s)
  16113. "Convert a string H:MM to a number of minutes.
  16114. If the string is just a number, interpret it as minutes.
  16115. In fact, the first hh:mm or number in the string will be taken,
  16116. there can be extra stuff in the string.
  16117. If no number is found, the return value is 0."
  16118. (cond
  16119. ((integerp s) s)
  16120. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16121. (+ (* (string-to-number (match-string 1 s)) 60)
  16122. (string-to-number (match-string 2 s))))
  16123. ((string-match "\\([0-9]+\\)" s)
  16124. (string-to-number (match-string 1 s)))
  16125. (t 0)))
  16126. (defcustom org-image-actual-width t
  16127. "Should we use the actual width of images when inlining them?
  16128. When set to `t', always use the image width.
  16129. When set to a number, use imagemagick (when available) to set
  16130. the image's width to this value.
  16131. When set to a number in a list, try to get the width from any
  16132. #+ATTR.* keyword if it matches a width specification like
  16133. #+ATTR_HTML: :width 300px
  16134. and fall back on that number if none is found.
  16135. When set to nil, try to get the width from an #+ATTR.* keyword
  16136. and fall back on the original width if none is found.
  16137. This requires Emacs >= 24.1, build with imagemagick support."
  16138. :group 'org-appearance
  16139. :version "24.4"
  16140. :package-version '(Org . "8.0")
  16141. :type '(choice
  16142. (const :tag "Use the image width" t)
  16143. (integer :tag "Use a number of pixels")
  16144. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16145. (const :tag "Use #+ATTR* or don't resize" nil)))
  16146. (defcustom org-agenda-inhibit-startup nil
  16147. "Inhibit startup when preparing agenda buffers.
  16148. When this variable is `t' (the default), the initialization of
  16149. the Org agenda buffers is inhibited: e.g. the visibility state
  16150. is not set, the tables are not re-aligned, etc."
  16151. :type 'boolean
  16152. :version "24.3"
  16153. :group 'org-agenda)
  16154. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16155. "Convert a duration string S to minutes.
  16156. A bare number is interpreted as minutes, modifiers can be set by
  16157. customizing `org-effort-durations' (which see).
  16158. Entries containing a colon are interpreted as H:MM by
  16159. `org-hh:mm-string-to-minutes'."
  16160. (let ((result 0)
  16161. (re (concat "\\([0-9.]+\\) *\\("
  16162. (regexp-opt (mapcar 'car org-effort-durations))
  16163. "\\)")))
  16164. (while (string-match re s)
  16165. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16166. (string-to-number (match-string 1 s))))
  16167. (setq s (replace-match "" nil t s)))
  16168. (setq result (floor result))
  16169. (incf result (org-hh:mm-string-to-minutes s))
  16170. (if output-to-string (number-to-string result) result)))
  16171. ;;;; Files
  16172. (defun org-save-all-org-buffers ()
  16173. "Save all Org-mode buffers without user confirmation."
  16174. (interactive)
  16175. (message "Saving all Org-mode buffers...")
  16176. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16177. (when (featurep 'org-id) (org-id-locations-save))
  16178. (message "Saving all Org-mode buffers... done"))
  16179. (defun org-revert-all-org-buffers ()
  16180. "Revert all Org-mode buffers.
  16181. Prompt for confirmation when there are unsaved changes.
  16182. Be sure you know what you are doing before letting this function
  16183. overwrite your changes.
  16184. This function is useful in a setup where one tracks org files
  16185. with a version control system, to revert on one machine after pulling
  16186. changes from another. I believe the procedure must be like this:
  16187. 1. M-x org-save-all-org-buffers
  16188. 2. Pull changes from the other machine, resolve conflicts
  16189. 3. M-x org-revert-all-org-buffers"
  16190. (interactive)
  16191. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16192. (user-error "Abort"))
  16193. (save-excursion
  16194. (save-window-excursion
  16195. (mapc
  16196. (lambda (b)
  16197. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16198. (with-current-buffer b buffer-file-name))
  16199. (org-pop-to-buffer-same-window b)
  16200. (revert-buffer t 'no-confirm)))
  16201. (buffer-list))
  16202. (when (and (featurep 'org-id) org-id-track-globally)
  16203. (org-id-locations-load)))))
  16204. ;;;; Agenda files
  16205. ;;;###autoload
  16206. (defun org-switchb (&optional arg)
  16207. "Switch between Org buffers.
  16208. With one prefix argument, restrict available buffers to files.
  16209. With two prefix arguments, restrict available buffers to agenda files.
  16210. Defaults to `iswitchb' for buffer name completion.
  16211. Set `org-completion-use-ido' to make it use ido instead."
  16212. (interactive "P")
  16213. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16214. ((equal arg '(16)) (org-buffer-list 'agenda))
  16215. (t (org-buffer-list))))
  16216. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16217. (org-completion-use-ido org-completion-use-ido))
  16218. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16219. (setq org-completion-use-iswitchb t))
  16220. (org-pop-to-buffer-same-window
  16221. (org-icompleting-read "Org buffer: "
  16222. (mapcar 'list (mapcar 'buffer-name blist))
  16223. nil t))))
  16224. ;;; Define some older names previously used for this functionality
  16225. ;;;###autoload
  16226. (defalias 'org-ido-switchb 'org-switchb)
  16227. ;;;###autoload
  16228. (defalias 'org-iswitchb 'org-switchb)
  16229. (defun org-buffer-list (&optional predicate exclude-tmp)
  16230. "Return a list of Org buffers.
  16231. PREDICATE can be `export', `files' or `agenda'.
  16232. export restrict the list to Export buffers.
  16233. files restrict the list to buffers visiting Org files.
  16234. agenda restrict the list to buffers visiting agenda files.
  16235. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16236. (let* ((bfn nil)
  16237. (agenda-files (and (eq predicate 'agenda)
  16238. (mapcar 'file-truename (org-agenda-files t))))
  16239. (filter
  16240. (cond
  16241. ((eq predicate 'files)
  16242. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16243. ((eq predicate 'export)
  16244. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16245. ((eq predicate 'agenda)
  16246. (lambda (b)
  16247. (with-current-buffer b
  16248. (and (derived-mode-p 'org-mode)
  16249. (setq bfn (buffer-file-name b))
  16250. (member (file-truename bfn) agenda-files)))))
  16251. (t (lambda (b) (with-current-buffer b
  16252. (or (derived-mode-p 'org-mode)
  16253. (string-match "\*Org .*Export"
  16254. (buffer-name b)))))))))
  16255. (delq nil
  16256. (mapcar
  16257. (lambda(b)
  16258. (if (and (funcall filter b)
  16259. (or (not exclude-tmp)
  16260. (not (string-match "tmp" (buffer-name b)))))
  16261. b
  16262. nil))
  16263. (buffer-list)))))
  16264. (defun org-agenda-files (&optional unrestricted archives)
  16265. "Get the list of agenda files.
  16266. Optional UNRESTRICTED means return the full list even if a restriction
  16267. is currently in place.
  16268. When ARCHIVES is t, include all archive files that are really being
  16269. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16270. `org-agenda-archives-mode' is t."
  16271. (let ((files
  16272. (cond
  16273. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16274. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16275. ((listp org-agenda-files) org-agenda-files)
  16276. (t (error "Invalid value of `org-agenda-files'")))))
  16277. (setq files (apply 'append
  16278. (mapcar (lambda (f)
  16279. (if (file-directory-p f)
  16280. (directory-files
  16281. f t org-agenda-file-regexp)
  16282. (list f)))
  16283. files)))
  16284. (when org-agenda-skip-unavailable-files
  16285. (setq files (delq nil
  16286. (mapcar (function
  16287. (lambda (file)
  16288. (and (file-readable-p file) file)))
  16289. files))))
  16290. (when (or (eq archives t)
  16291. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16292. (setq files (org-add-archive-files files)))
  16293. files))
  16294. (defun org-agenda-file-p (&optional file)
  16295. "Return non-nil, if FILE is an agenda file.
  16296. If FILE is omitted, use the file associated with the current
  16297. buffer."
  16298. (member (or file (buffer-file-name))
  16299. (org-agenda-files t)))
  16300. (defun org-edit-agenda-file-list ()
  16301. "Edit the list of agenda files.
  16302. Depending on setup, this either uses customize to edit the variable
  16303. `org-agenda-files', or it visits the file that is holding the list. In the
  16304. latter case, the buffer is set up in a way that saving it automatically kills
  16305. the buffer and restores the previous window configuration."
  16306. (interactive)
  16307. (if (stringp org-agenda-files)
  16308. (let ((cw (current-window-configuration)))
  16309. (find-file org-agenda-files)
  16310. (org-set-local 'org-window-configuration cw)
  16311. (org-add-hook 'after-save-hook
  16312. (lambda ()
  16313. (set-window-configuration
  16314. (prog1 org-window-configuration
  16315. (kill-buffer (current-buffer))))
  16316. (org-install-agenda-files-menu)
  16317. (message "New agenda file list installed"))
  16318. nil 'local)
  16319. (message "%s" (substitute-command-keys
  16320. "Edit list and finish with \\[save-buffer]")))
  16321. (customize-variable 'org-agenda-files)))
  16322. (defun org-store-new-agenda-file-list (list)
  16323. "Set new value for the agenda file list and save it correctly."
  16324. (if (stringp org-agenda-files)
  16325. (let ((fe (org-read-agenda-file-list t)) b u)
  16326. (while (setq b (find-buffer-visiting org-agenda-files))
  16327. (kill-buffer b))
  16328. (with-temp-file org-agenda-files
  16329. (insert
  16330. (mapconcat
  16331. (lambda (f) ;; Keep un-expanded entries.
  16332. (if (setq u (assoc f fe))
  16333. (cdr u)
  16334. f))
  16335. list "\n")
  16336. "\n")))
  16337. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16338. (org-insert-mode-line-in-empty-file nil))
  16339. (setq org-agenda-files list)
  16340. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16341. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16342. "Read the list of agenda files from a file.
  16343. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16344. filenames, used by `org-store-new-agenda-file-list' to write back
  16345. un-expanded file names."
  16346. (when (file-directory-p org-agenda-files)
  16347. (error "`org-agenda-files' cannot be a single directory"))
  16348. (when (stringp org-agenda-files)
  16349. (with-temp-buffer
  16350. (insert-file-contents org-agenda-files)
  16351. (mapcar
  16352. (lambda (f)
  16353. (let ((e (expand-file-name (substitute-in-file-name f)
  16354. org-directory)))
  16355. (if pair-with-expansion
  16356. (cons e f)
  16357. e)))
  16358. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16359. ;;;###autoload
  16360. (defun org-cycle-agenda-files ()
  16361. "Cycle through the files in `org-agenda-files'.
  16362. If the current buffer visits an agenda file, find the next one in the list.
  16363. If the current buffer does not, find the first agenda file."
  16364. (interactive)
  16365. (let* ((fs (org-agenda-files t))
  16366. (files (append fs (list (car fs))))
  16367. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16368. file)
  16369. (unless files (user-error "No agenda files"))
  16370. (catch 'exit
  16371. (while (setq file (pop files))
  16372. (if (equal (file-truename file) tcf)
  16373. (when (car files)
  16374. (find-file (car files))
  16375. (throw 'exit t))))
  16376. (find-file (car fs)))
  16377. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16378. (defun org-agenda-file-to-front (&optional to-end)
  16379. "Move/add the current file to the top of the agenda file list.
  16380. If the file is not present in the list, it is added to the front. If it is
  16381. present, it is moved there. With optional argument TO-END, add/move to the
  16382. end of the list."
  16383. (interactive "P")
  16384. (let ((org-agenda-skip-unavailable-files nil)
  16385. (file-alist (mapcar (lambda (x)
  16386. (cons (file-truename x) x))
  16387. (org-agenda-files t)))
  16388. (ctf (file-truename
  16389. (or buffer-file-name
  16390. (user-error "Please save the current buffer to a file"))))
  16391. x had)
  16392. (setq x (assoc ctf file-alist) had x)
  16393. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16394. (if to-end
  16395. (setq file-alist (append (delq x file-alist) (list x)))
  16396. (setq file-alist (cons x (delq x file-alist))))
  16397. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16398. (org-install-agenda-files-menu)
  16399. (message "File %s to %s of agenda file list"
  16400. (if had "moved" "added") (if to-end "end" "front"))))
  16401. (defun org-remove-file (&optional file)
  16402. "Remove current file from the list of files in variable `org-agenda-files'.
  16403. These are the files which are being checked for agenda entries.
  16404. Optional argument FILE means use this file instead of the current."
  16405. (interactive)
  16406. (let* ((org-agenda-skip-unavailable-files nil)
  16407. (file (or file buffer-file-name
  16408. (user-error "Current buffer does not visit a file")))
  16409. (true-file (file-truename file))
  16410. (afile (abbreviate-file-name file))
  16411. (files (delq nil (mapcar
  16412. (lambda (x)
  16413. (if (equal true-file
  16414. (file-truename x))
  16415. nil x))
  16416. (org-agenda-files t)))))
  16417. (if (not (= (length files) (length (org-agenda-files t))))
  16418. (progn
  16419. (org-store-new-agenda-file-list files)
  16420. (org-install-agenda-files-menu)
  16421. (message "Removed file: %s" afile))
  16422. (message "File was not in list: %s (not removed)" afile))))
  16423. (defun org-file-menu-entry (file)
  16424. (vector file (list 'find-file file) t))
  16425. (defun org-check-agenda-file (file)
  16426. "Make sure FILE exists. If not, ask user what to do."
  16427. (when (not (file-exists-p file))
  16428. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16429. (abbreviate-file-name file))
  16430. (let ((r (downcase (read-char-exclusive))))
  16431. (cond
  16432. ((equal r ?r)
  16433. (org-remove-file file)
  16434. (throw 'nextfile t))
  16435. (t (error "Abort"))))))
  16436. (defun org-get-agenda-file-buffer (file)
  16437. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16438. it to the list of buffers which might be released later."
  16439. (let ((buf (org-find-base-buffer-visiting file)))
  16440. (if buf
  16441. buf ; just return it
  16442. ;; Make a new buffer and remember it
  16443. (setq buf (find-file-noselect file))
  16444. (if buf (push buf org-agenda-new-buffers))
  16445. buf)))
  16446. (defun org-release-buffers (blist)
  16447. "Release all buffers in list, asking the user for confirmation when needed.
  16448. When a buffer is unmodified, it is just killed. When modified, it is saved
  16449. \(if the user agrees) and then killed."
  16450. (let (buf file)
  16451. (while (setq buf (pop blist))
  16452. (setq file (buffer-file-name buf))
  16453. (when (and (buffer-modified-p buf)
  16454. file
  16455. (y-or-n-p (format "Save file %s? " file)))
  16456. (with-current-buffer buf (save-buffer)))
  16457. (kill-buffer buf))))
  16458. (defun org-agenda-prepare-buffers (files)
  16459. "Create buffers for all agenda files, protect archived trees and comments."
  16460. (interactive)
  16461. (let ((pa '(:org-archived t))
  16462. (pc '(:org-comment t))
  16463. (pall '(:org-archived t :org-comment t))
  16464. (inhibit-read-only t)
  16465. (org-inhibit-startup org-agenda-inhibit-startup)
  16466. (rea (concat ":" org-archive-tag ":"))
  16467. file re)
  16468. (setq org-tag-alist-for-agenda nil
  16469. org-tag-groups-alist-for-agenda nil)
  16470. (save-excursion
  16471. (save-restriction
  16472. (while (setq file (pop files))
  16473. (catch 'nextfile
  16474. (if (bufferp file)
  16475. (set-buffer file)
  16476. (org-check-agenda-file file)
  16477. (set-buffer (org-get-agenda-file-buffer file)))
  16478. (widen)
  16479. (org-set-regexps-and-options-for-tags)
  16480. (org-refresh-category-properties)
  16481. (org-refresh-properties org-effort-property 'org-effort)
  16482. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16483. (setq org-todo-keywords-for-agenda
  16484. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16485. (setq org-done-keywords-for-agenda
  16486. (append org-done-keywords-for-agenda org-done-keywords))
  16487. (setq org-todo-keyword-alist-for-agenda
  16488. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16489. (setq org-drawers-for-agenda
  16490. (append org-drawers-for-agenda org-drawers))
  16491. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16492. (setq org-tag-alist-for-agenda
  16493. (append org-tag-alist-for-agenda org-tag-alist)))
  16494. (if org-group-tags
  16495. (setq org-tag-groups-alist-for-agenda
  16496. (org-uniquify-alist
  16497. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16498. (org-with-silent-modifications
  16499. (save-excursion
  16500. (remove-text-properties (point-min) (point-max) pall)
  16501. (when org-agenda-skip-archived-trees
  16502. (goto-char (point-min))
  16503. (while (re-search-forward rea nil t)
  16504. (if (org-at-heading-p t)
  16505. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16506. (goto-char (point-min))
  16507. (setq re (format org-heading-keyword-regexp-format
  16508. org-comment-string))
  16509. (while (re-search-forward re nil t)
  16510. (add-text-properties
  16511. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16512. (setq org-todo-keywords-for-agenda
  16513. (org-uniquify org-todo-keywords-for-agenda))
  16514. (setq org-todo-keyword-alist-for-agenda
  16515. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16516. ;;;; CDLaTeX minor mode
  16517. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16518. "Keymap for the minor `org-cdlatex-mode'.")
  16519. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16520. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16521. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16522. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16523. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16524. (defvar org-cdlatex-texmathp-advice-is-done nil
  16525. "Flag remembering if we have applied the advice to texmathp already.")
  16526. (define-minor-mode org-cdlatex-mode
  16527. "Toggle the minor `org-cdlatex-mode'.
  16528. This mode supports entering LaTeX environment and math in LaTeX fragments
  16529. in Org-mode.
  16530. \\{org-cdlatex-mode-map}"
  16531. nil " OCDL" nil
  16532. (when org-cdlatex-mode
  16533. (require 'cdlatex)
  16534. (run-hooks 'cdlatex-mode-hook)
  16535. (cdlatex-compute-tables))
  16536. (unless org-cdlatex-texmathp-advice-is-done
  16537. (setq org-cdlatex-texmathp-advice-is-done t)
  16538. (defadvice texmathp (around org-math-always-on activate)
  16539. "Always return t in org-mode buffers.
  16540. This is because we want to insert math symbols without dollars even outside
  16541. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16542. an embedded LaTeX fragment, let texmathp do its job.
  16543. \\[org-cdlatex-mode-map]"
  16544. (interactive)
  16545. (let (p)
  16546. (cond
  16547. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16548. ((eq this-command 'cdlatex-math-symbol)
  16549. (setq ad-return-value t
  16550. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16551. (t
  16552. (let ((p (org-inside-LaTeX-fragment-p)))
  16553. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16554. (setq ad-return-value t
  16555. texmathp-why '("Org-mode embedded math" . 0))
  16556. (if p ad-do-it)))))))))
  16557. (defun turn-on-org-cdlatex ()
  16558. "Unconditionally turn on `org-cdlatex-mode'."
  16559. (org-cdlatex-mode 1))
  16560. (defun org-try-cdlatex-tab ()
  16561. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16562. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16563. - inside a LaTeX fragment, or
  16564. - after the first word in a line, where an abbreviation expansion could
  16565. insert a LaTeX environment."
  16566. (when org-cdlatex-mode
  16567. (cond
  16568. ;; Before any word on the line: No expansion possible.
  16569. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16570. ;; Just after first word on the line: Expand it. Make sure it
  16571. ;; cannot happen on headlines, though.
  16572. ((save-excursion
  16573. (skip-chars-backward "a-zA-Z0-9*")
  16574. (skip-chars-backward " \t")
  16575. (and (bolp) (not (org-at-heading-p))))
  16576. (cdlatex-tab) t)
  16577. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16578. (defun org-cdlatex-underscore-caret (&optional arg)
  16579. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16580. Revert to the normal definition outside of these fragments."
  16581. (interactive "P")
  16582. (if (org-inside-LaTeX-fragment-p)
  16583. (call-interactively 'cdlatex-sub-superscript)
  16584. (let (org-cdlatex-mode)
  16585. (call-interactively (key-binding (vector last-input-event))))))
  16586. (defun org-cdlatex-math-modify (&optional arg)
  16587. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16588. Revert to the normal definition outside of these fragments."
  16589. (interactive "P")
  16590. (if (org-inside-LaTeX-fragment-p)
  16591. (call-interactively 'cdlatex-math-modify)
  16592. (let (org-cdlatex-mode)
  16593. (call-interactively (key-binding (vector last-input-event))))))
  16594. ;;;; LaTeX fragments
  16595. (defvar org-latex-regexps
  16596. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16597. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16598. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16599. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16600. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16601. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16602. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16603. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16604. "Regular expressions for matching embedded LaTeX.")
  16605. (defun org-inside-LaTeX-fragment-p ()
  16606. "Test if point is inside a LaTeX fragment.
  16607. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16608. sequence appearing also before point.
  16609. Even though the matchers for math are configurable, this function assumes
  16610. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16611. delimiters are skipped when they have been removed by customization.
  16612. The return value is nil, or a cons cell with the delimiter and the
  16613. position of this delimiter.
  16614. This function does a reasonably good job, but can locally be fooled by
  16615. for example currency specifications. For example it will assume being in
  16616. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16617. fragments that are properly closed, but during editing, we have to live
  16618. with the uncertainty caused by missing closing delimiters. This function
  16619. looks only before point, not after."
  16620. (catch 'exit
  16621. (let ((pos (point))
  16622. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16623. (lim (progn
  16624. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16625. (point)))
  16626. dd-on str (start 0) m re)
  16627. (goto-char pos)
  16628. (when dodollar
  16629. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16630. re (nth 1 (assoc "$" org-latex-regexps)))
  16631. (while (string-match re str start)
  16632. (cond
  16633. ((= (match-end 0) (length str))
  16634. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16635. ((= (match-end 0) (- (length str) 5))
  16636. (throw 'exit nil))
  16637. (t (setq start (match-end 0))))))
  16638. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16639. (goto-char pos)
  16640. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16641. (and (match-beginning 2) (throw 'exit nil))
  16642. ;; count $$
  16643. (while (re-search-backward "\\$\\$" lim t)
  16644. (setq dd-on (not dd-on)))
  16645. (goto-char pos)
  16646. (if dd-on (cons "$$" m))))))
  16647. (defun org-inside-latex-macro-p ()
  16648. "Is point inside a LaTeX macro or its arguments?"
  16649. (save-match-data
  16650. (org-in-regexp
  16651. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16652. (defvar org-latex-fragment-image-overlays nil
  16653. "List of overlays carrying the images of latex fragments.")
  16654. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16655. (defun org-remove-latex-fragment-image-overlays ()
  16656. "Remove all overlays with LaTeX fragment images in current buffer."
  16657. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16658. (setq org-latex-fragment-image-overlays nil))
  16659. (defun org-preview-latex-fragment (&optional subtree)
  16660. "Preview the LaTeX fragment at point, or all locally or globally.
  16661. If the cursor is in a LaTeX fragment, create the image and overlay
  16662. it over the source code. If there is no fragment at point, display
  16663. all fragments in the current text, from one headline to the next. With
  16664. prefix SUBTREE, display all fragments in the current subtree. With a
  16665. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16666. the cursor is before the first headline,
  16667. display all fragments in the buffer.
  16668. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16669. (interactive "P")
  16670. (unless buffer-file-name
  16671. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16672. (org-remove-latex-fragment-image-overlays)
  16673. (save-excursion
  16674. (save-restriction
  16675. (let (beg end at msg)
  16676. (cond
  16677. ((or (equal subtree '(16))
  16678. (not (save-excursion
  16679. (re-search-backward org-outline-regexp-bol nil t))))
  16680. (setq beg (point-min) end (point-max)
  16681. msg "Creating images for buffer...%s"))
  16682. ((equal subtree '(4))
  16683. (org-back-to-heading)
  16684. (setq beg (point) end (org-end-of-subtree t)
  16685. msg "Creating images for subtree...%s"))
  16686. (t
  16687. (if (setq at (org-inside-LaTeX-fragment-p))
  16688. (goto-char (max (point-min) (- (cdr at) 2)))
  16689. (org-back-to-heading))
  16690. (setq beg (point) end (progn (outline-next-heading) (point))
  16691. msg (if at "Creating image...%s"
  16692. "Creating images for entry...%s"))))
  16693. (message msg "")
  16694. (narrow-to-region beg end)
  16695. (goto-char beg)
  16696. (org-format-latex
  16697. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16698. (file-name-nondirectory
  16699. buffer-file-name)))
  16700. default-directory 'overlays msg at 'forbuffer
  16701. org-latex-create-formula-image-program)
  16702. (message msg "done. Use `C-c C-c' to remove images.")))))
  16703. (defun org-format-latex (prefix &optional dir overlays msg at
  16704. forbuffer processing-type)
  16705. "Replace LaTeX fragments with links to an image, and produce images.
  16706. Some of the options can be changed using the variable
  16707. `org-format-latex-options'."
  16708. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16709. (let* ((prefixnodir (file-name-nondirectory prefix))
  16710. (absprefix (expand-file-name prefix dir))
  16711. (todir (file-name-directory absprefix))
  16712. (opt org-format-latex-options)
  16713. (optnew org-format-latex-options)
  16714. (matchers (plist-get opt :matchers))
  16715. (re-list org-latex-regexps)
  16716. (cnt 0) txt hash link beg end re e checkdir
  16717. string
  16718. m n block-type block linkfile movefile ov)
  16719. ;; Check the different regular expressions
  16720. (while (setq e (pop re-list))
  16721. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16722. block (if block-type "\n\n" ""))
  16723. (when (member m matchers)
  16724. (goto-char (point-min))
  16725. (while (re-search-forward re nil t)
  16726. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16727. (or (not overlays)
  16728. (not (eq (get-char-property (match-beginning n)
  16729. 'org-overlay-type)
  16730. 'org-latex-overlay))))
  16731. (cond
  16732. ((eq processing-type 'verbatim))
  16733. ((eq processing-type 'mathjax)
  16734. ;; Prepare for MathJax processing.
  16735. (setq string (match-string n))
  16736. (when (member m '("$" "$1"))
  16737. (save-excursion
  16738. (delete-region (match-beginning n) (match-end n))
  16739. (goto-char (match-beginning n))
  16740. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16741. ((or (eq processing-type 'dvipng)
  16742. (eq processing-type 'imagemagick))
  16743. ;; Process to an image.
  16744. (setq txt (match-string n)
  16745. beg (match-beginning n) end (match-end n)
  16746. cnt (1+ cnt))
  16747. (let ((face (face-at-point))
  16748. (fg (plist-get opt :foreground))
  16749. (bg (plist-get opt :background))
  16750. ;; Ensure full list is printed.
  16751. print-length print-level)
  16752. (when forbuffer
  16753. ;; Get the colors from the face at point.
  16754. (goto-char beg)
  16755. (when (eq fg 'auto)
  16756. (setq fg (face-attribute face :foreground nil 'default)))
  16757. (when (eq bg 'auto)
  16758. (setq bg (face-attribute face :background nil 'default)))
  16759. (setq optnew (copy-sequence opt))
  16760. (plist-put optnew :foreground fg)
  16761. (plist-put optnew :background bg))
  16762. (setq hash (sha1 (prin1-to-string
  16763. (list org-format-latex-header
  16764. org-latex-default-packages-alist
  16765. org-latex-packages-alist
  16766. org-format-latex-options
  16767. forbuffer txt fg bg)))
  16768. linkfile (format "%s_%s.png" prefix hash)
  16769. movefile (format "%s_%s.png" absprefix hash)))
  16770. (setq link (concat block "[[file:" linkfile "]]" block))
  16771. (if msg (message msg cnt))
  16772. (goto-char beg)
  16773. (unless checkdir ; Ensure the directory exists.
  16774. (setq checkdir t)
  16775. (or (file-directory-p todir) (make-directory todir t)))
  16776. (unless (file-exists-p movefile)
  16777. (org-create-formula-image
  16778. txt movefile optnew forbuffer processing-type))
  16779. (if overlays
  16780. (progn
  16781. (mapc (lambda (o)
  16782. (if (eq (overlay-get o 'org-overlay-type)
  16783. 'org-latex-overlay)
  16784. (delete-overlay o)))
  16785. (overlays-in beg end))
  16786. (setq ov (make-overlay beg end))
  16787. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16788. (if (featurep 'xemacs)
  16789. (progn
  16790. (overlay-put ov 'invisible t)
  16791. (overlay-put
  16792. ov 'end-glyph
  16793. (make-glyph (vector 'png :file movefile))))
  16794. (overlay-put
  16795. ov 'display
  16796. (list 'image :type 'png :file movefile :ascent 'center)))
  16797. (push ov org-latex-fragment-image-overlays)
  16798. (goto-char end))
  16799. (delete-region beg end)
  16800. (insert (org-add-props link
  16801. (list 'org-latex-src
  16802. (replace-regexp-in-string
  16803. "\"" "" txt)
  16804. 'org-latex-src-embed-type
  16805. (if block-type 'paragraph 'character))))))
  16806. ((eq processing-type 'mathml)
  16807. ;; Process to MathML
  16808. (unless (save-match-data (org-format-latex-mathml-available-p))
  16809. (user-error "LaTeX to MathML converter not configured"))
  16810. (setq txt (match-string n)
  16811. beg (match-beginning n) end (match-end n)
  16812. cnt (1+ cnt))
  16813. (if msg (message msg cnt))
  16814. (goto-char beg)
  16815. (delete-region beg end)
  16816. (insert (org-format-latex-as-mathml
  16817. txt block-type prefix dir)))
  16818. (t
  16819. (error "Unknown conversion type %s for LaTeX fragments"
  16820. processing-type)))))))))
  16821. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16822. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16823. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16824. sucessful, return the portion between \"<math...> </math>\"
  16825. elements otherwise return nil. When MATHML-FILE is specified,
  16826. write the results in to that file. When invoked as an
  16827. interactive command, prompt for LATEX-FRAG, with initial value
  16828. set to the current active region and echo the results for user
  16829. inspection."
  16830. (interactive (list (let ((frag (when (org-region-active-p)
  16831. (buffer-substring-no-properties
  16832. (region-beginning) (region-end)))))
  16833. (read-string "LaTeX Fragment: " frag nil frag))))
  16834. (unless latex-frag (error "Invalid LaTeX fragment"))
  16835. (let* ((tmp-in-file (file-relative-name
  16836. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16837. (ignore (write-region latex-frag nil tmp-in-file))
  16838. (tmp-out-file (file-relative-name
  16839. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16840. (cmd (format-spec
  16841. org-latex-to-mathml-convert-command
  16842. `((?j . ,(shell-quote-argument
  16843. (expand-file-name org-latex-to-mathml-jar-file)))
  16844. (?I . ,(shell-quote-argument tmp-in-file))
  16845. (?o . ,(shell-quote-argument tmp-out-file)))))
  16846. mathml shell-command-output)
  16847. (when (org-called-interactively-p 'any)
  16848. (unless (org-format-latex-mathml-available-p)
  16849. (user-error "LaTeX to MathML converter not configured")))
  16850. (message "Running %s" cmd)
  16851. (setq shell-command-output (shell-command-to-string cmd))
  16852. (setq mathml
  16853. (when (file-readable-p tmp-out-file)
  16854. (with-current-buffer (find-file-noselect tmp-out-file t)
  16855. (goto-char (point-min))
  16856. (when (re-search-forward
  16857. (concat
  16858. (regexp-quote
  16859. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16860. "\\(.\\|\n\\)*"
  16861. (regexp-quote "</math>")) nil t)
  16862. (prog1 (match-string 0) (kill-buffer))))))
  16863. (cond
  16864. (mathml
  16865. (setq mathml
  16866. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16867. (when mathml-file
  16868. (write-region mathml nil mathml-file))
  16869. (when (org-called-interactively-p 'any)
  16870. (message mathml)))
  16871. ((message "LaTeX to MathML conversion failed")
  16872. (message shell-command-output)))
  16873. (delete-file tmp-in-file)
  16874. (when (file-exists-p tmp-out-file)
  16875. (delete-file tmp-out-file))
  16876. mathml))
  16877. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16878. prefix &optional dir)
  16879. "Use `org-create-math-formula' but check local cache first."
  16880. (let* ((absprefix (expand-file-name prefix dir))
  16881. (print-length nil) (print-level nil)
  16882. (formula-id (concat
  16883. "formula-"
  16884. (sha1
  16885. (prin1-to-string
  16886. (list latex-frag
  16887. org-latex-to-mathml-convert-command)))))
  16888. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16889. (formula-cache-dir (file-name-directory formula-cache)))
  16890. (unless (file-directory-p formula-cache-dir)
  16891. (make-directory formula-cache-dir t))
  16892. (unless (file-exists-p formula-cache)
  16893. (org-create-math-formula latex-frag formula-cache))
  16894. (if (file-exists-p formula-cache)
  16895. ;; Successful conversion. Return the link to MathML file.
  16896. (org-add-props
  16897. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16898. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16899. 'org-latex-src-embed-type (if latex-frag-type
  16900. 'paragraph 'character)))
  16901. ;; Failed conversion. Return the LaTeX fragment verbatim
  16902. latex-frag)))
  16903. (defun org-create-formula-image (string tofile options buffer &optional type)
  16904. "Create an image from LaTeX source using dvipng or convert.
  16905. This function calls either `org-create-formula-image-with-dvipng'
  16906. or `org-create-formula-image-with-imagemagick' depending on the
  16907. value of `org-latex-create-formula-image-program' or on the value
  16908. of the optional TYPE variable.
  16909. Note: ultimately these two function should be combined as they
  16910. share a good deal of logic."
  16911. (org-check-external-command
  16912. "latex" "needed to convert LaTeX fragments to images")
  16913. (funcall
  16914. (case (or type org-latex-create-formula-image-program)
  16915. ('dvipng
  16916. (org-check-external-command
  16917. "dvipng" "needed to convert LaTeX fragments to images")
  16918. #'org-create-formula-image-with-dvipng)
  16919. ('imagemagick
  16920. (org-check-external-command
  16921. "convert" "you need to install imagemagick")
  16922. #'org-create-formula-image-with-imagemagick)
  16923. (t (error
  16924. "Invalid value of `org-latex-create-formula-image-program'")))
  16925. string tofile options buffer))
  16926. (declare-function org-export--get-global-options "ox" (&optional backend))
  16927. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16928. (defun org-create-formula--latex-header ()
  16929. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16930. (org-latex-guess-inputenc
  16931. (org-splice-latex-header
  16932. org-format-latex-header
  16933. org-latex-default-packages-alist
  16934. org-latex-packages-alist t
  16935. (plist-get
  16936. (org-combine-plists
  16937. (org-export--get-global-options 'latex)
  16938. (org-export--get-inbuffer-options 'latex))
  16939. :latex-header))))
  16940. ;; This function borrows from Ganesh Swami's latex2png.el
  16941. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16942. "This calls dvipng."
  16943. (require 'ox-latex)
  16944. (let* ((tmpdir (if (featurep 'xemacs)
  16945. (temp-directory)
  16946. temporary-file-directory))
  16947. (texfilebase (make-temp-name
  16948. (expand-file-name "orgtex" tmpdir)))
  16949. (texfile (concat texfilebase ".tex"))
  16950. (dvifile (concat texfilebase ".dvi"))
  16951. (pngfile (concat texfilebase ".png"))
  16952. (fnh (if (featurep 'xemacs)
  16953. (font-height (face-font 'default))
  16954. (face-attribute 'default :height nil)))
  16955. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16956. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16957. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16958. "Black"))
  16959. (bg (or (plist-get options (if buffer :background :html-background))
  16960. "Transparent")))
  16961. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16962. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16963. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16964. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16965. (let ((latex-header (org-create-formula--latex-header)))
  16966. (with-temp-file texfile
  16967. (insert latex-header)
  16968. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16969. (let ((dir default-directory))
  16970. (condition-case nil
  16971. (progn
  16972. (cd tmpdir)
  16973. (call-process "latex" nil nil nil texfile))
  16974. (error nil))
  16975. (cd dir))
  16976. (if (not (file-exists-p dvifile))
  16977. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16978. (condition-case nil
  16979. (if (featurep 'xemacs)
  16980. (call-process "dvipng" nil nil nil
  16981. "-fg" fg "-bg" bg
  16982. "-T" "tight"
  16983. "-o" pngfile
  16984. dvifile)
  16985. (call-process "dvipng" nil nil nil
  16986. "-fg" fg "-bg" bg
  16987. "-D" dpi
  16988. ;;"-x" scale "-y" scale
  16989. "-T" "tight"
  16990. "-o" pngfile
  16991. dvifile))
  16992. (error nil))
  16993. (if (not (file-exists-p pngfile))
  16994. (if org-format-latex-signal-error
  16995. (error "Failed to create png file from %s" texfile)
  16996. (message "Failed to create png file from %s" texfile)
  16997. nil)
  16998. ;; Use the requested file name and clean up
  16999. (copy-file pngfile tofile 'replace)
  17000. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17001. (if (file-exists-p (concat texfilebase e))
  17002. (delete-file (concat texfilebase e))))
  17003. pngfile))))
  17004. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17005. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17006. "This calls convert, which is included into imagemagick."
  17007. (require 'ox-latex)
  17008. (let* ((tmpdir (if (featurep 'xemacs)
  17009. (temp-directory)
  17010. temporary-file-directory))
  17011. (texfilebase (make-temp-name
  17012. (expand-file-name "orgtex" tmpdir)))
  17013. (texfile (concat texfilebase ".tex"))
  17014. (pdffile (concat texfilebase ".pdf"))
  17015. (pngfile (concat texfilebase ".png"))
  17016. (fnh (if (featurep 'xemacs)
  17017. (font-height (face-font 'default))
  17018. (face-attribute 'default :height nil)))
  17019. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17020. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17021. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17022. "black"))
  17023. (bg (or (plist-get options (if buffer :background :html-background))
  17024. "white")))
  17025. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17026. (setq fg (org-latex-color-format fg)))
  17027. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17028. (setq bg (org-latex-color-format
  17029. (if (string= bg "Transparent") "white" bg))))
  17030. (let ((latex-header (org-create-formula--latex-header)))
  17031. (with-temp-file texfile
  17032. (insert latex-header)
  17033. (insert "\n\\begin{document}\n"
  17034. "\\definecolor{fg}{rgb}{" fg "}\n"
  17035. "\\definecolor{bg}{rgb}{" bg "}\n"
  17036. "\n\\pagecolor{bg}\n"
  17037. "\n{\\color{fg}\n"
  17038. string
  17039. "\n}\n"
  17040. "\n\\end{document}\n")))
  17041. (org-latex-compile texfile t)
  17042. (if (not (file-exists-p pdffile))
  17043. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17044. (condition-case nil
  17045. (if (featurep 'xemacs)
  17046. (call-process "convert" nil nil nil
  17047. "-density" "96"
  17048. "-trim"
  17049. "-antialias"
  17050. pdffile
  17051. "-quality" "100"
  17052. ;; "-sharpen" "0x1.0"
  17053. pngfile)
  17054. (call-process "convert" nil nil nil
  17055. "-density" dpi
  17056. "-trim"
  17057. "-antialias"
  17058. pdffile
  17059. "-quality" "100"
  17060. ;; "-sharpen" "0x1.0"
  17061. pngfile))
  17062. (error nil))
  17063. (if (not (file-exists-p pngfile))
  17064. (if org-format-latex-signal-error
  17065. (error "Failed to create png file from %s" texfile)
  17066. (message "Failed to create png file from %s" texfile)
  17067. nil)
  17068. ;; Use the requested file name and clean up
  17069. (copy-file pngfile tofile 'replace)
  17070. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17071. (if (file-exists-p (concat texfilebase e))
  17072. (delete-file (concat texfilebase e))))
  17073. pngfile))))
  17074. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17075. "Fill a LaTeX header template TPL.
  17076. In the template, the following place holders will be recognized:
  17077. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17078. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17079. [PACKAGES] \\usepackage statements for PKG
  17080. [NO-PACKAGES] do not include PKG
  17081. [EXTRA] the string EXTRA
  17082. [NO-EXTRA] do not include EXTRA
  17083. For backward compatibility, if both the positive and the negative place
  17084. holder is missing, the positive one (without the \"NO-\") will be
  17085. assumed to be present at the end of the template.
  17086. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17087. EXTRA is a string.
  17088. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17089. (let (rpl (end ""))
  17090. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17091. (setq rpl (if (or (match-end 1) (not def-pkg))
  17092. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17093. tpl (replace-match rpl t t tpl))
  17094. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17095. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17096. (setq rpl (if (or (match-end 1) (not pkg))
  17097. "" (org-latex-packages-to-string pkg snippets-p t))
  17098. tpl (replace-match rpl t t tpl))
  17099. (if pkg (setq end
  17100. (concat end "\n"
  17101. (org-latex-packages-to-string pkg snippets-p)))))
  17102. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17103. (setq rpl (if (or (match-end 1) (not extra))
  17104. "" (concat extra "\n"))
  17105. tpl (replace-match rpl t t tpl))
  17106. (if (and extra (string-match "\\S-" extra))
  17107. (setq end (concat end "\n" extra))))
  17108. (if (string-match "\\S-" end)
  17109. (concat tpl "\n" end)
  17110. tpl)))
  17111. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17112. "Turn an alist of packages into a string with the \\usepackage macros."
  17113. (setq pkg (mapconcat (lambda(p)
  17114. (cond
  17115. ((stringp p) p)
  17116. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17117. (format "%% Package %s omitted" (cadr p)))
  17118. ((equal "" (car p))
  17119. (format "\\usepackage{%s}" (cadr p)))
  17120. (t
  17121. (format "\\usepackage[%s]{%s}"
  17122. (car p) (cadr p)))))
  17123. pkg
  17124. "\n"))
  17125. (if newline (concat pkg "\n") pkg))
  17126. (defun org-dvipng-color (attr)
  17127. "Return a RGB color specification for dvipng."
  17128. (apply 'format "rgb %s %s %s"
  17129. (mapcar 'org-normalize-color
  17130. (if (featurep 'xemacs)
  17131. (color-rgb-components
  17132. (face-property 'default
  17133. (cond ((eq attr :foreground) 'foreground)
  17134. ((eq attr :background) 'background))))
  17135. (color-values (face-attribute 'default attr nil))))))
  17136. (defun org-dvipng-color-format (color-name)
  17137. "Convert COLOR-NAME to a RGB color value for dvipng."
  17138. (apply 'format "rgb %s %s %s"
  17139. (mapcar 'org-normalize-color
  17140. (color-values color-name))))
  17141. (defun org-latex-color (attr)
  17142. "Return a RGB color for the LaTeX color package."
  17143. (apply 'format "%s,%s,%s"
  17144. (mapcar 'org-normalize-color
  17145. (if (featurep 'xemacs)
  17146. (color-rgb-components
  17147. (face-property 'default
  17148. (cond ((eq attr :foreground) 'foreground)
  17149. ((eq attr :background) 'background))))
  17150. (color-values (face-attribute 'default attr nil))))))
  17151. (defun org-latex-color-format (color-name)
  17152. "Convert COLOR-NAME to a RGB color value."
  17153. (apply 'format "%s,%s,%s"
  17154. (mapcar 'org-normalize-color
  17155. (color-values color-name))))
  17156. (defun org-normalize-color (value)
  17157. "Return string to be used as color value for an RGB component."
  17158. (format "%g" (/ value 65535.0)))
  17159. ;; Image display
  17160. (defvar org-inline-image-overlays nil)
  17161. (make-variable-buffer-local 'org-inline-image-overlays)
  17162. (defun org-toggle-inline-images (&optional include-linked)
  17163. "Toggle the display of inline images.
  17164. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17165. (interactive "P")
  17166. (if org-inline-image-overlays
  17167. (progn
  17168. (org-remove-inline-images)
  17169. (message "Inline image display turned off"))
  17170. (org-display-inline-images include-linked)
  17171. (if (and (org-called-interactively-p)
  17172. org-inline-image-overlays)
  17173. (message "%d images displayed inline"
  17174. (length org-inline-image-overlays))
  17175. (message "No images to display inline"))))
  17176. (defun org-redisplay-inline-images ()
  17177. "Refresh the display of inline images."
  17178. (interactive)
  17179. (if (not org-inline-image-overlays)
  17180. (org-toggle-inline-images)
  17181. (org-toggle-inline-images)
  17182. (org-toggle-inline-images)))
  17183. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17184. "Display inline images.
  17185. Normally only links without a description part are inlined, because this
  17186. is how it will work for export. When INCLUDE-LINKED is set, also links
  17187. with a description part will be inlined. This can be nice for a quick
  17188. look at those images, but it does not reflect what exported files will look
  17189. like.
  17190. When REFRESH is set, refresh existing images between BEG and END.
  17191. This will create new image displays only if necessary.
  17192. BEG and END default to the buffer boundaries."
  17193. (interactive "P")
  17194. (unless refresh
  17195. (org-remove-inline-images)
  17196. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17197. (save-excursion
  17198. (save-restriction
  17199. (widen)
  17200. (setq beg (or beg (point-min)) end (or end (point-max)))
  17201. (goto-char beg)
  17202. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17203. (substring (org-image-file-name-regexp) 0 -2)
  17204. "\\)\\]" (if include-linked "" "\\]")))
  17205. (case-fold-search t)
  17206. old file ov img type attrwidth width)
  17207. (while (re-search-forward re end t)
  17208. (setq old (get-char-property-and-overlay (match-beginning 1)
  17209. 'org-image-overlay)
  17210. file (expand-file-name
  17211. (concat (or (match-string 3) "") (match-string 4))))
  17212. (when (image-type-available-p 'imagemagick)
  17213. (setq attrwidth (if (or (listp org-image-actual-width)
  17214. (null org-image-actual-width))
  17215. (save-excursion
  17216. (save-match-data
  17217. (when (re-search-backward
  17218. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17219. (save-excursion
  17220. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17221. (string-to-number (match-string 1))))))
  17222. width (cond ((eq org-image-actual-width t) nil)
  17223. ((null org-image-actual-width) attrwidth)
  17224. ((numberp org-image-actual-width)
  17225. org-image-actual-width)
  17226. ((listp org-image-actual-width)
  17227. (or attrwidth (car org-image-actual-width))))
  17228. type (if width 'imagemagick)))
  17229. (when (file-exists-p file)
  17230. (if (and (car-safe old) refresh)
  17231. (image-refresh (overlay-get (cdr old) 'display))
  17232. (setq img (save-match-data (create-image file type nil :width width)))
  17233. (when img
  17234. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17235. (overlay-put ov 'display img)
  17236. (overlay-put ov 'face 'default)
  17237. (overlay-put ov 'org-image-overlay t)
  17238. (overlay-put ov 'modification-hooks
  17239. (list 'org-display-inline-remove-overlay))
  17240. (push ov org-inline-image-overlays)))))))))
  17241. (define-obsolete-function-alias
  17242. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17243. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17244. "Remove inline-display overlay if a corresponding region is modified."
  17245. (let ((inhibit-modification-hooks t))
  17246. (when (and ov after)
  17247. (delete ov org-inline-image-overlays)
  17248. (delete-overlay ov))))
  17249. (defun org-remove-inline-images ()
  17250. "Remove inline display of images."
  17251. (interactive)
  17252. (mapc 'delete-overlay org-inline-image-overlays)
  17253. (setq org-inline-image-overlays nil))
  17254. ;;;; Key bindings
  17255. ;; Outline functions from `outline-mode-prefix-map'
  17256. ;; that can be remapped in Org:
  17257. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17258. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17259. (define-key org-mode-map [remap outline-forward-same-level]
  17260. 'org-forward-heading-same-level)
  17261. (define-key org-mode-map [remap outline-backward-same-level]
  17262. 'org-backward-heading-same-level)
  17263. (define-key org-mode-map [remap show-branches]
  17264. 'org-kill-note-or-show-branches)
  17265. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17266. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17267. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17268. ;; Outline functions from `outline-mode-prefix-map' that can not
  17269. ;; be remapped in Org:
  17270. ;;
  17271. ;; - the column "key binding" shows whether the Outline function is still
  17272. ;; available in Org mode on the same key that it has been bound to in
  17273. ;; Outline mode:
  17274. ;; - "overridden": key used for a different functionality in Org mode
  17275. ;; - else: key still bound to the same Outline function in Org mode
  17276. ;;
  17277. ;; | Outline function | key binding | Org replacement |
  17278. ;; |------------------------------------+-------------+-----------------------|
  17279. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17280. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17281. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17282. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17283. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17284. ;; | `show-entry' | overridden | no replacement |
  17285. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17286. ;; | `show-branches' | `C-c C-k' | still same function |
  17287. ;; | `show-subtree' | overridden | visibility cycling |
  17288. ;; | `show-all' | overridden | no replacement |
  17289. ;; | `hide-subtree' | overridden | visibility cycling |
  17290. ;; | `hide-body' | overridden | no replacement |
  17291. ;; | `hide-entry' | overridden | visibility cycling |
  17292. ;; | `hide-leaves' | overridden | no replacement |
  17293. ;; | `hide-sublevels' | overridden | no replacement |
  17294. ;; | `hide-other' | overridden | no replacement |
  17295. ;; Make `C-c C-x' a prefix key
  17296. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17297. ;; TAB key with modifiers
  17298. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17299. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17300. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17301. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17302. ;; The following line is necessary under Suse GNU/Linux
  17303. (unless (featurep 'xemacs)
  17304. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17305. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17306. (define-key org-mode-map [backtab] 'org-shifttab)
  17307. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17308. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17309. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17310. ;; Cursor keys with modifiers
  17311. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17312. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17313. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17314. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17315. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17316. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17317. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17318. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17319. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17320. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17321. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17322. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17323. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17324. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17325. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17326. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17327. ;; Babel keys
  17328. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17329. (mapc (lambda (pair)
  17330. (define-key org-babel-map (car pair) (cdr pair)))
  17331. org-babel-key-bindings)
  17332. ;;; Extra keys for tty access.
  17333. ;; We only set them when really needed because otherwise the
  17334. ;; menus don't show the simple keys
  17335. (when (or org-use-extra-keys
  17336. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17337. (not window-system))
  17338. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17339. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17340. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17341. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17342. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17343. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17344. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17345. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17346. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17347. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17348. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17349. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17350. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17351. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17352. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17353. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17354. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17355. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17356. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17357. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17358. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17359. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17360. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17361. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17362. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17363. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17364. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17365. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17366. ;; All the other keys
  17367. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17368. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17369. (if (boundp 'narrow-map)
  17370. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17371. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17372. (if (boundp 'narrow-map)
  17373. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17374. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17375. (if (boundp 'narrow-map)
  17376. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17377. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17378. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17379. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17380. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17381. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17382. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17383. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17384. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17385. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17386. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17387. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17388. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17389. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17390. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17391. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17392. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17393. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17394. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17395. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17396. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17397. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17398. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17399. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17400. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17401. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17402. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17403. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17404. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17405. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17406. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17407. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17408. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17409. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17410. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17411. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17412. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17413. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17414. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17415. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17416. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17417. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17418. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17419. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17420. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17421. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17422. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17423. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17424. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17425. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17426. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17427. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17428. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17429. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17430. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17431. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17432. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17433. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17434. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17435. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17436. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17437. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17438. (org-defkey org-mode-map "\C-m" 'org-return)
  17439. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17440. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17441. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17442. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17443. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17444. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17445. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17446. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17447. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17448. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17449. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17450. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17451. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17452. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17453. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17454. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17455. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17456. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17457. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17458. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17459. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17460. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17461. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17462. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17463. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17466. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17478. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17479. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17480. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17481. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17482. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17483. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17484. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17485. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17486. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17487. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17488. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17489. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17490. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17491. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17492. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17493. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17494. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17495. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17496. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17497. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17498. (when (featurep 'xemacs)
  17499. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17500. (defconst org-speed-commands-default
  17501. '(
  17502. ("Outline Navigation")
  17503. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17504. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17505. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17506. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17507. ("F" . org-next-block)
  17508. ("B" . org-previous-block)
  17509. ("u" . (org-speed-move-safe 'outline-up-heading))
  17510. ("j" . org-goto)
  17511. ("g" . (org-refile t))
  17512. ("Outline Visibility")
  17513. ("c" . org-cycle)
  17514. ("C" . org-shifttab)
  17515. (" " . org-display-outline-path)
  17516. ("s" . org-narrow-to-subtree)
  17517. ("=" . org-columns)
  17518. ("Outline Structure Editing")
  17519. ("U" . org-shiftmetaup)
  17520. ("D" . org-shiftmetadown)
  17521. ("r" . org-metaright)
  17522. ("l" . org-metaleft)
  17523. ("R" . org-shiftmetaright)
  17524. ("L" . org-shiftmetaleft)
  17525. ("i" . (progn (forward-char 1) (call-interactively
  17526. 'org-insert-heading-respect-content)))
  17527. ("^" . org-sort)
  17528. ("w" . org-refile)
  17529. ("a" . org-archive-subtree-default-with-confirmation)
  17530. ("@" . org-mark-subtree)
  17531. ("#" . org-toggle-comment)
  17532. ("Clock Commands")
  17533. ("I" . org-clock-in)
  17534. ("O" . org-clock-out)
  17535. ("Meta Data Editing")
  17536. ("t" . org-todo)
  17537. ("," . (org-priority))
  17538. ("0" . (org-priority ?\ ))
  17539. ("1" . (org-priority ?A))
  17540. ("2" . (org-priority ?B))
  17541. ("3" . (org-priority ?C))
  17542. (":" . org-set-tags-command)
  17543. ("e" . org-set-effort)
  17544. ("E" . org-inc-effort)
  17545. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17546. (org-entry-put (point) "APPT_WARNTIME" m)))
  17547. ("Agenda Views etc")
  17548. ("v" . org-agenda)
  17549. ("/" . org-sparse-tree)
  17550. ("Misc")
  17551. ("o" . org-open-at-point)
  17552. ("?" . org-speed-command-help)
  17553. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17554. (">" . (org-agenda-remove-restriction-lock))
  17555. )
  17556. "The default speed commands.")
  17557. (defun org-print-speed-command (e)
  17558. (if (> (length (car e)) 1)
  17559. (progn
  17560. (princ "\n")
  17561. (princ (car e))
  17562. (princ "\n")
  17563. (princ (make-string (length (car e)) ?-))
  17564. (princ "\n"))
  17565. (princ (car e))
  17566. (princ " ")
  17567. (if (symbolp (cdr e))
  17568. (princ (symbol-name (cdr e)))
  17569. (prin1 (cdr e)))
  17570. (princ "\n")))
  17571. (defun org-speed-command-help ()
  17572. "Show the available speed commands."
  17573. (interactive)
  17574. (if (not org-use-speed-commands)
  17575. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17576. (with-output-to-temp-buffer "*Help*"
  17577. (princ "User-defined Speed commands\n===========================\n")
  17578. (mapc 'org-print-speed-command org-speed-commands-user)
  17579. (princ "\n")
  17580. (princ "Built-in Speed commands\n=======================\n")
  17581. (mapc 'org-print-speed-command org-speed-commands-default))
  17582. (with-current-buffer "*Help*"
  17583. (setq truncate-lines t))))
  17584. (defun org-speed-move-safe (cmd)
  17585. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17586. If not, return to the original position and throw an error."
  17587. (interactive)
  17588. (let ((pos (point)))
  17589. (call-interactively cmd)
  17590. (unless (and (bolp) (org-at-heading-p))
  17591. (goto-char pos)
  17592. (error "Boundary reached while executing %s" cmd))))
  17593. (defvar org-self-insert-command-undo-counter 0)
  17594. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17595. (defvar org-speed-command nil)
  17596. (define-obsolete-function-alias
  17597. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17598. (defun org-speed-command-activate (keys)
  17599. "Hook for activating single-letter speed commands.
  17600. `org-speed-commands-default' specifies a minimal command set.
  17601. Use `org-speed-commands-user' for further customization."
  17602. (when (or (and (bolp) (looking-at org-outline-regexp))
  17603. (and (functionp org-use-speed-commands)
  17604. (funcall org-use-speed-commands)))
  17605. (cdr (assoc keys (append org-speed-commands-user
  17606. org-speed-commands-default)))))
  17607. (define-obsolete-function-alias
  17608. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17609. (defun org-babel-speed-command-activate (keys)
  17610. "Hook for activating single-letter code block commands."
  17611. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17612. (cdr (assoc keys org-babel-key-bindings))))
  17613. (defcustom org-speed-command-hook
  17614. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17615. "Hook for activating speed commands at strategic locations.
  17616. Hook functions are called in sequence until a valid handler is
  17617. found.
  17618. Each hook takes a single argument, a user-pressed command key
  17619. which is also a `self-insert-command' from the global map.
  17620. Within the hook, examine the cursor position and the command key
  17621. and return nil or a valid handler as appropriate. Handler could
  17622. be one of an interactive command, a function, or a form.
  17623. Set `org-use-speed-commands' to non-nil value to enable this
  17624. hook. The default setting is `org-speed-command-activate'."
  17625. :group 'org-structure
  17626. :version "24.1"
  17627. :type 'hook)
  17628. (defun org-self-insert-command (N)
  17629. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17630. If the cursor is in a table looking at whitespace, the whitespace is
  17631. overwritten, and the table is not marked as requiring realignment."
  17632. (interactive "p")
  17633. (org-check-before-invisible-edit 'insert)
  17634. (cond
  17635. ((and org-use-speed-commands
  17636. (setq org-speed-command
  17637. (run-hook-with-args-until-success
  17638. 'org-speed-command-hook (this-command-keys))))
  17639. (cond
  17640. ((commandp org-speed-command)
  17641. (setq this-command org-speed-command)
  17642. (call-interactively org-speed-command))
  17643. ((functionp org-speed-command)
  17644. (funcall org-speed-command))
  17645. ((and org-speed-command (listp org-speed-command))
  17646. (eval org-speed-command))
  17647. (t (let (org-use-speed-commands)
  17648. (call-interactively 'org-self-insert-command)))))
  17649. ((and
  17650. (org-table-p)
  17651. (progn
  17652. ;; check if we blank the field, and if that triggers align
  17653. (and (featurep 'org-table) org-table-auto-blank-field
  17654. (member last-command
  17655. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17656. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17657. ;; got extra space, this field does not determine column width
  17658. (let (org-table-may-need-update) (org-table-blank-field))
  17659. ;; no extra space, this field may determine column width
  17660. (org-table-blank-field)))
  17661. t)
  17662. (eq N 1)
  17663. (looking-at "[^|\n]* |"))
  17664. (let (org-table-may-need-update)
  17665. (goto-char (1- (match-end 0)))
  17666. (backward-delete-char 1)
  17667. (goto-char (match-beginning 0))
  17668. (self-insert-command N)))
  17669. (t
  17670. (setq org-table-may-need-update t)
  17671. (self-insert-command N)
  17672. (org-fix-tags-on-the-fly)
  17673. (if org-self-insert-cluster-for-undo
  17674. (if (not (eq last-command 'org-self-insert-command))
  17675. (setq org-self-insert-command-undo-counter 1)
  17676. (if (>= org-self-insert-command-undo-counter 20)
  17677. (setq org-self-insert-command-undo-counter 1)
  17678. (and (> org-self-insert-command-undo-counter 0)
  17679. buffer-undo-list (listp buffer-undo-list)
  17680. (not (cadr buffer-undo-list)) ; remove nil entry
  17681. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17682. (setq org-self-insert-command-undo-counter
  17683. (1+ org-self-insert-command-undo-counter))))))))
  17684. (defun org-check-before-invisible-edit (kind)
  17685. "Check is editing if kind KIND would be dangerous with invisible text around.
  17686. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17687. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17688. (if (and org-catch-invisible-edits
  17689. (or (not (boundp 'visible-mode)) (not visible-mode))
  17690. (or (get-char-property (point) 'invisible)
  17691. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17692. ;; OK, we need to take a closer look
  17693. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17694. (invisible-before-point (if (bobp) nil (get-char-property
  17695. (1- (point)) 'invisible)))
  17696. (border-and-ok-direction
  17697. (or
  17698. ;; Check if we are acting predictably before invisible text
  17699. (and invisible-at-point (not invisible-before-point)
  17700. (memq kind '(insert delete-backward)))
  17701. ;; Check if we are acting predictably after invisible text
  17702. ;; This works not well, and I have turned it off. It seems
  17703. ;; better to always show and stop after invisible text.
  17704. ;; (and (not invisible-at-point) invisible-before-point
  17705. ;; (memq kind '(insert delete)))
  17706. )))
  17707. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17708. (memq invisible-before-point '(outline org-hide-block t)))
  17709. (if (eq org-catch-invisible-edits 'error)
  17710. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17711. (if (and org-custom-properties-overlays
  17712. (y-or-n-p "Display invisible properties in this buffer? "))
  17713. (org-toggle-custom-properties-visibility)
  17714. ;; Make the area visible
  17715. (save-excursion
  17716. (if invisible-before-point
  17717. (goto-char (previous-single-char-property-change
  17718. (point) 'invisible)))
  17719. (org-cycle))
  17720. (cond
  17721. ((eq org-catch-invisible-edits 'show)
  17722. ;; That's it, we do the edit after showing
  17723. (message
  17724. "Unfolding invisible region around point before editing")
  17725. (sit-for 1))
  17726. ((and (eq org-catch-invisible-edits 'smart)
  17727. border-and-ok-direction)
  17728. (message "Unfolding invisible region around point before editing"))
  17729. (t
  17730. ;; Don't do the edit, make the user repeat it in full visibility
  17731. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17732. (defun org-fix-tags-on-the-fly ()
  17733. (when (and (equal (char-after (point-at-bol)) ?*)
  17734. (org-at-heading-p))
  17735. (org-align-tags-here org-tags-column)))
  17736. (defun org-delete-backward-char (N)
  17737. "Like `delete-backward-char', insert whitespace at field end in tables.
  17738. When deleting backwards, in tables this function will insert whitespace in
  17739. front of the next \"|\" separator, to keep the table aligned. The table will
  17740. still be marked for re-alignment if the field did fill the entire column,
  17741. because, in this case the deletion might narrow the column."
  17742. (interactive "p")
  17743. (save-match-data
  17744. (org-check-before-invisible-edit 'delete-backward)
  17745. (if (and (org-table-p)
  17746. (eq N 1)
  17747. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17748. (looking-at ".*?|"))
  17749. (let ((pos (point))
  17750. (noalign (looking-at "[^|\n\r]* |"))
  17751. (c org-table-may-need-update))
  17752. (backward-delete-char N)
  17753. (if (not overwrite-mode)
  17754. (progn
  17755. (skip-chars-forward "^|")
  17756. (insert " ")
  17757. (goto-char (1- pos))))
  17758. ;; noalign: if there were two spaces at the end, this field
  17759. ;; does not determine the width of the column.
  17760. (if noalign (setq org-table-may-need-update c)))
  17761. (backward-delete-char N)
  17762. (org-fix-tags-on-the-fly))))
  17763. (defun org-delete-char (N)
  17764. "Like `delete-char', but insert whitespace at field end in tables.
  17765. When deleting characters, in tables this function will insert whitespace in
  17766. front of the next \"|\" separator, to keep the table aligned. The table will
  17767. still be marked for re-alignment if the field did fill the entire column,
  17768. because, in this case the deletion might narrow the column."
  17769. (interactive "p")
  17770. (save-match-data
  17771. (org-check-before-invisible-edit 'delete)
  17772. (if (and (org-table-p)
  17773. (not (bolp))
  17774. (not (= (char-after) ?|))
  17775. (eq N 1))
  17776. (if (looking-at ".*?|")
  17777. (let ((pos (point))
  17778. (noalign (looking-at "[^|\n\r]* |"))
  17779. (c org-table-may-need-update))
  17780. (replace-match
  17781. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17782. (goto-char pos)
  17783. ;; noalign: if there were two spaces at the end, this field
  17784. ;; does not determine the width of the column.
  17785. (if noalign (setq org-table-may-need-update c)))
  17786. (delete-char N))
  17787. (delete-char N)
  17788. (org-fix-tags-on-the-fly))))
  17789. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17790. (put 'org-self-insert-command 'delete-selection t)
  17791. (put 'orgtbl-self-insert-command 'delete-selection t)
  17792. (put 'org-delete-char 'delete-selection 'supersede)
  17793. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17794. (put 'org-yank 'delete-selection 'yank)
  17795. ;; Make `flyspell-mode' delay after some commands
  17796. (put 'org-self-insert-command 'flyspell-delayed t)
  17797. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17798. (put 'org-delete-char 'flyspell-delayed t)
  17799. (put 'org-delete-backward-char 'flyspell-delayed t)
  17800. ;; Make pabbrev-mode expand after org-mode commands
  17801. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17802. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17803. ;; How to do this: Measure non-white length of current string
  17804. ;; If equal to column width, we should realign.
  17805. (defun org-remap (map &rest commands)
  17806. "In MAP, remap the functions given in COMMANDS.
  17807. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17808. (let (new old)
  17809. (while commands
  17810. (setq old (pop commands) new (pop commands))
  17811. (if (fboundp 'command-remapping)
  17812. (org-defkey map (vector 'remap old) new)
  17813. (substitute-key-definition old new map global-map)))))
  17814. (defun org-transpose-words ()
  17815. "Transpose words for Org.
  17816. This uses the `org-mode-transpose-word-syntax-table' syntax
  17817. table, which interprets characters in `org-emphasis-alist' as
  17818. word constituants."
  17819. (interactive)
  17820. (with-syntax-table org-mode-transpose-word-syntax-table
  17821. (call-interactively 'transpose-words)))
  17822. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17823. (when (eq org-enable-table-editor 'optimized)
  17824. ;; If the user wants maximum table support, we need to hijack
  17825. ;; some standard editing functions
  17826. (org-remap org-mode-map
  17827. 'self-insert-command 'org-self-insert-command
  17828. 'delete-char 'org-delete-char
  17829. 'delete-backward-char 'org-delete-backward-char)
  17830. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17831. (defvar org-ctrl-c-ctrl-c-hook nil
  17832. "Hook for functions attaching themselves to `C-c C-c'.
  17833. This can be used to add additional functionality to the C-c C-c
  17834. key which executes context-dependent commands. This hook is run
  17835. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17836. run after the last test.
  17837. Each function will be called with no arguments. The function
  17838. must check if the context is appropriate for it to act. If yes,
  17839. it should do its thing and then return a non-nil value. If the
  17840. context is wrong, just do nothing and return nil.")
  17841. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17842. "Hook for functions attaching themselves to `C-c C-c'.
  17843. This can be used to add additional functionality to the C-c C-c
  17844. key which executes context-dependent commands. This hook is run
  17845. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17846. before the first test.
  17847. Each function will be called with no arguments. The function
  17848. must check if the context is appropriate for it to act. If yes,
  17849. it should do its thing and then return a non-nil value. If the
  17850. context is wrong, just do nothing and return nil.")
  17851. (defvar org-tab-first-hook nil
  17852. "Hook for functions to attach themselves to TAB.
  17853. See `org-ctrl-c-ctrl-c-hook' for more information.
  17854. This hook runs as the first action when TAB is pressed, even before
  17855. `org-cycle' messes around with the `outline-regexp' to cater for
  17856. inline tasks and plain list item folding.
  17857. If any function in this hook returns t, any other actions that
  17858. would have been caused by TAB (such as table field motion or visibility
  17859. cycling) will not occur.")
  17860. (defvar org-tab-after-check-for-table-hook nil
  17861. "Hook for functions to attach themselves to TAB.
  17862. See `org-ctrl-c-ctrl-c-hook' for more information.
  17863. This hook runs after it has been established that the cursor is not in a
  17864. table, but before checking if the cursor is in a headline or if global cycling
  17865. should be done.
  17866. If any function in this hook returns t, not other actions like visibility
  17867. cycling will be done.")
  17868. (defvar org-tab-after-check-for-cycling-hook nil
  17869. "Hook for functions to attach themselves to TAB.
  17870. See `org-ctrl-c-ctrl-c-hook' for more information.
  17871. This hook runs after it has been established that not table field motion and
  17872. not visibility should be done because of current context. This is probably
  17873. the place where a package like yasnippets can hook in.")
  17874. (defvar org-tab-before-tab-emulation-hook nil
  17875. "Hook for functions to attach themselves to TAB.
  17876. See `org-ctrl-c-ctrl-c-hook' for more information.
  17877. This hook runs after every other options for TAB have been exhausted, but
  17878. before indentation and \t insertion takes place.")
  17879. (defvar org-metaleft-hook nil
  17880. "Hook for functions attaching themselves to `M-left'.
  17881. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17882. (defvar org-metaright-hook nil
  17883. "Hook for functions attaching themselves to `M-right'.
  17884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17885. (defvar org-metaup-hook nil
  17886. "Hook for functions attaching themselves to `M-up'.
  17887. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17888. (defvar org-metadown-hook nil
  17889. "Hook for functions attaching themselves to `M-down'.
  17890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17891. (defvar org-shiftmetaleft-hook nil
  17892. "Hook for functions attaching themselves to `M-S-left'.
  17893. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17894. (defvar org-shiftmetaright-hook nil
  17895. "Hook for functions attaching themselves to `M-S-right'.
  17896. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17897. (defvar org-shiftmetaup-hook nil
  17898. "Hook for functions attaching themselves to `M-S-up'.
  17899. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17900. (defvar org-shiftmetadown-hook nil
  17901. "Hook for functions attaching themselves to `M-S-down'.
  17902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17903. (defvar org-metareturn-hook nil
  17904. "Hook for functions attaching themselves to `M-RET'.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17906. (defvar org-shiftup-hook nil
  17907. "Hook for functions attaching themselves to `S-up'.
  17908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17909. (defvar org-shiftup-final-hook nil
  17910. "Hook for functions attaching themselves to `S-up'.
  17911. This one runs after all other options except shift-select have been excluded.
  17912. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17913. (defvar org-shiftdown-hook nil
  17914. "Hook for functions attaching themselves to `S-down'.
  17915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17916. (defvar org-shiftdown-final-hook nil
  17917. "Hook for functions attaching themselves to `S-down'.
  17918. This one runs after all other options except shift-select have been excluded.
  17919. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17920. (defvar org-shiftleft-hook nil
  17921. "Hook for functions attaching themselves to `S-left'.
  17922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17923. (defvar org-shiftleft-final-hook nil
  17924. "Hook for functions attaching themselves to `S-left'.
  17925. This one runs after all other options except shift-select have been excluded.
  17926. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17927. (defvar org-shiftright-hook nil
  17928. "Hook for functions attaching themselves to `S-right'.
  17929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17930. (defvar org-shiftright-final-hook nil
  17931. "Hook for functions attaching themselves to `S-right'.
  17932. This one runs after all other options except shift-select have been excluded.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17934. (defun org-modifier-cursor-error ()
  17935. "Throw an error, a modified cursor command was applied in wrong context."
  17936. (user-error "This command is active in special context like tables, headlines or items"))
  17937. (defun org-shiftselect-error ()
  17938. "Throw an error because Shift-Cursor command was applied in wrong context."
  17939. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17940. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17941. (user-error "This command works only in special context like headlines or timestamps")))
  17942. (defun org-call-for-shift-select (cmd)
  17943. (let ((this-command-keys-shift-translated t))
  17944. (call-interactively cmd)))
  17945. (defun org-shifttab (&optional arg)
  17946. "Global visibility cycling or move to previous table field.
  17947. Call `org-table-previous-field' within a table.
  17948. When ARG is nil, cycle globally through visibility states.
  17949. When ARG is a numeric prefix, show contents of this level."
  17950. (interactive "P")
  17951. (cond
  17952. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17953. ((integerp arg)
  17954. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17955. (message "Content view to level: %d" arg)
  17956. (org-content (prefix-numeric-value arg2))
  17957. (org-cycle-show-empty-lines t)
  17958. (setq org-cycle-global-status 'overview)))
  17959. (t (call-interactively 'org-global-cycle))))
  17960. (defun org-shiftmetaleft ()
  17961. "Promote subtree or delete table column.
  17962. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17963. `org-table-delete-column', depending on context. See the
  17964. individual commands for more information."
  17965. (interactive)
  17966. (cond
  17967. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17968. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17969. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17970. ((if (not (org-region-active-p)) (org-at-item-p)
  17971. (save-excursion (goto-char (region-beginning))
  17972. (org-at-item-p)))
  17973. (call-interactively 'org-outdent-item-tree))
  17974. (t (org-modifier-cursor-error))))
  17975. (defun org-shiftmetaright ()
  17976. "Demote subtree or insert table column.
  17977. Calls `org-demote-subtree', `org-indent-item-tree', or
  17978. `org-table-insert-column', depending on context. See the
  17979. individual commands for more information."
  17980. (interactive)
  17981. (cond
  17982. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17983. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17984. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17985. ((if (not (org-region-active-p)) (org-at-item-p)
  17986. (save-excursion (goto-char (region-beginning))
  17987. (org-at-item-p)))
  17988. (call-interactively 'org-indent-item-tree))
  17989. (t (org-modifier-cursor-error))))
  17990. (defun org-shiftmetaup (&optional arg)
  17991. "Move subtree up or kill table row.
  17992. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17993. `org-move-item-up' or `org-timestamp-up', depending on context.
  17994. See the individual commands for more information."
  17995. (interactive "P")
  17996. (cond
  17997. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17998. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17999. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18000. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18001. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18002. (call-interactively 'org-timestamp-up)))
  18003. (t (call-interactively 'org-drag-line-backward))))
  18004. (defun org-shiftmetadown (&optional arg)
  18005. "Move subtree down or insert table row.
  18006. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18007. `org-move-item-down' or `org-timestamp-up', depending on context.
  18008. See the individual commands for more information."
  18009. (interactive "P")
  18010. (cond
  18011. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18012. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18013. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18014. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18015. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18016. (call-interactively 'org-timestamp-down)))
  18017. (t (call-interactively 'org-drag-line-forward))))
  18018. (defsubst org-hidden-tree-error ()
  18019. (user-error
  18020. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18021. (defun org-metaleft (&optional arg)
  18022. "Promote heading or move table column to left.
  18023. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18024. With no specific context, calls the Emacs default `backward-word'.
  18025. See the individual commands for more information."
  18026. (interactive "P")
  18027. (cond
  18028. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18029. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18030. ((org-with-limited-levels
  18031. (or (org-at-heading-p)
  18032. (and (org-region-active-p)
  18033. (save-excursion
  18034. (goto-char (region-beginning))
  18035. (org-at-heading-p)))))
  18036. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18037. (call-interactively 'org-do-promote))
  18038. ;; At an inline task.
  18039. ((org-at-heading-p)
  18040. (call-interactively 'org-inlinetask-promote))
  18041. ((or (org-at-item-p)
  18042. (and (org-region-active-p)
  18043. (save-excursion
  18044. (goto-char (region-beginning))
  18045. (org-at-item-p))))
  18046. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18047. (call-interactively 'org-outdent-item))
  18048. (t (call-interactively 'backward-word))))
  18049. (defun org-metaright (&optional arg)
  18050. "Demote a subtree, a list item or move table column to right.
  18051. In front of a drawer or a block keyword, indent it correctly.
  18052. With no specific context, calls the Emacs default `forward-word'.
  18053. See the individual commands for more information."
  18054. (interactive "P")
  18055. (cond
  18056. ((run-hook-with-args-until-success 'org-metaright-hook))
  18057. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18058. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18059. ((org-at-block-p) (call-interactively 'org-indent-block))
  18060. ((org-with-limited-levels
  18061. (or (org-at-heading-p)
  18062. (and (org-region-active-p)
  18063. (save-excursion
  18064. (goto-char (region-beginning))
  18065. (org-at-heading-p)))))
  18066. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18067. (call-interactively 'org-do-demote))
  18068. ;; At an inline task.
  18069. ((org-at-heading-p)
  18070. (call-interactively 'org-inlinetask-demote))
  18071. ((or (org-at-item-p)
  18072. (and (org-region-active-p)
  18073. (save-excursion
  18074. (goto-char (region-beginning))
  18075. (org-at-item-p))))
  18076. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18077. (call-interactively 'org-indent-item))
  18078. (t (call-interactively 'forward-word))))
  18079. (defun org-check-for-hidden (what)
  18080. "Check if there are hidden headlines/items in the current visual line.
  18081. WHAT can be either `headlines' or `items'. If the current line is
  18082. an outline or item heading and it has a folded subtree below it,
  18083. this function returns t, nil otherwise."
  18084. (let ((re (cond
  18085. ((eq what 'headlines) org-outline-regexp-bol)
  18086. ((eq what 'items) (org-item-beginning-re))
  18087. (t (error "This should not happen"))))
  18088. beg end)
  18089. (save-excursion
  18090. (catch 'exit
  18091. (unless (org-region-active-p)
  18092. (setq beg (point-at-bol))
  18093. (beginning-of-line 2)
  18094. (while (and (not (eobp)) ;; this is like `next-line'
  18095. (get-char-property (1- (point)) 'invisible))
  18096. (beginning-of-line 2))
  18097. (setq end (point))
  18098. (goto-char beg)
  18099. (goto-char (point-at-eol))
  18100. (setq end (max end (point)))
  18101. (while (re-search-forward re end t)
  18102. (if (get-char-property (match-beginning 0) 'invisible)
  18103. (throw 'exit t))))
  18104. nil))))
  18105. (defun org-metaup (&optional arg)
  18106. "Move subtree up or move table row up.
  18107. Calls `org-move-subtree-up' or `org-table-move-row' or
  18108. `org-move-item-up', depending on context. See the individual commands
  18109. for more information."
  18110. (interactive "P")
  18111. (cond
  18112. ((run-hook-with-args-until-success 'org-metaup-hook))
  18113. ((org-region-active-p)
  18114. (let* ((a (min (region-beginning) (region-end)))
  18115. (b (1- (max (region-beginning) (region-end))))
  18116. (c (save-excursion (goto-char a)
  18117. (move-beginning-of-line 0)))
  18118. (d (save-excursion (goto-char a)
  18119. (move-end-of-line 0) (point))))
  18120. (transpose-regions a b c d)
  18121. (goto-char c)))
  18122. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18123. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18124. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18125. (t (org-drag-element-backward))))
  18126. (defun org-metadown (&optional arg)
  18127. "Move subtree down or move table row down.
  18128. Calls `org-move-subtree-down' or `org-table-move-row' or
  18129. `org-move-item-down', depending on context. See the individual
  18130. commands for more information."
  18131. (interactive "P")
  18132. (cond
  18133. ((run-hook-with-args-until-success 'org-metadown-hook))
  18134. ((org-region-active-p)
  18135. (let* ((a (min (region-beginning) (region-end)))
  18136. (b (max (region-beginning) (region-end)))
  18137. (c (save-excursion (goto-char b)
  18138. (move-beginning-of-line 1)))
  18139. (d (save-excursion (goto-char b)
  18140. (move-end-of-line 1) (1+ (point)))))
  18141. (transpose-regions a b c d)
  18142. (goto-char d)))
  18143. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18144. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18145. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18146. (t (org-drag-element-forward))))
  18147. (defun org-shiftup (&optional arg)
  18148. "Increase item in timestamp or increase priority of current headline.
  18149. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18150. depending on context. See the individual commands for more information."
  18151. (interactive "P")
  18152. (cond
  18153. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18154. ((and org-support-shift-select (org-region-active-p))
  18155. (org-call-for-shift-select 'previous-line))
  18156. ((org-at-timestamp-p t)
  18157. (call-interactively (if org-edit-timestamp-down-means-later
  18158. 'org-timestamp-down 'org-timestamp-up)))
  18159. ((and (not (eq org-support-shift-select 'always))
  18160. org-enable-priority-commands
  18161. (org-at-heading-p))
  18162. (call-interactively 'org-priority-up))
  18163. ((and (not org-support-shift-select) (org-at-item-p))
  18164. (call-interactively 'org-previous-item))
  18165. ((org-clocktable-try-shift 'up arg))
  18166. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18167. (org-support-shift-select
  18168. (org-call-for-shift-select 'previous-line))
  18169. (t (org-shiftselect-error))))
  18170. (defun org-shiftdown (&optional arg)
  18171. "Decrease item in timestamp or decrease priority of current headline.
  18172. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18173. depending on context. See the individual commands for more information."
  18174. (interactive "P")
  18175. (cond
  18176. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18177. ((and org-support-shift-select (org-region-active-p))
  18178. (org-call-for-shift-select 'next-line))
  18179. ((org-at-timestamp-p t)
  18180. (call-interactively (if org-edit-timestamp-down-means-later
  18181. 'org-timestamp-up 'org-timestamp-down)))
  18182. ((and (not (eq org-support-shift-select 'always))
  18183. org-enable-priority-commands
  18184. (org-at-heading-p))
  18185. (call-interactively 'org-priority-down))
  18186. ((and (not org-support-shift-select) (org-at-item-p))
  18187. (call-interactively 'org-next-item))
  18188. ((org-clocktable-try-shift 'down arg))
  18189. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18190. (org-support-shift-select
  18191. (org-call-for-shift-select 'next-line))
  18192. (t (org-shiftselect-error))))
  18193. (defun org-shiftright (&optional arg)
  18194. "Cycle the thing at point or in the current line, depending on context.
  18195. Depending on context, this does one of the following:
  18196. - switch a timestamp at point one day into the future
  18197. - on a headline, switch to the next TODO keyword.
  18198. - on an item, switch entire list to the next bullet type
  18199. - on a property line, switch to the next allowed value
  18200. - on a clocktable definition line, move time block into the future"
  18201. (interactive "P")
  18202. (cond
  18203. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18204. ((and org-support-shift-select (org-region-active-p))
  18205. (org-call-for-shift-select 'forward-char))
  18206. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18207. ((and (not (eq org-support-shift-select 'always))
  18208. (org-at-heading-p))
  18209. (let ((org-inhibit-logging
  18210. (not org-treat-S-cursor-todo-selection-as-state-change))
  18211. (org-inhibit-blocking
  18212. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18213. (org-call-with-arg 'org-todo 'right)))
  18214. ((or (and org-support-shift-select
  18215. (not (eq org-support-shift-select 'always))
  18216. (org-at-item-bullet-p))
  18217. (and (not org-support-shift-select) (org-at-item-p)))
  18218. (org-call-with-arg 'org-cycle-list-bullet nil))
  18219. ((and (not (eq org-support-shift-select 'always))
  18220. (org-at-property-p))
  18221. (call-interactively 'org-property-next-allowed-value))
  18222. ((org-clocktable-try-shift 'right arg))
  18223. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18224. (org-support-shift-select
  18225. (org-call-for-shift-select 'forward-char))
  18226. (t (org-shiftselect-error))))
  18227. (defun org-shiftleft (&optional arg)
  18228. "Cycle the thing at point or in the current line, depending on context.
  18229. Depending on context, this does one of the following:
  18230. - switch a timestamp at point one day into the past
  18231. - on a headline, switch to the previous TODO keyword.
  18232. - on an item, switch entire list to the previous bullet type
  18233. - on a property line, switch to the previous allowed value
  18234. - on a clocktable definition line, move time block into the past"
  18235. (interactive "P")
  18236. (cond
  18237. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18238. ((and org-support-shift-select (org-region-active-p))
  18239. (org-call-for-shift-select 'backward-char))
  18240. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18241. ((and (not (eq org-support-shift-select 'always))
  18242. (org-at-heading-p))
  18243. (let ((org-inhibit-logging
  18244. (not org-treat-S-cursor-todo-selection-as-state-change))
  18245. (org-inhibit-blocking
  18246. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18247. (org-call-with-arg 'org-todo 'left)))
  18248. ((or (and org-support-shift-select
  18249. (not (eq org-support-shift-select 'always))
  18250. (org-at-item-bullet-p))
  18251. (and (not org-support-shift-select) (org-at-item-p)))
  18252. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18253. ((and (not (eq org-support-shift-select 'always))
  18254. (org-at-property-p))
  18255. (call-interactively 'org-property-previous-allowed-value))
  18256. ((org-clocktable-try-shift 'left arg))
  18257. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18258. (org-support-shift-select
  18259. (org-call-for-shift-select 'backward-char))
  18260. (t (org-shiftselect-error))))
  18261. (defun org-shiftcontrolright ()
  18262. "Switch to next TODO set."
  18263. (interactive)
  18264. (cond
  18265. ((and org-support-shift-select (org-region-active-p))
  18266. (org-call-for-shift-select 'forward-word))
  18267. ((and (not (eq org-support-shift-select 'always))
  18268. (org-at-heading-p))
  18269. (org-call-with-arg 'org-todo 'nextset))
  18270. (org-support-shift-select
  18271. (org-call-for-shift-select 'forward-word))
  18272. (t (org-shiftselect-error))))
  18273. (defun org-shiftcontrolleft ()
  18274. "Switch to previous TODO set."
  18275. (interactive)
  18276. (cond
  18277. ((and org-support-shift-select (org-region-active-p))
  18278. (org-call-for-shift-select 'backward-word))
  18279. ((and (not (eq org-support-shift-select 'always))
  18280. (org-at-heading-p))
  18281. (org-call-with-arg 'org-todo 'previousset))
  18282. (org-support-shift-select
  18283. (org-call-for-shift-select 'backward-word))
  18284. (t (org-shiftselect-error))))
  18285. (defun org-shiftcontrolup (&optional n)
  18286. "Change timestamps synchronously up in CLOCK log lines.
  18287. Optional argument N tells to change by that many units."
  18288. (interactive "P")
  18289. (cond ((and (not org-support-shift-select)
  18290. (org-at-clock-log-p)
  18291. (org-at-timestamp-p t))
  18292. (org-clock-timestamps-up n))
  18293. (t (org-shiftselect-error))))
  18294. (defun org-shiftcontroldown (&optional n)
  18295. "Change timestamps synchronously down in CLOCK log lines.
  18296. Optional argument N tells to change by that many units."
  18297. (interactive "P")
  18298. (cond ((and (not org-support-shift-select)
  18299. (org-at-clock-log-p)
  18300. (org-at-timestamp-p t))
  18301. (org-clock-timestamps-down n))
  18302. (t (org-shiftselect-error))))
  18303. (defun org-ctrl-c-ret ()
  18304. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18305. (interactive)
  18306. (cond
  18307. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18308. (t (call-interactively 'org-insert-heading))))
  18309. (defun org-find-visible ()
  18310. (let ((s (point)))
  18311. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18312. (get-char-property s 'invisible)))
  18313. s))
  18314. (defun org-find-invisible ()
  18315. (let ((s (point)))
  18316. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18317. (not (get-char-property s 'invisible))))
  18318. s))
  18319. (defun org-copy-visible (beg end)
  18320. "Copy the visible parts of the region."
  18321. (interactive "r")
  18322. (let (snippets s)
  18323. (save-excursion
  18324. (save-restriction
  18325. (narrow-to-region beg end)
  18326. (setq s (goto-char (point-min)))
  18327. (while (not (= (point) (point-max)))
  18328. (goto-char (org-find-invisible))
  18329. (push (buffer-substring s (point)) snippets)
  18330. (setq s (goto-char (org-find-visible))))))
  18331. (kill-new (apply 'concat (nreverse snippets)))))
  18332. (defun org-copy-special ()
  18333. "Copy region in table or copy current subtree.
  18334. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18335. See the individual commands for more information."
  18336. (interactive)
  18337. (call-interactively
  18338. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18339. (defun org-cut-special ()
  18340. "Cut region in table or cut current subtree.
  18341. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18342. See the individual commands for more information."
  18343. (interactive)
  18344. (call-interactively
  18345. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18346. (defun org-paste-special (arg)
  18347. "Paste rectangular region into table, or past subtree relative to level.
  18348. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18349. See the individual commands for more information."
  18350. (interactive "P")
  18351. (if (org-at-table-p)
  18352. (org-table-paste-rectangle)
  18353. (org-paste-subtree arg)))
  18354. (defsubst org-in-fixed-width-region-p ()
  18355. "Is point in a fixed-width region?"
  18356. (save-match-data
  18357. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18358. (defun org-edit-special (&optional arg)
  18359. "Call a special editor for the element at point.
  18360. When at a table, call the formula editor with `org-table-edit-formulas'.
  18361. When in a source code block, call `org-edit-src-code'.
  18362. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18363. When at an #+INCLUDE keyword, visit the included file.
  18364. On a link, call `ffap' to visit the link at point.
  18365. Otherwise, return a user error."
  18366. (interactive "P")
  18367. (let ((element (org-element-at-point)))
  18368. (assert (not buffer-read-only) nil
  18369. "Buffer is read-only: %s" (buffer-name))
  18370. (case (org-element-type element)
  18371. (src-block
  18372. (if (not arg) (org-edit-src-code)
  18373. (let* ((info (org-babel-get-src-block-info))
  18374. (lang (nth 0 info))
  18375. (params (nth 2 info))
  18376. (session (cdr (assq :session params))))
  18377. (if (not session) (org-edit-src-code)
  18378. ;; At a src-block with a session and function called with
  18379. ;; an ARG: switch to the buffer related to the inferior
  18380. ;; process.
  18381. (switch-to-buffer
  18382. (funcall (intern (concat "org-babel-prep-session:" lang))
  18383. session params))))))
  18384. (keyword
  18385. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18386. (find-file
  18387. (org-remove-double-quotes
  18388. (car (org-split-string (org-element-property :value element)))))
  18389. (user-error "No special environment to edit here")))
  18390. (table
  18391. (if (eq (org-element-property :type element) 'table.el)
  18392. (org-edit-src-code)
  18393. (call-interactively 'org-table-edit-formulas)))
  18394. ;; Only Org tables contain `table-row' type elements.
  18395. (table-row (call-interactively 'org-table-edit-formulas))
  18396. ((example-block export-block) (org-edit-src-code))
  18397. (fixed-width (org-edit-fixed-width-region))
  18398. (otherwise
  18399. ;; No notable element at point. Though, we may be at a link,
  18400. ;; which is an object. Thus, scan deeper.
  18401. (if (eq (org-element-type (org-element-context element)) 'link)
  18402. (call-interactively 'ffap)
  18403. (user-error "No special environment to edit here"))))))
  18404. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18405. (defun org-ctrl-c-ctrl-c (&optional arg)
  18406. "Set tags in headline, or update according to changed information at point.
  18407. This command does many different things, depending on context:
  18408. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18409. this is what we do.
  18410. - If the cursor is on a statistics cookie, update it.
  18411. - If the cursor is in a headline, prompt for tags and insert them
  18412. into the current line, aligned to `org-tags-column'. When called
  18413. with prefix arg, realign all tags in the current buffer.
  18414. - If the cursor is in one of the special #+KEYWORD lines, this
  18415. triggers scanning the buffer for these lines and updating the
  18416. information.
  18417. - If the cursor is inside a table, realign the table. This command
  18418. works even if the automatic table editor has been turned off.
  18419. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18420. the entire table.
  18421. - If the cursor is at a footnote reference or definition, jump to
  18422. the corresponding definition or references, respectively.
  18423. - If the cursor is a the beginning of a dynamic block, update it.
  18424. - If the current buffer is a capture buffer, close note and file it.
  18425. - If the cursor is on a <<<target>>>, update radio targets and
  18426. corresponding links in this buffer.
  18427. - If the cursor is on a numbered item in a plain list, renumber the
  18428. ordered list.
  18429. - If the cursor is on a checkbox, toggle it.
  18430. - If the cursor is on a code block, evaluate it. The variable
  18431. `org-confirm-babel-evaluate' can be used to control prompting
  18432. before code block evaluation, by default every code block
  18433. evaluation requires confirmation. Code block evaluation can be
  18434. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18435. (interactive "P")
  18436. (cond
  18437. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18438. org-occur-highlights
  18439. org-latex-fragment-image-overlays)
  18440. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18441. (org-remove-occur-highlights)
  18442. (org-remove-latex-fragment-image-overlays)
  18443. (message "Temporary highlights/overlays removed from current buffer"))
  18444. ((and (local-variable-p 'org-finish-function (current-buffer))
  18445. (fboundp org-finish-function))
  18446. (funcall org-finish-function))
  18447. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18448. (t
  18449. (let* ((context (org-element-context)) (type (org-element-type context)))
  18450. ;; Test if point is within a blank line.
  18451. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18452. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18453. (user-error "C-c C-c can do nothing useful at this location"))
  18454. ;; For convenience: at the first line of a paragraph on the
  18455. ;; same line as an item, apply function on that item instead.
  18456. (when (eq type 'paragraph)
  18457. (let ((parent (org-element-property :parent context)))
  18458. (when (and (eq (org-element-type parent) 'item)
  18459. (= (point-at-bol) (org-element-property :begin parent)))
  18460. (setq context parent type 'item))))
  18461. ;; Act according to type of element or object at point.
  18462. (case type
  18463. (clock (org-clock-update-time-maybe))
  18464. (dynamic-block
  18465. (save-excursion
  18466. (goto-char (org-element-property :post-affiliated context))
  18467. (org-update-dblock)))
  18468. (footnote-definition
  18469. (goto-char (org-element-property :post-affiliated context))
  18470. (call-interactively 'org-footnote-action))
  18471. (footnote-reference (call-interactively 'org-footnote-action))
  18472. ((headline inlinetask)
  18473. (save-excursion (goto-char (org-element-property :begin context))
  18474. (call-interactively 'org-set-tags)))
  18475. (item
  18476. ;; At an item: a double C-u set checkbox to "[-]"
  18477. ;; unconditionally, whereas a single one will toggle its
  18478. ;; presence. Without an universal argument, if the item
  18479. ;; has a checkbox, toggle it. Otherwise repair the list.
  18480. (let* ((box (org-element-property :checkbox context))
  18481. (struct (org-element-property :structure context))
  18482. (old-struct (copy-tree struct))
  18483. (parents (org-list-parents-alist struct))
  18484. (prevs (org-list-prevs-alist struct))
  18485. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18486. (org-list-set-checkbox
  18487. (org-element-property :begin context) struct
  18488. (cond ((equal arg '(16)) "[-]")
  18489. ((and (not box) (equal arg '(4))) "[ ]")
  18490. ((or (not box) (equal arg '(4))) nil)
  18491. ((eq box 'on) "[ ]")
  18492. (t "[X]")))
  18493. ;; Mimic `org-list-write-struct' but with grabbing
  18494. ;; a return value from `org-list-struct-fix-box'.
  18495. (org-list-struct-fix-ind struct parents 2)
  18496. (org-list-struct-fix-item-end struct)
  18497. (org-list-struct-fix-bul struct prevs)
  18498. (org-list-struct-fix-ind struct parents)
  18499. (let ((block-item
  18500. (org-list-struct-fix-box struct parents prevs orderedp)))
  18501. (if (and box (equal struct old-struct))
  18502. (if (equal arg '(16))
  18503. (message "Checkboxes already reset")
  18504. (user-error "Cannot toggle this checkbox: %s"
  18505. (if (eq box 'on)
  18506. "all subitems checked"
  18507. "unchecked subitems")))
  18508. (org-list-struct-apply-struct struct old-struct)
  18509. (org-update-checkbox-count-maybe))
  18510. (when block-item
  18511. (message "Checkboxes were removed due to empty box at line %d"
  18512. (org-current-line block-item))))))
  18513. (keyword
  18514. (let ((org-inhibit-startup-visibility-stuff t)
  18515. (org-startup-align-all-tables nil))
  18516. (when (boundp 'org-table-coordinate-overlays)
  18517. (mapc 'delete-overlay org-table-coordinate-overlays)
  18518. (setq org-table-coordinate-overlays nil))
  18519. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18520. (message "Local setup has been refreshed"))
  18521. (plain-list
  18522. ;; At a plain list, with a double C-u argument, set
  18523. ;; checkboxes of each item to "[-]", whereas a single one
  18524. ;; will toggle their presence according to the state of the
  18525. ;; first item in the list. Without an argument, repair the
  18526. ;; list.
  18527. (let* ((begin (org-element-property :contents-begin context))
  18528. (beginm (move-marker (make-marker) begin))
  18529. (struct (org-element-property :structure context))
  18530. (old-struct (copy-tree struct))
  18531. (first-box (save-excursion
  18532. (goto-char begin)
  18533. (looking-at org-list-full-item-re)
  18534. (match-string-no-properties 3)))
  18535. (new-box (cond ((equal arg '(16)) "[-]")
  18536. ((equal arg '(4)) (unless first-box "[ ]"))
  18537. ((equal first-box "[X]") "[ ]")
  18538. (t "[X]"))))
  18539. (cond
  18540. (arg
  18541. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18542. (org-list-get-all-items
  18543. begin struct (org-list-prevs-alist struct))))
  18544. ((and first-box (eq (point) begin))
  18545. ;; For convenience, when point is at bol on the first
  18546. ;; item of the list and no argument is provided, simply
  18547. ;; toggle checkbox of that item, if any.
  18548. (org-list-set-checkbox begin struct new-box)))
  18549. (org-list-write-struct
  18550. struct (org-list-parents-alist struct) old-struct)
  18551. (org-update-checkbox-count-maybe)
  18552. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18553. ((property-drawer node-property)
  18554. (call-interactively 'org-property-action))
  18555. ((radio-target target)
  18556. (call-interactively 'org-update-radio-target-regexp))
  18557. (statistics-cookie
  18558. (call-interactively 'org-update-statistics-cookies))
  18559. ((table table-cell table-row)
  18560. ;; At a table, recalculate every field and align it. Also
  18561. ;; send the table if necessary. If the table has
  18562. ;; a `table.el' type, just give up. At a table row or
  18563. ;; cell, maybe recalculate line but always align table.
  18564. (if (eq (org-element-property :type context) 'table.el)
  18565. (message "Use C-c ' to edit table.el tables")
  18566. (let ((org-enable-table-editor t))
  18567. (if (or (eq type 'table)
  18568. ;; Check if point is at a TBLFM line.
  18569. (and (eq type 'table-row)
  18570. (= (point) (org-element-property :end context))))
  18571. (save-excursion
  18572. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18573. (goto-char (org-element-property :contents-begin context))
  18574. (org-call-with-arg 'org-table-recalculate (or arg t))
  18575. (orgtbl-send-table 'maybe)))
  18576. (org-table-maybe-eval-formula)
  18577. (cond (arg (call-interactively 'org-table-recalculate))
  18578. ((org-table-maybe-recalculate-line))
  18579. (t (org-table-align)))))))
  18580. (timestamp (org-timestamp-change 0 'day))
  18581. (otherwise
  18582. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18583. (user-error
  18584. "C-c C-c can do nothing useful at this location")))))))))
  18585. (defun org-mode-restart ()
  18586. "Restart Org-mode, to scan again for special lines.
  18587. Also updates the keyword regular expressions."
  18588. (interactive)
  18589. (org-mode)
  18590. (message "Org-mode restarted"))
  18591. (defun org-kill-note-or-show-branches ()
  18592. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18593. (interactive)
  18594. (if (not org-finish-function)
  18595. (progn
  18596. (hide-subtree)
  18597. (call-interactively 'show-branches))
  18598. (let ((org-note-abort t))
  18599. (funcall org-finish-function))))
  18600. (defun org-open-line (n)
  18601. "Insert a new row in tables, call `open-line' elsewhere."
  18602. (interactive "*p")
  18603. (if (org-at-table-p)
  18604. (org-table-insert-row)
  18605. (open-line n)))
  18606. (defun org-return (&optional indent)
  18607. "Goto next table row or insert a newline.
  18608. Calls `org-table-next-row' or `newline', depending on context.
  18609. See the individual commands for more information."
  18610. (interactive)
  18611. (let (org-ts-what)
  18612. (cond
  18613. ((or (bobp) (org-in-src-block-p))
  18614. (if indent (newline-and-indent) (newline)))
  18615. ((org-at-table-p)
  18616. (org-table-justify-field-maybe)
  18617. (call-interactively 'org-table-next-row))
  18618. ;; when `newline-and-indent' is called within a list, make sure
  18619. ;; text moved stays inside the item.
  18620. ((and (org-in-item-p) indent)
  18621. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18622. (progn
  18623. (save-match-data (newline))
  18624. (org-indent-line-to (length (match-string 0))))
  18625. (let ((ind (org-get-indentation)))
  18626. (newline)
  18627. (if (org-looking-back org-list-end-re)
  18628. (org-indent-line)
  18629. (org-indent-line-to ind)))))
  18630. ((and org-return-follows-link
  18631. (org-at-timestamp-p t)
  18632. (not (eq org-ts-what 'after)))
  18633. (org-follow-timestamp-link))
  18634. ((and org-return-follows-link
  18635. (let ((tprop (get-text-property (point) 'face)))
  18636. (or (eq tprop 'org-link)
  18637. (and (listp tprop) (memq 'org-link tprop)))))
  18638. (call-interactively 'org-open-at-point))
  18639. ((and (org-at-heading-p)
  18640. (looking-at
  18641. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18642. (org-show-entry)
  18643. (end-of-line 1)
  18644. (newline))
  18645. (t (if indent (newline-and-indent) (newline))))))
  18646. (defun org-return-indent ()
  18647. "Goto next table row or insert a newline and indent.
  18648. Calls `org-table-next-row' or `newline-and-indent', depending on
  18649. context. See the individual commands for more information."
  18650. (interactive)
  18651. (org-return t))
  18652. (defun org-ctrl-c-star ()
  18653. "Compute table, or change heading status of lines.
  18654. Calls `org-table-recalculate' or `org-toggle-heading',
  18655. depending on context."
  18656. (interactive)
  18657. (cond
  18658. ((org-at-table-p)
  18659. (call-interactively 'org-table-recalculate))
  18660. (t
  18661. ;; Convert all lines in region to list items
  18662. (call-interactively 'org-toggle-heading))))
  18663. (defun org-ctrl-c-minus ()
  18664. "Insert separator line in table or modify bullet status of line.
  18665. Also turns a plain line or a region of lines into list items.
  18666. Calls `org-table-insert-hline', `org-toggle-item', or
  18667. `org-cycle-list-bullet', depending on context."
  18668. (interactive)
  18669. (cond
  18670. ((org-at-table-p)
  18671. (call-interactively 'org-table-insert-hline))
  18672. ((org-region-active-p)
  18673. (call-interactively 'org-toggle-item))
  18674. ((org-in-item-p)
  18675. (call-interactively 'org-cycle-list-bullet))
  18676. (t
  18677. (call-interactively 'org-toggle-item))))
  18678. (defun org-toggle-item (arg)
  18679. "Convert headings or normal lines to items, items to normal lines.
  18680. If there is no active region, only the current line is considered.
  18681. If the first non blank line in the region is a headline, convert
  18682. all headlines to items, shifting text accordingly.
  18683. If it is an item, convert all items to normal lines.
  18684. If it is normal text, change region into a list of items.
  18685. With a prefix argument ARG, change the region in a single item."
  18686. (interactive "P")
  18687. (let ((shift-text
  18688. (function
  18689. ;; Shift text in current section to IND, from point to END.
  18690. ;; The function leaves point to END line.
  18691. (lambda (ind end)
  18692. (let ((min-i 1000) (end (copy-marker end)))
  18693. ;; First determine the minimum indentation (MIN-I) of
  18694. ;; the text.
  18695. (save-excursion
  18696. (catch 'exit
  18697. (while (< (point) end)
  18698. (let ((i (org-get-indentation)))
  18699. (cond
  18700. ;; Skip blank lines and inline tasks.
  18701. ((looking-at "^[ \t]*$"))
  18702. ((looking-at org-outline-regexp-bol))
  18703. ;; We can't find less than 0 indentation.
  18704. ((zerop i) (throw 'exit (setq min-i 0)))
  18705. ((< i min-i) (setq min-i i))))
  18706. (forward-line))))
  18707. ;; Then indent each line so that a line indented to
  18708. ;; MIN-I becomes indented to IND. Ignore blank lines
  18709. ;; and inline tasks in the process.
  18710. (let ((delta (- ind min-i)))
  18711. (while (< (point) end)
  18712. (unless (or (looking-at "^[ \t]*$")
  18713. (looking-at org-outline-regexp-bol))
  18714. (org-indent-line-to (+ (org-get-indentation) delta)))
  18715. (forward-line)))))))
  18716. (skip-blanks
  18717. (function
  18718. ;; Return beginning of first non-blank line, starting from
  18719. ;; line at POS.
  18720. (lambda (pos)
  18721. (save-excursion
  18722. (goto-char pos)
  18723. (skip-chars-forward " \r\t\n")
  18724. (point-at-bol)))))
  18725. beg end)
  18726. ;; Determine boundaries of changes.
  18727. (if (org-region-active-p)
  18728. (setq beg (funcall skip-blanks (region-beginning))
  18729. end (copy-marker (region-end)))
  18730. (setq beg (funcall skip-blanks (point-at-bol))
  18731. end (copy-marker (point-at-eol))))
  18732. ;; Depending on the starting line, choose an action on the text
  18733. ;; between BEG and END.
  18734. (org-with-limited-levels
  18735. (save-excursion
  18736. (goto-char beg)
  18737. (cond
  18738. ;; Case 1. Start at an item: de-itemize. Note that it only
  18739. ;; happens when a region is active: `org-ctrl-c-minus'
  18740. ;; would call `org-cycle-list-bullet' otherwise.
  18741. ((org-at-item-p)
  18742. (while (< (point) end)
  18743. (when (org-at-item-p)
  18744. (skip-chars-forward " \t")
  18745. (delete-region (point) (match-end 0)))
  18746. (forward-line)))
  18747. ;; Case 2. Start at an heading: convert to items.
  18748. ((org-at-heading-p)
  18749. (let* ((bul (org-list-bullet-string "-"))
  18750. (bul-len (length bul))
  18751. ;; Indentation of the first heading. It should be
  18752. ;; relative to the indentation of its parent, if any.
  18753. (start-ind (save-excursion
  18754. (cond
  18755. ((not org-adapt-indentation) 0)
  18756. ((not (outline-previous-heading)) 0)
  18757. (t (length (match-string 0))))))
  18758. ;; Level of first heading. Further headings will be
  18759. ;; compared to it to determine hierarchy in the list.
  18760. (ref-level (org-reduced-level (org-outline-level))))
  18761. (while (< (point) end)
  18762. (let* ((level (org-reduced-level (org-outline-level)))
  18763. (delta (max 0 (- level ref-level))))
  18764. ;; If current headline is less indented than the first
  18765. ;; one, set it as reference, in order to preserve
  18766. ;; subtrees.
  18767. (when (< level ref-level) (setq ref-level level))
  18768. (replace-match bul t t)
  18769. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18770. ;; Ensure all text down to END (or SECTION-END) belongs
  18771. ;; to the newly created item.
  18772. (let ((section-end (save-excursion
  18773. (or (outline-next-heading) (point)))))
  18774. (forward-line)
  18775. (funcall shift-text
  18776. (+ start-ind (* (1+ delta) bul-len))
  18777. (min end section-end)))))))
  18778. ;; Case 3. Normal line with ARG: make the first line of region
  18779. ;; an item, and shift indentation of others lines to
  18780. ;; set them as item's body.
  18781. (arg (let* ((bul (org-list-bullet-string "-"))
  18782. (bul-len (length bul))
  18783. (ref-ind (org-get-indentation)))
  18784. (skip-chars-forward " \t")
  18785. (insert bul)
  18786. (forward-line)
  18787. (while (< (point) end)
  18788. ;; Ensure that lines less indented than first one
  18789. ;; still get included in item body.
  18790. (funcall shift-text
  18791. (+ ref-ind bul-len)
  18792. (min end (save-excursion (or (outline-next-heading)
  18793. (point)))))
  18794. (forward-line))))
  18795. ;; Case 4. Normal line without ARG: turn each non-item line
  18796. ;; into an item.
  18797. (t
  18798. (while (< (point) end)
  18799. (unless (or (org-at-heading-p) (org-at-item-p))
  18800. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18801. (replace-match
  18802. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18803. (forward-line))))))))
  18804. (defun org-toggle-heading (&optional nstars)
  18805. "Convert headings to normal text, or items or text to headings.
  18806. If there is no active region, only convert the current line.
  18807. With a \\[universal-argument] prefix, convert the whole list at
  18808. point into heading.
  18809. In a region:
  18810. - If the first non blank line is a headline, remove the stars
  18811. from all headlines in the region.
  18812. - If it is a normal line, turn each and every normal line (i.e.,
  18813. not an heading or an item) in the region into headings. If you
  18814. want to convert only the first line of this region, use one
  18815. universal prefix argument.
  18816. - If it is a plain list item, turn all plain list items into headings.
  18817. When converting a line into a heading, the number of stars is chosen
  18818. such that the lines become children of the current entry. However,
  18819. when a numeric prefix argument is given, its value determines the
  18820. number of stars to add."
  18821. (interactive "P")
  18822. (let ((skip-blanks
  18823. (function
  18824. ;; Return beginning of first non-blank line, starting from
  18825. ;; line at POS.
  18826. (lambda (pos)
  18827. (save-excursion
  18828. (goto-char pos)
  18829. (while (org-at-comment-p) (forward-line))
  18830. (skip-chars-forward " \r\t\n")
  18831. (point-at-bol)))))
  18832. beg end toggled)
  18833. ;; Determine boundaries of changes. If a universal prefix has
  18834. ;; been given, put the list in a region. If region ends at a bol,
  18835. ;; do not consider the last line to be in the region.
  18836. (when (and current-prefix-arg (org-at-item-p))
  18837. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18838. (org-mark-element))
  18839. (if (org-region-active-p)
  18840. (setq beg (funcall skip-blanks (region-beginning))
  18841. end (copy-marker (save-excursion
  18842. (goto-char (region-end))
  18843. (if (bolp) (point) (point-at-eol)))))
  18844. (setq beg (funcall skip-blanks (point-at-bol))
  18845. end (copy-marker (point-at-eol))))
  18846. ;; Ensure inline tasks don't count as headings.
  18847. (org-with-limited-levels
  18848. (save-excursion
  18849. (goto-char beg)
  18850. (cond
  18851. ;; Case 1. Started at an heading: de-star headings.
  18852. ((org-at-heading-p)
  18853. (while (< (point) end)
  18854. (when (org-at-heading-p t)
  18855. (looking-at org-outline-regexp) (replace-match "")
  18856. (setq toggled t))
  18857. (forward-line)))
  18858. ;; Case 2. Started at an item: change items into headlines.
  18859. ;; One star will be added by `org-list-to-subtree'.
  18860. ((org-at-item-p)
  18861. (let* ((stars (make-string
  18862. ;; subtract the star that will be added again by
  18863. ;; `org-list-to-subtree'
  18864. (if (numberp nstars) (1- nstars)
  18865. (or (org-current-level) 0))
  18866. ?*))
  18867. (add-stars
  18868. (cond (nstars "") ; stars from prefix only
  18869. ((equal stars "") "") ; before first heading
  18870. (org-odd-levels-only "*") ; inside heading, odd
  18871. (t "")))) ; inside heading, oddeven
  18872. (while (< (point) end)
  18873. (when (org-at-item-p)
  18874. ;; Pay attention to cases when region ends before list.
  18875. (let* ((struct (org-list-struct))
  18876. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18877. (save-restriction
  18878. (narrow-to-region (point) list-end)
  18879. (insert
  18880. (org-list-to-subtree
  18881. (org-list-parse-list t)
  18882. '(:istart (concat stars add-stars (funcall get-stars depth))
  18883. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18884. (setq toggled t))
  18885. (forward-line))))
  18886. ;; Case 3. Started at normal text: make every line an heading,
  18887. ;; skipping headlines and items.
  18888. (t (let* ((stars
  18889. (make-string
  18890. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18891. (add-stars
  18892. (cond (nstars "") ; stars from prefix only
  18893. ((equal stars "") "*") ; before first heading
  18894. (org-odd-levels-only "**") ; inside heading, odd
  18895. (t "*"))) ; inside heading, oddeven
  18896. (rpl (concat stars add-stars " "))
  18897. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18898. (while (< (point) (if (equal nstars '(4)) lend end))
  18899. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18900. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18901. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18902. (forward-line)))))))
  18903. (unless toggled (message "Cannot toggle heading from here"))))
  18904. (defun org-meta-return (&optional arg)
  18905. "Insert a new heading or wrap a region in a table.
  18906. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18907. See the individual commands for more information."
  18908. (interactive "P")
  18909. (cond
  18910. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18911. ((or (org-at-drawer-p) (org-at-property-p))
  18912. (newline-and-indent))
  18913. ((org-at-table-p)
  18914. (call-interactively 'org-table-wrap-region))
  18915. (t (call-interactively 'org-insert-heading))))
  18916. ;;; Menu entries
  18917. (defsubst org-in-subtree-not-table-p ()
  18918. "Are we in a subtree and not in a table?"
  18919. (and (not (org-before-first-heading-p))
  18920. (not (org-at-table-p))))
  18921. ;; Define the Org-mode menus
  18922. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18923. '("Tbl"
  18924. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18925. ["Next Field" org-cycle (org-at-table-p)]
  18926. ["Previous Field" org-shifttab (org-at-table-p)]
  18927. ["Next Row" org-return (org-at-table-p)]
  18928. "--"
  18929. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18930. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18931. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18932. "--"
  18933. ("Column"
  18934. ["Move Column Left" org-metaleft (org-at-table-p)]
  18935. ["Move Column Right" org-metaright (org-at-table-p)]
  18936. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18937. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18938. ("Row"
  18939. ["Move Row Up" org-metaup (org-at-table-p)]
  18940. ["Move Row Down" org-metadown (org-at-table-p)]
  18941. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18942. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18943. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18944. "--"
  18945. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18946. ("Rectangle"
  18947. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18948. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18949. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18950. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18951. "--"
  18952. ("Calculate"
  18953. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18954. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18955. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18956. "--"
  18957. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18958. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18959. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18960. "--"
  18961. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18962. "--"
  18963. ["Sum Column/Rectangle" org-table-sum
  18964. (or (org-at-table-p) (org-region-active-p))]
  18965. ["Which Column?" org-table-current-column (org-at-table-p)])
  18966. ["Debug Formulas"
  18967. org-table-toggle-formula-debugger
  18968. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18969. ["Show Col/Row Numbers"
  18970. org-table-toggle-coordinate-overlays
  18971. :style toggle
  18972. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18973. "--"
  18974. ["Create" org-table-create (and (not (org-at-table-p))
  18975. org-enable-table-editor)]
  18976. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18977. ["Import from File" org-table-import (not (org-at-table-p))]
  18978. ["Export to File" org-table-export (org-at-table-p)]
  18979. "--"
  18980. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18981. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18982. '("Org"
  18983. ("Show/Hide"
  18984. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18985. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18986. ["Sparse Tree..." org-sparse-tree t]
  18987. ["Reveal Context" org-reveal t]
  18988. ["Show All" show-all t]
  18989. "--"
  18990. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18991. "--"
  18992. ["New Heading" org-insert-heading t]
  18993. ("Navigate Headings"
  18994. ["Up" outline-up-heading t]
  18995. ["Next" outline-next-visible-heading t]
  18996. ["Previous" outline-previous-visible-heading t]
  18997. ["Next Same Level" outline-forward-same-level t]
  18998. ["Previous Same Level" outline-backward-same-level t]
  18999. "--"
  19000. ["Jump" org-goto t])
  19001. ("Edit Structure"
  19002. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19003. "--"
  19004. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19005. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19006. "--"
  19007. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19008. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19009. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19010. "--"
  19011. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19012. "--"
  19013. ["Copy visible text" org-copy-visible t]
  19014. "--"
  19015. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19016. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19017. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19018. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19019. "--"
  19020. ["Sort Region/Children" org-sort t]
  19021. "--"
  19022. ["Convert to odd levels" org-convert-to-odd-levels t]
  19023. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19024. ("Editing"
  19025. ["Emphasis..." org-emphasize t]
  19026. ["Edit Source Example" org-edit-special t]
  19027. "--"
  19028. ["Footnote new/jump" org-footnote-action t]
  19029. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19030. ("Archive"
  19031. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19032. "--"
  19033. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19034. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19035. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19036. )
  19037. "--"
  19038. ("Hyperlinks"
  19039. ["Store Link (Global)" org-store-link t]
  19040. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19041. ["Insert Link" org-insert-link t]
  19042. ["Follow Link" org-open-at-point t]
  19043. "--"
  19044. ["Next link" org-next-link t]
  19045. ["Previous link" org-previous-link t]
  19046. "--"
  19047. ["Descriptive Links"
  19048. org-toggle-link-display
  19049. :style radio
  19050. :selected org-descriptive-links
  19051. ]
  19052. ["Literal Links"
  19053. org-toggle-link-display
  19054. :style radio
  19055. :selected (not org-descriptive-links)])
  19056. "--"
  19057. ("TODO Lists"
  19058. ["TODO/DONE/-" org-todo t]
  19059. ("Select keyword"
  19060. ["Next keyword" org-shiftright (org-at-heading-p)]
  19061. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19062. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19063. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19064. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19065. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19066. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19067. "--"
  19068. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19069. :selected org-enforce-todo-dependencies :style toggle :active t]
  19070. "Settings for tree at point"
  19071. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19072. :selected (org-entry-get nil "ORDERED")
  19073. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19074. ["Do Children parallel" org-toggle-ordered-property :style radio
  19075. :selected (not (org-entry-get nil "ORDERED"))
  19076. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19077. "--"
  19078. ["Set Priority" org-priority t]
  19079. ["Priority Up" org-shiftup t]
  19080. ["Priority Down" org-shiftdown t]
  19081. "--"
  19082. ["Get news from all feeds" org-feed-update-all t]
  19083. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19084. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19085. ("TAGS and Properties"
  19086. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19087. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19088. "--"
  19089. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19090. ["Column view of properties" org-columns t]
  19091. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19092. ("Dates and Scheduling"
  19093. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19094. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19095. ("Change Date"
  19096. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19097. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19098. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19099. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19100. ["Compute Time Range" org-evaluate-time-range t]
  19101. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19102. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19103. "--"
  19104. ["Custom time format" org-toggle-time-stamp-overlays
  19105. :style radio :selected org-display-custom-times]
  19106. "--"
  19107. ["Goto Calendar" org-goto-calendar t]
  19108. ["Date from Calendar" org-date-from-calendar t]
  19109. "--"
  19110. ["Start/Restart Timer" org-timer-start t]
  19111. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19112. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19113. ["Insert Timer String" org-timer t]
  19114. ["Insert Timer Item" org-timer-item t])
  19115. ("Logging work"
  19116. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19117. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19118. ["Clock out" org-clock-out t]
  19119. ["Clock cancel" org-clock-cancel t]
  19120. "--"
  19121. ["Mark as default task" org-clock-mark-default-task t]
  19122. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19123. ["Goto running clock" org-clock-goto t]
  19124. "--"
  19125. ["Display times" org-clock-display t]
  19126. ["Create clock table" org-clock-report t]
  19127. "--"
  19128. ["Record DONE time"
  19129. (progn (setq org-log-done (not org-log-done))
  19130. (message "Switching to %s will %s record a timestamp"
  19131. (car org-done-keywords)
  19132. (if org-log-done "automatically" "not")))
  19133. :style toggle :selected org-log-done])
  19134. "--"
  19135. ["Agenda Command..." org-agenda t]
  19136. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19137. ("File List for Agenda")
  19138. ("Special views current file"
  19139. ["TODO Tree" org-show-todo-tree t]
  19140. ["Check Deadlines" org-check-deadlines t]
  19141. ["Timeline" org-timeline t]
  19142. ["Tags/Property tree" org-match-sparse-tree t])
  19143. "--"
  19144. ["Export/Publish..." org-export-dispatch t]
  19145. ("LaTeX"
  19146. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19147. :selected org-cdlatex-mode]
  19148. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19149. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19150. ["Modify math symbol" org-cdlatex-math-modify
  19151. (org-inside-LaTeX-fragment-p)]
  19152. ["Insert citation" org-reftex-citation t]
  19153. "--"
  19154. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19155. "--"
  19156. ("MobileOrg"
  19157. ["Push Files and Views" org-mobile-push t]
  19158. ["Get Captured and Flagged" org-mobile-pull t]
  19159. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19160. "--"
  19161. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19162. "--"
  19163. ("Documentation"
  19164. ["Show Version" org-version t]
  19165. ["Info Documentation" org-info t])
  19166. ("Customize"
  19167. ["Browse Org Group" org-customize t]
  19168. "--"
  19169. ["Expand This Menu" org-create-customize-menu
  19170. (fboundp 'customize-menu-create)])
  19171. ["Send bug report" org-submit-bug-report t]
  19172. "--"
  19173. ("Refresh/Reload"
  19174. ["Refresh setup current buffer" org-mode-restart t]
  19175. ["Reload Org (after update)" org-reload t]
  19176. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19177. ))
  19178. (defun org-info (&optional node)
  19179. "Read documentation for Org-mode in the info system.
  19180. With optional NODE, go directly to that node."
  19181. (interactive)
  19182. (info (format "(org)%s" (or node ""))))
  19183. ;;;###autoload
  19184. (defun org-submit-bug-report ()
  19185. "Submit a bug report on Org-mode via mail.
  19186. Don't hesitate to report any problems or inaccurate documentation.
  19187. If you don't have setup sending mail from (X)Emacs, please copy the
  19188. output buffer into your mail program, as it gives us important
  19189. information about your Org-mode version and configuration."
  19190. (interactive)
  19191. (require 'reporter)
  19192. (org-load-modules-maybe)
  19193. (org-require-autoloaded-modules)
  19194. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19195. (reporter-submit-bug-report
  19196. "emacs-orgmode@gnu.org"
  19197. (org-version nil 'full)
  19198. (let (list)
  19199. (save-window-excursion
  19200. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19201. (delete-other-windows)
  19202. (erase-buffer)
  19203. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19204. We would like to add your full Org-mode and Outline configuration to the
  19205. bug report. This greatly simplifies the work of the maintainer and
  19206. other experts on the mailing list.
  19207. HOWEVER, some variables you have customized may contain private
  19208. information. The names of customers, colleagues, or friends, might
  19209. appear in the form of file names, tags, todo states, or search strings.
  19210. If you answer yes to the prompt, you might want to check and remove
  19211. such private information before sending the email.")
  19212. (add-text-properties (point-min) (point-max) '(face org-warning))
  19213. (when (yes-or-no-p "Include your Org-mode configuration ")
  19214. (mapatoms
  19215. (lambda (v)
  19216. (and (boundp v)
  19217. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19218. (or (and (symbol-value v)
  19219. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19220. (and
  19221. (get v 'custom-type) (get v 'standard-value)
  19222. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19223. (push v list)))))
  19224. (kill-buffer (get-buffer "*Warn about privacy*"))
  19225. list))
  19226. nil nil
  19227. "Remember to cover the basics, that is, what you expected to happen and
  19228. what in fact did happen. You don't know how to make a good report? See
  19229. http://orgmode.org/manual/Feedback.html#Feedback
  19230. Your bug report will be posted to the Org-mode mailing list.
  19231. ------------------------------------------------------------------------")
  19232. (save-excursion
  19233. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19234. (replace-match "\\1Bug: \\3 [\\2]")))))
  19235. (defun org-install-agenda-files-menu ()
  19236. (let ((bl (buffer-list)))
  19237. (save-excursion
  19238. (while bl
  19239. (set-buffer (pop bl))
  19240. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19241. (when (derived-mode-p 'org-mode)
  19242. (easy-menu-change
  19243. '("Org") "File List for Agenda"
  19244. (append
  19245. (list
  19246. ["Edit File List" (org-edit-agenda-file-list) t]
  19247. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19248. ["Remove Current File from List" org-remove-file t]
  19249. ["Cycle through agenda files" org-cycle-agenda-files t]
  19250. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19251. "--")
  19252. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19253. ;;;; Documentation
  19254. (defun org-require-autoloaded-modules ()
  19255. (interactive)
  19256. (mapc 'require
  19257. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19258. org-remember org-table org-timer)))
  19259. ;;;###autoload
  19260. (defun org-reload (&optional uncompiled)
  19261. "Reload all org lisp files.
  19262. With prefix arg UNCOMPILED, load the uncompiled versions."
  19263. (interactive "P")
  19264. (require 'loadhist)
  19265. (let* ((org-dir (org-find-library-dir "org"))
  19266. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19267. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19268. (remove-re (mapconcat 'identity
  19269. (mapcar (lambda (f) (concat "^" f "$"))
  19270. (list (if (featurep 'xemacs)
  19271. "org-colview"
  19272. "org-colview-xemacs")
  19273. "org" "org-loaddefs" "org-version"))
  19274. "\\|"))
  19275. (feats (delete-dups
  19276. (mapcar 'file-name-sans-extension
  19277. (mapcar 'file-name-nondirectory
  19278. (delq nil
  19279. (mapcar 'feature-file
  19280. features))))))
  19281. (lfeat (append
  19282. (sort
  19283. (setq feats
  19284. (delq nil (mapcar
  19285. (lambda (f)
  19286. (if (and (string-match feature-re f)
  19287. (not (string-match remove-re f)))
  19288. f nil))
  19289. feats)))
  19290. 'string-lessp)
  19291. (list "org-version" "org")))
  19292. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19293. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19294. load-uncore load-misses)
  19295. (setq load-misses
  19296. (delq 't
  19297. (mapcar (lambda (f)
  19298. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19299. (and (string= org-dir contrib-dir)
  19300. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19301. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19302. (add-to-list 'load-uncore f 'append)
  19303. 't)
  19304. f))
  19305. lfeat)))
  19306. (if load-uncore
  19307. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19308. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19309. (if load-misses
  19310. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19311. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19312. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19313. ;;;###autoload
  19314. (defun org-customize ()
  19315. "Call the customize function with org as argument."
  19316. (interactive)
  19317. (org-load-modules-maybe)
  19318. (org-require-autoloaded-modules)
  19319. (customize-browse 'org))
  19320. (defun org-create-customize-menu ()
  19321. "Create a full customization menu for Org-mode, insert it into the menu."
  19322. (interactive)
  19323. (org-load-modules-maybe)
  19324. (org-require-autoloaded-modules)
  19325. (if (fboundp 'customize-menu-create)
  19326. (progn
  19327. (easy-menu-change
  19328. '("Org") "Customize"
  19329. `(["Browse Org group" org-customize t]
  19330. "--"
  19331. ,(customize-menu-create 'org)
  19332. ["Set" Custom-set t]
  19333. ["Save" Custom-save t]
  19334. ["Reset to Current" Custom-reset-current t]
  19335. ["Reset to Saved" Custom-reset-saved t]
  19336. ["Reset to Standard Settings" Custom-reset-standard t]))
  19337. (message "\"Org\"-menu now contains full customization menu"))
  19338. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19339. ;;;; Miscellaneous stuff
  19340. ;;; Generally useful functions
  19341. (defun org-get-at-bol (property)
  19342. "Get text property PROPERTY at beginning of line."
  19343. (get-text-property (point-at-bol) property))
  19344. (defun org-find-text-property-in-string (prop s)
  19345. "Return the first non-nil value of property PROP in string S."
  19346. (or (get-text-property 0 prop s)
  19347. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19348. prop s)))
  19349. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19350. "Display the given MESSAGE as a warning."
  19351. (if (fboundp 'display-warning)
  19352. (display-warning 'org message
  19353. (if (featurep 'xemacs) 'warning :warning))
  19354. (let ((buf (get-buffer-create "*Org warnings*")))
  19355. (with-current-buffer buf
  19356. (goto-char (point-max))
  19357. (insert "Warning (Org): " message)
  19358. (unless (bolp)
  19359. (newline)))
  19360. (display-buffer buf)
  19361. (sit-for 0))))
  19362. (defun org-eval (form)
  19363. "Eval FORM and return result."
  19364. (condition-case error
  19365. (eval form)
  19366. (error (format "%%![Error: %s]" error))))
  19367. (defun org-in-clocktable-p ()
  19368. "Check if the cursor is in a clocktable."
  19369. (let ((pos (point)) start)
  19370. (save-excursion
  19371. (end-of-line 1)
  19372. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19373. (setq start (match-beginning 0))
  19374. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19375. (>= (match-end 0) pos)
  19376. start))))
  19377. (defun org-in-commented-line ()
  19378. "Is point in a line starting with `#'?"
  19379. (equal (char-after (point-at-bol)) ?#))
  19380. (defun org-in-indented-comment-line ()
  19381. "Is point in a line starting with `#' after some white space?"
  19382. (save-excursion
  19383. (save-match-data
  19384. (goto-char (point-at-bol))
  19385. (looking-at "[ \t]*#"))))
  19386. (defun org-in-verbatim-emphasis ()
  19387. (save-match-data
  19388. (and (org-in-regexp org-emph-re 2)
  19389. (>= (point) (match-beginning 3))
  19390. (<= (point) (match-end 4))
  19391. (member (match-string 3) '("=" "~")))))
  19392. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19393. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19394. (if (and marker (marker-buffer marker)
  19395. (buffer-live-p (marker-buffer marker)))
  19396. (progn
  19397. (org-pop-to-buffer-same-window (marker-buffer marker))
  19398. (if (or (> marker (point-max)) (< marker (point-min)))
  19399. (widen))
  19400. (goto-char marker)
  19401. (org-show-context 'org-goto))
  19402. (if bookmark
  19403. (bookmark-jump bookmark)
  19404. (error "Cannot find location"))))
  19405. (defun org-quote-csv-field (s)
  19406. "Quote field for inclusion in CSV material."
  19407. (if (string-match "[\",]" s)
  19408. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19409. s))
  19410. (defun org-force-self-insert (N)
  19411. "Needed to enforce self-insert under remapping."
  19412. (interactive "p")
  19413. (self-insert-command N))
  19414. (defun org-string-width (s)
  19415. "Compute width of string, ignoring invisible characters.
  19416. This ignores character with invisibility property `org-link', and also
  19417. characters with property `org-cwidth', because these will become invisible
  19418. upon the next fontification round."
  19419. (let (b l)
  19420. (when (or (eq t buffer-invisibility-spec)
  19421. (assq 'org-link buffer-invisibility-spec))
  19422. (while (setq b (text-property-any 0 (length s)
  19423. 'invisible 'org-link s))
  19424. (setq s (concat (substring s 0 b)
  19425. (substring s (or (next-single-property-change
  19426. b 'invisible s) (length s)))))))
  19427. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19428. (setq s (concat (substring s 0 b)
  19429. (substring s (or (next-single-property-change
  19430. b 'org-cwidth s) (length s))))))
  19431. (setq l (string-width s) b -1)
  19432. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19433. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19434. l))
  19435. (defun org-shorten-string (s maxlength)
  19436. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19437. If the string is shorter or has length MAXLENGTH, just return the
  19438. original string. If it is longer, the functions finds a space in the
  19439. string, breaks this string off at that locations and adds three dots
  19440. as ellipsis. Including the ellipsis, the string will not be longer
  19441. than MAXLENGTH. If finding a good breaking point in the string does
  19442. not work, the string is just chopped off in the middle of a word
  19443. if necessary."
  19444. (if (<= (length s) maxlength)
  19445. s
  19446. (let* ((n (max (- maxlength 4) 1))
  19447. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19448. (if (string-match re s)
  19449. (concat (match-string 1 s) "...")
  19450. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19451. (defun org-get-indentation (&optional line)
  19452. "Get the indentation of the current line, interpreting tabs.
  19453. When LINE is given, assume it represents a line and compute its indentation."
  19454. (if line
  19455. (if (string-match "^ *" (org-remove-tabs line))
  19456. (match-end 0))
  19457. (save-excursion
  19458. (beginning-of-line 1)
  19459. (skip-chars-forward " \t")
  19460. (current-column))))
  19461. (defun org-get-string-indentation (s)
  19462. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19463. (let ((n -1) (i 0) (w tab-width) c)
  19464. (catch 'exit
  19465. (while (< (setq n (1+ n)) (length s))
  19466. (setq c (aref s n))
  19467. (cond ((= c ?\ ) (setq i (1+ i)))
  19468. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19469. (t (throw 'exit t)))))
  19470. i))
  19471. (defun org-remove-tabs (s &optional width)
  19472. "Replace tabulators in S with spaces.
  19473. Assumes that s is a single line, starting in column 0."
  19474. (setq width (or width tab-width))
  19475. (while (string-match "\t" s)
  19476. (setq s (replace-match
  19477. (make-string
  19478. (- (* width (/ (+ (match-beginning 0) width) width))
  19479. (match-beginning 0)) ?\ )
  19480. t t s)))
  19481. s)
  19482. (defun org-fix-indentation (line ind)
  19483. "Fix indentation in LINE.
  19484. IND is a cons cell with target and minimum indentation.
  19485. If the current indentation in LINE is smaller than the minimum,
  19486. leave it alone. If it is larger than ind, set it to the target."
  19487. (let* ((l (org-remove-tabs line))
  19488. (i (org-get-indentation l))
  19489. (i1 (car ind)) (i2 (cdr ind)))
  19490. (if (>= i i2) (setq l (substring line i2)))
  19491. (if (> i1 0)
  19492. (concat (make-string i1 ?\ ) l)
  19493. l)))
  19494. (defun org-remove-indentation (code &optional n)
  19495. "Remove the maximum common indentation from the lines in CODE.
  19496. N may optionally be the number of spaces to remove."
  19497. (with-temp-buffer
  19498. (insert code)
  19499. (org-do-remove-indentation n)
  19500. (buffer-string)))
  19501. (defun org-do-remove-indentation (&optional n)
  19502. "Remove the maximum common indentation from the buffer."
  19503. (untabify (point-min) (point-max))
  19504. (let ((min 10000) re)
  19505. (if n
  19506. (setq min n)
  19507. (goto-char (point-min))
  19508. (while (re-search-forward "^ *[^ \n]" nil t)
  19509. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19510. (unless (or (= min 0) (= min 10000))
  19511. (setq re (format "^ \\{%d\\}" min))
  19512. (goto-char (point-min))
  19513. (while (re-search-forward re nil t)
  19514. (replace-match "")
  19515. (end-of-line 1))
  19516. min)))
  19517. (defun org-fill-template (template alist)
  19518. "Find each %key of ALIST in TEMPLATE and replace it."
  19519. (let ((case-fold-search nil)
  19520. entry key value)
  19521. (setq alist (sort (copy-sequence alist)
  19522. (lambda (a b) (< (length (car a)) (length (car b))))))
  19523. (while (setq entry (pop alist))
  19524. (setq template
  19525. (replace-regexp-in-string
  19526. (concat "%" (regexp-quote (car entry)))
  19527. (or (cdr entry) "") template t t)))
  19528. template))
  19529. (defun org-base-buffer (buffer)
  19530. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19531. (if (not buffer)
  19532. buffer
  19533. (or (buffer-base-buffer buffer)
  19534. buffer)))
  19535. (defun org-trim (s)
  19536. "Remove whitespace at beginning and end of string."
  19537. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19538. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19539. s)
  19540. (defun org-wrap (string &optional width lines)
  19541. "Wrap string to either a number of lines, or a width in characters.
  19542. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19543. that costs. If there is a word longer than WIDTH, the text is actually
  19544. wrapped to the length of that word.
  19545. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19546. many lines, whatever width that takes.
  19547. The return value is a list of lines, without newlines at the end."
  19548. (let* ((words (org-split-string string "[ \t\n]+"))
  19549. (maxword (apply 'max (mapcar 'org-string-width words)))
  19550. w ll)
  19551. (cond (width
  19552. (org-do-wrap words (max maxword width)))
  19553. (lines
  19554. (setq w maxword)
  19555. (setq ll (org-do-wrap words maxword))
  19556. (if (<= (length ll) lines)
  19557. ll
  19558. (setq ll words)
  19559. (while (> (length ll) lines)
  19560. (setq w (1+ w))
  19561. (setq ll (org-do-wrap words w)))
  19562. ll))
  19563. (t (error "Cannot wrap this")))))
  19564. (defun org-do-wrap (words width)
  19565. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19566. (let (lines line)
  19567. (while words
  19568. (setq line (pop words))
  19569. (while (and words (< (+ (length line) (length (car words))) width))
  19570. (setq line (concat line " " (pop words))))
  19571. (setq lines (push line lines)))
  19572. (nreverse lines)))
  19573. (defun org-split-string (string &optional separators)
  19574. "Splits STRING into substrings at SEPARATORS.
  19575. No empty strings are returned if there are matches at the beginning
  19576. and end of string."
  19577. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19578. (start 0)
  19579. notfirst
  19580. (list nil))
  19581. (while (and (string-match rexp string
  19582. (if (and notfirst
  19583. (= start (match-beginning 0))
  19584. (< start (length string)))
  19585. (1+ start) start))
  19586. (< (match-beginning 0) (length string)))
  19587. (setq notfirst t)
  19588. (or (eq (match-beginning 0) 0)
  19589. (and (eq (match-beginning 0) (match-end 0))
  19590. (eq (match-beginning 0) start))
  19591. (setq list
  19592. (cons (substring string start (match-beginning 0))
  19593. list)))
  19594. (setq start (match-end 0)))
  19595. (or (eq start (length string))
  19596. (setq list
  19597. (cons (substring string start)
  19598. list)))
  19599. (nreverse list)))
  19600. (defun org-quote-vert (s)
  19601. "Replace \"|\" with \"\\vert\"."
  19602. (while (string-match "|" s)
  19603. (setq s (replace-match "\\vert" t t s)))
  19604. s)
  19605. (defun org-uuidgen-p (s)
  19606. "Is S an ID created by UUIDGEN?"
  19607. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19608. (defun org-in-src-block-p (&optional inside)
  19609. "Whether point is in a code source block.
  19610. When INSIDE is non-nil, don't consider we are within a src block
  19611. when point is at #+BEGIN_SRC or #+END_SRC."
  19612. (let ((case-fold-search t) ov)
  19613. (or (and (setq ov (overlays-at (point)))
  19614. (memq 'org-block-background
  19615. (overlay-properties (car ov))))
  19616. (and (not inside)
  19617. (save-match-data
  19618. (save-excursion
  19619. (beginning-of-line)
  19620. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19621. (defun org-context ()
  19622. "Return a list of contexts of the current cursor position.
  19623. If several contexts apply, all are returned.
  19624. Each context entry is a list with a symbol naming the context, and
  19625. two positions indicating start and end of the context. Possible
  19626. contexts are:
  19627. :headline anywhere in a headline
  19628. :headline-stars on the leading stars in a headline
  19629. :todo-keyword on a TODO keyword (including DONE) in a headline
  19630. :tags on the TAGS in a headline
  19631. :priority on the priority cookie in a headline
  19632. :item on the first line of a plain list item
  19633. :item-bullet on the bullet/number of a plain list item
  19634. :checkbox on the checkbox in a plain list item
  19635. :table in an org-mode table
  19636. :table-special on a special filed in a table
  19637. :table-table in a table.el table
  19638. :clocktable in a clocktable
  19639. :src-block in a source block
  19640. :link on a hyperlink
  19641. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19642. :target on a <<target>>
  19643. :radio-target on a <<<radio-target>>>
  19644. :latex-fragment on a LaTeX fragment
  19645. :latex-preview on a LaTeX fragment with overlaid preview image
  19646. This function expects the position to be visible because it uses font-lock
  19647. faces as a help to recognize the following contexts: :table-special, :link,
  19648. and :keyword."
  19649. (let* ((f (get-text-property (point) 'face))
  19650. (faces (if (listp f) f (list f)))
  19651. (case-fold-search t)
  19652. (p (point)) clist o)
  19653. ;; First the large context
  19654. (cond
  19655. ((org-at-heading-p t)
  19656. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19657. (when (progn
  19658. (beginning-of-line 1)
  19659. (looking-at org-todo-line-tags-regexp))
  19660. (push (org-point-in-group p 1 :headline-stars) clist)
  19661. (push (org-point-in-group p 2 :todo-keyword) clist)
  19662. (push (org-point-in-group p 4 :tags) clist))
  19663. (goto-char p)
  19664. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19665. (if (looking-at "\\[#[A-Z0-9]\\]")
  19666. (push (org-point-in-group p 0 :priority) clist)))
  19667. ((org-at-item-p)
  19668. (push (org-point-in-group p 2 :item-bullet) clist)
  19669. (push (list :item (point-at-bol)
  19670. (save-excursion (org-end-of-item) (point)))
  19671. clist)
  19672. (and (org-at-item-checkbox-p)
  19673. (push (org-point-in-group p 0 :checkbox) clist)))
  19674. ((org-at-table-p)
  19675. (push (list :table (org-table-begin) (org-table-end)) clist)
  19676. (if (memq 'org-formula faces)
  19677. (push (list :table-special
  19678. (previous-single-property-change p 'face)
  19679. (next-single-property-change p 'face)) clist)))
  19680. ((org-at-table-p 'any)
  19681. (push (list :table-table) clist)))
  19682. (goto-char p)
  19683. (let ((case-fold-search t))
  19684. ;; New the "medium" contexts: clocktables, source blocks
  19685. (cond ((org-in-clocktable-p)
  19686. (push (list :clocktable
  19687. (and (or (looking-at "#\\+BEGIN: clocktable")
  19688. (search-backward "#+BEGIN: clocktable" nil t))
  19689. (match-beginning 0))
  19690. (and (re-search-forward "#\\+END:?" nil t)
  19691. (match-end 0))) clist))
  19692. ((org-in-src-block-p)
  19693. (push (list :src-block
  19694. (and (or (looking-at "#\\+BEGIN_SRC")
  19695. (search-backward "#+BEGIN_SRC" nil t))
  19696. (match-beginning 0))
  19697. (and (search-forward "#+END_SRC" nil t)
  19698. (match-beginning 0))) clist))))
  19699. (goto-char p)
  19700. ;; Now the small context
  19701. (cond
  19702. ((org-at-timestamp-p)
  19703. (push (org-point-in-group p 0 :timestamp) clist))
  19704. ((memq 'org-link faces)
  19705. (push (list :link
  19706. (previous-single-property-change p 'face)
  19707. (next-single-property-change p 'face)) clist))
  19708. ((memq 'org-special-keyword faces)
  19709. (push (list :keyword
  19710. (previous-single-property-change p 'face)
  19711. (next-single-property-change p 'face)) clist))
  19712. ((org-at-target-p)
  19713. (push (org-point-in-group p 0 :target) clist)
  19714. (goto-char (1- (match-beginning 0)))
  19715. (if (looking-at org-radio-target-regexp)
  19716. (push (org-point-in-group p 0 :radio-target) clist))
  19717. (goto-char p))
  19718. ((setq o (car (delq nil
  19719. (mapcar
  19720. (lambda (x)
  19721. (if (memq x org-latex-fragment-image-overlays) x))
  19722. (overlays-at (point))))))
  19723. (push (list :latex-fragment
  19724. (overlay-start o) (overlay-end o)) clist)
  19725. (push (list :latex-preview
  19726. (overlay-start o) (overlay-end o)) clist))
  19727. ((org-inside-LaTeX-fragment-p)
  19728. ;; FIXME: positions wrong.
  19729. (push (list :latex-fragment (point) (point)) clist)))
  19730. (setq clist (nreverse (delq nil clist)))
  19731. clist))
  19732. ;; FIXME: Compare with at-regexp-p Do we need both?
  19733. (defun org-in-regexp (re &optional nlines visually)
  19734. "Check if point is inside a match of regexp.
  19735. Normally only the current line is checked, but you can include NLINES extra
  19736. lines both before and after point into the search.
  19737. If VISUALLY is set, require that the cursor is not after the match but
  19738. really on, so that the block visually is on the match."
  19739. (catch 'exit
  19740. (let ((pos (point))
  19741. (eol (point-at-eol (+ 1 (or nlines 0))))
  19742. (inc (if visually 1 0)))
  19743. (save-excursion
  19744. (beginning-of-line (- 1 (or nlines 0)))
  19745. (while (re-search-forward re eol t)
  19746. (if (and (<= (match-beginning 0) pos)
  19747. (>= (+ inc (match-end 0)) pos))
  19748. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19749. (defun org-at-regexp-p (regexp)
  19750. "Is point inside a match of REGEXP in the current line?"
  19751. (catch 'exit
  19752. (save-excursion
  19753. (let ((pos (point)) (end (point-at-eol)))
  19754. (beginning-of-line 1)
  19755. (while (re-search-forward regexp end t)
  19756. (if (and (<= (match-beginning 0) pos)
  19757. (>= (match-end 0) pos))
  19758. (throw 'exit t)))
  19759. nil))))
  19760. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19761. "Non-nil when point is between matches of START-RE and END-RE.
  19762. Also return a non-nil value when point is on one of the matches.
  19763. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19764. buffer positions. Default values are the positions of headlines
  19765. surrounding the point.
  19766. The functions returns a cons cell whose car (resp. cdr) is the
  19767. position before START-RE (resp. after END-RE)."
  19768. (save-match-data
  19769. (let ((pos (point))
  19770. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19771. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19772. beg end)
  19773. (save-excursion
  19774. ;; Point is on a block when on START-RE or if START-RE can be
  19775. ;; found before it...
  19776. (and (or (org-at-regexp-p start-re)
  19777. (re-search-backward start-re limit-up t))
  19778. (setq beg (match-beginning 0))
  19779. ;; ... and END-RE after it...
  19780. (goto-char (match-end 0))
  19781. (re-search-forward end-re limit-down t)
  19782. (> (setq end (match-end 0)) pos)
  19783. ;; ... without another START-RE in-between.
  19784. (goto-char (match-beginning 0))
  19785. (not (re-search-backward start-re (1+ beg) t))
  19786. ;; Return value.
  19787. (cons beg end))))))
  19788. (defun org-in-block-p (names)
  19789. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19790. NAMES is a list of strings containing names of blocks.
  19791. Return first block name matched, or nil. Beware that in case of
  19792. nested blocks, the returned name may not belong to the closest
  19793. block from point."
  19794. (save-match-data
  19795. (catch 'exit
  19796. (let ((case-fold-search t)
  19797. (lim-up (save-excursion (outline-previous-heading)))
  19798. (lim-down (save-excursion (outline-next-heading))))
  19799. (mapc (lambda (name)
  19800. (let ((n (regexp-quote name)))
  19801. (when (org-between-regexps-p
  19802. (concat "^[ \t]*#\\+begin_" n)
  19803. (concat "^[ \t]*#\\+end_" n)
  19804. lim-up lim-down)
  19805. (throw 'exit n))))
  19806. names))
  19807. nil)))
  19808. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19809. "Call `multi-occur' with buffers for all agenda files."
  19810. (interactive "sOrg-files matching: \np")
  19811. (let* ((files (org-agenda-files))
  19812. (tnames (mapcar 'file-truename files))
  19813. (extra org-agenda-text-search-extra-files)
  19814. f)
  19815. (when (eq (car extra) 'agenda-archives)
  19816. (setq extra (cdr extra))
  19817. (setq files (org-add-archive-files files)))
  19818. (while (setq f (pop extra))
  19819. (unless (member (file-truename f) tnames)
  19820. (add-to-list 'files f 'append)
  19821. (add-to-list 'tnames (file-truename f) 'append)))
  19822. (multi-occur
  19823. (mapcar (lambda (x)
  19824. (with-current-buffer
  19825. (or (get-file-buffer x) (find-file-noselect x))
  19826. (widen)
  19827. (current-buffer)))
  19828. files)
  19829. regexp)))
  19830. (if (boundp 'occur-mode-find-occurrence-hook)
  19831. ;; Emacs 23
  19832. (add-hook 'occur-mode-find-occurrence-hook
  19833. (lambda ()
  19834. (when (derived-mode-p 'org-mode)
  19835. (org-reveal))))
  19836. ;; Emacs 22
  19837. (defadvice occur-mode-goto-occurrence
  19838. (after org-occur-reveal activate)
  19839. (and (derived-mode-p 'org-mode) (org-reveal)))
  19840. (defadvice occur-mode-goto-occurrence-other-window
  19841. (after org-occur-reveal activate)
  19842. (and (derived-mode-p 'org-mode) (org-reveal)))
  19843. (defadvice occur-mode-display-occurrence
  19844. (after org-occur-reveal activate)
  19845. (when (derived-mode-p 'org-mode)
  19846. (let ((pos (occur-mode-find-occurrence)))
  19847. (with-current-buffer (marker-buffer pos)
  19848. (save-excursion
  19849. (goto-char pos)
  19850. (org-reveal)))))))
  19851. (defun org-occur-link-in-agenda-files ()
  19852. "Create a link and search for it in the agendas.
  19853. The link is not stored in `org-stored-links', it is just created
  19854. for the search purpose."
  19855. (interactive)
  19856. (let ((link (condition-case nil
  19857. (org-store-link nil)
  19858. (error "Unable to create a link to here"))))
  19859. (org-occur-in-agenda-files (regexp-quote link))))
  19860. (defun org-reverse-string (string)
  19861. "Return the reverse of STRING."
  19862. (apply 'string (reverse (string-to-list string))))
  19863. (defsubst org-uniquify (list)
  19864. "Non-destructively remove duplicate elements from LIST."
  19865. (let ((res (copy-sequence list))) (delete-dups res)))
  19866. (defun org-uniquify-alist (alist)
  19867. "Merge elements of ALIST with the same key.
  19868. For example, in this alist:
  19869. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19870. => '((a 1 3) (b 2))
  19871. merge (a 1) and (a 3) into (a 1 3).
  19872. The function returns the new ALIST."
  19873. (let (rtn)
  19874. (mapc
  19875. (lambda (e)
  19876. (let (n)
  19877. (if (not (assoc (car e) rtn))
  19878. (push e rtn)
  19879. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19880. (setq rtn (assq-delete-all (car e) rtn))
  19881. (push n rtn))))
  19882. alist)
  19883. rtn))
  19884. (defun org-delete-all (elts list)
  19885. "Remove all elements in ELTS from LIST."
  19886. (while elts
  19887. (setq list (delete (pop elts) list)))
  19888. list)
  19889. (defun org-count (cl-item cl-seq)
  19890. "Count the number of occurrences of ITEM in SEQ.
  19891. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19892. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19893. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19894. (while (< cl-start cl-end)
  19895. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19896. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19897. (setq cl-start (1+ cl-start)))
  19898. cl-count))
  19899. (defun org-remove-if (predicate seq)
  19900. "Remove everything from SEQ that fulfills PREDICATE."
  19901. (let (res e)
  19902. (while seq
  19903. (setq e (pop seq))
  19904. (if (not (funcall predicate e)) (push e res)))
  19905. (nreverse res)))
  19906. (defun org-remove-if-not (predicate seq)
  19907. "Remove everything from SEQ that does not fulfill PREDICATE."
  19908. (let (res e)
  19909. (while seq
  19910. (setq e (pop seq))
  19911. (if (funcall predicate e) (push e res)))
  19912. (nreverse res)))
  19913. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19914. "Reduce two-argument FUNCTION across SEQ.
  19915. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19916. \":initial-value\" removed."
  19917. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19918. (cadr (memq :initial-value cl-keys)))
  19919. (cl-seq (pop cl-seq))
  19920. (t (funcall cl-func)))))
  19921. (while cl-seq
  19922. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19923. cl-accum))
  19924. (defun org-back-over-empty-lines ()
  19925. "Move backwards over whitespace, to the beginning of the first empty line.
  19926. Returns the number of empty lines passed."
  19927. (let ((pos (point)))
  19928. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19929. (skip-chars-backward " \t\n\r")
  19930. (unless (eobp)
  19931. (forward-line -1)))
  19932. (beginning-of-line 2)
  19933. (goto-char (min (point) pos))
  19934. (count-lines (point) pos)))
  19935. (defun org-skip-whitespace ()
  19936. (skip-chars-forward " \t\n\r"))
  19937. (defun org-point-in-group (point group &optional context)
  19938. "Check if POINT is in match-group GROUP.
  19939. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19940. match. If the match group does not exist or point is not inside it,
  19941. return nil."
  19942. (and (match-beginning group)
  19943. (>= point (match-beginning group))
  19944. (<= point (match-end group))
  19945. (if context
  19946. (list context (match-beginning group) (match-end group))
  19947. t)))
  19948. (defun org-switch-to-buffer-other-window (&rest args)
  19949. "Switch to buffer in a second window on the current frame.
  19950. In particular, do not allow pop-up frames.
  19951. Returns the newly created buffer."
  19952. (org-no-popups
  19953. (apply 'switch-to-buffer-other-window args)))
  19954. (defun org-combine-plists (&rest plists)
  19955. "Create a single property list from all plists in PLISTS.
  19956. The process starts by copying the first list, and then setting properties
  19957. from the other lists. Settings in the last list are the most significant
  19958. ones and overrule settings in the other lists."
  19959. (let ((rtn (copy-sequence (pop plists)))
  19960. p v ls)
  19961. (while plists
  19962. (setq ls (pop plists))
  19963. (while ls
  19964. (setq p (pop ls) v (pop ls))
  19965. (setq rtn (plist-put rtn p v))))
  19966. rtn))
  19967. (defun org-replace-escapes (string table)
  19968. "Replace %-escapes in STRING with values in TABLE.
  19969. TABLE is an association list with keys like \"%a\" and string values.
  19970. The sequences in STRING may contain normal field width and padding information,
  19971. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19972. so values can contain further %-escapes if they are define later in TABLE."
  19973. (let ((tbl (copy-alist table))
  19974. (case-fold-search nil)
  19975. (pchg 0)
  19976. e re rpl)
  19977. (while (setq e (pop tbl))
  19978. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19979. (when (and (cdr e) (string-match re (cdr e)))
  19980. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19981. (safe "SREF"))
  19982. (add-text-properties 0 3 (list 'sref sref) safe)
  19983. (setcdr e (replace-match safe t t (cdr e)))))
  19984. (while (string-match re string)
  19985. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19986. (cdr e)))
  19987. (setq string (replace-match rpl t t string))))
  19988. (while (setq pchg (next-property-change pchg string))
  19989. (let ((sref (get-text-property pchg 'sref string)))
  19990. (when (and sref (string-match "SREF" string pchg))
  19991. (setq string (replace-match sref t t string)))))
  19992. string))
  19993. (defun org-sublist (list start end)
  19994. "Return a section of LIST, from START to END.
  19995. Counting starts at 1."
  19996. (let (rtn (c start))
  19997. (setq list (nthcdr (1- start) list))
  19998. (while (and list (<= c end))
  19999. (push (pop list) rtn)
  20000. (setq c (1+ c)))
  20001. (nreverse rtn)))
  20002. (defun org-find-base-buffer-visiting (file)
  20003. "Like `find-buffer-visiting' but always return the base buffer and
  20004. not an indirect buffer."
  20005. (let ((buf (or (get-file-buffer file)
  20006. (find-buffer-visiting file))))
  20007. (if buf
  20008. (or (buffer-base-buffer buf) buf)
  20009. nil)))
  20010. (defun org-image-file-name-regexp (&optional extensions)
  20011. "Return regexp matching the file names of images.
  20012. If EXTENSIONS is given, only match these."
  20013. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20014. (image-file-name-regexp)
  20015. (let ((image-file-name-extensions
  20016. (or extensions
  20017. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20018. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20019. (concat "\\."
  20020. (regexp-opt (nconc (mapcar 'upcase
  20021. image-file-name-extensions)
  20022. image-file-name-extensions)
  20023. t)
  20024. "\\'"))))
  20025. (defun org-file-image-p (file &optional extensions)
  20026. "Return non-nil if FILE is an image."
  20027. (save-match-data
  20028. (string-match (org-image-file-name-regexp extensions) file)))
  20029. (defun org-get-cursor-date (&optional with-time)
  20030. "Return the date at cursor in as a time.
  20031. This works in the calendar and in the agenda, anywhere else it just
  20032. returns the current time.
  20033. If WITH-TIME is non-nil, returns the time of the event at point (in
  20034. the agenda) or the current time of the day."
  20035. (let (date day defd tp tm hod mod)
  20036. (when with-time
  20037. (setq tp (get-text-property (point) 'time))
  20038. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20039. (setq hod (string-to-number (match-string 1 tp))
  20040. mod (string-to-number (match-string 2 tp))))
  20041. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20042. mod (nth 1 (decode-time (current-time))))))
  20043. (cond
  20044. ((eq major-mode 'calendar-mode)
  20045. (setq date (calendar-cursor-to-date)
  20046. defd (encode-time 0 (or mod 0) (or hod 0)
  20047. (nth 1 date) (nth 0 date) (nth 2 date))))
  20048. ((eq major-mode 'org-agenda-mode)
  20049. (setq day (get-text-property (point) 'day))
  20050. (if day
  20051. (setq date (calendar-gregorian-from-absolute day)
  20052. defd (encode-time 0 (or mod 0) (or hod 0)
  20053. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20054. (or defd (current-time))))
  20055. (defun org-mark-subtree (&optional up)
  20056. "Mark the current subtree.
  20057. This puts point at the start of the current subtree, and mark at
  20058. the end. If a numeric prefix UP is given, move up into the
  20059. hierarchy of headlines by UP levels before marking the subtree."
  20060. (interactive "P")
  20061. (org-with-limited-levels
  20062. (cond ((org-at-heading-p) (beginning-of-line))
  20063. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20064. (t (outline-previous-visible-heading 1))))
  20065. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20066. (if (org-called-interactively-p 'any)
  20067. (call-interactively 'org-mark-element)
  20068. (org-mark-element)))
  20069. ;;; Indentation
  20070. (defun org-indent-line ()
  20071. "Indent line depending on context."
  20072. (interactive)
  20073. (let* ((pos (point))
  20074. (itemp (org-at-item-p))
  20075. (case-fold-search t)
  20076. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20077. (inline-task-p (and (featurep 'org-inlinetask)
  20078. (org-inlinetask-in-task-p)))
  20079. (inline-re (and inline-task-p
  20080. (org-inlinetask-outline-regexp)))
  20081. column)
  20082. (if (and orgstruct-is-++ (eq pos (point)))
  20083. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20084. (indent-according-to-mode))
  20085. (beginning-of-line 1)
  20086. (cond
  20087. ;; Headings
  20088. ((looking-at org-outline-regexp) (setq column 0))
  20089. ;; Footnote definition
  20090. ((looking-at org-footnote-definition-re) (setq column 0))
  20091. ;; Literal examples
  20092. ((looking-at "[ \t]*:\\( \\|$\\)")
  20093. (setq column (org-get-indentation))) ; do nothing
  20094. ;; Lists
  20095. ((ignore-errors (goto-char (org-in-item-p)))
  20096. (setq column (if itemp
  20097. (org-get-indentation)
  20098. (org-list-item-body-column (point))))
  20099. (goto-char pos))
  20100. ;; Drawers
  20101. ((and (looking-at "[ \t]*:END:")
  20102. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20103. (save-excursion
  20104. (goto-char (1- (match-beginning 1)))
  20105. (setq column (current-column))))
  20106. ;; Special blocks
  20107. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20108. (save-excursion
  20109. (re-search-backward
  20110. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20111. (setq column (org-get-indentation (match-string 0))))
  20112. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20113. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20114. (save-excursion
  20115. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20116. (setq column
  20117. (cond ((equal (downcase (match-string 1)) "src")
  20118. ;; src blocks: let `org-edit-src-exit' handle them
  20119. (org-get-indentation))
  20120. ((equal (downcase (match-string 1)) "example")
  20121. (max (org-get-indentation)
  20122. (org-get-indentation (match-string 0))))
  20123. (t
  20124. (org-get-indentation (match-string 0))))))
  20125. ;; This line has nothing special, look at the previous relevant
  20126. ;; line to compute indentation
  20127. (t
  20128. (beginning-of-line 0)
  20129. (while (and (not (bobp))
  20130. (not (looking-at org-table-line-regexp))
  20131. (not (looking-at org-drawer-regexp))
  20132. ;; When point started in an inline task, do not move
  20133. ;; above task starting line.
  20134. (not (and inline-task-p (looking-at inline-re)))
  20135. ;; Skip drawers, blocks, empty lines, verbatim,
  20136. ;; comments, tables, footnotes definitions, lists,
  20137. ;; inline tasks.
  20138. (or (and (looking-at "[ \t]*:END:")
  20139. (re-search-backward org-drawer-regexp nil t))
  20140. (and (looking-at "[ \t]*#\\+end_")
  20141. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20142. (looking-at "[ \t]*[\n:#|]")
  20143. (looking-at org-footnote-definition-re)
  20144. (and (not inline-task-p)
  20145. (featurep 'org-inlinetask)
  20146. (org-inlinetask-in-task-p)
  20147. (or (org-inlinetask-goto-beginning) t))))
  20148. (beginning-of-line 0))
  20149. (cond
  20150. ;; There was a list item above.
  20151. ((save-excursion
  20152. (and (ignore-errors (goto-char (org-in-item-p)))
  20153. (goto-char
  20154. (org-list-get-top-point (org-list-struct)))))
  20155. (looking-at org-list-full-item-re)
  20156. (setq column (length (match-string 0))))
  20157. ;; There was an heading above.
  20158. ((looking-at "\\*+[ \t]+")
  20159. (if (not org-adapt-indentation)
  20160. (setq column 0)
  20161. (goto-char (match-end 0))
  20162. (setq column (current-column))))
  20163. ;; A drawer had started and is unfinished
  20164. ((looking-at org-drawer-regexp)
  20165. (goto-char (1- (match-beginning 1)))
  20166. (setq column (current-column)))
  20167. ;; Else, nothing noticeable found: get indentation and go on.
  20168. (t (setq column (org-get-indentation))))))
  20169. ;; Now apply indentation and move cursor accordingly
  20170. (goto-char pos)
  20171. (if (<= (current-column) (current-indentation))
  20172. (org-indent-line-to column)
  20173. (save-excursion (org-indent-line-to column)))
  20174. ;; Special polishing for properties, see `org-property-format'
  20175. (setq column (current-column))
  20176. (beginning-of-line 1)
  20177. (if (looking-at
  20178. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20179. (replace-match (concat (match-string 1)
  20180. (format org-property-format
  20181. (match-string 2) (match-string 3)))
  20182. t t))
  20183. (org-move-to-column column))))
  20184. (defun org-indent-drawer ()
  20185. "Indent the drawer at point."
  20186. (interactive)
  20187. (let ((p (point))
  20188. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20189. (match-end 0)))
  20190. (folded
  20191. (save-excursion
  20192. (end-of-line)
  20193. (when (overlays-at (point))
  20194. (member 'invisible (overlay-properties
  20195. (car (overlays-at (point)))))))))
  20196. (when folded (org-cycle))
  20197. (indent-for-tab-command)
  20198. (while (and (move-beginning-of-line 2) (< (point) e))
  20199. (indent-for-tab-command))
  20200. (goto-char p)
  20201. (when folded (org-cycle)))
  20202. (message "Drawer at point indented"))
  20203. (defun org-indent-block ()
  20204. "Indent the block at point."
  20205. (interactive)
  20206. (let ((p (point))
  20207. (case-fold-search t)
  20208. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20209. (match-end 0)))
  20210. (folded
  20211. (save-excursion
  20212. (end-of-line)
  20213. (when (overlays-at (point))
  20214. (member 'invisible (overlay-properties
  20215. (car (overlays-at (point)))))))))
  20216. (when folded (org-cycle))
  20217. (indent-for-tab-command)
  20218. (while (and (move-beginning-of-line 2) (< (point) e))
  20219. (indent-for-tab-command))
  20220. (goto-char p)
  20221. (when folded (org-cycle)))
  20222. (message "Block at point indented"))
  20223. (defun org-indent-region (start end)
  20224. "Indent region."
  20225. (interactive "r")
  20226. (save-excursion
  20227. (let ((line-end (org-current-line end)))
  20228. (goto-char start)
  20229. (while (< (org-current-line) line-end)
  20230. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20231. (t (call-interactively 'org-indent-line)))
  20232. (move-beginning-of-line 2)))))
  20233. ;;; Filling
  20234. ;; We use our own fill-paragraph and auto-fill functions.
  20235. ;; `org-fill-paragraph' relies on adaptive filling and context
  20236. ;; checking. Appropriate `fill-prefix' is computed with
  20237. ;; `org-adaptive-fill-function'.
  20238. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20239. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20240. ;; `org-adaptive-fill-function' value. Internally,
  20241. ;; `org-comment-line-break-function' breaks the line.
  20242. ;; `org-setup-filling' installs filling and auto-filling related
  20243. ;; variables during `org-mode' initialization.
  20244. (defun org-setup-filling ()
  20245. (interactive)
  20246. ;; Prevent auto-fill from inserting unwanted new items.
  20247. (when (boundp 'fill-nobreak-predicate)
  20248. (org-set-local
  20249. 'fill-nobreak-predicate
  20250. (org-uniquify
  20251. (append fill-nobreak-predicate
  20252. '(org-fill-paragraph-separate-nobreak-p
  20253. org-fill-line-break-nobreak-p
  20254. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20255. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20256. (org-set-local 'auto-fill-inhibit-regexp nil)
  20257. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20258. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20259. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20260. (defvar org-element-paragraph-separate) ; org-element.el
  20261. (defun org-fill-paragraph-separate-nobreak-p ()
  20262. "Non-nil when a line break at point would insert a new item."
  20263. (looking-at (substring org-element-paragraph-separate 1)))
  20264. (defun org-fill-line-break-nobreak-p ()
  20265. "Non-nil when a line break at point would create an Org line break."
  20266. (save-excursion
  20267. (skip-chars-backward "[ \t]")
  20268. (skip-chars-backward "\\\\")
  20269. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20270. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20271. "Non-nil when a line break at point would insert a new item."
  20272. (and (org-at-timestamp-p t)
  20273. (not (looking-at org-ts-regexp-both))))
  20274. (declare-function message-in-body-p "message" ())
  20275. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20276. (defun org-adaptive-fill-function ()
  20277. "Compute a fill prefix for the current line.
  20278. Return fill prefix, as a string, or nil if current line isn't
  20279. meant to be filled."
  20280. (let (prefix)
  20281. (catch 'exit
  20282. (when (derived-mode-p 'message-mode)
  20283. (save-excursion
  20284. (beginning-of-line)
  20285. (cond ((or (not (message-in-body-p))
  20286. (looking-at orgtbl-line-start-regexp))
  20287. (throw 'exit nil))
  20288. ((looking-at message-cite-prefix-regexp)
  20289. (throw 'exit (match-string-no-properties 0)))
  20290. ((looking-at org-outline-regexp)
  20291. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20292. (org-with-wide-buffer
  20293. (let* ((p (line-beginning-position))
  20294. (element (save-excursion
  20295. (beginning-of-line)
  20296. (or (ignore-errors (org-element-at-point))
  20297. (user-error "An element cannot be parsed line %d"
  20298. (line-number-at-pos (point))))))
  20299. (type (org-element-type element))
  20300. (post-affiliated (org-element-property :post-affiliated element)))
  20301. (unless (and post-affiliated (< p post-affiliated))
  20302. (case type
  20303. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20304. (footnote-definition "")
  20305. ((item plain-list)
  20306. (make-string (org-list-item-body-column
  20307. (or post-affiliated
  20308. (org-element-property :begin element)))
  20309. ? ))
  20310. (paragraph
  20311. ;; Fill prefix is usually the same as the current line,
  20312. ;; except if the paragraph is at the beginning of an item.
  20313. (let ((parent (org-element-property :parent element)))
  20314. (cond ((eq (org-element-type parent) 'item)
  20315. (make-string (org-list-item-body-column
  20316. (org-element-property :begin parent))
  20317. ? ))
  20318. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20319. (match-string 0))
  20320. (t ""))))
  20321. (comment-block
  20322. ;; Only fill contents if P is within block boundaries.
  20323. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20324. (forward-line)
  20325. (point)))
  20326. (cend (save-excursion
  20327. (goto-char (org-element-property :end element))
  20328. (skip-chars-backward " \r\t\n")
  20329. (line-beginning-position))))
  20330. (when (and (>= p cbeg) (< p cend))
  20331. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20332. (match-string 0)
  20333. "")))))))))))
  20334. (declare-function message-goto-body "message" ())
  20335. (defvar message-cite-prefix-regexp) ; From message.el
  20336. (defun org-fill-paragraph (&optional justify)
  20337. "Fill element at point, when applicable.
  20338. This function only applies to comment blocks, comments, example
  20339. blocks and paragraphs. Also, as a special case, re-align table
  20340. when point is at one.
  20341. If JUSTIFY is non-nil (interactively, with prefix argument),
  20342. justify as well. If `sentence-end-double-space' is non-nil, then
  20343. period followed by one space does not end a sentence, so don't
  20344. break a line there. The variable `fill-column' controls the
  20345. width for filling.
  20346. For convenience, when point is at a plain list, an item or
  20347. a footnote definition, try to fill the first paragraph within."
  20348. (interactive)
  20349. (if (and (derived-mode-p 'message-mode)
  20350. (or (not (message-in-body-p))
  20351. (save-excursion (move-beginning-of-line 1)
  20352. (looking-at message-cite-prefix-regexp))))
  20353. ;; First ensure filling is correct in message-mode.
  20354. (let ((fill-paragraph-function
  20355. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20356. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20357. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20358. (paragraph-separate
  20359. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20360. (fill-paragraph nil))
  20361. (with-syntax-table org-mode-transpose-word-syntax-table
  20362. ;; Move to end of line in order to get the first paragraph
  20363. ;; within a plain list or a footnote definition.
  20364. (let ((element (save-excursion
  20365. (end-of-line)
  20366. (or (ignore-errors (org-element-at-point))
  20367. (user-error "An element cannot be parsed line %d"
  20368. (line-number-at-pos (point)))))))
  20369. ;; First check if point is in a blank line at the beginning of
  20370. ;; the buffer. In that case, ignore filling.
  20371. (case (org-element-type element)
  20372. ;; Use major mode filling function is src blocks.
  20373. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20374. ;; Align Org tables, leave table.el tables as-is.
  20375. (table-row (org-table-align) t)
  20376. (table
  20377. (when (eq (org-element-property :type element) 'org)
  20378. (org-table-align))
  20379. t)
  20380. (paragraph
  20381. ;; Paragraphs may contain `line-break' type objects.
  20382. (let ((beg (max (point-min)
  20383. (org-element-property :contents-begin element)))
  20384. (end (min (point-max)
  20385. (org-element-property :contents-end element))))
  20386. ;; Do nothing if point is at an affiliated keyword.
  20387. (if (< (line-end-position) beg) t
  20388. (when (derived-mode-p 'message-mode)
  20389. ;; In `message-mode', do not fill following citation
  20390. ;; in current paragraph nor text before message body.
  20391. (let ((body-start (save-excursion (message-goto-body))))
  20392. (when body-start (setq beg (max body-start beg))))
  20393. (when (save-excursion
  20394. (re-search-forward
  20395. (concat "^" message-cite-prefix-regexp) end t))
  20396. (setq end (match-beginning 0))))
  20397. ;; Fill paragraph, taking line breaks into account.
  20398. ;; For that, slice the paragraph using line breaks as
  20399. ;; separators, and fill the parts in reverse order to
  20400. ;; avoid messing with markers.
  20401. (save-excursion
  20402. (goto-char end)
  20403. (mapc
  20404. (lambda (pos)
  20405. (fill-region-as-paragraph pos (point) justify)
  20406. (goto-char pos))
  20407. ;; Find the list of ending positions for line breaks
  20408. ;; in the current paragraph. Add paragraph
  20409. ;; beginning to include first slice.
  20410. (nreverse
  20411. (cons beg
  20412. (org-element-map
  20413. (org-element--parse-objects
  20414. beg end nil (org-element-restriction 'paragraph))
  20415. 'line-break
  20416. (lambda (lb) (org-element-property :end lb)))))))
  20417. t)))
  20418. ;; Contents of `comment-block' type elements should be
  20419. ;; filled as plain text, but only if point is within block
  20420. ;; markers.
  20421. (comment-block
  20422. (let* ((case-fold-search t)
  20423. (beg (save-excursion
  20424. (goto-char (org-element-property :begin element))
  20425. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20426. (forward-line)
  20427. (point)))
  20428. (end (save-excursion
  20429. (goto-char (org-element-property :end element))
  20430. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20431. (line-beginning-position))))
  20432. (when (and (>= (point) beg) (< (point) end))
  20433. (fill-region-as-paragraph
  20434. (save-excursion
  20435. (end-of-line)
  20436. (re-search-backward "^[ \t]*$" beg 'move)
  20437. (line-beginning-position))
  20438. (save-excursion
  20439. (beginning-of-line)
  20440. (re-search-forward "^[ \t]*$" end 'move)
  20441. (line-beginning-position))
  20442. justify)))
  20443. t)
  20444. ;; Fill comments.
  20445. (comment (fill-comment-paragraph justify))
  20446. ;; Ignore every other element.
  20447. (otherwise t))))))
  20448. (defun org-auto-fill-function ()
  20449. "Auto-fill function."
  20450. ;; Check if auto-filling is meaningful.
  20451. (let ((fc (current-fill-column)))
  20452. (when (and fc (> (current-column) fc))
  20453. (let* ((fill-prefix (org-adaptive-fill-function))
  20454. ;; Enforce empty fill prefix, if required. Otherwise, it
  20455. ;; will be computed again.
  20456. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20457. (when fill-prefix (do-auto-fill))))))
  20458. (defun org-comment-line-break-function (&optional soft)
  20459. "Break line at point and indent, continuing comment if within one.
  20460. The inserted newline is marked hard if variable
  20461. `use-hard-newlines' is true, unless optional argument SOFT is
  20462. non-nil."
  20463. (if soft (insert-and-inherit ?\n) (newline 1))
  20464. (save-excursion (forward-char -1) (delete-horizontal-space))
  20465. (delete-horizontal-space)
  20466. (indent-to-left-margin)
  20467. (insert-before-markers-and-inherit fill-prefix))
  20468. ;;; Comments
  20469. ;; Org comments syntax is quite complex. It requires the entire line
  20470. ;; to be just a comment. Also, even with the right syntax at the
  20471. ;; beginning of line, some some elements (i.e. verse-block or
  20472. ;; example-block) don't accept comments. Usual Emacs comment commands
  20473. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20474. ;; Org still relies on `comment-dwim', but cannot trust
  20475. ;; `comment-only-p'. So, `comment-region-function' and
  20476. ;; `uncomment-region-function' both point
  20477. ;; to`org-comment-or-uncomment-region'. Eventually,
  20478. ;; `org-insert-comment' takes care of insertion of comments at the
  20479. ;; beginning of line.
  20480. ;; `org-setup-comments-handling' install comments related variables
  20481. ;; during `org-mode' initialization.
  20482. (defun org-setup-comments-handling ()
  20483. (interactive)
  20484. (org-set-local 'comment-use-syntax nil)
  20485. (org-set-local 'comment-start "# ")
  20486. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20487. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20488. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20489. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20490. (defun org-insert-comment ()
  20491. "Insert an empty comment above current line.
  20492. If the line is empty, insert comment at its beginning."
  20493. (beginning-of-line)
  20494. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20495. (org-indent-line)
  20496. (insert "# "))
  20497. (defvar comment-empty-lines) ; From newcomment.el.
  20498. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20499. "Comment or uncomment each non-blank line in the region.
  20500. Uncomment each non-blank line between BEG and END if it only
  20501. contains commented lines. Otherwise, comment them."
  20502. (save-restriction
  20503. ;; Restrict region
  20504. (narrow-to-region (save-excursion (goto-char beg)
  20505. (skip-chars-forward " \r\t\n" end)
  20506. (line-beginning-position))
  20507. (save-excursion (goto-char end)
  20508. (skip-chars-backward " \r\t\n" beg)
  20509. (line-end-position)))
  20510. (let ((uncommentp
  20511. ;; UNCOMMENTP is non-nil when every non blank line between
  20512. ;; BEG and END is a comment.
  20513. (save-excursion
  20514. (goto-char (point-min))
  20515. (while (and (not (eobp))
  20516. (let ((element (org-element-at-point)))
  20517. (and (eq (org-element-type element) 'comment)
  20518. (goto-char (min (point-max)
  20519. (org-element-property
  20520. :end element)))))))
  20521. (eobp))))
  20522. (if uncommentp
  20523. ;; Only blank lines and comments in region: uncomment it.
  20524. (save-excursion
  20525. (goto-char (point-min))
  20526. (while (not (eobp))
  20527. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20528. (replace-match "" nil nil nil 1))
  20529. (forward-line)))
  20530. ;; Comment each line in region.
  20531. (let ((min-indent (point-max)))
  20532. ;; First find the minimum indentation across all lines.
  20533. (save-excursion
  20534. (goto-char (point-min))
  20535. (while (and (not (eobp)) (not (zerop min-indent)))
  20536. (unless (looking-at "[ \t]*$")
  20537. (setq min-indent (min min-indent (current-indentation))))
  20538. (forward-line)))
  20539. ;; Then loop over all lines.
  20540. (save-excursion
  20541. (goto-char (point-min))
  20542. (while (not (eobp))
  20543. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20544. ;; Don't get fooled by invisible text (e.g. link path)
  20545. ;; when moving to column MIN-INDENT.
  20546. (let ((buffer-invisibility-spec nil))
  20547. (org-move-to-column min-indent t))
  20548. (insert comment-start))
  20549. (forward-line))))))))
  20550. ;;; Planning
  20551. ;; This section contains tools to operate on timestamp objects, as
  20552. ;; returned by, e.g. `org-element-context'.
  20553. (defun org-timestamp-has-time-p (timestamp)
  20554. "Non-nil when TIMESTAMP has a time specified."
  20555. (org-element-property :hour-start timestamp))
  20556. (defun org-timestamp-format (timestamp format &optional end utc)
  20557. "Format a TIMESTAMP element into a string.
  20558. FORMAT is a format specifier to be passed to
  20559. `format-time-string'.
  20560. When optional argument END is non-nil, use end of date-range or
  20561. time-range, if possible.
  20562. When optional argument UTC is non-nil, time will be expressed as
  20563. Universal Time."
  20564. (format-time-string
  20565. format
  20566. (apply 'encode-time
  20567. (cons 0
  20568. (mapcar
  20569. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20570. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20571. '(:minute-start :hour-start :day-start :month-start
  20572. :year-start)))))
  20573. utc))
  20574. (defun org-timestamp-split-range (timestamp &optional end)
  20575. "Extract a timestamp object from a date or time range.
  20576. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20577. the end of the range. Otherwise, extract its start.
  20578. Return a new timestamp object sharing the same parent as
  20579. TIMESTAMP."
  20580. (let ((type (org-element-property :type timestamp)))
  20581. (if (memq type '(active inactive diary)) timestamp
  20582. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20583. ;; Set new type.
  20584. (org-element-put-property
  20585. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20586. ;; Copy start properties over end properties if END is
  20587. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20588. (let ((p-alist '((:minute-start . :minute-end)
  20589. (:hour-start . :hour-end)
  20590. (:day-start . :day-end)
  20591. (:month-start . :month-end)
  20592. (:year-start . :year-end))))
  20593. (dolist (p-cell p-alist)
  20594. (org-element-put-property
  20595. split-ts
  20596. (funcall (if end 'car 'cdr) p-cell)
  20597. (org-element-property
  20598. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20599. ;; Eventually refresh `:raw-value'.
  20600. (org-element-put-property split-ts :raw-value nil)
  20601. (org-element-put-property
  20602. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20603. (defun org-timestamp-translate (timestamp &optional boundary)
  20604. "Apply `org-translate-time' on a TIMESTAMP object.
  20605. When optional argument BOUNDARY is non-nil, it is either the
  20606. symbol `start' or `end'. In this case, only translate the
  20607. starting or ending part of TIMESTAMP if it is a date or time
  20608. range. Otherwise, translate both parts."
  20609. (if (and (not boundary)
  20610. (memq (org-element-property :type timestamp)
  20611. '(active-range inactive-range)))
  20612. (concat
  20613. (org-translate-time
  20614. (org-element-property :raw-value
  20615. (org-timestamp-split-range timestamp)))
  20616. "--"
  20617. (org-translate-time
  20618. (org-element-property :raw-value
  20619. (org-timestamp-split-range timestamp t))))
  20620. (org-translate-time
  20621. (org-element-property
  20622. :raw-value
  20623. (if (not boundary) timestamp
  20624. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20625. ;;; Other stuff.
  20626. (defun org-toggle-fixed-width-section (arg)
  20627. "Toggle the fixed-width export.
  20628. If there is no active region, the QUOTE keyword at the current headline is
  20629. inserted or removed. When present, it causes the text between this headline
  20630. and the next to be exported as fixed-width text, and unmodified.
  20631. If there is an active region, this command adds or removes a colon as the
  20632. first character of this line. If the first character of a line is a colon,
  20633. this line is also exported in fixed-width font."
  20634. (interactive "P")
  20635. (let* ((cc 0)
  20636. (regionp (org-region-active-p))
  20637. (beg (if regionp (region-beginning) (point)))
  20638. (end (if regionp (region-end)))
  20639. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20640. (case-fold-search nil)
  20641. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20642. off)
  20643. (if regionp
  20644. (save-excursion
  20645. (goto-char beg)
  20646. (setq cc (current-column))
  20647. (beginning-of-line 1)
  20648. (setq off (looking-at re))
  20649. (while (> nlines 0)
  20650. (setq nlines (1- nlines))
  20651. (beginning-of-line 1)
  20652. (cond
  20653. (arg
  20654. (org-move-to-column cc t)
  20655. (insert ": \n")
  20656. (forward-line -1))
  20657. ((and off (looking-at re))
  20658. (replace-match "" t t nil 1))
  20659. ((not off) (org-move-to-column cc t) (insert ": ")))
  20660. (forward-line 1)))
  20661. (save-excursion
  20662. (org-back-to-heading)
  20663. (cond
  20664. ((looking-at (format org-heading-keyword-regexp-format
  20665. org-quote-string))
  20666. (goto-char (match-end 1))
  20667. (looking-at (concat " +" org-quote-string))
  20668. (replace-match "" t t)
  20669. (when (eolp) (insert " ")))
  20670. ((looking-at org-outline-regexp)
  20671. (goto-char (match-end 0))
  20672. (insert org-quote-string " ")))))))
  20673. (defun org-reftex-citation ()
  20674. "Use reftex-citation to insert a citation into the buffer.
  20675. This looks for a line like
  20676. #+BIBLIOGRAPHY: foo plain option:-d
  20677. and derives from it that foo.bib is the bibliography file relevant
  20678. for this document. It then installs the necessary environment for RefTeX
  20679. to work in this buffer and calls `reftex-citation' to insert a citation
  20680. into the buffer.
  20681. Export of such citations to both LaTeX and HTML is handled by the contributed
  20682. package org-exp-bibtex by Taru Karttunen."
  20683. (interactive)
  20684. (let ((reftex-docstruct-symbol 'rds)
  20685. (reftex-cite-format "\\cite{%l}")
  20686. rds bib)
  20687. (save-excursion
  20688. (save-restriction
  20689. (widen)
  20690. (let ((case-fold-search t)
  20691. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20692. (if (not (save-excursion
  20693. (or (re-search-forward re nil t)
  20694. (re-search-backward re nil t))))
  20695. (error "No bibliography defined in file")
  20696. (setq bib (concat (match-string 1) ".bib")
  20697. rds (list (list 'bib bib)))))))
  20698. (call-interactively 'reftex-citation)))
  20699. ;;;; Functions extending outline functionality
  20700. (defun org-beginning-of-line (&optional arg)
  20701. "Go to the beginning of the current line. If that is invisible, continue
  20702. to a visible line beginning. This makes the function of C-a more intuitive.
  20703. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20704. first attempt, and only move to after the tags when the cursor is already
  20705. beyond the end of the headline."
  20706. (interactive "P")
  20707. (let ((pos (point))
  20708. (special (if (consp org-special-ctrl-a/e)
  20709. (car org-special-ctrl-a/e)
  20710. org-special-ctrl-a/e))
  20711. refpos)
  20712. (if (org-bound-and-true-p visual-line-mode)
  20713. (beginning-of-visual-line 1)
  20714. (beginning-of-line 1))
  20715. (if (and arg (fboundp 'move-beginning-of-line))
  20716. (call-interactively 'move-beginning-of-line)
  20717. (if (bobp)
  20718. nil
  20719. (backward-char 1)
  20720. (if (org-truely-invisible-p)
  20721. (while (and (not (bobp)) (org-truely-invisible-p))
  20722. (backward-char 1)
  20723. (beginning-of-line 1))
  20724. (forward-char 1))))
  20725. (when special
  20726. (cond
  20727. ((and (looking-at org-complex-heading-regexp)
  20728. (= (char-after (match-end 1)) ?\ ))
  20729. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20730. (point-at-eol)))
  20731. (goto-char
  20732. (if (eq special t)
  20733. (cond ((> pos refpos) refpos)
  20734. ((= pos (point)) refpos)
  20735. (t (point)))
  20736. (cond ((> pos (point)) (point))
  20737. ((not (eq last-command this-command)) (point))
  20738. (t refpos)))))
  20739. ((org-at-item-p)
  20740. ;; Being at an item and not looking at an the item means point
  20741. ;; was previously moved to beginning of a visual line, which
  20742. ;; doesn't contain the item. Therefore, do nothing special,
  20743. ;; just stay here.
  20744. (when (looking-at org-list-full-item-re)
  20745. ;; Set special position at first white space character after
  20746. ;; bullet, and check-box, if any.
  20747. (let ((after-bullet
  20748. (let ((box (match-end 3)))
  20749. (if (not box) (match-end 1)
  20750. (let ((after (char-after box)))
  20751. (if (and after (= after ? )) (1+ box) box))))))
  20752. ;; Special case: Move point to special position when
  20753. ;; currently after it or at beginning of line.
  20754. (if (eq special t)
  20755. (when (or (> pos after-bullet) (= (point) pos))
  20756. (goto-char after-bullet))
  20757. ;; Reversed case: Move point to special position when
  20758. ;; point was already at beginning of line and command is
  20759. ;; repeated.
  20760. (when (and (= (point) pos) (eq last-command this-command))
  20761. (goto-char after-bullet))))))))
  20762. (org-no-warnings
  20763. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20764. (defun org-end-of-line (&optional arg)
  20765. "Go to the end of the line.
  20766. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20767. tags on the first attempt, and only move to after the tags when
  20768. the cursor is already beyond the end of the headline."
  20769. (interactive "P")
  20770. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20771. org-special-ctrl-a/e))
  20772. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20773. 'end-of-visual-line)
  20774. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20775. (t 'end-of-line))))
  20776. (if (or (not special) arg) (call-interactively move-fun)
  20777. (let* ((element (save-excursion (beginning-of-line)
  20778. (org-element-at-point)))
  20779. (type (org-element-type element)))
  20780. (cond
  20781. ((memq type '(headline inlinetask))
  20782. (let ((pos (point)))
  20783. (beginning-of-line 1)
  20784. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20785. (if (eq special t)
  20786. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20787. (goto-char (match-beginning 1))
  20788. (goto-char (match-end 0)))
  20789. (if (or (< pos (match-end 0))
  20790. (not (eq this-command last-command)))
  20791. (goto-char (match-end 0))
  20792. (goto-char (match-beginning 1))))
  20793. (call-interactively move-fun))))
  20794. ((org-element-property :hiddenp element)
  20795. ;; If element is hidden, `move-end-of-line' would put point
  20796. ;; after it. Use `end-of-line' to stay on current line.
  20797. (call-interactively 'end-of-line))
  20798. (t (call-interactively move-fun)))))
  20799. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20800. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20801. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20802. (defun org-backward-sentence (&optional arg)
  20803. "Go to beginning of sentence, or beginning of table field.
  20804. This will call `backward-sentence' or `org-table-beginning-of-field',
  20805. depending on context."
  20806. (interactive "P")
  20807. (cond
  20808. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20809. (t (call-interactively 'backward-sentence))))
  20810. (defun org-forward-sentence (&optional arg)
  20811. "Go to end of sentence, or end of table field.
  20812. This will call `forward-sentence' or `org-table-end-of-field',
  20813. depending on context."
  20814. (interactive "P")
  20815. (cond
  20816. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20817. (t (call-interactively 'forward-sentence))))
  20818. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20819. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20820. (defun org-kill-line (&optional arg)
  20821. "Kill line, to tags or end of line."
  20822. (interactive "P")
  20823. (cond
  20824. ((or (not org-special-ctrl-k)
  20825. (bolp)
  20826. (not (org-at-heading-p)))
  20827. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20828. org-ctrl-k-protect-subtree)
  20829. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20830. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20831. (user-error "C-k aborted as it would kill a hidden subtree")))
  20832. (call-interactively
  20833. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20834. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20835. (kill-region (point) (match-beginning 1))
  20836. (org-set-tags nil t))
  20837. (t (kill-region (point) (point-at-eol)))))
  20838. (define-key org-mode-map "\C-k" 'org-kill-line)
  20839. (defun org-yank (&optional arg)
  20840. "Yank. If the kill is a subtree, treat it specially.
  20841. This command will look at the current kill and check if is a single
  20842. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20843. cursor is at the beginning of a line or after the stars of a currently
  20844. empty headline, then the yank is handled specially. How exactly depends
  20845. on the value of the following variables, both set by default.
  20846. org-yank-folded-subtrees
  20847. When set, the subtree(s) will be folded after insertion, but only
  20848. if doing so would now swallow text after the yanked text.
  20849. org-yank-adjusted-subtrees
  20850. When set, the subtree will be promoted or demoted in order to
  20851. fit into the local outline tree structure, which means that the level
  20852. will be adjusted so that it becomes the smaller one of the two
  20853. *visible* surrounding headings.
  20854. Any prefix to this command will cause `yank' to be called directly with
  20855. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20856. will just
  20857. plainly yank the text as it is.
  20858. \[1] The test checks if the first non-white line is a heading
  20859. and if there are no other headings with fewer stars."
  20860. (interactive "P")
  20861. (org-yank-generic 'yank arg))
  20862. (defun org-yank-generic (command arg)
  20863. "Perform some yank-like command.
  20864. This function implements the behavior described in the `org-yank'
  20865. documentation. However, it has been generalized to work for any
  20866. interactive command with similar behavior."
  20867. ;; pretend to be command COMMAND
  20868. (setq this-command command)
  20869. (if arg
  20870. (call-interactively command)
  20871. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20872. (and (org-kill-is-subtree-p)
  20873. (or (bolp)
  20874. (and (looking-at "[ \t]*$")
  20875. (string-match
  20876. "\\`\\*+\\'"
  20877. (buffer-substring (point-at-bol) (point)))))))
  20878. swallowp)
  20879. (cond
  20880. ((and subtreep org-yank-folded-subtrees)
  20881. (let ((beg (point))
  20882. end)
  20883. (if (and subtreep org-yank-adjusted-subtrees)
  20884. (org-paste-subtree nil nil 'for-yank)
  20885. (call-interactively command))
  20886. (setq end (point))
  20887. (goto-char beg)
  20888. (when (and (bolp) subtreep
  20889. (not (setq swallowp
  20890. (org-yank-folding-would-swallow-text beg end))))
  20891. (org-with-limited-levels
  20892. (or (looking-at org-outline-regexp)
  20893. (re-search-forward org-outline-regexp-bol end t))
  20894. (while (and (< (point) end) (looking-at org-outline-regexp))
  20895. (hide-subtree)
  20896. (org-cycle-show-empty-lines 'folded)
  20897. (condition-case nil
  20898. (outline-forward-same-level 1)
  20899. (error (goto-char end))))))
  20900. (when swallowp
  20901. (message
  20902. "Inserted text not folded because that would swallow text"))
  20903. (goto-char end)
  20904. (skip-chars-forward " \t\n\r")
  20905. (beginning-of-line 1)
  20906. (push-mark beg 'nomsg)))
  20907. ((and subtreep org-yank-adjusted-subtrees)
  20908. (let ((beg (point-at-bol)))
  20909. (org-paste-subtree nil nil 'for-yank)
  20910. (push-mark beg 'nomsg)))
  20911. (t
  20912. (call-interactively command))))))
  20913. (defun org-yank-folding-would-swallow-text (beg end)
  20914. "Would hide-subtree at BEG swallow any text after END?"
  20915. (let (level)
  20916. (org-with-limited-levels
  20917. (save-excursion
  20918. (goto-char beg)
  20919. (when (or (looking-at org-outline-regexp)
  20920. (re-search-forward org-outline-regexp-bol end t))
  20921. (setq level (org-outline-level)))
  20922. (goto-char end)
  20923. (skip-chars-forward " \t\r\n\v\f")
  20924. (if (or (eobp)
  20925. (and (bolp) (looking-at org-outline-regexp)
  20926. (<= (org-outline-level) level)))
  20927. nil ; Nothing would be swallowed
  20928. t))))) ; something would swallow
  20929. (define-key org-mode-map "\C-y" 'org-yank)
  20930. (defun org-truely-invisible-p ()
  20931. "Check if point is at a character currently not visible.
  20932. This version does not only check the character property, but also
  20933. `visible-mode'."
  20934. ;; Early versions of noutline don't have `outline-invisible-p'.
  20935. (if (org-bound-and-true-p visible-mode)
  20936. nil
  20937. (outline-invisible-p)))
  20938. (defun org-invisible-p2 ()
  20939. "Check if point is at a character currently not visible."
  20940. (save-excursion
  20941. (if (and (eolp) (not (bobp))) (backward-char 1))
  20942. ;; Early versions of noutline don't have `outline-invisible-p'.
  20943. (outline-invisible-p)))
  20944. (defun org-back-to-heading (&optional invisible-ok)
  20945. "Call `outline-back-to-heading', but provide a better error message."
  20946. (condition-case nil
  20947. (outline-back-to-heading invisible-ok)
  20948. (error (error "Before first headline at position %d in buffer %s"
  20949. (point) (current-buffer)))))
  20950. (defun org-before-first-heading-p ()
  20951. "Before first heading?"
  20952. (save-excursion
  20953. (end-of-line)
  20954. (null (re-search-backward org-outline-regexp-bol nil t))))
  20955. (defun org-at-heading-p (&optional ignored)
  20956. (outline-on-heading-p t))
  20957. ;; Compatibility alias with Org versions < 7.8.03
  20958. (defalias 'org-on-heading-p 'org-at-heading-p)
  20959. (defun org-at-comment-p nil
  20960. "Is cursor in a line starting with a # character?"
  20961. (save-excursion
  20962. (beginning-of-line)
  20963. (looking-at "^#")))
  20964. (defun org-at-drawer-p nil
  20965. "Is cursor at a drawer keyword?"
  20966. (save-excursion
  20967. (move-beginning-of-line 1)
  20968. (looking-at org-drawer-regexp)))
  20969. (defun org-at-block-p nil
  20970. "Is cursor at a block keyword?"
  20971. (save-excursion
  20972. (move-beginning-of-line 1)
  20973. (looking-at org-block-regexp)))
  20974. (defun org-point-at-end-of-empty-headline ()
  20975. "If point is at the end of an empty headline, return t, else nil.
  20976. If the heading only contains a TODO keyword, it is still still considered
  20977. empty."
  20978. (and (looking-at "[ \t]*$")
  20979. (when org-todo-line-regexp
  20980. (save-excursion
  20981. (beginning-of-line 1)
  20982. (let ((case-fold-search nil))
  20983. (looking-at org-todo-line-regexp)
  20984. (string= (match-string 3) ""))))))
  20985. (defun org-at-heading-or-item-p ()
  20986. (or (org-at-heading-p) (org-at-item-p)))
  20987. (defun org-at-target-p ()
  20988. (or (org-in-regexp org-radio-target-regexp)
  20989. (org-in-regexp org-target-regexp)))
  20990. ;; Compatibility alias with Org versions < 7.8.03
  20991. (defalias 'org-on-target-p 'org-at-target-p)
  20992. (defun org-up-heading-all (arg)
  20993. "Move to the heading line of which the present line is a subheading.
  20994. This function considers both visible and invisible heading lines.
  20995. With argument, move up ARG levels."
  20996. (if (fboundp 'outline-up-heading-all)
  20997. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20998. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20999. (defun org-up-heading-safe ()
  21000. "Move to the heading line of which the present line is a subheading.
  21001. This version will not throw an error. It will return the level of the
  21002. headline found, or nil if no higher level is found.
  21003. Also, this function will be a lot faster than `outline-up-heading',
  21004. because it relies on stars being the outline starters. This can really
  21005. make a significant difference in outlines with very many siblings."
  21006. (let (start-level re)
  21007. (org-back-to-heading t)
  21008. (setq start-level (funcall outline-level))
  21009. (if (equal start-level 1)
  21010. nil
  21011. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21012. (if (re-search-backward re nil t)
  21013. (funcall outline-level)))))
  21014. (defun org-first-sibling-p ()
  21015. "Is this heading the first child of its parents?"
  21016. (interactive)
  21017. (let ((re org-outline-regexp-bol)
  21018. level l)
  21019. (unless (org-at-heading-p t)
  21020. (user-error "Not at a heading"))
  21021. (setq level (funcall outline-level))
  21022. (save-excursion
  21023. (if (not (re-search-backward re nil t))
  21024. t
  21025. (setq l (funcall outline-level))
  21026. (< l level)))))
  21027. (defun org-goto-sibling (&optional previous)
  21028. "Goto the next sibling, even if it is invisible.
  21029. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21030. when a sibling was found. When none is found, return nil and don't
  21031. move point."
  21032. (let ((fun (if previous 're-search-backward 're-search-forward))
  21033. (pos (point))
  21034. (re org-outline-regexp-bol)
  21035. level l)
  21036. (when (condition-case nil (org-back-to-heading t) (error nil))
  21037. (setq level (funcall outline-level))
  21038. (catch 'exit
  21039. (or previous (forward-char 1))
  21040. (while (funcall fun re nil t)
  21041. (setq l (funcall outline-level))
  21042. (when (< l level) (goto-char pos) (throw 'exit nil))
  21043. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21044. (goto-char pos)
  21045. nil))))
  21046. (defun org-show-siblings ()
  21047. "Show all siblings of the current headline."
  21048. (save-excursion
  21049. (while (org-goto-sibling) (org-flag-heading nil)))
  21050. (save-excursion
  21051. (while (org-goto-sibling 'previous)
  21052. (org-flag-heading nil))))
  21053. (defun org-goto-first-child ()
  21054. "Goto the first child, even if it is invisible.
  21055. Return t when a child was found. Otherwise don't move point and
  21056. return nil."
  21057. (let (level (pos (point)) (re org-outline-regexp-bol))
  21058. (when (condition-case nil (org-back-to-heading t) (error nil))
  21059. (setq level (outline-level))
  21060. (forward-char 1)
  21061. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21062. (progn (goto-char (match-beginning 0)) t)
  21063. (goto-char pos) nil))))
  21064. (defun org-show-hidden-entry ()
  21065. "Show an entry where even the heading is hidden."
  21066. (save-excursion
  21067. (org-show-entry)))
  21068. (defun org-flag-heading (flag &optional entry)
  21069. "Flag the current heading. FLAG non-nil means make invisible.
  21070. When ENTRY is non-nil, show the entire entry."
  21071. (save-excursion
  21072. (org-back-to-heading t)
  21073. ;; Check if we should show the entire entry
  21074. (if entry
  21075. (progn
  21076. (org-show-entry)
  21077. (save-excursion
  21078. (and (outline-next-heading)
  21079. (org-flag-heading nil))))
  21080. (outline-flag-region (max (point-min) (1- (point)))
  21081. (save-excursion (outline-end-of-heading) (point))
  21082. flag))))
  21083. (defun org-get-next-sibling ()
  21084. "Move to next heading of the same level, and return point.
  21085. If there is no such heading, return nil.
  21086. This is like outline-next-sibling, but invisible headings are ok."
  21087. (let ((level (funcall outline-level)))
  21088. (outline-next-heading)
  21089. (while (and (not (eobp)) (> (funcall outline-level) level))
  21090. (outline-next-heading))
  21091. (if (or (eobp) (< (funcall outline-level) level))
  21092. nil
  21093. (point))))
  21094. (defun org-get-last-sibling ()
  21095. "Move to previous heading of the same level, and return point.
  21096. If there is no such heading, return nil."
  21097. (let ((opoint (point))
  21098. (level (funcall outline-level)))
  21099. (outline-previous-heading)
  21100. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21101. (while (and (> (funcall outline-level) level)
  21102. (not (bobp)))
  21103. (outline-previous-heading))
  21104. (if (< (funcall outline-level) level)
  21105. nil
  21106. (point)))))
  21107. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21108. "Goto to the end of a subtree."
  21109. ;; This contains an exact copy of the original function, but it uses
  21110. ;; `org-back-to-heading', to make it work also in invisible
  21111. ;; trees. And is uses an invisible-ok argument.
  21112. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21113. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21114. ;; with many children and grandchildren etc, this can be much faster
  21115. ;; than the outline version.
  21116. (org-back-to-heading invisible-ok)
  21117. (let ((first t)
  21118. (level (funcall outline-level)))
  21119. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21120. ;; A true heading (not a plain list item), in Org-mode
  21121. ;; This means we can easily find the end by looking
  21122. ;; only for the right number of stars. Using a regexp to do
  21123. ;; this is so much faster than using a Lisp loop.
  21124. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21125. (forward-char 1)
  21126. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21127. ;; something else, do it the slow way
  21128. (while (and (not (eobp))
  21129. (or first (> (funcall outline-level) level)))
  21130. (setq first nil)
  21131. (outline-next-heading)))
  21132. (unless to-heading
  21133. (if (memq (preceding-char) '(?\n ?\^M))
  21134. (progn
  21135. ;; Go to end of line before heading
  21136. (forward-char -1)
  21137. (if (memq (preceding-char) '(?\n ?\^M))
  21138. ;; leave blank line before heading
  21139. (forward-char -1))))))
  21140. (point))
  21141. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21142. "Use Org version in org-mode, for dramatic speed-up."
  21143. (if (derived-mode-p 'org-mode)
  21144. (progn
  21145. (org-end-of-subtree nil t)
  21146. (unless (eobp) (backward-char 1)))
  21147. ad-do-it))
  21148. (defun org-end-of-meta-data-and-drawers ()
  21149. "Jump to the first text after meta data and drawers in the current entry.
  21150. This will move over empty lines, lines with planning time stamps,
  21151. clocking lines, and drawers."
  21152. (org-back-to-heading t)
  21153. (let ((end (save-excursion (outline-next-heading) (point)))
  21154. (re (concat "\\(" org-drawer-regexp "\\)"
  21155. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21156. (forward-line 1)
  21157. (while (re-search-forward re end t)
  21158. (if (not (match-end 1))
  21159. ;; empty or planning line
  21160. (forward-line 1)
  21161. ;; a drawer, find the end
  21162. (re-search-forward "^[ \t]*:END:" end 'move)
  21163. (forward-line 1)))
  21164. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21165. (point)))
  21166. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21167. "Move forward to the ARG'th subheading at same level as this one.
  21168. Stop at the first and last subheadings of a superior heading.
  21169. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21170. non-nil it will also look at invisible ones."
  21171. (interactive "p")
  21172. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21173. (if (and arg (< arg 0))
  21174. (goto-char (point-min))
  21175. (outline-next-heading))
  21176. (org-at-heading-p)
  21177. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21178. (f (if (and arg (< arg 0))
  21179. 're-search-backward
  21180. 're-search-forward))
  21181. (count (if arg (abs arg) 1))
  21182. (result (point)))
  21183. (while (and (prog1 (> count 0)
  21184. (forward-char (if (and arg (< arg 0)) -1 1)))
  21185. (funcall f org-outline-regexp-bol nil 'move))
  21186. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21187. (cond ((< l level) (setq count 0))
  21188. ((and (= l level)
  21189. (or invisible-ok
  21190. (progn
  21191. (goto-char (line-beginning-position))
  21192. (not (outline-invisible-p)))))
  21193. (setq count (1- count))
  21194. (when (eq l level)
  21195. (setq result (point)))))))
  21196. (goto-char result))
  21197. (beginning-of-line 1)))
  21198. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21199. "Move backward to the ARG'th subheading at same level as this one.
  21200. Stop at the first and last subheadings of a superior heading."
  21201. (interactive "p")
  21202. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21203. (defun org-next-block (arg &optional backward block-regexp)
  21204. "Jump to the next block.
  21205. With a prefix argument ARG, jump forward ARG many source blocks.
  21206. When BACKWARD is non-nil, jump to the previous block.
  21207. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21208. (interactive "p")
  21209. (let ((re (or block-regexp org-block-regexp))
  21210. (re-search-fn (or (and backward 're-search-backward)
  21211. 're-search-forward)))
  21212. (if (looking-at re) (forward-char 1))
  21213. (condition-case nil
  21214. (funcall re-search-fn re nil nil arg)
  21215. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21216. (goto-char (match-beginning 0)) (org-show-context)))
  21217. (defun org-previous-block (arg &optional block-regexp)
  21218. "Jump to the previous block.
  21219. With a prefix argument ARG, jump backward ARG many source blocks.
  21220. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21221. (interactive "p")
  21222. (org-next-block arg t block-regexp))
  21223. (defun org-forward-element ()
  21224. "Move forward by one element.
  21225. Move to the next element at the same level, when possible."
  21226. (interactive)
  21227. (cond ((eobp) (user-error "Cannot move further down"))
  21228. ((org-with-limited-levels (org-at-heading-p))
  21229. (let ((origin (point)))
  21230. (goto-char (org-end-of-subtree nil t))
  21231. (unless (org-with-limited-levels (org-at-heading-p))
  21232. (goto-char origin)
  21233. (user-error "Cannot move further down"))))
  21234. (t
  21235. (let* ((elem (org-element-at-point))
  21236. (end (org-element-property :end elem))
  21237. (parent (org-element-property :parent elem)))
  21238. (if (and parent (= (org-element-property :contents-end parent) end))
  21239. (goto-char (org-element-property :end parent))
  21240. (goto-char end))))))
  21241. (defun org-backward-element ()
  21242. "Move backward by one element.
  21243. Move to the previous element at the same level, when possible."
  21244. (interactive)
  21245. (cond ((bobp) (user-error "Cannot move further up"))
  21246. ((org-with-limited-levels (org-at-heading-p))
  21247. ;; At a headline, move to the previous one, if any, or stay
  21248. ;; here.
  21249. (let ((origin (point)))
  21250. (org-with-limited-levels (org-backward-heading-same-level 1))
  21251. ;; When current headline has no sibling above, move to its
  21252. ;; parent.
  21253. (when (= (point) origin)
  21254. (or (org-with-limited-levels (org-up-heading-safe))
  21255. (progn (goto-char origin)
  21256. (user-error "Cannot move further up"))))))
  21257. (t
  21258. (let* ((trail (org-element-at-point 'keep-trail))
  21259. (elem (car trail))
  21260. (prev-elem (nth 1 trail))
  21261. (beg (org-element-property :begin elem)))
  21262. (cond
  21263. ;; Move to beginning of current element if point isn't
  21264. ;; there already.
  21265. ((/= (point) beg) (goto-char beg))
  21266. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21267. ((org-before-first-heading-p) (goto-char (point-min)))
  21268. (t (org-back-to-heading)))))))
  21269. (defun org-up-element ()
  21270. "Move to upper element."
  21271. (interactive)
  21272. (if (org-with-limited-levels (org-at-heading-p))
  21273. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21274. (let* ((elem (org-element-at-point))
  21275. (parent (org-element-property :parent elem)))
  21276. (if parent (goto-char (org-element-property :begin parent))
  21277. (if (org-with-limited-levels (org-before-first-heading-p))
  21278. (user-error "No surrounding element")
  21279. (org-with-limited-levels (org-back-to-heading)))))))
  21280. (defvar org-element-greater-elements)
  21281. (defun org-down-element ()
  21282. "Move to inner element."
  21283. (interactive)
  21284. (let ((element (org-element-at-point)))
  21285. (cond
  21286. ((memq (org-element-type element) '(plain-list table))
  21287. (goto-char (org-element-property :contents-begin element))
  21288. (forward-char))
  21289. ((memq (org-element-type element) org-element-greater-elements)
  21290. ;; If contents are hidden, first disclose them.
  21291. (when (org-element-property :hiddenp element) (org-cycle))
  21292. (goto-char (or (org-element-property :contents-begin element)
  21293. (user-error "No content for this element"))))
  21294. (t (user-error "No inner element")))))
  21295. (defun org-drag-element-backward ()
  21296. "Move backward element at point."
  21297. (interactive)
  21298. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21299. (let* ((trail (org-element-at-point 'keep-trail))
  21300. (elem (car trail))
  21301. (prev-elem (nth 1 trail)))
  21302. ;; Error out if no previous element or previous element is
  21303. ;; a parent of the current one.
  21304. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21305. (user-error "Cannot drag element backward")
  21306. (let ((pos (point)))
  21307. (org-element-swap-A-B prev-elem elem)
  21308. (goto-char (+ (org-element-property :begin prev-elem)
  21309. (- pos (org-element-property :begin elem)))))))))
  21310. (defun org-drag-element-forward ()
  21311. "Move forward element at point."
  21312. (interactive)
  21313. (let* ((pos (point))
  21314. (elem (org-element-at-point)))
  21315. (when (= (point-max) (org-element-property :end elem))
  21316. (user-error "Cannot drag element forward"))
  21317. (goto-char (org-element-property :end elem))
  21318. (let ((next-elem (org-element-at-point)))
  21319. (when (or (org-element-nested-p elem next-elem)
  21320. (and (eq (org-element-type next-elem) 'headline)
  21321. (not (eq (org-element-type elem) 'headline))))
  21322. (goto-char pos)
  21323. (user-error "Cannot drag element forward"))
  21324. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21325. ;; body's length (without final blanks) and by the length of
  21326. ;; blanks between ELEM and NEXT-ELEM.
  21327. (let ((size-next (- (save-excursion
  21328. (goto-char (org-element-property :end next-elem))
  21329. (skip-chars-backward " \r\t\n")
  21330. (forward-line)
  21331. ;; Small correction if buffer doesn't end
  21332. ;; with a newline character.
  21333. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21334. (org-element-property :begin next-elem)))
  21335. (size-blank (- (org-element-property :end elem)
  21336. (save-excursion
  21337. (goto-char (org-element-property :end elem))
  21338. (skip-chars-backward " \r\t\n")
  21339. (forward-line)
  21340. (point)))))
  21341. (org-element-swap-A-B elem next-elem)
  21342. (goto-char (+ pos size-next size-blank))))))
  21343. (defun org-drag-line-forward (arg)
  21344. "Drag the line at point ARG lines forward."
  21345. (interactive "p")
  21346. (dotimes (n (abs arg))
  21347. (let ((c (current-column)))
  21348. (if (< 0 arg)
  21349. (progn
  21350. (beginning-of-line 2)
  21351. (transpose-lines 1)
  21352. (beginning-of-line 0))
  21353. (transpose-lines 1)
  21354. (beginning-of-line -1))
  21355. (org-move-to-column c))))
  21356. (defun org-drag-line-backward (arg)
  21357. "Drag the line at point ARG lines backward."
  21358. (interactive "p")
  21359. (org-drag-line-forward (- arg)))
  21360. (defun org-mark-element ()
  21361. "Put point at beginning of this element, mark at end.
  21362. Interactively, if this command is repeated or (in Transient Mark
  21363. mode) if the mark is active, it marks the next element after the
  21364. ones already marked."
  21365. (interactive)
  21366. (let (deactivate-mark)
  21367. (if (and (org-called-interactively-p 'any)
  21368. (or (and (eq last-command this-command) (mark t))
  21369. (and transient-mark-mode mark-active)))
  21370. (set-mark
  21371. (save-excursion
  21372. (goto-char (mark))
  21373. (goto-char (org-element-property :end (org-element-at-point)))))
  21374. (let ((element (org-element-at-point)))
  21375. (end-of-line)
  21376. (push-mark (org-element-property :end element) t t)
  21377. (goto-char (org-element-property :begin element))))))
  21378. (defun org-narrow-to-element ()
  21379. "Narrow buffer to current element."
  21380. (interactive)
  21381. (let ((elem (org-element-at-point)))
  21382. (cond
  21383. ((eq (car elem) 'headline)
  21384. (narrow-to-region
  21385. (org-element-property :begin elem)
  21386. (org-element-property :end elem)))
  21387. ((memq (car elem) org-element-greater-elements)
  21388. (narrow-to-region
  21389. (org-element-property :contents-begin elem)
  21390. (org-element-property :contents-end elem)))
  21391. (t
  21392. (narrow-to-region
  21393. (org-element-property :begin elem)
  21394. (org-element-property :end elem))))))
  21395. (defun org-transpose-element ()
  21396. "Transpose current and previous elements, keeping blank lines between.
  21397. Point is moved after both elements."
  21398. (interactive)
  21399. (org-skip-whitespace)
  21400. (let ((end (org-element-property :end (org-element-at-point))))
  21401. (org-drag-element-backward)
  21402. (goto-char end)))
  21403. (defun org-unindent-buffer ()
  21404. "Un-indent the visible part of the buffer.
  21405. Relative indentation (between items, inside blocks, etc.) isn't
  21406. modified."
  21407. (interactive)
  21408. (unless (eq major-mode 'org-mode)
  21409. (user-error "Cannot un-indent a buffer not in Org mode"))
  21410. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21411. unindent-tree ; For byte-compiler.
  21412. (unindent-tree
  21413. (function
  21414. (lambda (contents)
  21415. (mapc
  21416. (lambda (element)
  21417. (if (memq (org-element-type element) '(headline section))
  21418. (funcall unindent-tree (org-element-contents element))
  21419. (save-excursion
  21420. (save-restriction
  21421. (narrow-to-region
  21422. (org-element-property :begin element)
  21423. (org-element-property :end element))
  21424. (org-do-remove-indentation)))))
  21425. (reverse contents))))))
  21426. (funcall unindent-tree (org-element-contents parse-tree))))
  21427. (defun org-show-subtree ()
  21428. "Show everything after this heading at deeper levels."
  21429. (interactive)
  21430. (outline-flag-region
  21431. (point)
  21432. (save-excursion
  21433. (org-end-of-subtree t t))
  21434. nil))
  21435. (defun org-show-entry ()
  21436. "Show the body directly following this heading.
  21437. Show the heading too, if it is currently invisible."
  21438. (interactive)
  21439. (save-excursion
  21440. (condition-case nil
  21441. (progn
  21442. (org-back-to-heading t)
  21443. (outline-flag-region
  21444. (max (point-min) (1- (point)))
  21445. (save-excursion
  21446. (if (re-search-forward
  21447. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21448. (match-beginning 1)
  21449. (point-max)))
  21450. nil)
  21451. (org-cycle-hide-drawers 'children))
  21452. (error nil))))
  21453. (defun org-make-options-regexp (kwds &optional extra)
  21454. "Make a regular expression for keyword lines."
  21455. (concat
  21456. "^#\\+\\("
  21457. (mapconcat 'regexp-quote kwds "\\|")
  21458. (if extra (concat "\\|" extra))
  21459. "\\):[ \t]*\\(.*\\)"))
  21460. ;; Make isearch reveal the necessary context
  21461. (defun org-isearch-end ()
  21462. "Reveal context after isearch exits."
  21463. (when isearch-success ; only if search was successful
  21464. (if (featurep 'xemacs)
  21465. ;; Under XEmacs, the hook is run in the correct place,
  21466. ;; we directly show the context.
  21467. (org-show-context 'isearch)
  21468. ;; In Emacs the hook runs *before* restoring the overlays.
  21469. ;; So we have to use a one-time post-command-hook to do this.
  21470. ;; (Emacs 22 has a special variable, see function `org-mode')
  21471. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21472. isearch-mode-end-hook-quit)
  21473. ;; Only when the isearch was not quitted.
  21474. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21475. 'append 'local)))
  21476. (org-fix-ellipsis-at-bol)))
  21477. (defun org-isearch-post-command ()
  21478. "Remove self from hook, and show context."
  21479. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21480. (org-show-context 'isearch))
  21481. ;;;; Integration with and fixes for other packages
  21482. ;;; Imenu support
  21483. (defvar org-imenu-markers nil
  21484. "All markers currently used by Imenu.")
  21485. (make-variable-buffer-local 'org-imenu-markers)
  21486. (defun org-imenu-new-marker (&optional pos)
  21487. "Return a new marker for use by Imenu, and remember the marker."
  21488. (let ((m (make-marker)))
  21489. (move-marker m (or pos (point)))
  21490. (push m org-imenu-markers)
  21491. m))
  21492. (defun org-imenu-get-tree ()
  21493. "Produce the index for Imenu."
  21494. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21495. (setq org-imenu-markers nil)
  21496. (let* ((n org-imenu-depth)
  21497. (re (concat "^" (org-get-limited-outline-regexp)))
  21498. (subs (make-vector (1+ n) nil))
  21499. (last-level 0)
  21500. m level head0 head)
  21501. (save-excursion
  21502. (save-restriction
  21503. (widen)
  21504. (goto-char (point-max))
  21505. (while (re-search-backward re nil t)
  21506. (setq level (org-reduced-level (funcall outline-level)))
  21507. (when (and (<= level n)
  21508. (looking-at org-complex-heading-regexp)
  21509. (setq head0 (org-match-string-no-properties 4)))
  21510. (setq head (org-link-display-format head0)
  21511. m (org-imenu-new-marker))
  21512. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21513. (if (>= level last-level)
  21514. (push (cons head m) (aref subs level))
  21515. (push (cons head (aref subs (1+ level))) (aref subs level))
  21516. (loop for i from (1+ level) to n do (aset subs i nil)))
  21517. (setq last-level level)))))
  21518. (aref subs 1)))
  21519. (eval-after-load "imenu"
  21520. '(progn
  21521. (add-hook 'imenu-after-jump-hook
  21522. (lambda ()
  21523. (if (derived-mode-p 'org-mode)
  21524. (org-show-context 'org-goto))))))
  21525. (defun org-link-display-format (link)
  21526. "Replace a link with its the description.
  21527. If there is no description, use the link target."
  21528. (save-match-data
  21529. (if (string-match org-bracket-link-analytic-regexp link)
  21530. (replace-match (if (match-end 5)
  21531. (match-string 5 link)
  21532. (concat (match-string 1 link)
  21533. (match-string 3 link)))
  21534. nil t link)
  21535. link)))
  21536. (defun org-toggle-link-display ()
  21537. "Toggle the literal or descriptive display of links."
  21538. (interactive)
  21539. (if org-descriptive-links
  21540. (progn (org-remove-from-invisibility-spec '(org-link))
  21541. (org-restart-font-lock)
  21542. (setq org-descriptive-links nil))
  21543. (progn (add-to-invisibility-spec '(org-link))
  21544. (org-restart-font-lock)
  21545. (setq org-descriptive-links t))))
  21546. ;; Speedbar support
  21547. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21548. "Overlay marking the agenda restriction line in speedbar.")
  21549. (overlay-put org-speedbar-restriction-lock-overlay
  21550. 'face 'org-agenda-restriction-lock)
  21551. (overlay-put org-speedbar-restriction-lock-overlay
  21552. 'help-echo "Agendas are currently limited to this item.")
  21553. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21554. (defun org-speedbar-set-agenda-restriction ()
  21555. "Restrict future agenda commands to the location at point in speedbar.
  21556. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21557. (interactive)
  21558. (require 'org-agenda)
  21559. (let (p m tp np dir txt)
  21560. (cond
  21561. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21562. 'org-imenu t))
  21563. (setq m (get-text-property p 'org-imenu-marker))
  21564. (with-current-buffer (marker-buffer m)
  21565. (goto-char m)
  21566. (org-agenda-set-restriction-lock 'subtree)))
  21567. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21568. 'speedbar-function 'speedbar-find-file))
  21569. (setq tp (previous-single-property-change
  21570. (1+ p) 'speedbar-function)
  21571. np (next-single-property-change
  21572. tp 'speedbar-function)
  21573. dir (speedbar-line-directory)
  21574. txt (buffer-substring-no-properties (or tp (point-min))
  21575. (or np (point-max))))
  21576. (with-current-buffer (find-file-noselect
  21577. (let ((default-directory dir))
  21578. (expand-file-name txt)))
  21579. (unless (derived-mode-p 'org-mode)
  21580. (user-error "Cannot restrict to non-Org-mode file"))
  21581. (org-agenda-set-restriction-lock 'file)))
  21582. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21583. (move-overlay org-speedbar-restriction-lock-overlay
  21584. (point-at-bol) (point-at-eol))
  21585. (setq current-prefix-arg nil)
  21586. (org-agenda-maybe-redo)))
  21587. (eval-after-load "speedbar"
  21588. '(progn
  21589. (speedbar-add-supported-extension ".org")
  21590. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21591. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21592. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21593. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21594. (add-hook 'speedbar-visiting-tag-hook
  21595. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21596. ;;; Fixes and Hacks for problems with other packages
  21597. ;; Make flyspell not check words in links, to not mess up our keymap
  21598. (defvar org-element-affiliated-keywords) ; From org-element.el
  21599. (defvar org-element-block-name-alist) ; From org-element.el
  21600. (defun org-mode-flyspell-verify ()
  21601. "Don't let flyspell put overlays at active buttons, or on
  21602. {todo,all-time,additional-option-like}-keywords."
  21603. (require 'org-element) ; For `org-element-affiliated-keywords'
  21604. (let ((pos (max (1- (point)) (point-min)))
  21605. (word (thing-at-point 'word)))
  21606. (and (not (get-text-property pos 'keymap))
  21607. (not (get-text-property pos 'org-no-flyspell))
  21608. (not (member word org-todo-keywords-1))
  21609. (not (member word org-all-time-keywords))
  21610. (not (member word org-options-keywords))
  21611. (not (member word (mapcar 'car org-startup-options)))
  21612. (not (member-ignore-case word org-element-affiliated-keywords))
  21613. (not (member-ignore-case word (org-get-export-keywords)))
  21614. (not (member-ignore-case
  21615. word (mapcar 'car org-element-block-name-alist)))
  21616. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21617. (defun org-remove-flyspell-overlays-in (beg end)
  21618. "Remove flyspell overlays in region."
  21619. (and (org-bound-and-true-p flyspell-mode)
  21620. (fboundp 'flyspell-delete-region-overlays)
  21621. (flyspell-delete-region-overlays beg end))
  21622. (add-text-properties beg end '(org-no-flyspell t)))
  21623. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21624. (eval-after-load "bookmark"
  21625. '(if (boundp 'bookmark-after-jump-hook)
  21626. ;; We can use the hook
  21627. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21628. ;; Hook not available, use advice
  21629. (defadvice bookmark-jump (after org-make-visible activate)
  21630. "Make the position visible."
  21631. (org-bookmark-jump-unhide))))
  21632. ;; Make sure saveplace shows the location if it was hidden
  21633. (eval-after-load "saveplace"
  21634. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21635. "Make the position visible."
  21636. (org-bookmark-jump-unhide)))
  21637. ;; Make sure ecb shows the location if it was hidden
  21638. (eval-after-load "ecb"
  21639. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21640. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21641. (if (derived-mode-p 'org-mode)
  21642. (org-show-context))))
  21643. (defun org-bookmark-jump-unhide ()
  21644. "Unhide the current position, to show the bookmark location."
  21645. (and (derived-mode-p 'org-mode)
  21646. (or (outline-invisible-p)
  21647. (save-excursion (goto-char (max (point-min) (1- (point))))
  21648. (outline-invisible-p)))
  21649. (org-show-context 'bookmark-jump)))
  21650. ;; Make session.el ignore our circular variable
  21651. (eval-after-load "session"
  21652. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21653. ;;;; Finish up
  21654. (provide 'org)
  21655. (run-hooks 'org-load-hook)
  21656. ;;; org.el ends here