org.el 628 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852
  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-indented nil
  247. "Non-nil means, turn on `org-indent-mode' on startup.
  248. This can also be configured on a per-file basis by adding one of
  249. the following lines anywhere in the buffer:
  250. #+STARTUP: indent
  251. #+STARTUP: noindent"
  252. :group 'org-structure
  253. :type '(choice
  254. (const :tag "Not" nil)
  255. (const :tag "Globally (slow on startup in large files)" t)))
  256. (defcustom org-startup-align-all-tables nil
  257. "Non-nil means, align all tables when visiting a file.
  258. This is useful when the column width in tables is forced with <N> cookies
  259. in table fields. Such tables will look correct only after the first re-align.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: align
  263. #+STARTUP: noalign"
  264. :group 'org-startup
  265. :type 'boolean)
  266. (defcustom org-insert-mode-line-in-empty-file nil
  267. "Non-nil means insert the first line setting Org-mode in empty files.
  268. When the function `org-mode' is called interactively in an empty file, this
  269. normally means that the file name does not automatically trigger Org-mode.
  270. To ensure that the file will always be in Org-mode in the future, a
  271. line enforcing Org-mode will be inserted into the buffer, if this option
  272. has been set."
  273. :group 'org-startup
  274. :type 'boolean)
  275. (defcustom org-replace-disputed-keys nil
  276. "Non-nil means use alternative key bindings for some keys.
  277. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  278. These keys are also used by other packages like shift-selection-mode'
  279. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  280. If you want to use Org-mode together with one of these other modes,
  281. or more generally if you would like to move some Org-mode commands to
  282. other keys, set this variable and configure the keys with the variable
  283. `org-disputed-keys'.
  284. This option is only relevant at load-time of Org-mode, and must be set
  285. *before* org.el is loaded. Changing it requires a restart of Emacs to
  286. become effective."
  287. :group 'org-startup
  288. :type 'boolean)
  289. (defcustom org-use-extra-keys nil
  290. "Non-nil means use extra key sequence definitions for certain
  291. commands. This happens automatically if you run XEmacs or if
  292. window-system is nil. This variable lets you do the same
  293. manually. You must set it before loading org.
  294. Example: on Carbon Emacs 22 running graphically, with an external
  295. keyboard on a Powerbook, the default way of setting M-left might
  296. not work for either Alt or ESC. Setting this variable will make
  297. it work for ESC."
  298. :group 'org-startup
  299. :type 'boolean)
  300. (if (fboundp 'defvaralias)
  301. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  302. (defcustom org-disputed-keys
  303. '(([(shift up)] . [(meta p)])
  304. ([(shift down)] . [(meta n)])
  305. ([(shift left)] . [(meta -)])
  306. ([(shift right)] . [(meta +)])
  307. ([(control shift right)] . [(meta shift +)])
  308. ([(control shift left)] . [(meta shift -)]))
  309. "Keys for which Org-mode and other modes compete.
  310. This is an alist, cars are the default keys, second element specifies
  311. the alternative to use when `org-replace-disputed-keys' is t.
  312. Keys can be specified in any syntax supported by `define-key'.
  313. The value of this option takes effect only at Org-mode's startup,
  314. therefore you'll have to restart Emacs to apply it after changing."
  315. :group 'org-startup
  316. :type 'alist)
  317. (defun org-key (key)
  318. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  319. Or return the original if not disputed."
  320. (if org-replace-disputed-keys
  321. (let* ((nkey (key-description key))
  322. (x (org-find-if (lambda (x)
  323. (equal (key-description (car x)) nkey))
  324. org-disputed-keys)))
  325. (if x (cdr x) key))
  326. key))
  327. (defun org-find-if (predicate seq)
  328. (catch 'exit
  329. (while seq
  330. (if (funcall predicate (car seq))
  331. (throw 'exit (car seq))
  332. (pop seq)))))
  333. (defun org-defkey (keymap key def)
  334. "Define a key, possibly translated, as returned by `org-key'."
  335. (define-key keymap (org-key key) def))
  336. (defcustom org-ellipsis nil
  337. "The ellipsis to use in the Org-mode outline.
  338. When nil, just use the standard three dots. When a string, use that instead,
  339. When a face, use the standard 3 dots, but with the specified face.
  340. The change affects only Org-mode (which will then use its own display table).
  341. Changing this requires executing `M-x org-mode' in a buffer to become
  342. effective."
  343. :group 'org-startup
  344. :type '(choice (const :tag "Default" nil)
  345. (face :tag "Face" :value org-warning)
  346. (string :tag "String" :value "...#")))
  347. (defvar org-display-table nil
  348. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  349. (defgroup org-keywords nil
  350. "Keywords in Org-mode."
  351. :tag "Org Keywords"
  352. :group 'org)
  353. (defcustom org-deadline-string "DEADLINE:"
  354. "String to mark deadline entries.
  355. A deadline is this string, followed by a time stamp. Should be a word,
  356. terminated by a colon. You can insert a schedule keyword and
  357. a timestamp with \\[org-deadline].
  358. Changes become only effective after restarting Emacs."
  359. :group 'org-keywords
  360. :type 'string)
  361. (defcustom org-scheduled-string "SCHEDULED:"
  362. "String to mark scheduled TODO entries.
  363. A schedule is this string, followed by a time stamp. Should be a word,
  364. terminated by a colon. You can insert a schedule keyword and
  365. a timestamp with \\[org-schedule].
  366. Changes become only effective after restarting Emacs."
  367. :group 'org-keywords
  368. :type 'string)
  369. (defcustom org-closed-string "CLOSED:"
  370. "String used as the prefix for timestamps logging closing a TODO entry."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defcustom org-clock-string "CLOCK:"
  374. "String used as prefix for timestamps clocking work hours on an item."
  375. :group 'org-keywords
  376. :type 'string)
  377. (defcustom org-comment-string "COMMENT"
  378. "Entries starting with this keyword will never be exported.
  379. An entry can be toggled between COMMENT and normal with
  380. \\[org-toggle-comment].
  381. Changes become only effective after restarting Emacs."
  382. :group 'org-keywords
  383. :type 'string)
  384. (defcustom org-quote-string "QUOTE"
  385. "Entries starting with this keyword will be exported in fixed-width font.
  386. Quoting applies only to the text in the entry following the headline, and does
  387. not extend beyond the next headline, even if that is lower level.
  388. An entry can be toggled between QUOTE and normal with
  389. \\[org-toggle-fixed-width-section]."
  390. :group 'org-keywords
  391. :type 'string)
  392. (defconst org-repeat-re
  393. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  394. "Regular expression for specifying repeated events.
  395. After a match, group 1 contains the repeat expression.")
  396. (defgroup org-structure nil
  397. "Options concerning the general structure of Org-mode files."
  398. :tag "Org Structure"
  399. :group 'org)
  400. (defgroup org-reveal-location nil
  401. "Options about how to make context of a location visible."
  402. :tag "Org Reveal Location"
  403. :group 'org-structure)
  404. (defconst org-context-choice
  405. '(choice
  406. (const :tag "Always" t)
  407. (const :tag "Never" nil)
  408. (repeat :greedy t :tag "Individual contexts"
  409. (cons
  410. (choice :tag "Context"
  411. (const agenda)
  412. (const org-goto)
  413. (const occur-tree)
  414. (const tags-tree)
  415. (const link-search)
  416. (const mark-goto)
  417. (const bookmark-jump)
  418. (const isearch)
  419. (const default))
  420. (boolean))))
  421. "Contexts for the reveal options.")
  422. (defcustom org-show-hierarchy-above '((default . t))
  423. "Non-nil means, show full hierarchy when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the hierarchy of headings
  426. above the exposed location is shown.
  427. Turning this off for example for sparse trees makes them very compact.
  428. Instead of t, this can also be an alist specifying this option for different
  429. contexts. Valid contexts are
  430. agenda when exposing an entry from the agenda
  431. org-goto when using the command `org-goto' on key C-c C-j
  432. occur-tree when using the command `org-occur' on key C-c /
  433. tags-tree when constructing a sparse tree based on tags matches
  434. link-search when exposing search matches associated with a link
  435. mark-goto when exposing the jump goal of a mark
  436. bookmark-jump when exposing a bookmark location
  437. isearch when exiting from an incremental search
  438. default default for all contexts not set explicitly"
  439. :group 'org-reveal-location
  440. :type org-context-choice)
  441. (defcustom org-show-following-heading '((default . nil))
  442. "Non-nil means, show following heading when revealing a location.
  443. Org-mode often shows locations in an org-mode file which might have
  444. been invisible before. When this is set, the heading following the
  445. match is shown.
  446. Turning this off for example for sparse trees makes them very compact,
  447. but makes it harder to edit the location of the match. In such a case,
  448. use the command \\[org-reveal] to show more context.
  449. Instead of t, this can also be an alist specifying this option for different
  450. contexts. See `org-show-hierarchy-above' for valid contexts."
  451. :group 'org-reveal-location
  452. :type org-context-choice)
  453. (defcustom org-show-siblings '((default . nil) (isearch t))
  454. "Non-nil means, show all sibling heading when revealing a location.
  455. Org-mode often shows locations in an org-mode file which might have
  456. been invisible before. When this is set, the sibling of the current entry
  457. heading are all made visible. If `org-show-hierarchy-above' is t,
  458. the same happens on each level of the hierarchy above the current entry.
  459. By default this is on for the isearch context, off for all other contexts.
  460. Turning this off for example for sparse trees makes them very compact,
  461. but makes it harder to edit the location of the match. In such a case,
  462. use the command \\[org-reveal] to show more context.
  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-show-entry-below '((default . nil))
  468. "Non-nil means, show the entry below a headline when revealing a location.
  469. Org-mode often shows locations in an org-mode file which might have
  470. been invisible before. When this is set, the text below the headline that is
  471. exposed is also shown.
  472. By default this is off for all contexts.
  473. Instead of t, this can also be an alist specifying this option for different
  474. contexts. See `org-show-hierarchy-above' for valid contexts."
  475. :group 'org-reveal-location
  476. :type org-context-choice)
  477. (defcustom org-indirect-buffer-display 'other-window
  478. "How should indirect tree buffers be displayed?
  479. This applies to indirect buffers created with the commands
  480. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  481. Valid values are:
  482. current-window Display in the current window
  483. other-window Just display in another window.
  484. dedicated-frame Create one new frame, and re-use it each time.
  485. new-frame Make a new frame each time. Note that in this case
  486. previously-made indirect buffers are kept, and you need to
  487. kill these buffers yourself."
  488. :group 'org-structure
  489. :group 'org-agenda-windows
  490. :type '(choice
  491. (const :tag "In current window" current-window)
  492. (const :tag "In current frame, other window" other-window)
  493. (const :tag "Each time a new frame" new-frame)
  494. (const :tag "One dedicated frame" dedicated-frame)))
  495. (defgroup org-cycle nil
  496. "Options concerning visibility cycling in Org-mode."
  497. :tag "Org Cycle"
  498. :group 'org-structure)
  499. (defcustom org-cycle-max-level nil
  500. "Maximum level which should still be subject to visibility cycling.
  501. Levels higher than this will, for cycling, be treated as text, not a headline.
  502. When `org-odd-levels-only' is set, a value of N in this variable actually
  503. means 2N-1 stars as the limiting headline.
  504. When nil, cycle all levels.
  505. Note that the limiting level of cycling is also influenced by
  506. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  507. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  508. than its value."
  509. :group 'org-cycle
  510. :type '(choice
  511. (const :tag "No limit" nil)
  512. (integer :tag "Maximum level")))
  513. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  514. "Names of drawers. Drawers are not opened by cycling on the headline above.
  515. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  516. this:
  517. :DRAWERNAME:
  518. .....
  519. :END:
  520. The drawer \"PROPERTIES\" is special for capturing properties through
  521. the property API.
  522. Drawers can be defined on the per-file basis with a line like:
  523. #+DRAWERS: HIDDEN STATE PROPERTIES"
  524. :group 'org-structure
  525. :group 'org-cycle
  526. :type '(repeat (string :tag "Drawer Name")))
  527. (defcustom org-hide-block-startup nil
  528. "Non-nil means, , entering Org-mode will fold all blocks.
  529. This can also be set in on a per-file basis with
  530. #+STARTUP: hideblocks
  531. #+STARTUP: showblocks"
  532. :group 'org-startup
  533. :group 'org-cycle
  534. :type 'boolean)
  535. (defcustom org-cycle-global-at-bob nil
  536. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  537. This makes it possible to do global cycling without having to use S-TAB or
  538. C-u TAB. For this special case to work, the first line of the buffer
  539. must not be a headline - it may be empty or some other text. When used in
  540. this way, `org-cycle-hook' is disables temporarily, to make sure the
  541. cursor stays at the beginning of the buffer.
  542. When this option is nil, don't do anything special at the beginning
  543. of the buffer."
  544. :group 'org-cycle
  545. :type 'boolean)
  546. (defcustom org-cycle-emulate-tab t
  547. "Where should `org-cycle' emulate TAB.
  548. nil Never
  549. white Only in completely white lines
  550. whitestart Only at the beginning of lines, before the first non-white char
  551. t Everywhere except in headlines
  552. exc-hl-bol Everywhere except at the start of a headline
  553. If TAB is used in a place where it does not emulate TAB, the current subtree
  554. visibility is cycled."
  555. :group 'org-cycle
  556. :type '(choice (const :tag "Never" nil)
  557. (const :tag "Only in completely white lines" white)
  558. (const :tag "Before first char in a line" whitestart)
  559. (const :tag "Everywhere except in headlines" t)
  560. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  561. ))
  562. (defcustom org-cycle-separator-lines 2
  563. "Number of empty lines needed to keep an empty line between collapsed trees.
  564. If you leave an empty line between the end of a subtree and the following
  565. headline, this empty line is hidden when the subtree is folded.
  566. Org-mode will leave (exactly) one empty line visible if the number of
  567. empty lines is equal or larger to the number given in this variable.
  568. So the default 2 means, at least 2 empty lines after the end of a subtree
  569. are needed to produce free space between a collapsed subtree and the
  570. following headline.
  571. Special case: when 0, never leave empty lines in collapsed view."
  572. :group 'org-cycle
  573. :type 'integer)
  574. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  575. (defcustom org-pre-cycle-hook nil
  576. "Hook that is run before visibility cycling is happening.
  577. The function(s) in this hook must accept a single argument which indicates
  578. the new state that will be set right after running this hook. The
  579. argument is a symbol. Before a global state change, it can have the values
  580. `overview', `content', or `all'. Before a local state change, it can have
  581. the values `folded', `children', or `subtree'."
  582. :group 'org-cycle
  583. :type 'hook)
  584. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  585. org-cycle-hide-drawers
  586. org-cycle-show-empty-lines
  587. org-optimize-window-after-visibility-change)
  588. "Hook that is run after `org-cycle' has changed the buffer visibility.
  589. The function(s) in this hook must accept a single argument which indicates
  590. the new state that was set by the most recent `org-cycle' command. The
  591. argument is a symbol. After a global state change, it can have the values
  592. `overview', `content', or `all'. After a local state change, it can have
  593. the values `folded', `children', or `subtree'."
  594. :group 'org-cycle
  595. :type 'hook)
  596. (defgroup org-edit-structure nil
  597. "Options concerning structure editing in Org-mode."
  598. :tag "Org Edit Structure"
  599. :group 'org-structure)
  600. (defcustom org-odd-levels-only nil
  601. "Non-nil means, skip even levels and only use odd levels for the outline.
  602. This has the effect that two stars are being added/taken away in
  603. promotion/demotion commands. It also influences how levels are
  604. handled by the exporters.
  605. Changing it requires restart of `font-lock-mode' to become effective
  606. for fontification also in regions already fontified.
  607. You may also set this on a per-file basis by adding one of the following
  608. lines to the buffer:
  609. #+STARTUP: odd
  610. #+STARTUP: oddeven"
  611. :group 'org-edit-structure
  612. :group 'org-font-lock
  613. :type 'boolean)
  614. (defcustom org-adapt-indentation t
  615. "Non-nil means, adapt indentation to outline node level.
  616. When this variable is set, Org assumes that you write outlines by
  617. indenting text in each node to align with the headline (after the stars).
  618. The following issues are influenced by this variable:
  619. - When this is set and the *entire* text in an entry is indented, the
  620. indentation is increased by one space in a demotion command, and
  621. decreased by one in a promotion command. If any line in the entry
  622. body starts with text at column 0, indentation is not changed at all.
  623. - Property drawers and planning information is inserted indented when
  624. this variable s set. When nil, they will not be indented.
  625. - TAB indents a line relative to context. The lines below a headline
  626. will be indented when this variable is set.
  627. Note that this is all about true indentation, by adding and removing
  628. space characters. See also `org-indent.el' which does level-dependent
  629. indentation in a virtual way, i.e. at display time in Emacs."
  630. :group 'org-edit-structure
  631. :type 'boolean)
  632. (defcustom org-special-ctrl-a/e nil
  633. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  634. When t, `C-a' will bring back the cursor to the beginning of the
  635. headline text, i.e. after the stars and after a possible TODO keyword.
  636. In an item, this will be the position after the bullet.
  637. When the cursor is already at that position, another `C-a' will bring
  638. it to the beginning of the line.
  639. `C-e' will jump to the end of the headline, ignoring the presence of tags
  640. in the headline. A second `C-e' will then jump to the true end of the
  641. line, after any tags.
  642. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  643. going to the true line boundary first. Only a directly following, identical
  644. keypress will bring the cursor to the special positions.
  645. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  646. set separately."
  647. :group 'org-edit-structure
  648. :type '(choice
  649. (const :tag "off" nil)
  650. (const :tag "after stars/bullet and before tags first" t)
  651. (const :tag "true line boundary first" reversed)
  652. (cons :tag "Set C-a and C-e separately"
  653. (choice :tag "Special C-a"
  654. (const :tag "off" nil)
  655. (const :tag "after stars/bullet first" t)
  656. (const :tag "before stars/bullet first" reversed))
  657. (choice :tag "Special C-e"
  658. (const :tag "off" nil)
  659. (const :tag "before tags first" t)
  660. (const :tag "after tags first" reversed)))))
  661. (if (fboundp 'defvaralias)
  662. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  663. (defcustom org-special-ctrl-k nil
  664. "Non-nil means `C-k' will behave specially in headlines.
  665. When nil, `C-k' will call the default `kill-line' command.
  666. When t, the following will happen while the cursor is in the headline:
  667. - When the cursor is at the beginning of a headline, kill the entire
  668. line and possible the folded subtree below the line.
  669. - When in the middle of the headline text, kill the headline up to the tags.
  670. - When after the headline text, kill the tags."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-yank-folded-subtrees t
  674. "Non-nil means, when yanking subtrees, fold them.
  675. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  676. it starts with a heading and all other headings in it are either children
  677. or siblings, then fold all the subtrees. However, do this only if no
  678. text after the yank would be swallowed into a folded tree by this action."
  679. :group 'org-edit-structure
  680. :type 'boolean)
  681. (defcustom org-yank-adjusted-subtrees nil
  682. "Non-nil means, when yanking subtrees, adjust the level.
  683. With this setting, `org-paste-subtree' is used to insert the subtree, see
  684. this function for details."
  685. :group 'org-edit-structure
  686. :type 'boolean)
  687. (defcustom org-M-RET-may-split-line '((default . t))
  688. "Non-nil means, M-RET will split the line at the cursor position.
  689. When nil, it will go to the end of the line before making a
  690. new line.
  691. You may also set this option in a different way for different
  692. contexts. Valid contexts are:
  693. headline when creating a new headline
  694. item when creating a new item
  695. table in a table field
  696. default the value to be used for all contexts not explicitly
  697. customized"
  698. :group 'org-structure
  699. :group 'org-table
  700. :type '(choice
  701. (const :tag "Always" t)
  702. (const :tag "Never" nil)
  703. (repeat :greedy t :tag "Individual contexts"
  704. (cons
  705. (choice :tag "Context"
  706. (const headline)
  707. (const item)
  708. (const table)
  709. (const default))
  710. (boolean)))))
  711. (defcustom org-insert-heading-respect-content nil
  712. "Non-nil means, insert new headings after the current subtree.
  713. When nil, the new heading is created directly after the current line.
  714. The commands \\[org-insert-heading-respect-content] and
  715. \\[org-insert-todo-heading-respect-content] turn this variable on
  716. for the duration of the command."
  717. :group 'org-structure
  718. :type 'boolean)
  719. (defcustom org-blank-before-new-entry '((heading . auto)
  720. (plain-list-item . auto))
  721. "Should `org-insert-heading' leave a blank line before new heading/item?
  722. The value is an alist, with `heading' and `plain-list-item' as car,
  723. and a boolean flag as cdr. For plain lists, if the variable
  724. `org-empty-line-terminates-plain-lists' is set, the setting here
  725. is ignored and no empty line is inserted, to keep the list in tact."
  726. :group 'org-edit-structure
  727. :type '(list
  728. (cons (const heading)
  729. (choice (const :tag "Never" nil)
  730. (const :tag "Always" t)
  731. (const :tag "Auto" auto)))
  732. (cons (const plain-list-item)
  733. (choice (const :tag "Never" nil)
  734. (const :tag "Always" t)
  735. (const :tag "Auto" auto)))))
  736. (defcustom org-insert-heading-hook nil
  737. "Hook being run after inserting a new heading."
  738. :group 'org-edit-structure
  739. :type 'hook)
  740. (defcustom org-enable-fixed-width-editor t
  741. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  742. This currently only means, they are never auto-wrapped.
  743. When nil, such lines will be treated like ordinary lines.
  744. See also the QUOTE keyword."
  745. :group 'org-edit-structure
  746. :type 'boolean)
  747. (defcustom org-goto-auto-isearch t
  748. "Non-nil means, typing characters in org-goto starts incremental search."
  749. :group 'org-edit-structure
  750. :type 'boolean)
  751. (defgroup org-sparse-trees nil
  752. "Options concerning sparse trees in Org-mode."
  753. :tag "Org Sparse Trees"
  754. :group 'org-structure)
  755. (defcustom org-highlight-sparse-tree-matches t
  756. "Non-nil means, highlight all matches that define a sparse tree.
  757. The highlights will automatically disappear the next time the buffer is
  758. changed by an edit command."
  759. :group 'org-sparse-trees
  760. :type 'boolean)
  761. (defcustom org-remove-highlights-with-change t
  762. "Non-nil means, any change to the buffer will remove temporary highlights.
  763. Such highlights are created by `org-occur' and `org-clock-display'.
  764. When nil, `C-c C-c needs to be used to get rid of the highlights.
  765. The highlights created by `org-preview-latex-fragment' always need
  766. `C-c C-c' to be removed."
  767. :group 'org-sparse-trees
  768. :group 'org-time
  769. :type 'boolean)
  770. (defcustom org-occur-hook '(org-first-headline-recenter)
  771. "Hook that is run after `org-occur' has constructed a sparse tree.
  772. This can be used to recenter the window to show as much of the structure
  773. as possible."
  774. :group 'org-sparse-trees
  775. :type 'hook)
  776. (defgroup org-imenu-and-speedbar nil
  777. "Options concerning imenu and speedbar in Org-mode."
  778. :tag "Org Imenu and Speedbar"
  779. :group 'org-structure)
  780. (defcustom org-imenu-depth 2
  781. "The maximum level for Imenu access to Org-mode headlines.
  782. This also applied for speedbar access."
  783. :group 'org-imenu-and-speedbar
  784. :type 'integer)
  785. (defgroup org-table nil
  786. "Options concerning tables in Org-mode."
  787. :tag "Org Table"
  788. :group 'org)
  789. (defcustom org-enable-table-editor 'optimized
  790. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  791. When nil, such lines will be treated like ordinary lines.
  792. When equal to the symbol `optimized', the table editor will be optimized to
  793. do the following:
  794. - Automatic overwrite mode in front of whitespace in table fields.
  795. This makes the structure of the table stay in tact as long as the edited
  796. field does not exceed the column width.
  797. - Minimize the number of realigns. Normally, the table is aligned each time
  798. TAB or RET are pressed to move to another field. With optimization this
  799. happens only if changes to a field might have changed the column width.
  800. Optimization requires replacing the functions `self-insert-command',
  801. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  802. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  803. very good at guessing when a re-align will be necessary, but you can always
  804. force one with \\[org-ctrl-c-ctrl-c].
  805. If you would like to use the optimized version in Org-mode, but the
  806. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  807. This variable can be used to turn on and off the table editor during a session,
  808. but in order to toggle optimization, a restart is required.
  809. See also the variable `org-table-auto-blank-field'."
  810. :group 'org-table
  811. :type '(choice
  812. (const :tag "off" nil)
  813. (const :tag "on" t)
  814. (const :tag "on, optimized" optimized)))
  815. (defcustom org-self-insert-cluster-for-undo t
  816. "Non-nil means cluster self-insert commands for undo when possible.
  817. If this is set, then, like in the Emacs command loop, 20 consequtive
  818. characters will be undone together.
  819. This is configurable, because there is some impact on typing performance."
  820. :group 'org-table
  821. :type 'boolean)
  822. (defcustom org-table-tab-recognizes-table.el t
  823. "Non-nil means, TAB will automatically notice a table.el table.
  824. When it sees such a table, it moves point into it and - if necessary -
  825. calls `table-recognize-table'."
  826. :group 'org-table-editing
  827. :type 'boolean)
  828. (defgroup org-link nil
  829. "Options concerning links in Org-mode."
  830. :tag "Org Link"
  831. :group 'org)
  832. (defvar org-link-abbrev-alist-local nil
  833. "Buffer-local version of `org-link-abbrev-alist', which see.
  834. The value of this is taken from the #+LINK lines.")
  835. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  836. (defcustom org-link-abbrev-alist nil
  837. "Alist of link abbreviations.
  838. The car of each element is a string, to be replaced at the start of a link.
  839. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  840. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  841. [[linkkey:tag][description]]
  842. The 'linkkey' must be a word word, starting with a letter, followed
  843. by letters, numbers, '-' or '_'.
  844. If REPLACE is a string, the tag will simply be appended to create the link.
  845. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  846. the placeholder \"%h\" will cause a url-encoded version of the tag to
  847. be inserted at that point (see the function `url-hexify-string').
  848. REPLACE may also be a function that will be called with the tag as the
  849. only argument to create the link, which should be returned as a string.
  850. See the manual for examples."
  851. :group 'org-link
  852. :type '(repeat
  853. (cons
  854. (string :tag "Protocol")
  855. (choice
  856. (string :tag "Format")
  857. (function)))))
  858. (defcustom org-descriptive-links t
  859. "Non-nil means, hide link part and only show description of bracket links.
  860. Bracket links are like [[link][description]]. This variable sets the initial
  861. state in new org-mode buffers. The setting can then be toggled on a
  862. per-buffer basis from the Org->Hyperlinks menu."
  863. :group 'org-link
  864. :type 'boolean)
  865. (defcustom org-link-file-path-type 'adaptive
  866. "How the path name in file links should be stored.
  867. Valid values are:
  868. relative Relative to the current directory, i.e. the directory of the file
  869. into which the link is being inserted.
  870. absolute Absolute path, if possible with ~ for home directory.
  871. noabbrev Absolute path, no abbreviation of home directory.
  872. adaptive Use relative path for files in the current directory and sub-
  873. directories of it. For other files, use an absolute path."
  874. :group 'org-link
  875. :type '(choice
  876. (const relative)
  877. (const absolute)
  878. (const noabbrev)
  879. (const adaptive)))
  880. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  881. "Types of links that should be activated in Org-mode files.
  882. This is a list of symbols, each leading to the activation of a certain link
  883. type. In principle, it does not hurt to turn on most link types - there may
  884. be a small gain when turning off unused link types. The types are:
  885. bracket The recommended [[link][description]] or [[link]] links with hiding.
  886. angular Links in angular brackets that may contain whitespace like
  887. <bbdb:Carsten Dominik>.
  888. plain Plain links in normal text, no whitespace, like http://google.com.
  889. radio Text that is matched by a radio target, see manual for details.
  890. tag Tag settings in a headline (link to tag search).
  891. date Time stamps (link to calendar).
  892. footnote Footnote labels.
  893. Changing this variable requires a restart of Emacs to become effective."
  894. :group 'org-link
  895. :type '(set :greedy t
  896. (const :tag "Double bracket links (new style)" bracket)
  897. (const :tag "Angular bracket links (old style)" angular)
  898. (const :tag "Plain text links" plain)
  899. (const :tag "Radio target matches" radio)
  900. (const :tag "Tags" tag)
  901. (const :tag "Timestamps" date)
  902. (const :tag "Footnotes" footnote)))
  903. (defcustom org-make-link-description-function nil
  904. "Function to use to generate link descriptions from links. If
  905. nil the link location will be used. This function must take two
  906. parameters; the first is the link and the second the description
  907. org-insert-link has generated, and should return the description
  908. to use."
  909. :group 'org-link
  910. :type 'function)
  911. (defgroup org-link-store nil
  912. "Options concerning storing links in Org-mode."
  913. :tag "Org Store Link"
  914. :group 'org-link)
  915. (defcustom org-email-link-description-format "Email %c: %.30s"
  916. "Format of the description part of a link to an email or usenet message.
  917. The following %-escapes will be replaced by corresponding information:
  918. %F full \"From\" field
  919. %f name, taken from \"From\" field, address if no name
  920. %T full \"To\" field
  921. %t first name in \"To\" field, address if no name
  922. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  923. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  924. %s subject
  925. %m message-id.
  926. You may use normal field width specification between the % and the letter.
  927. This is for example useful to limit the length of the subject.
  928. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  929. :group 'org-link-store
  930. :type 'string)
  931. (defcustom org-from-is-user-regexp
  932. (let (r1 r2)
  933. (when (and user-mail-address (not (string= user-mail-address "")))
  934. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  935. (when (and user-full-name (not (string= user-full-name "")))
  936. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  937. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  938. "Regexp matched against the \"From:\" header of an email or usenet message.
  939. It should match if the message is from the user him/herself."
  940. :group 'org-link-store
  941. :type 'regexp)
  942. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  943. "Non-nil means, storing a link to an Org file will use entry IDs.
  944. Note that before this variable is even considered, org-id must be loaded,
  945. so please customize `org-modules' and turn it on.
  946. The variable can have the following values:
  947. t Create an ID if needed to make a link to the current entry.
  948. create-if-interactive
  949. If `org-store-link' is called directly (interactively, as a user
  950. command), do create an ID to support the link. But when doing the
  951. job for remember, only use the ID if it already exists. The
  952. purpose of this setting is to avoid proliferation of unwanted
  953. IDs, just because you happen to be in an Org file when you
  954. call `org-remember' that automatically and preemptively
  955. creates a link. If you do want to get an ID link in a remember
  956. template to an entry not having an ID, create it first by
  957. explicitly creating a link to it, using `C-c C-l' first.
  958. create-if-interactive-and-no-custom-id
  959. Like create-if-interactive, but do not create an ID if there is
  960. a CUSTOM_ID property defined in the entry. This is the default.
  961. use-existing
  962. Use existing ID, do not create one.
  963. nil Never use an ID to make a link, instead link using a text search for
  964. the headline text."
  965. :group 'org-link-store
  966. :type '(choice
  967. (const :tag "Create ID to make link" t)
  968. (const :tag "Create if storing link interactively"
  969. create-if-interactive)
  970. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  971. create-if-interactive-and-no-custom-id)
  972. (const :tag "Only use existing" use-existing)
  973. (const :tag "Do not use ID to create link" nil)))
  974. (defcustom org-context-in-file-links t
  975. "Non-nil means, file links from `org-store-link' contain context.
  976. A search string will be added to the file name with :: as separator and
  977. used to find the context when the link is activated by the command
  978. `org-open-at-point'.
  979. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  980. negates this setting for the duration of the command."
  981. :group 'org-link-store
  982. :type 'boolean)
  983. (defcustom org-keep-stored-link-after-insertion nil
  984. "Non-nil means, keep link in list for entire session.
  985. The command `org-store-link' adds a link pointing to the current
  986. location to an internal list. These links accumulate during a session.
  987. The command `org-insert-link' can be used to insert links into any
  988. Org-mode file (offering completion for all stored links). When this
  989. option is nil, every link which has been inserted once using \\[org-insert-link]
  990. will be removed from the list, to make completing the unused links
  991. more efficient."
  992. :group 'org-link-store
  993. :type 'boolean)
  994. (defgroup org-link-follow nil
  995. "Options concerning following links in Org-mode."
  996. :tag "Org Follow Link"
  997. :group 'org-link)
  998. (defcustom org-link-translation-function nil
  999. "Function to translate links with different syntax to Org syntax.
  1000. This can be used to translate links created for example by the Planner
  1001. or emacs-wiki packages to Org syntax.
  1002. The function must accept two parameters, a TYPE containing the link
  1003. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1004. which is everything after the link protocol. It should return a cons
  1005. with possibly modified values of type and path.
  1006. Org contains a function for this, so if you set this variable to
  1007. `org-translate-link-from-planner', you should be able follow many
  1008. links created by planner."
  1009. :group 'org-link-follow
  1010. :type 'function)
  1011. (defcustom org-follow-link-hook nil
  1012. "Hook that is run after a link has been followed."
  1013. :group 'org-link-follow
  1014. :type 'hook)
  1015. (defcustom org-tab-follows-link nil
  1016. "Non-nil means, on links TAB will follow the link.
  1017. Needs to be set before org.el is loaded.
  1018. This really should not be used, it does not make sense, and the
  1019. implementation is bad."
  1020. :group 'org-link-follow
  1021. :type 'boolean)
  1022. (defcustom org-return-follows-link nil
  1023. "Non-nil means, on links RET will follow the link.
  1024. Needs to be set before org.el is loaded."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-mouse-1-follows-link
  1028. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1029. "Non-nil means, mouse-1 on a link will follow the link.
  1030. A longer mouse click will still set point. Does not work on XEmacs.
  1031. Needs to be set before org.el is loaded."
  1032. :group 'org-link-follow
  1033. :type 'boolean)
  1034. (defcustom org-mark-ring-length 4
  1035. "Number of different positions to be recorded in the ring
  1036. Changing this requires a restart of Emacs to work correctly."
  1037. :group 'org-link-follow
  1038. :type 'integer)
  1039. (defcustom org-link-frame-setup
  1040. '((vm . vm-visit-folder-other-frame)
  1041. (gnus . gnus-other-frame)
  1042. (file . find-file-other-window))
  1043. "Setup the frame configuration for following links.
  1044. When following a link with Emacs, it may often be useful to display
  1045. this link in another window or frame. This variable can be used to
  1046. set this up for the different types of links.
  1047. For VM, use any of
  1048. `vm-visit-folder'
  1049. `vm-visit-folder-other-frame'
  1050. For Gnus, use any of
  1051. `gnus'
  1052. `gnus-other-frame'
  1053. `org-gnus-no-new-news'
  1054. For FILE, use any of
  1055. `find-file'
  1056. `find-file-other-window'
  1057. `find-file-other-frame'
  1058. For the calendar, use the variable `calendar-setup'.
  1059. For BBDB, it is currently only possible to display the matches in
  1060. another window."
  1061. :group 'org-link-follow
  1062. :type '(list
  1063. (cons (const vm)
  1064. (choice
  1065. (const vm-visit-folder)
  1066. (const vm-visit-folder-other-window)
  1067. (const vm-visit-folder-other-frame)))
  1068. (cons (const gnus)
  1069. (choice
  1070. (const gnus)
  1071. (const gnus-other-frame)
  1072. (const org-gnus-no-new-news)))
  1073. (cons (const file)
  1074. (choice
  1075. (const find-file)
  1076. (const find-file-other-window)
  1077. (const find-file-other-frame)))))
  1078. (defcustom org-display-internal-link-with-indirect-buffer nil
  1079. "Non-nil means, use indirect buffer to display infile links.
  1080. Activating internal links (from one location in a file to another location
  1081. in the same file) normally just jumps to the location. When the link is
  1082. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1083. another window. When this option is set, the other window actually displays
  1084. an indirect buffer clone of the current buffer, to avoid any visibility
  1085. changes to the current buffer."
  1086. :group 'org-link-follow
  1087. :type 'boolean)
  1088. (defcustom org-open-non-existing-files nil
  1089. "Non-nil means, `org-open-file' will open non-existing files.
  1090. When nil, an error will be generated.
  1091. This variable applies only to external applications because they
  1092. might choke on non-existing files. If the link is to a file that
  1093. will be openend in Emacs, the variable is ignored."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-open-directory-means-index-dot-org nil
  1097. "Non-nil means, a link to a directory really means to index.org.
  1098. When nil, following a directory link will run dired or open a finder/explorer
  1099. window on that directory."
  1100. :group 'org-link-follow
  1101. :type 'boolean)
  1102. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1103. "Function and arguments to call for following mailto links.
  1104. This is a list with the first element being a lisp function, and the
  1105. remaining elements being arguments to the function. In string arguments,
  1106. %a will be replaced by the address, and %s will be replaced by the subject
  1107. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1108. :group 'org-link-follow
  1109. :type '(choice
  1110. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1111. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1112. (const :tag "message-mail" (message-mail "%a" "%s"))
  1113. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1114. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1115. "Non-nil means, ask for confirmation before executing shell links.
  1116. Shell links can be dangerous: just think about a link
  1117. [[shell:rm -rf ~/*][Google Search]]
  1118. This link would show up in your Org-mode document as \"Google Search\",
  1119. but really it would remove your entire home directory.
  1120. Therefore we advise against setting this variable to nil.
  1121. Just change it to `y-or-n-p' if you want to confirm with a
  1122. single keystroke rather than having to type \"yes\"."
  1123. :group 'org-link-follow
  1124. :type '(choice
  1125. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1126. (const :tag "with y-or-n (faster)" y-or-n-p)
  1127. (const :tag "no confirmation (dangerous)" nil)))
  1128. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1129. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1130. Elisp links can be dangerous: just think about a link
  1131. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1132. This link would show up in your Org-mode document as \"Google Search\",
  1133. but really it would remove your entire home directory.
  1134. Therefore we advise against setting this variable to nil.
  1135. Just change it to `y-or-n-p' if you want to confirm with a
  1136. single keystroke rather than having to type \"yes\"."
  1137. :group 'org-link-follow
  1138. :type '(choice
  1139. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1140. (const :tag "with y-or-n (faster)" y-or-n-p)
  1141. (const :tag "no confirmation (dangerous)" nil)))
  1142. (defconst org-file-apps-defaults-gnu
  1143. '((remote . emacs)
  1144. (system . mailcap)
  1145. (t . mailcap))
  1146. "Default file applications on a UNIX or GNU/Linux system.
  1147. See `org-file-apps'.")
  1148. (defconst org-file-apps-defaults-macosx
  1149. '((remote . emacs)
  1150. (t . "open %s")
  1151. (system . "open %s")
  1152. ("ps.gz" . "gv %s")
  1153. ("eps.gz" . "gv %s")
  1154. ("dvi" . "xdvi %s")
  1155. ("fig" . "xfig %s"))
  1156. "Default file applications on a MacOS X system.
  1157. The system \"open\" is known as a default, but we use X11 applications
  1158. for some files for which the OS does not have a good default.
  1159. See `org-file-apps'.")
  1160. (defconst org-file-apps-defaults-windowsnt
  1161. (list
  1162. '(remote . emacs)
  1163. (cons t
  1164. (list (if (featurep 'xemacs)
  1165. 'mswindows-shell-execute
  1166. 'w32-shell-execute)
  1167. "open" 'file))
  1168. (cons 'system
  1169. (list (if (featurep 'xemacs)
  1170. 'mswindows-shell-execute
  1171. 'w32-shell-execute)
  1172. "open" 'file)))
  1173. "Default file applications on a Windows NT system.
  1174. The system \"open\" is used for most files.
  1175. See `org-file-apps'.")
  1176. (defcustom org-file-apps
  1177. '(
  1178. (auto-mode . emacs)
  1179. ("\\.x?html?\\'" . default)
  1180. ("\\.pdf\\'" . default)
  1181. )
  1182. "External applications for opening `file:path' items in a document.
  1183. Org-mode uses system defaults for different file types, but
  1184. you can use this variable to set the application for a given file
  1185. extension. The entries in this list are cons cells where the car identifies
  1186. files and the cdr the corresponding command. Possible values for the
  1187. file identifier are
  1188. \"regex\" Regular expression matched against the file name. For backward
  1189. compatibility, this can also be a string with only alphanumeric
  1190. characters, which is then interpreted as an extension.
  1191. `directory' Matches a directory
  1192. `remote' Matches a remote file, accessible through tramp or efs.
  1193. Remote files most likely should be visited through Emacs
  1194. because external applications cannot handle such paths.
  1195. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1196. so all files Emacs knows how to handle. Using this with
  1197. command `emacs' will open most files in Emacs. Beware that this
  1198. will also open html files inside Emacs, unless you add
  1199. (\"html\" . default) to the list as well.
  1200. t Default for files not matched by any of the other options.
  1201. `system' The system command to open files, like `open' on Windows
  1202. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1203. that will be selected if you call `C-c C-o' with a double
  1204. `C-u C-u' prefix.
  1205. Possible values for the command are:
  1206. `emacs' The file will be visited by the current Emacs process.
  1207. `default' Use the default application for this file type, which is the
  1208. association for t in the list, most likely in the system-specific
  1209. part.
  1210. This can be used to overrule an unwanted setting in the
  1211. system-specific variable.
  1212. `system' Use the system command for opening files, like \"open\".
  1213. This command is specified by the entry whose car is `system'.
  1214. Most likely, the system-specific version of this variable
  1215. does define this command, but you can overrule/replace it
  1216. here.
  1217. string A command to be executed by a shell; %s will be replaced
  1218. by the path to the file.
  1219. sexp A Lisp form which will be evaluated. The file path will
  1220. be available in the Lisp variable `file'.
  1221. For more examples, see the system specific constants
  1222. `org-file-apps-defaults-macosx'
  1223. `org-file-apps-defaults-windowsnt'
  1224. `org-file-apps-defaults-gnu'."
  1225. :group 'org-link-follow
  1226. :type '(repeat
  1227. (cons (choice :value ""
  1228. (string :tag "Extension")
  1229. (const :tag "System command to open files" system)
  1230. (const :tag "Default for unrecognized files" t)
  1231. (const :tag "Remote file" remote)
  1232. (const :tag "Links to a directory" directory)
  1233. (const :tag "Any files that have Emacs modes"
  1234. auto-mode))
  1235. (choice :value ""
  1236. (const :tag "Visit with Emacs" emacs)
  1237. (const :tag "Use default" default)
  1238. (const :tag "Use the system command" system)
  1239. (string :tag "Command")
  1240. (sexp :tag "Lisp form")))))
  1241. (defgroup org-refile nil
  1242. "Options concerning refiling entries in Org-mode."
  1243. :tag "Org Refile"
  1244. :group 'org)
  1245. (defcustom org-directory "~/org"
  1246. "Directory with org files.
  1247. This is just a default location to look for Org files. There is no need
  1248. at all to put your files into this directory. It is only used in the
  1249. following situations:
  1250. 1. When a remember template specifies a target file that is not an
  1251. absolute path. The path will then be interpreted relative to
  1252. `org-directory'
  1253. 2. When a remember note is filed away in an interactive way (when exiting the
  1254. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1255. with `org-directory' as the default path."
  1256. :group 'org-refile
  1257. :group 'org-remember
  1258. :type 'directory)
  1259. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1260. "Default target for storing notes.
  1261. Used by the hooks for remember.el. This can be a string, or nil to mean
  1262. the value of `remember-data-file'.
  1263. You can set this on a per-template basis with the variable
  1264. `org-remember-templates'."
  1265. :group 'org-refile
  1266. :group 'org-remember
  1267. :type '(choice
  1268. (const :tag "Default from remember-data-file" nil)
  1269. file))
  1270. (defcustom org-goto-interface 'outline
  1271. "The default interface to be used for `org-goto'.
  1272. Allowed values are:
  1273. outline The interface shows an outline of the relevant file
  1274. and the correct heading is found by moving through
  1275. the outline or by searching with incremental search.
  1276. outline-path-completion Headlines in the current buffer are offered via
  1277. completion. This is the interface also used by
  1278. the refile command."
  1279. :group 'org-refile
  1280. :type '(choice
  1281. (const :tag "Outline" outline)
  1282. (const :tag "Outline-path-completion" outline-path-completion)))
  1283. (defcustom org-goto-max-level 5
  1284. "Maximum level to be considered when running org-goto with refile interface."
  1285. :group 'org-refile
  1286. :type 'integer)
  1287. (defcustom org-reverse-note-order nil
  1288. "Non-nil means, store new notes at the beginning of a file or entry.
  1289. When nil, new notes will be filed to the end of a file or entry.
  1290. This can also be a list with cons cells of regular expressions that
  1291. are matched against file names, and values."
  1292. :group 'org-remember
  1293. :group 'org-refile
  1294. :type '(choice
  1295. (const :tag "Reverse always" t)
  1296. (const :tag "Reverse never" nil)
  1297. (repeat :tag "By file name regexp"
  1298. (cons regexp boolean))))
  1299. (defcustom org-refile-targets nil
  1300. "Targets for refiling entries with \\[org-refile].
  1301. This is list of cons cells. Each cell contains:
  1302. - a specification of the files to be considered, either a list of files,
  1303. or a symbol whose function or variable value will be used to retrieve
  1304. a file name or a list of file names. If you use `org-agenda-files' for
  1305. that, all agenda files will be scanned for targets. Nil means, consider
  1306. headings in the current buffer.
  1307. - A specification of how to find candidate refile targets. This may be
  1308. any of:
  1309. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1310. This tag has to be present in all target headlines, inheritance will
  1311. not be considered.
  1312. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1313. todo keyword.
  1314. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1315. headlines that are refiling targets.
  1316. - a cons cell (:level . N). Any headline of level N is considered a target.
  1317. Note that, when `org-odd-levels-only' is set, level corresponds to
  1318. order in hierarchy, not to the number of stars.
  1319. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1320. Note that, when `org-odd-levels-only' is set, level corresponds to
  1321. order in hierarchy, not to the number of stars.
  1322. You can set the variable `org-refile-target-verify-function' to a function
  1323. to verify each headline found by the simple critery above.
  1324. When this variable is nil, all top-level headlines in the current buffer
  1325. are used, equivalent to the value `((nil . (:level . 1))'."
  1326. :group 'org-refile
  1327. :type '(repeat
  1328. (cons
  1329. (choice :value org-agenda-files
  1330. (const :tag "All agenda files" org-agenda-files)
  1331. (const :tag "Current buffer" nil)
  1332. (function) (variable) (file))
  1333. (choice :tag "Identify target headline by"
  1334. (cons :tag "Specific tag" (const :value :tag) (string))
  1335. (cons :tag "TODO keyword" (const :value :todo) (string))
  1336. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1337. (cons :tag "Level number" (const :value :level) (integer))
  1338. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1339. (defcustom org-refile-target-verify-function nil
  1340. "Function to verify if the headline at point should be a refile target.
  1341. The function will be called without arguments, with point at the
  1342. beginning of the headline. It should return t and leave point
  1343. where it is if the headline is a valid target for refiling.
  1344. If the target should not be selected, the function must return nil.
  1345. In addition to this, it may move point to a place from where the search
  1346. should be continued. For example, the function may decide that the entire
  1347. subtree of the current entry should be excluded and move point to the end
  1348. of the subtree."
  1349. :group 'org-refile
  1350. :type 'function)
  1351. (defcustom org-refile-use-outline-path nil
  1352. "Non-nil means, provide refile targets as paths.
  1353. So a level 3 headline will be available as level1/level2/level3.
  1354. When the value is `file', also include the file name (without directory)
  1355. into the path. In this case, you can also stop the completion after
  1356. the file name, to get entries inserted as top level in the file.
  1357. When `full-file-path', include the full file path."
  1358. :group 'org-refile
  1359. :type '(choice
  1360. (const :tag "Not" nil)
  1361. (const :tag "Yes" t)
  1362. (const :tag "Start with file name" file)
  1363. (const :tag "Start with full file path" full-file-path)))
  1364. (defcustom org-outline-path-complete-in-steps t
  1365. "Non-nil means, complete the outline path in hierarchical steps.
  1366. When Org-mode uses the refile interface to select an outline path
  1367. \(see variable `org-refile-use-outline-path'), the completion of
  1368. the path can be done is a single go, or if can be done in steps down
  1369. the headline hierarchy. Going in steps is probably the best if you
  1370. do not use a special completion package like `ido' or `icicles'.
  1371. However, when using these packages, going in one step can be very
  1372. fast, while still showing the whole path to the entry."
  1373. :group 'org-refile
  1374. :type 'boolean)
  1375. (defcustom org-refile-allow-creating-parent-nodes nil
  1376. "Non-nil means, allow to create new nodes as refile targets.
  1377. New nodes are then created by adding \"/new node name\" to the completion
  1378. of an existing node. When the value of this variable is `confirm',
  1379. new node creation must be confirmed by the user (recommended)
  1380. When nil, the completion must match an existing entry.
  1381. Note that, if the new heading is not seen by the criteria
  1382. listed in `org-refile-targets', multiple instances of the same
  1383. heading would be created by trying again to file under the new
  1384. heading."
  1385. :group 'org-refile
  1386. :type '(choice
  1387. (const :tag "Never" nil)
  1388. (const :tag "Always" t)
  1389. (const :tag "Prompt for confirmation" confirm)))
  1390. (defgroup org-todo nil
  1391. "Options concerning TODO items in Org-mode."
  1392. :tag "Org TODO"
  1393. :group 'org)
  1394. (defgroup org-progress nil
  1395. "Options concerning Progress logging in Org-mode."
  1396. :tag "Org Progress"
  1397. :group 'org-time)
  1398. (defvar org-todo-interpretation-widgets
  1399. '(
  1400. (:tag "Sequence (cycling hits every state)" sequence)
  1401. (:tag "Type (cycling directly to DONE)" type))
  1402. "The available interpretation symbols for customizing
  1403. `org-todo-keywords'.
  1404. Interested libraries should add to this list.")
  1405. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1406. "List of TODO entry keyword sequences and their interpretation.
  1407. \\<org-mode-map>This is a list of sequences.
  1408. Each sequence starts with a symbol, either `sequence' or `type',
  1409. indicating if the keywords should be interpreted as a sequence of
  1410. action steps, or as different types of TODO items. The first
  1411. keywords are states requiring action - these states will select a headline
  1412. for inclusion into the global TODO list Org-mode produces. If one of
  1413. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1414. signify that no further action is necessary. If \"|\" is not found,
  1415. the last keyword is treated as the only DONE state of the sequence.
  1416. The command \\[org-todo] cycles an entry through these states, and one
  1417. additional state where no keyword is present. For details about this
  1418. cycling, see the manual.
  1419. TODO keywords and interpretation can also be set on a per-file basis with
  1420. the special #+SEQ_TODO and #+TYP_TODO lines.
  1421. Each keyword can optionally specify a character for fast state selection
  1422. \(in combination with the variable `org-use-fast-todo-selection')
  1423. and specifiers for state change logging, using the same syntax
  1424. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1425. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1426. indicates to record a time stamp each time this state is selected.
  1427. Each keyword may also specify if a timestamp or a note should be
  1428. recorded when entering or leaving the state, by adding additional
  1429. characters in the parenthesis after the keyword. This looks like this:
  1430. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1431. record only the time of the state change. With X and Y being either
  1432. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1433. Y when leaving the state if and only if the *target* state does not
  1434. define X. You may omit any of the fast-selection key or X or /Y,
  1435. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1436. For backward compatibility, this variable may also be just a list
  1437. of keywords - in this case the interpretation (sequence or type) will be
  1438. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1439. :group 'org-todo
  1440. :group 'org-keywords
  1441. :type '(choice
  1442. (repeat :tag "Old syntax, just keywords"
  1443. (string :tag "Keyword"))
  1444. (repeat :tag "New syntax"
  1445. (cons
  1446. (choice
  1447. :tag "Interpretation"
  1448. ;;Quick and dirty way to see
  1449. ;;`org-todo-interpretations'. This takes the
  1450. ;;place of item arguments
  1451. :convert-widget
  1452. (lambda (widget)
  1453. (widget-put widget
  1454. :args (mapcar
  1455. #'(lambda (x)
  1456. (widget-convert
  1457. (cons 'const x)))
  1458. org-todo-interpretation-widgets))
  1459. widget))
  1460. (repeat
  1461. (string :tag "Keyword"))))))
  1462. (defvar org-todo-keywords-1 nil
  1463. "All TODO and DONE keywords active in a buffer.")
  1464. (make-variable-buffer-local 'org-todo-keywords-1)
  1465. (defvar org-todo-keywords-for-agenda nil)
  1466. (defvar org-done-keywords-for-agenda nil)
  1467. (defvar org-todo-keyword-alist-for-agenda nil)
  1468. (defvar org-tag-alist-for-agenda nil)
  1469. (defvar org-agenda-contributing-files nil)
  1470. (defvar org-not-done-keywords nil)
  1471. (make-variable-buffer-local 'org-not-done-keywords)
  1472. (defvar org-done-keywords nil)
  1473. (make-variable-buffer-local 'org-done-keywords)
  1474. (defvar org-todo-heads nil)
  1475. (make-variable-buffer-local 'org-todo-heads)
  1476. (defvar org-todo-sets nil)
  1477. (make-variable-buffer-local 'org-todo-sets)
  1478. (defvar org-todo-log-states nil)
  1479. (make-variable-buffer-local 'org-todo-log-states)
  1480. (defvar org-todo-kwd-alist nil)
  1481. (make-variable-buffer-local 'org-todo-kwd-alist)
  1482. (defvar org-todo-key-alist nil)
  1483. (make-variable-buffer-local 'org-todo-key-alist)
  1484. (defvar org-todo-key-trigger nil)
  1485. (make-variable-buffer-local 'org-todo-key-trigger)
  1486. (defcustom org-todo-interpretation 'sequence
  1487. "Controls how TODO keywords are interpreted.
  1488. This variable is in principle obsolete and is only used for
  1489. backward compatibility, if the interpretation of todo keywords is
  1490. not given already in `org-todo-keywords'. See that variable for
  1491. more information."
  1492. :group 'org-todo
  1493. :group 'org-keywords
  1494. :type '(choice (const sequence)
  1495. (const type)))
  1496. (defcustom org-use-fast-todo-selection t
  1497. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1498. This variable describes if and under what circumstances the cycling
  1499. mechanism for TODO keywords will be replaced by a single-key, direct
  1500. selection scheme.
  1501. When nil, fast selection is never used.
  1502. When the symbol `prefix', it will be used when `org-todo' is called with
  1503. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1504. in an agenda buffer.
  1505. When t, fast selection is used by default. In this case, the prefix
  1506. argument forces cycling instead.
  1507. In all cases, the special interface is only used if access keys have actually
  1508. been assigned by the user, i.e. if keywords in the configuration are followed
  1509. by a letter in parenthesis, like TODO(t)."
  1510. :group 'org-todo
  1511. :type '(choice
  1512. (const :tag "Never" nil)
  1513. (const :tag "By default" t)
  1514. (const :tag "Only with C-u C-c C-t" prefix)))
  1515. (defcustom org-provide-todo-statistics t
  1516. "Non-nil means, update todo statistics after insert and toggle.
  1517. ALL-HEADLINES means update todo statistics by including headlines
  1518. with no TODO keyword as well, counting them as not done.
  1519. A list of TODO keywords means the same, but skip keywords that are
  1520. not in this list.
  1521. When this is set, todo statistics is updated in the parent of the
  1522. current entry each time a todo state is changed."
  1523. :group 'org-todo
  1524. :type '(choice
  1525. (const :tag "Yes, only for TODO entries" t)
  1526. (const :tag "Yes, including all entries" 'all-headlines)
  1527. (repeat :tag "Yes, for TODOs in this list"
  1528. (string :tag "TODO keyword"))
  1529. (other :tag "No TODO statistics" nil)))
  1530. (defcustom org-hierarchical-todo-statistics t
  1531. "Non-nil means, TODO statistics covers just direct children.
  1532. When nil, all entries in the subtree are considered.
  1533. This has only an effect if `org-provide-todo-statistics' is set."
  1534. :group 'org-todo
  1535. :type 'boolean)
  1536. (defcustom org-after-todo-state-change-hook nil
  1537. "Hook which is run after the state of a TODO item was changed.
  1538. The new state (a string with a TODO keyword, or nil) is available in the
  1539. Lisp variable `state'."
  1540. :group 'org-todo
  1541. :type 'hook)
  1542. (defvar org-blocker-hook nil
  1543. "Hook for functions that are allowed to block a state change.
  1544. Each function gets as its single argument a property list, see
  1545. `org-trigger-hook' for more information about this list.
  1546. If any of the functions in this hook returns nil, the state change
  1547. is blocked.")
  1548. (defvar org-trigger-hook nil
  1549. "Hook for functions that are triggered by a state change.
  1550. Each function gets as its single argument a property list with at least
  1551. the following elements:
  1552. (:type type-of-change :position pos-at-entry-start
  1553. :from old-state :to new-state)
  1554. Depending on the type, more properties may be present.
  1555. This mechanism is currently implemented for:
  1556. TODO state changes
  1557. ------------------
  1558. :type todo-state-change
  1559. :from previous state (keyword as a string), or nil, or a symbol
  1560. 'todo' or 'done', to indicate the general type of state.
  1561. :to new state, like in :from")
  1562. (defcustom org-enforce-todo-dependencies nil
  1563. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1564. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1565. be blocked if any prior sibling is not yet done.
  1566. Finally, if the parent is blocked because of ordered siblings of its own,
  1567. the child will also be blocked.
  1568. This variable needs to be set before org.el is loaded, and you need to
  1569. restart Emacs after a change to make the change effective. The only way
  1570. to change is while Emacs is running is through the customize interface."
  1571. :set (lambda (var val)
  1572. (set var val)
  1573. (if val
  1574. (add-hook 'org-blocker-hook
  1575. 'org-block-todo-from-children-or-siblings-or-parent)
  1576. (remove-hook 'org-blocker-hook
  1577. 'org-block-todo-from-children-or-siblings-or-parent)))
  1578. :group 'org-todo
  1579. :type 'boolean)
  1580. (defcustom org-enforce-todo-checkbox-dependencies nil
  1581. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1582. When this is nil, checkboxes have no influence on switching TODO states.
  1583. When non-nil, you first need to check off all check boxes before the TODO
  1584. entry can be switched to DONE.
  1585. This variable needs to be set before org.el is loaded, and you need to
  1586. restart Emacs after a change to make the change effective. The only way
  1587. to change is while Emacs is running is through the customize interface."
  1588. :set (lambda (var val)
  1589. (set var val)
  1590. (if val
  1591. (add-hook 'org-blocker-hook
  1592. 'org-block-todo-from-checkboxes)
  1593. (remove-hook 'org-blocker-hook
  1594. 'org-block-todo-from-checkboxes)))
  1595. :group 'org-todo
  1596. :type 'boolean)
  1597. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1598. "Non-nil means, inserting a TODO heading is treated as state change.
  1599. So when the command \\[org-insert-todo-heading] is used, state change
  1600. logging will apply if appropriate. When nil, the new TODO item will
  1601. be inserted directly, and no logging will take place."
  1602. :group 'org-todo
  1603. :type 'boolean)
  1604. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1605. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1606. This is the default behavior. However, setting this to nil allows a
  1607. convenient way to select a TODO state and bypass any logging associated
  1608. with that."
  1609. :group 'org-todo
  1610. :type 'boolean)
  1611. (defcustom org-todo-state-tags-triggers nil
  1612. "Tag changes that should be triggered by TODO state changes.
  1613. This is a list. Each entry is
  1614. (state-change (tag . flag) .......)
  1615. State-change can be a string with a state, and empty string to indicate the
  1616. state that has no TODO keyword, or it can be one of the symbols `todo'
  1617. or `done', meaning any not-done or done state, respectively."
  1618. :group 'org-todo
  1619. :group 'org-tags
  1620. :type '(repeat
  1621. (cons (choice :tag "When changing to"
  1622. (const :tag "Not-done state" todo)
  1623. (const :tag "Done state" done)
  1624. (string :tag "State"))
  1625. (repeat
  1626. (cons :tag "Tag action"
  1627. (string :tag "Tag")
  1628. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1629. (defcustom org-log-done nil
  1630. "Information to record when a task moves to the DONE state.
  1631. Possible values are:
  1632. nil Don't add anything, just change the keyword
  1633. time Add a time stamp to the task
  1634. note Prompt a closing note and add it with template `org-log-note-headings'
  1635. This option can also be set with on a per-file-basis with
  1636. #+STARTUP: nologdone
  1637. #+STARTUP: logdone
  1638. #+STARTUP: lognotedone
  1639. You can have local logging settings for a subtree by setting the LOGGING
  1640. property to one or more of these keywords."
  1641. :group 'org-todo
  1642. :group 'org-progress
  1643. :type '(choice
  1644. (const :tag "No logging" nil)
  1645. (const :tag "Record CLOSED timestamp" time)
  1646. (const :tag "Record CLOSED timestamp with closing note." note)))
  1647. ;; Normalize old uses of org-log-done.
  1648. (cond
  1649. ((eq org-log-done t) (setq org-log-done 'time))
  1650. ((and (listp org-log-done) (memq 'done org-log-done))
  1651. (setq org-log-done 'note)))
  1652. (defcustom org-log-note-clock-out nil
  1653. "Non-nil means, record a note when clocking out of an item.
  1654. This can also be configured on a per-file basis by adding one of
  1655. the following lines anywhere in the buffer:
  1656. #+STARTUP: lognoteclock-out
  1657. #+STARTUP: nolognoteclock-out"
  1658. :group 'org-todo
  1659. :group 'org-progress
  1660. :type 'boolean)
  1661. (defcustom org-log-done-with-time t
  1662. "Non-nil means, the CLOSED time stamp will contain date and time.
  1663. When nil, only the date will be recorded."
  1664. :group 'org-progress
  1665. :type 'boolean)
  1666. (defcustom org-log-note-headings
  1667. '((done . "CLOSING NOTE %t")
  1668. (state . "State %-12s from %-12S %t")
  1669. (note . "Note taken on %t")
  1670. (clock-out . ""))
  1671. "Headings for notes added to entries.
  1672. The value is an alist, with the car being a symbol indicating the note
  1673. context, and the cdr is the heading to be used. The heading may also be the
  1674. empty string.
  1675. %t in the heading will be replaced by a time stamp.
  1676. %s will be replaced by the new TODO state, in double quotes.
  1677. %S will be replaced by the old TODO state, in double quotes.
  1678. %u will be replaced by the user name.
  1679. %U will be replaced by the full user name."
  1680. :group 'org-todo
  1681. :group 'org-progress
  1682. :type '(list :greedy t
  1683. (cons (const :tag "Heading when closing an item" done) string)
  1684. (cons (const :tag
  1685. "Heading when changing todo state (todo sequence only)"
  1686. state) string)
  1687. (cons (const :tag "Heading when just taking a note" note) string)
  1688. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1689. (unless (assq 'note org-log-note-headings)
  1690. (push '(note . "%t") org-log-note-headings))
  1691. (defcustom org-log-into-drawer nil
  1692. "Non-nil means, insert state change notes and time stamps into a drawer.
  1693. When nil, state changes notes will be inserted after the headline and
  1694. any scheduling and clock lines, but not inside a drawer.
  1695. The value of this variable should be the name of the drawer to use.
  1696. LOGBOOK is proposed at the default drawer for this purpose, you can
  1697. also set this to a string to define the drawer of your choice.
  1698. A value of t is also allowed, representing \"LOGBOOK\".
  1699. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1700. will be ignored.
  1701. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1702. a subtree."
  1703. :group 'org-todo
  1704. :group 'org-progress
  1705. :type '(choice
  1706. (const :tag "Not into a drawer" nil)
  1707. (const :tag "LOGBOOK" t)
  1708. (string :tag "Other")))
  1709. (if (fboundp 'defvaralias)
  1710. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1711. (defun org-log-into-drawer ()
  1712. "Return the value of `org-log-into-drawer', but let properties overrule.
  1713. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1714. used instead of the default value."
  1715. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1716. (cond
  1717. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1718. ((equal p "t") "LOGBOOK")
  1719. (t p))))
  1720. (defcustom org-log-state-notes-insert-after-drawers nil
  1721. "Non-nil means, insert state change notes after any drawers in entry.
  1722. Only the drawers that *immediately* follow the headline and the
  1723. deadline/scheduled line are skipped.
  1724. When nil, insert notes right after the heading and perhaps the line
  1725. with deadline/scheduling if present.
  1726. This variable will have no effect if `org-log-into-drawer' is
  1727. set."
  1728. :group 'org-todo
  1729. :group 'org-progress
  1730. :type 'boolean)
  1731. (defcustom org-log-states-order-reversed t
  1732. "Non-nil means, the latest state change note will be directly after heading.
  1733. When nil, the notes will be orderer according to time."
  1734. :group 'org-todo
  1735. :group 'org-progress
  1736. :type 'boolean)
  1737. (defcustom org-log-repeat 'time
  1738. "Non-nil means, record moving through the DONE state when triggering repeat.
  1739. An auto-repeating tasks is immediately switched back to TODO when marked
  1740. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1741. the TODO keyword definition, or recording a closing note by setting
  1742. `org-log-done', there will be no record of the task moving through DONE.
  1743. This variable forces taking a note anyway. Possible values are:
  1744. nil Don't force a record
  1745. time Record a time stamp
  1746. note Record a note
  1747. This option can also be set with on a per-file-basis with
  1748. #+STARTUP: logrepeat
  1749. #+STARTUP: lognoterepeat
  1750. #+STARTUP: nologrepeat
  1751. You can have local logging settings for a subtree by setting the LOGGING
  1752. property to one or more of these keywords."
  1753. :group 'org-todo
  1754. :group 'org-progress
  1755. :type '(choice
  1756. (const :tag "Don't force a record" nil)
  1757. (const :tag "Force recording the DONE state" time)
  1758. (const :tag "Force recording a note with the DONE state" note)))
  1759. (defgroup org-priorities nil
  1760. "Priorities in Org-mode."
  1761. :tag "Org Priorities"
  1762. :group 'org-todo)
  1763. (defcustom org-enable-priority-commands t
  1764. "Non-nil means, priority commands are active.
  1765. When nil, these commands will be disabled, so that you never accidentally
  1766. set a priority."
  1767. :group 'org-priorities
  1768. :type 'boolean)
  1769. (defcustom org-highest-priority ?A
  1770. "The highest priority of TODO items. A character like ?A, ?B etc.
  1771. Must have a smaller ASCII number than `org-lowest-priority'."
  1772. :group 'org-priorities
  1773. :type 'character)
  1774. (defcustom org-lowest-priority ?C
  1775. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1776. Must have a larger ASCII number than `org-highest-priority'."
  1777. :group 'org-priorities
  1778. :type 'character)
  1779. (defcustom org-default-priority ?B
  1780. "The default priority of TODO items.
  1781. This is the priority an item get if no explicit priority is given."
  1782. :group 'org-priorities
  1783. :type 'character)
  1784. (defcustom org-priority-start-cycle-with-default t
  1785. "Non-nil means, start with default priority when starting to cycle.
  1786. When this is nil, the first step in the cycle will be (depending on the
  1787. command used) one higher or lower that the default priority."
  1788. :group 'org-priorities
  1789. :type 'boolean)
  1790. (defgroup org-time nil
  1791. "Options concerning time stamps and deadlines in Org-mode."
  1792. :tag "Org Time"
  1793. :group 'org)
  1794. (defcustom org-insert-labeled-timestamps-at-point nil
  1795. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1796. When nil, these labeled time stamps are forces into the second line of an
  1797. entry, just after the headline. When scheduling from the global TODO list,
  1798. the time stamp will always be forced into the second line."
  1799. :group 'org-time
  1800. :type 'boolean)
  1801. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1802. "Formats for `format-time-string' which are used for time stamps.
  1803. It is not recommended to change this constant.")
  1804. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1805. "Number of minutes to round time stamps to.
  1806. These are two values, the first applies when first creating a time stamp.
  1807. The second applies when changing it with the commands `S-up' and `S-down'.
  1808. When changing the time stamp, this means that it will change in steps
  1809. of N minutes, as given by the second value.
  1810. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1811. numbers should be factors of 60, so for example 5, 10, 15.
  1812. When this is larger than 1, you can still force an exact time-stamp by using
  1813. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1814. and by using a prefix arg to `S-up/down' to specify the exact number
  1815. of minutes to shift."
  1816. :group 'org-time
  1817. :get '(lambda (var) ; Make sure all entries have 5 elements
  1818. (if (integerp (default-value var))
  1819. (list (default-value var) 5)
  1820. (default-value var)))
  1821. :type '(list
  1822. (integer :tag "when inserting times")
  1823. (integer :tag "when modifying times")))
  1824. ;; Normalize old customizations of this variable.
  1825. (when (integerp org-time-stamp-rounding-minutes)
  1826. (setq org-time-stamp-rounding-minutes
  1827. (list org-time-stamp-rounding-minutes
  1828. org-time-stamp-rounding-minutes)))
  1829. (defcustom org-display-custom-times nil
  1830. "Non-nil means, overlay custom formats over all time stamps.
  1831. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1832. To turn this on on a per-file basis, insert anywhere in the file:
  1833. #+STARTUP: customtime"
  1834. :group 'org-time
  1835. :set 'set-default
  1836. :type 'sexp)
  1837. (make-variable-buffer-local 'org-display-custom-times)
  1838. (defcustom org-time-stamp-custom-formats
  1839. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1840. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1841. These are overlayed over the default ISO format if the variable
  1842. `org-display-custom-times' is set. Time like %H:%M should be at the
  1843. end of the second format. The custom formats are also honored by export
  1844. commands, if custom time display is turned on at the time of export."
  1845. :group 'org-time
  1846. :type 'sexp)
  1847. (defun org-time-stamp-format (&optional long inactive)
  1848. "Get the right format for a time string."
  1849. (let ((f (if long (cdr org-time-stamp-formats)
  1850. (car org-time-stamp-formats))))
  1851. (if inactive
  1852. (concat "[" (substring f 1 -1) "]")
  1853. f)))
  1854. (defcustom org-time-clocksum-format "%d:%02d"
  1855. "The format string used when creating CLOCKSUM lines, or when
  1856. org-mode generates a time duration."
  1857. :group 'org-time
  1858. :type 'string)
  1859. (defcustom org-deadline-warning-days 14
  1860. "No. of days before expiration during which a deadline becomes active.
  1861. This variable governs the display in sparse trees and in the agenda.
  1862. When 0 or negative, it means use this number (the absolute value of it)
  1863. even if a deadline has a different individual lead time specified.
  1864. Custom commands can set this variable in the options section."
  1865. :group 'org-time
  1866. :group 'org-agenda-daily/weekly
  1867. :type 'integer)
  1868. (defcustom org-read-date-prefer-future t
  1869. "Non-nil means, assume future for incomplete date input from user.
  1870. This affects the following situations:
  1871. 1. The user gives a day, but no month.
  1872. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1873. read this as the third of *next* month. However, if you enter \"17\",
  1874. it will be considered as *this* month.
  1875. 2. The user gives a month but not a year.
  1876. For example, if it is april and you enter \"feb 2\", this will be read
  1877. as feb 2, *next* year. \"May 5\", however, will be this year.
  1878. Currently this does not work for ISO week specifications.
  1879. When this option is nil, the current month and year will always be used
  1880. as defaults."
  1881. :group 'org-time
  1882. :type 'boolean)
  1883. (defcustom org-read-date-display-live t
  1884. "Non-nil means, display current interpretation of date prompt live.
  1885. This display will be in an overlay, in the minibuffer."
  1886. :group 'org-time
  1887. :type 'boolean)
  1888. (defcustom org-read-date-popup-calendar t
  1889. "Non-nil means, pop up a calendar when prompting for a date.
  1890. In the calendar, the date can be selected with mouse-1. However, the
  1891. minibuffer will also be active, and you can simply enter the date as well.
  1892. When nil, only the minibuffer will be available."
  1893. :group 'org-time
  1894. :type 'boolean)
  1895. (if (fboundp 'defvaralias)
  1896. (defvaralias 'org-popup-calendar-for-date-prompt
  1897. 'org-read-date-popup-calendar))
  1898. (defcustom org-read-date-minibuffer-setup-hook nil
  1899. "Hook to be used to set up keys for the date/time interface.
  1900. Add key definitions to `minibuffer-local-map', which will be a temporary
  1901. copy."
  1902. :group 'org-time
  1903. :type 'hook)
  1904. (defcustom org-extend-today-until 0
  1905. "The hour when your day really ends. Must be an integer.
  1906. This has influence for the following applications:
  1907. - When switching the agenda to \"today\". It it is still earlier than
  1908. the time given here, the day recognized as TODAY is actually yesterday.
  1909. - When a date is read from the user and it is still before the time given
  1910. here, the current date and time will be assumed to be yesterday, 23:59.
  1911. Also, timestamps inserted in remember templates follow this rule.
  1912. IMPORTANT: This is a feature whose implementation is and likely will
  1913. remain incomplete. Really, it is only here because past midnight seems to
  1914. be the favorite working time of John Wiegley :-)"
  1915. :group 'org-time
  1916. :type 'integer)
  1917. (defcustom org-edit-timestamp-down-means-later nil
  1918. "Non-nil means, S-down will increase the time in a time stamp.
  1919. When nil, S-up will increase."
  1920. :group 'org-time
  1921. :type 'boolean)
  1922. (defcustom org-calendar-follow-timestamp-change t
  1923. "Non-nil means, make the calendar window follow timestamp changes.
  1924. When a timestamp is modified and the calendar window is visible, it will be
  1925. moved to the new date."
  1926. :group 'org-time
  1927. :type 'boolean)
  1928. (defgroup org-tags nil
  1929. "Options concerning tags in Org-mode."
  1930. :tag "Org Tags"
  1931. :group 'org)
  1932. (defcustom org-tag-alist nil
  1933. "List of tags allowed in Org-mode files.
  1934. When this list is nil, Org-mode will base TAG input on what is already in the
  1935. buffer.
  1936. The value of this variable is an alist, the car of each entry must be a
  1937. keyword as a string, the cdr may be a character that is used to select
  1938. that tag through the fast-tag-selection interface.
  1939. See the manual for details."
  1940. :group 'org-tags
  1941. :type '(repeat
  1942. (choice
  1943. (cons (string :tag "Tag name")
  1944. (character :tag "Access char"))
  1945. (const :tag "Start radio group" (:startgroup))
  1946. (const :tag "End radio group" (:endgroup))
  1947. (const :tag "New line" (:newline)))))
  1948. (defcustom org-tag-persistent-alist nil
  1949. "List of tags that will always appear in all Org-mode files.
  1950. This is in addition to any in buffer settings or customizations
  1951. of `org-tag-alist'.
  1952. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1953. The value of this variable is an alist, the car of each entry must be a
  1954. keyword as a string, the cdr may be a character that is used to select
  1955. that tag through the fast-tag-selection interface.
  1956. See the manual for details.
  1957. To disable these tags on a per-file basis, insert anywhere in the file:
  1958. #+STARTUP: noptag"
  1959. :group 'org-tags
  1960. :type '(repeat
  1961. (choice
  1962. (cons (string :tag "Tag name")
  1963. (character :tag "Access char"))
  1964. (const :tag "Start radio group" (:startgroup))
  1965. (const :tag "End radio group" (:endgroup))
  1966. (const :tag "New line" (:newline)))))
  1967. (defvar org-file-tags nil
  1968. "List of tags that can be inherited by all entries in the file.
  1969. The tags will be inherited if the variable `org-use-tag-inheritance'
  1970. says they should be.
  1971. This variable is populated from #+TAG lines.")
  1972. (defcustom org-use-fast-tag-selection 'auto
  1973. "Non-nil means, use fast tag selection scheme.
  1974. This is a special interface to select and deselect tags with single keys.
  1975. When nil, fast selection is never used.
  1976. When the symbol `auto', fast selection is used if and only if selection
  1977. characters for tags have been configured, either through the variable
  1978. `org-tag-alist' or through a #+TAGS line in the buffer.
  1979. When t, fast selection is always used and selection keys are assigned
  1980. automatically if necessary."
  1981. :group 'org-tags
  1982. :type '(choice
  1983. (const :tag "Always" t)
  1984. (const :tag "Never" nil)
  1985. (const :tag "When selection characters are configured" 'auto)))
  1986. (defcustom org-fast-tag-selection-single-key nil
  1987. "Non-nil means, fast tag selection exits after first change.
  1988. When nil, you have to press RET to exit it.
  1989. During fast tag selection, you can toggle this flag with `C-c'.
  1990. This variable can also have the value `expert'. In this case, the window
  1991. displaying the tags menu is not even shown, until you press C-c again."
  1992. :group 'org-tags
  1993. :type '(choice
  1994. (const :tag "No" nil)
  1995. (const :tag "Yes" t)
  1996. (const :tag "Expert" expert)))
  1997. (defvar org-fast-tag-selection-include-todo nil
  1998. "Non-nil means, fast tags selection interface will also offer TODO states.
  1999. This is an undocumented feature, you should not rely on it.")
  2000. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2001. "The column to which tags should be indented in a headline.
  2002. If this number is positive, it specifies the column. If it is negative,
  2003. it means that the tags should be flushright to that column. For example,
  2004. -80 works well for a normal 80 character screen."
  2005. :group 'org-tags
  2006. :type 'integer)
  2007. (defcustom org-auto-align-tags t
  2008. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2009. These operations change the length of a headline and therefore shift
  2010. the tags around. With this options turned on, after each such operation
  2011. the tags are again aligned to `org-tags-column'."
  2012. :group 'org-tags
  2013. :type 'boolean)
  2014. (defcustom org-use-tag-inheritance t
  2015. "Non-nil means, tags in levels apply also for sublevels.
  2016. When nil, only the tags directly given in a specific line apply there.
  2017. This may also be a list of tags that should be inherited, or a regexp that
  2018. matches tags that should be inherited. Additional control is possible
  2019. with the variable `org-tags-exclude-from-inheritance' which gives an
  2020. explicit list of tags to be excluded from inheritance., even if the value of
  2021. `org-use-tag-inheritance' would select it for inheritance.
  2022. If this option is t, a match early-on in a tree can lead to a large
  2023. number of matches in the subtree when constructing the agenda or creating
  2024. a sparse tree. If you only want to see the first match in a tree during
  2025. a search, check out the variable `org-tags-match-list-sublevels'."
  2026. :group 'org-tags
  2027. :type '(choice
  2028. (const :tag "Not" nil)
  2029. (const :tag "Always" t)
  2030. (repeat :tag "Specific tags" (string :tag "Tag"))
  2031. (regexp :tag "Tags matched by regexp")))
  2032. (defcustom org-tags-exclude-from-inheritance nil
  2033. "List of tags that should never be inherited.
  2034. This is a way to exclude a few tags from inheritance. For way to do
  2035. the opposite, to actively allow inheritance for selected tags,
  2036. see the variable `org-use-tag-inheritance'."
  2037. :group 'org-tags
  2038. :type '(repeat (string :tag "Tag")))
  2039. (defun org-tag-inherit-p (tag)
  2040. "Check if TAG is one that should be inherited."
  2041. (cond
  2042. ((member tag org-tags-exclude-from-inheritance) nil)
  2043. ((eq org-use-tag-inheritance t) t)
  2044. ((not org-use-tag-inheritance) nil)
  2045. ((stringp org-use-tag-inheritance)
  2046. (string-match org-use-tag-inheritance tag))
  2047. ((listp org-use-tag-inheritance)
  2048. (member tag org-use-tag-inheritance))
  2049. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2050. (defcustom org-tags-match-list-sublevels t
  2051. "Non-nil means list also sublevels of headlines matching a search.
  2052. This variable applies to tags/property searches, and also to stuck
  2053. projects because this search is based on a tags match as well.
  2054. When set to the symbol `indented', sublevels are indented with
  2055. leading dots.
  2056. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2057. the sublevels of a headline matching a tag search often also match
  2058. the same search. Listing all of them can create very long lists.
  2059. Setting this variable to nil causes subtrees of a match to be skipped.
  2060. This variable is semi-obsolete and probably should always be true. It
  2061. is better to limit inheritance to certain tags using the variables
  2062. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2063. :group 'org-tags
  2064. :type '(choice
  2065. (const :tag "No, don't list them" nil)
  2066. (const :tag "Yes, do list them" t)
  2067. (const :tag "List them, indented with leading dots" indented)))
  2068. (defcustom org-tags-sort-function nil
  2069. "When set, tags are sorted using this function as a comparator"
  2070. :group 'org-tags
  2071. :type '(choice
  2072. (const :tag "No sorting" nil)
  2073. (const :tag "Alphabetical" string<)
  2074. (const :tag "Reverse alphabetical" string>)
  2075. (function :tag "Custom function" nil)))
  2076. (defvar org-tags-history nil
  2077. "History of minibuffer reads for tags.")
  2078. (defvar org-last-tags-completion-table nil
  2079. "The last used completion table for tags.")
  2080. (defvar org-after-tags-change-hook nil
  2081. "Hook that is run after the tags in a line have changed.")
  2082. (defgroup org-properties nil
  2083. "Options concerning properties in Org-mode."
  2084. :tag "Org Properties"
  2085. :group 'org)
  2086. (defcustom org-property-format "%-10s %s"
  2087. "How property key/value pairs should be formatted by `indent-line'.
  2088. When `indent-line' hits a property definition, it will format the line
  2089. according to this format, mainly to make sure that the values are
  2090. lined-up with respect to each other."
  2091. :group 'org-properties
  2092. :type 'string)
  2093. (defcustom org-use-property-inheritance nil
  2094. "Non-nil means, properties apply also for sublevels.
  2095. This setting is chiefly used during property searches. Turning it on can
  2096. cause significant overhead when doing a search, which is why it is not
  2097. on by default.
  2098. When nil, only the properties directly given in the current entry count.
  2099. When t, every property is inherited. The value may also be a list of
  2100. properties that should have inheritance, or a regular expression matching
  2101. properties that should be inherited.
  2102. However, note that some special properties use inheritance under special
  2103. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2104. and the properties ending in \"_ALL\" when they are used as descriptor
  2105. for valid values of a property.
  2106. Note for programmers:
  2107. When querying an entry with `org-entry-get', you can control if inheritance
  2108. should be used. By default, `org-entry-get' looks only at the local
  2109. properties. You can request inheritance by setting the inherit argument
  2110. to t (to force inheritance) or to `selective' (to respect the setting
  2111. in this variable)."
  2112. :group 'org-properties
  2113. :type '(choice
  2114. (const :tag "Not" nil)
  2115. (const :tag "Always" t)
  2116. (repeat :tag "Specific properties" (string :tag "Property"))
  2117. (regexp :tag "Properties matched by regexp")))
  2118. (defun org-property-inherit-p (property)
  2119. "Check if PROPERTY is one that should be inherited."
  2120. (cond
  2121. ((eq org-use-property-inheritance t) t)
  2122. ((not org-use-property-inheritance) nil)
  2123. ((stringp org-use-property-inheritance)
  2124. (string-match org-use-property-inheritance property))
  2125. ((listp org-use-property-inheritance)
  2126. (member property org-use-property-inheritance))
  2127. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2128. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2129. "The default column format, if no other format has been defined.
  2130. This variable can be set on the per-file basis by inserting a line
  2131. #+COLUMNS: %25ITEM ....."
  2132. :group 'org-properties
  2133. :type 'string)
  2134. (defcustom org-columns-ellipses ".."
  2135. "The ellipses to be used when a field in column view is truncated.
  2136. When this is the empty string, as many characters as possible are shown,
  2137. but then there will be no visual indication that the field has been truncated.
  2138. When this is a string of length N, the last N characters of a truncated
  2139. field are replaced by this string. If the column is narrower than the
  2140. ellipses string, only part of the ellipses string will be shown."
  2141. :group 'org-properties
  2142. :type 'string)
  2143. (defcustom org-columns-modify-value-for-display-function nil
  2144. "Function that modifies values for display in column view.
  2145. For example, it can be used to cut out a certain part from a time stamp.
  2146. The function must take 2 arguments:
  2147. column-title The title of the column (*not* the property name)
  2148. value The value that should be modified.
  2149. The function should return the value that should be displayed,
  2150. or nil if the normal value should be used."
  2151. :group 'org-properties
  2152. :type 'function)
  2153. (defcustom org-effort-property "Effort"
  2154. "The property that is being used to keep track of effort estimates.
  2155. Effort estimates given in this property need to have the format H:MM."
  2156. :group 'org-properties
  2157. :group 'org-progress
  2158. :type '(string :tag "Property"))
  2159. (defconst org-global-properties-fixed
  2160. '(("VISIBILITY_ALL" . "folded children content all")
  2161. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2162. "List of property/value pairs that can be inherited by any entry.
  2163. These are fixed values, for the preset properties. The user variable
  2164. that can be used to add to this list is `org-global-properties'.
  2165. The entries in this list are cons cells where the car is a property
  2166. name and cdr is a string with the value. If the value represents
  2167. multiple items like an \"_ALL\" property, separate the items by
  2168. spaces.")
  2169. (defcustom org-global-properties nil
  2170. "List of property/value pairs that can be inherited by any entry.
  2171. This list will be combined with the constant `org-global-properties-fixed'.
  2172. The entries in this list are cons cells where the car is a property
  2173. name and cdr is a string with the value.
  2174. You can set buffer-local values for the same purpose in the variable
  2175. `org-file-properties' this by adding lines like
  2176. #+PROPERTY: NAME VALUE"
  2177. :group 'org-properties
  2178. :type '(repeat
  2179. (cons (string :tag "Property")
  2180. (string :tag "Value"))))
  2181. (defvar org-file-properties nil
  2182. "List of property/value pairs that can be inherited by any entry.
  2183. Valid for the current buffer.
  2184. This variable is populated from #+PROPERTY lines.")
  2185. (make-variable-buffer-local 'org-file-properties)
  2186. (defgroup org-agenda nil
  2187. "Options concerning agenda views in Org-mode."
  2188. :tag "Org Agenda"
  2189. :group 'org)
  2190. (defvar org-category nil
  2191. "Variable used by org files to set a category for agenda display.
  2192. Such files should use a file variable to set it, for example
  2193. # -*- mode: org; org-category: \"ELisp\"
  2194. or contain a special line
  2195. #+CATEGORY: ELisp
  2196. If the file does not specify a category, then file's base name
  2197. is used instead.")
  2198. (make-variable-buffer-local 'org-category)
  2199. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2200. (defcustom org-agenda-files nil
  2201. "The files to be used for agenda display.
  2202. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2203. \\[org-remove-file]. You can also use customize to edit the list.
  2204. If an entry is a directory, all files in that directory that are matched by
  2205. `org-agenda-file-regexp' will be part of the file list.
  2206. If the value of the variable is not a list but a single file name, then
  2207. the list of agenda files is actually stored and maintained in that file, one
  2208. agenda file per line."
  2209. :group 'org-agenda
  2210. :type '(choice
  2211. (repeat :tag "List of files and directories" file)
  2212. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2213. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2214. "Regular expression to match files for `org-agenda-files'.
  2215. If any element in the list in that variable contains a directory instead
  2216. of a normal file, all files in that directory that are matched by this
  2217. regular expression will be included."
  2218. :group 'org-agenda
  2219. :type 'regexp)
  2220. (defcustom org-agenda-text-search-extra-files nil
  2221. "List of extra files to be searched by text search commands.
  2222. These files will be search in addition to the agenda files by the
  2223. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2224. Note that these files will only be searched for text search commands,
  2225. not for the other agenda views like todo lists, tag searches or the weekly
  2226. agenda. This variable is intended to list notes and possibly archive files
  2227. that should also be searched by these two commands.
  2228. In fact, if the first element in the list is the symbol `agenda-archives',
  2229. than all archive files of all agenda files will be added to the search
  2230. scope."
  2231. :group 'org-agenda
  2232. :type '(set :greedy t
  2233. (const :tag "Agenda Archives" agenda-archives)
  2234. (repeat :inline t (file))))
  2235. (if (fboundp 'defvaralias)
  2236. (defvaralias 'org-agenda-multi-occur-extra-files
  2237. 'org-agenda-text-search-extra-files))
  2238. (defcustom org-agenda-skip-unavailable-files nil
  2239. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2240. A nil value means to remove them, after a query, from the list."
  2241. :group 'org-agenda
  2242. :type 'boolean)
  2243. (defcustom org-calendar-to-agenda-key [?c]
  2244. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2245. The command `org-calendar-goto-agenda' will be bound to this key. The
  2246. default is the character `c' because then `c' can be used to switch back and
  2247. forth between agenda and calendar."
  2248. :group 'org-agenda
  2249. :type 'sexp)
  2250. (defcustom org-calendar-agenda-action-key [?k]
  2251. "The key to be installed in `calendar-mode-map' for agenda-action.
  2252. The command `org-agenda-action' will be bound to this key. The
  2253. default is the character `k' because we use the same key in the agenda."
  2254. :group 'org-agenda
  2255. :type 'sexp)
  2256. (eval-after-load "calendar"
  2257. '(progn
  2258. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2259. 'org-calendar-goto-agenda)
  2260. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2261. 'org-agenda-action)))
  2262. (defgroup org-latex nil
  2263. "Options for embedding LaTeX code into Org-mode."
  2264. :tag "Org LaTeX"
  2265. :group 'org)
  2266. (defcustom org-format-latex-options
  2267. '(:foreground default :background default :scale 1.0
  2268. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2269. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2270. "Options for creating images from LaTeX fragments.
  2271. This is a property list with the following properties:
  2272. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2273. `default' means use the foreground of the default face.
  2274. :background the background color, or \"Transparent\".
  2275. `default' means use the background of the default face.
  2276. :scale a scaling factor for the size of the images.
  2277. :html-foreground, :html-background, :html-scale
  2278. the same numbers for HTML export.
  2279. :matchers a list indicating which matchers should be used to
  2280. find LaTeX fragments. Valid members of this list are:
  2281. \"begin\" find environments
  2282. \"$1\" find single characters surrounded by $.$
  2283. \"$\" find math expressions surrounded by $...$
  2284. \"$$\" find math expressions surrounded by $$....$$
  2285. \"\\(\" find math expressions surrounded by \\(...\\)
  2286. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2287. :group 'org-latex
  2288. :type 'plist)
  2289. (defcustom org-format-latex-header "\\documentclass{article}
  2290. \\usepackage{fullpage} % do not remove
  2291. \\usepackage{amssymb}
  2292. \\usepackage[usenames]{color}
  2293. \\usepackage{amsmath}
  2294. \\usepackage{latexsym}
  2295. \\usepackage[mathscr]{eucal}
  2296. \\pagestyle{empty} % do not remove"
  2297. "The document header used for processing LaTeX fragments."
  2298. :group 'org-latex
  2299. :type 'string)
  2300. (defgroup org-font-lock nil
  2301. "Font-lock settings for highlighting in Org-mode."
  2302. :tag "Org Font Lock"
  2303. :group 'org)
  2304. (defcustom org-level-color-stars-only nil
  2305. "Non-nil means fontify only the stars in each headline.
  2306. When nil, the entire headline is fontified.
  2307. Changing it requires restart of `font-lock-mode' to become effective
  2308. also in regions already fontified."
  2309. :group 'org-font-lock
  2310. :type 'boolean)
  2311. (defcustom org-hide-leading-stars nil
  2312. "Non-nil means, hide the first N-1 stars in a headline.
  2313. This works by using the face `org-hide' for these stars. This
  2314. face is white for a light background, and black for a dark
  2315. background. You may have to customize the face `org-hide' to
  2316. make this work.
  2317. Changing it requires restart of `font-lock-mode' to become effective
  2318. also in regions already fontified.
  2319. You may also set this on a per-file basis by adding one of the following
  2320. lines to the buffer:
  2321. #+STARTUP: hidestars
  2322. #+STARTUP: showstars"
  2323. :group 'org-font-lock
  2324. :type 'boolean)
  2325. (defcustom org-fontify-done-headline nil
  2326. "Non-nil means, change the face of a headline if it is marked DONE.
  2327. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2328. When this is non-nil, the headline after the keyword is set to the
  2329. `org-headline-done' as an additional indication."
  2330. :group 'org-font-lock
  2331. :type 'boolean)
  2332. (defcustom org-fontify-emphasized-text t
  2333. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2334. Changing this variable requires a restart of Emacs to take effect."
  2335. :group 'org-font-lock
  2336. :type 'boolean)
  2337. (defcustom org-fontify-whole-heading-line nil
  2338. "Non-nil means fontify the whole line for headings.
  2339. This is useful when setting a background color for the
  2340. org-leve-* faces."
  2341. :group 'org-font-lock
  2342. :type 'boolean)
  2343. (defcustom org-highlight-latex-fragments-and-specials nil
  2344. "Non-nil means, fontify what is treated specially by the exporters."
  2345. :group 'org-font-lock
  2346. :type 'boolean)
  2347. (defcustom org-hide-emphasis-markers nil
  2348. "Non-nil mean font-lock should hide the emphasis marker characters."
  2349. :group 'org-font-lock
  2350. :type 'boolean)
  2351. (defvar org-emph-re nil
  2352. "Regular expression for matching emphasis.")
  2353. (defvar org-verbatim-re nil
  2354. "Regular expression for matching verbatim text.")
  2355. (defvar org-emphasis-regexp-components) ; defined just below
  2356. (defvar org-emphasis-alist) ; defined just below
  2357. (defun org-set-emph-re (var val)
  2358. "Set variable and compute the emphasis regular expression."
  2359. (set var val)
  2360. (when (and (boundp 'org-emphasis-alist)
  2361. (boundp 'org-emphasis-regexp-components)
  2362. org-emphasis-alist org-emphasis-regexp-components)
  2363. (let* ((e org-emphasis-regexp-components)
  2364. (pre (car e))
  2365. (post (nth 1 e))
  2366. (border (nth 2 e))
  2367. (body (nth 3 e))
  2368. (nl (nth 4 e))
  2369. (body1 (concat body "*?"))
  2370. (markers (mapconcat 'car org-emphasis-alist ""))
  2371. (vmarkers (mapconcat
  2372. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2373. org-emphasis-alist "")))
  2374. ;; make sure special characters appear at the right position in the class
  2375. (if (string-match "\\^" markers)
  2376. (setq markers (concat (replace-match "" t t markers) "^")))
  2377. (if (string-match "-" markers)
  2378. (setq markers (concat (replace-match "" t t markers) "-")))
  2379. (if (string-match "\\^" vmarkers)
  2380. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2381. (if (string-match "-" vmarkers)
  2382. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2383. (if (> nl 0)
  2384. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2385. (int-to-string nl) "\\}")))
  2386. ;; Make the regexp
  2387. (setq org-emph-re
  2388. (concat "\\([" pre "]\\|^\\)"
  2389. "\\("
  2390. "\\([" markers "]\\)"
  2391. "\\("
  2392. "[^" border "]\\|"
  2393. "[^" border "]"
  2394. body1
  2395. "[^" border "]"
  2396. "\\)"
  2397. "\\3\\)"
  2398. "\\([" post "]\\|$\\)"))
  2399. (setq org-verbatim-re
  2400. (concat "\\([" pre "]\\|^\\)"
  2401. "\\("
  2402. "\\([" vmarkers "]\\)"
  2403. "\\("
  2404. "[^" border "]\\|"
  2405. "[^" border "]"
  2406. body1
  2407. "[^" border "]"
  2408. "\\)"
  2409. "\\3\\)"
  2410. "\\([" post "]\\|$\\)")))))
  2411. (defcustom org-emphasis-regexp-components
  2412. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2413. "Components used to build the regular expression for emphasis.
  2414. This is a list with 6 entries. Terminology: In an emphasis string
  2415. like \" *strong word* \", we call the initial space PREMATCH, the final
  2416. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2417. and \"trong wor\" is the body. The different components in this variable
  2418. specify what is allowed/forbidden in each part:
  2419. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2420. post Chars allowed as postmatch. End of line will be allowed too.
  2421. border The chars *forbidden* as border characters.
  2422. body-regexp A regexp like \".\" to match a body character. Don't use
  2423. non-shy groups here, and don't allow newline here.
  2424. newline The maximum number of newlines allowed in an emphasis exp.
  2425. Use customize to modify this, or restart Emacs after changing it."
  2426. :group 'org-font-lock
  2427. :set 'org-set-emph-re
  2428. :type '(list
  2429. (sexp :tag "Allowed chars in pre ")
  2430. (sexp :tag "Allowed chars in post ")
  2431. (sexp :tag "Forbidden chars in border ")
  2432. (sexp :tag "Regexp for body ")
  2433. (integer :tag "number of newlines allowed")
  2434. (option (boolean :tag "Please ignore this button"))))
  2435. (defcustom org-emphasis-alist
  2436. `(("*" bold "<b>" "</b>")
  2437. ("/" italic "<i>" "</i>")
  2438. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2439. ("=" org-code "<code>" "</code>" verbatim)
  2440. ("~" org-verbatim "<code>" "</code>" verbatim)
  2441. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2442. "<del>" "</del>")
  2443. )
  2444. "Special syntax for emphasized text.
  2445. Text starting and ending with a special character will be emphasized, for
  2446. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2447. characters, the face to be used by font-lock for highlighting in Org-mode
  2448. Emacs buffers, and the HTML tags to be used for this.
  2449. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2450. Use customize to modify this, or restart Emacs after changing it."
  2451. :group 'org-font-lock
  2452. :set 'org-set-emph-re
  2453. :type '(repeat
  2454. (list
  2455. (string :tag "Marker character")
  2456. (choice
  2457. (face :tag "Font-lock-face")
  2458. (plist :tag "Face property list"))
  2459. (string :tag "HTML start tag")
  2460. (string :tag "HTML end tag")
  2461. (option (const verbatim)))))
  2462. (defvar org-protecting-blocks
  2463. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2464. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2465. This is needed for font-lock setup.")
  2466. ;;; Miscellaneous options
  2467. (defgroup org-completion nil
  2468. "Completion in Org-mode."
  2469. :tag "Org Completion"
  2470. :group 'org)
  2471. (defcustom org-completion-use-ido nil
  2472. "Non-nil means, use ido completion wherever possible.
  2473. Note that `ido-mode' must be active for this variable to be relevant.
  2474. If you decide to turn this variable on, you might well want to turn off
  2475. `org-outline-path-complete-in-steps'."
  2476. :group 'org-completion
  2477. :type 'boolean)
  2478. (defcustom org-completion-fallback-command 'hippie-expand
  2479. "The expansion command called by \\[org-complete] in normal context.
  2480. Normal means, no org-mode-specific context."
  2481. :group 'org-completion
  2482. :type 'function)
  2483. ;;; Functions and variables from ther packages
  2484. ;; Declared here to avoid compiler warnings
  2485. ;; XEmacs only
  2486. (defvar outline-mode-menu-heading)
  2487. (defvar outline-mode-menu-show)
  2488. (defvar outline-mode-menu-hide)
  2489. (defvar zmacs-regions) ; XEmacs regions
  2490. ;; Emacs only
  2491. (defvar mark-active)
  2492. ;; Various packages
  2493. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2494. (declare-function calendar-forward-day "cal-move" (arg))
  2495. (declare-function calendar-goto-date "cal-move" (date))
  2496. (declare-function calendar-goto-today "cal-move" ())
  2497. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2498. (defvar calc-embedded-close-formula)
  2499. (defvar calc-embedded-open-formula)
  2500. (declare-function cdlatex-tab "ext:cdlatex" ())
  2501. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2502. (defvar font-lock-unfontify-region-function)
  2503. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2504. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2505. (defvar iswitchb-temp-buflist)
  2506. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2507. (defvar org-agenda-tags-todo-honor-ignore-options)
  2508. (declare-function org-agenda-skip "org-agenda" ())
  2509. (declare-function org-format-agenda-item "org-agenda"
  2510. (extra txt &optional category tags dotime noprefix remove-re))
  2511. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2512. (declare-function org-agenda-change-all-lines "org-agenda"
  2513. (newhead hdmarker &optional fixface just-this))
  2514. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2515. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2516. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2517. (beg end))
  2518. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2519. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2520. "org-agenda" (&optional end))
  2521. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2522. (declare-function org-indent-mode "org-indent" (arg))
  2523. (declare-function parse-time-string "parse-time" (string))
  2524. (declare-function remember "remember" (&optional initial))
  2525. (declare-function remember-buffer-desc "remember" ())
  2526. (declare-function remember-finalize "remember" ())
  2527. (defvar remember-save-after-remembering)
  2528. (defvar remember-data-file)
  2529. (defvar remember-register)
  2530. (defvar remember-buffer)
  2531. (defvar remember-handler-functions)
  2532. (defvar remember-annotation-functions)
  2533. (defvar texmathp-why)
  2534. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2535. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2536. (defvar w3m-current-url)
  2537. (defvar w3m-current-title)
  2538. (defvar org-latex-regexps)
  2539. ;;; Autoload and prepare some org modules
  2540. ;; Some table stuff that needs to be defined here, because it is used
  2541. ;; by the functions setting up org-mode or checking for table context.
  2542. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2543. "Detects an org-type or table-type table.")
  2544. (defconst org-table-line-regexp "^[ \t]*|"
  2545. "Detects an org-type table line.")
  2546. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2547. "Detects an org-type table line.")
  2548. (defconst org-table-hline-regexp "^[ \t]*|-"
  2549. "Detects an org-type table hline.")
  2550. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2551. "Detects a table-type table hline.")
  2552. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2553. "Searching from within a table (any type) this finds the first line
  2554. outside the table.")
  2555. ;; Autoload the functions in org-table.el that are needed by functions here.
  2556. (eval-and-compile
  2557. (org-autoload "org-table"
  2558. '(org-table-align org-table-begin org-table-blank-field
  2559. org-table-convert org-table-convert-region org-table-copy-down
  2560. org-table-copy-region org-table-create
  2561. org-table-create-or-convert-from-region
  2562. org-table-create-with-table.el org-table-current-dline
  2563. org-table-cut-region org-table-delete-column org-table-edit-field
  2564. org-table-edit-formulas org-table-end org-table-eval-formula
  2565. org-table-export org-table-field-info
  2566. org-table-get-stored-formulas org-table-goto-column
  2567. org-table-hline-and-move org-table-import org-table-insert-column
  2568. org-table-insert-hline org-table-insert-row org-table-iterate
  2569. org-table-justify-field-maybe org-table-kill-row
  2570. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2571. org-table-move-column org-table-move-column-left
  2572. org-table-move-column-right org-table-move-row
  2573. org-table-move-row-down org-table-move-row-up
  2574. org-table-next-field org-table-next-row org-table-paste-rectangle
  2575. org-table-previous-field org-table-recalculate
  2576. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2577. org-table-toggle-coordinate-overlays
  2578. org-table-toggle-formula-debugger org-table-wrap-region
  2579. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2580. (defun org-at-table-p (&optional table-type)
  2581. "Return t if the cursor is inside an org-type table.
  2582. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2583. (if org-enable-table-editor
  2584. (save-excursion
  2585. (beginning-of-line 1)
  2586. (looking-at (if table-type org-table-any-line-regexp
  2587. org-table-line-regexp)))
  2588. nil))
  2589. (defsubst org-table-p () (org-at-table-p))
  2590. (defun org-at-table.el-p ()
  2591. "Return t if and only if we are at a table.el table."
  2592. (and (org-at-table-p 'any)
  2593. (save-excursion
  2594. (goto-char (org-table-begin 'any))
  2595. (looking-at org-table1-hline-regexp))))
  2596. (defun org-table-recognize-table.el ()
  2597. "If there is a table.el table nearby, recognize it and move into it."
  2598. (if org-table-tab-recognizes-table.el
  2599. (if (org-at-table.el-p)
  2600. (progn
  2601. (beginning-of-line 1)
  2602. (if (looking-at org-table-dataline-regexp)
  2603. nil
  2604. (if (looking-at org-table1-hline-regexp)
  2605. (progn
  2606. (beginning-of-line 2)
  2607. (if (looking-at org-table-any-border-regexp)
  2608. (beginning-of-line -1)))))
  2609. (if (re-search-forward "|" (org-table-end t) t)
  2610. (progn
  2611. (require 'table)
  2612. (if (table--at-cell-p (point))
  2613. t
  2614. (message "recognizing table.el table...")
  2615. (table-recognize-table)
  2616. (message "recognizing table.el table...done")))
  2617. (error "This should not happen..."))
  2618. t)
  2619. nil)
  2620. nil))
  2621. (defun org-at-table-hline-p ()
  2622. "Return t if the cursor is inside a hline in a table."
  2623. (if org-enable-table-editor
  2624. (save-excursion
  2625. (beginning-of-line 1)
  2626. (looking-at org-table-hline-regexp))
  2627. nil))
  2628. (defvar org-table-clean-did-remove-column nil)
  2629. (defun org-table-map-tables (function)
  2630. "Apply FUNCTION to the start of all tables in the buffer."
  2631. (save-excursion
  2632. (save-restriction
  2633. (widen)
  2634. (goto-char (point-min))
  2635. (while (re-search-forward org-table-any-line-regexp nil t)
  2636. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2637. (beginning-of-line 1)
  2638. (if (looking-at org-table-line-regexp)
  2639. (save-excursion (funcall function)))
  2640. (re-search-forward org-table-any-border-regexp nil 1))))
  2641. (message "Mapping tables: done"))
  2642. ;; Declare and autoload functions from org-exp.el & Co
  2643. (declare-function org-default-export-plist "org-exp")
  2644. (declare-function org-infile-export-plist "org-exp")
  2645. (declare-function org-get-current-options "org-exp")
  2646. (eval-and-compile
  2647. (org-autoload "org-exp"
  2648. '(org-export org-export-visible
  2649. org-insert-export-options-template
  2650. org-table-clean-before-export))
  2651. (org-autoload "org-ascii"
  2652. '(org-export-as-ascii org-export-ascii-preprocess
  2653. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2654. org-export-region-as-ascii))
  2655. (org-autoload "org-html"
  2656. '(org-export-as-html-and-open
  2657. org-export-as-html-batch org-export-as-html-to-buffer
  2658. org-replace-region-by-html org-export-region-as-html
  2659. org-export-as-html))
  2660. (org-autoload "org-icalendar"
  2661. '(org-export-icalendar-this-file
  2662. org-export-icalendar-all-agenda-files
  2663. org-export-icalendar-combine-agenda-files))
  2664. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2665. ;; Declare and autoload functions from org-agenda.el
  2666. (eval-and-compile
  2667. (org-autoload "org-agenda"
  2668. '(org-agenda org-agenda-list org-search-view
  2669. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2670. org-diary org-agenda-to-appt
  2671. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2672. ;; Autoload org-remember
  2673. (eval-and-compile
  2674. (org-autoload "org-remember"
  2675. '(org-remember-insinuate org-remember-annotation
  2676. org-remember-apply-template org-remember org-remember-handler)))
  2677. ;; Autoload org-clock.el
  2678. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2679. (beg end))
  2680. (declare-function org-clock-update-mode-line "org-clock" ())
  2681. (defvar org-clock-start-time)
  2682. (defvar org-clock-marker (make-marker)
  2683. "Marker recording the last clock-in.")
  2684. (defun org-clock-is-active ()
  2685. "Return non-nil if clock is currently running.
  2686. The return value is actually the clock marker."
  2687. (marker-buffer org-clock-marker))
  2688. (eval-and-compile
  2689. (org-autoload
  2690. "org-clock"
  2691. '(org-clock-in org-clock-out org-clock-cancel
  2692. org-clock-goto org-clock-sum org-clock-display
  2693. org-clock-remove-overlays org-clock-report
  2694. org-clocktable-shift org-dblock-write:clocktable
  2695. org-get-clocktable)))
  2696. (defun org-clock-update-time-maybe ()
  2697. "If this is a CLOCK line, update it and return t.
  2698. Otherwise, return nil."
  2699. (interactive)
  2700. (save-excursion
  2701. (beginning-of-line 1)
  2702. (skip-chars-forward " \t")
  2703. (when (looking-at org-clock-string)
  2704. (let ((re (concat "[ \t]*" org-clock-string
  2705. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2706. "\\([ \t]*=>.*\\)?\\)?"))
  2707. ts te h m s neg)
  2708. (cond
  2709. ((not (looking-at re))
  2710. nil)
  2711. ((not (match-end 2))
  2712. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2713. (> org-clock-marker (point))
  2714. (<= org-clock-marker (point-at-eol)))
  2715. ;; The clock is running here
  2716. (setq org-clock-start-time
  2717. (apply 'encode-time
  2718. (org-parse-time-string (match-string 1))))
  2719. (org-clock-update-mode-line)))
  2720. (t
  2721. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2722. (end-of-line 1)
  2723. (setq ts (match-string 1)
  2724. te (match-string 3))
  2725. (setq s (- (time-to-seconds
  2726. (apply 'encode-time (org-parse-time-string te)))
  2727. (time-to-seconds
  2728. (apply 'encode-time (org-parse-time-string ts))))
  2729. neg (< s 0)
  2730. s (abs s)
  2731. h (floor (/ s 3600))
  2732. s (- s (* 3600 h))
  2733. m (floor (/ s 60))
  2734. s (- s (* 60 s)))
  2735. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2736. t))))))
  2737. (defun org-check-running-clock ()
  2738. "Check if the current buffer contains the running clock.
  2739. If yes, offer to stop it and to save the buffer with the changes."
  2740. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2741. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2742. (buffer-name))))
  2743. (org-clock-out)
  2744. (when (y-or-n-p "Save changed buffer?")
  2745. (save-buffer))))
  2746. (defun org-clocktable-try-shift (dir n)
  2747. "Check if this line starts a clock table, if yes, shift the time block."
  2748. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2749. (org-clocktable-shift dir n)))
  2750. ;; Autoload org-timer.el
  2751. (eval-and-compile
  2752. (org-autoload
  2753. "org-timer"
  2754. '(org-timer-start org-timer org-timer-item
  2755. org-timer-change-times-in-region
  2756. org-timer-set-timer
  2757. org-timer-reset-timers
  2758. org-timer-show-remaining-time)))
  2759. ;; Autoload org-feed.el
  2760. (eval-and-compile
  2761. (org-autoload
  2762. "org-feed"
  2763. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2764. ;; Autoload org-indent.el
  2765. (eval-and-compile
  2766. (org-autoload
  2767. "org-indent"
  2768. '(org-indent-mode)))
  2769. ;; Autoload archiving code
  2770. ;; The stuff that is needed for cycling and tags has to be defined here.
  2771. (defgroup org-archive nil
  2772. "Options concerning archiving in Org-mode."
  2773. :tag "Org Archive"
  2774. :group 'org-structure)
  2775. (defcustom org-archive-location "%s_archive::"
  2776. "The location where subtrees should be archived.
  2777. The value of this variable is a string, consisting of two parts,
  2778. separated by a double-colon. The first part is a filename and
  2779. the second part is a headline.
  2780. When the filename is omitted, archiving happens in the same file.
  2781. %s in the filename will be replaced by the current file
  2782. name (without the directory part). Archiving to a different file
  2783. is useful to keep archived entries from contributing to the
  2784. Org-mode Agenda.
  2785. The archived entries will be filed as subtrees of the specified
  2786. headline. When the headline is omitted, the subtrees are simply
  2787. filed away at the end of the file, as top-level entries. Also in
  2788. the heading you can use %s to represent the file name, this can be
  2789. useful when using the same archive for a number of different files.
  2790. Here are a few examples:
  2791. \"%s_archive::\"
  2792. If the current file is Projects.org, archive in file
  2793. Projects.org_archive, as top-level trees. This is the default.
  2794. \"::* Archived Tasks\"
  2795. Archive in the current file, under the top-level headline
  2796. \"* Archived Tasks\".
  2797. \"~/org/archive.org::\"
  2798. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2799. \"~/org/archive.org::From %s\"
  2800. Archive in file ~/org/archive.org (absolute path), und headlines
  2801. \"From FILENAME\" where file name is the current file name.
  2802. \"basement::** Finished Tasks\"
  2803. Archive in file ./basement (relative path), as level 3 trees
  2804. below the level 2 heading \"** Finished Tasks\".
  2805. You may set this option on a per-file basis by adding to the buffer a
  2806. line like
  2807. #+ARCHIVE: basement::** Finished Tasks
  2808. You may also define it locally for a subtree by setting an ARCHIVE property
  2809. in the entry. If such a property is found in an entry, or anywhere up
  2810. the hierarchy, it will be used."
  2811. :group 'org-archive
  2812. :type 'string)
  2813. (defcustom org-archive-tag "ARCHIVE"
  2814. "The tag that marks a subtree as archived.
  2815. An archived subtree does not open during visibility cycling, and does
  2816. not contribute to the agenda listings.
  2817. After changing this, font-lock must be restarted in the relevant buffers to
  2818. get the proper fontification."
  2819. :group 'org-archive
  2820. :group 'org-keywords
  2821. :type 'string)
  2822. (defcustom org-agenda-skip-archived-trees t
  2823. "Non-nil means, the agenda will skip any items located in archived trees.
  2824. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2825. variable is no longer recommended, you should leave it at the value t.
  2826. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2827. :group 'org-archive
  2828. :group 'org-agenda-skip
  2829. :type 'boolean)
  2830. (defcustom org-columns-skip-arrchived-trees t
  2831. "Non-nil means, irgnore archived trees when creating column view."
  2832. :group 'org-archive
  2833. :group 'org-properties
  2834. :type 'boolean)
  2835. (defcustom org-cycle-open-archived-trees nil
  2836. "Non-nil means, `org-cycle' will open archived trees.
  2837. An archived tree is a tree marked with the tag ARCHIVE.
  2838. When nil, archived trees will stay folded. You can still open them with
  2839. normal outline commands like `show-all', but not with the cycling commands."
  2840. :group 'org-archive
  2841. :group 'org-cycle
  2842. :type 'boolean)
  2843. (defcustom org-sparse-tree-open-archived-trees nil
  2844. "Non-nil means sparse tree construction shows matches in archived trees.
  2845. When nil, matches in these trees are highlighted, but the trees are kept in
  2846. collapsed state."
  2847. :group 'org-archive
  2848. :group 'org-sparse-trees
  2849. :type 'boolean)
  2850. (defun org-cycle-hide-archived-subtrees (state)
  2851. "Re-hide all archived subtrees after a visibility state change."
  2852. (when (and (not org-cycle-open-archived-trees)
  2853. (not (memq state '(overview folded))))
  2854. (save-excursion
  2855. (let* ((globalp (memq state '(contents all)))
  2856. (beg (if globalp (point-min) (point)))
  2857. (end (if globalp (point-max) (org-end-of-subtree t))))
  2858. (org-hide-archived-subtrees beg end)
  2859. (goto-char beg)
  2860. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2861. (message "%s" (substitute-command-keys
  2862. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2863. (defun org-force-cycle-archived ()
  2864. "Cycle subtree even if it is archived."
  2865. (interactive)
  2866. (setq this-command 'org-cycle)
  2867. (let ((org-cycle-open-archived-trees t))
  2868. (call-interactively 'org-cycle)))
  2869. (defun org-hide-archived-subtrees (beg end)
  2870. "Re-hide all archived subtrees after a visibility state change."
  2871. (save-excursion
  2872. (let* ((re (concat ":" org-archive-tag ":")))
  2873. (goto-char beg)
  2874. (while (re-search-forward re end t)
  2875. (and (org-on-heading-p) (hide-subtree))
  2876. (org-end-of-subtree t)))))
  2877. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2878. (eval-and-compile
  2879. (org-autoload "org-archive"
  2880. '(org-add-archive-files org-archive-subtree
  2881. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2882. ;; Autoload Column View Code
  2883. (declare-function org-columns-number-to-string "org-colview")
  2884. (declare-function org-columns-get-format-and-top-level "org-colview")
  2885. (declare-function org-columns-compute "org-colview")
  2886. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2887. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2888. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2889. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2890. ;; Autoload ID code
  2891. (declare-function org-id-store-link "org-id")
  2892. (declare-function org-id-locations-load "org-id")
  2893. (declare-function org-id-locations-save "org-id")
  2894. (defvar org-id-track-globally)
  2895. (org-autoload "org-id"
  2896. '(org-id-get-create org-id-new org-id-copy org-id-get
  2897. org-id-get-with-outline-path-completion
  2898. org-id-get-with-outline-drilling
  2899. org-id-goto org-id-find org-id-store-link))
  2900. ;; Autoload Plotting Code
  2901. (org-autoload "org-plot"
  2902. '(org-plot/gnuplot))
  2903. ;;; Variables for pre-computed regular expressions, all buffer local
  2904. (defvar org-drawer-regexp nil
  2905. "Matches first line of a hidden block.")
  2906. (make-variable-buffer-local 'org-drawer-regexp)
  2907. (defvar org-todo-regexp nil
  2908. "Matches any of the TODO state keywords.")
  2909. (make-variable-buffer-local 'org-todo-regexp)
  2910. (defvar org-not-done-regexp nil
  2911. "Matches any of the TODO state keywords except the last one.")
  2912. (make-variable-buffer-local 'org-not-done-regexp)
  2913. (defvar org-not-done-heading-regexp nil
  2914. "Matches a TODO headline that is not done.")
  2915. (make-variable-buffer-local 'org-not-done-regexp)
  2916. (defvar org-todo-line-regexp nil
  2917. "Matches a headline and puts TODO state into group 2 if present.")
  2918. (make-variable-buffer-local 'org-todo-line-regexp)
  2919. (defvar org-complex-heading-regexp nil
  2920. "Matches a headline and puts everything into groups:
  2921. group 1: the stars
  2922. group 2: The todo keyword, maybe
  2923. group 3: Priority cookie
  2924. group 4: True headline
  2925. group 5: Tags")
  2926. (make-variable-buffer-local 'org-complex-heading-regexp)
  2927. (defvar org-todo-line-tags-regexp nil
  2928. "Matches a headline and puts TODO state into group 2 if present.
  2929. Also put tags into group 4 if tags are present.")
  2930. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2931. (defvar org-nl-done-regexp nil
  2932. "Matches newline followed by a headline with the DONE keyword.")
  2933. (make-variable-buffer-local 'org-nl-done-regexp)
  2934. (defvar org-looking-at-done-regexp nil
  2935. "Matches the DONE keyword a point.")
  2936. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2937. (defvar org-ds-keyword-length 12
  2938. "Maximum length of the Deadline and SCHEDULED keywords.")
  2939. (make-variable-buffer-local 'org-ds-keyword-length)
  2940. (defvar org-deadline-regexp nil
  2941. "Matches the DEADLINE keyword.")
  2942. (make-variable-buffer-local 'org-deadline-regexp)
  2943. (defvar org-deadline-time-regexp nil
  2944. "Matches the DEADLINE keyword together with a time stamp.")
  2945. (make-variable-buffer-local 'org-deadline-time-regexp)
  2946. (defvar org-deadline-line-regexp nil
  2947. "Matches the DEADLINE keyword and the rest of the line.")
  2948. (make-variable-buffer-local 'org-deadline-line-regexp)
  2949. (defvar org-scheduled-regexp nil
  2950. "Matches the SCHEDULED keyword.")
  2951. (make-variable-buffer-local 'org-scheduled-regexp)
  2952. (defvar org-scheduled-time-regexp nil
  2953. "Matches the SCHEDULED keyword together with a time stamp.")
  2954. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2955. (defvar org-closed-time-regexp nil
  2956. "Matches the CLOSED keyword together with a time stamp.")
  2957. (make-variable-buffer-local 'org-closed-time-regexp)
  2958. (defvar org-keyword-time-regexp nil
  2959. "Matches any of the 4 keywords, together with the time stamp.")
  2960. (make-variable-buffer-local 'org-keyword-time-regexp)
  2961. (defvar org-keyword-time-not-clock-regexp nil
  2962. "Matches any of the 3 keywords, together with the time stamp.")
  2963. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2964. (defvar org-maybe-keyword-time-regexp nil
  2965. "Matches a timestamp, possibly preceeded by a keyword.")
  2966. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2967. (defvar org-planning-or-clock-line-re nil
  2968. "Matches a line with planning or clock info.")
  2969. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2970. (defconst org-plain-time-of-day-regexp
  2971. (concat
  2972. "\\(\\<[012]?[0-9]"
  2973. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2974. "\\(--?"
  2975. "\\(\\<[012]?[0-9]"
  2976. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2977. "\\)?")
  2978. "Regular expression to match a plain time or time range.
  2979. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2980. groups carry important information:
  2981. 0 the full match
  2982. 1 the first time, range or not
  2983. 8 the second time, if it is a range.")
  2984. (defconst org-plain-time-extension-regexp
  2985. (concat
  2986. "\\(\\<[012]?[0-9]"
  2987. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2988. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2989. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2990. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2991. groups carry important information:
  2992. 0 the full match
  2993. 7 hours of duration
  2994. 9 minutes of duration")
  2995. (defconst org-stamp-time-of-day-regexp
  2996. (concat
  2997. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2998. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2999. "\\(--?"
  3000. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3001. "Regular expression to match a timestamp time or time range.
  3002. After a match, the following groups carry important information:
  3003. 0 the full match
  3004. 1 date plus weekday, for backreferencing to make sure both times on same day
  3005. 2 the first time, range or not
  3006. 4 the second time, if it is a range.")
  3007. (defconst org-startup-options
  3008. '(("fold" org-startup-folded t)
  3009. ("overview" org-startup-folded t)
  3010. ("nofold" org-startup-folded nil)
  3011. ("showall" org-startup-folded nil)
  3012. ("content" org-startup-folded content)
  3013. ("indent" org-startup-indented t)
  3014. ("noindent" org-startup-indented nil)
  3015. ("hidestars" org-hide-leading-stars t)
  3016. ("showstars" org-hide-leading-stars nil)
  3017. ("odd" org-odd-levels-only t)
  3018. ("oddeven" org-odd-levels-only nil)
  3019. ("align" org-startup-align-all-tables t)
  3020. ("noalign" org-startup-align-all-tables nil)
  3021. ("customtime" org-display-custom-times t)
  3022. ("logdone" org-log-done time)
  3023. ("lognotedone" org-log-done note)
  3024. ("nologdone" org-log-done nil)
  3025. ("lognoteclock-out" org-log-note-clock-out t)
  3026. ("nolognoteclock-out" org-log-note-clock-out nil)
  3027. ("logrepeat" org-log-repeat state)
  3028. ("lognoterepeat" org-log-repeat note)
  3029. ("nologrepeat" org-log-repeat nil)
  3030. ("fninline" org-footnote-define-inline t)
  3031. ("nofninline" org-footnote-define-inline nil)
  3032. ("fnlocal" org-footnote-section nil)
  3033. ("fnauto" org-footnote-auto-label t)
  3034. ("fnprompt" org-footnote-auto-label nil)
  3035. ("fnconfirm" org-footnote-auto-label confirm)
  3036. ("fnplain" org-footnote-auto-label plain)
  3037. ("fnadjust" org-footnote-auto-adjust t)
  3038. ("nofnadjust" org-footnote-auto-adjust nil)
  3039. ("constcgs" constants-unit-system cgs)
  3040. ("constSI" constants-unit-system SI)
  3041. ("noptag" org-tag-persistent-alist nil)
  3042. ("hideblocks" org-hide-block-startup t)
  3043. ("nohideblocks" org-hide-block-startup nil))
  3044. "Variable associated with STARTUP options for org-mode.
  3045. Each element is a list of three items: The startup options as written
  3046. in the #+STARTUP line, the corresponding variable, and the value to
  3047. set this variable to if the option is found. An optional forth element PUSH
  3048. means to push this value onto the list in the variable.")
  3049. (defun org-set-regexps-and-options ()
  3050. "Precompute regular expressions for current buffer."
  3051. (when (org-mode-p)
  3052. (org-set-local 'org-todo-kwd-alist nil)
  3053. (org-set-local 'org-todo-key-alist nil)
  3054. (org-set-local 'org-todo-key-trigger nil)
  3055. (org-set-local 'org-todo-keywords-1 nil)
  3056. (org-set-local 'org-done-keywords nil)
  3057. (org-set-local 'org-todo-heads nil)
  3058. (org-set-local 'org-todo-sets nil)
  3059. (org-set-local 'org-todo-log-states nil)
  3060. (org-set-local 'org-file-properties nil)
  3061. (org-set-local 'org-file-tags nil)
  3062. (let ((re (org-make-options-regexp
  3063. '("CATEGORY" "TODO" "COLUMNS"
  3064. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3065. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3066. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3067. (splitre "[ \t]+")
  3068. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3069. tail sep kws1 prio props ftags drawers
  3070. ext-setup-or-nil setup-contents (start 0))
  3071. (save-excursion
  3072. (save-restriction
  3073. (widen)
  3074. (goto-char (point-min))
  3075. (while (or (and ext-setup-or-nil
  3076. (string-match re ext-setup-or-nil start)
  3077. (setq start (match-end 0)))
  3078. (and (setq ext-setup-or-nil nil start 0)
  3079. (re-search-forward re nil t)))
  3080. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3081. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3082. (cond
  3083. ((equal key "CATEGORY")
  3084. (if (string-match "[ \t]+$" value)
  3085. (setq value (replace-match "" t t value)))
  3086. (setq cat value))
  3087. ((member key '("SEQ_TODO" "TODO"))
  3088. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3089. ((equal key "TYP_TODO")
  3090. (push (cons 'type (org-split-string value splitre)) kwds))
  3091. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3092. ;; general TODO-like setup
  3093. (push (cons (intern (downcase (match-string 1 key)))
  3094. (org-split-string value splitre)) kwds))
  3095. ((equal key "TAGS")
  3096. (setq tags (append tags (if tags '("\\n") nil)
  3097. (org-split-string value splitre))))
  3098. ((equal key "COLUMNS")
  3099. (org-set-local 'org-columns-default-format value))
  3100. ((equal key "LINK")
  3101. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3102. (push (cons (match-string 1 value)
  3103. (org-trim (match-string 2 value)))
  3104. links)))
  3105. ((equal key "PRIORITIES")
  3106. (setq prio (org-split-string value " +")))
  3107. ((equal key "PROPERTY")
  3108. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3109. (push (cons (match-string 1 value) (match-string 2 value))
  3110. props)))
  3111. ((equal key "FILETAGS")
  3112. (when (string-match "\\S-" value)
  3113. (setq ftags
  3114. (append
  3115. ftags
  3116. (apply 'append
  3117. (mapcar (lambda (x) (org-split-string x ":"))
  3118. (org-split-string value)))))))
  3119. ((equal key "DRAWERS")
  3120. (setq drawers (org-split-string value splitre)))
  3121. ((equal key "CONSTANTS")
  3122. (setq const (append const (org-split-string value splitre))))
  3123. ((equal key "STARTUP")
  3124. (let ((opts (org-split-string value splitre))
  3125. l var val)
  3126. (while (setq l (pop opts))
  3127. (when (setq l (assoc l org-startup-options))
  3128. (setq var (nth 1 l) val (nth 2 l))
  3129. (if (not (nth 3 l))
  3130. (set (make-local-variable var) val)
  3131. (if (not (listp (symbol-value var)))
  3132. (set (make-local-variable var) nil))
  3133. (set (make-local-variable var) (symbol-value var))
  3134. (add-to-list var val))))))
  3135. ((equal key "ARCHIVE")
  3136. (string-match " *$" value)
  3137. (setq arch (replace-match "" t t value))
  3138. (remove-text-properties 0 (length arch)
  3139. '(face t fontified t) arch))
  3140. ((equal key "SETUPFILE")
  3141. (setq setup-contents (org-file-contents
  3142. (expand-file-name
  3143. (org-remove-double-quotes value))
  3144. 'noerror))
  3145. (if (not ext-setup-or-nil)
  3146. (setq ext-setup-or-nil setup-contents start 0)
  3147. (setq ext-setup-or-nil
  3148. (concat (substring ext-setup-or-nil 0 start)
  3149. "\n" setup-contents "\n"
  3150. (substring ext-setup-or-nil start)))))
  3151. ))))
  3152. (when cat
  3153. (org-set-local 'org-category (intern cat))
  3154. (push (cons "CATEGORY" cat) props))
  3155. (when prio
  3156. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3157. (setq prio (mapcar 'string-to-char prio))
  3158. (org-set-local 'org-highest-priority (nth 0 prio))
  3159. (org-set-local 'org-lowest-priority (nth 1 prio))
  3160. (org-set-local 'org-default-priority (nth 2 prio)))
  3161. (and props (org-set-local 'org-file-properties (nreverse props)))
  3162. (and ftags (org-set-local 'org-file-tags
  3163. (mapcar 'org-add-prop-inherited ftags)))
  3164. (and drawers (org-set-local 'org-drawers drawers))
  3165. (and arch (org-set-local 'org-archive-location arch))
  3166. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3167. ;; Process the TODO keywords
  3168. (unless kwds
  3169. ;; Use the global values as if they had been given locally.
  3170. (setq kwds (default-value 'org-todo-keywords))
  3171. (if (stringp (car kwds))
  3172. (setq kwds (list (cons org-todo-interpretation
  3173. (default-value 'org-todo-keywords)))))
  3174. (setq kwds (reverse kwds)))
  3175. (setq kwds (nreverse kwds))
  3176. (let (inter kws kw)
  3177. (while (setq kws (pop kwds))
  3178. (let ((kws (or
  3179. (run-hook-with-args-until-success
  3180. 'org-todo-setup-filter-hook kws)
  3181. kws)))
  3182. (setq inter (pop kws) sep (member "|" kws)
  3183. kws0 (delete "|" (copy-sequence kws))
  3184. kwsa nil
  3185. kws1 (mapcar
  3186. (lambda (x)
  3187. ;; 1 2
  3188. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3189. (progn
  3190. (setq kw (match-string 1 x)
  3191. key (and (match-end 2) (match-string 2 x))
  3192. log (org-extract-log-state-settings x))
  3193. (push (cons kw (and key (string-to-char key))) kwsa)
  3194. (and log (push log org-todo-log-states))
  3195. kw)
  3196. (error "Invalid TODO keyword %s" x)))
  3197. kws0)
  3198. kwsa (if kwsa (append '((:startgroup))
  3199. (nreverse kwsa)
  3200. '((:endgroup))))
  3201. hw (car kws1)
  3202. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3203. tail (list inter hw (car dws) (org-last dws))))
  3204. (add-to-list 'org-todo-heads hw 'append)
  3205. (push kws1 org-todo-sets)
  3206. (setq org-done-keywords (append org-done-keywords dws nil))
  3207. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3208. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3209. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3210. (setq org-todo-sets (nreverse org-todo-sets)
  3211. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3212. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3213. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3214. ;; Process the constants
  3215. (when const
  3216. (let (e cst)
  3217. (while (setq e (pop const))
  3218. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3219. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3220. (setq org-table-formula-constants-local cst)))
  3221. ;; Process the tags.
  3222. (when tags
  3223. (let (e tgs)
  3224. (while (setq e (pop tags))
  3225. (cond
  3226. ((equal e "{") (push '(:startgroup) tgs))
  3227. ((equal e "}") (push '(:endgroup) tgs))
  3228. ((equal e "\\n") (push '(:newline) tgs))
  3229. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3230. (push (cons (match-string 1 e)
  3231. (string-to-char (match-string 2 e)))
  3232. tgs))
  3233. (t (push (list e) tgs))))
  3234. (org-set-local 'org-tag-alist nil)
  3235. (while (setq e (pop tgs))
  3236. (or (and (stringp (car e))
  3237. (assoc (car e) org-tag-alist))
  3238. (push e org-tag-alist)))))
  3239. ;; Compute the regular expressions and other local variables
  3240. (if (not org-done-keywords)
  3241. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3242. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3243. (length org-scheduled-string)
  3244. (length org-clock-string)
  3245. (length org-closed-string)))
  3246. org-drawer-regexp
  3247. (concat "^[ \t]*:\\("
  3248. (mapconcat 'regexp-quote org-drawers "\\|")
  3249. "\\):[ \t]*$")
  3250. org-not-done-keywords
  3251. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3252. org-todo-regexp
  3253. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3254. "\\|") "\\)\\>")
  3255. org-not-done-regexp
  3256. (concat "\\<\\("
  3257. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3258. "\\)\\>")
  3259. org-not-done-heading-regexp
  3260. (concat "^\\(\\*+\\)[ \t]+\\("
  3261. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3262. "\\)\\>")
  3263. org-todo-line-regexp
  3264. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3265. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3266. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3267. org-complex-heading-regexp
  3268. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3269. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3270. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3271. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3272. org-nl-done-regexp
  3273. (concat "\n\\*+[ \t]+"
  3274. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3275. "\\)" "\\>")
  3276. org-todo-line-tags-regexp
  3277. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3278. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3279. (org-re
  3280. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3281. org-looking-at-done-regexp
  3282. (concat "^" "\\(?:"
  3283. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3284. "\\>")
  3285. org-deadline-regexp (concat "\\<" org-deadline-string)
  3286. org-deadline-time-regexp
  3287. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3288. org-deadline-line-regexp
  3289. (concat "\\<\\(" org-deadline-string "\\).*")
  3290. org-scheduled-regexp
  3291. (concat "\\<" org-scheduled-string)
  3292. org-scheduled-time-regexp
  3293. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3294. org-closed-time-regexp
  3295. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3296. org-keyword-time-regexp
  3297. (concat "\\<\\(" org-scheduled-string
  3298. "\\|" org-deadline-string
  3299. "\\|" org-closed-string
  3300. "\\|" org-clock-string "\\)"
  3301. " *[[<]\\([^]>]+\\)[]>]")
  3302. org-keyword-time-not-clock-regexp
  3303. (concat "\\<\\(" org-scheduled-string
  3304. "\\|" org-deadline-string
  3305. "\\|" org-closed-string
  3306. "\\)"
  3307. " *[[<]\\([^]>]+\\)[]>]")
  3308. org-maybe-keyword-time-regexp
  3309. (concat "\\(\\<\\(" org-scheduled-string
  3310. "\\|" org-deadline-string
  3311. "\\|" org-closed-string
  3312. "\\|" org-clock-string "\\)\\)?"
  3313. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3314. org-planning-or-clock-line-re
  3315. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3316. "\\|" org-deadline-string
  3317. "\\|" org-closed-string "\\|" org-clock-string
  3318. "\\)\\>\\)")
  3319. )
  3320. (org-compute-latex-and-specials-regexp)
  3321. (org-set-font-lock-defaults))))
  3322. (defun org-file-contents (file &optional noerror)
  3323. "Return the contents of FILE, as a string."
  3324. (if (or (not file)
  3325. (not (file-readable-p file)))
  3326. (if noerror
  3327. (progn
  3328. (message "Cannot read file %s" file)
  3329. (ding) (sit-for 2)
  3330. "")
  3331. (error "Cannot read file %s" file))
  3332. (with-temp-buffer
  3333. (insert-file-contents file)
  3334. (buffer-string))))
  3335. (defun org-extract-log-state-settings (x)
  3336. "Extract the log state setting from a TODO keyword string.
  3337. This will extract info from a string like \"WAIT(w@/!)\"."
  3338. (let (kw key log1 log2)
  3339. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3340. (setq kw (match-string 1 x)
  3341. key (and (match-end 2) (match-string 2 x))
  3342. log1 (and (match-end 3) (match-string 3 x))
  3343. log2 (and (match-end 4) (match-string 4 x)))
  3344. (and (or log1 log2)
  3345. (list kw
  3346. (and log1 (if (equal log1 "!") 'time 'note))
  3347. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3348. (defun org-remove-keyword-keys (list)
  3349. "Remove a pair of parenthesis at the end of each string in LIST."
  3350. (mapcar (lambda (x)
  3351. (if (string-match "(.*)$" x)
  3352. (substring x 0 (match-beginning 0))
  3353. x))
  3354. list))
  3355. ;; FIXME: this could be done much better, using second characters etc.
  3356. (defun org-assign-fast-keys (alist)
  3357. "Assign fast keys to a keyword-key alist.
  3358. Respect keys that are already there."
  3359. (let (new e k c c1 c2 (char ?a))
  3360. (while (setq e (pop alist))
  3361. (cond
  3362. ((equal e '(:startgroup)) (push e new))
  3363. ((equal e '(:endgroup)) (push e new))
  3364. ((equal e '(:newline)) (push e new))
  3365. (t
  3366. (setq k (car e) c2 nil)
  3367. (if (cdr e)
  3368. (setq c (cdr e))
  3369. ;; automatically assign a character.
  3370. (setq c1 (string-to-char
  3371. (downcase (substring
  3372. k (if (= (string-to-char k) ?@) 1 0)))))
  3373. (if (or (rassoc c1 new) (rassoc c1 alist))
  3374. (while (or (rassoc char new) (rassoc char alist))
  3375. (setq char (1+ char)))
  3376. (setq c2 c1))
  3377. (setq c (or c2 char)))
  3378. (push (cons k c) new))))
  3379. (nreverse new)))
  3380. ;;; Some variables used in various places
  3381. (defvar org-window-configuration nil
  3382. "Used in various places to store a window configuration.")
  3383. (defvar org-finish-function nil
  3384. "Function to be called when `C-c C-c' is used.
  3385. This is for getting out of special buffers like remember.")
  3386. ;; FIXME: Occasionally check by commenting these, to make sure
  3387. ;; no other functions uses these, forgetting to let-bind them.
  3388. (defvar entry)
  3389. (defvar last-state)
  3390. (defvar date)
  3391. ;; Defined somewhere in this file, but used before definition.
  3392. (defvar org-html-entities)
  3393. (defvar org-struct-menu)
  3394. (defvar org-org-menu)
  3395. (defvar org-tbl-menu)
  3396. (defvar org-agenda-keymap)
  3397. ;;;; Define the Org-mode
  3398. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3399. (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."))
  3400. ;; We use a before-change function to check if a table might need
  3401. ;; an update.
  3402. (defvar org-table-may-need-update t
  3403. "Indicates that a table might need an update.
  3404. This variable is set by `org-before-change-function'.
  3405. `org-table-align' sets it back to nil.")
  3406. (defun org-before-change-function (beg end)
  3407. "Every change indicates that a table might need an update."
  3408. (setq org-table-may-need-update t))
  3409. (defvar org-mode-map)
  3410. (defvar org-mode-hook nil
  3411. "Mode hook for Org-mode, run after the mode was turned on.")
  3412. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3413. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3414. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3415. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3416. (defvar org-table-buffer-is-an nil)
  3417. (defconst org-outline-regexp "\\*+ ")
  3418. ;;;###autoload
  3419. (define-derived-mode org-mode outline-mode "Org"
  3420. "Outline-based notes management and organizer, alias
  3421. \"Carsten's outline-mode for keeping track of everything.\"
  3422. Org-mode develops organizational tasks around a NOTES file which
  3423. contains information about projects as plain text. Org-mode is
  3424. implemented on top of outline-mode, which is ideal to keep the content
  3425. of large files well structured. It supports ToDo items, deadlines and
  3426. time stamps, which magically appear in the diary listing of the Emacs
  3427. calendar. Tables are easily created with a built-in table editor.
  3428. Plain text URL-like links connect to websites, emails (VM), Usenet
  3429. messages (Gnus), BBDB entries, and any files related to the project.
  3430. For printing and sharing of notes, an Org-mode file (or a part of it)
  3431. can be exported as a structured ASCII or HTML file.
  3432. The following commands are available:
  3433. \\{org-mode-map}"
  3434. ;; Get rid of Outline menus, they are not needed
  3435. ;; Need to do this here because define-derived-mode sets up
  3436. ;; the keymap so late. Still, it is a waste to call this each time
  3437. ;; we switch another buffer into org-mode.
  3438. (if (featurep 'xemacs)
  3439. (when (boundp 'outline-mode-menu-heading)
  3440. ;; Assume this is Greg's port, it used easymenu
  3441. (easy-menu-remove outline-mode-menu-heading)
  3442. (easy-menu-remove outline-mode-menu-show)
  3443. (easy-menu-remove outline-mode-menu-hide))
  3444. (define-key org-mode-map [menu-bar headings] 'undefined)
  3445. (define-key org-mode-map [menu-bar hide] 'undefined)
  3446. (define-key org-mode-map [menu-bar show] 'undefined))
  3447. (org-load-modules-maybe)
  3448. (easy-menu-add org-org-menu)
  3449. (easy-menu-add org-tbl-menu)
  3450. (org-install-agenda-files-menu)
  3451. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3452. (org-add-to-invisibility-spec '(org-cwidth))
  3453. (org-add-to-invisibility-spec '(org-hide-block . t))
  3454. (when (featurep 'xemacs)
  3455. (org-set-local 'line-move-ignore-invisible t))
  3456. (org-set-local 'outline-regexp org-outline-regexp)
  3457. (org-set-local 'outline-level 'org-outline-level)
  3458. (when (and org-ellipsis
  3459. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3460. (fboundp 'make-glyph-code))
  3461. (unless org-display-table
  3462. (setq org-display-table (make-display-table)))
  3463. (set-display-table-slot
  3464. org-display-table 4
  3465. (vconcat (mapcar
  3466. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3467. org-ellipsis)))
  3468. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3469. (setq buffer-display-table org-display-table))
  3470. (org-set-regexps-and-options)
  3471. (when (and org-tag-faces (not org-tags-special-faces-re))
  3472. ;; tag faces set outside customize.... force initialization.
  3473. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3474. ;; Calc embedded
  3475. (org-set-local 'calc-embedded-open-mode "# ")
  3476. (modify-syntax-entry ?# "<")
  3477. (modify-syntax-entry ?@ "w")
  3478. (if org-startup-truncated (setq truncate-lines t))
  3479. (org-set-local 'font-lock-unfontify-region-function
  3480. 'org-unfontify-region)
  3481. ;; Activate before-change-function
  3482. (org-set-local 'org-table-may-need-update t)
  3483. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3484. 'local)
  3485. ;; Check for running clock before killing a buffer
  3486. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3487. ;; Paragraphs and auto-filling
  3488. (org-set-autofill-regexps)
  3489. (setq indent-line-function 'org-indent-line-function)
  3490. (org-update-radio-target-regexp)
  3491. ;; Make sure dependence stuff works reliably, even for users who set it
  3492. ;; too late :-(
  3493. (if org-enforce-todo-dependencies
  3494. (add-hook 'org-blocker-hook
  3495. 'org-block-todo-from-children-or-siblings-or-parent)
  3496. (remove-hook 'org-blocker-hook
  3497. 'org-block-todo-from-children-or-siblings-or-parent))
  3498. (if org-enforce-todo-checkbox-dependencies
  3499. (add-hook 'org-blocker-hook
  3500. 'org-block-todo-from-checkboxes)
  3501. (remove-hook 'org-blocker-hook
  3502. 'org-block-todo-from-checkboxes))
  3503. ;; Comment characters
  3504. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3505. (org-set-local 'comment-padding " ")
  3506. ;; Align options lines
  3507. (org-set-local
  3508. 'align-mode-rules-list
  3509. '((org-in-buffer-settings
  3510. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3511. (modes . '(org-mode)))))
  3512. ;; Imenu
  3513. (org-set-local 'imenu-create-index-function
  3514. 'org-imenu-get-tree)
  3515. ;; Make isearch reveal context
  3516. (if (or (featurep 'xemacs)
  3517. (not (boundp 'outline-isearch-open-invisible-function)))
  3518. ;; Emacs 21 and XEmacs make use of the hook
  3519. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3520. ;; Emacs 22 deals with this through a special variable
  3521. (org-set-local 'outline-isearch-open-invisible-function
  3522. (lambda (&rest ignore) (org-show-context 'isearch))))
  3523. ;; If empty file that did not turn on org-mode automatically, make it to.
  3524. (if (and org-insert-mode-line-in-empty-file
  3525. (interactive-p)
  3526. (= (point-min) (point-max)))
  3527. (insert "# -*- mode: org -*-\n\n"))
  3528. (unless org-inhibit-startup
  3529. (when org-startup-align-all-tables
  3530. (let ((bmp (buffer-modified-p)))
  3531. (org-table-map-tables 'org-table-align)
  3532. (set-buffer-modified-p bmp)))
  3533. (when org-startup-indented
  3534. (require 'org-indent)
  3535. (org-indent-mode 1))
  3536. (org-set-startup-visibility)))
  3537. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3538. (defun org-current-time ()
  3539. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3540. (if (> (car org-time-stamp-rounding-minutes) 1)
  3541. (let ((r (car org-time-stamp-rounding-minutes))
  3542. (time (decode-time)))
  3543. (apply 'encode-time
  3544. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3545. (nthcdr 2 time))))
  3546. (current-time)))
  3547. ;;;; Font-Lock stuff, including the activators
  3548. (defvar org-mouse-map (make-sparse-keymap))
  3549. (org-defkey org-mouse-map
  3550. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3551. (org-defkey org-mouse-map
  3552. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3553. (when org-mouse-1-follows-link
  3554. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3555. (when org-tab-follows-link
  3556. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3557. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3558. (require 'font-lock)
  3559. (defconst org-non-link-chars "]\t\n\r<>")
  3560. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3561. "shell" "elisp"))
  3562. (defvar org-link-types-re nil
  3563. "Matches a link that has a url-like prefix like \"http:\"")
  3564. (defvar org-link-re-with-space nil
  3565. "Matches a link with spaces, optional angular brackets around it.")
  3566. (defvar org-link-re-with-space2 nil
  3567. "Matches a link with spaces, optional angular brackets around it.")
  3568. (defvar org-link-re-with-space3 nil
  3569. "Matches a link with spaces, only for internal part in bracket links.")
  3570. (defvar org-angle-link-re nil
  3571. "Matches link with angular brackets, spaces are allowed.")
  3572. (defvar org-plain-link-re nil
  3573. "Matches plain link, without spaces.")
  3574. (defvar org-bracket-link-regexp nil
  3575. "Matches a link in double brackets.")
  3576. (defvar org-bracket-link-analytic-regexp nil
  3577. "Regular expression used to analyze links.
  3578. Here is what the match groups contain after a match:
  3579. 1: http:
  3580. 2: http
  3581. 3: path
  3582. 4: [desc]
  3583. 5: desc")
  3584. (defvar org-bracket-link-analytic-regexp++ nil
  3585. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3586. (defvar org-any-link-re nil
  3587. "Regular expression matching any link.")
  3588. (defun org-make-link-regexps ()
  3589. "Update the link regular expressions.
  3590. This should be called after the variable `org-link-types' has changed."
  3591. (setq org-link-types-re
  3592. (concat
  3593. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3594. org-link-re-with-space
  3595. (concat
  3596. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3597. "\\([^" org-non-link-chars " ]"
  3598. "[^" org-non-link-chars "]*"
  3599. "[^" org-non-link-chars " ]\\)>?")
  3600. org-link-re-with-space2
  3601. (concat
  3602. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3603. "\\([^" org-non-link-chars " ]"
  3604. "[^\t\n\r]*"
  3605. "[^" org-non-link-chars " ]\\)>?")
  3606. org-link-re-with-space3
  3607. (concat
  3608. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3609. "\\([^" org-non-link-chars " ]"
  3610. "[^\t\n\r]*\\)")
  3611. org-angle-link-re
  3612. (concat
  3613. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3614. "\\([^" org-non-link-chars " ]"
  3615. "[^" org-non-link-chars "]*"
  3616. "\\)>")
  3617. org-plain-link-re
  3618. (concat
  3619. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3620. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3621. org-bracket-link-regexp
  3622. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3623. org-bracket-link-analytic-regexp
  3624. (concat
  3625. "\\[\\["
  3626. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3627. "\\([^]]+\\)"
  3628. "\\]"
  3629. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3630. "\\]")
  3631. org-bracket-link-analytic-regexp++
  3632. (concat
  3633. "\\[\\["
  3634. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3635. "\\([^]]+\\)"
  3636. "\\]"
  3637. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3638. "\\]")
  3639. org-any-link-re
  3640. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3641. org-angle-link-re "\\)\\|\\("
  3642. org-plain-link-re "\\)")))
  3643. (org-make-link-regexps)
  3644. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3645. "Regular expression for fast time stamp matching.")
  3646. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3647. "Regular expression for fast time stamp matching.")
  3648. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3649. "Regular expression matching time strings for analysis.
  3650. This one does not require the space after the date, so it can be used
  3651. on a string that terminates immediately after the date.")
  3652. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3653. "Regular expression matching time strings for analysis.")
  3654. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3655. "Regular expression matching time stamps, with groups.")
  3656. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3657. "Regular expression matching time stamps (also [..]), with groups.")
  3658. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3659. "Regular expression matching a time stamp range.")
  3660. (defconst org-tr-regexp-both
  3661. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3662. "Regular expression matching a time stamp range.")
  3663. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3664. org-ts-regexp "\\)?")
  3665. "Regular expression matching a time stamp or time stamp range.")
  3666. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3667. org-ts-regexp-both "\\)?")
  3668. "Regular expression matching a time stamp or time stamp range.
  3669. The time stamps may be either active or inactive.")
  3670. (defvar org-emph-face nil)
  3671. (defun org-do-emphasis-faces (limit)
  3672. "Run through the buffer and add overlays to links."
  3673. (let (rtn a)
  3674. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3675. (if (not (= (char-after (match-beginning 3))
  3676. (char-after (match-beginning 4))))
  3677. (progn
  3678. (setq rtn t)
  3679. (setq a (assoc (match-string 3) org-emphasis-alist))
  3680. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3681. 'face
  3682. (nth 1 a))
  3683. (and (nth 4 a)
  3684. (org-remove-flyspell-overlays-in
  3685. (match-beginning 0) (match-end 0)))
  3686. (add-text-properties (match-beginning 2) (match-end 2)
  3687. '(font-lock-multiline t))
  3688. (when org-hide-emphasis-markers
  3689. (add-text-properties (match-end 4) (match-beginning 5)
  3690. '(invisible org-link))
  3691. (add-text-properties (match-beginning 3) (match-end 3)
  3692. '(invisible org-link)))))
  3693. (backward-char 1))
  3694. rtn))
  3695. (defun org-emphasize (&optional char)
  3696. "Insert or change an emphasis, i.e. a font like bold or italic.
  3697. If there is an active region, change that region to a new emphasis.
  3698. If there is no region, just insert the marker characters and position
  3699. the cursor between them.
  3700. CHAR should be either the marker character, or the first character of the
  3701. HTML tag associated with that emphasis. If CHAR is a space, the means
  3702. to remove the emphasis of the selected region.
  3703. If char is not given (for example in an interactive call) it
  3704. will be prompted for."
  3705. (interactive)
  3706. (let ((eal org-emphasis-alist) e det
  3707. (erc org-emphasis-regexp-components)
  3708. (prompt "")
  3709. (string "") beg end move tag c s)
  3710. (if (org-region-active-p)
  3711. (setq beg (region-beginning) end (region-end)
  3712. string (buffer-substring beg end))
  3713. (setq move t))
  3714. (while (setq e (pop eal))
  3715. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3716. c (aref tag 0))
  3717. (push (cons c (string-to-char (car e))) det)
  3718. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3719. (substring tag 1)))))
  3720. (setq det (nreverse det))
  3721. (unless char
  3722. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3723. (setq char (read-char-exclusive)))
  3724. (setq char (or (cdr (assoc char det)) char))
  3725. (if (equal char ?\ )
  3726. (setq s "" move nil)
  3727. (unless (assoc (char-to-string char) org-emphasis-alist)
  3728. (error "No such emphasis marker: \"%c\"" char))
  3729. (setq s (char-to-string char)))
  3730. (while (and (> (length string) 1)
  3731. (equal (substring string 0 1) (substring string -1))
  3732. (assoc (substring string 0 1) org-emphasis-alist))
  3733. (setq string (substring string 1 -1)))
  3734. (setq string (concat s string s))
  3735. (if beg (delete-region beg end))
  3736. (unless (or (bolp)
  3737. (string-match (concat "[" (nth 0 erc) "\n]")
  3738. (char-to-string (char-before (point)))))
  3739. (insert " "))
  3740. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3741. (char-to-string (char-after (point))))
  3742. (insert " ") (backward-char 1))
  3743. (insert string)
  3744. (and move (backward-char 1))))
  3745. (defconst org-nonsticky-props
  3746. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3747. (defsubst org-rear-nonsticky-at (pos)
  3748. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3749. (defun org-activate-plain-links (limit)
  3750. "Run through the buffer and add overlays to links."
  3751. (catch 'exit
  3752. (let (f)
  3753. (if (re-search-forward org-plain-link-re limit t)
  3754. (progn
  3755. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3756. (setq f (get-text-property (match-beginning 0) 'face))
  3757. (if (or (eq f 'org-tag)
  3758. (and (listp f) (memq 'org-tag f)))
  3759. nil
  3760. (add-text-properties (match-beginning 0) (match-end 0)
  3761. (list 'mouse-face 'highlight
  3762. 'keymap org-mouse-map))
  3763. (org-rear-nonsticky-at (match-end 0)))
  3764. t)))))
  3765. (defun org-activate-code (limit)
  3766. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3767. (progn
  3768. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3769. (remove-text-properties (match-beginning 0) (match-end 0)
  3770. '(display t invisible t intangible t))
  3771. t)))
  3772. (defun org-fontify-meta-lines-and-blocks (limit)
  3773. "Fontify #+ lines and blocks, in the correct ways."
  3774. (let ((case-fold-search t))
  3775. (if (re-search-forward
  3776. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3777. limit t)
  3778. (let ((beg (match-beginning 0))
  3779. (beg1 (line-beginning-position 2))
  3780. (dc1 (downcase (match-string 2)))
  3781. (dc3 (downcase (match-string 3)))
  3782. end end1 quoting)
  3783. (cond
  3784. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3785. ;; a single line of backend-specific content
  3786. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3787. (remove-text-properties (match-beginning 0) (match-end 0)
  3788. '(display t invisible t intangible t))
  3789. (add-text-properties (match-beginning 1) (match-end 3)
  3790. '(font-lock-fontified t face org-meta-line))
  3791. (add-text-properties (match-beginning 6) (match-end 6)
  3792. '(font-lock-fontified t face org-block))
  3793. t)
  3794. ((and (match-end 4) (equal dc3 "begin"))
  3795. ;; Truely a block
  3796. (setq quoting (member (downcase (match-string 5))
  3797. org-protecting-blocks))
  3798. (when (re-search-forward
  3799. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3800. nil t) ;; on purpose, we look further than LIMIT
  3801. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3802. (when quoting
  3803. (remove-text-properties beg end
  3804. '(display t invisible t intangible t)))
  3805. (add-text-properties
  3806. beg end
  3807. '(font-lock-fontified t font-lock-multiline t))
  3808. (add-text-properties beg beg1 '(face org-meta-line))
  3809. (add-text-properties end1 end '(face org-meta-line))
  3810. (when quoting
  3811. (add-text-properties beg1 end1 '(face org-block)))
  3812. t))
  3813. ((not (member (char-after beg) '(?\ ?\t)))
  3814. ;; just any other in-buffer setting, but not indented
  3815. (add-text-properties
  3816. beg (match-end 0)
  3817. '(font-lock-fontified t face org-meta-line))
  3818. t)
  3819. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3820. (and (match-end 4) (equal dc3 "attr")))
  3821. (add-text-properties
  3822. beg (match-end 0)
  3823. '(font-lock-fontified t face org-meta-line))
  3824. t)
  3825. (t nil))))))
  3826. (defun org-activate-angle-links (limit)
  3827. "Run through the buffer and add overlays to links."
  3828. (if (re-search-forward org-angle-link-re limit t)
  3829. (progn
  3830. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3831. (add-text-properties (match-beginning 0) (match-end 0)
  3832. (list 'mouse-face 'highlight
  3833. 'keymap org-mouse-map))
  3834. (org-rear-nonsticky-at (match-end 0))
  3835. t)))
  3836. (defun org-activate-footnote-links (limit)
  3837. "Run through the buffer and add overlays to links."
  3838. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3839. limit t)
  3840. (progn
  3841. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3842. (add-text-properties (match-beginning 2) (match-end 2)
  3843. (list 'mouse-face 'highlight
  3844. 'keymap org-mouse-map
  3845. 'help-echo
  3846. (if (= (point-at-bol) (match-beginning 2))
  3847. "Footnote definition"
  3848. "Footnote reference")
  3849. ))
  3850. (org-rear-nonsticky-at (match-end 2))
  3851. t)))
  3852. (defun org-activate-bracket-links (limit)
  3853. "Run through the buffer and add overlays to bracketed links."
  3854. (if (re-search-forward org-bracket-link-regexp limit t)
  3855. (let* ((help (concat "LINK: "
  3856. (org-match-string-no-properties 1)))
  3857. ;; FIXME: above we should remove the escapes.
  3858. ;; but that requires another match, protecting match data,
  3859. ;; a lot of overhead for font-lock.
  3860. (ip (org-maybe-intangible
  3861. (list 'invisible 'org-link
  3862. 'keymap org-mouse-map 'mouse-face 'highlight
  3863. 'font-lock-multiline t 'help-echo help)))
  3864. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3865. 'font-lock-multiline t 'help-echo help)))
  3866. ;; We need to remove the invisible property here. Table narrowing
  3867. ;; may have made some of this invisible.
  3868. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3869. (remove-text-properties (match-beginning 0) (match-end 0)
  3870. '(invisible nil))
  3871. (if (match-end 3)
  3872. (progn
  3873. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3874. (org-rear-nonsticky-at (match-beginning 3))
  3875. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3876. (org-rear-nonsticky-at (match-end 3))
  3877. (add-text-properties (match-end 3) (match-end 0) ip)
  3878. (org-rear-nonsticky-at (match-end 0)))
  3879. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3880. (org-rear-nonsticky-at (match-beginning 1))
  3881. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3882. (org-rear-nonsticky-at (match-end 1))
  3883. (add-text-properties (match-end 1) (match-end 0) ip)
  3884. (org-rear-nonsticky-at (match-end 0)))
  3885. t)))
  3886. (defun org-activate-dates (limit)
  3887. "Run through the buffer and add overlays to dates."
  3888. (if (re-search-forward org-tsr-regexp-both limit t)
  3889. (progn
  3890. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3891. (add-text-properties (match-beginning 0) (match-end 0)
  3892. (list 'mouse-face 'highlight
  3893. 'keymap org-mouse-map))
  3894. (org-rear-nonsticky-at (match-end 0))
  3895. (when org-display-custom-times
  3896. (if (match-end 3)
  3897. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3898. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3899. t)))
  3900. (defvar org-target-link-regexp nil
  3901. "Regular expression matching radio targets in plain text.")
  3902. (make-variable-buffer-local 'org-target-link-regexp)
  3903. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3904. "Regular expression matching a link target.")
  3905. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3906. "Regular expression matching a radio target.")
  3907. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3908. "Regular expression matching any target.")
  3909. (defun org-activate-target-links (limit)
  3910. "Run through the buffer and add overlays to target matches."
  3911. (when org-target-link-regexp
  3912. (let ((case-fold-search t))
  3913. (if (re-search-forward org-target-link-regexp limit t)
  3914. (progn
  3915. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3916. (add-text-properties (match-beginning 0) (match-end 0)
  3917. (list 'mouse-face 'highlight
  3918. 'keymap org-mouse-map
  3919. 'help-echo "Radio target link"
  3920. 'org-linked-text t))
  3921. (org-rear-nonsticky-at (match-end 0))
  3922. t)))))
  3923. (defun org-update-radio-target-regexp ()
  3924. "Find all radio targets in this file and update the regular expression."
  3925. (interactive)
  3926. (when (memq 'radio org-activate-links)
  3927. (setq org-target-link-regexp
  3928. (org-make-target-link-regexp (org-all-targets 'radio)))
  3929. (org-restart-font-lock)))
  3930. (defun org-hide-wide-columns (limit)
  3931. (let (s e)
  3932. (setq s (text-property-any (point) (or limit (point-max))
  3933. 'org-cwidth t))
  3934. (when s
  3935. (setq e (next-single-property-change s 'org-cwidth))
  3936. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3937. (goto-char e)
  3938. t)))
  3939. (defvar org-latex-and-specials-regexp nil
  3940. "Regular expression for highlighting export special stuff.")
  3941. (defvar org-match-substring-regexp)
  3942. (defvar org-match-substring-with-braces-regexp)
  3943. (defvar org-export-html-special-string-regexps)
  3944. (defun org-compute-latex-and-specials-regexp ()
  3945. "Compute regular expression for stuff treated specially by exporters."
  3946. (if (not org-highlight-latex-fragments-and-specials)
  3947. (org-set-local 'org-latex-and-specials-regexp nil)
  3948. (require 'org-exp)
  3949. (let*
  3950. ((matchers (plist-get org-format-latex-options :matchers))
  3951. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3952. org-latex-regexps)))
  3953. (options (org-combine-plists (org-default-export-plist)
  3954. (org-infile-export-plist)))
  3955. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3956. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3957. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3958. (org-export-html-expand (plist-get options :expand-quoted-html))
  3959. (org-export-with-special-strings (plist-get options :special-strings))
  3960. (re-sub
  3961. (cond
  3962. ((equal org-export-with-sub-superscripts '{})
  3963. (list org-match-substring-with-braces-regexp))
  3964. (org-export-with-sub-superscripts
  3965. (list org-match-substring-regexp))
  3966. (t nil)))
  3967. (re-latex
  3968. (if org-export-with-LaTeX-fragments
  3969. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3970. (re-macros
  3971. (if org-export-with-TeX-macros
  3972. (list (concat "\\\\"
  3973. (regexp-opt
  3974. (append (mapcar 'car org-html-entities)
  3975. (if (boundp 'org-latex-entities)
  3976. (mapcar (lambda (x)
  3977. (or (car-safe x) x))
  3978. org-latex-entities)
  3979. nil))
  3980. 'words))) ; FIXME
  3981. ))
  3982. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3983. (re-special (if org-export-with-special-strings
  3984. (mapcar (lambda (x) (car x))
  3985. org-export-html-special-string-regexps)))
  3986. (re-rest
  3987. (delq nil
  3988. (list
  3989. (if org-export-html-expand "@<[^>\n]+>")
  3990. ))))
  3991. (org-set-local
  3992. 'org-latex-and-specials-regexp
  3993. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3994. re-rest) "\\|")))))
  3995. (defun org-do-latex-and-special-faces (limit)
  3996. "Run through the buffer and add overlays to links."
  3997. (when org-latex-and-specials-regexp
  3998. (let (rtn d)
  3999. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4000. limit t))
  4001. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4002. 'face))
  4003. '(org-code org-verbatim underline)))
  4004. (progn
  4005. (setq rtn t
  4006. d (cond ((member (char-after (1+ (match-beginning 0)))
  4007. '(?_ ?^)) 1)
  4008. (t 0)))
  4009. (font-lock-prepend-text-property
  4010. (+ d (match-beginning 0)) (match-end 0)
  4011. 'face 'org-latex-and-export-specials)
  4012. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4013. '(font-lock-multiline t)))))
  4014. rtn)))
  4015. (defun org-restart-font-lock ()
  4016. "Restart font-lock-mode, to force refontification."
  4017. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4018. (font-lock-mode -1)
  4019. (font-lock-mode 1)))
  4020. (defun org-all-targets (&optional radio)
  4021. "Return a list of all targets in this file.
  4022. With optional argument RADIO, only find radio targets."
  4023. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4024. rtn)
  4025. (save-excursion
  4026. (goto-char (point-min))
  4027. (while (re-search-forward re nil t)
  4028. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4029. rtn)))
  4030. (defun org-make-target-link-regexp (targets)
  4031. "Make regular expression matching all strings in TARGETS.
  4032. The regular expression finds the targets also if there is a line break
  4033. between words."
  4034. (and targets
  4035. (concat
  4036. "\\<\\("
  4037. (mapconcat
  4038. (lambda (x)
  4039. (while (string-match " +" x)
  4040. (setq x (replace-match "\\s-+" t t x)))
  4041. x)
  4042. targets
  4043. "\\|")
  4044. "\\)\\>")))
  4045. (defun org-activate-tags (limit)
  4046. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4047. (progn
  4048. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4049. (add-text-properties (match-beginning 1) (match-end 1)
  4050. (list 'mouse-face 'highlight
  4051. 'keymap org-mouse-map))
  4052. (org-rear-nonsticky-at (match-end 1))
  4053. t)))
  4054. (defun org-outline-level ()
  4055. (save-excursion
  4056. (looking-at outline-regexp)
  4057. (if (match-beginning 1)
  4058. (+ (org-get-string-indentation (match-string 1)) 1000)
  4059. (1- (- (match-end 0) (match-beginning 0))))))
  4060. (defvar org-font-lock-keywords nil)
  4061. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4062. "Regular expression matching a property line.")
  4063. (defvar org-font-lock-hook nil
  4064. "Functions to be called for special font lock stuff.")
  4065. (defun org-font-lock-hook (limit)
  4066. (run-hook-with-args 'org-font-lock-hook limit))
  4067. (defun org-set-font-lock-defaults ()
  4068. (let* ((em org-fontify-emphasized-text)
  4069. (lk org-activate-links)
  4070. (org-font-lock-extra-keywords
  4071. (list
  4072. ;; Call the hook
  4073. '(org-font-lock-hook)
  4074. ;; Headlines
  4075. `(,(if org-fontify-whole-heading-line
  4076. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4077. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4078. (1 (org-get-level-face 1))
  4079. (2 (org-get-level-face 2))
  4080. (3 (org-get-level-face 3)))
  4081. ;; Table lines
  4082. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4083. (1 'org-table t))
  4084. ;; Table internals
  4085. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4086. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4087. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4088. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4089. ;; Drawers
  4090. (list org-drawer-regexp '(0 'org-special-keyword t))
  4091. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4092. ;; Properties
  4093. (list org-property-re
  4094. '(1 'org-special-keyword t)
  4095. '(3 'org-property-value t))
  4096. ;; Links
  4097. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4098. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4099. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4100. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4101. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4102. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4103. (if (memq 'footnote lk) '(org-activate-footnote-links
  4104. (2 'org-footnote t)))
  4105. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4106. '(org-hide-wide-columns (0 nil append))
  4107. ;; TODO lines
  4108. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4109. '(1 (org-get-todo-face 1) t))
  4110. ;; DONE
  4111. (if org-fontify-done-headline
  4112. (list (concat "^[*]+ +\\<\\("
  4113. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4114. "\\)\\(.*\\)")
  4115. '(2 'org-headline-done t))
  4116. nil)
  4117. ;; Priorities
  4118. '(org-font-lock-add-priority-faces)
  4119. ;; Tags
  4120. '(org-font-lock-add-tag-faces)
  4121. ;; Special keywords
  4122. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4123. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4124. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4125. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4126. ;; Emphasis
  4127. (if em
  4128. (if (featurep 'xemacs)
  4129. '(org-do-emphasis-faces (0 nil append))
  4130. '(org-do-emphasis-faces)))
  4131. ;; Checkboxes
  4132. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4133. 2 'org-checkbox prepend)
  4134. (if org-provide-checkbox-statistics
  4135. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4136. (0 (org-get-checkbox-statistics-face) t)))
  4137. ;; Description list items
  4138. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4139. 2 'bold prepend)
  4140. ;; ARCHIVEd headings
  4141. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4142. '(1 'org-archived prepend))
  4143. ;; Specials
  4144. '(org-do-latex-and-special-faces)
  4145. ;; Code
  4146. '(org-activate-code (1 'org-code t))
  4147. ;; COMMENT
  4148. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4149. "\\|" org-quote-string "\\)\\>")
  4150. '(1 'org-special-keyword t))
  4151. '("^#.*" (0 'font-lock-comment-face t))
  4152. ;; Blocks and meta lines
  4153. '(org-fontify-meta-lines-and-blocks)
  4154. )))
  4155. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4156. ;; Now set the full font-lock-keywords
  4157. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4158. (org-set-local 'font-lock-defaults
  4159. '(org-font-lock-keywords t nil nil backward-paragraph))
  4160. (kill-local-variable 'font-lock-keywords) nil))
  4161. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4162. "Fontify string S like in Org-mode"
  4163. (with-temp-buffer
  4164. (insert s)
  4165. (let ((org-odd-levels-only odd-levels))
  4166. (org-mode)
  4167. (font-lock-fontify-buffer)
  4168. (buffer-string))))
  4169. (defvar org-m nil)
  4170. (defvar org-l nil)
  4171. (defvar org-f nil)
  4172. (defun org-get-level-face (n)
  4173. "Get the right face for match N in font-lock matching of headlines."
  4174. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4175. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4176. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4177. (cond
  4178. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4179. ((eq n 2) org-f)
  4180. (t (if org-level-color-stars-only nil org-f))))
  4181. (defun org-get-todo-face (kwd)
  4182. "Get the right face for a TODO keyword KWD.
  4183. If KWD is a number, get the corresponding match group."
  4184. (if (numberp kwd) (setq kwd (match-string kwd)))
  4185. (or (cdr (assoc kwd org-todo-keyword-faces))
  4186. (and (member kwd org-done-keywords) 'org-done)
  4187. 'org-todo))
  4188. (defun org-font-lock-add-tag-faces (limit)
  4189. "Add the special tag faces."
  4190. (when (and org-tag-faces org-tags-special-faces-re)
  4191. (while (re-search-forward org-tags-special-faces-re limit t)
  4192. (add-text-properties (match-beginning 1) (match-end 1)
  4193. (list 'face (org-get-tag-face 1)
  4194. 'font-lock-fontified t))
  4195. (backward-char 1))))
  4196. (defun org-font-lock-add-priority-faces (limit)
  4197. "Add the special priority faces."
  4198. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4199. (add-text-properties
  4200. (match-beginning 0) (match-end 0)
  4201. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4202. org-priority-faces))
  4203. 'org-special-keyword)
  4204. 'font-lock-fontified t))))
  4205. (defun org-get-tag-face (kwd)
  4206. "Get the right face for a TODO keyword KWD.
  4207. If KWD is a number, get the corresponding match group."
  4208. (if (numberp kwd) (setq kwd (match-string kwd)))
  4209. (or (cdr (assoc kwd org-tag-faces))
  4210. 'org-tag))
  4211. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4212. "Remove fontification and activation overlays from links."
  4213. (font-lock-default-unfontify-region beg end)
  4214. (let* ((buffer-undo-list t)
  4215. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4216. (inhibit-modification-hooks t)
  4217. deactivate-mark buffer-file-name buffer-file-truename)
  4218. (remove-text-properties beg end
  4219. '(mouse-face t keymap t org-linked-text t
  4220. invisible t intangible t
  4221. line-prefix t wrap-prefix t
  4222. org-no-flyspell t))))
  4223. ;;;; Visibility cycling, including org-goto and indirect buffer
  4224. ;;; Cycling
  4225. (defvar org-cycle-global-status nil)
  4226. (make-variable-buffer-local 'org-cycle-global-status)
  4227. (defvar org-cycle-subtree-status nil)
  4228. (make-variable-buffer-local 'org-cycle-subtree-status)
  4229. ;;;###autoload
  4230. (defvar org-inlinetask-min-level)
  4231. (defun org-cycle (&optional arg)
  4232. "TAB-action and visibility cycling for Org-mode.
  4233. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4234. is outine visibility cycling, but it also invokes other actions
  4235. in special contexts.
  4236. - When this function is called with a prefix argument, rotate the entire
  4237. buffer through 3 states (global cycling)
  4238. 1. OVERVIEW: Show only top-level headlines.
  4239. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4240. 3. SHOW ALL: Show everything.
  4241. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4242. determined by the variable `org-startup-folded', and by any VISIBILITY
  4243. properties in the buffer.
  4244. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4245. including any drawers.
  4246. - When inside a table, re-align the table and move to the next field.
  4247. - When point is at the beginning of a headline, rotate the subtree started
  4248. by this line through 3 different states (local cycling)
  4249. 1. FOLDED: Only the main headline is shown.
  4250. 2. CHILDREN: The main headline and the direct children are shown.
  4251. From this state, you can move to one of the children
  4252. and zoom in further.
  4253. 3. SUBTREE: Show the entire subtree, including body text.
  4254. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4255. - When there is a numeric prefix, go up to a heading with level ARG, do
  4256. a `show-subtree' and return to the previous cursor position. If ARG
  4257. is negative, go up that many levels.
  4258. - When point is not at the beginning of a headline, execute the global
  4259. binding for TAB, which is re-indenting the line. See the option
  4260. `org-cycle-emulate-tab' for details.
  4261. - Special case: if point is at the beginning of the buffer and there is
  4262. no headline in line 1, this function will act as if called with prefix arg.
  4263. But only if also the variable `org-cycle-global-at-bob' is t."
  4264. (interactive "P")
  4265. (org-load-modules-maybe)
  4266. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4267. (let* ((limit-level
  4268. (or org-cycle-max-level
  4269. (and (boundp 'org-inlinetask-min-level)
  4270. org-inlinetask-min-level
  4271. (1- org-inlinetask-min-level))))
  4272. (nstars (and limit-level
  4273. (if org-odd-levels-only
  4274. (and limit-level (1- (* limit-level 2)))
  4275. limit-level)))
  4276. (outline-regexp
  4277. (cond
  4278. ((not (org-mode-p)) outline-regexp)
  4279. ((or (eq org-cycle-include-plain-lists 'integrate)
  4280. (and org-cycle-include-plain-lists (org-at-item-p)))
  4281. (concat "\\(?:\\*"
  4282. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4283. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4284. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4285. (bob-special (and org-cycle-global-at-bob (bobp)
  4286. (not (looking-at outline-regexp))))
  4287. (org-cycle-hook
  4288. (if bob-special
  4289. (delq 'org-optimize-window-after-visibility-change
  4290. (copy-sequence org-cycle-hook))
  4291. org-cycle-hook))
  4292. (pos (point)))
  4293. (if (or bob-special (equal arg '(4)))
  4294. ;; special case: use global cycling
  4295. (setq arg t))
  4296. (cond
  4297. ((equal arg '(16))
  4298. (org-set-startup-visibility)
  4299. (message "Startup visibility, plus VISIBILITY properties"))
  4300. ((equal arg '(64))
  4301. (show-all)
  4302. (message "Entire buffer visible, including drawers"))
  4303. ((org-at-table-p 'any)
  4304. ;; Enter the table or move to the next field in the table
  4305. (or (org-table-recognize-table.el)
  4306. (progn
  4307. (if arg (org-table-edit-field t)
  4308. (org-table-justify-field-maybe)
  4309. (call-interactively 'org-table-next-field)))))
  4310. ((run-hook-with-args-until-success
  4311. 'org-tab-after-check-for-table-hook))
  4312. ((eq arg t) ;; Global cycling
  4313. (org-cycle-internal-global))
  4314. ((and org-drawers org-drawer-regexp
  4315. (save-excursion
  4316. (beginning-of-line 1)
  4317. (looking-at org-drawer-regexp)))
  4318. ;; Toggle block visibility
  4319. (org-flag-drawer
  4320. (not (get-char-property (match-end 0) 'invisible))))
  4321. ((integerp arg)
  4322. ;; Show-subtree, ARG levels up from here.
  4323. (save-excursion
  4324. (org-back-to-heading)
  4325. (outline-up-heading (if (< arg 0) (- arg)
  4326. (- (funcall outline-level) arg)))
  4327. (org-show-subtree)))
  4328. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4329. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4330. (org-cycle-internal-local))
  4331. ;; TAB emulation and template completion
  4332. (buffer-read-only (org-back-to-heading))
  4333. ((run-hook-with-args-until-success
  4334. 'org-tab-after-check-for-cycling-hook))
  4335. ((org-try-structure-completion))
  4336. ((org-try-cdlatex-tab))
  4337. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4338. (or (not (bolp))
  4339. (not (looking-at outline-regexp))))
  4340. (call-interactively (global-key-binding "\t")))
  4341. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4342. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4343. (or (and (eq org-cycle-emulate-tab 'white)
  4344. (= (match-end 0) (point-at-eol)))
  4345. (and (eq org-cycle-emulate-tab 'whitestart)
  4346. (>= (match-end 0) pos))))
  4347. t
  4348. (eq org-cycle-emulate-tab t))
  4349. (call-interactively (global-key-binding "\t")))
  4350. (t (save-excursion
  4351. (org-back-to-heading)
  4352. (org-cycle)))))))
  4353. (defun org-cycle-internal-global ()
  4354. "Do the global cycling action."
  4355. (cond
  4356. ((and (eq last-command this-command)
  4357. (eq org-cycle-global-status 'overview))
  4358. ;; We just created the overview - now do table of contents
  4359. ;; This can be slow in very large buffers, so indicate action
  4360. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4361. (message "CONTENTS...")
  4362. (org-content)
  4363. (message "CONTENTS...done")
  4364. (setq org-cycle-global-status 'contents)
  4365. (run-hook-with-args 'org-cycle-hook 'contents))
  4366. ((and (eq last-command this-command)
  4367. (eq org-cycle-global-status 'contents))
  4368. ;; We just showed the table of contents - now show everything
  4369. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4370. (show-all)
  4371. (message "SHOW ALL")
  4372. (setq org-cycle-global-status 'all)
  4373. (run-hook-with-args 'org-cycle-hook 'all))
  4374. (t
  4375. ;; Default action: go to overview
  4376. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4377. (org-overview)
  4378. (message "OVERVIEW")
  4379. (setq org-cycle-global-status 'overview)
  4380. (run-hook-with-args 'org-cycle-hook 'overview))))
  4381. (defun org-cycle-internal-local ()
  4382. "Do the local cycling action."
  4383. (org-back-to-heading)
  4384. (let ((goal-column 0) eoh eol eos)
  4385. ;; First, some boundaries
  4386. (save-excursion
  4387. (org-back-to-heading)
  4388. (save-excursion
  4389. (beginning-of-line 2)
  4390. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4391. ; XEmacs does not have `next-single-char-property-change'
  4392. ; I'm not sure about Emacs 21.
  4393. (while (and (not (eobp)) ;; this is like `next-line'
  4394. (get-char-property (1- (point)) 'invisible))
  4395. (beginning-of-line 2))
  4396. (while (and (not (eobp)) ;; this is like `next-line'
  4397. (get-char-property (1- (point)) 'invisible))
  4398. (goto-char (next-single-char-property-change (point) 'invisible))
  4399. (or (bolp) (beginning-of-line 2))))
  4400. (setq eol (point)))
  4401. (outline-end-of-heading) (setq eoh (point))
  4402. (org-end-of-subtree t)
  4403. (unless (eobp)
  4404. (skip-chars-forward " \t\n")
  4405. (beginning-of-line 1) ; in case this is an item
  4406. )
  4407. (setq eos (1- (point))))
  4408. ;; Find out what to do next and set `this-command'
  4409. (cond
  4410. ((= eos eoh)
  4411. ;; Nothing is hidden behind this heading
  4412. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4413. (message "EMPTY ENTRY")
  4414. (setq org-cycle-subtree-status nil)
  4415. (save-excursion
  4416. (goto-char eos)
  4417. (outline-next-heading)
  4418. (if (org-invisible-p) (org-flag-heading nil))))
  4419. ((or (>= eol eos)
  4420. (not (string-match "\\S-" (buffer-substring eol eos))))
  4421. ;; Entire subtree is hidden in one line: open it
  4422. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4423. (org-show-entry)
  4424. (show-children)
  4425. (message "CHILDREN")
  4426. (save-excursion
  4427. (goto-char eos)
  4428. (outline-next-heading)
  4429. (if (org-invisible-p) (org-flag-heading nil)))
  4430. (setq org-cycle-subtree-status 'children)
  4431. (run-hook-with-args 'org-cycle-hook 'children))
  4432. ((and (eq last-command this-command)
  4433. (eq org-cycle-subtree-status 'children))
  4434. ;; We just showed the children, now show everything.
  4435. (if (save-excursion
  4436. (beginning-of-line 2)
  4437. (re-search-forward org-complex-heading-regexp eos t))
  4438. (progn
  4439. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4440. (org-show-subtree)
  4441. (message "SUBTREE")
  4442. (setq org-cycle-subtree-status 'subtree)
  4443. (run-hook-with-args 'org-cycle-hook 'subtree))
  4444. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4445. (hide-subtree)
  4446. (message "FOLDED (NO SUBTREE)")
  4447. (setq org-cycle-subtree-status 'folded)
  4448. (run-hook-with-args 'org-cycle-hook 'folded)))
  4449. (t
  4450. ;; Default action: hide the subtree.
  4451. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4452. (hide-subtree)
  4453. (message "FOLDED")
  4454. (setq org-cycle-subtree-status 'folded)
  4455. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4456. ;;;###autoload
  4457. (defun org-global-cycle (&optional arg)
  4458. "Cycle the global visibility. For details see `org-cycle'.
  4459. With C-u prefix arg, switch to startup visibility.
  4460. With a numeric prefix, show all headlines up to that level."
  4461. (interactive "P")
  4462. (let ((org-cycle-include-plain-lists
  4463. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4464. (cond
  4465. ((integerp arg)
  4466. (show-all)
  4467. (hide-sublevels arg)
  4468. (setq org-cycle-global-status 'contents))
  4469. ((equal arg '(4))
  4470. (org-set-startup-visibility)
  4471. (message "Startup visibility, plus VISIBILITY properties."))
  4472. (t
  4473. (org-cycle '(4))))))
  4474. (defun org-set-startup-visibility ()
  4475. "Set the visibility required by startup options and properties."
  4476. (cond
  4477. ((eq org-startup-folded t)
  4478. (org-cycle '(4)))
  4479. ((eq org-startup-folded 'content)
  4480. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4481. (org-cycle '(4)) (org-cycle '(4)))))
  4482. (if org-hide-block-startup (org-hide-block-all))
  4483. (org-set-visibility-according-to-property 'no-cleanup)
  4484. (org-cycle-hide-archived-subtrees 'all)
  4485. (org-cycle-hide-drawers 'all)
  4486. (org-cycle-show-empty-lines 'all))
  4487. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4488. "Switch subtree visibilities according to :VISIBILITY: property."
  4489. (interactive)
  4490. (let (org-show-entry-below state)
  4491. (save-excursion
  4492. (goto-char (point-min))
  4493. (while (re-search-forward
  4494. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4495. nil t)
  4496. (setq state (match-string 1))
  4497. (save-excursion
  4498. (org-back-to-heading t)
  4499. (hide-subtree)
  4500. (org-reveal)
  4501. (cond
  4502. ((equal state '("fold" "folded"))
  4503. (hide-subtree))
  4504. ((equal state "children")
  4505. (org-show-hidden-entry)
  4506. (show-children))
  4507. ((equal state "content")
  4508. (save-excursion
  4509. (save-restriction
  4510. (org-narrow-to-subtree)
  4511. (org-content))))
  4512. ((member state '("all" "showall"))
  4513. (show-subtree)))))
  4514. (unless no-cleanup
  4515. (org-cycle-hide-archived-subtrees 'all)
  4516. (org-cycle-hide-drawers 'all)
  4517. (org-cycle-show-empty-lines 'all)))))
  4518. (defun org-overview ()
  4519. "Switch to overview mode, showing only top-level headlines.
  4520. Really, this shows all headlines with level equal or greater than the level
  4521. of the first headline in the buffer. This is important, because if the
  4522. first headline is not level one, then (hide-sublevels 1) gives confusing
  4523. results."
  4524. (interactive)
  4525. (let ((level (save-excursion
  4526. (goto-char (point-min))
  4527. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4528. (progn
  4529. (goto-char (match-beginning 0))
  4530. (funcall outline-level))))))
  4531. (and level (hide-sublevels level))))
  4532. (defun org-content (&optional arg)
  4533. "Show all headlines in the buffer, like a table of contents.
  4534. With numerical argument N, show content up to level N."
  4535. (interactive "P")
  4536. (save-excursion
  4537. ;; Visit all headings and show their offspring
  4538. (and (integerp arg) (org-overview))
  4539. (goto-char (point-max))
  4540. (catch 'exit
  4541. (while (and (progn (condition-case nil
  4542. (outline-previous-visible-heading 1)
  4543. (error (goto-char (point-min))))
  4544. t)
  4545. (looking-at outline-regexp))
  4546. (if (integerp arg)
  4547. (show-children (1- arg))
  4548. (show-branches))
  4549. (if (bobp) (throw 'exit nil))))))
  4550. (defun org-optimize-window-after-visibility-change (state)
  4551. "Adjust the window after a change in outline visibility.
  4552. This function is the default value of the hook `org-cycle-hook'."
  4553. (when (get-buffer-window (current-buffer))
  4554. (cond
  4555. ((eq state 'content) nil)
  4556. ((eq state 'all) nil)
  4557. ((eq state 'folded) nil)
  4558. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4559. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4560. ;; FIXME: no longer in use
  4561. (defun org-compact-display-after-subtree-move ()
  4562. "Show a compacter version of the tree of the entry's parent."
  4563. (save-excursion
  4564. (if (org-up-heading-safe)
  4565. (progn
  4566. (hide-subtree)
  4567. (show-entry)
  4568. (show-children)
  4569. (org-cycle-show-empty-lines 'children)
  4570. (org-cycle-hide-drawers 'children))
  4571. (org-overview))))
  4572. (defun org-remove-empty-overlays-at (pos)
  4573. "Remove outline overlays that do not contain non-white stuff."
  4574. (mapc
  4575. (lambda (o)
  4576. (and (eq 'outline (org-overlay-get o 'invisible))
  4577. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4578. (org-overlay-end o))))
  4579. (org-delete-overlay o)))
  4580. (org-overlays-at pos)))
  4581. (defun org-clean-visibility-after-subtree-move ()
  4582. "Fix visibility issues after moving a subtree."
  4583. ;; First, find a reasonable region to look at:
  4584. ;; Start two siblings above, end three below
  4585. (let* ((beg (save-excursion
  4586. (and (outline-get-last-sibling)
  4587. (outline-get-last-sibling))
  4588. (point)))
  4589. (end (save-excursion
  4590. (and (outline-get-next-sibling)
  4591. (outline-get-next-sibling)
  4592. (outline-get-next-sibling))
  4593. (if (org-at-heading-p)
  4594. (point-at-eol)
  4595. (point))))
  4596. (level (looking-at "\\*+"))
  4597. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4598. (save-excursion
  4599. (save-restriction
  4600. (narrow-to-region beg end)
  4601. (when re
  4602. ;; Properly fold already folded siblings
  4603. (goto-char (point-min))
  4604. (while (re-search-forward re nil t)
  4605. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4606. (hide-entry))))
  4607. (org-cycle-show-empty-lines 'overview)
  4608. (org-cycle-hide-drawers 'overview)))))
  4609. (defun org-cycle-show-empty-lines (state)
  4610. "Show empty lines above all visible headlines.
  4611. The region to be covered depends on STATE when called through
  4612. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4613. entire buffer covered. Note that an empty line is only shown if there
  4614. are at least `org-cycle-separator-lines' empty lines before the headline."
  4615. (when (> org-cycle-separator-lines 0)
  4616. (save-excursion
  4617. (let* ((n org-cycle-separator-lines)
  4618. (re (cond
  4619. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4620. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4621. (t (let ((ns (number-to-string (- n 2))))
  4622. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4623. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4624. beg end)
  4625. (cond
  4626. ((memq state '(overview contents t))
  4627. (setq beg (point-min) end (point-max)))
  4628. ((memq state '(children folded))
  4629. (setq beg (point) end (progn (org-end-of-subtree t t)
  4630. (beginning-of-line 2)
  4631. (point)))))
  4632. (when beg
  4633. (goto-char beg)
  4634. (while (re-search-forward re end t)
  4635. (if (not (get-char-property (match-end 1) 'invisible))
  4636. (outline-flag-region
  4637. (match-beginning 1) (match-end 1) nil)))))))
  4638. ;; Never hide empty lines at the end of the file.
  4639. (save-excursion
  4640. (goto-char (point-max))
  4641. (outline-previous-heading)
  4642. (outline-end-of-heading)
  4643. (if (and (looking-at "[ \t\n]+")
  4644. (= (match-end 0) (point-max)))
  4645. (outline-flag-region (point) (match-end 0) nil))))
  4646. (defun org-show-empty-lines-in-parent ()
  4647. "Move to the parent and re-show empty lines before visible headlines."
  4648. (save-excursion
  4649. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4650. (org-cycle-show-empty-lines context))))
  4651. (defun org-cycle-hide-drawers (state)
  4652. "Re-hide all drawers after a visibility state change."
  4653. (when (and (org-mode-p)
  4654. (not (memq state '(overview folded contents))))
  4655. (save-excursion
  4656. (let* ((globalp (memq state '(contents all)))
  4657. (beg (if globalp (point-min) (point)))
  4658. (end (if globalp (point-max)
  4659. (if (eq state 'children)
  4660. (save-excursion (outline-next-heading) (point))
  4661. (org-end-of-subtree t)))))
  4662. (goto-char beg)
  4663. (while (re-search-forward org-drawer-regexp end t)
  4664. (org-flag-drawer t))))))
  4665. (defun org-flag-drawer (flag)
  4666. (save-excursion
  4667. (beginning-of-line 1)
  4668. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4669. (let ((b (match-end 0))
  4670. (outline-regexp org-outline-regexp))
  4671. (if (re-search-forward
  4672. "^[ \t]*:END:"
  4673. (save-excursion (outline-next-heading) (point)) t)
  4674. (outline-flag-region b (point-at-eol) flag)
  4675. (error ":END: line missing"))))))
  4676. (defun org-subtree-end-visible-p ()
  4677. "Is the end of the current subtree visible?"
  4678. (pos-visible-in-window-p
  4679. (save-excursion (org-end-of-subtree t) (point))))
  4680. (defun org-first-headline-recenter (&optional N)
  4681. "Move cursor to the first headline and recenter the headline.
  4682. Optional argument N means, put the headline into the Nth line of the window."
  4683. (goto-char (point-min))
  4684. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4685. (beginning-of-line)
  4686. (recenter (prefix-numeric-value N))))
  4687. ;;; Folding of blocks
  4688. (defconst org-block-regexp
  4689. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4690. "Regular expression for hiding blocks.")
  4691. (defvar org-hide-block-overlays nil
  4692. "Overays hiding blocks.")
  4693. (make-variable-buffer-local 'org-hide-block-overlays)
  4694. (defun org-block-map (function &optional start end)
  4695. "Call func at the head of all source blocks in the current
  4696. buffer. Optional arguments START and END can be used to limit
  4697. the range."
  4698. (let ((start (or start (point-min)))
  4699. (end (or end (point-max))))
  4700. (save-excursion
  4701. (goto-char start)
  4702. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4703. (save-excursion
  4704. (save-match-data
  4705. (goto-char (match-beginning 0))
  4706. (funcall function)))))))
  4707. (defun org-hide-block-toggle-all ()
  4708. "Toggle the visibility of all blocks in the current buffer."
  4709. (org-block-map #'org-hide-block-toggle))
  4710. (defun org-hide-block-all ()
  4711. "Fold all blocks in the current buffer."
  4712. (interactive)
  4713. (org-show-block-all)
  4714. (org-block-map #'org-hide-block-toggle-maybe))
  4715. (defun org-show-block-all ()
  4716. "Unfold all blocks in the current buffer."
  4717. (mapc 'org-delete-overlay org-hide-block-overlays)
  4718. (setq org-hide-block-overlays nil))
  4719. (defun org-hide-block-toggle-maybe ()
  4720. "Toggle visibility of block at point."
  4721. (interactive)
  4722. (let ((case-fold-search t))
  4723. (if (save-excursion
  4724. (beginning-of-line 1)
  4725. (looking-at org-block-regexp))
  4726. (progn (org-hide-block-toggle)
  4727. t) ;; to signal that we took action
  4728. nil))) ;; to signal that we did not
  4729. (defun org-hide-block-toggle (&optional force)
  4730. "Toggle the visibility of the current block."
  4731. (interactive)
  4732. (save-excursion
  4733. (beginning-of-line)
  4734. (if (re-search-forward org-block-regexp nil t)
  4735. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4736. (end (match-end 0))
  4737. ov) ;; end of entire body
  4738. (if (memq t (mapcar (lambda (overlay)
  4739. (eq (org-overlay-get overlay 'invisible)
  4740. 'org-hide-block))
  4741. (org-overlays-at start)))
  4742. (if (or (not force) (eq force 'off))
  4743. (mapc (lambda (ov)
  4744. (when (member ov org-hide-block-overlays)
  4745. (setq org-hide-block-overlays
  4746. (delq ov org-hide-block-overlays)))
  4747. (when (eq (org-overlay-get ov 'invisible)
  4748. 'org-hide-block)
  4749. (org-delete-overlay ov)))
  4750. (org-overlays-at start)))
  4751. (setq ov (org-make-overlay start end))
  4752. (org-overlay-put ov 'invisible 'org-hide-block)
  4753. (push ov org-hide-block-overlays)))
  4754. (error "Not looking at a source block"))))
  4755. ;; org-tab-after-check-for-cycling-hook
  4756. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4757. ;; Remove overlays when changing major mode
  4758. (add-hook 'org-mode-hook
  4759. (lambda () (org-add-hook 'change-major-mode-hook
  4760. 'org-show-block-all 'append 'local)))
  4761. ;;; Org-goto
  4762. (defvar org-goto-window-configuration nil)
  4763. (defvar org-goto-marker nil)
  4764. (defvar org-goto-map
  4765. (let ((map (make-sparse-keymap)))
  4766. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4767. (while (setq cmd (pop cmds))
  4768. (substitute-key-definition cmd cmd map global-map)))
  4769. (suppress-keymap map)
  4770. (org-defkey map "\C-m" 'org-goto-ret)
  4771. (org-defkey map [(return)] 'org-goto-ret)
  4772. (org-defkey map [(left)] 'org-goto-left)
  4773. (org-defkey map [(right)] 'org-goto-right)
  4774. (org-defkey map [(control ?g)] 'org-goto-quit)
  4775. (org-defkey map "\C-i" 'org-cycle)
  4776. (org-defkey map [(tab)] 'org-cycle)
  4777. (org-defkey map [(down)] 'outline-next-visible-heading)
  4778. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4779. (if org-goto-auto-isearch
  4780. (if (fboundp 'define-key-after)
  4781. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4782. nil)
  4783. (org-defkey map "q" 'org-goto-quit)
  4784. (org-defkey map "n" 'outline-next-visible-heading)
  4785. (org-defkey map "p" 'outline-previous-visible-heading)
  4786. (org-defkey map "f" 'outline-forward-same-level)
  4787. (org-defkey map "b" 'outline-backward-same-level)
  4788. (org-defkey map "u" 'outline-up-heading))
  4789. (org-defkey map "/" 'org-occur)
  4790. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4791. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4792. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4793. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4794. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4795. map))
  4796. (defconst org-goto-help
  4797. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4798. RET=jump to location [Q]uit and return to previous location
  4799. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4800. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4801. ;; FIXME: Docstring doe not mention both interfaces
  4802. (defun org-goto (&optional alternative-interface)
  4803. "Look up a different location in the current file, keeping current visibility.
  4804. When you want look-up or go to a different location in a document, the
  4805. fastest way is often to fold the entire buffer and then dive into the tree.
  4806. This method has the disadvantage, that the previous location will be folded,
  4807. which may not be what you want.
  4808. This command works around this by showing a copy of the current buffer
  4809. in an indirect buffer, in overview mode. You can dive into the tree in
  4810. that copy, use org-occur and incremental search to find a location.
  4811. When pressing RET or `Q', the command returns to the original buffer in
  4812. which the visibility is still unchanged. After RET is will also jump to
  4813. the location selected in the indirect buffer and expose the
  4814. the headline hierarchy above."
  4815. (interactive "P")
  4816. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4817. (org-refile-use-outline-path t)
  4818. (org-refile-target-verify-function nil)
  4819. (interface
  4820. (if (not alternative-interface)
  4821. org-goto-interface
  4822. (if (eq org-goto-interface 'outline)
  4823. 'outline-path-completion
  4824. 'outline)))
  4825. (org-goto-start-pos (point))
  4826. (selected-point
  4827. (if (eq interface 'outline)
  4828. (car (org-get-location (current-buffer) org-goto-help))
  4829. (nth 3 (org-refile-get-location "Goto: ")))))
  4830. (if selected-point
  4831. (progn
  4832. (org-mark-ring-push org-goto-start-pos)
  4833. (goto-char selected-point)
  4834. (if (or (org-invisible-p) (org-invisible-p2))
  4835. (org-show-context 'org-goto)))
  4836. (message "Quit"))))
  4837. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4838. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4839. (defvar org-goto-local-auto-isearch-map) ; defined below
  4840. (defun org-get-location (buf help)
  4841. "Let the user select a location in the Org-mode buffer BUF.
  4842. This function uses a recursive edit. It returns the selected position
  4843. or nil."
  4844. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4845. (isearch-hide-immediately nil)
  4846. (isearch-search-fun-function
  4847. (lambda () 'org-goto-local-search-headings))
  4848. (org-goto-selected-point org-goto-exit-command))
  4849. (save-excursion
  4850. (save-window-excursion
  4851. (delete-other-windows)
  4852. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4853. (switch-to-buffer
  4854. (condition-case nil
  4855. (make-indirect-buffer (current-buffer) "*org-goto*")
  4856. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4857. (with-output-to-temp-buffer "*Help*"
  4858. (princ help))
  4859. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4860. (setq buffer-read-only nil)
  4861. (let ((org-startup-truncated t)
  4862. (org-startup-folded nil)
  4863. (org-startup-align-all-tables nil))
  4864. (org-mode)
  4865. (org-overview))
  4866. (setq buffer-read-only t)
  4867. (if (and (boundp 'org-goto-start-pos)
  4868. (integer-or-marker-p org-goto-start-pos))
  4869. (let ((org-show-hierarchy-above t)
  4870. (org-show-siblings t)
  4871. (org-show-following-heading t))
  4872. (goto-char org-goto-start-pos)
  4873. (and (org-invisible-p) (org-show-context)))
  4874. (goto-char (point-min)))
  4875. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4876. (message "Select location and press RET")
  4877. (use-local-map org-goto-map)
  4878. (recursive-edit)
  4879. ))
  4880. (kill-buffer "*org-goto*")
  4881. (cons org-goto-selected-point org-goto-exit-command)))
  4882. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4883. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4884. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4885. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4886. (defun org-goto-local-search-headings (string bound noerror)
  4887. "Search and make sure that any matches are in headlines."
  4888. (catch 'return
  4889. (while (if isearch-forward
  4890. (search-forward string bound noerror)
  4891. (search-backward string bound noerror))
  4892. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4893. (and (member :headline context)
  4894. (not (member :tags context))))
  4895. (throw 'return (point))))))
  4896. (defun org-goto-local-auto-isearch ()
  4897. "Start isearch."
  4898. (interactive)
  4899. (goto-char (point-min))
  4900. (let ((keys (this-command-keys)))
  4901. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4902. (isearch-mode t)
  4903. (isearch-process-search-char (string-to-char keys)))))
  4904. (defun org-goto-ret (&optional arg)
  4905. "Finish `org-goto' by going to the new location."
  4906. (interactive "P")
  4907. (setq org-goto-selected-point (point)
  4908. org-goto-exit-command 'return)
  4909. (throw 'exit nil))
  4910. (defun org-goto-left ()
  4911. "Finish `org-goto' by going to the new location."
  4912. (interactive)
  4913. (if (org-on-heading-p)
  4914. (progn
  4915. (beginning-of-line 1)
  4916. (setq org-goto-selected-point (point)
  4917. org-goto-exit-command 'left)
  4918. (throw 'exit nil))
  4919. (error "Not on a heading")))
  4920. (defun org-goto-right ()
  4921. "Finish `org-goto' by going to the new location."
  4922. (interactive)
  4923. (if (org-on-heading-p)
  4924. (progn
  4925. (setq org-goto-selected-point (point)
  4926. org-goto-exit-command 'right)
  4927. (throw 'exit nil))
  4928. (error "Not on a heading")))
  4929. (defun org-goto-quit ()
  4930. "Finish `org-goto' without cursor motion."
  4931. (interactive)
  4932. (setq org-goto-selected-point nil)
  4933. (setq org-goto-exit-command 'quit)
  4934. (throw 'exit nil))
  4935. ;;; Indirect buffer display of subtrees
  4936. (defvar org-indirect-dedicated-frame nil
  4937. "This is the frame being used for indirect tree display.")
  4938. (defvar org-last-indirect-buffer nil)
  4939. (defun org-tree-to-indirect-buffer (&optional arg)
  4940. "Create indirect buffer and narrow it to current subtree.
  4941. With numerical prefix ARG, go up to this level and then take that tree.
  4942. If ARG is negative, go up that many levels.
  4943. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4944. indirect buffer previously made with this command, to avoid proliferation of
  4945. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4946. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4947. is kept so that you can work with several indirect buffers at the same time.
  4948. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4949. requests that a new frame be made for the new buffer, so that the dedicated
  4950. frame is not changed."
  4951. (interactive "P")
  4952. (let ((cbuf (current-buffer))
  4953. (cwin (selected-window))
  4954. (pos (point))
  4955. beg end level heading ibuf)
  4956. (save-excursion
  4957. (org-back-to-heading t)
  4958. (when (numberp arg)
  4959. (setq level (org-outline-level))
  4960. (if (< arg 0) (setq arg (+ level arg)))
  4961. (while (> (setq level (org-outline-level)) arg)
  4962. (outline-up-heading 1 t)))
  4963. (setq beg (point)
  4964. heading (org-get-heading))
  4965. (org-end-of-subtree t) (setq end (point)))
  4966. (if (and (buffer-live-p org-last-indirect-buffer)
  4967. (not (eq org-indirect-buffer-display 'new-frame))
  4968. (not arg))
  4969. (kill-buffer org-last-indirect-buffer))
  4970. (setq ibuf (org-get-indirect-buffer cbuf)
  4971. org-last-indirect-buffer ibuf)
  4972. (cond
  4973. ((or (eq org-indirect-buffer-display 'new-frame)
  4974. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4975. (select-frame (make-frame))
  4976. (delete-other-windows)
  4977. (switch-to-buffer ibuf)
  4978. (org-set-frame-title heading))
  4979. ((eq org-indirect-buffer-display 'dedicated-frame)
  4980. (raise-frame
  4981. (select-frame (or (and org-indirect-dedicated-frame
  4982. (frame-live-p org-indirect-dedicated-frame)
  4983. org-indirect-dedicated-frame)
  4984. (setq org-indirect-dedicated-frame (make-frame)))))
  4985. (delete-other-windows)
  4986. (switch-to-buffer ibuf)
  4987. (org-set-frame-title (concat "Indirect: " heading)))
  4988. ((eq org-indirect-buffer-display 'current-window)
  4989. (switch-to-buffer ibuf))
  4990. ((eq org-indirect-buffer-display 'other-window)
  4991. (pop-to-buffer ibuf))
  4992. (t (error "Invalid value.")))
  4993. (if (featurep 'xemacs)
  4994. (save-excursion (org-mode) (turn-on-font-lock)))
  4995. (narrow-to-region beg end)
  4996. (show-all)
  4997. (goto-char pos)
  4998. (and (window-live-p cwin) (select-window cwin))))
  4999. (defun org-get-indirect-buffer (&optional buffer)
  5000. (setq buffer (or buffer (current-buffer)))
  5001. (let ((n 1) (base (buffer-name buffer)) bname)
  5002. (while (buffer-live-p
  5003. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5004. (setq n (1+ n)))
  5005. (condition-case nil
  5006. (make-indirect-buffer buffer bname 'clone)
  5007. (error (make-indirect-buffer buffer bname)))))
  5008. (defun org-set-frame-title (title)
  5009. "Set the title of the current frame to the string TITLE."
  5010. ;; FIXME: how to name a single frame in XEmacs???
  5011. (unless (featurep 'xemacs)
  5012. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5013. ;;;; Structure editing
  5014. ;;; Inserting headlines
  5015. (defun org-previous-line-empty-p ()
  5016. (save-excursion
  5017. (and (not (bobp))
  5018. (or (beginning-of-line 0) t)
  5019. (save-match-data
  5020. (looking-at "[ \t]*$")))))
  5021. (defun org-insert-heading (&optional force-heading)
  5022. "Insert a new heading or item with same depth at point.
  5023. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5024. If point is at the beginning of a headline, insert a sibling before the
  5025. current headline. If point is not at the beginning, do not split the line,
  5026. but create the new headline after the current line."
  5027. (interactive "P")
  5028. (if (= (buffer-size) 0)
  5029. (insert "\n* ")
  5030. (when (or force-heading (not (org-insert-item)))
  5031. (let* ((empty-line-p nil)
  5032. (head (save-excursion
  5033. (condition-case nil
  5034. (progn
  5035. (org-back-to-heading)
  5036. (setq empty-line-p (org-previous-line-empty-p))
  5037. (match-string 0))
  5038. (error "*"))))
  5039. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5040. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5041. pos hide-previous previous-pos)
  5042. (cond
  5043. ((and (org-on-heading-p) (bolp)
  5044. (or (bobp)
  5045. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5046. ;; insert before the current line
  5047. (open-line (if blank 2 1)))
  5048. ((and (bolp)
  5049. (or (bobp)
  5050. (save-excursion
  5051. (backward-char 1) (not (org-invisible-p)))))
  5052. ;; insert right here
  5053. nil)
  5054. (t
  5055. ;; somewhere in the line
  5056. (save-excursion
  5057. (setq previous-pos (point-at-bol))
  5058. (end-of-line)
  5059. (setq hide-previous (org-invisible-p)))
  5060. (and org-insert-heading-respect-content (org-show-subtree))
  5061. (let ((split
  5062. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5063. (save-excursion
  5064. (let ((p (point)))
  5065. (goto-char (point-at-bol))
  5066. (and (looking-at org-complex-heading-regexp)
  5067. (> p (match-beginning 4)))))))
  5068. tags pos)
  5069. (cond
  5070. (org-insert-heading-respect-content
  5071. (org-end-of-subtree nil t)
  5072. (or (bolp) (newline))
  5073. (or (org-previous-line-empty-p)
  5074. (and blank (newline)))
  5075. (open-line 1))
  5076. ((org-on-heading-p)
  5077. (when hide-previous
  5078. (show-children)
  5079. (org-show-entry))
  5080. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5081. (setq tags (and (match-end 2) (match-string 2)))
  5082. (and (match-end 1)
  5083. (delete-region (match-beginning 1) (match-end 1)))
  5084. (setq pos (point-at-bol))
  5085. (or split (end-of-line 1))
  5086. (delete-horizontal-space)
  5087. (newline (if blank 2 1))
  5088. (when tags
  5089. (save-excursion
  5090. (goto-char pos)
  5091. (end-of-line 1)
  5092. (insert " " tags)
  5093. (org-set-tags nil 'align))))
  5094. (t
  5095. (or split (end-of-line 1))
  5096. (newline (if blank 2 1)))))))
  5097. (insert head) (just-one-space)
  5098. (setq pos (point))
  5099. (end-of-line 1)
  5100. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5101. (when (and org-insert-heading-respect-content hide-previous)
  5102. (save-excursion
  5103. (goto-char previous-pos)
  5104. (hide-subtree)))
  5105. (run-hooks 'org-insert-heading-hook)))))
  5106. (defun org-get-heading (&optional no-tags)
  5107. "Return the heading of the current entry, without the stars."
  5108. (save-excursion
  5109. (org-back-to-heading t)
  5110. (if (looking-at
  5111. (if no-tags
  5112. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5113. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5114. (match-string 1) "")))
  5115. (defun org-heading-components ()
  5116. "Return the components of the current heading.
  5117. This is a list with the following elements:
  5118. - the level as an integer
  5119. - the reduced level, different if `org-odd-levels-only' is set.
  5120. - the TODO keyword, or nil
  5121. - the priority character, like ?A, or nil if no priority is given
  5122. - the headline text itself, or the tags string if no headline text
  5123. - the tags string, or nil."
  5124. (save-excursion
  5125. (org-back-to-heading t)
  5126. (if (looking-at org-complex-heading-regexp)
  5127. (list (length (match-string 1))
  5128. (org-reduced-level (length (match-string 1)))
  5129. (org-match-string-no-properties 2)
  5130. (and (match-end 3) (aref (match-string 3) 2))
  5131. (org-match-string-no-properties 4)
  5132. (org-match-string-no-properties 5)))))
  5133. (defun org-get-entry ()
  5134. "Get the entry text, after heading, entire subtree."
  5135. (save-excursion
  5136. (org-back-to-heading t)
  5137. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5138. (defun org-insert-heading-after-current ()
  5139. "Insert a new heading with same level as current, after current subtree."
  5140. (interactive)
  5141. (org-back-to-heading)
  5142. (org-insert-heading)
  5143. (org-move-subtree-down)
  5144. (end-of-line 1))
  5145. (defun org-insert-heading-respect-content ()
  5146. (interactive)
  5147. (let ((org-insert-heading-respect-content t))
  5148. (org-insert-heading t)))
  5149. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5150. (interactive "P")
  5151. (let ((org-insert-heading-respect-content t))
  5152. (org-insert-todo-heading force-state t)))
  5153. (defun org-insert-todo-heading (arg &optional force-heading)
  5154. "Insert a new heading with the same level and TODO state as current heading.
  5155. If the heading has no TODO state, or if the state is DONE, use the first
  5156. state (TODO by default). Also with prefix arg, force first state."
  5157. (interactive "P")
  5158. (when (or force-heading (not (org-insert-item 'checkbox)))
  5159. (org-insert-heading force-heading)
  5160. (save-excursion
  5161. (org-back-to-heading)
  5162. (outline-previous-heading)
  5163. (looking-at org-todo-line-regexp))
  5164. (let*
  5165. ((new-mark-x
  5166. (if (or arg
  5167. (not (match-beginning 2))
  5168. (member (match-string 2) org-done-keywords))
  5169. (car org-todo-keywords-1)
  5170. (match-string 2)))
  5171. (new-mark
  5172. (or
  5173. (run-hook-with-args-until-success
  5174. 'org-todo-get-default-hook new-mark-x nil)
  5175. new-mark-x)))
  5176. (beginning-of-line 1)
  5177. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5178. (if org-treat-insert-todo-heading-as-state-change
  5179. (org-todo new-mark)
  5180. (insert new-mark " "))))
  5181. (when org-provide-todo-statistics
  5182. (org-update-parent-todo-statistics))))
  5183. (defun org-insert-subheading (arg)
  5184. "Insert a new subheading and demote it.
  5185. Works for outline headings and for plain lists alike."
  5186. (interactive "P")
  5187. (org-insert-heading arg)
  5188. (cond
  5189. ((org-on-heading-p) (org-do-demote))
  5190. ((org-at-item-p) (org-indent-item 1))))
  5191. (defun org-insert-todo-subheading (arg)
  5192. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5193. Works for outline headings and for plain lists alike."
  5194. (interactive "P")
  5195. (org-insert-todo-heading arg)
  5196. (cond
  5197. ((org-on-heading-p) (org-do-demote))
  5198. ((org-at-item-p) (org-indent-item 1))))
  5199. ;;; Promotion and Demotion
  5200. (defvar org-after-demote-entry-hook nil
  5201. "Hook run after an entry has been demoted.
  5202. The cursor will be at the beginning of the entry.
  5203. When a subtree is being demoted, the hook will be called for each node.")
  5204. (defvar org-after-promote-entry-hook nil
  5205. "Hook run after an entry has been promoted.
  5206. The cursor will be at the beginning of the entry.
  5207. When a subtree is being promoted, the hook will be called for each node.")
  5208. (defun org-promote-subtree ()
  5209. "Promote the entire subtree.
  5210. See also `org-promote'."
  5211. (interactive)
  5212. (save-excursion
  5213. (org-map-tree 'org-promote))
  5214. (org-fix-position-after-promote))
  5215. (defun org-demote-subtree ()
  5216. "Demote the entire subtree. See `org-demote'.
  5217. See also `org-promote'."
  5218. (interactive)
  5219. (save-excursion
  5220. (org-map-tree 'org-demote))
  5221. (org-fix-position-after-promote))
  5222. (defun org-do-promote ()
  5223. "Promote the current heading higher up the tree.
  5224. If the region is active in `transient-mark-mode', promote all headings
  5225. in the region."
  5226. (interactive)
  5227. (save-excursion
  5228. (if (org-region-active-p)
  5229. (org-map-region 'org-promote (region-beginning) (region-end))
  5230. (org-promote)))
  5231. (org-fix-position-after-promote))
  5232. (defun org-do-demote ()
  5233. "Demote the current heading lower down the tree.
  5234. If the region is active in `transient-mark-mode', demote all headings
  5235. in the region."
  5236. (interactive)
  5237. (save-excursion
  5238. (if (org-region-active-p)
  5239. (org-map-region 'org-demote (region-beginning) (region-end))
  5240. (org-demote)))
  5241. (org-fix-position-after-promote))
  5242. (defun org-fix-position-after-promote ()
  5243. "Make sure that after pro/demotion cursor position is right."
  5244. (let ((pos (point)))
  5245. (when (save-excursion
  5246. (beginning-of-line 1)
  5247. (looking-at org-todo-line-regexp)
  5248. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5249. (cond ((eobp) (insert " "))
  5250. ((eolp) (insert " "))
  5251. ((equal (char-after) ?\ ) (forward-char 1))))))
  5252. (defun org-reduced-level (l)
  5253. "Compute the effective level of a heading.
  5254. This takes into account the setting of `org-odd-levels-only'."
  5255. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5256. (defun org-get-valid-level (level &optional change)
  5257. "Rectify a level change under the influence of `org-odd-levels-only'
  5258. LEVEL is a current level, CHANGE is by how much the level should be
  5259. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5260. even level numbers will become the next higher odd number."
  5261. (if org-odd-levels-only
  5262. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5263. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5264. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5265. (max 1 (+ level (or change 0)))))
  5266. (if (boundp 'define-obsolete-function-alias)
  5267. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5268. (define-obsolete-function-alias 'org-get-legal-level
  5269. 'org-get-valid-level)
  5270. (define-obsolete-function-alias 'org-get-legal-level
  5271. 'org-get-valid-level "23.1")))
  5272. (defun org-promote ()
  5273. "Promote the current heading higher up the tree.
  5274. If the region is active in `transient-mark-mode', promote all headings
  5275. in the region."
  5276. (org-back-to-heading t)
  5277. (let* ((level (save-match-data (funcall outline-level)))
  5278. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5279. (diff (abs (- level (length up-head) -1))))
  5280. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5281. (replace-match up-head nil t)
  5282. ;; Fixup tag positioning
  5283. (and org-auto-align-tags (org-set-tags nil t))
  5284. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5285. (run-hooks 'org-after-promote-entry-hook)))
  5286. (defun org-demote ()
  5287. "Demote the current heading lower down the tree.
  5288. If the region is active in `transient-mark-mode', demote all headings
  5289. in the region."
  5290. (org-back-to-heading t)
  5291. (let* ((level (save-match-data (funcall outline-level)))
  5292. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5293. (diff (abs (- level (length down-head) -1))))
  5294. (replace-match down-head nil t)
  5295. ;; Fixup tag positioning
  5296. (and org-auto-align-tags (org-set-tags nil t))
  5297. (if org-adapt-indentation (org-fixup-indentation diff))
  5298. (run-hooks 'org-after-demote-entry-hook)))
  5299. (defun org-map-tree (fun)
  5300. "Call FUN for every heading underneath the current one."
  5301. (org-back-to-heading)
  5302. (let ((level (funcall outline-level)))
  5303. (save-excursion
  5304. (funcall fun)
  5305. (while (and (progn
  5306. (outline-next-heading)
  5307. (> (funcall outline-level) level))
  5308. (not (eobp)))
  5309. (funcall fun)))))
  5310. (defun org-map-region (fun beg end)
  5311. "Call FUN for every heading between BEG and END."
  5312. (let ((org-ignore-region t))
  5313. (save-excursion
  5314. (setq end (copy-marker end))
  5315. (goto-char beg)
  5316. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5317. (< (point) end))
  5318. (funcall fun))
  5319. (while (and (progn
  5320. (outline-next-heading)
  5321. (< (point) end))
  5322. (not (eobp)))
  5323. (funcall fun)))))
  5324. (defun org-fixup-indentation (diff)
  5325. "Change the indentation in the current entry by DIFF
  5326. However, if any line in the current entry has no indentation, or if it
  5327. would end up with no indentation after the change, nothing at all is done."
  5328. (save-excursion
  5329. (let ((end (save-excursion (outline-next-heading)
  5330. (point-marker)))
  5331. (prohibit (if (> diff 0)
  5332. "^\\S-"
  5333. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5334. col)
  5335. (unless (save-excursion (end-of-line 1)
  5336. (re-search-forward prohibit end t))
  5337. (while (and (< (point) end)
  5338. (re-search-forward "^[ \t]+" end t))
  5339. (goto-char (match-end 0))
  5340. (setq col (current-column))
  5341. (if (< diff 0) (replace-match ""))
  5342. (org-indent-to-column (+ diff col))))
  5343. (move-marker end nil))))
  5344. (defun org-convert-to-odd-levels ()
  5345. "Convert an org-mode file with all levels allowed to one with odd levels.
  5346. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5347. level 5 etc."
  5348. (interactive)
  5349. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5350. (let ((org-odd-levels-only nil) n)
  5351. (save-excursion
  5352. (goto-char (point-min))
  5353. (while (re-search-forward "^\\*\\*+ " nil t)
  5354. (setq n (- (length (match-string 0)) 2))
  5355. (while (>= (setq n (1- n)) 0)
  5356. (org-demote))
  5357. (end-of-line 1))))))
  5358. (defun org-convert-to-oddeven-levels ()
  5359. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5360. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5361. section with an even level, conversion would destroy the structure of the file. An error
  5362. is signaled in this case."
  5363. (interactive)
  5364. (goto-char (point-min))
  5365. ;; First check if there are no even levels
  5366. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5367. (org-show-context t)
  5368. (error "Not all levels are odd in this file. Conversion not possible."))
  5369. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5370. (let ((org-odd-levels-only nil) n)
  5371. (save-excursion
  5372. (goto-char (point-min))
  5373. (while (re-search-forward "^\\*\\*+ " nil t)
  5374. (setq n (/ (1- (length (match-string 0))) 2))
  5375. (while (>= (setq n (1- n)) 0)
  5376. (org-promote))
  5377. (end-of-line 1))))))
  5378. (defun org-tr-level (n)
  5379. "Make N odd if required."
  5380. (if org-odd-levels-only (1+ (/ n 2)) n))
  5381. ;;; Vertical tree motion, cutting and pasting of subtrees
  5382. (defun org-move-subtree-up (&optional arg)
  5383. "Move the current subtree up past ARG headlines of the same level."
  5384. (interactive "p")
  5385. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5386. (defun org-move-subtree-down (&optional arg)
  5387. "Move the current subtree down past ARG headlines of the same level."
  5388. (interactive "p")
  5389. (setq arg (prefix-numeric-value arg))
  5390. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5391. 'outline-get-last-sibling))
  5392. (ins-point (make-marker))
  5393. (cnt (abs arg))
  5394. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5395. ;; Select the tree
  5396. (org-back-to-heading)
  5397. (setq beg0 (point))
  5398. (save-excursion
  5399. (setq ne-beg (org-back-over-empty-lines))
  5400. (setq beg (point)))
  5401. (save-match-data
  5402. (save-excursion (outline-end-of-heading)
  5403. (setq folded (org-invisible-p)))
  5404. (outline-end-of-subtree))
  5405. (outline-next-heading)
  5406. (setq ne-end (org-back-over-empty-lines))
  5407. (setq end (point))
  5408. (goto-char beg0)
  5409. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5410. ;; include less whitespace
  5411. (save-excursion
  5412. (goto-char beg)
  5413. (forward-line (- ne-beg ne-end))
  5414. (setq beg (point))))
  5415. ;; Find insertion point, with error handling
  5416. (while (> cnt 0)
  5417. (or (and (funcall movfunc) (looking-at outline-regexp))
  5418. (progn (goto-char beg0)
  5419. (error "Cannot move past superior level or buffer limit")))
  5420. (setq cnt (1- cnt)))
  5421. (if (> arg 0)
  5422. ;; Moving forward - still need to move over subtree
  5423. (progn (org-end-of-subtree t t)
  5424. (save-excursion
  5425. (org-back-over-empty-lines)
  5426. (or (bolp) (newline)))))
  5427. (setq ne-ins (org-back-over-empty-lines))
  5428. (move-marker ins-point (point))
  5429. (setq txt (buffer-substring beg end))
  5430. (org-save-markers-in-region beg end)
  5431. (delete-region beg end)
  5432. (org-remove-empty-overlays-at beg)
  5433. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5434. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5435. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5436. (let ((bbb (point)))
  5437. (insert-before-markers txt)
  5438. (org-reinstall-markers-in-region bbb)
  5439. (move-marker ins-point bbb))
  5440. (or (bolp) (insert "\n"))
  5441. (setq ins-end (point))
  5442. (goto-char ins-point)
  5443. (org-skip-whitespace)
  5444. (when (and (< arg 0)
  5445. (org-first-sibling-p)
  5446. (> ne-ins ne-beg))
  5447. ;; Move whitespace back to beginning
  5448. (save-excursion
  5449. (goto-char ins-end)
  5450. (let ((kill-whole-line t))
  5451. (kill-line (- ne-ins ne-beg)) (point)))
  5452. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5453. (move-marker ins-point nil)
  5454. (if folded
  5455. (hide-entry)
  5456. (org-show-entry)
  5457. (show-children)
  5458. (org-cycle-hide-drawers 'children))
  5459. (org-clean-visibility-after-subtree-move)))
  5460. (defvar org-subtree-clip ""
  5461. "Clipboard for cut and paste of subtrees.
  5462. This is actually only a copy of the kill, because we use the normal kill
  5463. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5464. (defvar org-subtree-clip-folded nil
  5465. "Was the last copied subtree folded?
  5466. This is used to fold the tree back after pasting.")
  5467. (defun org-cut-subtree (&optional n)
  5468. "Cut the current subtree into the clipboard.
  5469. With prefix arg N, cut this many sequential subtrees.
  5470. This is a short-hand for marking the subtree and then cutting it."
  5471. (interactive "p")
  5472. (org-copy-subtree n 'cut))
  5473. (defun org-copy-subtree (&optional n cut force-store-markers)
  5474. "Cut the current subtree into the clipboard.
  5475. With prefix arg N, cut this many sequential subtrees.
  5476. This is a short-hand for marking the subtree and then copying it.
  5477. If CUT is non-nil, actually cut the subtree.
  5478. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5479. of some markers in the region, even if CUT is non-nil. This is
  5480. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5481. (interactive "p")
  5482. (let (beg end folded (beg0 (point)))
  5483. (if (interactive-p)
  5484. (org-back-to-heading nil) ; take what looks like a subtree
  5485. (org-back-to-heading t)) ; take what is really there
  5486. (org-back-over-empty-lines)
  5487. (setq beg (point))
  5488. (skip-chars-forward " \t\r\n")
  5489. (save-match-data
  5490. (save-excursion (outline-end-of-heading)
  5491. (setq folded (org-invisible-p)))
  5492. (condition-case nil
  5493. (outline-forward-same-level (1- n))
  5494. (error nil))
  5495. (org-end-of-subtree t t))
  5496. (org-back-over-empty-lines)
  5497. (setq end (point))
  5498. (goto-char beg0)
  5499. (when (> end beg)
  5500. (setq org-subtree-clip-folded folded)
  5501. (when (or cut force-store-markers)
  5502. (org-save-markers-in-region beg end))
  5503. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5504. (setq org-subtree-clip (current-kill 0))
  5505. (message "%s: Subtree(s) with %d characters"
  5506. (if cut "Cut" "Copied")
  5507. (length org-subtree-clip)))))
  5508. (defun org-paste-subtree (&optional level tree for-yank)
  5509. "Paste the clipboard as a subtree, with modification of headline level.
  5510. The entire subtree is promoted or demoted in order to match a new headline
  5511. level.
  5512. If the cursor is at the beginning of a headline, the same level as
  5513. that headline is used to paste the tree
  5514. If not, the new level is derived from the *visible* headings
  5515. before and after the insertion point, and taken to be the inferior headline
  5516. level of the two. So if the previous visible heading is level 3 and the
  5517. next is level 4 (or vice versa), level 4 will be used for insertion.
  5518. This makes sure that the subtree remains an independent subtree and does
  5519. not swallow low level entries.
  5520. You can also force a different level, either by using a numeric prefix
  5521. argument, or by inserting the heading marker by hand. For example, if the
  5522. cursor is after \"*****\", then the tree will be shifted to level 5.
  5523. If optional TREE is given, use this text instead of the kill ring.
  5524. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5525. move back over whitespace before inserting, and move point to the end of
  5526. the inserted text when done."
  5527. (interactive "P")
  5528. (unless (org-kill-is-subtree-p tree)
  5529. (error "%s"
  5530. (substitute-command-keys
  5531. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5532. (let* ((visp (not (org-invisible-p)))
  5533. (txt (or tree (and kill-ring (current-kill 0))))
  5534. (^re (concat "^\\(" outline-regexp "\\)"))
  5535. (re (concat "\\(" outline-regexp "\\)"))
  5536. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5537. (old-level (if (string-match ^re txt)
  5538. (- (match-end 0) (match-beginning 0) 1)
  5539. -1))
  5540. (force-level (cond (level (prefix-numeric-value level))
  5541. ((and (looking-at "[ \t]*$")
  5542. (string-match
  5543. ^re_ (buffer-substring
  5544. (point-at-bol) (point))))
  5545. (- (match-end 1) (match-beginning 1)))
  5546. ((and (bolp)
  5547. (looking-at org-outline-regexp))
  5548. (- (match-end 0) (point) 1))
  5549. (t nil)))
  5550. (previous-level (save-excursion
  5551. (condition-case nil
  5552. (progn
  5553. (outline-previous-visible-heading 1)
  5554. (if (looking-at re)
  5555. (- (match-end 0) (match-beginning 0) 1)
  5556. 1))
  5557. (error 1))))
  5558. (next-level (save-excursion
  5559. (condition-case nil
  5560. (progn
  5561. (or (looking-at outline-regexp)
  5562. (outline-next-visible-heading 1))
  5563. (if (looking-at re)
  5564. (- (match-end 0) (match-beginning 0) 1)
  5565. 1))
  5566. (error 1))))
  5567. (new-level (or force-level (max previous-level next-level)))
  5568. (shift (if (or (= old-level -1)
  5569. (= new-level -1)
  5570. (= old-level new-level))
  5571. 0
  5572. (- new-level old-level)))
  5573. (delta (if (> shift 0) -1 1))
  5574. (func (if (> shift 0) 'org-demote 'org-promote))
  5575. (org-odd-levels-only nil)
  5576. beg end newend)
  5577. ;; Remove the forced level indicator
  5578. (if force-level
  5579. (delete-region (point-at-bol) (point)))
  5580. ;; Paste
  5581. (beginning-of-line 1)
  5582. (unless for-yank (org-back-over-empty-lines))
  5583. (setq beg (point))
  5584. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5585. (insert-before-markers txt)
  5586. (unless (string-match "\n\\'" txt) (insert "\n"))
  5587. (setq newend (point))
  5588. (org-reinstall-markers-in-region beg)
  5589. (setq end (point))
  5590. (goto-char beg)
  5591. (skip-chars-forward " \t\n\r")
  5592. (setq beg (point))
  5593. (if (and (org-invisible-p) visp)
  5594. (save-excursion (outline-show-heading)))
  5595. ;; Shift if necessary
  5596. (unless (= shift 0)
  5597. (save-restriction
  5598. (narrow-to-region beg end)
  5599. (while (not (= shift 0))
  5600. (org-map-region func (point-min) (point-max))
  5601. (setq shift (+ delta shift)))
  5602. (goto-char (point-min))
  5603. (setq newend (point-max))))
  5604. (when (or (interactive-p) for-yank)
  5605. (message "Clipboard pasted as level %d subtree" new-level))
  5606. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5607. kill-ring
  5608. (eq org-subtree-clip (current-kill 0))
  5609. org-subtree-clip-folded)
  5610. ;; The tree was folded before it was killed/copied
  5611. (hide-subtree))
  5612. (and for-yank (goto-char newend))))
  5613. (defun org-kill-is-subtree-p (&optional txt)
  5614. "Check if the current kill is an outline subtree, or a set of trees.
  5615. Returns nil if kill does not start with a headline, or if the first
  5616. headline level is not the largest headline level in the tree.
  5617. So this will actually accept several entries of equal levels as well,
  5618. which is OK for `org-paste-subtree'.
  5619. If optional TXT is given, check this string instead of the current kill."
  5620. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5621. (start-level (and kill
  5622. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5623. org-outline-regexp "\\)")
  5624. kill)
  5625. (- (match-end 2) (match-beginning 2) 1)))
  5626. (re (concat "^" org-outline-regexp))
  5627. (start (1+ (or (match-beginning 2) -1))))
  5628. (if (not start-level)
  5629. (progn
  5630. nil) ;; does not even start with a heading
  5631. (catch 'exit
  5632. (while (setq start (string-match re kill (1+ start)))
  5633. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5634. (throw 'exit nil)))
  5635. t))))
  5636. (defvar org-markers-to-move nil
  5637. "Markers that should be moved with a cut-and-paste operation.
  5638. Those markers are stored together with their positions relative to
  5639. the start of the region.")
  5640. (defun org-save-markers-in-region (beg end)
  5641. "Check markers in region.
  5642. If these markers are between BEG and END, record their position relative
  5643. to BEG, so that after moving the block of text, we can put the markers back
  5644. into place.
  5645. This function gets called just before an entry or tree gets cut from the
  5646. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5647. called immediately, to move the markers with the entries."
  5648. (setq org-markers-to-move nil)
  5649. (when (featurep 'org-clock)
  5650. (org-clock-save-markers-for-cut-and-paste beg end))
  5651. (when (featurep 'org-agenda)
  5652. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5653. (defun org-check-and-save-marker (marker beg end)
  5654. "Check if MARKER is between BEG and END.
  5655. If yes, remember the marker and the distance to BEG."
  5656. (when (and (marker-buffer marker)
  5657. (equal (marker-buffer marker) (current-buffer)))
  5658. (if (and (>= marker beg) (< marker end))
  5659. (push (cons marker (- marker beg)) org-markers-to-move))))
  5660. (defun org-reinstall-markers-in-region (beg)
  5661. "Move all remembered markers to their position relative to BEG."
  5662. (mapc (lambda (x)
  5663. (move-marker (car x) (+ beg (cdr x))))
  5664. org-markers-to-move)
  5665. (setq org-markers-to-move nil))
  5666. (defun org-narrow-to-subtree ()
  5667. "Narrow buffer to the current subtree."
  5668. (interactive)
  5669. (save-excursion
  5670. (save-match-data
  5671. (narrow-to-region
  5672. (progn (org-back-to-heading t) (point))
  5673. (progn (org-end-of-subtree t) (point))))))
  5674. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5675. "Clone the task (subtree) at point N times.
  5676. The clones will be inserted as siblings.
  5677. In interactive use, the user will be prompted for the number of clones
  5678. to be produced, and for a time SHIFT, which may be a repeater as used
  5679. in time stamps, for example `+3d'.
  5680. When a valid repeater is given and the entry contains any time stamps,
  5681. the clones will become a sequence in time, with time stamps in the
  5682. subtree shifted for each clone produced. If SHIFT is nil or the
  5683. empty string, time stamps will be left alone.
  5684. If the original subtree did contain time stamps with a repeater,
  5685. the following will happen:
  5686. - the repeater will be removed in each clone
  5687. - an additional clone will be produced, with the current, unshifted
  5688. date(s) in the entry.
  5689. - the original entry will be placed *after* all the clones, with
  5690. repeater intact.
  5691. - the start days in the repeater in the original entry will be shifted
  5692. to past the last clone.
  5693. I this way you can spell out a number of instances of a repeating task,
  5694. and still retain the repeater to cover future instances of the task."
  5695. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5696. (let (beg end template task
  5697. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5698. (if (not (and (integerp n) (> n 0)))
  5699. (error "Invalid number of replications %s" n))
  5700. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5701. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5702. shift)))
  5703. (error "Invalid shift specification %s" shift))
  5704. (when doshift
  5705. (setq shift-n (string-to-number (match-string 1 shift))
  5706. shift-what (cdr (assoc (match-string 2 shift)
  5707. '(("d" . day) ("w" . week)
  5708. ("m" . month) ("y" . year))))))
  5709. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5710. (setq nmin 1 nmax n)
  5711. (org-back-to-heading t)
  5712. (setq beg (point))
  5713. (org-end-of-subtree t t)
  5714. (setq end (point))
  5715. (setq template (buffer-substring beg end))
  5716. (when (and doshift
  5717. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5718. (delete-region beg end)
  5719. (setq end beg)
  5720. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5721. (goto-char end)
  5722. (loop for n from nmin to nmax do
  5723. (if (not doshift)
  5724. (setq task template)
  5725. (with-temp-buffer
  5726. (insert template)
  5727. (org-mode)
  5728. (goto-char (point-min))
  5729. (while (re-search-forward org-ts-regexp-both nil t)
  5730. (org-timestamp-change (* n shift-n) shift-what))
  5731. (unless (= n n-no-remove)
  5732. (goto-char (point-min))
  5733. (while (re-search-forward org-ts-regexp nil t)
  5734. (save-excursion
  5735. (goto-char (match-beginning 0))
  5736. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5737. (delete-region (match-beginning 1) (match-end 1))))))
  5738. (setq task (buffer-string))))
  5739. (insert task))
  5740. (goto-char beg)))
  5741. ;;; Outline Sorting
  5742. (defun org-sort (with-case)
  5743. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5744. Optional argument WITH-CASE means sort case-sensitively.
  5745. With a double prefix argument, also remove duplicate entries."
  5746. (interactive "P")
  5747. (if (org-at-table-p)
  5748. (org-call-with-arg 'org-table-sort-lines with-case)
  5749. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5750. (defun org-sort-remove-invisible (s)
  5751. (remove-text-properties 0 (length s) org-rm-props s)
  5752. (while (string-match org-bracket-link-regexp s)
  5753. (setq s (replace-match (if (match-end 2)
  5754. (match-string 3 s)
  5755. (match-string 1 s)) t t s)))
  5756. s)
  5757. (defvar org-priority-regexp) ; defined later in the file
  5758. (defvar org-after-sorting-entries-or-items-hook nil
  5759. "Hook that is run after a bunch of entries or items have been sorted.
  5760. When children are sorted, the cursor is in the parent line when this
  5761. hook gets called. When a region or a plain list is sorted, the cursor
  5762. will be in the first entry of the sorted region/list.")
  5763. (defun org-sort-entries-or-items
  5764. (&optional with-case sorting-type getkey-func compare-func property)
  5765. "Sort entries on a certain level of an outline tree, or plain list items.
  5766. If there is an active region, the entries in the region are sorted.
  5767. Else, if the cursor is before the first entry, sort the top-level items.
  5768. Else, the children of the entry at point are sorted.
  5769. If the cursor is at the first item in a plain list, the list items will be
  5770. sorted.
  5771. Sorting can be alphabetically, numerically, by date/time as given by
  5772. a time stamp, by a property or by priority.
  5773. The command prompts for the sorting type unless it has been given to the
  5774. function through the SORTING-TYPE argument, which needs to a character,
  5775. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5776. precise meaning of each character:
  5777. n Numerically, by converting the beginning of the entry/item to a number.
  5778. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5779. t By date/time, either the first active time stamp in the entry, or, if
  5780. none exist, by the first inactive one.
  5781. In items, only the first line will be chekced.
  5782. s By the scheduled date/time.
  5783. d By deadline date/time.
  5784. c By creation time, which is assumed to be the first inactive time stamp
  5785. at the beginning of a line.
  5786. p By priority according to the cookie.
  5787. r By the value of a property.
  5788. Capital letters will reverse the sort order.
  5789. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5790. called with point at the beginning of the record. It must return either
  5791. a string or a number that should serve as the sorting key for that record.
  5792. Comparing entries ignores case by default. However, with an optional argument
  5793. WITH-CASE, the sorting considers case as well."
  5794. (interactive "P")
  5795. (let ((case-func (if with-case 'identity 'downcase))
  5796. start beg end stars re re2
  5797. txt what tmp plain-list-p)
  5798. ;; Find beginning and end of region to sort
  5799. (cond
  5800. ((org-region-active-p)
  5801. ;; we will sort the region
  5802. (setq end (region-end)
  5803. what "region")
  5804. (goto-char (region-beginning))
  5805. (if (not (org-on-heading-p)) (outline-next-heading))
  5806. (setq start (point)))
  5807. ((org-at-item-p)
  5808. ;; we will sort this plain list
  5809. (org-beginning-of-item-list) (setq start (point))
  5810. (org-end-of-item-list) (setq end (point))
  5811. (goto-char start)
  5812. (setq plain-list-p t
  5813. what "plain list"))
  5814. ((or (org-on-heading-p)
  5815. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5816. ;; we will sort the children of the current headline
  5817. (org-back-to-heading)
  5818. (setq start (point)
  5819. end (progn (org-end-of-subtree t t)
  5820. (org-back-over-empty-lines)
  5821. (point))
  5822. what "children")
  5823. (goto-char start)
  5824. (show-subtree)
  5825. (outline-next-heading))
  5826. (t
  5827. ;; we will sort the top-level entries in this file
  5828. (goto-char (point-min))
  5829. (or (org-on-heading-p) (outline-next-heading))
  5830. (setq start (point) end (point-max) what "top-level")
  5831. (goto-char start)
  5832. (show-all)))
  5833. (setq beg (point))
  5834. (if (>= beg end) (error "Nothing to sort"))
  5835. (unless plain-list-p
  5836. (looking-at "\\(\\*+\\)")
  5837. (setq stars (match-string 1)
  5838. re (concat "^" (regexp-quote stars) " +")
  5839. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5840. txt (buffer-substring beg end))
  5841. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5842. (if (and (not (equal stars "*")) (string-match re2 txt))
  5843. (error "Region to sort contains a level above the first entry")))
  5844. (unless sorting-type
  5845. (message
  5846. (if plain-list-p
  5847. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5848. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5849. [t]ime [s]cheduled [d]eadline [c]reated
  5850. A/N/T/S/D/C/P/O/F means reversed:")
  5851. what)
  5852. (setq sorting-type (read-char-exclusive))
  5853. (and (= (downcase sorting-type) ?f)
  5854. (setq getkey-func
  5855. (org-ido-completing-read "Sort using function: "
  5856. obarray 'fboundp t nil nil))
  5857. (setq getkey-func (intern getkey-func)))
  5858. (and (= (downcase sorting-type) ?r)
  5859. (setq property
  5860. (org-ido-completing-read "Property: "
  5861. (mapcar 'list (org-buffer-property-keys t))
  5862. nil t))))
  5863. (message "Sorting entries...")
  5864. (save-restriction
  5865. (narrow-to-region start end)
  5866. (let ((dcst (downcase sorting-type))
  5867. (case-fold-search nil)
  5868. (now (current-time)))
  5869. (sort-subr
  5870. (/= dcst sorting-type)
  5871. ;; This function moves to the beginning character of the "record" to
  5872. ;; be sorted.
  5873. (if plain-list-p
  5874. (lambda nil
  5875. (if (org-at-item-p) t (goto-char (point-max))))
  5876. (lambda nil
  5877. (if (re-search-forward re nil t)
  5878. (goto-char (match-beginning 0))
  5879. (goto-char (point-max)))))
  5880. ;; This function moves to the last character of the "record" being
  5881. ;; sorted.
  5882. (if plain-list-p
  5883. 'org-end-of-item
  5884. (lambda nil
  5885. (save-match-data
  5886. (condition-case nil
  5887. (outline-forward-same-level 1)
  5888. (error
  5889. (goto-char (point-max)))))))
  5890. ;; This function returns the value that gets sorted against.
  5891. (if plain-list-p
  5892. (lambda nil
  5893. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5894. (cond
  5895. ((= dcst ?n)
  5896. (string-to-number (buffer-substring (match-end 0)
  5897. (point-at-eol))))
  5898. ((= dcst ?a)
  5899. (buffer-substring (match-end 0) (point-at-eol)))
  5900. ((= dcst ?t)
  5901. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5902. (re-search-forward org-ts-regexp-both
  5903. (point-at-eol) t))
  5904. (org-time-string-to-seconds (match-string 0))
  5905. (time-to-seconds now)))
  5906. ((= dcst ?f)
  5907. (if getkey-func
  5908. (progn
  5909. (setq tmp (funcall getkey-func))
  5910. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5911. tmp)
  5912. (error "Invalid key function `%s'" getkey-func)))
  5913. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5914. (lambda nil
  5915. (cond
  5916. ((= dcst ?n)
  5917. (if (looking-at org-complex-heading-regexp)
  5918. (string-to-number (match-string 4))
  5919. nil))
  5920. ((= dcst ?a)
  5921. (if (looking-at org-complex-heading-regexp)
  5922. (funcall case-func (match-string 4))
  5923. nil))
  5924. ((= dcst ?t)
  5925. (let ((end (save-excursion (outline-next-heading) (point))))
  5926. (if (or (re-search-forward org-ts-regexp end t)
  5927. (re-search-forward org-ts-regexp-both end t))
  5928. (org-time-string-to-seconds (match-string 0))
  5929. (time-to-seconds now))))
  5930. ((= dcst ?c)
  5931. (let ((end (save-excursion (outline-next-heading) (point))))
  5932. (if (re-search-forward
  5933. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5934. end t)
  5935. (org-time-string-to-seconds (match-string 0))
  5936. (time-to-seconds now))))
  5937. ((= dcst ?s)
  5938. (let ((end (save-excursion (outline-next-heading) (point))))
  5939. (if (re-search-forward org-scheduled-time-regexp end t)
  5940. (org-time-string-to-seconds (match-string 1))
  5941. (time-to-seconds now))))
  5942. ((= dcst ?d)
  5943. (let ((end (save-excursion (outline-next-heading) (point))))
  5944. (if (re-search-forward org-deadline-time-regexp end t)
  5945. (org-time-string-to-seconds (match-string 1))
  5946. (time-to-seconds now))))
  5947. ((= dcst ?p)
  5948. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5949. (string-to-char (match-string 2))
  5950. org-default-priority))
  5951. ((= dcst ?r)
  5952. (or (org-entry-get nil property) ""))
  5953. ((= dcst ?o)
  5954. (if (looking-at org-complex-heading-regexp)
  5955. (- 9999 (length (member (match-string 2)
  5956. org-todo-keywords-1)))))
  5957. ((= dcst ?f)
  5958. (if getkey-func
  5959. (progn
  5960. (setq tmp (funcall getkey-func))
  5961. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5962. tmp)
  5963. (error "Invalid key function `%s'" getkey-func)))
  5964. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5965. nil
  5966. (cond
  5967. ((= dcst ?a) 'string<)
  5968. ((= dcst ?f) compare-func)
  5969. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5970. (t nil)))))
  5971. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5972. (message "Sorting entries...done")))
  5973. (defun org-do-sort (table what &optional with-case sorting-type)
  5974. "Sort TABLE of WHAT according to SORTING-TYPE.
  5975. The user will be prompted for the SORTING-TYPE if the call to this
  5976. function does not specify it. WHAT is only for the prompt, to indicate
  5977. what is being sorted. The sorting key will be extracted from
  5978. the car of the elements of the table.
  5979. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5980. (unless sorting-type
  5981. (message
  5982. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5983. what)
  5984. (setq sorting-type (read-char-exclusive)))
  5985. (let ((dcst (downcase sorting-type))
  5986. extractfun comparefun)
  5987. ;; Define the appropriate functions
  5988. (cond
  5989. ((= dcst ?n)
  5990. (setq extractfun 'string-to-number
  5991. comparefun (if (= dcst sorting-type) '< '>)))
  5992. ((= dcst ?a)
  5993. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5994. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5995. comparefun (if (= dcst sorting-type)
  5996. 'string<
  5997. (lambda (a b) (and (not (string< a b))
  5998. (not (string= a b)))))))
  5999. ((= dcst ?t)
  6000. (setq extractfun
  6001. (lambda (x)
  6002. (if (or (string-match org-ts-regexp x)
  6003. (string-match org-ts-regexp-both x))
  6004. (time-to-seconds
  6005. (org-time-string-to-time (match-string 0 x)))
  6006. 0))
  6007. comparefun (if (= dcst sorting-type) '< '>)))
  6008. (t (error "Invalid sorting type `%c'" sorting-type)))
  6009. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6010. table)
  6011. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6012. ;;; The orgstruct minor mode
  6013. ;; Define a minor mode which can be used in other modes in order to
  6014. ;; integrate the org-mode structure editing commands.
  6015. ;; This is really a hack, because the org-mode structure commands use
  6016. ;; keys which normally belong to the major mode. Here is how it
  6017. ;; works: The minor mode defines all the keys necessary to operate the
  6018. ;; structure commands, but wraps the commands into a function which
  6019. ;; tests if the cursor is currently at a headline or a plain list
  6020. ;; item. If that is the case, the structure command is used,
  6021. ;; temporarily setting many Org-mode variables like regular
  6022. ;; expressions for filling etc. However, when any of those keys is
  6023. ;; used at a different location, function uses `key-binding' to look
  6024. ;; up if the key has an associated command in another currently active
  6025. ;; keymap (minor modes, major mode, global), and executes that
  6026. ;; command. There might be problems if any of the keys is otherwise
  6027. ;; used as a prefix key.
  6028. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6029. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6030. ;; addresses this by checking explicitly for both bindings.
  6031. (defvar orgstruct-mode-map (make-sparse-keymap)
  6032. "Keymap for the minor `orgstruct-mode'.")
  6033. (defvar org-local-vars nil
  6034. "List of local variables, for use by `orgstruct-mode'")
  6035. ;;;###autoload
  6036. (define-minor-mode orgstruct-mode
  6037. "Toggle the minor more `orgstruct-mode'.
  6038. This mode is for using Org-mode structure commands in other modes.
  6039. The following key behave as if Org-mode was active, if the cursor
  6040. is on a headline, or on a plain list item (both in the definition
  6041. of Org-mode).
  6042. M-up Move entry/item up
  6043. M-down Move entry/item down
  6044. M-left Promote
  6045. M-right Demote
  6046. M-S-up Move entry/item up
  6047. M-S-down Move entry/item down
  6048. M-S-left Promote subtree
  6049. M-S-right Demote subtree
  6050. M-q Fill paragraph and items like in Org-mode
  6051. C-c ^ Sort entries
  6052. C-c - Cycle list bullet
  6053. TAB Cycle item visibility
  6054. M-RET Insert new heading/item
  6055. S-M-RET Insert new TODO heading / Checkbox item
  6056. C-c C-c Set tags / toggle checkbox"
  6057. nil " OrgStruct" nil
  6058. (org-load-modules-maybe)
  6059. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6060. ;;;###autoload
  6061. (defun turn-on-orgstruct ()
  6062. "Unconditionally turn on `orgstruct-mode'."
  6063. (orgstruct-mode 1))
  6064. (defun orgstruct++-mode (&optional arg)
  6065. "Toggle `orgstruct-mode', the enhanced version of it.
  6066. In addition to setting orgstruct-mode, this also exports all indentation
  6067. and autofilling variables from org-mode into the buffer. It will also
  6068. recognize item context in multiline items.
  6069. Note that turning off orgstruct-mode will *not* remove the
  6070. indentation/paragraph settings. This can only be done by refreshing the
  6071. major mode, for example with \\[normal-mode]."
  6072. (interactive "P")
  6073. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6074. (if (< arg 1)
  6075. (orgstruct-mode -1)
  6076. (orgstruct-mode 1)
  6077. (let (var val)
  6078. (mapc
  6079. (lambda (x)
  6080. (when (string-match
  6081. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6082. (symbol-name (car x)))
  6083. (setq var (car x) val (nth 1 x))
  6084. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6085. org-local-vars)
  6086. (org-set-local 'orgstruct-is-++ t))))
  6087. (defvar orgstruct-is-++ nil
  6088. "Is orgstruct-mode in ++ version in the current-buffer?")
  6089. (make-variable-buffer-local 'orgstruct-is-++)
  6090. ;;;###autoload
  6091. (defun turn-on-orgstruct++ ()
  6092. "Unconditionally turn on `orgstruct++-mode'."
  6093. (orgstruct++-mode 1))
  6094. (defun orgstruct-error ()
  6095. "Error when there is no default binding for a structure key."
  6096. (interactive)
  6097. (error "This key has no function outside structure elements"))
  6098. (defun orgstruct-setup ()
  6099. "Setup orgstruct keymaps."
  6100. (let ((nfunc 0)
  6101. (bindings
  6102. (list
  6103. '([(meta up)] org-metaup)
  6104. '([(meta down)] org-metadown)
  6105. '([(meta left)] org-metaleft)
  6106. '([(meta right)] org-metaright)
  6107. '([(meta shift up)] org-shiftmetaup)
  6108. '([(meta shift down)] org-shiftmetadown)
  6109. '([(meta shift left)] org-shiftmetaleft)
  6110. '([(meta shift right)] org-shiftmetaright)
  6111. '([?\e (up)] org-metaup)
  6112. '([?\e (down)] org-metadown)
  6113. '([?\e (left)] org-metaleft)
  6114. '([?\e (right)] org-metaright)
  6115. '([?\e (shift up)] org-shiftmetaup)
  6116. '([?\e (shift down)] org-shiftmetadown)
  6117. '([?\e (shift left)] org-shiftmetaleft)
  6118. '([?\e (shift right)] org-shiftmetaright)
  6119. '([(shift up)] org-shiftup)
  6120. '([(shift down)] org-shiftdown)
  6121. '([(shift left)] org-shiftleft)
  6122. '([(shift right)] org-shiftright)
  6123. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6124. '("\M-q" fill-paragraph)
  6125. '("\C-c^" org-sort)
  6126. '("\C-c-" org-cycle-list-bullet)))
  6127. elt key fun cmd)
  6128. (while (setq elt (pop bindings))
  6129. (setq nfunc (1+ nfunc))
  6130. (setq key (org-key (car elt))
  6131. fun (nth 1 elt)
  6132. cmd (orgstruct-make-binding fun nfunc key))
  6133. (org-defkey orgstruct-mode-map key cmd))
  6134. ;; Special treatment needed for TAB and RET
  6135. (org-defkey orgstruct-mode-map [(tab)]
  6136. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6137. (org-defkey orgstruct-mode-map "\C-i"
  6138. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6139. (org-defkey orgstruct-mode-map "\M-\C-m"
  6140. (orgstruct-make-binding 'org-insert-heading 105
  6141. "\M-\C-m" [(meta return)]))
  6142. (org-defkey orgstruct-mode-map [(meta return)]
  6143. (orgstruct-make-binding 'org-insert-heading 106
  6144. [(meta return)] "\M-\C-m"))
  6145. (org-defkey orgstruct-mode-map [(shift meta return)]
  6146. (orgstruct-make-binding 'org-insert-todo-heading 107
  6147. [(meta return)] "\M-\C-m"))
  6148. (org-defkey orgstruct-mode-map "\e\C-m"
  6149. (orgstruct-make-binding 'org-insert-heading 108
  6150. "\e\C-m" [?\e (return)]))
  6151. (org-defkey orgstruct-mode-map [?\e (return)]
  6152. (orgstruct-make-binding 'org-insert-heading 109
  6153. [?\e (return)] "\e\C-m"))
  6154. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6155. (orgstruct-make-binding 'org-insert-todo-heading 110
  6156. [?\e (return)] "\e\C-m"))
  6157. (unless org-local-vars
  6158. (setq org-local-vars (org-get-local-variables)))
  6159. t))
  6160. (defun orgstruct-make-binding (fun n &rest keys)
  6161. "Create a function for binding in the structure minor mode.
  6162. FUN is the command to call inside a table. N is used to create a unique
  6163. command name. KEYS are keys that should be checked in for a command
  6164. to execute outside of tables."
  6165. (eval
  6166. (list 'defun
  6167. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6168. '(arg)
  6169. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6170. "Outside of structure, run the binding of `"
  6171. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6172. "'.")
  6173. '(interactive "p")
  6174. (list 'if
  6175. `(org-context-p 'headline 'item
  6176. (and orgstruct-is-++
  6177. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6178. 'item-body))
  6179. (list 'org-run-like-in-org-mode (list 'quote fun))
  6180. (list 'let '(orgstruct-mode)
  6181. (list 'call-interactively
  6182. (append '(or)
  6183. (mapcar (lambda (k)
  6184. (list 'key-binding k))
  6185. keys)
  6186. '('orgstruct-error))))))))
  6187. (defun org-context-p (&rest contexts)
  6188. "Check if local context is any of CONTEXTS.
  6189. Possible values in the list of contexts are `table', `headline', and `item'."
  6190. (let ((pos (point)))
  6191. (goto-char (point-at-bol))
  6192. (prog1 (or (and (memq 'table contexts)
  6193. (looking-at "[ \t]*|"))
  6194. (and (memq 'headline contexts)
  6195. ;;????????? (looking-at "\\*+"))
  6196. (looking-at outline-regexp))
  6197. (and (memq 'item contexts)
  6198. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6199. (and (memq 'item-body contexts)
  6200. (org-in-item-p)))
  6201. (goto-char pos))))
  6202. (defun org-get-local-variables ()
  6203. "Return a list of all local variables in an org-mode buffer."
  6204. (let (varlist)
  6205. (with-current-buffer (get-buffer-create "*Org tmp*")
  6206. (erase-buffer)
  6207. (org-mode)
  6208. (setq varlist (buffer-local-variables)))
  6209. (kill-buffer "*Org tmp*")
  6210. (delq nil
  6211. (mapcar
  6212. (lambda (x)
  6213. (setq x
  6214. (if (symbolp x)
  6215. (list x)
  6216. (list (car x) (list 'quote (cdr x)))))
  6217. (if (string-match
  6218. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6219. (symbol-name (car x)))
  6220. x nil))
  6221. varlist))))
  6222. ;;;###autoload
  6223. (defun org-run-like-in-org-mode (cmd)
  6224. "Run a command, pretending that the current buffer is in Org-mode.
  6225. This will temporarily bind local variables that are typically bound in
  6226. Org-mode to the values they have in Org-mode, and then interactively
  6227. call CMD."
  6228. (org-load-modules-maybe)
  6229. (unless org-local-vars
  6230. (setq org-local-vars (org-get-local-variables)))
  6231. (eval (list 'let org-local-vars
  6232. (list 'call-interactively (list 'quote cmd)))))
  6233. ;;;; Archiving
  6234. (defun org-get-category (&optional pos)
  6235. "Get the category applying to position POS."
  6236. (get-text-property (or pos (point)) 'org-category))
  6237. (defun org-refresh-category-properties ()
  6238. "Refresh category text properties in the buffer."
  6239. (let ((def-cat (cond
  6240. ((null org-category)
  6241. (if buffer-file-name
  6242. (file-name-sans-extension
  6243. (file-name-nondirectory buffer-file-name))
  6244. "???"))
  6245. ((symbolp org-category) (symbol-name org-category))
  6246. (t org-category)))
  6247. beg end cat pos optionp)
  6248. (org-unmodified
  6249. (save-excursion
  6250. (save-restriction
  6251. (widen)
  6252. (goto-char (point-min))
  6253. (put-text-property (point) (point-max) 'org-category def-cat)
  6254. (while (re-search-forward
  6255. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6256. (setq pos (match-end 0)
  6257. optionp (equal (char-after (match-beginning 0)) ?#)
  6258. cat (org-trim (match-string 2)))
  6259. (if optionp
  6260. (setq beg (point-at-bol) end (point-max))
  6261. (org-back-to-heading t)
  6262. (setq beg (point) end (org-end-of-subtree t t)))
  6263. (put-text-property beg end 'org-category cat)
  6264. (goto-char pos)))))))
  6265. ;;;; Link Stuff
  6266. ;;; Link abbreviations
  6267. (defun org-link-expand-abbrev (link)
  6268. "Apply replacements as defined in `org-link-abbrev-alist."
  6269. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6270. (let* ((key (match-string 1 link))
  6271. (as (or (assoc key org-link-abbrev-alist-local)
  6272. (assoc key org-link-abbrev-alist)))
  6273. (tag (and (match-end 2) (match-string 3 link)))
  6274. rpl)
  6275. (if (not as)
  6276. link
  6277. (setq rpl (cdr as))
  6278. (cond
  6279. ((symbolp rpl) (funcall rpl tag))
  6280. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6281. ((string-match "%h" rpl)
  6282. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6283. (t (concat rpl tag)))))
  6284. link))
  6285. ;;; Storing and inserting links
  6286. (defvar org-insert-link-history nil
  6287. "Minibuffer history for links inserted with `org-insert-link'.")
  6288. (defvar org-stored-links nil
  6289. "Contains the links stored with `org-store-link'.")
  6290. (defvar org-store-link-plist nil
  6291. "Plist with info about the most recently link created with `org-store-link'.")
  6292. (defvar org-link-protocols nil
  6293. "Link protocols added to Org-mode using `org-add-link-type'.")
  6294. (defvar org-store-link-functions nil
  6295. "List of functions that are called to create and store a link.
  6296. Each function will be called in turn until one returns a non-nil
  6297. value. Each function should check if it is responsible for creating
  6298. this link (for example by looking at the major mode).
  6299. If not, it must exit and return nil.
  6300. If yes, it should return a non-nil value after a calling
  6301. `org-store-link-props' with a list of properties and values.
  6302. Special properties are:
  6303. :type The link prefix. like \"http\". This must be given.
  6304. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6305. This is obligatory as well.
  6306. :description Optional default description for the second pair
  6307. of brackets in an Org-mode link. The user can still change
  6308. this when inserting this link into an Org-mode buffer.
  6309. In addition to these, any additional properties can be specified
  6310. and then used in remember templates.")
  6311. (defun org-add-link-type (type &optional follow export)
  6312. "Add TYPE to the list of `org-link-types'.
  6313. Re-compute all regular expressions depending on `org-link-types'
  6314. FOLLOW and EXPORT are two functions.
  6315. FOLLOW should take the link path as the single argument and do whatever
  6316. is necessary to follow the link, for example find a file or display
  6317. a mail message.
  6318. EXPORT should format the link path for export to one of the export formats.
  6319. It should be a function accepting three arguments:
  6320. path the path of the link, the text after the prefix (like \"http:\")
  6321. desc the description of the link, if any, nil if there was no description
  6322. format the export format, a symbol like `html' or `latex'.
  6323. The function may use the FORMAT information to return different values
  6324. depending on the format. The return value will be put literally into
  6325. the exported file.
  6326. Org-mode has a built-in default for exporting links. If you are happy with
  6327. this default, there is no need to define an export function for the link
  6328. type. For a simple example of an export function, see `org-bbdb.el'."
  6329. (add-to-list 'org-link-types type t)
  6330. (org-make-link-regexps)
  6331. (if (assoc type org-link-protocols)
  6332. (setcdr (assoc type org-link-protocols) (list follow export))
  6333. (push (list type follow export) org-link-protocols)))
  6334. ;;;###autoload
  6335. (defun org-store-link (arg)
  6336. "\\<org-mode-map>Store an org-link to the current location.
  6337. This link is added to `org-stored-links' and can later be inserted
  6338. into an org-buffer with \\[org-insert-link].
  6339. For some link types, a prefix arg is interpreted:
  6340. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6341. For file links, arg negates `org-context-in-file-links'."
  6342. (interactive "P")
  6343. (org-load-modules-maybe)
  6344. (setq org-store-link-plist nil) ; reset
  6345. (let ((outline-regexp (org-get-limited-outline-regexp))
  6346. link cpltxt desc description search txt custom-id)
  6347. (cond
  6348. ((run-hook-with-args-until-success 'org-store-link-functions)
  6349. (setq link (plist-get org-store-link-plist :link)
  6350. desc (or (plist-get org-store-link-plist :description) link)))
  6351. ((equal (buffer-name) "*Org Edit Src Example*")
  6352. (let (label gc)
  6353. (while (or (not label)
  6354. (save-excursion
  6355. (save-restriction
  6356. (widen)
  6357. (goto-char (point-min))
  6358. (re-search-forward
  6359. (regexp-quote (format org-coderef-label-format label))
  6360. nil t))))
  6361. (when label (message "Label exists already") (sit-for 2))
  6362. (setq label (read-string "Code line label: " label)))
  6363. (end-of-line 1)
  6364. (setq link (format org-coderef-label-format label))
  6365. (setq gc (- 79 (length link)))
  6366. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6367. (insert link)
  6368. (setq link (concat "(" label ")") desc nil)))
  6369. ((eq major-mode 'calendar-mode)
  6370. (let ((cd (calendar-cursor-to-date)))
  6371. (setq link
  6372. (format-time-string
  6373. (car org-time-stamp-formats)
  6374. (apply 'encode-time
  6375. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6376. nil nil nil))))
  6377. (org-store-link-props :type "calendar" :date cd)))
  6378. ((eq major-mode 'w3-mode)
  6379. (setq cpltxt (if (and (buffer-name)
  6380. (not (string-match "Untitled" (buffer-name))))
  6381. (buffer-name)
  6382. (url-view-url t))
  6383. link (org-make-link (url-view-url t)))
  6384. (org-store-link-props :type "w3" :url (url-view-url t)))
  6385. ((eq major-mode 'w3m-mode)
  6386. (setq cpltxt (or w3m-current-title w3m-current-url)
  6387. link (org-make-link w3m-current-url))
  6388. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6389. ((setq search (run-hook-with-args-until-success
  6390. 'org-create-file-search-functions))
  6391. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6392. "::" search))
  6393. (setq cpltxt (or description link)))
  6394. ((eq major-mode 'image-mode)
  6395. (setq cpltxt (concat "file:"
  6396. (abbreviate-file-name buffer-file-name))
  6397. link (org-make-link cpltxt))
  6398. (org-store-link-props :type "image" :file buffer-file-name))
  6399. ((eq major-mode 'dired-mode)
  6400. ;; link to the file in the current line
  6401. (setq cpltxt (concat "file:"
  6402. (abbreviate-file-name
  6403. (expand-file-name
  6404. (dired-get-filename nil t))))
  6405. link (org-make-link cpltxt)))
  6406. ((and buffer-file-name (org-mode-p))
  6407. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6408. (cond
  6409. ((org-in-regexp "<<\\(.*?\\)>>")
  6410. (setq cpltxt
  6411. (concat "file:"
  6412. (abbreviate-file-name buffer-file-name)
  6413. "::" (match-string 1))
  6414. link (org-make-link cpltxt)))
  6415. ((and (featurep 'org-id)
  6416. (or (eq org-link-to-org-use-id t)
  6417. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6418. (interactive-p))
  6419. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6420. (interactive-p)
  6421. (not custom-id))
  6422. (and org-link-to-org-use-id
  6423. (condition-case nil
  6424. (org-entry-get nil "ID")
  6425. (error nil)))))
  6426. ;; We can make a link using the ID.
  6427. (setq link (condition-case nil
  6428. (prog1 (org-id-store-link)
  6429. (setq desc (plist-get org-store-link-plist
  6430. :description)))
  6431. (error
  6432. ;; probably before first headline, link to file only
  6433. (concat "file:"
  6434. (abbreviate-file-name buffer-file-name))))))
  6435. (t
  6436. ;; Just link to current headline
  6437. (setq cpltxt (concat "file:"
  6438. (abbreviate-file-name buffer-file-name)))
  6439. ;; Add a context search string
  6440. (when (org-xor org-context-in-file-links arg)
  6441. (setq txt (cond
  6442. ((org-on-heading-p) nil)
  6443. ((org-region-active-p)
  6444. (buffer-substring (region-beginning) (region-end)))
  6445. (t nil)))
  6446. (when (or (null txt) (string-match "\\S-" txt))
  6447. (setq cpltxt
  6448. (concat cpltxt "::"
  6449. (condition-case nil
  6450. (org-make-org-heading-search-string txt)
  6451. (error "")))
  6452. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6453. (if (string-match "::\\'" cpltxt)
  6454. (setq cpltxt (substring cpltxt 0 -2)))
  6455. (setq link (org-make-link cpltxt)))))
  6456. ((buffer-file-name (buffer-base-buffer))
  6457. ;; Just link to this file here.
  6458. (setq cpltxt (concat "file:"
  6459. (abbreviate-file-name
  6460. (buffer-file-name (buffer-base-buffer)))))
  6461. ;; Add a context string
  6462. (when (org-xor org-context-in-file-links arg)
  6463. (setq txt (if (org-region-active-p)
  6464. (buffer-substring (region-beginning) (region-end))
  6465. (buffer-substring (point-at-bol) (point-at-eol))))
  6466. ;; Only use search option if there is some text.
  6467. (when (string-match "\\S-" txt)
  6468. (setq cpltxt
  6469. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6470. desc "NONE")))
  6471. (setq link (org-make-link cpltxt)))
  6472. ((interactive-p)
  6473. (error "Cannot link to a buffer which is not visiting a file"))
  6474. (t (setq link nil)))
  6475. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6476. (setq link (or link cpltxt)
  6477. desc (or desc cpltxt))
  6478. (if (equal desc "NONE") (setq desc nil))
  6479. (if (and (or (interactive-p) executing-kbd-macro) link)
  6480. (progn
  6481. (setq org-stored-links
  6482. (cons (list link desc) org-stored-links))
  6483. (message "Stored: %s" (or desc link))
  6484. (when custom-id
  6485. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6486. "::#" custom-id))
  6487. (setq org-stored-links
  6488. (cons (list link desc) org-stored-links))))
  6489. (and link (org-make-link-string link desc)))))
  6490. (defun org-store-link-props (&rest plist)
  6491. "Store link properties, extract names and addresses."
  6492. (let (x adr)
  6493. (when (setq x (plist-get plist :from))
  6494. (setq adr (mail-extract-address-components x))
  6495. (setq plist (plist-put plist :fromname (car adr)))
  6496. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6497. (when (setq x (plist-get plist :to))
  6498. (setq adr (mail-extract-address-components x))
  6499. (setq plist (plist-put plist :toname (car adr)))
  6500. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6501. (let ((from (plist-get plist :from))
  6502. (to (plist-get plist :to)))
  6503. (when (and from to org-from-is-user-regexp)
  6504. (setq plist
  6505. (plist-put plist :fromto
  6506. (if (string-match org-from-is-user-regexp from)
  6507. (concat "to %t")
  6508. (concat "from %f"))))))
  6509. (setq org-store-link-plist plist))
  6510. (defun org-add-link-props (&rest plist)
  6511. "Add these properties to the link property list."
  6512. (let (key value)
  6513. (while plist
  6514. (setq key (pop plist) value (pop plist))
  6515. (setq org-store-link-plist
  6516. (plist-put org-store-link-plist key value)))))
  6517. (defun org-email-link-description (&optional fmt)
  6518. "Return the description part of an email link.
  6519. This takes information from `org-store-link-plist' and formats it
  6520. according to FMT (default from `org-email-link-description-format')."
  6521. (setq fmt (or fmt org-email-link-description-format))
  6522. (let* ((p org-store-link-plist)
  6523. (to (plist-get p :toaddress))
  6524. (from (plist-get p :fromaddress))
  6525. (table
  6526. (list
  6527. (cons "%c" (plist-get p :fromto))
  6528. (cons "%F" (plist-get p :from))
  6529. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6530. (cons "%T" (plist-get p :to))
  6531. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6532. (cons "%s" (plist-get p :subject))
  6533. (cons "%m" (plist-get p :message-id)))))
  6534. (when (string-match "%c" fmt)
  6535. ;; Check if the user wrote this message
  6536. (if (and org-from-is-user-regexp from to
  6537. (save-match-data (string-match org-from-is-user-regexp from)))
  6538. (setq fmt (replace-match "to %t" t t fmt))
  6539. (setq fmt (replace-match "from %f" t t fmt))))
  6540. (org-replace-escapes fmt table)))
  6541. (defun org-make-org-heading-search-string (&optional string heading)
  6542. "Make search string for STRING or current headline."
  6543. (interactive)
  6544. (let ((s (or string (org-get-heading))))
  6545. (unless (and string (not heading))
  6546. ;; We are using a headline, clean up garbage in there.
  6547. (if (string-match org-todo-regexp s)
  6548. (setq s (replace-match "" t t s)))
  6549. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6550. (setq s (replace-match "" t t s)))
  6551. (setq s (org-trim s))
  6552. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6553. org-comment-string "\\)") s)
  6554. (setq s (replace-match "" t t s)))
  6555. (while (string-match org-ts-regexp s)
  6556. (setq s (replace-match "" t t s))))
  6557. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6558. (setq s (replace-match " " t t s)))
  6559. (or string (setq s (concat "*" s))) ; Add * for headlines
  6560. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6561. (defun org-make-link (&rest strings)
  6562. "Concatenate STRINGS."
  6563. (apply 'concat strings))
  6564. (defun org-make-link-string (link &optional description)
  6565. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6566. (unless (string-match "\\S-" link)
  6567. (error "Empty link"))
  6568. (when (stringp description)
  6569. ;; Remove brackets from the description, they are fatal.
  6570. (while (string-match "\\[" description)
  6571. (setq description (replace-match "{" t t description)))
  6572. (while (string-match "\\]" description)
  6573. (setq description (replace-match "}" t t description))))
  6574. (when (equal (org-link-escape link) description)
  6575. ;; No description needed, it is identical
  6576. (setq description nil))
  6577. (when (and (not description)
  6578. (not (equal link (org-link-escape link))))
  6579. (setq description (org-extract-attributes link)))
  6580. (concat "[[" (org-link-escape link) "]"
  6581. (if description (concat "[" description "]") "")
  6582. "]"))
  6583. (defconst org-link-escape-chars
  6584. '((?\ . "%20")
  6585. (?\[ . "%5B")
  6586. (?\] . "%5D")
  6587. (?\340 . "%E0") ; `a
  6588. (?\342 . "%E2") ; ^a
  6589. (?\347 . "%E7") ; ,c
  6590. (?\350 . "%E8") ; `e
  6591. (?\351 . "%E9") ; 'e
  6592. (?\352 . "%EA") ; ^e
  6593. (?\356 . "%EE") ; ^i
  6594. (?\364 . "%F4") ; ^o
  6595. (?\371 . "%F9") ; `u
  6596. (?\373 . "%FB") ; ^u
  6597. (?\; . "%3B")
  6598. (?? . "%3F")
  6599. (?= . "%3D")
  6600. (?+ . "%2B")
  6601. )
  6602. "Association list of escapes for some characters problematic in links.
  6603. This is the list that is used for internal purposes.")
  6604. (defvar org-url-encoding-use-url-hexify nil)
  6605. (defconst org-link-escape-chars-browser
  6606. '((?\ . "%20")) ; 32 for the SPC char
  6607. "Association list of escapes for some characters problematic in links.
  6608. This is the list that is used before handing over to the browser.")
  6609. (defun org-link-escape (text &optional table)
  6610. "Escape characters in TEXT that are problematic for links."
  6611. (if org-url-encoding-use-url-hexify
  6612. (url-hexify-string text)
  6613. (setq table (or table org-link-escape-chars))
  6614. (when text
  6615. (let ((re (mapconcat (lambda (x) (regexp-quote
  6616. (char-to-string (car x))))
  6617. table "\\|")))
  6618. (while (string-match re text)
  6619. (setq text
  6620. (replace-match
  6621. (cdr (assoc (string-to-char (match-string 0 text))
  6622. table))
  6623. t t text)))
  6624. text))))
  6625. (defun org-link-unescape (text &optional table)
  6626. "Reverse the action of `org-link-escape'."
  6627. (if org-url-encoding-use-url-hexify
  6628. (url-unhex-string text)
  6629. (setq table (or table org-link-escape-chars))
  6630. (when text
  6631. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6632. table "\\|")))
  6633. (while (string-match re text)
  6634. (setq text
  6635. (replace-match
  6636. (char-to-string (car (rassoc (match-string 0 text) table)))
  6637. t t text)))
  6638. text))))
  6639. (defun org-xor (a b)
  6640. "Exclusive or."
  6641. (if a (not b) b))
  6642. (defun org-fixup-message-id-for-http (s)
  6643. "Replace special characters in a message id, so it can be used in an http query."
  6644. (while (string-match "<" s)
  6645. (setq s (replace-match "%3C" t t s)))
  6646. (while (string-match ">" s)
  6647. (setq s (replace-match "%3E" t t s)))
  6648. (while (string-match "@" s)
  6649. (setq s (replace-match "%40" t t s)))
  6650. s)
  6651. ;;;###autoload
  6652. (defun org-insert-link-global ()
  6653. "Insert a link like Org-mode does.
  6654. This command can be called in any mode to insert a link in Org-mode syntax."
  6655. (interactive)
  6656. (org-load-modules-maybe)
  6657. (org-run-like-in-org-mode 'org-insert-link))
  6658. (defun org-insert-link (&optional complete-file link-location)
  6659. "Insert a link. At the prompt, enter the link.
  6660. Completion can be used to insert any of the link protocol prefixes like
  6661. http or ftp in use.
  6662. The history can be used to select a link previously stored with
  6663. `org-store-link'. When the empty string is entered (i.e. if you just
  6664. press RET at the prompt), the link defaults to the most recently
  6665. stored link. As SPC triggers completion in the minibuffer, you need to
  6666. use M-SPC or C-q SPC to force the insertion of a space character.
  6667. You will also be prompted for a description, and if one is given, it will
  6668. be displayed in the buffer instead of the link.
  6669. If there is already a link at point, this command will allow you to edit link
  6670. and description parts.
  6671. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6672. be selected using completion. The path to the file will be relative to the
  6673. current directory if the file is in the current directory or a subdirectory.
  6674. Otherwise, the link will be the absolute path as completed in the minibuffer
  6675. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6676. option `org-link-file-path-type'.
  6677. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6678. the current directory or below.
  6679. With three \\[universal-argument] prefixes, negate the meaning of
  6680. `org-keep-stored-link-after-insertion'.
  6681. If `org-make-link-description-function' is non-nil, this function will be
  6682. called with the link target, and the result will be the default
  6683. link description.
  6684. If the LINK-LOCATION parameter is non-nil, this value will be
  6685. used as the link location instead of reading one interactively."
  6686. (interactive "P")
  6687. (let* ((wcf (current-window-configuration))
  6688. (region (if (org-region-active-p)
  6689. (buffer-substring (region-beginning) (region-end))))
  6690. (remove (and region (list (region-beginning) (region-end))))
  6691. (desc region)
  6692. tmphist ; byte-compile incorrectly complains about this
  6693. (link link-location)
  6694. entry file all-prefixes)
  6695. (cond
  6696. (link-location) ; specified by arg, just use it.
  6697. ((org-in-regexp org-bracket-link-regexp 1)
  6698. ;; We do have a link at point, and we are going to edit it.
  6699. (setq remove (list (match-beginning 0) (match-end 0)))
  6700. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6701. (setq link (read-string "Link: "
  6702. (org-link-unescape
  6703. (org-match-string-no-properties 1)))))
  6704. ((or (org-in-regexp org-angle-link-re)
  6705. (org-in-regexp org-plain-link-re))
  6706. ;; Convert to bracket link
  6707. (setq remove (list (match-beginning 0) (match-end 0))
  6708. link (read-string "Link: "
  6709. (org-remove-angle-brackets (match-string 0)))))
  6710. ((member complete-file '((4) (16)))
  6711. ;; Completing read for file names.
  6712. (setq link (org-file-complete-link complete-file)))
  6713. (t
  6714. ;; Read link, with completion for stored links.
  6715. (with-output-to-temp-buffer "*Org Links*"
  6716. (princ "Insert a link.
  6717. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6718. (when org-stored-links
  6719. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6720. (princ (mapconcat
  6721. (lambda (x)
  6722. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6723. (reverse org-stored-links) "\n"))))
  6724. (let ((cw (selected-window)))
  6725. (select-window (get-buffer-window "*Org Links*"))
  6726. (setq truncate-lines t)
  6727. (unless (pos-visible-in-window-p (point-max))
  6728. (org-fit-window-to-buffer))
  6729. (and (window-live-p cw) (select-window cw)))
  6730. ;; Fake a link history, containing the stored links.
  6731. (setq tmphist (append (mapcar 'car org-stored-links)
  6732. org-insert-link-history))
  6733. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6734. (mapcar 'car org-link-abbrev-alist)
  6735. org-link-types))
  6736. (unwind-protect
  6737. (progn
  6738. (setq link
  6739. (let ((org-completion-use-ido nil))
  6740. (org-completing-read
  6741. "Link: "
  6742. (append
  6743. (mapcar (lambda (x) (list (concat x ":")))
  6744. all-prefixes)
  6745. (mapcar 'car org-stored-links))
  6746. nil nil nil
  6747. 'tmphist
  6748. (car (car org-stored-links)))))
  6749. (if (or (member link all-prefixes)
  6750. (and (equal ":" (substring link -1))
  6751. (member (substring link 0 -1) all-prefixes)
  6752. (setq link (substring link 0 -1))))
  6753. (setq link (org-link-try-special-completion link))))
  6754. (set-window-configuration wcf)
  6755. (kill-buffer "*Org Links*"))
  6756. (setq entry (assoc link org-stored-links))
  6757. (or entry (push link org-insert-link-history))
  6758. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6759. (not org-keep-stored-link-after-insertion))
  6760. (setq org-stored-links (delq (assoc link org-stored-links)
  6761. org-stored-links)))
  6762. (setq desc (or desc (nth 1 entry)))))
  6763. (if (string-match org-plain-link-re link)
  6764. ;; URL-like link, normalize the use of angular brackets.
  6765. (setq link (org-make-link (org-remove-angle-brackets link))))
  6766. ;; Check if we are linking to the current file with a search option
  6767. ;; If yes, simplify the link by using only the search option.
  6768. (when (and buffer-file-name
  6769. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6770. (let* ((path (match-string 1 link))
  6771. (case-fold-search nil)
  6772. (search (match-string 2 link)))
  6773. (save-match-data
  6774. (if (equal (file-truename buffer-file-name) (file-truename path))
  6775. ;; We are linking to this same file, with a search option
  6776. (setq link search)))))
  6777. ;; Check if we can/should use a relative path. If yes, simplify the link
  6778. (when (string-match "^file:\\(.*\\)" link)
  6779. (let* ((path (match-string 1 link))
  6780. (origpath path)
  6781. (case-fold-search nil))
  6782. (cond
  6783. ((or (eq org-link-file-path-type 'absolute)
  6784. (equal complete-file '(16)))
  6785. (setq path (abbreviate-file-name (expand-file-name path))))
  6786. ((eq org-link-file-path-type 'noabbrev)
  6787. (setq path (expand-file-name path)))
  6788. ((eq org-link-file-path-type 'relative)
  6789. (setq path (file-relative-name path)))
  6790. (t
  6791. (save-match-data
  6792. (if (string-match (concat "^" (regexp-quote
  6793. (file-name-as-directory
  6794. (expand-file-name "."))))
  6795. (expand-file-name path))
  6796. ;; We are linking a file with relative path name.
  6797. (setq path (substring (expand-file-name path)
  6798. (match-end 0)))
  6799. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6800. (setq link (concat "file:" path))
  6801. (if (equal desc origpath)
  6802. (setq desc path))))
  6803. (if org-make-link-description-function
  6804. (setq desc (funcall org-make-link-description-function link desc)))
  6805. (setq desc (read-string "Description: " desc))
  6806. (unless (string-match "\\S-" desc) (setq desc nil))
  6807. (if remove (apply 'delete-region remove))
  6808. (insert (org-make-link-string link desc))))
  6809. (defun org-link-try-special-completion (type)
  6810. "If there is completion support for link type TYPE, offer it."
  6811. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6812. (if (functionp fun)
  6813. (funcall fun)
  6814. (read-string "Link (no completion support): " (concat type ":")))))
  6815. (defun org-file-complete-link (&optional arg)
  6816. "Create a file link using completion."
  6817. (let (file link)
  6818. (setq file (read-file-name "File: "))
  6819. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6820. (pwd1 (file-name-as-directory (abbreviate-file-name
  6821. (expand-file-name ".")))))
  6822. (cond
  6823. ((equal arg '(16))
  6824. (setq link (org-make-link
  6825. "file:"
  6826. (abbreviate-file-name (expand-file-name file)))))
  6827. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6828. (setq link (org-make-link "file:" (match-string 1 file))))
  6829. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6830. (expand-file-name file))
  6831. (setq link (org-make-link
  6832. "file:" (match-string 1 (expand-file-name file)))))
  6833. (t (setq link (org-make-link "file:" file)))))
  6834. link))
  6835. (defun org-completing-read (&rest args)
  6836. "Completing-read with SPACE being a normal character."
  6837. (let ((minibuffer-local-completion-map
  6838. (copy-keymap minibuffer-local-completion-map)))
  6839. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6840. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6841. (apply 'org-ido-completing-read args)))
  6842. (defun org-completing-read-no-ido (&rest args)
  6843. (let (org-completion-use-ido)
  6844. (apply 'org-completing-read args)))
  6845. (defun org-ido-completing-read (&rest args)
  6846. "Completing-read using `ido-mode' speedups if available"
  6847. (if (and org-completion-use-ido
  6848. (fboundp 'ido-completing-read)
  6849. (boundp 'ido-mode) ido-mode
  6850. (listp (second args)))
  6851. (let ((ido-enter-matching-directory nil))
  6852. (apply 'ido-completing-read (concat (car args))
  6853. (mapcar (lambda (x) (car x)) (nth 1 args))
  6854. (cddr args)))
  6855. (apply 'completing-read args)))
  6856. (defun org-extract-attributes (s)
  6857. "Extract the attributes cookie from a string and set as text property."
  6858. (let (a attr (start 0) key value)
  6859. (save-match-data
  6860. (when (string-match "{{\\([^}]+\\)}}$" s)
  6861. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6862. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6863. (setq key (match-string 1 a) value (match-string 2 a)
  6864. start (match-end 0)
  6865. attr (plist-put attr (intern key) value))))
  6866. (org-add-props s nil 'org-attr attr))
  6867. s))
  6868. (defun org-extract-attributes-from-string (tag)
  6869. (let (key value attr)
  6870. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6871. (setq key (match-string 1 tag) value (match-string 2 tag)
  6872. tag (replace-match "" t t tag)
  6873. attr (plist-put attr (intern key) value)))
  6874. (cons tag attr)))
  6875. (defun org-attributes-to-string (plist)
  6876. "Format a property list into an HTML attribute list."
  6877. (let ((s "") key value)
  6878. (while plist
  6879. (setq key (pop plist) value (pop plist))
  6880. (and value
  6881. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6882. s))
  6883. ;;; Opening/following a link
  6884. (defvar org-link-search-failed nil)
  6885. (defun org-next-link ()
  6886. "Move forward to the next link.
  6887. If the link is in hidden text, expose it."
  6888. (interactive)
  6889. (when (and org-link-search-failed (eq this-command last-command))
  6890. (goto-char (point-min))
  6891. (message "Link search wrapped back to beginning of buffer"))
  6892. (setq org-link-search-failed nil)
  6893. (let* ((pos (point))
  6894. (ct (org-context))
  6895. (a (assoc :link ct)))
  6896. (if a (goto-char (nth 2 a)))
  6897. (if (re-search-forward org-any-link-re nil t)
  6898. (progn
  6899. (goto-char (match-beginning 0))
  6900. (if (org-invisible-p) (org-show-context)))
  6901. (goto-char pos)
  6902. (setq org-link-search-failed t)
  6903. (error "No further link found"))))
  6904. (defun org-previous-link ()
  6905. "Move backward to the previous link.
  6906. If the link is in hidden text, expose it."
  6907. (interactive)
  6908. (when (and org-link-search-failed (eq this-command last-command))
  6909. (goto-char (point-max))
  6910. (message "Link search wrapped back to end of buffer"))
  6911. (setq org-link-search-failed nil)
  6912. (let* ((pos (point))
  6913. (ct (org-context))
  6914. (a (assoc :link ct)))
  6915. (if a (goto-char (nth 1 a)))
  6916. (if (re-search-backward org-any-link-re nil t)
  6917. (progn
  6918. (goto-char (match-beginning 0))
  6919. (if (org-invisible-p) (org-show-context)))
  6920. (goto-char pos)
  6921. (setq org-link-search-failed t)
  6922. (error "No further link found"))))
  6923. (defun org-translate-link (s)
  6924. "Translate a link string if a translation function has been defined."
  6925. (if (and org-link-translation-function
  6926. (fboundp org-link-translation-function)
  6927. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6928. (progn
  6929. (setq s (funcall org-link-translation-function
  6930. (match-string 1) (match-string 2)))
  6931. (concat (car s) ":" (cdr s)))
  6932. s))
  6933. (defun org-translate-link-from-planner (type path)
  6934. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6935. This is still an experimental function, your mileage may vary."
  6936. (cond
  6937. ((member type '("http" "https" "news" "ftp"))
  6938. ;; standard Internet links are the same.
  6939. nil)
  6940. ((and (equal type "irc") (string-match "^//" path))
  6941. ;; Planner has two / at the beginning of an irc link, we have 1.
  6942. ;; We should have zero, actually....
  6943. (setq path (substring path 1)))
  6944. ((and (equal type "lisp") (string-match "^/" path))
  6945. ;; Planner has a slash, we do not.
  6946. (setq type "elisp" path (substring path 1)))
  6947. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6948. ;; A typical message link. Planner has the id after the fina slash,
  6949. ;; we separate it with a hash mark
  6950. (setq path (concat (match-string 1 path) "#"
  6951. (org-remove-angle-brackets (match-string 2 path)))))
  6952. )
  6953. (cons type path))
  6954. (defun org-find-file-at-mouse (ev)
  6955. "Open file link or URL at mouse."
  6956. (interactive "e")
  6957. (mouse-set-point ev)
  6958. (org-open-at-point 'in-emacs))
  6959. (defun org-open-at-mouse (ev)
  6960. "Open file link or URL at mouse."
  6961. (interactive "e")
  6962. (mouse-set-point ev)
  6963. (if (eq major-mode 'org-agenda-mode)
  6964. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6965. (org-open-at-point))
  6966. (defvar org-window-config-before-follow-link nil
  6967. "The window configuration before following a link.
  6968. This is saved in case the need arises to restore it.")
  6969. (defvar org-open-link-marker (make-marker)
  6970. "Marker pointing to the location where `org-open-at-point; was called.")
  6971. ;;;###autoload
  6972. (defun org-open-at-point-global ()
  6973. "Follow a link like Org-mode does.
  6974. This command can be called in any mode to follow a link that has
  6975. Org-mode syntax."
  6976. (interactive)
  6977. (org-run-like-in-org-mode 'org-open-at-point))
  6978. ;;;###autoload
  6979. (defun org-open-link-from-string (s &optional arg)
  6980. "Open a link in the string S, as if it was in Org-mode."
  6981. (interactive "sLink: \nP")
  6982. (let ((reference-buffer (current-buffer)))
  6983. (with-temp-buffer
  6984. (let ((org-inhibit-startup t))
  6985. (org-mode)
  6986. (insert s)
  6987. (goto-char (point-min))
  6988. (org-open-at-point arg reference-buffer)))))
  6989. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6990. "Open link at or after point.
  6991. If there is no link at point, this function will search forward up to
  6992. the end of the current line.
  6993. Normally, files will be opened by an appropriate application. If the
  6994. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6995. With a double prefix argument, try to open outside of Emacs, in the
  6996. application the system uses for this file type."
  6997. (interactive "P")
  6998. (org-load-modules-maybe)
  6999. (move-marker org-open-link-marker (point))
  7000. (setq org-window-config-before-follow-link (current-window-configuration))
  7001. (org-remove-occur-highlights nil nil t)
  7002. (cond
  7003. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7004. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7005. (org-footnote-action))
  7006. (t
  7007. (let (type path link line search (pos (point)))
  7008. (catch 'match
  7009. (save-excursion
  7010. (skip-chars-forward "^]\n\r")
  7011. (when (org-in-regexp org-bracket-link-regexp)
  7012. (setq link (org-extract-attributes
  7013. (org-link-unescape (org-match-string-no-properties 1))))
  7014. (while (string-match " *\n *" link)
  7015. (setq link (replace-match " " t t link)))
  7016. (setq link (org-link-expand-abbrev link))
  7017. (cond
  7018. ((or (file-name-absolute-p link)
  7019. (string-match "^\\.\\.?/" link))
  7020. (setq type "file" path link))
  7021. ((string-match org-link-re-with-space3 link)
  7022. (setq type (match-string 1 link) path (match-string 2 link)))
  7023. (t (setq type "thisfile" path link)))
  7024. (throw 'match t)))
  7025. (when (get-text-property (point) 'org-linked-text)
  7026. (setq type "thisfile"
  7027. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7028. (1+ (point)) (point))
  7029. path (buffer-substring
  7030. (previous-single-property-change pos 'org-linked-text)
  7031. (next-single-property-change pos 'org-linked-text)))
  7032. (throw 'match t))
  7033. (save-excursion
  7034. (when (or (org-in-regexp org-angle-link-re)
  7035. (org-in-regexp org-plain-link-re))
  7036. (setq type (match-string 1) path (match-string 2))
  7037. (throw 'match t)))
  7038. (save-excursion
  7039. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7040. (setq type "tags"
  7041. path (match-string 1))
  7042. (while (string-match ":" path)
  7043. (setq path (replace-match "+" t t path)))
  7044. (throw 'match t)))
  7045. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7046. (setq type "tree-match"
  7047. path (match-string 1))
  7048. (throw 'match t)))
  7049. (unless path
  7050. (error "No link found"))
  7051. ;; switch back to reference buffer
  7052. ;; needed when if called in a temporary buffer through
  7053. ;; org-open-link-from-string
  7054. (and reference-buffer (switch-to-buffer reference-buffer))
  7055. ;; Remove any trailing spaces in path
  7056. (if (string-match " +\\'" path)
  7057. (setq path (replace-match "" t t path)))
  7058. (if (and org-link-translation-function
  7059. (fboundp org-link-translation-function))
  7060. ;; Check if we need to translate the link
  7061. (let ((tmp (funcall org-link-translation-function type path)))
  7062. (setq type (car tmp) path (cdr tmp))))
  7063. (cond
  7064. ((assoc type org-link-protocols)
  7065. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7066. ((equal type "mailto")
  7067. (let ((cmd (car org-link-mailto-program))
  7068. (args (cdr org-link-mailto-program)) args1
  7069. (address path) (subject "") a)
  7070. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7071. (setq address (match-string 1 path)
  7072. subject (org-link-escape (match-string 2 path))))
  7073. (while args
  7074. (cond
  7075. ((not (stringp (car args))) (push (pop args) args1))
  7076. (t (setq a (pop args))
  7077. (if (string-match "%a" a)
  7078. (setq a (replace-match address t t a)))
  7079. (if (string-match "%s" a)
  7080. (setq a (replace-match subject t t a)))
  7081. (push a args1))))
  7082. (apply cmd (nreverse args1))))
  7083. ((member type '("http" "https" "ftp" "news"))
  7084. (browse-url (concat type ":" (org-link-escape
  7085. path org-link-escape-chars-browser))))
  7086. ((member type '("message"))
  7087. (browse-url (concat type ":" path)))
  7088. ((string= type "tags")
  7089. (org-tags-view in-emacs path))
  7090. ((string= type "thisfile")
  7091. (if in-emacs
  7092. (switch-to-buffer-other-window
  7093. (org-get-buffer-for-internal-link (current-buffer)))
  7094. (org-mark-ring-push))
  7095. (let ((cmd `(org-link-search
  7096. ,path
  7097. ,(cond ((equal in-emacs '(4)) 'occur)
  7098. ((equal in-emacs '(16)) 'org-occur)
  7099. (t nil))
  7100. ,pos)))
  7101. (condition-case nil (eval cmd)
  7102. (error (progn (widen) (eval cmd))))))
  7103. ((string= type "tree-match")
  7104. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7105. ((string= type "file")
  7106. (if (string-match "::\\([0-9]+\\)\\'" path)
  7107. (setq line (string-to-number (match-string 1 path))
  7108. path (substring path 0 (match-beginning 0)))
  7109. (if (string-match "::\\(.+\\)\\'" path)
  7110. (setq search (match-string 1 path)
  7111. path (substring path 0 (match-beginning 0)))))
  7112. (if (string-match "[*?{]" (file-name-nondirectory path))
  7113. (dired path)
  7114. (org-open-file path in-emacs line search)))
  7115. ((string= type "news")
  7116. (require 'org-gnus)
  7117. (org-gnus-follow-link path))
  7118. ((string= type "shell")
  7119. (let ((cmd path))
  7120. (if (or (not org-confirm-shell-link-function)
  7121. (funcall org-confirm-shell-link-function
  7122. (format "Execute \"%s\" in shell? "
  7123. (org-add-props cmd nil
  7124. 'face 'org-warning))))
  7125. (progn
  7126. (message "Executing %s" cmd)
  7127. (shell-command cmd))
  7128. (error "Abort"))))
  7129. ((string= type "elisp")
  7130. (let ((cmd path))
  7131. (if (or (not org-confirm-elisp-link-function)
  7132. (funcall org-confirm-elisp-link-function
  7133. (format "Execute \"%s\" as elisp? "
  7134. (org-add-props cmd nil
  7135. 'face 'org-warning))))
  7136. (message "%s => %s" cmd
  7137. (if (equal (string-to-char cmd) ?\()
  7138. (eval (read cmd))
  7139. (call-interactively (read cmd))))
  7140. (error "Abort"))))
  7141. (t
  7142. (browse-url-at-point))))))
  7143. (move-marker org-open-link-marker nil)
  7144. (run-hook-with-args 'org-follow-link-hook))
  7145. ;;;; Time estimates
  7146. (defun org-get-effort (&optional pom)
  7147. "Get the effort estimate for the current entry."
  7148. (org-entry-get pom org-effort-property))
  7149. ;;; File search
  7150. (defvar org-create-file-search-functions nil
  7151. "List of functions to construct the right search string for a file link.
  7152. These functions are called in turn with point at the location to
  7153. which the link should point.
  7154. A function in the hook should first test if it would like to
  7155. handle this file type, for example by checking the major-mode or
  7156. the file extension. If it decides not to handle this file, it
  7157. should just return nil to give other functions a chance. If it
  7158. does handle the file, it must return the search string to be used
  7159. when following the link. The search string will be part of the
  7160. file link, given after a double colon, and `org-open-at-point'
  7161. will automatically search for it. If special measures must be
  7162. taken to make the search successful, another function should be
  7163. added to the companion hook `org-execute-file-search-functions',
  7164. which see.
  7165. A function in this hook may also use `setq' to set the variable
  7166. `description' to provide a suggestion for the descriptive text to
  7167. be used for this link when it gets inserted into an Org-mode
  7168. buffer with \\[org-insert-link].")
  7169. (defvar org-execute-file-search-functions nil
  7170. "List of functions to execute a file search triggered by a link.
  7171. Functions added to this hook must accept a single argument, the
  7172. search string that was part of the file link, the part after the
  7173. double colon. The function must first check if it would like to
  7174. handle this search, for example by checking the major-mode or the
  7175. file extension. If it decides not to handle this search, it
  7176. should just return nil to give other functions a chance. If it
  7177. does handle the search, it must return a non-nil value to keep
  7178. other functions from trying.
  7179. Each function can access the current prefix argument through the
  7180. variable `current-prefix-argument'. Note that a single prefix is
  7181. used to force opening a link in Emacs, so it may be good to only
  7182. use a numeric or double prefix to guide the search function.
  7183. In case this is needed, a function in this hook can also restore
  7184. the window configuration before `org-open-at-point' was called using:
  7185. (set-window-configuration org-window-config-before-follow-link)")
  7186. (defun org-link-search (s &optional type avoid-pos)
  7187. "Search for a link search option.
  7188. If S is surrounded by forward slashes, it is interpreted as a
  7189. regular expression. In org-mode files, this will create an `org-occur'
  7190. sparse tree. In ordinary files, `occur' will be used to list matches.
  7191. If the current buffer is in `dired-mode', grep will be used to search
  7192. in all files. If AVOID-POS is given, ignore matches near that position."
  7193. (let ((case-fold-search t)
  7194. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7195. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7196. (append '(("") (" ") ("\t") ("\n"))
  7197. org-emphasis-alist)
  7198. "\\|") "\\)"))
  7199. (pos (point))
  7200. (pre nil) (post nil)
  7201. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7202. (cond
  7203. ;; First check if there are any special
  7204. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7205. ;; Now try the builtin stuff
  7206. ((and (equal (string-to-char s0) ?#)
  7207. (> (length s0) 1)
  7208. (save-excursion
  7209. (goto-char (point-min))
  7210. (and
  7211. (re-search-forward
  7212. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7213. (setq type 'dedicated
  7214. pos (match-beginning 0))))
  7215. ;; There is an exact target for this
  7216. (goto-char pos)
  7217. (org-back-to-heading t)))
  7218. ((save-excursion
  7219. (goto-char (point-min))
  7220. (and
  7221. (re-search-forward
  7222. (concat "<<" (regexp-quote s0) ">>") nil t)
  7223. (setq type 'dedicated
  7224. pos (match-beginning 0))))
  7225. ;; There is an exact target for this
  7226. (goto-char pos))
  7227. ((and (string-match "^(\\(.*\\))$" s0)
  7228. (save-excursion
  7229. (goto-char (point-min))
  7230. (and
  7231. (re-search-forward
  7232. (concat "[^[]" (regexp-quote
  7233. (format org-coderef-label-format
  7234. (match-string 1 s0))))
  7235. nil t)
  7236. (setq type 'dedicated
  7237. pos (1+ (match-beginning 0))))))
  7238. ;; There is a coderef target for this
  7239. (goto-char pos))
  7240. ((string-match "^/\\(.*\\)/$" s)
  7241. ;; A regular expression
  7242. (cond
  7243. ((org-mode-p)
  7244. (org-occur (match-string 1 s)))
  7245. ;;((eq major-mode 'dired-mode)
  7246. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7247. (t (org-do-occur (match-string 1 s)))))
  7248. (t
  7249. ;; A normal search strings
  7250. (when (equal (string-to-char s) ?*)
  7251. ;; Anchor on headlines, post may include tags.
  7252. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7253. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7254. s (substring s 1)))
  7255. (remove-text-properties
  7256. 0 (length s)
  7257. '(face nil mouse-face nil keymap nil fontified nil) s)
  7258. ;; Make a series of regular expressions to find a match
  7259. (setq words (org-split-string s "[ \n\r\t]+")
  7260. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7261. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7262. "\\)" markers)
  7263. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7264. re2a (concat "[ \t\r\n]" re2a_)
  7265. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7266. re4 (concat "[^a-zA-Z_]" re4_)
  7267. re1 (concat pre re2 post)
  7268. re3 (concat pre (if pre re4_ re4) post)
  7269. re5 (concat pre ".*" re4)
  7270. re2 (concat pre re2)
  7271. re2a (concat pre (if pre re2a_ re2a))
  7272. re4 (concat pre (if pre re4_ re4))
  7273. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7274. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7275. re5 "\\)"
  7276. ))
  7277. (cond
  7278. ((eq type 'org-occur) (org-occur reall))
  7279. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7280. (t (goto-char (point-min))
  7281. (setq type 'fuzzy)
  7282. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7283. (org-search-not-self 1 re1 nil t)
  7284. (org-search-not-self 1 re2 nil t)
  7285. (org-search-not-self 1 re2a nil t)
  7286. (org-search-not-self 1 re3 nil t)
  7287. (org-search-not-self 1 re4 nil t)
  7288. (org-search-not-self 1 re5 nil t)
  7289. )
  7290. (goto-char (match-beginning 1))
  7291. (goto-char pos)
  7292. (error "No match")))))
  7293. (t
  7294. ;; Normal string-search
  7295. (goto-char (point-min))
  7296. (if (search-forward s nil t)
  7297. (goto-char (match-beginning 0))
  7298. (error "No match"))))
  7299. (and (org-mode-p) (org-show-context 'link-search))
  7300. type))
  7301. (defun org-search-not-self (group &rest args)
  7302. "Execute `re-search-forward', but only accept matches that do not
  7303. enclose the position of `org-open-link-marker'."
  7304. (let ((m org-open-link-marker))
  7305. (catch 'exit
  7306. (while (apply 're-search-forward args)
  7307. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7308. (goto-char (match-end group))
  7309. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7310. (> (match-beginning 0) (marker-position m))
  7311. (< (match-end 0) (marker-position m)))
  7312. (save-match-data
  7313. (or (not (org-in-regexp
  7314. org-bracket-link-analytic-regexp 1))
  7315. (not (match-end 4)) ; no description
  7316. (and (<= (match-beginning 4) (point))
  7317. (>= (match-end 4) (point))))))
  7318. (throw 'exit (point))))))))
  7319. (defun org-get-buffer-for-internal-link (buffer)
  7320. "Return a buffer to be used for displaying the link target of internal links."
  7321. (cond
  7322. ((not org-display-internal-link-with-indirect-buffer)
  7323. buffer)
  7324. ((string-match "(Clone)$" (buffer-name buffer))
  7325. (message "Buffer is already a clone, not making another one")
  7326. ;; we also do not modify visibility in this case
  7327. buffer)
  7328. (t ; make a new indirect buffer for displaying the link
  7329. (let* ((bn (buffer-name buffer))
  7330. (ibn (concat bn "(Clone)"))
  7331. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7332. (with-current-buffer ib (org-overview))
  7333. ib))))
  7334. (defun org-do-occur (regexp &optional cleanup)
  7335. "Call the Emacs command `occur'.
  7336. If CLEANUP is non-nil, remove the printout of the regular expression
  7337. in the *Occur* buffer. This is useful if the regex is long and not useful
  7338. to read."
  7339. (occur regexp)
  7340. (when cleanup
  7341. (let ((cwin (selected-window)) win beg end)
  7342. (when (setq win (get-buffer-window "*Occur*"))
  7343. (select-window win))
  7344. (goto-char (point-min))
  7345. (when (re-search-forward "match[a-z]+" nil t)
  7346. (setq beg (match-end 0))
  7347. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7348. (setq end (1- (match-beginning 0)))))
  7349. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7350. (goto-char (point-min))
  7351. (select-window cwin))))
  7352. ;;; The mark ring for links jumps
  7353. (defvar org-mark-ring nil
  7354. "Mark ring for positions before jumps in Org-mode.")
  7355. (defvar org-mark-ring-last-goto nil
  7356. "Last position in the mark ring used to go back.")
  7357. ;; Fill and close the ring
  7358. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7359. (loop for i from 1 to org-mark-ring-length do
  7360. (push (make-marker) org-mark-ring))
  7361. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7362. org-mark-ring)
  7363. (defun org-mark-ring-push (&optional pos buffer)
  7364. "Put the current position or POS into the mark ring and rotate it."
  7365. (interactive)
  7366. (setq pos (or pos (point)))
  7367. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7368. (move-marker (car org-mark-ring)
  7369. (or pos (point))
  7370. (or buffer (current-buffer)))
  7371. (message "%s"
  7372. (substitute-command-keys
  7373. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7374. (defun org-mark-ring-goto (&optional n)
  7375. "Jump to the previous position in the mark ring.
  7376. With prefix arg N, jump back that many stored positions. When
  7377. called several times in succession, walk through the entire ring.
  7378. Org-mode commands jumping to a different position in the current file,
  7379. or to another Org-mode file, automatically push the old position
  7380. onto the ring."
  7381. (interactive "p")
  7382. (let (p m)
  7383. (if (eq last-command this-command)
  7384. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7385. (setq p org-mark-ring))
  7386. (setq org-mark-ring-last-goto p)
  7387. (setq m (car p))
  7388. (switch-to-buffer (marker-buffer m))
  7389. (goto-char m)
  7390. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7391. (defun org-remove-angle-brackets (s)
  7392. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7393. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7394. s)
  7395. (defun org-add-angle-brackets (s)
  7396. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7397. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7398. s)
  7399. (defun org-remove-double-quotes (s)
  7400. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7401. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7402. s)
  7403. ;;; Following specific links
  7404. (defun org-follow-timestamp-link ()
  7405. (cond
  7406. ((org-at-date-range-p t)
  7407. (let ((org-agenda-start-on-weekday)
  7408. (t1 (match-string 1))
  7409. (t2 (match-string 2)))
  7410. (setq t1 (time-to-days (org-time-string-to-time t1))
  7411. t2 (time-to-days (org-time-string-to-time t2)))
  7412. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7413. ((org-at-timestamp-p t)
  7414. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7415. (substring (match-string 1) 0 10)))
  7416. 1))
  7417. (t (error "This should not happen"))))
  7418. ;;; Following file links
  7419. (defvar org-wait nil)
  7420. (defun org-open-file (path &optional in-emacs line search)
  7421. "Open the file at PATH.
  7422. First, this expands any special file name abbreviations. Then the
  7423. configuration variable `org-file-apps' is checked if it contains an
  7424. entry for this file type, and if yes, the corresponding command is launched.
  7425. If no application is found, Emacs simply visits the file.
  7426. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7427. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7428. and o use an external application to visit the file.
  7429. Optional LINE specifies a line to go to, optional SEARCH a string to
  7430. search for. If LINE or SEARCH is given, the file will always be
  7431. opened in Emacs.
  7432. If the file does not exist, an error is thrown."
  7433. (setq in-emacs (or in-emacs line search))
  7434. (let* ((file (if (equal path "")
  7435. buffer-file-name
  7436. (substitute-in-file-name (expand-file-name path))))
  7437. (apps (append org-file-apps (org-default-apps)))
  7438. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7439. (dirp (if remp nil (file-directory-p file)))
  7440. (file (if (and dirp org-open-directory-means-index-dot-org)
  7441. (concat (file-name-as-directory file) "index.org")
  7442. file))
  7443. (a-m-a-p (assq 'auto-mode apps))
  7444. (dfile (downcase file))
  7445. (old-buffer (current-buffer))
  7446. (old-pos (point))
  7447. (old-mode major-mode)
  7448. ext cmd)
  7449. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7450. (setq ext (match-string 1 dfile))
  7451. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7452. (setq ext (match-string 1 dfile))))
  7453. (cond
  7454. ((equal in-emacs '(16))
  7455. (setq cmd (cdr (assoc 'system apps))))
  7456. (in-emacs (setq cmd 'emacs))
  7457. (t
  7458. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7459. (and dirp (cdr (assoc 'directory apps)))
  7460. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7461. 'string-match)
  7462. (cdr (assoc ext apps))
  7463. (cdr (assoc t apps))))))
  7464. (when (eq cmd 'system)
  7465. (setq cmd (cdr (assoc 'system apps))))
  7466. (when (eq cmd 'default)
  7467. (setq cmd (cdr (assoc t apps))))
  7468. (when (eq cmd 'mailcap)
  7469. (require 'mailcap)
  7470. (mailcap-parse-mailcaps)
  7471. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7472. (command (mailcap-mime-info mime-type)))
  7473. (if (stringp command)
  7474. (setq cmd command)
  7475. (setq cmd 'emacs))))
  7476. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7477. (not (file-exists-p file))
  7478. (not org-open-non-existing-files))
  7479. (error "No such file: %s" file))
  7480. (cond
  7481. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7482. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7483. (while (string-match "['\"]%s['\"]" cmd)
  7484. (setq cmd (replace-match "%s" t t cmd)))
  7485. (while (string-match "%s" cmd)
  7486. (setq cmd (replace-match
  7487. (save-match-data
  7488. (shell-quote-argument
  7489. (convert-standard-filename file)))
  7490. t t cmd)))
  7491. (save-window-excursion
  7492. (start-process-shell-command cmd nil cmd)
  7493. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7494. ))
  7495. ((or (stringp cmd)
  7496. (eq cmd 'emacs))
  7497. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7498. (widen)
  7499. (if line (goto-line line)
  7500. (if search (org-link-search search))))
  7501. ((consp cmd)
  7502. (let ((file (convert-standard-filename file)))
  7503. (eval cmd)))
  7504. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7505. (and (org-mode-p) (eq old-mode 'org-mode)
  7506. (or (not (equal old-buffer (current-buffer)))
  7507. (not (equal old-pos (point))))
  7508. (org-mark-ring-push old-pos old-buffer))))
  7509. (defun org-default-apps ()
  7510. "Return the default applications for this operating system."
  7511. (cond
  7512. ((eq system-type 'darwin)
  7513. org-file-apps-defaults-macosx)
  7514. ((eq system-type 'windows-nt)
  7515. org-file-apps-defaults-windowsnt)
  7516. (t org-file-apps-defaults-gnu)))
  7517. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7518. "Convert extensions to regular expressions in the cars of LIST.
  7519. Also, weed out any non-string entries, because the return value is used
  7520. only for regexp matching.
  7521. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7522. point to the symbol `emacs', indicating that the file should
  7523. be opened in Emacs."
  7524. (append
  7525. (delq nil
  7526. (mapcar (lambda (x)
  7527. (if (not (stringp (car x)))
  7528. nil
  7529. (if (string-match "\\W" (car x))
  7530. x
  7531. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7532. list))
  7533. (if add-auto-mode
  7534. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7535. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7536. (defun org-file-remote-p (file)
  7537. "Test whether FILE specifies a location on a remote system.
  7538. Return non-nil if the location is indeed remote.
  7539. For example, the filename \"/user@host:/foo\" specifies a location
  7540. on the system \"/user@host:\"."
  7541. (cond ((fboundp 'file-remote-p)
  7542. (file-remote-p file))
  7543. ((fboundp 'tramp-handle-file-remote-p)
  7544. (tramp-handle-file-remote-p file))
  7545. ((and (boundp 'ange-ftp-name-format)
  7546. (string-match (car ange-ftp-name-format) file))
  7547. t)
  7548. (t nil)))
  7549. ;;;; Refiling
  7550. (defun org-get-org-file ()
  7551. "Read a filename, with default directory `org-directory'."
  7552. (let ((default (or org-default-notes-file remember-data-file)))
  7553. (read-file-name (format "File name [%s]: " default)
  7554. (file-name-as-directory org-directory)
  7555. default)))
  7556. (defun org-notes-order-reversed-p ()
  7557. "Check if the current file should receive notes in reversed order."
  7558. (cond
  7559. ((not org-reverse-note-order) nil)
  7560. ((eq t org-reverse-note-order) t)
  7561. ((not (listp org-reverse-note-order)) nil)
  7562. (t (catch 'exit
  7563. (let ((all org-reverse-note-order)
  7564. entry)
  7565. (while (setq entry (pop all))
  7566. (if (string-match (car entry) buffer-file-name)
  7567. (throw 'exit (cdr entry))))
  7568. nil)))))
  7569. (defvar org-refile-target-table nil
  7570. "The list of refile targets, created by `org-refile'.")
  7571. (defvar org-agenda-new-buffers nil
  7572. "Buffers created to visit agenda files.")
  7573. (defun org-get-refile-targets (&optional default-buffer)
  7574. "Produce a table with refile targets."
  7575. (let ((case-fold-search nil)
  7576. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7577. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7578. targets txt re files f desc descre fast-path-p level pos0)
  7579. (message "Getting targets...")
  7580. (with-current-buffer (or default-buffer (current-buffer))
  7581. (while (setq entry (pop entries))
  7582. (setq files (car entry) desc (cdr entry))
  7583. (setq fast-path-p nil)
  7584. (cond
  7585. ((null files) (setq files (list (current-buffer))))
  7586. ((eq files 'org-agenda-files)
  7587. (setq files (org-agenda-files 'unrestricted)))
  7588. ((and (symbolp files) (fboundp files))
  7589. (setq files (funcall files)))
  7590. ((and (symbolp files) (boundp files))
  7591. (setq files (symbol-value files))))
  7592. (if (stringp files) (setq files (list files)))
  7593. (cond
  7594. ((eq (car desc) :tag)
  7595. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7596. ((eq (car desc) :todo)
  7597. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7598. ((eq (car desc) :regexp)
  7599. (setq descre (cdr desc)))
  7600. ((eq (car desc) :level)
  7601. (setq descre (concat "^\\*\\{" (number-to-string
  7602. (if org-odd-levels-only
  7603. (1- (* 2 (cdr desc)))
  7604. (cdr desc)))
  7605. "\\}[ \t]")))
  7606. ((eq (car desc) :maxlevel)
  7607. (setq fast-path-p t)
  7608. (setq descre (concat "^\\*\\{1," (number-to-string
  7609. (if org-odd-levels-only
  7610. (1- (* 2 (cdr desc)))
  7611. (cdr desc)))
  7612. "\\}[ \t]")))
  7613. (t (error "Bad refiling target description %s" desc)))
  7614. (while (setq f (pop files))
  7615. (save-excursion
  7616. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7617. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7618. (setq f (expand-file-name f))
  7619. (if (eq org-refile-use-outline-path 'file)
  7620. (push (list (file-name-nondirectory f) f nil nil) targets))
  7621. (save-excursion
  7622. (save-restriction
  7623. (widen)
  7624. (goto-char (point-min))
  7625. (while (re-search-forward descre nil t)
  7626. (goto-char (setq pos0 (point-at-bol)))
  7627. (catch 'next
  7628. (when org-refile-target-verify-function
  7629. (save-match-data
  7630. (or (funcall org-refile-target-verify-function)
  7631. (throw 'next t))))
  7632. (when (looking-at org-complex-heading-regexp)
  7633. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7634. txt (org-link-display-format (match-string 4))
  7635. re (concat "^" (regexp-quote
  7636. (buffer-substring (match-beginning 1)
  7637. (match-end 4)))))
  7638. (if (match-end 5) (setq re (concat re "[ \t]+"
  7639. (regexp-quote
  7640. (match-string 5)))))
  7641. (setq re (concat re "[ \t]*$"))
  7642. (when org-refile-use-outline-path
  7643. (setq txt (mapconcat 'org-protect-slash
  7644. (append
  7645. (if (eq org-refile-use-outline-path 'file)
  7646. (list (file-name-nondirectory
  7647. (buffer-file-name (buffer-base-buffer))))
  7648. (if (eq org-refile-use-outline-path 'full-file-path)
  7649. (list (buffer-file-name (buffer-base-buffer)))))
  7650. (org-get-outline-path fast-path-p level txt)
  7651. (list txt))
  7652. "/")))
  7653. (push (list txt f re (point)) targets)))
  7654. (when (= (point) pos0)
  7655. ;; verification function has not moved point
  7656. (goto-char (point-at-eol))))))))))
  7657. (message "Getting targets...done")
  7658. (nreverse targets)))
  7659. (defun org-protect-slash (s)
  7660. (while (string-match "/" s)
  7661. (setq s (replace-match "\\" t t s)))
  7662. s)
  7663. (defvar org-olpa (make-vector 20 nil))
  7664. (defun org-get-outline-path (&optional fastp level heading)
  7665. "Return the outline path to the current entry, as a list."
  7666. (if fastp
  7667. (progn
  7668. (if (> level 19)
  7669. (error "Outline path failure, more than 19 levels."))
  7670. (loop for i from level upto 19 do
  7671. (aset org-olpa i nil))
  7672. (prog1
  7673. (delq nil (append org-olpa nil))
  7674. (aset org-olpa level heading)))
  7675. (let (rtn)
  7676. (save-excursion
  7677. (while (org-up-heading-safe)
  7678. (when (looking-at org-complex-heading-regexp)
  7679. (push (org-match-string-no-properties 4) rtn)))
  7680. rtn))))
  7681. (defvar org-refile-history nil
  7682. "History for refiling operations.")
  7683. (defvar org-after-refile-insert-hook nil
  7684. "Hook run after `org-refile' has inserted its stuff at the new location.
  7685. Note that this is still *before* the stuff will be removed from
  7686. the *old* location.")
  7687. (defun org-refile (&optional goto default-buffer rfloc)
  7688. "Move the entry at point to another heading.
  7689. The list of target headings is compiled using the information in
  7690. `org-refile-targets', which see. This list is created before each use
  7691. and will therefore always be up-to-date.
  7692. At the target location, the entry is filed as a subitem of the target heading.
  7693. Depending on `org-reverse-note-order', the new subitem will either be the
  7694. first or the last subitem.
  7695. If there is an active region, all entries in that region will be moved.
  7696. However, the region must fulfil the requirement that the first heading
  7697. is the first one sets the top-level of the moved text - at most siblings
  7698. below it are allowed.
  7699. With prefix arg GOTO, the command will only visit the target location,
  7700. not actually move anything.
  7701. With a double prefix `C-u C-u', go to the location where the last refiling
  7702. operation has put the subtree.
  7703. RFLOC can be a refile location obtained in a different way.
  7704. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7705. (interactive "P")
  7706. (let* ((cbuf (current-buffer))
  7707. (regionp (org-region-active-p))
  7708. (region-start (and regionp (region-beginning)))
  7709. (region-end (and regionp (region-end)))
  7710. (region-length (and regionp (- region-end region-start)))
  7711. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7712. pos it nbuf file re level reversed)
  7713. (when regionp (goto-char region-start)
  7714. (unless (org-kill-is-subtree-p
  7715. (buffer-substring region-start region-end))
  7716. (error "The region is not a (sequence of) subtree(s)")))
  7717. (if (equal goto '(16))
  7718. (org-refile-goto-last-stored)
  7719. (when (setq it (or rfloc
  7720. (save-excursion
  7721. (org-refile-get-location
  7722. (if goto "Goto: " "Refile to: ") default-buffer
  7723. org-refile-allow-creating-parent-nodes))))
  7724. (setq file (nth 1 it)
  7725. re (nth 2 it)
  7726. pos (nth 3 it))
  7727. (if (and (not goto)
  7728. pos
  7729. (equal (buffer-file-name) file)
  7730. (if regionp
  7731. (and (>= pos region-start)
  7732. (<= pos region-end))
  7733. (and (>= pos (point))
  7734. (< pos (save-excursion
  7735. (org-end-of-subtree t t))))))
  7736. (error "Cannot refile to position inside the tree or region"))
  7737. (setq nbuf (or (find-buffer-visiting file)
  7738. (find-file-noselect file)))
  7739. (if goto
  7740. (progn
  7741. (switch-to-buffer nbuf)
  7742. (goto-char pos)
  7743. (org-show-context 'org-goto))
  7744. (if regionp
  7745. (progn
  7746. (org-kill-new (buffer-substring region-start region-end))
  7747. (org-save-markers-in-region region-start region-end))
  7748. (org-copy-subtree 1 nil t))
  7749. (save-excursion
  7750. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7751. (find-file-noselect file))))
  7752. (setq reversed (org-notes-order-reversed-p))
  7753. (save-excursion
  7754. (save-restriction
  7755. (widen)
  7756. (if pos
  7757. (progn
  7758. (goto-char pos)
  7759. (looking-at outline-regexp)
  7760. (setq level (org-get-valid-level (funcall outline-level) 1))
  7761. (goto-char
  7762. (if reversed
  7763. (or (outline-next-heading) (point-max))
  7764. (or (save-excursion (outline-get-next-sibling))
  7765. (org-end-of-subtree t t)
  7766. (point-max)))))
  7767. (setq level 1)
  7768. (if (not reversed)
  7769. (goto-char (point-max))
  7770. (goto-char (point-min))
  7771. (or (outline-next-heading) (goto-char (point-max)))))
  7772. (if (not (bolp)) (newline))
  7773. (bookmark-set "org-refile-last-stored")
  7774. (org-paste-subtree level)
  7775. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7776. (run-hooks 'org-after-refile-insert-hook))))
  7777. (if regionp
  7778. (delete-region (point) (+ (point) region-length))
  7779. (org-cut-subtree))
  7780. (when (featurep 'org-inlinetask)
  7781. (org-inlinetask-remove-END-maybe))
  7782. (setq org-markers-to-move nil)
  7783. (message "Refiled to \"%s\"" (car it))))))
  7784. (org-reveal))
  7785. (defun org-refile-goto-last-stored ()
  7786. "Go to the location where the last refile was stored."
  7787. (interactive)
  7788. (bookmark-jump "org-refile-last-stored")
  7789. (message "This is the location of the last refile"))
  7790. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7791. "Prompt the user for a refile location, using PROMPT."
  7792. (let ((org-refile-targets org-refile-targets)
  7793. (org-refile-use-outline-path org-refile-use-outline-path))
  7794. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7795. (unless org-refile-target-table
  7796. (error "No refile targets"))
  7797. (let* ((cbuf (current-buffer))
  7798. (partial-completion-mode nil)
  7799. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7800. (cfunc (if (and org-refile-use-outline-path
  7801. org-outline-path-complete-in-steps)
  7802. 'org-olpath-completing-read
  7803. 'org-ido-completing-read))
  7804. (extra (if org-refile-use-outline-path "/" ""))
  7805. (filename (and cfn (expand-file-name cfn)))
  7806. (tbl (mapcar
  7807. (lambda (x)
  7808. (if (and (not (member org-refile-use-outline-path
  7809. '(file full-file-path)))
  7810. (not (equal filename (nth 1 x))))
  7811. (cons (concat (car x) extra " ("
  7812. (file-name-nondirectory (nth 1 x)) ")")
  7813. (cdr x))
  7814. (cons (concat (car x) extra) (cdr x))))
  7815. org-refile-target-table))
  7816. (completion-ignore-case t)
  7817. pa answ parent-target child parent)
  7818. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7819. nil 'org-refile-history))
  7820. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7821. (if pa
  7822. (progn
  7823. (setcar org-refile-history (car pa))
  7824. pa)
  7825. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7826. (setq parent (match-string 1 answ)
  7827. child (match-string 2 answ))
  7828. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7829. (when (and parent-target
  7830. (or (eq new-nodes t)
  7831. (and (eq new-nodes 'confirm)
  7832. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7833. (org-refile-new-child parent-target child))))))
  7834. (defun org-refile-new-child (parent-target child)
  7835. "Use refile target PARENT-TARGET to add new CHILD below it."
  7836. (unless parent-target
  7837. (error "Cannot find parent for new node"))
  7838. (let ((file (nth 1 parent-target))
  7839. (pos (nth 3 parent-target))
  7840. level)
  7841. (with-current-buffer (or (find-buffer-visiting file)
  7842. (find-file-noselect file))
  7843. (save-excursion
  7844. (save-restriction
  7845. (widen)
  7846. (if pos
  7847. (goto-char pos)
  7848. (goto-char (point-max))
  7849. (if (not (bolp)) (newline)))
  7850. (when (looking-at outline-regexp)
  7851. (setq level (funcall outline-level))
  7852. (org-end-of-subtree t t))
  7853. (org-back-over-empty-lines)
  7854. (insert "\n" (make-string
  7855. (if pos (org-get-valid-level level 1) 1) ?*)
  7856. " " child "\n")
  7857. (beginning-of-line 0)
  7858. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7859. (defun org-olpath-completing-read (prompt collection &rest args)
  7860. "Read an outline path like a file name."
  7861. (let ((thetable collection)
  7862. (org-completion-use-ido nil)) ; does not work with ido.
  7863. (apply
  7864. 'org-ido-completing-read prompt
  7865. (lambda (string predicate &optional flag)
  7866. (let (rtn r f (l (length string)))
  7867. (cond
  7868. ((eq flag nil)
  7869. ;; try completion
  7870. (try-completion string thetable))
  7871. ((eq flag t)
  7872. ;; all-completions
  7873. (setq rtn (all-completions string thetable predicate))
  7874. (mapcar
  7875. (lambda (x)
  7876. (setq r (substring x l))
  7877. (if (string-match " ([^)]*)$" x)
  7878. (setq f (match-string 0 x))
  7879. (setq f ""))
  7880. (if (string-match "/" r)
  7881. (concat string (substring r 0 (match-end 0)) f)
  7882. x))
  7883. rtn))
  7884. ((eq flag 'lambda)
  7885. ;; exact match?
  7886. (assoc string thetable)))
  7887. ))
  7888. args)))
  7889. ;;;; Dynamic blocks
  7890. (defun org-find-dblock (name)
  7891. "Find the first dynamic block with name NAME in the buffer.
  7892. If not found, stay at current position and return nil."
  7893. (let (pos)
  7894. (save-excursion
  7895. (goto-char (point-min))
  7896. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7897. nil t)
  7898. (match-beginning 0))))
  7899. (if pos (goto-char pos))
  7900. pos))
  7901. (defconst org-dblock-start-re
  7902. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7903. "Matches the startline of a dynamic block, with parameters.")
  7904. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7905. "Matches the end of a dynamic block.")
  7906. (defun org-create-dblock (plist)
  7907. "Create a dynamic block section, with parameters taken from PLIST.
  7908. PLIST must contain a :name entry which is used as name of the block."
  7909. (unless (bolp) (newline))
  7910. (let ((name (plist-get plist :name)))
  7911. (insert "#+BEGIN: " name)
  7912. (while plist
  7913. (if (eq (car plist) :name)
  7914. (setq plist (cddr plist))
  7915. (insert " " (prin1-to-string (pop plist)))))
  7916. (insert "\n\n#+END:\n")
  7917. (beginning-of-line -2)))
  7918. (defun org-prepare-dblock ()
  7919. "Prepare dynamic block for refresh.
  7920. This empties the block, puts the cursor at the insert position and returns
  7921. the property list including an extra property :name with the block name."
  7922. (unless (looking-at org-dblock-start-re)
  7923. (error "Not at a dynamic block"))
  7924. (let* ((begdel (1+ (match-end 0)))
  7925. (name (org-no-properties (match-string 1)))
  7926. (params (append (list :name name)
  7927. (read (concat "(" (match-string 3) ")")))))
  7928. (unless (re-search-forward org-dblock-end-re nil t)
  7929. (error "Dynamic block not terminated"))
  7930. (setq params
  7931. (append params
  7932. (list :content (buffer-substring
  7933. begdel (match-beginning 0)))))
  7934. (delete-region begdel (match-beginning 0))
  7935. (goto-char begdel)
  7936. (open-line 1)
  7937. params))
  7938. (defun org-map-dblocks (&optional command)
  7939. "Apply COMMAND to all dynamic blocks in the current buffer.
  7940. If COMMAND is not given, use `org-update-dblock'."
  7941. (let ((cmd (or command 'org-update-dblock))
  7942. pos)
  7943. (save-excursion
  7944. (goto-char (point-min))
  7945. (while (re-search-forward org-dblock-start-re nil t)
  7946. (goto-char (setq pos (match-beginning 0)))
  7947. (condition-case nil
  7948. (funcall cmd)
  7949. (error (message "Error during update of dynamic block")))
  7950. (goto-char pos)
  7951. (unless (re-search-forward org-dblock-end-re nil t)
  7952. (error "Dynamic block not terminated"))))))
  7953. (defun org-dblock-update (&optional arg)
  7954. "User command for updating dynamic blocks.
  7955. Update the dynamic block at point. With prefix ARG, update all dynamic
  7956. blocks in the buffer."
  7957. (interactive "P")
  7958. (if arg
  7959. (org-update-all-dblocks)
  7960. (or (looking-at org-dblock-start-re)
  7961. (org-beginning-of-dblock))
  7962. (org-update-dblock)))
  7963. (defun org-update-dblock ()
  7964. "Update the dynamic block at point
  7965. This means to empty the block, parse for parameters and then call
  7966. the correct writing function."
  7967. (save-window-excursion
  7968. (let* ((pos (point))
  7969. (line (org-current-line))
  7970. (params (org-prepare-dblock))
  7971. (name (plist-get params :name))
  7972. (cmd (intern (concat "org-dblock-write:" name))))
  7973. (message "Updating dynamic block `%s' at line %d..." name line)
  7974. (funcall cmd params)
  7975. (message "Updating dynamic block `%s' at line %d...done" name line)
  7976. (goto-char pos))))
  7977. (defun org-beginning-of-dblock ()
  7978. "Find the beginning of the dynamic block at point.
  7979. Error if there is no such block at point."
  7980. (let ((pos (point))
  7981. beg)
  7982. (end-of-line 1)
  7983. (if (and (re-search-backward org-dblock-start-re nil t)
  7984. (setq beg (match-beginning 0))
  7985. (re-search-forward org-dblock-end-re nil t)
  7986. (> (match-end 0) pos))
  7987. (goto-char beg)
  7988. (goto-char pos)
  7989. (error "Not in a dynamic block"))))
  7990. (defun org-update-all-dblocks ()
  7991. "Update all dynamic blocks in the buffer.
  7992. This function can be used in a hook."
  7993. (when (org-mode-p)
  7994. (org-map-dblocks 'org-update-dblock)))
  7995. ;;;; Completion
  7996. (defconst org-additional-option-like-keywords
  7997. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  7998. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  7999. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8000. "BEGIN:" "END:"
  8001. "ORGTBL" "TBLFM:" "TBLNAME:"
  8002. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8003. "BEGIN_QUOTE" "END_QUOTE"
  8004. "BEGIN_VERSE" "END_VERSE"
  8005. "BEGIN_CENTER" "END_CENTER"
  8006. "BEGIN_SRC" "END_SRC"
  8007. "CATEGORY" "COLUMNS"
  8008. "CAPTION" "LABEL"
  8009. "BIND"))
  8010. (defcustom org-structure-template-alist
  8011. '(
  8012. ("s" "#+begin_src ?\n\n#+end_src"
  8013. "<src lang=\"?\">\n\n</src>")
  8014. ("e" "#+begin_example\n?\n#+end_example"
  8015. "<example>\n?\n</example>")
  8016. ("q" "#+begin_quote\n?\n#+end_quote"
  8017. "<quote>\n?\n</quote>")
  8018. ("v" "#+begin_verse\n?\n#+end_verse"
  8019. "<verse>\n?\n/verse>")
  8020. ("c" "#+begin_center\n?\n#+end_center"
  8021. "<center>\n?\n/center>")
  8022. ("l" "#+begin_latex\n?\n#+end_latex"
  8023. "<literal style=\"latex\">\n?\n</literal>")
  8024. ("L" "#+latex: "
  8025. "<literal style=\"latex\">?</literal>")
  8026. ("h" "#+begin_html\n?\n#+end_html"
  8027. "<literal style=\"html\">\n?\n</literal>")
  8028. ("H" "#+html: "
  8029. "<literal style=\"html\">?</literal>")
  8030. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8031. ("A" "#+ascii: ")
  8032. ("i" "#+include %file ?"
  8033. "<include file=%file markup=\"?\">")
  8034. )
  8035. "Structure completion elements.
  8036. This is a list of abbreviation keys and values. The value gets inserted
  8037. it you type @samp{.} followed by the key and then the completion key,
  8038. usually `M-TAB'. %file will be replaced by a file name after prompting
  8039. for the file using completion.
  8040. There are two templates for each key, the first uses the original Org syntax,
  8041. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8042. the default when the /org-mtags.el/ module has been loaded. See also the
  8043. variable `org-mtags-prefer-muse-templates'.
  8044. This is an experimental feature, it is undecided if it is going to stay in."
  8045. :group 'org-completion
  8046. :type '(repeat
  8047. (string :tag "Key")
  8048. (string :tag "Template")
  8049. (string :tag "Muse Template")))
  8050. (defun org-try-structure-completion ()
  8051. "Try to complete a structure template before point.
  8052. This looks for strings like \"<e\" on an otherwise empty line and
  8053. expands them."
  8054. (let ((l (buffer-substring (point-at-bol) (point)))
  8055. a)
  8056. (when (and (looking-at "[ \t]*$")
  8057. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8058. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8059. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8060. (match-beginning 1)) a)
  8061. t)))
  8062. (defun org-complete-expand-structure-template (start cell)
  8063. "Expand a structure template."
  8064. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8065. (rpl (nth (if musep 2 1) cell))
  8066. (ind ""))
  8067. (delete-region start (point))
  8068. (when (string-match "\\`#\\+" rpl)
  8069. (cond
  8070. ((bolp))
  8071. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8072. (setq ind (buffer-substring (point-at-bol) (point))))
  8073. (t (newline))))
  8074. (setq start (point))
  8075. (if (string-match "%file" rpl)
  8076. (setq rpl (replace-match
  8077. (concat
  8078. "\""
  8079. (save-match-data
  8080. (abbreviate-file-name (read-file-name "Include file: ")))
  8081. "\"")
  8082. t t rpl)))
  8083. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8084. (concat "\n" ind)))
  8085. (insert rpl)
  8086. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8087. (defun org-complete (&optional arg)
  8088. "Perform completion on word at point.
  8089. At the beginning of a headline, this completes TODO keywords as given in
  8090. `org-todo-keywords'.
  8091. If the current word is preceded by a backslash, completes the TeX symbols
  8092. that are supported for HTML support.
  8093. If the current word is preceded by \"#+\", completes special words for
  8094. setting file options.
  8095. In the line after \"#+STARTUP:, complete valid keywords.\"
  8096. At all other locations, this simply calls the value of
  8097. `org-completion-fallback-command'."
  8098. (interactive "P")
  8099. (org-without-partial-completion
  8100. (catch 'exit
  8101. (let* ((a nil)
  8102. (end (point))
  8103. (beg1 (save-excursion
  8104. (skip-chars-backward (org-re "[:alnum:]_@"))
  8105. (point)))
  8106. (beg (save-excursion
  8107. (skip-chars-backward "a-zA-Z0-9_:$")
  8108. (point)))
  8109. (confirm (lambda (x) (stringp (car x))))
  8110. (searchhead (equal (char-before beg) ?*))
  8111. (struct
  8112. (when (and (member (char-before beg1) '(?. ?<))
  8113. (setq a (assoc (buffer-substring beg1 (point))
  8114. org-structure-template-alist)))
  8115. (org-complete-expand-structure-template (1- beg1) a)
  8116. (throw 'exit t)))
  8117. (tag (and (equal (char-before beg1) ?:)
  8118. (equal (char-after (point-at-bol)) ?*)))
  8119. (prop (and (equal (char-before beg1) ?:)
  8120. (not (equal (char-after (point-at-bol)) ?*))))
  8121. (texp (equal (char-before beg) ?\\))
  8122. (link (equal (char-before beg) ?\[))
  8123. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8124. beg)
  8125. "#+"))
  8126. (startup (string-match "^#\\+STARTUP:.*"
  8127. (buffer-substring (point-at-bol) (point))))
  8128. (completion-ignore-case opt)
  8129. (type nil)
  8130. (tbl nil)
  8131. (table (cond
  8132. (opt
  8133. (setq type :opt)
  8134. (require 'org-exp)
  8135. (append
  8136. (mapcar
  8137. (lambda (x)
  8138. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8139. (cons (match-string 2 x) (match-string 1 x)))
  8140. (org-split-string (org-get-current-options) "\n"))
  8141. (mapcar 'list org-additional-option-like-keywords)))
  8142. (startup
  8143. (setq type :startup)
  8144. org-startup-options)
  8145. (link (append org-link-abbrev-alist-local
  8146. org-link-abbrev-alist))
  8147. (texp
  8148. (setq type :tex)
  8149. org-html-entities)
  8150. ((string-match "\\`\\*+[ \t]+\\'"
  8151. (buffer-substring (point-at-bol) beg))
  8152. (setq type :todo)
  8153. (mapcar 'list org-todo-keywords-1))
  8154. (searchhead
  8155. (setq type :searchhead)
  8156. (save-excursion
  8157. (goto-char (point-min))
  8158. (while (re-search-forward org-todo-line-regexp nil t)
  8159. (push (list
  8160. (org-make-org-heading-search-string
  8161. (match-string 3) t))
  8162. tbl)))
  8163. tbl)
  8164. (tag (setq type :tag beg beg1)
  8165. (or org-tag-alist (org-get-buffer-tags)))
  8166. (prop (setq type :prop beg beg1)
  8167. (mapcar 'list (org-buffer-property-keys nil t t)))
  8168. (t (progn
  8169. (call-interactively org-completion-fallback-command)
  8170. (throw 'exit nil)))))
  8171. (pattern (buffer-substring-no-properties beg end))
  8172. (completion (try-completion pattern table confirm)))
  8173. (cond ((eq completion t)
  8174. (if (not (assoc (upcase pattern) table))
  8175. (message "Already complete")
  8176. (if (and (equal type :opt)
  8177. (not (member (car (assoc (upcase pattern) table))
  8178. org-additional-option-like-keywords)))
  8179. (insert (substring (cdr (assoc (upcase pattern) table))
  8180. (length pattern)))
  8181. (if (memq type '(:tag :prop)) (insert ":")))))
  8182. ((null completion)
  8183. (message "Can't find completion for \"%s\"" pattern)
  8184. (ding))
  8185. ((not (string= pattern completion))
  8186. (delete-region beg end)
  8187. (if (string-match " +$" completion)
  8188. (setq completion (replace-match "" t t completion)))
  8189. (insert completion)
  8190. (if (get-buffer-window "*Completions*")
  8191. (delete-window (get-buffer-window "*Completions*")))
  8192. (if (assoc completion table)
  8193. (if (eq type :todo) (insert " ")
  8194. (if (memq type '(:tag :prop)) (insert ":"))))
  8195. (if (and (equal type :opt) (assoc completion table))
  8196. (message "%s" (substitute-command-keys
  8197. "Press \\[org-complete] again to insert example settings"))))
  8198. (t
  8199. (message "Making completion list...")
  8200. (let ((list (sort (all-completions pattern table confirm)
  8201. 'string<)))
  8202. (with-output-to-temp-buffer "*Completions*"
  8203. (condition-case nil
  8204. ;; Protection needed for XEmacs and emacs 21
  8205. (display-completion-list list pattern)
  8206. (error (display-completion-list list)))))
  8207. (message "Making completion list...%s" "done")))))))
  8208. ;;;; TODO, DEADLINE, Comments
  8209. (defun org-toggle-comment ()
  8210. "Change the COMMENT state of an entry."
  8211. (interactive)
  8212. (save-excursion
  8213. (org-back-to-heading)
  8214. (let (case-fold-search)
  8215. (if (looking-at (concat outline-regexp
  8216. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8217. (replace-match "" t t nil 1)
  8218. (if (looking-at outline-regexp)
  8219. (progn
  8220. (goto-char (match-end 0))
  8221. (insert org-comment-string " ")))))))
  8222. (defvar org-last-todo-state-is-todo nil
  8223. "This is non-nil when the last TODO state change led to a TODO state.
  8224. If the last change removed the TODO tag or switched to DONE, then
  8225. this is nil.")
  8226. (defvar org-setting-tags nil) ; dynamically skipped
  8227. (defun org-parse-local-options (string var)
  8228. "Parse STRING for startup setting relevant for variable VAR."
  8229. (let ((rtn (symbol-value var))
  8230. e opts)
  8231. (save-match-data
  8232. (if (or (not string) (not (string-match "\\S-" string)))
  8233. rtn
  8234. (setq opts (delq nil (mapcar (lambda (x)
  8235. (setq e (assoc x org-startup-options))
  8236. (if (eq (nth 1 e) var) e nil))
  8237. (org-split-string string "[ \t]+"))))
  8238. (if (not opts)
  8239. rtn
  8240. (setq rtn nil)
  8241. (while (setq e (pop opts))
  8242. (if (not (nth 3 e))
  8243. (setq rtn (nth 2 e))
  8244. (if (not (listp rtn)) (setq rtn nil))
  8245. (push (nth 2 e) rtn)))
  8246. rtn)))))
  8247. (defvar org-todo-setup-filter-hook nil
  8248. "Hook for functions that pre-filter todo specs.
  8249. Each function takes a todo spec and returns either `nil' or the spec
  8250. transformed into canonical form." )
  8251. (defvar org-todo-get-default-hook nil
  8252. "Hook for functions that get a default item for todo.
  8253. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8254. `nil' or a string to be used for the todo mark." )
  8255. (defvar org-agenda-headline-snapshot-before-repeat)
  8256. (defun org-todo (&optional arg)
  8257. "Change the TODO state of an item.
  8258. The state of an item is given by a keyword at the start of the heading,
  8259. like
  8260. *** TODO Write paper
  8261. *** DONE Call mom
  8262. The different keywords are specified in the variable `org-todo-keywords'.
  8263. By default the available states are \"TODO\" and \"DONE\".
  8264. So for this example: when the item starts with TODO, it is changed to DONE.
  8265. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8266. DONE are present, add TODO at the beginning of the heading.
  8267. With C-u prefix arg, use completion to determine the new state.
  8268. With numeric prefix arg, switch to that state.
  8269. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8270. With a tripple C-u prefix, circumvent any state blocking.
  8271. For calling through lisp, arg is also interpreted in the following way:
  8272. 'none -> empty state
  8273. \"\"(empty string) -> switch to empty state
  8274. 'done -> switch to DONE
  8275. 'nextset -> switch to the next set of keywords
  8276. 'previousset -> switch to the previous set of keywords
  8277. \"WAITING\" -> switch to the specified keyword, but only if it
  8278. really is a member of `org-todo-keywords'."
  8279. (interactive "P")
  8280. (if (equal arg '(16)) (setq arg 'nextset))
  8281. (let ((org-blocker-hook org-blocker-hook)
  8282. (case-fold-search nil))
  8283. (when (equal arg '(64))
  8284. (setq arg nil org-blocker-hook nil))
  8285. (when (and org-blocker-hook
  8286. (or org-inhibit-blocking
  8287. (org-entry-get nil "NOBLOCKING")))
  8288. (setq org-blocker-hook nil))
  8289. (save-excursion
  8290. (catch 'exit
  8291. (org-back-to-heading)
  8292. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8293. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8294. (looking-at " *"))
  8295. (let* ((match-data (match-data))
  8296. (startpos (point-at-bol))
  8297. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8298. (org-log-done org-log-done)
  8299. (org-log-repeat org-log-repeat)
  8300. (org-todo-log-states org-todo-log-states)
  8301. (this (match-string 1))
  8302. (hl-pos (match-beginning 0))
  8303. (head (org-get-todo-sequence-head this))
  8304. (ass (assoc head org-todo-kwd-alist))
  8305. (interpret (nth 1 ass))
  8306. (done-word (nth 3 ass))
  8307. (final-done-word (nth 4 ass))
  8308. (last-state (or this ""))
  8309. (completion-ignore-case t)
  8310. (member (member this org-todo-keywords-1))
  8311. (tail (cdr member))
  8312. (state (cond
  8313. ((and org-todo-key-trigger
  8314. (or (and (equal arg '(4))
  8315. (eq org-use-fast-todo-selection 'prefix))
  8316. (and (not arg) org-use-fast-todo-selection
  8317. (not (eq org-use-fast-todo-selection
  8318. 'prefix)))))
  8319. ;; Use fast selection
  8320. (org-fast-todo-selection))
  8321. ((and (equal arg '(4))
  8322. (or (not org-use-fast-todo-selection)
  8323. (not org-todo-key-trigger)))
  8324. ;; Read a state with completion
  8325. (org-ido-completing-read
  8326. "State: " (mapcar (lambda(x) (list x))
  8327. org-todo-keywords-1)
  8328. nil t))
  8329. ((eq arg 'right)
  8330. (if this
  8331. (if tail (car tail) nil)
  8332. (car org-todo-keywords-1)))
  8333. ((eq arg 'left)
  8334. (if (equal member org-todo-keywords-1)
  8335. nil
  8336. (if this
  8337. (nth (- (length org-todo-keywords-1)
  8338. (length tail) 2)
  8339. org-todo-keywords-1)
  8340. (org-last org-todo-keywords-1))))
  8341. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8342. (setq arg nil))) ; hack to fall back to cycling
  8343. (arg
  8344. ;; user or caller requests a specific state
  8345. (cond
  8346. ((equal arg "") nil)
  8347. ((eq arg 'none) nil)
  8348. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8349. ((eq arg 'nextset)
  8350. (or (car (cdr (member head org-todo-heads)))
  8351. (car org-todo-heads)))
  8352. ((eq arg 'previousset)
  8353. (let ((org-todo-heads (reverse org-todo-heads)))
  8354. (or (car (cdr (member head org-todo-heads)))
  8355. (car org-todo-heads))))
  8356. ((car (member arg org-todo-keywords-1)))
  8357. ((nth (1- (prefix-numeric-value arg))
  8358. org-todo-keywords-1))))
  8359. ((null member) (or head (car org-todo-keywords-1)))
  8360. ((equal this final-done-word) nil) ;; -> make empty
  8361. ((null tail) nil) ;; -> first entry
  8362. ((memq interpret '(type priority))
  8363. (if (eq this-command last-command)
  8364. (car tail)
  8365. (if (> (length tail) 0)
  8366. (or done-word (car org-done-keywords))
  8367. nil)))
  8368. (t
  8369. (car tail))))
  8370. (state (or
  8371. (run-hook-with-args-until-success
  8372. 'org-todo-get-default-hook state last-state)
  8373. state))
  8374. (next (if state (concat " " state " ") " "))
  8375. (change-plist (list :type 'todo-state-change :from this :to state
  8376. :position startpos))
  8377. dolog now-done-p)
  8378. (when org-blocker-hook
  8379. (setq org-last-todo-state-is-todo
  8380. (not (member this org-done-keywords)))
  8381. (unless (save-excursion
  8382. (save-match-data
  8383. (run-hook-with-args-until-failure
  8384. 'org-blocker-hook change-plist)))
  8385. (if (interactive-p)
  8386. (error "TODO state change from %s to %s blocked" this state)
  8387. ;; fail silently
  8388. (message "TODO state change from %s to %s blocked" this state)
  8389. (throw 'exit nil))))
  8390. (store-match-data match-data)
  8391. (replace-match next t t)
  8392. (unless (pos-visible-in-window-p hl-pos)
  8393. (message "TODO state changed to %s" (org-trim next)))
  8394. (unless head
  8395. (setq head (org-get-todo-sequence-head state)
  8396. ass (assoc head org-todo-kwd-alist)
  8397. interpret (nth 1 ass)
  8398. done-word (nth 3 ass)
  8399. final-done-word (nth 4 ass)))
  8400. (when (memq arg '(nextset previousset))
  8401. (message "Keyword-Set %d/%d: %s"
  8402. (- (length org-todo-sets) -1
  8403. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8404. (length org-todo-sets)
  8405. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8406. (setq org-last-todo-state-is-todo
  8407. (not (member state org-done-keywords)))
  8408. (setq now-done-p (and (member state org-done-keywords)
  8409. (not (member this org-done-keywords))))
  8410. (and logging (org-local-logging logging))
  8411. (when (and (or org-todo-log-states org-log-done)
  8412. (not (eq org-inhibit-logging t))
  8413. (not (memq arg '(nextset previousset))))
  8414. ;; we need to look at recording a time and note
  8415. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8416. (nth 2 (assoc this org-todo-log-states))))
  8417. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8418. (setq dolog 'time))
  8419. (when (and state
  8420. (member state org-not-done-keywords)
  8421. (not (member this org-not-done-keywords)))
  8422. ;; This is now a todo state and was not one before
  8423. ;; If there was a CLOSED time stamp, get rid of it.
  8424. (org-add-planning-info nil nil 'closed))
  8425. (when (and now-done-p org-log-done)
  8426. ;; It is now done, and it was not done before
  8427. (org-add-planning-info 'closed (org-current-time))
  8428. (if (and (not dolog) (eq 'note org-log-done))
  8429. (org-add-log-setup 'done state this 'findpos 'note)))
  8430. (when (and state dolog)
  8431. ;; This is a non-nil state, and we need to log it
  8432. (org-add-log-setup 'state state this 'findpos dolog)))
  8433. ;; Fixup tag positioning
  8434. (org-todo-trigger-tag-changes state)
  8435. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8436. (when org-provide-todo-statistics
  8437. (org-update-parent-todo-statistics))
  8438. (run-hooks 'org-after-todo-state-change-hook)
  8439. (if (and arg (not (member state org-done-keywords)))
  8440. (setq head (org-get-todo-sequence-head state)))
  8441. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8442. ;; Do we need to trigger a repeat?
  8443. (when now-done-p
  8444. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8445. ;; This is for the agenda, take a snapshot of the headline.
  8446. (save-match-data
  8447. (setq org-agenda-headline-snapshot-before-repeat
  8448. (org-get-heading))))
  8449. (org-auto-repeat-maybe state))
  8450. ;; Fixup cursor location if close to the keyword
  8451. (if (and (outline-on-heading-p)
  8452. (not (bolp))
  8453. (save-excursion (beginning-of-line 1)
  8454. (looking-at org-todo-line-regexp))
  8455. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8456. (progn
  8457. (goto-char (or (match-end 2) (match-end 1)))
  8458. (and (looking-at " ") (just-one-space))))
  8459. (when org-trigger-hook
  8460. (save-excursion
  8461. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8462. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8463. "Block turning an entry into a TODO, using the hierarchy.
  8464. This checks whether the current task should be blocked from state
  8465. changes. Such blocking occurs when:
  8466. 1. The task has children which are not all in a completed state.
  8467. 2. A task has a parent with the property :ORDERED:, and there
  8468. are siblings prior to the current task with incomplete
  8469. status.
  8470. 3. The parent of the task is blocked because it has siblings that should
  8471. be done first, or is child of a block grandparent TODO entry."
  8472. (catch 'dont-block
  8473. ;; If this is not a todo state change, or if this entry is already DONE,
  8474. ;; do not block
  8475. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8476. (member (plist-get change-plist :from)
  8477. (cons 'done org-done-keywords))
  8478. (member (plist-get change-plist :to)
  8479. (cons 'todo org-not-done-keywords)))
  8480. (throw 'dont-block t))
  8481. ;; If this task has children, and any are undone, it's blocked
  8482. (save-excursion
  8483. (org-back-to-heading t)
  8484. (let ((this-level (funcall outline-level)))
  8485. (outline-next-heading)
  8486. (let ((child-level (funcall outline-level)))
  8487. (while (and (not (eobp))
  8488. (> child-level this-level))
  8489. ;; this todo has children, check whether they are all
  8490. ;; completed
  8491. (if (and (not (org-entry-is-done-p))
  8492. (org-entry-is-todo-p))
  8493. (throw 'dont-block nil))
  8494. (outline-next-heading)
  8495. (setq child-level (funcall outline-level))))))
  8496. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8497. ;; any previous siblings are undone, it's blocked
  8498. (save-excursion
  8499. (org-back-to-heading t)
  8500. (let* ((pos (point))
  8501. (parent-pos (and (org-up-heading-safe) (point))))
  8502. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8503. (when (and (org-entry-get (point) "ORDERED")
  8504. (forward-line 1)
  8505. (re-search-forward org-not-done-heading-regexp pos t))
  8506. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8507. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8508. (while t
  8509. (goto-char parent-pos)
  8510. (if (not (looking-at org-not-done-heading-regexp))
  8511. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8512. (setq pos (point))
  8513. (setq parent-pos (and (org-up-heading-safe) (point)))
  8514. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8515. (when (and (org-entry-get (point) "ORDERED")
  8516. (forward-line 1)
  8517. (re-search-forward org-not-done-heading-regexp pos t))
  8518. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8519. (defcustom org-track-ordered-property-with-tag nil
  8520. "Should the ORDERED property also be shown as a tag?
  8521. The ORDERED property decides if an entry should require subtasks to be
  8522. completed in sequence. Since a property is not very visible, setting
  8523. this option means that toggling the ORDERED property with the command
  8524. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8525. not relevant for the behavior, but it makes things more visible.
  8526. Note that toggling the tag with tags commands will not change the property
  8527. and therefore not influence behavior!
  8528. This can be t, meaning the tag ORDERED should be used, It can also be a
  8529. string to select a different tag for this task."
  8530. :group 'org-todo
  8531. :type '(choice
  8532. (const :tag "No tracking" nil)
  8533. (const :tag "Track with ORDERED tag" t)
  8534. (string :tag "Use other tag")))
  8535. (defun org-toggle-ordered-property ()
  8536. "Toggle the ORDERED property of the current entry.
  8537. For better visibility, you can track the value of this property with a tag.
  8538. See variable `org-track-ordered-property-with-tag'."
  8539. (interactive)
  8540. (let* ((t1 org-track-ordered-property-with-tag)
  8541. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8542. (save-excursion
  8543. (org-back-to-heading)
  8544. (if (org-entry-get nil "ORDERED")
  8545. (progn
  8546. (org-delete-property "ORDERED")
  8547. (and tag (org-toggle-tag tag 'off))
  8548. (message "Subtasks can be completed in arbitrary order"))
  8549. (org-entry-put nil "ORDERED" "t")
  8550. (and tag (org-toggle-tag tag 'on))
  8551. (message "Subtasks must be completed in sequence")))))
  8552. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8553. (defun org-block-todo-from-checkboxes (change-plist)
  8554. "Block turning an entry into a TODO, using checkboxes.
  8555. This checks whether the current task should be blocked from state
  8556. changes because there are uncheckd boxes in this entry."
  8557. (catch 'dont-block
  8558. ;; If this is not a todo state change, or if this entry is already DONE,
  8559. ;; do not block
  8560. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8561. (member (plist-get change-plist :from)
  8562. (cons 'done org-done-keywords))
  8563. (member (plist-get change-plist :to)
  8564. (cons 'todo org-not-done-keywords)))
  8565. (throw 'dont-block t))
  8566. ;; If this task has checkboxes that are not checked, it's blocked
  8567. (save-excursion
  8568. (org-back-to-heading t)
  8569. (let ((beg (point)) end)
  8570. (outline-next-heading)
  8571. (setq end (point))
  8572. (goto-char beg)
  8573. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8574. end t)
  8575. (progn
  8576. (if (boundp 'org-blocked-by-checkboxes)
  8577. (setq org-blocked-by-checkboxes t))
  8578. (throw 'dont-block nil)))))
  8579. t)) ; do not block
  8580. (defvar org-entry-property-inherited-from) ;; defined below
  8581. (defun org-update-parent-todo-statistics ()
  8582. "Update any statistics cookie in the parent of the current headline.
  8583. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8584. the entire subtree and this will travel up the hierarchy and update
  8585. statistics everywhere."
  8586. (interactive)
  8587. (let* ((lim 0) prop
  8588. (recursive (or (not org-hierarchical-todo-statistics)
  8589. (string-match
  8590. "\\<recursive\\>"
  8591. (or (setq prop (org-entry-get
  8592. nil "COOKIE_DATA" 'inherit)) ""))))
  8593. (lim (or (and prop (marker-position
  8594. org-entry-property-inherited-from))
  8595. lim))
  8596. (first t)
  8597. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8598. level ltoggle l1
  8599. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8600. (catch 'exit
  8601. (save-excursion
  8602. (beginning-of-line 1)
  8603. (if (org-at-heading-p)
  8604. (setq ltoggle (funcall outline-level))
  8605. (error "This should not happen"))
  8606. (while (and (setq level (org-up-heading-safe))
  8607. (or recursive first)
  8608. (>= (point) lim))
  8609. (setq first nil)
  8610. (unless (and level
  8611. (not (string-match
  8612. "\\<checkbox\\>"
  8613. (downcase
  8614. (or (org-entry-get
  8615. nil "COOKIE_DATA")
  8616. "")))))
  8617. (throw 'exit nil))
  8618. (while (re-search-forward box-re (point-at-eol) t)
  8619. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8620. (setq is-percent (match-end 2))
  8621. (save-match-data
  8622. (unless (outline-next-heading) (throw 'exit nil))
  8623. (while (and (looking-at org-complex-heading-regexp)
  8624. (> (setq l1 (length (match-string 1))) level))
  8625. (setq kwd (and (or recursive (= l1 ltoggle))
  8626. (match-string 2)))
  8627. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8628. (and (listp org-provide-todo-statistics)
  8629. (or (member kwd org-provide-todo-statistics)
  8630. (member kwd org-done-keywords))))
  8631. (setq cnt-all (1+ cnt-all))
  8632. (if (eq org-provide-todo-statistics t)
  8633. (and kwd (setq cnt-all (1+ cnt-all)))))
  8634. (and (member kwd org-done-keywords)
  8635. (setq cnt-done (1+ cnt-done)))
  8636. (outline-next-heading)))
  8637. (replace-match
  8638. (if is-percent
  8639. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8640. (format "[%d/%d]" cnt-done cnt-all)))))
  8641. (when cookie-present
  8642. (run-hook-with-args 'org-after-todo-statistics-hook
  8643. cnt-done (- cnt-all cnt-done)))))
  8644. (run-hooks 'org-todo-statistics-hook)))
  8645. (defvar org-after-todo-statistics-hook nil
  8646. "Hook that is called after a TODO statistics cookie has been updated.
  8647. Each function is called with two arguments: the number of not-done entries
  8648. and the number of done entries.
  8649. For example, the following function, when added to this hook, will switch
  8650. an entry to DONE when all children are done, and back to TODO when new
  8651. entries are set to a TODO status. Note that this hook is only called
  8652. when there is a statistics cookie in the headline!
  8653. (defun org-summary-todo (n-done n-not-done)
  8654. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8655. (let (org-log-done org-log-states) ; turn off logging
  8656. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8657. ")
  8658. (defvar org-todo-statistics-hook nil
  8659. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8660. This hook runs even if there is no statisics cookie present, in which case
  8661. `org-after-todo-statistics-hook' would not run.")
  8662. (defun org-todo-trigger-tag-changes (state)
  8663. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8664. (let ((l org-todo-state-tags-triggers)
  8665. changes)
  8666. (when (or (not state) (equal state ""))
  8667. (setq changes (append changes (cdr (assoc "" l)))))
  8668. (when (and (stringp state) (> (length state) 0))
  8669. (setq changes (append changes (cdr (assoc state l)))))
  8670. (when (member state org-not-done-keywords)
  8671. (setq changes (append changes (cdr (assoc 'todo l)))))
  8672. (when (member state org-done-keywords)
  8673. (setq changes (append changes (cdr (assoc 'done l)))))
  8674. (dolist (c changes)
  8675. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8676. (defun org-local-logging (value)
  8677. "Get logging settings from a property VALUE."
  8678. (let* (words w a)
  8679. ;; directly set the variables, they are already local.
  8680. (setq org-log-done nil
  8681. org-log-repeat nil
  8682. org-todo-log-states nil)
  8683. (setq words (org-split-string value))
  8684. (while (setq w (pop words))
  8685. (cond
  8686. ((setq a (assoc w org-startup-options))
  8687. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8688. (set (nth 1 a) (nth 2 a))))
  8689. ((setq a (org-extract-log-state-settings w))
  8690. (and (member (car a) org-todo-keywords-1)
  8691. (push a org-todo-log-states)))))))
  8692. (defun org-get-todo-sequence-head (kwd)
  8693. "Return the head of the TODO sequence to which KWD belongs.
  8694. If KWD is not set, check if there is a text property remembering the
  8695. right sequence."
  8696. (let (p)
  8697. (cond
  8698. ((not kwd)
  8699. (or (get-text-property (point-at-bol) 'org-todo-head)
  8700. (progn
  8701. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8702. nil (point-at-eol)))
  8703. (get-text-property p 'org-todo-head))))
  8704. ((not (member kwd org-todo-keywords-1))
  8705. (car org-todo-keywords-1))
  8706. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8707. (defun org-fast-todo-selection ()
  8708. "Fast TODO keyword selection with single keys.
  8709. Returns the new TODO keyword, or nil if no state change should occur."
  8710. (let* ((fulltable org-todo-key-alist)
  8711. (done-keywords org-done-keywords) ;; needed for the faces.
  8712. (maxlen (apply 'max (mapcar
  8713. (lambda (x)
  8714. (if (stringp (car x)) (string-width (car x)) 0))
  8715. fulltable)))
  8716. (expert nil)
  8717. (fwidth (+ maxlen 3 1 3))
  8718. (ncol (/ (- (window-width) 4) fwidth))
  8719. tg cnt e c tbl
  8720. groups ingroup)
  8721. (save-excursion
  8722. (save-window-excursion
  8723. (if expert
  8724. (set-buffer (get-buffer-create " *Org todo*"))
  8725. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8726. (erase-buffer)
  8727. (org-set-local 'org-done-keywords done-keywords)
  8728. (setq tbl fulltable cnt 0)
  8729. (while (setq e (pop tbl))
  8730. (cond
  8731. ((equal e '(:startgroup))
  8732. (push '() groups) (setq ingroup t)
  8733. (when (not (= cnt 0))
  8734. (setq cnt 0)
  8735. (insert "\n"))
  8736. (insert "{ "))
  8737. ((equal e '(:endgroup))
  8738. (setq ingroup nil cnt 0)
  8739. (insert "}\n"))
  8740. ((equal e '(:newline))
  8741. (when (not (= cnt 0))
  8742. (setq cnt 0)
  8743. (insert "\n")
  8744. (setq e (car tbl))
  8745. (while (equal (car tbl) '(:newline))
  8746. (insert "\n")
  8747. (setq tbl (cdr tbl)))))
  8748. (t
  8749. (setq tg (car e) c (cdr e))
  8750. (if ingroup (push tg (car groups)))
  8751. (setq tg (org-add-props tg nil 'face
  8752. (org-get-todo-face tg)))
  8753. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8754. (insert "[" c "] " tg (make-string
  8755. (- fwidth 4 (length tg)) ?\ ))
  8756. (when (= (setq cnt (1+ cnt)) ncol)
  8757. (insert "\n")
  8758. (if ingroup (insert " "))
  8759. (setq cnt 0)))))
  8760. (insert "\n")
  8761. (goto-char (point-min))
  8762. (if (not expert) (org-fit-window-to-buffer))
  8763. (message "[a-z..]:Set [SPC]:clear")
  8764. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8765. (cond
  8766. ((or (= c ?\C-g)
  8767. (and (= c ?q) (not (rassoc c fulltable))))
  8768. (setq quit-flag t))
  8769. ((= c ?\ ) nil)
  8770. ((setq e (rassoc c fulltable) tg (car e))
  8771. tg)
  8772. (t (setq quit-flag t)))))))
  8773. (defun org-entry-is-todo-p ()
  8774. (member (org-get-todo-state) org-not-done-keywords))
  8775. (defun org-entry-is-done-p ()
  8776. (member (org-get-todo-state) org-done-keywords))
  8777. (defun org-get-todo-state ()
  8778. (save-excursion
  8779. (org-back-to-heading t)
  8780. (and (looking-at org-todo-line-regexp)
  8781. (match-end 2)
  8782. (match-string 2))))
  8783. (defun org-at-date-range-p (&optional inactive-ok)
  8784. "Is the cursor inside a date range?"
  8785. (interactive)
  8786. (save-excursion
  8787. (catch 'exit
  8788. (let ((pos (point)))
  8789. (skip-chars-backward "^[<\r\n")
  8790. (skip-chars-backward "<[")
  8791. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8792. (>= (match-end 0) pos)
  8793. (throw 'exit t))
  8794. (skip-chars-backward "^<[\r\n")
  8795. (skip-chars-backward "<[")
  8796. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8797. (>= (match-end 0) pos)
  8798. (throw 'exit t)))
  8799. nil)))
  8800. (defun org-get-repeat ()
  8801. "Check if there is a deadline/schedule with repeater in this entry."
  8802. (save-match-data
  8803. (save-excursion
  8804. (org-back-to-heading t)
  8805. (if (re-search-forward
  8806. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8807. (match-string 1)))))
  8808. (defvar org-last-changed-timestamp)
  8809. (defvar org-last-inserted-timestamp)
  8810. (defvar org-log-post-message)
  8811. (defvar org-log-note-purpose)
  8812. (defvar org-log-note-how)
  8813. (defvar org-log-note-extra)
  8814. (defun org-auto-repeat-maybe (done-word)
  8815. "Check if the current headline contains a repeated deadline/schedule.
  8816. If yes, set TODO state back to what it was and change the base date
  8817. of repeating deadline/scheduled time stamps to new date.
  8818. This function is run automatically after each state change to a DONE state."
  8819. ;; last-state is dynamically scoped into this function
  8820. (let* ((repeat (org-get-repeat))
  8821. (aa (assoc last-state org-todo-kwd-alist))
  8822. (interpret (nth 1 aa))
  8823. (head (nth 2 aa))
  8824. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8825. (msg "Entry repeats: ")
  8826. (org-log-done nil)
  8827. (org-todo-log-states nil)
  8828. (nshiftmax 10) (nshift 0)
  8829. re type n what ts time)
  8830. (when repeat
  8831. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8832. (org-todo (if (eq interpret 'type) last-state head))
  8833. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8834. (org-time-stamp-format t t)))
  8835. (when org-log-repeat
  8836. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8837. (memq 'org-add-log-note post-command-hook))
  8838. ;; OK, we are already setup for some record
  8839. (if (eq org-log-repeat 'note)
  8840. ;; make sure we take a note, not only a time stamp
  8841. (setq org-log-note-how 'note))
  8842. ;; Set up for taking a record
  8843. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8844. last-state
  8845. 'findpos org-log-repeat)))
  8846. (org-back-to-heading t)
  8847. (org-add-planning-info nil nil 'closed)
  8848. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8849. org-deadline-time-regexp "\\)\\|\\("
  8850. org-ts-regexp "\\)"))
  8851. (while (re-search-forward
  8852. re (save-excursion (outline-next-heading) (point)) t)
  8853. (setq type (if (match-end 1) org-scheduled-string
  8854. (if (match-end 3) org-deadline-string "Plain:"))
  8855. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8856. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8857. (setq n (string-to-number (match-string 2 ts))
  8858. what (match-string 3 ts))
  8859. (if (equal what "w") (setq n (* n 7) what "d"))
  8860. ;; Preparation, see if we need to modify the start date for the change
  8861. (when (match-end 1)
  8862. (setq time (save-match-data (org-time-string-to-time ts)))
  8863. (cond
  8864. ((equal (match-string 1 ts) ".")
  8865. ;; Shift starting date to today
  8866. (org-timestamp-change
  8867. (- (time-to-days (current-time)) (time-to-days time))
  8868. 'day))
  8869. ((equal (match-string 1 ts) "+")
  8870. (while (or (= nshift 0)
  8871. (<= (time-to-days time) (time-to-days (current-time))))
  8872. (when (= (incf nshift) nshiftmax)
  8873. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8874. (error "Abort")))
  8875. (org-timestamp-change n (cdr (assoc what whata)))
  8876. (org-at-timestamp-p t)
  8877. (setq ts (match-string 1))
  8878. (setq time (save-match-data (org-time-string-to-time ts))))
  8879. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8880. ;; rematch, so that we have everything in place for the real shift
  8881. (org-at-timestamp-p t)
  8882. (setq ts (match-string 1))
  8883. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8884. (org-timestamp-change n (cdr (assoc what whata)))
  8885. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8886. (setq org-log-post-message msg)
  8887. (message "%s" msg))))
  8888. (defun org-show-todo-tree (arg)
  8889. "Make a compact tree which shows all headlines marked with TODO.
  8890. The tree will show the lines where the regexp matches, and all higher
  8891. headlines above the match.
  8892. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8893. With a numeric prefix N, construct a sparse tree for the Nth element
  8894. of `org-todo-keywords-1'."
  8895. (interactive "P")
  8896. (let ((case-fold-search nil)
  8897. (kwd-re
  8898. (cond ((null arg) org-not-done-regexp)
  8899. ((equal arg '(4))
  8900. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8901. (mapcar 'list org-todo-keywords-1))))
  8902. (concat "\\("
  8903. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8904. "\\)\\>")))
  8905. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8906. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8907. org-todo-keywords-1)))
  8908. (t (error "Invalid prefix argument: %s" arg)))))
  8909. (message "%d TODO entries found"
  8910. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8911. (defun org-deadline (&optional remove time)
  8912. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8913. With argument REMOVE, remove any deadline from the item.
  8914. When TIME is set, it should be an internal time specification, and the
  8915. scheduling will use the corresponding date."
  8916. (interactive "P")
  8917. (if remove
  8918. (progn
  8919. (org-remove-timestamp-with-keyword org-deadline-string)
  8920. (message "Item no longer has a deadline."))
  8921. (if (org-get-repeat)
  8922. (error "Cannot change deadline on task with repeater, please do that by hand")
  8923. (org-add-planning-info 'deadline time 'closed)
  8924. (message "Deadline on %s" org-last-inserted-timestamp))))
  8925. (defun org-schedule (&optional remove time)
  8926. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8927. With argument REMOVE, remove any scheduling date from the item.
  8928. When TIME is set, it should be an internal time specification, and the
  8929. scheduling will use the corresponding date."
  8930. (interactive "P")
  8931. (if remove
  8932. (progn
  8933. (org-remove-timestamp-with-keyword org-scheduled-string)
  8934. (message "Item is no longer scheduled."))
  8935. (if (org-get-repeat)
  8936. (error "Cannot reschedule task with repeater, please do that by hand")
  8937. (org-add-planning-info 'scheduled time 'closed)
  8938. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8939. (defun org-get-scheduled-time (pom &optional inherit)
  8940. "Get the scheduled time as a time tuple, of a format suitable
  8941. for calling org-schedule with, or if there is no scheduling,
  8942. returns nil."
  8943. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8944. (when time
  8945. (apply 'encode-time (org-parse-time-string time)))))
  8946. (defun org-get-deadline-time (pom &optional inherit)
  8947. "Get the deadine as a time tuple, of a format suitable for
  8948. calling org-deadlin with, or if there is no scheduling, returns
  8949. nil."
  8950. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8951. (when time
  8952. (apply 'encode-time (org-parse-time-string time)))))
  8953. (defun org-remove-timestamp-with-keyword (keyword)
  8954. "Remove all time stamps with KEYWORD in the current entry."
  8955. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8956. beg)
  8957. (save-excursion
  8958. (org-back-to-heading t)
  8959. (setq beg (point))
  8960. (org-end-of-subtree t t)
  8961. (while (re-search-backward re beg t)
  8962. (replace-match "")
  8963. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8964. (equal (char-before) ?\ ))
  8965. (backward-delete-char 1)
  8966. (if (string-match "^[ \t]*$" (buffer-substring
  8967. (point-at-bol) (point-at-eol)))
  8968. (delete-region (point-at-bol)
  8969. (min (point-max) (1+ (point-at-eol))))))))))
  8970. (defun org-add-planning-info (what &optional time &rest remove)
  8971. "Insert new timestamp with keyword in the line directly after the headline.
  8972. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8973. If non is given, the user is prompted for a date.
  8974. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8975. be removed."
  8976. (interactive)
  8977. (let (org-time-was-given org-end-time-was-given ts
  8978. end default-time default-input)
  8979. (catch 'exit
  8980. (when (and (not time) (memq what '(scheduled deadline)))
  8981. ;; Try to get a default date/time from existing timestamp
  8982. (save-excursion
  8983. (org-back-to-heading t)
  8984. (setq end (save-excursion (outline-next-heading) (point)))
  8985. (when (re-search-forward (if (eq what 'scheduled)
  8986. org-scheduled-time-regexp
  8987. org-deadline-time-regexp)
  8988. end t)
  8989. (setq ts (match-string 1)
  8990. default-time
  8991. (apply 'encode-time (org-parse-time-string ts))
  8992. default-input (and ts (org-get-compact-tod ts))))))
  8993. (when what
  8994. ;; If necessary, get the time from the user
  8995. (setq time (or time (org-read-date nil 'to-time nil nil
  8996. default-time default-input))))
  8997. (when (and org-insert-labeled-timestamps-at-point
  8998. (member what '(scheduled deadline)))
  8999. (insert
  9000. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9001. (org-insert-time-stamp time org-time-was-given
  9002. nil nil nil (list org-end-time-was-given))
  9003. (setq what nil))
  9004. (save-excursion
  9005. (save-restriction
  9006. (let (col list elt ts buffer-invisibility-spec)
  9007. (org-back-to-heading t)
  9008. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9009. (goto-char (match-end 1))
  9010. (setq col (current-column))
  9011. (goto-char (match-end 0))
  9012. (if (eobp) (insert "\n") (forward-char 1))
  9013. (when (and (not what)
  9014. (not (looking-at
  9015. (concat "[ \t]*"
  9016. org-keyword-time-not-clock-regexp))))
  9017. ;; Nothing to add, nothing to remove...... :-)
  9018. (throw 'exit nil))
  9019. (if (and (not (looking-at outline-regexp))
  9020. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9021. "[^\r\n]*"))
  9022. (not (equal (match-string 1) org-clock-string)))
  9023. (narrow-to-region (match-beginning 0) (match-end 0))
  9024. (insert-before-markers "\n")
  9025. (backward-char 1)
  9026. (narrow-to-region (point) (point))
  9027. (and org-adapt-indentation (org-indent-to-column col)))
  9028. ;; Check if we have to remove something.
  9029. (setq list (cons what remove))
  9030. (while list
  9031. (setq elt (pop list))
  9032. (goto-char (point-min))
  9033. (when (or (and (eq elt 'scheduled)
  9034. (re-search-forward org-scheduled-time-regexp nil t))
  9035. (and (eq elt 'deadline)
  9036. (re-search-forward org-deadline-time-regexp nil t))
  9037. (and (eq elt 'closed)
  9038. (re-search-forward org-closed-time-regexp nil t)))
  9039. (replace-match "")
  9040. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9041. (if (looking-at " +") (replace-match ""))))
  9042. (goto-char (point-max))
  9043. (when what
  9044. (insert
  9045. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9046. (cond ((eq what 'scheduled) org-scheduled-string)
  9047. ((eq what 'deadline) org-deadline-string)
  9048. ((eq what 'closed) org-closed-string))
  9049. " ")
  9050. (setq ts (org-insert-time-stamp
  9051. time
  9052. (or org-time-was-given
  9053. (and (eq what 'closed) org-log-done-with-time))
  9054. (eq what 'closed)
  9055. nil nil (list org-end-time-was-given)))
  9056. (end-of-line 1))
  9057. (goto-char (point-min))
  9058. (widen)
  9059. (if (and (looking-at "[ \t]+\n")
  9060. (equal (char-before) ?\n))
  9061. (delete-region (1- (point)) (point-at-eol)))
  9062. ts))))))
  9063. (defvar org-log-note-marker (make-marker))
  9064. (defvar org-log-note-purpose nil)
  9065. (defvar org-log-note-state nil)
  9066. (defvar org-log-note-previous-state nil)
  9067. (defvar org-log-note-how nil)
  9068. (defvar org-log-note-extra nil)
  9069. (defvar org-log-note-window-configuration nil)
  9070. (defvar org-log-note-return-to (make-marker))
  9071. (defvar org-log-post-message nil
  9072. "Message to be displayed after a log note has been stored.
  9073. The auto-repeater uses this.")
  9074. (defun org-add-note ()
  9075. "Add a note to the current entry.
  9076. This is done in the same way as adding a state change note."
  9077. (interactive)
  9078. (org-add-log-setup 'note nil nil 'findpos nil))
  9079. (defvar org-property-end-re)
  9080. (defun org-add-log-setup (&optional purpose state prev-state
  9081. findpos how &optional extra)
  9082. "Set up the post command hook to take a note.
  9083. If this is about to TODO state change, the new state is expected in STATE.
  9084. When FINDPOS is non-nil, find the correct position for the note in
  9085. the current entry. If not, assume that it can be inserted at point.
  9086. HOW is an indicator what kind of note should be created.
  9087. EXTRA is additional text that will be inserted into the notes buffer."
  9088. (let* ((org-log-into-drawer (org-log-into-drawer))
  9089. (drawer (cond ((stringp org-log-into-drawer)
  9090. org-log-into-drawer)
  9091. (org-log-into-drawer "LOGBOOK")
  9092. (t nil))))
  9093. (save-restriction
  9094. (save-excursion
  9095. (when findpos
  9096. (org-back-to-heading t)
  9097. (narrow-to-region (point) (save-excursion
  9098. (outline-next-heading) (point)))
  9099. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9100. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9101. "[^\r\n]*\\)?"))
  9102. (goto-char (match-end 0))
  9103. (cond
  9104. (drawer
  9105. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9106. nil t)
  9107. (progn
  9108. (goto-char (match-end 0))
  9109. (or org-log-states-order-reversed
  9110. (and (re-search-forward org-property-end-re nil t)
  9111. (goto-char (1- (match-beginning 0))))))
  9112. (insert "\n:" drawer ":\n:END:")
  9113. (beginning-of-line 0)
  9114. (org-indent-line-function)
  9115. (beginning-of-line 2)
  9116. (org-indent-line-function)
  9117. (end-of-line 0)))
  9118. ((and org-log-state-notes-insert-after-drawers
  9119. (save-excursion
  9120. (forward-line) (looking-at org-drawer-regexp)))
  9121. (forward-line)
  9122. (while (looking-at org-drawer-regexp)
  9123. (goto-char (match-end 0))
  9124. (re-search-forward org-property-end-re (point-max) t)
  9125. (forward-line))
  9126. (forward-line -1)))
  9127. (unless org-log-states-order-reversed
  9128. (and (= (char-after) ?\n) (forward-char 1))
  9129. (org-skip-over-state-notes)
  9130. (skip-chars-backward " \t\n\r")))
  9131. (move-marker org-log-note-marker (point))
  9132. (setq org-log-note-purpose purpose
  9133. org-log-note-state state
  9134. org-log-note-previous-state prev-state
  9135. org-log-note-how how
  9136. org-log-note-extra extra)
  9137. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9138. (defun org-skip-over-state-notes ()
  9139. "Skip past the list of State notes in an entry."
  9140. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9141. (while (looking-at "[ \t]*- State")
  9142. (condition-case nil
  9143. (org-next-item)
  9144. (error (org-end-of-item)))))
  9145. (defun org-add-log-note (&optional purpose)
  9146. "Pop up a window for taking a note, and add this note later at point."
  9147. (remove-hook 'post-command-hook 'org-add-log-note)
  9148. (setq org-log-note-window-configuration (current-window-configuration))
  9149. (delete-other-windows)
  9150. (move-marker org-log-note-return-to (point))
  9151. (switch-to-buffer (marker-buffer org-log-note-marker))
  9152. (goto-char org-log-note-marker)
  9153. (org-switch-to-buffer-other-window "*Org Note*")
  9154. (erase-buffer)
  9155. (if (memq org-log-note-how '(time state))
  9156. (let (current-prefix-arg) (org-store-log-note))
  9157. (let ((org-inhibit-startup t)) (org-mode))
  9158. (insert (format "# Insert note for %s.
  9159. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9160. (cond
  9161. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9162. ((eq org-log-note-purpose 'done) "closed todo item")
  9163. ((eq org-log-note-purpose 'state)
  9164. (format "state change from \"%s\" to \"%s\""
  9165. (or org-log-note-previous-state "")
  9166. (or org-log-note-state "")))
  9167. ((eq org-log-note-purpose 'note)
  9168. "this entry")
  9169. (t (error "This should not happen")))))
  9170. (if org-log-note-extra (insert org-log-note-extra))
  9171. (org-set-local 'org-finish-function 'org-store-log-note)))
  9172. (defvar org-note-abort nil) ; dynamically scoped
  9173. (defun org-store-log-note ()
  9174. "Finish taking a log note, and insert it to where it belongs."
  9175. (let ((txt (buffer-string))
  9176. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9177. lines ind)
  9178. (kill-buffer (current-buffer))
  9179. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9180. (setq txt (replace-match "" t t txt)))
  9181. (if (string-match "\\s-+\\'" txt)
  9182. (setq txt (replace-match "" t t txt)))
  9183. (setq lines (org-split-string txt "\n"))
  9184. (when (and note (string-match "\\S-" note))
  9185. (setq note
  9186. (org-replace-escapes
  9187. note
  9188. (list (cons "%u" (user-login-name))
  9189. (cons "%U" user-full-name)
  9190. (cons "%t" (format-time-string
  9191. (org-time-stamp-format 'long 'inactive)
  9192. (current-time)))
  9193. (cons "%s" (if org-log-note-state
  9194. (concat "\"" org-log-note-state "\"")
  9195. ""))
  9196. (cons "%S" (if org-log-note-previous-state
  9197. (concat "\"" org-log-note-previous-state "\"")
  9198. "\"\"")))))
  9199. (if lines (setq note (concat note " \\\\")))
  9200. (push note lines))
  9201. (when (or current-prefix-arg org-note-abort)
  9202. (when org-log-into-drawer
  9203. (org-remove-empty-drawer-at
  9204. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9205. org-log-note-marker))
  9206. (setq lines nil))
  9207. (when lines
  9208. (save-excursion
  9209. (set-buffer (marker-buffer org-log-note-marker))
  9210. (save-excursion
  9211. (goto-char org-log-note-marker)
  9212. (move-marker org-log-note-marker nil)
  9213. (end-of-line 1)
  9214. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9215. (insert "- " (pop lines))
  9216. (org-indent-line-function)
  9217. (beginning-of-line 1)
  9218. (looking-at "[ \t]*")
  9219. (setq ind (concat (match-string 0) " "))
  9220. (end-of-line 1)
  9221. (while lines (insert "\n" ind (pop lines)))
  9222. (message "Note stored")
  9223. (org-back-to-heading t)
  9224. (org-cycle-hide-drawers 'children)))))
  9225. (set-window-configuration org-log-note-window-configuration)
  9226. (with-current-buffer (marker-buffer org-log-note-return-to)
  9227. (goto-char org-log-note-return-to))
  9228. (move-marker org-log-note-return-to nil)
  9229. (and org-log-post-message (message "%s" org-log-post-message)))
  9230. (defun org-remove-empty-drawer-at (drawer pos)
  9231. "Remove an emptyr DARWER drawer at position POS.
  9232. POS may also be a marker."
  9233. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9234. (save-excursion
  9235. (save-restriction
  9236. (widen)
  9237. (goto-char pos)
  9238. (if (org-in-regexp
  9239. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9240. (replace-match ""))))))
  9241. (defun org-sparse-tree (&optional arg)
  9242. "Create a sparse tree, prompt for the details.
  9243. This command can create sparse trees. You first need to select the type
  9244. of match used to create the tree:
  9245. t Show entries with a specific TODO keyword.
  9246. m Show entries selected by a tags/property match.
  9247. p Enter a property name and its value (both with completion on existing
  9248. names/values) and show entries with that property.
  9249. r Show entries matching a regular expression.
  9250. d Show deadlines due within `org-deadline-warning-days'.
  9251. b Show deadlines and scheduled items before a date.
  9252. a Show deadlines and scheduled items after a date."
  9253. (interactive "P")
  9254. (let (ans kwd value)
  9255. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9256. (setq ans (read-char-exclusive))
  9257. (cond
  9258. ((equal ans ?d)
  9259. (call-interactively 'org-check-deadlines))
  9260. ((equal ans ?b)
  9261. (call-interactively 'org-check-before-date))
  9262. ((equal ans ?a)
  9263. (call-interactively 'org-check-after-date))
  9264. ((equal ans ?t)
  9265. (org-show-todo-tree '(4)))
  9266. ((member ans '(?T ?m))
  9267. (call-interactively 'org-match-sparse-tree))
  9268. ((member ans '(?p ?P))
  9269. (setq kwd (org-ido-completing-read "Property: "
  9270. (mapcar 'list (org-buffer-property-keys))))
  9271. (setq value (org-ido-completing-read "Value: "
  9272. (mapcar 'list (org-property-values kwd))))
  9273. (unless (string-match "\\`{.*}\\'" value)
  9274. (setq value (concat "\"" value "\"")))
  9275. (org-match-sparse-tree arg (concat kwd "=" value)))
  9276. ((member ans '(?r ?R ?/))
  9277. (call-interactively 'org-occur))
  9278. (t (error "No such sparse tree command \"%c\"" ans)))))
  9279. (defvar org-occur-highlights nil
  9280. "List of overlays used for occur matches.")
  9281. (make-variable-buffer-local 'org-occur-highlights)
  9282. (defvar org-occur-parameters nil
  9283. "Parameters of the active org-occur calls.
  9284. This is a list, each call to org-occur pushes as cons cell,
  9285. containing the regular expression and the callback, onto the list.
  9286. The list can contain several entries if `org-occur' has been called
  9287. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9288. will only contain one set of parameters. When the highlights are
  9289. removed (for example with `C-c C-c', or with the next edit (depending
  9290. on `org-remove-highlights-with-change'), this variable is emptied
  9291. as well.")
  9292. (make-variable-buffer-local 'org-occur-parameters)
  9293. (defun org-occur (regexp &optional keep-previous callback)
  9294. "Make a compact tree which shows all matches of REGEXP.
  9295. The tree will show the lines where the regexp matches, and all higher
  9296. headlines above the match. It will also show the heading after the match,
  9297. to make sure editing the matching entry is easy.
  9298. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9299. call to `org-occur' will be kept, to allow stacking of calls to this
  9300. command.
  9301. If CALLBACK is non-nil, it is a function which is called to confirm
  9302. that the match should indeed be shown."
  9303. (interactive "sRegexp: \nP")
  9304. (when (equal regexp "")
  9305. (error "Regexp cannot be empty"))
  9306. (unless keep-previous
  9307. (org-remove-occur-highlights nil nil t))
  9308. (push (cons regexp callback) org-occur-parameters)
  9309. (let ((cnt 0))
  9310. (save-excursion
  9311. (goto-char (point-min))
  9312. (if (or (not keep-previous) ; do not want to keep
  9313. (not org-occur-highlights)) ; no previous matches
  9314. ;; hide everything
  9315. (org-overview))
  9316. (while (re-search-forward regexp nil t)
  9317. (when (or (not callback)
  9318. (save-match-data (funcall callback)))
  9319. (setq cnt (1+ cnt))
  9320. (when org-highlight-sparse-tree-matches
  9321. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9322. (org-show-context 'occur-tree))))
  9323. (when org-remove-highlights-with-change
  9324. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9325. nil 'local))
  9326. (unless org-sparse-tree-open-archived-trees
  9327. (org-hide-archived-subtrees (point-min) (point-max)))
  9328. (run-hooks 'org-occur-hook)
  9329. (if (interactive-p)
  9330. (message "%d match(es) for regexp %s" cnt regexp))
  9331. cnt))
  9332. (defun org-show-context (&optional key)
  9333. "Make sure point and context and visible.
  9334. How much context is shown depends upon the variables
  9335. `org-show-hierarchy-above', `org-show-following-heading'. and
  9336. `org-show-siblings'."
  9337. (let ((heading-p (org-on-heading-p t))
  9338. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9339. (following-p (org-get-alist-option org-show-following-heading key))
  9340. (entry-p (org-get-alist-option org-show-entry-below key))
  9341. (siblings-p (org-get-alist-option org-show-siblings key)))
  9342. (catch 'exit
  9343. ;; Show heading or entry text
  9344. (if (and heading-p (not entry-p))
  9345. (org-flag-heading nil) ; only show the heading
  9346. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9347. (org-show-hidden-entry))) ; show entire entry
  9348. (when following-p
  9349. ;; Show next sibling, or heading below text
  9350. (save-excursion
  9351. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9352. (org-flag-heading nil))))
  9353. (when siblings-p (org-show-siblings))
  9354. (when hierarchy-p
  9355. ;; show all higher headings, possibly with siblings
  9356. (save-excursion
  9357. (while (and (condition-case nil
  9358. (progn (org-up-heading-all 1) t)
  9359. (error nil))
  9360. (not (bobp)))
  9361. (org-flag-heading nil)
  9362. (when siblings-p (org-show-siblings))))))))
  9363. (defun org-reveal (&optional siblings)
  9364. "Show current entry, hierarchy above it, and the following headline.
  9365. This can be used to show a consistent set of context around locations
  9366. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9367. not t for the search context.
  9368. With optional argument SIBLINGS, on each level of the hierarchy all
  9369. siblings are shown. This repairs the tree structure to what it would
  9370. look like when opened with hierarchical calls to `org-cycle'."
  9371. (interactive "P")
  9372. (let ((org-show-hierarchy-above t)
  9373. (org-show-following-heading t)
  9374. (org-show-siblings (if siblings t org-show-siblings)))
  9375. (org-show-context nil)))
  9376. (defun org-highlight-new-match (beg end)
  9377. "Highlight from BEG to END and mark the highlight is an occur headline."
  9378. (let ((ov (org-make-overlay beg end)))
  9379. (org-overlay-put ov 'face 'secondary-selection)
  9380. (push ov org-occur-highlights)))
  9381. (defun org-remove-occur-highlights (&optional beg end noremove)
  9382. "Remove the occur highlights from the buffer.
  9383. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9384. from the `before-change-functions' in the current buffer."
  9385. (interactive)
  9386. (unless org-inhibit-highlight-removal
  9387. (mapc 'org-delete-overlay org-occur-highlights)
  9388. (setq org-occur-highlights nil)
  9389. (setq org-occur-parameters nil)
  9390. (unless noremove
  9391. (remove-hook 'before-change-functions
  9392. 'org-remove-occur-highlights 'local))))
  9393. ;;;; Priorities
  9394. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9395. "Regular expression matching the priority indicator.")
  9396. (defvar org-remove-priority-next-time nil)
  9397. (defun org-priority-up ()
  9398. "Increase the priority of the current item."
  9399. (interactive)
  9400. (org-priority 'up))
  9401. (defun org-priority-down ()
  9402. "Decrease the priority of the current item."
  9403. (interactive)
  9404. (org-priority 'down))
  9405. (defun org-priority (&optional action)
  9406. "Change the priority of an item by ARG.
  9407. ACTION can be `set', `up', `down', or a character."
  9408. (interactive)
  9409. (unless org-enable-priority-commands
  9410. (error "Priority commands are disabled"))
  9411. (setq action (or action 'set))
  9412. (let (current new news have remove)
  9413. (save-excursion
  9414. (org-back-to-heading t)
  9415. (if (looking-at org-priority-regexp)
  9416. (setq current (string-to-char (match-string 2))
  9417. have t)
  9418. (setq current org-default-priority))
  9419. (cond
  9420. ((or (eq action 'set)
  9421. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9422. (if (not (eq action 'set))
  9423. (setq new action)
  9424. (message "Priority %c-%c, SPC to remove: "
  9425. org-highest-priority org-lowest-priority)
  9426. (setq new (read-char-exclusive)))
  9427. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9428. (= (upcase org-lowest-priority) org-lowest-priority))
  9429. (setq new (upcase new)))
  9430. (cond ((equal new ?\ ) (setq remove t))
  9431. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9432. (error "Priority must be between `%c' and `%c'"
  9433. org-highest-priority org-lowest-priority))))
  9434. ((eq action 'up)
  9435. (if (and (not have) (eq last-command this-command))
  9436. (setq new org-lowest-priority)
  9437. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9438. org-default-priority (1- current)))))
  9439. ((eq action 'down)
  9440. (if (and (not have) (eq last-command this-command))
  9441. (setq new org-highest-priority)
  9442. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9443. org-default-priority (1+ current)))))
  9444. (t (error "Invalid action")))
  9445. (if (or (< (upcase new) org-highest-priority)
  9446. (> (upcase new) org-lowest-priority))
  9447. (setq remove t))
  9448. (setq news (format "%c" new))
  9449. (if have
  9450. (if remove
  9451. (replace-match "" t t nil 1)
  9452. (replace-match news t t nil 2))
  9453. (if remove
  9454. (error "No priority cookie found in line")
  9455. (let ((case-fold-search nil))
  9456. (looking-at org-todo-line-regexp))
  9457. (if (match-end 2)
  9458. (progn
  9459. (goto-char (match-end 2))
  9460. (insert " [#" news "]"))
  9461. (goto-char (match-beginning 3))
  9462. (insert "[#" news "] "))))
  9463. (org-preserve-lc (org-set-tags nil 'align)))
  9464. (if remove
  9465. (message "Priority removed")
  9466. (message "Priority of current item set to %s" news))))
  9467. (defun org-get-priority (s)
  9468. "Find priority cookie and return priority."
  9469. (save-match-data
  9470. (if (not (string-match org-priority-regexp s))
  9471. (* 1000 (- org-lowest-priority org-default-priority))
  9472. (* 1000 (- org-lowest-priority
  9473. (string-to-char (match-string 2 s)))))))
  9474. ;;;; Tags
  9475. (defvar org-agenda-archives-mode)
  9476. (defvar org-map-continue-from nil
  9477. "Position from where mapping should continue.
  9478. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9479. (defvar org-scanner-tags nil
  9480. "The current tag list while the tags scanner is running.")
  9481. (defvar org-trust-scanner-tags nil
  9482. "Should `org-get-tags-at' use the tags fro the scanner.
  9483. This is for internal dynamical scoping only.
  9484. When this is non-nil, the function `org-get-tags-at' will return the value
  9485. of `org-scanner-tags' instead of building the list by itself. This
  9486. can lead to large speed-ups when the tags scanner is used in a file with
  9487. many entries, and when the list of tags is retrieved, for example to
  9488. obtain a list of properties. Building the tags list for each entry in such
  9489. a file becomes an N^2 operation - but with this variable set, it scales
  9490. as N.")
  9491. (defun org-scan-tags (action matcher &optional todo-only)
  9492. "Scan headline tags with inheritance and produce output ACTION.
  9493. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9494. or `agenda' to produce an entry list for an agenda view. It can also be
  9495. a Lisp form or a function that should be called at each matched headline, in
  9496. this case the return value is a list of all return values from these calls.
  9497. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9498. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9499. only lines with a TODO keyword are included in the output."
  9500. (require 'org-agenda)
  9501. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9502. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9503. (org-re
  9504. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9505. (props (list 'face 'default
  9506. 'done-face 'org-agenda-done
  9507. 'undone-face 'default
  9508. 'mouse-face 'highlight
  9509. 'org-not-done-regexp org-not-done-regexp
  9510. 'org-todo-regexp org-todo-regexp
  9511. 'keymap org-agenda-keymap
  9512. 'help-echo
  9513. (format "mouse-2 or RET jump to org file %s"
  9514. (abbreviate-file-name
  9515. (or (buffer-file-name (buffer-base-buffer))
  9516. (buffer-name (buffer-base-buffer)))))))
  9517. (case-fold-search nil)
  9518. (org-map-continue-from nil)
  9519. lspos tags tags-list
  9520. (tags-alist (list (cons 0 org-file-tags)))
  9521. (llast 0) rtn rtn1 level category i txt
  9522. todo marker entry priority)
  9523. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9524. (setq action (list 'lambda nil action)))
  9525. (save-excursion
  9526. (goto-char (point-min))
  9527. (when (eq action 'sparse-tree)
  9528. (org-overview)
  9529. (org-remove-occur-highlights))
  9530. (while (re-search-forward re nil t)
  9531. (catch :skip
  9532. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9533. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9534. (goto-char (setq lspos (match-beginning 0)))
  9535. (setq level (org-reduced-level (funcall outline-level))
  9536. category (org-get-category))
  9537. (setq i llast llast level)
  9538. ;; remove tag lists from same and sublevels
  9539. (while (>= i level)
  9540. (when (setq entry (assoc i tags-alist))
  9541. (setq tags-alist (delete entry tags-alist)))
  9542. (setq i (1- i)))
  9543. ;; add the next tags
  9544. (when tags
  9545. (setq tags (org-split-string tags ":")
  9546. tags-alist
  9547. (cons (cons level tags) tags-alist)))
  9548. ;; compile tags for current headline
  9549. (setq tags-list
  9550. (if org-use-tag-inheritance
  9551. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9552. tags)
  9553. org-scanner-tags tags-list)
  9554. (when org-use-tag-inheritance
  9555. (setcdr (car tags-alist)
  9556. (mapcar (lambda (x)
  9557. (setq x (copy-sequence x))
  9558. (org-add-prop-inherited x))
  9559. (cdar tags-alist))))
  9560. (when (and tags org-use-tag-inheritance
  9561. (or (not (eq t org-use-tag-inheritance))
  9562. org-tags-exclude-from-inheritance))
  9563. ;; selective inheritance, remove uninherited ones
  9564. (setcdr (car tags-alist)
  9565. (org-remove-uniherited-tags (cdar tags-alist))))
  9566. (when (and (or (not todo-only)
  9567. (and (member todo org-not-done-keywords)
  9568. (or (not org-agenda-tags-todo-honor-ignore-options)
  9569. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9570. (let ((case-fold-search t)) (eval matcher))
  9571. (or
  9572. (not (member org-archive-tag tags-list))
  9573. ;; we have an archive tag, should we use this anyway?
  9574. (or (not org-agenda-skip-archived-trees)
  9575. (and (eq action 'agenda) org-agenda-archives-mode))))
  9576. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9577. ;; select this headline
  9578. (cond
  9579. ((eq action 'sparse-tree)
  9580. (and org-highlight-sparse-tree-matches
  9581. (org-get-heading) (match-end 0)
  9582. (org-highlight-new-match
  9583. (match-beginning 0) (match-beginning 1)))
  9584. (org-show-context 'tags-tree))
  9585. ((eq action 'agenda)
  9586. (setq txt (org-format-agenda-item
  9587. ""
  9588. (concat
  9589. (if (eq org-tags-match-list-sublevels 'indented)
  9590. (make-string (1- level) ?.) "")
  9591. (org-get-heading))
  9592. category
  9593. tags-list
  9594. )
  9595. priority (org-get-priority txt))
  9596. (goto-char lspos)
  9597. (setq marker (org-agenda-new-marker))
  9598. (org-add-props txt props
  9599. 'org-marker marker 'org-hd-marker marker 'org-category category
  9600. 'todo-state todo
  9601. 'priority priority 'type "tagsmatch")
  9602. (push txt rtn))
  9603. ((functionp action)
  9604. (setq org-map-continue-from nil)
  9605. (save-excursion
  9606. (setq rtn1 (funcall action))
  9607. (push rtn1 rtn)))
  9608. (t (error "Invalid action")))
  9609. ;; if we are to skip sublevels, jump to end of subtree
  9610. (unless org-tags-match-list-sublevels
  9611. (org-end-of-subtree t)
  9612. (backward-char 1))))
  9613. ;; Get the correct position from where to continue
  9614. (if org-map-continue-from
  9615. (goto-char org-map-continue-from)
  9616. (and (= (point) lspos) (end-of-line 1)))))
  9617. (when (and (eq action 'sparse-tree)
  9618. (not org-sparse-tree-open-archived-trees))
  9619. (org-hide-archived-subtrees (point-min) (point-max)))
  9620. (nreverse rtn)))
  9621. (defun org-remove-uniherited-tags (tags)
  9622. "Remove all tags that are not inherited from the list TAGS."
  9623. (cond
  9624. ((eq org-use-tag-inheritance t)
  9625. (if org-tags-exclude-from-inheritance
  9626. (org-delete-all org-tags-exclude-from-inheritance tags)
  9627. tags))
  9628. ((not org-use-tag-inheritance) nil)
  9629. ((stringp org-use-tag-inheritance)
  9630. (delq nil (mapcar
  9631. (lambda (x)
  9632. (if (and (string-match org-use-tag-inheritance x)
  9633. (not (member x org-tags-exclude-from-inheritance)))
  9634. x nil))
  9635. tags)))
  9636. ((listp org-use-tag-inheritance)
  9637. (delq nil (mapcar
  9638. (lambda (x)
  9639. (if (member x org-use-tag-inheritance) x nil))
  9640. tags)))))
  9641. (defvar todo-only) ;; dynamically scoped
  9642. (defun org-match-sparse-tree (&optional todo-only match)
  9643. "Create a sparse tree according to tags string MATCH.
  9644. MATCH can contain positive and negative selection of tags, like
  9645. \"+WORK+URGENT-WITHBOSS\".
  9646. If optional argument TODO-ONLY is non-nil, only select lines that are
  9647. also TODO lines."
  9648. (interactive "P")
  9649. (org-prepare-agenda-buffers (list (current-buffer)))
  9650. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9651. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9652. (defvar org-cached-props nil)
  9653. (defun org-cached-entry-get (pom property)
  9654. (if (or (eq t org-use-property-inheritance)
  9655. (and (stringp org-use-property-inheritance)
  9656. (string-match org-use-property-inheritance property))
  9657. (and (listp org-use-property-inheritance)
  9658. (member property org-use-property-inheritance)))
  9659. ;; Caching is not possible, check it directly
  9660. (org-entry-get pom property 'inherit)
  9661. ;; Get all properties, so that we can do complicated checks easily
  9662. (cdr (assoc property (or org-cached-props
  9663. (setq org-cached-props
  9664. (org-entry-properties pom)))))))
  9665. (defun org-global-tags-completion-table (&optional files)
  9666. "Return the list of all tags in all agenda buffer/files."
  9667. (save-excursion
  9668. (org-uniquify
  9669. (delq nil
  9670. (apply 'append
  9671. (mapcar
  9672. (lambda (file)
  9673. (set-buffer (find-file-noselect file))
  9674. (append (org-get-buffer-tags)
  9675. (mapcar (lambda (x) (if (stringp (car-safe x))
  9676. (list (car-safe x)) nil))
  9677. org-tag-alist)))
  9678. (if (and files (car files))
  9679. files
  9680. (org-agenda-files))))))))
  9681. (defun org-make-tags-matcher (match)
  9682. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9683. ;; todo-only is scoped dynamically into this function, and the function
  9684. ;; may change it if the matcher asks for it.
  9685. (unless match
  9686. ;; Get a new match request, with completion
  9687. (let ((org-last-tags-completion-table
  9688. (org-global-tags-completion-table)))
  9689. (setq match (org-completing-read-no-ido
  9690. "Match: " 'org-tags-completion-function nil nil nil
  9691. 'org-tags-history))))
  9692. ;; Parse the string and create a lisp form
  9693. (let ((match0 match)
  9694. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9695. minus tag mm
  9696. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9697. orterms term orlist re-p str-p level-p level-op time-p
  9698. prop-p pn pv po cat-p gv rest)
  9699. (if (string-match "/+" match)
  9700. ;; match contains also a todo-matching request
  9701. (progn
  9702. (setq tagsmatch (substring match 0 (match-beginning 0))
  9703. todomatch (substring match (match-end 0)))
  9704. (if (string-match "^!" todomatch)
  9705. (setq todo-only t todomatch (substring todomatch 1)))
  9706. (if (string-match "^\\s-*$" todomatch)
  9707. (setq todomatch nil)))
  9708. ;; only matching tags
  9709. (setq tagsmatch match todomatch nil))
  9710. ;; Make the tags matcher
  9711. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9712. (setq tagsmatcher t)
  9713. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9714. (while (setq term (pop orterms))
  9715. (while (and (equal (substring term -1) "\\") orterms)
  9716. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9717. (while (string-match re term)
  9718. (setq rest (substring term (match-end 0))
  9719. minus (and (match-end 1)
  9720. (equal (match-string 1 term) "-"))
  9721. tag (match-string 2 term)
  9722. re-p (equal (string-to-char tag) ?{)
  9723. level-p (match-end 4)
  9724. prop-p (match-end 5)
  9725. mm (cond
  9726. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9727. (level-p
  9728. (setq level-op (org-op-to-function (match-string 3 term)))
  9729. `(,level-op level ,(string-to-number
  9730. (match-string 4 term))))
  9731. (prop-p
  9732. (setq pn (match-string 5 term)
  9733. po (match-string 6 term)
  9734. pv (match-string 7 term)
  9735. cat-p (equal pn "CATEGORY")
  9736. re-p (equal (string-to-char pv) ?{)
  9737. str-p (equal (string-to-char pv) ?\")
  9738. time-p (save-match-data
  9739. (string-match "^\"[[<].*[]>]\"$" pv))
  9740. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9741. (if time-p (setq pv (org-matcher-time pv)))
  9742. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9743. (cond
  9744. ((equal pn "CATEGORY")
  9745. (setq gv '(get-text-property (point) 'org-category)))
  9746. ((equal pn "TODO")
  9747. (setq gv 'todo))
  9748. (t
  9749. (setq gv `(org-cached-entry-get nil ,pn))))
  9750. (if re-p
  9751. (if (eq po 'org<>)
  9752. `(not (string-match ,pv (or ,gv "")))
  9753. `(string-match ,pv (or ,gv "")))
  9754. (if str-p
  9755. `(,po (or ,gv "") ,pv)
  9756. `(,po (string-to-number (or ,gv ""))
  9757. ,(string-to-number pv) ))))
  9758. (t `(member ,tag tags-list)))
  9759. mm (if minus (list 'not mm) mm)
  9760. term rest)
  9761. (push mm tagsmatcher))
  9762. (push (if (> (length tagsmatcher) 1)
  9763. (cons 'and tagsmatcher)
  9764. (car tagsmatcher))
  9765. orlist)
  9766. (setq tagsmatcher nil))
  9767. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9768. (setq tagsmatcher
  9769. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9770. ;; Make the todo matcher
  9771. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9772. (setq todomatcher t)
  9773. (setq orterms (org-split-string todomatch "|") orlist nil)
  9774. (while (setq term (pop orterms))
  9775. (while (string-match re term)
  9776. (setq minus (and (match-end 1)
  9777. (equal (match-string 1 term) "-"))
  9778. kwd (match-string 2 term)
  9779. re-p (equal (string-to-char kwd) ?{)
  9780. term (substring term (match-end 0))
  9781. mm (if re-p
  9782. `(string-match ,(substring kwd 1 -1) todo)
  9783. (list 'equal 'todo kwd))
  9784. mm (if minus (list 'not mm) mm))
  9785. (push mm todomatcher))
  9786. (push (if (> (length todomatcher) 1)
  9787. (cons 'and todomatcher)
  9788. (car todomatcher))
  9789. orlist)
  9790. (setq todomatcher nil))
  9791. (setq todomatcher (if (> (length orlist) 1)
  9792. (cons 'or orlist) (car orlist))))
  9793. ;; Return the string and lisp forms of the matcher
  9794. (setq matcher (if todomatcher
  9795. (list 'and tagsmatcher todomatcher)
  9796. tagsmatcher))
  9797. (cons match0 matcher)))
  9798. (defun org-op-to-function (op &optional stringp)
  9799. "Turn an operator into the appropriate function."
  9800. (setq op
  9801. (cond
  9802. ((equal op "<" ) '(< string< org-time<))
  9803. ((equal op ">" ) '(> org-string> org-time>))
  9804. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9805. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9806. ((member op '("=" "==")) '(= string= org-time=))
  9807. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9808. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9809. (defun org<> (a b) (not (= a b)))
  9810. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9811. (defun org-string>= (a b) (not (string< a b)))
  9812. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9813. (defun org-string<> (a b) (not (string= a b)))
  9814. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9815. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9816. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9817. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9818. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9819. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9820. (defun org-2ft (s)
  9821. "Convert S to a floating point time.
  9822. If S is already a number, just return it. If it is a string, parse
  9823. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9824. (cond
  9825. ((numberp s) s)
  9826. ((stringp s)
  9827. (condition-case nil
  9828. (float-time (apply 'encode-time (org-parse-time-string s)))
  9829. (error 0.)))
  9830. (t 0.)))
  9831. (defun org-time-today ()
  9832. "Time in seconds today at 0:00.
  9833. Returns the float number of seconds since the beginning of the
  9834. epoch to the beginning of today (00:00)."
  9835. (float-time (apply 'encode-time
  9836. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9837. (defun org-matcher-time (s)
  9838. "Interpret a time comparison value."
  9839. (save-match-data
  9840. (cond
  9841. ((string= s "<now>") (float-time))
  9842. ((string= s "<today>") (org-time-today))
  9843. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9844. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9845. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9846. (+ (org-time-today)
  9847. (* (string-to-number (match-string 1 s))
  9848. (cdr (assoc (match-string 2 s)
  9849. '(("d" . 86400.0) ("w" . 604800.0)
  9850. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9851. (t (org-2ft s)))))
  9852. (defun org-match-any-p (re list)
  9853. "Does re match any element of list?"
  9854. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9855. (delq nil list))
  9856. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9857. (defvar org-tags-overlay (org-make-overlay 1 1))
  9858. (org-detach-overlay org-tags-overlay)
  9859. (defun org-get-local-tags-at (&optional pos)
  9860. "Get a list of tags defined in the current headline."
  9861. (org-get-tags-at pos 'local))
  9862. (defun org-get-local-tags ()
  9863. "Get a list of tags defined in the current headline."
  9864. (org-get-tags-at nil 'local))
  9865. (defun org-get-tags-at (&optional pos local)
  9866. "Get a list of all headline tags applicable at POS.
  9867. POS defaults to point. If tags are inherited, the list contains
  9868. the targets in the same sequence as the headlines appear, i.e.
  9869. the tags of the current headline come last.
  9870. When LOCAL is non-nil, only return tags from the current headline,
  9871. ignore inherited ones."
  9872. (interactive)
  9873. (if (and org-trust-scanner-tags
  9874. (or (not pos) (equal pos (point)))
  9875. (not local))
  9876. org-scanner-tags
  9877. (let (tags ltags lastpos parent)
  9878. (save-excursion
  9879. (save-restriction
  9880. (widen)
  9881. (goto-char (or pos (point)))
  9882. (save-match-data
  9883. (catch 'done
  9884. (condition-case nil
  9885. (progn
  9886. (org-back-to-heading t)
  9887. (while (not (equal lastpos (point)))
  9888. (setq lastpos (point))
  9889. (when (looking-at
  9890. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9891. (setq ltags (org-split-string
  9892. (org-match-string-no-properties 1) ":"))
  9893. (when parent
  9894. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9895. (setq tags (append
  9896. (if parent
  9897. (org-remove-uniherited-tags ltags)
  9898. ltags)
  9899. tags)))
  9900. (or org-use-tag-inheritance (throw 'done t))
  9901. (if local (throw 'done t))
  9902. (or (org-up-heading-safe) (error nil))
  9903. (setq parent t)))
  9904. (error nil)))))
  9905. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9906. (defun org-add-prop-inherited (s)
  9907. (add-text-properties 0 (length s) '(inherited t) s)
  9908. s)
  9909. (defun org-toggle-tag (tag &optional onoff)
  9910. "Toggle the tag TAG for the current line.
  9911. If ONOFF is `on' or `off', don't toggle but set to this state."
  9912. (let (res current)
  9913. (save-excursion
  9914. (org-back-to-heading t)
  9915. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9916. (point-at-eol) t)
  9917. (progn
  9918. (setq current (match-string 1))
  9919. (replace-match ""))
  9920. (setq current ""))
  9921. (setq current (nreverse (org-split-string current ":")))
  9922. (cond
  9923. ((eq onoff 'on)
  9924. (setq res t)
  9925. (or (member tag current) (push tag current)))
  9926. ((eq onoff 'off)
  9927. (or (not (member tag current)) (setq current (delete tag current))))
  9928. (t (if (member tag current)
  9929. (setq current (delete tag current))
  9930. (setq res t)
  9931. (push tag current))))
  9932. (end-of-line 1)
  9933. (if current
  9934. (progn
  9935. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9936. (org-set-tags nil t))
  9937. (delete-horizontal-space))
  9938. (run-hooks 'org-after-tags-change-hook))
  9939. res))
  9940. (defun org-align-tags-here (to-col)
  9941. ;; Assumes that this is a headline
  9942. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9943. (beginning-of-line 1)
  9944. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9945. (< pos (match-beginning 2)))
  9946. (progn
  9947. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9948. (goto-char (match-beginning 1))
  9949. (insert " ")
  9950. (delete-region (point) (1+ (match-beginning 2)))
  9951. (setq ncol (max (1+ (current-column))
  9952. (1+ col)
  9953. (if (> to-col 0)
  9954. to-col
  9955. (- (abs to-col) tags-l))))
  9956. (setq p (point))
  9957. (insert (make-string (- ncol (current-column)) ?\ ))
  9958. (setq ncol (current-column))
  9959. (when indent-tabs-mode (tabify p (point-at-eol)))
  9960. (org-move-to-column (min ncol col) t))
  9961. (goto-char pos))))
  9962. (defun org-set-tags-command (&optional arg just-align)
  9963. "Call the set-tags command for the current entry."
  9964. (interactive "P")
  9965. (if (org-on-heading-p)
  9966. (org-set-tags arg just-align)
  9967. (save-excursion
  9968. (org-back-to-heading t)
  9969. (org-set-tags arg just-align))))
  9970. (defun org-set-tags (&optional arg just-align)
  9971. "Set the tags for the current headline.
  9972. With prefix ARG, realign all tags in headings in the current buffer."
  9973. (interactive "P")
  9974. (let* ((re (concat "^" outline-regexp))
  9975. (current (org-get-tags-string))
  9976. (col (current-column))
  9977. (org-setting-tags t)
  9978. table current-tags inherited-tags ; computed below when needed
  9979. tags p0 c0 c1 rpl)
  9980. (if arg
  9981. (save-excursion
  9982. (goto-char (point-min))
  9983. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9984. (while (re-search-forward re nil t)
  9985. (org-set-tags nil t)
  9986. (end-of-line 1)))
  9987. (message "All tags realigned to column %d" org-tags-column))
  9988. (if just-align
  9989. (setq tags current)
  9990. ;; Get a new set of tags from the user
  9991. (save-excursion
  9992. (setq table (append org-tag-persistent-alist
  9993. (or org-tag-alist (org-get-buffer-tags)))
  9994. org-last-tags-completion-table table
  9995. current-tags (org-split-string current ":")
  9996. inherited-tags (nreverse
  9997. (nthcdr (length current-tags)
  9998. (nreverse (org-get-tags-at))))
  9999. tags
  10000. (if (or (eq t org-use-fast-tag-selection)
  10001. (and org-use-fast-tag-selection
  10002. (delq nil (mapcar 'cdr table))))
  10003. (org-fast-tag-selection
  10004. current-tags inherited-tags table
  10005. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10006. (let ((org-add-colon-after-tag-completion t))
  10007. (org-trim
  10008. (org-without-partial-completion
  10009. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10010. nil nil current 'org-tags-history)))))))
  10011. (while (string-match "[-+&]+" tags)
  10012. ;; No boolean logic, just a list
  10013. (setq tags (replace-match ":" t t tags))))
  10014. (if org-tags-sort-function
  10015. (setq tags (mapconcat 'identity
  10016. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10017. org-tags-sort-function) ":")))
  10018. (if (string-match "\\`[\t ]*\\'" tags)
  10019. (setq tags "")
  10020. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10021. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10022. ;; Insert new tags at the correct column
  10023. (beginning-of-line 1)
  10024. (cond
  10025. ((and (equal current "") (equal tags "")))
  10026. ((re-search-forward
  10027. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10028. (point-at-eol) t)
  10029. (if (equal tags "")
  10030. (setq rpl "")
  10031. (goto-char (match-beginning 0))
  10032. (setq c0 (current-column) p0 (point)
  10033. c1 (max (1+ c0) (if (> org-tags-column 0)
  10034. org-tags-column
  10035. (- (- org-tags-column) (length tags))))
  10036. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10037. (replace-match rpl t t)
  10038. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10039. tags)
  10040. (t (error "Tags alignment failed")))
  10041. (org-move-to-column col)
  10042. (unless just-align
  10043. (run-hooks 'org-after-tags-change-hook)))))
  10044. (defun org-change-tag-in-region (beg end tag off)
  10045. "Add or remove TAG for each entry in the region.
  10046. This works in the agenda, and also in an org-mode buffer."
  10047. (interactive
  10048. (list (region-beginning) (region-end)
  10049. (let ((org-last-tags-completion-table
  10050. (if (org-mode-p)
  10051. (org-get-buffer-tags)
  10052. (org-global-tags-completion-table))))
  10053. (org-ido-completing-read
  10054. "Tag: " 'org-tags-completion-function nil nil nil
  10055. 'org-tags-history))
  10056. (progn
  10057. (message "[s]et or [r]emove? ")
  10058. (equal (read-char-exclusive) ?r))))
  10059. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10060. (let ((agendap (equal major-mode 'org-agenda-mode))
  10061. l1 l2 m buf pos newhead (cnt 0))
  10062. (goto-char end)
  10063. (setq l2 (1- (org-current-line)))
  10064. (goto-char beg)
  10065. (setq l1 (org-current-line))
  10066. (loop for l from l1 to l2 do
  10067. (goto-line l)
  10068. (setq m (get-text-property (point) 'org-hd-marker))
  10069. (when (or (and (org-mode-p) (org-on-heading-p))
  10070. (and agendap m))
  10071. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10072. pos (if agendap m (point)))
  10073. (with-current-buffer buf
  10074. (save-excursion
  10075. (save-restriction
  10076. (goto-char pos)
  10077. (setq cnt (1+ cnt))
  10078. (org-toggle-tag tag (if off 'off 'on))
  10079. (setq newhead (org-get-heading)))))
  10080. (and agendap (org-agenda-change-all-lines newhead m))))
  10081. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10082. (defun org-tags-completion-function (string predicate &optional flag)
  10083. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10084. (confirm (lambda (x) (stringp (car x)))))
  10085. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10086. (setq s1 (match-string 1 string)
  10087. s2 (match-string 2 string))
  10088. (setq s1 "" s2 string))
  10089. (cond
  10090. ((eq flag nil)
  10091. ;; try completion
  10092. (setq rtn (try-completion s2 ctable confirm))
  10093. (if (stringp rtn)
  10094. (setq rtn
  10095. (concat s1 s2 (substring rtn (length s2))
  10096. (if (and org-add-colon-after-tag-completion
  10097. (assoc rtn ctable))
  10098. ":" ""))))
  10099. rtn)
  10100. ((eq flag t)
  10101. ;; all-completions
  10102. (all-completions s2 ctable confirm)
  10103. )
  10104. ((eq flag 'lambda)
  10105. ;; exact match?
  10106. (assoc s2 ctable)))
  10107. ))
  10108. (defun org-fast-tag-insert (kwd tags face &optional end)
  10109. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10110. (insert (format "%-12s" (concat kwd ":"))
  10111. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10112. (or end "")))
  10113. (defun org-fast-tag-show-exit (flag)
  10114. (save-excursion
  10115. (goto-line 3)
  10116. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10117. (replace-match ""))
  10118. (when flag
  10119. (end-of-line 1)
  10120. (org-move-to-column (- (window-width) 19) t)
  10121. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10122. (defun org-set-current-tags-overlay (current prefix)
  10123. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10124. (if (featurep 'xemacs)
  10125. (org-overlay-display org-tags-overlay (concat prefix s)
  10126. 'secondary-selection)
  10127. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10128. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10129. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10130. "Fast tag selection with single keys.
  10131. CURRENT is the current list of tags in the headline, INHERITED is the
  10132. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10133. possibly with grouping information. TODO-TABLE is a similar table with
  10134. TODO keywords, should these have keys assigned to them.
  10135. If the keys are nil, a-z are automatically assigned.
  10136. Returns the new tags string, or nil to not change the current settings."
  10137. (let* ((fulltable (append table todo-table))
  10138. (maxlen (apply 'max (mapcar
  10139. (lambda (x)
  10140. (if (stringp (car x)) (string-width (car x)) 0))
  10141. fulltable)))
  10142. (buf (current-buffer))
  10143. (expert (eq org-fast-tag-selection-single-key 'expert))
  10144. (buffer-tags nil)
  10145. (fwidth (+ maxlen 3 1 3))
  10146. (ncol (/ (- (window-width) 4) fwidth))
  10147. (i-face 'org-done)
  10148. (c-face 'org-todo)
  10149. tg cnt e c char c1 c2 ntable tbl rtn
  10150. ov-start ov-end ov-prefix
  10151. (exit-after-next org-fast-tag-selection-single-key)
  10152. (done-keywords org-done-keywords)
  10153. groups ingroup)
  10154. (save-excursion
  10155. (beginning-of-line 1)
  10156. (if (looking-at
  10157. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10158. (setq ov-start (match-beginning 1)
  10159. ov-end (match-end 1)
  10160. ov-prefix "")
  10161. (setq ov-start (1- (point-at-eol))
  10162. ov-end (1+ ov-start))
  10163. (skip-chars-forward "^\n\r")
  10164. (setq ov-prefix
  10165. (concat
  10166. (buffer-substring (1- (point)) (point))
  10167. (if (> (current-column) org-tags-column)
  10168. " "
  10169. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10170. (org-move-overlay org-tags-overlay ov-start ov-end)
  10171. (save-window-excursion
  10172. (if expert
  10173. (set-buffer (get-buffer-create " *Org tags*"))
  10174. (delete-other-windows)
  10175. (split-window-vertically)
  10176. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10177. (erase-buffer)
  10178. (org-set-local 'org-done-keywords done-keywords)
  10179. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10180. (org-fast-tag-insert "Current" current c-face "\n\n")
  10181. (org-fast-tag-show-exit exit-after-next)
  10182. (org-set-current-tags-overlay current ov-prefix)
  10183. (setq tbl fulltable char ?a cnt 0)
  10184. (while (setq e (pop tbl))
  10185. (cond
  10186. ((equal e '(:startgroup))
  10187. (push '() groups) (setq ingroup t)
  10188. (when (not (= cnt 0))
  10189. (setq cnt 0)
  10190. (insert "\n"))
  10191. (insert "{ "))
  10192. ((equal e '(:endgroup))
  10193. (setq ingroup nil cnt 0)
  10194. (insert "}\n"))
  10195. ((equal e '(:newline))
  10196. (when (not (= cnt 0))
  10197. (setq cnt 0)
  10198. (insert "\n")
  10199. (setq e (car tbl))
  10200. (while (equal (car tbl) '(:newline))
  10201. (insert "\n")
  10202. (setq tbl (cdr tbl)))))
  10203. (t
  10204. (setq tg (car e) c2 nil)
  10205. (if (cdr e)
  10206. (setq c (cdr e))
  10207. ;; automatically assign a character.
  10208. (setq c1 (string-to-char
  10209. (downcase (substring
  10210. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10211. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10212. (while (or (rassoc char ntable) (rassoc char table))
  10213. (setq char (1+ char)))
  10214. (setq c2 c1))
  10215. (setq c (or c2 char)))
  10216. (if ingroup (push tg (car groups)))
  10217. (setq tg (org-add-props tg nil 'face
  10218. (cond
  10219. ((not (assoc tg table))
  10220. (org-get-todo-face tg))
  10221. ((member tg current) c-face)
  10222. ((member tg inherited) i-face)
  10223. (t nil))))
  10224. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10225. (insert "[" c "] " tg (make-string
  10226. (- fwidth 4 (length tg)) ?\ ))
  10227. (push (cons tg c) ntable)
  10228. (when (= (setq cnt (1+ cnt)) ncol)
  10229. (insert "\n")
  10230. (if ingroup (insert " "))
  10231. (setq cnt 0)))))
  10232. (setq ntable (nreverse ntable))
  10233. (insert "\n")
  10234. (goto-char (point-min))
  10235. (if (not expert) (org-fit-window-to-buffer))
  10236. (setq rtn
  10237. (catch 'exit
  10238. (while t
  10239. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10240. (if groups " [!] no groups" " [!]groups")
  10241. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10242. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10243. (cond
  10244. ((= c ?\r) (throw 'exit t))
  10245. ((= c ?!)
  10246. (setq groups (not groups))
  10247. (goto-char (point-min))
  10248. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10249. ((= c ?\C-c)
  10250. (if (not expert)
  10251. (org-fast-tag-show-exit
  10252. (setq exit-after-next (not exit-after-next)))
  10253. (setq expert nil)
  10254. (delete-other-windows)
  10255. (split-window-vertically)
  10256. (org-switch-to-buffer-other-window " *Org tags*")
  10257. (org-fit-window-to-buffer)))
  10258. ((or (= c ?\C-g)
  10259. (and (= c ?q) (not (rassoc c ntable))))
  10260. (org-detach-overlay org-tags-overlay)
  10261. (setq quit-flag t))
  10262. ((= c ?\ )
  10263. (setq current nil)
  10264. (if exit-after-next (setq exit-after-next 'now)))
  10265. ((= c ?\t)
  10266. (condition-case nil
  10267. (setq tg (org-ido-completing-read
  10268. "Tag: "
  10269. (or buffer-tags
  10270. (with-current-buffer buf
  10271. (org-get-buffer-tags)))))
  10272. (quit (setq tg "")))
  10273. (when (string-match "\\S-" tg)
  10274. (add-to-list 'buffer-tags (list tg))
  10275. (if (member tg current)
  10276. (setq current (delete tg current))
  10277. (push tg current)))
  10278. (if exit-after-next (setq exit-after-next 'now)))
  10279. ((setq e (rassoc c todo-table) tg (car e))
  10280. (with-current-buffer buf
  10281. (save-excursion (org-todo tg)))
  10282. (if exit-after-next (setq exit-after-next 'now)))
  10283. ((setq e (rassoc c ntable) tg (car e))
  10284. (if (member tg current)
  10285. (setq current (delete tg current))
  10286. (loop for g in groups do
  10287. (if (member tg g)
  10288. (mapc (lambda (x)
  10289. (setq current (delete x current)))
  10290. g)))
  10291. (push tg current))
  10292. (if exit-after-next (setq exit-after-next 'now))))
  10293. ;; Create a sorted list
  10294. (setq current
  10295. (sort current
  10296. (lambda (a b)
  10297. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10298. (if (eq exit-after-next 'now) (throw 'exit t))
  10299. (goto-char (point-min))
  10300. (beginning-of-line 2)
  10301. (delete-region (point) (point-at-eol))
  10302. (org-fast-tag-insert "Current" current c-face)
  10303. (org-set-current-tags-overlay current ov-prefix)
  10304. (while (re-search-forward
  10305. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10306. (setq tg (match-string 1))
  10307. (add-text-properties
  10308. (match-beginning 1) (match-end 1)
  10309. (list 'face
  10310. (cond
  10311. ((member tg current) c-face)
  10312. ((member tg inherited) i-face)
  10313. (t (get-text-property (match-beginning 1) 'face))))))
  10314. (goto-char (point-min)))))
  10315. (org-detach-overlay org-tags-overlay)
  10316. (if rtn
  10317. (mapconcat 'identity current ":")
  10318. nil))))
  10319. (defun org-get-tags-string ()
  10320. "Get the TAGS string in the current headline."
  10321. (unless (org-on-heading-p t)
  10322. (error "Not on a heading"))
  10323. (save-excursion
  10324. (beginning-of-line 1)
  10325. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10326. (org-match-string-no-properties 1)
  10327. "")))
  10328. (defun org-get-tags ()
  10329. "Get the list of tags specified in the current headline."
  10330. (org-split-string (org-get-tags-string) ":"))
  10331. (defun org-get-buffer-tags ()
  10332. "Get a table of all tags used in the buffer, for completion."
  10333. (let (tags)
  10334. (save-excursion
  10335. (goto-char (point-min))
  10336. (while (re-search-forward
  10337. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10338. (when (equal (char-after (point-at-bol 0)) ?*)
  10339. (mapc (lambda (x) (add-to-list 'tags x))
  10340. (org-split-string (org-match-string-no-properties 1) ":")))))
  10341. (mapcar 'list tags)))
  10342. ;;;; The mapping API
  10343. ;;;###autoload
  10344. (defun org-map-entries (func &optional match scope &rest skip)
  10345. "Call FUNC at each headline selected by MATCH in SCOPE.
  10346. FUNC is a function or a lisp form. The function will be called without
  10347. arguments, with the cursor positioned at the beginning of the headline.
  10348. The return values of all calls to the function will be collected and
  10349. returned as a list.
  10350. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10351. does not need to preserve point. After evaluation, the cursor will be
  10352. moved to the end of the line (presumably of the headline of the
  10353. processed entry) and search continues from there. Under some
  10354. circumstances, this may not produce the wanted results. For example,
  10355. if you have removed (e.g. archived) the current (sub)tree it could
  10356. mean that the next entry will be skipped entirely. In such cases, you
  10357. can specify the position from where search should continue by making
  10358. FUNC set the variable `org-map-continue-from' to the desired buffer
  10359. position.
  10360. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10361. Only headlines that are matched by this query will be considered during
  10362. the iteration. When MATCH is nil or t, all headlines will be
  10363. visited by the iteration.
  10364. SCOPE determines the scope of this command. It can be any of:
  10365. nil The current buffer, respecting the restriction if any
  10366. tree The subtree started with the entry at point
  10367. file The current buffer, without restriction
  10368. file-with-archives
  10369. The current buffer, and any archives associated with it
  10370. agenda All agenda files
  10371. agenda-with-archives
  10372. All agenda files with any archive files associated with them
  10373. \(file1 file2 ...)
  10374. If this is a list, all files in the list will be scanned
  10375. The remaining args are treated as settings for the skipping facilities of
  10376. the scanner. The following items can be given here:
  10377. archive skip trees with the archive tag.
  10378. comment skip trees with the COMMENT keyword
  10379. function or Emacs Lisp form:
  10380. will be used as value for `org-agenda-skip-function', so whenever
  10381. the the function returns t, FUNC will not be called for that
  10382. entry and search will continue from the point where the
  10383. function leaves it.
  10384. If your function needs to retrieve the tags including inherited tags
  10385. at the *current* entry, you can use the value of the variable
  10386. `org-scanner-tags' which will be much faster than getting the value
  10387. with `org-get-tags-at'. If your function gets properties with
  10388. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10389. to t around the call to `org-entry-properties' to get the same speedup.
  10390. Note that if your function moves around to retrieve tags and properties at
  10391. a *different* entry, you cannot use these techniques."
  10392. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10393. (org-agenda-skip-archived-trees (memq 'archive skip))
  10394. (org-agenda-skip-comment-trees (memq 'comment skip))
  10395. (org-agenda-skip-function
  10396. (car (org-delete-all '(comment archive) skip)))
  10397. (org-tags-match-list-sublevels t)
  10398. matcher file res
  10399. org-todo-keywords-for-agenda
  10400. org-done-keywords-for-agenda
  10401. org-todo-keyword-alist-for-agenda
  10402. org-tag-alist-for-agenda)
  10403. (cond
  10404. ((eq match t) (setq matcher t))
  10405. ((eq match nil) (setq matcher t))
  10406. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10407. (save-excursion
  10408. (save-restriction
  10409. (when (eq scope 'tree)
  10410. (org-back-to-heading t)
  10411. (org-narrow-to-subtree)
  10412. (setq scope nil))
  10413. (if (not scope)
  10414. (progn
  10415. (org-prepare-agenda-buffers
  10416. (list (buffer-file-name (current-buffer))))
  10417. (setq res (org-scan-tags func matcher)))
  10418. ;; Get the right scope
  10419. (cond
  10420. ((and scope (listp scope) (symbolp (car scope)))
  10421. (setq scope (eval scope)))
  10422. ((eq scope 'agenda)
  10423. (setq scope (org-agenda-files t)))
  10424. ((eq scope 'agenda-with-archives)
  10425. (setq scope (org-agenda-files t))
  10426. (setq scope (org-add-archive-files scope)))
  10427. ((eq scope 'file)
  10428. (setq scope (list (buffer-file-name))))
  10429. ((eq scope 'file-with-archives)
  10430. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10431. (org-prepare-agenda-buffers scope)
  10432. (while (setq file (pop scope))
  10433. (with-current-buffer (org-find-base-buffer-visiting file)
  10434. (save-excursion
  10435. (save-restriction
  10436. (widen)
  10437. (goto-char (point-min))
  10438. (setq res (append res (org-scan-tags func matcher))))))))))
  10439. res))
  10440. ;;;; Properties
  10441. ;;; Setting and retrieving properties
  10442. (defconst org-special-properties
  10443. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10444. "TIMESTAMP" "TIMESTAMP_IA")
  10445. "The special properties valid in Org-mode.
  10446. These are properties that are not defined in the property drawer,
  10447. but in some other way.")
  10448. (defconst org-default-properties
  10449. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10450. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10451. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10452. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10453. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10454. "CLOCK_MODELINE_TOTAL")
  10455. "Some properties that are used by Org-mode for various purposes.
  10456. Being in this list makes sure that they are offered for completion.")
  10457. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10458. "Regular expression matching the first line of a property drawer.")
  10459. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10460. "Regular expression matching the first line of a property drawer.")
  10461. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10462. "Regular expression matching the first line of a property drawer.")
  10463. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10464. "Regular expression matching the first line of a property drawer.")
  10465. (defconst org-property-drawer-re
  10466. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10467. org-property-end-re "\\)\n?")
  10468. "Matches an entire property drawer.")
  10469. (defconst org-clock-drawer-re
  10470. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10471. org-property-end-re "\\)\n?")
  10472. "Matches an entire clock drawer.")
  10473. (defun org-property-action ()
  10474. "Do an action on properties."
  10475. (interactive)
  10476. (let (c)
  10477. (org-at-property-p)
  10478. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10479. (setq c (read-char-exclusive))
  10480. (cond
  10481. ((equal c ?s)
  10482. (call-interactively 'org-set-property))
  10483. ((equal c ?d)
  10484. (call-interactively 'org-delete-property))
  10485. ((equal c ?D)
  10486. (call-interactively 'org-delete-property-globally))
  10487. ((equal c ?c)
  10488. (call-interactively 'org-compute-property-at-point))
  10489. (t (error "No such property action %c" c)))))
  10490. (defun org-at-property-p ()
  10491. "Is the cursor in a property line?"
  10492. ;; FIXME: Does not check if we are actually in the drawer.
  10493. ;; FIXME: also returns true on any drawers.....
  10494. ;; This is used by C-c C-c for property action.
  10495. (save-excursion
  10496. (beginning-of-line 1)
  10497. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10498. (defun org-get-property-block (&optional beg end force)
  10499. "Return the (beg . end) range of the body of the property drawer.
  10500. BEG and END can be beginning and end of subtree, if not given
  10501. they will be found.
  10502. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10503. (catch 'exit
  10504. (save-excursion
  10505. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10506. (end (or end (progn (outline-next-heading) (point)))))
  10507. (goto-char beg)
  10508. (if (re-search-forward org-property-start-re end t)
  10509. (setq beg (1+ (match-end 0)))
  10510. (if force
  10511. (save-excursion
  10512. (org-insert-property-drawer)
  10513. (setq end (progn (outline-next-heading) (point))))
  10514. (throw 'exit nil))
  10515. (goto-char beg)
  10516. (if (re-search-forward org-property-start-re end t)
  10517. (setq beg (1+ (match-end 0)))))
  10518. (if (re-search-forward org-property-end-re end t)
  10519. (setq end (match-beginning 0))
  10520. (or force (throw 'exit nil))
  10521. (goto-char beg)
  10522. (setq end beg)
  10523. (org-indent-line-function)
  10524. (insert ":END:\n"))
  10525. (cons beg end)))))
  10526. (defun org-entry-properties (&optional pom which)
  10527. "Get all properties of the entry at point-or-marker POM.
  10528. This includes the TODO keyword, the tags, time strings for deadline,
  10529. scheduled, and clocking, and any additional properties defined in the
  10530. entry. The return value is an alist, keys may occur multiple times
  10531. if the property key was used several times.
  10532. POM may also be nil, in which case the current entry is used.
  10533. If WHICH is nil or `all', get all properties. If WHICH is
  10534. `special' or `standard', only get that subclass."
  10535. (setq which (or which 'all))
  10536. (org-with-point-at pom
  10537. (let ((clockstr (substring org-clock-string 0 -1))
  10538. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10539. beg end range props sum-props key value string clocksum)
  10540. (save-excursion
  10541. (when (condition-case nil
  10542. (and (org-mode-p) (org-back-to-heading t))
  10543. (error nil))
  10544. (setq beg (point))
  10545. (setq sum-props (get-text-property (point) 'org-summaries))
  10546. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10547. (outline-next-heading)
  10548. (setq end (point))
  10549. (when (memq which '(all special))
  10550. ;; Get the special properties, like TODO and tags
  10551. (goto-char beg)
  10552. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10553. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10554. (when (looking-at org-priority-regexp)
  10555. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10556. (when (and (setq value (org-get-tags-string))
  10557. (string-match "\\S-" value))
  10558. (push (cons "TAGS" value) props))
  10559. (when (setq value (org-get-tags-at))
  10560. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10561. props))
  10562. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10563. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10564. string (if (equal key clockstr)
  10565. (org-no-properties
  10566. (org-trim
  10567. (buffer-substring
  10568. (match-beginning 3) (goto-char (point-at-eol)))))
  10569. (substring (org-match-string-no-properties 3) 1 -1)))
  10570. (unless key
  10571. (if (= (char-after (match-beginning 3)) ?\[)
  10572. (setq key "TIMESTAMP_IA")
  10573. (setq key "TIMESTAMP")))
  10574. (when (or (equal key clockstr) (not (assoc key props)))
  10575. (push (cons key string) props)))
  10576. )
  10577. (when (memq which '(all standard))
  10578. ;; Get the standard properties, like :PROP: ...
  10579. (setq range (org-get-property-block beg end))
  10580. (when range
  10581. (goto-char (car range))
  10582. (while (re-search-forward
  10583. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10584. (cdr range) t)
  10585. (setq key (org-match-string-no-properties 1)
  10586. value (org-trim (or (org-match-string-no-properties 2) "")))
  10587. (unless (member key excluded)
  10588. (push (cons key (or value "")) props)))))
  10589. (if clocksum
  10590. (push (cons "CLOCKSUM"
  10591. (org-columns-number-to-string (/ (float clocksum) 60.)
  10592. 'add_times))
  10593. props))
  10594. (unless (assoc "CATEGORY" props)
  10595. (setq value (or (org-get-category)
  10596. (progn (org-refresh-category-properties)
  10597. (org-get-category))))
  10598. (push (cons "CATEGORY" value) props))
  10599. (append sum-props (nreverse props)))))))
  10600. (defun org-entry-get (pom property &optional inherit)
  10601. "Get value of PROPERTY for entry at point-or-marker POM.
  10602. If INHERIT is non-nil and the entry does not have the property,
  10603. then also check higher levels of the hierarchy.
  10604. If INHERIT is the symbol `selective', use inheritance only if the setting
  10605. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10606. If the property is present but empty, the return value is the empty string.
  10607. If the property is not present at all, nil is returned."
  10608. (org-with-point-at pom
  10609. (if (and inherit (if (eq inherit 'selective)
  10610. (org-property-inherit-p property)
  10611. t))
  10612. (org-entry-get-with-inheritance property)
  10613. (if (member property org-special-properties)
  10614. ;; We need a special property. Use brute force, get all properties.
  10615. (cdr (assoc property (org-entry-properties nil 'special)))
  10616. (let ((range (org-get-property-block)))
  10617. (if (and range
  10618. (goto-char (car range))
  10619. (re-search-forward
  10620. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10621. (cdr range) t))
  10622. ;; Found the property, return it.
  10623. (if (match-end 1)
  10624. (org-match-string-no-properties 1)
  10625. "")))))))
  10626. (defun org-property-or-variable-value (var &optional inherit)
  10627. "Check if there is a property fixing the value of VAR.
  10628. If yes, return this value. If not, return the current value of the variable."
  10629. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10630. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10631. (read prop)
  10632. (symbol-value var))))
  10633. (defun org-entry-delete (pom property)
  10634. "Delete the property PROPERTY from entry at point-or-marker POM."
  10635. (org-with-point-at pom
  10636. (if (member property org-special-properties)
  10637. nil ; cannot delete these properties.
  10638. (let ((range (org-get-property-block)))
  10639. (if (and range
  10640. (goto-char (car range))
  10641. (re-search-forward
  10642. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10643. (cdr range) t))
  10644. (progn
  10645. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10646. t)
  10647. nil)))))
  10648. ;; Multi-values properties are properties that contain multiple values
  10649. ;; These values are assumed to be single words, separated by whitespace.
  10650. (defun org-entry-add-to-multivalued-property (pom property value)
  10651. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10652. (let* ((old (org-entry-get pom property))
  10653. (values (and old (org-split-string old "[ \t]"))))
  10654. (setq value (org-entry-protect-space value))
  10655. (unless (member value values)
  10656. (setq values (cons value values))
  10657. (org-entry-put pom property
  10658. (mapconcat 'identity values " ")))))
  10659. (defun org-entry-remove-from-multivalued-property (pom property value)
  10660. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10661. (let* ((old (org-entry-get pom property))
  10662. (values (and old (org-split-string old "[ \t]"))))
  10663. (setq value (org-entry-protect-space value))
  10664. (when (member value values)
  10665. (setq values (delete value values))
  10666. (org-entry-put pom property
  10667. (mapconcat 'identity values " ")))))
  10668. (defun org-entry-member-in-multivalued-property (pom property value)
  10669. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10670. (let* ((old (org-entry-get pom property))
  10671. (values (and old (org-split-string old "[ \t]"))))
  10672. (setq value (org-entry-protect-space value))
  10673. (member value values)))
  10674. (defun org-entry-get-multivalued-property (pom property)
  10675. "Return a list of values in a multivalued property."
  10676. (let* ((value (org-entry-get pom property))
  10677. (values (and value (org-split-string value "[ \t]"))))
  10678. (mapcar 'org-entry-restore-space values)))
  10679. (defun org-entry-put-multivalued-property (pom property &rest values)
  10680. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10681. VALUES should be a list of strings. Spaces will be protected."
  10682. (org-entry-put pom property
  10683. (mapconcat 'org-entry-protect-space values " "))
  10684. (let* ((value (org-entry-get pom property))
  10685. (values (and value (org-split-string value "[ \t]"))))
  10686. (mapcar 'org-entry-restore-space values)))
  10687. (defun org-entry-protect-space (s)
  10688. "Protect spaces and newline in string S."
  10689. (while (string-match " " s)
  10690. (setq s (replace-match "%20" t t s)))
  10691. (while (string-match "\n" s)
  10692. (setq s (replace-match "%0A" t t s)))
  10693. s)
  10694. (defun org-entry-restore-space (s)
  10695. "Restore spaces and newline in string S."
  10696. (while (string-match "%20" s)
  10697. (setq s (replace-match " " t t s)))
  10698. (while (string-match "%0A" s)
  10699. (setq s (replace-match "\n" t t s)))
  10700. s)
  10701. (defvar org-entry-property-inherited-from (make-marker)
  10702. "Marker pointing to the entry from where a property was inherited.
  10703. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10704. location of the entry where the inheritance search matched. If there was
  10705. no match, the marker will point nowhere.
  10706. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10707. is set.")
  10708. (defun org-entry-get-with-inheritance (property)
  10709. "Get entry property, and search higher levels if not present."
  10710. (move-marker org-entry-property-inherited-from nil)
  10711. (let (tmp)
  10712. (save-excursion
  10713. (save-restriction
  10714. (widen)
  10715. (catch 'ex
  10716. (while t
  10717. (when (setq tmp (org-entry-get nil property))
  10718. (org-back-to-heading t)
  10719. (move-marker org-entry-property-inherited-from (point))
  10720. (throw 'ex tmp))
  10721. (or (org-up-heading-safe) (throw 'ex nil)))))
  10722. (or tmp
  10723. (cdr (assoc property org-file-properties))
  10724. (cdr (assoc property org-global-properties))
  10725. (cdr (assoc property org-global-properties-fixed))))))
  10726. (defun org-entry-put (pom property value)
  10727. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10728. (org-with-point-at pom
  10729. (org-back-to-heading t)
  10730. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10731. range)
  10732. (cond
  10733. ((equal property "TODO")
  10734. (when (and (stringp value) (string-match "\\S-" value)
  10735. (not (member value org-todo-keywords-1)))
  10736. (error "\"%s\" is not a valid TODO state" value))
  10737. (if (or (not value)
  10738. (not (string-match "\\S-" value)))
  10739. (setq value 'none))
  10740. (org-todo value)
  10741. (org-set-tags nil 'align))
  10742. ((equal property "PRIORITY")
  10743. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10744. (string-to-char value) ?\ ))
  10745. (org-set-tags nil 'align))
  10746. ((equal property "SCHEDULED")
  10747. (if (re-search-forward org-scheduled-time-regexp end t)
  10748. (cond
  10749. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10750. ((eq value 'later) (org-timestamp-change 1 'day))
  10751. (t (call-interactively 'org-schedule)))
  10752. (call-interactively 'org-schedule)))
  10753. ((equal property "DEADLINE")
  10754. (if (re-search-forward org-deadline-time-regexp end t)
  10755. (cond
  10756. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10757. ((eq value 'later) (org-timestamp-change 1 'day))
  10758. (t (call-interactively 'org-deadline)))
  10759. (call-interactively 'org-deadline)))
  10760. ((member property org-special-properties)
  10761. (error "The %s property can not yet be set with `org-entry-put'"
  10762. property))
  10763. (t ; a non-special property
  10764. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10765. (setq range (org-get-property-block beg end 'force))
  10766. (goto-char (car range))
  10767. (if (re-search-forward
  10768. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10769. (progn
  10770. (delete-region (match-beginning 1) (match-end 1))
  10771. (goto-char (match-beginning 1)))
  10772. (goto-char (cdr range))
  10773. (insert "\n")
  10774. (backward-char 1)
  10775. (org-indent-line-function)
  10776. (insert ":" property ":"))
  10777. (and value (insert " " value))
  10778. (org-indent-line-function)))))))
  10779. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10780. "Get all property keys in the current buffer.
  10781. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10782. like tags and TODO state.
  10783. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10784. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10785. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10786. formats in the current buffer."
  10787. (let (rtn range cfmt s p)
  10788. (save-excursion
  10789. (save-restriction
  10790. (widen)
  10791. (goto-char (point-min))
  10792. (while (re-search-forward org-property-start-re nil t)
  10793. (setq range (org-get-property-block))
  10794. (goto-char (car range))
  10795. (while (re-search-forward
  10796. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10797. (cdr range) t)
  10798. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10799. (outline-next-heading))))
  10800. (when include-specials
  10801. (setq rtn (append org-special-properties rtn)))
  10802. (when include-defaults
  10803. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10804. (add-to-list 'rtn org-effort-property))
  10805. (when include-columns
  10806. (save-excursion
  10807. (save-restriction
  10808. (widen)
  10809. (goto-char (point-min))
  10810. (while (re-search-forward
  10811. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10812. nil t)
  10813. (setq cfmt (match-string 2) s 0)
  10814. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10815. cfmt s)
  10816. (setq s (match-end 0)
  10817. p (match-string 1 cfmt))
  10818. (unless (or (equal p "ITEM")
  10819. (member p org-special-properties))
  10820. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10821. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10822. (defun org-property-values (key)
  10823. "Return a list of all values of property KEY."
  10824. (save-excursion
  10825. (save-restriction
  10826. (widen)
  10827. (goto-char (point-min))
  10828. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10829. values)
  10830. (while (re-search-forward re nil t)
  10831. (add-to-list 'values (org-trim (match-string 1))))
  10832. (delete "" values)))))
  10833. (defun org-insert-property-drawer ()
  10834. "Insert a property drawer into the current entry."
  10835. (interactive)
  10836. (org-back-to-heading t)
  10837. (looking-at outline-regexp)
  10838. (let ((indent (if org-adapt-indentation
  10839. (- (match-end 0)(match-beginning 0))
  10840. 0))
  10841. (beg (point))
  10842. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10843. end hiddenp)
  10844. (outline-next-heading)
  10845. (setq end (point))
  10846. (goto-char beg)
  10847. (while (re-search-forward re end t))
  10848. (setq hiddenp (org-invisible-p))
  10849. (end-of-line 1)
  10850. (and (equal (char-after) ?\n) (forward-char 1))
  10851. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10852. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10853. ;; just skip this line
  10854. (beginning-of-line 2)
  10855. ;; Drawer start, find the end
  10856. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10857. (beginning-of-line 1)))
  10858. (org-skip-over-state-notes)
  10859. (skip-chars-backward " \t\n\r")
  10860. (if (eq (char-before) ?*) (forward-char 1))
  10861. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10862. (beginning-of-line 0)
  10863. (org-indent-to-column indent)
  10864. (beginning-of-line 2)
  10865. (org-indent-to-column indent)
  10866. (beginning-of-line 0)
  10867. (if hiddenp
  10868. (save-excursion
  10869. (org-back-to-heading t)
  10870. (hide-entry))
  10871. (org-flag-drawer t))))
  10872. (defun org-set-property (property value)
  10873. "In the current entry, set PROPERTY to VALUE.
  10874. When called interactively, this will prompt for a property name, offering
  10875. completion on existing and default properties. And then it will prompt
  10876. for a value, offering completion either on allowed values (via an inherited
  10877. xxx_ALL property) or on existing values in other instances of this property
  10878. in the current file."
  10879. (interactive
  10880. (let* ((completion-ignore-case t)
  10881. (keys (org-buffer-property-keys nil t t))
  10882. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10883. (prop (if (member prop0 keys)
  10884. prop0
  10885. (or (cdr (assoc (downcase prop0)
  10886. (mapcar (lambda (x) (cons (downcase x) x))
  10887. keys)))
  10888. prop0)))
  10889. (cur (org-entry-get nil prop))
  10890. (allowed (org-property-get-allowed-values nil prop 'table))
  10891. (existing (mapcar 'list (org-property-values prop)))
  10892. (val (if allowed
  10893. (org-completing-read "Value: " allowed nil 'req-match)
  10894. (let (org-completion-use-ido)
  10895. (org-completing-read
  10896. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10897. (concat "[" cur "]") "")
  10898. ": ")
  10899. existing nil nil "" nil cur)))))
  10900. (list prop (if (equal val "") cur val))))
  10901. (unless (equal (org-entry-get nil property) value)
  10902. (org-entry-put nil property value)))
  10903. (defun org-delete-property (property)
  10904. "In the current entry, delete PROPERTY."
  10905. (interactive
  10906. (let* ((completion-ignore-case t)
  10907. (prop (org-ido-completing-read
  10908. "Property: " (org-entry-properties nil 'standard))))
  10909. (list prop)))
  10910. (message "Property %s %s" property
  10911. (if (org-entry-delete nil property)
  10912. "deleted"
  10913. "was not present in the entry")))
  10914. (defun org-delete-property-globally (property)
  10915. "Remove PROPERTY globally, from all entries."
  10916. (interactive
  10917. (let* ((completion-ignore-case t)
  10918. (prop (org-ido-completing-read
  10919. "Globally remove property: "
  10920. (mapcar 'list (org-buffer-property-keys)))))
  10921. (list prop)))
  10922. (save-excursion
  10923. (save-restriction
  10924. (widen)
  10925. (goto-char (point-min))
  10926. (let ((cnt 0))
  10927. (while (re-search-forward
  10928. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10929. nil t)
  10930. (setq cnt (1+ cnt))
  10931. (replace-match ""))
  10932. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10933. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10934. (defun org-compute-property-at-point ()
  10935. "Compute the property at point.
  10936. This looks for an enclosing column format, extracts the operator and
  10937. then applies it to the property in the column format's scope."
  10938. (interactive)
  10939. (unless (org-at-property-p)
  10940. (error "Not at a property"))
  10941. (let ((prop (org-match-string-no-properties 2)))
  10942. (org-columns-get-format-and-top-level)
  10943. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10944. (error "No operator defined for property %s" prop))
  10945. (org-columns-compute prop)))
  10946. (defun org-property-get-allowed-values (pom property &optional table)
  10947. "Get allowed values for the property PROPERTY.
  10948. When TABLE is non-nil, return an alist that can directly be used for
  10949. completion."
  10950. (let (vals)
  10951. (cond
  10952. ((equal property "TODO")
  10953. (setq vals (org-with-point-at pom
  10954. (append org-todo-keywords-1 '("")))))
  10955. ((equal property "PRIORITY")
  10956. (let ((n org-lowest-priority))
  10957. (while (>= n org-highest-priority)
  10958. (push (char-to-string n) vals)
  10959. (setq n (1- n)))))
  10960. ((member property org-special-properties))
  10961. (t
  10962. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10963. (when (and vals (string-match "\\S-" vals))
  10964. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10965. (setq vals (mapcar (lambda (x)
  10966. (cond ((stringp x) x)
  10967. ((numberp x) (number-to-string x))
  10968. ((symbolp x) (symbol-name x))
  10969. (t "???")))
  10970. vals)))))
  10971. (if table (mapcar 'list vals) vals)))
  10972. (defun org-property-previous-allowed-value (&optional previous)
  10973. "Switch to the next allowed value for this property."
  10974. (interactive)
  10975. (org-property-next-allowed-value t))
  10976. (defun org-property-next-allowed-value (&optional previous)
  10977. "Switch to the next allowed value for this property."
  10978. (interactive)
  10979. (unless (org-at-property-p)
  10980. (error "Not at a property"))
  10981. (let* ((key (match-string 2))
  10982. (value (match-string 3))
  10983. (allowed (or (org-property-get-allowed-values (point) key)
  10984. (and (member value '("[ ]" "[-]" "[X]"))
  10985. '("[ ]" "[X]"))))
  10986. nval)
  10987. (unless allowed
  10988. (error "Allowed values for this property have not been defined"))
  10989. (if previous (setq allowed (reverse allowed)))
  10990. (if (member value allowed)
  10991. (setq nval (car (cdr (member value allowed)))))
  10992. (setq nval (or nval (car allowed)))
  10993. (if (equal nval value)
  10994. (error "Only one allowed value for this property"))
  10995. (org-at-property-p)
  10996. (replace-match (concat " :" key ": " nval) t t)
  10997. (org-indent-line-function)
  10998. (beginning-of-line 1)
  10999. (skip-chars-forward " \t")))
  11000. (defun org-find-entry-with-id (ident)
  11001. "Locate the entry that contains the ID property with exact value IDENT.
  11002. IDENT can be a string, a symbol or a number, this function will search for
  11003. the string representation of it.
  11004. Return the position where this entry starts, or nil if there is no such entry."
  11005. (interactive "sID: ")
  11006. (let ((id (cond
  11007. ((stringp ident) ident)
  11008. ((symbol-name ident) (symbol-name ident))
  11009. ((numberp ident) (number-to-string ident))
  11010. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11011. (case-fold-search nil))
  11012. (save-excursion
  11013. (save-restriction
  11014. (widen)
  11015. (goto-char (point-min))
  11016. (when (re-search-forward
  11017. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11018. nil t)
  11019. (org-back-to-heading t)
  11020. (point))))))
  11021. ;;;; Timestamps
  11022. (defvar org-last-changed-timestamp nil)
  11023. (defvar org-last-inserted-timestamp nil
  11024. "The last time stamp inserted with `org-insert-time-stamp'.")
  11025. (defvar org-time-was-given) ; dynamically scoped parameter
  11026. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11027. (defvar org-ts-what) ; dynamically scoped parameter
  11028. (defun org-time-stamp (arg &optional inactive)
  11029. "Prompt for a date/time and insert a time stamp.
  11030. If the user specifies a time like HH:MM, or if this command is called
  11031. with a prefix argument, the time stamp will contain date and time.
  11032. Otherwise, only the date will be included. All parts of a date not
  11033. specified by the user will be filled in from the current date/time.
  11034. So if you press just return without typing anything, the time stamp
  11035. will represent the current date/time. If there is already a timestamp
  11036. at the cursor, it will be modified."
  11037. (interactive "P")
  11038. (let* ((ts nil)
  11039. (default-time
  11040. ;; Default time is either today, or, when entering a range,
  11041. ;; the range start.
  11042. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11043. (save-excursion
  11044. (re-search-backward
  11045. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11046. (- (point) 20) t)))
  11047. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11048. (current-time)))
  11049. (default-input (and ts (org-get-compact-tod ts)))
  11050. org-time-was-given org-end-time-was-given time)
  11051. (cond
  11052. ((and (org-at-timestamp-p t)
  11053. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11054. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11055. (insert "--")
  11056. (setq time (let ((this-command this-command))
  11057. (org-read-date arg 'totime nil nil
  11058. default-time default-input)))
  11059. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11060. ((org-at-timestamp-p t)
  11061. (setq time (let ((this-command this-command))
  11062. (org-read-date arg 'totime nil nil default-time default-input)))
  11063. (when (org-at-timestamp-p t) ; just to get the match data
  11064. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11065. (replace-match "")
  11066. (setq org-last-changed-timestamp
  11067. (org-insert-time-stamp
  11068. time (or org-time-was-given arg)
  11069. inactive nil nil (list org-end-time-was-given))))
  11070. (message "Timestamp updated"))
  11071. (t
  11072. (setq time (let ((this-command this-command))
  11073. (org-read-date arg 'totime nil nil default-time default-input)))
  11074. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11075. nil nil (list org-end-time-was-given))))))
  11076. ;; FIXME: can we use this for something else, like computing time differences?
  11077. (defun org-get-compact-tod (s)
  11078. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11079. (let* ((t1 (match-string 1 s))
  11080. (h1 (string-to-number (match-string 2 s)))
  11081. (m1 (string-to-number (match-string 3 s)))
  11082. (t2 (and (match-end 4) (match-string 5 s)))
  11083. (h2 (and t2 (string-to-number (match-string 6 s))))
  11084. (m2 (and t2 (string-to-number (match-string 7 s))))
  11085. dh dm)
  11086. (if (not t2)
  11087. t1
  11088. (setq dh (- h2 h1) dm (- m2 m1))
  11089. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11090. (concat t1 "+" (number-to-string dh)
  11091. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11092. (defun org-time-stamp-inactive (&optional arg)
  11093. "Insert an inactive time stamp.
  11094. An inactive time stamp is enclosed in square brackets instead of angle
  11095. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11096. does not link to the calendar and cannot be changed with the S-cursor keys.
  11097. So these are more for recording a certain time/date."
  11098. (interactive "P")
  11099. (org-time-stamp arg 'inactive))
  11100. (defvar org-date-ovl (org-make-overlay 1 1))
  11101. (org-overlay-put org-date-ovl 'face 'org-warning)
  11102. (org-detach-overlay org-date-ovl)
  11103. (defvar org-ans1) ; dynamically scoped parameter
  11104. (defvar org-ans2) ; dynamically scoped parameter
  11105. (defvar org-plain-time-of-day-regexp) ; defined below
  11106. (defvar org-overriding-default-time nil) ; dynamically scoped
  11107. (defvar org-read-date-overlay nil)
  11108. (defvar org-dcst nil) ; dynamically scoped
  11109. (defvar org-read-date-history nil)
  11110. (defvar org-read-date-final-answer nil)
  11111. (defun org-read-date (&optional with-time to-time from-string prompt
  11112. default-time default-input)
  11113. "Read a date, possibly a time, and make things smooth for the user.
  11114. The prompt will suggest to enter an ISO date, but you can also enter anything
  11115. which will at least partially be understood by `parse-time-string'.
  11116. Unrecognized parts of the date will default to the current day, month, year,
  11117. hour and minute. If this command is called to replace a timestamp at point,
  11118. of to enter the second timestamp of a range, the default time is taken from the
  11119. existing stamp. For example,
  11120. 3-2-5 --> 2003-02-05
  11121. feb 15 --> currentyear-02-15
  11122. sep 12 9 --> 2009-09-12
  11123. 12:45 --> today 12:45
  11124. 22 sept 0:34 --> currentyear-09-22 0:34
  11125. 12 --> currentyear-currentmonth-12
  11126. Fri --> nearest Friday (today or later)
  11127. etc.
  11128. Furthermore you can specify a relative date by giving, as the *first* thing
  11129. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11130. change in days weeks, months, years.
  11131. With a single plus or minus, the date is relative to today. With a double
  11132. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11133. +4d --> four days from today
  11134. +4 --> same as above
  11135. +2w --> two weeks from today
  11136. ++5 --> five days from default date
  11137. The function understands only English month and weekday abbreviations,
  11138. but this can be configured with the variables `parse-time-months' and
  11139. `parse-time-weekdays'.
  11140. While prompting, a calendar is popped up - you can also select the
  11141. date with the mouse (button 1). The calendar shows a period of three
  11142. months. To scroll it to other months, use the keys `>' and `<'.
  11143. If you don't like the calendar, turn it off with
  11144. \(setq org-read-date-popup-calendar nil)
  11145. With optional argument TO-TIME, the date will immediately be converted
  11146. to an internal time.
  11147. With an optional argument WITH-TIME, the prompt will suggest to also
  11148. insert a time. Note that when WITH-TIME is not set, you can still
  11149. enter a time, and this function will inform the calling routine about
  11150. this change. The calling routine may then choose to change the format
  11151. used to insert the time stamp into the buffer to include the time.
  11152. With optional argument FROM-STRING, read from this string instead from
  11153. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11154. the time/date that is used for everything that is not specified by the
  11155. user."
  11156. (require 'parse-time)
  11157. (let* ((org-time-stamp-rounding-minutes
  11158. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11159. (org-dcst org-display-custom-times)
  11160. (ct (org-current-time))
  11161. (def (or org-overriding-default-time default-time ct))
  11162. (defdecode (decode-time def))
  11163. (dummy (progn
  11164. (when (< (nth 2 defdecode) org-extend-today-until)
  11165. (setcar (nthcdr 2 defdecode) -1)
  11166. (setcar (nthcdr 1 defdecode) 59)
  11167. (setq def (apply 'encode-time defdecode)
  11168. defdecode (decode-time def)))))
  11169. (calendar-frame-setup nil)
  11170. (calendar-move-hook nil)
  11171. (calendar-view-diary-initially-flag nil)
  11172. (view-diary-entries-initially nil)
  11173. (calendar-view-holidays-initially-flag nil)
  11174. (view-calendar-holidays-initially nil)
  11175. (timestr (format-time-string
  11176. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11177. (prompt (concat (if prompt (concat prompt " ") "")
  11178. (format "Date+time [%s]: " timestr)))
  11179. ans (org-ans0 "") org-ans1 org-ans2 final)
  11180. (cond
  11181. (from-string (setq ans from-string))
  11182. (org-read-date-popup-calendar
  11183. (save-excursion
  11184. (save-window-excursion
  11185. (calendar)
  11186. (calendar-forward-day (- (time-to-days def)
  11187. (calendar-absolute-from-gregorian
  11188. (calendar-current-date))))
  11189. (org-eval-in-calendar nil t)
  11190. (let* ((old-map (current-local-map))
  11191. (map (copy-keymap calendar-mode-map))
  11192. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11193. (org-defkey map (kbd "RET") 'org-calendar-select)
  11194. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11195. 'org-calendar-select-mouse)
  11196. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11197. 'org-calendar-select-mouse)
  11198. (org-defkey minibuffer-local-map [(meta shift left)]
  11199. (lambda () (interactive)
  11200. (org-eval-in-calendar '(calendar-backward-month 1))))
  11201. (org-defkey minibuffer-local-map [(meta shift right)]
  11202. (lambda () (interactive)
  11203. (org-eval-in-calendar '(calendar-forward-month 1))))
  11204. (org-defkey minibuffer-local-map [(meta shift up)]
  11205. (lambda () (interactive)
  11206. (org-eval-in-calendar '(calendar-backward-year 1))))
  11207. (org-defkey minibuffer-local-map [(meta shift down)]
  11208. (lambda () (interactive)
  11209. (org-eval-in-calendar '(calendar-forward-year 1))))
  11210. (org-defkey minibuffer-local-map [?\e (shift left)]
  11211. (lambda () (interactive)
  11212. (org-eval-in-calendar '(calendar-backward-month 1))))
  11213. (org-defkey minibuffer-local-map [?\e (shift right)]
  11214. (lambda () (interactive)
  11215. (org-eval-in-calendar '(calendar-forward-month 1))))
  11216. (org-defkey minibuffer-local-map [?\e (shift up)]
  11217. (lambda () (interactive)
  11218. (org-eval-in-calendar '(calendar-backward-year 1))))
  11219. (org-defkey minibuffer-local-map [?\e (shift down)]
  11220. (lambda () (interactive)
  11221. (org-eval-in-calendar '(calendar-forward-year 1))))
  11222. (org-defkey minibuffer-local-map [(shift up)]
  11223. (lambda () (interactive)
  11224. (org-eval-in-calendar '(calendar-backward-week 1))))
  11225. (org-defkey minibuffer-local-map [(shift down)]
  11226. (lambda () (interactive)
  11227. (org-eval-in-calendar '(calendar-forward-week 1))))
  11228. (org-defkey minibuffer-local-map [(shift left)]
  11229. (lambda () (interactive)
  11230. (org-eval-in-calendar '(calendar-backward-day 1))))
  11231. (org-defkey minibuffer-local-map [(shift right)]
  11232. (lambda () (interactive)
  11233. (org-eval-in-calendar '(calendar-forward-day 1))))
  11234. (org-defkey minibuffer-local-map ">"
  11235. (lambda () (interactive)
  11236. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11237. (org-defkey minibuffer-local-map "<"
  11238. (lambda () (interactive)
  11239. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11240. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11241. (unwind-protect
  11242. (progn
  11243. (use-local-map map)
  11244. (add-hook 'post-command-hook 'org-read-date-display)
  11245. (setq org-ans0 (read-string prompt default-input
  11246. 'org-read-date-history nil))
  11247. ;; org-ans0: from prompt
  11248. ;; org-ans1: from mouse click
  11249. ;; org-ans2: from calendar motion
  11250. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11251. (remove-hook 'post-command-hook 'org-read-date-display)
  11252. (use-local-map old-map)
  11253. (when org-read-date-overlay
  11254. (org-delete-overlay org-read-date-overlay)
  11255. (setq org-read-date-overlay nil)))))))
  11256. (t ; Naked prompt only
  11257. (unwind-protect
  11258. (setq ans (read-string prompt default-input
  11259. 'org-read-date-history timestr))
  11260. (when org-read-date-overlay
  11261. (org-delete-overlay org-read-date-overlay)
  11262. (setq org-read-date-overlay nil)))))
  11263. (setq final (org-read-date-analyze ans def defdecode))
  11264. (setq org-read-date-final-answer ans)
  11265. (if to-time
  11266. (apply 'encode-time final)
  11267. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11268. (format "%04d-%02d-%02d %02d:%02d"
  11269. (nth 5 final) (nth 4 final) (nth 3 final)
  11270. (nth 2 final) (nth 1 final))
  11271. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11272. (defvar def)
  11273. (defvar defdecode)
  11274. (defvar with-time)
  11275. (defun org-read-date-display ()
  11276. "Display the current date prompt interpretation in the minibuffer."
  11277. (when org-read-date-display-live
  11278. (when org-read-date-overlay
  11279. (org-delete-overlay org-read-date-overlay))
  11280. (let ((p (point)))
  11281. (end-of-line 1)
  11282. (while (not (equal (buffer-substring
  11283. (max (point-min) (- (point) 4)) (point))
  11284. " "))
  11285. (insert " "))
  11286. (goto-char p))
  11287. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11288. " " (or org-ans1 org-ans2)))
  11289. (org-end-time-was-given nil)
  11290. (f (org-read-date-analyze ans def defdecode))
  11291. (fmts (if org-dcst
  11292. org-time-stamp-custom-formats
  11293. org-time-stamp-formats))
  11294. (fmt (if (or with-time
  11295. (and (boundp 'org-time-was-given) org-time-was-given))
  11296. (cdr fmts)
  11297. (car fmts)))
  11298. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11299. (when (and org-end-time-was-given
  11300. (string-match org-plain-time-of-day-regexp txt))
  11301. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11302. org-end-time-was-given
  11303. (substring txt (match-end 0)))))
  11304. (setq org-read-date-overlay
  11305. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11306. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11307. (defun org-read-date-analyze (ans def defdecode)
  11308. "Analyse the combined answer of the date prompt."
  11309. ;; FIXME: cleanup and comment
  11310. (let (delta deltan deltaw deltadef year month day
  11311. hour minute second wday pm h2 m2 tl wday1
  11312. iso-year iso-weekday iso-week iso-year iso-date)
  11313. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11314. (setq ans "+0"))
  11315. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11316. (setq ans (replace-match "" t t ans)
  11317. deltan (car delta)
  11318. deltaw (nth 1 delta)
  11319. deltadef (nth 2 delta)))
  11320. ;; Check if there is an iso week date in there
  11321. ;; If yes, sore the info and postpone interpreting it until the rest
  11322. ;; of the parsing is done
  11323. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11324. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11325. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11326. iso-week (string-to-number (match-string 2 ans)))
  11327. (setq ans (replace-match "" t t ans)))
  11328. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11329. (when (string-match
  11330. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11331. (setq year (if (match-end 2)
  11332. (string-to-number (match-string 2 ans))
  11333. (string-to-number (format-time-string "%Y")))
  11334. month (string-to-number (match-string 3 ans))
  11335. day (string-to-number (match-string 4 ans)))
  11336. (if (< year 100) (setq year (+ 2000 year)))
  11337. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11338. t nil ans)))
  11339. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11340. ;; If there is a time with am/pm, and *no* time without it, we convert
  11341. ;; so that matching will be successful.
  11342. (loop for i from 1 to 2 do ; twice, for end time as well
  11343. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11344. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11345. (setq hour (string-to-number (match-string 1 ans))
  11346. minute (if (match-end 3)
  11347. (string-to-number (match-string 3 ans))
  11348. 0)
  11349. pm (equal ?p
  11350. (string-to-char (downcase (match-string 4 ans)))))
  11351. (if (and (= hour 12) (not pm))
  11352. (setq hour 0)
  11353. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11354. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11355. t t ans))))
  11356. ;; Check if a time range is given as a duration
  11357. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11358. (setq hour (string-to-number (match-string 1 ans))
  11359. h2 (+ hour (string-to-number (match-string 3 ans)))
  11360. minute (string-to-number (match-string 2 ans))
  11361. m2 (+ minute (if (match-end 5) (string-to-number
  11362. (match-string 5 ans))0)))
  11363. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11364. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11365. t t ans)))
  11366. ;; Check if there is a time range
  11367. (when (boundp 'org-end-time-was-given)
  11368. (setq org-time-was-given nil)
  11369. (when (and (string-match org-plain-time-of-day-regexp ans)
  11370. (match-end 8))
  11371. (setq org-end-time-was-given (match-string 8 ans))
  11372. (setq ans (concat (substring ans 0 (match-beginning 7))
  11373. (substring ans (match-end 7))))))
  11374. (setq tl (parse-time-string ans)
  11375. day (or (nth 3 tl) (nth 3 defdecode))
  11376. month (or (nth 4 tl)
  11377. (if (and org-read-date-prefer-future
  11378. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11379. (1+ (nth 4 defdecode))
  11380. (nth 4 defdecode)))
  11381. year (or (nth 5 tl)
  11382. (if (and org-read-date-prefer-future
  11383. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11384. (1+ (nth 5 defdecode))
  11385. (nth 5 defdecode)))
  11386. hour (or (nth 2 tl) (nth 2 defdecode))
  11387. minute (or (nth 1 tl) (nth 1 defdecode))
  11388. second (or (nth 0 tl) 0)
  11389. wday (nth 6 tl))
  11390. ;; Special date definitions below
  11391. (cond
  11392. (iso-week
  11393. ;; There was an iso week
  11394. (setq year (or iso-year year)
  11395. day (or iso-weekday wday 1)
  11396. wday nil ; to make sure that the trigger below does not match
  11397. iso-date (calendar-gregorian-from-absolute
  11398. (calendar-absolute-from-iso
  11399. (list iso-week day year))))
  11400. ; FIXME: Should we also push ISO weeks into the future?
  11401. ; (when (and org-read-date-prefer-future
  11402. ; (not iso-year)
  11403. ; (< (calendar-absolute-from-gregorian iso-date)
  11404. ; (time-to-days (current-time))))
  11405. ; (setq year (1+ year)
  11406. ; iso-date (calendar-gregorian-from-absolute
  11407. ; (calendar-absolute-from-iso
  11408. ; (list iso-week day year)))))
  11409. (setq month (car iso-date)
  11410. year (nth 2 iso-date)
  11411. day (nth 1 iso-date)))
  11412. (deltan
  11413. (unless deltadef
  11414. (let ((now (decode-time (current-time))))
  11415. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11416. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11417. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11418. ((equal deltaw "m") (setq month (+ month deltan)))
  11419. ((equal deltaw "y") (setq year (+ year deltan)))))
  11420. ((and wday (not (nth 3 tl)))
  11421. ;; Weekday was given, but no day, so pick that day in the week
  11422. ;; on or after the derived date.
  11423. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11424. (unless (equal wday wday1)
  11425. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11426. (if (and (boundp 'org-time-was-given)
  11427. (nth 2 tl))
  11428. (setq org-time-was-given t))
  11429. (if (< year 100) (setq year (+ 2000 year)))
  11430. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11431. (list second minute hour day month year)))
  11432. (defvar parse-time-weekdays)
  11433. (defun org-read-date-get-relative (s today default)
  11434. "Check string S for special relative date string.
  11435. TODAY and DEFAULT are internal times, for today and for a default.
  11436. Return shift list (N what def-flag)
  11437. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11438. N is the number of WHATs to shift.
  11439. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11440. the DEFAULT date rather than TODAY."
  11441. (when (and
  11442. (string-match
  11443. (concat
  11444. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11445. "\\([0-9]+\\)?"
  11446. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11447. "\\([ \t]\\|$\\)") s)
  11448. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11449. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11450. (string-to-char (substring (match-string 1 s) -1))
  11451. ?+))
  11452. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11453. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11454. (what (if (match-end 3) (match-string 3 s) "d"))
  11455. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11456. (date (if rel default today))
  11457. (wday (nth 6 (decode-time date)))
  11458. delta)
  11459. (if wday1
  11460. (progn
  11461. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11462. (if (= dir ?-) (setq delta (- delta 7)))
  11463. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11464. (list delta "d" rel))
  11465. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11466. (defun org-eval-in-calendar (form &optional keepdate)
  11467. "Eval FORM in the calendar window and return to current window.
  11468. Also, store the cursor date in variable org-ans2."
  11469. (let ((sf (selected-frame))
  11470. (sw (selected-window)))
  11471. (select-window (get-buffer-window "*Calendar*" t))
  11472. (eval form)
  11473. (when (and (not keepdate) (calendar-cursor-to-date))
  11474. (let* ((date (calendar-cursor-to-date))
  11475. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11476. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11477. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11478. (select-window sw)
  11479. (select-frame-set-input-focus sf)))
  11480. (defun org-calendar-select ()
  11481. "Return to `org-read-date' with the date currently selected.
  11482. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11483. (interactive)
  11484. (when (calendar-cursor-to-date)
  11485. (let* ((date (calendar-cursor-to-date))
  11486. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11487. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11488. (if (active-minibuffer-window) (exit-minibuffer))))
  11489. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11490. "Insert a date stamp for the date given by the internal TIME.
  11491. WITH-HM means, use the stamp format that includes the time of the day.
  11492. INACTIVE means use square brackets instead of angular ones, so that the
  11493. stamp will not contribute to the agenda.
  11494. PRE and POST are optional strings to be inserted before and after the
  11495. stamp.
  11496. The command returns the inserted time stamp."
  11497. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11498. stamp)
  11499. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11500. (insert-before-markers (or pre ""))
  11501. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11502. (when (listp extra)
  11503. (setq extra (car extra))
  11504. (if (and (stringp extra)
  11505. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11506. (setq extra (format "-%02d:%02d"
  11507. (string-to-number (match-string 1 extra))
  11508. (string-to-number (match-string 2 extra))))
  11509. (setq extra nil)))
  11510. (when extra
  11511. (backward-char 1)
  11512. (insert-before-markers extra)
  11513. (forward-char 1))
  11514. (insert-before-markers (or post ""))
  11515. (setq org-last-inserted-timestamp stamp)))
  11516. (defun org-toggle-time-stamp-overlays ()
  11517. "Toggle the use of custom time stamp formats."
  11518. (interactive)
  11519. (setq org-display-custom-times (not org-display-custom-times))
  11520. (unless org-display-custom-times
  11521. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11522. (while (setq p (next-single-property-change p 'display))
  11523. (if (and (get-text-property p 'display)
  11524. (eq (get-text-property p 'face) 'org-date))
  11525. (remove-text-properties
  11526. p (setq p (next-single-property-change p 'display))
  11527. '(display t))))
  11528. (set-buffer-modified-p bmp)))
  11529. (if (featurep 'xemacs)
  11530. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11531. (org-restart-font-lock)
  11532. (setq org-table-may-need-update t)
  11533. (if org-display-custom-times
  11534. (message "Time stamps are overlayed with custom format")
  11535. (message "Time stamp overlays removed")))
  11536. (defun org-display-custom-time (beg end)
  11537. "Overlay modified time stamp format over timestamp between BEG and END."
  11538. (let* ((ts (buffer-substring beg end))
  11539. t1 w1 with-hm tf time str w2 (off 0))
  11540. (save-match-data
  11541. (setq t1 (org-parse-time-string ts t))
  11542. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11543. (setq off (- (match-end 0) (match-beginning 0)))))
  11544. (setq end (- end off))
  11545. (setq w1 (- end beg)
  11546. with-hm (and (nth 1 t1) (nth 2 t1))
  11547. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11548. time (org-fix-decoded-time t1)
  11549. str (org-add-props
  11550. (format-time-string
  11551. (substring tf 1 -1) (apply 'encode-time time))
  11552. nil 'mouse-face 'highlight)
  11553. w2 (length str))
  11554. (if (not (= w2 w1))
  11555. (add-text-properties (1+ beg) (+ 2 beg)
  11556. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11557. (if (featurep 'xemacs)
  11558. (progn
  11559. (put-text-property beg end 'invisible t)
  11560. (put-text-property beg end 'end-glyph (make-glyph str)))
  11561. (put-text-property beg end 'display str))))
  11562. (defun org-translate-time (string)
  11563. "Translate all timestamps in STRING to custom format.
  11564. But do this only if the variable `org-display-custom-times' is set."
  11565. (when org-display-custom-times
  11566. (save-match-data
  11567. (let* ((start 0)
  11568. (re org-ts-regexp-both)
  11569. t1 with-hm inactive tf time str beg end)
  11570. (while (setq start (string-match re string start))
  11571. (setq beg (match-beginning 0)
  11572. end (match-end 0)
  11573. t1 (save-match-data
  11574. (org-parse-time-string (substring string beg end) t))
  11575. with-hm (and (nth 1 t1) (nth 2 t1))
  11576. inactive (equal (substring string beg (1+ beg)) "[")
  11577. tf (funcall (if with-hm 'cdr 'car)
  11578. org-time-stamp-custom-formats)
  11579. time (org-fix-decoded-time t1)
  11580. str (format-time-string
  11581. (concat
  11582. (if inactive "[" "<") (substring tf 1 -1)
  11583. (if inactive "]" ">"))
  11584. (apply 'encode-time time))
  11585. string (replace-match str t t string)
  11586. start (+ start (length str)))))))
  11587. string)
  11588. (defun org-fix-decoded-time (time)
  11589. "Set 0 instead of nil for the first 6 elements of time.
  11590. Don't touch the rest."
  11591. (let ((n 0))
  11592. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11593. (defun org-days-to-time (timestamp-string)
  11594. "Difference between TIMESTAMP-STRING and now in days."
  11595. (- (time-to-days (org-time-string-to-time timestamp-string))
  11596. (time-to-days (current-time))))
  11597. (defun org-deadline-close (timestamp-string &optional ndays)
  11598. "Is the time in TIMESTAMP-STRING close to the current date?"
  11599. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11600. (and (< (org-days-to-time timestamp-string) ndays)
  11601. (not (org-entry-is-done-p))))
  11602. (defun org-get-wdays (ts)
  11603. "Get the deadline lead time appropriate for timestring TS."
  11604. (cond
  11605. ((<= org-deadline-warning-days 0)
  11606. ;; 0 or negative, enforce this value no matter what
  11607. (- org-deadline-warning-days))
  11608. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11609. ;; lead time is specified.
  11610. (floor (* (string-to-number (match-string 1 ts))
  11611. (cdr (assoc (match-string 2 ts)
  11612. '(("d" . 1) ("w" . 7)
  11613. ("m" . 30.4) ("y" . 365.25)))))))
  11614. ;; go for the default.
  11615. (t org-deadline-warning-days)))
  11616. (defun org-calendar-select-mouse (ev)
  11617. "Return to `org-read-date' with the date currently selected.
  11618. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11619. (interactive "e")
  11620. (mouse-set-point ev)
  11621. (when (calendar-cursor-to-date)
  11622. (let* ((date (calendar-cursor-to-date))
  11623. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11624. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11625. (if (active-minibuffer-window) (exit-minibuffer))))
  11626. (defun org-check-deadlines (ndays)
  11627. "Check if there are any deadlines due or past due.
  11628. A deadline is considered due if it happens within `org-deadline-warning-days'
  11629. days from today's date. If the deadline appears in an entry marked DONE,
  11630. it is not shown. The prefix arg NDAYS can be used to test that many
  11631. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11632. (interactive "P")
  11633. (let* ((org-warn-days
  11634. (cond
  11635. ((equal ndays '(4)) 100000)
  11636. (ndays (prefix-numeric-value ndays))
  11637. (t (abs org-deadline-warning-days))))
  11638. (case-fold-search nil)
  11639. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11640. (callback
  11641. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11642. (message "%d deadlines past-due or due within %d days"
  11643. (org-occur regexp nil callback)
  11644. org-warn-days)))
  11645. (defun org-check-before-date (date)
  11646. "Check if there are deadlines or scheduled entries before DATE."
  11647. (interactive (list (org-read-date)))
  11648. (let ((case-fold-search nil)
  11649. (regexp (concat "\\<\\(" org-deadline-string
  11650. "\\|" org-scheduled-string
  11651. "\\) *<\\([^>]+\\)>"))
  11652. (callback
  11653. (lambda () (time-less-p
  11654. (org-time-string-to-time (match-string 2))
  11655. (org-time-string-to-time date)))))
  11656. (message "%d entries before %s"
  11657. (org-occur regexp nil callback) date)))
  11658. (defun org-check-after-date (date)
  11659. "Check if there are deadlines or scheduled entries after DATE."
  11660. (interactive (list (org-read-date)))
  11661. (let ((case-fold-search nil)
  11662. (regexp (concat "\\<\\(" org-deadline-string
  11663. "\\|" org-scheduled-string
  11664. "\\) *<\\([^>]+\\)>"))
  11665. (callback
  11666. (lambda () (not
  11667. (time-less-p
  11668. (org-time-string-to-time (match-string 2))
  11669. (org-time-string-to-time date))))))
  11670. (message "%d entries after %s"
  11671. (org-occur regexp nil callback) date)))
  11672. (defun org-evaluate-time-range (&optional to-buffer)
  11673. "Evaluate a time range by computing the difference between start and end.
  11674. Normally the result is just printed in the echo area, but with prefix arg
  11675. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11676. If the time range is actually in a table, the result is inserted into the
  11677. next column.
  11678. For time difference computation, a year is assumed to be exactly 365
  11679. days in order to avoid rounding problems."
  11680. (interactive "P")
  11681. (or
  11682. (org-clock-update-time-maybe)
  11683. (save-excursion
  11684. (unless (org-at-date-range-p t)
  11685. (goto-char (point-at-bol))
  11686. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11687. (if (not (org-at-date-range-p t))
  11688. (error "Not at a time-stamp range, and none found in current line")))
  11689. (let* ((ts1 (match-string 1))
  11690. (ts2 (match-string 2))
  11691. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11692. (match-end (match-end 0))
  11693. (time1 (org-time-string-to-time ts1))
  11694. (time2 (org-time-string-to-time ts2))
  11695. (t1 (time-to-seconds time1))
  11696. (t2 (time-to-seconds time2))
  11697. (diff (abs (- t2 t1)))
  11698. (negative (< (- t2 t1) 0))
  11699. ;; (ys (floor (* 365 24 60 60)))
  11700. (ds (* 24 60 60))
  11701. (hs (* 60 60))
  11702. (fy "%dy %dd %02d:%02d")
  11703. (fy1 "%dy %dd")
  11704. (fd "%dd %02d:%02d")
  11705. (fd1 "%dd")
  11706. (fh "%02d:%02d")
  11707. y d h m align)
  11708. (if havetime
  11709. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11710. y 0
  11711. d (floor (/ diff ds)) diff (mod diff ds)
  11712. h (floor (/ diff hs)) diff (mod diff hs)
  11713. m (floor (/ diff 60)))
  11714. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11715. y 0
  11716. d (floor (+ (/ diff ds) 0.5))
  11717. h 0 m 0))
  11718. (if (not to-buffer)
  11719. (message "%s" (org-make-tdiff-string y d h m))
  11720. (if (org-at-table-p)
  11721. (progn
  11722. (goto-char match-end)
  11723. (setq align t)
  11724. (and (looking-at " *|") (goto-char (match-end 0))))
  11725. (goto-char match-end))
  11726. (if (looking-at
  11727. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11728. (replace-match ""))
  11729. (if negative (insert " -"))
  11730. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11731. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11732. (insert " " (format fh h m))))
  11733. (if align (org-table-align))
  11734. (message "Time difference inserted")))))
  11735. (defun org-make-tdiff-string (y d h m)
  11736. (let ((fmt "")
  11737. (l nil))
  11738. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11739. l (push y l)))
  11740. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11741. l (push d l)))
  11742. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11743. l (push h l)))
  11744. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11745. l (push m l)))
  11746. (apply 'format fmt (nreverse l))))
  11747. (defun org-time-string-to-time (s)
  11748. (apply 'encode-time (org-parse-time-string s)))
  11749. (defun org-time-string-to-seconds (s)
  11750. (time-to-seconds (org-time-string-to-time s)))
  11751. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11752. "Convert a time stamp to an absolute day number.
  11753. If there is a specifyer for a cyclic time stamp, get the closest date to
  11754. DAYNR.
  11755. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11756. the variable date is bound by the calendar when this is called."
  11757. (cond
  11758. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11759. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11760. daynr
  11761. (+ daynr 1000)))
  11762. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11763. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11764. (time-to-days (current-time))) (match-string 0 s)
  11765. prefer show-all))
  11766. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11767. (defun org-days-to-iso-week (days)
  11768. "Return the iso week number."
  11769. (require 'cal-iso)
  11770. (car (calendar-iso-from-absolute days)))
  11771. (defun org-small-year-to-year (year)
  11772. "Convert 2-digit years into 4-digit years.
  11773. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11774. The year 2000 cannot be abbreviated. Any year larger than 99
  11775. is returned unchanged."
  11776. (if (< year 38)
  11777. (setq year (+ 2000 year))
  11778. (if (< year 100)
  11779. (setq year (+ 1900 year))))
  11780. year)
  11781. (defun org-time-from-absolute (d)
  11782. "Return the time corresponding to date D.
  11783. D may be an absolute day number, or a calendar-type list (month day year)."
  11784. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11785. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11786. (defun org-calendar-holiday ()
  11787. "List of holidays, for Diary display in Org-mode."
  11788. (require 'holidays)
  11789. (let ((hl (funcall
  11790. (if (fboundp 'calendar-check-holidays)
  11791. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11792. (if hl (mapconcat 'identity hl "; "))))
  11793. (defun org-diary-sexp-entry (sexp entry date)
  11794. "Process a SEXP diary ENTRY for DATE."
  11795. (require 'diary-lib)
  11796. (let ((result (if calendar-debug-sexp
  11797. (let ((stack-trace-on-error t))
  11798. (eval (car (read-from-string sexp))))
  11799. (condition-case nil
  11800. (eval (car (read-from-string sexp)))
  11801. (error
  11802. (beep)
  11803. (message "Bad sexp at line %d in %s: %s"
  11804. (org-current-line)
  11805. (buffer-file-name) sexp)
  11806. (sleep-for 2))))))
  11807. (cond ((stringp result) result)
  11808. ((and (consp result)
  11809. (stringp (cdr result))) (cdr result))
  11810. (result entry)
  11811. (t nil))))
  11812. (defun org-diary-to-ical-string (frombuf)
  11813. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11814. This uses the icalendar.el library."
  11815. (let* ((tmpdir (if (featurep 'xemacs)
  11816. (temp-directory)
  11817. temporary-file-directory))
  11818. (tmpfile (make-temp-name
  11819. (expand-file-name "orgics" tmpdir)))
  11820. buf rtn b e)
  11821. (save-excursion
  11822. (set-buffer frombuf)
  11823. (icalendar-export-region (point-min) (point-max) tmpfile)
  11824. (setq buf (find-buffer-visiting tmpfile))
  11825. (set-buffer buf)
  11826. (goto-char (point-min))
  11827. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11828. (setq b (match-beginning 0)))
  11829. (goto-char (point-max))
  11830. (if (re-search-backward "^END:VEVENT" nil t)
  11831. (setq e (match-end 0)))
  11832. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11833. (kill-buffer buf)
  11834. (delete-file tmpfile)
  11835. rtn))
  11836. (defun org-closest-date (start current change prefer show-all)
  11837. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11838. When PREFER is `past' return a date that is either CURRENT or past.
  11839. When PREFER is `future', return a date that is either CURRENT or future.
  11840. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11841. ;; Make the proper lists from the dates
  11842. (catch 'exit
  11843. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11844. dn dw sday cday n1 n2 n0
  11845. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11846. (setq start (org-date-to-gregorian start)
  11847. current (org-date-to-gregorian
  11848. (if show-all
  11849. current
  11850. (time-to-days (current-time))))
  11851. sday (calendar-absolute-from-gregorian start)
  11852. cday (calendar-absolute-from-gregorian current))
  11853. (if (<= cday sday) (throw 'exit sday))
  11854. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11855. (setq dn (string-to-number (match-string 1 change))
  11856. dw (cdr (assoc (match-string 2 change) a1)))
  11857. (error "Invalid change specifyer: %s" change))
  11858. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11859. (cond
  11860. ((eq dw 'day)
  11861. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11862. n2 (+ n1 dn)))
  11863. ((eq dw 'year)
  11864. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11865. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11866. (setq date1 (list m d y1)
  11867. n1 (calendar-absolute-from-gregorian date1)
  11868. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11869. n2 (calendar-absolute-from-gregorian date2)))
  11870. ((eq dw 'month)
  11871. ;; approx number of month between the two dates
  11872. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11873. ;; How often does dn fit in there?
  11874. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11875. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11876. m (+ m nm)
  11877. ny (floor (/ m 12))
  11878. y (+ y ny)
  11879. m (- m (* ny 12)))
  11880. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11881. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11882. (setq m2 (+ m dn) y2 y)
  11883. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11884. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11885. (while (<= n2 cday)
  11886. (setq n1 n2 m m2 y y2)
  11887. (setq m2 (+ m dn) y2 y)
  11888. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11889. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11890. ;; Make sure n1 is the earlier date
  11891. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11892. (if show-all
  11893. (cond
  11894. ((eq prefer 'past) n1)
  11895. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11896. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11897. (cond
  11898. ((eq prefer 'past) n1)
  11899. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11900. (t (if (= cday n1) n1 n2)))))))
  11901. (defun org-date-to-gregorian (date)
  11902. "Turn any specification of DATE into a gregorian date for the calendar."
  11903. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11904. ((and (listp date) (= (length date) 3)) date)
  11905. ((stringp date)
  11906. (setq date (org-parse-time-string date))
  11907. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11908. ((listp date)
  11909. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11910. (defun org-parse-time-string (s &optional nodefault)
  11911. "Parse the standard Org-mode time string.
  11912. This should be a lot faster than the normal `parse-time-string'.
  11913. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11914. hour and minute fields will be nil if not given."
  11915. (if (string-match org-ts-regexp0 s)
  11916. (list 0
  11917. (if (or (match-beginning 8) (not nodefault))
  11918. (string-to-number (or (match-string 8 s) "0")))
  11919. (if (or (match-beginning 7) (not nodefault))
  11920. (string-to-number (or (match-string 7 s) "0")))
  11921. (string-to-number (match-string 4 s))
  11922. (string-to-number (match-string 3 s))
  11923. (string-to-number (match-string 2 s))
  11924. nil nil nil)
  11925. (make-list 9 0)))
  11926. (defun org-timestamp-up (&optional arg)
  11927. "Increase the date item at the cursor by one.
  11928. If the cursor is on the year, change the year. If it is on the month or
  11929. the day, change that.
  11930. With prefix ARG, change by that many units."
  11931. (interactive "p")
  11932. (org-timestamp-change (prefix-numeric-value arg)))
  11933. (defun org-timestamp-down (&optional arg)
  11934. "Decrease the date item at the cursor by one.
  11935. If the cursor is on the year, change the year. If it is on the month or
  11936. the day, change that.
  11937. With prefix ARG, change by that many units."
  11938. (interactive "p")
  11939. (org-timestamp-change (- (prefix-numeric-value arg))))
  11940. (defun org-timestamp-up-day (&optional arg)
  11941. "Increase the date in the time stamp by one day.
  11942. With prefix ARG, change that many days."
  11943. (interactive "p")
  11944. (if (and (not (org-at-timestamp-p t))
  11945. (org-on-heading-p))
  11946. (org-todo 'up)
  11947. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11948. (defun org-timestamp-down-day (&optional arg)
  11949. "Decrease the date in the time stamp by one day.
  11950. With prefix ARG, change that many days."
  11951. (interactive "p")
  11952. (if (and (not (org-at-timestamp-p t))
  11953. (org-on-heading-p))
  11954. (org-todo 'down)
  11955. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11956. (defun org-at-timestamp-p (&optional inactive-ok)
  11957. "Determine if the cursor is in or at a timestamp."
  11958. (interactive)
  11959. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11960. (pos (point))
  11961. (ans (or (looking-at tsr)
  11962. (save-excursion
  11963. (skip-chars-backward "^[<\n\r\t")
  11964. (if (> (point) (point-min)) (backward-char 1))
  11965. (and (looking-at tsr)
  11966. (> (- (match-end 0) pos) -1))))))
  11967. (and ans
  11968. (boundp 'org-ts-what)
  11969. (setq org-ts-what
  11970. (cond
  11971. ((= pos (match-beginning 0)) 'bracket)
  11972. ((= pos (1- (match-end 0))) 'bracket)
  11973. ((org-pos-in-match-range pos 2) 'year)
  11974. ((org-pos-in-match-range pos 3) 'month)
  11975. ((org-pos-in-match-range pos 7) 'hour)
  11976. ((org-pos-in-match-range pos 8) 'minute)
  11977. ((or (org-pos-in-match-range pos 4)
  11978. (org-pos-in-match-range pos 5)) 'day)
  11979. ((and (> pos (or (match-end 8) (match-end 5)))
  11980. (< pos (match-end 0)))
  11981. (- pos (or (match-end 8) (match-end 5))))
  11982. (t 'day))))
  11983. ans))
  11984. (defun org-toggle-timestamp-type ()
  11985. "Toggle the type (<active> or [inactive]) of a time stamp."
  11986. (interactive)
  11987. (when (org-at-timestamp-p t)
  11988. (let ((beg (match-beginning 0)) (end (match-end 0))
  11989. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11990. (save-excursion
  11991. (goto-char beg)
  11992. (while (re-search-forward "[][<>]" end t)
  11993. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11994. t t)))
  11995. (message "Timestamp is now %sactive"
  11996. (if (equal (char-after beg) ?<) "" "in")))))
  11997. (defun org-timestamp-change (n &optional what)
  11998. "Change the date in the time stamp at point.
  11999. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12000. `year', `minute', `second'. If WHAT is not given, the cursor position
  12001. in the timestamp determines what will be changed."
  12002. (let ((pos (point))
  12003. with-hm inactive
  12004. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12005. org-ts-what
  12006. extra rem
  12007. ts time time0)
  12008. (if (not (org-at-timestamp-p t))
  12009. (error "Not at a timestamp"))
  12010. (if (and (not what) (eq org-ts-what 'bracket))
  12011. (org-toggle-timestamp-type)
  12012. (if (and (not what) (not (eq org-ts-what 'day))
  12013. org-display-custom-times
  12014. (get-text-property (point) 'display)
  12015. (not (get-text-property (1- (point)) 'display)))
  12016. (setq org-ts-what 'day))
  12017. (setq org-ts-what (or what org-ts-what)
  12018. inactive (= (char-after (match-beginning 0)) ?\[)
  12019. ts (match-string 0))
  12020. (replace-match "")
  12021. (if (string-match
  12022. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12023. ts)
  12024. (setq extra (match-string 1 ts)))
  12025. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12026. (setq with-hm t))
  12027. (setq time0 (org-parse-time-string ts))
  12028. (when (and (eq org-ts-what 'minute)
  12029. (eq current-prefix-arg nil))
  12030. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12031. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12032. (setcar (cdr time0) (+ (nth 1 time0)
  12033. (if (> n 0) (- rem) (- dm rem))))))
  12034. (setq time
  12035. (encode-time (or (car time0) 0)
  12036. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12037. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12038. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12039. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12040. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12041. (nthcdr 6 time0)))
  12042. (when (and (member org-ts-what '(hour minute))
  12043. extra
  12044. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12045. (setq extra (org-modify-ts-extra
  12046. extra
  12047. (if (eq org-ts-what 'hour) 2 5)
  12048. n dm)))
  12049. (when (integerp org-ts-what)
  12050. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12051. (if (eq what 'calendar)
  12052. (let ((cal-date (org-get-date-from-calendar)))
  12053. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12054. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12055. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12056. (setcar time0 (or (car time0) 0))
  12057. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12058. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12059. (setq time (apply 'encode-time time0))))
  12060. (setq org-last-changed-timestamp
  12061. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12062. (org-clock-update-time-maybe)
  12063. (goto-char pos)
  12064. ;; Try to recenter the calendar window, if any
  12065. (if (and org-calendar-follow-timestamp-change
  12066. (get-buffer-window "*Calendar*" t)
  12067. (memq org-ts-what '(day month year)))
  12068. (org-recenter-calendar (time-to-days time))))))
  12069. (defun org-modify-ts-extra (s pos n dm)
  12070. "Change the different parts of the lead-time and repeat fields in timestamp."
  12071. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12072. ng h m new rem)
  12073. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12074. (cond
  12075. ((or (org-pos-in-match-range pos 2)
  12076. (org-pos-in-match-range pos 3))
  12077. (setq m (string-to-number (match-string 3 s))
  12078. h (string-to-number (match-string 2 s)))
  12079. (if (org-pos-in-match-range pos 2)
  12080. (setq h (+ h n))
  12081. (setq n (* dm (org-no-warnings (signum n))))
  12082. (when (not (= 0 (setq rem (% m dm))))
  12083. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12084. (setq m (+ m n)))
  12085. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12086. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12087. (setq h (min 24 (max 0 h)))
  12088. (setq ng 1 new (format "-%02d:%02d" h m)))
  12089. ((org-pos-in-match-range pos 6)
  12090. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12091. ((org-pos-in-match-range pos 5)
  12092. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12093. ((org-pos-in-match-range pos 9)
  12094. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12095. ((org-pos-in-match-range pos 8)
  12096. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12097. (when ng
  12098. (setq s (concat
  12099. (substring s 0 (match-beginning ng))
  12100. new
  12101. (substring s (match-end ng))))))
  12102. s))
  12103. (defun org-recenter-calendar (date)
  12104. "If the calendar is visible, recenter it to DATE."
  12105. (let* ((win (selected-window))
  12106. (cwin (get-buffer-window "*Calendar*" t))
  12107. (calendar-move-hook nil))
  12108. (when cwin
  12109. (select-window cwin)
  12110. (calendar-goto-date (if (listp date) date
  12111. (calendar-gregorian-from-absolute date)))
  12112. (select-window win))))
  12113. (defun org-goto-calendar (&optional arg)
  12114. "Go to the Emacs calendar at the current date.
  12115. If there is a time stamp in the current line, go to that date.
  12116. A prefix ARG can be used to force the current date."
  12117. (interactive "P")
  12118. (let ((tsr org-ts-regexp) diff
  12119. (calendar-move-hook nil)
  12120. (calendar-view-holidays-initially-flag nil)
  12121. (view-calendar-holidays-initially nil)
  12122. (calendar-view-diary-initially-flag nil)
  12123. (view-diary-entries-initially nil))
  12124. (if (or (org-at-timestamp-p)
  12125. (save-excursion
  12126. (beginning-of-line 1)
  12127. (looking-at (concat ".*" tsr))))
  12128. (let ((d1 (time-to-days (current-time)))
  12129. (d2 (time-to-days
  12130. (org-time-string-to-time (match-string 1)))))
  12131. (setq diff (- d2 d1))))
  12132. (calendar)
  12133. (calendar-goto-today)
  12134. (if (and diff (not arg)) (calendar-forward-day diff))))
  12135. (defun org-get-date-from-calendar ()
  12136. "Return a list (month day year) of date at point in calendar."
  12137. (with-current-buffer "*Calendar*"
  12138. (save-match-data
  12139. (calendar-cursor-to-date))))
  12140. (defun org-date-from-calendar ()
  12141. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12142. If there is already a time stamp at the cursor position, update it."
  12143. (interactive)
  12144. (if (org-at-timestamp-p t)
  12145. (org-timestamp-change 0 'calendar)
  12146. (let ((cal-date (org-get-date-from-calendar)))
  12147. (org-insert-time-stamp
  12148. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12149. (defun org-minutes-to-hh:mm-string (m)
  12150. "Compute H:MM from a number of minutes."
  12151. (let ((h (/ m 60)))
  12152. (setq m (- m (* 60 h)))
  12153. (format org-time-clocksum-format h m)))
  12154. (defun org-hh:mm-string-to-minutes (s)
  12155. "Convert a string H:MM to a number of minutes.
  12156. If the string is just a number, interprete it as minutes.
  12157. In fact, the first hh:mm or number in the string will be taken,
  12158. there can be extra stuff in the string.
  12159. If no number is found, the return value is 0."
  12160. (cond
  12161. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12162. (+ (* (string-to-number (match-string 1 s)) 60)
  12163. (string-to-number (match-string 2 s))))
  12164. ((string-match "\\([0-9]+\\)" s)
  12165. (string-to-number (match-string 1 s)))
  12166. (t 0)))
  12167. ;;;; Files
  12168. (defun org-save-all-org-buffers ()
  12169. "Save all Org-mode buffers without user confirmation."
  12170. (interactive)
  12171. (message "Saving all Org-mode buffers...")
  12172. (save-some-buffers t 'org-mode-p)
  12173. (when (featurep 'org-id) (org-id-locations-save))
  12174. (message "Saving all Org-mode buffers... done"))
  12175. (defun org-revert-all-org-buffers ()
  12176. "Revert all Org-mode buffers.
  12177. Prompt for confirmation when there are unsaved changes.
  12178. Be sure you know what you are doing before letting this function
  12179. overwrite your changes.
  12180. This function is useful in a setup where one tracks org files
  12181. with a version control system, to revert on one machine after pulling
  12182. changes from another. I believe the procedure must be like this:
  12183. 1. M-x org-save-all-org-buffers
  12184. 2. Pull changes from the other machine, resolve conflicts
  12185. 3. M-x org-revert-all-org-buffers"
  12186. (interactive)
  12187. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12188. (error "Abort"))
  12189. (save-excursion
  12190. (save-window-excursion
  12191. (mapc
  12192. (lambda (b)
  12193. (when (and (with-current-buffer b (org-mode-p))
  12194. (with-current-buffer b buffer-file-name))
  12195. (switch-to-buffer b)
  12196. (revert-buffer t 'no-confirm)))
  12197. (buffer-list))
  12198. (when (and (featurep 'org-id) org-id-track-globally)
  12199. (org-id-locations-load)))))
  12200. ;;;; Agenda files
  12201. ;;;###autoload
  12202. (defun org-iswitchb (&optional arg)
  12203. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12204. With a prefix argument, restrict available to files.
  12205. With two prefix arguments, restrict available buffers to agenda files.
  12206. Due to some yet unresolved reason, the global function
  12207. `iswitchb-mode' needs to be active for this function to work."
  12208. (interactive "P")
  12209. (require 'iswitchb)
  12210. (let ((enabled iswitchb-mode) blist)
  12211. (or enabled (iswitchb-mode 1))
  12212. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12213. ((equal arg '(16)) (org-buffer-list 'agenda))
  12214. (t (org-buffer-list))))
  12215. (unwind-protect
  12216. (let ((iswitchb-make-buflist-hook
  12217. (lambda ()
  12218. (setq iswitchb-temp-buflist
  12219. (mapcar 'buffer-name blist)))))
  12220. (switch-to-buffer
  12221. (iswitchb-read-buffer
  12222. "Switch-to: " nil t))
  12223. (or enabled (iswitchb-mode -1))))))
  12224. ;;;###autoload
  12225. (defun org-ido-switchb (&optional arg)
  12226. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12227. With a prefix argument, restrict available to files.
  12228. With two prefix arguments, restrict available buffers to agenda files."
  12229. (interactive "P")
  12230. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12231. ((equal arg '(16)) (org-buffer-list 'agenda))
  12232. (t (org-buffer-list)))))
  12233. (switch-to-buffer
  12234. (org-ido-completing-read "Org buffer: "
  12235. (mapcar 'list (mapcar 'buffer-name blist))
  12236. nil t))))
  12237. (defun org-buffer-list (&optional predicate exclude-tmp)
  12238. "Return a list of Org buffers.
  12239. PREDICATE can be `export', `files' or `agenda'.
  12240. export restrict the list to Export buffers.
  12241. files restrict the list to buffers visiting Org files.
  12242. agenda restrict the list to buffers visiting agenda files.
  12243. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12244. (let* ((bfn nil)
  12245. (agenda-files (and (eq predicate 'agenda)
  12246. (mapcar 'file-truename (org-agenda-files t))))
  12247. (filter
  12248. (cond
  12249. ((eq predicate 'files)
  12250. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12251. ((eq predicate 'export)
  12252. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12253. ((eq predicate 'agenda)
  12254. (lambda (b)
  12255. (with-current-buffer b
  12256. (and (eq major-mode 'org-mode)
  12257. (setq bfn (buffer-file-name b))
  12258. (member (file-truename bfn) agenda-files)))))
  12259. (t (lambda (b) (with-current-buffer b
  12260. (or (eq major-mode 'org-mode)
  12261. (string-match "\*Org .*Export"
  12262. (buffer-name b)))))))))
  12263. (delq nil
  12264. (mapcar
  12265. (lambda(b)
  12266. (if (and (funcall filter b)
  12267. (or (not exclude-tmp)
  12268. (not (string-match "tmp" (buffer-name b)))))
  12269. b
  12270. nil))
  12271. (buffer-list)))))
  12272. (defun org-agenda-files (&optional unrestricted archives)
  12273. "Get the list of agenda files.
  12274. Optional UNRESTRICTED means return the full list even if a restriction
  12275. is currently in place.
  12276. When ARCHIVES is t, include all archive files hat are really being
  12277. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12278. `org-agenda-archives-mode' is t."
  12279. (let ((files
  12280. (cond
  12281. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12282. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12283. ((listp org-agenda-files) org-agenda-files)
  12284. (t (error "Invalid value of `org-agenda-files'")))))
  12285. (setq files (apply 'append
  12286. (mapcar (lambda (f)
  12287. (if (file-directory-p f)
  12288. (directory-files
  12289. f t org-agenda-file-regexp)
  12290. (list f)))
  12291. files)))
  12292. (when org-agenda-skip-unavailable-files
  12293. (setq files (delq nil
  12294. (mapcar (function
  12295. (lambda (file)
  12296. (and (file-readable-p file) file)))
  12297. files))))
  12298. (when (or (eq archives t)
  12299. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12300. (setq files (org-add-archive-files files)))
  12301. files))
  12302. (defun org-edit-agenda-file-list ()
  12303. "Edit the list of agenda files.
  12304. Depending on setup, this either uses customize to edit the variable
  12305. `org-agenda-files', or it visits the file that is holding the list. In the
  12306. latter case, the buffer is set up in a way that saving it automatically kills
  12307. the buffer and restores the previous window configuration."
  12308. (interactive)
  12309. (if (stringp org-agenda-files)
  12310. (let ((cw (current-window-configuration)))
  12311. (find-file org-agenda-files)
  12312. (org-set-local 'org-window-configuration cw)
  12313. (org-add-hook 'after-save-hook
  12314. (lambda ()
  12315. (set-window-configuration
  12316. (prog1 org-window-configuration
  12317. (kill-buffer (current-buffer))))
  12318. (org-install-agenda-files-menu)
  12319. (message "New agenda file list installed"))
  12320. nil 'local)
  12321. (message "%s" (substitute-command-keys
  12322. "Edit list and finish with \\[save-buffer]")))
  12323. (customize-variable 'org-agenda-files)))
  12324. (defun org-store-new-agenda-file-list (list)
  12325. "Set new value for the agenda file list and save it correctly."
  12326. (if (stringp org-agenda-files)
  12327. (let ((f org-agenda-files) b)
  12328. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12329. (with-temp-file f
  12330. (insert (mapconcat 'identity list "\n") "\n")))
  12331. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12332. (setq org-agenda-files list)
  12333. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12334. (defun org-read-agenda-file-list ()
  12335. "Read the list of agenda files from a file."
  12336. (when (file-directory-p org-agenda-files)
  12337. (error "`org-agenda-files' cannot be a single directory"))
  12338. (when (stringp org-agenda-files)
  12339. (with-temp-buffer
  12340. (insert-file-contents org-agenda-files)
  12341. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12342. ;;;###autoload
  12343. (defun org-cycle-agenda-files ()
  12344. "Cycle through the files in `org-agenda-files'.
  12345. If the current buffer visits an agenda file, find the next one in the list.
  12346. If the current buffer does not, find the first agenda file."
  12347. (interactive)
  12348. (let* ((fs (org-agenda-files t))
  12349. (files (append fs (list (car fs))))
  12350. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12351. file)
  12352. (unless files (error "No agenda files"))
  12353. (catch 'exit
  12354. (while (setq file (pop files))
  12355. (if (equal (file-truename file) tcf)
  12356. (when (car files)
  12357. (find-file (car files))
  12358. (throw 'exit t))))
  12359. (find-file (car fs)))
  12360. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12361. (defun org-agenda-file-to-front (&optional to-end)
  12362. "Move/add the current file to the top of the agenda file list.
  12363. If the file is not present in the list, it is added to the front. If it is
  12364. present, it is moved there. With optional argument TO-END, add/move to the
  12365. end of the list."
  12366. (interactive "P")
  12367. (let ((org-agenda-skip-unavailable-files nil)
  12368. (file-alist (mapcar (lambda (x)
  12369. (cons (file-truename x) x))
  12370. (org-agenda-files t)))
  12371. (ctf (file-truename buffer-file-name))
  12372. x had)
  12373. (setq x (assoc ctf file-alist) had x)
  12374. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12375. (if to-end
  12376. (setq file-alist (append (delq x file-alist) (list x)))
  12377. (setq file-alist (cons x (delq x file-alist))))
  12378. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12379. (org-install-agenda-files-menu)
  12380. (message "File %s to %s of agenda file list"
  12381. (if had "moved" "added") (if to-end "end" "front"))))
  12382. (defun org-remove-file (&optional file)
  12383. "Remove current file from the list of files in variable `org-agenda-files'.
  12384. These are the files which are being checked for agenda entries.
  12385. Optional argument FILE means, use this file instead of the current."
  12386. (interactive)
  12387. (let* ((org-agenda-skip-unavailable-files nil)
  12388. (file (or file buffer-file-name))
  12389. (true-file (file-truename file))
  12390. (afile (abbreviate-file-name file))
  12391. (files (delq nil (mapcar
  12392. (lambda (x)
  12393. (if (equal true-file
  12394. (file-truename x))
  12395. nil x))
  12396. (org-agenda-files t)))))
  12397. (if (not (= (length files) (length (org-agenda-files t))))
  12398. (progn
  12399. (org-store-new-agenda-file-list files)
  12400. (org-install-agenda-files-menu)
  12401. (message "Removed file: %s" afile))
  12402. (message "File was not in list: %s (not removed)" afile))))
  12403. (defun org-file-menu-entry (file)
  12404. (vector file (list 'find-file file) t))
  12405. (defun org-check-agenda-file (file)
  12406. "Make sure FILE exists. If not, ask user what to do."
  12407. (when (not (file-exists-p file))
  12408. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12409. (abbreviate-file-name file))
  12410. (let ((r (downcase (read-char-exclusive))))
  12411. (cond
  12412. ((equal r ?r)
  12413. (org-remove-file file)
  12414. (throw 'nextfile t))
  12415. (t (error "Abort"))))))
  12416. (defun org-get-agenda-file-buffer (file)
  12417. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12418. it to the list of buffers which might be released later."
  12419. (let ((buf (org-find-base-buffer-visiting file)))
  12420. (if buf
  12421. buf ; just return it
  12422. ;; Make a new buffer and remember it
  12423. (setq buf (find-file-noselect file))
  12424. (if buf (push buf org-agenda-new-buffers))
  12425. buf)))
  12426. (defun org-release-buffers (blist)
  12427. "Release all buffers in list, asking the user for confirmation when needed.
  12428. When a buffer is unmodified, it is just killed. When modified, it is saved
  12429. \(if the user agrees) and then killed."
  12430. (let (buf file)
  12431. (while (setq buf (pop blist))
  12432. (setq file (buffer-file-name buf))
  12433. (when (and (buffer-modified-p buf)
  12434. file
  12435. (y-or-n-p (format "Save file %s? " file)))
  12436. (with-current-buffer buf (save-buffer)))
  12437. (kill-buffer buf))))
  12438. (defun org-prepare-agenda-buffers (files)
  12439. "Create buffers for all agenda files, protect archived trees and comments."
  12440. (interactive)
  12441. (let ((pa '(:org-archived t))
  12442. (pc '(:org-comment t))
  12443. (pall '(:org-archived t :org-comment t))
  12444. (inhibit-read-only t)
  12445. (rea (concat ":" org-archive-tag ":"))
  12446. bmp file re)
  12447. (save-excursion
  12448. (save-restriction
  12449. (while (setq file (pop files))
  12450. (catch 'nextfile
  12451. (if (bufferp file)
  12452. (set-buffer file)
  12453. (org-check-agenda-file file)
  12454. (set-buffer (org-get-agenda-file-buffer file)))
  12455. (widen)
  12456. (setq bmp (buffer-modified-p))
  12457. (org-refresh-category-properties)
  12458. (setq org-todo-keywords-for-agenda
  12459. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12460. (setq org-done-keywords-for-agenda
  12461. (append org-done-keywords-for-agenda org-done-keywords))
  12462. (setq org-todo-keyword-alist-for-agenda
  12463. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12464. (setq org-tag-alist-for-agenda
  12465. (append org-tag-alist-for-agenda org-tag-alist))
  12466. (save-excursion
  12467. (remove-text-properties (point-min) (point-max) pall)
  12468. (when org-agenda-skip-archived-trees
  12469. (goto-char (point-min))
  12470. (while (re-search-forward rea nil t)
  12471. (if (org-on-heading-p t)
  12472. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12473. (goto-char (point-min))
  12474. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12475. (while (re-search-forward re nil t)
  12476. (add-text-properties
  12477. (match-beginning 0) (org-end-of-subtree t) pc)))
  12478. (set-buffer-modified-p bmp)))))
  12479. (setq org-todo-keyword-alist-for-agenda
  12480. (org-uniquify org-todo-keyword-alist-for-agenda)
  12481. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12482. ;;;; Embedded LaTeX
  12483. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12484. "Keymap for the minor `org-cdlatex-mode'.")
  12485. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12486. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12487. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12488. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12489. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12490. (defvar org-cdlatex-texmathp-advice-is-done nil
  12491. "Flag remembering if we have applied the advice to texmathp already.")
  12492. (define-minor-mode org-cdlatex-mode
  12493. "Toggle the minor `org-cdlatex-mode'.
  12494. This mode supports entering LaTeX environment and math in LaTeX fragments
  12495. in Org-mode.
  12496. \\{org-cdlatex-mode-map}"
  12497. nil " OCDL" nil
  12498. (when org-cdlatex-mode (require 'cdlatex))
  12499. (unless org-cdlatex-texmathp-advice-is-done
  12500. (setq org-cdlatex-texmathp-advice-is-done t)
  12501. (defadvice texmathp (around org-math-always-on activate)
  12502. "Always return t in org-mode buffers.
  12503. This is because we want to insert math symbols without dollars even outside
  12504. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12505. an embedded LaTeX fragment, let texmathp do its job.
  12506. \\[org-cdlatex-mode-map]"
  12507. (interactive)
  12508. (let (p)
  12509. (cond
  12510. ((not (org-mode-p)) ad-do-it)
  12511. ((eq this-command 'cdlatex-math-symbol)
  12512. (setq ad-return-value t
  12513. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12514. (t
  12515. (let ((p (org-inside-LaTeX-fragment-p)))
  12516. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12517. (setq ad-return-value t
  12518. texmathp-why '("Org-mode embedded math" . 0))
  12519. (if p ad-do-it)))))))))
  12520. (defun turn-on-org-cdlatex ()
  12521. "Unconditionally turn on `org-cdlatex-mode'."
  12522. (org-cdlatex-mode 1))
  12523. (defun org-inside-LaTeX-fragment-p ()
  12524. "Test if point is inside a LaTeX fragment.
  12525. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12526. sequence appearing also before point.
  12527. Even though the matchers for math are configurable, this function assumes
  12528. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12529. delimiters are skipped when they have been removed by customization.
  12530. The return value is nil, or a cons cell with the delimiter and
  12531. and the position of this delimiter.
  12532. This function does a reasonably good job, but can locally be fooled by
  12533. for example currency specifications. For example it will assume being in
  12534. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12535. fragments that are properly closed, but during editing, we have to live
  12536. with the uncertainty caused by missing closing delimiters. This function
  12537. looks only before point, not after."
  12538. (catch 'exit
  12539. (let ((pos (point))
  12540. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12541. (lim (progn
  12542. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12543. (point)))
  12544. dd-on str (start 0) m re)
  12545. (goto-char pos)
  12546. (when dodollar
  12547. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12548. re (nth 1 (assoc "$" org-latex-regexps)))
  12549. (while (string-match re str start)
  12550. (cond
  12551. ((= (match-end 0) (length str))
  12552. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12553. ((= (match-end 0) (- (length str) 5))
  12554. (throw 'exit nil))
  12555. (t (setq start (match-end 0))))))
  12556. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12557. (goto-char pos)
  12558. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12559. (and (match-beginning 2) (throw 'exit nil))
  12560. ;; count $$
  12561. (while (re-search-backward "\\$\\$" lim t)
  12562. (setq dd-on (not dd-on)))
  12563. (goto-char pos)
  12564. (if dd-on (cons "$$" m))))))
  12565. (defun org-try-cdlatex-tab ()
  12566. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12567. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12568. - inside a LaTeX fragment, or
  12569. - after the first word in a line, where an abbreviation expansion could
  12570. insert a LaTeX environment."
  12571. (when org-cdlatex-mode
  12572. (cond
  12573. ((save-excursion
  12574. (skip-chars-backward "a-zA-Z0-9*")
  12575. (skip-chars-backward " \t")
  12576. (bolp))
  12577. (cdlatex-tab) t)
  12578. ((org-inside-LaTeX-fragment-p)
  12579. (cdlatex-tab) t)
  12580. (t nil))))
  12581. (defun org-cdlatex-underscore-caret (&optional arg)
  12582. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12583. Revert to the normal definition outside of these fragments."
  12584. (interactive "P")
  12585. (if (org-inside-LaTeX-fragment-p)
  12586. (call-interactively 'cdlatex-sub-superscript)
  12587. (let (org-cdlatex-mode)
  12588. (call-interactively (key-binding (vector last-input-event))))))
  12589. (defun org-cdlatex-math-modify (&optional arg)
  12590. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12591. Revert to the normal definition outside of these fragments."
  12592. (interactive "P")
  12593. (if (org-inside-LaTeX-fragment-p)
  12594. (call-interactively 'cdlatex-math-modify)
  12595. (let (org-cdlatex-mode)
  12596. (call-interactively (key-binding (vector last-input-event))))))
  12597. (defvar org-latex-fragment-image-overlays nil
  12598. "List of overlays carrying the images of latex fragments.")
  12599. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12600. (defun org-remove-latex-fragment-image-overlays ()
  12601. "Remove all overlays with LaTeX fragment images in current buffer."
  12602. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12603. (setq org-latex-fragment-image-overlays nil))
  12604. (defun org-preview-latex-fragment (&optional subtree)
  12605. "Preview the LaTeX fragment at point, or all locally or globally.
  12606. If the cursor is in a LaTeX fragment, create the image and overlay
  12607. it over the source code. If there is no fragment at point, display
  12608. all fragments in the current text, from one headline to the next. With
  12609. prefix SUBTREE, display all fragments in the current subtree. With a
  12610. double prefix `C-u C-u', or when the cursor is before the first headline,
  12611. display all fragments in the buffer.
  12612. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12613. (interactive "P")
  12614. (org-remove-latex-fragment-image-overlays)
  12615. (save-excursion
  12616. (save-restriction
  12617. (let (beg end at msg)
  12618. (cond
  12619. ((or (equal subtree '(16))
  12620. (not (save-excursion
  12621. (re-search-backward (concat "^" outline-regexp) nil t))))
  12622. (setq beg (point-min) end (point-max)
  12623. msg "Creating images for buffer...%s"))
  12624. ((equal subtree '(4))
  12625. (org-back-to-heading)
  12626. (setq beg (point) end (org-end-of-subtree t)
  12627. msg "Creating images for subtree...%s"))
  12628. (t
  12629. (if (setq at (org-inside-LaTeX-fragment-p))
  12630. (goto-char (max (point-min) (- (cdr at) 2)))
  12631. (org-back-to-heading))
  12632. (setq beg (point) end (progn (outline-next-heading) (point))
  12633. msg (if at "Creating image...%s"
  12634. "Creating images for entry...%s"))))
  12635. (message msg "")
  12636. (narrow-to-region beg end)
  12637. (goto-char beg)
  12638. (org-format-latex
  12639. (concat "ltxpng/" (file-name-sans-extension
  12640. (file-name-nondirectory
  12641. buffer-file-name)))
  12642. default-directory 'overlays msg at 'forbuffer)
  12643. (message msg "done. Use `C-c C-c' to remove images.")))))
  12644. (defvar org-latex-regexps
  12645. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12646. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12647. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12648. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12649. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12650. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12651. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12652. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12653. "Regular expressions for matching embedded LaTeX.")
  12654. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12655. "Replace LaTeX fragments with links to an image, and produce images."
  12656. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12657. (let* ((prefixnodir (file-name-nondirectory prefix))
  12658. (absprefix (expand-file-name prefix dir))
  12659. (todir (file-name-directory absprefix))
  12660. (opt org-format-latex-options)
  12661. (matchers (plist-get opt :matchers))
  12662. (re-list org-latex-regexps)
  12663. (cnt 0) txt link beg end re e checkdir
  12664. executables-checked
  12665. m n block linkfile movefile ov)
  12666. ;; Check if there are old images files with this prefix, and remove them
  12667. (when (file-directory-p todir)
  12668. (mapc 'delete-file
  12669. (directory-files
  12670. todir 'full
  12671. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12672. ;; Check the different regular expressions
  12673. (while (setq e (pop re-list))
  12674. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12675. block (if (nth 3 e) "\n\n" ""))
  12676. (when (member m matchers)
  12677. (goto-char (point-min))
  12678. (while (re-search-forward re nil t)
  12679. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12680. (not (get-text-property (match-beginning n)
  12681. 'org-protected)))
  12682. (setq txt (match-string n)
  12683. beg (match-beginning n) end (match-end n)
  12684. cnt (1+ cnt)
  12685. linkfile (format "%s_%04d.png" prefix cnt)
  12686. movefile (format "%s_%04d.png" absprefix cnt)
  12687. link (concat block "[[file:" linkfile "]]" block))
  12688. (if msg (message msg cnt))
  12689. (goto-char beg)
  12690. (unless checkdir ; make sure the directory exists
  12691. (setq checkdir t)
  12692. (or (file-directory-p todir) (make-directory todir)))
  12693. (unless executables-checked
  12694. (org-check-external-command
  12695. "latex" "needed to convert LaTeX fragments to images")
  12696. (org-check-external-command
  12697. "dvipng" "needed to convert LaTeX fragments to images")
  12698. (setq executables-checked t))
  12699. (org-create-formula-image
  12700. txt movefile opt forbuffer)
  12701. (if overlays
  12702. (progn
  12703. (setq ov (org-make-overlay beg end))
  12704. (if (featurep 'xemacs)
  12705. (progn
  12706. (org-overlay-put ov 'invisible t)
  12707. (org-overlay-put
  12708. ov 'end-glyph
  12709. (make-glyph (vector 'png :file movefile))))
  12710. (org-overlay-put
  12711. ov 'display
  12712. (list 'image :type 'png :file movefile :ascent 'center)))
  12713. (push ov org-latex-fragment-image-overlays)
  12714. (goto-char end))
  12715. (delete-region beg end)
  12716. (insert link))))))))
  12717. ;; This function borrows from Ganesh Swami's latex2png.el
  12718. (defun org-create-formula-image (string tofile options buffer)
  12719. (let* ((tmpdir (if (featurep 'xemacs)
  12720. (temp-directory)
  12721. temporary-file-directory))
  12722. (texfilebase (make-temp-name
  12723. (expand-file-name "orgtex" tmpdir)))
  12724. (texfile (concat texfilebase ".tex"))
  12725. (dvifile (concat texfilebase ".dvi"))
  12726. (pngfile (concat texfilebase ".png"))
  12727. (fnh (if (featurep 'xemacs)
  12728. (font-height (get-face-font 'default))
  12729. (face-attribute 'default :height nil)))
  12730. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12731. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12732. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12733. "Black"))
  12734. (bg (or (plist-get options (if buffer :background :html-background))
  12735. "Transparent")))
  12736. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12737. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12738. (with-temp-file texfile
  12739. (insert org-format-latex-header
  12740. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12741. (let ((dir default-directory))
  12742. (condition-case nil
  12743. (progn
  12744. (cd tmpdir)
  12745. (call-process "latex" nil nil nil texfile))
  12746. (error nil))
  12747. (cd dir))
  12748. (if (not (file-exists-p dvifile))
  12749. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12750. (condition-case nil
  12751. (call-process "dvipng" nil nil nil
  12752. "-E" "-fg" fg "-bg" bg
  12753. "-D" dpi
  12754. ;;"-x" scale "-y" scale
  12755. "-T" "tight"
  12756. "-o" pngfile
  12757. dvifile)
  12758. (error nil))
  12759. (if (not (file-exists-p pngfile))
  12760. (progn (message "Failed to create png file from %s" texfile) nil)
  12761. ;; Use the requested file name and clean up
  12762. (copy-file pngfile tofile 'replace)
  12763. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12764. (delete-file (concat texfilebase e)))
  12765. pngfile))))
  12766. (defun org-dvipng-color (attr)
  12767. "Return an rgb color specification for dvipng."
  12768. (apply 'format "rgb %s %s %s"
  12769. (mapcar 'org-normalize-color
  12770. (color-values (face-attribute 'default attr nil)))))
  12771. (defun org-normalize-color (value)
  12772. "Return string to be used as color value for an RGB component."
  12773. (format "%g" (/ value 65535.0)))
  12774. ;;;; Key bindings
  12775. ;; Make `C-c C-x' a prefix key
  12776. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12777. ;; TAB key with modifiers
  12778. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12779. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12780. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12781. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12782. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12783. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12784. ;; The following line is necessary under Suse GNU/Linux
  12785. (unless (featurep 'xemacs)
  12786. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12787. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12788. (define-key org-mode-map [backtab] 'org-shifttab)
  12789. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12790. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12791. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12792. ;; Cursor keys with modifiers
  12793. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12794. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12795. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12796. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12797. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12798. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12799. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12800. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12801. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12802. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12803. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12804. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12805. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12806. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12807. ;;; Extra keys for tty access.
  12808. ;; We only set them when really needed because otherwise the
  12809. ;; menus don't show the simple keys
  12810. (when (or org-use-extra-keys
  12811. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12812. (not window-system))
  12813. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12814. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12815. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12816. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12817. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12818. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12819. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12820. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12821. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12822. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12823. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12824. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12825. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12826. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12827. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12828. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12829. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12830. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12831. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12832. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12833. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12834. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12835. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12836. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12837. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12838. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12839. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12840. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12841. ;; All the other keys
  12842. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12843. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12844. (if (boundp 'narrow-map)
  12845. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12846. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12847. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12848. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12849. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12850. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12851. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12852. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12853. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12854. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12855. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12856. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12857. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12858. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12859. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12860. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12861. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12862. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12863. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12864. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12865. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12866. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12867. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12868. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12869. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12870. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12871. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12872. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12873. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12874. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12875. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12876. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12877. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12878. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12879. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12880. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12881. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12882. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12883. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12884. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12885. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12886. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12887. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12888. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12889. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12890. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12891. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12892. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12893. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12894. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12895. (org-defkey org-mode-map "\C-m" 'org-return)
  12896. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12897. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12898. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12899. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12900. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12901. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12902. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12903. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12904. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12905. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12906. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12907. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12908. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12909. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12910. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12911. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12912. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12913. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  12914. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  12915. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12916. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12917. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12918. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12919. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12920. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12921. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12922. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12923. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12924. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12925. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12926. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12927. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12928. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12929. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12930. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12931. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12932. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  12933. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12934. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12935. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12936. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12937. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12938. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12939. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12940. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12941. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12942. (when (featurep 'xemacs)
  12943. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12944. (defvar org-self-insert-command-undo-counter 0)
  12945. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12946. (defun org-self-insert-command (N)
  12947. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12948. If the cursor is in a table looking at whitespace, the whitespace is
  12949. overwritten, and the table is not marked as requiring realignment."
  12950. (interactive "p")
  12951. (if (and
  12952. (org-table-p)
  12953. (progn
  12954. ;; check if we blank the field, and if that triggers align
  12955. (and (featurep 'org-table) org-table-auto-blank-field
  12956. (member last-command
  12957. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  12958. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12959. ;; got extra space, this field does not determine column width
  12960. (let (org-table-may-need-update) (org-table-blank-field))
  12961. ;; no extra space, this field may determine column width
  12962. (org-table-blank-field)))
  12963. t)
  12964. (eq N 1)
  12965. (looking-at "[^|\n]* |"))
  12966. (let (org-table-may-need-update)
  12967. (goto-char (1- (match-end 0)))
  12968. (delete-backward-char 1)
  12969. (goto-char (match-beginning 0))
  12970. (self-insert-command N))
  12971. (setq org-table-may-need-update t)
  12972. (self-insert-command N)
  12973. (org-fix-tags-on-the-fly)
  12974. (if org-self-insert-cluster-for-undo
  12975. (if (not (eq last-command 'org-self-insert-command))
  12976. (setq org-self-insert-command-undo-counter 1)
  12977. (if (>= org-self-insert-command-undo-counter 20)
  12978. (setq org-self-insert-command-undo-counter 1)
  12979. (and (> org-self-insert-command-undo-counter 0)
  12980. buffer-undo-list
  12981. (not (cadr buffer-undo-list)) ; remove nil entry
  12982. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12983. (setq org-self-insert-command-undo-counter
  12984. (1+ org-self-insert-command-undo-counter)))))))
  12985. (defun org-fix-tags-on-the-fly ()
  12986. (when (and (equal (char-after (point-at-bol)) ?*)
  12987. (org-on-heading-p))
  12988. (org-align-tags-here org-tags-column)))
  12989. (defun org-delete-backward-char (N)
  12990. "Like `delete-backward-char', insert whitespace at field end in tables.
  12991. When deleting backwards, in tables this function will insert whitespace in
  12992. front of the next \"|\" separator, to keep the table aligned. The table will
  12993. still be marked for re-alignment if the field did fill the entire column,
  12994. because, in this case the deletion might narrow the column."
  12995. (interactive "p")
  12996. (if (and (org-table-p)
  12997. (eq N 1)
  12998. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12999. (looking-at ".*?|"))
  13000. (let ((pos (point))
  13001. (noalign (looking-at "[^|\n\r]* |"))
  13002. (c org-table-may-need-update))
  13003. (backward-delete-char N)
  13004. (skip-chars-forward "^|")
  13005. (insert " ")
  13006. (goto-char (1- pos))
  13007. ;; noalign: if there were two spaces at the end, this field
  13008. ;; does not determine the width of the column.
  13009. (if noalign (setq org-table-may-need-update c)))
  13010. (backward-delete-char N)
  13011. (org-fix-tags-on-the-fly)))
  13012. (defun org-delete-char (N)
  13013. "Like `delete-char', but insert whitespace at field end in tables.
  13014. When deleting characters, in tables this function will insert whitespace in
  13015. front of the next \"|\" separator, to keep the table aligned. The table will
  13016. still be marked for re-alignment if the field did fill the entire column,
  13017. because, in this case the deletion might narrow the column."
  13018. (interactive "p")
  13019. (if (and (org-table-p)
  13020. (not (bolp))
  13021. (not (= (char-after) ?|))
  13022. (eq N 1))
  13023. (if (looking-at ".*?|")
  13024. (let ((pos (point))
  13025. (noalign (looking-at "[^|\n\r]* |"))
  13026. (c org-table-may-need-update))
  13027. (replace-match (concat
  13028. (substring (match-string 0) 1 -1)
  13029. " |"))
  13030. (goto-char pos)
  13031. ;; noalign: if there were two spaces at the end, this field
  13032. ;; does not determine the width of the column.
  13033. (if noalign (setq org-table-may-need-update c)))
  13034. (delete-char N))
  13035. (delete-char N)
  13036. (org-fix-tags-on-the-fly)))
  13037. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13038. (put 'org-self-insert-command 'delete-selection t)
  13039. (put 'orgtbl-self-insert-command 'delete-selection t)
  13040. (put 'org-delete-char 'delete-selection 'supersede)
  13041. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13042. (put 'org-yank 'delete-selection 'yank)
  13043. ;; Make `flyspell-mode' delay after some commands
  13044. (put 'org-self-insert-command 'flyspell-delayed t)
  13045. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13046. (put 'org-delete-char 'flyspell-delayed t)
  13047. (put 'org-delete-backward-char 'flyspell-delayed t)
  13048. ;; Make pabbrev-mode expand after org-mode commands
  13049. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13050. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13051. ;; How to do this: Measure non-white length of current string
  13052. ;; If equal to column width, we should realign.
  13053. (defun org-remap (map &rest commands)
  13054. "In MAP, remap the functions given in COMMANDS.
  13055. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13056. (let (new old)
  13057. (while commands
  13058. (setq old (pop commands) new (pop commands))
  13059. (if (fboundp 'command-remapping)
  13060. (org-defkey map (vector 'remap old) new)
  13061. (substitute-key-definition old new map global-map)))))
  13062. (when (eq org-enable-table-editor 'optimized)
  13063. ;; If the user wants maximum table support, we need to hijack
  13064. ;; some standard editing functions
  13065. (org-remap org-mode-map
  13066. 'self-insert-command 'org-self-insert-command
  13067. 'delete-char 'org-delete-char
  13068. 'delete-backward-char 'org-delete-backward-char)
  13069. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13070. (defvar org-ctrl-c-ctrl-c-hook nil
  13071. "Hook for functions attaching themselves to `C-c C-c'.
  13072. This can be used to add additional functionality to the C-c C-c key which
  13073. executes context-dependent commands.
  13074. Each function will be called with no arguments. The function must check
  13075. if the context is appropriate for it to act. If yes, it should do its
  13076. thing and then return a non-nil value. If the context is wrong,
  13077. just do nothing and return nil.")
  13078. (defvar org-tab-first-hook nil
  13079. "Hook for functions to attach themselves to TAB.
  13080. See `org-ctrl-c-ctrl-c-hook' for more information.
  13081. This hook runs as the first action when TAB is pressed, even before
  13082. `org-cycle' messes around with the `outline-regexp' to cater for
  13083. inline tasks and plain list item folding.
  13084. If any function in this hook returns t, not other actions like table
  13085. field motion visibility cycling will be done.")
  13086. (defvar org-tab-after-check-for-table-hook nil
  13087. "Hook for functions to attach themselves to TAB.
  13088. See `org-ctrl-c-ctrl-c-hook' for more information.
  13089. This hook runs after it has been established that the cursor is not in a
  13090. table, but before checking if the cursor is in a headline or if global cycling
  13091. should be done.
  13092. If any function in this hook returns t, not other actions like visibility
  13093. cycling will be done.")
  13094. (defvar org-tab-after-check-for-cycling-hook nil
  13095. "Hook for functions to attach themselves to TAB.
  13096. See `org-ctrl-c-ctrl-c-hook' for more information.
  13097. This hook runs after it has been established that not table field motion and
  13098. not visibility should be done because of current context. This is probably
  13099. the place where a package like yasnippets can hook in.")
  13100. (defvar org-metaleft-hook nil
  13101. "Hook for functions attaching themselves to `M-left'.
  13102. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13103. (defvar org-metaright-hook nil
  13104. "Hook for functions attaching themselves to `M-right'.
  13105. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13106. (defvar org-metaup-hook nil
  13107. "Hook for functions attaching themselves to `M-up'.
  13108. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13109. (defvar org-metadown-hook nil
  13110. "Hook for functions attaching themselves to `M-down'.
  13111. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13112. (defvar org-shiftmetaleft-hook nil
  13113. "Hook for functions attaching themselves to `M-S-left'.
  13114. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13115. (defvar org-shiftmetaright-hook nil
  13116. "Hook for functions attaching themselves to `M-S-right'.
  13117. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13118. (defvar org-shiftmetaup-hook nil
  13119. "Hook for functions attaching themselves to `M-S-up'.
  13120. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13121. (defvar org-shiftmetadown-hook nil
  13122. "Hook for functions attaching themselves to `M-S-down'.
  13123. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13124. (defvar org-metareturn-hook nil
  13125. "Hook for functions attaching themselves to `M-RET'.
  13126. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13127. (defun org-modifier-cursor-error ()
  13128. "Throw an error, a modified cursor command was applied in wrong context."
  13129. (error "This command is active in special context like tables, headlines or items"))
  13130. (defun org-shiftselect-error ()
  13131. "Throw an error because Shift-Cursor command was applied in wrong context."
  13132. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13133. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13134. (error "This command works only in special context like headlines or timestamps.")))
  13135. (defun org-call-for-shift-select (cmd)
  13136. (let ((this-command-keys-shift-translated t))
  13137. (call-interactively cmd)))
  13138. (defun org-shifttab (&optional arg)
  13139. "Global visibility cycling or move to previous table field.
  13140. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13141. on context.
  13142. See the individual commands for more information."
  13143. (interactive "P")
  13144. (cond
  13145. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13146. ((integerp arg)
  13147. (message "Content view to level: %d" arg)
  13148. (org-content (prefix-numeric-value arg))
  13149. (setq org-cycle-global-status 'overview))
  13150. (t (call-interactively 'org-global-cycle))))
  13151. (defun org-shiftmetaleft ()
  13152. "Promote subtree or delete table column.
  13153. Calls `org-promote-subtree', `org-outdent-item',
  13154. or `org-table-delete-column', depending on context.
  13155. See the individual commands for more information."
  13156. (interactive)
  13157. (cond
  13158. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13159. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13160. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13161. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13162. (t (org-modifier-cursor-error))))
  13163. (defun org-shiftmetaright ()
  13164. "Demote subtree or insert table column.
  13165. Calls `org-demote-subtree', `org-indent-item',
  13166. or `org-table-insert-column', depending on context.
  13167. See the individual commands for more information."
  13168. (interactive)
  13169. (cond
  13170. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13171. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13172. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13173. ((org-at-item-p) (call-interactively 'org-indent-item))
  13174. (t (org-modifier-cursor-error))))
  13175. (defun org-shiftmetaup (&optional arg)
  13176. "Move subtree up or kill table row.
  13177. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13178. `org-move-item-up' depending on context. See the individual commands
  13179. for more information."
  13180. (interactive "P")
  13181. (cond
  13182. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13183. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13184. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13185. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13186. (t (org-modifier-cursor-error))))
  13187. (defun org-shiftmetadown (&optional arg)
  13188. "Move subtree down or insert table row.
  13189. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13190. `org-move-item-down', depending on context. See the individual
  13191. commands for more information."
  13192. (interactive "P")
  13193. (cond
  13194. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13195. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13196. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13197. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13198. (t (org-modifier-cursor-error))))
  13199. (defun org-metaleft (&optional arg)
  13200. "Promote heading or move table column to left.
  13201. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13202. With no specific context, calls the Emacs default `backward-word'.
  13203. See the individual commands for more information."
  13204. (interactive "P")
  13205. (cond
  13206. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13207. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13208. ((or (org-on-heading-p)
  13209. (and (org-region-active-p)
  13210. (save-excursion
  13211. (goto-char (region-beginning))
  13212. (org-on-heading-p))))
  13213. (call-interactively 'org-do-promote))
  13214. ((or (org-at-item-p)
  13215. (and (org-region-active-p)
  13216. (save-excursion
  13217. (goto-char (region-beginning))
  13218. (org-at-item-p))))
  13219. (call-interactively 'org-outdent-item))
  13220. (t (call-interactively 'backward-word))))
  13221. (defun org-metaright (&optional arg)
  13222. "Demote subtree or move table column to right.
  13223. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13224. With no specific context, calls the Emacs default `forward-word'.
  13225. See the individual commands for more information."
  13226. (interactive "P")
  13227. (cond
  13228. ((run-hook-with-args-until-success 'org-metaright-hook))
  13229. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13230. ((or (org-on-heading-p)
  13231. (and (org-region-active-p)
  13232. (save-excursion
  13233. (goto-char (region-beginning))
  13234. (org-on-heading-p))))
  13235. (call-interactively 'org-do-demote))
  13236. ((or (org-at-item-p)
  13237. (and (org-region-active-p)
  13238. (save-excursion
  13239. (goto-char (region-beginning))
  13240. (org-at-item-p))))
  13241. (call-interactively 'org-indent-item))
  13242. (t (call-interactively 'forward-word))))
  13243. (defun org-metaup (&optional arg)
  13244. "Move subtree up or move table row up.
  13245. Calls `org-move-subtree-up' or `org-table-move-row' or
  13246. `org-move-item-up', depending on context. See the individual commands
  13247. for more information."
  13248. (interactive "P")
  13249. (cond
  13250. ((run-hook-with-args-until-success 'org-metaup-hook))
  13251. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13252. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13253. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13254. (t (transpose-lines 1) (beginning-of-line -1))))
  13255. (defun org-metadown (&optional arg)
  13256. "Move subtree down or move table row down.
  13257. Calls `org-move-subtree-down' or `org-table-move-row' or
  13258. `org-move-item-down', depending on context. See the individual
  13259. commands for more information."
  13260. (interactive "P")
  13261. (cond
  13262. ((run-hook-with-args-until-success 'org-metadown-hook))
  13263. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13264. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13265. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13266. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13267. (defun org-shiftup (&optional arg)
  13268. "Increase item in timestamp or increase priority of current headline.
  13269. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13270. depending on context. See the individual commands for more information."
  13271. (interactive "P")
  13272. (cond
  13273. ((and org-support-shift-select (org-region-active-p))
  13274. (org-call-for-shift-select 'previous-line))
  13275. ((org-at-timestamp-p t)
  13276. (call-interactively (if org-edit-timestamp-down-means-later
  13277. 'org-timestamp-down 'org-timestamp-up)))
  13278. ((and (not (eq org-support-shift-select 'always))
  13279. org-enable-priority-commands
  13280. (org-on-heading-p))
  13281. (call-interactively 'org-priority-up))
  13282. ((and (not org-support-shift-select) (org-at-item-p))
  13283. (call-interactively 'org-previous-item))
  13284. ((org-clocktable-try-shift 'up arg))
  13285. (org-support-shift-select
  13286. (org-call-for-shift-select 'previous-line))
  13287. (t (org-shiftselect-error))))
  13288. (defun org-shiftdown (&optional arg)
  13289. "Decrease item in timestamp or decrease priority of current headline.
  13290. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13291. depending on context. See the individual commands for more information."
  13292. (interactive "P")
  13293. (cond
  13294. ((and org-support-shift-select (org-region-active-p))
  13295. (org-call-for-shift-select 'next-line))
  13296. ((org-at-timestamp-p t)
  13297. (call-interactively (if org-edit-timestamp-down-means-later
  13298. 'org-timestamp-up 'org-timestamp-down)))
  13299. ((and (not (eq org-support-shift-select 'always))
  13300. org-enable-priority-commands
  13301. (org-on-heading-p))
  13302. (call-interactively 'org-priority-down))
  13303. ((and (not org-support-shift-select) (org-at-item-p))
  13304. (call-interactively 'org-next-item))
  13305. ((org-clocktable-try-shift 'down arg))
  13306. (org-support-shift-select
  13307. (org-call-for-shift-select 'next-line))
  13308. (t (org-shiftselect-error))))
  13309. (defun org-shiftright (&optional arg)
  13310. "Cycle the thing at point or in the current line, depending on context.
  13311. Depending on context, this does one of the following:
  13312. - switch a timestamp at point one day into the future
  13313. - on a headline, switch to the next TODO keyword.
  13314. - on an item, switch entire list to the next bullet type
  13315. - on a property line, switch to the next allowed value
  13316. - on a clocktable definition line, move time block into the future"
  13317. (interactive "P")
  13318. (cond
  13319. ((and org-support-shift-select (org-region-active-p))
  13320. (org-call-for-shift-select 'forward-char))
  13321. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13322. ((and (not (eq org-support-shift-select 'always))
  13323. (org-on-heading-p))
  13324. (let ((org-inhibit-logging
  13325. (not org-treat-S-cursor-todo-selection-as-state-change))
  13326. (org-inhibit-blocking
  13327. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13328. (org-call-with-arg 'org-todo 'right)))
  13329. ((or (and org-support-shift-select
  13330. (not (eq org-support-shift-select 'always))
  13331. (org-at-item-bullet-p))
  13332. (and (not org-support-shift-select) (org-at-item-p)))
  13333. (org-call-with-arg 'org-cycle-list-bullet nil))
  13334. ((and (not (eq org-support-shift-select 'always))
  13335. (org-at-property-p))
  13336. (call-interactively 'org-property-next-allowed-value))
  13337. ((org-clocktable-try-shift 'right arg))
  13338. (org-support-shift-select
  13339. (org-call-for-shift-select 'forward-char))
  13340. (t (org-shiftselect-error))))
  13341. (defun org-shiftleft (&optional arg)
  13342. "Cycle the thing at point or in the current line, depending on context.
  13343. Depending on context, this does one of the following:
  13344. - switch a timestamp at point one day into the past
  13345. - on a headline, switch to the previous TODO keyword.
  13346. - on an item, switch entire list to the previous bullet type
  13347. - on a property line, switch to the previous allowed value
  13348. - on a clocktable definition line, move time block into the past"
  13349. (interactive "P")
  13350. (cond
  13351. ((and org-support-shift-select (org-region-active-p))
  13352. (org-call-for-shift-select 'backward-char))
  13353. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13354. ((and (not (eq org-support-shift-select 'always))
  13355. (org-on-heading-p))
  13356. (let ((org-inhibit-logging
  13357. (not org-treat-S-cursor-todo-selection-as-state-change))
  13358. (org-inhibit-blocking
  13359. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13360. (org-call-with-arg 'org-todo 'left)))
  13361. ((or (and org-support-shift-select
  13362. (not (eq org-support-shift-select 'always))
  13363. (org-at-item-bullet-p))
  13364. (and (not org-support-shift-select) (org-at-item-p)))
  13365. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13366. ((and (not (eq org-support-shift-select 'always))
  13367. (org-at-property-p))
  13368. (call-interactively 'org-property-previous-allowed-value))
  13369. ((org-clocktable-try-shift 'left arg))
  13370. (org-support-shift-select
  13371. (org-call-for-shift-select 'backward-char))
  13372. (t (org-shiftselect-error))))
  13373. (defun org-shiftcontrolright ()
  13374. "Switch to next TODO set."
  13375. (interactive)
  13376. (cond
  13377. ((and org-support-shift-select (org-region-active-p))
  13378. (org-call-for-shift-select 'forward-word))
  13379. ((and (not (eq org-support-shift-select 'always))
  13380. (org-on-heading-p))
  13381. (org-call-with-arg 'org-todo 'nextset))
  13382. (org-support-shift-select
  13383. (org-call-for-shift-select 'forward-word))
  13384. (t (org-shiftselect-error))))
  13385. (defun org-shiftcontrolleft ()
  13386. "Switch to previous TODO set."
  13387. (interactive)
  13388. (cond
  13389. ((and org-support-shift-select (org-region-active-p))
  13390. (org-call-for-shift-select 'backward-word))
  13391. ((and (not (eq org-support-shift-select 'always))
  13392. (org-on-heading-p))
  13393. (org-call-with-arg 'org-todo 'previousset))
  13394. (org-support-shift-select
  13395. (org-call-for-shift-select 'backward-word))
  13396. (t (org-shiftselect-error))))
  13397. (defun org-ctrl-c-ret ()
  13398. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13399. (interactive)
  13400. (cond
  13401. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13402. (t (call-interactively 'org-insert-heading))))
  13403. (defun org-copy-special ()
  13404. "Copy region in table or copy current subtree.
  13405. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13406. See the individual commands for more information."
  13407. (interactive)
  13408. (call-interactively
  13409. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13410. (defun org-cut-special ()
  13411. "Cut region in table or cut current subtree.
  13412. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13413. See the individual commands for more information."
  13414. (interactive)
  13415. (call-interactively
  13416. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13417. (defun org-paste-special (arg)
  13418. "Paste rectangular region into table, or past subtree relative to level.
  13419. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13420. See the individual commands for more information."
  13421. (interactive "P")
  13422. (if (org-at-table-p)
  13423. (org-table-paste-rectangle)
  13424. (org-paste-subtree arg)))
  13425. (defun org-edit-special ()
  13426. "Call a special editor for the stuff at point.
  13427. When at a table, call the formula editor with `org-table-edit-formulas'.
  13428. When at the first line of an src example, call `org-edit-src-code'.
  13429. When in an #+include line, visit the include file. Otherwise call
  13430. `ffap' to visit the file at point."
  13431. (interactive)
  13432. (cond
  13433. ((org-at-table-p)
  13434. (call-interactively 'org-table-edit-formulas))
  13435. ((save-excursion
  13436. (beginning-of-line 1)
  13437. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13438. (find-file (org-trim (match-string 1))))
  13439. ((org-edit-src-code))
  13440. ((org-edit-fixed-width-region))
  13441. (t (call-interactively 'ffap))))
  13442. (defun org-ctrl-c-ctrl-c (&optional arg)
  13443. "Set tags in headline, or update according to changed information at point.
  13444. This command does many different things, depending on context:
  13445. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13446. this is what we do.
  13447. - If the cursor is in a headline, prompt for tags and insert them
  13448. into the current line, aligned to `org-tags-column'. When called
  13449. with prefix arg, realign all tags in the current buffer.
  13450. - If the cursor is in one of the special #+KEYWORD lines, this
  13451. triggers scanning the buffer for these lines and updating the
  13452. information.
  13453. - If the cursor is inside a table, realign the table. This command
  13454. works even if the automatic table editor has been turned off.
  13455. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13456. the entire table.
  13457. - If the cursor is at a footnote reference or definition, jump to
  13458. the corresponding definition or references, respectively.
  13459. - If the cursor is a the beginning of a dynamic block, update it.
  13460. - If the cursor is inside a table created by the table.el package,
  13461. activate that table.
  13462. - If the current buffer is a remember buffer, close note and file
  13463. it. A prefix argument of 1 files to the default location
  13464. without further interaction. A prefix argument of 2 files to
  13465. the currently clocking task.
  13466. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13467. links in this buffer.
  13468. - If the cursor is on a numbered item in a plain list, renumber the
  13469. ordered list.
  13470. - If the cursor is on a checkbox, toggle it."
  13471. (interactive "P")
  13472. (let ((org-enable-table-editor t))
  13473. (cond
  13474. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13475. org-occur-highlights
  13476. org-latex-fragment-image-overlays)
  13477. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13478. (org-remove-occur-highlights)
  13479. (org-remove-latex-fragment-image-overlays)
  13480. (message "Temporary highlights/overlays removed from current buffer"))
  13481. ((and (local-variable-p 'org-finish-function (current-buffer))
  13482. (fboundp org-finish-function))
  13483. (funcall org-finish-function))
  13484. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13485. ((org-at-property-p)
  13486. (call-interactively 'org-property-action))
  13487. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13488. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13489. ((org-at-table.el-p)
  13490. (require 'table)
  13491. (beginning-of-line 1)
  13492. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13493. (call-interactively 'table-recognize-table))
  13494. ((org-at-table-p)
  13495. (org-table-maybe-eval-formula)
  13496. (if arg
  13497. (call-interactively 'org-table-recalculate)
  13498. (org-table-maybe-recalculate-line))
  13499. (call-interactively 'org-table-align))
  13500. ((or (org-footnote-at-reference-p)
  13501. (org-footnote-at-definition-p))
  13502. (call-interactively 'org-footnote-action))
  13503. ((org-at-item-checkbox-p)
  13504. (call-interactively 'org-toggle-checkbox))
  13505. ((org-at-item-p)
  13506. (if arg
  13507. (call-interactively 'org-toggle-checkbox)
  13508. (call-interactively 'org-maybe-renumber-ordered-list)))
  13509. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13510. ;; Dynamic block
  13511. (beginning-of-line 1)
  13512. (save-excursion (org-update-dblock)))
  13513. ((save-excursion
  13514. (beginning-of-line 1)
  13515. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13516. (cond
  13517. ((equal (match-string 1) "TBLFM")
  13518. ;; Recalculate the table before this line
  13519. (save-excursion
  13520. (beginning-of-line 1)
  13521. (skip-chars-backward " \r\n\t")
  13522. (if (org-at-table-p)
  13523. (org-call-with-arg 'org-table-recalculate t))))
  13524. (t
  13525. ; (org-set-regexps-and-options)
  13526. ; (org-restart-font-lock)
  13527. (let ((org-inhibit-startup t)) (org-mode-restart))
  13528. (message "Local setup has been refreshed"))))
  13529. ((org-clock-update-time-maybe))
  13530. (t (error "C-c C-c can do nothing useful at this location.")))))
  13531. (defun org-mode-restart ()
  13532. "Restart Org-mode, to scan again for special lines.
  13533. Also updates the keyword regular expressions."
  13534. (interactive)
  13535. (org-mode)
  13536. (message "Org-mode restarted"))
  13537. (defun org-kill-note-or-show-branches ()
  13538. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13539. (interactive)
  13540. (if (not org-finish-function)
  13541. (call-interactively 'show-branches)
  13542. (let ((org-note-abort t))
  13543. (funcall org-finish-function))))
  13544. (defun org-return (&optional indent)
  13545. "Goto next table row or insert a newline.
  13546. Calls `org-table-next-row' or `newline', depending on context.
  13547. See the individual commands for more information."
  13548. (interactive)
  13549. (cond
  13550. ((bobp) (if indent (newline-and-indent) (newline)))
  13551. ((org-at-table-p)
  13552. (org-table-justify-field-maybe)
  13553. (call-interactively 'org-table-next-row))
  13554. ((and org-return-follows-link
  13555. (eq (get-text-property (point) 'face) 'org-link))
  13556. (call-interactively 'org-open-at-point))
  13557. ((and (org-at-heading-p)
  13558. (looking-at
  13559. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13560. (org-show-entry)
  13561. (end-of-line 1)
  13562. (newline))
  13563. (t (if indent (newline-and-indent) (newline)))))
  13564. (defun org-return-indent ()
  13565. "Goto next table row or insert a newline and indent.
  13566. Calls `org-table-next-row' or `newline-and-indent', depending on
  13567. context. See the individual commands for more information."
  13568. (interactive)
  13569. (org-return t))
  13570. (defun org-ctrl-c-star ()
  13571. "Compute table, or change heading status of lines.
  13572. Calls `org-table-recalculate' or `org-toggle-heading',
  13573. depending on context."
  13574. (interactive)
  13575. (cond
  13576. ((org-at-table-p)
  13577. (call-interactively 'org-table-recalculate))
  13578. (t
  13579. ;; Convert all lines in region to list items
  13580. (call-interactively 'org-toggle-heading))))
  13581. (defun org-ctrl-c-minus ()
  13582. "Insert separator line in table or modify bullet status of line.
  13583. Also turns a plain line or a region of lines into list items.
  13584. Calls `org-table-insert-hline', `org-toggle-item', or
  13585. `org-cycle-list-bullet', depending on context."
  13586. (interactive)
  13587. (cond
  13588. ((org-at-table-p)
  13589. (call-interactively 'org-table-insert-hline))
  13590. ((org-region-active-p)
  13591. (call-interactively 'org-toggle-item))
  13592. ((org-in-item-p)
  13593. (call-interactively 'org-cycle-list-bullet))
  13594. (t
  13595. (call-interactively 'org-toggle-item))))
  13596. (defun org-toggle-item ()
  13597. "Convert headings or normal lines to items, items to normal lines.
  13598. If there is no active region, only the current line is considered.
  13599. If the first line in the region is a headline, convert all headlines to items.
  13600. If the first line in the region is an item, convert all items to normal lines.
  13601. If the first line is normal text, add an item bullet to each line."
  13602. (interactive)
  13603. (let (l2 l beg end)
  13604. (if (org-region-active-p)
  13605. (setq beg (region-beginning) end (region-end))
  13606. (setq beg (point-at-bol)
  13607. end (min (1+ (point-at-eol)) (point-max))))
  13608. (save-excursion
  13609. (goto-char end)
  13610. (setq l2 (org-current-line))
  13611. (goto-char beg)
  13612. (beginning-of-line 1)
  13613. (setq l (1- (org-current-line)))
  13614. (if (org-at-item-p)
  13615. ;; We already have items, de-itemize
  13616. (while (< (setq l (1+ l)) l2)
  13617. (when (org-at-item-p)
  13618. (goto-char (match-beginning 2))
  13619. (delete-region (match-beginning 2) (match-end 2))
  13620. (and (looking-at "[ \t]+") (replace-match "")))
  13621. (beginning-of-line 2))
  13622. (if (org-on-heading-p)
  13623. ;; Headings, convert to items
  13624. (while (< (setq l (1+ l)) l2)
  13625. (if (looking-at org-outline-regexp)
  13626. (replace-match "- " t t))
  13627. (beginning-of-line 2))
  13628. ;; normal lines, turn them into items
  13629. (while (< (setq l (1+ l)) l2)
  13630. (unless (org-at-item-p)
  13631. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13632. (replace-match "\\1- \\2")))
  13633. (beginning-of-line 2)))))))
  13634. (defun org-toggle-heading (&optional nstars)
  13635. "Convert headings to normal text, or items or text to headings.
  13636. If there is no active region, only the current line is considered.
  13637. If the first line is a heading, remove the stars from all headlines
  13638. in the region.
  13639. If the first line is a plain list item, turn all plain list items
  13640. into headings.
  13641. If the first line is a normal line, turn each and every line in the
  13642. region into a heading.
  13643. When converting a line into a heading, the number of stars is chosen
  13644. such that the lines become children of the current entry. However,
  13645. when a prefix argument is given, its value determines the number of
  13646. stars to add."
  13647. (interactive "P")
  13648. (let (l2 l itemp beg end)
  13649. (if (org-region-active-p)
  13650. (setq beg (region-beginning) end (region-end))
  13651. (setq beg (point-at-bol)
  13652. end (min (1+ (point-at-eol)) (point-max))))
  13653. (save-excursion
  13654. (goto-char end)
  13655. (setq l2 (org-current-line))
  13656. (goto-char beg)
  13657. (beginning-of-line 1)
  13658. (setq l (1- (org-current-line)))
  13659. (if (org-on-heading-p)
  13660. ;; We already have headlines, de-star them
  13661. (while (< (setq l (1+ l)) l2)
  13662. (when (org-on-heading-p t)
  13663. (and (looking-at outline-regexp) (replace-match "")))
  13664. (beginning-of-line 2))
  13665. (setq itemp (org-at-item-p))
  13666. (let* ((stars
  13667. (if nstars
  13668. (make-string (prefix-numeric-value current-prefix-arg)
  13669. ?*)
  13670. (save-excursion
  13671. (if (re-search-backward org-complex-heading-regexp nil t)
  13672. (match-string 1) ""))))
  13673. (add-stars (cond (nstars "")
  13674. ((equal stars "") "*")
  13675. (org-odd-levels-only "**")
  13676. (t "*")))
  13677. (rpl (concat stars add-stars " ")))
  13678. (while (< (setq l (1+ l)) l2)
  13679. (if itemp
  13680. (and (org-at-item-p) (replace-match rpl t t))
  13681. (unless (org-on-heading-p)
  13682. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13683. (replace-match (concat rpl (match-string 2))))))
  13684. (beginning-of-line 2)))))))
  13685. (defun org-meta-return (&optional arg)
  13686. "Insert a new heading or wrap a region in a table.
  13687. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13688. See the individual commands for more information."
  13689. (interactive "P")
  13690. (cond
  13691. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13692. ((org-at-table-p)
  13693. (call-interactively 'org-table-wrap-region))
  13694. (t (call-interactively 'org-insert-heading))))
  13695. ;;; Menu entries
  13696. ;; Define the Org-mode menus
  13697. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13698. '("Tbl"
  13699. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13700. ["Next Field" org-cycle (org-at-table-p)]
  13701. ["Previous Field" org-shifttab (org-at-table-p)]
  13702. ["Next Row" org-return (org-at-table-p)]
  13703. "--"
  13704. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13705. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13706. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13707. "--"
  13708. ("Column"
  13709. ["Move Column Left" org-metaleft (org-at-table-p)]
  13710. ["Move Column Right" org-metaright (org-at-table-p)]
  13711. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13712. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13713. ("Row"
  13714. ["Move Row Up" org-metaup (org-at-table-p)]
  13715. ["Move Row Down" org-metadown (org-at-table-p)]
  13716. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13717. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13718. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13719. "--"
  13720. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13721. ("Rectangle"
  13722. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13723. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13724. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13725. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13726. "--"
  13727. ("Calculate"
  13728. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13729. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13730. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13731. "--"
  13732. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13733. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13734. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13735. "--"
  13736. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13737. "--"
  13738. ["Sum Column/Rectangle" org-table-sum
  13739. (or (org-at-table-p) (org-region-active-p))]
  13740. ["Which Column?" org-table-current-column (org-at-table-p)])
  13741. ["Debug Formulas"
  13742. org-table-toggle-formula-debugger
  13743. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13744. ["Show Col/Row Numbers"
  13745. org-table-toggle-coordinate-overlays
  13746. :style toggle
  13747. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13748. "--"
  13749. ["Create" org-table-create (and (not (org-at-table-p))
  13750. org-enable-table-editor)]
  13751. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13752. ["Import from File" org-table-import (not (org-at-table-p))]
  13753. ["Export to File" org-table-export (org-at-table-p)]
  13754. "--"
  13755. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13756. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13757. '("Org"
  13758. ("Show/Hide"
  13759. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13760. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13761. ["Sparse Tree..." org-sparse-tree t]
  13762. ["Reveal Context" org-reveal t]
  13763. ["Show All" show-all t]
  13764. "--"
  13765. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13766. "--"
  13767. ["New Heading" org-insert-heading t]
  13768. ("Navigate Headings"
  13769. ["Up" outline-up-heading t]
  13770. ["Next" outline-next-visible-heading t]
  13771. ["Previous" outline-previous-visible-heading t]
  13772. ["Next Same Level" outline-forward-same-level t]
  13773. ["Previous Same Level" outline-backward-same-level t]
  13774. "--"
  13775. ["Jump" org-goto t])
  13776. ("Edit Structure"
  13777. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13778. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13779. "--"
  13780. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13781. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13782. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13783. "--"
  13784. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13785. "--"
  13786. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13787. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13788. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13789. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13790. "--"
  13791. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13792. "--"
  13793. ["Convert to odd levels" org-convert-to-odd-levels t]
  13794. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13795. ("Editing"
  13796. ["Emphasis..." org-emphasize t]
  13797. ["Edit Source Example" org-edit-special t]
  13798. "--"
  13799. ["Footnote new/jump" org-footnote-action t]
  13800. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13801. ("Archive"
  13802. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13803. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13804. ; :active t :keys "C-u C-c C-x C-a"]
  13805. ["Sparse trees open ARCHIVE trees"
  13806. (setq org-sparse-tree-open-archived-trees
  13807. (not org-sparse-tree-open-archived-trees))
  13808. :style toggle :selected org-sparse-tree-open-archived-trees]
  13809. ["Cycling opens ARCHIVE trees"
  13810. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13811. :style toggle :selected org-cycle-open-archived-trees]
  13812. "--"
  13813. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13814. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13815. ; ["Check and Move Children" (org-archive-subtree '(4))
  13816. ; :active t :keys "C-u C-c C-x C-s"]
  13817. )
  13818. "--"
  13819. ("Hyperlinks"
  13820. ["Store Link (Global)" org-store-link t]
  13821. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13822. ["Insert Link" org-insert-link t]
  13823. ["Follow Link" org-open-at-point t]
  13824. "--"
  13825. ["Next link" org-next-link t]
  13826. ["Previous link" org-previous-link t]
  13827. "--"
  13828. ["Descriptive Links"
  13829. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13830. :style radio
  13831. :selected (member '(org-link) buffer-invisibility-spec)]
  13832. ["Literal Links"
  13833. (progn
  13834. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13835. :style radio
  13836. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13837. "--"
  13838. ("TODO Lists"
  13839. ["TODO/DONE/-" org-todo t]
  13840. ("Select keyword"
  13841. ["Next keyword" org-shiftright (org-on-heading-p)]
  13842. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13843. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13844. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13845. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13846. ["Show TODO Tree" org-show-todo-tree t]
  13847. ["Global TODO list" org-todo-list t]
  13848. "--"
  13849. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13850. :selected org-enforce-todo-dependencies :style toggle :active t]
  13851. "Settings for tree at point"
  13852. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13853. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13854. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13855. ["Do Children parallel" org-toggle-ordered-property :style radio
  13856. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13857. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13858. "--"
  13859. ["Set Priority" org-priority t]
  13860. ["Priority Up" org-shiftup t]
  13861. ["Priority Down" org-shiftdown t]
  13862. "--"
  13863. ["Get news from all feeds" org-feed-update-all t]
  13864. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13865. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13866. ("TAGS and Properties"
  13867. ["Set Tags" org-set-tags-command t]
  13868. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13869. "--"
  13870. ["Set property" org-set-property t]
  13871. ["Column view of properties" org-columns t]
  13872. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13873. ("Dates and Scheduling"
  13874. ["Timestamp" org-time-stamp t]
  13875. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13876. ("Change Date"
  13877. ["1 Day Later" org-shiftright t]
  13878. ["1 Day Earlier" org-shiftleft t]
  13879. ["1 ... Later" org-shiftup t]
  13880. ["1 ... Earlier" org-shiftdown t])
  13881. ["Compute Time Range" org-evaluate-time-range t]
  13882. ["Schedule Item" org-schedule t]
  13883. ["Deadline" org-deadline t]
  13884. "--"
  13885. ["Custom time format" org-toggle-time-stamp-overlays
  13886. :style radio :selected org-display-custom-times]
  13887. "--"
  13888. ["Goto Calendar" org-goto-calendar t]
  13889. ["Date from Calendar" org-date-from-calendar t]
  13890. "--"
  13891. ["Start/Restart Timer" org-timer-start t]
  13892. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13893. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13894. ["Insert Timer String" org-timer t]
  13895. ["Insert Timer Item" org-timer-item t])
  13896. ("Logging work"
  13897. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  13898. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  13899. ["Clock out" org-clock-out t]
  13900. ["Clock cancel" org-clock-cancel t]
  13901. "--"
  13902. ["Mark as default task" org-clock-mark-default-task t]
  13903. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  13904. ["Goto running clock" org-clock-goto t]
  13905. "--"
  13906. ["Display times" org-clock-display t]
  13907. ["Create clock table" org-clock-report t]
  13908. "--"
  13909. ["Record DONE time"
  13910. (progn (setq org-log-done (not org-log-done))
  13911. (message "Switching to %s will %s record a timestamp"
  13912. (car org-done-keywords)
  13913. (if org-log-done "automatically" "not")))
  13914. :style toggle :selected org-log-done])
  13915. "--"
  13916. ["Agenda Command..." org-agenda t]
  13917. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13918. ("File List for Agenda")
  13919. ("Special views current file"
  13920. ["TODO Tree" org-show-todo-tree t]
  13921. ["Check Deadlines" org-check-deadlines t]
  13922. ["Timeline" org-timeline t]
  13923. ["Tags/Property tree" org-match-sparse-tree t])
  13924. "--"
  13925. ["Export/Publish..." org-export t]
  13926. ("LaTeX"
  13927. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13928. :selected org-cdlatex-mode]
  13929. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13930. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13931. ["Modify math symbol" org-cdlatex-math-modify
  13932. (org-inside-LaTeX-fragment-p)]
  13933. ["Insert citation" org-reftex-citation t]
  13934. "--"
  13935. ["Export LaTeX fragments as images"
  13936. (if (featurep 'org-exp)
  13937. (setq org-export-with-LaTeX-fragments
  13938. (not org-export-with-LaTeX-fragments))
  13939. (require 'org-exp))
  13940. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13941. org-export-with-LaTeX-fragments)])
  13942. "--"
  13943. ("Documentation"
  13944. ["Show Version" org-version t]
  13945. ["Info Documentation" org-info t])
  13946. ("Customize"
  13947. ["Browse Org Group" org-customize t]
  13948. "--"
  13949. ["Expand This Menu" org-create-customize-menu
  13950. (fboundp 'customize-menu-create)])
  13951. "--"
  13952. ("Refresh/Reload"
  13953. ["Refresh setup current buffer" org-mode-restart t]
  13954. ["Reload Org (after update)" org-reload t]
  13955. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13956. ))
  13957. (defun org-info (&optional node)
  13958. "Read documentation for Org-mode in the info system.
  13959. With optional NODE, go directly to that node."
  13960. (interactive)
  13961. (info (format "(org)%s" (or node ""))))
  13962. (defun org-install-agenda-files-menu ()
  13963. (let ((bl (buffer-list)))
  13964. (save-excursion
  13965. (while bl
  13966. (set-buffer (pop bl))
  13967. (if (org-mode-p) (setq bl nil)))
  13968. (when (org-mode-p)
  13969. (easy-menu-change
  13970. '("Org") "File List for Agenda"
  13971. (append
  13972. (list
  13973. ["Edit File List" (org-edit-agenda-file-list) t]
  13974. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13975. ["Remove Current File from List" org-remove-file t]
  13976. ["Cycle through agenda files" org-cycle-agenda-files t]
  13977. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13978. "--")
  13979. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13980. ;;;; Documentation
  13981. ;;;###autoload
  13982. (defun org-require-autoloaded-modules ()
  13983. (interactive)
  13984. (mapc 'require
  13985. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13986. org-docbook org-exp org-html org-icalendar
  13987. org-id org-latex
  13988. org-publish org-remember org-table
  13989. org-timer org-xoxo)))
  13990. ;;;###autoload
  13991. (defun org-reload (&optional uncompiled)
  13992. "Reload all org lisp files.
  13993. With prefix arg UNCOMPILED, load the uncompiled versions."
  13994. (interactive "P")
  13995. (require 'find-func)
  13996. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13997. (dir-org (file-name-directory (org-find-library-name "org")))
  13998. (dir-org-contrib (ignore-errors
  13999. (file-name-directory
  14000. (org-find-library-name "org-contribdir"))))
  14001. (files
  14002. (append (directory-files dir-org t file-re)
  14003. (and dir-org-contrib
  14004. (directory-files dir-org-contrib t file-re))))
  14005. (remove-re (concat (if (featurep 'xemacs)
  14006. "org-colview" "org-colview-xemacs")
  14007. "\\'")))
  14008. (setq files (mapcar 'file-name-sans-extension files))
  14009. (setq files (mapcar
  14010. (lambda (x) (if (string-match remove-re x) nil x))
  14011. files))
  14012. (setq files (delq nil files))
  14013. (mapc
  14014. (lambda (f)
  14015. (when (featurep (intern (file-name-nondirectory f)))
  14016. (if (and (not uncompiled)
  14017. (file-exists-p (concat f ".elc")))
  14018. (load (concat f ".elc") nil nil t)
  14019. (load (concat f ".el") nil nil t))))
  14020. files))
  14021. (org-version))
  14022. ;;;###autoload
  14023. (defun org-customize ()
  14024. "Call the customize function with org as argument."
  14025. (interactive)
  14026. (org-load-modules-maybe)
  14027. (org-require-autoloaded-modules)
  14028. (customize-browse 'org))
  14029. (defun org-create-customize-menu ()
  14030. "Create a full customization menu for Org-mode, insert it into the menu."
  14031. (interactive)
  14032. (org-load-modules-maybe)
  14033. (org-require-autoloaded-modules)
  14034. (if (fboundp 'customize-menu-create)
  14035. (progn
  14036. (easy-menu-change
  14037. '("Org") "Customize"
  14038. `(["Browse Org group" org-customize t]
  14039. "--"
  14040. ,(customize-menu-create 'org)
  14041. ["Set" Custom-set t]
  14042. ["Save" Custom-save t]
  14043. ["Reset to Current" Custom-reset-current t]
  14044. ["Reset to Saved" Custom-reset-saved t]
  14045. ["Reset to Standard Settings" Custom-reset-standard t]))
  14046. (message "\"Org\"-menu now contains full customization menu"))
  14047. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14048. ;;;; Miscellaneous stuff
  14049. ;;; Generally useful functions
  14050. (defun org-find-text-property-in-string (prop s)
  14051. "Return the first non-nil value of property PROP in string S."
  14052. (or (get-text-property 0 prop s)
  14053. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14054. prop s)))
  14055. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14056. "Display the given MESSAGE as a warning."
  14057. (if (fboundp 'display-warning)
  14058. (display-warning 'org message
  14059. (if (featurep 'xemacs)
  14060. 'warning
  14061. :warning))
  14062. (let ((buf (get-buffer-create "*Org warnings*")))
  14063. (with-current-buffer buf
  14064. (goto-char (point-max))
  14065. (insert "Warning (Org): " message)
  14066. (unless (bolp)
  14067. (newline)))
  14068. (display-buffer buf)
  14069. (sit-for 0))))
  14070. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14071. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14072. (if (and marker (marker-buffer marker)
  14073. (buffer-live-p (marker-buffer marker)))
  14074. (progn
  14075. (switch-to-buffer (marker-buffer marker))
  14076. (if (or (> marker (point-max)) (< marker (point-min)))
  14077. (widen))
  14078. (goto-char marker)
  14079. (org-show-context 'org-goto))
  14080. (if bookmark
  14081. (bookmark-jump bookmark)
  14082. (error "Cannot find location"))))
  14083. (defun org-quote-csv-field (s)
  14084. "Quote field for inclusion in CSV material."
  14085. (if (string-match "[\",]" s)
  14086. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14087. s))
  14088. (defun org-plist-delete (plist property)
  14089. "Delete PROPERTY from PLIST.
  14090. This is in contrast to merely setting it to 0."
  14091. (let (p)
  14092. (while plist
  14093. (if (not (eq property (car plist)))
  14094. (setq p (plist-put p (car plist) (nth 1 plist))))
  14095. (setq plist (cddr plist)))
  14096. p))
  14097. (defun org-force-self-insert (N)
  14098. "Needed to enforce self-insert under remapping."
  14099. (interactive "p")
  14100. (self-insert-command N))
  14101. (defun org-string-width (s)
  14102. "Compute width of string, ignoring invisible characters.
  14103. This ignores character with invisibility property `org-link', and also
  14104. characters with property `org-cwidth', because these will become invisible
  14105. upon the next fontification round."
  14106. (let (b l)
  14107. (when (or (eq t buffer-invisibility-spec)
  14108. (assq 'org-link buffer-invisibility-spec))
  14109. (while (setq b (text-property-any 0 (length s)
  14110. 'invisible 'org-link s))
  14111. (setq s (concat (substring s 0 b)
  14112. (substring s (or (next-single-property-change
  14113. b 'invisible s) (length s)))))))
  14114. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14115. (setq s (concat (substring s 0 b)
  14116. (substring s (or (next-single-property-change
  14117. b 'org-cwidth s) (length s))))))
  14118. (setq l (string-width s) b -1)
  14119. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14120. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14121. l))
  14122. (defun org-get-indentation (&optional line)
  14123. "Get the indentation of the current line, interpreting tabs.
  14124. When LINE is given, assume it represents a line and compute its indentation."
  14125. (if line
  14126. (if (string-match "^ *" (org-remove-tabs line))
  14127. (match-end 0))
  14128. (save-excursion
  14129. (beginning-of-line 1)
  14130. (skip-chars-forward " \t")
  14131. (current-column))))
  14132. (defun org-remove-tabs (s &optional width)
  14133. "Replace tabulators in S with spaces.
  14134. Assumes that s is a single line, starting in column 0."
  14135. (setq width (or width tab-width))
  14136. (while (string-match "\t" s)
  14137. (setq s (replace-match
  14138. (make-string
  14139. (- (* width (/ (+ (match-beginning 0) width) width))
  14140. (match-beginning 0)) ?\ )
  14141. t t s)))
  14142. s)
  14143. (defun org-fix-indentation (line ind)
  14144. "Fix indentation in LINE.
  14145. IND is a cons cell with target and minimum indentation.
  14146. If the current indentation in LINE is smaller than the minimum,
  14147. leave it alone. If it is larger than ind, set it to the target."
  14148. (let* ((l (org-remove-tabs line))
  14149. (i (org-get-indentation l))
  14150. (i1 (car ind)) (i2 (cdr ind)))
  14151. (if (>= i i2) (setq l (substring line i2)))
  14152. (if (> i1 0)
  14153. (concat (make-string i1 ?\ ) l)
  14154. l)))
  14155. (defun org-remove-indentation (code &optional n)
  14156. "Remove the maximum common indentation from the lines in CODE.
  14157. N may optionally be the number of spaces to remove."
  14158. (with-temp-buffer
  14159. (insert code)
  14160. (org-do-remove-indentation n)
  14161. (buffer-string)))
  14162. (defun org-do-remove-indentation (&optional n)
  14163. "Remove the maximum common indentation from the buffer."
  14164. (untabify (point-min) (point-max))
  14165. (let ((min 10000) re)
  14166. (if n
  14167. (setq min n)
  14168. (goto-char (point-min))
  14169. (while (re-search-forward "^ *[^ \n]" nil t)
  14170. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14171. (unless (or (= min 0) (= min 10000))
  14172. (setq re (format "^ \\{%d\\}" min))
  14173. (goto-char (point-min))
  14174. (while (re-search-forward re nil t)
  14175. (replace-match "")
  14176. (end-of-line 1))
  14177. min)))
  14178. (defun org-base-buffer (buffer)
  14179. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14180. (if (not buffer)
  14181. buffer
  14182. (or (buffer-base-buffer buffer)
  14183. buffer)))
  14184. (defun org-trim (s)
  14185. "Remove whitespace at beginning and end of string."
  14186. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14187. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14188. s)
  14189. (defun org-wrap (string &optional width lines)
  14190. "Wrap string to either a number of lines, or a width in characters.
  14191. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14192. that costs. If there is a word longer than WIDTH, the text is actually
  14193. wrapped to the length of that word.
  14194. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14195. many lines, whatever width that takes.
  14196. The return value is a list of lines, without newlines at the end."
  14197. (let* ((words (org-split-string string "[ \t\n]+"))
  14198. (maxword (apply 'max (mapcar 'org-string-width words)))
  14199. w ll)
  14200. (cond (width
  14201. (org-do-wrap words (max maxword width)))
  14202. (lines
  14203. (setq w maxword)
  14204. (setq ll (org-do-wrap words maxword))
  14205. (if (<= (length ll) lines)
  14206. ll
  14207. (setq ll words)
  14208. (while (> (length ll) lines)
  14209. (setq w (1+ w))
  14210. (setq ll (org-do-wrap words w)))
  14211. ll))
  14212. (t (error "Cannot wrap this")))))
  14213. (defun org-do-wrap (words width)
  14214. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14215. (let (lines line)
  14216. (while words
  14217. (setq line (pop words))
  14218. (while (and words (< (+ (length line) (length (car words))) width))
  14219. (setq line (concat line " " (pop words))))
  14220. (setq lines (push line lines)))
  14221. (nreverse lines)))
  14222. (defun org-split-string (string &optional separators)
  14223. "Splits STRING into substrings at SEPARATORS.
  14224. No empty strings are returned if there are matches at the beginning
  14225. and end of string."
  14226. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14227. (start 0)
  14228. notfirst
  14229. (list nil))
  14230. (while (and (string-match rexp string
  14231. (if (and notfirst
  14232. (= start (match-beginning 0))
  14233. (< start (length string)))
  14234. (1+ start) start))
  14235. (< (match-beginning 0) (length string)))
  14236. (setq notfirst t)
  14237. (or (eq (match-beginning 0) 0)
  14238. (and (eq (match-beginning 0) (match-end 0))
  14239. (eq (match-beginning 0) start))
  14240. (setq list
  14241. (cons (substring string start (match-beginning 0))
  14242. list)))
  14243. (setq start (match-end 0)))
  14244. (or (eq start (length string))
  14245. (setq list
  14246. (cons (substring string start)
  14247. list)))
  14248. (nreverse list)))
  14249. (defun org-quote-vert (s)
  14250. "Replace \"|\" with \"\\vert\"."
  14251. (while (string-match "|" s)
  14252. (setq s (replace-match "\\vert" t t s)))
  14253. s)
  14254. (defun org-uuidgen-p (s)
  14255. "Is S an ID created by UUIDGEN?"
  14256. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14257. (defun org-context ()
  14258. "Return a list of contexts of the current cursor position.
  14259. If several contexts apply, all are returned.
  14260. Each context entry is a list with a symbol naming the context, and
  14261. two positions indicating start and end of the context. Possible
  14262. contexts are:
  14263. :headline anywhere in a headline
  14264. :headline-stars on the leading stars in a headline
  14265. :todo-keyword on a TODO keyword (including DONE) in a headline
  14266. :tags on the TAGS in a headline
  14267. :priority on the priority cookie in a headline
  14268. :item on the first line of a plain list item
  14269. :item-bullet on the bullet/number of a plain list item
  14270. :checkbox on the checkbox in a plain list item
  14271. :table in an org-mode table
  14272. :table-special on a special filed in a table
  14273. :table-table in a table.el table
  14274. :link on a hyperlink
  14275. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14276. :target on a <<target>>
  14277. :radio-target on a <<<radio-target>>>
  14278. :latex-fragment on a LaTeX fragment
  14279. :latex-preview on a LaTeX fragment with overlayed preview image
  14280. This function expects the position to be visible because it uses font-lock
  14281. faces as a help to recognize the following contexts: :table-special, :link,
  14282. and :keyword."
  14283. (let* ((f (get-text-property (point) 'face))
  14284. (faces (if (listp f) f (list f)))
  14285. (p (point)) clist o)
  14286. ;; First the large context
  14287. (cond
  14288. ((org-on-heading-p t)
  14289. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14290. (when (progn
  14291. (beginning-of-line 1)
  14292. (looking-at org-todo-line-tags-regexp))
  14293. (push (org-point-in-group p 1 :headline-stars) clist)
  14294. (push (org-point-in-group p 2 :todo-keyword) clist)
  14295. (push (org-point-in-group p 4 :tags) clist))
  14296. (goto-char p)
  14297. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14298. (if (looking-at "\\[#[A-Z0-9]\\]")
  14299. (push (org-point-in-group p 0 :priority) clist)))
  14300. ((org-at-item-p)
  14301. (push (org-point-in-group p 2 :item-bullet) clist)
  14302. (push (list :item (point-at-bol)
  14303. (save-excursion (org-end-of-item) (point)))
  14304. clist)
  14305. (and (org-at-item-checkbox-p)
  14306. (push (org-point-in-group p 0 :checkbox) clist)))
  14307. ((org-at-table-p)
  14308. (push (list :table (org-table-begin) (org-table-end)) clist)
  14309. (if (memq 'org-formula faces)
  14310. (push (list :table-special
  14311. (previous-single-property-change p 'face)
  14312. (next-single-property-change p 'face)) clist)))
  14313. ((org-at-table-p 'any)
  14314. (push (list :table-table) clist)))
  14315. (goto-char p)
  14316. ;; Now the small context
  14317. (cond
  14318. ((org-at-timestamp-p)
  14319. (push (org-point-in-group p 0 :timestamp) clist))
  14320. ((memq 'org-link faces)
  14321. (push (list :link
  14322. (previous-single-property-change p 'face)
  14323. (next-single-property-change p 'face)) clist))
  14324. ((memq 'org-special-keyword faces)
  14325. (push (list :keyword
  14326. (previous-single-property-change p 'face)
  14327. (next-single-property-change p 'face)) clist))
  14328. ((org-on-target-p)
  14329. (push (org-point-in-group p 0 :target) clist)
  14330. (goto-char (1- (match-beginning 0)))
  14331. (if (looking-at org-radio-target-regexp)
  14332. (push (org-point-in-group p 0 :radio-target) clist))
  14333. (goto-char p))
  14334. ((setq o (car (delq nil
  14335. (mapcar
  14336. (lambda (x)
  14337. (if (memq x org-latex-fragment-image-overlays) x))
  14338. (org-overlays-at (point))))))
  14339. (push (list :latex-fragment
  14340. (org-overlay-start o) (org-overlay-end o)) clist)
  14341. (push (list :latex-preview
  14342. (org-overlay-start o) (org-overlay-end o)) clist))
  14343. ((org-inside-LaTeX-fragment-p)
  14344. ;; FIXME: positions wrong.
  14345. (push (list :latex-fragment (point) (point)) clist)))
  14346. (setq clist (nreverse (delq nil clist)))
  14347. clist))
  14348. ;; FIXME: Compare with at-regexp-p Do we need both?
  14349. (defun org-in-regexp (re &optional nlines visually)
  14350. "Check if point is inside a match of regexp.
  14351. Normally only the current line is checked, but you can include NLINES extra
  14352. lines both before and after point into the search.
  14353. If VISUALLY is set, require that the cursor is not after the match but
  14354. really on, so that the block visually is on the match."
  14355. (catch 'exit
  14356. (let ((pos (point))
  14357. (eol (point-at-eol (+ 1 (or nlines 0))))
  14358. (inc (if visually 1 0)))
  14359. (save-excursion
  14360. (beginning-of-line (- 1 (or nlines 0)))
  14361. (while (re-search-forward re eol t)
  14362. (if (and (<= (match-beginning 0) pos)
  14363. (>= (+ inc (match-end 0)) pos))
  14364. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14365. (defun org-at-regexp-p (regexp)
  14366. "Is point inside a match of REGEXP in the current line?"
  14367. (catch 'exit
  14368. (save-excursion
  14369. (let ((pos (point)) (end (point-at-eol)))
  14370. (beginning-of-line 1)
  14371. (while (re-search-forward regexp end t)
  14372. (if (and (<= (match-beginning 0) pos)
  14373. (>= (match-end 0) pos))
  14374. (throw 'exit t)))
  14375. nil))))
  14376. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14377. "Call `multi-occur' with buffers for all agenda files."
  14378. (interactive "sOrg-files matching: \np")
  14379. (let* ((files (org-agenda-files))
  14380. (tnames (mapcar 'file-truename files))
  14381. (extra org-agenda-text-search-extra-files)
  14382. f)
  14383. (when (eq (car extra) 'agenda-archives)
  14384. (setq extra (cdr extra))
  14385. (setq files (org-add-archive-files files)))
  14386. (while (setq f (pop extra))
  14387. (unless (member (file-truename f) tnames)
  14388. (add-to-list 'files f 'append)
  14389. (add-to-list 'tnames (file-truename f) 'append)))
  14390. (multi-occur
  14391. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14392. regexp)))
  14393. (if (boundp 'occur-mode-find-occurrence-hook)
  14394. ;; Emacs 23
  14395. (add-hook 'occur-mode-find-occurrence-hook
  14396. (lambda ()
  14397. (when (org-mode-p)
  14398. (org-reveal))))
  14399. ;; Emacs 22
  14400. (defadvice occur-mode-goto-occurrence
  14401. (after org-occur-reveal activate)
  14402. (and (org-mode-p) (org-reveal)))
  14403. (defadvice occur-mode-goto-occurrence-other-window
  14404. (after org-occur-reveal activate)
  14405. (and (org-mode-p) (org-reveal)))
  14406. (defadvice occur-mode-display-occurrence
  14407. (after org-occur-reveal activate)
  14408. (when (org-mode-p)
  14409. (let ((pos (occur-mode-find-occurrence)))
  14410. (with-current-buffer (marker-buffer pos)
  14411. (save-excursion
  14412. (goto-char pos)
  14413. (org-reveal)))))))
  14414. (defun org-occur-link-in-agenda-files ()
  14415. "Create a link and search for it in the agendas.
  14416. The link is not stored in `org-stored-links', it is just created
  14417. for the search purpose."
  14418. (interactive)
  14419. (let ((link (condition-case nil
  14420. (org-store-link nil)
  14421. (error "Unable to create a link to here"))))
  14422. (org-occur-in-agenda-files (regexp-quote link))))
  14423. (defun org-uniquify (list)
  14424. "Remove duplicate elements from LIST."
  14425. (let (res)
  14426. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14427. res))
  14428. (defun org-delete-all (elts list)
  14429. "Remove all elements in ELTS from LIST."
  14430. (while elts
  14431. (setq list (delete (pop elts) list)))
  14432. list)
  14433. (defun org-back-over-empty-lines ()
  14434. "Move backwards over whitespace, to the beginning of the first empty line.
  14435. Returns the number of empty lines passed."
  14436. (let ((pos (point)))
  14437. (skip-chars-backward " \t\n\r")
  14438. (beginning-of-line 2)
  14439. (goto-char (min (point) pos))
  14440. (count-lines (point) pos)))
  14441. (defun org-skip-whitespace ()
  14442. (skip-chars-forward " \t\n\r"))
  14443. (defun org-point-in-group (point group &optional context)
  14444. "Check if POINT is in match-group GROUP.
  14445. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14446. match. If the match group does ot exist or point is not inside it,
  14447. return nil."
  14448. (and (match-beginning group)
  14449. (>= point (match-beginning group))
  14450. (<= point (match-end group))
  14451. (if context
  14452. (list context (match-beginning group) (match-end group))
  14453. t)))
  14454. (defun org-switch-to-buffer-other-window (&rest args)
  14455. "Switch to buffer in a second window on the current frame.
  14456. In particular, do not allow pop-up frames."
  14457. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14458. special-display-function)
  14459. (apply 'switch-to-buffer-other-window args)))
  14460. (defun org-combine-plists (&rest plists)
  14461. "Create a single property list from all plists in PLISTS.
  14462. The process starts by copying the first list, and then setting properties
  14463. from the other lists. Settings in the last list are the most significant
  14464. ones and overrule settings in the other lists."
  14465. (let ((rtn (copy-sequence (pop plists)))
  14466. p v ls)
  14467. (while plists
  14468. (setq ls (pop plists))
  14469. (while ls
  14470. (setq p (pop ls) v (pop ls))
  14471. (setq rtn (plist-put rtn p v))))
  14472. rtn))
  14473. (defun org-move-line-down (arg)
  14474. "Move the current line down. With prefix argument, move it past ARG lines."
  14475. (interactive "p")
  14476. (let ((col (current-column))
  14477. beg end pos)
  14478. (beginning-of-line 1) (setq beg (point))
  14479. (beginning-of-line 2) (setq end (point))
  14480. (beginning-of-line (+ 1 arg))
  14481. (setq pos (move-marker (make-marker) (point)))
  14482. (insert (delete-and-extract-region beg end))
  14483. (goto-char pos)
  14484. (org-move-to-column col)))
  14485. (defun org-move-line-up (arg)
  14486. "Move the current line up. With prefix argument, move it past ARG lines."
  14487. (interactive "p")
  14488. (let ((col (current-column))
  14489. beg end pos)
  14490. (beginning-of-line 1) (setq beg (point))
  14491. (beginning-of-line 2) (setq end (point))
  14492. (beginning-of-line (- arg))
  14493. (setq pos (move-marker (make-marker) (point)))
  14494. (insert (delete-and-extract-region beg end))
  14495. (goto-char pos)
  14496. (org-move-to-column col)))
  14497. (defun org-replace-escapes (string table)
  14498. "Replace %-escapes in STRING with values in TABLE.
  14499. TABLE is an association list with keys like \"%a\" and string values.
  14500. The sequences in STRING may contain normal field width and padding information,
  14501. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14502. so values can contain further %-escapes if they are define later in TABLE."
  14503. (let ((case-fold-search nil)
  14504. e re rpl)
  14505. (while (setq e (pop table))
  14506. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14507. (while (string-match re string)
  14508. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14509. (cdr e)))
  14510. (setq string (replace-match rpl t t string))))
  14511. string))
  14512. (defun org-sublist (list start end)
  14513. "Return a section of LIST, from START to END.
  14514. Counting starts at 1."
  14515. (let (rtn (c start))
  14516. (setq list (nthcdr (1- start) list))
  14517. (while (and list (<= c end))
  14518. (push (pop list) rtn)
  14519. (setq c (1+ c)))
  14520. (nreverse rtn)))
  14521. (defun org-find-base-buffer-visiting (file)
  14522. "Like `find-buffer-visiting' but always return the base buffer and
  14523. not an indirect buffer."
  14524. (let ((buf (or (get-file-buffer file)
  14525. (find-buffer-visiting file))))
  14526. (if buf
  14527. (or (buffer-base-buffer buf) buf)
  14528. nil)))
  14529. (defun org-image-file-name-regexp (&optional extensions)
  14530. "Return regexp matching the file names of images.
  14531. If EXTENSIONS is given, only match these."
  14532. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14533. (image-file-name-regexp)
  14534. (let ((image-file-name-extensions
  14535. (or extensions
  14536. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14537. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14538. (concat "\\."
  14539. (regexp-opt (nconc (mapcar 'upcase
  14540. image-file-name-extensions)
  14541. image-file-name-extensions)
  14542. t)
  14543. "\\'"))))
  14544. (defun org-file-image-p (file &optional extensions)
  14545. "Return non-nil if FILE is an image."
  14546. (save-match-data
  14547. (string-match (org-image-file-name-regexp extensions) file)))
  14548. (defun org-get-cursor-date ()
  14549. "Return the date at cursor in as a time.
  14550. This works in the calendar and in the agenda, anywhere else it just
  14551. returns the current time."
  14552. (let (date day defd)
  14553. (cond
  14554. ((eq major-mode 'calendar-mode)
  14555. (setq date (calendar-cursor-to-date)
  14556. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14557. ((eq major-mode 'org-agenda-mode)
  14558. (setq day (get-text-property (point) 'day))
  14559. (if day
  14560. (setq date (calendar-gregorian-from-absolute day)
  14561. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14562. (nth 2 date))))))
  14563. (or defd (current-time))))
  14564. (defvar org-agenda-action-marker (make-marker)
  14565. "Marker pointing to the entry for the next agenda action.")
  14566. (defun org-mark-entry-for-agenda-action ()
  14567. "Mark the current entry as target of an agenda action.
  14568. Agenda actions are actions executed from the agenda with the key `k',
  14569. which make use of the date at the cursor."
  14570. (interactive)
  14571. (move-marker org-agenda-action-marker
  14572. (save-excursion (org-back-to-heading t) (point))
  14573. (current-buffer))
  14574. (message
  14575. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14576. ;;; Paragraph filling stuff.
  14577. ;; We want this to be just right, so use the full arsenal.
  14578. (defun org-indent-line-function ()
  14579. "Indent line like previous, but further if previous was headline or item."
  14580. (interactive)
  14581. (let* ((pos (point))
  14582. (itemp (org-at-item-p))
  14583. (case-fold-search t)
  14584. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14585. column bpos bcol tpos tcol bullet btype bullet-type)
  14586. ;; Find the previous relevant line
  14587. (beginning-of-line 1)
  14588. (cond
  14589. ((looking-at "#") (setq column 0))
  14590. ((looking-at "\\*+ ") (setq column 0))
  14591. ((and (looking-at "[ \t]*:END:")
  14592. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14593. (save-excursion
  14594. (goto-char (1- (match-beginning 1)))
  14595. (setq column (current-column))))
  14596. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14597. (save-excursion
  14598. (re-search-backward
  14599. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14600. (setq column (org-get-indentation (match-string 0))))
  14601. (t
  14602. (beginning-of-line 0)
  14603. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14604. (not (looking-at "[ \t]*:END:"))
  14605. (not (looking-at org-drawer-regexp)))
  14606. (beginning-of-line 0))
  14607. (cond
  14608. ((looking-at "\\*+[ \t]+")
  14609. (if (not org-adapt-indentation)
  14610. (setq column 0)
  14611. (goto-char (match-end 0))
  14612. (setq column (current-column))))
  14613. ((looking-at org-drawer-regexp)
  14614. (goto-char (1- (match-beginning 1)))
  14615. (setq column (current-column)))
  14616. ((looking-at "\\([ \t]*\\):END:")
  14617. (goto-char (match-end 1))
  14618. (setq column (current-column)))
  14619. ((org-in-item-p)
  14620. (org-beginning-of-item)
  14621. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14622. (setq bpos (match-beginning 1) tpos (match-end 0)
  14623. bcol (progn (goto-char bpos) (current-column))
  14624. tcol (progn (goto-char tpos) (current-column))
  14625. bullet (match-string 1)
  14626. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14627. (if (> tcol (+ bcol org-description-max-indent))
  14628. (setq tcol (+ bcol 5)))
  14629. (if (not itemp)
  14630. (setq column tcol)
  14631. (goto-char pos)
  14632. (beginning-of-line 1)
  14633. (if (looking-at "\\S-")
  14634. (progn
  14635. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14636. (setq bullet (match-string 1)
  14637. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14638. (setq column (if (equal btype bullet-type) bcol tcol)))
  14639. (setq column (org-get-indentation)))))
  14640. (t (setq column (org-get-indentation))))))
  14641. (goto-char pos)
  14642. (if (<= (current-column) (current-indentation))
  14643. (org-indent-line-to column)
  14644. (save-excursion (org-indent-line-to column)))
  14645. (setq column (current-column))
  14646. (beginning-of-line 1)
  14647. (if (looking-at
  14648. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14649. (replace-match (concat "\\1" (format org-property-format
  14650. (match-string 2) (match-string 3)))
  14651. t nil))
  14652. (org-move-to-column column)))
  14653. (defun org-set-autofill-regexps ()
  14654. (interactive)
  14655. ;; In the paragraph separator we include headlines, because filling
  14656. ;; text in a line directly attached to a headline would otherwise
  14657. ;; fill the headline as well.
  14658. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14659. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14660. ;; The paragraph starter includes hand-formatted lists.
  14661. (org-set-local
  14662. 'paragraph-start
  14663. (concat
  14664. "\f" "\\|"
  14665. "[ ]*$" "\\|"
  14666. "\\*+ " "\\|"
  14667. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14668. "[ \t]*[:|]" "\\|"
  14669. "\\$\\$" "\\|"
  14670. "\\\\\\(begin\\|end\\|[][]\\)"))
  14671. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14672. ;; But only if the user has not turned off tables or fixed-width regions
  14673. (org-set-local
  14674. 'auto-fill-inhibit-regexp
  14675. (concat "\\*+ \\|#\\+"
  14676. "\\|[ \t]*" org-keyword-time-regexp
  14677. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14678. (concat
  14679. "\\|[ \t]*["
  14680. (if org-enable-table-editor "|" "")
  14681. (if org-enable-fixed-width-editor ":" "")
  14682. "]"))))
  14683. ;; We use our own fill-paragraph function, to make sure that tables
  14684. ;; and fixed-width regions are not wrapped. That function will pass
  14685. ;; through to `fill-paragraph' when appropriate.
  14686. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14687. ; Adaptive filling: To get full control, first make sure that
  14688. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14689. (org-set-local 'adaptive-fill-regexp "\000")
  14690. (org-set-local 'adaptive-fill-function
  14691. 'org-adaptive-fill-function)
  14692. (org-set-local
  14693. 'align-mode-rules-list
  14694. '((org-in-buffer-settings
  14695. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14696. (modes . '(org-mode))))))
  14697. (defun org-fill-paragraph (&optional justify)
  14698. "Re-align a table, pass through to fill-paragraph if no table."
  14699. (let ((table-p (org-at-table-p))
  14700. (table.el-p (org-at-table.el-p)))
  14701. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14702. (save-excursion (goto-char (point-at-bol))
  14703. (looking-at outline-regexp)))
  14704. t) ; skip headlines
  14705. (table.el-p t) ; skip table.el tables
  14706. (table-p (org-table-align) t) ; align org-mode tables
  14707. (t nil)))) ; call paragraph-fill
  14708. ;; For reference, this is the default value of adaptive-fill-regexp
  14709. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14710. (defun org-adaptive-fill-function ()
  14711. "Return a fill prefix for org-mode files.
  14712. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14713. work correctly."
  14714. (cond ((looking-at "#[ \t]+")
  14715. (match-string 0))
  14716. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14717. (save-excursion
  14718. (if (> (match-end 1) (+ (match-beginning 1)
  14719. org-description-max-indent))
  14720. (goto-char (+ (match-beginning 1) 5))
  14721. (goto-char (match-end 0)))
  14722. (make-string (current-column) ?\ )))
  14723. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14724. (save-excursion
  14725. (goto-char (match-end 0))
  14726. (make-string (current-column) ?\ )))
  14727. (t nil)))
  14728. ;;; Other stuff.
  14729. (defun org-toggle-fixed-width-section (arg)
  14730. "Toggle the fixed-width export.
  14731. If there is no active region, the QUOTE keyword at the current headline is
  14732. inserted or removed. When present, it causes the text between this headline
  14733. and the next to be exported as fixed-width text, and unmodified.
  14734. If there is an active region, this command adds or removes a colon as the
  14735. first character of this line. If the first character of a line is a colon,
  14736. this line is also exported in fixed-width font."
  14737. (interactive "P")
  14738. (let* ((cc 0)
  14739. (regionp (org-region-active-p))
  14740. (beg (if regionp (region-beginning) (point)))
  14741. (end (if regionp (region-end)))
  14742. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14743. (case-fold-search nil)
  14744. (re "[ \t]*\\(: \\)")
  14745. off)
  14746. (if regionp
  14747. (save-excursion
  14748. (goto-char beg)
  14749. (setq cc (current-column))
  14750. (beginning-of-line 1)
  14751. (setq off (looking-at re))
  14752. (while (> nlines 0)
  14753. (setq nlines (1- nlines))
  14754. (beginning-of-line 1)
  14755. (cond
  14756. (arg
  14757. (org-move-to-column cc t)
  14758. (insert ": \n")
  14759. (forward-line -1))
  14760. ((and off (looking-at re))
  14761. (replace-match "" t t nil 1))
  14762. ((not off) (org-move-to-column cc t) (insert ": ")))
  14763. (forward-line 1)))
  14764. (save-excursion
  14765. (org-back-to-heading)
  14766. (if (looking-at (concat outline-regexp
  14767. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14768. (replace-match "" t t nil 1)
  14769. (if (looking-at outline-regexp)
  14770. (progn
  14771. (goto-char (match-end 0))
  14772. (insert org-quote-string " "))))))))
  14773. (defun org-reftex-citation ()
  14774. "Use reftex-citation to insert a citation into the buffer.
  14775. This looks for a line like
  14776. #+BIBLIOGRAPHY: foo plain option:-d
  14777. and derives from it that foo.bib is the bbliography file relevant
  14778. for this document. It then installs the necessary environment for RefTeX
  14779. to work in this buffer and calls `reftex-citation' to insert a citation
  14780. into the buffer.
  14781. Export of such citations to both LaTeX and HTML is handled by the contributed
  14782. package org-exp-bibtex by Taru Karttunen."
  14783. (interactive)
  14784. (let ((reftex-docstruct-symbol 'rds)
  14785. (reftex-cite-format "\\cite{%l}")
  14786. rds bib)
  14787. (save-excursion
  14788. (save-restriction
  14789. (widen)
  14790. (let ((case-fold-search t)
  14791. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14792. (if (not (save-excursion
  14793. (or (re-search-forward re nil t)
  14794. (re-search-backward re nil t))))
  14795. (error "No bibliography defined in file")
  14796. (setq bib (concat (match-string 1) ".bib")
  14797. rds (list (list 'bib bib)))))))
  14798. (call-interactively 'reftex-citation)))
  14799. ;;;; Functions extending outline functionality
  14800. (defun org-beginning-of-line (&optional arg)
  14801. "Go to the beginning of the current line. If that is invisible, continue
  14802. to a visible line beginning. This makes the function of C-a more intuitive.
  14803. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14804. first attempt, and only move to after the tags when the cursor is already
  14805. beyond the end of the headline."
  14806. (interactive "P")
  14807. (let ((pos (point))
  14808. (special (if (consp org-special-ctrl-a/e)
  14809. (car org-special-ctrl-a/e)
  14810. org-special-ctrl-a/e))
  14811. refpos)
  14812. (if (org-bound-and-true-p line-move-visual)
  14813. (beginning-of-visual-line 1)
  14814. (beginning-of-line 1))
  14815. (if (and arg (fboundp 'move-beginning-of-line))
  14816. (call-interactively 'move-beginning-of-line)
  14817. (if (bobp)
  14818. nil
  14819. (backward-char 1)
  14820. (if (org-invisible-p)
  14821. (while (and (not (bobp)) (org-invisible-p))
  14822. (backward-char 1)
  14823. (beginning-of-line 1))
  14824. (forward-char 1))))
  14825. (when special
  14826. (cond
  14827. ((and (looking-at org-complex-heading-regexp)
  14828. (= (char-after (match-end 1)) ?\ ))
  14829. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14830. (point-at-eol)))
  14831. (goto-char
  14832. (if (eq special t)
  14833. (cond ((> pos refpos) refpos)
  14834. ((= pos (point)) refpos)
  14835. (t (point)))
  14836. (cond ((> pos (point)) (point))
  14837. ((not (eq last-command this-command)) (point))
  14838. (t refpos)))))
  14839. ((org-at-item-p)
  14840. (goto-char
  14841. (if (eq special t)
  14842. (cond ((> pos (match-end 4)) (match-end 4))
  14843. ((= pos (point)) (match-end 4))
  14844. (t (point)))
  14845. (cond ((> pos (point)) (point))
  14846. ((not (eq last-command this-command)) (point))
  14847. (t (match-end 4))))))))
  14848. (org-no-warnings
  14849. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14850. (defun org-end-of-line (&optional arg)
  14851. "Go to the end of the line.
  14852. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14853. first attempt, and only move to after the tags when the cursor is already
  14854. beyond the end of the headline."
  14855. (interactive "P")
  14856. (let ((special (if (consp org-special-ctrl-a/e)
  14857. (cdr org-special-ctrl-a/e)
  14858. org-special-ctrl-a/e)))
  14859. (if (or (not special)
  14860. (not (org-on-heading-p))
  14861. arg)
  14862. (call-interactively
  14863. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  14864. ((fboundp 'move-end-of-line) 'move-end-of-line)
  14865. (t 'end-of-line)))
  14866. (let ((pos (point)))
  14867. (beginning-of-line 1)
  14868. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14869. (if (eq special t)
  14870. (if (or (< pos (match-beginning 1))
  14871. (= pos (match-end 0)))
  14872. (goto-char (match-beginning 1))
  14873. (goto-char (match-end 0)))
  14874. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14875. (goto-char (match-end 0))
  14876. (goto-char (match-beginning 1))))
  14877. (call-interactively (if (fboundp 'move-end-of-line)
  14878. 'move-end-of-line
  14879. 'end-of-line)))))
  14880. (org-no-warnings
  14881. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14882. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14883. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14884. (defun org-backward-sentence (&optional arg)
  14885. "Go to beginning of sentence, or beginning of table field.
  14886. This will call `backward-sentence' or `org-table-beginning-of-field',
  14887. depending on context."
  14888. (interactive "P")
  14889. (cond
  14890. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14891. (t (call-interactively 'backward-sentence))))
  14892. (defun org-forward-sentence (&optional arg)
  14893. "Go to end of sentence, or end of table field.
  14894. This will call `forward-sentence' or `org-table-end-of-field',
  14895. depending on context."
  14896. (interactive "P")
  14897. (cond
  14898. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14899. (t (call-interactively 'forward-sentence))))
  14900. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14901. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14902. (defun org-kill-line (&optional arg)
  14903. "Kill line, to tags or end of line."
  14904. (interactive "P")
  14905. (cond
  14906. ((or (not org-special-ctrl-k)
  14907. (bolp)
  14908. (not (org-on-heading-p)))
  14909. (call-interactively 'kill-line))
  14910. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14911. (kill-region (point) (match-beginning 1))
  14912. (org-set-tags nil t))
  14913. (t (kill-region (point) (point-at-eol)))))
  14914. (define-key org-mode-map "\C-k" 'org-kill-line)
  14915. (defun org-yank (&optional arg)
  14916. "Yank. If the kill is a subtree, treat it specially.
  14917. This command will look at the current kill and check if is a single
  14918. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14919. cursor is at the beginning of a line or after the stars of a currently
  14920. empty headline, then the yank is handled specially. How exactly depends
  14921. on the value of the following variables, both set by default.
  14922. org-yank-folded-subtrees
  14923. When set, the subtree(s) will be folded after insertion, but only
  14924. if doing so would now swallow text after the yanked text.
  14925. org-yank-adjusted-subtrees
  14926. When set, the subtree will be promoted or demoted in order to
  14927. fit into the local outline tree structure, which means that the level
  14928. will be adjusted so that it becomes the smaller one of the two
  14929. *visible* surrounding headings.
  14930. Any prefix to this command will cause `yank' to be called directly with
  14931. no special treatment. In particular, a simple `C-u' prefix will just
  14932. plainly yank the text as it is.
  14933. \[1] The test checks if the first non-white line is a heading
  14934. and if there are no other headings with fewer stars."
  14935. (interactive "P")
  14936. (org-yank-generic 'yank arg))
  14937. (defun org-yank-generic (command arg)
  14938. "Perform some yank-like command.
  14939. This function implements the behavior described in the `org-yank'
  14940. documentation. However, it has been generalized to work for any
  14941. interactive command with similar behavior."
  14942. ;; pretend to be command COMMAND
  14943. (setq this-command command)
  14944. (if arg
  14945. (call-interactively command)
  14946. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14947. (and (org-kill-is-subtree-p)
  14948. (or (bolp)
  14949. (and (looking-at "[ \t]*$")
  14950. (string-match
  14951. "\\`\\*+\\'"
  14952. (buffer-substring (point-at-bol) (point)))))))
  14953. swallowp)
  14954. (cond
  14955. ((and subtreep org-yank-folded-subtrees)
  14956. (let ((beg (point))
  14957. end)
  14958. (if (and subtreep org-yank-adjusted-subtrees)
  14959. (org-paste-subtree nil nil 'for-yank)
  14960. (call-interactively command))
  14961. (setq end (point))
  14962. (goto-char beg)
  14963. (when (and (bolp) subtreep
  14964. (not (setq swallowp
  14965. (org-yank-folding-would-swallow-text beg end))))
  14966. (or (looking-at outline-regexp)
  14967. (re-search-forward (concat "^" outline-regexp) end t))
  14968. (while (and (< (point) end) (looking-at outline-regexp))
  14969. (hide-subtree)
  14970. (org-cycle-show-empty-lines 'folded)
  14971. (condition-case nil
  14972. (outline-forward-same-level 1)
  14973. (error (goto-char end)))))
  14974. (when swallowp
  14975. (message
  14976. "Inserted text not folded because that would swallow text"))
  14977. (goto-char end)
  14978. (skip-chars-forward " \t\n\r")
  14979. (beginning-of-line 1)
  14980. (push-mark beg 'nomsg)))
  14981. ((and subtreep org-yank-adjusted-subtrees)
  14982. (let ((beg (point-at-bol)))
  14983. (org-paste-subtree nil nil 'for-yank)
  14984. (push-mark beg 'nomsg)))
  14985. (t
  14986. (call-interactively command))))))
  14987. (defun org-yank-folding-would-swallow-text (beg end)
  14988. "Would hide-subtree at BEG swallow any text after END?"
  14989. (let (level)
  14990. (save-excursion
  14991. (goto-char beg)
  14992. (when (or (looking-at outline-regexp)
  14993. (re-search-forward (concat "^" outline-regexp) end t))
  14994. (setq level (org-outline-level)))
  14995. (goto-char end)
  14996. (skip-chars-forward " \t\r\n\v\f")
  14997. (if (or (eobp)
  14998. (and (bolp) (looking-at org-outline-regexp)
  14999. (<= (org-outline-level) level)))
  15000. nil ; Nothing would be swallowed
  15001. t)))) ; something would swallow
  15002. (define-key org-mode-map "\C-y" 'org-yank)
  15003. (defun org-invisible-p ()
  15004. "Check if point is at a character currently not visible."
  15005. ;; Early versions of noutline don't have `outline-invisible-p'.
  15006. (if (fboundp 'outline-invisible-p)
  15007. (outline-invisible-p)
  15008. (get-char-property (point) 'invisible)))
  15009. (defun org-invisible-p2 ()
  15010. "Check if point is at a character currently not visible."
  15011. (save-excursion
  15012. (if (and (eolp) (not (bobp))) (backward-char 1))
  15013. ;; Early versions of noutline don't have `outline-invisible-p'.
  15014. (if (fboundp 'outline-invisible-p)
  15015. (outline-invisible-p)
  15016. (get-char-property (point) 'invisible))))
  15017. (defun org-back-to-heading (&optional invisible-ok)
  15018. "Call `outline-back-to-heading', but provide a better error message."
  15019. (condition-case nil
  15020. (outline-back-to-heading invisible-ok)
  15021. (error (error "Before first headline at position %d in buffer %s"
  15022. (point) (current-buffer)))))
  15023. (defun org-before-first-heading-p ()
  15024. "Before first heading?"
  15025. (save-excursion
  15026. (null (re-search-backward "^\\*+ " nil t))))
  15027. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15028. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15029. (defun org-at-heading-or-item-p ()
  15030. (or (org-on-heading-p) (org-at-item-p)))
  15031. (defun org-on-target-p ()
  15032. (or (org-in-regexp org-radio-target-regexp)
  15033. (org-in-regexp org-target-regexp)))
  15034. (defun org-up-heading-all (arg)
  15035. "Move to the heading line of which the present line is a subheading.
  15036. This function considers both visible and invisible heading lines.
  15037. With argument, move up ARG levels."
  15038. (if (fboundp 'outline-up-heading-all)
  15039. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15040. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15041. (defun org-up-heading-safe ()
  15042. "Move to the heading line of which the present line is a subheading.
  15043. This version will not throw an error. It will return the level of the
  15044. headline found, or nil if no higher level is found.
  15045. Also, this function will be a lot faster than `outline-up-heading',
  15046. because it relies on stars being the outline starters. This can really
  15047. make a significant difference in outlines with very many siblings."
  15048. (let (start-level re)
  15049. (org-back-to-heading t)
  15050. (setq start-level (funcall outline-level))
  15051. (if (equal start-level 1)
  15052. nil
  15053. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15054. (if (re-search-backward re nil t)
  15055. (funcall outline-level)))))
  15056. (defun org-first-sibling-p ()
  15057. "Is this heading the first child of its parents?"
  15058. (interactive)
  15059. (let ((re (concat "^" outline-regexp))
  15060. level l)
  15061. (unless (org-at-heading-p t)
  15062. (error "Not at a heading"))
  15063. (setq level (funcall outline-level))
  15064. (save-excursion
  15065. (if (not (re-search-backward re nil t))
  15066. t
  15067. (setq l (funcall outline-level))
  15068. (< l level)))))
  15069. (defun org-goto-sibling (&optional previous)
  15070. "Goto the next sibling, even if it is invisible.
  15071. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15072. when a sibling was found. When none is found, return nil and don't
  15073. move point."
  15074. (let ((fun (if previous 're-search-backward 're-search-forward))
  15075. (pos (point))
  15076. (re (concat "^" outline-regexp))
  15077. level l)
  15078. (when (condition-case nil (org-back-to-heading t) (error nil))
  15079. (setq level (funcall outline-level))
  15080. (catch 'exit
  15081. (or previous (forward-char 1))
  15082. (while (funcall fun re nil t)
  15083. (setq l (funcall outline-level))
  15084. (when (< l level) (goto-char pos) (throw 'exit nil))
  15085. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15086. (goto-char pos)
  15087. nil))))
  15088. (defun org-show-siblings ()
  15089. "Show all siblings of the current headline."
  15090. (save-excursion
  15091. (while (org-goto-sibling) (org-flag-heading nil)))
  15092. (save-excursion
  15093. (while (org-goto-sibling 'previous)
  15094. (org-flag-heading nil))))
  15095. (defun org-show-hidden-entry ()
  15096. "Show an entry where even the heading is hidden."
  15097. (save-excursion
  15098. (org-show-entry)))
  15099. (defun org-flag-heading (flag &optional entry)
  15100. "Flag the current heading. FLAG non-nil means make invisible.
  15101. When ENTRY is non-nil, show the entire entry."
  15102. (save-excursion
  15103. (org-back-to-heading t)
  15104. ;; Check if we should show the entire entry
  15105. (if entry
  15106. (progn
  15107. (org-show-entry)
  15108. (save-excursion
  15109. (and (outline-next-heading)
  15110. (org-flag-heading nil))))
  15111. (outline-flag-region (max (point-min) (1- (point)))
  15112. (save-excursion (outline-end-of-heading) (point))
  15113. flag))))
  15114. (defun org-get-next-sibling ()
  15115. "Move to next heading of the same level, and return point.
  15116. If there is no such heading, return nil.
  15117. This is like outline-next-sibling, but invisible headings are ok."
  15118. (let ((level (funcall outline-level)))
  15119. (outline-next-heading)
  15120. (while (and (not (eobp)) (> (funcall outline-level) level))
  15121. (outline-next-heading))
  15122. (if (or (eobp) (< (funcall outline-level) level))
  15123. nil
  15124. (point))))
  15125. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15126. ;; This contains an exact copy of the original function, but it uses
  15127. ;; `org-back-to-heading', to make it work also in invisible
  15128. ;; trees. And is uses an invisible-OK argument.
  15129. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15130. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15131. ;; with many children and grandchildren etc, this can be much faster
  15132. ;; than the outline version.
  15133. (org-back-to-heading invisible-OK)
  15134. (let ((first t)
  15135. (level (funcall outline-level)))
  15136. (if (and (org-mode-p) (< level 1000))
  15137. ;; A true heading (not a plain list item), in Org-mode
  15138. ;; This means we can easily find the end by looking
  15139. ;; only for the right number of stars. Using a regexp to do
  15140. ;; this is so much faster than using a Lisp loop.
  15141. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15142. (forward-char 1)
  15143. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15144. ;; something else, do it the slow way
  15145. (while (and (not (eobp))
  15146. (or first (> (funcall outline-level) level)))
  15147. (setq first nil)
  15148. (outline-next-heading)))
  15149. (unless to-heading
  15150. (if (memq (preceding-char) '(?\n ?\^M))
  15151. (progn
  15152. ;; Go to end of line before heading
  15153. (forward-char -1)
  15154. (if (memq (preceding-char) '(?\n ?\^M))
  15155. ;; leave blank line before heading
  15156. (forward-char -1))))))
  15157. (point))
  15158. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15159. "Use Org version in org-mode, for dramatic speed-up."
  15160. (if (eq major-mode 'org-mode)
  15161. (org-end-of-subtree)
  15162. ad-do-it))
  15163. (defun org-forward-same-level (arg &optional invisible-ok)
  15164. "Move forward to the arg'th subheading at same level as this one.
  15165. Stop at the first and last subheadings of a superior heading."
  15166. (interactive "p")
  15167. (org-back-to-heading)
  15168. (org-on-heading-p)
  15169. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15170. (re (format "^\\*\\{1,%d\\} " level))
  15171. l)
  15172. (forward-char 1)
  15173. (while (> arg 0)
  15174. (while (and (re-search-forward re nil 'move)
  15175. (setq l (- (match-end 0) (match-beginning 0) 1))
  15176. (= l level)
  15177. (not invisible-ok)
  15178. (org-invisible-p))
  15179. (if (< l level) (setq arg 1)))
  15180. (setq arg (1- arg)))
  15181. (beginning-of-line 1)))
  15182. (defun org-backward-same-level (arg &optional invisible-ok)
  15183. "Move backward to the arg'th subheading at same level as this one.
  15184. Stop at the first and last subheadings of a superior heading."
  15185. (interactive "p")
  15186. (org-back-to-heading)
  15187. (org-on-heading-p)
  15188. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15189. (re (format "^\\*\\{1,%d\\} " level))
  15190. l)
  15191. (while (> arg 0)
  15192. (while (and (re-search-backward re nil 'move)
  15193. (setq l (- (match-end 0) (match-beginning 0) 1))
  15194. (= l level)
  15195. (not invisible-ok)
  15196. (org-invisible-p))
  15197. (if (< l level) (setq arg 1)))
  15198. (setq arg (1- arg)))))
  15199. (defun org-show-subtree ()
  15200. "Show everything after this heading at deeper levels."
  15201. (outline-flag-region
  15202. (point)
  15203. (save-excursion
  15204. (outline-end-of-subtree) (outline-next-heading) (point))
  15205. nil))
  15206. (defun org-show-entry ()
  15207. "Show the body directly following this heading.
  15208. Show the heading too, if it is currently invisible."
  15209. (interactive)
  15210. (save-excursion
  15211. (condition-case nil
  15212. (progn
  15213. (org-back-to-heading t)
  15214. (outline-flag-region
  15215. (max (point-min) (1- (point)))
  15216. (save-excursion
  15217. (if (re-search-forward
  15218. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15219. (match-beginning 1)
  15220. (point-max)))
  15221. nil)
  15222. (org-cycle-hide-drawers 'children))
  15223. (error nil))))
  15224. (defun org-make-options-regexp (kwds &optional extra)
  15225. "Make a regular expression for keyword lines."
  15226. (concat
  15227. "^"
  15228. "#?[ \t]*\\+\\("
  15229. (mapconcat 'regexp-quote kwds "\\|")
  15230. (if extra (concat "\\|" extra))
  15231. "\\):[ \t]*"
  15232. "\\(.*\\)"))
  15233. ;; Make isearch reveal the necessary context
  15234. (defun org-isearch-end ()
  15235. "Reveal context after isearch exits."
  15236. (when isearch-success ; only if search was successful
  15237. (if (featurep 'xemacs)
  15238. ;; Under XEmacs, the hook is run in the correct place,
  15239. ;; we directly show the context.
  15240. (org-show-context 'isearch)
  15241. ;; In Emacs the hook runs *before* restoring the overlays.
  15242. ;; So we have to use a one-time post-command-hook to do this.
  15243. ;; (Emacs 22 has a special variable, see function `org-mode')
  15244. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15245. isearch-mode-end-hook-quit)
  15246. ;; Only when the isearch was not quitted.
  15247. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15248. 'append 'local)))))
  15249. (defun org-isearch-post-command ()
  15250. "Remove self from hook, and show context."
  15251. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15252. (org-show-context 'isearch))
  15253. ;;;; Integration with and fixes for other packages
  15254. ;;; Imenu support
  15255. (defvar org-imenu-markers nil
  15256. "All markers currently used by Imenu.")
  15257. (make-variable-buffer-local 'org-imenu-markers)
  15258. (defun org-imenu-new-marker (&optional pos)
  15259. "Return a new marker for use by Imenu, and remember the marker."
  15260. (let ((m (make-marker)))
  15261. (move-marker m (or pos (point)))
  15262. (push m org-imenu-markers)
  15263. m))
  15264. (defun org-imenu-get-tree ()
  15265. "Produce the index for Imenu."
  15266. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15267. (setq org-imenu-markers nil)
  15268. (let* ((n org-imenu-depth)
  15269. (re (concat "^" outline-regexp))
  15270. (subs (make-vector (1+ n) nil))
  15271. (last-level 0)
  15272. m level head)
  15273. (save-excursion
  15274. (save-restriction
  15275. (widen)
  15276. (goto-char (point-max))
  15277. (while (re-search-backward re nil t)
  15278. (setq level (org-reduced-level (funcall outline-level)))
  15279. (when (<= level n)
  15280. (looking-at org-complex-heading-regexp)
  15281. (setq head (org-link-display-format
  15282. (org-match-string-no-properties 4))
  15283. m (org-imenu-new-marker))
  15284. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15285. (if (>= level last-level)
  15286. (push (cons head m) (aref subs level))
  15287. (push (cons head (aref subs (1+ level))) (aref subs level))
  15288. (loop for i from (1+ level) to n do (aset subs i nil)))
  15289. (setq last-level level)))))
  15290. (aref subs 1)))
  15291. (eval-after-load "imenu"
  15292. '(progn
  15293. (add-hook 'imenu-after-jump-hook
  15294. (lambda ()
  15295. (if (eq major-mode 'org-mode)
  15296. (org-show-context 'org-goto))))))
  15297. (defun org-link-display-format (link)
  15298. "Replace a link with either the description, or the link target
  15299. if no description is present"
  15300. (save-match-data
  15301. (if (string-match org-bracket-link-analytic-regexp link)
  15302. (replace-match (or (match-string 5 link)
  15303. (concat (match-string 1 link)
  15304. (match-string 3 link)))
  15305. nil nil link)
  15306. link)))
  15307. ;; Speedbar support
  15308. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15309. "Overlay marking the agenda restriction line in speedbar.")
  15310. (org-overlay-put org-speedbar-restriction-lock-overlay
  15311. 'face 'org-agenda-restriction-lock)
  15312. (org-overlay-put org-speedbar-restriction-lock-overlay
  15313. 'help-echo "Agendas are currently limited to this item.")
  15314. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15315. (defun org-speedbar-set-agenda-restriction ()
  15316. "Restrict future agenda commands to the location at point in speedbar.
  15317. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15318. (interactive)
  15319. (require 'org-agenda)
  15320. (let (p m tp np dir txt)
  15321. (cond
  15322. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15323. 'org-imenu t))
  15324. (setq m (get-text-property p 'org-imenu-marker))
  15325. (save-excursion
  15326. (save-restriction
  15327. (set-buffer (marker-buffer m))
  15328. (goto-char m)
  15329. (org-agenda-set-restriction-lock 'subtree))))
  15330. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15331. 'speedbar-function 'speedbar-find-file))
  15332. (setq tp (previous-single-property-change
  15333. (1+ p) 'speedbar-function)
  15334. np (next-single-property-change
  15335. tp 'speedbar-function)
  15336. dir (speedbar-line-directory)
  15337. txt (buffer-substring-no-properties (or tp (point-min))
  15338. (or np (point-max))))
  15339. (save-excursion
  15340. (save-restriction
  15341. (set-buffer (find-file-noselect
  15342. (let ((default-directory dir))
  15343. (expand-file-name txt))))
  15344. (unless (org-mode-p)
  15345. (error "Cannot restrict to non-Org-mode file"))
  15346. (org-agenda-set-restriction-lock 'file))))
  15347. (t (error "Don't know how to restrict Org-mode's agenda")))
  15348. (org-move-overlay org-speedbar-restriction-lock-overlay
  15349. (point-at-bol) (point-at-eol))
  15350. (setq current-prefix-arg nil)
  15351. (org-agenda-maybe-redo)))
  15352. (eval-after-load "speedbar"
  15353. '(progn
  15354. (speedbar-add-supported-extension ".org")
  15355. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15356. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15357. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15358. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15359. (add-hook 'speedbar-visiting-tag-hook
  15360. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15361. ;;; Fixes and Hacks for problems with other packages
  15362. ;; Make flyspell not check words in links, to not mess up our keymap
  15363. (defun org-mode-flyspell-verify ()
  15364. "Don't let flyspell put overlays at active buttons."
  15365. (and (not (get-text-property (point) 'keymap))
  15366. (not (get-text-property (point) 'org-no-flyspell))))
  15367. (defun org-remove-flyspell-overlays-in (beg end)
  15368. "Remove flyspell overlays in region."
  15369. (and (org-bound-and-true-p flyspell-mode)
  15370. (fboundp 'flyspell-delete-region-overlays)
  15371. (flyspell-delete-region-overlays beg end))
  15372. (add-text-properties beg end '(org-no-flyspell t)))
  15373. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15374. (eval-after-load "bookmark"
  15375. '(if (boundp 'bookmark-after-jump-hook)
  15376. ;; We can use the hook
  15377. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15378. ;; Hook not available, use advice
  15379. (defadvice bookmark-jump (after org-make-visible activate)
  15380. "Make the position visible."
  15381. (org-bookmark-jump-unhide))))
  15382. ;; Make sure saveplace show the location if it was hidden
  15383. (eval-after-load "saveplace"
  15384. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15385. "Make the position visible."
  15386. (org-bookmark-jump-unhide)))
  15387. (defun org-bookmark-jump-unhide ()
  15388. "Unhide the current position, to show the bookmark location."
  15389. (and (org-mode-p)
  15390. (or (org-invisible-p)
  15391. (save-excursion (goto-char (max (point-min) (1- (point))))
  15392. (org-invisible-p)))
  15393. (org-show-context 'bookmark-jump)))
  15394. ;; Make session.el ignore our circular variable
  15395. (eval-after-load "session"
  15396. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15397. ;;;; Experimental code
  15398. (defun org-closed-in-range ()
  15399. "Sparse tree of items closed in a certain time range.
  15400. Still experimental, may disappear in the future."
  15401. (interactive)
  15402. ;; Get the time interval from the user.
  15403. (let* ((time1 (time-to-seconds
  15404. (org-read-date nil 'to-time nil "Starting date: ")))
  15405. (time2 (time-to-seconds
  15406. (org-read-date nil 'to-time nil "End date:")))
  15407. ;; callback function
  15408. (callback (lambda ()
  15409. (let ((time
  15410. (time-to-seconds
  15411. (apply 'encode-time
  15412. (org-parse-time-string
  15413. (match-string 1))))))
  15414. ;; check if time in interval
  15415. (and (>= time time1) (<= time time2))))))
  15416. ;; make tree, check each match with the callback
  15417. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15418. ;;;; Finish up
  15419. (provide 'org)
  15420. (run-hooks 'org-load-hook)
  15421. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15422. ;;; org.el ends here