org.el 622 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.28trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.28trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let ((version (format "Org-mode version %s" org-version)))
  96. (message version)
  97. (if here
  98. (insert version))))
  99. ;;; Compatibility constants
  100. ;;; The custom variables
  101. (defgroup org nil
  102. "Outline-based notes management and organizer."
  103. :tag "Org"
  104. :group 'outlines
  105. :group 'hypermedia
  106. :group 'calendar)
  107. (defcustom org-load-hook nil
  108. "Hook that is run after org.el has been loaded."
  109. :group 'org
  110. :type 'hook)
  111. (defvar org-modules) ; defined below
  112. (defvar org-modules-loaded nil
  113. "Have the modules been loaded already?")
  114. (defun org-load-modules-maybe (&optional force)
  115. "Load all extensions listed in `org-modules'."
  116. (when (or force (not org-modules-loaded))
  117. (mapc (lambda (ext)
  118. (condition-case nil (require ext)
  119. (error (message "Problems while trying to load feature `%s'" ext))))
  120. org-modules)
  121. (setq org-modules-loaded t)))
  122. (defun org-set-modules (var value)
  123. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  124. (set var value)
  125. (when (featurep 'org)
  126. (org-load-modules-maybe 'force)))
  127. (when (org-bound-and-true-p org-modules)
  128. (let ((a (member 'org-infojs org-modules)))
  129. (and a (setcar a 'org-jsinfo))))
  130. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  131. "Modules that should always be loaded together with org.el.
  132. If a description starts with <C>, the file is not part of Emacs
  133. and loading it will require that you have downloaded and properly installed
  134. the org-mode distribution.
  135. You can also use this system to load external packages (i.e. neither Org
  136. core modules, not modules from the CONTRIB directory). Just add symbols
  137. to the end of the list. If the package is called org-xyz.el, then you need
  138. to add the symbol `xyz', and the package must have a call to
  139. (provide 'org-xyz)"
  140. :group 'org
  141. :set 'org-set-modules
  142. :type
  143. '(set :greedy t
  144. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  145. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  146. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  147. (const :tag " id: Global IDs for identifying entries" org-id)
  148. (const :tag " info: Links to Info nodes" org-info)
  149. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  150. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  151. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  152. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  153. (const :tag " mew Links to Mew folders/messages" org-mew)
  154. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  155. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  156. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  157. (const :tag " vm: Links to VM folders/messages" org-vm)
  158. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  159. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  160. (const :tag " mouse: Additional mouse support" org-mouse)
  161. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  162. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  163. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  164. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  165. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  166. (const :tag "C collector: Collect properties into tables" org-collector)
  167. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  168. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  169. (const :tag "C eval: Include command output as text" org-eval)
  170. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  171. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  172. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  175. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  176. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  177. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  178. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  179. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  180. (const :tag "C R: Computation using the R language" org-R)
  181. (const :tag "C registry: A registry for Org links" org-registry)
  182. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  183. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  184. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  185. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  186. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  187. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  188. (defcustom org-support-shift-select nil
  189. "Non-nil means, make shift-cursor commands select text when possible.
  190. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  191. selecting a region, or enlarge thusly regions started in this way.
  192. In Org-mode, in special contexts, these same keys are used for other
  193. purposes, important enough to compete with shift selection. Org tries
  194. to balance these needs by supporting `shift-select-mode' outside these
  195. special contexts, under control of this variable.
  196. The default of this variable is nil, to avoid confusing behavior. Shifted
  197. cursor keys will then execute Org commands in the following contexts:
  198. - on a headline, changing TODO state (left/right) and priority (up/down)
  199. - on a time stamp, changing the time
  200. - in a plain list item, changing the bullet type
  201. - in a property definition line, switching between allowed values
  202. - in the BEGIN line of a clock table (changing the time block).
  203. Outside these contexts, the commands will throw an error.
  204. When this variable is t and the cursor is not in a special context,
  205. Org-mode will support shift-selection for making and enlarging regions.
  206. To make this more effective, the bullet cycling will no longer happen
  207. anywhere in an item line, but only if the cursor is exactly on the bullet.
  208. If you set this variable to the symbol `always', then the keys
  209. will not be special in headlines, property lines, and item lines, to make
  210. shift selection work there as well. If this is what you want, you can
  211. use the following alternative commands: `C-c C-t' and `C-c ,' to
  212. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  213. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  214. edited by hand or in column view.
  215. However, when the cursor is on a timestamp, shift-cursor commands
  216. will still edit the time stamp - this is just too good to give up.
  217. XEmacs user should have this variable set to nil, because shift-select-mode
  218. is Emacs 23 only."
  219. :group 'org
  220. :type '(choice
  221. (const :tag "Never" nil)
  222. (const :tag "When outside special context" t)
  223. (const :tag "Everywhere except timestamps" always)))
  224. (defgroup org-startup nil
  225. "Options concerning startup of Org-mode."
  226. :tag "Org Startup"
  227. :group 'org)
  228. (defcustom org-startup-folded t
  229. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  230. This can also be configured on a per-file basis by adding one of
  231. the following lines anywhere in the buffer:
  232. #+STARTUP: fold
  233. #+STARTUP: nofold
  234. #+STARTUP: content"
  235. :group 'org-startup
  236. :type '(choice
  237. (const :tag "nofold: show all" nil)
  238. (const :tag "fold: overview" t)
  239. (const :tag "content: all headlines" content)))
  240. (defcustom org-startup-truncated t
  241. "Non-nil means, entering Org-mode will set `truncate-lines'.
  242. This is useful since some lines containing links can be very long and
  243. uninteresting. Also tables look terrible when wrapped."
  244. :group 'org-startup
  245. :type 'boolean)
  246. (defcustom org-startup-align-all-tables nil
  247. "Non-nil means, align all tables when visiting a file.
  248. This is useful when the column width in tables is forced with <N> cookies
  249. in table fields. Such tables will look correct only after the first re-align.
  250. This can also be configured on a per-file basis by adding one of
  251. the following lines anywhere in the buffer:
  252. #+STARTUP: align
  253. #+STARTUP: noalign"
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-insert-mode-line-in-empty-file nil
  257. "Non-nil means insert the first line setting Org-mode in empty files.
  258. When the function `org-mode' is called interactively in an empty file, this
  259. normally means that the file name does not automatically trigger Org-mode.
  260. To ensure that the file will always be in Org-mode in the future, a
  261. line enforcing Org-mode will be inserted into the buffer, if this option
  262. has been set."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-replace-disputed-keys nil
  266. "Non-nil means use alternative key bindings for some keys.
  267. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  268. These keys are also used by other packages like shift-selection-mode'
  269. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  270. If you want to use Org-mode together with one of these other modes,
  271. or more generally if you would like to move some Org-mode commands to
  272. other keys, set this variable and configure the keys with the variable
  273. `org-disputed-keys'.
  274. This option is only relevant at load-time of Org-mode, and must be set
  275. *before* org.el is loaded. Changing it requires a restart of Emacs to
  276. become effective."
  277. :group 'org-startup
  278. :type 'boolean)
  279. (defcustom org-use-extra-keys nil
  280. "Non-nil means use extra key sequence definitions for certain
  281. commands. This happens automatically if you run XEmacs or if
  282. window-system is nil. This variable lets you do the same
  283. manually. You must set it before loading org.
  284. Example: on Carbon Emacs 22 running graphically, with an external
  285. keyboard on a Powerbook, the default way of setting M-left might
  286. not work for either Alt or ESC. Setting this variable will make
  287. it work for ESC."
  288. :group 'org-startup
  289. :type 'boolean)
  290. (if (fboundp 'defvaralias)
  291. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  292. (defcustom org-disputed-keys
  293. '(([(shift up)] . [(meta p)])
  294. ([(shift down)] . [(meta n)])
  295. ([(shift left)] . [(meta -)])
  296. ([(shift right)] . [(meta +)])
  297. ([(control shift right)] . [(meta shift +)])
  298. ([(control shift left)] . [(meta shift -)]))
  299. "Keys for which Org-mode and other modes compete.
  300. This is an alist, cars are the default keys, second element specifies
  301. the alternative to use when `org-replace-disputed-keys' is t.
  302. Keys can be specified in any syntax supported by `define-key'.
  303. The value of this option takes effect only at Org-mode's startup,
  304. therefore you'll have to restart Emacs to apply it after changing."
  305. :group 'org-startup
  306. :type 'alist)
  307. (defun org-key (key)
  308. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  309. Or return the original if not disputed."
  310. (if org-replace-disputed-keys
  311. (let* ((nkey (key-description key))
  312. (x (org-find-if (lambda (x)
  313. (equal (key-description (car x)) nkey))
  314. org-disputed-keys)))
  315. (if x (cdr x) key))
  316. key))
  317. (defun org-find-if (predicate seq)
  318. (catch 'exit
  319. (while seq
  320. (if (funcall predicate (car seq))
  321. (throw 'exit (car seq))
  322. (pop seq)))))
  323. (defun org-defkey (keymap key def)
  324. "Define a key, possibly translated, as returned by `org-key'."
  325. (define-key keymap (org-key key) def))
  326. (defcustom org-ellipsis nil
  327. "The ellipsis to use in the Org-mode outline.
  328. When nil, just use the standard three dots. When a string, use that instead,
  329. When a face, use the standard 3 dots, but with the specified face.
  330. The change affects only Org-mode (which will then use its own display table).
  331. Changing this requires executing `M-x org-mode' in a buffer to become
  332. effective."
  333. :group 'org-startup
  334. :type '(choice (const :tag "Default" nil)
  335. (face :tag "Face" :value org-warning)
  336. (string :tag "String" :value "...#")))
  337. (defvar org-display-table nil
  338. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  339. (defgroup org-keywords nil
  340. "Keywords in Org-mode."
  341. :tag "Org Keywords"
  342. :group 'org)
  343. (defcustom org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-scheduled-string "SCHEDULED:"
  352. "String to mark scheduled TODO entries.
  353. A schedule is this string, followed by a time stamp. Should be a word,
  354. terminated by a colon. You can insert a schedule keyword and
  355. a timestamp with \\[org-schedule].
  356. Changes become only effective after restarting Emacs."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-closed-string "CLOSED:"
  360. "String used as the prefix for timestamps logging closing a TODO entry."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-clock-string "CLOCK:"
  364. "String used as prefix for timestamps clocking work hours on an item."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defcustom org-comment-string "COMMENT"
  368. "Entries starting with this keyword will never be exported.
  369. An entry can be toggled between COMMENT and normal with
  370. \\[org-toggle-comment].
  371. Changes become only effective after restarting Emacs."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defcustom org-quote-string "QUOTE"
  375. "Entries starting with this keyword will be exported in fixed-width font.
  376. Quoting applies only to the text in the entry following the headline, and does
  377. not extend beyond the next headline, even if that is lower level.
  378. An entry can be toggled between QUOTE and normal with
  379. \\[org-toggle-fixed-width-section]."
  380. :group 'org-keywords
  381. :type 'string)
  382. (defconst org-repeat-re
  383. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  384. "Regular expression for specifying repeated events.
  385. After a match, group 1 contains the repeat expression.")
  386. (defgroup org-structure nil
  387. "Options concerning the general structure of Org-mode files."
  388. :tag "Org Structure"
  389. :group 'org)
  390. (defgroup org-reveal-location nil
  391. "Options about how to make context of a location visible."
  392. :tag "Org Reveal Location"
  393. :group 'org-structure)
  394. (defconst org-context-choice
  395. '(choice
  396. (const :tag "Always" t)
  397. (const :tag "Never" nil)
  398. (repeat :greedy t :tag "Individual contexts"
  399. (cons
  400. (choice :tag "Context"
  401. (const agenda)
  402. (const org-goto)
  403. (const occur-tree)
  404. (const tags-tree)
  405. (const link-search)
  406. (const mark-goto)
  407. (const bookmark-jump)
  408. (const isearch)
  409. (const default))
  410. (boolean))))
  411. "Contexts for the reveal options.")
  412. (defcustom org-show-hierarchy-above '((default . t))
  413. "Non-nil means, show full hierarchy when revealing a location.
  414. Org-mode often shows locations in an org-mode file which might have
  415. been invisible before. When this is set, the hierarchy of headings
  416. above the exposed location is shown.
  417. Turning this off for example for sparse trees makes them very compact.
  418. Instead of t, this can also be an alist specifying this option for different
  419. contexts. Valid contexts are
  420. agenda when exposing an entry from the agenda
  421. org-goto when using the command `org-goto' on key C-c C-j
  422. occur-tree when using the command `org-occur' on key C-c /
  423. tags-tree when constructing a sparse tree based on tags matches
  424. link-search when exposing search matches associated with a link
  425. mark-goto when exposing the jump goal of a mark
  426. bookmark-jump when exposing a bookmark location
  427. isearch when exiting from an incremental search
  428. default default for all contexts not set explicitly"
  429. :group 'org-reveal-location
  430. :type org-context-choice)
  431. (defcustom org-show-following-heading '((default . nil))
  432. "Non-nil means, show following heading when revealing a location.
  433. Org-mode often shows locations in an org-mode file which might have
  434. been invisible before. When this is set, the heading following the
  435. match is shown.
  436. Turning this off for example for sparse trees makes them very compact,
  437. but makes it harder to edit the location of the match. In such a case,
  438. use the command \\[org-reveal] to show more context.
  439. Instead of t, this can also be an alist specifying this option for different
  440. contexts. See `org-show-hierarchy-above' for valid contexts."
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-siblings '((default . nil) (isearch t))
  444. "Non-nil means, show all sibling heading when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the sibling of the current entry
  447. heading are all made visible. If `org-show-hierarchy-above' is t,
  448. the same happens on each level of the hierarchy above the current entry.
  449. By default this is on for the isearch context, off for all other contexts.
  450. Turning this off for example for sparse trees makes them very compact,
  451. but makes it harder to edit the location of the match. In such a case,
  452. use the command \\[org-reveal] to show more context.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-show-entry-below '((default . nil))
  458. "Non-nil means, show the entry below a headline when revealing a location.
  459. Org-mode often shows locations in an org-mode file which might have
  460. been invisible before. When this is set, the text below the headline that is
  461. exposed is also shown.
  462. By default this is off for all contexts.
  463. Instead of t, this can also be an alist specifying this option for different
  464. contexts. See `org-show-hierarchy-above' for valid contexts."
  465. :group 'org-reveal-location
  466. :type org-context-choice)
  467. (defcustom org-indirect-buffer-display 'other-window
  468. "How should indirect tree buffers be displayed?
  469. This applies to indirect buffers created with the commands
  470. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  471. Valid values are:
  472. current-window Display in the current window
  473. other-window Just display in another window.
  474. dedicated-frame Create one new frame, and re-use it each time.
  475. new-frame Make a new frame each time. Note that in this case
  476. previously-made indirect buffers are kept, and you need to
  477. kill these buffers yourself."
  478. :group 'org-structure
  479. :group 'org-agenda-windows
  480. :type '(choice
  481. (const :tag "In current window" current-window)
  482. (const :tag "In current frame, other window" other-window)
  483. (const :tag "Each time a new frame" new-frame)
  484. (const :tag "One dedicated frame" dedicated-frame)))
  485. (defgroup org-cycle nil
  486. "Options concerning visibility cycling in Org-mode."
  487. :tag "Org Cycle"
  488. :group 'org-structure)
  489. (defcustom org-cycle-max-level nil
  490. "Maximum level which should still be subject to visibility cycling.
  491. Levels higher than this will, for cycling, be treated as text, not a headline.
  492. When `org-odd-levels-only' is set, a value of N in this variable actually
  493. means 2N-1 stars as the limiting headline.
  494. When nil, cycle all levels.
  495. Note that the limiting level of cycling is also influenced by
  496. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  497. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  498. than its value."
  499. :group 'org-cycle
  500. :type '(choice
  501. (const :tag "No limit" nil)
  502. (integer :tag "Maximum level")))
  503. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  504. "Names of drawers. Drawers are not opened by cycling on the headline above.
  505. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  506. this:
  507. :DRAWERNAME:
  508. .....
  509. :END:
  510. The drawer \"PROPERTIES\" is special for capturing properties through
  511. the property API.
  512. Drawers can be defined on the per-file basis with a line like:
  513. #+DRAWERS: HIDDEN STATE PROPERTIES"
  514. :group 'org-structure
  515. :group 'org-cycle
  516. :type '(repeat (string :tag "Drawer Name")))
  517. (defcustom org-hide-block-startup nil
  518. "Non-nil means, , entering Org-mode will fold all blocks.
  519. This can also be set in on a per-file basis with
  520. #+STARTUP: hideblocks
  521. #+STARTUP: showblocks"
  522. :group 'org-startup
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-global-at-bob nil
  526. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  527. This makes it possible to do global cycling without having to use S-TAB or
  528. C-u TAB. For this special case to work, the first line of the buffer
  529. must not be a headline - it may be empty or some other text. When used in
  530. this way, `org-cycle-hook' is disables temporarily, to make sure the
  531. cursor stays at the beginning of the buffer.
  532. When this option is nil, don't do anything special at the beginning
  533. of the buffer."
  534. :group 'org-cycle
  535. :type 'boolean)
  536. (defcustom org-cycle-emulate-tab t
  537. "Where should `org-cycle' emulate TAB.
  538. nil Never
  539. white Only in completely white lines
  540. whitestart Only at the beginning of lines, before the first non-white char
  541. t Everywhere except in headlines
  542. exc-hl-bol Everywhere except at the start of a headline
  543. If TAB is used in a place where it does not emulate TAB, the current subtree
  544. visibility is cycled."
  545. :group 'org-cycle
  546. :type '(choice (const :tag "Never" nil)
  547. (const :tag "Only in completely white lines" white)
  548. (const :tag "Before first char in a line" whitestart)
  549. (const :tag "Everywhere except in headlines" t)
  550. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  551. ))
  552. (defcustom org-cycle-separator-lines 2
  553. "Number of empty lines needed to keep an empty line between collapsed trees.
  554. If you leave an empty line between the end of a subtree and the following
  555. headline, this empty line is hidden when the subtree is folded.
  556. Org-mode will leave (exactly) one empty line visible if the number of
  557. empty lines is equal or larger to the number given in this variable.
  558. So the default 2 means, at least 2 empty lines after the end of a subtree
  559. are needed to produce free space between a collapsed subtree and the
  560. following headline.
  561. Special case: when 0, never leave empty lines in collapsed view."
  562. :group 'org-cycle
  563. :type 'integer)
  564. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  565. (defcustom org-pre-cycle-hook nil
  566. "Hook that is run before visibility cycling is happening.
  567. The function(s) in this hook must accept a single argument which indicates
  568. the new state that will be set right after running this hook. The
  569. argument is a symbol. Before a global state change, it can have the values
  570. `overview', `content', or `all'. Before a local state change, it can have
  571. the values `folded', `children', or `subtree'."
  572. :group 'org-cycle
  573. :type 'hook)
  574. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  575. org-cycle-hide-drawers
  576. org-cycle-show-empty-lines
  577. org-optimize-window-after-visibility-change)
  578. "Hook that is run after `org-cycle' has changed the buffer visibility.
  579. The function(s) in this hook must accept a single argument which indicates
  580. the new state that was set by the most recent `org-cycle' command. The
  581. argument is a symbol. After a global state change, it can have the values
  582. `overview', `content', or `all'. After a local state change, it can have
  583. the values `folded', `children', or `subtree'."
  584. :group 'org-cycle
  585. :type 'hook)
  586. (defgroup org-edit-structure nil
  587. "Options concerning structure editing in Org-mode."
  588. :tag "Org Edit Structure"
  589. :group 'org-structure)
  590. (defcustom org-odd-levels-only nil
  591. "Non-nil means, skip even levels and only use odd levels for the outline.
  592. This has the effect that two stars are being added/taken away in
  593. promotion/demotion commands. It also influences how levels are
  594. handled by the exporters.
  595. Changing it requires restart of `font-lock-mode' to become effective
  596. for fontification also in regions already fontified.
  597. You may also set this on a per-file basis by adding one of the following
  598. lines to the buffer:
  599. #+STARTUP: odd
  600. #+STARTUP: oddeven"
  601. :group 'org-edit-structure
  602. :group 'org-font-lock
  603. :type 'boolean)
  604. (defcustom org-adapt-indentation t
  605. "Non-nil means, adapt indentation when promoting and demoting.
  606. When this is set and the *entire* text in an entry is indented, the
  607. indentation is increased by one space in a demotion command, and
  608. decreased by one in a promotion command. If any line in the entry
  609. body starts at column 0, indentation is not changed at all.
  610. This variable also influences how property drawers and planning
  611. information is inserted. When t, these lines drawers will be inserted
  612. indented. When nil, they will not be indented."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-special-ctrl-a/e nil
  616. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  617. When t, `C-a' will bring back the cursor to the beginning of the
  618. headline text, i.e. after the stars and after a possible TODO keyword.
  619. In an item, this will be the position after the bullet.
  620. When the cursor is already at that position, another `C-a' will bring
  621. it to the beginning of the line.
  622. `C-e' will jump to the end of the headline, ignoring the presence of tags
  623. in the headline. A second `C-e' will then jump to the true end of the
  624. line, after any tags.
  625. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  626. going to the true line boundary first. Only a directly following, identical
  627. keypress will bring the cursor to the special positions.
  628. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  629. set separately."
  630. :group 'org-edit-structure
  631. :type '(choice
  632. (const :tag "off" nil)
  633. (const :tag "after stars/bullet and before tags first" t)
  634. (const :tag "true line boundary first" reversed)
  635. (cons :tag "Set C-a and C-e separately"
  636. (choice :tag "Special C-a"
  637. (const :tag "off" nil)
  638. (const :tag "after stars/bullet first" t)
  639. (const :tag "before stars/bullet first" reversed))
  640. (choice :tag "Special C-e"
  641. (const :tag "off" nil)
  642. (const :tag "before tags first" t)
  643. (const :tag "after tags first" reversed)))))
  644. (if (fboundp 'defvaralias)
  645. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  646. (defcustom org-special-ctrl-k nil
  647. "Non-nil means `C-k' will behave specially in headlines.
  648. When nil, `C-k' will call the default `kill-line' command.
  649. When t, the following will happen while the cursor is in the headline:
  650. - When the cursor is at the beginning of a headline, kill the entire
  651. line and possible the folded subtree below the line.
  652. - When in the middle of the headline text, kill the headline up to the tags.
  653. - When after the headline text, kill the tags."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-yank-folded-subtrees t
  657. "Non-nil means, when yanking subtrees, fold them.
  658. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  659. it starts with a heading and all other headings in it are either children
  660. or siblings, then fold all the subtrees. However, do this only if no
  661. text after the yank would be swallowed into a folded tree by this action."
  662. :group 'org-edit-structure
  663. :type 'boolean)
  664. (defcustom org-yank-adjusted-subtrees nil
  665. "Non-nil means, when yanking subtrees, adjust the level.
  666. With this setting, `org-paste-subtree' is used to insert the subtree, see
  667. this function for details."
  668. :group 'org-edit-structure
  669. :type 'boolean)
  670. (defcustom org-M-RET-may-split-line '((default . t))
  671. "Non-nil means, M-RET will split the line at the cursor position.
  672. When nil, it will go to the end of the line before making a
  673. new line.
  674. You may also set this option in a different way for different
  675. contexts. Valid contexts are:
  676. headline when creating a new headline
  677. item when creating a new item
  678. table in a table field
  679. default the value to be used for all contexts not explicitly
  680. customized"
  681. :group 'org-structure
  682. :group 'org-table
  683. :type '(choice
  684. (const :tag "Always" t)
  685. (const :tag "Never" nil)
  686. (repeat :greedy t :tag "Individual contexts"
  687. (cons
  688. (choice :tag "Context"
  689. (const headline)
  690. (const item)
  691. (const table)
  692. (const default))
  693. (boolean)))))
  694. (defcustom org-insert-heading-respect-content nil
  695. "Non-nil means, insert new headings after the current subtree.
  696. When nil, the new heading is created directly after the current line.
  697. The commands \\[org-insert-heading-respect-content] and
  698. \\[org-insert-todo-heading-respect-content] turn this variable on
  699. for the duration of the command."
  700. :group 'org-structure
  701. :type 'boolean)
  702. (defcustom org-blank-before-new-entry '((heading . auto)
  703. (plain-list-item . auto))
  704. "Should `org-insert-heading' leave a blank line before new heading/item?
  705. The value is an alist, with `heading' and `plain-list-item' as car,
  706. and a boolean flag as cdr. For plain lists, if the variable
  707. `org-empty-line-terminates-plain-lists' is set, the setting here
  708. is ignored and no empty line is inserted, to keep the list in tact."
  709. :group 'org-edit-structure
  710. :type '(list
  711. (cons (const heading)
  712. (choice (const :tag "Never" nil)
  713. (const :tag "Always" t)
  714. (const :tag "Auto" auto)))
  715. (cons (const plain-list-item)
  716. (choice (const :tag "Never" nil)
  717. (const :tag "Always" t)
  718. (const :tag "Auto" auto)))))
  719. (defcustom org-insert-heading-hook nil
  720. "Hook being run after inserting a new heading."
  721. :group 'org-edit-structure
  722. :type 'hook)
  723. (defcustom org-enable-fixed-width-editor t
  724. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  725. This currently only means, they are never auto-wrapped.
  726. When nil, such lines will be treated like ordinary lines.
  727. See also the QUOTE keyword."
  728. :group 'org-edit-structure
  729. :type 'boolean)
  730. (defcustom org-goto-auto-isearch t
  731. "Non-nil means, typing characters in org-goto starts incremental search."
  732. :group 'org-edit-structure
  733. :type 'boolean)
  734. (defgroup org-sparse-trees nil
  735. "Options concerning sparse trees in Org-mode."
  736. :tag "Org Sparse Trees"
  737. :group 'org-structure)
  738. (defcustom org-highlight-sparse-tree-matches t
  739. "Non-nil means, highlight all matches that define a sparse tree.
  740. The highlights will automatically disappear the next time the buffer is
  741. changed by an edit command."
  742. :group 'org-sparse-trees
  743. :type 'boolean)
  744. (defcustom org-remove-highlights-with-change t
  745. "Non-nil means, any change to the buffer will remove temporary highlights.
  746. Such highlights are created by `org-occur' and `org-clock-display'.
  747. When nil, `C-c C-c needs to be used to get rid of the highlights.
  748. The highlights created by `org-preview-latex-fragment' always need
  749. `C-c C-c' to be removed."
  750. :group 'org-sparse-trees
  751. :group 'org-time
  752. :type 'boolean)
  753. (defcustom org-occur-hook '(org-first-headline-recenter)
  754. "Hook that is run after `org-occur' has constructed a sparse tree.
  755. This can be used to recenter the window to show as much of the structure
  756. as possible."
  757. :group 'org-sparse-trees
  758. :type 'hook)
  759. (defgroup org-imenu-and-speedbar nil
  760. "Options concerning imenu and speedbar in Org-mode."
  761. :tag "Org Imenu and Speedbar"
  762. :group 'org-structure)
  763. (defcustom org-imenu-depth 2
  764. "The maximum level for Imenu access to Org-mode headlines.
  765. This also applied for speedbar access."
  766. :group 'org-imenu-and-speedbar
  767. :type 'integer)
  768. (defgroup org-table nil
  769. "Options concerning tables in Org-mode."
  770. :tag "Org Table"
  771. :group 'org)
  772. (defcustom org-enable-table-editor 'optimized
  773. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  774. When nil, such lines will be treated like ordinary lines.
  775. When equal to the symbol `optimized', the table editor will be optimized to
  776. do the following:
  777. - Automatic overwrite mode in front of whitespace in table fields.
  778. This makes the structure of the table stay in tact as long as the edited
  779. field does not exceed the column width.
  780. - Minimize the number of realigns. Normally, the table is aligned each time
  781. TAB or RET are pressed to move to another field. With optimization this
  782. happens only if changes to a field might have changed the column width.
  783. Optimization requires replacing the functions `self-insert-command',
  784. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  785. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  786. very good at guessing when a re-align will be necessary, but you can always
  787. force one with \\[org-ctrl-c-ctrl-c].
  788. If you would like to use the optimized version in Org-mode, but the
  789. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  790. This variable can be used to turn on and off the table editor during a session,
  791. but in order to toggle optimization, a restart is required.
  792. See also the variable `org-table-auto-blank-field'."
  793. :group 'org-table
  794. :type '(choice
  795. (const :tag "off" nil)
  796. (const :tag "on" t)
  797. (const :tag "on, optimized" optimized)))
  798. (defcustom org-self-insert-cluster-for-undo t
  799. "Non-nil means cluster self-insert commands for undo when possible.
  800. If this is set, then, like in the Emacs command loop, 20 consequtive
  801. characters will be undone together.
  802. This is configurable, because there is some impact on typing performance."
  803. :group 'org-table
  804. :type 'boolean)
  805. (defcustom org-table-tab-recognizes-table.el t
  806. "Non-nil means, TAB will automatically notice a table.el table.
  807. When it sees such a table, it moves point into it and - if necessary -
  808. calls `table-recognize-table'."
  809. :group 'org-table-editing
  810. :type 'boolean)
  811. (defgroup org-link nil
  812. "Options concerning links in Org-mode."
  813. :tag "Org Link"
  814. :group 'org)
  815. (defvar org-link-abbrev-alist-local nil
  816. "Buffer-local version of `org-link-abbrev-alist', which see.
  817. The value of this is taken from the #+LINK lines.")
  818. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  819. (defcustom org-link-abbrev-alist nil
  820. "Alist of link abbreviations.
  821. The car of each element is a string, to be replaced at the start of a link.
  822. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  823. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  824. [[linkkey:tag][description]]
  825. The 'linkkey' must be a word word, starting with a letter, followed
  826. by letters, numbers, '-' or '_'.
  827. If REPLACE is a string, the tag will simply be appended to create the link.
  828. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  829. the placeholder \"%h\" will cause a url-encoded version of the tag to
  830. be inserted at that point (see the function `url-hexify-string').
  831. REPLACE may also be a function that will be called with the tag as the
  832. only argument to create the link, which should be returned as a string.
  833. See the manual for examples."
  834. :group 'org-link
  835. :type '(repeat
  836. (cons
  837. (string :tag "Protocol")
  838. (choice
  839. (string :tag "Format")
  840. (function)))))
  841. (defcustom org-descriptive-links t
  842. "Non-nil means, hide link part and only show description of bracket links.
  843. Bracket links are like [[link][description]]. This variable sets the initial
  844. state in new org-mode buffers. The setting can then be toggled on a
  845. per-buffer basis from the Org->Hyperlinks menu."
  846. :group 'org-link
  847. :type 'boolean)
  848. (defcustom org-link-file-path-type 'adaptive
  849. "How the path name in file links should be stored.
  850. Valid values are:
  851. relative Relative to the current directory, i.e. the directory of the file
  852. into which the link is being inserted.
  853. absolute Absolute path, if possible with ~ for home directory.
  854. noabbrev Absolute path, no abbreviation of home directory.
  855. adaptive Use relative path for files in the current directory and sub-
  856. directories of it. For other files, use an absolute path."
  857. :group 'org-link
  858. :type '(choice
  859. (const relative)
  860. (const absolute)
  861. (const noabbrev)
  862. (const adaptive)))
  863. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  864. "Types of links that should be activated in Org-mode files.
  865. This is a list of symbols, each leading to the activation of a certain link
  866. type. In principle, it does not hurt to turn on most link types - there may
  867. be a small gain when turning off unused link types. The types are:
  868. bracket The recommended [[link][description]] or [[link]] links with hiding.
  869. angular Links in angular brackets that may contain whitespace like
  870. <bbdb:Carsten Dominik>.
  871. plain Plain links in normal text, no whitespace, like http://google.com.
  872. radio Text that is matched by a radio target, see manual for details.
  873. tag Tag settings in a headline (link to tag search).
  874. date Time stamps (link to calendar).
  875. footnote Footnote labels.
  876. Changing this variable requires a restart of Emacs to become effective."
  877. :group 'org-link
  878. :type '(set :greedy t
  879. (const :tag "Double bracket links (new style)" bracket)
  880. (const :tag "Angular bracket links (old style)" angular)
  881. (const :tag "Plain text links" plain)
  882. (const :tag "Radio target matches" radio)
  883. (const :tag "Tags" tag)
  884. (const :tag "Timestamps" date)
  885. (const :tag "Footnotes" footnote)))
  886. (defcustom org-make-link-description-function nil
  887. "Function to use to generate link descriptions from links. If
  888. nil the link location will be used. This function must take two
  889. parameters; the first is the link and the second the description
  890. org-insert-link has generated, and should return the description
  891. to use."
  892. :group 'org-link
  893. :type 'function)
  894. (defgroup org-link-store nil
  895. "Options concerning storing links in Org-mode."
  896. :tag "Org Store Link"
  897. :group 'org-link)
  898. (defcustom org-email-link-description-format "Email %c: %.30s"
  899. "Format of the description part of a link to an email or usenet message.
  900. The following %-escapes will be replaced by corresponding information:
  901. %F full \"From\" field
  902. %f name, taken from \"From\" field, address if no name
  903. %T full \"To\" field
  904. %t first name in \"To\" field, address if no name
  905. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  906. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  907. %s subject
  908. %m message-id.
  909. You may use normal field width specification between the % and the letter.
  910. This is for example useful to limit the length of the subject.
  911. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  912. :group 'org-link-store
  913. :type 'string)
  914. (defcustom org-from-is-user-regexp
  915. (let (r1 r2)
  916. (when (and user-mail-address (not (string= user-mail-address "")))
  917. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  918. (when (and user-full-name (not (string= user-full-name "")))
  919. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  920. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  921. "Regexp matched against the \"From:\" header of an email or usenet message.
  922. It should match if the message is from the user him/herself."
  923. :group 'org-link-store
  924. :type 'regexp)
  925. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  926. "Non-nil means, storing a link to an Org file will use entry IDs.
  927. Note that before this variable is even considered, org-id must be loaded,
  928. so please customize `org-modules' and turn it on.
  929. The variable can have the following values:
  930. t Create an ID if needed to make a link to the current entry.
  931. create-if-interactive
  932. If `org-store-link' is called directly (interactively, as a user
  933. command), do create an ID to support the link. But when doing the
  934. job for remember, only use the ID if it already exists. The
  935. purpose of this setting is to avoid proliferation of unwanted
  936. IDs, just because you happen to be in an Org file when you
  937. call `org-remember' that automatically and preemptively
  938. creates a link. If you do want to get an ID link in a remember
  939. template to an entry not having an ID, create it first by
  940. explicitly creating a link to it, using `C-c C-l' first.
  941. create-if-interactive-and-no-custom-id
  942. Like create-if-interactive, but do not create an ID if there is
  943. a CUSTOM_ID property defined in the entry. This is the default.
  944. use-existing
  945. Use existing ID, do not create one.
  946. nil Never use an ID to make a link, instead link using a text search for
  947. the headline text."
  948. :group 'org-link-store
  949. :type '(choice
  950. (const :tag "Create ID to make link" t)
  951. (const :tag "Create if storing link interactively"
  952. create-if-interactive)
  953. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  954. create-if-interactive-and-no-custom-id)
  955. (const :tag "Only use existing" use-existing)
  956. (const :tag "Do not use ID to create link" nil)))
  957. (defcustom org-context-in-file-links t
  958. "Non-nil means, file links from `org-store-link' contain context.
  959. A search string will be added to the file name with :: as separator and
  960. used to find the context when the link is activated by the command
  961. `org-open-at-point'.
  962. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  963. negates this setting for the duration of the command."
  964. :group 'org-link-store
  965. :type 'boolean)
  966. (defcustom org-keep-stored-link-after-insertion nil
  967. "Non-nil means, keep link in list for entire session.
  968. The command `org-store-link' adds a link pointing to the current
  969. location to an internal list. These links accumulate during a session.
  970. The command `org-insert-link' can be used to insert links into any
  971. Org-mode file (offering completion for all stored links). When this
  972. option is nil, every link which has been inserted once using \\[org-insert-link]
  973. will be removed from the list, to make completing the unused links
  974. more efficient."
  975. :group 'org-link-store
  976. :type 'boolean)
  977. (defgroup org-link-follow nil
  978. "Options concerning following links in Org-mode."
  979. :tag "Org Follow Link"
  980. :group 'org-link)
  981. (defcustom org-link-translation-function nil
  982. "Function to translate links with different syntax to Org syntax.
  983. This can be used to translate links created for example by the Planner
  984. or emacs-wiki packages to Org syntax.
  985. The function must accept two parameters, a TYPE containing the link
  986. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  987. which is everything after the link protocol. It should return a cons
  988. with possibly modified values of type and path.
  989. Org contains a function for this, so if you set this variable to
  990. `org-translate-link-from-planner', you should be able follow many
  991. links created by planner."
  992. :group 'org-link-follow
  993. :type 'function)
  994. (defcustom org-follow-link-hook nil
  995. "Hook that is run after a link has been followed."
  996. :group 'org-link-follow
  997. :type 'hook)
  998. (defcustom org-tab-follows-link nil
  999. "Non-nil means, on links TAB will follow the link.
  1000. Needs to be set before org.el is loaded.
  1001. This really should not be used, it does not make sense, and the
  1002. implementation is bad."
  1003. :group 'org-link-follow
  1004. :type 'boolean)
  1005. (defcustom org-return-follows-link nil
  1006. "Non-nil means, on links RET will follow the link.
  1007. Needs to be set before org.el is loaded."
  1008. :group 'org-link-follow
  1009. :type 'boolean)
  1010. (defcustom org-mouse-1-follows-link
  1011. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1012. "Non-nil means, mouse-1 on a link will follow the link.
  1013. A longer mouse click will still set point. Does not work on XEmacs.
  1014. Needs to be set before org.el is loaded."
  1015. :group 'org-link-follow
  1016. :type 'boolean)
  1017. (defcustom org-mark-ring-length 4
  1018. "Number of different positions to be recorded in the ring
  1019. Changing this requires a restart of Emacs to work correctly."
  1020. :group 'org-link-follow
  1021. :type 'integer)
  1022. (defcustom org-link-frame-setup
  1023. '((vm . vm-visit-folder-other-frame)
  1024. (gnus . gnus-other-frame)
  1025. (file . find-file-other-window))
  1026. "Setup the frame configuration for following links.
  1027. When following a link with Emacs, it may often be useful to display
  1028. this link in another window or frame. This variable can be used to
  1029. set this up for the different types of links.
  1030. For VM, use any of
  1031. `vm-visit-folder'
  1032. `vm-visit-folder-other-frame'
  1033. For Gnus, use any of
  1034. `gnus'
  1035. `gnus-other-frame'
  1036. `org-gnus-no-new-news'
  1037. For FILE, use any of
  1038. `find-file'
  1039. `find-file-other-window'
  1040. `find-file-other-frame'
  1041. For the calendar, use the variable `calendar-setup'.
  1042. For BBDB, it is currently only possible to display the matches in
  1043. another window."
  1044. :group 'org-link-follow
  1045. :type '(list
  1046. (cons (const vm)
  1047. (choice
  1048. (const vm-visit-folder)
  1049. (const vm-visit-folder-other-window)
  1050. (const vm-visit-folder-other-frame)))
  1051. (cons (const gnus)
  1052. (choice
  1053. (const gnus)
  1054. (const gnus-other-frame)
  1055. (const org-gnus-no-new-news)))
  1056. (cons (const file)
  1057. (choice
  1058. (const find-file)
  1059. (const find-file-other-window)
  1060. (const find-file-other-frame)))))
  1061. (defcustom org-display-internal-link-with-indirect-buffer nil
  1062. "Non-nil means, use indirect buffer to display infile links.
  1063. Activating internal links (from one location in a file to another location
  1064. in the same file) normally just jumps to the location. When the link is
  1065. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1066. another window. When this option is set, the other window actually displays
  1067. an indirect buffer clone of the current buffer, to avoid any visibility
  1068. changes to the current buffer."
  1069. :group 'org-link-follow
  1070. :type 'boolean)
  1071. (defcustom org-open-non-existing-files nil
  1072. "Non-nil means, `org-open-file' will open non-existing files.
  1073. When nil, an error will be generated.
  1074. This variable applies only to external applications because they
  1075. might choke on non-existing files. If the link is to a file that
  1076. will be openend in Emacs, the variable is ignored."
  1077. :group 'org-link-follow
  1078. :type 'boolean)
  1079. (defcustom org-open-directory-means-index-dot-org nil
  1080. "Non-nil means, a link to a directory really means to index.org.
  1081. When nil, following a directory link will run dired or open a finder/explorer
  1082. window on that directory."
  1083. :group 'org-link-follow
  1084. :type 'boolean)
  1085. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1086. "Function and arguments to call for following mailto links.
  1087. This is a list with the first element being a lisp function, and the
  1088. remaining elements being arguments to the function. In string arguments,
  1089. %a will be replaced by the address, and %s will be replaced by the subject
  1090. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1091. :group 'org-link-follow
  1092. :type '(choice
  1093. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1094. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1095. (const :tag "message-mail" (message-mail "%a" "%s"))
  1096. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1097. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1098. "Non-nil means, ask for confirmation before executing shell links.
  1099. Shell links can be dangerous: just think about a link
  1100. [[shell:rm -rf ~/*][Google Search]]
  1101. This link would show up in your Org-mode document as \"Google Search\",
  1102. but really it would remove your entire home directory.
  1103. Therefore we advise against setting this variable to nil.
  1104. Just change it to `y-or-n-p' if you want to confirm with a
  1105. single keystroke rather than having to type \"yes\"."
  1106. :group 'org-link-follow
  1107. :type '(choice
  1108. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1109. (const :tag "with y-or-n (faster)" y-or-n-p)
  1110. (const :tag "no confirmation (dangerous)" nil)))
  1111. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1112. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1113. Elisp links can be dangerous: just think about a link
  1114. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1115. This link would show up in your Org-mode document as \"Google Search\",
  1116. but really it would remove your entire home directory.
  1117. Therefore we advise against setting this variable to nil.
  1118. Just change it to `y-or-n-p' if you want to confirm with a
  1119. single keystroke rather than having to type \"yes\"."
  1120. :group 'org-link-follow
  1121. :type '(choice
  1122. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1123. (const :tag "with y-or-n (faster)" y-or-n-p)
  1124. (const :tag "no confirmation (dangerous)" nil)))
  1125. (defconst org-file-apps-defaults-gnu
  1126. '((remote . emacs)
  1127. (system . mailcap)
  1128. (t . mailcap))
  1129. "Default file applications on a UNIX or GNU/Linux system.
  1130. See `org-file-apps'.")
  1131. (defconst org-file-apps-defaults-macosx
  1132. '((remote . emacs)
  1133. (t . "open %s")
  1134. (system . "open %s")
  1135. ("ps.gz" . "gv %s")
  1136. ("eps.gz" . "gv %s")
  1137. ("dvi" . "xdvi %s")
  1138. ("fig" . "xfig %s"))
  1139. "Default file applications on a MacOS X system.
  1140. The system \"open\" is known as a default, but we use X11 applications
  1141. for some files for which the OS does not have a good default.
  1142. See `org-file-apps'.")
  1143. (defconst org-file-apps-defaults-windowsnt
  1144. (list
  1145. '(remote . emacs)
  1146. (cons t
  1147. (list (if (featurep 'xemacs)
  1148. 'mswindows-shell-execute
  1149. 'w32-shell-execute)
  1150. "open" 'file))
  1151. (cons 'system
  1152. (list (if (featurep 'xemacs)
  1153. 'mswindows-shell-execute
  1154. 'w32-shell-execute)
  1155. "open" 'file)))
  1156. "Default file applications on a Windows NT system.
  1157. The system \"open\" is used for most files.
  1158. See `org-file-apps'.")
  1159. (defcustom org-file-apps
  1160. '(
  1161. (auto-mode . emacs)
  1162. ("\\.x?html?\\'" . default)
  1163. ("\\.pdf\\'" . default)
  1164. )
  1165. "External applications for opening `file:path' items in a document.
  1166. Org-mode uses system defaults for different file types, but
  1167. you can use this variable to set the application for a given file
  1168. extension. The entries in this list are cons cells where the car identifies
  1169. files and the cdr the corresponding command. Possible values for the
  1170. file identifier are
  1171. \"regex\" Regular expression matched against the file name. For backward
  1172. compatibility, this can also be a string with only alphanumeric
  1173. characters, which is then interpreted as an extension.
  1174. `directory' Matches a directory
  1175. `remote' Matches a remote file, accessible through tramp or efs.
  1176. Remote files most likely should be visited through Emacs
  1177. because external applications cannot handle such paths.
  1178. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1179. so all files Emacs knows how to handle. Using this with
  1180. command `emacs' will open most files in Emacs. Beware that this
  1181. will also open html files inside Emacs, unless you add
  1182. (\"html\" . default) to the list as well.
  1183. t Default for files not matched by any of the other options.
  1184. `system' The system command to open files, like `open' on Windows
  1185. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1186. that will be selected if you call `C-c C-o' with a double
  1187. `C-u C-u' prefix.
  1188. Possible values for the command are:
  1189. `emacs' The file will be visited by the current Emacs process.
  1190. `default' Use the default application for this file type, which is the
  1191. association for t in the list, most likely in the system-specific
  1192. part.
  1193. This can be used to overrule an unwanted setting in the
  1194. system-specific variable.
  1195. `system' Use the system command for opening files, like \"open\".
  1196. This command is specified by the entry whose car is `system'.
  1197. Most likely, the system-specific version of this variable
  1198. does define this command, but you can overrule/replace it
  1199. here.
  1200. string A command to be executed by a shell; %s will be replaced
  1201. by the path to the file.
  1202. sexp A Lisp form which will be evaluated. The file path will
  1203. be available in the Lisp variable `file'.
  1204. For more examples, see the system specific constants
  1205. `org-file-apps-defaults-macosx'
  1206. `org-file-apps-defaults-windowsnt'
  1207. `org-file-apps-defaults-gnu'."
  1208. :group 'org-link-follow
  1209. :type '(repeat
  1210. (cons (choice :value ""
  1211. (string :tag "Extension")
  1212. (const :tag "System command to open files" system)
  1213. (const :tag "Default for unrecognized files" t)
  1214. (const :tag "Remote file" remote)
  1215. (const :tag "Links to a directory" directory)
  1216. (const :tag "Any files that have Emacs modes"
  1217. auto-mode))
  1218. (choice :value ""
  1219. (const :tag "Visit with Emacs" emacs)
  1220. (const :tag "Use default" default)
  1221. (const :tag "Use the system command" system)
  1222. (string :tag "Command")
  1223. (sexp :tag "Lisp form")))))
  1224. (defgroup org-refile nil
  1225. "Options concerning refiling entries in Org-mode."
  1226. :tag "Org Refile"
  1227. :group 'org)
  1228. (defcustom org-directory "~/org"
  1229. "Directory with org files.
  1230. This is just a default location to look for Org files. There is no need
  1231. at all to put your files into this directory. It is only used in the
  1232. following situations:
  1233. 1. When a remember template specifies a target file that is not an
  1234. absolute path. The path will then be interpreted relative to
  1235. `org-directory'
  1236. 2. When a remember note is filed away in an interactive way (when exiting the
  1237. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1238. with `org-directory' as the default path."
  1239. :group 'org-refile
  1240. :group 'org-remember
  1241. :type 'directory)
  1242. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1243. "Default target for storing notes.
  1244. Used by the hooks for remember.el. This can be a string, or nil to mean
  1245. the value of `remember-data-file'.
  1246. You can set this on a per-template basis with the variable
  1247. `org-remember-templates'."
  1248. :group 'org-refile
  1249. :group 'org-remember
  1250. :type '(choice
  1251. (const :tag "Default from remember-data-file" nil)
  1252. file))
  1253. (defcustom org-goto-interface 'outline
  1254. "The default interface to be used for `org-goto'.
  1255. Allowed values are:
  1256. outline The interface shows an outline of the relevant file
  1257. and the correct heading is found by moving through
  1258. the outline or by searching with incremental search.
  1259. outline-path-completion Headlines in the current buffer are offered via
  1260. completion. This is the interface also used by
  1261. the refile command."
  1262. :group 'org-refile
  1263. :type '(choice
  1264. (const :tag "Outline" outline)
  1265. (const :tag "Outline-path-completion" outline-path-completion)))
  1266. (defcustom org-goto-max-level 5
  1267. "Maximum level to be considered when running org-goto with refile interface."
  1268. :group 'org-refile
  1269. :type 'integer)
  1270. (defcustom org-reverse-note-order nil
  1271. "Non-nil means, store new notes at the beginning of a file or entry.
  1272. When nil, new notes will be filed to the end of a file or entry.
  1273. This can also be a list with cons cells of regular expressions that
  1274. are matched against file names, and values."
  1275. :group 'org-remember
  1276. :group 'org-refile
  1277. :type '(choice
  1278. (const :tag "Reverse always" t)
  1279. (const :tag "Reverse never" nil)
  1280. (repeat :tag "By file name regexp"
  1281. (cons regexp boolean))))
  1282. (defcustom org-refile-targets nil
  1283. "Targets for refiling entries with \\[org-refile].
  1284. This is list of cons cells. Each cell contains:
  1285. - a specification of the files to be considered, either a list of files,
  1286. or a symbol whose function or variable value will be used to retrieve
  1287. a file name or a list of file names. If you use `org-agenda-files' for
  1288. that, all agenda files will be scanned for targets. Nil means, consider
  1289. headings in the current buffer.
  1290. - A specification of how to find candidate refile targets. This may be
  1291. any of:
  1292. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1293. This tag has to be present in all target headlines, inheritance will
  1294. not be considered.
  1295. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1296. todo keyword.
  1297. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1298. headlines that are refiling targets.
  1299. - a cons cell (:level . N). Any headline of level N is considered a target.
  1300. Note that, when `org-odd-levels-only' is set, level corresponds to
  1301. order in hierarchy, not to the number of stars.
  1302. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1303. Note that, when `org-odd-levels-only' is set, level corresponds to
  1304. order in hierarchy, not to the number of stars.
  1305. You can set the variable `org-refile-target-verify-function' to a function
  1306. to verify each headline found by the simple critery above.
  1307. When this variable is nil, all top-level headlines in the current buffer
  1308. are used, equivalent to the value `((nil . (:level . 1))'."
  1309. :group 'org-refile
  1310. :type '(repeat
  1311. (cons
  1312. (choice :value org-agenda-files
  1313. (const :tag "All agenda files" org-agenda-files)
  1314. (const :tag "Current buffer" nil)
  1315. (function) (variable) (file))
  1316. (choice :tag "Identify target headline by"
  1317. (cons :tag "Specific tag" (const :value :tag) (string))
  1318. (cons :tag "TODO keyword" (const :value :todo) (string))
  1319. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1320. (cons :tag "Level number" (const :value :level) (integer))
  1321. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1322. (defcustom org-refile-target-verify-function nil
  1323. "Function to verify if the headline at point should be a refile target.
  1324. The function will be called without arguments, with point at the
  1325. beginning of the headline. It should return t and leave point
  1326. where it is if the headline is a valid target for refiling.
  1327. If the target should not be selected, the function must return nil.
  1328. In addition to this, it may move point to a place from where the search
  1329. should be continued. For example, the function may decide that the entire
  1330. subtree of the current entry should be excluded and move point to the end
  1331. of the subtree."
  1332. :group 'org-refile
  1333. :type 'function)
  1334. (defcustom org-refile-use-outline-path nil
  1335. "Non-nil means, provide refile targets as paths.
  1336. So a level 3 headline will be available as level1/level2/level3.
  1337. When the value is `file', also include the file name (without directory)
  1338. into the path. In this case, you can also stop the completion after
  1339. the file name, to get entries inserted as top level in the file.
  1340. When `full-file-path', include the full file path."
  1341. :group 'org-refile
  1342. :type '(choice
  1343. (const :tag "Not" nil)
  1344. (const :tag "Yes" t)
  1345. (const :tag "Start with file name" file)
  1346. (const :tag "Start with full file path" full-file-path)))
  1347. (defcustom org-outline-path-complete-in-steps t
  1348. "Non-nil means, complete the outline path in hierarchical steps.
  1349. When Org-mode uses the refile interface to select an outline path
  1350. \(see variable `org-refile-use-outline-path'), the completion of
  1351. the path can be done is a single go, or if can be done in steps down
  1352. the headline hierarchy. Going in steps is probably the best if you
  1353. do not use a special completion package like `ido' or `icicles'.
  1354. However, when using these packages, going in one step can be very
  1355. fast, while still showing the whole path to the entry."
  1356. :group 'org-refile
  1357. :type 'boolean)
  1358. (defcustom org-refile-allow-creating-parent-nodes nil
  1359. "Non-nil means, allow to create new nodes as refile targets.
  1360. New nodes are then created by adding \"/new node name\" to the completion
  1361. of an existing node. When the value of this variable is `confirm',
  1362. new node creation must be confirmed by the user (recommended)
  1363. When nil, the completion must match an existing entry.
  1364. Note that, if the new heading is not seen by the criteria
  1365. listed in `org-refile-targets', multiple instances of the same
  1366. heading would be created by trying again to file under the new
  1367. heading."
  1368. :group 'org-refile
  1369. :type '(choice
  1370. (const :tag "Never" nil)
  1371. (const :tag "Always" t)
  1372. (const :tag "Prompt for confirmation" confirm)))
  1373. (defgroup org-todo nil
  1374. "Options concerning TODO items in Org-mode."
  1375. :tag "Org TODO"
  1376. :group 'org)
  1377. (defgroup org-progress nil
  1378. "Options concerning Progress logging in Org-mode."
  1379. :tag "Org Progress"
  1380. :group 'org-time)
  1381. (defvar org-todo-interpretation-widgets
  1382. '(
  1383. (:tag "Sequence (cycling hits every state)" sequence)
  1384. (:tag "Type (cycling directly to DONE)" type))
  1385. "The available interpretation symbols for customizing
  1386. `org-todo-keywords'.
  1387. Interested libraries should add to this list.")
  1388. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1389. "List of TODO entry keyword sequences and their interpretation.
  1390. \\<org-mode-map>This is a list of sequences.
  1391. Each sequence starts with a symbol, either `sequence' or `type',
  1392. indicating if the keywords should be interpreted as a sequence of
  1393. action steps, or as different types of TODO items. The first
  1394. keywords are states requiring action - these states will select a headline
  1395. for inclusion into the global TODO list Org-mode produces. If one of
  1396. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1397. signify that no further action is necessary. If \"|\" is not found,
  1398. the last keyword is treated as the only DONE state of the sequence.
  1399. The command \\[org-todo] cycles an entry through these states, and one
  1400. additional state where no keyword is present. For details about this
  1401. cycling, see the manual.
  1402. TODO keywords and interpretation can also be set on a per-file basis with
  1403. the special #+SEQ_TODO and #+TYP_TODO lines.
  1404. Each keyword can optionally specify a character for fast state selection
  1405. \(in combination with the variable `org-use-fast-todo-selection')
  1406. and specifiers for state change logging, using the same syntax
  1407. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1408. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1409. indicates to record a time stamp each time this state is selected.
  1410. Each keyword may also specify if a timestamp or a note should be
  1411. recorded when entering or leaving the state, by adding additional
  1412. characters in the parenthesis after the keyword. This looks like this:
  1413. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1414. record only the time of the state change. With X and Y being either
  1415. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1416. Y when leaving the state if and only if the *target* state does not
  1417. define X. You may omit any of the fast-selection key or X or /Y,
  1418. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1419. For backward compatibility, this variable may also be just a list
  1420. of keywords - in this case the interpretation (sequence or type) will be
  1421. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1422. :group 'org-todo
  1423. :group 'org-keywords
  1424. :type '(choice
  1425. (repeat :tag "Old syntax, just keywords"
  1426. (string :tag "Keyword"))
  1427. (repeat :tag "New syntax"
  1428. (cons
  1429. (choice
  1430. :tag "Interpretation"
  1431. ;;Quick and dirty way to see
  1432. ;;`org-todo-interpretations'. This takes the
  1433. ;;place of item arguments
  1434. :convert-widget
  1435. (lambda (widget)
  1436. (widget-put widget
  1437. :args (mapcar
  1438. #'(lambda (x)
  1439. (widget-convert
  1440. (cons 'const x)))
  1441. org-todo-interpretation-widgets))
  1442. widget))
  1443. (repeat
  1444. (string :tag "Keyword"))))))
  1445. (defvar org-todo-keywords-1 nil
  1446. "All TODO and DONE keywords active in a buffer.")
  1447. (make-variable-buffer-local 'org-todo-keywords-1)
  1448. (defvar org-todo-keywords-for-agenda nil)
  1449. (defvar org-done-keywords-for-agenda nil)
  1450. (defvar org-todo-keyword-alist-for-agenda nil)
  1451. (defvar org-tag-alist-for-agenda nil)
  1452. (defvar org-agenda-contributing-files nil)
  1453. (defvar org-not-done-keywords nil)
  1454. (make-variable-buffer-local 'org-not-done-keywords)
  1455. (defvar org-done-keywords nil)
  1456. (make-variable-buffer-local 'org-done-keywords)
  1457. (defvar org-todo-heads nil)
  1458. (make-variable-buffer-local 'org-todo-heads)
  1459. (defvar org-todo-sets nil)
  1460. (make-variable-buffer-local 'org-todo-sets)
  1461. (defvar org-todo-log-states nil)
  1462. (make-variable-buffer-local 'org-todo-log-states)
  1463. (defvar org-todo-kwd-alist nil)
  1464. (make-variable-buffer-local 'org-todo-kwd-alist)
  1465. (defvar org-todo-key-alist nil)
  1466. (make-variable-buffer-local 'org-todo-key-alist)
  1467. (defvar org-todo-key-trigger nil)
  1468. (make-variable-buffer-local 'org-todo-key-trigger)
  1469. (defcustom org-todo-interpretation 'sequence
  1470. "Controls how TODO keywords are interpreted.
  1471. This variable is in principle obsolete and is only used for
  1472. backward compatibility, if the interpretation of todo keywords is
  1473. not given already in `org-todo-keywords'. See that variable for
  1474. more information."
  1475. :group 'org-todo
  1476. :group 'org-keywords
  1477. :type '(choice (const sequence)
  1478. (const type)))
  1479. (defcustom org-use-fast-todo-selection t
  1480. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1481. This variable describes if and under what circumstances the cycling
  1482. mechanism for TODO keywords will be replaced by a single-key, direct
  1483. selection scheme.
  1484. When nil, fast selection is never used.
  1485. When the symbol `prefix', it will be used when `org-todo' is called with
  1486. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1487. in an agenda buffer.
  1488. When t, fast selection is used by default. In this case, the prefix
  1489. argument forces cycling instead.
  1490. In all cases, the special interface is only used if access keys have actually
  1491. been assigned by the user, i.e. if keywords in the configuration are followed
  1492. by a letter in parenthesis, like TODO(t)."
  1493. :group 'org-todo
  1494. :type '(choice
  1495. (const :tag "Never" nil)
  1496. (const :tag "By default" t)
  1497. (const :tag "Only with C-u C-c C-t" prefix)))
  1498. (defcustom org-provide-todo-statistics t
  1499. "Non-nil means, update todo statistics after insert and toggle.
  1500. ALL-HEADLINES means update todo statistics by including headlines
  1501. with no TODO keyword as well. A list of TODO keywords means the
  1502. same, but skip keywords that are not in this list.
  1503. When this is set, todo statistics is updated in the parent of the
  1504. current entry each time a todo state is changed."
  1505. :group 'org-todo
  1506. :type '(choice
  1507. (const :tag "Yes, only for TODO entries" t)
  1508. (const :tag "Yes, including all entries" 'all-headlines)
  1509. (repeat :tag "Yes, for TODOs in this list"
  1510. (string :tag "TODO keyword"))
  1511. (other :tag "No TODO statistics" nil)))
  1512. (defcustom org-hierarchical-todo-statistics t
  1513. "Non-nil means, TODO statistics covers just direct children.
  1514. When nil, all entries in the subtree are considered.
  1515. This has only an effect if `org-provide-todo-statistics' is set."
  1516. :group 'org-todo
  1517. :type 'boolean)
  1518. (defcustom org-after-todo-state-change-hook nil
  1519. "Hook which is run after the state of a TODO item was changed.
  1520. The new state (a string with a TODO keyword, or nil) is available in the
  1521. Lisp variable `state'."
  1522. :group 'org-todo
  1523. :type 'hook)
  1524. (defvar org-blocker-hook nil
  1525. "Hook for functions that are allowed to block a state change.
  1526. Each function gets as its single argument a property list, see
  1527. `org-trigger-hook' for more information about this list.
  1528. If any of the functions in this hook returns nil, the state change
  1529. is blocked.")
  1530. (defvar org-trigger-hook nil
  1531. "Hook for functions that are triggered by a state change.
  1532. Each function gets as its single argument a property list with at least
  1533. the following elements:
  1534. (:type type-of-change :position pos-at-entry-start
  1535. :from old-state :to new-state)
  1536. Depending on the type, more properties may be present.
  1537. This mechanism is currently implemented for:
  1538. TODO state changes
  1539. ------------------
  1540. :type todo-state-change
  1541. :from previous state (keyword as a string), or nil, or a symbol
  1542. 'todo' or 'done', to indicate the general type of state.
  1543. :to new state, like in :from")
  1544. (defcustom org-enforce-todo-dependencies nil
  1545. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1546. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1547. be blocked if any prior sibling is not yet done.
  1548. Finally, if the parent is blocked because of ordered siblings of its own,
  1549. the child will also be blocked.
  1550. This variable needs to be set before org.el is loaded, and you need to
  1551. restart Emacs after a change to make the change effective. The only way
  1552. to change is while Emacs is running is through the customize interface."
  1553. :set (lambda (var val)
  1554. (set var val)
  1555. (if val
  1556. (add-hook 'org-blocker-hook
  1557. 'org-block-todo-from-children-or-siblings-or-parent)
  1558. (remove-hook 'org-blocker-hook
  1559. 'org-block-todo-from-children-or-siblings-or-parent)))
  1560. :group 'org-todo
  1561. :type 'boolean)
  1562. (defcustom org-enforce-todo-checkbox-dependencies nil
  1563. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1564. When this is nil, checkboxes have no influence on switching TODO states.
  1565. When non-nil, you first need to check off all check boxes before the TODO
  1566. entry can be switched to DONE.
  1567. This variable needs to be set before org.el is loaded, and you need to
  1568. restart Emacs after a change to make the change effective. The only way
  1569. to change is while Emacs is running is through the customize interface."
  1570. :set (lambda (var val)
  1571. (set var val)
  1572. (if val
  1573. (add-hook 'org-blocker-hook
  1574. 'org-block-todo-from-checkboxes)
  1575. (remove-hook 'org-blocker-hook
  1576. 'org-block-todo-from-checkboxes)))
  1577. :group 'org-todo
  1578. :type 'boolean)
  1579. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1580. "Non-nil means, inserting a TODO heading is treated as state change.
  1581. So when the command \\[org-insert-todo-heading] is used, state change
  1582. logging will apply if appropriate. When nil, the new TODO item will
  1583. be inserted directly, and no logging will take place."
  1584. :group 'org-todo
  1585. :type 'boolean)
  1586. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1587. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1588. This is the default behavior. However, setting this to nil allows a
  1589. convenient way to select a TODO state and bypass any logging associated
  1590. with that."
  1591. :group 'org-todo
  1592. :type 'boolean)
  1593. (defcustom org-todo-state-tags-triggers nil
  1594. "Tag changes that should be triggered by TODO state changes.
  1595. This is a list. Each entry is
  1596. (state-change (tag . flag) .......)
  1597. State-change can be a string with a state, and empty string to indicate the
  1598. state that has no TODO keyword, or it can be one of the symbols `todo'
  1599. or `done', meaning any not-done or done state, respectively."
  1600. :group 'org-todo
  1601. :group 'org-tags
  1602. :type '(repeat
  1603. (cons (choice :tag "When changing to"
  1604. (const :tag "Not-done state" todo)
  1605. (const :tag "Done state" done)
  1606. (string :tag "State"))
  1607. (repeat
  1608. (cons :tag "Tag action"
  1609. (string :tag "Tag")
  1610. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1611. (defcustom org-log-done nil
  1612. "Information to record when a task moves to the DONE state.
  1613. Possible values are:
  1614. nil Don't add anything, just change the keyword
  1615. time Add a time stamp to the task
  1616. note Prompt a closing note and add it with template `org-log-note-headings'
  1617. This option can also be set with on a per-file-basis with
  1618. #+STARTUP: nologdone
  1619. #+STARTUP: logdone
  1620. #+STARTUP: lognotedone
  1621. You can have local logging settings for a subtree by setting the LOGGING
  1622. property to one or more of these keywords."
  1623. :group 'org-todo
  1624. :group 'org-progress
  1625. :type '(choice
  1626. (const :tag "No logging" nil)
  1627. (const :tag "Record CLOSED timestamp" time)
  1628. (const :tag "Record CLOSED timestamp with closing note." note)))
  1629. ;; Normalize old uses of org-log-done.
  1630. (cond
  1631. ((eq org-log-done t) (setq org-log-done 'time))
  1632. ((and (listp org-log-done) (memq 'done org-log-done))
  1633. (setq org-log-done 'note)))
  1634. (defcustom org-log-note-clock-out nil
  1635. "Non-nil means, record a note when clocking out of an item.
  1636. This can also be configured on a per-file basis by adding one of
  1637. the following lines anywhere in the buffer:
  1638. #+STARTUP: lognoteclock-out
  1639. #+STARTUP: nolognoteclock-out"
  1640. :group 'org-todo
  1641. :group 'org-progress
  1642. :type 'boolean)
  1643. (defcustom org-log-done-with-time t
  1644. "Non-nil means, the CLOSED time stamp will contain date and time.
  1645. When nil, only the date will be recorded."
  1646. :group 'org-progress
  1647. :type 'boolean)
  1648. (defcustom org-log-note-headings
  1649. '((done . "CLOSING NOTE %t")
  1650. (state . "State %-12s from %-12S %t")
  1651. (note . "Note taken on %t")
  1652. (clock-out . ""))
  1653. "Headings for notes added to entries.
  1654. The value is an alist, with the car being a symbol indicating the note
  1655. context, and the cdr is the heading to be used. The heading may also be the
  1656. empty string.
  1657. %t in the heading will be replaced by a time stamp.
  1658. %s will be replaced by the new TODO state, in double quotes.
  1659. %S will be replaced by the old TODO state, in double quotes.
  1660. %u will be replaced by the user name.
  1661. %U will be replaced by the full user name."
  1662. :group 'org-todo
  1663. :group 'org-progress
  1664. :type '(list :greedy t
  1665. (cons (const :tag "Heading when closing an item" done) string)
  1666. (cons (const :tag
  1667. "Heading when changing todo state (todo sequence only)"
  1668. state) string)
  1669. (cons (const :tag "Heading when just taking a note" note) string)
  1670. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1671. (unless (assq 'note org-log-note-headings)
  1672. (push '(note . "%t") org-log-note-headings))
  1673. (defcustom org-log-into-drawer nil
  1674. "Non-nil means, insert state change notes and time stamps into a drawer.
  1675. When nil, state changes notes will be inserted after the headline and
  1676. any scheduling and clock lines, but not inside a drawer.
  1677. The value of this variable should be the name of the drawer to use.
  1678. LOGBOOK is proposed at the default drawer for this purpose, you can
  1679. also set this to a string to define the drawer of your choice.
  1680. A value of t is also allowed, representing \"LOGBOOK\".
  1681. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1682. will be ignored.
  1683. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1684. a subtree."
  1685. :group 'org-todo
  1686. :group 'org-progress
  1687. :type '(choice
  1688. (const :tag "Not into a drawer" nil)
  1689. (const :tag "LOGBOOK" t)
  1690. (string :tag "Other")))
  1691. (if (fboundp 'defvaralias)
  1692. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1693. (defun org-log-into-drawer ()
  1694. "Return the value of `org-log-into-drawer', but let properties overrule.
  1695. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1696. used instead of the default value."
  1697. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1698. (cond
  1699. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1700. ((equal p "t") "LOGBOOK")
  1701. (t p))))
  1702. (defcustom org-log-state-notes-insert-after-drawers nil
  1703. "Non-nil means, insert state change notes after any drawers in entry.
  1704. Only the drawers that *immediately* follow the headline and the
  1705. deadline/scheduled line are skipped.
  1706. When nil, insert notes right after the heading and perhaps the line
  1707. with deadline/scheduling if present.
  1708. This variable will have no effect if `org-log-into-drawer' is
  1709. set."
  1710. :group 'org-todo
  1711. :group 'org-progress
  1712. :type 'boolean)
  1713. (defcustom org-log-states-order-reversed t
  1714. "Non-nil means, the latest state change note will be directly after heading.
  1715. When nil, the notes will be orderer according to time."
  1716. :group 'org-todo
  1717. :group 'org-progress
  1718. :type 'boolean)
  1719. (defcustom org-log-repeat 'time
  1720. "Non-nil means, record moving through the DONE state when triggering repeat.
  1721. An auto-repeating tasks is immediately switched back to TODO when marked
  1722. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1723. the TODO keyword definition, or recording a closing note by setting
  1724. `org-log-done', there will be no record of the task moving through DONE.
  1725. This variable forces taking a note anyway. Possible values are:
  1726. nil Don't force a record
  1727. time Record a time stamp
  1728. note Record a note
  1729. This option can also be set with on a per-file-basis with
  1730. #+STARTUP: logrepeat
  1731. #+STARTUP: lognoterepeat
  1732. #+STARTUP: nologrepeat
  1733. You can have local logging settings for a subtree by setting the LOGGING
  1734. property to one or more of these keywords."
  1735. :group 'org-todo
  1736. :group 'org-progress
  1737. :type '(choice
  1738. (const :tag "Don't force a record" nil)
  1739. (const :tag "Force recording the DONE state" time)
  1740. (const :tag "Force recording a note with the DONE state" note)))
  1741. (defgroup org-priorities nil
  1742. "Priorities in Org-mode."
  1743. :tag "Org Priorities"
  1744. :group 'org-todo)
  1745. (defcustom org-enable-priority-commands t
  1746. "Non-nil means, priority commands are active.
  1747. When nil, these commands will be disabled, so that you never accidentally
  1748. set a priority."
  1749. :group 'org-priorities
  1750. :type 'boolean)
  1751. (defcustom org-highest-priority ?A
  1752. "The highest priority of TODO items. A character like ?A, ?B etc.
  1753. Must have a smaller ASCII number than `org-lowest-priority'."
  1754. :group 'org-priorities
  1755. :type 'character)
  1756. (defcustom org-lowest-priority ?C
  1757. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1758. Must have a larger ASCII number than `org-highest-priority'."
  1759. :group 'org-priorities
  1760. :type 'character)
  1761. (defcustom org-default-priority ?B
  1762. "The default priority of TODO items.
  1763. This is the priority an item get if no explicit priority is given."
  1764. :group 'org-priorities
  1765. :type 'character)
  1766. (defcustom org-priority-start-cycle-with-default t
  1767. "Non-nil means, start with default priority when starting to cycle.
  1768. When this is nil, the first step in the cycle will be (depending on the
  1769. command used) one higher or lower that the default priority."
  1770. :group 'org-priorities
  1771. :type 'boolean)
  1772. (defgroup org-time nil
  1773. "Options concerning time stamps and deadlines in Org-mode."
  1774. :tag "Org Time"
  1775. :group 'org)
  1776. (defcustom org-insert-labeled-timestamps-at-point nil
  1777. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1778. When nil, these labeled time stamps are forces into the second line of an
  1779. entry, just after the headline. When scheduling from the global TODO list,
  1780. the time stamp will always be forced into the second line."
  1781. :group 'org-time
  1782. :type 'boolean)
  1783. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1784. "Formats for `format-time-string' which are used for time stamps.
  1785. It is not recommended to change this constant.")
  1786. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1787. "Number of minutes to round time stamps to.
  1788. These are two values, the first applies when first creating a time stamp.
  1789. The second applies when changing it with the commands `S-up' and `S-down'.
  1790. When changing the time stamp, this means that it will change in steps
  1791. of N minutes, as given by the second value.
  1792. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1793. numbers should be factors of 60, so for example 5, 10, 15.
  1794. When this is larger than 1, you can still force an exact time-stamp by using
  1795. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1796. and by using a prefix arg to `S-up/down' to specify the exact number
  1797. of minutes to shift."
  1798. :group 'org-time
  1799. :get '(lambda (var) ; Make sure all entries have 5 elements
  1800. (if (integerp (default-value var))
  1801. (list (default-value var) 5)
  1802. (default-value var)))
  1803. :type '(list
  1804. (integer :tag "when inserting times")
  1805. (integer :tag "when modifying times")))
  1806. ;; Normalize old customizations of this variable.
  1807. (when (integerp org-time-stamp-rounding-minutes)
  1808. (setq org-time-stamp-rounding-minutes
  1809. (list org-time-stamp-rounding-minutes
  1810. org-time-stamp-rounding-minutes)))
  1811. (defcustom org-display-custom-times nil
  1812. "Non-nil means, overlay custom formats over all time stamps.
  1813. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1814. To turn this on on a per-file basis, insert anywhere in the file:
  1815. #+STARTUP: customtime"
  1816. :group 'org-time
  1817. :set 'set-default
  1818. :type 'sexp)
  1819. (make-variable-buffer-local 'org-display-custom-times)
  1820. (defcustom org-time-stamp-custom-formats
  1821. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1822. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1823. These are overlayed over the default ISO format if the variable
  1824. `org-display-custom-times' is set. Time like %H:%M should be at the
  1825. end of the second format. The custom formats are also honored by export
  1826. commands, if custom time display is turned on at the time of export."
  1827. :group 'org-time
  1828. :type 'sexp)
  1829. (defun org-time-stamp-format (&optional long inactive)
  1830. "Get the right format for a time string."
  1831. (let ((f (if long (cdr org-time-stamp-formats)
  1832. (car org-time-stamp-formats))))
  1833. (if inactive
  1834. (concat "[" (substring f 1 -1) "]")
  1835. f)))
  1836. (defcustom org-time-clocksum-format "%d:%02d"
  1837. "The format string used when creating CLOCKSUM lines, or when
  1838. org-mode generates a time duration."
  1839. :group 'org-time
  1840. :type 'string)
  1841. (defcustom org-deadline-warning-days 14
  1842. "No. of days before expiration during which a deadline becomes active.
  1843. This variable governs the display in sparse trees and in the agenda.
  1844. When 0 or negative, it means use this number (the absolute value of it)
  1845. even if a deadline has a different individual lead time specified.
  1846. Custom commands can set this variable in the options section."
  1847. :group 'org-time
  1848. :group 'org-agenda-daily/weekly
  1849. :type 'integer)
  1850. (defcustom org-read-date-prefer-future t
  1851. "Non-nil means, assume future for incomplete date input from user.
  1852. This affects the following situations:
  1853. 1. The user gives a day, but no month.
  1854. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1855. read this as the third of *next* month. However, if you enter \"17\",
  1856. it will be considered as *this* month.
  1857. 2. The user gives a month but not a year.
  1858. For example, if it is april and you enter \"feb 2\", this will be read
  1859. as feb 2, *next* year. \"May 5\", however, will be this year.
  1860. Currently this does not work for ISO week specifications.
  1861. When this option is nil, the current month and year will always be used
  1862. as defaults."
  1863. :group 'org-time
  1864. :type 'boolean)
  1865. (defcustom org-read-date-display-live t
  1866. "Non-nil means, display current interpretation of date prompt live.
  1867. This display will be in an overlay, in the minibuffer."
  1868. :group 'org-time
  1869. :type 'boolean)
  1870. (defcustom org-read-date-popup-calendar t
  1871. "Non-nil means, pop up a calendar when prompting for a date.
  1872. In the calendar, the date can be selected with mouse-1. However, the
  1873. minibuffer will also be active, and you can simply enter the date as well.
  1874. When nil, only the minibuffer will be available."
  1875. :group 'org-time
  1876. :type 'boolean)
  1877. (if (fboundp 'defvaralias)
  1878. (defvaralias 'org-popup-calendar-for-date-prompt
  1879. 'org-read-date-popup-calendar))
  1880. (defcustom org-read-date-minibuffer-setup-hook nil
  1881. "Hook to be used to set up keys for the date/time interface.
  1882. Add key definitions to `minibuffer-local-map', which will be a temporary
  1883. copy."
  1884. :group 'org-time
  1885. :type 'hook)
  1886. (defcustom org-extend-today-until 0
  1887. "The hour when your day really ends. Must be an integer.
  1888. This has influence for the following applications:
  1889. - When switching the agenda to \"today\". It it is still earlier than
  1890. the time given here, the day recognized as TODAY is actually yesterday.
  1891. - When a date is read from the user and it is still before the time given
  1892. here, the current date and time will be assumed to be yesterday, 23:59.
  1893. Also, timestamps inserted in remember templates follow this rule.
  1894. IMPORTANT: This is a feature whose implementation is and likely will
  1895. remain incomplete. Really, it is only here because past midnight seems to
  1896. be the favorite working time of John Wiegley :-)"
  1897. :group 'org-time
  1898. :type 'integer)
  1899. (defcustom org-edit-timestamp-down-means-later nil
  1900. "Non-nil means, S-down will increase the time in a time stamp.
  1901. When nil, S-up will increase."
  1902. :group 'org-time
  1903. :type 'boolean)
  1904. (defcustom org-calendar-follow-timestamp-change t
  1905. "Non-nil means, make the calendar window follow timestamp changes.
  1906. When a timestamp is modified and the calendar window is visible, it will be
  1907. moved to the new date."
  1908. :group 'org-time
  1909. :type 'boolean)
  1910. (defgroup org-tags nil
  1911. "Options concerning tags in Org-mode."
  1912. :tag "Org Tags"
  1913. :group 'org)
  1914. (defcustom org-tag-alist nil
  1915. "List of tags allowed in Org-mode files.
  1916. When this list is nil, Org-mode will base TAG input on what is already in the
  1917. buffer.
  1918. The value of this variable is an alist, the car of each entry must be a
  1919. keyword as a string, the cdr may be a character that is used to select
  1920. that tag through the fast-tag-selection interface.
  1921. See the manual for details."
  1922. :group 'org-tags
  1923. :type '(repeat
  1924. (choice
  1925. (cons (string :tag "Tag name")
  1926. (character :tag "Access char"))
  1927. (const :tag "Start radio group" (:startgroup))
  1928. (const :tag "End radio group" (:endgroup))
  1929. (const :tag "New line" (:newline)))))
  1930. (defcustom org-tag-persistent-alist nil
  1931. "List of tags that will always appear in all Org-mode files.
  1932. This is in addition to any in buffer settings or customizations
  1933. of `org-tag-alist'.
  1934. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1935. The value of this variable is an alist, the car of each entry must be a
  1936. keyword as a string, the cdr may be a character that is used to select
  1937. that tag through the fast-tag-selection interface.
  1938. See the manual for details.
  1939. To disable these tags on a per-file basis, insert anywhere in the file:
  1940. #+STARTUP: noptag"
  1941. :group 'org-tags
  1942. :type '(repeat
  1943. (choice
  1944. (cons (string :tag "Tag name")
  1945. (character :tag "Access char"))
  1946. (const :tag "Start radio group" (:startgroup))
  1947. (const :tag "End radio group" (:endgroup))
  1948. (const :tag "New line" (:newline)))))
  1949. (defvar org-file-tags nil
  1950. "List of tags that can be inherited by all entries in the file.
  1951. The tags will be inherited if the variable `org-use-tag-inheritance'
  1952. says they should be.
  1953. This variable is populated from #+TAG lines.")
  1954. (defcustom org-use-fast-tag-selection 'auto
  1955. "Non-nil means, use fast tag selection scheme.
  1956. This is a special interface to select and deselect tags with single keys.
  1957. When nil, fast selection is never used.
  1958. When the symbol `auto', fast selection is used if and only if selection
  1959. characters for tags have been configured, either through the variable
  1960. `org-tag-alist' or through a #+TAGS line in the buffer.
  1961. When t, fast selection is always used and selection keys are assigned
  1962. automatically if necessary."
  1963. :group 'org-tags
  1964. :type '(choice
  1965. (const :tag "Always" t)
  1966. (const :tag "Never" nil)
  1967. (const :tag "When selection characters are configured" 'auto)))
  1968. (defcustom org-fast-tag-selection-single-key nil
  1969. "Non-nil means, fast tag selection exits after first change.
  1970. When nil, you have to press RET to exit it.
  1971. During fast tag selection, you can toggle this flag with `C-c'.
  1972. This variable can also have the value `expert'. In this case, the window
  1973. displaying the tags menu is not even shown, until you press C-c again."
  1974. :group 'org-tags
  1975. :type '(choice
  1976. (const :tag "No" nil)
  1977. (const :tag "Yes" t)
  1978. (const :tag "Expert" expert)))
  1979. (defvar org-fast-tag-selection-include-todo nil
  1980. "Non-nil means, fast tags selection interface will also offer TODO states.
  1981. This is an undocumented feature, you should not rely on it.")
  1982. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1983. "The column to which tags should be indented in a headline.
  1984. If this number is positive, it specifies the column. If it is negative,
  1985. it means that the tags should be flushright to that column. For example,
  1986. -80 works well for a normal 80 character screen."
  1987. :group 'org-tags
  1988. :type 'integer)
  1989. (defcustom org-auto-align-tags t
  1990. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1991. These operations change the length of a headline and therefore shift
  1992. the tags around. With this options turned on, after each such operation
  1993. the tags are again aligned to `org-tags-column'."
  1994. :group 'org-tags
  1995. :type 'boolean)
  1996. (defcustom org-use-tag-inheritance t
  1997. "Non-nil means, tags in levels apply also for sublevels.
  1998. When nil, only the tags directly given in a specific line apply there.
  1999. This may also be a list of tags that should be inherited, or a regexp that
  2000. matches tags that should be inherited. Additional control is possible
  2001. with the variable `org-tags-exclude-from-inheritance' which gives an
  2002. explicit list of tags to be excluded from inheritance., even if the value of
  2003. `org-use-tag-inheritance' would select it for inheritance.
  2004. If this option is t, a match early-on in a tree can lead to a large
  2005. number of matches in the subtree when constructing the agenda or creating
  2006. a sparse tree. If you only want to see the first match in a tree during
  2007. a search, check out the variable `org-tags-match-list-sublevels'."
  2008. :group 'org-tags
  2009. :type '(choice
  2010. (const :tag "Not" nil)
  2011. (const :tag "Always" t)
  2012. (repeat :tag "Specific tags" (string :tag "Tag"))
  2013. (regexp :tag "Tags matched by regexp")))
  2014. (defcustom org-tags-exclude-from-inheritance nil
  2015. "List of tags that should never be inherited.
  2016. This is a way to exclude a few tags from inheritance. For way to do
  2017. the opposite, to actively allow inheritance for selected tags,
  2018. see the variable `org-use-tag-inheritance'."
  2019. :group 'org-tags
  2020. :type '(repeat (string :tag "Tag")))
  2021. (defun org-tag-inherit-p (tag)
  2022. "Check if TAG is one that should be inherited."
  2023. (cond
  2024. ((member tag org-tags-exclude-from-inheritance) nil)
  2025. ((eq org-use-tag-inheritance t) t)
  2026. ((not org-use-tag-inheritance) nil)
  2027. ((stringp org-use-tag-inheritance)
  2028. (string-match org-use-tag-inheritance tag))
  2029. ((listp org-use-tag-inheritance)
  2030. (member tag org-use-tag-inheritance))
  2031. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2032. (defcustom org-tags-match-list-sublevels t
  2033. "Non-nil means list also sublevels of headlines matching a search.
  2034. This variable applies to tags/property searches, and also to stuck
  2035. projects because this search is based on a tags match as well.
  2036. When set to the symbol `indented', sublevels are indented with
  2037. leading dots.
  2038. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2039. the sublevels of a headline matching a tag search often also match
  2040. the same search. Listing all of them can create very long lists.
  2041. Setting this variable to nil causes subtrees of a match to be skipped.
  2042. This variable is semi-obsolete and probably should always be true. It
  2043. is better to limit inheritance to certain tags using the variables
  2044. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2045. :group 'org-tags
  2046. :type '(choice
  2047. (const :tag "No, don't list them" nil)
  2048. (const :tag "Yes, do list them" t)
  2049. (const :tag "List them, indented with leading dots" indented)))
  2050. (defcustom org-tags-sort-function nil
  2051. "When set, tags are sorted using this function as a comparator"
  2052. :group 'org-tags
  2053. :type '(choice
  2054. (const :tag "No sorting" nil)
  2055. (const :tag "Alphabetical" string<)
  2056. (const :tag "Reverse alphabetical" string>)
  2057. (function :tag "Custom function" nil)))
  2058. (defvar org-tags-history nil
  2059. "History of minibuffer reads for tags.")
  2060. (defvar org-last-tags-completion-table nil
  2061. "The last used completion table for tags.")
  2062. (defvar org-after-tags-change-hook nil
  2063. "Hook that is run after the tags in a line have changed.")
  2064. (defgroup org-properties nil
  2065. "Options concerning properties in Org-mode."
  2066. :tag "Org Properties"
  2067. :group 'org)
  2068. (defcustom org-property-format "%-10s %s"
  2069. "How property key/value pairs should be formatted by `indent-line'.
  2070. When `indent-line' hits a property definition, it will format the line
  2071. according to this format, mainly to make sure that the values are
  2072. lined-up with respect to each other."
  2073. :group 'org-properties
  2074. :type 'string)
  2075. (defcustom org-use-property-inheritance nil
  2076. "Non-nil means, properties apply also for sublevels.
  2077. This setting is chiefly used during property searches. Turning it on can
  2078. cause significant overhead when doing a search, which is why it is not
  2079. on by default.
  2080. When nil, only the properties directly given in the current entry count.
  2081. When t, every property is inherited. The value may also be a list of
  2082. properties that should have inheritance, or a regular expression matching
  2083. properties that should be inherited.
  2084. However, note that some special properties use inheritance under special
  2085. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2086. and the properties ending in \"_ALL\" when they are used as descriptor
  2087. for valid values of a property.
  2088. Note for programmers:
  2089. When querying an entry with `org-entry-get', you can control if inheritance
  2090. should be used. By default, `org-entry-get' looks only at the local
  2091. properties. You can request inheritance by setting the inherit argument
  2092. to t (to force inheritance) or to `selective' (to respect the setting
  2093. in this variable)."
  2094. :group 'org-properties
  2095. :type '(choice
  2096. (const :tag "Not" nil)
  2097. (const :tag "Always" t)
  2098. (repeat :tag "Specific properties" (string :tag "Property"))
  2099. (regexp :tag "Properties matched by regexp")))
  2100. (defun org-property-inherit-p (property)
  2101. "Check if PROPERTY is one that should be inherited."
  2102. (cond
  2103. ((eq org-use-property-inheritance t) t)
  2104. ((not org-use-property-inheritance) nil)
  2105. ((stringp org-use-property-inheritance)
  2106. (string-match org-use-property-inheritance property))
  2107. ((listp org-use-property-inheritance)
  2108. (member property org-use-property-inheritance))
  2109. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2110. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2111. "The default column format, if no other format has been defined.
  2112. This variable can be set on the per-file basis by inserting a line
  2113. #+COLUMNS: %25ITEM ....."
  2114. :group 'org-properties
  2115. :type 'string)
  2116. (defcustom org-columns-ellipses ".."
  2117. "The ellipses to be used when a field in column view is truncated.
  2118. When this is the empty string, as many characters as possible are shown,
  2119. but then there will be no visual indication that the field has been truncated.
  2120. When this is a string of length N, the last N characters of a truncated
  2121. field are replaced by this string. If the column is narrower than the
  2122. ellipses string, only part of the ellipses string will be shown."
  2123. :group 'org-properties
  2124. :type 'string)
  2125. (defcustom org-columns-modify-value-for-display-function nil
  2126. "Function that modifies values for display in column view.
  2127. For example, it can be used to cut out a certain part from a time stamp.
  2128. The function must take 2 arguments:
  2129. column-title The title of the column (*not* the property name)
  2130. value The value that should be modified.
  2131. The function should return the value that should be displayed,
  2132. or nil if the normal value should be used."
  2133. :group 'org-properties
  2134. :type 'function)
  2135. (defcustom org-effort-property "Effort"
  2136. "The property that is being used to keep track of effort estimates.
  2137. Effort estimates given in this property need to have the format H:MM."
  2138. :group 'org-properties
  2139. :group 'org-progress
  2140. :type '(string :tag "Property"))
  2141. (defconst org-global-properties-fixed
  2142. '(("VISIBILITY_ALL" . "folded children content all")
  2143. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2144. "List of property/value pairs that can be inherited by any entry.
  2145. These are fixed values, for the preset properties. The user variable
  2146. that can be used to add to this list is `org-global-properties'.
  2147. The entries in this list are cons cells where the car is a property
  2148. name and cdr is a string with the value. If the value represents
  2149. multiple items like an \"_ALL\" property, separate the items by
  2150. spaces.")
  2151. (defcustom org-global-properties nil
  2152. "List of property/value pairs that can be inherited by any entry.
  2153. This list will be combined with the constant `org-global-properties-fixed'.
  2154. The entries in this list are cons cells where the car is a property
  2155. name and cdr is a string with the value.
  2156. You can set buffer-local values for the same purpose in the variable
  2157. `org-file-properties' this by adding lines like
  2158. #+PROPERTY: NAME VALUE"
  2159. :group 'org-properties
  2160. :type '(repeat
  2161. (cons (string :tag "Property")
  2162. (string :tag "Value"))))
  2163. (defvar org-file-properties nil
  2164. "List of property/value pairs that can be inherited by any entry.
  2165. Valid for the current buffer.
  2166. This variable is populated from #+PROPERTY lines.")
  2167. (make-variable-buffer-local 'org-file-properties)
  2168. (defgroup org-agenda nil
  2169. "Options concerning agenda views in Org-mode."
  2170. :tag "Org Agenda"
  2171. :group 'org)
  2172. (defvar org-category nil
  2173. "Variable used by org files to set a category for agenda display.
  2174. Such files should use a file variable to set it, for example
  2175. # -*- mode: org; org-category: \"ELisp\"
  2176. or contain a special line
  2177. #+CATEGORY: ELisp
  2178. If the file does not specify a category, then file's base name
  2179. is used instead.")
  2180. (make-variable-buffer-local 'org-category)
  2181. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2182. (defcustom org-agenda-files nil
  2183. "The files to be used for agenda display.
  2184. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2185. \\[org-remove-file]. You can also use customize to edit the list.
  2186. If an entry is a directory, all files in that directory that are matched by
  2187. `org-agenda-file-regexp' will be part of the file list.
  2188. If the value of the variable is not a list but a single file name, then
  2189. the list of agenda files is actually stored and maintained in that file, one
  2190. agenda file per line."
  2191. :group 'org-agenda
  2192. :type '(choice
  2193. (repeat :tag "List of files and directories" file)
  2194. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2195. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2196. "Regular expression to match files for `org-agenda-files'.
  2197. If any element in the list in that variable contains a directory instead
  2198. of a normal file, all files in that directory that are matched by this
  2199. regular expression will be included."
  2200. :group 'org-agenda
  2201. :type 'regexp)
  2202. (defcustom org-agenda-text-search-extra-files nil
  2203. "List of extra files to be searched by text search commands.
  2204. These files will be search in addition to the agenda files by the
  2205. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2206. Note that these files will only be searched for text search commands,
  2207. not for the other agenda views like todo lists, tag searches or the weekly
  2208. agenda. This variable is intended to list notes and possibly archive files
  2209. that should also be searched by these two commands.
  2210. In fact, if the first element in the list is the symbol `agenda-archives',
  2211. than all archive files of all agenda files will be added to the search
  2212. scope."
  2213. :group 'org-agenda
  2214. :type '(set :greedy t
  2215. (const :tag "Agenda Archives" agenda-archives)
  2216. (repeat :inline t (file))))
  2217. (if (fboundp 'defvaralias)
  2218. (defvaralias 'org-agenda-multi-occur-extra-files
  2219. 'org-agenda-text-search-extra-files))
  2220. (defcustom org-agenda-skip-unavailable-files nil
  2221. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2222. A nil value means to remove them, after a query, from the list."
  2223. :group 'org-agenda
  2224. :type 'boolean)
  2225. (defcustom org-calendar-to-agenda-key [?c]
  2226. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2227. The command `org-calendar-goto-agenda' will be bound to this key. The
  2228. default is the character `c' because then `c' can be used to switch back and
  2229. forth between agenda and calendar."
  2230. :group 'org-agenda
  2231. :type 'sexp)
  2232. (defcustom org-calendar-agenda-action-key [?k]
  2233. "The key to be installed in `calendar-mode-map' for agenda-action.
  2234. The command `org-agenda-action' will be bound to this key. The
  2235. default is the character `k' because we use the same key in the agenda."
  2236. :group 'org-agenda
  2237. :type 'sexp)
  2238. (eval-after-load "calendar"
  2239. '(progn
  2240. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2241. 'org-calendar-goto-agenda)
  2242. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2243. 'org-agenda-action)))
  2244. (defgroup org-latex nil
  2245. "Options for embedding LaTeX code into Org-mode."
  2246. :tag "Org LaTeX"
  2247. :group 'org)
  2248. (defcustom org-format-latex-options
  2249. '(:foreground default :background default :scale 1.0
  2250. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2251. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2252. "Options for creating images from LaTeX fragments.
  2253. This is a property list with the following properties:
  2254. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2255. `default' means use the foreground of the default face.
  2256. :background the background color, or \"Transparent\".
  2257. `default' means use the background of the default face.
  2258. :scale a scaling factor for the size of the images.
  2259. :html-foreground, :html-background, :html-scale
  2260. the same numbers for HTML export.
  2261. :matchers a list indicating which matchers should be used to
  2262. find LaTeX fragments. Valid members of this list are:
  2263. \"begin\" find environments
  2264. \"$1\" find single characters surrounded by $.$
  2265. \"$\" find math expressions surrounded by $...$
  2266. \"$$\" find math expressions surrounded by $$....$$
  2267. \"\\(\" find math expressions surrounded by \\(...\\)
  2268. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2269. :group 'org-latex
  2270. :type 'plist)
  2271. (defcustom org-format-latex-header "\\documentclass{article}
  2272. \\usepackage{fullpage} % do not remove
  2273. \\usepackage{amssymb}
  2274. \\usepackage[usenames]{color}
  2275. \\usepackage{amsmath}
  2276. \\usepackage{latexsym}
  2277. \\usepackage[mathscr]{eucal}
  2278. \\pagestyle{empty} % do not remove"
  2279. "The document header used for processing LaTeX fragments."
  2280. :group 'org-latex
  2281. :type 'string)
  2282. (defgroup org-font-lock nil
  2283. "Font-lock settings for highlighting in Org-mode."
  2284. :tag "Org Font Lock"
  2285. :group 'org)
  2286. (defcustom org-level-color-stars-only nil
  2287. "Non-nil means fontify only the stars in each headline.
  2288. When nil, the entire headline is fontified.
  2289. Changing it requires restart of `font-lock-mode' to become effective
  2290. also in regions already fontified."
  2291. :group 'org-font-lock
  2292. :type 'boolean)
  2293. (defcustom org-hide-leading-stars nil
  2294. "Non-nil means, hide the first N-1 stars in a headline.
  2295. This works by using the face `org-hide' for these stars. This
  2296. face is white for a light background, and black for a dark
  2297. background. You may have to customize the face `org-hide' to
  2298. make this work.
  2299. Changing it requires restart of `font-lock-mode' to become effective
  2300. also in regions already fontified.
  2301. You may also set this on a per-file basis by adding one of the following
  2302. lines to the buffer:
  2303. #+STARTUP: hidestars
  2304. #+STARTUP: showstars"
  2305. :group 'org-font-lock
  2306. :type 'boolean)
  2307. (defcustom org-fontify-done-headline nil
  2308. "Non-nil means, change the face of a headline if it is marked DONE.
  2309. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2310. When this is non-nil, the headline after the keyword is set to the
  2311. `org-headline-done' as an additional indication."
  2312. :group 'org-font-lock
  2313. :type 'boolean)
  2314. (defcustom org-fontify-emphasized-text t
  2315. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2316. Changing this variable requires a restart of Emacs to take effect."
  2317. :group 'org-font-lock
  2318. :type 'boolean)
  2319. (defcustom org-fontify-whole-heading-line nil
  2320. "Non-nil means fontify the whole line for headings.
  2321. This is useful when setting a background color for the
  2322. org-leve-* faces."
  2323. :group 'org-font-lock
  2324. :type 'boolean)
  2325. (defcustom org-highlight-latex-fragments-and-specials nil
  2326. "Non-nil means, fontify what is treated specially by the exporters."
  2327. :group 'org-font-lock
  2328. :type 'boolean)
  2329. (defcustom org-hide-emphasis-markers nil
  2330. "Non-nil mean font-lock should hide the emphasis marker characters."
  2331. :group 'org-font-lock
  2332. :type 'boolean)
  2333. (defvar org-emph-re nil
  2334. "Regular expression for matching emphasis.")
  2335. (defvar org-verbatim-re nil
  2336. "Regular expression for matching verbatim text.")
  2337. (defvar org-emphasis-regexp-components) ; defined just below
  2338. (defvar org-emphasis-alist) ; defined just below
  2339. (defun org-set-emph-re (var val)
  2340. "Set variable and compute the emphasis regular expression."
  2341. (set var val)
  2342. (when (and (boundp 'org-emphasis-alist)
  2343. (boundp 'org-emphasis-regexp-components)
  2344. org-emphasis-alist org-emphasis-regexp-components)
  2345. (let* ((e org-emphasis-regexp-components)
  2346. (pre (car e))
  2347. (post (nth 1 e))
  2348. (border (nth 2 e))
  2349. (body (nth 3 e))
  2350. (nl (nth 4 e))
  2351. (body1 (concat body "*?"))
  2352. (markers (mapconcat 'car org-emphasis-alist ""))
  2353. (vmarkers (mapconcat
  2354. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2355. org-emphasis-alist "")))
  2356. ;; make sure special characters appear at the right position in the class
  2357. (if (string-match "\\^" markers)
  2358. (setq markers (concat (replace-match "" t t markers) "^")))
  2359. (if (string-match "-" markers)
  2360. (setq markers (concat (replace-match "" t t markers) "-")))
  2361. (if (string-match "\\^" vmarkers)
  2362. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2363. (if (string-match "-" vmarkers)
  2364. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2365. (if (> nl 0)
  2366. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2367. (int-to-string nl) "\\}")))
  2368. ;; Make the regexp
  2369. (setq org-emph-re
  2370. (concat "\\([" pre "]\\|^\\)"
  2371. "\\("
  2372. "\\([" markers "]\\)"
  2373. "\\("
  2374. "[^" border "]\\|"
  2375. "[^" border "]"
  2376. body1
  2377. "[^" border "]"
  2378. "\\)"
  2379. "\\3\\)"
  2380. "\\([" post "]\\|$\\)?"))
  2381. (setq org-verbatim-re
  2382. (concat "\\([" pre "]\\|^\\)"
  2383. "\\("
  2384. "\\([" vmarkers "]\\)"
  2385. "\\("
  2386. "[^" border "]\\|"
  2387. "[^" border "]"
  2388. body1
  2389. "[^" border "]"
  2390. "\\)"
  2391. "\\3\\)"
  2392. "\\([" post "]\\|$\\)?")))))
  2393. (defcustom org-emphasis-regexp-components
  2394. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2395. "Components used to build the regular expression for emphasis.
  2396. This is a list with 6 entries. Terminology: In an emphasis string
  2397. like \" *strong word* \", we call the initial space PREMATCH, the final
  2398. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2399. and \"trong wor\" is the body. The different components in this variable
  2400. specify what is allowed/forbidden in each part:
  2401. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2402. post Chars allowed as postmatch. End of line will be allowed too.
  2403. border The chars *forbidden* as border characters.
  2404. body-regexp A regexp like \".\" to match a body character. Don't use
  2405. non-shy groups here, and don't allow newline here.
  2406. newline The maximum number of newlines allowed in an emphasis exp.
  2407. Use customize to modify this, or restart Emacs after changing it."
  2408. :group 'org-font-lock
  2409. :set 'org-set-emph-re
  2410. :type '(list
  2411. (sexp :tag "Allowed chars in pre ")
  2412. (sexp :tag "Allowed chars in post ")
  2413. (sexp :tag "Forbidden chars in border ")
  2414. (sexp :tag "Regexp for body ")
  2415. (integer :tag "number of newlines allowed")
  2416. (option (boolean :tag "Please ignore this button"))))
  2417. (defcustom org-emphasis-alist
  2418. `(("*" bold "<b>" "</b>")
  2419. ("/" italic "<i>" "</i>")
  2420. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2421. ("=" org-code "<code>" "</code>" verbatim)
  2422. ("~" org-verbatim "<code>" "</code>" verbatim)
  2423. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2424. "<del>" "</del>")
  2425. )
  2426. "Special syntax for emphasized text.
  2427. Text starting and ending with a special character will be emphasized, for
  2428. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2429. characters, the face to be used by font-lock for highlighting in Org-mode
  2430. Emacs buffers, and the HTML tags to be used for this.
  2431. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2432. Use customize to modify this, or restart Emacs after changing it."
  2433. :group 'org-font-lock
  2434. :set 'org-set-emph-re
  2435. :type '(repeat
  2436. (list
  2437. (string :tag "Marker character")
  2438. (choice
  2439. (face :tag "Font-lock-face")
  2440. (plist :tag "Face property list"))
  2441. (string :tag "HTML start tag")
  2442. (string :tag "HTML end tag")
  2443. (option (const verbatim)))))
  2444. (defvar org-protecting-blocks
  2445. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2446. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2447. This is needed for font-lock setup.")
  2448. ;;; Miscellaneous options
  2449. (defgroup org-completion nil
  2450. "Completion in Org-mode."
  2451. :tag "Org Completion"
  2452. :group 'org)
  2453. (defcustom org-completion-use-ido nil
  2454. "Non-nil means, use ido completion wherever possible.
  2455. Note that `ido-mode' must be active for this variable to be relevant.
  2456. If you decide to turn this variable on, you might well want to turn off
  2457. `org-outline-path-complete-in-steps'."
  2458. :group 'org-completion
  2459. :type 'boolean)
  2460. (defcustom org-completion-fallback-command 'hippie-expand
  2461. "The expansion command called by \\[org-complete] in normal context.
  2462. Normal means, no org-mode-specific context."
  2463. :group 'org-completion
  2464. :type 'function)
  2465. ;;; Functions and variables from ther packages
  2466. ;; Declared here to avoid compiler warnings
  2467. ;; XEmacs only
  2468. (defvar outline-mode-menu-heading)
  2469. (defvar outline-mode-menu-show)
  2470. (defvar outline-mode-menu-hide)
  2471. (defvar zmacs-regions) ; XEmacs regions
  2472. ;; Emacs only
  2473. (defvar mark-active)
  2474. ;; Various packages
  2475. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2476. (declare-function calendar-forward-day "cal-move" (arg))
  2477. (declare-function calendar-goto-date "cal-move" (date))
  2478. (declare-function calendar-goto-today "cal-move" ())
  2479. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2480. (defvar calc-embedded-close-formula)
  2481. (defvar calc-embedded-open-formula)
  2482. (declare-function cdlatex-tab "ext:cdlatex" ())
  2483. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2484. (defvar font-lock-unfontify-region-function)
  2485. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2486. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2487. (defvar iswitchb-temp-buflist)
  2488. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2489. (defvar org-agenda-tags-todo-honor-ignore-options)
  2490. (declare-function org-agenda-skip "org-agenda" ())
  2491. (declare-function org-format-agenda-item "org-agenda"
  2492. (extra txt &optional category tags dotime noprefix remove-re))
  2493. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2494. (declare-function org-agenda-change-all-lines "org-agenda"
  2495. (newhead hdmarker &optional fixface just-this))
  2496. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2497. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2498. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2499. (beg end))
  2500. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2501. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2502. "org-agenda" (&optional end))
  2503. (declare-function parse-time-string "parse-time" (string))
  2504. (declare-function remember "remember" (&optional initial))
  2505. (declare-function remember-buffer-desc "remember" ())
  2506. (declare-function remember-finalize "remember" ())
  2507. (defvar remember-save-after-remembering)
  2508. (defvar remember-data-file)
  2509. (defvar remember-register)
  2510. (defvar remember-buffer)
  2511. (defvar remember-handler-functions)
  2512. (defvar remember-annotation-functions)
  2513. (defvar texmathp-why)
  2514. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2515. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2516. (defvar w3m-current-url)
  2517. (defvar w3m-current-title)
  2518. (defvar org-latex-regexps)
  2519. ;;; Autoload and prepare some org modules
  2520. ;; Some table stuff that needs to be defined here, because it is used
  2521. ;; by the functions setting up org-mode or checking for table context.
  2522. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2523. "Detects an org-type or table-type table.")
  2524. (defconst org-table-line-regexp "^[ \t]*|"
  2525. "Detects an org-type table line.")
  2526. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2527. "Detects an org-type table line.")
  2528. (defconst org-table-hline-regexp "^[ \t]*|-"
  2529. "Detects an org-type table hline.")
  2530. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2531. "Detects a table-type table hline.")
  2532. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2533. "Searching from within a table (any type) this finds the first line
  2534. outside the table.")
  2535. ;; Autoload the functions in org-table.el that are needed by functions here.
  2536. (eval-and-compile
  2537. (org-autoload "org-table"
  2538. '(org-table-align org-table-begin org-table-blank-field
  2539. org-table-convert org-table-convert-region org-table-copy-down
  2540. org-table-copy-region org-table-create
  2541. org-table-create-or-convert-from-region
  2542. org-table-create-with-table.el org-table-current-dline
  2543. org-table-cut-region org-table-delete-column org-table-edit-field
  2544. org-table-edit-formulas org-table-end org-table-eval-formula
  2545. org-table-export org-table-field-info
  2546. org-table-get-stored-formulas org-table-goto-column
  2547. org-table-hline-and-move org-table-import org-table-insert-column
  2548. org-table-insert-hline org-table-insert-row org-table-iterate
  2549. org-table-justify-field-maybe org-table-kill-row
  2550. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2551. org-table-move-column org-table-move-column-left
  2552. org-table-move-column-right org-table-move-row
  2553. org-table-move-row-down org-table-move-row-up
  2554. org-table-next-field org-table-next-row org-table-paste-rectangle
  2555. org-table-previous-field org-table-recalculate
  2556. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2557. org-table-toggle-coordinate-overlays
  2558. org-table-toggle-formula-debugger org-table-wrap-region
  2559. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2560. (defun org-at-table-p (&optional table-type)
  2561. "Return t if the cursor is inside an org-type table.
  2562. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2563. (if org-enable-table-editor
  2564. (save-excursion
  2565. (beginning-of-line 1)
  2566. (looking-at (if table-type org-table-any-line-regexp
  2567. org-table-line-regexp)))
  2568. nil))
  2569. (defsubst org-table-p () (org-at-table-p))
  2570. (defun org-at-table.el-p ()
  2571. "Return t if and only if we are at a table.el table."
  2572. (and (org-at-table-p 'any)
  2573. (save-excursion
  2574. (goto-char (org-table-begin 'any))
  2575. (looking-at org-table1-hline-regexp))))
  2576. (defun org-table-recognize-table.el ()
  2577. "If there is a table.el table nearby, recognize it and move into it."
  2578. (if org-table-tab-recognizes-table.el
  2579. (if (org-at-table.el-p)
  2580. (progn
  2581. (beginning-of-line 1)
  2582. (if (looking-at org-table-dataline-regexp)
  2583. nil
  2584. (if (looking-at org-table1-hline-regexp)
  2585. (progn
  2586. (beginning-of-line 2)
  2587. (if (looking-at org-table-any-border-regexp)
  2588. (beginning-of-line -1)))))
  2589. (if (re-search-forward "|" (org-table-end t) t)
  2590. (progn
  2591. (require 'table)
  2592. (if (table--at-cell-p (point))
  2593. t
  2594. (message "recognizing table.el table...")
  2595. (table-recognize-table)
  2596. (message "recognizing table.el table...done")))
  2597. (error "This should not happen..."))
  2598. t)
  2599. nil)
  2600. nil))
  2601. (defun org-at-table-hline-p ()
  2602. "Return t if the cursor is inside a hline in a table."
  2603. (if org-enable-table-editor
  2604. (save-excursion
  2605. (beginning-of-line 1)
  2606. (looking-at org-table-hline-regexp))
  2607. nil))
  2608. (defvar org-table-clean-did-remove-column nil)
  2609. (defun org-table-map-tables (function)
  2610. "Apply FUNCTION to the start of all tables in the buffer."
  2611. (save-excursion
  2612. (save-restriction
  2613. (widen)
  2614. (goto-char (point-min))
  2615. (while (re-search-forward org-table-any-line-regexp nil t)
  2616. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2617. (beginning-of-line 1)
  2618. (if (looking-at org-table-line-regexp)
  2619. (save-excursion (funcall function)))
  2620. (re-search-forward org-table-any-border-regexp nil 1))))
  2621. (message "Mapping tables: done"))
  2622. ;; Declare and autoload functions from org-exp.el & Co
  2623. (declare-function org-default-export-plist "org-exp")
  2624. (declare-function org-infile-export-plist "org-exp")
  2625. (declare-function org-get-current-options "org-exp")
  2626. (eval-and-compile
  2627. (org-autoload "org-exp"
  2628. '(org-export org-export-visible
  2629. org-insert-export-options-template
  2630. org-table-clean-before-export))
  2631. (org-autoload "org-ascii"
  2632. '(org-export-as-ascii org-export-ascii-preprocess
  2633. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2634. org-export-region-as-ascii))
  2635. (org-autoload "org-html"
  2636. '(org-export-as-html-and-open
  2637. org-export-as-html-batch org-export-as-html-to-buffer
  2638. org-replace-region-by-html org-export-region-as-html
  2639. org-export-as-html))
  2640. (org-autoload "org-icalendar"
  2641. '(org-export-icalendar-this-file
  2642. org-export-icalendar-all-agenda-files
  2643. org-export-icalendar-combine-agenda-files))
  2644. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2645. ;; Declare and autoload functions from org-agenda.el
  2646. (eval-and-compile
  2647. (org-autoload "org-agenda"
  2648. '(org-agenda org-agenda-list org-search-view
  2649. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2650. org-diary org-agenda-to-appt
  2651. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2652. ;; Autoload org-remember
  2653. (eval-and-compile
  2654. (org-autoload "org-remember"
  2655. '(org-remember-insinuate org-remember-annotation
  2656. org-remember-apply-template org-remember org-remember-handler)))
  2657. ;; Autoload org-clock.el
  2658. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2659. (beg end))
  2660. (declare-function org-clock-update-mode-line "org-clock" ())
  2661. (defvar org-clock-start-time)
  2662. (defvar org-clock-marker (make-marker)
  2663. "Marker recording the last clock-in.")
  2664. (defun org-clock-is-active ()
  2665. "Return non-nil if clock is currently running.
  2666. The return value is actually the clock marker."
  2667. (marker-buffer org-clock-marker))
  2668. (eval-and-compile
  2669. (org-autoload
  2670. "org-clock"
  2671. '(org-clock-in org-clock-out org-clock-cancel
  2672. org-clock-goto org-clock-sum org-clock-display
  2673. org-clock-remove-overlays org-clock-report
  2674. org-clocktable-shift org-dblock-write:clocktable
  2675. org-get-clocktable)))
  2676. (defun org-clock-update-time-maybe ()
  2677. "If this is a CLOCK line, update it and return t.
  2678. Otherwise, return nil."
  2679. (interactive)
  2680. (save-excursion
  2681. (beginning-of-line 1)
  2682. (skip-chars-forward " \t")
  2683. (when (looking-at org-clock-string)
  2684. (let ((re (concat "[ \t]*" org-clock-string
  2685. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2686. "\\([ \t]*=>.*\\)?\\)?"))
  2687. ts te h m s neg)
  2688. (cond
  2689. ((not (looking-at re))
  2690. nil)
  2691. ((not (match-end 2))
  2692. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2693. (> org-clock-marker (point))
  2694. (<= org-clock-marker (point-at-eol)))
  2695. ;; The clock is running here
  2696. (setq org-clock-start-time
  2697. (apply 'encode-time
  2698. (org-parse-time-string (match-string 1))))
  2699. (org-clock-update-mode-line)))
  2700. (t
  2701. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2702. (end-of-line 1)
  2703. (setq ts (match-string 1)
  2704. te (match-string 3))
  2705. (setq s (- (time-to-seconds
  2706. (apply 'encode-time (org-parse-time-string te)))
  2707. (time-to-seconds
  2708. (apply 'encode-time (org-parse-time-string ts))))
  2709. neg (< s 0)
  2710. s (abs s)
  2711. h (floor (/ s 3600))
  2712. s (- s (* 3600 h))
  2713. m (floor (/ s 60))
  2714. s (- s (* 60 s)))
  2715. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2716. t))))))
  2717. (defun org-check-running-clock ()
  2718. "Check if the current buffer contains the running clock.
  2719. If yes, offer to stop it and to save the buffer with the changes."
  2720. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2721. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2722. (buffer-name))))
  2723. (org-clock-out)
  2724. (when (y-or-n-p "Save changed buffer?")
  2725. (save-buffer))))
  2726. (defun org-clocktable-try-shift (dir n)
  2727. "Check if this line starts a clock table, if yes, shift the time block."
  2728. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2729. (org-clocktable-shift dir n)))
  2730. ;; Autoload org-timer.el
  2731. (eval-and-compile
  2732. (org-autoload
  2733. "org-timer"
  2734. '(org-timer-start org-timer org-timer-item
  2735. org-timer-change-times-in-region)))
  2736. ;; Autoload org-feed.el
  2737. (eval-and-compile
  2738. (org-autoload
  2739. "org-feed"
  2740. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2741. ;; Autoload archiving code
  2742. ;; The stuff that is needed for cycling and tags has to be defined here.
  2743. (defgroup org-archive nil
  2744. "Options concerning archiving in Org-mode."
  2745. :tag "Org Archive"
  2746. :group 'org-structure)
  2747. (defcustom org-archive-location "%s_archive::"
  2748. "The location where subtrees should be archived.
  2749. The value of this variable is a string, consisting of two parts,
  2750. separated by a double-colon. The first part is a filename and
  2751. the second part is a headline.
  2752. When the filename is omitted, archiving happens in the same file.
  2753. %s in the filename will be replaced by the current file
  2754. name (without the directory part). Archiving to a different file
  2755. is useful to keep archived entries from contributing to the
  2756. Org-mode Agenda.
  2757. The archived entries will be filed as subtrees of the specified
  2758. headline. When the headline is omitted, the subtrees are simply
  2759. filed away at the end of the file, as top-level entries. Also in
  2760. the heading you can use %s to represent the file name, this can be
  2761. useful when using the same archive for a number of different files.
  2762. Here are a few examples:
  2763. \"%s_archive::\"
  2764. If the current file is Projects.org, archive in file
  2765. Projects.org_archive, as top-level trees. This is the default.
  2766. \"::* Archived Tasks\"
  2767. Archive in the current file, under the top-level headline
  2768. \"* Archived Tasks\".
  2769. \"~/org/archive.org::\"
  2770. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2771. \"~/org/archive.org::From %s\"
  2772. Archive in file ~/org/archive.org (absolute path), und headlines
  2773. \"From FILENAME\" where file name is the current file name.
  2774. \"basement::** Finished Tasks\"
  2775. Archive in file ./basement (relative path), as level 3 trees
  2776. below the level 2 heading \"** Finished Tasks\".
  2777. You may set this option on a per-file basis by adding to the buffer a
  2778. line like
  2779. #+ARCHIVE: basement::** Finished Tasks
  2780. You may also define it locally for a subtree by setting an ARCHIVE property
  2781. in the entry. If such a property is found in an entry, or anywhere up
  2782. the hierarchy, it will be used."
  2783. :group 'org-archive
  2784. :type 'string)
  2785. (defcustom org-archive-tag "ARCHIVE"
  2786. "The tag that marks a subtree as archived.
  2787. An archived subtree does not open during visibility cycling, and does
  2788. not contribute to the agenda listings.
  2789. After changing this, font-lock must be restarted in the relevant buffers to
  2790. get the proper fontification."
  2791. :group 'org-archive
  2792. :group 'org-keywords
  2793. :type 'string)
  2794. (defcustom org-agenda-skip-archived-trees t
  2795. "Non-nil means, the agenda will skip any items located in archived trees.
  2796. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2797. variable is no longer recommended, you should leave it at the value t.
  2798. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2799. :group 'org-archive
  2800. :group 'org-agenda-skip
  2801. :type 'boolean)
  2802. (defcustom org-columns-skip-arrchived-trees t
  2803. "Non-nil means, irgnore archived trees when creating column view."
  2804. :group 'org-archive
  2805. :group 'org-properties
  2806. :type 'boolean)
  2807. (defcustom org-cycle-open-archived-trees nil
  2808. "Non-nil means, `org-cycle' will open archived trees.
  2809. An archived tree is a tree marked with the tag ARCHIVE.
  2810. When nil, archived trees will stay folded. You can still open them with
  2811. normal outline commands like `show-all', but not with the cycling commands."
  2812. :group 'org-archive
  2813. :group 'org-cycle
  2814. :type 'boolean)
  2815. (defcustom org-sparse-tree-open-archived-trees nil
  2816. "Non-nil means sparse tree construction shows matches in archived trees.
  2817. When nil, matches in these trees are highlighted, but the trees are kept in
  2818. collapsed state."
  2819. :group 'org-archive
  2820. :group 'org-sparse-trees
  2821. :type 'boolean)
  2822. (defun org-cycle-hide-archived-subtrees (state)
  2823. "Re-hide all archived subtrees after a visibility state change."
  2824. (when (and (not org-cycle-open-archived-trees)
  2825. (not (memq state '(overview folded))))
  2826. (save-excursion
  2827. (let* ((globalp (memq state '(contents all)))
  2828. (beg (if globalp (point-min) (point)))
  2829. (end (if globalp (point-max) (org-end-of-subtree t))))
  2830. (org-hide-archived-subtrees beg end)
  2831. (goto-char beg)
  2832. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2833. (message "%s" (substitute-command-keys
  2834. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2835. (defun org-force-cycle-archived ()
  2836. "Cycle subtree even if it is archived."
  2837. (interactive)
  2838. (setq this-command 'org-cycle)
  2839. (let ((org-cycle-open-archived-trees t))
  2840. (call-interactively 'org-cycle)))
  2841. (defun org-hide-archived-subtrees (beg end)
  2842. "Re-hide all archived subtrees after a visibility state change."
  2843. (save-excursion
  2844. (let* ((re (concat ":" org-archive-tag ":")))
  2845. (goto-char beg)
  2846. (while (re-search-forward re end t)
  2847. (and (org-on-heading-p) (hide-subtree))
  2848. (org-end-of-subtree t)))))
  2849. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2850. (eval-and-compile
  2851. (org-autoload "org-archive"
  2852. '(org-add-archive-files org-archive-subtree
  2853. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2854. ;; Autoload Column View Code
  2855. (declare-function org-columns-number-to-string "org-colview")
  2856. (declare-function org-columns-get-format-and-top-level "org-colview")
  2857. (declare-function org-columns-compute "org-colview")
  2858. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2859. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2860. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2861. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2862. ;; Autoload ID code
  2863. (declare-function org-id-store-link "org-id")
  2864. (declare-function org-id-locations-load "org-id")
  2865. (declare-function org-id-locations-save "org-id")
  2866. (defvar org-id-track-globally)
  2867. (org-autoload "org-id"
  2868. '(org-id-get-create org-id-new org-id-copy org-id-get
  2869. org-id-get-with-outline-path-completion
  2870. org-id-get-with-outline-drilling
  2871. org-id-goto org-id-find org-id-store-link))
  2872. ;; Autoload Plotting Code
  2873. (org-autoload "org-plot"
  2874. '(org-plot/gnuplot))
  2875. ;;; Variables for pre-computed regular expressions, all buffer local
  2876. (defvar org-drawer-regexp nil
  2877. "Matches first line of a hidden block.")
  2878. (make-variable-buffer-local 'org-drawer-regexp)
  2879. (defvar org-todo-regexp nil
  2880. "Matches any of the TODO state keywords.")
  2881. (make-variable-buffer-local 'org-todo-regexp)
  2882. (defvar org-not-done-regexp nil
  2883. "Matches any of the TODO state keywords except the last one.")
  2884. (make-variable-buffer-local 'org-not-done-regexp)
  2885. (defvar org-not-done-heading-regexp nil
  2886. "Matches a TODO headline that is not done.")
  2887. (make-variable-buffer-local 'org-not-done-regexp)
  2888. (defvar org-todo-line-regexp nil
  2889. "Matches a headline and puts TODO state into group 2 if present.")
  2890. (make-variable-buffer-local 'org-todo-line-regexp)
  2891. (defvar org-complex-heading-regexp nil
  2892. "Matches a headline and puts everything into groups:
  2893. group 1: the stars
  2894. group 2: The todo keyword, maybe
  2895. group 3: Priority cookie
  2896. group 4: True headline
  2897. group 5: Tags")
  2898. (make-variable-buffer-local 'org-complex-heading-regexp)
  2899. (defvar org-todo-line-tags-regexp nil
  2900. "Matches a headline and puts TODO state into group 2 if present.
  2901. Also put tags into group 4 if tags are present.")
  2902. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2903. (defvar org-nl-done-regexp nil
  2904. "Matches newline followed by a headline with the DONE keyword.")
  2905. (make-variable-buffer-local 'org-nl-done-regexp)
  2906. (defvar org-looking-at-done-regexp nil
  2907. "Matches the DONE keyword a point.")
  2908. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2909. (defvar org-ds-keyword-length 12
  2910. "Maximum length of the Deadline and SCHEDULED keywords.")
  2911. (make-variable-buffer-local 'org-ds-keyword-length)
  2912. (defvar org-deadline-regexp nil
  2913. "Matches the DEADLINE keyword.")
  2914. (make-variable-buffer-local 'org-deadline-regexp)
  2915. (defvar org-deadline-time-regexp nil
  2916. "Matches the DEADLINE keyword together with a time stamp.")
  2917. (make-variable-buffer-local 'org-deadline-time-regexp)
  2918. (defvar org-deadline-line-regexp nil
  2919. "Matches the DEADLINE keyword and the rest of the line.")
  2920. (make-variable-buffer-local 'org-deadline-line-regexp)
  2921. (defvar org-scheduled-regexp nil
  2922. "Matches the SCHEDULED keyword.")
  2923. (make-variable-buffer-local 'org-scheduled-regexp)
  2924. (defvar org-scheduled-time-regexp nil
  2925. "Matches the SCHEDULED keyword together with a time stamp.")
  2926. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2927. (defvar org-closed-time-regexp nil
  2928. "Matches the CLOSED keyword together with a time stamp.")
  2929. (make-variable-buffer-local 'org-closed-time-regexp)
  2930. (defvar org-keyword-time-regexp nil
  2931. "Matches any of the 4 keywords, together with the time stamp.")
  2932. (make-variable-buffer-local 'org-keyword-time-regexp)
  2933. (defvar org-keyword-time-not-clock-regexp nil
  2934. "Matches any of the 3 keywords, together with the time stamp.")
  2935. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2936. (defvar org-maybe-keyword-time-regexp nil
  2937. "Matches a timestamp, possibly preceeded by a keyword.")
  2938. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2939. (defvar org-planning-or-clock-line-re nil
  2940. "Matches a line with planning or clock info.")
  2941. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2942. (defconst org-plain-time-of-day-regexp
  2943. (concat
  2944. "\\(\\<[012]?[0-9]"
  2945. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2946. "\\(--?"
  2947. "\\(\\<[012]?[0-9]"
  2948. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2949. "\\)?")
  2950. "Regular expression to match a plain time or time range.
  2951. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2952. groups carry important information:
  2953. 0 the full match
  2954. 1 the first time, range or not
  2955. 8 the second time, if it is a range.")
  2956. (defconst org-plain-time-extension-regexp
  2957. (concat
  2958. "\\(\\<[012]?[0-9]"
  2959. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2960. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2961. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2962. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2963. groups carry important information:
  2964. 0 the full match
  2965. 7 hours of duration
  2966. 9 minutes of duration")
  2967. (defconst org-stamp-time-of-day-regexp
  2968. (concat
  2969. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2970. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2971. "\\(--?"
  2972. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2973. "Regular expression to match a timestamp time or time range.
  2974. After a match, the following groups carry important information:
  2975. 0 the full match
  2976. 1 date plus weekday, for backreferencing to make sure both times on same day
  2977. 2 the first time, range or not
  2978. 4 the second time, if it is a range.")
  2979. (defconst org-startup-options
  2980. '(("fold" org-startup-folded t)
  2981. ("overview" org-startup-folded t)
  2982. ("nofold" org-startup-folded nil)
  2983. ("showall" org-startup-folded nil)
  2984. ("content" org-startup-folded content)
  2985. ("hidestars" org-hide-leading-stars t)
  2986. ("showstars" org-hide-leading-stars nil)
  2987. ("odd" org-odd-levels-only t)
  2988. ("oddeven" org-odd-levels-only nil)
  2989. ("align" org-startup-align-all-tables t)
  2990. ("noalign" org-startup-align-all-tables nil)
  2991. ("customtime" org-display-custom-times t)
  2992. ("logdone" org-log-done time)
  2993. ("lognotedone" org-log-done note)
  2994. ("nologdone" org-log-done nil)
  2995. ("lognoteclock-out" org-log-note-clock-out t)
  2996. ("nolognoteclock-out" org-log-note-clock-out nil)
  2997. ("logrepeat" org-log-repeat state)
  2998. ("lognoterepeat" org-log-repeat note)
  2999. ("nologrepeat" org-log-repeat nil)
  3000. ("fninline" org-footnote-define-inline t)
  3001. ("nofninline" org-footnote-define-inline nil)
  3002. ("fnlocal" org-footnote-section nil)
  3003. ("fnauto" org-footnote-auto-label t)
  3004. ("fnprompt" org-footnote-auto-label nil)
  3005. ("fnconfirm" org-footnote-auto-label confirm)
  3006. ("fnplain" org-footnote-auto-label plain)
  3007. ("fnadjust" org-footnote-auto-adjust t)
  3008. ("nofnadjust" org-footnote-auto-adjust nil)
  3009. ("constcgs" constants-unit-system cgs)
  3010. ("constSI" constants-unit-system SI)
  3011. ("noptag" org-tag-persistent-alist nil)
  3012. ("hideblocks" org-hide-block-startup t)
  3013. ("nohideblocks" org-hide-block-startup nil))
  3014. "Variable associated with STARTUP options for org-mode.
  3015. Each element is a list of three items: The startup options as written
  3016. in the #+STARTUP line, the corresponding variable, and the value to
  3017. set this variable to if the option is found. An optional forth element PUSH
  3018. means to push this value onto the list in the variable.")
  3019. (defun org-set-regexps-and-options ()
  3020. "Precompute regular expressions for current buffer."
  3021. (when (org-mode-p)
  3022. (org-set-local 'org-todo-kwd-alist nil)
  3023. (org-set-local 'org-todo-key-alist nil)
  3024. (org-set-local 'org-todo-key-trigger nil)
  3025. (org-set-local 'org-todo-keywords-1 nil)
  3026. (org-set-local 'org-done-keywords nil)
  3027. (org-set-local 'org-todo-heads nil)
  3028. (org-set-local 'org-todo-sets nil)
  3029. (org-set-local 'org-todo-log-states nil)
  3030. (org-set-local 'org-file-properties nil)
  3031. (org-set-local 'org-file-tags nil)
  3032. (let ((re (org-make-options-regexp
  3033. '("CATEGORY" "TODO" "COLUMNS"
  3034. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3035. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3036. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3037. (splitre "[ \t]+")
  3038. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3039. tail sep kws1 prio props ftags drawers
  3040. ext-setup-or-nil setup-contents (start 0))
  3041. (save-excursion
  3042. (save-restriction
  3043. (widen)
  3044. (goto-char (point-min))
  3045. (while (or (and ext-setup-or-nil
  3046. (string-match re ext-setup-or-nil start)
  3047. (setq start (match-end 0)))
  3048. (and (setq ext-setup-or-nil nil start 0)
  3049. (re-search-forward re nil t)))
  3050. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3051. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3052. (cond
  3053. ((equal key "CATEGORY")
  3054. (if (string-match "[ \t]+$" value)
  3055. (setq value (replace-match "" t t value)))
  3056. (setq cat value))
  3057. ((member key '("SEQ_TODO" "TODO"))
  3058. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3059. ((equal key "TYP_TODO")
  3060. (push (cons 'type (org-split-string value splitre)) kwds))
  3061. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3062. ;; general TODO-like setup
  3063. (push (cons (intern (downcase (match-string 1 key)))
  3064. (org-split-string value splitre)) kwds))
  3065. ((equal key "TAGS")
  3066. (setq tags (append tags (if tags '("\\n") nil)
  3067. (org-split-string value splitre))))
  3068. ((equal key "COLUMNS")
  3069. (org-set-local 'org-columns-default-format value))
  3070. ((equal key "LINK")
  3071. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3072. (push (cons (match-string 1 value)
  3073. (org-trim (match-string 2 value)))
  3074. links)))
  3075. ((equal key "PRIORITIES")
  3076. (setq prio (org-split-string value " +")))
  3077. ((equal key "PROPERTY")
  3078. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3079. (push (cons (match-string 1 value) (match-string 2 value))
  3080. props)))
  3081. ((equal key "FILETAGS")
  3082. (when (string-match "\\S-" value)
  3083. (setq ftags
  3084. (append
  3085. ftags
  3086. (apply 'append
  3087. (mapcar (lambda (x) (org-split-string x ":"))
  3088. (org-split-string value)))))))
  3089. ((equal key "DRAWERS")
  3090. (setq drawers (org-split-string value splitre)))
  3091. ((equal key "CONSTANTS")
  3092. (setq const (append const (org-split-string value splitre))))
  3093. ((equal key "STARTUP")
  3094. (let ((opts (org-split-string value splitre))
  3095. l var val)
  3096. (while (setq l (pop opts))
  3097. (when (setq l (assoc l org-startup-options))
  3098. (setq var (nth 1 l) val (nth 2 l))
  3099. (if (not (nth 3 l))
  3100. (set (make-local-variable var) val)
  3101. (if (not (listp (symbol-value var)))
  3102. (set (make-local-variable var) nil))
  3103. (set (make-local-variable var) (symbol-value var))
  3104. (add-to-list var val))))))
  3105. ((equal key "ARCHIVE")
  3106. (string-match " *$" value)
  3107. (setq arch (replace-match "" t t value))
  3108. (remove-text-properties 0 (length arch)
  3109. '(face t fontified t) arch))
  3110. ((equal key "SETUPFILE")
  3111. (setq setup-contents (org-file-contents
  3112. (expand-file-name
  3113. (org-remove-double-quotes value))
  3114. 'noerror))
  3115. (if (not ext-setup-or-nil)
  3116. (setq ext-setup-or-nil setup-contents start 0)
  3117. (setq ext-setup-or-nil
  3118. (concat (substring ext-setup-or-nil 0 start)
  3119. "\n" setup-contents "\n"
  3120. (substring ext-setup-or-nil start)))))
  3121. ))))
  3122. (when cat
  3123. (org-set-local 'org-category (intern cat))
  3124. (push (cons "CATEGORY" cat) props))
  3125. (when prio
  3126. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3127. (setq prio (mapcar 'string-to-char prio))
  3128. (org-set-local 'org-highest-priority (nth 0 prio))
  3129. (org-set-local 'org-lowest-priority (nth 1 prio))
  3130. (org-set-local 'org-default-priority (nth 2 prio)))
  3131. (and props (org-set-local 'org-file-properties (nreverse props)))
  3132. (and ftags (org-set-local 'org-file-tags
  3133. (mapcar 'org-add-prop-inherited ftags)))
  3134. (and drawers (org-set-local 'org-drawers drawers))
  3135. (and arch (org-set-local 'org-archive-location arch))
  3136. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3137. ;; Process the TODO keywords
  3138. (unless kwds
  3139. ;; Use the global values as if they had been given locally.
  3140. (setq kwds (default-value 'org-todo-keywords))
  3141. (if (stringp (car kwds))
  3142. (setq kwds (list (cons org-todo-interpretation
  3143. (default-value 'org-todo-keywords)))))
  3144. (setq kwds (reverse kwds)))
  3145. (setq kwds (nreverse kwds))
  3146. (let (inter kws kw)
  3147. (while (setq kws (pop kwds))
  3148. (let ((kws (or
  3149. (run-hook-with-args-until-success
  3150. 'org-todo-setup-filter-hook kws)
  3151. kws)))
  3152. (setq inter (pop kws) sep (member "|" kws)
  3153. kws0 (delete "|" (copy-sequence kws))
  3154. kwsa nil
  3155. kws1 (mapcar
  3156. (lambda (x)
  3157. ;; 1 2
  3158. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3159. (progn
  3160. (setq kw (match-string 1 x)
  3161. key (and (match-end 2) (match-string 2 x))
  3162. log (org-extract-log-state-settings x))
  3163. (push (cons kw (and key (string-to-char key))) kwsa)
  3164. (and log (push log org-todo-log-states))
  3165. kw)
  3166. (error "Invalid TODO keyword %s" x)))
  3167. kws0)
  3168. kwsa (if kwsa (append '((:startgroup))
  3169. (nreverse kwsa)
  3170. '((:endgroup))))
  3171. hw (car kws1)
  3172. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3173. tail (list inter hw (car dws) (org-last dws))))
  3174. (add-to-list 'org-todo-heads hw 'append)
  3175. (push kws1 org-todo-sets)
  3176. (setq org-done-keywords (append org-done-keywords dws nil))
  3177. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3178. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3179. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3180. (setq org-todo-sets (nreverse org-todo-sets)
  3181. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3182. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3183. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3184. ;; Process the constants
  3185. (when const
  3186. (let (e cst)
  3187. (while (setq e (pop const))
  3188. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3189. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3190. (setq org-table-formula-constants-local cst)))
  3191. ;; Process the tags.
  3192. (when tags
  3193. (let (e tgs)
  3194. (while (setq e (pop tags))
  3195. (cond
  3196. ((equal e "{") (push '(:startgroup) tgs))
  3197. ((equal e "}") (push '(:endgroup) tgs))
  3198. ((equal e "\\n") (push '(:newline) tgs))
  3199. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3200. (push (cons (match-string 1 e)
  3201. (string-to-char (match-string 2 e)))
  3202. tgs))
  3203. (t (push (list e) tgs))))
  3204. (org-set-local 'org-tag-alist nil)
  3205. (while (setq e (pop tgs))
  3206. (or (and (stringp (car e))
  3207. (assoc (car e) org-tag-alist))
  3208. (push e org-tag-alist)))))
  3209. ;; Compute the regular expressions and other local variables
  3210. (if (not org-done-keywords)
  3211. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3212. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3213. (length org-scheduled-string)
  3214. (length org-clock-string)
  3215. (length org-closed-string)))
  3216. org-drawer-regexp
  3217. (concat "^[ \t]*:\\("
  3218. (mapconcat 'regexp-quote org-drawers "\\|")
  3219. "\\):[ \t]*$")
  3220. org-not-done-keywords
  3221. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3222. org-todo-regexp
  3223. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3224. "\\|") "\\)\\>")
  3225. org-not-done-regexp
  3226. (concat "\\<\\("
  3227. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3228. "\\)\\>")
  3229. org-not-done-heading-regexp
  3230. (concat "^\\(\\*+\\)[ \t]+\\("
  3231. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3232. "\\)\\>")
  3233. org-todo-line-regexp
  3234. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3235. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3236. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3237. org-complex-heading-regexp
  3238. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3239. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3240. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3241. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3242. org-nl-done-regexp
  3243. (concat "\n\\*+[ \t]+"
  3244. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3245. "\\)" "\\>")
  3246. org-todo-line-tags-regexp
  3247. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3248. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3249. (org-re
  3250. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3251. org-looking-at-done-regexp
  3252. (concat "^" "\\(?:"
  3253. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3254. "\\>")
  3255. org-deadline-regexp (concat "\\<" org-deadline-string)
  3256. org-deadline-time-regexp
  3257. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3258. org-deadline-line-regexp
  3259. (concat "\\<\\(" org-deadline-string "\\).*")
  3260. org-scheduled-regexp
  3261. (concat "\\<" org-scheduled-string)
  3262. org-scheduled-time-regexp
  3263. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3264. org-closed-time-regexp
  3265. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3266. org-keyword-time-regexp
  3267. (concat "\\<\\(" org-scheduled-string
  3268. "\\|" org-deadline-string
  3269. "\\|" org-closed-string
  3270. "\\|" org-clock-string "\\)"
  3271. " *[[<]\\([^]>]+\\)[]>]")
  3272. org-keyword-time-not-clock-regexp
  3273. (concat "\\<\\(" org-scheduled-string
  3274. "\\|" org-deadline-string
  3275. "\\|" org-closed-string
  3276. "\\)"
  3277. " *[[<]\\([^]>]+\\)[]>]")
  3278. org-maybe-keyword-time-regexp
  3279. (concat "\\(\\<\\(" org-scheduled-string
  3280. "\\|" org-deadline-string
  3281. "\\|" org-closed-string
  3282. "\\|" org-clock-string "\\)\\)?"
  3283. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3284. org-planning-or-clock-line-re
  3285. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3286. "\\|" org-deadline-string
  3287. "\\|" org-closed-string "\\|" org-clock-string
  3288. "\\)\\>\\)")
  3289. )
  3290. (org-compute-latex-and-specials-regexp)
  3291. (org-set-font-lock-defaults))))
  3292. (defun org-file-contents (file &optional noerror)
  3293. "Return the contents of FILE, as a string."
  3294. (if (or (not file)
  3295. (not (file-readable-p file)))
  3296. (if noerror
  3297. (progn
  3298. (message "Cannot read file %s" file)
  3299. (ding) (sit-for 2)
  3300. "")
  3301. (error "Cannot read file %s" file))
  3302. (with-temp-buffer
  3303. (insert-file-contents file)
  3304. (buffer-string))))
  3305. (defun org-extract-log-state-settings (x)
  3306. "Extract the log state setting from a TODO keyword string.
  3307. This will extract info from a string like \"WAIT(w@/!)\"."
  3308. (let (kw key log1 log2)
  3309. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3310. (setq kw (match-string 1 x)
  3311. key (and (match-end 2) (match-string 2 x))
  3312. log1 (and (match-end 3) (match-string 3 x))
  3313. log2 (and (match-end 4) (match-string 4 x)))
  3314. (and (or log1 log2)
  3315. (list kw
  3316. (and log1 (if (equal log1 "!") 'time 'note))
  3317. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3318. (defun org-remove-keyword-keys (list)
  3319. "Remove a pair of parenthesis at the end of each string in LIST."
  3320. (mapcar (lambda (x)
  3321. (if (string-match "(.*)$" x)
  3322. (substring x 0 (match-beginning 0))
  3323. x))
  3324. list))
  3325. ;; FIXME: this could be done much better, using second characters etc.
  3326. (defun org-assign-fast-keys (alist)
  3327. "Assign fast keys to a keyword-key alist.
  3328. Respect keys that are already there."
  3329. (let (new e k c c1 c2 (char ?a))
  3330. (while (setq e (pop alist))
  3331. (cond
  3332. ((equal e '(:startgroup)) (push e new))
  3333. ((equal e '(:endgroup)) (push e new))
  3334. ((equal e '(:newline)) (push e new))
  3335. (t
  3336. (setq k (car e) c2 nil)
  3337. (if (cdr e)
  3338. (setq c (cdr e))
  3339. ;; automatically assign a character.
  3340. (setq c1 (string-to-char
  3341. (downcase (substring
  3342. k (if (= (string-to-char k) ?@) 1 0)))))
  3343. (if (or (rassoc c1 new) (rassoc c1 alist))
  3344. (while (or (rassoc char new) (rassoc char alist))
  3345. (setq char (1+ char)))
  3346. (setq c2 c1))
  3347. (setq c (or c2 char)))
  3348. (push (cons k c) new))))
  3349. (nreverse new)))
  3350. ;;; Some variables used in various places
  3351. (defvar org-window-configuration nil
  3352. "Used in various places to store a window configuration.")
  3353. (defvar org-finish-function nil
  3354. "Function to be called when `C-c C-c' is used.
  3355. This is for getting out of special buffers like remember.")
  3356. ;; FIXME: Occasionally check by commenting these, to make sure
  3357. ;; no other functions uses these, forgetting to let-bind them.
  3358. (defvar entry)
  3359. (defvar last-state)
  3360. (defvar date)
  3361. ;; Defined somewhere in this file, but used before definition.
  3362. (defvar org-html-entities)
  3363. (defvar org-struct-menu)
  3364. (defvar org-org-menu)
  3365. (defvar org-tbl-menu)
  3366. (defvar org-agenda-keymap)
  3367. ;;;; Define the Org-mode
  3368. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3369. (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."))
  3370. ;; We use a before-change function to check if a table might need
  3371. ;; an update.
  3372. (defvar org-table-may-need-update t
  3373. "Indicates that a table might need an update.
  3374. This variable is set by `org-before-change-function'.
  3375. `org-table-align' sets it back to nil.")
  3376. (defun org-before-change-function (beg end)
  3377. "Every change indicates that a table might need an update."
  3378. (setq org-table-may-need-update t))
  3379. (defvar org-mode-map)
  3380. (defvar org-mode-hook nil
  3381. "Mode hook for Org-mode, run after the mode was turned on.")
  3382. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3383. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3384. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3385. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3386. (defvar org-table-buffer-is-an nil)
  3387. (defconst org-outline-regexp "\\*+ ")
  3388. ;;;###autoload
  3389. (define-derived-mode org-mode outline-mode "Org"
  3390. "Outline-based notes management and organizer, alias
  3391. \"Carsten's outline-mode for keeping track of everything.\"
  3392. Org-mode develops organizational tasks around a NOTES file which
  3393. contains information about projects as plain text. Org-mode is
  3394. implemented on top of outline-mode, which is ideal to keep the content
  3395. of large files well structured. It supports ToDo items, deadlines and
  3396. time stamps, which magically appear in the diary listing of the Emacs
  3397. calendar. Tables are easily created with a built-in table editor.
  3398. Plain text URL-like links connect to websites, emails (VM), Usenet
  3399. messages (Gnus), BBDB entries, and any files related to the project.
  3400. For printing and sharing of notes, an Org-mode file (or a part of it)
  3401. can be exported as a structured ASCII or HTML file.
  3402. The following commands are available:
  3403. \\{org-mode-map}"
  3404. ;; Get rid of Outline menus, they are not needed
  3405. ;; Need to do this here because define-derived-mode sets up
  3406. ;; the keymap so late. Still, it is a waste to call this each time
  3407. ;; we switch another buffer into org-mode.
  3408. (if (featurep 'xemacs)
  3409. (when (boundp 'outline-mode-menu-heading)
  3410. ;; Assume this is Greg's port, it used easymenu
  3411. (easy-menu-remove outline-mode-menu-heading)
  3412. (easy-menu-remove outline-mode-menu-show)
  3413. (easy-menu-remove outline-mode-menu-hide))
  3414. (define-key org-mode-map [menu-bar headings] 'undefined)
  3415. (define-key org-mode-map [menu-bar hide] 'undefined)
  3416. (define-key org-mode-map [menu-bar show] 'undefined))
  3417. (org-load-modules-maybe)
  3418. (easy-menu-add org-org-menu)
  3419. (easy-menu-add org-tbl-menu)
  3420. (org-install-agenda-files-menu)
  3421. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3422. (org-add-to-invisibility-spec '(org-cwidth))
  3423. (org-add-to-invisibility-spec '(org-hide-block . t))
  3424. (when (featurep 'xemacs)
  3425. (org-set-local 'line-move-ignore-invisible t))
  3426. (org-set-local 'outline-regexp org-outline-regexp)
  3427. (org-set-local 'outline-level 'org-outline-level)
  3428. (when (and org-ellipsis
  3429. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3430. (fboundp 'make-glyph-code))
  3431. (unless org-display-table
  3432. (setq org-display-table (make-display-table)))
  3433. (set-display-table-slot
  3434. org-display-table 4
  3435. (vconcat (mapcar
  3436. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3437. org-ellipsis)))
  3438. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3439. (setq buffer-display-table org-display-table))
  3440. (org-set-regexps-and-options)
  3441. (when (and org-tag-faces (not org-tags-special-faces-re))
  3442. ;; tag faces set outside customize.... force initialization.
  3443. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3444. ;; Calc embedded
  3445. (org-set-local 'calc-embedded-open-mode "# ")
  3446. (modify-syntax-entry ?# "<")
  3447. (modify-syntax-entry ?@ "w")
  3448. (if org-startup-truncated (setq truncate-lines t))
  3449. (org-set-local 'font-lock-unfontify-region-function
  3450. 'org-unfontify-region)
  3451. ;; Activate before-change-function
  3452. (org-set-local 'org-table-may-need-update t)
  3453. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3454. 'local)
  3455. ;; Check for running clock before killing a buffer
  3456. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3457. ;; Paragraphs and auto-filling
  3458. (org-set-autofill-regexps)
  3459. (setq indent-line-function 'org-indent-line-function)
  3460. (org-update-radio-target-regexp)
  3461. ;; Make sure dependence stuff works reliably, even for users who set it
  3462. ;; too late :-(
  3463. (if org-enforce-todo-dependencies
  3464. (add-hook 'org-blocker-hook
  3465. 'org-block-todo-from-children-or-siblings-or-parent)
  3466. (remove-hook 'org-blocker-hook
  3467. 'org-block-todo-from-children-or-siblings-or-parent))
  3468. (if org-enforce-todo-checkbox-dependencies
  3469. (add-hook 'org-blocker-hook
  3470. 'org-block-todo-from-checkboxes)
  3471. (remove-hook 'org-blocker-hook
  3472. 'org-block-todo-from-checkboxes))
  3473. ;; Comment characters
  3474. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3475. (org-set-local 'comment-padding " ")
  3476. ;; Align options lines
  3477. (org-set-local
  3478. 'align-mode-rules-list
  3479. '((org-in-buffer-settings
  3480. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3481. (modes . '(org-mode)))))
  3482. ;; Imenu
  3483. (org-set-local 'imenu-create-index-function
  3484. 'org-imenu-get-tree)
  3485. ;; Make isearch reveal context
  3486. (if (or (featurep 'xemacs)
  3487. (not (boundp 'outline-isearch-open-invisible-function)))
  3488. ;; Emacs 21 and XEmacs make use of the hook
  3489. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3490. ;; Emacs 22 deals with this through a special variable
  3491. (org-set-local 'outline-isearch-open-invisible-function
  3492. (lambda (&rest ignore) (org-show-context 'isearch))))
  3493. ;; If empty file that did not turn on org-mode automatically, make it to.
  3494. (if (and org-insert-mode-line-in-empty-file
  3495. (interactive-p)
  3496. (= (point-min) (point-max)))
  3497. (insert "# -*- mode: org -*-\n\n"))
  3498. (unless org-inhibit-startup
  3499. (when org-startup-align-all-tables
  3500. (let ((bmp (buffer-modified-p)))
  3501. (org-table-map-tables 'org-table-align)
  3502. (set-buffer-modified-p bmp)))
  3503. (org-set-startup-visibility)))
  3504. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3505. (defun org-current-time ()
  3506. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3507. (if (> (car org-time-stamp-rounding-minutes) 1)
  3508. (let ((r (car org-time-stamp-rounding-minutes))
  3509. (time (decode-time)))
  3510. (apply 'encode-time
  3511. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3512. (nthcdr 2 time))))
  3513. (current-time)))
  3514. ;;;; Font-Lock stuff, including the activators
  3515. (defvar org-mouse-map (make-sparse-keymap))
  3516. (org-defkey org-mouse-map
  3517. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3518. (org-defkey org-mouse-map
  3519. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3520. (when org-mouse-1-follows-link
  3521. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3522. (when org-tab-follows-link
  3523. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3524. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3525. (require 'font-lock)
  3526. (defconst org-non-link-chars "]\t\n\r<>")
  3527. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3528. "shell" "elisp"))
  3529. (defvar org-link-types-re nil
  3530. "Matches a link that has a url-like prefix like \"http:\"")
  3531. (defvar org-link-re-with-space nil
  3532. "Matches a link with spaces, optional angular brackets around it.")
  3533. (defvar org-link-re-with-space2 nil
  3534. "Matches a link with spaces, optional angular brackets around it.")
  3535. (defvar org-link-re-with-space3 nil
  3536. "Matches a link with spaces, only for internal part in bracket links.")
  3537. (defvar org-angle-link-re nil
  3538. "Matches link with angular brackets, spaces are allowed.")
  3539. (defvar org-plain-link-re nil
  3540. "Matches plain link, without spaces.")
  3541. (defvar org-bracket-link-regexp nil
  3542. "Matches a link in double brackets.")
  3543. (defvar org-bracket-link-analytic-regexp nil
  3544. "Regular expression used to analyze links.
  3545. Here is what the match groups contain after a match:
  3546. 1: http:
  3547. 2: http
  3548. 3: path
  3549. 4: [desc]
  3550. 5: desc")
  3551. (defvar org-bracket-link-analytic-regexp++ nil
  3552. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3553. (defvar org-any-link-re nil
  3554. "Regular expression matching any link.")
  3555. (defun org-make-link-regexps ()
  3556. "Update the link regular expressions.
  3557. This should be called after the variable `org-link-types' has changed."
  3558. (setq org-link-types-re
  3559. (concat
  3560. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3561. org-link-re-with-space
  3562. (concat
  3563. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3564. "\\([^" org-non-link-chars " ]"
  3565. "[^" org-non-link-chars "]*"
  3566. "[^" org-non-link-chars " ]\\)>?")
  3567. org-link-re-with-space2
  3568. (concat
  3569. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3570. "\\([^" org-non-link-chars " ]"
  3571. "[^\t\n\r]*"
  3572. "[^" org-non-link-chars " ]\\)>?")
  3573. org-link-re-with-space3
  3574. (concat
  3575. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3576. "\\([^" org-non-link-chars " ]"
  3577. "[^\t\n\r]*\\)")
  3578. org-angle-link-re
  3579. (concat
  3580. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3581. "\\([^" org-non-link-chars " ]"
  3582. "[^" org-non-link-chars "]*"
  3583. "\\)>")
  3584. org-plain-link-re
  3585. (concat
  3586. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3587. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3588. org-bracket-link-regexp
  3589. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3590. org-bracket-link-analytic-regexp
  3591. (concat
  3592. "\\[\\["
  3593. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3594. "\\([^]]+\\)"
  3595. "\\]"
  3596. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3597. "\\]")
  3598. org-bracket-link-analytic-regexp++
  3599. (concat
  3600. "\\[\\["
  3601. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3602. "\\([^]]+\\)"
  3603. "\\]"
  3604. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3605. "\\]")
  3606. org-any-link-re
  3607. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3608. org-angle-link-re "\\)\\|\\("
  3609. org-plain-link-re "\\)")))
  3610. (org-make-link-regexps)
  3611. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3612. "Regular expression for fast time stamp matching.")
  3613. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3614. "Regular expression for fast time stamp matching.")
  3615. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3616. "Regular expression matching time strings for analysis.
  3617. This one does not require the space after the date, so it can be used
  3618. on a string that terminates immediately after the date.")
  3619. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3620. "Regular expression matching time strings for analysis.")
  3621. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3622. "Regular expression matching time stamps, with groups.")
  3623. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3624. "Regular expression matching time stamps (also [..]), with groups.")
  3625. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3626. "Regular expression matching a time stamp range.")
  3627. (defconst org-tr-regexp-both
  3628. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3629. "Regular expression matching a time stamp range.")
  3630. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3631. org-ts-regexp "\\)?")
  3632. "Regular expression matching a time stamp or time stamp range.")
  3633. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3634. org-ts-regexp-both "\\)?")
  3635. "Regular expression matching a time stamp or time stamp range.
  3636. The time stamps may be either active or inactive.")
  3637. (defvar org-emph-face nil)
  3638. (defun org-do-emphasis-faces (limit)
  3639. "Run through the buffer and add overlays to links."
  3640. (let (rtn a)
  3641. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3642. (if (not (= (char-after (match-beginning 3))
  3643. (char-after (match-beginning 4))))
  3644. (progn
  3645. (setq rtn t)
  3646. (setq a (assoc (match-string 3) org-emphasis-alist))
  3647. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3648. 'face
  3649. (nth 1 a))
  3650. (and (nth 4 a)
  3651. (org-remove-flyspell-overlays-in
  3652. (match-beginning 0) (match-end 0)))
  3653. (add-text-properties (match-beginning 2) (match-end 2)
  3654. '(font-lock-multiline t))
  3655. (when org-hide-emphasis-markers
  3656. (add-text-properties (match-end 4) (match-beginning 5)
  3657. '(invisible org-link))
  3658. (add-text-properties (match-beginning 3) (match-end 3)
  3659. '(invisible org-link)))))
  3660. (backward-char 1))
  3661. rtn))
  3662. (defun org-emphasize (&optional char)
  3663. "Insert or change an emphasis, i.e. a font like bold or italic.
  3664. If there is an active region, change that region to a new emphasis.
  3665. If there is no region, just insert the marker characters and position
  3666. the cursor between them.
  3667. CHAR should be either the marker character, or the first character of the
  3668. HTML tag associated with that emphasis. If CHAR is a space, the means
  3669. to remove the emphasis of the selected region.
  3670. If char is not given (for example in an interactive call) it
  3671. will be prompted for."
  3672. (interactive)
  3673. (let ((eal org-emphasis-alist) e det
  3674. (erc org-emphasis-regexp-components)
  3675. (prompt "")
  3676. (string "") beg end move tag c s)
  3677. (if (org-region-active-p)
  3678. (setq beg (region-beginning) end (region-end)
  3679. string (buffer-substring beg end))
  3680. (setq move t))
  3681. (while (setq e (pop eal))
  3682. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3683. c (aref tag 0))
  3684. (push (cons c (string-to-char (car e))) det)
  3685. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3686. (substring tag 1)))))
  3687. (setq det (nreverse det))
  3688. (unless char
  3689. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3690. (setq char (read-char-exclusive)))
  3691. (setq char (or (cdr (assoc char det)) char))
  3692. (if (equal char ?\ )
  3693. (setq s "" move nil)
  3694. (unless (assoc (char-to-string char) org-emphasis-alist)
  3695. (error "No such emphasis marker: \"%c\"" char))
  3696. (setq s (char-to-string char)))
  3697. (while (and (> (length string) 1)
  3698. (equal (substring string 0 1) (substring string -1))
  3699. (assoc (substring string 0 1) org-emphasis-alist))
  3700. (setq string (substring string 1 -1)))
  3701. (setq string (concat s string s))
  3702. (if beg (delete-region beg end))
  3703. (unless (or (bolp)
  3704. (string-match (concat "[" (nth 0 erc) "\n]")
  3705. (char-to-string (char-before (point)))))
  3706. (insert " "))
  3707. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3708. (char-to-string (char-after (point))))
  3709. (insert " ") (backward-char 1))
  3710. (insert string)
  3711. (and move (backward-char 1))))
  3712. (defconst org-nonsticky-props
  3713. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3714. (defsubst org-rear-nonsticky-at (pos)
  3715. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3716. (defun org-activate-plain-links (limit)
  3717. "Run through the buffer and add overlays to links."
  3718. (catch 'exit
  3719. (let (f)
  3720. (while (re-search-forward org-plain-link-re limit t)
  3721. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3722. (setq f (get-text-property (match-beginning 0) 'face))
  3723. (if (or (eq f 'org-tag)
  3724. (and (listp f) (memq 'org-tag f)))
  3725. nil
  3726. (add-text-properties (match-beginning 0) (match-end 0)
  3727. (list 'mouse-face 'highlight
  3728. 'keymap org-mouse-map))
  3729. (org-rear-nonsticky-at (match-end 0))
  3730. (throw 'exit t))))))
  3731. (defun org-activate-code (limit)
  3732. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3733. (progn
  3734. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3735. (remove-text-properties (match-beginning 0) (match-end 0)
  3736. '(display t invisible t intangible t))
  3737. t)))
  3738. (defun org-fontify-meta-lines-and-blocks (limit)
  3739. "Fontify #+ lines and blocks, in the correct ways."
  3740. (let ((case-fold-search t))
  3741. (if (re-search-forward
  3742. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3743. limit t)
  3744. (let ((beg (match-beginning 0))
  3745. (beg1 (line-beginning-position 2))
  3746. (dc1 (downcase (match-string 2)))
  3747. (dc3 (downcase (match-string 3)))
  3748. end end1 quoting)
  3749. (cond
  3750. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3751. ;; a single line of backend-specific content
  3752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3753. (remove-text-properties (match-beginning 0) (match-end 0)
  3754. '(display t invisible t intangible t))
  3755. (add-text-properties (match-beginning 1) (match-end 3)
  3756. '(font-lock-fontified t face org-meta-line))
  3757. (add-text-properties (match-beginning 6) (match-end 6)
  3758. '(font-lock-fontified t face org-block))
  3759. t)
  3760. ((and (match-end 4) (equal dc3 "begin"))
  3761. ;; Truely a block
  3762. (setq quoting (member (downcase (match-string 5))
  3763. org-protecting-blocks))
  3764. (when (re-search-forward
  3765. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3766. nil t) ;; on purpose, we look further than LIMIT
  3767. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3768. (when quoting
  3769. (remove-text-properties beg end
  3770. '(display t invisible t intangible t)))
  3771. (add-text-properties
  3772. beg end
  3773. '(font-lock-fontified t font-lock-multiline t))
  3774. (add-text-properties beg beg1 '(face org-meta-line))
  3775. (add-text-properties end1 end '(face org-meta-line))
  3776. (when quoting
  3777. (add-text-properties beg1 end1 '(face org-block)))
  3778. t))
  3779. ((not (member (char-after beg) '(?\ ?\t)))
  3780. ;; just any other in-buffer setting, but not indented
  3781. (add-text-properties
  3782. beg (match-end 0)
  3783. '(font-lock-fontified t face org-meta-line))
  3784. t)
  3785. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3786. (and (match-end 4) (equal dc3 "attr")))
  3787. (add-text-properties
  3788. beg (match-end 0)
  3789. '(font-lock-fontified t face org-meta-line))
  3790. t)
  3791. (t nil))))))
  3792. (defun org-activate-angle-links (limit)
  3793. "Run through the buffer and add overlays to links."
  3794. (if (re-search-forward org-angle-link-re limit t)
  3795. (progn
  3796. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3797. (add-text-properties (match-beginning 0) (match-end 0)
  3798. (list 'mouse-face 'highlight
  3799. 'keymap org-mouse-map))
  3800. (org-rear-nonsticky-at (match-end 0))
  3801. t)))
  3802. (defun org-activate-footnote-links (limit)
  3803. "Run through the buffer and add overlays to links."
  3804. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3805. limit t)
  3806. (progn
  3807. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3808. (add-text-properties (match-beginning 2) (match-end 2)
  3809. (list 'mouse-face 'highlight
  3810. 'keymap org-mouse-map
  3811. 'help-echo
  3812. (if (= (point-at-bol) (match-beginning 2))
  3813. "Footnote definition"
  3814. "Footnote reference")
  3815. ))
  3816. (org-rear-nonsticky-at (match-end 2))
  3817. t)))
  3818. (defun org-activate-bracket-links (limit)
  3819. "Run through the buffer and add overlays to bracketed links."
  3820. (if (re-search-forward org-bracket-link-regexp limit t)
  3821. (let* ((help (concat "LINK: "
  3822. (org-match-string-no-properties 1)))
  3823. ;; FIXME: above we should remove the escapes.
  3824. ;; but that requires another match, protecting match data,
  3825. ;; a lot of overhead for font-lock.
  3826. (ip (org-maybe-intangible
  3827. (list 'invisible 'org-link
  3828. 'keymap org-mouse-map 'mouse-face 'highlight
  3829. 'font-lock-multiline t 'help-echo help)))
  3830. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3831. 'font-lock-multiline t 'help-echo help)))
  3832. ;; We need to remove the invisible property here. Table narrowing
  3833. ;; may have made some of this invisible.
  3834. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3835. (remove-text-properties (match-beginning 0) (match-end 0)
  3836. '(invisible nil))
  3837. (if (match-end 3)
  3838. (progn
  3839. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3840. (org-rear-nonsticky-at (match-beginning 3))
  3841. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3842. (org-rear-nonsticky-at (match-end 3))
  3843. (add-text-properties (match-end 3) (match-end 0) ip)
  3844. (org-rear-nonsticky-at (match-end 0)))
  3845. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3846. (org-rear-nonsticky-at (match-beginning 1))
  3847. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3848. (org-rear-nonsticky-at (match-end 1))
  3849. (add-text-properties (match-end 1) (match-end 0) ip)
  3850. (org-rear-nonsticky-at (match-end 0)))
  3851. t)))
  3852. (defun org-activate-dates (limit)
  3853. "Run through the buffer and add overlays to dates."
  3854. (if (re-search-forward org-tsr-regexp-both limit t)
  3855. (progn
  3856. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3857. (add-text-properties (match-beginning 0) (match-end 0)
  3858. (list 'mouse-face 'highlight
  3859. 'keymap org-mouse-map))
  3860. (org-rear-nonsticky-at (match-end 0))
  3861. (when org-display-custom-times
  3862. (if (match-end 3)
  3863. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3864. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3865. t)))
  3866. (defvar org-target-link-regexp nil
  3867. "Regular expression matching radio targets in plain text.")
  3868. (make-variable-buffer-local 'org-target-link-regexp)
  3869. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3870. "Regular expression matching a link target.")
  3871. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3872. "Regular expression matching a radio target.")
  3873. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3874. "Regular expression matching any target.")
  3875. (defun org-activate-target-links (limit)
  3876. "Run through the buffer and add overlays to target matches."
  3877. (when org-target-link-regexp
  3878. (let ((case-fold-search t))
  3879. (if (re-search-forward org-target-link-regexp limit t)
  3880. (progn
  3881. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3882. (add-text-properties (match-beginning 0) (match-end 0)
  3883. (list 'mouse-face 'highlight
  3884. 'keymap org-mouse-map
  3885. 'help-echo "Radio target link"
  3886. 'org-linked-text t))
  3887. (org-rear-nonsticky-at (match-end 0))
  3888. t)))))
  3889. (defun org-update-radio-target-regexp ()
  3890. "Find all radio targets in this file and update the regular expression."
  3891. (interactive)
  3892. (when (memq 'radio org-activate-links)
  3893. (setq org-target-link-regexp
  3894. (org-make-target-link-regexp (org-all-targets 'radio)))
  3895. (org-restart-font-lock)))
  3896. (defun org-hide-wide-columns (limit)
  3897. (let (s e)
  3898. (setq s (text-property-any (point) (or limit (point-max))
  3899. 'org-cwidth t))
  3900. (when s
  3901. (setq e (next-single-property-change s 'org-cwidth))
  3902. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3903. (goto-char e)
  3904. t)))
  3905. (defvar org-latex-and-specials-regexp nil
  3906. "Regular expression for highlighting export special stuff.")
  3907. (defvar org-match-substring-regexp)
  3908. (defvar org-match-substring-with-braces-regexp)
  3909. (defvar org-export-html-special-string-regexps)
  3910. (defun org-compute-latex-and-specials-regexp ()
  3911. "Compute regular expression for stuff treated specially by exporters."
  3912. (if (not org-highlight-latex-fragments-and-specials)
  3913. (org-set-local 'org-latex-and-specials-regexp nil)
  3914. (require 'org-exp)
  3915. (let*
  3916. ((matchers (plist-get org-format-latex-options :matchers))
  3917. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3918. org-latex-regexps)))
  3919. (options (org-combine-plists (org-default-export-plist)
  3920. (org-infile-export-plist)))
  3921. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3922. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3923. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3924. (org-export-html-expand (plist-get options :expand-quoted-html))
  3925. (org-export-with-special-strings (plist-get options :special-strings))
  3926. (re-sub
  3927. (cond
  3928. ((equal org-export-with-sub-superscripts '{})
  3929. (list org-match-substring-with-braces-regexp))
  3930. (org-export-with-sub-superscripts
  3931. (list org-match-substring-regexp))
  3932. (t nil)))
  3933. (re-latex
  3934. (if org-export-with-LaTeX-fragments
  3935. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3936. (re-macros
  3937. (if org-export-with-TeX-macros
  3938. (list (concat "\\\\"
  3939. (regexp-opt
  3940. (append (mapcar 'car org-html-entities)
  3941. (if (boundp 'org-latex-entities)
  3942. (mapcar (lambda (x)
  3943. (or (car-safe x) x))
  3944. org-latex-entities)
  3945. nil))
  3946. 'words))) ; FIXME
  3947. ))
  3948. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3949. (re-special (if org-export-with-special-strings
  3950. (mapcar (lambda (x) (car x))
  3951. org-export-html-special-string-regexps)))
  3952. (re-rest
  3953. (delq nil
  3954. (list
  3955. (if org-export-html-expand "@<[^>\n]+>")
  3956. ))))
  3957. (org-set-local
  3958. 'org-latex-and-specials-regexp
  3959. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3960. re-rest) "\\|")))))
  3961. (defun org-do-latex-and-special-faces (limit)
  3962. "Run through the buffer and add overlays to links."
  3963. (when org-latex-and-specials-regexp
  3964. (let (rtn d)
  3965. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3966. limit t))
  3967. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3968. 'face))
  3969. '(org-code org-verbatim underline)))
  3970. (progn
  3971. (setq rtn t
  3972. d (cond ((member (char-after (1+ (match-beginning 0)))
  3973. '(?_ ?^)) 1)
  3974. (t 0)))
  3975. (font-lock-prepend-text-property
  3976. (+ d (match-beginning 0)) (match-end 0)
  3977. 'face 'org-latex-and-export-specials)
  3978. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3979. '(font-lock-multiline t)))))
  3980. rtn)))
  3981. (defun org-restart-font-lock ()
  3982. "Restart font-lock-mode, to force refontification."
  3983. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3984. (font-lock-mode -1)
  3985. (font-lock-mode 1)))
  3986. (defun org-all-targets (&optional radio)
  3987. "Return a list of all targets in this file.
  3988. With optional argument RADIO, only find radio targets."
  3989. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3990. rtn)
  3991. (save-excursion
  3992. (goto-char (point-min))
  3993. (while (re-search-forward re nil t)
  3994. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3995. rtn)))
  3996. (defun org-make-target-link-regexp (targets)
  3997. "Make regular expression matching all strings in TARGETS.
  3998. The regular expression finds the targets also if there is a line break
  3999. between words."
  4000. (and targets
  4001. (concat
  4002. "\\<\\("
  4003. (mapconcat
  4004. (lambda (x)
  4005. (while (string-match " +" x)
  4006. (setq x (replace-match "\\s-+" t t x)))
  4007. x)
  4008. targets
  4009. "\\|")
  4010. "\\)\\>")))
  4011. (defun org-activate-tags (limit)
  4012. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4013. (progn
  4014. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4015. (add-text-properties (match-beginning 1) (match-end 1)
  4016. (list 'mouse-face 'highlight
  4017. 'keymap org-mouse-map))
  4018. (org-rear-nonsticky-at (match-end 1))
  4019. t)))
  4020. (defun org-outline-level ()
  4021. (save-excursion
  4022. (looking-at outline-regexp)
  4023. (if (match-beginning 1)
  4024. (+ (org-get-string-indentation (match-string 1)) 1000)
  4025. (1- (- (match-end 0) (match-beginning 0))))))
  4026. (defvar org-font-lock-keywords nil)
  4027. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4028. "Regular expression matching a property line.")
  4029. (defvar org-font-lock-hook nil
  4030. "Functions to be called for special font lock stuff.")
  4031. (defun org-font-lock-hook (limit)
  4032. (run-hook-with-args 'org-font-lock-hook limit))
  4033. (defun org-set-font-lock-defaults ()
  4034. (let* ((em org-fontify-emphasized-text)
  4035. (lk org-activate-links)
  4036. (org-font-lock-extra-keywords
  4037. (list
  4038. ;; Call the hook
  4039. '(org-font-lock-hook)
  4040. ;; Headlines
  4041. `(,(if org-fontify-whole-heading-line
  4042. "^\\(\\**\\)\\(\\* \\)\\(.*\xa\\)"
  4043. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4044. (1 (org-get-level-face 1))
  4045. (2 (org-get-level-face 2))
  4046. (3 (org-get-level-face 3)))
  4047. ;; Table lines
  4048. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4049. (1 'org-table t))
  4050. ;; Table internals
  4051. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4052. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4053. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4054. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4055. ;; Drawers
  4056. (list org-drawer-regexp '(0 'org-special-keyword t))
  4057. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4058. ;; Properties
  4059. (list org-property-re
  4060. '(1 'org-special-keyword t)
  4061. '(3 'org-property-value t))
  4062. ;; Links
  4063. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4064. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4065. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4066. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4067. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4068. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4069. (if (memq 'footnote lk) '(org-activate-footnote-links
  4070. (2 'org-footnote t)))
  4071. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4072. '(org-hide-wide-columns (0 nil append))
  4073. ;; TODO lines
  4074. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4075. '(1 (org-get-todo-face 1) t))
  4076. ;; DONE
  4077. (if org-fontify-done-headline
  4078. (list (concat "^[*]+ +\\<\\("
  4079. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4080. "\\)\\(.*\\)")
  4081. '(2 'org-headline-done t))
  4082. nil)
  4083. ;; Priorities
  4084. '(org-font-lock-add-priority-faces)
  4085. ;; Tags
  4086. '(org-font-lock-add-tag-faces)
  4087. ;; Special keywords
  4088. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4089. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4090. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4091. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4092. ;; Emphasis
  4093. (if em
  4094. (if (featurep 'xemacs)
  4095. '(org-do-emphasis-faces (0 nil append))
  4096. '(org-do-emphasis-faces)))
  4097. ;; Checkboxes
  4098. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4099. 2 'org-checkbox prepend)
  4100. (if org-provide-checkbox-statistics
  4101. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4102. (0 (org-get-checkbox-statistics-face) t)))
  4103. ;; Description list items
  4104. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4105. 2 'bold prepend)
  4106. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4107. '(1 'org-archived prepend))
  4108. ;; Specials
  4109. '(org-do-latex-and-special-faces)
  4110. ;; Code
  4111. '(org-activate-code (1 'org-code t))
  4112. ;; COMMENT
  4113. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4114. "\\|" org-quote-string "\\)\\>")
  4115. '(1 'org-special-keyword t))
  4116. '("^#.*" (0 'font-lock-comment-face t))
  4117. ;; Blocks and meta lines
  4118. '(org-fontify-meta-lines-and-blocks)
  4119. )))
  4120. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4121. ;; Now set the full font-lock-keywords
  4122. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4123. (org-set-local 'font-lock-defaults
  4124. '(org-font-lock-keywords t nil nil backward-paragraph))
  4125. (kill-local-variable 'font-lock-keywords) nil))
  4126. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4127. "Fontify string S like in Org-mode"
  4128. (with-temp-buffer
  4129. (insert s)
  4130. (let ((org-odd-levels-only odd-levels))
  4131. (org-mode)
  4132. (font-lock-fontify-buffer)
  4133. (buffer-string))))
  4134. (defvar org-m nil)
  4135. (defvar org-l nil)
  4136. (defvar org-f nil)
  4137. (defun org-get-level-face (n)
  4138. "Get the right face for match N in font-lock matching of headlines."
  4139. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4140. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4141. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4142. (cond
  4143. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4144. ((eq n 2) org-f)
  4145. (t (if org-level-color-stars-only nil org-f))))
  4146. (defun org-get-todo-face (kwd)
  4147. "Get the right face for a TODO keyword KWD.
  4148. If KWD is a number, get the corresponding match group."
  4149. (if (numberp kwd) (setq kwd (match-string kwd)))
  4150. (or (cdr (assoc kwd org-todo-keyword-faces))
  4151. (and (member kwd org-done-keywords) 'org-done)
  4152. 'org-todo))
  4153. (defun org-font-lock-add-tag-faces (limit)
  4154. "Add the special tag faces."
  4155. (when (and org-tag-faces org-tags-special-faces-re)
  4156. (while (re-search-forward org-tags-special-faces-re limit t)
  4157. (add-text-properties (match-beginning 1) (match-end 1)
  4158. (list 'face (org-get-tag-face 1)
  4159. 'font-lock-fontified t))
  4160. (backward-char 1))))
  4161. (defun org-font-lock-add-priority-faces (limit)
  4162. "Add the special priority faces."
  4163. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4164. (add-text-properties
  4165. (match-beginning 0) (match-end 0)
  4166. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4167. org-priority-faces))
  4168. 'org-special-keyword)
  4169. 'font-lock-fontified t))))
  4170. (defun org-get-tag-face (kwd)
  4171. "Get the right face for a TODO keyword KWD.
  4172. If KWD is a number, get the corresponding match group."
  4173. (if (numberp kwd) (setq kwd (match-string kwd)))
  4174. (or (cdr (assoc kwd org-tag-faces))
  4175. 'org-tag))
  4176. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4177. "Remove fontification and activation overlays from links."
  4178. (font-lock-default-unfontify-region beg end)
  4179. (let* ((buffer-undo-list t)
  4180. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4181. (inhibit-modification-hooks t)
  4182. deactivate-mark buffer-file-name buffer-file-truename)
  4183. (remove-text-properties beg end
  4184. '(mouse-face t keymap t org-linked-text t
  4185. invisible t intangible t
  4186. org-no-flyspell t))))
  4187. ;;;; Visibility cycling, including org-goto and indirect buffer
  4188. ;;; Cycling
  4189. (defvar org-cycle-global-status nil)
  4190. (make-variable-buffer-local 'org-cycle-global-status)
  4191. (defvar org-cycle-subtree-status nil)
  4192. (make-variable-buffer-local 'org-cycle-subtree-status)
  4193. ;;;###autoload
  4194. (defvar org-inlinetask-min-level)
  4195. (defun org-cycle (&optional arg)
  4196. "TAB-action and visibility cycling for Org-mode.
  4197. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4198. is outine visibility cycling, but it also invokes other actions
  4199. in special contexts.
  4200. - When this function is called with a prefix argument, rotate the entire
  4201. buffer through 3 states (global cycling)
  4202. 1. OVERVIEW: Show only top-level headlines.
  4203. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4204. 3. SHOW ALL: Show everything.
  4205. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4206. determined by the variable `org-startup-folded', and by any VISIBILITY
  4207. properties in the buffer.
  4208. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4209. including any drawers.
  4210. - When inside a table, re-align the table and move to the next field.
  4211. - When point is at the beginning of a headline, rotate the subtree started
  4212. by this line through 3 different states (local cycling)
  4213. 1. FOLDED: Only the main headline is shown.
  4214. 2. CHILDREN: The main headline and the direct children are shown.
  4215. From this state, you can move to one of the children
  4216. and zoom in further.
  4217. 3. SUBTREE: Show the entire subtree, including body text.
  4218. - When there is a numeric prefix, go up to a heading with level ARG, do
  4219. a `show-subtree' and return to the previous cursor position. If ARG
  4220. is negative, go up that many levels.
  4221. - When point is not at the beginning of a headline, execute the global
  4222. binding for TAB, which is re-indenting the line. See the option
  4223. `org-cycle-emulate-tab' for details.
  4224. - Special case: if point is at the beginning of the buffer and there is
  4225. no headline in line 1, this function will act as if called with prefix arg.
  4226. But only if also the variable `org-cycle-global-at-bob' is t."
  4227. (interactive "P")
  4228. (org-load-modules-maybe)
  4229. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4230. (let* ((limit-level
  4231. (or org-cycle-max-level
  4232. (and (boundp 'org-inlinetask-min-level)
  4233. org-inlinetask-min-level
  4234. (1- org-inlinetask-min-level))))
  4235. (nstars (and limit-level
  4236. (if org-odd-levels-only
  4237. (and limit-level (1- (* limit-level 2)))
  4238. limit-level)))
  4239. (outline-regexp
  4240. (cond
  4241. ((not (org-mode-p)) outline-regexp)
  4242. ((or (eq org-cycle-include-plain-lists 'integrate)
  4243. (and org-cycle-include-plain-lists (org-at-item-p)))
  4244. (concat "\\(?:\\*"
  4245. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4246. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4247. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4248. (bob-special (and org-cycle-global-at-bob (bobp)
  4249. (not (looking-at outline-regexp))))
  4250. (org-cycle-hook
  4251. (if bob-special
  4252. (delq 'org-optimize-window-after-visibility-change
  4253. (copy-sequence org-cycle-hook))
  4254. org-cycle-hook))
  4255. (pos (point)))
  4256. (if (or bob-special (equal arg '(4)))
  4257. ;; special case: use global cycling
  4258. (setq arg t))
  4259. (cond
  4260. ((equal arg '(16))
  4261. (org-set-startup-visibility)
  4262. (message "Startup visibility, plus VISIBILITY properties"))
  4263. ((equal arg '(64))
  4264. (show-all)
  4265. (message "Entire buffer visible, including drawers"))
  4266. ((org-at-table-p 'any)
  4267. ;; Enter the table or move to the next field in the table
  4268. (or (org-table-recognize-table.el)
  4269. (progn
  4270. (if arg (org-table-edit-field t)
  4271. (org-table-justify-field-maybe)
  4272. (call-interactively 'org-table-next-field)))))
  4273. ((run-hook-with-args-until-success
  4274. 'org-tab-after-check-for-table-hook))
  4275. ((eq arg t) ;; Global cycling
  4276. (org-cycle-internal-global))
  4277. ((and org-drawers org-drawer-regexp
  4278. (save-excursion
  4279. (beginning-of-line 1)
  4280. (looking-at org-drawer-regexp)))
  4281. ;; Toggle block visibility
  4282. (org-flag-drawer
  4283. (not (get-char-property (match-end 0) 'invisible))))
  4284. ((integerp arg)
  4285. ;; Show-subtree, ARG levels up from here.
  4286. (save-excursion
  4287. (org-back-to-heading)
  4288. (outline-up-heading (if (< arg 0) (- arg)
  4289. (- (funcall outline-level) arg)))
  4290. (org-show-subtree)))
  4291. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4292. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4293. (org-cycle-internal-local))
  4294. ;; TAB emulation and template completion
  4295. (buffer-read-only (org-back-to-heading))
  4296. ((run-hook-with-args-until-success
  4297. 'org-tab-after-check-for-cycling-hook))
  4298. ((org-try-structure-completion))
  4299. ((org-try-cdlatex-tab))
  4300. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4301. (or (not (bolp))
  4302. (not (looking-at outline-regexp))))
  4303. (call-interactively (global-key-binding "\t")))
  4304. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4305. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4306. (or (and (eq org-cycle-emulate-tab 'white)
  4307. (= (match-end 0) (point-at-eol)))
  4308. (and (eq org-cycle-emulate-tab 'whitestart)
  4309. (>= (match-end 0) pos))))
  4310. t
  4311. (eq org-cycle-emulate-tab t))
  4312. (call-interactively (global-key-binding "\t")))
  4313. (t (save-excursion
  4314. (org-back-to-heading)
  4315. (org-cycle)))))))
  4316. (defun org-cycle-internal-global ()
  4317. "Do the global cycling action."
  4318. (cond
  4319. ((and (eq last-command this-command)
  4320. (eq org-cycle-global-status 'overview))
  4321. ;; We just created the overview - now do table of contents
  4322. ;; This can be slow in very large buffers, so indicate action
  4323. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4324. (message "CONTENTS...")
  4325. (org-content)
  4326. (message "CONTENTS...done")
  4327. (setq org-cycle-global-status 'contents)
  4328. (run-hook-with-args 'org-cycle-hook 'contents))
  4329. ((and (eq last-command this-command)
  4330. (eq org-cycle-global-status 'contents))
  4331. ;; We just showed the table of contents - now show everything
  4332. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4333. (show-all)
  4334. (message "SHOW ALL")
  4335. (setq org-cycle-global-status 'all)
  4336. (run-hook-with-args 'org-cycle-hook 'all))
  4337. (t
  4338. ;; Default action: go to overview
  4339. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4340. (org-overview)
  4341. (message "OVERVIEW")
  4342. (setq org-cycle-global-status 'overview)
  4343. (run-hook-with-args 'org-cycle-hook 'overview))))
  4344. (defun org-cycle-internal-local ()
  4345. "Do the local cycling action."
  4346. (org-back-to-heading)
  4347. (let ((goal-column 0) eoh eol eos)
  4348. ;; First, some boundaries
  4349. (save-excursion
  4350. (org-back-to-heading)
  4351. (save-excursion
  4352. (beginning-of-line 2)
  4353. (while (and (not (eobp)) ;; this is like `next-line'
  4354. (get-char-property (1- (point)) 'invisible))
  4355. (beginning-of-line 2)) (setq eol (point)))
  4356. (outline-end-of-heading) (setq eoh (point))
  4357. (org-end-of-subtree t)
  4358. (unless (eobp)
  4359. (skip-chars-forward " \t\n")
  4360. (beginning-of-line 1) ; in case this is an item
  4361. )
  4362. (setq eos (1- (point))))
  4363. ;; Find out what to do next and set `this-command'
  4364. (cond
  4365. ((= eos eoh)
  4366. ;; Nothing is hidden behind this heading
  4367. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4368. (message "EMPTY ENTRY")
  4369. (setq org-cycle-subtree-status nil)
  4370. (save-excursion
  4371. (goto-char eos)
  4372. (outline-next-heading)
  4373. (if (org-invisible-p) (org-flag-heading nil))))
  4374. ((or (>= eol eos)
  4375. (not (string-match "\\S-" (buffer-substring eol eos))))
  4376. ;; Entire subtree is hidden in one line: open it
  4377. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4378. (org-show-entry)
  4379. (show-children)
  4380. (message "CHILDREN")
  4381. (save-excursion
  4382. (goto-char eos)
  4383. (outline-next-heading)
  4384. (if (org-invisible-p) (org-flag-heading nil)))
  4385. (setq org-cycle-subtree-status 'children)
  4386. (run-hook-with-args 'org-cycle-hook 'children))
  4387. ((and (eq last-command this-command)
  4388. (eq org-cycle-subtree-status 'children))
  4389. ;; We just showed the children, now show everything.
  4390. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4391. (org-show-subtree)
  4392. (message "SUBTREE")
  4393. (setq org-cycle-subtree-status 'subtree)
  4394. (run-hook-with-args 'org-cycle-hook 'subtree))
  4395. (t
  4396. ;; Default action: hide the subtree.
  4397. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4398. (hide-subtree)
  4399. (message "FOLDED")
  4400. (setq org-cycle-subtree-status 'folded)
  4401. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4402. ;;;###autoload
  4403. (defun org-global-cycle (&optional arg)
  4404. "Cycle the global visibility. For details see `org-cycle'.
  4405. With C-u prefix arg, switch to startup visibility.
  4406. With a numeric prefix, show all headlines up to that level."
  4407. (interactive "P")
  4408. (let ((org-cycle-include-plain-lists
  4409. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4410. (cond
  4411. ((integerp arg)
  4412. (show-all)
  4413. (hide-sublevels arg)
  4414. (setq org-cycle-global-status 'contents))
  4415. ((equal arg '(4))
  4416. (org-set-startup-visibility)
  4417. (message "Startup visibility, plus VISIBILITY properties."))
  4418. (t
  4419. (org-cycle '(4))))))
  4420. (defun org-set-startup-visibility ()
  4421. "Set the visibility required by startup options and properties."
  4422. (cond
  4423. ((eq org-startup-folded t)
  4424. (org-cycle '(4)))
  4425. ((eq org-startup-folded 'content)
  4426. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4427. (org-cycle '(4)) (org-cycle '(4)))))
  4428. (if org-hide-block-startup (org-hide-block-all))
  4429. (org-set-visibility-according-to-property 'no-cleanup)
  4430. (org-cycle-hide-archived-subtrees 'all)
  4431. (org-cycle-hide-drawers 'all)
  4432. (org-cycle-show-empty-lines 'all))
  4433. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4434. "Switch subtree visibilities according to :VISIBILITY: property."
  4435. (interactive)
  4436. (let (org-show-entry-below state)
  4437. (save-excursion
  4438. (goto-char (point-min))
  4439. (while (re-search-forward
  4440. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4441. nil t)
  4442. (setq state (match-string 1))
  4443. (save-excursion
  4444. (org-back-to-heading t)
  4445. (hide-subtree)
  4446. (org-reveal)
  4447. (cond
  4448. ((equal state '("fold" "folded"))
  4449. (hide-subtree))
  4450. ((equal state "children")
  4451. (org-show-hidden-entry)
  4452. (show-children))
  4453. ((equal state "content")
  4454. (save-excursion
  4455. (save-restriction
  4456. (org-narrow-to-subtree)
  4457. (org-content))))
  4458. ((member state '("all" "showall"))
  4459. (show-subtree)))))
  4460. (unless no-cleanup
  4461. (org-cycle-hide-archived-subtrees 'all)
  4462. (org-cycle-hide-drawers 'all)
  4463. (org-cycle-show-empty-lines 'all)))))
  4464. (defun org-overview ()
  4465. "Switch to overview mode, showing only top-level headlines.
  4466. Really, this shows all headlines with level equal or greater than the level
  4467. of the first headline in the buffer. This is important, because if the
  4468. first headline is not level one, then (hide-sublevels 1) gives confusing
  4469. results."
  4470. (interactive)
  4471. (let ((level (save-excursion
  4472. (goto-char (point-min))
  4473. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4474. (progn
  4475. (goto-char (match-beginning 0))
  4476. (funcall outline-level))))))
  4477. (and level (hide-sublevels level))))
  4478. (defun org-content (&optional arg)
  4479. "Show all headlines in the buffer, like a table of contents.
  4480. With numerical argument N, show content up to level N."
  4481. (interactive "P")
  4482. (save-excursion
  4483. ;; Visit all headings and show their offspring
  4484. (and (integerp arg) (org-overview))
  4485. (goto-char (point-max))
  4486. (catch 'exit
  4487. (while (and (progn (condition-case nil
  4488. (outline-previous-visible-heading 1)
  4489. (error (goto-char (point-min))))
  4490. t)
  4491. (looking-at outline-regexp))
  4492. (if (integerp arg)
  4493. (show-children (1- arg))
  4494. (show-branches))
  4495. (if (bobp) (throw 'exit nil))))))
  4496. (defun org-optimize-window-after-visibility-change (state)
  4497. "Adjust the window after a change in outline visibility.
  4498. This function is the default value of the hook `org-cycle-hook'."
  4499. (when (get-buffer-window (current-buffer))
  4500. (cond
  4501. ((eq state 'content) nil)
  4502. ((eq state 'all) nil)
  4503. ((eq state 'folded) nil)
  4504. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4505. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4506. (defun org-compact-display-after-subtree-move ()
  4507. "Show a compacter version of the tree of the entry's parent."
  4508. (save-excursion
  4509. (if (org-up-heading-safe)
  4510. (progn
  4511. (hide-subtree)
  4512. (show-entry)
  4513. (show-children)
  4514. (org-cycle-show-empty-lines 'children)
  4515. (org-cycle-hide-drawers 'children))
  4516. (org-overview))))
  4517. (defun org-cycle-show-empty-lines (state)
  4518. "Show empty lines above all visible headlines.
  4519. The region to be covered depends on STATE when called through
  4520. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4521. entire buffer covered. Note that an empty line is only shown if there
  4522. are at least `org-cycle-separator-lines' empty lines before the headline."
  4523. (when (> org-cycle-separator-lines 0)
  4524. (save-excursion
  4525. (let* ((n org-cycle-separator-lines)
  4526. (re (cond
  4527. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4528. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4529. (t (let ((ns (number-to-string (- n 2))))
  4530. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4531. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4532. beg end)
  4533. (cond
  4534. ((memq state '(overview contents t))
  4535. (setq beg (point-min) end (point-max)))
  4536. ((memq state '(children folded))
  4537. (setq beg (point) end (progn (org-end-of-subtree t t)
  4538. (beginning-of-line 2)
  4539. (point)))))
  4540. (when beg
  4541. (goto-char beg)
  4542. (while (re-search-forward re end t)
  4543. (if (not (get-char-property (match-end 1) 'invisible))
  4544. (outline-flag-region
  4545. (match-beginning 1) (match-end 1) nil)))))))
  4546. ;; Never hide empty lines at the end of the file.
  4547. (save-excursion
  4548. (goto-char (point-max))
  4549. (outline-previous-heading)
  4550. (outline-end-of-heading)
  4551. (if (and (looking-at "[ \t\n]+")
  4552. (= (match-end 0) (point-max)))
  4553. (outline-flag-region (point) (match-end 0) nil))))
  4554. (defun org-show-empty-lines-in-parent ()
  4555. "Move to the parent and re-show empty lines before visible headlines."
  4556. (save-excursion
  4557. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4558. (org-cycle-show-empty-lines context))))
  4559. (defun org-cycle-hide-drawers (state)
  4560. "Re-hide all drawers after a visibility state change."
  4561. (when (and (org-mode-p)
  4562. (not (memq state '(overview folded))))
  4563. (save-excursion
  4564. (let* ((globalp (memq state '(contents all)))
  4565. (beg (if globalp (point-min) (point)))
  4566. (end (if globalp (point-max) (org-end-of-subtree t))))
  4567. (goto-char beg)
  4568. (while (re-search-forward org-drawer-regexp end t)
  4569. (org-flag-drawer t))))))
  4570. (defun org-flag-drawer (flag)
  4571. (save-excursion
  4572. (beginning-of-line 1)
  4573. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4574. (let ((b (match-end 0))
  4575. (outline-regexp org-outline-regexp))
  4576. (if (re-search-forward
  4577. "^[ \t]*:END:"
  4578. (save-excursion (outline-next-heading) (point)) t)
  4579. (outline-flag-region b (point-at-eol) flag)
  4580. (error ":END: line missing"))))))
  4581. (defun org-subtree-end-visible-p ()
  4582. "Is the end of the current subtree visible?"
  4583. (pos-visible-in-window-p
  4584. (save-excursion (org-end-of-subtree t) (point))))
  4585. (defun org-first-headline-recenter (&optional N)
  4586. "Move cursor to the first headline and recenter the headline.
  4587. Optional argument N means, put the headline into the Nth line of the window."
  4588. (goto-char (point-min))
  4589. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4590. (beginning-of-line)
  4591. (recenter (prefix-numeric-value N))))
  4592. ;;; Folding of blocks
  4593. (defconst org-block-regexp
  4594. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4595. "Regular expression for hiding blocks.")
  4596. (defvar org-hide-block-overlays nil
  4597. "Overays hiding blocks.")
  4598. (make-variable-buffer-local 'org-hide-block-overlays)
  4599. (defun org-block-map (function &optional start end)
  4600. "Call func at the head of all source blocks in the current
  4601. buffer. Optional arguments START and END can be used to limit
  4602. the range."
  4603. (let ((start (or start (point-min)))
  4604. (end (or end (point-max))))
  4605. (save-excursion
  4606. (goto-char start)
  4607. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4608. (save-excursion
  4609. (save-match-data
  4610. (goto-char (match-beginning 0))
  4611. (funcall function)))))))
  4612. (defun org-hide-block-toggle-all ()
  4613. "Toggle the visibility of all blocks in the current buffer."
  4614. (org-block-map #'org-hide-block-toggle))
  4615. (defun org-hide-block-all ()
  4616. "Fold all blocks in the current buffer."
  4617. (interactive)
  4618. (org-show-block-all)
  4619. (org-block-map #'org-hide-block-toggle-maybe))
  4620. (defun org-show-block-all ()
  4621. "Unfold all blocks in the current buffer."
  4622. (mapc 'org-delete-overlay org-hide-block-overlays)
  4623. (setq org-hide-block-overlays nil))
  4624. (defun org-hide-block-toggle-maybe ()
  4625. "Toggle visibility of block at point."
  4626. (interactive)
  4627. (let ((case-fold-search t))
  4628. (if (save-excursion
  4629. (beginning-of-line 1)
  4630. (looking-at org-block-regexp))
  4631. (progn (org-hide-block-toggle)
  4632. t) ;; to signal that we took action
  4633. nil))) ;; to signal that we did not
  4634. (defun org-hide-block-toggle (&optional force)
  4635. "Toggle the visibility of the current block."
  4636. (interactive)
  4637. (save-excursion
  4638. (beginning-of-line)
  4639. (if (re-search-forward org-block-regexp nil t)
  4640. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4641. (end (match-end 0))
  4642. ov) ;; end of entire body
  4643. (if (memq t (mapcar (lambda (overlay)
  4644. (eq (org-overlay-get overlay 'invisible)
  4645. 'org-hide-block))
  4646. (org-overlays-at start)))
  4647. (if (or (not force) (eq force 'off))
  4648. (mapc (lambda (ov)
  4649. (when (member ov org-hide-block-overlays)
  4650. (setq org-hide-block-overlays
  4651. (delq ov org-hide-block-overlays)))
  4652. (when (eq (org-overlay-get ov 'invisible)
  4653. 'org-hide-block)
  4654. (org-delete-overlay ov)))
  4655. (org-overlays-at start)))
  4656. (setq ov (org-make-overlay start end))
  4657. (org-overlay-put ov 'invisible 'org-hide-block)
  4658. (push ov org-hide-block-overlays)))
  4659. (error "Not looking at a source block"))))
  4660. ;; org-tab-after-check-for-cycling-hook
  4661. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4662. ;; Remove overlays when changing major mode
  4663. (add-hook 'org-mode-hook
  4664. (lambda () (org-add-hook 'change-major-mode-hook
  4665. 'org-show-block-all 'append 'local)))
  4666. ;;; Org-goto
  4667. (defvar org-goto-window-configuration nil)
  4668. (defvar org-goto-marker nil)
  4669. (defvar org-goto-map
  4670. (let ((map (make-sparse-keymap)))
  4671. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4672. (while (setq cmd (pop cmds))
  4673. (substitute-key-definition cmd cmd map global-map)))
  4674. (suppress-keymap map)
  4675. (org-defkey map "\C-m" 'org-goto-ret)
  4676. (org-defkey map [(return)] 'org-goto-ret)
  4677. (org-defkey map [(left)] 'org-goto-left)
  4678. (org-defkey map [(right)] 'org-goto-right)
  4679. (org-defkey map [(control ?g)] 'org-goto-quit)
  4680. (org-defkey map "\C-i" 'org-cycle)
  4681. (org-defkey map [(tab)] 'org-cycle)
  4682. (org-defkey map [(down)] 'outline-next-visible-heading)
  4683. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4684. (if org-goto-auto-isearch
  4685. (if (fboundp 'define-key-after)
  4686. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4687. nil)
  4688. (org-defkey map "q" 'org-goto-quit)
  4689. (org-defkey map "n" 'outline-next-visible-heading)
  4690. (org-defkey map "p" 'outline-previous-visible-heading)
  4691. (org-defkey map "f" 'outline-forward-same-level)
  4692. (org-defkey map "b" 'outline-backward-same-level)
  4693. (org-defkey map "u" 'outline-up-heading))
  4694. (org-defkey map "/" 'org-occur)
  4695. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4696. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4697. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4698. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4699. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4700. map))
  4701. (defconst org-goto-help
  4702. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4703. RET=jump to location [Q]uit and return to previous location
  4704. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4705. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4706. ;; FIXME: Docstring doe not mention both interfaces
  4707. (defun org-goto (&optional alternative-interface)
  4708. "Look up a different location in the current file, keeping current visibility.
  4709. When you want look-up or go to a different location in a document, the
  4710. fastest way is often to fold the entire buffer and then dive into the tree.
  4711. This method has the disadvantage, that the previous location will be folded,
  4712. which may not be what you want.
  4713. This command works around this by showing a copy of the current buffer
  4714. in an indirect buffer, in overview mode. You can dive into the tree in
  4715. that copy, use org-occur and incremental search to find a location.
  4716. When pressing RET or `Q', the command returns to the original buffer in
  4717. which the visibility is still unchanged. After RET is will also jump to
  4718. the location selected in the indirect buffer and expose the
  4719. the headline hierarchy above."
  4720. (interactive "P")
  4721. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4722. (org-refile-use-outline-path t)
  4723. (org-refile-target-verify-function nil)
  4724. (interface
  4725. (if (not alternative-interface)
  4726. org-goto-interface
  4727. (if (eq org-goto-interface 'outline)
  4728. 'outline-path-completion
  4729. 'outline)))
  4730. (org-goto-start-pos (point))
  4731. (selected-point
  4732. (if (eq interface 'outline)
  4733. (car (org-get-location (current-buffer) org-goto-help))
  4734. (nth 3 (org-refile-get-location "Goto: ")))))
  4735. (if selected-point
  4736. (progn
  4737. (org-mark-ring-push org-goto-start-pos)
  4738. (goto-char selected-point)
  4739. (if (or (org-invisible-p) (org-invisible-p2))
  4740. (org-show-context 'org-goto)))
  4741. (message "Quit"))))
  4742. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4743. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4744. (defvar org-goto-local-auto-isearch-map) ; defined below
  4745. (defun org-get-location (buf help)
  4746. "Let the user select a location in the Org-mode buffer BUF.
  4747. This function uses a recursive edit. It returns the selected position
  4748. or nil."
  4749. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4750. (isearch-hide-immediately nil)
  4751. (isearch-search-fun-function
  4752. (lambda () 'org-goto-local-search-headings))
  4753. (org-goto-selected-point org-goto-exit-command))
  4754. (save-excursion
  4755. (save-window-excursion
  4756. (delete-other-windows)
  4757. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4758. (switch-to-buffer
  4759. (condition-case nil
  4760. (make-indirect-buffer (current-buffer) "*org-goto*")
  4761. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4762. (with-output-to-temp-buffer "*Help*"
  4763. (princ help))
  4764. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4765. (setq buffer-read-only nil)
  4766. (let ((org-startup-truncated t)
  4767. (org-startup-folded nil)
  4768. (org-startup-align-all-tables nil))
  4769. (org-mode)
  4770. (org-overview))
  4771. (setq buffer-read-only t)
  4772. (if (and (boundp 'org-goto-start-pos)
  4773. (integer-or-marker-p org-goto-start-pos))
  4774. (let ((org-show-hierarchy-above t)
  4775. (org-show-siblings t)
  4776. (org-show-following-heading t))
  4777. (goto-char org-goto-start-pos)
  4778. (and (org-invisible-p) (org-show-context)))
  4779. (goto-char (point-min)))
  4780. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4781. (message "Select location and press RET")
  4782. (use-local-map org-goto-map)
  4783. (recursive-edit)
  4784. ))
  4785. (kill-buffer "*org-goto*")
  4786. (cons org-goto-selected-point org-goto-exit-command)))
  4787. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4788. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4789. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4790. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4791. (defun org-goto-local-search-headings (string bound noerror)
  4792. "Search and make sure that any matches are in headlines."
  4793. (catch 'return
  4794. (while (if isearch-forward
  4795. (search-forward string bound noerror)
  4796. (search-backward string bound noerror))
  4797. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4798. (and (member :headline context)
  4799. (not (member :tags context))))
  4800. (throw 'return (point))))))
  4801. (defun org-goto-local-auto-isearch ()
  4802. "Start isearch."
  4803. (interactive)
  4804. (goto-char (point-min))
  4805. (let ((keys (this-command-keys)))
  4806. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4807. (isearch-mode t)
  4808. (isearch-process-search-char (string-to-char keys)))))
  4809. (defun org-goto-ret (&optional arg)
  4810. "Finish `org-goto' by going to the new location."
  4811. (interactive "P")
  4812. (setq org-goto-selected-point (point)
  4813. org-goto-exit-command 'return)
  4814. (throw 'exit nil))
  4815. (defun org-goto-left ()
  4816. "Finish `org-goto' by going to the new location."
  4817. (interactive)
  4818. (if (org-on-heading-p)
  4819. (progn
  4820. (beginning-of-line 1)
  4821. (setq org-goto-selected-point (point)
  4822. org-goto-exit-command 'left)
  4823. (throw 'exit nil))
  4824. (error "Not on a heading")))
  4825. (defun org-goto-right ()
  4826. "Finish `org-goto' by going to the new location."
  4827. (interactive)
  4828. (if (org-on-heading-p)
  4829. (progn
  4830. (setq org-goto-selected-point (point)
  4831. org-goto-exit-command 'right)
  4832. (throw 'exit nil))
  4833. (error "Not on a heading")))
  4834. (defun org-goto-quit ()
  4835. "Finish `org-goto' without cursor motion."
  4836. (interactive)
  4837. (setq org-goto-selected-point nil)
  4838. (setq org-goto-exit-command 'quit)
  4839. (throw 'exit nil))
  4840. ;;; Indirect buffer display of subtrees
  4841. (defvar org-indirect-dedicated-frame nil
  4842. "This is the frame being used for indirect tree display.")
  4843. (defvar org-last-indirect-buffer nil)
  4844. (defun org-tree-to-indirect-buffer (&optional arg)
  4845. "Create indirect buffer and narrow it to current subtree.
  4846. With numerical prefix ARG, go up to this level and then take that tree.
  4847. If ARG is negative, go up that many levels.
  4848. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4849. indirect buffer previously made with this command, to avoid proliferation of
  4850. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4851. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4852. is kept so that you can work with several indirect buffers at the same time.
  4853. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4854. requests that a new frame be made for the new buffer, so that the dedicated
  4855. frame is not changed."
  4856. (interactive "P")
  4857. (let ((cbuf (current-buffer))
  4858. (cwin (selected-window))
  4859. (pos (point))
  4860. beg end level heading ibuf)
  4861. (save-excursion
  4862. (org-back-to-heading t)
  4863. (when (numberp arg)
  4864. (setq level (org-outline-level))
  4865. (if (< arg 0) (setq arg (+ level arg)))
  4866. (while (> (setq level (org-outline-level)) arg)
  4867. (outline-up-heading 1 t)))
  4868. (setq beg (point)
  4869. heading (org-get-heading))
  4870. (org-end-of-subtree t) (setq end (point)))
  4871. (if (and (buffer-live-p org-last-indirect-buffer)
  4872. (not (eq org-indirect-buffer-display 'new-frame))
  4873. (not arg))
  4874. (kill-buffer org-last-indirect-buffer))
  4875. (setq ibuf (org-get-indirect-buffer cbuf)
  4876. org-last-indirect-buffer ibuf)
  4877. (cond
  4878. ((or (eq org-indirect-buffer-display 'new-frame)
  4879. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4880. (select-frame (make-frame))
  4881. (delete-other-windows)
  4882. (switch-to-buffer ibuf)
  4883. (org-set-frame-title heading))
  4884. ((eq org-indirect-buffer-display 'dedicated-frame)
  4885. (raise-frame
  4886. (select-frame (or (and org-indirect-dedicated-frame
  4887. (frame-live-p org-indirect-dedicated-frame)
  4888. org-indirect-dedicated-frame)
  4889. (setq org-indirect-dedicated-frame (make-frame)))))
  4890. (delete-other-windows)
  4891. (switch-to-buffer ibuf)
  4892. (org-set-frame-title (concat "Indirect: " heading)))
  4893. ((eq org-indirect-buffer-display 'current-window)
  4894. (switch-to-buffer ibuf))
  4895. ((eq org-indirect-buffer-display 'other-window)
  4896. (pop-to-buffer ibuf))
  4897. (t (error "Invalid value.")))
  4898. (if (featurep 'xemacs)
  4899. (save-excursion (org-mode) (turn-on-font-lock)))
  4900. (narrow-to-region beg end)
  4901. (show-all)
  4902. (goto-char pos)
  4903. (and (window-live-p cwin) (select-window cwin))))
  4904. (defun org-get-indirect-buffer (&optional buffer)
  4905. (setq buffer (or buffer (current-buffer)))
  4906. (let ((n 1) (base (buffer-name buffer)) bname)
  4907. (while (buffer-live-p
  4908. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4909. (setq n (1+ n)))
  4910. (condition-case nil
  4911. (make-indirect-buffer buffer bname 'clone)
  4912. (error (make-indirect-buffer buffer bname)))))
  4913. (defun org-set-frame-title (title)
  4914. "Set the title of the current frame to the string TITLE."
  4915. ;; FIXME: how to name a single frame in XEmacs???
  4916. (unless (featurep 'xemacs)
  4917. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4918. ;;;; Structure editing
  4919. ;;; Inserting headlines
  4920. (defun org-previous-line-empty-p ()
  4921. (save-excursion
  4922. (and (not (bobp))
  4923. (or (beginning-of-line 0) t)
  4924. (save-match-data
  4925. (looking-at "[ \t]*$")))))
  4926. (defun org-insert-heading (&optional force-heading)
  4927. "Insert a new heading or item with same depth at point.
  4928. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4929. If point is at the beginning of a headline, insert a sibling before the
  4930. current headline. If point is not at the beginning, do not split the line,
  4931. but create the new headline after the current line."
  4932. (interactive "P")
  4933. (if (= (buffer-size) 0)
  4934. (insert "\n* ")
  4935. (when (or force-heading (not (org-insert-item)))
  4936. (let* ((empty-line-p nil)
  4937. (head (save-excursion
  4938. (condition-case nil
  4939. (progn
  4940. (org-back-to-heading)
  4941. (setq empty-line-p (org-previous-line-empty-p))
  4942. (match-string 0))
  4943. (error "*"))))
  4944. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4945. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4946. pos hide-previous previous-pos)
  4947. (cond
  4948. ((and (org-on-heading-p) (bolp)
  4949. (or (bobp)
  4950. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4951. ;; insert before the current line
  4952. (open-line (if blank 2 1)))
  4953. ((and (bolp)
  4954. (or (bobp)
  4955. (save-excursion
  4956. (backward-char 1) (not (org-invisible-p)))))
  4957. ;; insert right here
  4958. nil)
  4959. (t
  4960. ;; somewhere in the line
  4961. (save-excursion
  4962. (setq previous-pos (point-at-bol))
  4963. (end-of-line)
  4964. (setq hide-previous (org-invisible-p)))
  4965. (and org-insert-heading-respect-content (org-show-subtree))
  4966. (let ((split
  4967. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4968. (save-excursion
  4969. (let ((p (point)))
  4970. (goto-char (point-at-bol))
  4971. (and (looking-at org-complex-heading-regexp)
  4972. (> p (match-beginning 4)))))))
  4973. tags pos)
  4974. (cond
  4975. (org-insert-heading-respect-content
  4976. (org-end-of-subtree nil t)
  4977. (or (bolp) (newline))
  4978. (or (org-previous-line-empty-p)
  4979. (and blank (newline)))
  4980. (open-line 1))
  4981. ((org-on-heading-p)
  4982. (when hide-previous
  4983. (show-children)
  4984. (org-show-entry))
  4985. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4986. (setq tags (and (match-end 2) (match-string 2)))
  4987. (and (match-end 1)
  4988. (delete-region (match-beginning 1) (match-end 1)))
  4989. (setq pos (point-at-bol))
  4990. (or split (end-of-line 1))
  4991. (delete-horizontal-space)
  4992. (newline (if blank 2 1))
  4993. (when tags
  4994. (save-excursion
  4995. (goto-char pos)
  4996. (end-of-line 1)
  4997. (insert " " tags)
  4998. (org-set-tags nil 'align))))
  4999. (t
  5000. (or split (end-of-line 1))
  5001. (newline (if blank 2 1)))))))
  5002. (insert head) (just-one-space)
  5003. (setq pos (point))
  5004. (end-of-line 1)
  5005. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5006. (when (and org-insert-heading-respect-content hide-previous)
  5007. (save-excursion
  5008. (goto-char previous-pos)
  5009. (hide-subtree)))
  5010. (run-hooks 'org-insert-heading-hook)))))
  5011. (defun org-get-heading (&optional no-tags)
  5012. "Return the heading of the current entry, without the stars."
  5013. (save-excursion
  5014. (org-back-to-heading t)
  5015. (if (looking-at
  5016. (if no-tags
  5017. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5018. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5019. (match-string 1) "")))
  5020. (defun org-heading-components ()
  5021. "Return the components of the current heading.
  5022. This is a list with the following elements:
  5023. - the level as an integer
  5024. - the reduced level, different if `org-odd-levels-only' is set.
  5025. - the TODO keyword, or nil
  5026. - the priority character, like ?A, or nil if no priority is given
  5027. - the headline text itself, or the tags string if no headline text
  5028. - the tags string, or nil."
  5029. (save-excursion
  5030. (org-back-to-heading t)
  5031. (if (looking-at org-complex-heading-regexp)
  5032. (list (length (match-string 1))
  5033. (org-reduced-level (length (match-string 1)))
  5034. (org-match-string-no-properties 2)
  5035. (and (match-end 3) (aref (match-string 3) 2))
  5036. (org-match-string-no-properties 4)
  5037. (org-match-string-no-properties 5)))))
  5038. (defun org-get-entry ()
  5039. "Get the entry text, after heading, entire subtree."
  5040. (save-excursion
  5041. (org-back-to-heading t)
  5042. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5043. (defun org-insert-heading-after-current ()
  5044. "Insert a new heading with same level as current, after current subtree."
  5045. (interactive)
  5046. (org-back-to-heading)
  5047. (org-insert-heading)
  5048. (org-move-subtree-down)
  5049. (end-of-line 1))
  5050. (defun org-insert-heading-respect-content ()
  5051. (interactive)
  5052. (let ((org-insert-heading-respect-content t))
  5053. (org-insert-heading t)))
  5054. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5055. (interactive "P")
  5056. (let ((org-insert-heading-respect-content t))
  5057. (org-insert-todo-heading force-state t)))
  5058. (defun org-insert-todo-heading (arg &optional force-heading)
  5059. "Insert a new heading with the same level and TODO state as current heading.
  5060. If the heading has no TODO state, or if the state is DONE, use the first
  5061. state (TODO by default). Also with prefix arg, force first state."
  5062. (interactive "P")
  5063. (when (or force-heading (not (org-insert-item 'checkbox)))
  5064. (org-insert-heading force-heading)
  5065. (save-excursion
  5066. (org-back-to-heading)
  5067. (outline-previous-heading)
  5068. (looking-at org-todo-line-regexp))
  5069. (let*
  5070. ((new-mark-x
  5071. (if (or arg
  5072. (not (match-beginning 2))
  5073. (member (match-string 2) org-done-keywords))
  5074. (car org-todo-keywords-1)
  5075. (match-string 2)))
  5076. (new-mark
  5077. (or
  5078. (run-hook-with-args-until-success
  5079. 'org-todo-get-default-hook new-mark-x nil)
  5080. new-mark-x)))
  5081. (beginning-of-line 1)
  5082. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5083. (if org-treat-insert-todo-heading-as-state-change
  5084. (org-todo new-mark)
  5085. (insert new-mark " "))))
  5086. (when org-provide-todo-statistics
  5087. (org-update-parent-todo-statistics))))
  5088. (defun org-insert-subheading (arg)
  5089. "Insert a new subheading and demote it.
  5090. Works for outline headings and for plain lists alike."
  5091. (interactive "P")
  5092. (org-insert-heading arg)
  5093. (cond
  5094. ((org-on-heading-p) (org-do-demote))
  5095. ((org-at-item-p) (org-indent-item 1))))
  5096. (defun org-insert-todo-subheading (arg)
  5097. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5098. Works for outline headings and for plain lists alike."
  5099. (interactive "P")
  5100. (org-insert-todo-heading arg)
  5101. (cond
  5102. ((org-on-heading-p) (org-do-demote))
  5103. ((org-at-item-p) (org-indent-item 1))))
  5104. ;;; Promotion and Demotion
  5105. (defun org-promote-subtree ()
  5106. "Promote the entire subtree.
  5107. See also `org-promote'."
  5108. (interactive)
  5109. (save-excursion
  5110. (org-map-tree 'org-promote))
  5111. (org-fix-position-after-promote))
  5112. (defun org-demote-subtree ()
  5113. "Demote the entire subtree. See `org-demote'.
  5114. See also `org-promote'."
  5115. (interactive)
  5116. (save-excursion
  5117. (org-map-tree 'org-demote))
  5118. (org-fix-position-after-promote))
  5119. (defun org-do-promote ()
  5120. "Promote the current heading higher up the tree.
  5121. If the region is active in `transient-mark-mode', promote all headings
  5122. in the region."
  5123. (interactive)
  5124. (save-excursion
  5125. (if (org-region-active-p)
  5126. (org-map-region 'org-promote (region-beginning) (region-end))
  5127. (org-promote)))
  5128. (org-fix-position-after-promote))
  5129. (defun org-do-demote ()
  5130. "Demote the current heading lower down the tree.
  5131. If the region is active in `transient-mark-mode', demote all headings
  5132. in the region."
  5133. (interactive)
  5134. (save-excursion
  5135. (if (org-region-active-p)
  5136. (org-map-region 'org-demote (region-beginning) (region-end))
  5137. (org-demote)))
  5138. (org-fix-position-after-promote))
  5139. (defun org-fix-position-after-promote ()
  5140. "Make sure that after pro/demotion cursor position is right."
  5141. (let ((pos (point)))
  5142. (when (save-excursion
  5143. (beginning-of-line 1)
  5144. (looking-at org-todo-line-regexp)
  5145. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5146. (cond ((eobp) (insert " "))
  5147. ((eolp) (insert " "))
  5148. ((equal (char-after) ?\ ) (forward-char 1))))))
  5149. (defun org-reduced-level (l)
  5150. "Compute the effective level of a heading.
  5151. This takes into account the setting of `org-odd-levels-only'."
  5152. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5153. (defun org-get-valid-level (level &optional change)
  5154. "Rectify a level change under the influence of `org-odd-levels-only'
  5155. LEVEL is a current level, CHANGE is by how much the level should be
  5156. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5157. even level numbers will become the next higher odd number."
  5158. (if org-odd-levels-only
  5159. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5160. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5161. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5162. (max 1 (+ level (or change 0)))))
  5163. (if (boundp 'define-obsolete-function-alias)
  5164. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5165. (define-obsolete-function-alias 'org-get-legal-level
  5166. 'org-get-valid-level)
  5167. (define-obsolete-function-alias 'org-get-legal-level
  5168. 'org-get-valid-level "23.1")))
  5169. (defun org-promote ()
  5170. "Promote the current heading higher up the tree.
  5171. If the region is active in `transient-mark-mode', promote all headings
  5172. in the region."
  5173. (org-back-to-heading t)
  5174. (let* ((level (save-match-data (funcall outline-level)))
  5175. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5176. (diff (abs (- level (length up-head) -1))))
  5177. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5178. (replace-match up-head nil t)
  5179. ;; Fixup tag positioning
  5180. (and org-auto-align-tags (org-set-tags nil t))
  5181. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5182. (defun org-demote ()
  5183. "Demote the current heading lower down the tree.
  5184. If the region is active in `transient-mark-mode', demote all headings
  5185. in the region."
  5186. (org-back-to-heading t)
  5187. (let* ((level (save-match-data (funcall outline-level)))
  5188. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5189. (diff (abs (- level (length down-head) -1))))
  5190. (replace-match down-head nil t)
  5191. ;; Fixup tag positioning
  5192. (and org-auto-align-tags (org-set-tags nil t))
  5193. (if org-adapt-indentation (org-fixup-indentation diff))))
  5194. (defun org-map-tree (fun)
  5195. "Call FUN for every heading underneath the current one."
  5196. (org-back-to-heading)
  5197. (let ((level (funcall outline-level)))
  5198. (save-excursion
  5199. (funcall fun)
  5200. (while (and (progn
  5201. (outline-next-heading)
  5202. (> (funcall outline-level) level))
  5203. (not (eobp)))
  5204. (funcall fun)))))
  5205. (defun org-map-region (fun beg end)
  5206. "Call FUN for every heading between BEG and END."
  5207. (let ((org-ignore-region t))
  5208. (save-excursion
  5209. (setq end (copy-marker end))
  5210. (goto-char beg)
  5211. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5212. (< (point) end))
  5213. (funcall fun))
  5214. (while (and (progn
  5215. (outline-next-heading)
  5216. (< (point) end))
  5217. (not (eobp)))
  5218. (funcall fun)))))
  5219. (defun org-fixup-indentation (diff)
  5220. "Change the indentation in the current entry by DIFF
  5221. However, if any line in the current entry has no indentation, or if it
  5222. would end up with no indentation after the change, nothing at all is done."
  5223. (save-excursion
  5224. (let ((end (save-excursion (outline-next-heading)
  5225. (point-marker)))
  5226. (prohibit (if (> diff 0)
  5227. "^\\S-"
  5228. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5229. col)
  5230. (unless (save-excursion (end-of-line 1)
  5231. (re-search-forward prohibit end t))
  5232. (while (and (< (point) end)
  5233. (re-search-forward "^[ \t]+" end t))
  5234. (goto-char (match-end 0))
  5235. (setq col (current-column))
  5236. (if (< diff 0) (replace-match ""))
  5237. (org-indent-to-column (+ diff col))))
  5238. (move-marker end nil))))
  5239. (defun org-convert-to-odd-levels ()
  5240. "Convert an org-mode file with all levels allowed to one with odd levels.
  5241. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5242. level 5 etc."
  5243. (interactive)
  5244. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5245. (let ((org-odd-levels-only nil) n)
  5246. (save-excursion
  5247. (goto-char (point-min))
  5248. (while (re-search-forward "^\\*\\*+ " nil t)
  5249. (setq n (- (length (match-string 0)) 2))
  5250. (while (>= (setq n (1- n)) 0)
  5251. (org-demote))
  5252. (end-of-line 1))))))
  5253. (defun org-convert-to-oddeven-levels ()
  5254. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5255. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5256. section with an even level, conversion would destroy the structure of the file. An error
  5257. is signaled in this case."
  5258. (interactive)
  5259. (goto-char (point-min))
  5260. ;; First check if there are no even levels
  5261. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5262. (org-show-context t)
  5263. (error "Not all levels are odd in this file. Conversion not possible."))
  5264. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5265. (let ((org-odd-levels-only nil) n)
  5266. (save-excursion
  5267. (goto-char (point-min))
  5268. (while (re-search-forward "^\\*\\*+ " nil t)
  5269. (setq n (/ (1- (length (match-string 0))) 2))
  5270. (while (>= (setq n (1- n)) 0)
  5271. (org-promote))
  5272. (end-of-line 1))))))
  5273. (defun org-tr-level (n)
  5274. "Make N odd if required."
  5275. (if org-odd-levels-only (1+ (/ n 2)) n))
  5276. ;;; Vertical tree motion, cutting and pasting of subtrees
  5277. (defun org-move-subtree-up (&optional arg)
  5278. "Move the current subtree up past ARG headlines of the same level."
  5279. (interactive "p")
  5280. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5281. (defun org-move-subtree-down (&optional arg)
  5282. "Move the current subtree down past ARG headlines of the same level."
  5283. (interactive "p")
  5284. (setq arg (prefix-numeric-value arg))
  5285. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5286. 'outline-get-last-sibling))
  5287. (ins-point (make-marker))
  5288. (cnt (abs arg))
  5289. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5290. ;; Select the tree
  5291. (org-back-to-heading)
  5292. (setq beg0 (point))
  5293. (save-excursion
  5294. (setq ne-beg (org-back-over-empty-lines))
  5295. (setq beg (point)))
  5296. (save-match-data
  5297. (save-excursion (outline-end-of-heading)
  5298. (setq folded (org-invisible-p)))
  5299. (outline-end-of-subtree))
  5300. (outline-next-heading)
  5301. (setq ne-end (org-back-over-empty-lines))
  5302. (setq end (point))
  5303. (goto-char beg0)
  5304. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5305. ;; include less whitespace
  5306. (save-excursion
  5307. (goto-char beg)
  5308. (forward-line (- ne-beg ne-end))
  5309. (setq beg (point))))
  5310. ;; Find insertion point, with error handling
  5311. (while (> cnt 0)
  5312. (or (and (funcall movfunc) (looking-at outline-regexp))
  5313. (progn (goto-char beg0)
  5314. (error "Cannot move past superior level or buffer limit")))
  5315. (setq cnt (1- cnt)))
  5316. (if (> arg 0)
  5317. ;; Moving forward - still need to move over subtree
  5318. (progn (org-end-of-subtree t t)
  5319. (save-excursion
  5320. (org-back-over-empty-lines)
  5321. (or (bolp) (newline)))))
  5322. (setq ne-ins (org-back-over-empty-lines))
  5323. (move-marker ins-point (point))
  5324. (setq txt (buffer-substring beg end))
  5325. (org-save-markers-in-region beg end)
  5326. (delete-region beg end)
  5327. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5328. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5329. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5330. (let ((bbb (point)))
  5331. (insert-before-markers txt)
  5332. (org-reinstall-markers-in-region bbb)
  5333. (move-marker ins-point bbb))
  5334. (or (bolp) (insert "\n"))
  5335. (setq ins-end (point))
  5336. (goto-char ins-point)
  5337. (org-skip-whitespace)
  5338. (when (and (< arg 0)
  5339. (org-first-sibling-p)
  5340. (> ne-ins ne-beg))
  5341. ;; Move whitespace back to beginning
  5342. (save-excursion
  5343. (goto-char ins-end)
  5344. (let ((kill-whole-line t))
  5345. (kill-line (- ne-ins ne-beg)) (point)))
  5346. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5347. (move-marker ins-point nil)
  5348. (org-compact-display-after-subtree-move)
  5349. (org-show-empty-lines-in-parent)
  5350. (unless folded
  5351. (org-show-entry)
  5352. (show-children)
  5353. (org-cycle-hide-drawers 'children))))
  5354. (defvar org-subtree-clip ""
  5355. "Clipboard for cut and paste of subtrees.
  5356. This is actually only a copy of the kill, because we use the normal kill
  5357. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5358. (defvar org-subtree-clip-folded nil
  5359. "Was the last copied subtree folded?
  5360. This is used to fold the tree back after pasting.")
  5361. (defun org-cut-subtree (&optional n)
  5362. "Cut the current subtree into the clipboard.
  5363. With prefix arg N, cut this many sequential subtrees.
  5364. This is a short-hand for marking the subtree and then cutting it."
  5365. (interactive "p")
  5366. (org-copy-subtree n 'cut))
  5367. (defun org-copy-subtree (&optional n cut force-store-markers)
  5368. "Cut the current subtree into the clipboard.
  5369. With prefix arg N, cut this many sequential subtrees.
  5370. This is a short-hand for marking the subtree and then copying it.
  5371. If CUT is non-nil, actually cut the subtree.
  5372. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5373. of some markers in the region, even if CUT is non-nil. This is
  5374. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5375. (interactive "p")
  5376. (let (beg end folded (beg0 (point)))
  5377. (if (interactive-p)
  5378. (org-back-to-heading nil) ; take what looks like a subtree
  5379. (org-back-to-heading t)) ; take what is really there
  5380. (org-back-over-empty-lines)
  5381. (setq beg (point))
  5382. (skip-chars-forward " \t\r\n")
  5383. (save-match-data
  5384. (save-excursion (outline-end-of-heading)
  5385. (setq folded (org-invisible-p)))
  5386. (condition-case nil
  5387. (outline-forward-same-level (1- n))
  5388. (error nil))
  5389. (org-end-of-subtree t t))
  5390. (org-back-over-empty-lines)
  5391. (setq end (point))
  5392. (goto-char beg0)
  5393. (when (> end beg)
  5394. (setq org-subtree-clip-folded folded)
  5395. (when (or cut force-store-markers)
  5396. (org-save-markers-in-region beg end))
  5397. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5398. (setq org-subtree-clip (current-kill 0))
  5399. (message "%s: Subtree(s) with %d characters"
  5400. (if cut "Cut" "Copied")
  5401. (length org-subtree-clip)))))
  5402. (defun org-paste-subtree (&optional level tree for-yank)
  5403. "Paste the clipboard as a subtree, with modification of headline level.
  5404. The entire subtree is promoted or demoted in order to match a new headline
  5405. level.
  5406. If the cursor is at the beginning of a headline, the same level as
  5407. that headline is used to paste the tree
  5408. If not, the new level is derived from the *visible* headings
  5409. before and after the insertion point, and taken to be the inferior headline
  5410. level of the two. So if the previous visible heading is level 3 and the
  5411. next is level 4 (or vice versa), level 4 will be used for insertion.
  5412. This makes sure that the subtree remains an independent subtree and does
  5413. not swallow low level entries.
  5414. You can also force a different level, either by using a numeric prefix
  5415. argument, or by inserting the heading marker by hand. For example, if the
  5416. cursor is after \"*****\", then the tree will be shifted to level 5.
  5417. If optional TREE is given, use this text instead of the kill ring.
  5418. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5419. move back over whitespace before inserting, and move point to the end of
  5420. the inserted text when done."
  5421. (interactive "P")
  5422. (unless (org-kill-is-subtree-p tree)
  5423. (error "%s"
  5424. (substitute-command-keys
  5425. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5426. (let* ((visp (not (org-invisible-p)))
  5427. (txt (or tree (and kill-ring (current-kill 0))))
  5428. (^re (concat "^\\(" outline-regexp "\\)"))
  5429. (re (concat "\\(" outline-regexp "\\)"))
  5430. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5431. (old-level (if (string-match ^re txt)
  5432. (- (match-end 0) (match-beginning 0) 1)
  5433. -1))
  5434. (force-level (cond (level (prefix-numeric-value level))
  5435. ((and (looking-at "[ \t]*$")
  5436. (string-match
  5437. ^re_ (buffer-substring
  5438. (point-at-bol) (point))))
  5439. (- (match-end 1) (match-beginning 1)))
  5440. ((and (bolp)
  5441. (looking-at org-outline-regexp))
  5442. (- (match-end 0) (point) 1))
  5443. (t nil)))
  5444. (previous-level (save-excursion
  5445. (condition-case nil
  5446. (progn
  5447. (outline-previous-visible-heading 1)
  5448. (if (looking-at re)
  5449. (- (match-end 0) (match-beginning 0) 1)
  5450. 1))
  5451. (error 1))))
  5452. (next-level (save-excursion
  5453. (condition-case nil
  5454. (progn
  5455. (or (looking-at outline-regexp)
  5456. (outline-next-visible-heading 1))
  5457. (if (looking-at re)
  5458. (- (match-end 0) (match-beginning 0) 1)
  5459. 1))
  5460. (error 1))))
  5461. (new-level (or force-level (max previous-level next-level)))
  5462. (shift (if (or (= old-level -1)
  5463. (= new-level -1)
  5464. (= old-level new-level))
  5465. 0
  5466. (- new-level old-level)))
  5467. (delta (if (> shift 0) -1 1))
  5468. (func (if (> shift 0) 'org-demote 'org-promote))
  5469. (org-odd-levels-only nil)
  5470. beg end newend)
  5471. ;; Remove the forced level indicator
  5472. (if force-level
  5473. (delete-region (point-at-bol) (point)))
  5474. ;; Paste
  5475. (beginning-of-line 1)
  5476. (unless for-yank (org-back-over-empty-lines))
  5477. (setq beg (point))
  5478. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5479. (insert-before-markers txt)
  5480. (unless (string-match "\n\\'" txt) (insert "\n"))
  5481. (setq newend (point))
  5482. (org-reinstall-markers-in-region beg)
  5483. (setq end (point))
  5484. (goto-char beg)
  5485. (skip-chars-forward " \t\n\r")
  5486. (setq beg (point))
  5487. (if (and (org-invisible-p) visp)
  5488. (save-excursion (outline-show-heading)))
  5489. ;; Shift if necessary
  5490. (unless (= shift 0)
  5491. (save-restriction
  5492. (narrow-to-region beg end)
  5493. (while (not (= shift 0))
  5494. (org-map-region func (point-min) (point-max))
  5495. (setq shift (+ delta shift)))
  5496. (goto-char (point-min))
  5497. (setq newend (point-max))))
  5498. (when (or (interactive-p) for-yank)
  5499. (message "Clipboard pasted as level %d subtree" new-level))
  5500. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5501. kill-ring
  5502. (eq org-subtree-clip (current-kill 0))
  5503. org-subtree-clip-folded)
  5504. ;; The tree was folded before it was killed/copied
  5505. (hide-subtree))
  5506. (and for-yank (goto-char newend))))
  5507. (defun org-kill-is-subtree-p (&optional txt)
  5508. "Check if the current kill is an outline subtree, or a set of trees.
  5509. Returns nil if kill does not start with a headline, or if the first
  5510. headline level is not the largest headline level in the tree.
  5511. So this will actually accept several entries of equal levels as well,
  5512. which is OK for `org-paste-subtree'.
  5513. If optional TXT is given, check this string instead of the current kill."
  5514. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5515. (start-level (and kill
  5516. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5517. org-outline-regexp "\\)")
  5518. kill)
  5519. (- (match-end 2) (match-beginning 2) 1)))
  5520. (re (concat "^" org-outline-regexp))
  5521. (start (1+ (or (match-beginning 2) -1))))
  5522. (if (not start-level)
  5523. (progn
  5524. nil) ;; does not even start with a heading
  5525. (catch 'exit
  5526. (while (setq start (string-match re kill (1+ start)))
  5527. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5528. (throw 'exit nil)))
  5529. t))))
  5530. (defvar org-markers-to-move nil
  5531. "Markers that should be moved with a cut-and-paste operation.
  5532. Those markers are stored together with their positions relative to
  5533. the start of the region.")
  5534. (defun org-save-markers-in-region (beg end)
  5535. "Check markers in region.
  5536. If these markers are between BEG and END, record their position relative
  5537. to BEG, so that after moving the block of text, we can put the markers back
  5538. into place.
  5539. This function gets called just before an entry or tree gets cut from the
  5540. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5541. called immediately, to move the markers with the entries."
  5542. (setq org-markers-to-move nil)
  5543. (when (featurep 'org-clock)
  5544. (org-clock-save-markers-for-cut-and-paste beg end))
  5545. (when (featurep 'org-agenda)
  5546. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5547. (defun org-check-and-save-marker (marker beg end)
  5548. "Check if MARKER is between BEG and END.
  5549. If yes, remember the marker and the distance to BEG."
  5550. (when (and (marker-buffer marker)
  5551. (equal (marker-buffer marker) (current-buffer)))
  5552. (if (and (>= marker beg) (< marker end))
  5553. (push (cons marker (- marker beg)) org-markers-to-move))))
  5554. (defun org-reinstall-markers-in-region (beg)
  5555. "Move all remembered markers to their position relative to BEG."
  5556. (mapc (lambda (x)
  5557. (move-marker (car x) (+ beg (cdr x))))
  5558. org-markers-to-move)
  5559. (setq org-markers-to-move nil))
  5560. (defun org-narrow-to-subtree ()
  5561. "Narrow buffer to the current subtree."
  5562. (interactive)
  5563. (save-excursion
  5564. (save-match-data
  5565. (narrow-to-region
  5566. (progn (org-back-to-heading t) (point))
  5567. (progn (org-end-of-subtree t) (point))))))
  5568. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5569. "Clone the task (subtree) at point N times.
  5570. The clones will be inserted as siblings.
  5571. In interactive use, the user will be prompted for the number of clones
  5572. to be produced, and for a time SHIFT, which may be a repeater as used
  5573. in time stamps, for example `+3d'.
  5574. When a valid repeater is given and the entry contains any time stamps,
  5575. the clones will become a sequence in time, with time stamps in the
  5576. subtree shifted for each clone produced. If SHIFT is nil or the
  5577. empty string, time stamps will be left alone.
  5578. If the original subtree did contain time stamps with a repeater,
  5579. the following will happen:
  5580. - the repeater will be removed in each clone
  5581. - an additional clone will be produced, with the current, unshifted
  5582. date(s) in the entry.
  5583. - the original entry will be placed *after* all the clones, with
  5584. repeater intact.
  5585. - the start days in the repeater in the original entry will be shifted
  5586. to past the last clone.
  5587. I this way you can spell out a number of instances of a repeating task,
  5588. and still retain the repeater to cover future instances of the task."
  5589. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5590. (let (beg end template task
  5591. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5592. (if (not (and (integerp n) (> n 0)))
  5593. (error "Invalid number of replications %s" n))
  5594. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5595. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5596. shift)))
  5597. (error "Invalid shift specification %s" shift))
  5598. (when doshift
  5599. (setq shift-n (string-to-number (match-string 1 shift))
  5600. shift-what (cdr (assoc (match-string 2 shift)
  5601. '(("d" . day) ("w" . week)
  5602. ("m" . month) ("y" . year))))))
  5603. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5604. (setq nmin 1 nmax n)
  5605. (org-back-to-heading t)
  5606. (setq beg (point))
  5607. (org-end-of-subtree t t)
  5608. (setq end (point))
  5609. (setq template (buffer-substring beg end))
  5610. (when (and doshift
  5611. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5612. (delete-region beg end)
  5613. (setq end beg)
  5614. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5615. (goto-char end)
  5616. (loop for n from nmin to nmax do
  5617. (if (not doshift)
  5618. (setq task template)
  5619. (with-temp-buffer
  5620. (insert template)
  5621. (org-mode)
  5622. (goto-char (point-min))
  5623. (while (re-search-forward org-ts-regexp-both nil t)
  5624. (org-timestamp-change (* n shift-n) shift-what))
  5625. (unless (= n n-no-remove)
  5626. (goto-char (point-min))
  5627. (while (re-search-forward org-ts-regexp nil t)
  5628. (save-excursion
  5629. (goto-char (match-beginning 0))
  5630. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5631. (delete-region (match-beginning 1) (match-end 1))))))
  5632. (setq task (buffer-string))))
  5633. (insert task))
  5634. (goto-char beg)))
  5635. ;;; Outline Sorting
  5636. (defun org-sort (with-case)
  5637. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5638. Optional argument WITH-CASE means sort case-sensitively.
  5639. With a double prefix argument, also remove duplicate entries."
  5640. (interactive "P")
  5641. (if (org-at-table-p)
  5642. (org-call-with-arg 'org-table-sort-lines with-case)
  5643. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5644. (defun org-sort-remove-invisible (s)
  5645. (remove-text-properties 0 (length s) org-rm-props s)
  5646. (while (string-match org-bracket-link-regexp s)
  5647. (setq s (replace-match (if (match-end 2)
  5648. (match-string 3 s)
  5649. (match-string 1 s)) t t s)))
  5650. s)
  5651. (defvar org-priority-regexp) ; defined later in the file
  5652. (defvar org-after-sorting-entries-or-items-hook nil
  5653. "Hook that is run after a bunch of entries or items have been sorted.
  5654. When children are sorted, the cursor is in the parent line when this
  5655. hook gets called. When a region or a plain list is sorted, the cursor
  5656. will be in the first entry of the sorted region/list.")
  5657. (defun org-sort-entries-or-items
  5658. (&optional with-case sorting-type getkey-func compare-func property)
  5659. "Sort entries on a certain level of an outline tree, or plain list items.
  5660. If there is an active region, the entries in the region are sorted.
  5661. Else, if the cursor is before the first entry, sort the top-level items.
  5662. Else, the children of the entry at point are sorted.
  5663. If the cursor is at the first item in a plain list, the list items will be
  5664. sorted.
  5665. Sorting can be alphabetically, numerically, by date/time as given by
  5666. a time stamp, by a property or by priority.
  5667. The command prompts for the sorting type unless it has been given to the
  5668. function through the SORTING-TYPE argument, which needs to a character,
  5669. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5670. precise meaning of each character:
  5671. n Numerically, by converting the beginning of the entry/item to a number.
  5672. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5673. t By date/time, either the first active time stamp in the entry, or, if
  5674. none exist, by the first inactive one.
  5675. In items, only the first line will be chekced.
  5676. s By the scheduled date/time.
  5677. d By deadline date/time.
  5678. c By creation time, which is assumed to be the first inactive time stamp
  5679. at the beginning of a line.
  5680. p By priority according to the cookie.
  5681. r By the value of a property.
  5682. Capital letters will reverse the sort order.
  5683. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5684. called with point at the beginning of the record. It must return either
  5685. a string or a number that should serve as the sorting key for that record.
  5686. Comparing entries ignores case by default. However, with an optional argument
  5687. WITH-CASE, the sorting considers case as well."
  5688. (interactive "P")
  5689. (let ((case-func (if with-case 'identity 'downcase))
  5690. start beg end stars re re2
  5691. txt what tmp plain-list-p)
  5692. ;; Find beginning and end of region to sort
  5693. (cond
  5694. ((org-region-active-p)
  5695. ;; we will sort the region
  5696. (setq end (region-end)
  5697. what "region")
  5698. (goto-char (region-beginning))
  5699. (if (not (org-on-heading-p)) (outline-next-heading))
  5700. (setq start (point)))
  5701. ((org-at-item-p)
  5702. ;; we will sort this plain list
  5703. (org-beginning-of-item-list) (setq start (point))
  5704. (org-end-of-item-list) (setq end (point))
  5705. (goto-char start)
  5706. (setq plain-list-p t
  5707. what "plain list"))
  5708. ((or (org-on-heading-p)
  5709. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5710. ;; we will sort the children of the current headline
  5711. (org-back-to-heading)
  5712. (setq start (point)
  5713. end (progn (org-end-of-subtree t t)
  5714. (org-back-over-empty-lines)
  5715. (point))
  5716. what "children")
  5717. (goto-char start)
  5718. (show-subtree)
  5719. (outline-next-heading))
  5720. (t
  5721. ;; we will sort the top-level entries in this file
  5722. (goto-char (point-min))
  5723. (or (org-on-heading-p) (outline-next-heading))
  5724. (setq start (point) end (point-max) what "top-level")
  5725. (goto-char start)
  5726. (show-all)))
  5727. (setq beg (point))
  5728. (if (>= beg end) (error "Nothing to sort"))
  5729. (unless plain-list-p
  5730. (looking-at "\\(\\*+\\)")
  5731. (setq stars (match-string 1)
  5732. re (concat "^" (regexp-quote stars) " +")
  5733. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5734. txt (buffer-substring beg end))
  5735. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5736. (if (and (not (equal stars "*")) (string-match re2 txt))
  5737. (error "Region to sort contains a level above the first entry")))
  5738. (unless sorting-type
  5739. (message
  5740. (if plain-list-p
  5741. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5742. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5743. [t]ime [s]cheduled [d]eadline [c]reated
  5744. A/N/T/S/D/C/P/O/F means reversed:")
  5745. what)
  5746. (setq sorting-type (read-char-exclusive))
  5747. (and (= (downcase sorting-type) ?f)
  5748. (setq getkey-func
  5749. (org-ido-completing-read "Sort using function: "
  5750. obarray 'fboundp t nil nil))
  5751. (setq getkey-func (intern getkey-func)))
  5752. (and (= (downcase sorting-type) ?r)
  5753. (setq property
  5754. (org-ido-completing-read "Property: "
  5755. (mapcar 'list (org-buffer-property-keys t))
  5756. nil t))))
  5757. (message "Sorting entries...")
  5758. (save-restriction
  5759. (narrow-to-region start end)
  5760. (let ((dcst (downcase sorting-type))
  5761. (case-fold-search nil)
  5762. (now (current-time)))
  5763. (sort-subr
  5764. (/= dcst sorting-type)
  5765. ;; This function moves to the beginning character of the "record" to
  5766. ;; be sorted.
  5767. (if plain-list-p
  5768. (lambda nil
  5769. (if (org-at-item-p) t (goto-char (point-max))))
  5770. (lambda nil
  5771. (if (re-search-forward re nil t)
  5772. (goto-char (match-beginning 0))
  5773. (goto-char (point-max)))))
  5774. ;; This function moves to the last character of the "record" being
  5775. ;; sorted.
  5776. (if plain-list-p
  5777. 'org-end-of-item
  5778. (lambda nil
  5779. (save-match-data
  5780. (condition-case nil
  5781. (outline-forward-same-level 1)
  5782. (error
  5783. (goto-char (point-max)))))))
  5784. ;; This function returns the value that gets sorted against.
  5785. (if plain-list-p
  5786. (lambda nil
  5787. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5788. (cond
  5789. ((= dcst ?n)
  5790. (string-to-number (buffer-substring (match-end 0)
  5791. (point-at-eol))))
  5792. ((= dcst ?a)
  5793. (buffer-substring (match-end 0) (point-at-eol)))
  5794. ((= dcst ?t)
  5795. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5796. (re-search-forward org-ts-regexp-both
  5797. (point-at-eol) t))
  5798. (org-time-string-to-seconds (match-string 0))
  5799. (time-to-seconds now)))
  5800. ((= dcst ?f)
  5801. (if getkey-func
  5802. (progn
  5803. (setq tmp (funcall getkey-func))
  5804. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5805. tmp)
  5806. (error "Invalid key function `%s'" getkey-func)))
  5807. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5808. (lambda nil
  5809. (cond
  5810. ((= dcst ?n)
  5811. (if (looking-at org-complex-heading-regexp)
  5812. (string-to-number (match-string 4))
  5813. nil))
  5814. ((= dcst ?a)
  5815. (if (looking-at org-complex-heading-regexp)
  5816. (funcall case-func (match-string 4))
  5817. nil))
  5818. ((= dcst ?t)
  5819. (let ((end (save-excursion (outline-next-heading) (point))))
  5820. (if (or (re-search-forward org-ts-regexp end t)
  5821. (re-search-forward org-ts-regexp-both end t))
  5822. (org-time-string-to-seconds (match-string 0))
  5823. (time-to-seconds now))))
  5824. ((= dcst ?c)
  5825. (let ((end (save-excursion (outline-next-heading) (point))))
  5826. (if (re-search-forward
  5827. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5828. end t)
  5829. (org-time-string-to-seconds (match-string 0))
  5830. (time-to-seconds now))))
  5831. ((= dcst ?s)
  5832. (let ((end (save-excursion (outline-next-heading) (point))))
  5833. (if (re-search-forward org-scheduled-time-regexp end t)
  5834. (org-time-string-to-seconds (match-string 1))
  5835. (time-to-seconds now))))
  5836. ((= dcst ?d)
  5837. (let ((end (save-excursion (outline-next-heading) (point))))
  5838. (if (re-search-forward org-deadline-time-regexp end t)
  5839. (org-time-string-to-seconds (match-string 1))
  5840. (time-to-seconds now))))
  5841. ((= dcst ?p)
  5842. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5843. (string-to-char (match-string 2))
  5844. org-default-priority))
  5845. ((= dcst ?r)
  5846. (or (org-entry-get nil property) ""))
  5847. ((= dcst ?o)
  5848. (if (looking-at org-complex-heading-regexp)
  5849. (- 9999 (length (member (match-string 2)
  5850. org-todo-keywords-1)))))
  5851. ((= dcst ?f)
  5852. (if getkey-func
  5853. (progn
  5854. (setq tmp (funcall getkey-func))
  5855. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5856. tmp)
  5857. (error "Invalid key function `%s'" getkey-func)))
  5858. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5859. nil
  5860. (cond
  5861. ((= dcst ?a) 'string<)
  5862. ((= dcst ?f) compare-func)
  5863. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5864. (t nil)))))
  5865. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5866. (message "Sorting entries...done")))
  5867. (defun org-do-sort (table what &optional with-case sorting-type)
  5868. "Sort TABLE of WHAT according to SORTING-TYPE.
  5869. The user will be prompted for the SORTING-TYPE if the call to this
  5870. function does not specify it. WHAT is only for the prompt, to indicate
  5871. what is being sorted. The sorting key will be extracted from
  5872. the car of the elements of the table.
  5873. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5874. (unless sorting-type
  5875. (message
  5876. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5877. what)
  5878. (setq sorting-type (read-char-exclusive)))
  5879. (let ((dcst (downcase sorting-type))
  5880. extractfun comparefun)
  5881. ;; Define the appropriate functions
  5882. (cond
  5883. ((= dcst ?n)
  5884. (setq extractfun 'string-to-number
  5885. comparefun (if (= dcst sorting-type) '< '>)))
  5886. ((= dcst ?a)
  5887. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5888. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5889. comparefun (if (= dcst sorting-type)
  5890. 'string<
  5891. (lambda (a b) (and (not (string< a b))
  5892. (not (string= a b)))))))
  5893. ((= dcst ?t)
  5894. (setq extractfun
  5895. (lambda (x)
  5896. (if (or (string-match org-ts-regexp x)
  5897. (string-match org-ts-regexp-both x))
  5898. (time-to-seconds
  5899. (org-time-string-to-time (match-string 0 x)))
  5900. 0))
  5901. comparefun (if (= dcst sorting-type) '< '>)))
  5902. (t (error "Invalid sorting type `%c'" sorting-type)))
  5903. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5904. table)
  5905. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5906. ;;; The orgstruct minor mode
  5907. ;; Define a minor mode which can be used in other modes in order to
  5908. ;; integrate the org-mode structure editing commands.
  5909. ;; This is really a hack, because the org-mode structure commands use
  5910. ;; keys which normally belong to the major mode. Here is how it
  5911. ;; works: The minor mode defines all the keys necessary to operate the
  5912. ;; structure commands, but wraps the commands into a function which
  5913. ;; tests if the cursor is currently at a headline or a plain list
  5914. ;; item. If that is the case, the structure command is used,
  5915. ;; temporarily setting many Org-mode variables like regular
  5916. ;; expressions for filling etc. However, when any of those keys is
  5917. ;; used at a different location, function uses `key-binding' to look
  5918. ;; up if the key has an associated command in another currently active
  5919. ;; keymap (minor modes, major mode, global), and executes that
  5920. ;; command. There might be problems if any of the keys is otherwise
  5921. ;; used as a prefix key.
  5922. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5923. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5924. ;; addresses this by checking explicitly for both bindings.
  5925. (defvar orgstruct-mode-map (make-sparse-keymap)
  5926. "Keymap for the minor `orgstruct-mode'.")
  5927. (defvar org-local-vars nil
  5928. "List of local variables, for use by `orgstruct-mode'")
  5929. ;;;###autoload
  5930. (define-minor-mode orgstruct-mode
  5931. "Toggle the minor more `orgstruct-mode'.
  5932. This mode is for using Org-mode structure commands in other modes.
  5933. The following key behave as if Org-mode was active, if the cursor
  5934. is on a headline, or on a plain list item (both in the definition
  5935. of Org-mode).
  5936. M-up Move entry/item up
  5937. M-down Move entry/item down
  5938. M-left Promote
  5939. M-right Demote
  5940. M-S-up Move entry/item up
  5941. M-S-down Move entry/item down
  5942. M-S-left Promote subtree
  5943. M-S-right Demote subtree
  5944. M-q Fill paragraph and items like in Org-mode
  5945. C-c ^ Sort entries
  5946. C-c - Cycle list bullet
  5947. TAB Cycle item visibility
  5948. M-RET Insert new heading/item
  5949. S-M-RET Insert new TODO heading / Checkbox item
  5950. C-c C-c Set tags / toggle checkbox"
  5951. nil " OrgStruct" nil
  5952. (org-load-modules-maybe)
  5953. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5954. ;;;###autoload
  5955. (defun turn-on-orgstruct ()
  5956. "Unconditionally turn on `orgstruct-mode'."
  5957. (orgstruct-mode 1))
  5958. (defun orgstruct++-mode (&optional arg)
  5959. "Toggle `orgstruct-mode', the enhanced version of it.
  5960. In addition to setting orgstruct-mode, this also exports all indentation
  5961. and autofilling variables from org-mode into the buffer. It will also
  5962. recognize item context in multiline items.
  5963. Note that turning off orgstruct-mode will *not* remove the
  5964. indentation/paragraph settings. This can only be done by refreshing the
  5965. major mode, for example with \\[normal-mode]."
  5966. (interactive "P")
  5967. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5968. (if (< arg 1)
  5969. (orgstruct-mode -1)
  5970. (orgstruct-mode 1)
  5971. (let (var val)
  5972. (mapc
  5973. (lambda (x)
  5974. (when (string-match
  5975. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5976. (symbol-name (car x)))
  5977. (setq var (car x) val (nth 1 x))
  5978. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5979. org-local-vars)
  5980. (org-set-local 'orgstruct-is-++ t))))
  5981. (defvar orgstruct-is-++ nil
  5982. "Is orgstruct-mode in ++ version in the current-buffer?")
  5983. (make-variable-buffer-local 'orgstruct-is-++)
  5984. ;;;###autoload
  5985. (defun turn-on-orgstruct++ ()
  5986. "Unconditionally turn on `orgstruct++-mode'."
  5987. (orgstruct++-mode 1))
  5988. (defun orgstruct-error ()
  5989. "Error when there is no default binding for a structure key."
  5990. (interactive)
  5991. (error "This key has no function outside structure elements"))
  5992. (defun orgstruct-setup ()
  5993. "Setup orgstruct keymaps."
  5994. (let ((nfunc 0)
  5995. (bindings
  5996. (list
  5997. '([(meta up)] org-metaup)
  5998. '([(meta down)] org-metadown)
  5999. '([(meta left)] org-metaleft)
  6000. '([(meta right)] org-metaright)
  6001. '([(meta shift up)] org-shiftmetaup)
  6002. '([(meta shift down)] org-shiftmetadown)
  6003. '([(meta shift left)] org-shiftmetaleft)
  6004. '([(meta shift right)] org-shiftmetaright)
  6005. '([?\e (up)] org-metaup)
  6006. '([?\e (down)] org-metadown)
  6007. '([?\e (left)] org-metaleft)
  6008. '([?\e (right)] org-metaright)
  6009. '([?\e (shift up)] org-shiftmetaup)
  6010. '([?\e (shift down)] org-shiftmetadown)
  6011. '([?\e (shift left)] org-shiftmetaleft)
  6012. '([?\e (shift right)] org-shiftmetaright)
  6013. '([(shift up)] org-shiftup)
  6014. '([(shift down)] org-shiftdown)
  6015. '([(shift left)] org-shiftleft)
  6016. '([(shift right)] org-shiftright)
  6017. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6018. '("\M-q" fill-paragraph)
  6019. '("\C-c^" org-sort)
  6020. '("\C-c-" org-cycle-list-bullet)))
  6021. elt key fun cmd)
  6022. (while (setq elt (pop bindings))
  6023. (setq nfunc (1+ nfunc))
  6024. (setq key (org-key (car elt))
  6025. fun (nth 1 elt)
  6026. cmd (orgstruct-make-binding fun nfunc key))
  6027. (org-defkey orgstruct-mode-map key cmd))
  6028. ;; Special treatment needed for TAB and RET
  6029. (org-defkey orgstruct-mode-map [(tab)]
  6030. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6031. (org-defkey orgstruct-mode-map "\C-i"
  6032. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6033. (org-defkey orgstruct-mode-map "\M-\C-m"
  6034. (orgstruct-make-binding 'org-insert-heading 105
  6035. "\M-\C-m" [(meta return)]))
  6036. (org-defkey orgstruct-mode-map [(meta return)]
  6037. (orgstruct-make-binding 'org-insert-heading 106
  6038. [(meta return)] "\M-\C-m"))
  6039. (org-defkey orgstruct-mode-map [(shift meta return)]
  6040. (orgstruct-make-binding 'org-insert-todo-heading 107
  6041. [(meta return)] "\M-\C-m"))
  6042. (org-defkey orgstruct-mode-map "\e\C-m"
  6043. (orgstruct-make-binding 'org-insert-heading 108
  6044. "\e\C-m" [?\e (return)]))
  6045. (org-defkey orgstruct-mode-map [?\e (return)]
  6046. (orgstruct-make-binding 'org-insert-heading 109
  6047. [?\e (return)] "\e\C-m"))
  6048. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6049. (orgstruct-make-binding 'org-insert-todo-heading 110
  6050. [?\e (return)] "\e\C-m"))
  6051. (unless org-local-vars
  6052. (setq org-local-vars (org-get-local-variables)))
  6053. t))
  6054. (defun orgstruct-make-binding (fun n &rest keys)
  6055. "Create a function for binding in the structure minor mode.
  6056. FUN is the command to call inside a table. N is used to create a unique
  6057. command name. KEYS are keys that should be checked in for a command
  6058. to execute outside of tables."
  6059. (eval
  6060. (list 'defun
  6061. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6062. '(arg)
  6063. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6064. "Outside of structure, run the binding of `"
  6065. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6066. "'.")
  6067. '(interactive "p")
  6068. (list 'if
  6069. `(org-context-p 'headline 'item
  6070. (and orgstruct-is-++
  6071. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6072. 'item-body))
  6073. (list 'org-run-like-in-org-mode (list 'quote fun))
  6074. (list 'let '(orgstruct-mode)
  6075. (list 'call-interactively
  6076. (append '(or)
  6077. (mapcar (lambda (k)
  6078. (list 'key-binding k))
  6079. keys)
  6080. '('orgstruct-error))))))))
  6081. (defun org-context-p (&rest contexts)
  6082. "Check if local context is any of CONTEXTS.
  6083. Possible values in the list of contexts are `table', `headline', and `item'."
  6084. (let ((pos (point)))
  6085. (goto-char (point-at-bol))
  6086. (prog1 (or (and (memq 'table contexts)
  6087. (looking-at "[ \t]*|"))
  6088. (and (memq 'headline contexts)
  6089. ;;????????? (looking-at "\\*+"))
  6090. (looking-at outline-regexp))
  6091. (and (memq 'item contexts)
  6092. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6093. (and (memq 'item-body contexts)
  6094. (org-in-item-p)))
  6095. (goto-char pos))))
  6096. (defun org-get-local-variables ()
  6097. "Return a list of all local variables in an org-mode buffer."
  6098. (let (varlist)
  6099. (with-current-buffer (get-buffer-create "*Org tmp*")
  6100. (erase-buffer)
  6101. (org-mode)
  6102. (setq varlist (buffer-local-variables)))
  6103. (kill-buffer "*Org tmp*")
  6104. (delq nil
  6105. (mapcar
  6106. (lambda (x)
  6107. (setq x
  6108. (if (symbolp x)
  6109. (list x)
  6110. (list (car x) (list 'quote (cdr x)))))
  6111. (if (string-match
  6112. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6113. (symbol-name (car x)))
  6114. x nil))
  6115. varlist))))
  6116. ;;;###autoload
  6117. (defun org-run-like-in-org-mode (cmd)
  6118. "Run a command, pretending that the current buffer is in Org-mode.
  6119. This will temporarily bind local variables that are typically bound in
  6120. Org-mode to the values they have in Org-mode, and then interactively
  6121. call CMD."
  6122. (org-load-modules-maybe)
  6123. (unless org-local-vars
  6124. (setq org-local-vars (org-get-local-variables)))
  6125. (eval (list 'let org-local-vars
  6126. (list 'call-interactively (list 'quote cmd)))))
  6127. ;;;; Archiving
  6128. (defun org-get-category (&optional pos)
  6129. "Get the category applying to position POS."
  6130. (get-text-property (or pos (point)) 'org-category))
  6131. (defun org-refresh-category-properties ()
  6132. "Refresh category text properties in the buffer."
  6133. (let ((def-cat (cond
  6134. ((null org-category)
  6135. (if buffer-file-name
  6136. (file-name-sans-extension
  6137. (file-name-nondirectory buffer-file-name))
  6138. "???"))
  6139. ((symbolp org-category) (symbol-name org-category))
  6140. (t org-category)))
  6141. beg end cat pos optionp)
  6142. (org-unmodified
  6143. (save-excursion
  6144. (save-restriction
  6145. (widen)
  6146. (goto-char (point-min))
  6147. (put-text-property (point) (point-max) 'org-category def-cat)
  6148. (while (re-search-forward
  6149. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6150. (setq pos (match-end 0)
  6151. optionp (equal (char-after (match-beginning 0)) ?#)
  6152. cat (org-trim (match-string 2)))
  6153. (if optionp
  6154. (setq beg (point-at-bol) end (point-max))
  6155. (org-back-to-heading t)
  6156. (setq beg (point) end (org-end-of-subtree t t)))
  6157. (put-text-property beg end 'org-category cat)
  6158. (goto-char pos)))))))
  6159. ;;;; Link Stuff
  6160. ;;; Link abbreviations
  6161. (defun org-link-expand-abbrev (link)
  6162. "Apply replacements as defined in `org-link-abbrev-alist."
  6163. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6164. (let* ((key (match-string 1 link))
  6165. (as (or (assoc key org-link-abbrev-alist-local)
  6166. (assoc key org-link-abbrev-alist)))
  6167. (tag (and (match-end 2) (match-string 3 link)))
  6168. rpl)
  6169. (if (not as)
  6170. link
  6171. (setq rpl (cdr as))
  6172. (cond
  6173. ((symbolp rpl) (funcall rpl tag))
  6174. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6175. ((string-match "%h" rpl)
  6176. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6177. (t (concat rpl tag)))))
  6178. link))
  6179. ;;; Storing and inserting links
  6180. (defvar org-insert-link-history nil
  6181. "Minibuffer history for links inserted with `org-insert-link'.")
  6182. (defvar org-stored-links nil
  6183. "Contains the links stored with `org-store-link'.")
  6184. (defvar org-store-link-plist nil
  6185. "Plist with info about the most recently link created with `org-store-link'.")
  6186. (defvar org-link-protocols nil
  6187. "Link protocols added to Org-mode using `org-add-link-type'.")
  6188. (defvar org-store-link-functions nil
  6189. "List of functions that are called to create and store a link.
  6190. Each function will be called in turn until one returns a non-nil
  6191. value. Each function should check if it is responsible for creating
  6192. this link (for example by looking at the major mode).
  6193. If not, it must exit and return nil.
  6194. If yes, it should return a non-nil value after a calling
  6195. `org-store-link-props' with a list of properties and values.
  6196. Special properties are:
  6197. :type The link prefix. like \"http\". This must be given.
  6198. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6199. This is obligatory as well.
  6200. :description Optional default description for the second pair
  6201. of brackets in an Org-mode link. The user can still change
  6202. this when inserting this link into an Org-mode buffer.
  6203. In addition to these, any additional properties can be specified
  6204. and then used in remember templates.")
  6205. (defun org-add-link-type (type &optional follow export)
  6206. "Add TYPE to the list of `org-link-types'.
  6207. Re-compute all regular expressions depending on `org-link-types'
  6208. FOLLOW and EXPORT are two functions.
  6209. FOLLOW should take the link path as the single argument and do whatever
  6210. is necessary to follow the link, for example find a file or display
  6211. a mail message.
  6212. EXPORT should format the link path for export to one of the export formats.
  6213. It should be a function accepting three arguments:
  6214. path the path of the link, the text after the prefix (like \"http:\")
  6215. desc the description of the link, if any, nil if there was no description
  6216. format the export format, a symbol like `html' or `latex'.
  6217. The function may use the FORMAT information to return different values
  6218. depending on the format. The return value will be put literally into
  6219. the exported file.
  6220. Org-mode has a built-in default for exporting links. If you are happy with
  6221. this default, there is no need to define an export function for the link
  6222. type. For a simple example of an export function, see `org-bbdb.el'."
  6223. (add-to-list 'org-link-types type t)
  6224. (org-make-link-regexps)
  6225. (if (assoc type org-link-protocols)
  6226. (setcdr (assoc type org-link-protocols) (list follow export))
  6227. (push (list type follow export) org-link-protocols)))
  6228. ;;;###autoload
  6229. (defun org-store-link (arg)
  6230. "\\<org-mode-map>Store an org-link to the current location.
  6231. This link is added to `org-stored-links' and can later be inserted
  6232. into an org-buffer with \\[org-insert-link].
  6233. For some link types, a prefix arg is interpreted:
  6234. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6235. For file links, arg negates `org-context-in-file-links'."
  6236. (interactive "P")
  6237. (org-load-modules-maybe)
  6238. (setq org-store-link-plist nil) ; reset
  6239. (let (link cpltxt desc description search txt custom-id)
  6240. (cond
  6241. ((run-hook-with-args-until-success 'org-store-link-functions)
  6242. (setq link (plist-get org-store-link-plist :link)
  6243. desc (or (plist-get org-store-link-plist :description) link)))
  6244. ((equal (buffer-name) "*Org Edit Src Example*")
  6245. (let (label gc)
  6246. (while (or (not label)
  6247. (save-excursion
  6248. (save-restriction
  6249. (widen)
  6250. (goto-char (point-min))
  6251. (re-search-forward
  6252. (regexp-quote (format org-coderef-label-format label))
  6253. nil t))))
  6254. (when label (message "Label exists already") (sit-for 2))
  6255. (setq label (read-string "Code line label: " label)))
  6256. (end-of-line 1)
  6257. (setq link (format org-coderef-label-format label))
  6258. (setq gc (- 79 (length link)))
  6259. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6260. (insert link)
  6261. (setq link (concat "(" label ")") desc nil)))
  6262. ((eq major-mode 'calendar-mode)
  6263. (let ((cd (calendar-cursor-to-date)))
  6264. (setq link
  6265. (format-time-string
  6266. (car org-time-stamp-formats)
  6267. (apply 'encode-time
  6268. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6269. nil nil nil))))
  6270. (org-store-link-props :type "calendar" :date cd)))
  6271. ((eq major-mode 'w3-mode)
  6272. (setq cpltxt (if (and (buffer-name)
  6273. (not (string-match "Untitled" (buffer-name))))
  6274. (buffer-name)
  6275. (url-view-url t))
  6276. link (org-make-link (url-view-url t)))
  6277. (org-store-link-props :type "w3" :url (url-view-url t)))
  6278. ((eq major-mode 'w3m-mode)
  6279. (setq cpltxt (or w3m-current-title w3m-current-url)
  6280. link (org-make-link w3m-current-url))
  6281. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6282. ((setq search (run-hook-with-args-until-success
  6283. 'org-create-file-search-functions))
  6284. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6285. "::" search))
  6286. (setq cpltxt (or description link)))
  6287. ((eq major-mode 'image-mode)
  6288. (setq cpltxt (concat "file:"
  6289. (abbreviate-file-name buffer-file-name))
  6290. link (org-make-link cpltxt))
  6291. (org-store-link-props :type "image" :file buffer-file-name))
  6292. ((eq major-mode 'dired-mode)
  6293. ;; link to the file in the current line
  6294. (setq cpltxt (concat "file:"
  6295. (abbreviate-file-name
  6296. (expand-file-name
  6297. (dired-get-filename nil t))))
  6298. link (org-make-link cpltxt)))
  6299. ((and buffer-file-name (org-mode-p))
  6300. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6301. (cond
  6302. ((org-in-regexp "<<\\(.*?\\)>>")
  6303. (setq cpltxt
  6304. (concat "file:"
  6305. (abbreviate-file-name buffer-file-name)
  6306. "::" (match-string 1))
  6307. link (org-make-link cpltxt)))
  6308. ((and (featurep 'org-id)
  6309. (or (eq org-link-to-org-use-id t)
  6310. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6311. (interactive-p))
  6312. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6313. (interactive-p)
  6314. (not custom-id))
  6315. (and org-link-to-org-use-id
  6316. (condition-case nil
  6317. (org-entry-get nil "ID")
  6318. (error nil)))))
  6319. ;; We can make a link using the ID.
  6320. (setq link (condition-case nil
  6321. (prog1 (org-id-store-link)
  6322. (setq desc (plist-get org-store-link-plist
  6323. :description)))
  6324. (error
  6325. ;; probably before first headline, link to file only
  6326. (concat "file:"
  6327. (abbreviate-file-name buffer-file-name))))))
  6328. (t
  6329. ;; Just link to current headline
  6330. (setq cpltxt (concat "file:"
  6331. (abbreviate-file-name buffer-file-name)))
  6332. ;; Add a context search string
  6333. (when (org-xor org-context-in-file-links arg)
  6334. (setq txt (cond
  6335. ((org-on-heading-p) nil)
  6336. ((org-region-active-p)
  6337. (buffer-substring (region-beginning) (region-end)))
  6338. (t nil)))
  6339. (when (or (null txt) (string-match "\\S-" txt))
  6340. (setq cpltxt
  6341. (concat cpltxt "::"
  6342. (condition-case nil
  6343. (org-make-org-heading-search-string txt)
  6344. (error "")))
  6345. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6346. (if (string-match "::\\'" cpltxt)
  6347. (setq cpltxt (substring cpltxt 0 -2)))
  6348. (setq link (org-make-link cpltxt)))))
  6349. ((buffer-file-name (buffer-base-buffer))
  6350. ;; Just link to this file here.
  6351. (setq cpltxt (concat "file:"
  6352. (abbreviate-file-name
  6353. (buffer-file-name (buffer-base-buffer)))))
  6354. ;; Add a context string
  6355. (when (org-xor org-context-in-file-links arg)
  6356. (setq txt (if (org-region-active-p)
  6357. (buffer-substring (region-beginning) (region-end))
  6358. (buffer-substring (point-at-bol) (point-at-eol))))
  6359. ;; Only use search option if there is some text.
  6360. (when (string-match "\\S-" txt)
  6361. (setq cpltxt
  6362. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6363. desc "NONE")))
  6364. (setq link (org-make-link cpltxt)))
  6365. ((interactive-p)
  6366. (error "Cannot link to a buffer which is not visiting a file"))
  6367. (t (setq link nil)))
  6368. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6369. (setq link (or link cpltxt)
  6370. desc (or desc cpltxt))
  6371. (if (equal desc "NONE") (setq desc nil))
  6372. (if (and (or (interactive-p) executing-kbd-macro) link)
  6373. (progn
  6374. (setq org-stored-links
  6375. (cons (list link desc) org-stored-links))
  6376. (message "Stored: %s" (or desc link))
  6377. (when custom-id
  6378. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6379. "::#" custom-id))
  6380. (setq org-stored-links
  6381. (cons (list link desc) org-stored-links))))
  6382. (and link (org-make-link-string link desc)))))
  6383. (defun org-store-link-props (&rest plist)
  6384. "Store link properties, extract names and addresses."
  6385. (let (x adr)
  6386. (when (setq x (plist-get plist :from))
  6387. (setq adr (mail-extract-address-components x))
  6388. (setq plist (plist-put plist :fromname (car adr)))
  6389. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6390. (when (setq x (plist-get plist :to))
  6391. (setq adr (mail-extract-address-components x))
  6392. (setq plist (plist-put plist :toname (car adr)))
  6393. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6394. (let ((from (plist-get plist :from))
  6395. (to (plist-get plist :to)))
  6396. (when (and from to org-from-is-user-regexp)
  6397. (setq plist
  6398. (plist-put plist :fromto
  6399. (if (string-match org-from-is-user-regexp from)
  6400. (concat "to %t")
  6401. (concat "from %f"))))))
  6402. (setq org-store-link-plist plist))
  6403. (defun org-add-link-props (&rest plist)
  6404. "Add these properties to the link property list."
  6405. (let (key value)
  6406. (while plist
  6407. (setq key (pop plist) value (pop plist))
  6408. (setq org-store-link-plist
  6409. (plist-put org-store-link-plist key value)))))
  6410. (defun org-email-link-description (&optional fmt)
  6411. "Return the description part of an email link.
  6412. This takes information from `org-store-link-plist' and formats it
  6413. according to FMT (default from `org-email-link-description-format')."
  6414. (setq fmt (or fmt org-email-link-description-format))
  6415. (let* ((p org-store-link-plist)
  6416. (to (plist-get p :toaddress))
  6417. (from (plist-get p :fromaddress))
  6418. (table
  6419. (list
  6420. (cons "%c" (plist-get p :fromto))
  6421. (cons "%F" (plist-get p :from))
  6422. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6423. (cons "%T" (plist-get p :to))
  6424. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6425. (cons "%s" (plist-get p :subject))
  6426. (cons "%m" (plist-get p :message-id)))))
  6427. (when (string-match "%c" fmt)
  6428. ;; Check if the user wrote this message
  6429. (if (and org-from-is-user-regexp from to
  6430. (save-match-data (string-match org-from-is-user-regexp from)))
  6431. (setq fmt (replace-match "to %t" t t fmt))
  6432. (setq fmt (replace-match "from %f" t t fmt))))
  6433. (org-replace-escapes fmt table)))
  6434. (defun org-make-org-heading-search-string (&optional string heading)
  6435. "Make search string for STRING or current headline."
  6436. (interactive)
  6437. (let ((s (or string (org-get-heading))))
  6438. (unless (and string (not heading))
  6439. ;; We are using a headline, clean up garbage in there.
  6440. (if (string-match org-todo-regexp s)
  6441. (setq s (replace-match "" t t s)))
  6442. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6443. (setq s (replace-match "" t t s)))
  6444. (setq s (org-trim s))
  6445. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6446. org-comment-string "\\)") s)
  6447. (setq s (replace-match "" t t s)))
  6448. (while (string-match org-ts-regexp s)
  6449. (setq s (replace-match "" t t s))))
  6450. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6451. (setq s (replace-match " " t t s)))
  6452. (or string (setq s (concat "*" s))) ; Add * for headlines
  6453. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6454. (defun org-make-link (&rest strings)
  6455. "Concatenate STRINGS."
  6456. (apply 'concat strings))
  6457. (defun org-make-link-string (link &optional description)
  6458. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6459. (unless (string-match "\\S-" link)
  6460. (error "Empty link"))
  6461. (when (stringp description)
  6462. ;; Remove brackets from the description, they are fatal.
  6463. (while (string-match "\\[" description)
  6464. (setq description (replace-match "{" t t description)))
  6465. (while (string-match "\\]" description)
  6466. (setq description (replace-match "}" t t description))))
  6467. (when (equal (org-link-escape link) description)
  6468. ;; No description needed, it is identical
  6469. (setq description nil))
  6470. (when (and (not description)
  6471. (not (equal link (org-link-escape link))))
  6472. (setq description (org-extract-attributes link)))
  6473. (concat "[[" (org-link-escape link) "]"
  6474. (if description (concat "[" description "]") "")
  6475. "]"))
  6476. (defconst org-link-escape-chars
  6477. '((?\ . "%20")
  6478. (?\[ . "%5B")
  6479. (?\] . "%5D")
  6480. (?\340 . "%E0") ; `a
  6481. (?\342 . "%E2") ; ^a
  6482. (?\347 . "%E7") ; ,c
  6483. (?\350 . "%E8") ; `e
  6484. (?\351 . "%E9") ; 'e
  6485. (?\352 . "%EA") ; ^e
  6486. (?\356 . "%EE") ; ^i
  6487. (?\364 . "%F4") ; ^o
  6488. (?\371 . "%F9") ; `u
  6489. (?\373 . "%FB") ; ^u
  6490. (?\; . "%3B")
  6491. (?? . "%3F")
  6492. (?= . "%3D")
  6493. (?+ . "%2B")
  6494. )
  6495. "Association list of escapes for some characters problematic in links.
  6496. This is the list that is used for internal purposes.")
  6497. (defvar org-url-encoding-use-url-hexify nil)
  6498. (defconst org-link-escape-chars-browser
  6499. '((?\ . "%20")) ; 32 for the SPC char
  6500. "Association list of escapes for some characters problematic in links.
  6501. This is the list that is used before handing over to the browser.")
  6502. (defun org-link-escape (text &optional table)
  6503. "Escape characters in TEXT that are problematic for links."
  6504. (if org-url-encoding-use-url-hexify
  6505. (url-hexify-string text)
  6506. (setq table (or table org-link-escape-chars))
  6507. (when text
  6508. (let ((re (mapconcat (lambda (x) (regexp-quote
  6509. (char-to-string (car x))))
  6510. table "\\|")))
  6511. (while (string-match re text)
  6512. (setq text
  6513. (replace-match
  6514. (cdr (assoc (string-to-char (match-string 0 text))
  6515. table))
  6516. t t text)))
  6517. text))))
  6518. (defun org-link-unescape (text &optional table)
  6519. "Reverse the action of `org-link-escape'."
  6520. (if org-url-encoding-use-url-hexify
  6521. (url-unhex-string text)
  6522. (setq table (or table org-link-escape-chars))
  6523. (when text
  6524. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6525. table "\\|")))
  6526. (while (string-match re text)
  6527. (setq text
  6528. (replace-match
  6529. (char-to-string (car (rassoc (match-string 0 text) table)))
  6530. t t text)))
  6531. text))))
  6532. (defun org-xor (a b)
  6533. "Exclusive or."
  6534. (if a (not b) b))
  6535. (defun org-fixup-message-id-for-http (s)
  6536. "Replace special characters in a message id, so it can be used in an http query."
  6537. (while (string-match "<" s)
  6538. (setq s (replace-match "%3C" t t s)))
  6539. (while (string-match ">" s)
  6540. (setq s (replace-match "%3E" t t s)))
  6541. (while (string-match "@" s)
  6542. (setq s (replace-match "%40" t t s)))
  6543. s)
  6544. ;;;###autoload
  6545. (defun org-insert-link-global ()
  6546. "Insert a link like Org-mode does.
  6547. This command can be called in any mode to insert a link in Org-mode syntax."
  6548. (interactive)
  6549. (org-load-modules-maybe)
  6550. (org-run-like-in-org-mode 'org-insert-link))
  6551. (defun org-insert-link (&optional complete-file link-location)
  6552. "Insert a link. At the prompt, enter the link.
  6553. Completion can be used to insert any of the link protocol prefixes like
  6554. http or ftp in use.
  6555. The history can be used to select a link previously stored with
  6556. `org-store-link'. When the empty string is entered (i.e. if you just
  6557. press RET at the prompt), the link defaults to the most recently
  6558. stored link. As SPC triggers completion in the minibuffer, you need to
  6559. use M-SPC or C-q SPC to force the insertion of a space character.
  6560. You will also be prompted for a description, and if one is given, it will
  6561. be displayed in the buffer instead of the link.
  6562. If there is already a link at point, this command will allow you to edit link
  6563. and description parts.
  6564. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6565. be selected using completion. The path to the file will be relative to the
  6566. current directory if the file is in the current directory or a subdirectory.
  6567. Otherwise, the link will be the absolute path as completed in the minibuffer
  6568. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6569. option `org-link-file-path-type'.
  6570. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6571. the current directory or below.
  6572. With three \\[universal-argument] prefixes, negate the meaning of
  6573. `org-keep-stored-link-after-insertion'.
  6574. If `org-make-link-description-function' is non-nil, this function will be
  6575. called with the link target, and the result will be the default
  6576. link description.
  6577. If the LINK-LOCATION parameter is non-nil, this value will be
  6578. used as the link location instead of reading one interactively."
  6579. (interactive "P")
  6580. (let* ((wcf (current-window-configuration))
  6581. (region (if (org-region-active-p)
  6582. (buffer-substring (region-beginning) (region-end))))
  6583. (remove (and region (list (region-beginning) (region-end))))
  6584. (desc region)
  6585. tmphist ; byte-compile incorrectly complains about this
  6586. (link link-location)
  6587. entry file all-prefixes)
  6588. (cond
  6589. (link-location) ; specified by arg, just use it.
  6590. ((org-in-regexp org-bracket-link-regexp 1)
  6591. ;; We do have a link at point, and we are going to edit it.
  6592. (setq remove (list (match-beginning 0) (match-end 0)))
  6593. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6594. (setq link (read-string "Link: "
  6595. (org-link-unescape
  6596. (org-match-string-no-properties 1)))))
  6597. ((or (org-in-regexp org-angle-link-re)
  6598. (org-in-regexp org-plain-link-re))
  6599. ;; Convert to bracket link
  6600. (setq remove (list (match-beginning 0) (match-end 0))
  6601. link (read-string "Link: "
  6602. (org-remove-angle-brackets (match-string 0)))))
  6603. ((member complete-file '((4) (16)))
  6604. ;; Completing read for file names.
  6605. (setq link (org-file-complete-link complete-file)))
  6606. (t
  6607. ;; Read link, with completion for stored links.
  6608. (with-output-to-temp-buffer "*Org Links*"
  6609. (princ "Insert a link.
  6610. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6611. (when org-stored-links
  6612. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6613. (princ (mapconcat
  6614. (lambda (x)
  6615. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6616. (reverse org-stored-links) "\n"))))
  6617. (let ((cw (selected-window)))
  6618. (select-window (get-buffer-window "*Org Links*"))
  6619. (setq truncate-lines t)
  6620. (unless (pos-visible-in-window-p (point-max))
  6621. (org-fit-window-to-buffer))
  6622. (and (window-live-p cw) (select-window cw)))
  6623. ;; Fake a link history, containing the stored links.
  6624. (setq tmphist (append (mapcar 'car org-stored-links)
  6625. org-insert-link-history))
  6626. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6627. (mapcar 'car org-link-abbrev-alist)
  6628. org-link-types))
  6629. (unwind-protect
  6630. (progn
  6631. (setq link
  6632. (let ((org-completion-use-ido nil))
  6633. (org-completing-read
  6634. "Link: "
  6635. (append
  6636. (mapcar (lambda (x) (list (concat x ":")))
  6637. all-prefixes)
  6638. (mapcar 'car org-stored-links))
  6639. nil nil nil
  6640. 'tmphist
  6641. (car (car org-stored-links)))))
  6642. (if (or (member link all-prefixes)
  6643. (and (equal ":" (substring link -1))
  6644. (member (substring link 0 -1) all-prefixes)
  6645. (setq link (substring link 0 -1))))
  6646. (setq link (org-link-try-special-completion link))))
  6647. (set-window-configuration wcf)
  6648. (kill-buffer "*Org Links*"))
  6649. (setq entry (assoc link org-stored-links))
  6650. (or entry (push link org-insert-link-history))
  6651. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6652. (not org-keep-stored-link-after-insertion))
  6653. (setq org-stored-links (delq (assoc link org-stored-links)
  6654. org-stored-links)))
  6655. (setq desc (or desc (nth 1 entry)))))
  6656. (if (string-match org-plain-link-re link)
  6657. ;; URL-like link, normalize the use of angular brackets.
  6658. (setq link (org-make-link (org-remove-angle-brackets link))))
  6659. ;; Check if we are linking to the current file with a search option
  6660. ;; If yes, simplify the link by using only the search option.
  6661. (when (and buffer-file-name
  6662. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6663. (let* ((path (match-string 1 link))
  6664. (case-fold-search nil)
  6665. (search (match-string 2 link)))
  6666. (save-match-data
  6667. (if (equal (file-truename buffer-file-name) (file-truename path))
  6668. ;; We are linking to this same file, with a search option
  6669. (setq link search)))))
  6670. ;; Check if we can/should use a relative path. If yes, simplify the link
  6671. (when (string-match "^file:\\(.*\\)" link)
  6672. (let* ((path (match-string 1 link))
  6673. (origpath path)
  6674. (case-fold-search nil))
  6675. (cond
  6676. ((or (eq org-link-file-path-type 'absolute)
  6677. (equal complete-file '(16)))
  6678. (setq path (abbreviate-file-name (expand-file-name path))))
  6679. ((eq org-link-file-path-type 'noabbrev)
  6680. (setq path (expand-file-name path)))
  6681. ((eq org-link-file-path-type 'relative)
  6682. (setq path (file-relative-name path)))
  6683. (t
  6684. (save-match-data
  6685. (if (string-match (concat "^" (regexp-quote
  6686. (file-name-as-directory
  6687. (expand-file-name "."))))
  6688. (expand-file-name path))
  6689. ;; We are linking a file with relative path name.
  6690. (setq path (substring (expand-file-name path)
  6691. (match-end 0)))
  6692. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6693. (setq link (concat "file:" path))
  6694. (if (equal desc origpath)
  6695. (setq desc path))))
  6696. (if org-make-link-description-function
  6697. (setq desc (funcall org-make-link-description-function link desc)))
  6698. (setq desc (read-string "Description: " desc))
  6699. (unless (string-match "\\S-" desc) (setq desc nil))
  6700. (if remove (apply 'delete-region remove))
  6701. (insert (org-make-link-string link desc))))
  6702. (defun org-link-try-special-completion (type)
  6703. "If there is completion support for link type TYPE, offer it."
  6704. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6705. (if (functionp fun)
  6706. (funcall fun)
  6707. (read-string "Link (no completion support): " (concat type ":")))))
  6708. (defun org-file-complete-link (&optional arg)
  6709. "Create a file link using completion."
  6710. (let (file link)
  6711. (setq file (read-file-name "File: "))
  6712. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6713. (pwd1 (file-name-as-directory (abbreviate-file-name
  6714. (expand-file-name ".")))))
  6715. (cond
  6716. ((equal arg '(16))
  6717. (setq link (org-make-link
  6718. "file:"
  6719. (abbreviate-file-name (expand-file-name file)))))
  6720. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6721. (setq link (org-make-link "file:" (match-string 1 file))))
  6722. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6723. (expand-file-name file))
  6724. (setq link (org-make-link
  6725. "file:" (match-string 1 (expand-file-name file)))))
  6726. (t (setq link (org-make-link "file:" file)))))
  6727. link))
  6728. (defun org-completing-read (&rest args)
  6729. "Completing-read with SPACE being a normal character."
  6730. (let ((minibuffer-local-completion-map
  6731. (copy-keymap minibuffer-local-completion-map)))
  6732. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6733. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6734. (apply 'org-ido-completing-read args)))
  6735. (defun org-completing-read-no-ido (&rest args)
  6736. (let (org-completion-use-ido)
  6737. (apply 'org-completing-read args)))
  6738. (defun org-ido-completing-read (&rest args)
  6739. "Completing-read using `ido-mode' speedups if available"
  6740. (if (and org-completion-use-ido
  6741. (fboundp 'ido-completing-read)
  6742. (boundp 'ido-mode) ido-mode
  6743. (listp (second args)))
  6744. (let ((ido-enter-matching-directory nil))
  6745. (apply 'ido-completing-read (concat (car args))
  6746. (mapcar (lambda (x) (car x)) (nth 1 args))
  6747. (cddr args)))
  6748. (apply 'completing-read args)))
  6749. (defun org-extract-attributes (s)
  6750. "Extract the attributes cookie from a string and set as text property."
  6751. (let (a attr (start 0) key value)
  6752. (save-match-data
  6753. (when (string-match "{{\\([^}]+\\)}}$" s)
  6754. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6755. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6756. (setq key (match-string 1 a) value (match-string 2 a)
  6757. start (match-end 0)
  6758. attr (plist-put attr (intern key) value))))
  6759. (org-add-props s nil 'org-attr attr))
  6760. s))
  6761. (defun org-extract-attributes-from-string (tag)
  6762. (let (key value attr)
  6763. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6764. (setq key (match-string 1 tag) value (match-string 2 tag)
  6765. tag (replace-match "" t t tag)
  6766. attr (plist-put attr (intern key) value)))
  6767. (cons tag attr)))
  6768. (defun org-attributes-to-string (plist)
  6769. "Format a property list into an HTML attribute list."
  6770. (let ((s "") key value)
  6771. (while plist
  6772. (setq key (pop plist) value (pop plist))
  6773. (and value
  6774. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6775. s))
  6776. ;;; Opening/following a link
  6777. (defvar org-link-search-failed nil)
  6778. (defun org-next-link ()
  6779. "Move forward to the next link.
  6780. If the link is in hidden text, expose it."
  6781. (interactive)
  6782. (when (and org-link-search-failed (eq this-command last-command))
  6783. (goto-char (point-min))
  6784. (message "Link search wrapped back to beginning of buffer"))
  6785. (setq org-link-search-failed nil)
  6786. (let* ((pos (point))
  6787. (ct (org-context))
  6788. (a (assoc :link ct)))
  6789. (if a (goto-char (nth 2 a)))
  6790. (if (re-search-forward org-any-link-re nil t)
  6791. (progn
  6792. (goto-char (match-beginning 0))
  6793. (if (org-invisible-p) (org-show-context)))
  6794. (goto-char pos)
  6795. (setq org-link-search-failed t)
  6796. (error "No further link found"))))
  6797. (defun org-previous-link ()
  6798. "Move backward to the previous link.
  6799. If the link is in hidden text, expose it."
  6800. (interactive)
  6801. (when (and org-link-search-failed (eq this-command last-command))
  6802. (goto-char (point-max))
  6803. (message "Link search wrapped back to end of buffer"))
  6804. (setq org-link-search-failed nil)
  6805. (let* ((pos (point))
  6806. (ct (org-context))
  6807. (a (assoc :link ct)))
  6808. (if a (goto-char (nth 1 a)))
  6809. (if (re-search-backward org-any-link-re nil t)
  6810. (progn
  6811. (goto-char (match-beginning 0))
  6812. (if (org-invisible-p) (org-show-context)))
  6813. (goto-char pos)
  6814. (setq org-link-search-failed t)
  6815. (error "No further link found"))))
  6816. (defun org-translate-link (s)
  6817. "Translate a link string if a translation function has been defined."
  6818. (if (and org-link-translation-function
  6819. (fboundp org-link-translation-function)
  6820. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6821. (progn
  6822. (setq s (funcall org-link-translation-function
  6823. (match-string 1) (match-string 2)))
  6824. (concat (car s) ":" (cdr s)))
  6825. s))
  6826. (defun org-translate-link-from-planner (type path)
  6827. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6828. This is still an experimental function, your mileage may vary."
  6829. (cond
  6830. ((member type '("http" "https" "news" "ftp"))
  6831. ;; standard Internet links are the same.
  6832. nil)
  6833. ((and (equal type "irc") (string-match "^//" path))
  6834. ;; Planner has two / at the beginning of an irc link, we have 1.
  6835. ;; We should have zero, actually....
  6836. (setq path (substring path 1)))
  6837. ((and (equal type "lisp") (string-match "^/" path))
  6838. ;; Planner has a slash, we do not.
  6839. (setq type "elisp" path (substring path 1)))
  6840. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6841. ;; A typical message link. Planner has the id after the fina slash,
  6842. ;; we separate it with a hash mark
  6843. (setq path (concat (match-string 1 path) "#"
  6844. (org-remove-angle-brackets (match-string 2 path)))))
  6845. )
  6846. (cons type path))
  6847. (defun org-find-file-at-mouse (ev)
  6848. "Open file link or URL at mouse."
  6849. (interactive "e")
  6850. (mouse-set-point ev)
  6851. (org-open-at-point 'in-emacs))
  6852. (defun org-open-at-mouse (ev)
  6853. "Open file link or URL at mouse."
  6854. (interactive "e")
  6855. (mouse-set-point ev)
  6856. (if (eq major-mode 'org-agenda-mode)
  6857. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6858. (org-open-at-point))
  6859. (defvar org-window-config-before-follow-link nil
  6860. "The window configuration before following a link.
  6861. This is saved in case the need arises to restore it.")
  6862. (defvar org-open-link-marker (make-marker)
  6863. "Marker pointing to the location where `org-open-at-point; was called.")
  6864. ;;;###autoload
  6865. (defun org-open-at-point-global ()
  6866. "Follow a link like Org-mode does.
  6867. This command can be called in any mode to follow a link that has
  6868. Org-mode syntax."
  6869. (interactive)
  6870. (org-run-like-in-org-mode 'org-open-at-point))
  6871. ;;;###autoload
  6872. (defun org-open-link-from-string (s &optional arg)
  6873. "Open a link in the string S, as if it was in Org-mode."
  6874. (interactive "sLink: \nP")
  6875. (let ((reference-buffer (current-buffer)))
  6876. (with-temp-buffer
  6877. (let ((org-inhibit-startup t))
  6878. (org-mode)
  6879. (insert s)
  6880. (goto-char (point-min))
  6881. (org-open-at-point arg reference-buffer)))))
  6882. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6883. "Open link at or after point.
  6884. If there is no link at point, this function will search forward up to
  6885. the end of the current line.
  6886. Normally, files will be opened by an appropriate application. If the
  6887. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6888. With a double prefix argument, try to open outside of Emacs, in the
  6889. application the system uses for this file type."
  6890. (interactive "P")
  6891. (org-load-modules-maybe)
  6892. (move-marker org-open-link-marker (point))
  6893. (setq org-window-config-before-follow-link (current-window-configuration))
  6894. (org-remove-occur-highlights nil nil t)
  6895. (cond
  6896. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6897. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6898. (org-footnote-action))
  6899. (t
  6900. (let (type path link line search (pos (point)))
  6901. (catch 'match
  6902. (save-excursion
  6903. (skip-chars-forward "^]\n\r")
  6904. (when (org-in-regexp org-bracket-link-regexp)
  6905. (setq link (org-extract-attributes
  6906. (org-link-unescape (org-match-string-no-properties 1))))
  6907. (while (string-match " *\n *" link)
  6908. (setq link (replace-match " " t t link)))
  6909. (setq link (org-link-expand-abbrev link))
  6910. (cond
  6911. ((or (file-name-absolute-p link)
  6912. (string-match "^\\.\\.?/" link))
  6913. (setq type "file" path link))
  6914. ((string-match org-link-re-with-space3 link)
  6915. (setq type (match-string 1 link) path (match-string 2 link)))
  6916. (t (setq type "thisfile" path link)))
  6917. (throw 'match t)))
  6918. (when (get-text-property (point) 'org-linked-text)
  6919. (setq type "thisfile"
  6920. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6921. (1+ (point)) (point))
  6922. path (buffer-substring
  6923. (previous-single-property-change pos 'org-linked-text)
  6924. (next-single-property-change pos 'org-linked-text)))
  6925. (throw 'match t))
  6926. (save-excursion
  6927. (when (or (org-in-regexp org-angle-link-re)
  6928. (org-in-regexp org-plain-link-re))
  6929. (setq type (match-string 1) path (match-string 2))
  6930. (throw 'match t)))
  6931. (save-excursion
  6932. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6933. (setq type "tags"
  6934. path (match-string 1))
  6935. (while (string-match ":" path)
  6936. (setq path (replace-match "+" t t path)))
  6937. (throw 'match t)))
  6938. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6939. (setq type "tree-match"
  6940. path (match-string 1))
  6941. (throw 'match t)))
  6942. (unless path
  6943. (error "No link found"))
  6944. ;; switch back to reference buffer
  6945. ;; needed when if called in a temporary buffer through
  6946. ;; org-open-link-from-string
  6947. (and reference-buffer (switch-to-buffer reference-buffer))
  6948. ;; Remove any trailing spaces in path
  6949. (if (string-match " +\\'" path)
  6950. (setq path (replace-match "" t t path)))
  6951. (if (and org-link-translation-function
  6952. (fboundp org-link-translation-function))
  6953. ;; Check if we need to translate the link
  6954. (let ((tmp (funcall org-link-translation-function type path)))
  6955. (setq type (car tmp) path (cdr tmp))))
  6956. (cond
  6957. ((assoc type org-link-protocols)
  6958. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6959. ((equal type "mailto")
  6960. (let ((cmd (car org-link-mailto-program))
  6961. (args (cdr org-link-mailto-program)) args1
  6962. (address path) (subject "") a)
  6963. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6964. (setq address (match-string 1 path)
  6965. subject (org-link-escape (match-string 2 path))))
  6966. (while args
  6967. (cond
  6968. ((not (stringp (car args))) (push (pop args) args1))
  6969. (t (setq a (pop args))
  6970. (if (string-match "%a" a)
  6971. (setq a (replace-match address t t a)))
  6972. (if (string-match "%s" a)
  6973. (setq a (replace-match subject t t a)))
  6974. (push a args1))))
  6975. (apply cmd (nreverse args1))))
  6976. ((member type '("http" "https" "ftp" "news"))
  6977. (browse-url (concat type ":" (org-link-escape
  6978. path org-link-escape-chars-browser))))
  6979. ((member type '("message"))
  6980. (browse-url (concat type ":" path)))
  6981. ((string= type "tags")
  6982. (org-tags-view in-emacs path))
  6983. ((string= type "thisfile")
  6984. (if in-emacs
  6985. (switch-to-buffer-other-window
  6986. (org-get-buffer-for-internal-link (current-buffer)))
  6987. (org-mark-ring-push))
  6988. (let ((cmd `(org-link-search
  6989. ,path
  6990. ,(cond ((equal in-emacs '(4)) 'occur)
  6991. ((equal in-emacs '(16)) 'org-occur)
  6992. (t nil))
  6993. ,pos)))
  6994. (condition-case nil (eval cmd)
  6995. (error (progn (widen) (eval cmd))))))
  6996. ((string= type "tree-match")
  6997. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6998. ((string= type "file")
  6999. (if (string-match "::\\([0-9]+\\)\\'" path)
  7000. (setq line (string-to-number (match-string 1 path))
  7001. path (substring path 0 (match-beginning 0)))
  7002. (if (string-match "::\\(.+\\)\\'" path)
  7003. (setq search (match-string 1 path)
  7004. path (substring path 0 (match-beginning 0)))))
  7005. (if (string-match "[*?{]" (file-name-nondirectory path))
  7006. (dired path)
  7007. (org-open-file path in-emacs line search)))
  7008. ((string= type "news")
  7009. (require 'org-gnus)
  7010. (org-gnus-follow-link path))
  7011. ((string= type "shell")
  7012. (let ((cmd path))
  7013. (if (or (not org-confirm-shell-link-function)
  7014. (funcall org-confirm-shell-link-function
  7015. (format "Execute \"%s\" in shell? "
  7016. (org-add-props cmd nil
  7017. 'face 'org-warning))))
  7018. (progn
  7019. (message "Executing %s" cmd)
  7020. (shell-command cmd))
  7021. (error "Abort"))))
  7022. ((string= type "elisp")
  7023. (let ((cmd path))
  7024. (if (or (not org-confirm-elisp-link-function)
  7025. (funcall org-confirm-elisp-link-function
  7026. (format "Execute \"%s\" as elisp? "
  7027. (org-add-props cmd nil
  7028. 'face 'org-warning))))
  7029. (message "%s => %s" cmd
  7030. (if (equal (string-to-char cmd) ?\()
  7031. (eval (read cmd))
  7032. (call-interactively (read cmd))))
  7033. (error "Abort"))))
  7034. (t
  7035. (browse-url-at-point))))))
  7036. (move-marker org-open-link-marker nil)
  7037. (run-hook-with-args 'org-follow-link-hook))
  7038. ;;;; Time estimates
  7039. (defun org-get-effort (&optional pom)
  7040. "Get the effort estimate for the current entry."
  7041. (org-entry-get pom org-effort-property))
  7042. ;;; File search
  7043. (defvar org-create-file-search-functions nil
  7044. "List of functions to construct the right search string for a file link.
  7045. These functions are called in turn with point at the location to
  7046. which the link should point.
  7047. A function in the hook should first test if it would like to
  7048. handle this file type, for example by checking the major-mode or
  7049. the file extension. If it decides not to handle this file, it
  7050. should just return nil to give other functions a chance. If it
  7051. does handle the file, it must return the search string to be used
  7052. when following the link. The search string will be part of the
  7053. file link, given after a double colon, and `org-open-at-point'
  7054. will automatically search for it. If special measures must be
  7055. taken to make the search successful, another function should be
  7056. added to the companion hook `org-execute-file-search-functions',
  7057. which see.
  7058. A function in this hook may also use `setq' to set the variable
  7059. `description' to provide a suggestion for the descriptive text to
  7060. be used for this link when it gets inserted into an Org-mode
  7061. buffer with \\[org-insert-link].")
  7062. (defvar org-execute-file-search-functions nil
  7063. "List of functions to execute a file search triggered by a link.
  7064. Functions added to this hook must accept a single argument, the
  7065. search string that was part of the file link, the part after the
  7066. double colon. The function must first check if it would like to
  7067. handle this search, for example by checking the major-mode or the
  7068. file extension. If it decides not to handle this search, it
  7069. should just return nil to give other functions a chance. If it
  7070. does handle the search, it must return a non-nil value to keep
  7071. other functions from trying.
  7072. Each function can access the current prefix argument through the
  7073. variable `current-prefix-argument'. Note that a single prefix is
  7074. used to force opening a link in Emacs, so it may be good to only
  7075. use a numeric or double prefix to guide the search function.
  7076. In case this is needed, a function in this hook can also restore
  7077. the window configuration before `org-open-at-point' was called using:
  7078. (set-window-configuration org-window-config-before-follow-link)")
  7079. (defun org-link-search (s &optional type avoid-pos)
  7080. "Search for a link search option.
  7081. If S is surrounded by forward slashes, it is interpreted as a
  7082. regular expression. In org-mode files, this will create an `org-occur'
  7083. sparse tree. In ordinary files, `occur' will be used to list matches.
  7084. If the current buffer is in `dired-mode', grep will be used to search
  7085. in all files. If AVOID-POS is given, ignore matches near that position."
  7086. (let ((case-fold-search t)
  7087. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7088. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7089. (append '(("") (" ") ("\t") ("\n"))
  7090. org-emphasis-alist)
  7091. "\\|") "\\)"))
  7092. (pos (point))
  7093. (pre nil) (post nil)
  7094. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7095. (cond
  7096. ;; First check if there are any special
  7097. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7098. ;; Now try the builtin stuff
  7099. ((and (equal (string-to-char s0) ?#)
  7100. (> (length s0) 1)
  7101. (save-excursion
  7102. (goto-char (point-min))
  7103. (and
  7104. (re-search-forward
  7105. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7106. (setq type 'dedicated
  7107. pos (match-beginning 0))))
  7108. ;; There is an exact target for this
  7109. (goto-char pos)
  7110. (org-back-to-heading t)))
  7111. ((save-excursion
  7112. (goto-char (point-min))
  7113. (and
  7114. (re-search-forward
  7115. (concat "<<" (regexp-quote s0) ">>") nil t)
  7116. (setq type 'dedicated
  7117. pos (match-beginning 0))))
  7118. ;; There is an exact target for this
  7119. (goto-char pos))
  7120. ((and (string-match "^(\\(.*\\))$" s0)
  7121. (save-excursion
  7122. (goto-char (point-min))
  7123. (and
  7124. (re-search-forward
  7125. (concat "[^[]" (regexp-quote
  7126. (format org-coderef-label-format
  7127. (match-string 1 s0))))
  7128. nil t)
  7129. (setq type 'dedicated
  7130. pos (1+ (match-beginning 0))))))
  7131. ;; There is a coderef target for this
  7132. (goto-char pos))
  7133. ((string-match "^/\\(.*\\)/$" s)
  7134. ;; A regular expression
  7135. (cond
  7136. ((org-mode-p)
  7137. (org-occur (match-string 1 s)))
  7138. ;;((eq major-mode 'dired-mode)
  7139. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7140. (t (org-do-occur (match-string 1 s)))))
  7141. (t
  7142. ;; A normal search strings
  7143. (when (equal (string-to-char s) ?*)
  7144. ;; Anchor on headlines, post may include tags.
  7145. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7146. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7147. s (substring s 1)))
  7148. (remove-text-properties
  7149. 0 (length s)
  7150. '(face nil mouse-face nil keymap nil fontified nil) s)
  7151. ;; Make a series of regular expressions to find a match
  7152. (setq words (org-split-string s "[ \n\r\t]+")
  7153. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7154. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7155. "\\)" markers)
  7156. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7157. re2a (concat "[ \t\r\n]" re2a_)
  7158. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7159. re4 (concat "[^a-zA-Z_]" re4_)
  7160. re1 (concat pre re2 post)
  7161. re3 (concat pre (if pre re4_ re4) post)
  7162. re5 (concat pre ".*" re4)
  7163. re2 (concat pre re2)
  7164. re2a (concat pre (if pre re2a_ re2a))
  7165. re4 (concat pre (if pre re4_ re4))
  7166. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7167. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7168. re5 "\\)"
  7169. ))
  7170. (cond
  7171. ((eq type 'org-occur) (org-occur reall))
  7172. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7173. (t (goto-char (point-min))
  7174. (setq type 'fuzzy)
  7175. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7176. (org-search-not-self 1 re1 nil t)
  7177. (org-search-not-self 1 re2 nil t)
  7178. (org-search-not-self 1 re2a nil t)
  7179. (org-search-not-self 1 re3 nil t)
  7180. (org-search-not-self 1 re4 nil t)
  7181. (org-search-not-self 1 re5 nil t)
  7182. )
  7183. (goto-char (match-beginning 1))
  7184. (goto-char pos)
  7185. (error "No match")))))
  7186. (t
  7187. ;; Normal string-search
  7188. (goto-char (point-min))
  7189. (if (search-forward s nil t)
  7190. (goto-char (match-beginning 0))
  7191. (error "No match"))))
  7192. (and (org-mode-p) (org-show-context 'link-search))
  7193. type))
  7194. (defun org-search-not-self (group &rest args)
  7195. "Execute `re-search-forward', but only accept matches that do not
  7196. enclose the position of `org-open-link-marker'."
  7197. (let ((m org-open-link-marker))
  7198. (catch 'exit
  7199. (while (apply 're-search-forward args)
  7200. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7201. (goto-char (match-end group))
  7202. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7203. (> (match-beginning 0) (marker-position m))
  7204. (< (match-end 0) (marker-position m)))
  7205. (save-match-data
  7206. (or (not (org-in-regexp
  7207. org-bracket-link-analytic-regexp 1))
  7208. (not (match-end 4)) ; no description
  7209. (and (<= (match-beginning 4) (point))
  7210. (>= (match-end 4) (point))))))
  7211. (throw 'exit (point))))))))
  7212. (defun org-get-buffer-for-internal-link (buffer)
  7213. "Return a buffer to be used for displaying the link target of internal links."
  7214. (cond
  7215. ((not org-display-internal-link-with-indirect-buffer)
  7216. buffer)
  7217. ((string-match "(Clone)$" (buffer-name buffer))
  7218. (message "Buffer is already a clone, not making another one")
  7219. ;; we also do not modify visibility in this case
  7220. buffer)
  7221. (t ; make a new indirect buffer for displaying the link
  7222. (let* ((bn (buffer-name buffer))
  7223. (ibn (concat bn "(Clone)"))
  7224. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7225. (with-current-buffer ib (org-overview))
  7226. ib))))
  7227. (defun org-do-occur (regexp &optional cleanup)
  7228. "Call the Emacs command `occur'.
  7229. If CLEANUP is non-nil, remove the printout of the regular expression
  7230. in the *Occur* buffer. This is useful if the regex is long and not useful
  7231. to read."
  7232. (occur regexp)
  7233. (when cleanup
  7234. (let ((cwin (selected-window)) win beg end)
  7235. (when (setq win (get-buffer-window "*Occur*"))
  7236. (select-window win))
  7237. (goto-char (point-min))
  7238. (when (re-search-forward "match[a-z]+" nil t)
  7239. (setq beg (match-end 0))
  7240. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7241. (setq end (1- (match-beginning 0)))))
  7242. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7243. (goto-char (point-min))
  7244. (select-window cwin))))
  7245. ;;; The mark ring for links jumps
  7246. (defvar org-mark-ring nil
  7247. "Mark ring for positions before jumps in Org-mode.")
  7248. (defvar org-mark-ring-last-goto nil
  7249. "Last position in the mark ring used to go back.")
  7250. ;; Fill and close the ring
  7251. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7252. (loop for i from 1 to org-mark-ring-length do
  7253. (push (make-marker) org-mark-ring))
  7254. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7255. org-mark-ring)
  7256. (defun org-mark-ring-push (&optional pos buffer)
  7257. "Put the current position or POS into the mark ring and rotate it."
  7258. (interactive)
  7259. (setq pos (or pos (point)))
  7260. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7261. (move-marker (car org-mark-ring)
  7262. (or pos (point))
  7263. (or buffer (current-buffer)))
  7264. (message "%s"
  7265. (substitute-command-keys
  7266. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7267. (defun org-mark-ring-goto (&optional n)
  7268. "Jump to the previous position in the mark ring.
  7269. With prefix arg N, jump back that many stored positions. When
  7270. called several times in succession, walk through the entire ring.
  7271. Org-mode commands jumping to a different position in the current file,
  7272. or to another Org-mode file, automatically push the old position
  7273. onto the ring."
  7274. (interactive "p")
  7275. (let (p m)
  7276. (if (eq last-command this-command)
  7277. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7278. (setq p org-mark-ring))
  7279. (setq org-mark-ring-last-goto p)
  7280. (setq m (car p))
  7281. (switch-to-buffer (marker-buffer m))
  7282. (goto-char m)
  7283. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7284. (defun org-remove-angle-brackets (s)
  7285. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7286. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7287. s)
  7288. (defun org-add-angle-brackets (s)
  7289. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7290. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7291. s)
  7292. (defun org-remove-double-quotes (s)
  7293. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7294. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7295. s)
  7296. ;;; Following specific links
  7297. (defun org-follow-timestamp-link ()
  7298. (cond
  7299. ((org-at-date-range-p t)
  7300. (let ((org-agenda-start-on-weekday)
  7301. (t1 (match-string 1))
  7302. (t2 (match-string 2)))
  7303. (setq t1 (time-to-days (org-time-string-to-time t1))
  7304. t2 (time-to-days (org-time-string-to-time t2)))
  7305. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7306. ((org-at-timestamp-p t)
  7307. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7308. (substring (match-string 1) 0 10)))
  7309. 1))
  7310. (t (error "This should not happen"))))
  7311. ;;; Following file links
  7312. (defvar org-wait nil)
  7313. (defun org-open-file (path &optional in-emacs line search)
  7314. "Open the file at PATH.
  7315. First, this expands any special file name abbreviations. Then the
  7316. configuration variable `org-file-apps' is checked if it contains an
  7317. entry for this file type, and if yes, the corresponding command is launched.
  7318. If no application is found, Emacs simply visits the file.
  7319. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7320. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7321. and o use an external application to visit the file.
  7322. Optional LINE specifies a line to go to, optional SEARCH a string to
  7323. search for. If LINE or SEARCH is given, the file will always be
  7324. opened in Emacs.
  7325. If the file does not exist, an error is thrown."
  7326. (setq in-emacs (or in-emacs line search))
  7327. (let* ((file (if (equal path "")
  7328. buffer-file-name
  7329. (substitute-in-file-name (expand-file-name path))))
  7330. (apps (append org-file-apps (org-default-apps)))
  7331. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7332. (dirp (if remp nil (file-directory-p file)))
  7333. (file (if (and dirp org-open-directory-means-index-dot-org)
  7334. (concat (file-name-as-directory file) "index.org")
  7335. file))
  7336. (a-m-a-p (assq 'auto-mode apps))
  7337. (dfile (downcase file))
  7338. (old-buffer (current-buffer))
  7339. (old-pos (point))
  7340. (old-mode major-mode)
  7341. ext cmd)
  7342. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7343. (setq ext (match-string 1 dfile))
  7344. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7345. (setq ext (match-string 1 dfile))))
  7346. (cond
  7347. ((equal in-emacs '(16))
  7348. (setq cmd (cdr (assoc 'system apps))))
  7349. (in-emacs (setq cmd 'emacs))
  7350. (t
  7351. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7352. (and dirp (cdr (assoc 'directory apps)))
  7353. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7354. 'string-match)
  7355. (cdr (assoc ext apps))
  7356. (cdr (assoc t apps))))))
  7357. (when (eq cmd 'system)
  7358. (setq cmd (cdr (assoc 'system apps))))
  7359. (when (eq cmd 'default)
  7360. (setq cmd (cdr (assoc t apps))))
  7361. (when (eq cmd 'mailcap)
  7362. (require 'mailcap)
  7363. (mailcap-parse-mailcaps)
  7364. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7365. (command (mailcap-mime-info mime-type)))
  7366. (if (stringp command)
  7367. (setq cmd command)
  7368. (setq cmd 'emacs))))
  7369. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7370. (not (file-exists-p file))
  7371. (not org-open-non-existing-files))
  7372. (error "No such file: %s" file))
  7373. (cond
  7374. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7375. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7376. (while (string-match "['\"]%s['\"]" cmd)
  7377. (setq cmd (replace-match "%s" t t cmd)))
  7378. (while (string-match "%s" cmd)
  7379. (setq cmd (replace-match
  7380. (save-match-data
  7381. (shell-quote-argument
  7382. (convert-standard-filename file)))
  7383. t t cmd)))
  7384. (save-window-excursion
  7385. (start-process-shell-command cmd nil cmd)
  7386. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7387. ))
  7388. ((or (stringp cmd)
  7389. (eq cmd 'emacs))
  7390. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7391. (widen)
  7392. (if line (goto-line line)
  7393. (if search (org-link-search search))))
  7394. ((consp cmd)
  7395. (let ((file (convert-standard-filename file)))
  7396. (eval cmd)))
  7397. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7398. (and (org-mode-p) (eq old-mode 'org-mode)
  7399. (or (not (equal old-buffer (current-buffer)))
  7400. (not (equal old-pos (point))))
  7401. (org-mark-ring-push old-pos old-buffer))))
  7402. (defun org-default-apps ()
  7403. "Return the default applications for this operating system."
  7404. (cond
  7405. ((eq system-type 'darwin)
  7406. org-file-apps-defaults-macosx)
  7407. ((eq system-type 'windows-nt)
  7408. org-file-apps-defaults-windowsnt)
  7409. (t org-file-apps-defaults-gnu)))
  7410. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7411. "Convert extensions to regular expressions in the cars of LIST.
  7412. Also, weed out any non-string entries, because the return value is used
  7413. only for regexp matching.
  7414. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7415. point to the symbol `emacs', indicating that the file should
  7416. be opened in Emacs."
  7417. (append
  7418. (delq nil
  7419. (mapcar (lambda (x)
  7420. (if (not (stringp (car x)))
  7421. nil
  7422. (if (string-match "\\W" (car x))
  7423. x
  7424. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7425. list))
  7426. (if add-auto-mode
  7427. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7428. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7429. (defun org-file-remote-p (file)
  7430. "Test whether FILE specifies a location on a remote system.
  7431. Return non-nil if the location is indeed remote.
  7432. For example, the filename \"/user@host:/foo\" specifies a location
  7433. on the system \"/user@host:\"."
  7434. (cond ((fboundp 'file-remote-p)
  7435. (file-remote-p file))
  7436. ((fboundp 'tramp-handle-file-remote-p)
  7437. (tramp-handle-file-remote-p file))
  7438. ((and (boundp 'ange-ftp-name-format)
  7439. (string-match (car ange-ftp-name-format) file))
  7440. t)
  7441. (t nil)))
  7442. ;;;; Refiling
  7443. (defun org-get-org-file ()
  7444. "Read a filename, with default directory `org-directory'."
  7445. (let ((default (or org-default-notes-file remember-data-file)))
  7446. (read-file-name (format "File name [%s]: " default)
  7447. (file-name-as-directory org-directory)
  7448. default)))
  7449. (defun org-notes-order-reversed-p ()
  7450. "Check if the current file should receive notes in reversed order."
  7451. (cond
  7452. ((not org-reverse-note-order) nil)
  7453. ((eq t org-reverse-note-order) t)
  7454. ((not (listp org-reverse-note-order)) nil)
  7455. (t (catch 'exit
  7456. (let ((all org-reverse-note-order)
  7457. entry)
  7458. (while (setq entry (pop all))
  7459. (if (string-match (car entry) buffer-file-name)
  7460. (throw 'exit (cdr entry))))
  7461. nil)))))
  7462. (defvar org-refile-target-table nil
  7463. "The list of refile targets, created by `org-refile'.")
  7464. (defvar org-agenda-new-buffers nil
  7465. "Buffers created to visit agenda files.")
  7466. (defun org-get-refile-targets (&optional default-buffer)
  7467. "Produce a table with refile targets."
  7468. (let ((case-fold-search nil)
  7469. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7470. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7471. targets txt re files f desc descre fast-path-p level pos0)
  7472. (message "Getting targets...")
  7473. (with-current-buffer (or default-buffer (current-buffer))
  7474. (while (setq entry (pop entries))
  7475. (setq files (car entry) desc (cdr entry))
  7476. (setq fast-path-p nil)
  7477. (cond
  7478. ((null files) (setq files (list (current-buffer))))
  7479. ((eq files 'org-agenda-files)
  7480. (setq files (org-agenda-files 'unrestricted)))
  7481. ((and (symbolp files) (fboundp files))
  7482. (setq files (funcall files)))
  7483. ((and (symbolp files) (boundp files))
  7484. (setq files (symbol-value files))))
  7485. (if (stringp files) (setq files (list files)))
  7486. (cond
  7487. ((eq (car desc) :tag)
  7488. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7489. ((eq (car desc) :todo)
  7490. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7491. ((eq (car desc) :regexp)
  7492. (setq descre (cdr desc)))
  7493. ((eq (car desc) :level)
  7494. (setq descre (concat "^\\*\\{" (number-to-string
  7495. (if org-odd-levels-only
  7496. (1- (* 2 (cdr desc)))
  7497. (cdr desc)))
  7498. "\\}[ \t]")))
  7499. ((eq (car desc) :maxlevel)
  7500. (setq fast-path-p t)
  7501. (setq descre (concat "^\\*\\{1," (number-to-string
  7502. (if org-odd-levels-only
  7503. (1- (* 2 (cdr desc)))
  7504. (cdr desc)))
  7505. "\\}[ \t]")))
  7506. (t (error "Bad refiling target description %s" desc)))
  7507. (while (setq f (pop files))
  7508. (save-excursion
  7509. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7510. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7511. (setq f (expand-file-name f))
  7512. (if (eq org-refile-use-outline-path 'file)
  7513. (push (list (file-name-nondirectory f) f nil nil) targets))
  7514. (save-excursion
  7515. (save-restriction
  7516. (widen)
  7517. (goto-char (point-min))
  7518. (while (re-search-forward descre nil t)
  7519. (goto-char (setq pos0 (point-at-bol)))
  7520. (catch 'next
  7521. (when org-refile-target-verify-function
  7522. (save-match-data
  7523. (or (funcall org-refile-target-verify-function)
  7524. (throw 'next t))))
  7525. (when (looking-at org-complex-heading-regexp)
  7526. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7527. txt (org-link-display-format (match-string 4))
  7528. re (concat "^" (regexp-quote
  7529. (buffer-substring (match-beginning 1)
  7530. (match-end 4)))))
  7531. (if (match-end 5) (setq re (concat re "[ \t]+"
  7532. (regexp-quote
  7533. (match-string 5)))))
  7534. (setq re (concat re "[ \t]*$"))
  7535. (when org-refile-use-outline-path
  7536. (setq txt (mapconcat 'org-protect-slash
  7537. (append
  7538. (if (eq org-refile-use-outline-path 'file)
  7539. (list (file-name-nondirectory
  7540. (buffer-file-name (buffer-base-buffer))))
  7541. (if (eq org-refile-use-outline-path 'full-file-path)
  7542. (list (buffer-file-name (buffer-base-buffer)))))
  7543. (org-get-outline-path fast-path-p level txt)
  7544. (list txt))
  7545. "/")))
  7546. (push (list txt f re (point)) targets)))
  7547. (when (= (point) pos0)
  7548. ;; verification function has not moved point
  7549. (goto-char (point-at-eol))))))))))
  7550. (message "Getting targets...done")
  7551. (nreverse targets)))
  7552. (defun org-protect-slash (s)
  7553. (while (string-match "/" s)
  7554. (setq s (replace-match "\\" t t s)))
  7555. s)
  7556. (defvar org-olpa (make-vector 20 nil))
  7557. (defun org-get-outline-path (&optional fastp level heading)
  7558. "Return the outline path to the current entry, as a list."
  7559. (if fastp
  7560. (progn
  7561. (if (> level 19)
  7562. (error "Outline path failure, more than 19 levels."))
  7563. (loop for i from level upto 19 do
  7564. (aset org-olpa i nil))
  7565. (prog1
  7566. (delq nil (append org-olpa nil))
  7567. (aset org-olpa level heading)))
  7568. (let (rtn)
  7569. (save-excursion
  7570. (while (org-up-heading-safe)
  7571. (when (looking-at org-complex-heading-regexp)
  7572. (push (org-match-string-no-properties 4) rtn)))
  7573. rtn))))
  7574. (defvar org-refile-history nil
  7575. "History for refiling operations.")
  7576. (defvar org-after-refile-insert-hook nil
  7577. "Hook run after `org-refile' has inserted its stuff at the new location.
  7578. Note that this is still *before* the stuff will be removed from
  7579. the *old* location.")
  7580. (defun org-refile (&optional goto default-buffer rfloc)
  7581. "Move the entry at point to another heading.
  7582. The list of target headings is compiled using the information in
  7583. `org-refile-targets', which see. This list is created before each use
  7584. and will therefore always be up-to-date.
  7585. At the target location, the entry is filed as a subitem of the target heading.
  7586. Depending on `org-reverse-note-order', the new subitem will either be the
  7587. first or the last subitem.
  7588. If there is an active region, all entries in that region will be moved.
  7589. However, the region must fulfil the requirement that the first heading
  7590. is the first one sets the top-level of the moved text - at most siblings
  7591. below it are allowed.
  7592. With prefix arg GOTO, the command will only visit the target location,
  7593. not actually move anything.
  7594. With a double prefix `C-u C-u', go to the location where the last refiling
  7595. operation has put the subtree.
  7596. RFLOC can be a refile location obtained in a different way.
  7597. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7598. (interactive "P")
  7599. (let* ((cbuf (current-buffer))
  7600. (regionp (org-region-active-p))
  7601. (region-start (and regionp (region-beginning)))
  7602. (region-end (and regionp (region-end)))
  7603. (region-length (and regionp (- region-end region-start)))
  7604. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7605. pos it nbuf file re level reversed)
  7606. (when regionp (goto-char region-start)
  7607. (unless (org-kill-is-subtree-p
  7608. (buffer-substring region-start region-end))
  7609. (error "The region is not a (sequence of) subtree(s)")))
  7610. (if (equal goto '(16))
  7611. (org-refile-goto-last-stored)
  7612. (when (setq it (or rfloc
  7613. (org-refile-get-location
  7614. (if goto "Goto: " "Refile to: ") default-buffer
  7615. org-refile-allow-creating-parent-nodes)))
  7616. (setq file (nth 1 it)
  7617. re (nth 2 it)
  7618. pos (nth 3 it))
  7619. (if (and (not goto)
  7620. pos
  7621. (equal (buffer-file-name) file)
  7622. (if regionp
  7623. (and (>= pos region-start)
  7624. (<= pos region-end))
  7625. (and (>= pos (point))
  7626. (< pos (save-excursion
  7627. (org-end-of-subtree t t))))))
  7628. (error "Cannot refile to position inside the tree or region"))
  7629. (setq nbuf (or (find-buffer-visiting file)
  7630. (find-file-noselect file)))
  7631. (if goto
  7632. (progn
  7633. (switch-to-buffer nbuf)
  7634. (goto-char pos)
  7635. (org-show-context 'org-goto))
  7636. (if regionp
  7637. (progn
  7638. (kill-new (buffer-substring region-start region-end))
  7639. (org-save-markers-in-region region-start region-end))
  7640. (org-copy-subtree 1 nil t))
  7641. (save-excursion
  7642. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7643. (find-file-noselect file))))
  7644. (setq reversed (org-notes-order-reversed-p))
  7645. (save-excursion
  7646. (save-restriction
  7647. (widen)
  7648. (if pos
  7649. (progn
  7650. (goto-char pos)
  7651. (looking-at outline-regexp)
  7652. (setq level (org-get-valid-level (funcall outline-level) 1))
  7653. (goto-char
  7654. (if reversed
  7655. (or (outline-next-heading) (point-max))
  7656. (or (save-excursion (outline-get-next-sibling))
  7657. (org-end-of-subtree t t)
  7658. (point-max)))))
  7659. (setq level 1)
  7660. (if (not reversed)
  7661. (goto-char (point-max))
  7662. (goto-char (point-min))
  7663. (or (outline-next-heading) (goto-char (point-max)))))
  7664. (if (not (bolp)) (newline))
  7665. (bookmark-set "org-refile-last-stored")
  7666. (org-paste-subtree level)
  7667. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7668. (run-hooks 'org-after-refile-insert-hook))))
  7669. (if regionp
  7670. (delete-region (point) (+ (point) region-length))
  7671. (org-cut-subtree))
  7672. (setq org-markers-to-move nil)
  7673. (message "Refiled to \"%s\"" (car it))))))
  7674. (org-reveal))
  7675. (defun org-refile-goto-last-stored ()
  7676. "Go to the location where the last refile was stored."
  7677. (interactive)
  7678. (bookmark-jump "org-refile-last-stored")
  7679. (message "This is the location of the last refile"))
  7680. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7681. "Prompt the user for a refile location, using PROMPT."
  7682. (let ((org-refile-targets org-refile-targets)
  7683. (org-refile-use-outline-path org-refile-use-outline-path))
  7684. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7685. (unless org-refile-target-table
  7686. (error "No refile targets"))
  7687. (let* ((cbuf (current-buffer))
  7688. (partial-completion-mode nil)
  7689. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7690. (cfunc (if (and org-refile-use-outline-path
  7691. org-outline-path-complete-in-steps)
  7692. 'org-olpath-completing-read
  7693. 'org-ido-completing-read))
  7694. (extra (if org-refile-use-outline-path "/" ""))
  7695. (filename (and cfn (expand-file-name cfn)))
  7696. (tbl (mapcar
  7697. (lambda (x)
  7698. (if (and (not (member org-refile-use-outline-path
  7699. '(file full-file-path)))
  7700. (not (equal filename (nth 1 x))))
  7701. (cons (concat (car x) extra " ("
  7702. (file-name-nondirectory (nth 1 x)) ")")
  7703. (cdr x))
  7704. (cons (concat (car x) extra) (cdr x))))
  7705. org-refile-target-table))
  7706. (completion-ignore-case t)
  7707. pa answ parent-target child parent)
  7708. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7709. nil 'org-refile-history))
  7710. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7711. (if pa
  7712. pa
  7713. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7714. (setq parent (match-string 1 answ)
  7715. child (match-string 2 answ))
  7716. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7717. (when (and parent-target
  7718. (or (eq new-nodes t)
  7719. (and (eq new-nodes 'confirm)
  7720. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7721. (org-refile-new-child parent-target child))))))
  7722. (defun org-refile-new-child (parent-target child)
  7723. "Use refile target PARENT-TARGET to add new CHILD below it."
  7724. (unless parent-target
  7725. (error "Cannot find parent for new node"))
  7726. (let ((file (nth 1 parent-target))
  7727. (pos (nth 3 parent-target))
  7728. level)
  7729. (with-current-buffer (or (find-buffer-visiting file)
  7730. (find-file-noselect file))
  7731. (save-excursion
  7732. (save-restriction
  7733. (widen)
  7734. (if pos
  7735. (goto-char pos)
  7736. (goto-char (point-max))
  7737. (if (not (bolp)) (newline)))
  7738. (when (looking-at outline-regexp)
  7739. (setq level (funcall outline-level))
  7740. (org-end-of-subtree t t))
  7741. (org-back-over-empty-lines)
  7742. (insert "\n" (make-string
  7743. (if pos (org-get-valid-level level 1) 1) ?*)
  7744. " " child "\n")
  7745. (beginning-of-line 0)
  7746. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7747. (defun org-olpath-completing-read (prompt collection &rest args)
  7748. "Read an outline path like a file name."
  7749. (let ((thetable collection)
  7750. (org-completion-use-ido nil)) ; does not work with ido.
  7751. (apply
  7752. 'org-ido-completing-read prompt
  7753. (lambda (string predicate &optional flag)
  7754. (let (rtn r f (l (length string)))
  7755. (cond
  7756. ((eq flag nil)
  7757. ;; try completion
  7758. (try-completion string thetable))
  7759. ((eq flag t)
  7760. ;; all-completions
  7761. (setq rtn (all-completions string thetable predicate))
  7762. (mapcar
  7763. (lambda (x)
  7764. (setq r (substring x l))
  7765. (if (string-match " ([^)]*)$" x)
  7766. (setq f (match-string 0 x))
  7767. (setq f ""))
  7768. (if (string-match "/" r)
  7769. (concat string (substring r 0 (match-end 0)) f)
  7770. x))
  7771. rtn))
  7772. ((eq flag 'lambda)
  7773. ;; exact match?
  7774. (assoc string thetable)))
  7775. ))
  7776. args)))
  7777. ;;;; Dynamic blocks
  7778. (defun org-find-dblock (name)
  7779. "Find the first dynamic block with name NAME in the buffer.
  7780. If not found, stay at current position and return nil."
  7781. (let (pos)
  7782. (save-excursion
  7783. (goto-char (point-min))
  7784. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7785. nil t)
  7786. (match-beginning 0))))
  7787. (if pos (goto-char pos))
  7788. pos))
  7789. (defconst org-dblock-start-re
  7790. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7791. "Matches the startline of a dynamic block, with parameters.")
  7792. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7793. "Matches the end of a dynamic block.")
  7794. (defun org-create-dblock (plist)
  7795. "Create a dynamic block section, with parameters taken from PLIST.
  7796. PLIST must contain a :name entry which is used as name of the block."
  7797. (unless (bolp) (newline))
  7798. (let ((name (plist-get plist :name)))
  7799. (insert "#+BEGIN: " name)
  7800. (while plist
  7801. (if (eq (car plist) :name)
  7802. (setq plist (cddr plist))
  7803. (insert " " (prin1-to-string (pop plist)))))
  7804. (insert "\n\n#+END:\n")
  7805. (beginning-of-line -2)))
  7806. (defun org-prepare-dblock ()
  7807. "Prepare dynamic block for refresh.
  7808. This empties the block, puts the cursor at the insert position and returns
  7809. the property list including an extra property :name with the block name."
  7810. (unless (looking-at org-dblock-start-re)
  7811. (error "Not at a dynamic block"))
  7812. (let* ((begdel (1+ (match-end 0)))
  7813. (name (org-no-properties (match-string 1)))
  7814. (params (append (list :name name)
  7815. (read (concat "(" (match-string 3) ")")))))
  7816. (unless (re-search-forward org-dblock-end-re nil t)
  7817. (error "Dynamic block not terminated"))
  7818. (setq params
  7819. (append params
  7820. (list :content (buffer-substring
  7821. begdel (match-beginning 0)))))
  7822. (delete-region begdel (match-beginning 0))
  7823. (goto-char begdel)
  7824. (open-line 1)
  7825. params))
  7826. (defun org-map-dblocks (&optional command)
  7827. "Apply COMMAND to all dynamic blocks in the current buffer.
  7828. If COMMAND is not given, use `org-update-dblock'."
  7829. (let ((cmd (or command 'org-update-dblock))
  7830. pos)
  7831. (save-excursion
  7832. (goto-char (point-min))
  7833. (while (re-search-forward org-dblock-start-re nil t)
  7834. (goto-char (setq pos (match-beginning 0)))
  7835. (condition-case nil
  7836. (funcall cmd)
  7837. (error (message "Error during update of dynamic block")))
  7838. (goto-char pos)
  7839. (unless (re-search-forward org-dblock-end-re nil t)
  7840. (error "Dynamic block not terminated"))))))
  7841. (defun org-dblock-update (&optional arg)
  7842. "User command for updating dynamic blocks.
  7843. Update the dynamic block at point. With prefix ARG, update all dynamic
  7844. blocks in the buffer."
  7845. (interactive "P")
  7846. (if arg
  7847. (org-update-all-dblocks)
  7848. (or (looking-at org-dblock-start-re)
  7849. (org-beginning-of-dblock))
  7850. (org-update-dblock)))
  7851. (defun org-update-dblock ()
  7852. "Update the dynamic block at point
  7853. This means to empty the block, parse for parameters and then call
  7854. the correct writing function."
  7855. (save-window-excursion
  7856. (let* ((pos (point))
  7857. (line (org-current-line))
  7858. (params (org-prepare-dblock))
  7859. (name (plist-get params :name))
  7860. (cmd (intern (concat "org-dblock-write:" name))))
  7861. (message "Updating dynamic block `%s' at line %d..." name line)
  7862. (funcall cmd params)
  7863. (message "Updating dynamic block `%s' at line %d...done" name line)
  7864. (goto-char pos))))
  7865. (defun org-beginning-of-dblock ()
  7866. "Find the beginning of the dynamic block at point.
  7867. Error if there is no such block at point."
  7868. (let ((pos (point))
  7869. beg)
  7870. (end-of-line 1)
  7871. (if (and (re-search-backward org-dblock-start-re nil t)
  7872. (setq beg (match-beginning 0))
  7873. (re-search-forward org-dblock-end-re nil t)
  7874. (> (match-end 0) pos))
  7875. (goto-char beg)
  7876. (goto-char pos)
  7877. (error "Not in a dynamic block"))))
  7878. (defun org-update-all-dblocks ()
  7879. "Update all dynamic blocks in the buffer.
  7880. This function can be used in a hook."
  7881. (when (org-mode-p)
  7882. (org-map-dblocks 'org-update-dblock)))
  7883. ;;;; Completion
  7884. (defconst org-additional-option-like-keywords
  7885. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  7886. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  7887. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  7888. "BEGIN:" "END:"
  7889. "ORGTBL" "TBLFM:" "TBLNAME:"
  7890. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7891. "BEGIN_QUOTE" "END_QUOTE"
  7892. "BEGIN_VERSE" "END_VERSE"
  7893. "BEGIN_CENTER" "END_CENTER"
  7894. "BEGIN_SRC" "END_SRC"
  7895. "CATEGORY" "COLUMNS"
  7896. "CAPTION" "LABEL"
  7897. "BIND"))
  7898. (defcustom org-structure-template-alist
  7899. '(
  7900. ("s" "#+begin_src ?\n\n#+end_src"
  7901. "<src lang=\"?\">\n\n</src>")
  7902. ("e" "#+begin_example\n?\n#+end_example"
  7903. "<example>\n?\n</example>")
  7904. ("q" "#+begin_quote\n?\n#+end_quote"
  7905. "<quote>\n?\n</quote>")
  7906. ("v" "#+begin_verse\n?\n#+end_verse"
  7907. "<verse>\n?\n/verse>")
  7908. ("c" "#+begin_center\n?\n#+end_center"
  7909. "<center>\n?\n/center>")
  7910. ("l" "#+begin_latex\n?\n#+end_latex"
  7911. "<literal style=\"latex\">\n?\n</literal>")
  7912. ("L" "#+latex: "
  7913. "<literal style=\"latex\">?</literal>")
  7914. ("h" "#+begin_html\n?\n#+end_html"
  7915. "<literal style=\"html\">\n?\n</literal>")
  7916. ("H" "#+html: "
  7917. "<literal style=\"html\">?</literal>")
  7918. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7919. ("A" "#+ascii: ")
  7920. ("i" "#+include %file ?"
  7921. "<include file=%file markup=\"?\">")
  7922. )
  7923. "Structure completion elements.
  7924. This is a list of abbreviation keys and values. The value gets inserted
  7925. it you type @samp{.} followed by the key and then the completion key,
  7926. usually `M-TAB'. %file will be replaced by a file name after prompting
  7927. for the file using completion.
  7928. There are two templates for each key, the first uses the original Org syntax,
  7929. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7930. the default when the /org-mtags.el/ module has been loaded. See also the
  7931. variable `org-mtags-prefer-muse-templates'.
  7932. This is an experimental feature, it is undecided if it is going to stay in."
  7933. :group 'org-completion
  7934. :type '(repeat
  7935. (string :tag "Key")
  7936. (string :tag "Template")
  7937. (string :tag "Muse Template")))
  7938. (defun org-try-structure-completion ()
  7939. "Try to complete a structure template before point.
  7940. This looks for strings like \"<e\" on an otherwise empty line and
  7941. expands them."
  7942. (let ((l (buffer-substring (point-at-bol) (point)))
  7943. a)
  7944. (when (and (looking-at "[ \t]*$")
  7945. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7946. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7947. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7948. (match-beginning 1)) a)
  7949. t)))
  7950. (defun org-complete-expand-structure-template (start cell)
  7951. "Expand a structure template."
  7952. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7953. (rpl (nth (if musep 2 1) cell))
  7954. (ind ""))
  7955. (delete-region start (point))
  7956. (when (string-match "\\`#\\+" rpl)
  7957. (cond
  7958. ((bolp))
  7959. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7960. (setq ind (buffer-substring (point-at-bol) (point))))
  7961. (t (newline))))
  7962. (setq start (point))
  7963. (if (string-match "%file" rpl)
  7964. (setq rpl (replace-match
  7965. (concat
  7966. "\""
  7967. (save-match-data
  7968. (abbreviate-file-name (read-file-name "Include file: ")))
  7969. "\"")
  7970. t t rpl)))
  7971. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  7972. (concat "\n" ind)))
  7973. (insert rpl)
  7974. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7975. (defun org-complete (&optional arg)
  7976. "Perform completion on word at point.
  7977. At the beginning of a headline, this completes TODO keywords as given in
  7978. `org-todo-keywords'.
  7979. If the current word is preceded by a backslash, completes the TeX symbols
  7980. that are supported for HTML support.
  7981. If the current word is preceded by \"#+\", completes special words for
  7982. setting file options.
  7983. In the line after \"#+STARTUP:, complete valid keywords.\"
  7984. At all other locations, this simply calls the value of
  7985. `org-completion-fallback-command'."
  7986. (interactive "P")
  7987. (org-without-partial-completion
  7988. (catch 'exit
  7989. (let* ((a nil)
  7990. (end (point))
  7991. (beg1 (save-excursion
  7992. (skip-chars-backward (org-re "[:alnum:]_@"))
  7993. (point)))
  7994. (beg (save-excursion
  7995. (skip-chars-backward "a-zA-Z0-9_:$")
  7996. (point)))
  7997. (confirm (lambda (x) (stringp (car x))))
  7998. (searchhead (equal (char-before beg) ?*))
  7999. (struct
  8000. (when (and (member (char-before beg1) '(?. ?<))
  8001. (setq a (assoc (buffer-substring beg1 (point))
  8002. org-structure-template-alist)))
  8003. (org-complete-expand-structure-template (1- beg1) a)
  8004. (throw 'exit t)))
  8005. (tag (and (equal (char-before beg1) ?:)
  8006. (equal (char-after (point-at-bol)) ?*)))
  8007. (prop (and (equal (char-before beg1) ?:)
  8008. (not (equal (char-after (point-at-bol)) ?*))))
  8009. (texp (equal (char-before beg) ?\\))
  8010. (link (equal (char-before beg) ?\[))
  8011. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8012. beg)
  8013. "#+"))
  8014. (startup (string-match "^#\\+STARTUP:.*"
  8015. (buffer-substring (point-at-bol) (point))))
  8016. (completion-ignore-case opt)
  8017. (type nil)
  8018. (tbl nil)
  8019. (table (cond
  8020. (opt
  8021. (setq type :opt)
  8022. (require 'org-exp)
  8023. (append
  8024. (mapcar
  8025. (lambda (x)
  8026. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8027. (cons (match-string 2 x) (match-string 1 x)))
  8028. (org-split-string (org-get-current-options) "\n"))
  8029. (mapcar 'list org-additional-option-like-keywords)))
  8030. (startup
  8031. (setq type :startup)
  8032. org-startup-options)
  8033. (link (append org-link-abbrev-alist-local
  8034. org-link-abbrev-alist))
  8035. (texp
  8036. (setq type :tex)
  8037. org-html-entities)
  8038. ((string-match "\\`\\*+[ \t]+\\'"
  8039. (buffer-substring (point-at-bol) beg))
  8040. (setq type :todo)
  8041. (mapcar 'list org-todo-keywords-1))
  8042. (searchhead
  8043. (setq type :searchhead)
  8044. (save-excursion
  8045. (goto-char (point-min))
  8046. (while (re-search-forward org-todo-line-regexp nil t)
  8047. (push (list
  8048. (org-make-org-heading-search-string
  8049. (match-string 3) t))
  8050. tbl)))
  8051. tbl)
  8052. (tag (setq type :tag beg beg1)
  8053. (or org-tag-alist (org-get-buffer-tags)))
  8054. (prop (setq type :prop beg beg1)
  8055. (mapcar 'list (org-buffer-property-keys nil t t)))
  8056. (t (progn
  8057. (call-interactively org-completion-fallback-command)
  8058. (throw 'exit nil)))))
  8059. (pattern (buffer-substring-no-properties beg end))
  8060. (completion (try-completion pattern table confirm)))
  8061. (cond ((eq completion t)
  8062. (if (not (assoc (upcase pattern) table))
  8063. (message "Already complete")
  8064. (if (and (equal type :opt)
  8065. (not (member (car (assoc (upcase pattern) table))
  8066. org-additional-option-like-keywords)))
  8067. (insert (substring (cdr (assoc (upcase pattern) table))
  8068. (length pattern)))
  8069. (if (memq type '(:tag :prop)) (insert ":")))))
  8070. ((null completion)
  8071. (message "Can't find completion for \"%s\"" pattern)
  8072. (ding))
  8073. ((not (string= pattern completion))
  8074. (delete-region beg end)
  8075. (if (string-match " +$" completion)
  8076. (setq completion (replace-match "" t t completion)))
  8077. (insert completion)
  8078. (if (get-buffer-window "*Completions*")
  8079. (delete-window (get-buffer-window "*Completions*")))
  8080. (if (assoc completion table)
  8081. (if (eq type :todo) (insert " ")
  8082. (if (memq type '(:tag :prop)) (insert ":"))))
  8083. (if (and (equal type :opt) (assoc completion table))
  8084. (message "%s" (substitute-command-keys
  8085. "Press \\[org-complete] again to insert example settings"))))
  8086. (t
  8087. (message "Making completion list...")
  8088. (let ((list (sort (all-completions pattern table confirm)
  8089. 'string<)))
  8090. (with-output-to-temp-buffer "*Completions*"
  8091. (condition-case nil
  8092. ;; Protection needed for XEmacs and emacs 21
  8093. (display-completion-list list pattern)
  8094. (error (display-completion-list list)))))
  8095. (message "Making completion list...%s" "done")))))))
  8096. ;;;; TODO, DEADLINE, Comments
  8097. (defun org-toggle-comment ()
  8098. "Change the COMMENT state of an entry."
  8099. (interactive)
  8100. (save-excursion
  8101. (org-back-to-heading)
  8102. (let (case-fold-search)
  8103. (if (looking-at (concat outline-regexp
  8104. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8105. (replace-match "" t t nil 1)
  8106. (if (looking-at outline-regexp)
  8107. (progn
  8108. (goto-char (match-end 0))
  8109. (insert org-comment-string " ")))))))
  8110. (defvar org-last-todo-state-is-todo nil
  8111. "This is non-nil when the last TODO state change led to a TODO state.
  8112. If the last change removed the TODO tag or switched to DONE, then
  8113. this is nil.")
  8114. (defvar org-setting-tags nil) ; dynamically skipped
  8115. (defun org-parse-local-options (string var)
  8116. "Parse STRING for startup setting relevant for variable VAR."
  8117. (let ((rtn (symbol-value var))
  8118. e opts)
  8119. (save-match-data
  8120. (if (or (not string) (not (string-match "\\S-" string)))
  8121. rtn
  8122. (setq opts (delq nil (mapcar (lambda (x)
  8123. (setq e (assoc x org-startup-options))
  8124. (if (eq (nth 1 e) var) e nil))
  8125. (org-split-string string "[ \t]+"))))
  8126. (if (not opts)
  8127. rtn
  8128. (setq rtn nil)
  8129. (while (setq e (pop opts))
  8130. (if (not (nth 3 e))
  8131. (setq rtn (nth 2 e))
  8132. (if (not (listp rtn)) (setq rtn nil))
  8133. (push (nth 2 e) rtn)))
  8134. rtn)))))
  8135. (defvar org-todo-setup-filter-hook nil
  8136. "Hook for functions that pre-filter todo specs.
  8137. Each function takes a todo spec and returns either `nil' or the spec
  8138. transformed into canonical form." )
  8139. (defvar org-todo-get-default-hook nil
  8140. "Hook for functions that get a default item for todo.
  8141. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8142. `nil' or a string to be used for the todo mark." )
  8143. (defvar org-agenda-headline-snapshot-before-repeat)
  8144. (defun org-todo (&optional arg)
  8145. "Change the TODO state of an item.
  8146. The state of an item is given by a keyword at the start of the heading,
  8147. like
  8148. *** TODO Write paper
  8149. *** DONE Call mom
  8150. The different keywords are specified in the variable `org-todo-keywords'.
  8151. By default the available states are \"TODO\" and \"DONE\".
  8152. So for this example: when the item starts with TODO, it is changed to DONE.
  8153. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8154. DONE are present, add TODO at the beginning of the heading.
  8155. With C-u prefix arg, use completion to determine the new state.
  8156. With numeric prefix arg, switch to that state.
  8157. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8158. With a tripple C-u prefix, circumvent any state blocking.
  8159. For calling through lisp, arg is also interpreted in the following way:
  8160. 'none -> empty state
  8161. \"\"(empty string) -> switch to empty state
  8162. 'done -> switch to DONE
  8163. 'nextset -> switch to the next set of keywords
  8164. 'previousset -> switch to the previous set of keywords
  8165. \"WAITING\" -> switch to the specified keyword, but only if it
  8166. really is a member of `org-todo-keywords'."
  8167. (interactive "P")
  8168. (if (equal arg '(16)) (setq arg 'nextset))
  8169. (let ((org-blocker-hook org-blocker-hook)
  8170. (case-fold-search nil))
  8171. (when (equal arg '(64))
  8172. (setq arg nil org-blocker-hook nil))
  8173. (when (and org-blocker-hook
  8174. (or org-inhibit-blocking
  8175. (org-entry-get nil "NOBLOCKING")))
  8176. (setq org-blocker-hook nil))
  8177. (save-excursion
  8178. (catch 'exit
  8179. (org-back-to-heading)
  8180. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8181. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8182. (looking-at " *"))
  8183. (let* ((match-data (match-data))
  8184. (startpos (point-at-bol))
  8185. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8186. (org-log-done org-log-done)
  8187. (org-log-repeat org-log-repeat)
  8188. (org-todo-log-states org-todo-log-states)
  8189. (this (match-string 1))
  8190. (hl-pos (match-beginning 0))
  8191. (head (org-get-todo-sequence-head this))
  8192. (ass (assoc head org-todo-kwd-alist))
  8193. (interpret (nth 1 ass))
  8194. (done-word (nth 3 ass))
  8195. (final-done-word (nth 4 ass))
  8196. (last-state (or this ""))
  8197. (completion-ignore-case t)
  8198. (member (member this org-todo-keywords-1))
  8199. (tail (cdr member))
  8200. (state (cond
  8201. ((and org-todo-key-trigger
  8202. (or (and (equal arg '(4))
  8203. (eq org-use-fast-todo-selection 'prefix))
  8204. (and (not arg) org-use-fast-todo-selection
  8205. (not (eq org-use-fast-todo-selection
  8206. 'prefix)))))
  8207. ;; Use fast selection
  8208. (org-fast-todo-selection))
  8209. ((and (equal arg '(4))
  8210. (or (not org-use-fast-todo-selection)
  8211. (not org-todo-key-trigger)))
  8212. ;; Read a state with completion
  8213. (org-ido-completing-read
  8214. "State: " (mapcar (lambda(x) (list x))
  8215. org-todo-keywords-1)
  8216. nil t))
  8217. ((eq arg 'right)
  8218. (if this
  8219. (if tail (car tail) nil)
  8220. (car org-todo-keywords-1)))
  8221. ((eq arg 'left)
  8222. (if (equal member org-todo-keywords-1)
  8223. nil
  8224. (if this
  8225. (nth (- (length org-todo-keywords-1)
  8226. (length tail) 2)
  8227. org-todo-keywords-1)
  8228. (org-last org-todo-keywords-1))))
  8229. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8230. (setq arg nil))) ; hack to fall back to cycling
  8231. (arg
  8232. ;; user or caller requests a specific state
  8233. (cond
  8234. ((equal arg "") nil)
  8235. ((eq arg 'none) nil)
  8236. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8237. ((eq arg 'nextset)
  8238. (or (car (cdr (member head org-todo-heads)))
  8239. (car org-todo-heads)))
  8240. ((eq arg 'previousset)
  8241. (let ((org-todo-heads (reverse org-todo-heads)))
  8242. (or (car (cdr (member head org-todo-heads)))
  8243. (car org-todo-heads))))
  8244. ((car (member arg org-todo-keywords-1)))
  8245. ((nth (1- (prefix-numeric-value arg))
  8246. org-todo-keywords-1))))
  8247. ((null member) (or head (car org-todo-keywords-1)))
  8248. ((equal this final-done-word) nil) ;; -> make empty
  8249. ((null tail) nil) ;; -> first entry
  8250. ((memq interpret '(type priority))
  8251. (if (eq this-command last-command)
  8252. (car tail)
  8253. (if (> (length tail) 0)
  8254. (or done-word (car org-done-keywords))
  8255. nil)))
  8256. (t
  8257. (car tail))))
  8258. (state (or
  8259. (run-hook-with-args-until-success
  8260. 'org-todo-get-default-hook state last-state)
  8261. state))
  8262. (next (if state (concat " " state " ") " "))
  8263. (change-plist (list :type 'todo-state-change :from this :to state
  8264. :position startpos))
  8265. dolog now-done-p)
  8266. (when org-blocker-hook
  8267. (setq org-last-todo-state-is-todo
  8268. (not (member this org-done-keywords)))
  8269. (unless (save-excursion
  8270. (save-match-data
  8271. (run-hook-with-args-until-failure
  8272. 'org-blocker-hook change-plist)))
  8273. (if (interactive-p)
  8274. (error "TODO state change from %s to %s blocked" this state)
  8275. ;; fail silently
  8276. (message "TODO state change from %s to %s blocked" this state)
  8277. (throw 'exit nil))))
  8278. (store-match-data match-data)
  8279. (replace-match next t t)
  8280. (unless (pos-visible-in-window-p hl-pos)
  8281. (message "TODO state changed to %s" (org-trim next)))
  8282. (unless head
  8283. (setq head (org-get-todo-sequence-head state)
  8284. ass (assoc head org-todo-kwd-alist)
  8285. interpret (nth 1 ass)
  8286. done-word (nth 3 ass)
  8287. final-done-word (nth 4 ass)))
  8288. (when (memq arg '(nextset previousset))
  8289. (message "Keyword-Set %d/%d: %s"
  8290. (- (length org-todo-sets) -1
  8291. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8292. (length org-todo-sets)
  8293. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8294. (setq org-last-todo-state-is-todo
  8295. (not (member state org-done-keywords)))
  8296. (setq now-done-p (and (member state org-done-keywords)
  8297. (not (member this org-done-keywords))))
  8298. (and logging (org-local-logging logging))
  8299. (when (and (or org-todo-log-states org-log-done)
  8300. (not (eq org-inhibit-logging t))
  8301. (not (memq arg '(nextset previousset))))
  8302. ;; we need to look at recording a time and note
  8303. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8304. (nth 2 (assoc this org-todo-log-states))))
  8305. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8306. (setq dolog 'time))
  8307. (when (and state
  8308. (member state org-not-done-keywords)
  8309. (not (member this org-not-done-keywords)))
  8310. ;; This is now a todo state and was not one before
  8311. ;; If there was a CLOSED time stamp, get rid of it.
  8312. (org-add-planning-info nil nil 'closed))
  8313. (when (and now-done-p org-log-done)
  8314. ;; It is now done, and it was not done before
  8315. (org-add-planning-info 'closed (org-current-time))
  8316. (if (and (not dolog) (eq 'note org-log-done))
  8317. (org-add-log-setup 'done state this 'findpos 'note)))
  8318. (when (and state dolog)
  8319. ;; This is a non-nil state, and we need to log it
  8320. (org-add-log-setup 'state state this 'findpos dolog)))
  8321. ;; Fixup tag positioning
  8322. (org-todo-trigger-tag-changes state)
  8323. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8324. (when org-provide-todo-statistics
  8325. (org-update-parent-todo-statistics))
  8326. (run-hooks 'org-after-todo-state-change-hook)
  8327. (if (and arg (not (member state org-done-keywords)))
  8328. (setq head (org-get-todo-sequence-head state)))
  8329. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8330. ;; Do we need to trigger a repeat?
  8331. (when now-done-p
  8332. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8333. ;; This is for the agenda, take a snapshot of the headline.
  8334. (save-match-data
  8335. (setq org-agenda-headline-snapshot-before-repeat
  8336. (org-get-heading))))
  8337. (org-auto-repeat-maybe state))
  8338. ;; Fixup cursor location if close to the keyword
  8339. (if (and (outline-on-heading-p)
  8340. (not (bolp))
  8341. (save-excursion (beginning-of-line 1)
  8342. (looking-at org-todo-line-regexp))
  8343. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8344. (progn
  8345. (goto-char (or (match-end 2) (match-end 1)))
  8346. (and (looking-at " ") (just-one-space))))
  8347. (when org-trigger-hook
  8348. (save-excursion
  8349. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8350. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8351. "Block turning an entry into a TODO, using the hierarchy.
  8352. This checks whether the current task should be blocked from state
  8353. changes. Such blocking occurs when:
  8354. 1. The task has children which are not all in a completed state.
  8355. 2. A task has a parent with the property :ORDERED:, and there
  8356. are siblings prior to the current task with incomplete
  8357. status.
  8358. 3. The parent of the task is blocked because it has siblings that should
  8359. be done first, or is child of a block grandparent TODO entry."
  8360. (catch 'dont-block
  8361. ;; If this is not a todo state change, or if this entry is already DONE,
  8362. ;; do not block
  8363. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8364. (member (plist-get change-plist :from)
  8365. (cons 'done org-done-keywords))
  8366. (member (plist-get change-plist :to)
  8367. (cons 'todo org-not-done-keywords)))
  8368. (throw 'dont-block t))
  8369. ;; If this task has children, and any are undone, it's blocked
  8370. (save-excursion
  8371. (org-back-to-heading t)
  8372. (let ((this-level (funcall outline-level)))
  8373. (outline-next-heading)
  8374. (let ((child-level (funcall outline-level)))
  8375. (while (and (not (eobp))
  8376. (> child-level this-level))
  8377. ;; this todo has children, check whether they are all
  8378. ;; completed
  8379. (if (and (not (org-entry-is-done-p))
  8380. (org-entry-is-todo-p))
  8381. (throw 'dont-block nil))
  8382. (outline-next-heading)
  8383. (setq child-level (funcall outline-level))))))
  8384. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8385. ;; any previous siblings are undone, it's blocked
  8386. (save-excursion
  8387. (org-back-to-heading t)
  8388. (let* ((pos (point))
  8389. (parent-pos (and (org-up-heading-safe) (point))))
  8390. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8391. (when (and (org-entry-get (point) "ORDERED")
  8392. (forward-line 1)
  8393. (re-search-forward org-not-done-heading-regexp pos t))
  8394. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8395. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8396. (while t
  8397. (goto-char parent-pos)
  8398. (if (not (looking-at org-not-done-heading-regexp))
  8399. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8400. (setq pos (point))
  8401. (setq parent-pos (and (org-up-heading-safe) (point)))
  8402. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8403. (when (and (org-entry-get (point) "ORDERED")
  8404. (forward-line 1)
  8405. (re-search-forward org-not-done-heading-regexp pos t))
  8406. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8407. (defcustom org-track-ordered-property-with-tag nil
  8408. "Should the ORDERED property also be shown as a tag?
  8409. The ORDERED property decides if an entry should require subtasks to be
  8410. completed in sequence. Since a property is not very visible, setting
  8411. this option means that toggling the ORDERED property with the command
  8412. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8413. not relevant for the behavior, but it makes things more visible.
  8414. Note that toggling the tag with tags commands will not change the property
  8415. and therefore not influence behavior!
  8416. This can be t, meaning the tag ORDERED should be used, It can also be a
  8417. string to select a different tag for this task."
  8418. :group 'org-todo
  8419. :type '(choice
  8420. (const :tag "No tracking" nil)
  8421. (const :tag "Track with ORDERED tag" t)
  8422. (string :tag "Use other tag")))
  8423. (defun org-toggle-ordered-property ()
  8424. "Toggle the ORDERED property of the current entry.
  8425. For better visibility, you can track the value of this property with a tag.
  8426. See variable `org-track-ordered-property-with-tag'."
  8427. (interactive)
  8428. (let* ((t1 org-track-ordered-property-with-tag)
  8429. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8430. (save-excursion
  8431. (org-back-to-heading)
  8432. (if (org-entry-get nil "ORDERED")
  8433. (progn
  8434. (org-delete-property "ORDERED")
  8435. (and tag (org-toggle-tag tag 'off))
  8436. (message "Subtasks can be completed in arbitrary order"))
  8437. (org-entry-put nil "ORDERED" "t")
  8438. (and tag (org-toggle-tag tag 'on))
  8439. (message "Subtasks must be completed in sequence")))))
  8440. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8441. (defun org-block-todo-from-checkboxes (change-plist)
  8442. "Block turning an entry into a TODO, using checkboxes.
  8443. This checks whether the current task should be blocked from state
  8444. changes because there are uncheckd boxes in this entry."
  8445. (catch 'dont-block
  8446. ;; If this is not a todo state change, or if this entry is already DONE,
  8447. ;; do not block
  8448. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8449. (member (plist-get change-plist :from)
  8450. (cons 'done org-done-keywords))
  8451. (member (plist-get change-plist :to)
  8452. (cons 'todo org-not-done-keywords)))
  8453. (throw 'dont-block t))
  8454. ;; If this task has checkboxes that are not checked, it's blocked
  8455. (save-excursion
  8456. (org-back-to-heading t)
  8457. (let ((beg (point)) end)
  8458. (outline-next-heading)
  8459. (setq end (point))
  8460. (goto-char beg)
  8461. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8462. end t)
  8463. (progn
  8464. (if (boundp 'org-blocked-by-checkboxes)
  8465. (setq org-blocked-by-checkboxes t))
  8466. (throw 'dont-block nil)))))
  8467. t)) ; do not block
  8468. (defvar org-entry-property-inherited-from) ;; defined below
  8469. (defun org-update-parent-todo-statistics ()
  8470. "Update any statistics cookie in the parent of the current headline.
  8471. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8472. the entire subtree and this will travel up the hierarchy and update
  8473. statistics everywhere."
  8474. (interactive)
  8475. (let* ((lim 0) prop
  8476. (recursive (or (not org-hierarchical-todo-statistics)
  8477. (string-match
  8478. "\\<recursive\\>"
  8479. (or (setq prop (org-entry-get
  8480. nil "COOKIE_DATA" 'inherit)) ""))))
  8481. (lim (or (and prop (marker-position
  8482. org-entry-property-inherited-from))
  8483. lim))
  8484. (first t)
  8485. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8486. level ltoggle l1
  8487. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8488. (catch 'exit
  8489. (save-excursion
  8490. (beginning-of-line 1)
  8491. (if (org-at-heading-p)
  8492. (setq ltoggle (funcall outline-level))
  8493. (error "This should not happen"))
  8494. (while (and (setq level (org-up-heading-safe))
  8495. (or recursive first)
  8496. (>= (point) lim))
  8497. (setq first nil)
  8498. (unless (and level
  8499. (not (string-match
  8500. "\\<checkbox\\>"
  8501. (downcase
  8502. (or (org-entry-get
  8503. nil "COOKIE_DATA")
  8504. "")))))
  8505. (throw 'exit nil))
  8506. (while (re-search-forward box-re (point-at-eol) t)
  8507. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8508. (setq is-percent (match-end 2))
  8509. (save-match-data
  8510. (unless (outline-next-heading) (throw 'exit nil))
  8511. (while (and (looking-at org-complex-heading-regexp)
  8512. (> (setq l1 (length (match-string 1))) level))
  8513. (setq kwd (and (or recursive (= l1 ltoggle))
  8514. (match-string 2)))
  8515. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8516. (and (listp org-provide-todo-statistics)
  8517. (or (member kwd org-provide-todo-statistics)
  8518. (member kwd org-done-keywords))))
  8519. (setq cnt-all (1+ cnt-all))
  8520. (if (eq org-provide-todo-statistics t)
  8521. (and kwd (setq cnt-all (1+ cnt-all)))))
  8522. (and (member kwd org-done-keywords)
  8523. (setq cnt-done (1+ cnt-done)))
  8524. (outline-next-heading)))
  8525. (replace-match
  8526. (if is-percent
  8527. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8528. (format "[%d/%d]" cnt-done cnt-all)))))
  8529. (when cookie-present
  8530. (run-hook-with-args 'org-after-todo-statistics-hook
  8531. cnt-done (- cnt-all cnt-done)))))
  8532. (run-hooks 'org-todo-statistics-hook)))
  8533. (defvar org-after-todo-statistics-hook nil
  8534. "Hook that is called after a TODO statistics cookie has been updated.
  8535. Each function is called with two arguments: the number of not-done entries
  8536. and the number of done entries.
  8537. For example, the following function, when added to this hook, will switch
  8538. an entry to DONE when all children are done, and back to TODO when new
  8539. entries are set to a TODO status. Note that this hook is only called
  8540. when there is a statistics cookie in the headline!
  8541. (defun org-summary-todo (n-done n-not-done)
  8542. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8543. (let (org-log-done org-log-states) ; turn off logging
  8544. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8545. ")
  8546. (defvar org-todo-statistics-hook nil
  8547. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8548. This hook runs even if there is no statisics cookie present, in which case
  8549. `org-after-todo-statistics-hook' would not run.")
  8550. (defun org-todo-trigger-tag-changes (state)
  8551. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8552. (let ((l org-todo-state-tags-triggers)
  8553. changes)
  8554. (when (or (not state) (equal state ""))
  8555. (setq changes (append changes (cdr (assoc "" l)))))
  8556. (when (and (stringp state) (> (length state) 0))
  8557. (setq changes (append changes (cdr (assoc state l)))))
  8558. (when (member state org-not-done-keywords)
  8559. (setq changes (append changes (cdr (assoc 'todo l)))))
  8560. (when (member state org-done-keywords)
  8561. (setq changes (append changes (cdr (assoc 'done l)))))
  8562. (dolist (c changes)
  8563. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8564. (defun org-local-logging (value)
  8565. "Get logging settings from a property VALUE."
  8566. (let* (words w a)
  8567. ;; directly set the variables, they are already local.
  8568. (setq org-log-done nil
  8569. org-log-repeat nil
  8570. org-todo-log-states nil)
  8571. (setq words (org-split-string value))
  8572. (while (setq w (pop words))
  8573. (cond
  8574. ((setq a (assoc w org-startup-options))
  8575. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8576. (set (nth 1 a) (nth 2 a))))
  8577. ((setq a (org-extract-log-state-settings w))
  8578. (and (member (car a) org-todo-keywords-1)
  8579. (push a org-todo-log-states)))))))
  8580. (defun org-get-todo-sequence-head (kwd)
  8581. "Return the head of the TODO sequence to which KWD belongs.
  8582. If KWD is not set, check if there is a text property remembering the
  8583. right sequence."
  8584. (let (p)
  8585. (cond
  8586. ((not kwd)
  8587. (or (get-text-property (point-at-bol) 'org-todo-head)
  8588. (progn
  8589. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8590. nil (point-at-eol)))
  8591. (get-text-property p 'org-todo-head))))
  8592. ((not (member kwd org-todo-keywords-1))
  8593. (car org-todo-keywords-1))
  8594. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8595. (defun org-fast-todo-selection ()
  8596. "Fast TODO keyword selection with single keys.
  8597. Returns the new TODO keyword, or nil if no state change should occur."
  8598. (let* ((fulltable org-todo-key-alist)
  8599. (done-keywords org-done-keywords) ;; needed for the faces.
  8600. (maxlen (apply 'max (mapcar
  8601. (lambda (x)
  8602. (if (stringp (car x)) (string-width (car x)) 0))
  8603. fulltable)))
  8604. (expert nil)
  8605. (fwidth (+ maxlen 3 1 3))
  8606. (ncol (/ (- (window-width) 4) fwidth))
  8607. tg cnt e c tbl
  8608. groups ingroup)
  8609. (save-excursion
  8610. (save-window-excursion
  8611. (if expert
  8612. (set-buffer (get-buffer-create " *Org todo*"))
  8613. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8614. (erase-buffer)
  8615. (org-set-local 'org-done-keywords done-keywords)
  8616. (setq tbl fulltable cnt 0)
  8617. (while (setq e (pop tbl))
  8618. (cond
  8619. ((equal e '(:startgroup))
  8620. (push '() groups) (setq ingroup t)
  8621. (when (not (= cnt 0))
  8622. (setq cnt 0)
  8623. (insert "\n"))
  8624. (insert "{ "))
  8625. ((equal e '(:endgroup))
  8626. (setq ingroup nil cnt 0)
  8627. (insert "}\n"))
  8628. ((equal e '(:newline))
  8629. (when (not (= cnt 0))
  8630. (setq cnt 0)
  8631. (insert "\n")
  8632. (setq e (car tbl))
  8633. (while (equal (car tbl) '(:newline))
  8634. (insert "\n")
  8635. (setq tbl (cdr tbl)))))
  8636. (t
  8637. (setq tg (car e) c (cdr e))
  8638. (if ingroup (push tg (car groups)))
  8639. (setq tg (org-add-props tg nil 'face
  8640. (org-get-todo-face tg)))
  8641. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8642. (insert "[" c "] " tg (make-string
  8643. (- fwidth 4 (length tg)) ?\ ))
  8644. (when (= (setq cnt (1+ cnt)) ncol)
  8645. (insert "\n")
  8646. (if ingroup (insert " "))
  8647. (setq cnt 0)))))
  8648. (insert "\n")
  8649. (goto-char (point-min))
  8650. (if (not expert) (org-fit-window-to-buffer))
  8651. (message "[a-z..]:Set [SPC]:clear")
  8652. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8653. (cond
  8654. ((or (= c ?\C-g)
  8655. (and (= c ?q) (not (rassoc c fulltable))))
  8656. (setq quit-flag t))
  8657. ((= c ?\ ) nil)
  8658. ((setq e (rassoc c fulltable) tg (car e))
  8659. tg)
  8660. (t (setq quit-flag t)))))))
  8661. (defun org-entry-is-todo-p ()
  8662. (member (org-get-todo-state) org-not-done-keywords))
  8663. (defun org-entry-is-done-p ()
  8664. (member (org-get-todo-state) org-done-keywords))
  8665. (defun org-get-todo-state ()
  8666. (save-excursion
  8667. (org-back-to-heading t)
  8668. (and (looking-at org-todo-line-regexp)
  8669. (match-end 2)
  8670. (match-string 2))))
  8671. (defun org-at-date-range-p (&optional inactive-ok)
  8672. "Is the cursor inside a date range?"
  8673. (interactive)
  8674. (save-excursion
  8675. (catch 'exit
  8676. (let ((pos (point)))
  8677. (skip-chars-backward "^[<\r\n")
  8678. (skip-chars-backward "<[")
  8679. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8680. (>= (match-end 0) pos)
  8681. (throw 'exit t))
  8682. (skip-chars-backward "^<[\r\n")
  8683. (skip-chars-backward "<[")
  8684. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8685. (>= (match-end 0) pos)
  8686. (throw 'exit t)))
  8687. nil)))
  8688. (defun org-get-repeat ()
  8689. "Check if there is a deadline/schedule with repeater in this entry."
  8690. (save-match-data
  8691. (save-excursion
  8692. (org-back-to-heading t)
  8693. (if (re-search-forward
  8694. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8695. (match-string 1)))))
  8696. (defvar org-last-changed-timestamp)
  8697. (defvar org-last-inserted-timestamp)
  8698. (defvar org-log-post-message)
  8699. (defvar org-log-note-purpose)
  8700. (defvar org-log-note-how)
  8701. (defvar org-log-note-extra)
  8702. (defun org-auto-repeat-maybe (done-word)
  8703. "Check if the current headline contains a repeated deadline/schedule.
  8704. If yes, set TODO state back to what it was and change the base date
  8705. of repeating deadline/scheduled time stamps to new date.
  8706. This function is run automatically after each state change to a DONE state."
  8707. ;; last-state is dynamically scoped into this function
  8708. (let* ((repeat (org-get-repeat))
  8709. (aa (assoc last-state org-todo-kwd-alist))
  8710. (interpret (nth 1 aa))
  8711. (head (nth 2 aa))
  8712. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8713. (msg "Entry repeats: ")
  8714. (org-log-done nil)
  8715. (org-todo-log-states nil)
  8716. (nshiftmax 10) (nshift 0)
  8717. re type n what ts time)
  8718. (when repeat
  8719. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8720. (org-todo (if (eq interpret 'type) last-state head))
  8721. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8722. (org-time-stamp-format t t)))
  8723. (when org-log-repeat
  8724. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8725. (memq 'org-add-log-note post-command-hook))
  8726. ;; OK, we are already setup for some record
  8727. (if (eq org-log-repeat 'note)
  8728. ;; make sure we take a note, not only a time stamp
  8729. (setq org-log-note-how 'note))
  8730. ;; Set up for taking a record
  8731. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8732. last-state
  8733. 'findpos org-log-repeat)))
  8734. (org-back-to-heading t)
  8735. (org-add-planning-info nil nil 'closed)
  8736. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8737. org-deadline-time-regexp "\\)\\|\\("
  8738. org-ts-regexp "\\)"))
  8739. (while (re-search-forward
  8740. re (save-excursion (outline-next-heading) (point)) t)
  8741. (setq type (if (match-end 1) org-scheduled-string
  8742. (if (match-end 3) org-deadline-string "Plain:"))
  8743. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8744. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8745. (setq n (string-to-number (match-string 2 ts))
  8746. what (match-string 3 ts))
  8747. (if (equal what "w") (setq n (* n 7) what "d"))
  8748. ;; Preparation, see if we need to modify the start date for the change
  8749. (when (match-end 1)
  8750. (setq time (save-match-data (org-time-string-to-time ts)))
  8751. (cond
  8752. ((equal (match-string 1 ts) ".")
  8753. ;; Shift starting date to today
  8754. (org-timestamp-change
  8755. (- (time-to-days (current-time)) (time-to-days time))
  8756. 'day))
  8757. ((equal (match-string 1 ts) "+")
  8758. (while (or (= nshift 0)
  8759. (<= (time-to-days time) (time-to-days (current-time))))
  8760. (when (= (incf nshift) nshiftmax)
  8761. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8762. (error "Abort")))
  8763. (org-timestamp-change n (cdr (assoc what whata)))
  8764. (org-at-timestamp-p t)
  8765. (setq ts (match-string 1))
  8766. (setq time (save-match-data (org-time-string-to-time ts))))
  8767. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8768. ;; rematch, so that we have everything in place for the real shift
  8769. (org-at-timestamp-p t)
  8770. (setq ts (match-string 1))
  8771. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8772. (org-timestamp-change n (cdr (assoc what whata)))
  8773. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8774. (setq org-log-post-message msg)
  8775. (message "%s" msg))))
  8776. (defun org-show-todo-tree (arg)
  8777. "Make a compact tree which shows all headlines marked with TODO.
  8778. The tree will show the lines where the regexp matches, and all higher
  8779. headlines above the match.
  8780. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8781. With a numeric prefix N, construct a sparse tree for the Nth element
  8782. of `org-todo-keywords-1'."
  8783. (interactive "P")
  8784. (let ((case-fold-search nil)
  8785. (kwd-re
  8786. (cond ((null arg) org-not-done-regexp)
  8787. ((equal arg '(4))
  8788. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8789. (mapcar 'list org-todo-keywords-1))))
  8790. (concat "\\("
  8791. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8792. "\\)\\>")))
  8793. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8794. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8795. org-todo-keywords-1)))
  8796. (t (error "Invalid prefix argument: %s" arg)))))
  8797. (message "%d TODO entries found"
  8798. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8799. (defun org-deadline (&optional remove time)
  8800. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8801. With argument REMOVE, remove any deadline from the item.
  8802. When TIME is set, it should be an internal time specification, and the
  8803. scheduling will use the corresponding date."
  8804. (interactive "P")
  8805. (if remove
  8806. (progn
  8807. (org-remove-timestamp-with-keyword org-deadline-string)
  8808. (message "Item no longer has a deadline."))
  8809. (if (org-get-repeat)
  8810. (error "Cannot change deadline on task with repeater, please do that by hand")
  8811. (org-add-planning-info 'deadline time 'closed)
  8812. (message "Deadline on %s" org-last-inserted-timestamp))))
  8813. (defun org-schedule (&optional remove time)
  8814. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8815. With argument REMOVE, remove any scheduling date from the item.
  8816. When TIME is set, it should be an internal time specification, and the
  8817. scheduling will use the corresponding date."
  8818. (interactive "P")
  8819. (if remove
  8820. (progn
  8821. (org-remove-timestamp-with-keyword org-scheduled-string)
  8822. (message "Item is no longer scheduled."))
  8823. (if (org-get-repeat)
  8824. (error "Cannot reschedule task with repeater, please do that by hand")
  8825. (org-add-planning-info 'scheduled time 'closed)
  8826. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8827. (defun org-get-scheduled-time (pom &optional inherit)
  8828. "Get the scheduled time as a time tuple, of a format suitable
  8829. for calling org-schedule with, or if there is no scheduling,
  8830. returns nil."
  8831. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8832. (when time
  8833. (apply 'encode-time (org-parse-time-string time)))))
  8834. (defun org-get-deadline-time (pom &optional inherit)
  8835. "Get the deadine as a time tuple, of a format suitable for
  8836. calling org-deadlin with, or if there is no scheduling, returns
  8837. nil."
  8838. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8839. (when time
  8840. (apply 'encode-time (org-parse-time-string time)))))
  8841. (defun org-remove-timestamp-with-keyword (keyword)
  8842. "Remove all time stamps with KEYWORD in the current entry."
  8843. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8844. beg)
  8845. (save-excursion
  8846. (org-back-to-heading t)
  8847. (setq beg (point))
  8848. (org-end-of-subtree t t)
  8849. (while (re-search-backward re beg t)
  8850. (replace-match "")
  8851. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8852. (equal (char-before) ?\ ))
  8853. (backward-delete-char 1)
  8854. (if (string-match "^[ \t]*$" (buffer-substring
  8855. (point-at-bol) (point-at-eol)))
  8856. (delete-region (point-at-bol)
  8857. (min (point-max) (1+ (point-at-eol))))))))))
  8858. (defun org-add-planning-info (what &optional time &rest remove)
  8859. "Insert new timestamp with keyword in the line directly after the headline.
  8860. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8861. If non is given, the user is prompted for a date.
  8862. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8863. be removed."
  8864. (interactive)
  8865. (let (org-time-was-given org-end-time-was-given ts
  8866. end default-time default-input)
  8867. (catch 'exit
  8868. (when (and (not time) (memq what '(scheduled deadline)))
  8869. ;; Try to get a default date/time from existing timestamp
  8870. (save-excursion
  8871. (org-back-to-heading t)
  8872. (setq end (save-excursion (outline-next-heading) (point)))
  8873. (when (re-search-forward (if (eq what 'scheduled)
  8874. org-scheduled-time-regexp
  8875. org-deadline-time-regexp)
  8876. end t)
  8877. (setq ts (match-string 1)
  8878. default-time
  8879. (apply 'encode-time (org-parse-time-string ts))
  8880. default-input (and ts (org-get-compact-tod ts))))))
  8881. (when what
  8882. ;; If necessary, get the time from the user
  8883. (setq time (or time (org-read-date nil 'to-time nil nil
  8884. default-time default-input))))
  8885. (when (and org-insert-labeled-timestamps-at-point
  8886. (member what '(scheduled deadline)))
  8887. (insert
  8888. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8889. (org-insert-time-stamp time org-time-was-given
  8890. nil nil nil (list org-end-time-was-given))
  8891. (setq what nil))
  8892. (save-excursion
  8893. (save-restriction
  8894. (let (col list elt ts buffer-invisibility-spec)
  8895. (org-back-to-heading t)
  8896. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8897. (goto-char (match-end 1))
  8898. (setq col (current-column))
  8899. (goto-char (match-end 0))
  8900. (if (eobp) (insert "\n") (forward-char 1))
  8901. (when (and (not what)
  8902. (not (looking-at
  8903. (concat "[ \t]*"
  8904. org-keyword-time-not-clock-regexp))))
  8905. ;; Nothing to add, nothing to remove...... :-)
  8906. (throw 'exit nil))
  8907. (if (and (not (looking-at outline-regexp))
  8908. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8909. "[^\r\n]*"))
  8910. (not (equal (match-string 1) org-clock-string)))
  8911. (narrow-to-region (match-beginning 0) (match-end 0))
  8912. (insert-before-markers "\n")
  8913. (backward-char 1)
  8914. (narrow-to-region (point) (point))
  8915. (and org-adapt-indentation (org-indent-to-column col)))
  8916. ;; Check if we have to remove something.
  8917. (setq list (cons what remove))
  8918. (while list
  8919. (setq elt (pop list))
  8920. (goto-char (point-min))
  8921. (when (or (and (eq elt 'scheduled)
  8922. (re-search-forward org-scheduled-time-regexp nil t))
  8923. (and (eq elt 'deadline)
  8924. (re-search-forward org-deadline-time-regexp nil t))
  8925. (and (eq elt 'closed)
  8926. (re-search-forward org-closed-time-regexp nil t)))
  8927. (replace-match "")
  8928. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8929. (if (looking-at " +") (replace-match ""))))
  8930. (goto-char (point-max))
  8931. (when what
  8932. (insert
  8933. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8934. (cond ((eq what 'scheduled) org-scheduled-string)
  8935. ((eq what 'deadline) org-deadline-string)
  8936. ((eq what 'closed) org-closed-string))
  8937. " ")
  8938. (setq ts (org-insert-time-stamp
  8939. time
  8940. (or org-time-was-given
  8941. (and (eq what 'closed) org-log-done-with-time))
  8942. (eq what 'closed)
  8943. nil nil (list org-end-time-was-given)))
  8944. (end-of-line 1))
  8945. (goto-char (point-min))
  8946. (widen)
  8947. (if (and (looking-at "[ \t]+\n")
  8948. (equal (char-before) ?\n))
  8949. (delete-region (1- (point)) (point-at-eol)))
  8950. ts))))))
  8951. (defvar org-log-note-marker (make-marker))
  8952. (defvar org-log-note-purpose nil)
  8953. (defvar org-log-note-state nil)
  8954. (defvar org-log-note-previous-state nil)
  8955. (defvar org-log-note-how nil)
  8956. (defvar org-log-note-extra nil)
  8957. (defvar org-log-note-window-configuration nil)
  8958. (defvar org-log-note-return-to (make-marker))
  8959. (defvar org-log-post-message nil
  8960. "Message to be displayed after a log note has been stored.
  8961. The auto-repeater uses this.")
  8962. (defun org-add-note ()
  8963. "Add a note to the current entry.
  8964. This is done in the same way as adding a state change note."
  8965. (interactive)
  8966. (org-add-log-setup 'note nil nil 'findpos nil))
  8967. (defvar org-property-end-re)
  8968. (defun org-add-log-setup (&optional purpose state prev-state
  8969. findpos how &optional extra)
  8970. "Set up the post command hook to take a note.
  8971. If this is about to TODO state change, the new state is expected in STATE.
  8972. When FINDPOS is non-nil, find the correct position for the note in
  8973. the current entry. If not, assume that it can be inserted at point.
  8974. HOW is an indicator what kind of note should be created.
  8975. EXTRA is additional text that will be inserted into the notes buffer."
  8976. (let* ((org-log-into-drawer (org-log-into-drawer))
  8977. (drawer (cond ((stringp org-log-into-drawer)
  8978. org-log-into-drawer)
  8979. (org-log-into-drawer "LOGBOOK")
  8980. (t nil))))
  8981. (save-restriction
  8982. (save-excursion
  8983. (when findpos
  8984. (org-back-to-heading t)
  8985. (narrow-to-region (point) (save-excursion
  8986. (outline-next-heading) (point)))
  8987. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8988. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8989. "[^\r\n]*\\)?"))
  8990. (goto-char (match-end 0))
  8991. (cond
  8992. (drawer
  8993. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8994. nil t)
  8995. (progn
  8996. (goto-char (match-end 0))
  8997. (or org-log-states-order-reversed
  8998. (and (re-search-forward org-property-end-re nil t)
  8999. (goto-char (1- (match-beginning 0))))))
  9000. (insert "\n:" drawer ":\n:END:")
  9001. (beginning-of-line 0)
  9002. (org-indent-line-function)
  9003. (beginning-of-line 2)
  9004. (org-indent-line-function)
  9005. (end-of-line 0)))
  9006. ((and org-log-state-notes-insert-after-drawers
  9007. (save-excursion
  9008. (forward-line) (looking-at org-drawer-regexp)))
  9009. (forward-line)
  9010. (while (looking-at org-drawer-regexp)
  9011. (goto-char (match-end 0))
  9012. (re-search-forward org-property-end-re (point-max) t)
  9013. (forward-line))
  9014. (forward-line -1)))
  9015. (unless org-log-states-order-reversed
  9016. (and (= (char-after) ?\n) (forward-char 1))
  9017. (org-skip-over-state-notes)
  9018. (skip-chars-backward " \t\n\r")))
  9019. (move-marker org-log-note-marker (point))
  9020. (setq org-log-note-purpose purpose
  9021. org-log-note-state state
  9022. org-log-note-previous-state prev-state
  9023. org-log-note-how how
  9024. org-log-note-extra extra)
  9025. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9026. (defun org-skip-over-state-notes ()
  9027. "Skip past the list of State notes in an entry."
  9028. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9029. (while (looking-at "[ \t]*- State")
  9030. (condition-case nil
  9031. (org-next-item)
  9032. (error (org-end-of-item)))))
  9033. (defun org-add-log-note (&optional purpose)
  9034. "Pop up a window for taking a note, and add this note later at point."
  9035. (remove-hook 'post-command-hook 'org-add-log-note)
  9036. (setq org-log-note-window-configuration (current-window-configuration))
  9037. (delete-other-windows)
  9038. (move-marker org-log-note-return-to (point))
  9039. (switch-to-buffer (marker-buffer org-log-note-marker))
  9040. (goto-char org-log-note-marker)
  9041. (org-switch-to-buffer-other-window "*Org Note*")
  9042. (erase-buffer)
  9043. (if (memq org-log-note-how '(time state))
  9044. (let (current-prefix-arg) (org-store-log-note))
  9045. (let ((org-inhibit-startup t)) (org-mode))
  9046. (insert (format "# Insert note for %s.
  9047. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9048. (cond
  9049. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9050. ((eq org-log-note-purpose 'done) "closed todo item")
  9051. ((eq org-log-note-purpose 'state)
  9052. (format "state change from \"%s\" to \"%s\""
  9053. (or org-log-note-previous-state "")
  9054. (or org-log-note-state "")))
  9055. ((eq org-log-note-purpose 'note)
  9056. "this entry")
  9057. (t (error "This should not happen")))))
  9058. (if org-log-note-extra (insert org-log-note-extra))
  9059. (org-set-local 'org-finish-function 'org-store-log-note)))
  9060. (defvar org-note-abort nil) ; dynamically scoped
  9061. (defun org-store-log-note ()
  9062. "Finish taking a log note, and insert it to where it belongs."
  9063. (let ((txt (buffer-string))
  9064. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9065. lines ind)
  9066. (kill-buffer (current-buffer))
  9067. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9068. (setq txt (replace-match "" t t txt)))
  9069. (if (string-match "\\s-+\\'" txt)
  9070. (setq txt (replace-match "" t t txt)))
  9071. (setq lines (org-split-string txt "\n"))
  9072. (when (and note (string-match "\\S-" note))
  9073. (setq note
  9074. (org-replace-escapes
  9075. note
  9076. (list (cons "%u" (user-login-name))
  9077. (cons "%U" user-full-name)
  9078. (cons "%t" (format-time-string
  9079. (org-time-stamp-format 'long 'inactive)
  9080. (current-time)))
  9081. (cons "%s" (if org-log-note-state
  9082. (concat "\"" org-log-note-state "\"")
  9083. ""))
  9084. (cons "%S" (if org-log-note-previous-state
  9085. (concat "\"" org-log-note-previous-state "\"")
  9086. "\"\"")))))
  9087. (if lines (setq note (concat note " \\\\")))
  9088. (push note lines))
  9089. (when (or current-prefix-arg org-note-abort)
  9090. (when org-log-into-drawer
  9091. (org-remove-empty-drawer-at
  9092. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9093. org-log-note-marker))
  9094. (setq lines nil))
  9095. (when lines
  9096. (save-excursion
  9097. (set-buffer (marker-buffer org-log-note-marker))
  9098. (save-excursion
  9099. (goto-char org-log-note-marker)
  9100. (move-marker org-log-note-marker nil)
  9101. (end-of-line 1)
  9102. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9103. (insert "- " (pop lines))
  9104. (org-indent-line-function)
  9105. (beginning-of-line 1)
  9106. (looking-at "[ \t]*")
  9107. (setq ind (concat (match-string 0) " "))
  9108. (end-of-line 1)
  9109. (while lines (insert "\n" ind (pop lines)))
  9110. (message "Note stored")
  9111. (org-back-to-heading t)
  9112. (org-cycle-hide-drawers 'children)))))
  9113. (set-window-configuration org-log-note-window-configuration)
  9114. (with-current-buffer (marker-buffer org-log-note-return-to)
  9115. (goto-char org-log-note-return-to))
  9116. (move-marker org-log-note-return-to nil)
  9117. (and org-log-post-message (message "%s" org-log-post-message)))
  9118. (defun org-remove-empty-drawer-at (drawer pos)
  9119. "Remove an emptyr DARWER drawer at position POS.
  9120. POS may also be a marker."
  9121. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9122. (save-excursion
  9123. (save-restriction
  9124. (widen)
  9125. (goto-char pos)
  9126. (if (org-in-regexp
  9127. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9128. (replace-match ""))))))
  9129. (defun org-sparse-tree (&optional arg)
  9130. "Create a sparse tree, prompt for the details.
  9131. This command can create sparse trees. You first need to select the type
  9132. of match used to create the tree:
  9133. t Show entries with a specific TODO keyword.
  9134. m Show entries selected by a tags/property match.
  9135. p Enter a property name and its value (both with completion on existing
  9136. names/values) and show entries with that property.
  9137. r Show entries matching a regular expression.
  9138. d Show deadlines due within `org-deadline-warning-days'.
  9139. b Show deadlines and scheduled items before a date.
  9140. a Show deadlines and scheduled items after a date."
  9141. (interactive "P")
  9142. (let (ans kwd value)
  9143. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9144. (setq ans (read-char-exclusive))
  9145. (cond
  9146. ((equal ans ?d)
  9147. (call-interactively 'org-check-deadlines))
  9148. ((equal ans ?b)
  9149. (call-interactively 'org-check-before-date))
  9150. ((equal ans ?a)
  9151. (call-interactively 'org-check-after-date))
  9152. ((equal ans ?t)
  9153. (org-show-todo-tree '(4)))
  9154. ((member ans '(?T ?m))
  9155. (call-interactively 'org-match-sparse-tree))
  9156. ((member ans '(?p ?P))
  9157. (setq kwd (org-ido-completing-read "Property: "
  9158. (mapcar 'list (org-buffer-property-keys))))
  9159. (setq value (org-ido-completing-read "Value: "
  9160. (mapcar 'list (org-property-values kwd))))
  9161. (unless (string-match "\\`{.*}\\'" value)
  9162. (setq value (concat "\"" value "\"")))
  9163. (org-match-sparse-tree arg (concat kwd "=" value)))
  9164. ((member ans '(?r ?R ?/))
  9165. (call-interactively 'org-occur))
  9166. (t (error "No such sparse tree command \"%c\"" ans)))))
  9167. (defvar org-occur-highlights nil
  9168. "List of overlays used for occur matches.")
  9169. (make-variable-buffer-local 'org-occur-highlights)
  9170. (defvar org-occur-parameters nil
  9171. "Parameters of the active org-occur calls.
  9172. This is a list, each call to org-occur pushes as cons cell,
  9173. containing the regular expression and the callback, onto the list.
  9174. The list can contain several entries if `org-occur' has been called
  9175. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9176. will only contain one set of parameters. When the highlights are
  9177. removed (for example with `C-c C-c', or with the next edit (depending
  9178. on `org-remove-highlights-with-change'), this variable is emptied
  9179. as well.")
  9180. (make-variable-buffer-local 'org-occur-parameters)
  9181. (defun org-occur (regexp &optional keep-previous callback)
  9182. "Make a compact tree which shows all matches of REGEXP.
  9183. The tree will show the lines where the regexp matches, and all higher
  9184. headlines above the match. It will also show the heading after the match,
  9185. to make sure editing the matching entry is easy.
  9186. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9187. call to `org-occur' will be kept, to allow stacking of calls to this
  9188. command.
  9189. If CALLBACK is non-nil, it is a function which is called to confirm
  9190. that the match should indeed be shown."
  9191. (interactive "sRegexp: \nP")
  9192. (unless keep-previous
  9193. (org-remove-occur-highlights nil nil t))
  9194. (push (cons regexp callback) org-occur-parameters)
  9195. (let ((cnt 0))
  9196. (save-excursion
  9197. (goto-char (point-min))
  9198. (if (or (not keep-previous) ; do not want to keep
  9199. (not org-occur-highlights)) ; no previous matches
  9200. ;; hide everything
  9201. (org-overview))
  9202. (while (re-search-forward regexp nil t)
  9203. (when (or (not callback)
  9204. (save-match-data (funcall callback)))
  9205. (setq cnt (1+ cnt))
  9206. (when org-highlight-sparse-tree-matches
  9207. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9208. (org-show-context 'occur-tree))))
  9209. (when org-remove-highlights-with-change
  9210. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9211. nil 'local))
  9212. (unless org-sparse-tree-open-archived-trees
  9213. (org-hide-archived-subtrees (point-min) (point-max)))
  9214. (run-hooks 'org-occur-hook)
  9215. (if (interactive-p)
  9216. (message "%d match(es) for regexp %s" cnt regexp))
  9217. cnt))
  9218. (defun org-show-context (&optional key)
  9219. "Make sure point and context and visible.
  9220. How much context is shown depends upon the variables
  9221. `org-show-hierarchy-above', `org-show-following-heading'. and
  9222. `org-show-siblings'."
  9223. (let ((heading-p (org-on-heading-p t))
  9224. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9225. (following-p (org-get-alist-option org-show-following-heading key))
  9226. (entry-p (org-get-alist-option org-show-entry-below key))
  9227. (siblings-p (org-get-alist-option org-show-siblings key)))
  9228. (catch 'exit
  9229. ;; Show heading or entry text
  9230. (if (and heading-p (not entry-p))
  9231. (org-flag-heading nil) ; only show the heading
  9232. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9233. (org-show-hidden-entry))) ; show entire entry
  9234. (when following-p
  9235. ;; Show next sibling, or heading below text
  9236. (save-excursion
  9237. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9238. (org-flag-heading nil))))
  9239. (when siblings-p (org-show-siblings))
  9240. (when hierarchy-p
  9241. ;; show all higher headings, possibly with siblings
  9242. (save-excursion
  9243. (while (and (condition-case nil
  9244. (progn (org-up-heading-all 1) t)
  9245. (error nil))
  9246. (not (bobp)))
  9247. (org-flag-heading nil)
  9248. (when siblings-p (org-show-siblings))))))))
  9249. (defun org-reveal (&optional siblings)
  9250. "Show current entry, hierarchy above it, and the following headline.
  9251. This can be used to show a consistent set of context around locations
  9252. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9253. not t for the search context.
  9254. With optional argument SIBLINGS, on each level of the hierarchy all
  9255. siblings are shown. This repairs the tree structure to what it would
  9256. look like when opened with hierarchical calls to `org-cycle'."
  9257. (interactive "P")
  9258. (let ((org-show-hierarchy-above t)
  9259. (org-show-following-heading t)
  9260. (org-show-siblings (if siblings t org-show-siblings)))
  9261. (org-show-context nil)))
  9262. (defun org-highlight-new-match (beg end)
  9263. "Highlight from BEG to END and mark the highlight is an occur headline."
  9264. (let ((ov (org-make-overlay beg end)))
  9265. (org-overlay-put ov 'face 'secondary-selection)
  9266. (push ov org-occur-highlights)))
  9267. (defun org-remove-occur-highlights (&optional beg end noremove)
  9268. "Remove the occur highlights from the buffer.
  9269. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9270. from the `before-change-functions' in the current buffer."
  9271. (interactive)
  9272. (unless org-inhibit-highlight-removal
  9273. (mapc 'org-delete-overlay org-occur-highlights)
  9274. (setq org-occur-highlights nil)
  9275. (setq org-occur-parameters nil)
  9276. (unless noremove
  9277. (remove-hook 'before-change-functions
  9278. 'org-remove-occur-highlights 'local))))
  9279. ;;;; Priorities
  9280. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9281. "Regular expression matching the priority indicator.")
  9282. (defvar org-remove-priority-next-time nil)
  9283. (defun org-priority-up ()
  9284. "Increase the priority of the current item."
  9285. (interactive)
  9286. (org-priority 'up))
  9287. (defun org-priority-down ()
  9288. "Decrease the priority of the current item."
  9289. (interactive)
  9290. (org-priority 'down))
  9291. (defun org-priority (&optional action)
  9292. "Change the priority of an item by ARG.
  9293. ACTION can be `set', `up', `down', or a character."
  9294. (interactive)
  9295. (unless org-enable-priority-commands
  9296. (error "Priority commands are disabled"))
  9297. (setq action (or action 'set))
  9298. (let (current new news have remove)
  9299. (save-excursion
  9300. (org-back-to-heading t)
  9301. (if (looking-at org-priority-regexp)
  9302. (setq current (string-to-char (match-string 2))
  9303. have t)
  9304. (setq current org-default-priority))
  9305. (cond
  9306. ((or (eq action 'set)
  9307. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9308. (if (not (eq action 'set))
  9309. (setq new action)
  9310. (message "Priority %c-%c, SPC to remove: "
  9311. org-highest-priority org-lowest-priority)
  9312. (setq new (read-char-exclusive)))
  9313. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9314. (= (upcase org-lowest-priority) org-lowest-priority))
  9315. (setq new (upcase new)))
  9316. (cond ((equal new ?\ ) (setq remove t))
  9317. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9318. (error "Priority must be between `%c' and `%c'"
  9319. org-highest-priority org-lowest-priority))))
  9320. ((eq action 'up)
  9321. (if (and (not have) (eq last-command this-command))
  9322. (setq new org-lowest-priority)
  9323. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9324. org-default-priority (1- current)))))
  9325. ((eq action 'down)
  9326. (if (and (not have) (eq last-command this-command))
  9327. (setq new org-highest-priority)
  9328. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9329. org-default-priority (1+ current)))))
  9330. (t (error "Invalid action")))
  9331. (if (or (< (upcase new) org-highest-priority)
  9332. (> (upcase new) org-lowest-priority))
  9333. (setq remove t))
  9334. (setq news (format "%c" new))
  9335. (if have
  9336. (if remove
  9337. (replace-match "" t t nil 1)
  9338. (replace-match news t t nil 2))
  9339. (if remove
  9340. (error "No priority cookie found in line")
  9341. (let ((case-fold-search nil))
  9342. (looking-at org-todo-line-regexp))
  9343. (if (match-end 2)
  9344. (progn
  9345. (goto-char (match-end 2))
  9346. (insert " [#" news "]"))
  9347. (goto-char (match-beginning 3))
  9348. (insert "[#" news "] ")))))
  9349. (org-preserve-lc (org-set-tags nil 'align))
  9350. (if remove
  9351. (message "Priority removed")
  9352. (message "Priority of current item set to %s" news))))
  9353. (defun org-get-priority (s)
  9354. "Find priority cookie and return priority."
  9355. (save-match-data
  9356. (if (not (string-match org-priority-regexp s))
  9357. (* 1000 (- org-lowest-priority org-default-priority))
  9358. (* 1000 (- org-lowest-priority
  9359. (string-to-char (match-string 2 s)))))))
  9360. ;;;; Tags
  9361. (defvar org-agenda-archives-mode)
  9362. (defvar org-map-continue-from nil
  9363. "Position from where mapping should continue.
  9364. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9365. (defvar org-scanner-tags nil
  9366. "The current tag list while the tags scanner is running.")
  9367. (defvar org-trust-scanner-tags nil
  9368. "Should `org-get-tags-at' use the tags fro the scanner.
  9369. This is for internal dynamical scoping only.
  9370. When this is non-nil, the function `org-get-tags-at' will return the value
  9371. of `org-scanner-tags' instead of building the list by itself. This
  9372. can lead to large speed-ups when the tags scanner is used in a file with
  9373. many entries, and when the list of tags is retrieved, for example to
  9374. obtain a list of properties. Building the tags list for each entry in such
  9375. a file becomes an N^2 operation - but with this variable set, it scales
  9376. as N.")
  9377. (defun org-scan-tags (action matcher &optional todo-only)
  9378. "Scan headline tags with inheritance and produce output ACTION.
  9379. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9380. or `agenda' to produce an entry list for an agenda view. It can also be
  9381. a Lisp form or a function that should be called at each matched headline, in
  9382. this case the return value is a list of all return values from these calls.
  9383. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9384. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9385. only lines with a TODO keyword are included in the output."
  9386. (require 'org-agenda)
  9387. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9388. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9389. (org-re
  9390. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9391. (props (list 'face 'default
  9392. 'done-face 'org-agenda-done
  9393. 'undone-face 'default
  9394. 'mouse-face 'highlight
  9395. 'org-not-done-regexp org-not-done-regexp
  9396. 'org-todo-regexp org-todo-regexp
  9397. 'keymap org-agenda-keymap
  9398. 'help-echo
  9399. (format "mouse-2 or RET jump to org file %s"
  9400. (abbreviate-file-name
  9401. (or (buffer-file-name (buffer-base-buffer))
  9402. (buffer-name (buffer-base-buffer)))))))
  9403. (case-fold-search nil)
  9404. (org-map-continue-from nil)
  9405. lspos tags tags-list
  9406. (tags-alist (list (cons 0 org-file-tags)))
  9407. (llast 0) rtn rtn1 level category i txt
  9408. todo marker entry priority)
  9409. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9410. (setq action (list 'lambda nil action)))
  9411. (save-excursion
  9412. (goto-char (point-min))
  9413. (when (eq action 'sparse-tree)
  9414. (org-overview)
  9415. (org-remove-occur-highlights))
  9416. (while (re-search-forward re nil t)
  9417. (catch :skip
  9418. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9419. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9420. (goto-char (setq lspos (match-beginning 0)))
  9421. (setq level (org-reduced-level (funcall outline-level))
  9422. category (org-get-category))
  9423. (setq i llast llast level)
  9424. ;; remove tag lists from same and sublevels
  9425. (while (>= i level)
  9426. (when (setq entry (assoc i tags-alist))
  9427. (setq tags-alist (delete entry tags-alist)))
  9428. (setq i (1- i)))
  9429. ;; add the next tags
  9430. (when tags
  9431. (setq tags (org-split-string tags ":")
  9432. tags-alist
  9433. (cons (cons level tags) tags-alist)))
  9434. ;; compile tags for current headline
  9435. (setq tags-list
  9436. (if org-use-tag-inheritance
  9437. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9438. tags)
  9439. org-scanner-tags tags-list)
  9440. (when org-use-tag-inheritance
  9441. (setcdr (car tags-alist)
  9442. (mapcar (lambda (x)
  9443. (setq x (copy-sequence x))
  9444. (org-add-prop-inherited x))
  9445. (cdar tags-alist))))
  9446. (when (and tags org-use-tag-inheritance
  9447. (or (not (eq t org-use-tag-inheritance))
  9448. org-tags-exclude-from-inheritance))
  9449. ;; selective inheritance, remove uninherited ones
  9450. (setcdr (car tags-alist)
  9451. (org-remove-uniherited-tags (cdar tags-alist))))
  9452. (when (and (or (not todo-only)
  9453. (and (member todo org-not-done-keywords)
  9454. (or (not org-agenda-tags-todo-honor-ignore-options)
  9455. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9456. (let ((case-fold-search t)) (eval matcher))
  9457. (or
  9458. (not (member org-archive-tag tags-list))
  9459. ;; we have an archive tag, should we use this anyway?
  9460. (or (not org-agenda-skip-archived-trees)
  9461. (and (eq action 'agenda) org-agenda-archives-mode))))
  9462. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9463. ;; select this headline
  9464. (cond
  9465. ((eq action 'sparse-tree)
  9466. (and org-highlight-sparse-tree-matches
  9467. (org-get-heading) (match-end 0)
  9468. (org-highlight-new-match
  9469. (match-beginning 0) (match-beginning 1)))
  9470. (org-show-context 'tags-tree))
  9471. ((eq action 'agenda)
  9472. (setq txt (org-format-agenda-item
  9473. ""
  9474. (concat
  9475. (if (eq org-tags-match-list-sublevels 'indented)
  9476. (make-string (1- level) ?.) "")
  9477. (org-get-heading))
  9478. category
  9479. tags-list
  9480. )
  9481. priority (org-get-priority txt))
  9482. (goto-char lspos)
  9483. (setq marker (org-agenda-new-marker))
  9484. (org-add-props txt props
  9485. 'org-marker marker 'org-hd-marker marker 'org-category category
  9486. 'todo-state todo
  9487. 'priority priority 'type "tagsmatch")
  9488. (push txt rtn))
  9489. ((functionp action)
  9490. (setq org-map-continue-from nil)
  9491. (save-excursion
  9492. (setq rtn1 (funcall action))
  9493. (push rtn1 rtn)))
  9494. (t (error "Invalid action")))
  9495. ;; if we are to skip sublevels, jump to end of subtree
  9496. (unless org-tags-match-list-sublevels
  9497. (org-end-of-subtree t)
  9498. (backward-char 1))))
  9499. ;; Get the correct position from where to continue
  9500. (if org-map-continue-from
  9501. (goto-char org-map-continue-from)
  9502. (and (= (point) lspos) (end-of-line 1)))))
  9503. (when (and (eq action 'sparse-tree)
  9504. (not org-sparse-tree-open-archived-trees))
  9505. (org-hide-archived-subtrees (point-min) (point-max)))
  9506. (nreverse rtn)))
  9507. (defun org-remove-uniherited-tags (tags)
  9508. "Remove all tags that are not inherited from the list TAGS."
  9509. (cond
  9510. ((eq org-use-tag-inheritance t)
  9511. (if org-tags-exclude-from-inheritance
  9512. (org-delete-all org-tags-exclude-from-inheritance tags)
  9513. tags))
  9514. ((not org-use-tag-inheritance) nil)
  9515. ((stringp org-use-tag-inheritance)
  9516. (delq nil (mapcar
  9517. (lambda (x)
  9518. (if (and (string-match org-use-tag-inheritance x)
  9519. (not (member x org-tags-exclude-from-inheritance)))
  9520. x nil))
  9521. tags)))
  9522. ((listp org-use-tag-inheritance)
  9523. (delq nil (mapcar
  9524. (lambda (x)
  9525. (if (member x org-use-tag-inheritance) x nil))
  9526. tags)))))
  9527. (defvar todo-only) ;; dynamically scoped
  9528. (defun org-match-sparse-tree (&optional todo-only match)
  9529. "Create a sparse tree according to tags string MATCH.
  9530. MATCH can contain positive and negative selection of tags, like
  9531. \"+WORK+URGENT-WITHBOSS\".
  9532. If optional argument TODO-ONLY is non-nil, only select lines that are
  9533. also TODO lines."
  9534. (interactive "P")
  9535. (org-prepare-agenda-buffers (list (current-buffer)))
  9536. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9537. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9538. (defvar org-cached-props nil)
  9539. (defun org-cached-entry-get (pom property)
  9540. (if (or (eq t org-use-property-inheritance)
  9541. (and (stringp org-use-property-inheritance)
  9542. (string-match org-use-property-inheritance property))
  9543. (and (listp org-use-property-inheritance)
  9544. (member property org-use-property-inheritance)))
  9545. ;; Caching is not possible, check it directly
  9546. (org-entry-get pom property 'inherit)
  9547. ;; Get all properties, so that we can do complicated checks easily
  9548. (cdr (assoc property (or org-cached-props
  9549. (setq org-cached-props
  9550. (org-entry-properties pom)))))))
  9551. (defun org-global-tags-completion-table (&optional files)
  9552. "Return the list of all tags in all agenda buffer/files."
  9553. (save-excursion
  9554. (org-uniquify
  9555. (delq nil
  9556. (apply 'append
  9557. (mapcar
  9558. (lambda (file)
  9559. (set-buffer (find-file-noselect file))
  9560. (append (org-get-buffer-tags)
  9561. (mapcar (lambda (x) (if (stringp (car-safe x))
  9562. (list (car-safe x)) nil))
  9563. org-tag-alist)))
  9564. (if (and files (car files))
  9565. files
  9566. (org-agenda-files))))))))
  9567. (defun org-make-tags-matcher (match)
  9568. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9569. ;; todo-only is scoped dynamically into this function, and the function
  9570. ;; may change it if the matcher asks for it.
  9571. (unless match
  9572. ;; Get a new match request, with completion
  9573. (let ((org-last-tags-completion-table
  9574. (org-global-tags-completion-table)))
  9575. (setq match (org-completing-read-no-ido
  9576. "Match: " 'org-tags-completion-function nil nil nil
  9577. 'org-tags-history))))
  9578. ;; Parse the string and create a lisp form
  9579. (let ((match0 match)
  9580. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9581. minus tag mm
  9582. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9583. orterms term orlist re-p str-p level-p level-op time-p
  9584. prop-p pn pv po cat-p gv rest)
  9585. (if (string-match "/+" match)
  9586. ;; match contains also a todo-matching request
  9587. (progn
  9588. (setq tagsmatch (substring match 0 (match-beginning 0))
  9589. todomatch (substring match (match-end 0)))
  9590. (if (string-match "^!" todomatch)
  9591. (setq todo-only t todomatch (substring todomatch 1)))
  9592. (if (string-match "^\\s-*$" todomatch)
  9593. (setq todomatch nil)))
  9594. ;; only matching tags
  9595. (setq tagsmatch match todomatch nil))
  9596. ;; Make the tags matcher
  9597. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9598. (setq tagsmatcher t)
  9599. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9600. (while (setq term (pop orterms))
  9601. (while (and (equal (substring term -1) "\\") orterms)
  9602. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9603. (while (string-match re term)
  9604. (setq rest (substring term (match-end 0))
  9605. minus (and (match-end 1)
  9606. (equal (match-string 1 term) "-"))
  9607. tag (match-string 2 term)
  9608. re-p (equal (string-to-char tag) ?{)
  9609. level-p (match-end 4)
  9610. prop-p (match-end 5)
  9611. mm (cond
  9612. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9613. (level-p
  9614. (setq level-op (org-op-to-function (match-string 3 term)))
  9615. `(,level-op level ,(string-to-number
  9616. (match-string 4 term))))
  9617. (prop-p
  9618. (setq pn (match-string 5 term)
  9619. po (match-string 6 term)
  9620. pv (match-string 7 term)
  9621. cat-p (equal pn "CATEGORY")
  9622. re-p (equal (string-to-char pv) ?{)
  9623. str-p (equal (string-to-char pv) ?\")
  9624. time-p (save-match-data
  9625. (string-match "^\"[[<].*[]>]\"$" pv))
  9626. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9627. (if time-p (setq pv (org-matcher-time pv)))
  9628. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9629. (cond
  9630. ((equal pn "CATEGORY")
  9631. (setq gv '(get-text-property (point) 'org-category)))
  9632. ((equal pn "TODO")
  9633. (setq gv 'todo))
  9634. (t
  9635. (setq gv `(org-cached-entry-get nil ,pn))))
  9636. (if re-p
  9637. (if (eq po 'org<>)
  9638. `(not (string-match ,pv (or ,gv "")))
  9639. `(string-match ,pv (or ,gv "")))
  9640. (if str-p
  9641. `(,po (or ,gv "") ,pv)
  9642. `(,po (string-to-number (or ,gv ""))
  9643. ,(string-to-number pv) ))))
  9644. (t `(member ,tag tags-list)))
  9645. mm (if minus (list 'not mm) mm)
  9646. term rest)
  9647. (push mm tagsmatcher))
  9648. (push (if (> (length tagsmatcher) 1)
  9649. (cons 'and tagsmatcher)
  9650. (car tagsmatcher))
  9651. orlist)
  9652. (setq tagsmatcher nil))
  9653. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9654. (setq tagsmatcher
  9655. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9656. ;; Make the todo matcher
  9657. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9658. (setq todomatcher t)
  9659. (setq orterms (org-split-string todomatch "|") orlist nil)
  9660. (while (setq term (pop orterms))
  9661. (while (string-match re term)
  9662. (setq minus (and (match-end 1)
  9663. (equal (match-string 1 term) "-"))
  9664. kwd (match-string 2 term)
  9665. re-p (equal (string-to-char kwd) ?{)
  9666. term (substring term (match-end 0))
  9667. mm (if re-p
  9668. `(string-match ,(substring kwd 1 -1) todo)
  9669. (list 'equal 'todo kwd))
  9670. mm (if minus (list 'not mm) mm))
  9671. (push mm todomatcher))
  9672. (push (if (> (length todomatcher) 1)
  9673. (cons 'and todomatcher)
  9674. (car todomatcher))
  9675. orlist)
  9676. (setq todomatcher nil))
  9677. (setq todomatcher (if (> (length orlist) 1)
  9678. (cons 'or orlist) (car orlist))))
  9679. ;; Return the string and lisp forms of the matcher
  9680. (setq matcher (if todomatcher
  9681. (list 'and tagsmatcher todomatcher)
  9682. tagsmatcher))
  9683. (cons match0 matcher)))
  9684. (defun org-op-to-function (op &optional stringp)
  9685. "Turn an operator into the appropriate function."
  9686. (setq op
  9687. (cond
  9688. ((equal op "<" ) '(< string< org-time<))
  9689. ((equal op ">" ) '(> org-string> org-time>))
  9690. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9691. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9692. ((member op '("=" "==")) '(= string= org-time=))
  9693. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9694. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9695. (defun org<> (a b) (not (= a b)))
  9696. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9697. (defun org-string>= (a b) (not (string< a b)))
  9698. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9699. (defun org-string<> (a b) (not (string= a b)))
  9700. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9701. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9702. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9703. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9704. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9705. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9706. (defun org-2ft (s)
  9707. "Convert S to a floating point time.
  9708. If S is already a number, just return it. If it is a string, parse
  9709. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9710. (cond
  9711. ((numberp s) s)
  9712. ((stringp s)
  9713. (condition-case nil
  9714. (float-time (apply 'encode-time (org-parse-time-string s)))
  9715. (error 0.)))
  9716. (t 0.)))
  9717. (defun org-time-today ()
  9718. "Time in seconds today at 0:00.
  9719. Returns the float number of seconds since the beginning of the
  9720. epoch to the beginning of today (00:00)."
  9721. (float-time (apply 'encode-time
  9722. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9723. (defun org-matcher-time (s)
  9724. "Interpret a time comparison value."
  9725. (save-match-data
  9726. (cond
  9727. ((string= s "<now>") (float-time))
  9728. ((string= s "<today>") (org-time-today))
  9729. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9730. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9731. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9732. (+ (org-time-today)
  9733. (* (string-to-number (match-string 1 s))
  9734. (cdr (assoc (match-string 2 s)
  9735. '(("d" . 86400.0) ("w" . 604800.0)
  9736. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9737. (t (org-2ft s)))))
  9738. (defun org-match-any-p (re list)
  9739. "Does re match any element of list?"
  9740. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9741. (delq nil list))
  9742. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9743. (defvar org-tags-overlay (org-make-overlay 1 1))
  9744. (org-detach-overlay org-tags-overlay)
  9745. (defun org-get-local-tags-at (&optional pos)
  9746. "Get a list of tags defined in the current headline."
  9747. (org-get-tags-at pos 'local))
  9748. (defun org-get-local-tags ()
  9749. "Get a list of tags defined in the current headline."
  9750. (org-get-tags-at nil 'local))
  9751. (defun org-get-tags-at (&optional pos local)
  9752. "Get a list of all headline tags applicable at POS.
  9753. POS defaults to point. If tags are inherited, the list contains
  9754. the targets in the same sequence as the headlines appear, i.e.
  9755. the tags of the current headline come last.
  9756. When LOCAL is non-nil, only return tags from the current headline,
  9757. ignore inherited ones."
  9758. (interactive)
  9759. (if (and org-trust-scanner-tags
  9760. (or (not pos) (equal pos (point)))
  9761. (not local))
  9762. org-scanner-tags
  9763. (let (tags ltags lastpos parent)
  9764. (save-excursion
  9765. (save-restriction
  9766. (widen)
  9767. (goto-char (or pos (point)))
  9768. (save-match-data
  9769. (catch 'done
  9770. (condition-case nil
  9771. (progn
  9772. (org-back-to-heading t)
  9773. (while (not (equal lastpos (point)))
  9774. (setq lastpos (point))
  9775. (when (looking-at
  9776. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9777. (setq ltags (org-split-string
  9778. (org-match-string-no-properties 1) ":"))
  9779. (when parent
  9780. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9781. (setq tags (append
  9782. (if parent
  9783. (org-remove-uniherited-tags ltags)
  9784. ltags)
  9785. tags)))
  9786. (or org-use-tag-inheritance (throw 'done t))
  9787. (if local (throw 'done t))
  9788. (or (org-up-heading-safe) (error nil))
  9789. (setq parent t)))
  9790. (error nil)))))
  9791. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9792. (defun org-add-prop-inherited (s)
  9793. (add-text-properties 0 (length s) '(inherited t) s)
  9794. s)
  9795. (defun org-toggle-tag (tag &optional onoff)
  9796. "Toggle the tag TAG for the current line.
  9797. If ONOFF is `on' or `off', don't toggle but set to this state."
  9798. (let (res current)
  9799. (save-excursion
  9800. (org-back-to-heading t)
  9801. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9802. (point-at-eol) t)
  9803. (progn
  9804. (setq current (match-string 1))
  9805. (replace-match ""))
  9806. (setq current ""))
  9807. (setq current (nreverse (org-split-string current ":")))
  9808. (cond
  9809. ((eq onoff 'on)
  9810. (setq res t)
  9811. (or (member tag current) (push tag current)))
  9812. ((eq onoff 'off)
  9813. (or (not (member tag current)) (setq current (delete tag current))))
  9814. (t (if (member tag current)
  9815. (setq current (delete tag current))
  9816. (setq res t)
  9817. (push tag current))))
  9818. (end-of-line 1)
  9819. (if current
  9820. (progn
  9821. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9822. (org-set-tags nil t))
  9823. (delete-horizontal-space))
  9824. (run-hooks 'org-after-tags-change-hook))
  9825. res))
  9826. (defun org-align-tags-here (to-col)
  9827. ;; Assumes that this is a headline
  9828. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9829. (beginning-of-line 1)
  9830. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9831. (< pos (match-beginning 2)))
  9832. (progn
  9833. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9834. (goto-char (match-beginning 1))
  9835. (insert " ")
  9836. (delete-region (point) (1+ (match-beginning 2)))
  9837. (setq ncol (max (1+ (current-column))
  9838. (1+ col)
  9839. (if (> to-col 0)
  9840. to-col
  9841. (- (abs to-col) tags-l))))
  9842. (setq p (point))
  9843. (insert (make-string (- ncol (current-column)) ?\ ))
  9844. (setq ncol (current-column))
  9845. (when indent-tabs-mode (tabify p (point-at-eol)))
  9846. (org-move-to-column (min ncol col) t))
  9847. (goto-char pos))))
  9848. (defun org-set-tags-command (&optional arg just-align)
  9849. "Call the set-tags command for the current entry."
  9850. (interactive "P")
  9851. (if (org-on-heading-p)
  9852. (org-set-tags arg just-align)
  9853. (save-excursion
  9854. (org-back-to-heading t)
  9855. (org-set-tags arg just-align))))
  9856. (defun org-set-tags (&optional arg just-align)
  9857. "Set the tags for the current headline.
  9858. With prefix ARG, realign all tags in headings in the current buffer."
  9859. (interactive "P")
  9860. (let* ((re (concat "^" outline-regexp))
  9861. (current (org-get-tags-string))
  9862. (col (current-column))
  9863. (org-setting-tags t)
  9864. table current-tags inherited-tags ; computed below when needed
  9865. tags p0 c0 c1 rpl)
  9866. (if arg
  9867. (save-excursion
  9868. (goto-char (point-min))
  9869. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9870. (while (re-search-forward re nil t)
  9871. (org-set-tags nil t)
  9872. (end-of-line 1)))
  9873. (message "All tags realigned to column %d" org-tags-column))
  9874. (if just-align
  9875. (setq tags current)
  9876. ;; Get a new set of tags from the user
  9877. (save-excursion
  9878. (setq table (append org-tag-persistent-alist
  9879. (or org-tag-alist (org-get-buffer-tags)))
  9880. org-last-tags-completion-table table
  9881. current-tags (org-split-string current ":")
  9882. inherited-tags (nreverse
  9883. (nthcdr (length current-tags)
  9884. (nreverse (org-get-tags-at))))
  9885. tags
  9886. (if (or (eq t org-use-fast-tag-selection)
  9887. (and org-use-fast-tag-selection
  9888. (delq nil (mapcar 'cdr table))))
  9889. (org-fast-tag-selection
  9890. current-tags inherited-tags table
  9891. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9892. (let ((org-add-colon-after-tag-completion t))
  9893. (org-trim
  9894. (org-without-partial-completion
  9895. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9896. nil nil current 'org-tags-history)))))))
  9897. (while (string-match "[-+&]+" tags)
  9898. ;; No boolean logic, just a list
  9899. (setq tags (replace-match ":" t t tags))))
  9900. (if org-tags-sort-function
  9901. (setq tags (mapconcat 'identity
  9902. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  9903. org-tags-sort-function) ":")))
  9904. (if (string-match "\\`[\t ]*\\'" tags)
  9905. (setq tags "")
  9906. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9907. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9908. ;; Insert new tags at the correct column
  9909. (beginning-of-line 1)
  9910. (cond
  9911. ((and (equal current "") (equal tags "")))
  9912. ((re-search-forward
  9913. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9914. (point-at-eol) t)
  9915. (if (equal tags "")
  9916. (setq rpl "")
  9917. (goto-char (match-beginning 0))
  9918. (setq c0 (current-column) p0 (point)
  9919. c1 (max (1+ c0) (if (> org-tags-column 0)
  9920. org-tags-column
  9921. (- (- org-tags-column) (length tags))))
  9922. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9923. (replace-match rpl t t)
  9924. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9925. tags)
  9926. (t (error "Tags alignment failed")))
  9927. (org-move-to-column col)
  9928. (unless just-align
  9929. (run-hooks 'org-after-tags-change-hook)))))
  9930. (defun org-change-tag-in-region (beg end tag off)
  9931. "Add or remove TAG for each entry in the region.
  9932. This works in the agenda, and also in an org-mode buffer."
  9933. (interactive
  9934. (list (region-beginning) (region-end)
  9935. (let ((org-last-tags-completion-table
  9936. (if (org-mode-p)
  9937. (org-get-buffer-tags)
  9938. (org-global-tags-completion-table))))
  9939. (org-ido-completing-read
  9940. "Tag: " 'org-tags-completion-function nil nil nil
  9941. 'org-tags-history))
  9942. (progn
  9943. (message "[s]et or [r]emove? ")
  9944. (equal (read-char-exclusive) ?r))))
  9945. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9946. (let ((agendap (equal major-mode 'org-agenda-mode))
  9947. l1 l2 m buf pos newhead (cnt 0))
  9948. (goto-char end)
  9949. (setq l2 (1- (org-current-line)))
  9950. (goto-char beg)
  9951. (setq l1 (org-current-line))
  9952. (loop for l from l1 to l2 do
  9953. (goto-line l)
  9954. (setq m (get-text-property (point) 'org-hd-marker))
  9955. (when (or (and (org-mode-p) (org-on-heading-p))
  9956. (and agendap m))
  9957. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9958. pos (if agendap m (point)))
  9959. (with-current-buffer buf
  9960. (save-excursion
  9961. (save-restriction
  9962. (goto-char pos)
  9963. (setq cnt (1+ cnt))
  9964. (org-toggle-tag tag (if off 'off 'on))
  9965. (setq newhead (org-get-heading)))))
  9966. (and agendap (org-agenda-change-all-lines newhead m))))
  9967. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9968. (defun org-tags-completion-function (string predicate &optional flag)
  9969. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9970. (confirm (lambda (x) (stringp (car x)))))
  9971. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9972. (setq s1 (match-string 1 string)
  9973. s2 (match-string 2 string))
  9974. (setq s1 "" s2 string))
  9975. (cond
  9976. ((eq flag nil)
  9977. ;; try completion
  9978. (setq rtn (try-completion s2 ctable confirm))
  9979. (if (stringp rtn)
  9980. (setq rtn
  9981. (concat s1 s2 (substring rtn (length s2))
  9982. (if (and org-add-colon-after-tag-completion
  9983. (assoc rtn ctable))
  9984. ":" ""))))
  9985. rtn)
  9986. ((eq flag t)
  9987. ;; all-completions
  9988. (all-completions s2 ctable confirm)
  9989. )
  9990. ((eq flag 'lambda)
  9991. ;; exact match?
  9992. (assoc s2 ctable)))
  9993. ))
  9994. (defun org-fast-tag-insert (kwd tags face &optional end)
  9995. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9996. (insert (format "%-12s" (concat kwd ":"))
  9997. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9998. (or end "")))
  9999. (defun org-fast-tag-show-exit (flag)
  10000. (save-excursion
  10001. (goto-line 3)
  10002. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10003. (replace-match ""))
  10004. (when flag
  10005. (end-of-line 1)
  10006. (org-move-to-column (- (window-width) 19) t)
  10007. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10008. (defun org-set-current-tags-overlay (current prefix)
  10009. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10010. (if (featurep 'xemacs)
  10011. (org-overlay-display org-tags-overlay (concat prefix s)
  10012. 'secondary-selection)
  10013. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10014. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10015. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10016. "Fast tag selection with single keys.
  10017. CURRENT is the current list of tags in the headline, INHERITED is the
  10018. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10019. possibly with grouping information. TODO-TABLE is a similar table with
  10020. TODO keywords, should these have keys assigned to them.
  10021. If the keys are nil, a-z are automatically assigned.
  10022. Returns the new tags string, or nil to not change the current settings."
  10023. (let* ((fulltable (append table todo-table))
  10024. (maxlen (apply 'max (mapcar
  10025. (lambda (x)
  10026. (if (stringp (car x)) (string-width (car x)) 0))
  10027. fulltable)))
  10028. (buf (current-buffer))
  10029. (expert (eq org-fast-tag-selection-single-key 'expert))
  10030. (buffer-tags nil)
  10031. (fwidth (+ maxlen 3 1 3))
  10032. (ncol (/ (- (window-width) 4) fwidth))
  10033. (i-face 'org-done)
  10034. (c-face 'org-todo)
  10035. tg cnt e c char c1 c2 ntable tbl rtn
  10036. ov-start ov-end ov-prefix
  10037. (exit-after-next org-fast-tag-selection-single-key)
  10038. (done-keywords org-done-keywords)
  10039. groups ingroup)
  10040. (save-excursion
  10041. (beginning-of-line 1)
  10042. (if (looking-at
  10043. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10044. (setq ov-start (match-beginning 1)
  10045. ov-end (match-end 1)
  10046. ov-prefix "")
  10047. (setq ov-start (1- (point-at-eol))
  10048. ov-end (1+ ov-start))
  10049. (skip-chars-forward "^\n\r")
  10050. (setq ov-prefix
  10051. (concat
  10052. (buffer-substring (1- (point)) (point))
  10053. (if (> (current-column) org-tags-column)
  10054. " "
  10055. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10056. (org-move-overlay org-tags-overlay ov-start ov-end)
  10057. (save-window-excursion
  10058. (if expert
  10059. (set-buffer (get-buffer-create " *Org tags*"))
  10060. (delete-other-windows)
  10061. (split-window-vertically)
  10062. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10063. (erase-buffer)
  10064. (org-set-local 'org-done-keywords done-keywords)
  10065. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10066. (org-fast-tag-insert "Current" current c-face "\n\n")
  10067. (org-fast-tag-show-exit exit-after-next)
  10068. (org-set-current-tags-overlay current ov-prefix)
  10069. (setq tbl fulltable char ?a cnt 0)
  10070. (while (setq e (pop tbl))
  10071. (cond
  10072. ((equal e '(:startgroup))
  10073. (push '() groups) (setq ingroup t)
  10074. (when (not (= cnt 0))
  10075. (setq cnt 0)
  10076. (insert "\n"))
  10077. (insert "{ "))
  10078. ((equal e '(:endgroup))
  10079. (setq ingroup nil cnt 0)
  10080. (insert "}\n"))
  10081. ((equal e '(:newline))
  10082. (when (not (= cnt 0))
  10083. (setq cnt 0)
  10084. (insert "\n")
  10085. (setq e (car tbl))
  10086. (while (equal (car tbl) '(:newline))
  10087. (insert "\n")
  10088. (setq tbl (cdr tbl)))))
  10089. (t
  10090. (setq tg (car e) c2 nil)
  10091. (if (cdr e)
  10092. (setq c (cdr e))
  10093. ;; automatically assign a character.
  10094. (setq c1 (string-to-char
  10095. (downcase (substring
  10096. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10097. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10098. (while (or (rassoc char ntable) (rassoc char table))
  10099. (setq char (1+ char)))
  10100. (setq c2 c1))
  10101. (setq c (or c2 char)))
  10102. (if ingroup (push tg (car groups)))
  10103. (setq tg (org-add-props tg nil 'face
  10104. (cond
  10105. ((not (assoc tg table))
  10106. (org-get-todo-face tg))
  10107. ((member tg current) c-face)
  10108. ((member tg inherited) i-face)
  10109. (t nil))))
  10110. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10111. (insert "[" c "] " tg (make-string
  10112. (- fwidth 4 (length tg)) ?\ ))
  10113. (push (cons tg c) ntable)
  10114. (when (= (setq cnt (1+ cnt)) ncol)
  10115. (insert "\n")
  10116. (if ingroup (insert " "))
  10117. (setq cnt 0)))))
  10118. (setq ntable (nreverse ntable))
  10119. (insert "\n")
  10120. (goto-char (point-min))
  10121. (if (not expert) (org-fit-window-to-buffer))
  10122. (setq rtn
  10123. (catch 'exit
  10124. (while t
  10125. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10126. (if groups " [!] no groups" " [!]groups")
  10127. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10128. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10129. (cond
  10130. ((= c ?\r) (throw 'exit t))
  10131. ((= c ?!)
  10132. (setq groups (not groups))
  10133. (goto-char (point-min))
  10134. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10135. ((= c ?\C-c)
  10136. (if (not expert)
  10137. (org-fast-tag-show-exit
  10138. (setq exit-after-next (not exit-after-next)))
  10139. (setq expert nil)
  10140. (delete-other-windows)
  10141. (split-window-vertically)
  10142. (org-switch-to-buffer-other-window " *Org tags*")
  10143. (org-fit-window-to-buffer)))
  10144. ((or (= c ?\C-g)
  10145. (and (= c ?q) (not (rassoc c ntable))))
  10146. (org-detach-overlay org-tags-overlay)
  10147. (setq quit-flag t))
  10148. ((= c ?\ )
  10149. (setq current nil)
  10150. (if exit-after-next (setq exit-after-next 'now)))
  10151. ((= c ?\t)
  10152. (condition-case nil
  10153. (setq tg (org-ido-completing-read
  10154. "Tag: "
  10155. (or buffer-tags
  10156. (with-current-buffer buf
  10157. (org-get-buffer-tags)))))
  10158. (quit (setq tg "")))
  10159. (when (string-match "\\S-" tg)
  10160. (add-to-list 'buffer-tags (list tg))
  10161. (if (member tg current)
  10162. (setq current (delete tg current))
  10163. (push tg current)))
  10164. (if exit-after-next (setq exit-after-next 'now)))
  10165. ((setq e (rassoc c todo-table) tg (car e))
  10166. (with-current-buffer buf
  10167. (save-excursion (org-todo tg)))
  10168. (if exit-after-next (setq exit-after-next 'now)))
  10169. ((setq e (rassoc c ntable) tg (car e))
  10170. (if (member tg current)
  10171. (setq current (delete tg current))
  10172. (loop for g in groups do
  10173. (if (member tg g)
  10174. (mapc (lambda (x)
  10175. (setq current (delete x current)))
  10176. g)))
  10177. (push tg current))
  10178. (if exit-after-next (setq exit-after-next 'now))))
  10179. ;; Create a sorted list
  10180. (setq current
  10181. (sort current
  10182. (lambda (a b)
  10183. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10184. (if (eq exit-after-next 'now) (throw 'exit t))
  10185. (goto-char (point-min))
  10186. (beginning-of-line 2)
  10187. (delete-region (point) (point-at-eol))
  10188. (org-fast-tag-insert "Current" current c-face)
  10189. (org-set-current-tags-overlay current ov-prefix)
  10190. (while (re-search-forward
  10191. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10192. (setq tg (match-string 1))
  10193. (add-text-properties
  10194. (match-beginning 1) (match-end 1)
  10195. (list 'face
  10196. (cond
  10197. ((member tg current) c-face)
  10198. ((member tg inherited) i-face)
  10199. (t (get-text-property (match-beginning 1) 'face))))))
  10200. (goto-char (point-min)))))
  10201. (org-detach-overlay org-tags-overlay)
  10202. (if rtn
  10203. (mapconcat 'identity current ":")
  10204. nil))))
  10205. (defun org-get-tags-string ()
  10206. "Get the TAGS string in the current headline."
  10207. (unless (org-on-heading-p t)
  10208. (error "Not on a heading"))
  10209. (save-excursion
  10210. (beginning-of-line 1)
  10211. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10212. (org-match-string-no-properties 1)
  10213. "")))
  10214. (defun org-get-tags ()
  10215. "Get the list of tags specified in the current headline."
  10216. (org-split-string (org-get-tags-string) ":"))
  10217. (defun org-get-buffer-tags ()
  10218. "Get a table of all tags used in the buffer, for completion."
  10219. (let (tags)
  10220. (save-excursion
  10221. (goto-char (point-min))
  10222. (while (re-search-forward
  10223. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10224. (when (equal (char-after (point-at-bol 0)) ?*)
  10225. (mapc (lambda (x) (add-to-list 'tags x))
  10226. (org-split-string (org-match-string-no-properties 1) ":")))))
  10227. (mapcar 'list tags)))
  10228. ;;;; The mapping API
  10229. ;;;###autoload
  10230. (defun org-map-entries (func &optional match scope &rest skip)
  10231. "Call FUNC at each headline selected by MATCH in SCOPE.
  10232. FUNC is a function or a lisp form. The function will be called without
  10233. arguments, with the cursor positioned at the beginning of the headline.
  10234. The return values of all calls to the function will be collected and
  10235. returned as a list.
  10236. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10237. does not need to preserve point. After evaluation, the cursor will be
  10238. moved to the end of the line (presumably of the headline of the
  10239. processed entry) and search continues from there. Under some
  10240. circumstances, this may not produce the wanted results. For example,
  10241. if you have removed (e.g. archived) the current (sub)tree it could
  10242. mean that the next entry will be skipped entirely. In such cases, you
  10243. can specify the position from where search should continue by making
  10244. FUNC set the variable `org-map-continue-from' to the desired buffer
  10245. position.
  10246. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10247. Only headlines that are matched by this query will be considered during
  10248. the iteration. When MATCH is nil or t, all headlines will be
  10249. visited by the iteration.
  10250. SCOPE determines the scope of this command. It can be any of:
  10251. nil The current buffer, respecting the restriction if any
  10252. tree The subtree started with the entry at point
  10253. file The current buffer, without restriction
  10254. file-with-archives
  10255. The current buffer, and any archives associated with it
  10256. agenda All agenda files
  10257. agenda-with-archives
  10258. All agenda files with any archive files associated with them
  10259. \(file1 file2 ...)
  10260. If this is a list, all files in the list will be scanned
  10261. The remaining args are treated as settings for the skipping facilities of
  10262. the scanner. The following items can be given here:
  10263. archive skip trees with the archive tag.
  10264. comment skip trees with the COMMENT keyword
  10265. function or Emacs Lisp form:
  10266. will be used as value for `org-agenda-skip-function', so whenever
  10267. the the function returns t, FUNC will not be called for that
  10268. entry and search will continue from the point where the
  10269. function leaves it.
  10270. If your function needs to retrieve the tags including inherited tags
  10271. at the *current* entry, you can use the value of the variable
  10272. `org-scanner-tags' which will be much faster than getting the value
  10273. with `org-get-tags-at'. If your function gets properties with
  10274. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10275. to t around the call to `org-entry-properties' to get the same speedup.
  10276. Note that if your function moves around to retrieve tags and properties at
  10277. a *different* entry, you cannot use these techniques."
  10278. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10279. (org-agenda-skip-archived-trees (memq 'archive skip))
  10280. (org-agenda-skip-comment-trees (memq 'comment skip))
  10281. (org-agenda-skip-function
  10282. (car (org-delete-all '(comment archive) skip)))
  10283. (org-tags-match-list-sublevels t)
  10284. matcher file res
  10285. org-todo-keywords-for-agenda
  10286. org-done-keywords-for-agenda
  10287. org-todo-keyword-alist-for-agenda
  10288. org-tag-alist-for-agenda)
  10289. (cond
  10290. ((eq match t) (setq matcher t))
  10291. ((eq match nil) (setq matcher t))
  10292. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10293. (save-excursion
  10294. (save-restriction
  10295. (when (eq scope 'tree)
  10296. (org-back-to-heading t)
  10297. (org-narrow-to-subtree)
  10298. (setq scope nil))
  10299. (if (not scope)
  10300. (progn
  10301. (org-prepare-agenda-buffers
  10302. (list (buffer-file-name (current-buffer))))
  10303. (setq res (org-scan-tags func matcher)))
  10304. ;; Get the right scope
  10305. (cond
  10306. ((and scope (listp scope) (symbolp (car scope)))
  10307. (setq scope (eval scope)))
  10308. ((eq scope 'agenda)
  10309. (setq scope (org-agenda-files t)))
  10310. ((eq scope 'agenda-with-archives)
  10311. (setq scope (org-agenda-files t))
  10312. (setq scope (org-add-archive-files scope)))
  10313. ((eq scope 'file)
  10314. (setq scope (list (buffer-file-name))))
  10315. ((eq scope 'file-with-archives)
  10316. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10317. (org-prepare-agenda-buffers scope)
  10318. (while (setq file (pop scope))
  10319. (with-current-buffer (org-find-base-buffer-visiting file)
  10320. (save-excursion
  10321. (save-restriction
  10322. (widen)
  10323. (goto-char (point-min))
  10324. (setq res (append res (org-scan-tags func matcher))))))))))
  10325. res))
  10326. ;;;; Properties
  10327. ;;; Setting and retrieving properties
  10328. (defconst org-special-properties
  10329. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10330. "TIMESTAMP" "TIMESTAMP_IA")
  10331. "The special properties valid in Org-mode.
  10332. These are properties that are not defined in the property drawer,
  10333. but in some other way.")
  10334. (defconst org-default-properties
  10335. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10336. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10337. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10338. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10339. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10340. "CLOCK_MODELINE_TOTAL")
  10341. "Some properties that are used by Org-mode for various purposes.
  10342. Being in this list makes sure that they are offered for completion.")
  10343. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10344. "Regular expression matching the first line of a property drawer.")
  10345. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10346. "Regular expression matching the first line of a property drawer.")
  10347. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10348. "Regular expression matching the first line of a property drawer.")
  10349. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10350. "Regular expression matching the first line of a property drawer.")
  10351. (defconst org-property-drawer-re
  10352. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10353. org-property-end-re "\\)\n?")
  10354. "Matches an entire property drawer.")
  10355. (defconst org-clock-drawer-re
  10356. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10357. org-property-end-re "\\)\n?")
  10358. "Matches an entire clock drawer.")
  10359. (defun org-property-action ()
  10360. "Do an action on properties."
  10361. (interactive)
  10362. (let (c)
  10363. (org-at-property-p)
  10364. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10365. (setq c (read-char-exclusive))
  10366. (cond
  10367. ((equal c ?s)
  10368. (call-interactively 'org-set-property))
  10369. ((equal c ?d)
  10370. (call-interactively 'org-delete-property))
  10371. ((equal c ?D)
  10372. (call-interactively 'org-delete-property-globally))
  10373. ((equal c ?c)
  10374. (call-interactively 'org-compute-property-at-point))
  10375. (t (error "No such property action %c" c)))))
  10376. (defun org-at-property-p ()
  10377. "Is the cursor in a property line?"
  10378. ;; FIXME: Does not check if we are actually in the drawer.
  10379. ;; FIXME: also returns true on any drawers.....
  10380. ;; This is used by C-c C-c for property action.
  10381. (save-excursion
  10382. (beginning-of-line 1)
  10383. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10384. (defun org-get-property-block (&optional beg end force)
  10385. "Return the (beg . end) range of the body of the property drawer.
  10386. BEG and END can be beginning and end of subtree, if not given
  10387. they will be found.
  10388. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10389. (catch 'exit
  10390. (save-excursion
  10391. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10392. (end (or end (progn (outline-next-heading) (point)))))
  10393. (goto-char beg)
  10394. (if (re-search-forward org-property-start-re end t)
  10395. (setq beg (1+ (match-end 0)))
  10396. (if force
  10397. (save-excursion
  10398. (org-insert-property-drawer)
  10399. (setq end (progn (outline-next-heading) (point))))
  10400. (throw 'exit nil))
  10401. (goto-char beg)
  10402. (if (re-search-forward org-property-start-re end t)
  10403. (setq beg (1+ (match-end 0)))))
  10404. (if (re-search-forward org-property-end-re end t)
  10405. (setq end (match-beginning 0))
  10406. (or force (throw 'exit nil))
  10407. (goto-char beg)
  10408. (setq end beg)
  10409. (org-indent-line-function)
  10410. (insert ":END:\n"))
  10411. (cons beg end)))))
  10412. (defun org-entry-properties (&optional pom which)
  10413. "Get all properties of the entry at point-or-marker POM.
  10414. This includes the TODO keyword, the tags, time strings for deadline,
  10415. scheduled, and clocking, and any additional properties defined in the
  10416. entry. The return value is an alist, keys may occur multiple times
  10417. if the property key was used several times.
  10418. POM may also be nil, in which case the current entry is used.
  10419. If WHICH is nil or `all', get all properties. If WHICH is
  10420. `special' or `standard', only get that subclass."
  10421. (setq which (or which 'all))
  10422. (org-with-point-at pom
  10423. (let ((clockstr (substring org-clock-string 0 -1))
  10424. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10425. beg end range props sum-props key value string clocksum)
  10426. (save-excursion
  10427. (when (condition-case nil
  10428. (and (org-mode-p) (org-back-to-heading t))
  10429. (error nil))
  10430. (setq beg (point))
  10431. (setq sum-props (get-text-property (point) 'org-summaries))
  10432. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10433. (outline-next-heading)
  10434. (setq end (point))
  10435. (when (memq which '(all special))
  10436. ;; Get the special properties, like TODO and tags
  10437. (goto-char beg)
  10438. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10439. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10440. (when (looking-at org-priority-regexp)
  10441. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10442. (when (and (setq value (org-get-tags-string))
  10443. (string-match "\\S-" value))
  10444. (push (cons "TAGS" value) props))
  10445. (when (setq value (org-get-tags-at))
  10446. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10447. props))
  10448. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10449. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10450. string (if (equal key clockstr)
  10451. (org-no-properties
  10452. (org-trim
  10453. (buffer-substring
  10454. (match-beginning 3) (goto-char (point-at-eol)))))
  10455. (substring (org-match-string-no-properties 3) 1 -1)))
  10456. (unless key
  10457. (if (= (char-after (match-beginning 3)) ?\[)
  10458. (setq key "TIMESTAMP_IA")
  10459. (setq key "TIMESTAMP")))
  10460. (when (or (equal key clockstr) (not (assoc key props)))
  10461. (push (cons key string) props)))
  10462. )
  10463. (when (memq which '(all standard))
  10464. ;; Get the standard properties, like :PROP: ...
  10465. (setq range (org-get-property-block beg end))
  10466. (when range
  10467. (goto-char (car range))
  10468. (while (re-search-forward
  10469. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10470. (cdr range) t)
  10471. (setq key (org-match-string-no-properties 1)
  10472. value (org-trim (or (org-match-string-no-properties 2) "")))
  10473. (unless (member key excluded)
  10474. (push (cons key (or value "")) props)))))
  10475. (if clocksum
  10476. (push (cons "CLOCKSUM"
  10477. (org-columns-number-to-string (/ (float clocksum) 60.)
  10478. 'add_times))
  10479. props))
  10480. (unless (assoc "CATEGORY" props)
  10481. (setq value (or (org-get-category)
  10482. (progn (org-refresh-category-properties)
  10483. (org-get-category))))
  10484. (push (cons "CATEGORY" value) props))
  10485. (append sum-props (nreverse props)))))))
  10486. (defun org-entry-get (pom property &optional inherit)
  10487. "Get value of PROPERTY for entry at point-or-marker POM.
  10488. If INHERIT is non-nil and the entry does not have the property,
  10489. then also check higher levels of the hierarchy.
  10490. If INHERIT is the symbol `selective', use inheritance only if the setting
  10491. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10492. If the property is present but empty, the return value is the empty string.
  10493. If the property is not present at all, nil is returned."
  10494. (org-with-point-at pom
  10495. (if (and inherit (if (eq inherit 'selective)
  10496. (org-property-inherit-p property)
  10497. t))
  10498. (org-entry-get-with-inheritance property)
  10499. (if (member property org-special-properties)
  10500. ;; We need a special property. Use brute force, get all properties.
  10501. (cdr (assoc property (org-entry-properties nil 'special)))
  10502. (let ((range (org-get-property-block)))
  10503. (if (and range
  10504. (goto-char (car range))
  10505. (re-search-forward
  10506. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10507. (cdr range) t))
  10508. ;; Found the property, return it.
  10509. (if (match-end 1)
  10510. (org-match-string-no-properties 1)
  10511. "")))))))
  10512. (defun org-property-or-variable-value (var &optional inherit)
  10513. "Check if there is a property fixing the value of VAR.
  10514. If yes, return this value. If not, return the current value of the variable."
  10515. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10516. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10517. (read prop)
  10518. (symbol-value var))))
  10519. (defun org-entry-delete (pom property)
  10520. "Delete the property PROPERTY from entry at point-or-marker POM."
  10521. (org-with-point-at pom
  10522. (if (member property org-special-properties)
  10523. nil ; cannot delete these properties.
  10524. (let ((range (org-get-property-block)))
  10525. (if (and range
  10526. (goto-char (car range))
  10527. (re-search-forward
  10528. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10529. (cdr range) t))
  10530. (progn
  10531. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10532. t)
  10533. nil)))))
  10534. ;; Multi-values properties are properties that contain multiple values
  10535. ;; These values are assumed to be single words, separated by whitespace.
  10536. (defun org-entry-add-to-multivalued-property (pom property value)
  10537. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10538. (let* ((old (org-entry-get pom property))
  10539. (values (and old (org-split-string old "[ \t]"))))
  10540. (setq value (org-entry-protect-space value))
  10541. (unless (member value values)
  10542. (setq values (cons value values))
  10543. (org-entry-put pom property
  10544. (mapconcat 'identity values " ")))))
  10545. (defun org-entry-remove-from-multivalued-property (pom property value)
  10546. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10547. (let* ((old (org-entry-get pom property))
  10548. (values (and old (org-split-string old "[ \t]"))))
  10549. (setq value (org-entry-protect-space value))
  10550. (when (member value values)
  10551. (setq values (delete value values))
  10552. (org-entry-put pom property
  10553. (mapconcat 'identity values " ")))))
  10554. (defun org-entry-member-in-multivalued-property (pom property value)
  10555. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10556. (let* ((old (org-entry-get pom property))
  10557. (values (and old (org-split-string old "[ \t]"))))
  10558. (setq value (org-entry-protect-space value))
  10559. (member value values)))
  10560. (defun org-entry-get-multivalued-property (pom property)
  10561. "Return a list of values in a multivalued property."
  10562. (let* ((value (org-entry-get pom property))
  10563. (values (and value (org-split-string value "[ \t]"))))
  10564. (mapcar 'org-entry-restore-space values)))
  10565. (defun org-entry-put-multivalued-property (pom property &rest values)
  10566. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10567. VALUES should be a list of strings. Spaces will be protected."
  10568. (org-entry-put pom property
  10569. (mapconcat 'org-entry-protect-space values " "))
  10570. (let* ((value (org-entry-get pom property))
  10571. (values (and value (org-split-string value "[ \t]"))))
  10572. (mapcar 'org-entry-restore-space values)))
  10573. (defun org-entry-protect-space (s)
  10574. "Protect spaces and newline in string S."
  10575. (while (string-match " " s)
  10576. (setq s (replace-match "%20" t t s)))
  10577. (while (string-match "\n" s)
  10578. (setq s (replace-match "%0A" t t s)))
  10579. s)
  10580. (defun org-entry-restore-space (s)
  10581. "Restore spaces and newline in string S."
  10582. (while (string-match "%20" s)
  10583. (setq s (replace-match " " t t s)))
  10584. (while (string-match "%0A" s)
  10585. (setq s (replace-match "\n" t t s)))
  10586. s)
  10587. (defvar org-entry-property-inherited-from (make-marker)
  10588. "Marker pointing to the entry from where a property was inherited.
  10589. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10590. location of the entry where the inheritance search matched. If there was
  10591. no match, the marker will point nowhere.
  10592. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10593. is set.")
  10594. (defun org-entry-get-with-inheritance (property)
  10595. "Get entry property, and search higher levels if not present."
  10596. (move-marker org-entry-property-inherited-from nil)
  10597. (let (tmp)
  10598. (save-excursion
  10599. (save-restriction
  10600. (widen)
  10601. (catch 'ex
  10602. (while t
  10603. (when (setq tmp (org-entry-get nil property))
  10604. (org-back-to-heading t)
  10605. (move-marker org-entry-property-inherited-from (point))
  10606. (throw 'ex tmp))
  10607. (or (org-up-heading-safe) (throw 'ex nil)))))
  10608. (or tmp
  10609. (cdr (assoc property org-file-properties))
  10610. (cdr (assoc property org-global-properties))
  10611. (cdr (assoc property org-global-properties-fixed))))))
  10612. (defun org-entry-put (pom property value)
  10613. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10614. (org-with-point-at pom
  10615. (org-back-to-heading t)
  10616. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10617. range)
  10618. (cond
  10619. ((equal property "TODO")
  10620. (when (and (stringp value) (string-match "\\S-" value)
  10621. (not (member value org-todo-keywords-1)))
  10622. (error "\"%s\" is not a valid TODO state" value))
  10623. (if (or (not value)
  10624. (not (string-match "\\S-" value)))
  10625. (setq value 'none))
  10626. (org-todo value)
  10627. (org-set-tags nil 'align))
  10628. ((equal property "PRIORITY")
  10629. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10630. (string-to-char value) ?\ ))
  10631. (org-set-tags nil 'align))
  10632. ((equal property "SCHEDULED")
  10633. (if (re-search-forward org-scheduled-time-regexp end t)
  10634. (cond
  10635. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10636. ((eq value 'later) (org-timestamp-change 1 'day))
  10637. (t (call-interactively 'org-schedule)))
  10638. (call-interactively 'org-schedule)))
  10639. ((equal property "DEADLINE")
  10640. (if (re-search-forward org-deadline-time-regexp end t)
  10641. (cond
  10642. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10643. ((eq value 'later) (org-timestamp-change 1 'day))
  10644. (t (call-interactively 'org-deadline)))
  10645. (call-interactively 'org-deadline)))
  10646. ((member property org-special-properties)
  10647. (error "The %s property can not yet be set with `org-entry-put'"
  10648. property))
  10649. (t ; a non-special property
  10650. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10651. (setq range (org-get-property-block beg end 'force))
  10652. (goto-char (car range))
  10653. (if (re-search-forward
  10654. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10655. (progn
  10656. (delete-region (match-beginning 1) (match-end 1))
  10657. (goto-char (match-beginning 1)))
  10658. (goto-char (cdr range))
  10659. (insert "\n")
  10660. (backward-char 1)
  10661. (org-indent-line-function)
  10662. (insert ":" property ":"))
  10663. (and value (insert " " value))
  10664. (org-indent-line-function)))))))
  10665. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10666. "Get all property keys in the current buffer.
  10667. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10668. like tags and TODO state.
  10669. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10670. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10671. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10672. formats in the current buffer."
  10673. (let (rtn range cfmt s p)
  10674. (save-excursion
  10675. (save-restriction
  10676. (widen)
  10677. (goto-char (point-min))
  10678. (while (re-search-forward org-property-start-re nil t)
  10679. (setq range (org-get-property-block))
  10680. (goto-char (car range))
  10681. (while (re-search-forward
  10682. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10683. (cdr range) t)
  10684. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10685. (outline-next-heading))))
  10686. (when include-specials
  10687. (setq rtn (append org-special-properties rtn)))
  10688. (when include-defaults
  10689. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10690. (add-to-list 'rtn org-effort-property))
  10691. (when include-columns
  10692. (save-excursion
  10693. (save-restriction
  10694. (widen)
  10695. (goto-char (point-min))
  10696. (while (re-search-forward
  10697. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10698. nil t)
  10699. (setq cfmt (match-string 2) s 0)
  10700. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10701. cfmt s)
  10702. (setq s (match-end 0)
  10703. p (match-string 1 cfmt))
  10704. (unless (or (equal p "ITEM")
  10705. (member p org-special-properties))
  10706. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10707. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10708. (defun org-property-values (key)
  10709. "Return a list of all values of property KEY."
  10710. (save-excursion
  10711. (save-restriction
  10712. (widen)
  10713. (goto-char (point-min))
  10714. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10715. values)
  10716. (while (re-search-forward re nil t)
  10717. (add-to-list 'values (org-trim (match-string 1))))
  10718. (delete "" values)))))
  10719. (defun org-insert-property-drawer ()
  10720. "Insert a property drawer into the current entry."
  10721. (interactive)
  10722. (org-back-to-heading t)
  10723. (looking-at outline-regexp)
  10724. (let ((indent (if org-adapt-indentation
  10725. (- (match-end 0)(match-beginning 0))
  10726. 0))
  10727. (beg (point))
  10728. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10729. end hiddenp)
  10730. (outline-next-heading)
  10731. (setq end (point))
  10732. (goto-char beg)
  10733. (while (re-search-forward re end t))
  10734. (setq hiddenp (org-invisible-p))
  10735. (end-of-line 1)
  10736. (and (equal (char-after) ?\n) (forward-char 1))
  10737. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10738. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10739. ;; just skip this line
  10740. (beginning-of-line 2)
  10741. ;; Drawer start, find the end
  10742. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10743. (beginning-of-line 1)))
  10744. (org-skip-over-state-notes)
  10745. (skip-chars-backward " \t\n\r")
  10746. (if (eq (char-before) ?*) (forward-char 1))
  10747. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10748. (beginning-of-line 0)
  10749. (org-indent-to-column indent)
  10750. (beginning-of-line 2)
  10751. (org-indent-to-column indent)
  10752. (beginning-of-line 0)
  10753. (if hiddenp
  10754. (save-excursion
  10755. (org-back-to-heading t)
  10756. (hide-entry))
  10757. (org-flag-drawer t))))
  10758. (defun org-set-property (property value)
  10759. "In the current entry, set PROPERTY to VALUE.
  10760. When called interactively, this will prompt for a property name, offering
  10761. completion on existing and default properties. And then it will prompt
  10762. for a value, offering completion either on allowed values (via an inherited
  10763. xxx_ALL property) or on existing values in other instances of this property
  10764. in the current file."
  10765. (interactive
  10766. (let* ((completion-ignore-case t)
  10767. (keys (org-buffer-property-keys nil t t))
  10768. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10769. (prop (if (member prop0 keys)
  10770. prop0
  10771. (or (cdr (assoc (downcase prop0)
  10772. (mapcar (lambda (x) (cons (downcase x) x))
  10773. keys)))
  10774. prop0)))
  10775. (cur (org-entry-get nil prop))
  10776. (allowed (org-property-get-allowed-values nil prop 'table))
  10777. (existing (mapcar 'list (org-property-values prop)))
  10778. (val (if allowed
  10779. (org-completing-read "Value: " allowed nil 'req-match)
  10780. (let (org-completion-use-ido)
  10781. (org-completing-read
  10782. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10783. (concat "[" cur "]") "")
  10784. ": ")
  10785. existing nil nil "" nil cur)))))
  10786. (list prop (if (equal val "") cur val))))
  10787. (unless (equal (org-entry-get nil property) value)
  10788. (org-entry-put nil property value)))
  10789. (defun org-delete-property (property)
  10790. "In the current entry, delete PROPERTY."
  10791. (interactive
  10792. (let* ((completion-ignore-case t)
  10793. (prop (org-ido-completing-read
  10794. "Property: " (org-entry-properties nil 'standard))))
  10795. (list prop)))
  10796. (message "Property %s %s" property
  10797. (if (org-entry-delete nil property)
  10798. "deleted"
  10799. "was not present in the entry")))
  10800. (defun org-delete-property-globally (property)
  10801. "Remove PROPERTY globally, from all entries."
  10802. (interactive
  10803. (let* ((completion-ignore-case t)
  10804. (prop (org-ido-completing-read
  10805. "Globally remove property: "
  10806. (mapcar 'list (org-buffer-property-keys)))))
  10807. (list prop)))
  10808. (save-excursion
  10809. (save-restriction
  10810. (widen)
  10811. (goto-char (point-min))
  10812. (let ((cnt 0))
  10813. (while (re-search-forward
  10814. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10815. nil t)
  10816. (setq cnt (1+ cnt))
  10817. (replace-match ""))
  10818. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10819. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10820. (defun org-compute-property-at-point ()
  10821. "Compute the property at point.
  10822. This looks for an enclosing column format, extracts the operator and
  10823. then applies it to the property in the column format's scope."
  10824. (interactive)
  10825. (unless (org-at-property-p)
  10826. (error "Not at a property"))
  10827. (let ((prop (org-match-string-no-properties 2)))
  10828. (org-columns-get-format-and-top-level)
  10829. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10830. (error "No operator defined for property %s" prop))
  10831. (org-columns-compute prop)))
  10832. (defun org-property-get-allowed-values (pom property &optional table)
  10833. "Get allowed values for the property PROPERTY.
  10834. When TABLE is non-nil, return an alist that can directly be used for
  10835. completion."
  10836. (let (vals)
  10837. (cond
  10838. ((equal property "TODO")
  10839. (setq vals (org-with-point-at pom
  10840. (append org-todo-keywords-1 '("")))))
  10841. ((equal property "PRIORITY")
  10842. (let ((n org-lowest-priority))
  10843. (while (>= n org-highest-priority)
  10844. (push (char-to-string n) vals)
  10845. (setq n (1- n)))))
  10846. ((member property org-special-properties))
  10847. (t
  10848. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10849. (when (and vals (string-match "\\S-" vals))
  10850. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10851. (setq vals (mapcar (lambda (x)
  10852. (cond ((stringp x) x)
  10853. ((numberp x) (number-to-string x))
  10854. ((symbolp x) (symbol-name x))
  10855. (t "???")))
  10856. vals)))))
  10857. (if table (mapcar 'list vals) vals)))
  10858. (defun org-property-previous-allowed-value (&optional previous)
  10859. "Switch to the next allowed value for this property."
  10860. (interactive)
  10861. (org-property-next-allowed-value t))
  10862. (defun org-property-next-allowed-value (&optional previous)
  10863. "Switch to the next allowed value for this property."
  10864. (interactive)
  10865. (unless (org-at-property-p)
  10866. (error "Not at a property"))
  10867. (let* ((key (match-string 2))
  10868. (value (match-string 3))
  10869. (allowed (or (org-property-get-allowed-values (point) key)
  10870. (and (member value '("[ ]" "[-]" "[X]"))
  10871. '("[ ]" "[X]"))))
  10872. nval)
  10873. (unless allowed
  10874. (error "Allowed values for this property have not been defined"))
  10875. (if previous (setq allowed (reverse allowed)))
  10876. (if (member value allowed)
  10877. (setq nval (car (cdr (member value allowed)))))
  10878. (setq nval (or nval (car allowed)))
  10879. (if (equal nval value)
  10880. (error "Only one allowed value for this property"))
  10881. (org-at-property-p)
  10882. (replace-match (concat " :" key ": " nval) t t)
  10883. (org-indent-line-function)
  10884. (beginning-of-line 1)
  10885. (skip-chars-forward " \t")))
  10886. (defun org-find-entry-with-id (ident)
  10887. "Locate the entry that contains the ID property with exact value IDENT.
  10888. IDENT can be a string, a symbol or a number, this function will search for
  10889. the string representation of it.
  10890. Return the position where this entry starts, or nil if there is no such entry."
  10891. (interactive "sID: ")
  10892. (let ((id (cond
  10893. ((stringp ident) ident)
  10894. ((symbol-name ident) (symbol-name ident))
  10895. ((numberp ident) (number-to-string ident))
  10896. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10897. (case-fold-search nil))
  10898. (save-excursion
  10899. (save-restriction
  10900. (widen)
  10901. (goto-char (point-min))
  10902. (when (re-search-forward
  10903. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10904. nil t)
  10905. (org-back-to-heading t)
  10906. (point))))))
  10907. ;;;; Timestamps
  10908. (defvar org-last-changed-timestamp nil)
  10909. (defvar org-last-inserted-timestamp nil
  10910. "The last time stamp inserted with `org-insert-time-stamp'.")
  10911. (defvar org-time-was-given) ; dynamically scoped parameter
  10912. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10913. (defvar org-ts-what) ; dynamically scoped parameter
  10914. (defun org-time-stamp (arg &optional inactive)
  10915. "Prompt for a date/time and insert a time stamp.
  10916. If the user specifies a time like HH:MM, or if this command is called
  10917. with a prefix argument, the time stamp will contain date and time.
  10918. Otherwise, only the date will be included. All parts of a date not
  10919. specified by the user will be filled in from the current date/time.
  10920. So if you press just return without typing anything, the time stamp
  10921. will represent the current date/time. If there is already a timestamp
  10922. at the cursor, it will be modified."
  10923. (interactive "P")
  10924. (let* ((ts nil)
  10925. (default-time
  10926. ;; Default time is either today, or, when entering a range,
  10927. ;; the range start.
  10928. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10929. (save-excursion
  10930. (re-search-backward
  10931. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10932. (- (point) 20) t)))
  10933. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10934. (current-time)))
  10935. (default-input (and ts (org-get-compact-tod ts)))
  10936. org-time-was-given org-end-time-was-given time)
  10937. (cond
  10938. ((and (org-at-timestamp-p t)
  10939. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10940. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10941. (insert "--")
  10942. (setq time (let ((this-command this-command))
  10943. (org-read-date arg 'totime nil nil
  10944. default-time default-input)))
  10945. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10946. ((org-at-timestamp-p t)
  10947. (setq time (let ((this-command this-command))
  10948. (org-read-date arg 'totime nil nil default-time default-input)))
  10949. (when (org-at-timestamp-p t) ; just to get the match data
  10950. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10951. (replace-match "")
  10952. (setq org-last-changed-timestamp
  10953. (org-insert-time-stamp
  10954. time (or org-time-was-given arg)
  10955. inactive nil nil (list org-end-time-was-given))))
  10956. (message "Timestamp updated"))
  10957. (t
  10958. (setq time (let ((this-command this-command))
  10959. (org-read-date arg 'totime nil nil default-time default-input)))
  10960. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10961. nil nil (list org-end-time-was-given))))))
  10962. ;; FIXME: can we use this for something else, like computing time differences?
  10963. (defun org-get-compact-tod (s)
  10964. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10965. (let* ((t1 (match-string 1 s))
  10966. (h1 (string-to-number (match-string 2 s)))
  10967. (m1 (string-to-number (match-string 3 s)))
  10968. (t2 (and (match-end 4) (match-string 5 s)))
  10969. (h2 (and t2 (string-to-number (match-string 6 s))))
  10970. (m2 (and t2 (string-to-number (match-string 7 s))))
  10971. dh dm)
  10972. (if (not t2)
  10973. t1
  10974. (setq dh (- h2 h1) dm (- m2 m1))
  10975. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10976. (concat t1 "+" (number-to-string dh)
  10977. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10978. (defun org-time-stamp-inactive (&optional arg)
  10979. "Insert an inactive time stamp.
  10980. An inactive time stamp is enclosed in square brackets instead of angle
  10981. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10982. does not link to the calendar and cannot be changed with the S-cursor keys.
  10983. So these are more for recording a certain time/date."
  10984. (interactive "P")
  10985. (org-time-stamp arg 'inactive))
  10986. (defvar org-date-ovl (org-make-overlay 1 1))
  10987. (org-overlay-put org-date-ovl 'face 'org-warning)
  10988. (org-detach-overlay org-date-ovl)
  10989. (defvar org-ans1) ; dynamically scoped parameter
  10990. (defvar org-ans2) ; dynamically scoped parameter
  10991. (defvar org-plain-time-of-day-regexp) ; defined below
  10992. (defvar org-overriding-default-time nil) ; dynamically scoped
  10993. (defvar org-read-date-overlay nil)
  10994. (defvar org-dcst nil) ; dynamically scoped
  10995. (defvar org-read-date-history nil)
  10996. (defvar org-read-date-final-answer nil)
  10997. (defun org-read-date (&optional with-time to-time from-string prompt
  10998. default-time default-input)
  10999. "Read a date, possibly a time, and make things smooth for the user.
  11000. The prompt will suggest to enter an ISO date, but you can also enter anything
  11001. which will at least partially be understood by `parse-time-string'.
  11002. Unrecognized parts of the date will default to the current day, month, year,
  11003. hour and minute. If this command is called to replace a timestamp at point,
  11004. of to enter the second timestamp of a range, the default time is taken from the
  11005. existing stamp. For example,
  11006. 3-2-5 --> 2003-02-05
  11007. feb 15 --> currentyear-02-15
  11008. sep 12 9 --> 2009-09-12
  11009. 12:45 --> today 12:45
  11010. 22 sept 0:34 --> currentyear-09-22 0:34
  11011. 12 --> currentyear-currentmonth-12
  11012. Fri --> nearest Friday (today or later)
  11013. etc.
  11014. Furthermore you can specify a relative date by giving, as the *first* thing
  11015. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11016. change in days weeks, months, years.
  11017. With a single plus or minus, the date is relative to today. With a double
  11018. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11019. +4d --> four days from today
  11020. +4 --> same as above
  11021. +2w --> two weeks from today
  11022. ++5 --> five days from default date
  11023. The function understands only English month and weekday abbreviations,
  11024. but this can be configured with the variables `parse-time-months' and
  11025. `parse-time-weekdays'.
  11026. While prompting, a calendar is popped up - you can also select the
  11027. date with the mouse (button 1). The calendar shows a period of three
  11028. months. To scroll it to other months, use the keys `>' and `<'.
  11029. If you don't like the calendar, turn it off with
  11030. \(setq org-read-date-popup-calendar nil)
  11031. With optional argument TO-TIME, the date will immediately be converted
  11032. to an internal time.
  11033. With an optional argument WITH-TIME, the prompt will suggest to also
  11034. insert a time. Note that when WITH-TIME is not set, you can still
  11035. enter a time, and this function will inform the calling routine about
  11036. this change. The calling routine may then choose to change the format
  11037. used to insert the time stamp into the buffer to include the time.
  11038. With optional argument FROM-STRING, read from this string instead from
  11039. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11040. the time/date that is used for everything that is not specified by the
  11041. user."
  11042. (require 'parse-time)
  11043. (let* ((org-time-stamp-rounding-minutes
  11044. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11045. (org-dcst org-display-custom-times)
  11046. (ct (org-current-time))
  11047. (def (or org-overriding-default-time default-time ct))
  11048. (defdecode (decode-time def))
  11049. (dummy (progn
  11050. (when (< (nth 2 defdecode) org-extend-today-until)
  11051. (setcar (nthcdr 2 defdecode) -1)
  11052. (setcar (nthcdr 1 defdecode) 59)
  11053. (setq def (apply 'encode-time defdecode)
  11054. defdecode (decode-time def)))))
  11055. (calendar-move-hook nil)
  11056. (calendar-view-diary-initially-flag nil)
  11057. (view-diary-entries-initially nil)
  11058. (calendar-view-holidays-initially-flag nil)
  11059. (view-calendar-holidays-initially nil)
  11060. (timestr (format-time-string
  11061. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11062. (prompt (concat (if prompt (concat prompt " ") "")
  11063. (format "Date+time [%s]: " timestr)))
  11064. ans (org-ans0 "") org-ans1 org-ans2 final)
  11065. (cond
  11066. (from-string (setq ans from-string))
  11067. (org-read-date-popup-calendar
  11068. (save-excursion
  11069. (save-window-excursion
  11070. (calendar)
  11071. (calendar-forward-day (- (time-to-days def)
  11072. (calendar-absolute-from-gregorian
  11073. (calendar-current-date))))
  11074. (org-eval-in-calendar nil t)
  11075. (let* ((old-map (current-local-map))
  11076. (map (copy-keymap calendar-mode-map))
  11077. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11078. (org-defkey map (kbd "RET") 'org-calendar-select)
  11079. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11080. 'org-calendar-select-mouse)
  11081. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11082. 'org-calendar-select-mouse)
  11083. (org-defkey minibuffer-local-map [(meta shift left)]
  11084. (lambda () (interactive)
  11085. (org-eval-in-calendar '(calendar-backward-month 1))))
  11086. (org-defkey minibuffer-local-map [(meta shift right)]
  11087. (lambda () (interactive)
  11088. (org-eval-in-calendar '(calendar-forward-month 1))))
  11089. (org-defkey minibuffer-local-map [(meta shift up)]
  11090. (lambda () (interactive)
  11091. (org-eval-in-calendar '(calendar-backward-year 1))))
  11092. (org-defkey minibuffer-local-map [(meta shift down)]
  11093. (lambda () (interactive)
  11094. (org-eval-in-calendar '(calendar-forward-year 1))))
  11095. (org-defkey minibuffer-local-map [?\e (shift left)]
  11096. (lambda () (interactive)
  11097. (org-eval-in-calendar '(calendar-backward-month 1))))
  11098. (org-defkey minibuffer-local-map [?\e (shift right)]
  11099. (lambda () (interactive)
  11100. (org-eval-in-calendar '(calendar-forward-month 1))))
  11101. (org-defkey minibuffer-local-map [?\e (shift up)]
  11102. (lambda () (interactive)
  11103. (org-eval-in-calendar '(calendar-backward-year 1))))
  11104. (org-defkey minibuffer-local-map [?\e (shift down)]
  11105. (lambda () (interactive)
  11106. (org-eval-in-calendar '(calendar-forward-year 1))))
  11107. (org-defkey minibuffer-local-map [(shift up)]
  11108. (lambda () (interactive)
  11109. (org-eval-in-calendar '(calendar-backward-week 1))))
  11110. (org-defkey minibuffer-local-map [(shift down)]
  11111. (lambda () (interactive)
  11112. (org-eval-in-calendar '(calendar-forward-week 1))))
  11113. (org-defkey minibuffer-local-map [(shift left)]
  11114. (lambda () (interactive)
  11115. (org-eval-in-calendar '(calendar-backward-day 1))))
  11116. (org-defkey minibuffer-local-map [(shift right)]
  11117. (lambda () (interactive)
  11118. (org-eval-in-calendar '(calendar-forward-day 1))))
  11119. (org-defkey minibuffer-local-map ">"
  11120. (lambda () (interactive)
  11121. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11122. (org-defkey minibuffer-local-map "<"
  11123. (lambda () (interactive)
  11124. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11125. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11126. (unwind-protect
  11127. (progn
  11128. (use-local-map map)
  11129. (add-hook 'post-command-hook 'org-read-date-display)
  11130. (setq org-ans0 (read-string prompt default-input
  11131. 'org-read-date-history nil))
  11132. ;; org-ans0: from prompt
  11133. ;; org-ans1: from mouse click
  11134. ;; org-ans2: from calendar motion
  11135. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11136. (remove-hook 'post-command-hook 'org-read-date-display)
  11137. (use-local-map old-map)
  11138. (when org-read-date-overlay
  11139. (org-delete-overlay org-read-date-overlay)
  11140. (setq org-read-date-overlay nil)))))))
  11141. (t ; Naked prompt only
  11142. (unwind-protect
  11143. (setq ans (read-string prompt default-input
  11144. 'org-read-date-history timestr))
  11145. (when org-read-date-overlay
  11146. (org-delete-overlay org-read-date-overlay)
  11147. (setq org-read-date-overlay nil)))))
  11148. (setq final (org-read-date-analyze ans def defdecode))
  11149. (setq org-read-date-final-answer ans)
  11150. (if to-time
  11151. (apply 'encode-time final)
  11152. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11153. (format "%04d-%02d-%02d %02d:%02d"
  11154. (nth 5 final) (nth 4 final) (nth 3 final)
  11155. (nth 2 final) (nth 1 final))
  11156. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11157. (defvar def)
  11158. (defvar defdecode)
  11159. (defvar with-time)
  11160. (defun org-read-date-display ()
  11161. "Display the current date prompt interpretation in the minibuffer."
  11162. (when org-read-date-display-live
  11163. (when org-read-date-overlay
  11164. (org-delete-overlay org-read-date-overlay))
  11165. (let ((p (point)))
  11166. (end-of-line 1)
  11167. (while (not (equal (buffer-substring
  11168. (max (point-min) (- (point) 4)) (point))
  11169. " "))
  11170. (insert " "))
  11171. (goto-char p))
  11172. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11173. " " (or org-ans1 org-ans2)))
  11174. (org-end-time-was-given nil)
  11175. (f (org-read-date-analyze ans def defdecode))
  11176. (fmts (if org-dcst
  11177. org-time-stamp-custom-formats
  11178. org-time-stamp-formats))
  11179. (fmt (if (or with-time
  11180. (and (boundp 'org-time-was-given) org-time-was-given))
  11181. (cdr fmts)
  11182. (car fmts)))
  11183. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11184. (when (and org-end-time-was-given
  11185. (string-match org-plain-time-of-day-regexp txt))
  11186. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11187. org-end-time-was-given
  11188. (substring txt (match-end 0)))))
  11189. (setq org-read-date-overlay
  11190. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11191. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11192. (defun org-read-date-analyze (ans def defdecode)
  11193. "Analyse the combined answer of the date prompt."
  11194. ;; FIXME: cleanup and comment
  11195. (let (delta deltan deltaw deltadef year month day
  11196. hour minute second wday pm h2 m2 tl wday1
  11197. iso-year iso-weekday iso-week iso-year iso-date)
  11198. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11199. (setq ans "+0"))
  11200. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11201. (setq ans (replace-match "" t t ans)
  11202. deltan (car delta)
  11203. deltaw (nth 1 delta)
  11204. deltadef (nth 2 delta)))
  11205. ;; Check if there is an iso week date in there
  11206. ;; If yes, sore the info and postpone interpreting it until the rest
  11207. ;; of the parsing is done
  11208. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11209. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11210. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11211. iso-week (string-to-number (match-string 2 ans)))
  11212. (setq ans (replace-match "" t t ans)))
  11213. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11214. (when (string-match
  11215. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11216. (setq year (if (match-end 2)
  11217. (string-to-number (match-string 2 ans))
  11218. (string-to-number (format-time-string "%Y")))
  11219. month (string-to-number (match-string 3 ans))
  11220. day (string-to-number (match-string 4 ans)))
  11221. (if (< year 100) (setq year (+ 2000 year)))
  11222. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11223. t nil ans)))
  11224. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11225. ;; If there is a time with am/pm, and *no* time without it, we convert
  11226. ;; so that matching will be successful.
  11227. (loop for i from 1 to 2 do ; twice, for end time as well
  11228. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11229. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11230. (setq hour (string-to-number (match-string 1 ans))
  11231. minute (if (match-end 3)
  11232. (string-to-number (match-string 3 ans))
  11233. 0)
  11234. pm (equal ?p
  11235. (string-to-char (downcase (match-string 4 ans)))))
  11236. (if (and (= hour 12) (not pm))
  11237. (setq hour 0)
  11238. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11239. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11240. t t ans))))
  11241. ;; Check if a time range is given as a duration
  11242. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11243. (setq hour (string-to-number (match-string 1 ans))
  11244. h2 (+ hour (string-to-number (match-string 3 ans)))
  11245. minute (string-to-number (match-string 2 ans))
  11246. m2 (+ minute (if (match-end 5) (string-to-number
  11247. (match-string 5 ans))0)))
  11248. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11249. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11250. t t ans)))
  11251. ;; Check if there is a time range
  11252. (when (boundp 'org-end-time-was-given)
  11253. (setq org-time-was-given nil)
  11254. (when (and (string-match org-plain-time-of-day-regexp ans)
  11255. (match-end 8))
  11256. (setq org-end-time-was-given (match-string 8 ans))
  11257. (setq ans (concat (substring ans 0 (match-beginning 7))
  11258. (substring ans (match-end 7))))))
  11259. (setq tl (parse-time-string ans)
  11260. day (or (nth 3 tl) (nth 3 defdecode))
  11261. month (or (nth 4 tl)
  11262. (if (and org-read-date-prefer-future
  11263. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11264. (1+ (nth 4 defdecode))
  11265. (nth 4 defdecode)))
  11266. year (or (nth 5 tl)
  11267. (if (and org-read-date-prefer-future
  11268. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11269. (1+ (nth 5 defdecode))
  11270. (nth 5 defdecode)))
  11271. hour (or (nth 2 tl) (nth 2 defdecode))
  11272. minute (or (nth 1 tl) (nth 1 defdecode))
  11273. second (or (nth 0 tl) 0)
  11274. wday (nth 6 tl))
  11275. ;; Special date definitions below
  11276. (cond
  11277. (iso-week
  11278. ;; There was an iso week
  11279. (setq year (or iso-year year)
  11280. day (or iso-weekday wday 1)
  11281. wday nil ; to make sure that the trigger below does not match
  11282. iso-date (calendar-gregorian-from-absolute
  11283. (calendar-absolute-from-iso
  11284. (list iso-week day year))))
  11285. ; FIXME: Should we also push ISO weeks into the future?
  11286. ; (when (and org-read-date-prefer-future
  11287. ; (not iso-year)
  11288. ; (< (calendar-absolute-from-gregorian iso-date)
  11289. ; (time-to-days (current-time))))
  11290. ; (setq year (1+ year)
  11291. ; iso-date (calendar-gregorian-from-absolute
  11292. ; (calendar-absolute-from-iso
  11293. ; (list iso-week day year)))))
  11294. (setq month (car iso-date)
  11295. year (nth 2 iso-date)
  11296. day (nth 1 iso-date)))
  11297. (deltan
  11298. (unless deltadef
  11299. (let ((now (decode-time (current-time))))
  11300. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11301. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11302. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11303. ((equal deltaw "m") (setq month (+ month deltan)))
  11304. ((equal deltaw "y") (setq year (+ year deltan)))))
  11305. ((and wday (not (nth 3 tl)))
  11306. ;; Weekday was given, but no day, so pick that day in the week
  11307. ;; on or after the derived date.
  11308. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11309. (unless (equal wday wday1)
  11310. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11311. (if (and (boundp 'org-time-was-given)
  11312. (nth 2 tl))
  11313. (setq org-time-was-given t))
  11314. (if (< year 100) (setq year (+ 2000 year)))
  11315. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11316. (list second minute hour day month year)))
  11317. (defvar parse-time-weekdays)
  11318. (defun org-read-date-get-relative (s today default)
  11319. "Check string S for special relative date string.
  11320. TODAY and DEFAULT are internal times, for today and for a default.
  11321. Return shift list (N what def-flag)
  11322. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11323. N is the number of WHATs to shift.
  11324. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11325. the DEFAULT date rather than TODAY."
  11326. (when (and
  11327. (string-match
  11328. (concat
  11329. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11330. "\\([0-9]+\\)?"
  11331. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11332. "\\([ \t]\\|$\\)") s)
  11333. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11334. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11335. (string-to-char (substring (match-string 1 s) -1))
  11336. ?+))
  11337. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11338. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11339. (what (if (match-end 3) (match-string 3 s) "d"))
  11340. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11341. (date (if rel default today))
  11342. (wday (nth 6 (decode-time date)))
  11343. delta)
  11344. (if wday1
  11345. (progn
  11346. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11347. (if (= dir ?-) (setq delta (- delta 7)))
  11348. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11349. (list delta "d" rel))
  11350. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11351. (defun org-eval-in-calendar (form &optional keepdate)
  11352. "Eval FORM in the calendar window and return to current window.
  11353. Also, store the cursor date in variable org-ans2."
  11354. (let ((sf (selected-frame))
  11355. (sw (selected-window)))
  11356. (select-window (get-buffer-window "*Calendar*" t))
  11357. (eval form)
  11358. (when (and (not keepdate) (calendar-cursor-to-date))
  11359. (let* ((date (calendar-cursor-to-date))
  11360. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11361. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11362. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11363. (select-window sw)
  11364. (select-frame-set-input-focus sf)))
  11365. (defun org-calendar-select ()
  11366. "Return to `org-read-date' with the date currently selected.
  11367. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11368. (interactive)
  11369. (when (calendar-cursor-to-date)
  11370. (let* ((date (calendar-cursor-to-date))
  11371. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11372. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11373. (if (active-minibuffer-window) (exit-minibuffer))))
  11374. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11375. "Insert a date stamp for the date given by the internal TIME.
  11376. WITH-HM means, use the stamp format that includes the time of the day.
  11377. INACTIVE means use square brackets instead of angular ones, so that the
  11378. stamp will not contribute to the agenda.
  11379. PRE and POST are optional strings to be inserted before and after the
  11380. stamp.
  11381. The command returns the inserted time stamp."
  11382. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11383. stamp)
  11384. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11385. (insert-before-markers (or pre ""))
  11386. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11387. (when (listp extra)
  11388. (setq extra (car extra))
  11389. (if (and (stringp extra)
  11390. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11391. (setq extra (format "-%02d:%02d"
  11392. (string-to-number (match-string 1 extra))
  11393. (string-to-number (match-string 2 extra))))
  11394. (setq extra nil)))
  11395. (when extra
  11396. (backward-char 1)
  11397. (insert-before-markers extra)
  11398. (forward-char 1))
  11399. (insert-before-markers (or post ""))
  11400. (setq org-last-inserted-timestamp stamp)))
  11401. (defun org-toggle-time-stamp-overlays ()
  11402. "Toggle the use of custom time stamp formats."
  11403. (interactive)
  11404. (setq org-display-custom-times (not org-display-custom-times))
  11405. (unless org-display-custom-times
  11406. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11407. (while (setq p (next-single-property-change p 'display))
  11408. (if (and (get-text-property p 'display)
  11409. (eq (get-text-property p 'face) 'org-date))
  11410. (remove-text-properties
  11411. p (setq p (next-single-property-change p 'display))
  11412. '(display t))))
  11413. (set-buffer-modified-p bmp)))
  11414. (if (featurep 'xemacs)
  11415. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11416. (org-restart-font-lock)
  11417. (setq org-table-may-need-update t)
  11418. (if org-display-custom-times
  11419. (message "Time stamps are overlayed with custom format")
  11420. (message "Time stamp overlays removed")))
  11421. (defun org-display-custom-time (beg end)
  11422. "Overlay modified time stamp format over timestamp between BEG and END."
  11423. (let* ((ts (buffer-substring beg end))
  11424. t1 w1 with-hm tf time str w2 (off 0))
  11425. (save-match-data
  11426. (setq t1 (org-parse-time-string ts t))
  11427. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11428. (setq off (- (match-end 0) (match-beginning 0)))))
  11429. (setq end (- end off))
  11430. (setq w1 (- end beg)
  11431. with-hm (and (nth 1 t1) (nth 2 t1))
  11432. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11433. time (org-fix-decoded-time t1)
  11434. str (org-add-props
  11435. (format-time-string
  11436. (substring tf 1 -1) (apply 'encode-time time))
  11437. nil 'mouse-face 'highlight)
  11438. w2 (length str))
  11439. (if (not (= w2 w1))
  11440. (add-text-properties (1+ beg) (+ 2 beg)
  11441. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11442. (if (featurep 'xemacs)
  11443. (progn
  11444. (put-text-property beg end 'invisible t)
  11445. (put-text-property beg end 'end-glyph (make-glyph str)))
  11446. (put-text-property beg end 'display str))))
  11447. (defun org-translate-time (string)
  11448. "Translate all timestamps in STRING to custom format.
  11449. But do this only if the variable `org-display-custom-times' is set."
  11450. (when org-display-custom-times
  11451. (save-match-data
  11452. (let* ((start 0)
  11453. (re org-ts-regexp-both)
  11454. t1 with-hm inactive tf time str beg end)
  11455. (while (setq start (string-match re string start))
  11456. (setq beg (match-beginning 0)
  11457. end (match-end 0)
  11458. t1 (save-match-data
  11459. (org-parse-time-string (substring string beg end) t))
  11460. with-hm (and (nth 1 t1) (nth 2 t1))
  11461. inactive (equal (substring string beg (1+ beg)) "[")
  11462. tf (funcall (if with-hm 'cdr 'car)
  11463. org-time-stamp-custom-formats)
  11464. time (org-fix-decoded-time t1)
  11465. str (format-time-string
  11466. (concat
  11467. (if inactive "[" "<") (substring tf 1 -1)
  11468. (if inactive "]" ">"))
  11469. (apply 'encode-time time))
  11470. string (replace-match str t t string)
  11471. start (+ start (length str)))))))
  11472. string)
  11473. (defun org-fix-decoded-time (time)
  11474. "Set 0 instead of nil for the first 6 elements of time.
  11475. Don't touch the rest."
  11476. (let ((n 0))
  11477. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11478. (defun org-days-to-time (timestamp-string)
  11479. "Difference between TIMESTAMP-STRING and now in days."
  11480. (- (time-to-days (org-time-string-to-time timestamp-string))
  11481. (time-to-days (current-time))))
  11482. (defun org-deadline-close (timestamp-string &optional ndays)
  11483. "Is the time in TIMESTAMP-STRING close to the current date?"
  11484. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11485. (and (< (org-days-to-time timestamp-string) ndays)
  11486. (not (org-entry-is-done-p))))
  11487. (defun org-get-wdays (ts)
  11488. "Get the deadline lead time appropriate for timestring TS."
  11489. (cond
  11490. ((<= org-deadline-warning-days 0)
  11491. ;; 0 or negative, enforce this value no matter what
  11492. (- org-deadline-warning-days))
  11493. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11494. ;; lead time is specified.
  11495. (floor (* (string-to-number (match-string 1 ts))
  11496. (cdr (assoc (match-string 2 ts)
  11497. '(("d" . 1) ("w" . 7)
  11498. ("m" . 30.4) ("y" . 365.25)))))))
  11499. ;; go for the default.
  11500. (t org-deadline-warning-days)))
  11501. (defun org-calendar-select-mouse (ev)
  11502. "Return to `org-read-date' with the date currently selected.
  11503. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11504. (interactive "e")
  11505. (mouse-set-point ev)
  11506. (when (calendar-cursor-to-date)
  11507. (let* ((date (calendar-cursor-to-date))
  11508. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11509. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11510. (if (active-minibuffer-window) (exit-minibuffer))))
  11511. (defun org-check-deadlines (ndays)
  11512. "Check if there are any deadlines due or past due.
  11513. A deadline is considered due if it happens within `org-deadline-warning-days'
  11514. days from today's date. If the deadline appears in an entry marked DONE,
  11515. it is not shown. The prefix arg NDAYS can be used to test that many
  11516. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11517. (interactive "P")
  11518. (let* ((org-warn-days
  11519. (cond
  11520. ((equal ndays '(4)) 100000)
  11521. (ndays (prefix-numeric-value ndays))
  11522. (t (abs org-deadline-warning-days))))
  11523. (case-fold-search nil)
  11524. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11525. (callback
  11526. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11527. (message "%d deadlines past-due or due within %d days"
  11528. (org-occur regexp nil callback)
  11529. org-warn-days)))
  11530. (defun org-check-before-date (date)
  11531. "Check if there are deadlines or scheduled entries before DATE."
  11532. (interactive (list (org-read-date)))
  11533. (let ((case-fold-search nil)
  11534. (regexp (concat "\\<\\(" org-deadline-string
  11535. "\\|" org-scheduled-string
  11536. "\\) *<\\([^>]+\\)>"))
  11537. (callback
  11538. (lambda () (time-less-p
  11539. (org-time-string-to-time (match-string 2))
  11540. (org-time-string-to-time date)))))
  11541. (message "%d entries before %s"
  11542. (org-occur regexp nil callback) date)))
  11543. (defun org-check-after-date (date)
  11544. "Check if there are deadlines or scheduled entries after DATE."
  11545. (interactive (list (org-read-date)))
  11546. (let ((case-fold-search nil)
  11547. (regexp (concat "\\<\\(" org-deadline-string
  11548. "\\|" org-scheduled-string
  11549. "\\) *<\\([^>]+\\)>"))
  11550. (callback
  11551. (lambda () (not
  11552. (time-less-p
  11553. (org-time-string-to-time (match-string 2))
  11554. (org-time-string-to-time date))))))
  11555. (message "%d entries after %s"
  11556. (org-occur regexp nil callback) date)))
  11557. (defun org-evaluate-time-range (&optional to-buffer)
  11558. "Evaluate a time range by computing the difference between start and end.
  11559. Normally the result is just printed in the echo area, but with prefix arg
  11560. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11561. If the time range is actually in a table, the result is inserted into the
  11562. next column.
  11563. For time difference computation, a year is assumed to be exactly 365
  11564. days in order to avoid rounding problems."
  11565. (interactive "P")
  11566. (or
  11567. (org-clock-update-time-maybe)
  11568. (save-excursion
  11569. (unless (org-at-date-range-p t)
  11570. (goto-char (point-at-bol))
  11571. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11572. (if (not (org-at-date-range-p t))
  11573. (error "Not at a time-stamp range, and none found in current line")))
  11574. (let* ((ts1 (match-string 1))
  11575. (ts2 (match-string 2))
  11576. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11577. (match-end (match-end 0))
  11578. (time1 (org-time-string-to-time ts1))
  11579. (time2 (org-time-string-to-time ts2))
  11580. (t1 (time-to-seconds time1))
  11581. (t2 (time-to-seconds time2))
  11582. (diff (abs (- t2 t1)))
  11583. (negative (< (- t2 t1) 0))
  11584. ;; (ys (floor (* 365 24 60 60)))
  11585. (ds (* 24 60 60))
  11586. (hs (* 60 60))
  11587. (fy "%dy %dd %02d:%02d")
  11588. (fy1 "%dy %dd")
  11589. (fd "%dd %02d:%02d")
  11590. (fd1 "%dd")
  11591. (fh "%02d:%02d")
  11592. y d h m align)
  11593. (if havetime
  11594. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11595. y 0
  11596. d (floor (/ diff ds)) diff (mod diff ds)
  11597. h (floor (/ diff hs)) diff (mod diff hs)
  11598. m (floor (/ diff 60)))
  11599. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11600. y 0
  11601. d (floor (+ (/ diff ds) 0.5))
  11602. h 0 m 0))
  11603. (if (not to-buffer)
  11604. (message "%s" (org-make-tdiff-string y d h m))
  11605. (if (org-at-table-p)
  11606. (progn
  11607. (goto-char match-end)
  11608. (setq align t)
  11609. (and (looking-at " *|") (goto-char (match-end 0))))
  11610. (goto-char match-end))
  11611. (if (looking-at
  11612. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11613. (replace-match ""))
  11614. (if negative (insert " -"))
  11615. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11616. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11617. (insert " " (format fh h m))))
  11618. (if align (org-table-align))
  11619. (message "Time difference inserted")))))
  11620. (defun org-make-tdiff-string (y d h m)
  11621. (let ((fmt "")
  11622. (l nil))
  11623. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11624. l (push y l)))
  11625. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11626. l (push d l)))
  11627. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11628. l (push h l)))
  11629. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11630. l (push m l)))
  11631. (apply 'format fmt (nreverse l))))
  11632. (defun org-time-string-to-time (s)
  11633. (apply 'encode-time (org-parse-time-string s)))
  11634. (defun org-time-string-to-seconds (s)
  11635. (time-to-seconds (org-time-string-to-time s)))
  11636. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11637. "Convert a time stamp to an absolute day number.
  11638. If there is a specifyer for a cyclic time stamp, get the closest date to
  11639. DAYNR.
  11640. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11641. the variable date is bound by the calendar when this is called."
  11642. (cond
  11643. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11644. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11645. daynr
  11646. (+ daynr 1000)))
  11647. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11648. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11649. (time-to-days (current-time))) (match-string 0 s)
  11650. prefer show-all))
  11651. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11652. (defun org-days-to-iso-week (days)
  11653. "Return the iso week number."
  11654. (require 'cal-iso)
  11655. (car (calendar-iso-from-absolute days)))
  11656. (defun org-small-year-to-year (year)
  11657. "Convert 2-digit years into 4-digit years.
  11658. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11659. The year 2000 cannot be abbreviated. Any year larger than 99
  11660. is returned unchanged."
  11661. (if (< year 38)
  11662. (setq year (+ 2000 year))
  11663. (if (< year 100)
  11664. (setq year (+ 1900 year))))
  11665. year)
  11666. (defun org-time-from-absolute (d)
  11667. "Return the time corresponding to date D.
  11668. D may be an absolute day number, or a calendar-type list (month day year)."
  11669. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11670. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11671. (defun org-calendar-holiday ()
  11672. "List of holidays, for Diary display in Org-mode."
  11673. (require 'holidays)
  11674. (let ((hl (funcall
  11675. (if (fboundp 'calendar-check-holidays)
  11676. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11677. (if hl (mapconcat 'identity hl "; "))))
  11678. (defun org-diary-sexp-entry (sexp entry date)
  11679. "Process a SEXP diary ENTRY for DATE."
  11680. (require 'diary-lib)
  11681. (let ((result (if calendar-debug-sexp
  11682. (let ((stack-trace-on-error t))
  11683. (eval (car (read-from-string sexp))))
  11684. (condition-case nil
  11685. (eval (car (read-from-string sexp)))
  11686. (error
  11687. (beep)
  11688. (message "Bad sexp at line %d in %s: %s"
  11689. (org-current-line)
  11690. (buffer-file-name) sexp)
  11691. (sleep-for 2))))))
  11692. (cond ((stringp result) result)
  11693. ((and (consp result)
  11694. (stringp (cdr result))) (cdr result))
  11695. (result entry)
  11696. (t nil))))
  11697. (defun org-diary-to-ical-string (frombuf)
  11698. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11699. This uses the icalendar.el library."
  11700. (let* ((tmpdir (if (featurep 'xemacs)
  11701. (temp-directory)
  11702. temporary-file-directory))
  11703. (tmpfile (make-temp-name
  11704. (expand-file-name "orgics" tmpdir)))
  11705. buf rtn b e)
  11706. (save-excursion
  11707. (set-buffer frombuf)
  11708. (icalendar-export-region (point-min) (point-max) tmpfile)
  11709. (setq buf (find-buffer-visiting tmpfile))
  11710. (set-buffer buf)
  11711. (goto-char (point-min))
  11712. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11713. (setq b (match-beginning 0)))
  11714. (goto-char (point-max))
  11715. (if (re-search-backward "^END:VEVENT" nil t)
  11716. (setq e (match-end 0)))
  11717. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11718. (kill-buffer buf)
  11719. (delete-file tmpfile)
  11720. rtn))
  11721. (defun org-closest-date (start current change prefer show-all)
  11722. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11723. When PREFER is `past' return a date that is either CURRENT or past.
  11724. When PREFER is `future', return a date that is either CURRENT or future.
  11725. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11726. ;; Make the proper lists from the dates
  11727. (catch 'exit
  11728. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11729. dn dw sday cday n1 n2 n0
  11730. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11731. (setq start (org-date-to-gregorian start)
  11732. current (org-date-to-gregorian
  11733. (if show-all
  11734. current
  11735. (time-to-days (current-time))))
  11736. sday (calendar-absolute-from-gregorian start)
  11737. cday (calendar-absolute-from-gregorian current))
  11738. (if (<= cday sday) (throw 'exit sday))
  11739. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11740. (setq dn (string-to-number (match-string 1 change))
  11741. dw (cdr (assoc (match-string 2 change) a1)))
  11742. (error "Invalid change specifyer: %s" change))
  11743. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11744. (cond
  11745. ((eq dw 'day)
  11746. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11747. n2 (+ n1 dn)))
  11748. ((eq dw 'year)
  11749. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11750. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11751. (setq date1 (list m d y1)
  11752. n1 (calendar-absolute-from-gregorian date1)
  11753. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11754. n2 (calendar-absolute-from-gregorian date2)))
  11755. ((eq dw 'month)
  11756. ;; approx number of month between the two dates
  11757. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11758. ;; How often does dn fit in there?
  11759. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11760. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11761. m (+ m nm)
  11762. ny (floor (/ m 12))
  11763. y (+ y ny)
  11764. m (- m (* ny 12)))
  11765. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11766. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11767. (setq m2 (+ m dn) y2 y)
  11768. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11769. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11770. (while (<= n2 cday)
  11771. (setq n1 n2 m m2 y y2)
  11772. (setq m2 (+ m dn) y2 y)
  11773. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11774. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11775. ;; Make sure n1 is the earlier date
  11776. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11777. (if show-all
  11778. (cond
  11779. ((eq prefer 'past) n1)
  11780. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11781. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11782. (cond
  11783. ((eq prefer 'past) n1)
  11784. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11785. (t (if (= cday n1) n1 n2)))))))
  11786. (defun org-date-to-gregorian (date)
  11787. "Turn any specification of DATE into a gregorian date for the calendar."
  11788. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11789. ((and (listp date) (= (length date) 3)) date)
  11790. ((stringp date)
  11791. (setq date (org-parse-time-string date))
  11792. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11793. ((listp date)
  11794. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11795. (defun org-parse-time-string (s &optional nodefault)
  11796. "Parse the standard Org-mode time string.
  11797. This should be a lot faster than the normal `parse-time-string'.
  11798. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11799. hour and minute fields will be nil if not given."
  11800. (if (string-match org-ts-regexp0 s)
  11801. (list 0
  11802. (if (or (match-beginning 8) (not nodefault))
  11803. (string-to-number (or (match-string 8 s) "0")))
  11804. (if (or (match-beginning 7) (not nodefault))
  11805. (string-to-number (or (match-string 7 s) "0")))
  11806. (string-to-number (match-string 4 s))
  11807. (string-to-number (match-string 3 s))
  11808. (string-to-number (match-string 2 s))
  11809. nil nil nil)
  11810. (make-list 9 0)))
  11811. (defun org-timestamp-up (&optional arg)
  11812. "Increase the date item at the cursor by one.
  11813. If the cursor is on the year, change the year. If it is on the month or
  11814. the day, change that.
  11815. With prefix ARG, change by that many units."
  11816. (interactive "p")
  11817. (org-timestamp-change (prefix-numeric-value arg)))
  11818. (defun org-timestamp-down (&optional arg)
  11819. "Decrease the date item at the cursor by one.
  11820. If the cursor is on the year, change the year. If it is on the month or
  11821. the day, change that.
  11822. With prefix ARG, change by that many units."
  11823. (interactive "p")
  11824. (org-timestamp-change (- (prefix-numeric-value arg))))
  11825. (defun org-timestamp-up-day (&optional arg)
  11826. "Increase the date in the time stamp by one day.
  11827. With prefix ARG, change that many days."
  11828. (interactive "p")
  11829. (if (and (not (org-at-timestamp-p t))
  11830. (org-on-heading-p))
  11831. (org-todo 'up)
  11832. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11833. (defun org-timestamp-down-day (&optional arg)
  11834. "Decrease the date in the time stamp by one day.
  11835. With prefix ARG, change that many days."
  11836. (interactive "p")
  11837. (if (and (not (org-at-timestamp-p t))
  11838. (org-on-heading-p))
  11839. (org-todo 'down)
  11840. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11841. (defun org-at-timestamp-p (&optional inactive-ok)
  11842. "Determine if the cursor is in or at a timestamp."
  11843. (interactive)
  11844. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11845. (pos (point))
  11846. (ans (or (looking-at tsr)
  11847. (save-excursion
  11848. (skip-chars-backward "^[<\n\r\t")
  11849. (if (> (point) (point-min)) (backward-char 1))
  11850. (and (looking-at tsr)
  11851. (> (- (match-end 0) pos) -1))))))
  11852. (and ans
  11853. (boundp 'org-ts-what)
  11854. (setq org-ts-what
  11855. (cond
  11856. ((= pos (match-beginning 0)) 'bracket)
  11857. ((= pos (1- (match-end 0))) 'bracket)
  11858. ((org-pos-in-match-range pos 2) 'year)
  11859. ((org-pos-in-match-range pos 3) 'month)
  11860. ((org-pos-in-match-range pos 7) 'hour)
  11861. ((org-pos-in-match-range pos 8) 'minute)
  11862. ((or (org-pos-in-match-range pos 4)
  11863. (org-pos-in-match-range pos 5)) 'day)
  11864. ((and (> pos (or (match-end 8) (match-end 5)))
  11865. (< pos (match-end 0)))
  11866. (- pos (or (match-end 8) (match-end 5))))
  11867. (t 'day))))
  11868. ans))
  11869. (defun org-toggle-timestamp-type ()
  11870. "Toggle the type (<active> or [inactive]) of a time stamp."
  11871. (interactive)
  11872. (when (org-at-timestamp-p t)
  11873. (let ((beg (match-beginning 0)) (end (match-end 0))
  11874. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11875. (save-excursion
  11876. (goto-char beg)
  11877. (while (re-search-forward "[][<>]" end t)
  11878. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11879. t t)))
  11880. (message "Timestamp is now %sactive"
  11881. (if (equal (char-after beg) ?<) "" "in")))))
  11882. (defun org-timestamp-change (n &optional what)
  11883. "Change the date in the time stamp at point.
  11884. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11885. `year', `minute', `second'. If WHAT is not given, the cursor position
  11886. in the timestamp determines what will be changed."
  11887. (let ((pos (point))
  11888. with-hm inactive
  11889. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11890. org-ts-what
  11891. extra rem
  11892. ts time time0)
  11893. (if (not (org-at-timestamp-p t))
  11894. (error "Not at a timestamp"))
  11895. (if (and (not what) (eq org-ts-what 'bracket))
  11896. (org-toggle-timestamp-type)
  11897. (if (and (not what) (not (eq org-ts-what 'day))
  11898. org-display-custom-times
  11899. (get-text-property (point) 'display)
  11900. (not (get-text-property (1- (point)) 'display)))
  11901. (setq org-ts-what 'day))
  11902. (setq org-ts-what (or what org-ts-what)
  11903. inactive (= (char-after (match-beginning 0)) ?\[)
  11904. ts (match-string 0))
  11905. (replace-match "")
  11906. (if (string-match
  11907. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11908. ts)
  11909. (setq extra (match-string 1 ts)))
  11910. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11911. (setq with-hm t))
  11912. (setq time0 (org-parse-time-string ts))
  11913. (when (and (eq org-ts-what 'minute)
  11914. (eq current-prefix-arg nil))
  11915. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11916. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11917. (setcar (cdr time0) (+ (nth 1 time0)
  11918. (if (> n 0) (- rem) (- dm rem))))))
  11919. (setq time
  11920. (encode-time (or (car time0) 0)
  11921. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11922. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11923. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11924. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11925. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11926. (nthcdr 6 time0)))
  11927. (when (and (member org-ts-what '(hour minute))
  11928. extra
  11929. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11930. (setq extra (org-modify-ts-extra
  11931. extra
  11932. (if (eq org-ts-what 'hour) 2 5)
  11933. n dm)))
  11934. (when (integerp org-ts-what)
  11935. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11936. (if (eq what 'calendar)
  11937. (let ((cal-date (org-get-date-from-calendar)))
  11938. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11939. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11940. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11941. (setcar time0 (or (car time0) 0))
  11942. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11943. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11944. (setq time (apply 'encode-time time0))))
  11945. (setq org-last-changed-timestamp
  11946. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11947. (org-clock-update-time-maybe)
  11948. (goto-char pos)
  11949. ;; Try to recenter the calendar window, if any
  11950. (if (and org-calendar-follow-timestamp-change
  11951. (get-buffer-window "*Calendar*" t)
  11952. (memq org-ts-what '(day month year)))
  11953. (org-recenter-calendar (time-to-days time))))))
  11954. (defun org-modify-ts-extra (s pos n dm)
  11955. "Change the different parts of the lead-time and repeat fields in timestamp."
  11956. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11957. ng h m new rem)
  11958. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11959. (cond
  11960. ((or (org-pos-in-match-range pos 2)
  11961. (org-pos-in-match-range pos 3))
  11962. (setq m (string-to-number (match-string 3 s))
  11963. h (string-to-number (match-string 2 s)))
  11964. (if (org-pos-in-match-range pos 2)
  11965. (setq h (+ h n))
  11966. (setq n (* dm (org-no-warnings (signum n))))
  11967. (when (not (= 0 (setq rem (% m dm))))
  11968. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11969. (setq m (+ m n)))
  11970. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11971. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11972. (setq h (min 24 (max 0 h)))
  11973. (setq ng 1 new (format "-%02d:%02d" h m)))
  11974. ((org-pos-in-match-range pos 6)
  11975. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11976. ((org-pos-in-match-range pos 5)
  11977. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11978. ((org-pos-in-match-range pos 9)
  11979. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11980. ((org-pos-in-match-range pos 8)
  11981. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11982. (when ng
  11983. (setq s (concat
  11984. (substring s 0 (match-beginning ng))
  11985. new
  11986. (substring s (match-end ng))))))
  11987. s))
  11988. (defun org-recenter-calendar (date)
  11989. "If the calendar is visible, recenter it to DATE."
  11990. (let* ((win (selected-window))
  11991. (cwin (get-buffer-window "*Calendar*" t))
  11992. (calendar-move-hook nil))
  11993. (when cwin
  11994. (select-window cwin)
  11995. (calendar-goto-date (if (listp date) date
  11996. (calendar-gregorian-from-absolute date)))
  11997. (select-window win))))
  11998. (defun org-goto-calendar (&optional arg)
  11999. "Go to the Emacs calendar at the current date.
  12000. If there is a time stamp in the current line, go to that date.
  12001. A prefix ARG can be used to force the current date."
  12002. (interactive "P")
  12003. (let ((tsr org-ts-regexp) diff
  12004. (calendar-move-hook nil)
  12005. (calendar-view-holidays-initially-flag nil)
  12006. (view-calendar-holidays-initially nil)
  12007. (calendar-view-diary-initially-flag nil)
  12008. (view-diary-entries-initially nil))
  12009. (if (or (org-at-timestamp-p)
  12010. (save-excursion
  12011. (beginning-of-line 1)
  12012. (looking-at (concat ".*" tsr))))
  12013. (let ((d1 (time-to-days (current-time)))
  12014. (d2 (time-to-days
  12015. (org-time-string-to-time (match-string 1)))))
  12016. (setq diff (- d2 d1))))
  12017. (calendar)
  12018. (calendar-goto-today)
  12019. (if (and diff (not arg)) (calendar-forward-day diff))))
  12020. (defun org-get-date-from-calendar ()
  12021. "Return a list (month day year) of date at point in calendar."
  12022. (with-current-buffer "*Calendar*"
  12023. (save-match-data
  12024. (calendar-cursor-to-date))))
  12025. (defun org-date-from-calendar ()
  12026. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12027. If there is already a time stamp at the cursor position, update it."
  12028. (interactive)
  12029. (if (org-at-timestamp-p t)
  12030. (org-timestamp-change 0 'calendar)
  12031. (let ((cal-date (org-get-date-from-calendar)))
  12032. (org-insert-time-stamp
  12033. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12034. (defun org-minutes-to-hh:mm-string (m)
  12035. "Compute H:MM from a number of minutes."
  12036. (let ((h (/ m 60)))
  12037. (setq m (- m (* 60 h)))
  12038. (format org-time-clocksum-format h m)))
  12039. (defun org-hh:mm-string-to-minutes (s)
  12040. "Convert a string H:MM to a number of minutes.
  12041. If the string is just a number, interprete it as minutes.
  12042. In fact, the first hh:mm or number in the string will be taken,
  12043. there can be extra stuff in the string.
  12044. If no number is found, the return value is 0."
  12045. (cond
  12046. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12047. (+ (* (string-to-number (match-string 1 s)) 60)
  12048. (string-to-number (match-string 2 s))))
  12049. ((string-match "\\([0-9]+\\)" s)
  12050. (string-to-number (match-string 1 s)))
  12051. (t 0)))
  12052. ;;;; Files
  12053. (defun org-save-all-org-buffers ()
  12054. "Save all Org-mode buffers without user confirmation."
  12055. (interactive)
  12056. (message "Saving all Org-mode buffers...")
  12057. (save-some-buffers t 'org-mode-p)
  12058. (when (featurep 'org-id) (org-id-locations-save))
  12059. (message "Saving all Org-mode buffers... done"))
  12060. (defun org-revert-all-org-buffers ()
  12061. "Revert all Org-mode buffers.
  12062. Prompt for confirmation when there are unsaved changes.
  12063. Be sure you know what you are doing before letting this function
  12064. overwrite your changes.
  12065. This function is useful in a setup where one tracks org files
  12066. with a version control system, to revert on one machine after pulling
  12067. changes from another. I believe the procedure must be like this:
  12068. 1. M-x org-save-all-org-buffers
  12069. 2. Pull changes from the other machine, resolve conflicts
  12070. 3. M-x org-revert-all-org-buffers"
  12071. (interactive)
  12072. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12073. (error "Abort"))
  12074. (save-excursion
  12075. (save-window-excursion
  12076. (mapc
  12077. (lambda (b)
  12078. (when (and (with-current-buffer b (org-mode-p))
  12079. (with-current-buffer b buffer-file-name))
  12080. (switch-to-buffer b)
  12081. (revert-buffer t 'no-confirm)))
  12082. (buffer-list))
  12083. (when (and (featurep 'org-id) org-id-track-globally)
  12084. (org-id-locations-load)))))
  12085. ;;;; Agenda files
  12086. ;;;###autoload
  12087. (defun org-iswitchb (&optional arg)
  12088. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12089. With a prefix argument, restrict available to files.
  12090. With two prefix arguments, restrict available buffers to agenda files.
  12091. Due to some yet unresolved reason, the global function
  12092. `iswitchb-mode' needs to be active for this function to work."
  12093. (interactive "P")
  12094. (require 'iswitchb)
  12095. (let ((enabled iswitchb-mode) blist)
  12096. (or enabled (iswitchb-mode 1))
  12097. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12098. ((equal arg '(16)) (org-buffer-list 'agenda))
  12099. (t (org-buffer-list))))
  12100. (unwind-protect
  12101. (let ((iswitchb-make-buflist-hook
  12102. (lambda ()
  12103. (setq iswitchb-temp-buflist
  12104. (mapcar 'buffer-name blist)))))
  12105. (switch-to-buffer
  12106. (iswitchb-read-buffer
  12107. "Switch-to: " nil t))
  12108. (or enabled (iswitchb-mode -1))))))
  12109. ;;;###autoload
  12110. (defun org-ido-switchb (&optional arg)
  12111. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12112. With a prefix argument, restrict available to files.
  12113. With two prefix arguments, restrict available buffers to agenda files."
  12114. (interactive "P")
  12115. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12116. ((equal arg '(16)) (org-buffer-list 'agenda))
  12117. (t (org-buffer-list)))))
  12118. (switch-to-buffer
  12119. (org-ido-completing-read "Org buffer: "
  12120. (mapcar 'list (mapcar 'buffer-name blist))
  12121. nil t))))
  12122. (defun org-buffer-list (&optional predicate exclude-tmp)
  12123. "Return a list of Org buffers.
  12124. PREDICATE can be `export', `files' or `agenda'.
  12125. export restrict the list to Export buffers.
  12126. files restrict the list to buffers visiting Org files.
  12127. agenda restrict the list to buffers visiting agenda files.
  12128. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12129. (let* ((bfn nil)
  12130. (agenda-files (and (eq predicate 'agenda)
  12131. (mapcar 'file-truename (org-agenda-files t))))
  12132. (filter
  12133. (cond
  12134. ((eq predicate 'files)
  12135. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12136. ((eq predicate 'export)
  12137. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12138. ((eq predicate 'agenda)
  12139. (lambda (b)
  12140. (with-current-buffer b
  12141. (and (eq major-mode 'org-mode)
  12142. (setq bfn (buffer-file-name b))
  12143. (member (file-truename bfn) agenda-files)))))
  12144. (t (lambda (b) (with-current-buffer b
  12145. (or (eq major-mode 'org-mode)
  12146. (string-match "\*Org .*Export"
  12147. (buffer-name b)))))))))
  12148. (delq nil
  12149. (mapcar
  12150. (lambda(b)
  12151. (if (and (funcall filter b)
  12152. (or (not exclude-tmp)
  12153. (not (string-match "tmp" (buffer-name b)))))
  12154. b
  12155. nil))
  12156. (buffer-list)))))
  12157. (defun org-agenda-files (&optional unrestricted archives)
  12158. "Get the list of agenda files.
  12159. Optional UNRESTRICTED means return the full list even if a restriction
  12160. is currently in place.
  12161. When ARCHIVES is t, include all archive files hat are really being
  12162. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12163. `org-agenda-archives-mode' is t."
  12164. (let ((files
  12165. (cond
  12166. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12167. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12168. ((listp org-agenda-files) org-agenda-files)
  12169. (t (error "Invalid value of `org-agenda-files'")))))
  12170. (setq files (apply 'append
  12171. (mapcar (lambda (f)
  12172. (if (file-directory-p f)
  12173. (directory-files
  12174. f t org-agenda-file-regexp)
  12175. (list f)))
  12176. files)))
  12177. (when org-agenda-skip-unavailable-files
  12178. (setq files (delq nil
  12179. (mapcar (function
  12180. (lambda (file)
  12181. (and (file-readable-p file) file)))
  12182. files))))
  12183. (when (or (eq archives t)
  12184. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12185. (setq files (org-add-archive-files files)))
  12186. files))
  12187. (defun org-edit-agenda-file-list ()
  12188. "Edit the list of agenda files.
  12189. Depending on setup, this either uses customize to edit the variable
  12190. `org-agenda-files', or it visits the file that is holding the list. In the
  12191. latter case, the buffer is set up in a way that saving it automatically kills
  12192. the buffer and restores the previous window configuration."
  12193. (interactive)
  12194. (if (stringp org-agenda-files)
  12195. (let ((cw (current-window-configuration)))
  12196. (find-file org-agenda-files)
  12197. (org-set-local 'org-window-configuration cw)
  12198. (org-add-hook 'after-save-hook
  12199. (lambda ()
  12200. (set-window-configuration
  12201. (prog1 org-window-configuration
  12202. (kill-buffer (current-buffer))))
  12203. (org-install-agenda-files-menu)
  12204. (message "New agenda file list installed"))
  12205. nil 'local)
  12206. (message "%s" (substitute-command-keys
  12207. "Edit list and finish with \\[save-buffer]")))
  12208. (customize-variable 'org-agenda-files)))
  12209. (defun org-store-new-agenda-file-list (list)
  12210. "Set new value for the agenda file list and save it correctly."
  12211. (if (stringp org-agenda-files)
  12212. (let ((f org-agenda-files) b)
  12213. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12214. (with-temp-file f
  12215. (insert (mapconcat 'identity list "\n") "\n")))
  12216. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12217. (setq org-agenda-files list)
  12218. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12219. (defun org-read-agenda-file-list ()
  12220. "Read the list of agenda files from a file."
  12221. (when (file-directory-p org-agenda-files)
  12222. (error "`org-agenda-files' cannot be a single directory"))
  12223. (when (stringp org-agenda-files)
  12224. (with-temp-buffer
  12225. (insert-file-contents org-agenda-files)
  12226. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12227. ;;;###autoload
  12228. (defun org-cycle-agenda-files ()
  12229. "Cycle through the files in `org-agenda-files'.
  12230. If the current buffer visits an agenda file, find the next one in the list.
  12231. If the current buffer does not, find the first agenda file."
  12232. (interactive)
  12233. (let* ((fs (org-agenda-files t))
  12234. (files (append fs (list (car fs))))
  12235. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12236. file)
  12237. (unless files (error "No agenda files"))
  12238. (catch 'exit
  12239. (while (setq file (pop files))
  12240. (if (equal (file-truename file) tcf)
  12241. (when (car files)
  12242. (find-file (car files))
  12243. (throw 'exit t))))
  12244. (find-file (car fs)))
  12245. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12246. (defun org-agenda-file-to-front (&optional to-end)
  12247. "Move/add the current file to the top of the agenda file list.
  12248. If the file is not present in the list, it is added to the front. If it is
  12249. present, it is moved there. With optional argument TO-END, add/move to the
  12250. end of the list."
  12251. (interactive "P")
  12252. (let ((org-agenda-skip-unavailable-files nil)
  12253. (file-alist (mapcar (lambda (x)
  12254. (cons (file-truename x) x))
  12255. (org-agenda-files t)))
  12256. (ctf (file-truename buffer-file-name))
  12257. x had)
  12258. (setq x (assoc ctf file-alist) had x)
  12259. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12260. (if to-end
  12261. (setq file-alist (append (delq x file-alist) (list x)))
  12262. (setq file-alist (cons x (delq x file-alist))))
  12263. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12264. (org-install-agenda-files-menu)
  12265. (message "File %s to %s of agenda file list"
  12266. (if had "moved" "added") (if to-end "end" "front"))))
  12267. (defun org-remove-file (&optional file)
  12268. "Remove current file from the list of files in variable `org-agenda-files'.
  12269. These are the files which are being checked for agenda entries.
  12270. Optional argument FILE means, use this file instead of the current."
  12271. (interactive)
  12272. (let* ((org-agenda-skip-unavailable-files nil)
  12273. (file (or file buffer-file-name))
  12274. (true-file (file-truename file))
  12275. (afile (abbreviate-file-name file))
  12276. (files (delq nil (mapcar
  12277. (lambda (x)
  12278. (if (equal true-file
  12279. (file-truename x))
  12280. nil x))
  12281. (org-agenda-files t)))))
  12282. (if (not (= (length files) (length (org-agenda-files t))))
  12283. (progn
  12284. (org-store-new-agenda-file-list files)
  12285. (org-install-agenda-files-menu)
  12286. (message "Removed file: %s" afile))
  12287. (message "File was not in list: %s (not removed)" afile))))
  12288. (defun org-file-menu-entry (file)
  12289. (vector file (list 'find-file file) t))
  12290. (defun org-check-agenda-file (file)
  12291. "Make sure FILE exists. If not, ask user what to do."
  12292. (when (not (file-exists-p file))
  12293. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12294. (abbreviate-file-name file))
  12295. (let ((r (downcase (read-char-exclusive))))
  12296. (cond
  12297. ((equal r ?r)
  12298. (org-remove-file file)
  12299. (throw 'nextfile t))
  12300. (t (error "Abort"))))))
  12301. (defun org-get-agenda-file-buffer (file)
  12302. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12303. it to the list of buffers which might be released later."
  12304. (let ((buf (org-find-base-buffer-visiting file)))
  12305. (if buf
  12306. buf ; just return it
  12307. ;; Make a new buffer and remember it
  12308. (setq buf (find-file-noselect file))
  12309. (if buf (push buf org-agenda-new-buffers))
  12310. buf)))
  12311. (defun org-release-buffers (blist)
  12312. "Release all buffers in list, asking the user for confirmation when needed.
  12313. When a buffer is unmodified, it is just killed. When modified, it is saved
  12314. \(if the user agrees) and then killed."
  12315. (let (buf file)
  12316. (while (setq buf (pop blist))
  12317. (setq file (buffer-file-name buf))
  12318. (when (and (buffer-modified-p buf)
  12319. file
  12320. (y-or-n-p (format "Save file %s? " file)))
  12321. (with-current-buffer buf (save-buffer)))
  12322. (kill-buffer buf))))
  12323. (defun org-prepare-agenda-buffers (files)
  12324. "Create buffers for all agenda files, protect archived trees and comments."
  12325. (interactive)
  12326. (let ((pa '(:org-archived t))
  12327. (pc '(:org-comment t))
  12328. (pall '(:org-archived t :org-comment t))
  12329. (inhibit-read-only t)
  12330. (rea (concat ":" org-archive-tag ":"))
  12331. bmp file re)
  12332. (save-excursion
  12333. (save-restriction
  12334. (while (setq file (pop files))
  12335. (catch 'nextfile
  12336. (if (bufferp file)
  12337. (set-buffer file)
  12338. (org-check-agenda-file file)
  12339. (set-buffer (org-get-agenda-file-buffer file)))
  12340. (widen)
  12341. (setq bmp (buffer-modified-p))
  12342. (org-refresh-category-properties)
  12343. (setq org-todo-keywords-for-agenda
  12344. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12345. (setq org-done-keywords-for-agenda
  12346. (append org-done-keywords-for-agenda org-done-keywords))
  12347. (setq org-todo-keyword-alist-for-agenda
  12348. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12349. (setq org-tag-alist-for-agenda
  12350. (append org-tag-alist-for-agenda org-tag-alist))
  12351. (save-excursion
  12352. (remove-text-properties (point-min) (point-max) pall)
  12353. (when org-agenda-skip-archived-trees
  12354. (goto-char (point-min))
  12355. (while (re-search-forward rea nil t)
  12356. (if (org-on-heading-p t)
  12357. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12358. (goto-char (point-min))
  12359. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12360. (while (re-search-forward re nil t)
  12361. (add-text-properties
  12362. (match-beginning 0) (org-end-of-subtree t) pc)))
  12363. (set-buffer-modified-p bmp)))))
  12364. (setq org-todo-keyword-alist-for-agenda
  12365. (org-uniquify org-todo-keyword-alist-for-agenda)
  12366. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12367. ;;;; Embedded LaTeX
  12368. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12369. "Keymap for the minor `org-cdlatex-mode'.")
  12370. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12371. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12372. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12373. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12374. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12375. (defvar org-cdlatex-texmathp-advice-is-done nil
  12376. "Flag remembering if we have applied the advice to texmathp already.")
  12377. (define-minor-mode org-cdlatex-mode
  12378. "Toggle the minor `org-cdlatex-mode'.
  12379. This mode supports entering LaTeX environment and math in LaTeX fragments
  12380. in Org-mode.
  12381. \\{org-cdlatex-mode-map}"
  12382. nil " OCDL" nil
  12383. (when org-cdlatex-mode (require 'cdlatex))
  12384. (unless org-cdlatex-texmathp-advice-is-done
  12385. (setq org-cdlatex-texmathp-advice-is-done t)
  12386. (defadvice texmathp (around org-math-always-on activate)
  12387. "Always return t in org-mode buffers.
  12388. This is because we want to insert math symbols without dollars even outside
  12389. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12390. an embedded LaTeX fragment, let texmathp do its job.
  12391. \\[org-cdlatex-mode-map]"
  12392. (interactive)
  12393. (let (p)
  12394. (cond
  12395. ((not (org-mode-p)) ad-do-it)
  12396. ((eq this-command 'cdlatex-math-symbol)
  12397. (setq ad-return-value t
  12398. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12399. (t
  12400. (let ((p (org-inside-LaTeX-fragment-p)))
  12401. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12402. (setq ad-return-value t
  12403. texmathp-why '("Org-mode embedded math" . 0))
  12404. (if p ad-do-it)))))))))
  12405. (defun turn-on-org-cdlatex ()
  12406. "Unconditionally turn on `org-cdlatex-mode'."
  12407. (org-cdlatex-mode 1))
  12408. (defun org-inside-LaTeX-fragment-p ()
  12409. "Test if point is inside a LaTeX fragment.
  12410. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12411. sequence appearing also before point.
  12412. Even though the matchers for math are configurable, this function assumes
  12413. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12414. delimiters are skipped when they have been removed by customization.
  12415. The return value is nil, or a cons cell with the delimiter and
  12416. and the position of this delimiter.
  12417. This function does a reasonably good job, but can locally be fooled by
  12418. for example currency specifications. For example it will assume being in
  12419. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12420. fragments that are properly closed, but during editing, we have to live
  12421. with the uncertainty caused by missing closing delimiters. This function
  12422. looks only before point, not after."
  12423. (catch 'exit
  12424. (let ((pos (point))
  12425. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12426. (lim (progn
  12427. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12428. (point)))
  12429. dd-on str (start 0) m re)
  12430. (goto-char pos)
  12431. (when dodollar
  12432. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12433. re (nth 1 (assoc "$" org-latex-regexps)))
  12434. (while (string-match re str start)
  12435. (cond
  12436. ((= (match-end 0) (length str))
  12437. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12438. ((= (match-end 0) (- (length str) 5))
  12439. (throw 'exit nil))
  12440. (t (setq start (match-end 0))))))
  12441. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12442. (goto-char pos)
  12443. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12444. (and (match-beginning 2) (throw 'exit nil))
  12445. ;; count $$
  12446. (while (re-search-backward "\\$\\$" lim t)
  12447. (setq dd-on (not dd-on)))
  12448. (goto-char pos)
  12449. (if dd-on (cons "$$" m))))))
  12450. (defun org-try-cdlatex-tab ()
  12451. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12452. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12453. - inside a LaTeX fragment, or
  12454. - after the first word in a line, where an abbreviation expansion could
  12455. insert a LaTeX environment."
  12456. (when org-cdlatex-mode
  12457. (cond
  12458. ((save-excursion
  12459. (skip-chars-backward "a-zA-Z0-9*")
  12460. (skip-chars-backward " \t")
  12461. (bolp))
  12462. (cdlatex-tab) t)
  12463. ((org-inside-LaTeX-fragment-p)
  12464. (cdlatex-tab) t)
  12465. (t nil))))
  12466. (defun org-cdlatex-underscore-caret (&optional arg)
  12467. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12468. Revert to the normal definition outside of these fragments."
  12469. (interactive "P")
  12470. (if (org-inside-LaTeX-fragment-p)
  12471. (call-interactively 'cdlatex-sub-superscript)
  12472. (let (org-cdlatex-mode)
  12473. (call-interactively (key-binding (vector last-input-event))))))
  12474. (defun org-cdlatex-math-modify (&optional arg)
  12475. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12476. Revert to the normal definition outside of these fragments."
  12477. (interactive "P")
  12478. (if (org-inside-LaTeX-fragment-p)
  12479. (call-interactively 'cdlatex-math-modify)
  12480. (let (org-cdlatex-mode)
  12481. (call-interactively (key-binding (vector last-input-event))))))
  12482. (defvar org-latex-fragment-image-overlays nil
  12483. "List of overlays carrying the images of latex fragments.")
  12484. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12485. (defun org-remove-latex-fragment-image-overlays ()
  12486. "Remove all overlays with LaTeX fragment images in current buffer."
  12487. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12488. (setq org-latex-fragment-image-overlays nil))
  12489. (defun org-preview-latex-fragment (&optional subtree)
  12490. "Preview the LaTeX fragment at point, or all locally or globally.
  12491. If the cursor is in a LaTeX fragment, create the image and overlay
  12492. it over the source code. If there is no fragment at point, display
  12493. all fragments in the current text, from one headline to the next. With
  12494. prefix SUBTREE, display all fragments in the current subtree. With a
  12495. double prefix `C-u C-u', or when the cursor is before the first headline,
  12496. display all fragments in the buffer.
  12497. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12498. (interactive "P")
  12499. (org-remove-latex-fragment-image-overlays)
  12500. (save-excursion
  12501. (save-restriction
  12502. (let (beg end at msg)
  12503. (cond
  12504. ((or (equal subtree '(16))
  12505. (not (save-excursion
  12506. (re-search-backward (concat "^" outline-regexp) nil t))))
  12507. (setq beg (point-min) end (point-max)
  12508. msg "Creating images for buffer...%s"))
  12509. ((equal subtree '(4))
  12510. (org-back-to-heading)
  12511. (setq beg (point) end (org-end-of-subtree t)
  12512. msg "Creating images for subtree...%s"))
  12513. (t
  12514. (if (setq at (org-inside-LaTeX-fragment-p))
  12515. (goto-char (max (point-min) (- (cdr at) 2)))
  12516. (org-back-to-heading))
  12517. (setq beg (point) end (progn (outline-next-heading) (point))
  12518. msg (if at "Creating image...%s"
  12519. "Creating images for entry...%s"))))
  12520. (message msg "")
  12521. (narrow-to-region beg end)
  12522. (goto-char beg)
  12523. (org-format-latex
  12524. (concat "ltxpng/" (file-name-sans-extension
  12525. (file-name-nondirectory
  12526. buffer-file-name)))
  12527. default-directory 'overlays msg at 'forbuffer)
  12528. (message msg "done. Use `C-c C-c' to remove images.")))))
  12529. (defvar org-latex-regexps
  12530. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12531. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12532. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12533. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12534. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12535. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12536. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12537. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12538. "Regular expressions for matching embedded LaTeX.")
  12539. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12540. "Replace LaTeX fragments with links to an image, and produce images."
  12541. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12542. (let* ((prefixnodir (file-name-nondirectory prefix))
  12543. (absprefix (expand-file-name prefix dir))
  12544. (todir (file-name-directory absprefix))
  12545. (opt org-format-latex-options)
  12546. (matchers (plist-get opt :matchers))
  12547. (re-list org-latex-regexps)
  12548. (cnt 0) txt link beg end re e checkdir
  12549. executables-checked
  12550. m n block linkfile movefile ov)
  12551. ;; Check if there are old images files with this prefix, and remove them
  12552. (when (file-directory-p todir)
  12553. (mapc 'delete-file
  12554. (directory-files
  12555. todir 'full
  12556. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12557. ;; Check the different regular expressions
  12558. (while (setq e (pop re-list))
  12559. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12560. block (if (nth 3 e) "\n\n" ""))
  12561. (when (member m matchers)
  12562. (goto-char (point-min))
  12563. (while (re-search-forward re nil t)
  12564. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12565. (not (get-text-property (match-beginning n)
  12566. 'org-protected)))
  12567. (setq txt (match-string n)
  12568. beg (match-beginning n) end (match-end n)
  12569. cnt (1+ cnt)
  12570. linkfile (format "%s_%04d.png" prefix cnt)
  12571. movefile (format "%s_%04d.png" absprefix cnt)
  12572. link (concat block "[[file:" linkfile "]]" block))
  12573. (if msg (message msg cnt))
  12574. (goto-char beg)
  12575. (unless checkdir ; make sure the directory exists
  12576. (setq checkdir t)
  12577. (or (file-directory-p todir) (make-directory todir)))
  12578. (unless executables-checked
  12579. (org-check-external-command
  12580. "latex" "needed to convert LaTeX fragments to images")
  12581. (org-check-external-command
  12582. "dvipng" "needed to convert LaTeX fragments to images")
  12583. (setq executables-checked t))
  12584. (org-create-formula-image
  12585. txt movefile opt forbuffer)
  12586. (if overlays
  12587. (progn
  12588. (setq ov (org-make-overlay beg end))
  12589. (if (featurep 'xemacs)
  12590. (progn
  12591. (org-overlay-put ov 'invisible t)
  12592. (org-overlay-put
  12593. ov 'end-glyph
  12594. (make-glyph (vector 'png :file movefile))))
  12595. (org-overlay-put
  12596. ov 'display
  12597. (list 'image :type 'png :file movefile :ascent 'center)))
  12598. (push ov org-latex-fragment-image-overlays)
  12599. (goto-char end))
  12600. (delete-region beg end)
  12601. (insert link))))))))
  12602. ;; This function borrows from Ganesh Swami's latex2png.el
  12603. (defun org-create-formula-image (string tofile options buffer)
  12604. (let* ((tmpdir (if (featurep 'xemacs)
  12605. (temp-directory)
  12606. temporary-file-directory))
  12607. (texfilebase (make-temp-name
  12608. (expand-file-name "orgtex" tmpdir)))
  12609. (texfile (concat texfilebase ".tex"))
  12610. (dvifile (concat texfilebase ".dvi"))
  12611. (pngfile (concat texfilebase ".png"))
  12612. (fnh (if (featurep 'xemacs)
  12613. (font-height (get-face-font 'default))
  12614. (face-attribute 'default :height nil)))
  12615. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12616. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12617. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12618. "Black"))
  12619. (bg (or (plist-get options (if buffer :background :html-background))
  12620. "Transparent")))
  12621. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12622. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12623. (with-temp-file texfile
  12624. (insert org-format-latex-header
  12625. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12626. (let ((dir default-directory))
  12627. (condition-case nil
  12628. (progn
  12629. (cd tmpdir)
  12630. (call-process "latex" nil nil nil texfile))
  12631. (error nil))
  12632. (cd dir))
  12633. (if (not (file-exists-p dvifile))
  12634. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12635. (condition-case nil
  12636. (call-process "dvipng" nil nil nil
  12637. "-E" "-fg" fg "-bg" bg
  12638. "-D" dpi
  12639. ;;"-x" scale "-y" scale
  12640. "-T" "tight"
  12641. "-o" pngfile
  12642. dvifile)
  12643. (error nil))
  12644. (if (not (file-exists-p pngfile))
  12645. (progn (message "Failed to create png file from %s" texfile) nil)
  12646. ;; Use the requested file name and clean up
  12647. (copy-file pngfile tofile 'replace)
  12648. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12649. (delete-file (concat texfilebase e)))
  12650. pngfile))))
  12651. (defun org-dvipng-color (attr)
  12652. "Return an rgb color specification for dvipng."
  12653. (apply 'format "rgb %s %s %s"
  12654. (mapcar 'org-normalize-color
  12655. (color-values (face-attribute 'default attr nil)))))
  12656. (defun org-normalize-color (value)
  12657. "Return string to be used as color value for an RGB component."
  12658. (format "%g" (/ value 65535.0)))
  12659. ;;;; Key bindings
  12660. ;; Make `C-c C-x' a prefix key
  12661. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12662. ;; TAB key with modifiers
  12663. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12664. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12665. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12666. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12667. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12668. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12669. ;; The following line is necessary under Suse GNU/Linux
  12670. (unless (featurep 'xemacs)
  12671. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12672. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12673. (define-key org-mode-map [backtab] 'org-shifttab)
  12674. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12675. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12676. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12677. ;; Cursor keys with modifiers
  12678. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12679. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12680. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12681. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12682. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12683. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12684. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12685. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12686. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12687. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12688. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12689. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12690. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12691. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12692. ;;; Extra keys for tty access.
  12693. ;; We only set them when really needed because otherwise the
  12694. ;; menus don't show the simple keys
  12695. (when (or org-use-extra-keys
  12696. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12697. (not window-system))
  12698. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12699. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12700. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12701. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12702. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12703. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12704. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12705. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12706. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12707. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12708. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12709. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12710. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12711. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12712. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12713. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12714. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12715. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12716. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12717. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12718. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12719. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12720. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12721. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12722. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12723. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12724. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12725. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12726. ;; All the other keys
  12727. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12728. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12729. (if (boundp 'narrow-map)
  12730. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12731. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12732. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12733. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12734. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12735. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12736. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12737. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12738. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12739. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12740. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12741. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12742. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12743. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12744. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12745. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12746. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12747. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12748. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12749. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12750. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12751. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12752. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12753. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12754. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12755. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12756. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12757. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12758. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12759. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12760. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12761. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12762. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12763. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12764. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12765. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12766. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12767. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12768. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12769. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12770. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12771. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12772. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12773. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12774. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12775. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12776. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12777. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12778. (org-defkey org-mode-map "\C-m" 'org-return)
  12779. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12780. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12781. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12782. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12783. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12784. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12785. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12786. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12787. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12788. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12789. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12790. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12791. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12792. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12793. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12794. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12795. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12796. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  12797. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  12798. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12799. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12800. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12801. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12802. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12803. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12804. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12805. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12806. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12807. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12808. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12809. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12810. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12811. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12812. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12813. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12814. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12815. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  12816. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12817. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12818. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12819. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12820. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12821. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12822. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12823. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12824. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12825. (when (featurep 'xemacs)
  12826. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12827. (defvar org-self-insert-command-undo-counter 0)
  12828. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12829. (defun org-self-insert-command (N)
  12830. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12831. If the cursor is in a table looking at whitespace, the whitespace is
  12832. overwritten, and the table is not marked as requiring realignment."
  12833. (interactive "p")
  12834. (if (and
  12835. (org-table-p)
  12836. (progn
  12837. ;; check if we blank the field, and if that triggers align
  12838. (and (featurep 'org-table) org-table-auto-blank-field
  12839. (member last-command
  12840. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  12841. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12842. ;; got extra space, this field does not determine column width
  12843. (let (org-table-may-need-update) (org-table-blank-field))
  12844. ;; no extra space, this field may determine column width
  12845. (org-table-blank-field)))
  12846. t)
  12847. (eq N 1)
  12848. (looking-at "[^|\n]* |"))
  12849. (let (org-table-may-need-update)
  12850. (goto-char (1- (match-end 0)))
  12851. (delete-backward-char 1)
  12852. (goto-char (match-beginning 0))
  12853. (self-insert-command N))
  12854. (setq org-table-may-need-update t)
  12855. (self-insert-command N)
  12856. (org-fix-tags-on-the-fly)
  12857. (if org-self-insert-cluster-for-undo
  12858. (if (not (eq last-command 'org-self-insert-command))
  12859. (setq org-self-insert-command-undo-counter 1)
  12860. (if (>= org-self-insert-command-undo-counter 20)
  12861. (setq org-self-insert-command-undo-counter 1)
  12862. (and (> org-self-insert-command-undo-counter 0)
  12863. buffer-undo-list
  12864. (not (cadr buffer-undo-list)) ; remove nil entry
  12865. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12866. (setq org-self-insert-command-undo-counter
  12867. (1+ org-self-insert-command-undo-counter)))))))
  12868. (defun org-fix-tags-on-the-fly ()
  12869. (when (and (equal (char-after (point-at-bol)) ?*)
  12870. (org-on-heading-p))
  12871. (org-align-tags-here org-tags-column)))
  12872. (defun org-delete-backward-char (N)
  12873. "Like `delete-backward-char', insert whitespace at field end in tables.
  12874. When deleting backwards, in tables this function will insert whitespace in
  12875. front of the next \"|\" separator, to keep the table aligned. The table will
  12876. still be marked for re-alignment if the field did fill the entire column,
  12877. because, in this case the deletion might narrow the column."
  12878. (interactive "p")
  12879. (if (and (org-table-p)
  12880. (eq N 1)
  12881. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12882. (looking-at ".*?|"))
  12883. (let ((pos (point))
  12884. (noalign (looking-at "[^|\n\r]* |"))
  12885. (c org-table-may-need-update))
  12886. (backward-delete-char N)
  12887. (skip-chars-forward "^|")
  12888. (insert " ")
  12889. (goto-char (1- pos))
  12890. ;; noalign: if there were two spaces at the end, this field
  12891. ;; does not determine the width of the column.
  12892. (if noalign (setq org-table-may-need-update c)))
  12893. (backward-delete-char N)
  12894. (org-fix-tags-on-the-fly)))
  12895. (defun org-delete-char (N)
  12896. "Like `delete-char', but insert whitespace at field end in tables.
  12897. When deleting characters, in tables this function will insert whitespace in
  12898. front of the next \"|\" separator, to keep the table aligned. The table will
  12899. still be marked for re-alignment if the field did fill the entire column,
  12900. because, in this case the deletion might narrow the column."
  12901. (interactive "p")
  12902. (if (and (org-table-p)
  12903. (not (bolp))
  12904. (not (= (char-after) ?|))
  12905. (eq N 1))
  12906. (if (looking-at ".*?|")
  12907. (let ((pos (point))
  12908. (noalign (looking-at "[^|\n\r]* |"))
  12909. (c org-table-may-need-update))
  12910. (replace-match (concat
  12911. (substring (match-string 0) 1 -1)
  12912. " |"))
  12913. (goto-char pos)
  12914. ;; noalign: if there were two spaces at the end, this field
  12915. ;; does not determine the width of the column.
  12916. (if noalign (setq org-table-may-need-update c)))
  12917. (delete-char N))
  12918. (delete-char N)
  12919. (org-fix-tags-on-the-fly)))
  12920. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12921. (put 'org-self-insert-command 'delete-selection t)
  12922. (put 'orgtbl-self-insert-command 'delete-selection t)
  12923. (put 'org-delete-char 'delete-selection 'supersede)
  12924. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12925. (put 'org-yank 'delete-selection 'yank)
  12926. ;; Make `flyspell-mode' delay after some commands
  12927. (put 'org-self-insert-command 'flyspell-delayed t)
  12928. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12929. (put 'org-delete-char 'flyspell-delayed t)
  12930. (put 'org-delete-backward-char 'flyspell-delayed t)
  12931. ;; Make pabbrev-mode expand after org-mode commands
  12932. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12933. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12934. ;; How to do this: Measure non-white length of current string
  12935. ;; If equal to column width, we should realign.
  12936. (defun org-remap (map &rest commands)
  12937. "In MAP, remap the functions given in COMMANDS.
  12938. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12939. (let (new old)
  12940. (while commands
  12941. (setq old (pop commands) new (pop commands))
  12942. (if (fboundp 'command-remapping)
  12943. (org-defkey map (vector 'remap old) new)
  12944. (substitute-key-definition old new map global-map)))))
  12945. (when (eq org-enable-table-editor 'optimized)
  12946. ;; If the user wants maximum table support, we need to hijack
  12947. ;; some standard editing functions
  12948. (org-remap org-mode-map
  12949. 'self-insert-command 'org-self-insert-command
  12950. 'delete-char 'org-delete-char
  12951. 'delete-backward-char 'org-delete-backward-char)
  12952. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12953. (defvar org-ctrl-c-ctrl-c-hook nil
  12954. "Hook for functions attaching themselves to `C-c C-c'.
  12955. This can be used to add additional functionality to the C-c C-c key which
  12956. executes context-dependent commands.
  12957. Each function will be called with no arguments. The function must check
  12958. if the context is appropriate for it to act. If yes, it should do its
  12959. thing and then return a non-nil value. If the context is wrong,
  12960. just do nothing and return nil.")
  12961. (defvar org-tab-first-hook nil
  12962. "Hook for functions to attach themselves to TAB.
  12963. See `org-ctrl-c-ctrl-c-hook' for more information.
  12964. This hook runs as the first action when TAB is pressed, even before
  12965. `org-cycle' messes around with the `outline-regexp' to cater for
  12966. inline tasks and plain list item folding.
  12967. If any function in this hook returns t, not other actions like table
  12968. field motion visibility cycling will be done.")
  12969. (defvar org-tab-after-check-for-table-hook nil
  12970. "Hook for functions to attach themselves to TAB.
  12971. See `org-ctrl-c-ctrl-c-hook' for more information.
  12972. This hook runs after it has been established that the cursor is not in a
  12973. table, but before checking if the cursor is in a headline or if global cycling
  12974. should be done.
  12975. If any function in this hook returns t, not other actions like visibility
  12976. cycling will be done.")
  12977. (defvar org-tab-after-check-for-cycling-hook nil
  12978. "Hook for functions to attach themselves to TAB.
  12979. See `org-ctrl-c-ctrl-c-hook' for more information.
  12980. This hook runs after it has been established that not table field motion and
  12981. not visibility should be done because of current context. This is probably
  12982. the place where a package like yasnippets can hook in.")
  12983. (defvar org-metaleft-hook nil
  12984. "Hook for functions attaching themselves to `M-left'.
  12985. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12986. (defvar org-metaright-hook nil
  12987. "Hook for functions attaching themselves to `M-right'.
  12988. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12989. (defvar org-metaup-hook nil
  12990. "Hook for functions attaching themselves to `M-up'.
  12991. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12992. (defvar org-metadown-hook nil
  12993. "Hook for functions attaching themselves to `M-down'.
  12994. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12995. (defvar org-shiftmetaleft-hook nil
  12996. "Hook for functions attaching themselves to `M-S-left'.
  12997. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12998. (defvar org-shiftmetaright-hook nil
  12999. "Hook for functions attaching themselves to `M-S-right'.
  13000. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13001. (defvar org-shiftmetaup-hook nil
  13002. "Hook for functions attaching themselves to `M-S-up'.
  13003. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13004. (defvar org-shiftmetadown-hook nil
  13005. "Hook for functions attaching themselves to `M-S-down'.
  13006. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13007. (defvar org-metareturn-hook nil
  13008. "Hook for functions attaching themselves to `M-RET'.
  13009. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13010. (defun org-modifier-cursor-error ()
  13011. "Throw an error, a modified cursor command was applied in wrong context."
  13012. (error "This command is active in special context like tables, headlines or items"))
  13013. (defun org-shiftselect-error ()
  13014. "Throw an error because Shift-Cursor command was applied in wrong context."
  13015. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13016. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13017. (error "This command works only in special context like headlines or timestamps.")))
  13018. (defun org-call-for-shift-select (cmd)
  13019. (let ((this-command-keys-shift-translated t))
  13020. (call-interactively cmd)))
  13021. (defun org-shifttab (&optional arg)
  13022. "Global visibility cycling or move to previous table field.
  13023. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13024. on context.
  13025. See the individual commands for more information."
  13026. (interactive "P")
  13027. (cond
  13028. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13029. ((integerp arg)
  13030. (message "Content view to level: %d" arg)
  13031. (org-content (prefix-numeric-value arg))
  13032. (setq org-cycle-global-status 'overview))
  13033. (t (call-interactively 'org-global-cycle))))
  13034. (defun org-shiftmetaleft ()
  13035. "Promote subtree or delete table column.
  13036. Calls `org-promote-subtree', `org-outdent-item',
  13037. or `org-table-delete-column', depending on context.
  13038. See the individual commands for more information."
  13039. (interactive)
  13040. (cond
  13041. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13042. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13043. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13044. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13045. (t (org-modifier-cursor-error))))
  13046. (defun org-shiftmetaright ()
  13047. "Demote subtree or insert table column.
  13048. Calls `org-demote-subtree', `org-indent-item',
  13049. or `org-table-insert-column', depending on context.
  13050. See the individual commands for more information."
  13051. (interactive)
  13052. (cond
  13053. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13054. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13055. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13056. ((org-at-item-p) (call-interactively 'org-indent-item))
  13057. (t (org-modifier-cursor-error))))
  13058. (defun org-shiftmetaup (&optional arg)
  13059. "Move subtree up or kill table row.
  13060. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13061. `org-move-item-up' depending on context. See the individual commands
  13062. for more information."
  13063. (interactive "P")
  13064. (cond
  13065. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13066. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13067. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13068. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13069. (t (org-modifier-cursor-error))))
  13070. (defun org-shiftmetadown (&optional arg)
  13071. "Move subtree down or insert table row.
  13072. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13073. `org-move-item-down', depending on context. See the individual
  13074. commands for more information."
  13075. (interactive "P")
  13076. (cond
  13077. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13078. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13079. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13080. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13081. (t (org-modifier-cursor-error))))
  13082. (defun org-metaleft (&optional arg)
  13083. "Promote heading or move table column to left.
  13084. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13085. With no specific context, calls the Emacs default `backward-word'.
  13086. See the individual commands for more information."
  13087. (interactive "P")
  13088. (cond
  13089. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13090. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13091. ((or (org-on-heading-p)
  13092. (and (org-region-active-p)
  13093. (save-excursion
  13094. (goto-char (region-beginning))
  13095. (org-on-heading-p))))
  13096. (call-interactively 'org-do-promote))
  13097. ((or (org-at-item-p)
  13098. (and (org-region-active-p)
  13099. (save-excursion
  13100. (goto-char (region-beginning))
  13101. (org-at-item-p))))
  13102. (call-interactively 'org-outdent-item))
  13103. (t (call-interactively 'backward-word))))
  13104. (defun org-metaright (&optional arg)
  13105. "Demote subtree or move table column to right.
  13106. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13107. With no specific context, calls the Emacs default `forward-word'.
  13108. See the individual commands for more information."
  13109. (interactive "P")
  13110. (cond
  13111. ((run-hook-with-args-until-success 'org-metaright-hook))
  13112. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13113. ((or (org-on-heading-p)
  13114. (and (org-region-active-p)
  13115. (save-excursion
  13116. (goto-char (region-beginning))
  13117. (org-on-heading-p))))
  13118. (call-interactively 'org-do-demote))
  13119. ((or (org-at-item-p)
  13120. (and (org-region-active-p)
  13121. (save-excursion
  13122. (goto-char (region-beginning))
  13123. (org-at-item-p))))
  13124. (call-interactively 'org-indent-item))
  13125. (t (call-interactively 'forward-word))))
  13126. (defun org-metaup (&optional arg)
  13127. "Move subtree up or move table row up.
  13128. Calls `org-move-subtree-up' or `org-table-move-row' or
  13129. `org-move-item-up', depending on context. See the individual commands
  13130. for more information."
  13131. (interactive "P")
  13132. (cond
  13133. ((run-hook-with-args-until-success 'org-metaup-hook))
  13134. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13135. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13136. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13137. (t (transpose-lines 1) (beginning-of-line -1))))
  13138. (defun org-metadown (&optional arg)
  13139. "Move subtree down or move table row down.
  13140. Calls `org-move-subtree-down' or `org-table-move-row' or
  13141. `org-move-item-down', depending on context. See the individual
  13142. commands for more information."
  13143. (interactive "P")
  13144. (cond
  13145. ((run-hook-with-args-until-success 'org-metadown-hook))
  13146. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13147. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13148. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13149. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13150. (defun org-shiftup (&optional arg)
  13151. "Increase item in timestamp or increase priority of current headline.
  13152. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13153. depending on context. See the individual commands for more information."
  13154. (interactive "P")
  13155. (cond
  13156. ((and org-support-shift-select (org-region-active-p))
  13157. (org-call-for-shift-select 'previous-line))
  13158. ((org-at-timestamp-p t)
  13159. (call-interactively (if org-edit-timestamp-down-means-later
  13160. 'org-timestamp-down 'org-timestamp-up)))
  13161. ((and (not (eq org-support-shift-select 'always))
  13162. org-enable-priority-commands
  13163. (org-on-heading-p))
  13164. (call-interactively 'org-priority-up))
  13165. ((and (not org-support-shift-select) (org-at-item-p))
  13166. (call-interactively 'org-previous-item))
  13167. ((org-clocktable-try-shift 'up arg))
  13168. (org-support-shift-select
  13169. (org-call-for-shift-select 'previous-line))
  13170. (t (org-shiftselect-error))))
  13171. (defun org-shiftdown (&optional arg)
  13172. "Decrease item in timestamp or decrease priority of current headline.
  13173. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13174. depending on context. See the individual commands for more information."
  13175. (interactive "P")
  13176. (cond
  13177. ((and org-support-shift-select (org-region-active-p))
  13178. (org-call-for-shift-select 'next-line))
  13179. ((org-at-timestamp-p t)
  13180. (call-interactively (if org-edit-timestamp-down-means-later
  13181. 'org-timestamp-up 'org-timestamp-down)))
  13182. ((and (not (eq org-support-shift-select 'always))
  13183. org-enable-priority-commands
  13184. (org-on-heading-p))
  13185. (call-interactively 'org-priority-down))
  13186. ((and (not org-support-shift-select) (org-at-item-p))
  13187. (call-interactively 'org-next-item))
  13188. ((org-clocktable-try-shift 'down arg))
  13189. (org-support-shift-select
  13190. (org-call-for-shift-select 'next-line))
  13191. (t (org-shiftselect-error))))
  13192. (defun org-shiftright (&optional arg)
  13193. "Cycle the thing at point or in the current line, depending on context.
  13194. Depending on context, this does one of the following:
  13195. - switch a timestamp at point one day into the future
  13196. - on a headline, switch to the next TODO keyword.
  13197. - on an item, switch entire list to the next bullet type
  13198. - on a property line, switch to the next allowed value
  13199. - on a clocktable definition line, move time block into the future"
  13200. (interactive "P")
  13201. (cond
  13202. ((and org-support-shift-select (org-region-active-p))
  13203. (org-call-for-shift-select 'forward-char))
  13204. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13205. ((and (not (eq org-support-shift-select 'always))
  13206. (org-on-heading-p))
  13207. (let ((org-inhibit-logging
  13208. (not org-treat-S-cursor-todo-selection-as-state-change))
  13209. (org-inhibit-blocking
  13210. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13211. (org-call-with-arg 'org-todo 'right)))
  13212. ((or (and org-support-shift-select
  13213. (not (eq org-support-shift-select 'always))
  13214. (org-at-item-bullet-p))
  13215. (and (not org-support-shift-select) (org-at-item-p)))
  13216. (org-call-with-arg 'org-cycle-list-bullet nil))
  13217. ((and (not (eq org-support-shift-select 'always))
  13218. (org-at-property-p))
  13219. (call-interactively 'org-property-next-allowed-value))
  13220. ((org-clocktable-try-shift 'right arg))
  13221. (org-support-shift-select
  13222. (org-call-for-shift-select 'forward-char))
  13223. (t (org-shiftselect-error))))
  13224. (defun org-shiftleft (&optional arg)
  13225. "Cycle the thing at point or in the current line, depending on context.
  13226. Depending on context, this does one of the following:
  13227. - switch a timestamp at point one day into the past
  13228. - on a headline, switch to the previous TODO keyword.
  13229. - on an item, switch entire list to the previous bullet type
  13230. - on a property line, switch to the previous allowed value
  13231. - on a clocktable definition line, move time block into the past"
  13232. (interactive "P")
  13233. (cond
  13234. ((and org-support-shift-select (org-region-active-p))
  13235. (org-call-for-shift-select 'backward-char))
  13236. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13237. ((and (not (eq org-support-shift-select 'always))
  13238. (org-on-heading-p))
  13239. (let ((org-inhibit-logging
  13240. (not org-treat-S-cursor-todo-selection-as-state-change))
  13241. (org-inhibit-blocking
  13242. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13243. (org-call-with-arg 'org-todo 'left)))
  13244. ((or (and org-support-shift-select
  13245. (not (eq org-support-shift-select 'always))
  13246. (org-at-item-bullet-p))
  13247. (and (not org-support-shift-select) (org-at-item-p)))
  13248. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13249. ((and (not (eq org-support-shift-select 'always))
  13250. (org-at-property-p))
  13251. (call-interactively 'org-property-previous-allowed-value))
  13252. ((org-clocktable-try-shift 'left arg))
  13253. (org-support-shift-select
  13254. (org-call-for-shift-select 'backward-char))
  13255. (t (org-shiftselect-error))))
  13256. (defun org-shiftcontrolright ()
  13257. "Switch to next TODO set."
  13258. (interactive)
  13259. (cond
  13260. ((and org-support-shift-select (org-region-active-p))
  13261. (org-call-for-shift-select 'forward-word))
  13262. ((and (not (eq org-support-shift-select 'always))
  13263. (org-on-heading-p))
  13264. (org-call-with-arg 'org-todo 'nextset))
  13265. (org-support-shift-select
  13266. (org-call-for-shift-select 'forward-word))
  13267. (t (org-shiftselect-error))))
  13268. (defun org-shiftcontrolleft ()
  13269. "Switch to previous TODO set."
  13270. (interactive)
  13271. (cond
  13272. ((and org-support-shift-select (org-region-active-p))
  13273. (org-call-for-shift-select 'backward-word))
  13274. ((and (not (eq org-support-shift-select 'always))
  13275. (org-on-heading-p))
  13276. (org-call-with-arg 'org-todo 'previousset))
  13277. (org-support-shift-select
  13278. (org-call-for-shift-select 'backward-word))
  13279. (t (org-shiftselect-error))))
  13280. (defun org-ctrl-c-ret ()
  13281. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13282. (interactive)
  13283. (cond
  13284. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13285. (t (call-interactively 'org-insert-heading))))
  13286. (defun org-copy-special ()
  13287. "Copy region in table or copy current subtree.
  13288. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13289. See the individual commands for more information."
  13290. (interactive)
  13291. (call-interactively
  13292. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13293. (defun org-cut-special ()
  13294. "Cut region in table or cut current subtree.
  13295. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13296. See the individual commands for more information."
  13297. (interactive)
  13298. (call-interactively
  13299. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13300. (defun org-paste-special (arg)
  13301. "Paste rectangular region into table, or past subtree relative to level.
  13302. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13303. See the individual commands for more information."
  13304. (interactive "P")
  13305. (if (org-at-table-p)
  13306. (org-table-paste-rectangle)
  13307. (org-paste-subtree arg)))
  13308. (defun org-edit-special ()
  13309. "Call a special editor for the stuff at point.
  13310. When at a table, call the formula editor with `org-table-edit-formulas'.
  13311. When at the first line of an src example, call `org-edit-src-code'.
  13312. When in an #+include line, visit the include file. Otherwise call
  13313. `ffap' to visit the file at point."
  13314. (interactive)
  13315. (cond
  13316. ((org-at-table-p)
  13317. (call-interactively 'org-table-edit-formulas))
  13318. ((save-excursion
  13319. (beginning-of-line 1)
  13320. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13321. (find-file (org-trim (match-string 1))))
  13322. ((org-edit-src-code))
  13323. ((org-edit-fixed-width-region))
  13324. (t (call-interactively 'ffap))))
  13325. (defun org-ctrl-c-ctrl-c (&optional arg)
  13326. "Set tags in headline, or update according to changed information at point.
  13327. This command does many different things, depending on context:
  13328. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13329. this is what we do.
  13330. - If the cursor is in a headline, prompt for tags and insert them
  13331. into the current line, aligned to `org-tags-column'. When called
  13332. with prefix arg, realign all tags in the current buffer.
  13333. - If the cursor is in one of the special #+KEYWORD lines, this
  13334. triggers scanning the buffer for these lines and updating the
  13335. information.
  13336. - If the cursor is inside a table, realign the table. This command
  13337. works even if the automatic table editor has been turned off.
  13338. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13339. the entire table.
  13340. - If the cursor is at a footnote reference or definition, jump to
  13341. the corresponding definition or references, respectively.
  13342. - If the cursor is a the beginning of a dynamic block, update it.
  13343. - If the cursor is inside a table created by the table.el package,
  13344. activate that table.
  13345. - If the current buffer is a remember buffer, close note and file
  13346. it. A prefix argument of 1 files to the default location
  13347. without further interaction. A prefix argument of 2 files to
  13348. the currently clocking task.
  13349. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13350. links in this buffer.
  13351. - If the cursor is on a numbered item in a plain list, renumber the
  13352. ordered list.
  13353. - If the cursor is on a checkbox, toggle it."
  13354. (interactive "P")
  13355. (let ((org-enable-table-editor t))
  13356. (cond
  13357. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13358. org-occur-highlights
  13359. org-latex-fragment-image-overlays)
  13360. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13361. (org-remove-occur-highlights)
  13362. (org-remove-latex-fragment-image-overlays)
  13363. (message "Temporary highlights/overlays removed from current buffer"))
  13364. ((and (local-variable-p 'org-finish-function (current-buffer))
  13365. (fboundp org-finish-function))
  13366. (funcall org-finish-function))
  13367. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13368. ((org-at-property-p)
  13369. (call-interactively 'org-property-action))
  13370. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13371. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13372. ((org-at-table.el-p)
  13373. (require 'table)
  13374. (beginning-of-line 1)
  13375. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13376. (call-interactively 'table-recognize-table))
  13377. ((org-at-table-p)
  13378. (org-table-maybe-eval-formula)
  13379. (if arg
  13380. (call-interactively 'org-table-recalculate)
  13381. (org-table-maybe-recalculate-line))
  13382. (call-interactively 'org-table-align))
  13383. ((or (org-footnote-at-reference-p)
  13384. (org-footnote-at-definition-p))
  13385. (call-interactively 'org-footnote-action))
  13386. ((org-at-item-checkbox-p)
  13387. (call-interactively 'org-toggle-checkbox))
  13388. ((org-at-item-p)
  13389. (if arg
  13390. (call-interactively 'org-toggle-checkbox)
  13391. (call-interactively 'org-maybe-renumber-ordered-list)))
  13392. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13393. ;; Dynamic block
  13394. (beginning-of-line 1)
  13395. (save-excursion (org-update-dblock)))
  13396. ((save-excursion
  13397. (beginning-of-line 1)
  13398. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13399. (cond
  13400. ((equal (match-string 1) "TBLFM")
  13401. ;; Recalculate the table before this line
  13402. (save-excursion
  13403. (beginning-of-line 1)
  13404. (skip-chars-backward " \r\n\t")
  13405. (if (org-at-table-p)
  13406. (org-call-with-arg 'org-table-recalculate t))))
  13407. (t
  13408. ; (org-set-regexps-and-options)
  13409. ; (org-restart-font-lock)
  13410. (let ((org-inhibit-startup t)) (org-mode-restart))
  13411. (message "Local setup has been refreshed"))))
  13412. ((org-clock-update-time-maybe))
  13413. (t (error "C-c C-c can do nothing useful at this location.")))))
  13414. (defun org-mode-restart ()
  13415. "Restart Org-mode, to scan again for special lines.
  13416. Also updates the keyword regular expressions."
  13417. (interactive)
  13418. (org-mode)
  13419. (message "Org-mode restarted"))
  13420. (defun org-kill-note-or-show-branches ()
  13421. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13422. (interactive)
  13423. (if (not org-finish-function)
  13424. (call-interactively 'show-branches)
  13425. (let ((org-note-abort t))
  13426. (funcall org-finish-function))))
  13427. (defun org-return (&optional indent)
  13428. "Goto next table row or insert a newline.
  13429. Calls `org-table-next-row' or `newline', depending on context.
  13430. See the individual commands for more information."
  13431. (interactive)
  13432. (cond
  13433. ((bobp) (if indent (newline-and-indent) (newline)))
  13434. ((org-at-table-p)
  13435. (org-table-justify-field-maybe)
  13436. (call-interactively 'org-table-next-row))
  13437. ((and org-return-follows-link
  13438. (eq (get-text-property (point) 'face) 'org-link))
  13439. (call-interactively 'org-open-at-point))
  13440. ((and (org-at-heading-p)
  13441. (looking-at
  13442. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13443. (org-show-entry)
  13444. (end-of-line 1)
  13445. (newline))
  13446. (t (if indent (newline-and-indent) (newline)))))
  13447. (defun org-return-indent ()
  13448. "Goto next table row or insert a newline and indent.
  13449. Calls `org-table-next-row' or `newline-and-indent', depending on
  13450. context. See the individual commands for more information."
  13451. (interactive)
  13452. (org-return t))
  13453. (defun org-ctrl-c-star ()
  13454. "Compute table, or change heading status of lines.
  13455. Calls `org-table-recalculate' or `org-toggle-heading',
  13456. depending on context."
  13457. (interactive)
  13458. (cond
  13459. ((org-at-table-p)
  13460. (call-interactively 'org-table-recalculate))
  13461. (t
  13462. ;; Convert all lines in region to list items
  13463. (call-interactively 'org-toggle-heading))))
  13464. (defun org-ctrl-c-minus ()
  13465. "Insert separator line in table or modify bullet status of line.
  13466. Also turns a plain line or a region of lines into list items.
  13467. Calls `org-table-insert-hline', `org-toggle-item', or
  13468. `org-cycle-list-bullet', depending on context."
  13469. (interactive)
  13470. (cond
  13471. ((org-at-table-p)
  13472. (call-interactively 'org-table-insert-hline))
  13473. ((org-region-active-p)
  13474. (call-interactively 'org-toggle-item))
  13475. ((org-in-item-p)
  13476. (call-interactively 'org-cycle-list-bullet))
  13477. (t
  13478. (call-interactively 'org-toggle-item))))
  13479. (defun org-toggle-item ()
  13480. "Convert headings or normal lines to items, items to normal lines.
  13481. If there is no active region, only the current line is considered.
  13482. If the first line in the region is a headline, convert all headlines to items.
  13483. If the first line in the region is an item, convert all items to normal lines.
  13484. If the first line is normal text, add an item bullet to each line."
  13485. (interactive)
  13486. (let (l2 l beg end)
  13487. (if (org-region-active-p)
  13488. (setq beg (region-beginning) end (region-end))
  13489. (setq beg (point-at-bol)
  13490. end (min (1+ (point-at-eol)) (point-max))))
  13491. (save-excursion
  13492. (goto-char end)
  13493. (setq l2 (org-current-line))
  13494. (goto-char beg)
  13495. (beginning-of-line 1)
  13496. (setq l (1- (org-current-line)))
  13497. (if (org-at-item-p)
  13498. ;; We already have items, de-itemize
  13499. (while (< (setq l (1+ l)) l2)
  13500. (when (org-at-item-p)
  13501. (goto-char (match-beginning 2))
  13502. (delete-region (match-beginning 2) (match-end 2))
  13503. (and (looking-at "[ \t]+") (replace-match "")))
  13504. (beginning-of-line 2))
  13505. (if (org-on-heading-p)
  13506. ;; Headings, convert to items
  13507. (while (< (setq l (1+ l)) l2)
  13508. (if (looking-at org-outline-regexp)
  13509. (replace-match "- " t t))
  13510. (beginning-of-line 2))
  13511. ;; normal lines, turn them into items
  13512. (while (< (setq l (1+ l)) l2)
  13513. (unless (org-at-item-p)
  13514. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13515. (replace-match "\\1- \\2")))
  13516. (beginning-of-line 2)))))))
  13517. (defun org-toggle-heading (&optional nstars)
  13518. "Convert headings to normal text, or items or text to headings.
  13519. If there is no active region, only the current line is considered.
  13520. If the first line is a heading, remove the stars from all headlines
  13521. in the region.
  13522. If the first line is a plain list item, turn all plain list items into
  13523. headings.
  13524. If the first line is a normal line, turn each and every line in the region
  13525. into a heading.
  13526. When converting a line into a heading, the number of stars is chosen
  13527. such that the lines become children of the current entry. However, when
  13528. a prefix argument is given, its value determines the number of stars to add."
  13529. (interactive "P")
  13530. (let (l2 l itemp beg end)
  13531. (if (org-region-active-p)
  13532. (setq beg (region-beginning) end (region-end))
  13533. (setq beg (point-at-bol)
  13534. end (min (1+ (point-at-eol)) (point-max))))
  13535. (save-excursion
  13536. (goto-char end)
  13537. (setq l2 (org-current-line))
  13538. (goto-char beg)
  13539. (beginning-of-line 1)
  13540. (setq l (1- (org-current-line)))
  13541. (if (org-on-heading-p)
  13542. ;; We already have headlines, de-star them
  13543. (while (< (setq l (1+ l)) l2)
  13544. (when (org-on-heading-p t)
  13545. (and (looking-at outline-regexp) (replace-match "")))
  13546. (beginning-of-line 2))
  13547. (setq itemp (org-at-item-p))
  13548. (let* ((stars
  13549. (if nstars
  13550. (make-string (prefix-numeric-value current-prefix-arg)
  13551. ?*)
  13552. (save-excursion
  13553. (re-search-backward org-complex-heading-regexp nil t)
  13554. (or (match-string 1) ""))))
  13555. (add-stars (cond (nstars "")
  13556. ((equal stars "") "*")
  13557. (org-odd-levels-only "**")
  13558. (t "*")))
  13559. (rpl (concat stars add-stars " ")))
  13560. (while (< (setq l (1+ l)) l2)
  13561. (if itemp
  13562. (and (org-at-item-p) (replace-match rpl t t))
  13563. (unless (org-on-heading-p)
  13564. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13565. (replace-match (concat rpl (match-string 2))))))
  13566. (beginning-of-line 2)))))))
  13567. (defun org-meta-return (&optional arg)
  13568. "Insert a new heading or wrap a region in a table.
  13569. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13570. See the individual commands for more information."
  13571. (interactive "P")
  13572. (cond
  13573. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13574. ((org-at-table-p)
  13575. (call-interactively 'org-table-wrap-region))
  13576. (t (call-interactively 'org-insert-heading))))
  13577. ;;; Menu entries
  13578. ;; Define the Org-mode menus
  13579. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13580. '("Tbl"
  13581. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13582. ["Next Field" org-cycle (org-at-table-p)]
  13583. ["Previous Field" org-shifttab (org-at-table-p)]
  13584. ["Next Row" org-return (org-at-table-p)]
  13585. "--"
  13586. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13587. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13588. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13589. "--"
  13590. ("Column"
  13591. ["Move Column Left" org-metaleft (org-at-table-p)]
  13592. ["Move Column Right" org-metaright (org-at-table-p)]
  13593. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13594. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13595. ("Row"
  13596. ["Move Row Up" org-metaup (org-at-table-p)]
  13597. ["Move Row Down" org-metadown (org-at-table-p)]
  13598. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13599. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13600. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13601. "--"
  13602. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13603. ("Rectangle"
  13604. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13605. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13606. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13607. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13608. "--"
  13609. ("Calculate"
  13610. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13611. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13612. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13613. "--"
  13614. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13615. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13616. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13617. "--"
  13618. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13619. "--"
  13620. ["Sum Column/Rectangle" org-table-sum
  13621. (or (org-at-table-p) (org-region-active-p))]
  13622. ["Which Column?" org-table-current-column (org-at-table-p)])
  13623. ["Debug Formulas"
  13624. org-table-toggle-formula-debugger
  13625. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13626. ["Show Col/Row Numbers"
  13627. org-table-toggle-coordinate-overlays
  13628. :style toggle
  13629. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13630. "--"
  13631. ["Create" org-table-create (and (not (org-at-table-p))
  13632. org-enable-table-editor)]
  13633. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13634. ["Import from File" org-table-import (not (org-at-table-p))]
  13635. ["Export to File" org-table-export (org-at-table-p)]
  13636. "--"
  13637. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13638. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13639. '("Org"
  13640. ("Show/Hide"
  13641. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13642. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13643. ["Sparse Tree..." org-sparse-tree t]
  13644. ["Reveal Context" org-reveal t]
  13645. ["Show All" show-all t]
  13646. "--"
  13647. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13648. "--"
  13649. ["New Heading" org-insert-heading t]
  13650. ("Navigate Headings"
  13651. ["Up" outline-up-heading t]
  13652. ["Next" outline-next-visible-heading t]
  13653. ["Previous" outline-previous-visible-heading t]
  13654. ["Next Same Level" outline-forward-same-level t]
  13655. ["Previous Same Level" outline-backward-same-level t]
  13656. "--"
  13657. ["Jump" org-goto t])
  13658. ("Edit Structure"
  13659. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13660. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13661. "--"
  13662. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13663. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13664. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13665. "--"
  13666. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13667. "--"
  13668. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13669. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13670. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13671. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13672. "--"
  13673. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13674. "--"
  13675. ["Convert to odd levels" org-convert-to-odd-levels t]
  13676. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13677. ("Editing"
  13678. ["Emphasis..." org-emphasize t]
  13679. ["Edit Source Example" org-edit-special t]
  13680. "--"
  13681. ["Footnote new/jump" org-footnote-action t]
  13682. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13683. ("Archive"
  13684. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13685. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13686. ; :active t :keys "C-u C-c C-x C-a"]
  13687. ["Sparse trees open ARCHIVE trees"
  13688. (setq org-sparse-tree-open-archived-trees
  13689. (not org-sparse-tree-open-archived-trees))
  13690. :style toggle :selected org-sparse-tree-open-archived-trees]
  13691. ["Cycling opens ARCHIVE trees"
  13692. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13693. :style toggle :selected org-cycle-open-archived-trees]
  13694. "--"
  13695. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13696. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13697. ; ["Check and Move Children" (org-archive-subtree '(4))
  13698. ; :active t :keys "C-u C-c C-x C-s"]
  13699. )
  13700. "--"
  13701. ("Hyperlinks"
  13702. ["Store Link (Global)" org-store-link t]
  13703. ["Insert Link" org-insert-link t]
  13704. ["Follow Link" org-open-at-point t]
  13705. "--"
  13706. ["Next link" org-next-link t]
  13707. ["Previous link" org-previous-link t]
  13708. "--"
  13709. ["Descriptive Links"
  13710. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13711. :style radio
  13712. :selected (member '(org-link) buffer-invisibility-spec)]
  13713. ["Literal Links"
  13714. (progn
  13715. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13716. :style radio
  13717. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13718. "--"
  13719. ("TODO Lists"
  13720. ["TODO/DONE/-" org-todo t]
  13721. ("Select keyword"
  13722. ["Next keyword" org-shiftright (org-on-heading-p)]
  13723. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13724. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13725. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13726. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13727. ["Show TODO Tree" org-show-todo-tree t]
  13728. ["Global TODO list" org-todo-list t]
  13729. "--"
  13730. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13731. :selected org-enforce-todo-dependencies :style toggle :active t]
  13732. "Settings for tree at point"
  13733. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13734. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13735. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13736. ["Do Children parallel" org-toggle-ordered-property :style radio
  13737. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13738. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13739. "--"
  13740. ["Set Priority" org-priority t]
  13741. ["Priority Up" org-shiftup t]
  13742. ["Priority Down" org-shiftdown t]
  13743. "--"
  13744. ["Get news from all feeds" org-feed-update-all t]
  13745. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13746. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13747. ("TAGS and Properties"
  13748. ["Set Tags" org-set-tags-command t]
  13749. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13750. "--"
  13751. ["Set property" org-set-property t]
  13752. ["Column view of properties" org-columns t]
  13753. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13754. ("Dates and Scheduling"
  13755. ["Timestamp" org-time-stamp t]
  13756. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13757. ("Change Date"
  13758. ["1 Day Later" org-shiftright t]
  13759. ["1 Day Earlier" org-shiftleft t]
  13760. ["1 ... Later" org-shiftup t]
  13761. ["1 ... Earlier" org-shiftdown t])
  13762. ["Compute Time Range" org-evaluate-time-range t]
  13763. ["Schedule Item" org-schedule t]
  13764. ["Deadline" org-deadline t]
  13765. "--"
  13766. ["Custom time format" org-toggle-time-stamp-overlays
  13767. :style radio :selected org-display-custom-times]
  13768. "--"
  13769. ["Goto Calendar" org-goto-calendar t]
  13770. ["Date from Calendar" org-date-from-calendar t]
  13771. "--"
  13772. ["Start/Restart Timer" org-timer-start t]
  13773. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13774. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13775. ["Insert Timer String" org-timer t]
  13776. ["Insert Timer Item" org-timer-item t])
  13777. ("Logging work"
  13778. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  13779. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  13780. ["Clock out" org-clock-out t]
  13781. ["Clock cancel" org-clock-cancel t]
  13782. "--"
  13783. ["Mark as default task" org-clock-mark-default-task t]
  13784. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  13785. ["Goto running clock" org-clock-goto t]
  13786. "--"
  13787. ["Display times" org-clock-display t]
  13788. ["Create clock table" org-clock-report t]
  13789. "--"
  13790. ["Record DONE time"
  13791. (progn (setq org-log-done (not org-log-done))
  13792. (message "Switching to %s will %s record a timestamp"
  13793. (car org-done-keywords)
  13794. (if org-log-done "automatically" "not")))
  13795. :style toggle :selected org-log-done])
  13796. "--"
  13797. ["Agenda Command..." org-agenda t]
  13798. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13799. ("File List for Agenda")
  13800. ("Special views current file"
  13801. ["TODO Tree" org-show-todo-tree t]
  13802. ["Check Deadlines" org-check-deadlines t]
  13803. ["Timeline" org-timeline t]
  13804. ["Tags/Property tree" org-match-sparse-tree t])
  13805. "--"
  13806. ["Export/Publish..." org-export t]
  13807. ("LaTeX"
  13808. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13809. :selected org-cdlatex-mode]
  13810. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13811. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13812. ["Modify math symbol" org-cdlatex-math-modify
  13813. (org-inside-LaTeX-fragment-p)]
  13814. ["Insert citation" org-reftex-citation t]
  13815. "--"
  13816. ["Export LaTeX fragments as images"
  13817. (if (featurep 'org-exp)
  13818. (setq org-export-with-LaTeX-fragments
  13819. (not org-export-with-LaTeX-fragments))
  13820. (require 'org-exp))
  13821. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13822. org-export-with-LaTeX-fragments)])
  13823. "--"
  13824. ("Documentation"
  13825. ["Show Version" org-version t]
  13826. ["Info Documentation" org-info t])
  13827. ("Customize"
  13828. ["Browse Org Group" org-customize t]
  13829. "--"
  13830. ["Expand This Menu" org-create-customize-menu
  13831. (fboundp 'customize-menu-create)])
  13832. "--"
  13833. ("Refresh/Reload"
  13834. ["Refresh setup current buffer" org-mode-restart t]
  13835. ["Reload Org (after update)" org-reload t]
  13836. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13837. ))
  13838. (defun org-info (&optional node)
  13839. "Read documentation for Org-mode in the info system.
  13840. With optional NODE, go directly to that node."
  13841. (interactive)
  13842. (info (format "(org)%s" (or node ""))))
  13843. (defun org-install-agenda-files-menu ()
  13844. (let ((bl (buffer-list)))
  13845. (save-excursion
  13846. (while bl
  13847. (set-buffer (pop bl))
  13848. (if (org-mode-p) (setq bl nil)))
  13849. (when (org-mode-p)
  13850. (easy-menu-change
  13851. '("Org") "File List for Agenda"
  13852. (append
  13853. (list
  13854. ["Edit File List" (org-edit-agenda-file-list) t]
  13855. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13856. ["Remove Current File from List" org-remove-file t]
  13857. ["Cycle through agenda files" org-cycle-agenda-files t]
  13858. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13859. "--")
  13860. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13861. ;;;; Documentation
  13862. ;;;###autoload
  13863. (defun org-require-autoloaded-modules ()
  13864. (interactive)
  13865. (mapc 'require
  13866. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13867. org-docbook org-exp org-html org-icalendar
  13868. org-id org-latex
  13869. org-publish org-remember org-table
  13870. org-timer org-xoxo)))
  13871. ;;;###autoload
  13872. (defun org-reload (&optional uncompiled)
  13873. "Reload all org lisp files.
  13874. With prefix arg UNCOMPILED, load the uncompiled versions."
  13875. (interactive "P")
  13876. (require 'find-func)
  13877. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13878. (dir-org (file-name-directory (org-find-library-name "org")))
  13879. (dir-org-contrib (ignore-errors
  13880. (file-name-directory
  13881. (org-find-library-name "org-contribdir"))))
  13882. (files
  13883. (append (directory-files dir-org t file-re)
  13884. (and dir-org-contrib
  13885. (directory-files dir-org-contrib t file-re))))
  13886. (remove-re (concat (if (featurep 'xemacs)
  13887. "org-colview" "org-colview-xemacs")
  13888. "\\'")))
  13889. (setq files (mapcar 'file-name-sans-extension files))
  13890. (setq files (mapcar
  13891. (lambda (x) (if (string-match remove-re x) nil x))
  13892. files))
  13893. (setq files (delq nil files))
  13894. (mapc
  13895. (lambda (f)
  13896. (when (featurep (intern (file-name-nondirectory f)))
  13897. (if (and (not uncompiled)
  13898. (file-exists-p (concat f ".elc")))
  13899. (load (concat f ".elc") nil nil t)
  13900. (load (concat f ".el") nil nil t))))
  13901. files))
  13902. (org-version))
  13903. ;;;###autoload
  13904. (defun org-customize ()
  13905. "Call the customize function with org as argument."
  13906. (interactive)
  13907. (org-load-modules-maybe)
  13908. (org-require-autoloaded-modules)
  13909. (customize-browse 'org))
  13910. (defun org-create-customize-menu ()
  13911. "Create a full customization menu for Org-mode, insert it into the menu."
  13912. (interactive)
  13913. (org-load-modules-maybe)
  13914. (org-require-autoloaded-modules)
  13915. (if (fboundp 'customize-menu-create)
  13916. (progn
  13917. (easy-menu-change
  13918. '("Org") "Customize"
  13919. `(["Browse Org group" org-customize t]
  13920. "--"
  13921. ,(customize-menu-create 'org)
  13922. ["Set" Custom-set t]
  13923. ["Save" Custom-save t]
  13924. ["Reset to Current" Custom-reset-current t]
  13925. ["Reset to Saved" Custom-reset-saved t]
  13926. ["Reset to Standard Settings" Custom-reset-standard t]))
  13927. (message "\"Org\"-menu now contains full customization menu"))
  13928. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13929. ;;;; Miscellaneous stuff
  13930. ;;; Generally useful functions
  13931. (defun org-find-text-property-in-string (prop s)
  13932. "Return the first non-nil value of property PROP in string S."
  13933. (or (get-text-property 0 prop s)
  13934. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13935. prop s)))
  13936. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13937. "Display the given MESSAGE as a warning."
  13938. (if (fboundp 'display-warning)
  13939. (display-warning 'org message
  13940. (if (featurep 'xemacs)
  13941. 'warning
  13942. :warning))
  13943. (let ((buf (get-buffer-create "*Org warnings*")))
  13944. (with-current-buffer buf
  13945. (goto-char (point-max))
  13946. (insert "Warning (Org): " message)
  13947. (unless (bolp)
  13948. (newline)))
  13949. (display-buffer buf)
  13950. (sit-for 0))))
  13951. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13952. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13953. (if (and marker (marker-buffer marker)
  13954. (buffer-live-p (marker-buffer marker)))
  13955. (progn
  13956. (switch-to-buffer (marker-buffer marker))
  13957. (if (or (> marker (point-max)) (< marker (point-min)))
  13958. (widen))
  13959. (goto-char marker)
  13960. (org-show-context 'org-goto))
  13961. (if bookmark
  13962. (bookmark-jump bookmark)
  13963. (error "Cannot find location"))))
  13964. (defun org-quote-csv-field (s)
  13965. "Quote field for inclusion in CSV material."
  13966. (if (string-match "[\",]" s)
  13967. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13968. s))
  13969. (defun org-plist-delete (plist property)
  13970. "Delete PROPERTY from PLIST.
  13971. This is in contrast to merely setting it to 0."
  13972. (let (p)
  13973. (while plist
  13974. (if (not (eq property (car plist)))
  13975. (setq p (plist-put p (car plist) (nth 1 plist))))
  13976. (setq plist (cddr plist)))
  13977. p))
  13978. (defun org-force-self-insert (N)
  13979. "Needed to enforce self-insert under remapping."
  13980. (interactive "p")
  13981. (self-insert-command N))
  13982. (defun org-string-width (s)
  13983. "Compute width of string, ignoring invisible characters.
  13984. This ignores character with invisibility property `org-link', and also
  13985. characters with property `org-cwidth', because these will become invisible
  13986. upon the next fontification round."
  13987. (let (b l)
  13988. (when (or (eq t buffer-invisibility-spec)
  13989. (assq 'org-link buffer-invisibility-spec))
  13990. (while (setq b (text-property-any 0 (length s)
  13991. 'invisible 'org-link s))
  13992. (setq s (concat (substring s 0 b)
  13993. (substring s (or (next-single-property-change
  13994. b 'invisible s) (length s)))))))
  13995. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13996. (setq s (concat (substring s 0 b)
  13997. (substring s (or (next-single-property-change
  13998. b 'org-cwidth s) (length s))))))
  13999. (setq l (string-width s) b -1)
  14000. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14001. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14002. l))
  14003. (defun org-get-indentation (&optional line)
  14004. "Get the indentation of the current line, interpreting tabs.
  14005. When LINE is given, assume it represents a line and compute its indentation."
  14006. (if line
  14007. (if (string-match "^ *" (org-remove-tabs line))
  14008. (match-end 0))
  14009. (save-excursion
  14010. (beginning-of-line 1)
  14011. (skip-chars-forward " \t")
  14012. (current-column))))
  14013. (defun org-remove-tabs (s &optional width)
  14014. "Replace tabulators in S with spaces.
  14015. Assumes that s is a single line, starting in column 0."
  14016. (setq width (or width tab-width))
  14017. (while (string-match "\t" s)
  14018. (setq s (replace-match
  14019. (make-string
  14020. (- (* width (/ (+ (match-beginning 0) width) width))
  14021. (match-beginning 0)) ?\ )
  14022. t t s)))
  14023. s)
  14024. (defun org-fix-indentation (line ind)
  14025. "Fix indentation in LINE.
  14026. IND is a cons cell with target and minimum indentation.
  14027. If the current indentation in LINE is smaller than the minimum,
  14028. leave it alone. If it is larger than ind, set it to the target."
  14029. (let* ((l (org-remove-tabs line))
  14030. (i (org-get-indentation l))
  14031. (i1 (car ind)) (i2 (cdr ind)))
  14032. (if (>= i i2) (setq l (substring line i2)))
  14033. (if (> i1 0)
  14034. (concat (make-string i1 ?\ ) l)
  14035. l)))
  14036. (defun org-remove-indentation (code &optional n)
  14037. "Remove the maximum common indentation from the lines in CODE.
  14038. N may optionally be the number of spaces to remove."
  14039. (with-temp-buffer
  14040. (insert code)
  14041. (org-do-remove-indentation n)
  14042. (buffer-string)))
  14043. (defun org-do-remove-indentation (&optional n)
  14044. "Remove the maximum common indentation from the buffer."
  14045. (untabify (point-min) (point-max))
  14046. (let ((min 10000) re)
  14047. (if n
  14048. (setq min n)
  14049. (goto-char (point-min))
  14050. (while (re-search-forward "^ *[^ \n]" nil t)
  14051. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14052. (unless (or (= min 0) (= min 10000))
  14053. (setq re (format "^ \\{%d\\}" min))
  14054. (goto-char (point-min))
  14055. (while (re-search-forward re nil t)
  14056. (replace-match "")
  14057. (end-of-line 1))
  14058. min)))
  14059. (defun org-base-buffer (buffer)
  14060. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14061. (if (not buffer)
  14062. buffer
  14063. (or (buffer-base-buffer buffer)
  14064. buffer)))
  14065. (defun org-trim (s)
  14066. "Remove whitespace at beginning and end of string."
  14067. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14068. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14069. s)
  14070. (defun org-wrap (string &optional width lines)
  14071. "Wrap string to either a number of lines, or a width in characters.
  14072. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14073. that costs. If there is a word longer than WIDTH, the text is actually
  14074. wrapped to the length of that word.
  14075. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14076. many lines, whatever width that takes.
  14077. The return value is a list of lines, without newlines at the end."
  14078. (let* ((words (org-split-string string "[ \t\n]+"))
  14079. (maxword (apply 'max (mapcar 'org-string-width words)))
  14080. w ll)
  14081. (cond (width
  14082. (org-do-wrap words (max maxword width)))
  14083. (lines
  14084. (setq w maxword)
  14085. (setq ll (org-do-wrap words maxword))
  14086. (if (<= (length ll) lines)
  14087. ll
  14088. (setq ll words)
  14089. (while (> (length ll) lines)
  14090. (setq w (1+ w))
  14091. (setq ll (org-do-wrap words w)))
  14092. ll))
  14093. (t (error "Cannot wrap this")))))
  14094. (defun org-do-wrap (words width)
  14095. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14096. (let (lines line)
  14097. (while words
  14098. (setq line (pop words))
  14099. (while (and words (< (+ (length line) (length (car words))) width))
  14100. (setq line (concat line " " (pop words))))
  14101. (setq lines (push line lines)))
  14102. (nreverse lines)))
  14103. (defun org-split-string (string &optional separators)
  14104. "Splits STRING into substrings at SEPARATORS.
  14105. No empty strings are returned if there are matches at the beginning
  14106. and end of string."
  14107. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14108. (start 0)
  14109. notfirst
  14110. (list nil))
  14111. (while (and (string-match rexp string
  14112. (if (and notfirst
  14113. (= start (match-beginning 0))
  14114. (< start (length string)))
  14115. (1+ start) start))
  14116. (< (match-beginning 0) (length string)))
  14117. (setq notfirst t)
  14118. (or (eq (match-beginning 0) 0)
  14119. (and (eq (match-beginning 0) (match-end 0))
  14120. (eq (match-beginning 0) start))
  14121. (setq list
  14122. (cons (substring string start (match-beginning 0))
  14123. list)))
  14124. (setq start (match-end 0)))
  14125. (or (eq start (length string))
  14126. (setq list
  14127. (cons (substring string start)
  14128. list)))
  14129. (nreverse list)))
  14130. (defun org-quote-vert (s)
  14131. "Replace \"|\" with \"\\vert\"."
  14132. (while (string-match "|" s)
  14133. (setq s (replace-match "\\vert" t t s)))
  14134. s)
  14135. (defun org-uuidgen-p (s)
  14136. "Is S an ID created by UUIDGEN?"
  14137. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14138. (defun org-context ()
  14139. "Return a list of contexts of the current cursor position.
  14140. If several contexts apply, all are returned.
  14141. Each context entry is a list with a symbol naming the context, and
  14142. two positions indicating start and end of the context. Possible
  14143. contexts are:
  14144. :headline anywhere in a headline
  14145. :headline-stars on the leading stars in a headline
  14146. :todo-keyword on a TODO keyword (including DONE) in a headline
  14147. :tags on the TAGS in a headline
  14148. :priority on the priority cookie in a headline
  14149. :item on the first line of a plain list item
  14150. :item-bullet on the bullet/number of a plain list item
  14151. :checkbox on the checkbox in a plain list item
  14152. :table in an org-mode table
  14153. :table-special on a special filed in a table
  14154. :table-table in a table.el table
  14155. :link on a hyperlink
  14156. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14157. :target on a <<target>>
  14158. :radio-target on a <<<radio-target>>>
  14159. :latex-fragment on a LaTeX fragment
  14160. :latex-preview on a LaTeX fragment with overlayed preview image
  14161. This function expects the position to be visible because it uses font-lock
  14162. faces as a help to recognize the following contexts: :table-special, :link,
  14163. and :keyword."
  14164. (let* ((f (get-text-property (point) 'face))
  14165. (faces (if (listp f) f (list f)))
  14166. (p (point)) clist o)
  14167. ;; First the large context
  14168. (cond
  14169. ((org-on-heading-p t)
  14170. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14171. (when (progn
  14172. (beginning-of-line 1)
  14173. (looking-at org-todo-line-tags-regexp))
  14174. (push (org-point-in-group p 1 :headline-stars) clist)
  14175. (push (org-point-in-group p 2 :todo-keyword) clist)
  14176. (push (org-point-in-group p 4 :tags) clist))
  14177. (goto-char p)
  14178. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14179. (if (looking-at "\\[#[A-Z0-9]\\]")
  14180. (push (org-point-in-group p 0 :priority) clist)))
  14181. ((org-at-item-p)
  14182. (push (org-point-in-group p 2 :item-bullet) clist)
  14183. (push (list :item (point-at-bol)
  14184. (save-excursion (org-end-of-item) (point)))
  14185. clist)
  14186. (and (org-at-item-checkbox-p)
  14187. (push (org-point-in-group p 0 :checkbox) clist)))
  14188. ((org-at-table-p)
  14189. (push (list :table (org-table-begin) (org-table-end)) clist)
  14190. (if (memq 'org-formula faces)
  14191. (push (list :table-special
  14192. (previous-single-property-change p 'face)
  14193. (next-single-property-change p 'face)) clist)))
  14194. ((org-at-table-p 'any)
  14195. (push (list :table-table) clist)))
  14196. (goto-char p)
  14197. ;; Now the small context
  14198. (cond
  14199. ((org-at-timestamp-p)
  14200. (push (org-point-in-group p 0 :timestamp) clist))
  14201. ((memq 'org-link faces)
  14202. (push (list :link
  14203. (previous-single-property-change p 'face)
  14204. (next-single-property-change p 'face)) clist))
  14205. ((memq 'org-special-keyword faces)
  14206. (push (list :keyword
  14207. (previous-single-property-change p 'face)
  14208. (next-single-property-change p 'face)) clist))
  14209. ((org-on-target-p)
  14210. (push (org-point-in-group p 0 :target) clist)
  14211. (goto-char (1- (match-beginning 0)))
  14212. (if (looking-at org-radio-target-regexp)
  14213. (push (org-point-in-group p 0 :radio-target) clist))
  14214. (goto-char p))
  14215. ((setq o (car (delq nil
  14216. (mapcar
  14217. (lambda (x)
  14218. (if (memq x org-latex-fragment-image-overlays) x))
  14219. (org-overlays-at (point))))))
  14220. (push (list :latex-fragment
  14221. (org-overlay-start o) (org-overlay-end o)) clist)
  14222. (push (list :latex-preview
  14223. (org-overlay-start o) (org-overlay-end o)) clist))
  14224. ((org-inside-LaTeX-fragment-p)
  14225. ;; FIXME: positions wrong.
  14226. (push (list :latex-fragment (point) (point)) clist)))
  14227. (setq clist (nreverse (delq nil clist)))
  14228. clist))
  14229. ;; FIXME: Compare with at-regexp-p Do we need both?
  14230. (defun org-in-regexp (re &optional nlines visually)
  14231. "Check if point is inside a match of regexp.
  14232. Normally only the current line is checked, but you can include NLINES extra
  14233. lines both before and after point into the search.
  14234. If VISUALLY is set, require that the cursor is not after the match but
  14235. really on, so that the block visually is on the match."
  14236. (catch 'exit
  14237. (let ((pos (point))
  14238. (eol (point-at-eol (+ 1 (or nlines 0))))
  14239. (inc (if visually 1 0)))
  14240. (save-excursion
  14241. (beginning-of-line (- 1 (or nlines 0)))
  14242. (while (re-search-forward re eol t)
  14243. (if (and (<= (match-beginning 0) pos)
  14244. (>= (+ inc (match-end 0)) pos))
  14245. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14246. (defun org-at-regexp-p (regexp)
  14247. "Is point inside a match of REGEXP in the current line?"
  14248. (catch 'exit
  14249. (save-excursion
  14250. (let ((pos (point)) (end (point-at-eol)))
  14251. (beginning-of-line 1)
  14252. (while (re-search-forward regexp end t)
  14253. (if (and (<= (match-beginning 0) pos)
  14254. (>= (match-end 0) pos))
  14255. (throw 'exit t)))
  14256. nil))))
  14257. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14258. "Call `multi-occur' with buffers for all agenda files."
  14259. (interactive "sOrg-files matching: \np")
  14260. (let* ((files (org-agenda-files))
  14261. (tnames (mapcar 'file-truename files))
  14262. (extra org-agenda-text-search-extra-files)
  14263. f)
  14264. (when (eq (car extra) 'agenda-archives)
  14265. (setq extra (cdr extra))
  14266. (setq files (org-add-archive-files files)))
  14267. (while (setq f (pop extra))
  14268. (unless (member (file-truename f) tnames)
  14269. (add-to-list 'files f 'append)
  14270. (add-to-list 'tnames (file-truename f) 'append)))
  14271. (multi-occur
  14272. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14273. regexp)))
  14274. (if (boundp 'occur-mode-find-occurrence-hook)
  14275. ;; Emacs 23
  14276. (add-hook 'occur-mode-find-occurrence-hook
  14277. (lambda ()
  14278. (when (org-mode-p)
  14279. (org-reveal))))
  14280. ;; Emacs 22
  14281. (defadvice occur-mode-goto-occurrence
  14282. (after org-occur-reveal activate)
  14283. (and (org-mode-p) (org-reveal)))
  14284. (defadvice occur-mode-goto-occurrence-other-window
  14285. (after org-occur-reveal activate)
  14286. (and (org-mode-p) (org-reveal)))
  14287. (defadvice occur-mode-display-occurrence
  14288. (after org-occur-reveal activate)
  14289. (when (org-mode-p)
  14290. (let ((pos (occur-mode-find-occurrence)))
  14291. (with-current-buffer (marker-buffer pos)
  14292. (save-excursion
  14293. (goto-char pos)
  14294. (org-reveal)))))))
  14295. (defun org-occur-link-in-agenda-files ()
  14296. "Create a link and search for it in the agendas.
  14297. The link is not stored in `org-stored-links', it is just created
  14298. for the search purpose."
  14299. (interactive)
  14300. (let ((link (condition-case nil
  14301. (org-store-link nil)
  14302. (error "Unable to create a link from here"))))
  14303. (org-occur-in-agenda-files (regexp-quote link))))
  14304. (defun org-uniquify (list)
  14305. "Remove duplicate elements from LIST."
  14306. (let (res)
  14307. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14308. res))
  14309. (defun org-delete-all (elts list)
  14310. "Remove all elements in ELTS from LIST."
  14311. (while elts
  14312. (setq list (delete (pop elts) list)))
  14313. list)
  14314. (defun org-back-over-empty-lines ()
  14315. "Move backwards over whitespace, to the beginning of the first empty line.
  14316. Returns the number of empty lines passed."
  14317. (let ((pos (point)))
  14318. (skip-chars-backward " \t\n\r")
  14319. (beginning-of-line 2)
  14320. (goto-char (min (point) pos))
  14321. (count-lines (point) pos)))
  14322. (defun org-skip-whitespace ()
  14323. (skip-chars-forward " \t\n\r"))
  14324. (defun org-point-in-group (point group &optional context)
  14325. "Check if POINT is in match-group GROUP.
  14326. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14327. match. If the match group does ot exist or point is not inside it,
  14328. return nil."
  14329. (and (match-beginning group)
  14330. (>= point (match-beginning group))
  14331. (<= point (match-end group))
  14332. (if context
  14333. (list context (match-beginning group) (match-end group))
  14334. t)))
  14335. (defun org-switch-to-buffer-other-window (&rest args)
  14336. "Switch to buffer in a second window on the current frame.
  14337. In particular, do not allow pop-up frames."
  14338. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14339. special-display-function)
  14340. (apply 'switch-to-buffer-other-window args)))
  14341. (defun org-combine-plists (&rest plists)
  14342. "Create a single property list from all plists in PLISTS.
  14343. The process starts by copying the first list, and then setting properties
  14344. from the other lists. Settings in the last list are the most significant
  14345. ones and overrule settings in the other lists."
  14346. (let ((rtn (copy-sequence (pop plists)))
  14347. p v ls)
  14348. (while plists
  14349. (setq ls (pop plists))
  14350. (while ls
  14351. (setq p (pop ls) v (pop ls))
  14352. (setq rtn (plist-put rtn p v))))
  14353. rtn))
  14354. (defun org-move-line-down (arg)
  14355. "Move the current line down. With prefix argument, move it past ARG lines."
  14356. (interactive "p")
  14357. (let ((col (current-column))
  14358. beg end pos)
  14359. (beginning-of-line 1) (setq beg (point))
  14360. (beginning-of-line 2) (setq end (point))
  14361. (beginning-of-line (+ 1 arg))
  14362. (setq pos (move-marker (make-marker) (point)))
  14363. (insert (delete-and-extract-region beg end))
  14364. (goto-char pos)
  14365. (org-move-to-column col)))
  14366. (defun org-move-line-up (arg)
  14367. "Move the current line up. With prefix argument, move it past ARG lines."
  14368. (interactive "p")
  14369. (let ((col (current-column))
  14370. beg end pos)
  14371. (beginning-of-line 1) (setq beg (point))
  14372. (beginning-of-line 2) (setq end (point))
  14373. (beginning-of-line (- arg))
  14374. (setq pos (move-marker (make-marker) (point)))
  14375. (insert (delete-and-extract-region beg end))
  14376. (goto-char pos)
  14377. (org-move-to-column col)))
  14378. (defun org-replace-escapes (string table)
  14379. "Replace %-escapes in STRING with values in TABLE.
  14380. TABLE is an association list with keys like \"%a\" and string values.
  14381. The sequences in STRING may contain normal field width and padding information,
  14382. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14383. so values can contain further %-escapes if they are define later in TABLE."
  14384. (let ((case-fold-search nil)
  14385. e re rpl)
  14386. (while (setq e (pop table))
  14387. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14388. (while (string-match re string)
  14389. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14390. (cdr e)))
  14391. (setq string (replace-match rpl t t string))))
  14392. string))
  14393. (defun org-sublist (list start end)
  14394. "Return a section of LIST, from START to END.
  14395. Counting starts at 1."
  14396. (let (rtn (c start))
  14397. (setq list (nthcdr (1- start) list))
  14398. (while (and list (<= c end))
  14399. (push (pop list) rtn)
  14400. (setq c (1+ c)))
  14401. (nreverse rtn)))
  14402. (defun org-find-base-buffer-visiting (file)
  14403. "Like `find-buffer-visiting' but always return the base buffer and
  14404. not an indirect buffer."
  14405. (let ((buf (or (get-file-buffer file)
  14406. (find-buffer-visiting file))))
  14407. (if buf
  14408. (or (buffer-base-buffer buf) buf)
  14409. nil)))
  14410. (defun org-image-file-name-regexp (&optional extensions)
  14411. "Return regexp matching the file names of images.
  14412. If EXTENSIONS is given, only match these."
  14413. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14414. (image-file-name-regexp)
  14415. (let ((image-file-name-extensions
  14416. (or extensions
  14417. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14418. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14419. (concat "\\."
  14420. (regexp-opt (nconc (mapcar 'upcase
  14421. image-file-name-extensions)
  14422. image-file-name-extensions)
  14423. t)
  14424. "\\'"))))
  14425. (defun org-file-image-p (file &optional extensions)
  14426. "Return non-nil if FILE is an image."
  14427. (save-match-data
  14428. (string-match (org-image-file-name-regexp extensions) file)))
  14429. (defun org-get-cursor-date ()
  14430. "Return the date at cursor in as a time.
  14431. This works in the calendar and in the agenda, anywhere else it just
  14432. returns the current time."
  14433. (let (date day defd)
  14434. (cond
  14435. ((eq major-mode 'calendar-mode)
  14436. (setq date (calendar-cursor-to-date)
  14437. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14438. ((eq major-mode 'org-agenda-mode)
  14439. (setq day (get-text-property (point) 'day))
  14440. (if day
  14441. (setq date (calendar-gregorian-from-absolute day)
  14442. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14443. (nth 2 date))))))
  14444. (or defd (current-time))))
  14445. (defvar org-agenda-action-marker (make-marker)
  14446. "Marker pointing to the entry for the next agenda action.")
  14447. (defun org-mark-entry-for-agenda-action ()
  14448. "Mark the current entry as target of an agenda action.
  14449. Agenda actions are actions executed from the agenda with the key `k',
  14450. which make use of the date at the cursor."
  14451. (interactive)
  14452. (move-marker org-agenda-action-marker
  14453. (save-excursion (org-back-to-heading t) (point))
  14454. (current-buffer))
  14455. (message
  14456. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14457. ;;; Paragraph filling stuff.
  14458. ;; We want this to be just right, so use the full arsenal.
  14459. (defun org-indent-line-function ()
  14460. "Indent line like previous, but further if previous was headline or item."
  14461. (interactive)
  14462. (let* ((pos (point))
  14463. (itemp (org-at-item-p))
  14464. (case-fold-search t)
  14465. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14466. column bpos bcol tpos tcol bullet btype bullet-type)
  14467. ;; Find the previous relevant line
  14468. (beginning-of-line 1)
  14469. (cond
  14470. ((looking-at "#") (setq column 0))
  14471. ((looking-at "\\*+ ") (setq column 0))
  14472. ((and (looking-at "[ \t]*:END:")
  14473. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14474. (save-excursion
  14475. (goto-char (1- (match-beginning 1)))
  14476. (setq column (current-column))))
  14477. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14478. (save-excursion
  14479. (re-search-backward
  14480. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14481. (setq column (org-get-indentation (match-string 0))))
  14482. (t
  14483. (beginning-of-line 0)
  14484. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14485. (not (looking-at "[ \t]*:END:"))
  14486. (not (looking-at org-drawer-regexp)))
  14487. (beginning-of-line 0))
  14488. (cond
  14489. ((looking-at "\\*+[ \t]+")
  14490. (if (not org-adapt-indentation)
  14491. (setq column 0)
  14492. (goto-char (match-end 0))
  14493. (setq column (current-column))))
  14494. ((looking-at org-drawer-regexp)
  14495. (goto-char (1- (match-beginning 1)))
  14496. (setq column (current-column)))
  14497. ((looking-at "\\([ \t]*\\):END:")
  14498. (goto-char (match-end 1))
  14499. (setq column (current-column)))
  14500. ((org-in-item-p)
  14501. (org-beginning-of-item)
  14502. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14503. (setq bpos (match-beginning 1) tpos (match-end 0)
  14504. bcol (progn (goto-char bpos) (current-column))
  14505. tcol (progn (goto-char tpos) (current-column))
  14506. bullet (match-string 1)
  14507. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14508. (if (> tcol (+ bcol org-description-max-indent))
  14509. (setq tcol (+ bcol 5)))
  14510. (if (not itemp)
  14511. (setq column tcol)
  14512. (goto-char pos)
  14513. (beginning-of-line 1)
  14514. (if (looking-at "\\S-")
  14515. (progn
  14516. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14517. (setq bullet (match-string 1)
  14518. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14519. (setq column (if (equal btype bullet-type) bcol tcol)))
  14520. (setq column (org-get-indentation)))))
  14521. (t (setq column (org-get-indentation))))))
  14522. (goto-char pos)
  14523. (if (<= (current-column) (current-indentation))
  14524. (org-indent-line-to column)
  14525. (save-excursion (org-indent-line-to column)))
  14526. (setq column (current-column))
  14527. (beginning-of-line 1)
  14528. (if (looking-at
  14529. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14530. (replace-match (concat "\\1" (format org-property-format
  14531. (match-string 2) (match-string 3)))
  14532. t nil))
  14533. (org-move-to-column column)))
  14534. (defun org-set-autofill-regexps ()
  14535. (interactive)
  14536. ;; In the paragraph separator we include headlines, because filling
  14537. ;; text in a line directly attached to a headline would otherwise
  14538. ;; fill the headline as well.
  14539. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14540. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14541. ;; The paragraph starter includes hand-formatted lists.
  14542. (org-set-local
  14543. 'paragraph-start
  14544. (concat
  14545. "\f" "\\|"
  14546. "[ ]*$" "\\|"
  14547. "\\*+ " "\\|"
  14548. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14549. "[ \t]*[:|]" "\\|"
  14550. "\\$\\$" "\\|"
  14551. "\\\\\\(begin\\|end\\|[][]\\)"))
  14552. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14553. ;; But only if the user has not turned off tables or fixed-width regions
  14554. (org-set-local
  14555. 'auto-fill-inhibit-regexp
  14556. (concat "\\*+ \\|#\\+"
  14557. "\\|[ \t]*" org-keyword-time-regexp
  14558. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14559. (concat
  14560. "\\|[ \t]*["
  14561. (if org-enable-table-editor "|" "")
  14562. (if org-enable-fixed-width-editor ":" "")
  14563. "]"))))
  14564. ;; We use our own fill-paragraph function, to make sure that tables
  14565. ;; and fixed-width regions are not wrapped. That function will pass
  14566. ;; through to `fill-paragraph' when appropriate.
  14567. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14568. ; Adaptive filling: To get full control, first make sure that
  14569. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14570. (org-set-local 'adaptive-fill-regexp "\000")
  14571. (org-set-local 'adaptive-fill-function
  14572. 'org-adaptive-fill-function)
  14573. (org-set-local
  14574. 'align-mode-rules-list
  14575. '((org-in-buffer-settings
  14576. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14577. (modes . '(org-mode))))))
  14578. (defun org-fill-paragraph (&optional justify)
  14579. "Re-align a table, pass through to fill-paragraph if no table."
  14580. (let ((table-p (org-at-table-p))
  14581. (table.el-p (org-at-table.el-p)))
  14582. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14583. (save-excursion (goto-char (point-at-bol))
  14584. (looking-at outline-regexp)))
  14585. t) ; skip headlines
  14586. (table.el-p t) ; skip table.el tables
  14587. (table-p (org-table-align) t) ; align org-mode tables
  14588. (t nil)))) ; call paragraph-fill
  14589. ;; For reference, this is the default value of adaptive-fill-regexp
  14590. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14591. (defun org-adaptive-fill-function ()
  14592. "Return a fill prefix for org-mode files.
  14593. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14594. work correctly."
  14595. (cond ((looking-at "#[ \t]+")
  14596. (match-string 0))
  14597. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14598. (save-excursion
  14599. (if (> (match-end 1) (+ (match-beginning 1)
  14600. org-description-max-indent))
  14601. (goto-char (+ (match-beginning 1) 5))
  14602. (goto-char (match-end 0)))
  14603. (make-string (current-column) ?\ )))
  14604. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14605. (save-excursion
  14606. (goto-char (match-end 0))
  14607. (make-string (current-column) ?\ )))
  14608. (t nil)))
  14609. ;;; Other stuff.
  14610. (defun org-toggle-fixed-width-section (arg)
  14611. "Toggle the fixed-width export.
  14612. If there is no active region, the QUOTE keyword at the current headline is
  14613. inserted or removed. When present, it causes the text between this headline
  14614. and the next to be exported as fixed-width text, and unmodified.
  14615. If there is an active region, this command adds or removes a colon as the
  14616. first character of this line. If the first character of a line is a colon,
  14617. this line is also exported in fixed-width font."
  14618. (interactive "P")
  14619. (let* ((cc 0)
  14620. (regionp (org-region-active-p))
  14621. (beg (if regionp (region-beginning) (point)))
  14622. (end (if regionp (region-end)))
  14623. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14624. (case-fold-search nil)
  14625. (re "[ \t]*\\(: \\)")
  14626. off)
  14627. (if regionp
  14628. (save-excursion
  14629. (goto-char beg)
  14630. (setq cc (current-column))
  14631. (beginning-of-line 1)
  14632. (setq off (looking-at re))
  14633. (while (> nlines 0)
  14634. (setq nlines (1- nlines))
  14635. (beginning-of-line 1)
  14636. (cond
  14637. (arg
  14638. (org-move-to-column cc t)
  14639. (insert ": \n")
  14640. (forward-line -1))
  14641. ((and off (looking-at re))
  14642. (replace-match "" t t nil 1))
  14643. ((not off) (org-move-to-column cc t) (insert ": ")))
  14644. (forward-line 1)))
  14645. (save-excursion
  14646. (org-back-to-heading)
  14647. (if (looking-at (concat outline-regexp
  14648. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14649. (replace-match "" t t nil 1)
  14650. (if (looking-at outline-regexp)
  14651. (progn
  14652. (goto-char (match-end 0))
  14653. (insert org-quote-string " "))))))))
  14654. (defun org-reftex-citation ()
  14655. "Use reftex-citation to insert a citation into the buffer.
  14656. This looks for a line like
  14657. #+BIBLIOGRAPHY: foo plain option:-d
  14658. and derives from it that foo.bib is the bbliography file relevant
  14659. for this document. It then installs the necessary environment for RefTeX
  14660. to work in this buffer and calls `reftex-citation' to insert a citation
  14661. into the buffer.
  14662. Export of such citations to both LaTeX and HTML is handled by the contributed
  14663. package org-exp-bibtex by Taru Karttunen."
  14664. (interactive)
  14665. (let ((reftex-docstruct-symbol 'rds)
  14666. (reftex-cite-format "\\cite{%l}")
  14667. rds bib)
  14668. (save-excursion
  14669. (save-restriction
  14670. (widen)
  14671. (let ((case-fold-search t)
  14672. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14673. (if (not (save-excursion
  14674. (or (re-search-forward re nil t)
  14675. (re-search-backward re nil t))))
  14676. (error "No bibliography defined in file")
  14677. (setq bib (concat (match-string 1) ".bib")
  14678. rds (list (list 'bib bib)))))))
  14679. (call-interactively 'reftex-citation)))
  14680. ;;;; Functions extending outline functionality
  14681. (defun org-beginning-of-line (&optional arg)
  14682. "Go to the beginning of the current line. If that is invisible, continue
  14683. to a visible line beginning. This makes the function of C-a more intuitive.
  14684. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14685. first attempt, and only move to after the tags when the cursor is already
  14686. beyond the end of the headline."
  14687. (interactive "P")
  14688. (let ((pos (point))
  14689. (special (if (consp org-special-ctrl-a/e)
  14690. (car org-special-ctrl-a/e)
  14691. org-special-ctrl-a/e))
  14692. refpos)
  14693. (if (org-bound-and-true-p line-move-visual)
  14694. (beginning-of-visual-line 1)
  14695. (beginning-of-line 1))
  14696. (if (and arg (fboundp 'move-beginning-of-line))
  14697. (call-interactively 'move-beginning-of-line)
  14698. (if (bobp)
  14699. nil
  14700. (backward-char 1)
  14701. (if (org-invisible-p)
  14702. (while (and (not (bobp)) (org-invisible-p))
  14703. (backward-char 1)
  14704. (beginning-of-line 1))
  14705. (forward-char 1))))
  14706. (when special
  14707. (cond
  14708. ((and (looking-at org-complex-heading-regexp)
  14709. (= (char-after (match-end 1)) ?\ ))
  14710. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14711. (point-at-eol)))
  14712. (goto-char
  14713. (if (eq special t)
  14714. (cond ((> pos refpos) refpos)
  14715. ((= pos (point)) refpos)
  14716. (t (point)))
  14717. (cond ((> pos (point)) (point))
  14718. ((not (eq last-command this-command)) (point))
  14719. (t refpos)))))
  14720. ((org-at-item-p)
  14721. (goto-char
  14722. (if (eq special t)
  14723. (cond ((> pos (match-end 4)) (match-end 4))
  14724. ((= pos (point)) (match-end 4))
  14725. (t (point)))
  14726. (cond ((> pos (point)) (point))
  14727. ((not (eq last-command this-command)) (point))
  14728. (t (match-end 4))))))))
  14729. (org-no-warnings
  14730. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14731. (defun org-end-of-line (&optional arg)
  14732. "Go to the end of the line.
  14733. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14734. first attempt, and only move to after the tags when the cursor is already
  14735. beyond the end of the headline."
  14736. (interactive "P")
  14737. (let ((special (if (consp org-special-ctrl-a/e)
  14738. (cdr org-special-ctrl-a/e)
  14739. org-special-ctrl-a/e)))
  14740. (if (or (not special)
  14741. (not (org-on-heading-p))
  14742. arg)
  14743. (call-interactively
  14744. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  14745. ((fboundp 'move-end-of-line) 'move-end-of-line)
  14746. (t 'end-of-line)))
  14747. (let ((pos (point)))
  14748. (beginning-of-line 1)
  14749. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14750. (if (eq special t)
  14751. (if (or (< pos (match-beginning 1))
  14752. (= pos (match-end 0)))
  14753. (goto-char (match-beginning 1))
  14754. (goto-char (match-end 0)))
  14755. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14756. (goto-char (match-end 0))
  14757. (goto-char (match-beginning 1))))
  14758. (call-interactively (if (fboundp 'move-end-of-line)
  14759. 'move-end-of-line
  14760. 'end-of-line)))))
  14761. (org-no-warnings
  14762. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14763. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14764. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14765. (defun org-backward-sentence (&optional arg)
  14766. "Go to beginning of sentence, or beginning of table field.
  14767. This will call `backward-sentence' or `org-table-beginning-of-field',
  14768. depending on context."
  14769. (interactive "P")
  14770. (cond
  14771. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14772. (t (call-interactively 'backward-sentence))))
  14773. (defun org-forward-sentence (&optional arg)
  14774. "Go to end of sentence, or end of table field.
  14775. This will call `forward-sentence' or `org-table-end-of-field',
  14776. depending on context."
  14777. (interactive "P")
  14778. (cond
  14779. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14780. (t (call-interactively 'forward-sentence))))
  14781. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14782. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14783. (defun org-kill-line (&optional arg)
  14784. "Kill line, to tags or end of line."
  14785. (interactive "P")
  14786. (cond
  14787. ((or (not org-special-ctrl-k)
  14788. (bolp)
  14789. (not (org-on-heading-p)))
  14790. (call-interactively 'kill-line))
  14791. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14792. (kill-region (point) (match-beginning 1))
  14793. (org-set-tags nil t))
  14794. (t (kill-region (point) (point-at-eol)))))
  14795. (define-key org-mode-map "\C-k" 'org-kill-line)
  14796. (defun org-yank (&optional arg)
  14797. "Yank. If the kill is a subtree, treat it specially.
  14798. This command will look at the current kill and check if is a single
  14799. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14800. cursor is at the beginning of a line or after the stars of a currently
  14801. empty headline, then the yank is handled specially. How exactly depends
  14802. on the value of the following variables, both set by default.
  14803. org-yank-folded-subtrees
  14804. When set, the subtree(s) will be folded after insertion, but only
  14805. if doing so would now swallow text after the yanked text.
  14806. org-yank-adjusted-subtrees
  14807. When set, the subtree will be promoted or demoted in order to
  14808. fit into the local outline tree structure, which means that the level
  14809. will be adjusted so that it becomes the smaller one of the two
  14810. *visible* surrounding headings.
  14811. Any prefix to this command will cause `yank' to be called directly with
  14812. no special treatment. In particular, a simple `C-u' prefix will just
  14813. plainly yank the text as it is.
  14814. \[1] The test checks if the first non-white line is a heading
  14815. and if there are no other headings with fewer stars."
  14816. (interactive "P")
  14817. (org-yank-generic 'yank arg))
  14818. (defun org-yank-generic (command arg)
  14819. "Perform some yank-like command.
  14820. This function implements the behavior described in the `org-yank'
  14821. documentation. However, it has been generalized to work for any
  14822. interactive command with similar behavior."
  14823. ;; pretend to be command COMMAND
  14824. (setq this-command command)
  14825. (if arg
  14826. (call-interactively command)
  14827. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14828. (and (org-kill-is-subtree-p)
  14829. (or (bolp)
  14830. (and (looking-at "[ \t]*$")
  14831. (string-match
  14832. "\\`\\*+\\'"
  14833. (buffer-substring (point-at-bol) (point)))))))
  14834. swallowp)
  14835. (cond
  14836. ((and subtreep org-yank-folded-subtrees)
  14837. (let ((beg (point))
  14838. end)
  14839. (if (and subtreep org-yank-adjusted-subtrees)
  14840. (org-paste-subtree nil nil 'for-yank)
  14841. (call-interactively command))
  14842. (setq end (point))
  14843. (goto-char beg)
  14844. (when (and (bolp) subtreep
  14845. (not (setq swallowp
  14846. (org-yank-folding-would-swallow-text beg end))))
  14847. (or (looking-at outline-regexp)
  14848. (re-search-forward (concat "^" outline-regexp) end t))
  14849. (while (and (< (point) end) (looking-at outline-regexp))
  14850. (hide-subtree)
  14851. (org-cycle-show-empty-lines 'folded)
  14852. (condition-case nil
  14853. (outline-forward-same-level 1)
  14854. (error (goto-char end)))))
  14855. (when swallowp
  14856. (message
  14857. "Inserted text not folded because that would swallow text"))
  14858. (goto-char end)
  14859. (skip-chars-forward " \t\n\r")
  14860. (beginning-of-line 1)
  14861. (push-mark beg 'nomsg)))
  14862. ((and subtreep org-yank-adjusted-subtrees)
  14863. (let ((beg (point-at-bol)))
  14864. (org-paste-subtree nil nil 'for-yank)
  14865. (push-mark beg 'nomsg)))
  14866. (t
  14867. (call-interactively command))))))
  14868. (defun org-yank-folding-would-swallow-text (beg end)
  14869. "Would hide-subtree at BEG swallow any text after END?"
  14870. (let (level)
  14871. (save-excursion
  14872. (goto-char beg)
  14873. (when (or (looking-at outline-regexp)
  14874. (re-search-forward (concat "^" outline-regexp) end t))
  14875. (setq level (org-outline-level)))
  14876. (goto-char end)
  14877. (skip-chars-forward " \t\r\n\v\f")
  14878. (if (or (eobp)
  14879. (and (bolp) (looking-at org-outline-regexp)
  14880. (<= (org-outline-level) level)))
  14881. nil ; Nothing would be swallowed
  14882. t)))) ; something would swallow
  14883. (define-key org-mode-map "\C-y" 'org-yank)
  14884. (defun org-invisible-p ()
  14885. "Check if point is at a character currently not visible."
  14886. ;; Early versions of noutline don't have `outline-invisible-p'.
  14887. (if (fboundp 'outline-invisible-p)
  14888. (outline-invisible-p)
  14889. (get-char-property (point) 'invisible)))
  14890. (defun org-invisible-p2 ()
  14891. "Check if point is at a character currently not visible."
  14892. (save-excursion
  14893. (if (and (eolp) (not (bobp))) (backward-char 1))
  14894. ;; Early versions of noutline don't have `outline-invisible-p'.
  14895. (if (fboundp 'outline-invisible-p)
  14896. (outline-invisible-p)
  14897. (get-char-property (point) 'invisible))))
  14898. (defun org-back-to-heading (&optional invisible-ok)
  14899. "Call `outline-back-to-heading', but provide a better error message."
  14900. (condition-case nil
  14901. (outline-back-to-heading invisible-ok)
  14902. (error (error "Before first headline at position %d in buffer %s"
  14903. (point) (current-buffer)))))
  14904. (defun org-before-first-heading-p ()
  14905. "Before first heading?"
  14906. (save-excursion
  14907. (null (re-search-backward "^\\*+ " nil t))))
  14908. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14909. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14910. (defun org-at-heading-or-item-p ()
  14911. (or (org-on-heading-p) (org-at-item-p)))
  14912. (defun org-on-target-p ()
  14913. (or (org-in-regexp org-radio-target-regexp)
  14914. (org-in-regexp org-target-regexp)))
  14915. (defun org-up-heading-all (arg)
  14916. "Move to the heading line of which the present line is a subheading.
  14917. This function considers both visible and invisible heading lines.
  14918. With argument, move up ARG levels."
  14919. (if (fboundp 'outline-up-heading-all)
  14920. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14921. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14922. (defun org-up-heading-safe ()
  14923. "Move to the heading line of which the present line is a subheading.
  14924. This version will not throw an error. It will return the level of the
  14925. headline found, or nil if no higher level is found.
  14926. Also, this function will be a lot faster than `outline-up-heading',
  14927. because it relies on stars being the outline starters. This can really
  14928. make a significant difference in outlines with very many siblings."
  14929. (let (start-level re)
  14930. (org-back-to-heading t)
  14931. (setq start-level (funcall outline-level))
  14932. (if (equal start-level 1)
  14933. nil
  14934. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14935. (if (re-search-backward re nil t)
  14936. (funcall outline-level)))))
  14937. (defun org-first-sibling-p ()
  14938. "Is this heading the first child of its parents?"
  14939. (interactive)
  14940. (let ((re (concat "^" outline-regexp))
  14941. level l)
  14942. (unless (org-at-heading-p t)
  14943. (error "Not at a heading"))
  14944. (setq level (funcall outline-level))
  14945. (save-excursion
  14946. (if (not (re-search-backward re nil t))
  14947. t
  14948. (setq l (funcall outline-level))
  14949. (< l level)))))
  14950. (defun org-goto-sibling (&optional previous)
  14951. "Goto the next sibling, even if it is invisible.
  14952. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14953. when a sibling was found. When none is found, return nil and don't
  14954. move point."
  14955. (let ((fun (if previous 're-search-backward 're-search-forward))
  14956. (pos (point))
  14957. (re (concat "^" outline-regexp))
  14958. level l)
  14959. (when (condition-case nil (org-back-to-heading t) (error nil))
  14960. (setq level (funcall outline-level))
  14961. (catch 'exit
  14962. (or previous (forward-char 1))
  14963. (while (funcall fun re nil t)
  14964. (setq l (funcall outline-level))
  14965. (when (< l level) (goto-char pos) (throw 'exit nil))
  14966. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14967. (goto-char pos)
  14968. nil))))
  14969. (defun org-show-siblings ()
  14970. "Show all siblings of the current headline."
  14971. (save-excursion
  14972. (while (org-goto-sibling) (org-flag-heading nil)))
  14973. (save-excursion
  14974. (while (org-goto-sibling 'previous)
  14975. (org-flag-heading nil))))
  14976. (defun org-show-hidden-entry ()
  14977. "Show an entry where even the heading is hidden."
  14978. (save-excursion
  14979. (org-show-entry)))
  14980. (defun org-flag-heading (flag &optional entry)
  14981. "Flag the current heading. FLAG non-nil means make invisible.
  14982. When ENTRY is non-nil, show the entire entry."
  14983. (save-excursion
  14984. (org-back-to-heading t)
  14985. ;; Check if we should show the entire entry
  14986. (if entry
  14987. (progn
  14988. (org-show-entry)
  14989. (save-excursion
  14990. (and (outline-next-heading)
  14991. (org-flag-heading nil))))
  14992. (outline-flag-region (max (point-min) (1- (point)))
  14993. (save-excursion (outline-end-of-heading) (point))
  14994. flag))))
  14995. (defun org-forward-same-level (arg)
  14996. "Move forward to the ARG'th subheading at same level as this one.
  14997. Stop at the first and last subheadings of a superior heading.
  14998. This is like outline-forward-same-level, but invisible headings are ok."
  14999. (interactive "p")
  15000. (org-back-to-heading t)
  15001. (while (> arg 0)
  15002. (let ((point-to-move-to (save-excursion
  15003. (org-get-next-sibling))))
  15004. (if point-to-move-to
  15005. (progn
  15006. (goto-char point-to-move-to)
  15007. (setq arg (1- arg)))
  15008. (progn
  15009. (setq arg 0)
  15010. (error "No following same-level heading"))))))
  15011. (defun org-get-next-sibling ()
  15012. "Move to next heading of the same level, and return point.
  15013. If there is no such heading, return nil.
  15014. This is like outline-next-sibling, but invisible headings are ok."
  15015. (let ((level (funcall outline-level)))
  15016. (outline-next-heading)
  15017. (while (and (not (eobp)) (> (funcall outline-level) level))
  15018. (outline-next-heading))
  15019. (if (or (eobp) (< (funcall outline-level) level))
  15020. nil
  15021. (point))))
  15022. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15023. ;; This contains an exact copy of the original function, but it uses
  15024. ;; `org-back-to-heading', to make it work also in invisible
  15025. ;; trees. And is uses an invisible-OK argument.
  15026. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15027. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15028. ;; with many children and grandchildren etc, this can be much faster
  15029. ;; than the outline version.
  15030. (org-back-to-heading invisible-OK)
  15031. (let ((first t)
  15032. (level (funcall outline-level)))
  15033. (if (and (org-mode-p) (< level 1000))
  15034. ;; A true heading (not a plain list item), in Org-mode
  15035. ;; This means we can easily find the end by looking
  15036. ;; only for the right number of stars. Using a regexp to do
  15037. ;; this is so much faster than using a Lisp loop.
  15038. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15039. (forward-char 1)
  15040. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15041. ;; something else, do it the slow way
  15042. (while (and (not (eobp))
  15043. (or first (> (funcall outline-level) level)))
  15044. (setq first nil)
  15045. (outline-next-heading)))
  15046. (unless to-heading
  15047. (if (memq (preceding-char) '(?\n ?\^M))
  15048. (progn
  15049. ;; Go to end of line before heading
  15050. (forward-char -1)
  15051. (if (memq (preceding-char) '(?\n ?\^M))
  15052. ;; leave blank line before heading
  15053. (forward-char -1))))))
  15054. (point))
  15055. (defun org-show-subtree ()
  15056. "Show everything after this heading at deeper levels."
  15057. (outline-flag-region
  15058. (point)
  15059. (save-excursion
  15060. (outline-end-of-subtree) (outline-next-heading) (point))
  15061. nil))
  15062. (defun org-show-entry ()
  15063. "Show the body directly following this heading.
  15064. Show the heading too, if it is currently invisible."
  15065. (interactive)
  15066. (save-excursion
  15067. (condition-case nil
  15068. (progn
  15069. (org-back-to-heading t)
  15070. (outline-flag-region
  15071. (max (point-min) (1- (point)))
  15072. (save-excursion
  15073. (if (re-search-forward
  15074. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15075. (match-beginning 1)
  15076. (point-max)))
  15077. nil)
  15078. (org-cycle-hide-drawers 'children))
  15079. (error nil))))
  15080. (defun org-make-options-regexp (kwds &optional extra)
  15081. "Make a regular expression for keyword lines."
  15082. (concat
  15083. "^"
  15084. "#?[ \t]*\\+\\("
  15085. (mapconcat 'regexp-quote kwds "\\|")
  15086. (if extra (concat "\\|" extra))
  15087. "\\):[ \t]*"
  15088. "\\(.+\\)"))
  15089. ;; Make isearch reveal the necessary context
  15090. (defun org-isearch-end ()
  15091. "Reveal context after isearch exits."
  15092. (when isearch-success ; only if search was successful
  15093. (if (featurep 'xemacs)
  15094. ;; Under XEmacs, the hook is run in the correct place,
  15095. ;; we directly show the context.
  15096. (org-show-context 'isearch)
  15097. ;; In Emacs the hook runs *before* restoring the overlays.
  15098. ;; So we have to use a one-time post-command-hook to do this.
  15099. ;; (Emacs 22 has a special variable, see function `org-mode')
  15100. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15101. isearch-mode-end-hook-quit)
  15102. ;; Only when the isearch was not quitted.
  15103. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15104. 'append 'local)))))
  15105. (defun org-isearch-post-command ()
  15106. "Remove self from hook, and show context."
  15107. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15108. (org-show-context 'isearch))
  15109. ;;;; Integration with and fixes for other packages
  15110. ;;; Imenu support
  15111. (defvar org-imenu-markers nil
  15112. "All markers currently used by Imenu.")
  15113. (make-variable-buffer-local 'org-imenu-markers)
  15114. (defun org-imenu-new-marker (&optional pos)
  15115. "Return a new marker for use by Imenu, and remember the marker."
  15116. (let ((m (make-marker)))
  15117. (move-marker m (or pos (point)))
  15118. (push m org-imenu-markers)
  15119. m))
  15120. (defun org-imenu-get-tree ()
  15121. "Produce the index for Imenu."
  15122. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15123. (setq org-imenu-markers nil)
  15124. (let* ((n org-imenu-depth)
  15125. (re (concat "^" outline-regexp))
  15126. (subs (make-vector (1+ n) nil))
  15127. (last-level 0)
  15128. m level head)
  15129. (save-excursion
  15130. (save-restriction
  15131. (widen)
  15132. (goto-char (point-max))
  15133. (while (re-search-backward re nil t)
  15134. (setq level (org-reduced-level (funcall outline-level)))
  15135. (when (<= level n)
  15136. (looking-at org-complex-heading-regexp)
  15137. (setq head (org-link-display-format
  15138. (org-match-string-no-properties 4))
  15139. m (org-imenu-new-marker))
  15140. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15141. (if (>= level last-level)
  15142. (push (cons head m) (aref subs level))
  15143. (push (cons head (aref subs (1+ level))) (aref subs level))
  15144. (loop for i from (1+ level) to n do (aset subs i nil)))
  15145. (setq last-level level)))))
  15146. (aref subs 1)))
  15147. (eval-after-load "imenu"
  15148. '(progn
  15149. (add-hook 'imenu-after-jump-hook
  15150. (lambda ()
  15151. (if (eq major-mode 'org-mode)
  15152. (org-show-context 'org-goto))))))
  15153. (defun org-link-display-format (link)
  15154. "Replace a link with either the description, or the link target
  15155. if no description is present"
  15156. (save-match-data
  15157. (if (string-match org-bracket-link-analytic-regexp link)
  15158. (replace-match (or (match-string 5 link)
  15159. (concat (match-string 1 link)
  15160. (match-string 3 link)))
  15161. nil nil link)
  15162. link)))
  15163. ;; Speedbar support
  15164. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15165. "Overlay marking the agenda restriction line in speedbar.")
  15166. (org-overlay-put org-speedbar-restriction-lock-overlay
  15167. 'face 'org-agenda-restriction-lock)
  15168. (org-overlay-put org-speedbar-restriction-lock-overlay
  15169. 'help-echo "Agendas are currently limited to this item.")
  15170. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15171. (defun org-speedbar-set-agenda-restriction ()
  15172. "Restrict future agenda commands to the location at point in speedbar.
  15173. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15174. (interactive)
  15175. (require 'org-agenda)
  15176. (let (p m tp np dir txt)
  15177. (cond
  15178. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15179. 'org-imenu t))
  15180. (setq m (get-text-property p 'org-imenu-marker))
  15181. (save-excursion
  15182. (save-restriction
  15183. (set-buffer (marker-buffer m))
  15184. (goto-char m)
  15185. (org-agenda-set-restriction-lock 'subtree))))
  15186. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15187. 'speedbar-function 'speedbar-find-file))
  15188. (setq tp (previous-single-property-change
  15189. (1+ p) 'speedbar-function)
  15190. np (next-single-property-change
  15191. tp 'speedbar-function)
  15192. dir (speedbar-line-directory)
  15193. txt (buffer-substring-no-properties (or tp (point-min))
  15194. (or np (point-max))))
  15195. (save-excursion
  15196. (save-restriction
  15197. (set-buffer (find-file-noselect
  15198. (let ((default-directory dir))
  15199. (expand-file-name txt))))
  15200. (unless (org-mode-p)
  15201. (error "Cannot restrict to non-Org-mode file"))
  15202. (org-agenda-set-restriction-lock 'file))))
  15203. (t (error "Don't know how to restrict Org-mode's agenda")))
  15204. (org-move-overlay org-speedbar-restriction-lock-overlay
  15205. (point-at-bol) (point-at-eol))
  15206. (setq current-prefix-arg nil)
  15207. (org-agenda-maybe-redo)))
  15208. (eval-after-load "speedbar"
  15209. '(progn
  15210. (speedbar-add-supported-extension ".org")
  15211. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15212. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15213. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15214. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15215. (add-hook 'speedbar-visiting-tag-hook
  15216. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15217. ;;; Fixes and Hacks for problems with other packages
  15218. ;; Make flyspell not check words in links, to not mess up our keymap
  15219. (defun org-mode-flyspell-verify ()
  15220. "Don't let flyspell put overlays at active buttons."
  15221. (and (not (get-text-property (point) 'keymap))
  15222. (not (get-text-property (point) 'org-no-flyspell))))
  15223. (defun org-remove-flyspell-overlays-in (beg end)
  15224. "Remove flyspell overlays in region."
  15225. (and (org-bound-and-true-p flyspell-mode)
  15226. (fboundp 'flyspell-delete-region-overlays)
  15227. (flyspell-delete-region-overlays beg end))
  15228. (add-text-properties beg end '(org-no-flyspell t)))
  15229. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15230. (eval-after-load "bookmark"
  15231. '(if (boundp 'bookmark-after-jump-hook)
  15232. ;; We can use the hook
  15233. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15234. ;; Hook not available, use advice
  15235. (defadvice bookmark-jump (after org-make-visible activate)
  15236. "Make the position visible."
  15237. (org-bookmark-jump-unhide))))
  15238. ;; Make sure saveplace show the location if it was hidden
  15239. (eval-after-load "saveplace"
  15240. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15241. "Make the position visible."
  15242. (org-bookmark-jump-unhide)))
  15243. (defun org-bookmark-jump-unhide ()
  15244. "Unhide the current position, to show the bookmark location."
  15245. (and (org-mode-p)
  15246. (or (org-invisible-p)
  15247. (save-excursion (goto-char (max (point-min) (1- (point))))
  15248. (org-invisible-p)))
  15249. (org-show-context 'bookmark-jump)))
  15250. ;; Make session.el ignore our circular variable
  15251. (eval-after-load "session"
  15252. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15253. ;;;; Experimental code
  15254. (defun org-closed-in-range ()
  15255. "Sparse tree of items closed in a certain time range.
  15256. Still experimental, may disappear in the future."
  15257. (interactive)
  15258. ;; Get the time interval from the user.
  15259. (let* ((time1 (time-to-seconds
  15260. (org-read-date nil 'to-time nil "Starting date: ")))
  15261. (time2 (time-to-seconds
  15262. (org-read-date nil 'to-time nil "End date:")))
  15263. ;; callback function
  15264. (callback (lambda ()
  15265. (let ((time
  15266. (time-to-seconds
  15267. (apply 'encode-time
  15268. (org-parse-time-string
  15269. (match-string 1))))))
  15270. ;; check if time in interval
  15271. (and (>= time time1) (<= time time2))))))
  15272. ;; make tree, check each match with the callback
  15273. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15274. ;;;; Finish up
  15275. (provide 'org)
  15276. (run-hooks 'org-load-hook)
  15277. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15278. ;;; org.el ends here