org.el 630 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917
  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.27trans
  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-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.27trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (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)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  185. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  186. (defcustom org-support-shift-select nil
  187. "Non-nil means, make shift-cursor commands select text when possible.
  188. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  189. selecting a region, or enlarge thusly regions started in this way.
  190. In Org-mode, in special contexts, these same keys are used for other
  191. purposes, important enough to compete with shift selection. Org tries
  192. to balance these needs by supporting `shift-select-mode' outside these
  193. special contexts, under control of this variable.
  194. The default of this variable is nil, to avoid confusing behavior. Shifted
  195. cursor keys will then execute Org commands in the following contexts:
  196. - on a headline, changing TODO state (left/right) and priority (up/down)
  197. - on a time stamp, changing the time
  198. - in a plain list item, changing the bullet type
  199. - in a property definition line, switching between allowed values
  200. - in the BEGIN line of a clock table (changing the time block).
  201. Outside these contexts, the commands will throw an error.
  202. When this variable is t and the cursor is not in a special context,
  203. Org-mode will support shift-selection for making and enlarging regions.
  204. To make this more effective, the bullet cycling will no longer happen
  205. anywhere in an item line, but only if the cursor is exactly on the bullet.
  206. If you set this variable to the symbol `always', then the keys
  207. will not be special in headlines, property lines, and item lines, to make
  208. shift selection work there as well. If this is what you want, you can
  209. use the following alternative commands: `C-c C-t' and `C-c ,' to
  210. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  211. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  212. edited by hand or in column view.
  213. However, when the cursor is on a timestamp, shift-cursor commands
  214. will still edit the time stamp - this is just too good to give up.
  215. XEmacs user should have this variable set to nil, because shift-select-mode
  216. is Emacs 23 only."
  217. :group 'org
  218. :type '(choice
  219. (const :tag "Never" nil)
  220. (const :tag "When outside special context" t)
  221. (const :tag "Everywhere except timestamps" always)))
  222. (defgroup org-startup nil
  223. "Options concerning startup of Org-mode."
  224. :tag "Org Startup"
  225. :group 'org)
  226. (defcustom org-startup-folded t
  227. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  228. This can also be configured on a per-file basis by adding one of
  229. the following lines anywhere in the buffer:
  230. #+STARTUP: fold
  231. #+STARTUP: nofold
  232. #+STARTUP: content"
  233. :group 'org-startup
  234. :type '(choice
  235. (const :tag "nofold: show all" nil)
  236. (const :tag "fold: overview" t)
  237. (const :tag "content: all headlines" content)))
  238. (defcustom org-startup-truncated t
  239. "Non-nil means, entering Org-mode will set `truncate-lines'.
  240. This is useful since some lines containing links can be very long and
  241. uninteresting. Also tables look terrible when wrapped."
  242. :group 'org-startup
  243. :type 'boolean)
  244. (defcustom org-startup-align-all-tables nil
  245. "Non-nil means, align all tables when visiting a file.
  246. This is useful when the column width in tables is forced with <N> cookies
  247. in table fields. Such tables will look correct only after the first re-align.
  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: align
  251. #+STARTUP: noalign"
  252. :group 'org-startup
  253. :type 'boolean)
  254. (defcustom org-insert-mode-line-in-empty-file nil
  255. "Non-nil means insert the first line setting Org-mode in empty files.
  256. When the function `org-mode' is called interactively in an empty file, this
  257. normally means that the file name does not automatically trigger Org-mode.
  258. To ensure that the file will always be in Org-mode in the future, a
  259. line enforcing Org-mode will be inserted into the buffer, if this option
  260. has been set."
  261. :group 'org-startup
  262. :type 'boolean)
  263. (defcustom org-replace-disputed-keys nil
  264. "Non-nil means use alternative key bindings for some keys.
  265. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  266. These keys are also used by other packages like shift-selection-mode'
  267. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  268. If you want to use Org-mode together with one of these other modes,
  269. or more generally if you would like to move some Org-mode commands to
  270. other keys, set this variable and configure the keys with the variable
  271. `org-disputed-keys'.
  272. This option is only relevant at load-time of Org-mode, and must be set
  273. *before* org.el is loaded. Changing it requires a restart of Emacs to
  274. become effective."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-use-extra-keys nil
  278. "Non-nil means use extra key sequence definitions for certain
  279. commands. This happens automatically if you run XEmacs or if
  280. window-system is nil. This variable lets you do the same
  281. manually. You must set it before loading org.
  282. Example: on Carbon Emacs 22 running graphically, with an external
  283. keyboard on a Powerbook, the default way of setting M-left might
  284. not work for either Alt or ESC. Setting this variable will make
  285. it work for ESC."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (if (fboundp 'defvaralias)
  289. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  290. (defcustom org-disputed-keys
  291. '(([(shift up)] . [(meta p)])
  292. ([(shift down)] . [(meta n)])
  293. ([(shift left)] . [(meta -)])
  294. ([(shift right)] . [(meta +)])
  295. ([(control shift right)] . [(meta shift +)])
  296. ([(control shift left)] . [(meta shift -)]))
  297. "Keys for which Org-mode and other modes compete.
  298. This is an alist, cars are the default keys, second element specifies
  299. the alternative to use when `org-replace-disputed-keys' is t.
  300. Keys can be specified in any syntax supported by `define-key'.
  301. The value of this option takes effect only at Org-mode's startup,
  302. therefore you'll have to restart Emacs to apply it after changing."
  303. :group 'org-startup
  304. :type 'alist)
  305. (defun org-key (key)
  306. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  307. Or return the original if not disputed."
  308. (if org-replace-disputed-keys
  309. (let* ((nkey (key-description key))
  310. (x (org-find-if (lambda (x)
  311. (equal (key-description (car x)) nkey))
  312. org-disputed-keys)))
  313. (if x (cdr x) key))
  314. key))
  315. (defun org-find-if (predicate seq)
  316. (catch 'exit
  317. (while seq
  318. (if (funcall predicate (car seq))
  319. (throw 'exit (car seq))
  320. (pop seq)))))
  321. (defun org-defkey (keymap key def)
  322. "Define a key, possibly translated, as returned by `org-key'."
  323. (define-key keymap (org-key key) def))
  324. (defcustom org-ellipsis nil
  325. "The ellipsis to use in the Org-mode outline.
  326. When nil, just use the standard three dots. When a string, use that instead,
  327. When a face, use the standard 3 dots, but with the specified face.
  328. The change affects only Org-mode (which will then use its own display table).
  329. Changing this requires executing `M-x org-mode' in a buffer to become
  330. effective."
  331. :group 'org-startup
  332. :type '(choice (const :tag "Default" nil)
  333. (face :tag "Face" :value org-warning)
  334. (string :tag "String" :value "...#")))
  335. (defvar org-display-table nil
  336. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  337. (defgroup org-keywords nil
  338. "Keywords in Org-mode."
  339. :tag "Org Keywords"
  340. :group 'org)
  341. (defcustom org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-scheduled-string "SCHEDULED:"
  350. "String to mark scheduled TODO entries.
  351. A schedule is this string, followed by a time stamp. Should be a word,
  352. terminated by a colon. You can insert a schedule keyword and
  353. a timestamp with \\[org-schedule].
  354. Changes become only effective after restarting Emacs."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-closed-string "CLOSED:"
  358. "String used as the prefix for timestamps logging closing a TODO entry."
  359. :group 'org-keywords
  360. :type 'string)
  361. (defcustom org-clock-string "CLOCK:"
  362. "String used as prefix for timestamps clocking work hours on an item."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-comment-string "COMMENT"
  366. "Entries starting with this keyword will never be exported.
  367. An entry can be toggled between COMMENT and normal with
  368. \\[org-toggle-comment].
  369. Changes become only effective after restarting Emacs."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defcustom org-quote-string "QUOTE"
  373. "Entries starting with this keyword will be exported in fixed-width font.
  374. Quoting applies only to the text in the entry following the headline, and does
  375. not extend beyond the next headline, even if that is lower level.
  376. An entry can be toggled between QUOTE and normal with
  377. \\[org-toggle-fixed-width-section]."
  378. :group 'org-keywords
  379. :type 'string)
  380. (defconst org-repeat-re
  381. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  382. "Regular expression for specifying repeated events.
  383. After a match, group 1 contains the repeat expression.")
  384. (defgroup org-structure nil
  385. "Options concerning the general structure of Org-mode files."
  386. :tag "Org Structure"
  387. :group 'org)
  388. (defgroup org-reveal-location nil
  389. "Options about how to make context of a location visible."
  390. :tag "Org Reveal Location"
  391. :group 'org-structure)
  392. (defconst org-context-choice
  393. '(choice
  394. (const :tag "Always" t)
  395. (const :tag "Never" nil)
  396. (repeat :greedy t :tag "Individual contexts"
  397. (cons
  398. (choice :tag "Context"
  399. (const agenda)
  400. (const org-goto)
  401. (const occur-tree)
  402. (const tags-tree)
  403. (const link-search)
  404. (const mark-goto)
  405. (const bookmark-jump)
  406. (const isearch)
  407. (const default))
  408. (boolean))))
  409. "Contexts for the reveal options.")
  410. (defcustom org-show-hierarchy-above '((default . t))
  411. "Non-nil means, show full hierarchy when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the hierarchy of headings
  414. above the exposed location is shown.
  415. Turning this off for example for sparse trees makes them very compact.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. Valid contexts are
  418. agenda when exposing an entry from the agenda
  419. org-goto when using the command `org-goto' on key C-c C-j
  420. occur-tree when using the command `org-occur' on key C-c /
  421. tags-tree when constructing a sparse tree based on tags matches
  422. link-search when exposing search matches associated with a link
  423. mark-goto when exposing the jump goal of a mark
  424. bookmark-jump when exposing a bookmark location
  425. isearch when exiting from an incremental search
  426. default default for all contexts not set explicitly"
  427. :group 'org-reveal-location
  428. :type org-context-choice)
  429. (defcustom org-show-following-heading '((default . nil))
  430. "Non-nil means, show following heading when revealing a location.
  431. Org-mode often shows locations in an org-mode file which might have
  432. been invisible before. When this is set, the heading following the
  433. match is shown.
  434. Turning this off for example for sparse trees makes them very compact,
  435. but makes it harder to edit the location of the match. In such a case,
  436. use the command \\[org-reveal] to show more context.
  437. Instead of t, this can also be an alist specifying this option for different
  438. contexts. See `org-show-hierarchy-above' for valid contexts."
  439. :group 'org-reveal-location
  440. :type org-context-choice)
  441. (defcustom org-show-siblings '((default . nil) (isearch t))
  442. "Non-nil means, show all sibling 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 sibling of the current entry
  445. heading are all made visible. If `org-show-hierarchy-above' is t,
  446. the same happens on each level of the hierarchy above the current entry.
  447. By default this is on for the isearch context, off for all other contexts.
  448. Turning this off for example for sparse trees makes them very compact,
  449. but makes it harder to edit the location of the match. In such a case,
  450. use the command \\[org-reveal] to show more context.
  451. Instead of t, this can also be an alist specifying this option for different
  452. contexts. See `org-show-hierarchy-above' for valid contexts."
  453. :group 'org-reveal-location
  454. :type org-context-choice)
  455. (defcustom org-show-entry-below '((default . nil))
  456. "Non-nil means, show the entry below a headline when revealing a location.
  457. Org-mode often shows locations in an org-mode file which might have
  458. been invisible before. When this is set, the text below the headline that is
  459. exposed is also shown.
  460. By default this is off for all contexts.
  461. Instead of t, this can also be an alist specifying this option for different
  462. contexts. See `org-show-hierarchy-above' for valid contexts."
  463. :group 'org-reveal-location
  464. :type org-context-choice)
  465. (defcustom org-indirect-buffer-display 'other-window
  466. "How should indirect tree buffers be displayed?
  467. This applies to indirect buffers created with the commands
  468. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  469. Valid values are:
  470. current-window Display in the current window
  471. other-window Just display in another window.
  472. dedicated-frame Create one new frame, and re-use it each time.
  473. new-frame Make a new frame each time. Note that in this case
  474. previously-made indirect buffers are kept, and you need to
  475. kill these buffers yourself."
  476. :group 'org-structure
  477. :group 'org-agenda-windows
  478. :type '(choice
  479. (const :tag "In current window" current-window)
  480. (const :tag "In current frame, other window" other-window)
  481. (const :tag "Each time a new frame" new-frame)
  482. (const :tag "One dedicated frame" dedicated-frame)))
  483. (defgroup org-cycle nil
  484. "Options concerning visibility cycling in Org-mode."
  485. :tag "Org Cycle"
  486. :group 'org-structure)
  487. (defcustom org-cycle-max-level nil
  488. "Maximum level which should still be subject to visibility cycling.
  489. Levels higher than this will, for cycling, be treated as text, not a headline.
  490. When `org-odd-levels-only' is set, a value of N in this variable actually
  491. means 2N-1 stars as the limiting headline.
  492. When nil, cycle all levels.
  493. Note that the limiting level of cycling is also influenced by
  494. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  495. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  496. than its value."
  497. :group 'org-cycle
  498. :type '(choice
  499. (const :tag "No limit" nil)
  500. (integer :tag "Maximum level")))
  501. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  502. "Names of drawers. Drawers are not opened by cycling on the headline above.
  503. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  504. this:
  505. :DRAWERNAME:
  506. .....
  507. :END:
  508. The drawer \"PROPERTIES\" is special for capturing properties through
  509. the property API.
  510. Drawers can be defined on the per-file basis with a line like:
  511. #+DRAWERS: HIDDEN STATE PROPERTIES"
  512. :group 'org-structure
  513. :group 'org-cycle
  514. :type '(repeat (string :tag "Drawer Name")))
  515. (defcustom org-cycle-global-at-bob nil
  516. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  517. This makes it possible to do global cycling without having to use S-TAB or
  518. C-u TAB. For this special case to work, the first line of the buffer
  519. must not be a headline - it may be empty or some other text. When used in
  520. this way, `org-cycle-hook' is disables temporarily, to make sure the
  521. cursor stays at the beginning of the buffer.
  522. When this option is nil, don't do anything special at the beginning
  523. of the buffer."
  524. :group 'org-cycle
  525. :type 'boolean)
  526. (defcustom org-cycle-emulate-tab t
  527. "Where should `org-cycle' emulate TAB.
  528. nil Never
  529. white Only in completely white lines
  530. whitestart Only at the beginning of lines, before the first non-white char
  531. t Everywhere except in headlines
  532. exc-hl-bol Everywhere except at the start of a headline
  533. If TAB is used in a place where it does not emulate TAB, the current subtree
  534. visibility is cycled."
  535. :group 'org-cycle
  536. :type '(choice (const :tag "Never" nil)
  537. (const :tag "Only in completely white lines" white)
  538. (const :tag "Before first char in a line" whitestart)
  539. (const :tag "Everywhere except in headlines" t)
  540. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  541. ))
  542. (defcustom org-cycle-separator-lines 2
  543. "Number of empty lines needed to keep an empty line between collapsed trees.
  544. If you leave an empty line between the end of a subtree and the following
  545. headline, this empty line is hidden when the subtree is folded.
  546. Org-mode will leave (exactly) one empty line visible if the number of
  547. empty lines is equal or larger to the number given in this variable.
  548. So the default 2 means, at least 2 empty lines after the end of a subtree
  549. are needed to produce free space between a collapsed subtree and the
  550. following headline.
  551. Special case: when 0, never leave empty lines in collapsed view."
  552. :group 'org-cycle
  553. :type 'integer)
  554. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  555. (defcustom org-pre-cycle-hook nil
  556. "Hook that is run before visibility cycling is happening.
  557. The function(s) in this hook must accept a single argument which indicates
  558. the new state that will be set right after running this hook. The
  559. argument is a symbol. Before a global state change, it can have the values
  560. `overview', `content', or `all'. Before a local state change, it can have
  561. the values `folded', `children', or `subtree'."
  562. :group 'org-cycle
  563. :type 'hook)
  564. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  565. org-cycle-hide-drawers
  566. org-cycle-show-empty-lines
  567. org-optimize-window-after-visibility-change)
  568. "Hook that is run after `org-cycle' has changed the buffer visibility.
  569. The function(s) in this hook must accept a single argument which indicates
  570. the new state that was set by the most recent `org-cycle' command. The
  571. argument is a symbol. After a global state change, it can have the values
  572. `overview', `content', or `all'. After a local state change, it can have
  573. the values `folded', `children', or `subtree'."
  574. :group 'org-cycle
  575. :type 'hook)
  576. (defgroup org-edit-structure nil
  577. "Options concerning structure editing in Org-mode."
  578. :tag "Org Edit Structure"
  579. :group 'org-structure)
  580. (defcustom org-odd-levels-only nil
  581. "Non-nil means, skip even levels and only use odd levels for the outline.
  582. This has the effect that two stars are being added/taken away in
  583. promotion/demotion commands. It also influences how levels are
  584. handled by the exporters.
  585. Changing it requires restart of `font-lock-mode' to become effective
  586. for fontification also in regions already fontified.
  587. You may also set this on a per-file basis by adding one of the following
  588. lines to the buffer:
  589. #+STARTUP: odd
  590. #+STARTUP: oddeven"
  591. :group 'org-edit-structure
  592. :group 'org-font-lock
  593. :type 'boolean)
  594. (defcustom org-adapt-indentation t
  595. "Non-nil means, adapt indentation when promoting and demoting.
  596. When this is set and the *entire* text in an entry is indented, the
  597. indentation is increased by one space in a demotion command, and
  598. decreased by one in a promotion command. If any line in the entry
  599. body starts at column 0, indentation is not changed at all."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-special-ctrl-a/e nil
  603. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  604. When t, `C-a' will bring back the cursor to the beginning of the
  605. headline text, i.e. after the stars and after a possible TODO keyword.
  606. In an item, this will be the position after the bullet.
  607. When the cursor is already at that position, another `C-a' will bring
  608. it to the beginning of the line.
  609. `C-e' will jump to the end of the headline, ignoring the presence of tags
  610. in the headline. A second `C-e' will then jump to the true end of the
  611. line, after any tags.
  612. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  613. and only a directly following, identical keypress will bring the cursor
  614. to the special positions.
  615. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  616. set separately."
  617. :group 'org-edit-structure
  618. :type '(choice
  619. (const :tag "off" nil)
  620. (const :tag "after stars/bullet and before tags first" t)
  621. (const :tag "true line boundary first" reversed)
  622. (cons :tag "Set C-a and C-e separately"
  623. (choice :tag "Special C-a"
  624. (const :tag "off" nil)
  625. (const :tag "after stars/bullet first" t)
  626. (const :tag "before stars/bullet first" reversed))
  627. (choice :tag "Special C-e"
  628. (const :tag "off" nil)
  629. (const :tag "before tags first" t)
  630. (const :tag "after tags first" reversed)))))
  631. (if (fboundp 'defvaralias)
  632. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  633. (defcustom org-special-ctrl-k nil
  634. "Non-nil means `C-k' will behave specially in headlines.
  635. When nil, `C-k' will call the default `kill-line' command.
  636. When t, the following will happen while the cursor is in the headline:
  637. - When the cursor is at the beginning of a headline, kill the entire
  638. line and possible the folded subtree below the line.
  639. - When in the middle of the headline text, kill the headline up to the tags.
  640. - When after the headline text, kill the tags."
  641. :group 'org-edit-structure
  642. :type 'boolean)
  643. (defcustom org-yank-folded-subtrees t
  644. "Non-nil means, when yanking subtrees, fold them.
  645. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  646. it starts with a heading and all other headings in it are either children
  647. or siblings, then fold all the subtrees. However, do this only if no
  648. text after the yank would be swallowed into a folded tree by this action."
  649. :group 'org-edit-structure
  650. :type 'boolean)
  651. (defcustom org-yank-adjusted-subtrees nil
  652. "Non-nil means, when yanking subtrees, adjust the level.
  653. With this setting, `org-paste-subtree' is used to insert the subtree, see
  654. this function for details."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defcustom org-M-RET-may-split-line '((default . t))
  658. "Non-nil means, M-RET will split the line at the cursor position.
  659. When nil, it will go to the end of the line before making a
  660. new line.
  661. You may also set this option in a different way for different
  662. contexts. Valid contexts are:
  663. headline when creating a new headline
  664. item when creating a new item
  665. table in a table field
  666. default the value to be used for all contexts not explicitly
  667. customized"
  668. :group 'org-structure
  669. :group 'org-table
  670. :type '(choice
  671. (const :tag "Always" t)
  672. (const :tag "Never" nil)
  673. (repeat :greedy t :tag "Individual contexts"
  674. (cons
  675. (choice :tag "Context"
  676. (const headline)
  677. (const item)
  678. (const table)
  679. (const default))
  680. (boolean)))))
  681. (defcustom org-insert-heading-respect-content nil
  682. "Non-nil means, insert new headings after the current subtree.
  683. When nil, the new heading is created directly after the current line.
  684. The commands \\[org-insert-heading-respect-content] and
  685. \\[org-insert-todo-heading-respect-content] turn this variable on
  686. for the duration of the command."
  687. :group 'org-structure
  688. :type 'boolean)
  689. (defcustom org-blank-before-new-entry '((heading . auto)
  690. (plain-list-item . auto))
  691. "Should `org-insert-heading' leave a blank line before new heading/item?
  692. The value is an alist, with `heading' and `plain-list-item' as car,
  693. and a boolean flag as cdr. For plain lists, if the variable
  694. `org-empty-line-terminates-plain-lists' is set, the setting here
  695. is ignored and no empty line is inserted, to keep the list in tact."
  696. :group 'org-edit-structure
  697. :type '(list
  698. (cons (const heading)
  699. (choice (const :tag "Never" nil)
  700. (const :tag "Always" t)
  701. (const :tag "Auto" auto)))
  702. (cons (const plain-list-item)
  703. (choice (const :tag "Never" nil)
  704. (const :tag "Always" t)
  705. (const :tag "Auto" auto)))))
  706. (defcustom org-insert-heading-hook nil
  707. "Hook being run after inserting a new heading."
  708. :group 'org-edit-structure
  709. :type 'hook)
  710. (defcustom org-enable-fixed-width-editor t
  711. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  712. This currently only means, they are never auto-wrapped.
  713. When nil, such lines will be treated like ordinary lines.
  714. See also the QUOTE keyword."
  715. :group 'org-edit-structure
  716. :type 'boolean)
  717. (defcustom org-edit-src-region-extra nil
  718. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  719. For examples see the function `org-edit-src-find-region-and-lang'.
  720. The regular expression identifying the begin marker should end with a newline,
  721. and the regexp marking the end line should start with a newline, to make sure
  722. there are kept outside the narrowed region."
  723. :group 'org-edit-structure
  724. :type '(repeat
  725. (list
  726. (regexp :tag "begin regexp")
  727. (regexp :tag "end regexp")
  728. (choice :tag "language"
  729. (string :tag "specify")
  730. (integer :tag "from match group")
  731. (const :tag "from `lang' element")
  732. (const :tag "from `style' element")))))
  733. (defcustom org-coderef-label-format "(ref:%s)"
  734. "The default coderef format.
  735. This format string will be used to search for coderef labels in literal
  736. examples (EXAMPLE and SRC blocks). The format can be overwritten
  737. an individual literal example with the -f option, like
  738. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  739. ...
  740. #+END_SRC
  741. If you want to use this for HTML export, make sure that the format does
  742. not introduce special font-locking, and avoid the HTML special
  743. characters `<', `>', and `&'. The reason for this restriction is that
  744. the labels are searched for only after htmlize has done its job."
  745. :group 'org-edit-structure ; FIXME this is not in the right group
  746. :type 'string)
  747. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  748. "The mode that should be used to edit fixed-width regions.
  749. These are the regions where each line starts with a colon."
  750. :group 'org-edit-structure
  751. :type '(choice
  752. (const artist-mode)
  753. (const picture-mode)
  754. (const fundamental-mode)
  755. (function :tag "Other (specify)")))
  756. (defcustom org-edit-src-persistent-message t
  757. "Non-nil means show persistent exit help message while editing src examples.
  758. The message is shown in the header-line, which will be created in the
  759. first line of the window showing the editing buffer.
  760. When nil, the message will only be shown intermittently in the echo area."
  761. :group 'org-edit-structure
  762. :type 'boolean)
  763. (defcustom org-goto-auto-isearch t
  764. "Non-nil means, typing characters in org-goto starts incremental search."
  765. :group 'org-edit-structure
  766. :type 'boolean)
  767. (defgroup org-sparse-trees nil
  768. "Options concerning sparse trees in Org-mode."
  769. :tag "Org Sparse Trees"
  770. :group 'org-structure)
  771. (defcustom org-highlight-sparse-tree-matches t
  772. "Non-nil means, highlight all matches that define a sparse tree.
  773. The highlights will automatically disappear the next time the buffer is
  774. changed by an edit command."
  775. :group 'org-sparse-trees
  776. :type 'boolean)
  777. (defcustom org-remove-highlights-with-change t
  778. "Non-nil means, any change to the buffer will remove temporary highlights.
  779. Such highlights are created by `org-occur' and `org-clock-display'.
  780. When nil, `C-c C-c needs to be used to get rid of the highlights.
  781. The highlights created by `org-preview-latex-fragment' always need
  782. `C-c C-c' to be removed."
  783. :group 'org-sparse-trees
  784. :group 'org-time
  785. :type 'boolean)
  786. (defcustom org-occur-hook '(org-first-headline-recenter)
  787. "Hook that is run after `org-occur' has constructed a sparse tree.
  788. This can be used to recenter the window to show as much of the structure
  789. as possible."
  790. :group 'org-sparse-trees
  791. :type 'hook)
  792. (defgroup org-imenu-and-speedbar nil
  793. "Options concerning imenu and speedbar in Org-mode."
  794. :tag "Org Imenu and Speedbar"
  795. :group 'org-structure)
  796. (defcustom org-imenu-depth 2
  797. "The maximum level for Imenu access to Org-mode headlines.
  798. This also applied for speedbar access."
  799. :group 'org-imenu-and-speedbar
  800. :type 'integer)
  801. (defgroup org-table nil
  802. "Options concerning tables in Org-mode."
  803. :tag "Org Table"
  804. :group 'org)
  805. (defcustom org-enable-table-editor 'optimized
  806. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  807. When nil, such lines will be treated like ordinary lines.
  808. When equal to the symbol `optimized', the table editor will be optimized to
  809. do the following:
  810. - Automatic overwrite mode in front of whitespace in table fields.
  811. This makes the structure of the table stay in tact as long as the edited
  812. field does not exceed the column width.
  813. - Minimize the number of realigns. Normally, the table is aligned each time
  814. TAB or RET are pressed to move to another field. With optimization this
  815. happens only if changes to a field might have changed the column width.
  816. Optimization requires replacing the functions `self-insert-command',
  817. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  818. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  819. very good at guessing when a re-align will be necessary, but you can always
  820. force one with \\[org-ctrl-c-ctrl-c].
  821. If you would like to use the optimized version in Org-mode, but the
  822. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  823. This variable can be used to turn on and off the table editor during a session,
  824. but in order to toggle optimization, a restart is required.
  825. See also the variable `org-table-auto-blank-field'."
  826. :group 'org-table
  827. :type '(choice
  828. (const :tag "off" nil)
  829. (const :tag "on" t)
  830. (const :tag "on, optimized" optimized)))
  831. (defcustom org-self-insert-cluster-for-undo t
  832. "Non-nil means cluster self-insert commands for undo when possible.
  833. If this is set, then, like in the Emacs command loop, 20 consequtive
  834. characters will be undone together.
  835. This is configurable, because there is some impact on typing performance."
  836. :group 'org-table
  837. :type 'boolean)
  838. (defcustom org-table-tab-recognizes-table.el t
  839. "Non-nil means, TAB will automatically notice a table.el table.
  840. When it sees such a table, it moves point into it and - if necessary -
  841. calls `table-recognize-table'."
  842. :group 'org-table-editing
  843. :type 'boolean)
  844. (defgroup org-link nil
  845. "Options concerning links in Org-mode."
  846. :tag "Org Link"
  847. :group 'org)
  848. (defvar org-link-abbrev-alist-local nil
  849. "Buffer-local version of `org-link-abbrev-alist', which see.
  850. The value of this is taken from the #+LINK lines.")
  851. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  852. (defcustom org-link-abbrev-alist nil
  853. "Alist of link abbreviations.
  854. The car of each element is a string, to be replaced at the start of a link.
  855. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  856. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  857. [[linkkey:tag][description]]
  858. The 'linkkey' must be a word word, starting with a letter, followed
  859. by letters, numbers, '-' or '_'.
  860. If REPLACE is a string, the tag will simply be appended to create the link.
  861. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  862. the placeholder \"%h\" will cause a url-encoded version of the tag to
  863. be inserted at that point (see the function `url-hexify-string').
  864. REPLACE may also be a function that will be called with the tag as the
  865. only argument to create the link, which should be returned as a string.
  866. See the manual for examples."
  867. :group 'org-link
  868. :type '(repeat
  869. (cons
  870. (string :tag "Protocol")
  871. (choice
  872. (string :tag "Format")
  873. (function)))))
  874. (defcustom org-descriptive-links t
  875. "Non-nil means, hide link part and only show description of bracket links.
  876. Bracket links are like [[link][description]]. This variable sets the initial
  877. state in new org-mode buffers. The setting can then be toggled on a
  878. per-buffer basis from the Org->Hyperlinks menu."
  879. :group 'org-link
  880. :type 'boolean)
  881. (defcustom org-link-file-path-type 'adaptive
  882. "How the path name in file links should be stored.
  883. Valid values are:
  884. relative Relative to the current directory, i.e. the directory of the file
  885. into which the link is being inserted.
  886. absolute Absolute path, if possible with ~ for home directory.
  887. noabbrev Absolute path, no abbreviation of home directory.
  888. adaptive Use relative path for files in the current directory and sub-
  889. directories of it. For other files, use an absolute path."
  890. :group 'org-link
  891. :type '(choice
  892. (const relative)
  893. (const absolute)
  894. (const noabbrev)
  895. (const adaptive)))
  896. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  897. "Types of links that should be activated in Org-mode files.
  898. This is a list of symbols, each leading to the activation of a certain link
  899. type. In principle, it does not hurt to turn on most link types - there may
  900. be a small gain when turning off unused link types. The types are:
  901. bracket The recommended [[link][description]] or [[link]] links with hiding.
  902. angular Links in angular brackets that may contain whitespace like
  903. <bbdb:Carsten Dominik>.
  904. plain Plain links in normal text, no whitespace, like http://google.com.
  905. radio Text that is matched by a radio target, see manual for details.
  906. tag Tag settings in a headline (link to tag search).
  907. date Time stamps (link to calendar).
  908. footnote Footnote labels.
  909. Changing this variable requires a restart of Emacs to become effective."
  910. :group 'org-link
  911. :type '(set :greedy t
  912. (const :tag "Double bracket links (new style)" bracket)
  913. (const :tag "Angular bracket links (old style)" angular)
  914. (const :tag "Plain text links" plain)
  915. (const :tag "Radio target matches" radio)
  916. (const :tag "Tags" tag)
  917. (const :tag "Timestamps" date)
  918. (const :tag "Footnotes" footnote)))
  919. (defcustom org-make-link-description-function nil
  920. "Function to use to generate link descriptions from links. If
  921. nil the link location will be used. This function must take two
  922. parameters; the first is the link and the second the description
  923. org-insert-link has generated, and should return the description
  924. to use."
  925. :group 'org-link
  926. :type 'function)
  927. (defgroup org-link-store nil
  928. "Options concerning storing links in Org-mode."
  929. :tag "Org Store Link"
  930. :group 'org-link)
  931. (defcustom org-email-link-description-format "Email %c: %.30s"
  932. "Format of the description part of a link to an email or usenet message.
  933. The following %-escapes will be replaced by corresponding information:
  934. %F full \"From\" field
  935. %f name, taken from \"From\" field, address if no name
  936. %T full \"To\" field
  937. %t first name in \"To\" field, address if no name
  938. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  939. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  940. %s subject
  941. %m message-id.
  942. You may use normal field width specification between the % and the letter.
  943. This is for example useful to limit the length of the subject.
  944. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  945. :group 'org-link-store
  946. :type 'string)
  947. (defcustom org-from-is-user-regexp
  948. (let (r1 r2)
  949. (when (and user-mail-address (not (string= user-mail-address "")))
  950. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  951. (when (and user-full-name (not (string= user-full-name "")))
  952. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  953. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  954. "Regexp matched against the \"From:\" header of an email or usenet message.
  955. It should match if the message is from the user him/herself."
  956. :group 'org-link-store
  957. :type 'regexp)
  958. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  959. "Non-nil means, storing a link to an Org file will use entry IDs.
  960. Note that before this variable is even considered, org-id must be loaded,
  961. to please customize `org-modules' and turn it on.
  962. The variable can have the following values:
  963. t Create an ID if needed to make a link to the current entry.
  964. create-if-interactive
  965. If `org-store-link' is called directly (interactively, as a user
  966. command), do create an ID to support the link. But when doing the
  967. job for remember, only use the ID if it already exists. The
  968. purpose of this setting is to avoid proliferation of unwanted
  969. IDs, just because you happen to be in an Org file when you
  970. call `org-remember' that automatically and preemptively
  971. creates a link. If you do want to get an ID link in a remember
  972. template to an entry not having an ID, create it first by
  973. explicitly creating a link to it, using `C-c C-l' first.
  974. use-existing
  975. Use existing ID, do not create one.
  976. nil Never use an ID to make a link, instead link using a text search for
  977. the headline text."
  978. :group 'org-link-store
  979. :type '(choice
  980. (const :tag "Create ID to make link" t)
  981. (const :tag "Create if storing link interactively"
  982. create-if-interactive)
  983. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  984. create-if-interactive-and-no-custom-id)
  985. (const :tag "Only use existing" use-existing)
  986. (const :tag "Do not use ID to create link" nil)))
  987. (defcustom org-context-in-file-links t
  988. "Non-nil means, file links from `org-store-link' contain context.
  989. A search string will be added to the file name with :: as separator and
  990. used to find the context when the link is activated by the command
  991. `org-open-at-point'.
  992. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  993. negates this setting for the duration of the command."
  994. :group 'org-link-store
  995. :type 'boolean)
  996. (defcustom org-keep-stored-link-after-insertion nil
  997. "Non-nil means, keep link in list for entire session.
  998. The command `org-store-link' adds a link pointing to the current
  999. location to an internal list. These links accumulate during a session.
  1000. The command `org-insert-link' can be used to insert links into any
  1001. Org-mode file (offering completion for all stored links). When this
  1002. option is nil, every link which has been inserted once using \\[org-insert-link]
  1003. will be removed from the list, to make completing the unused links
  1004. more efficient."
  1005. :group 'org-link-store
  1006. :type 'boolean)
  1007. (defgroup org-link-follow nil
  1008. "Options concerning following links in Org-mode."
  1009. :tag "Org Follow Link"
  1010. :group 'org-link)
  1011. (defcustom org-link-translation-function nil
  1012. "Function to translate links with different syntax to Org syntax.
  1013. This can be used to translate links created for example by the Planner
  1014. or emacs-wiki packages to Org syntax.
  1015. The function must accept two parameters, a TYPE containing the link
  1016. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1017. which is everything after the link protocol. It should return a cons
  1018. with possibly modified values of type and path.
  1019. Org contains a function for this, so if you set this variable to
  1020. `org-translate-link-from-planner', you should be able follow many
  1021. links created by planner."
  1022. :group 'org-link-follow
  1023. :type 'function)
  1024. (defcustom org-follow-link-hook nil
  1025. "Hook that is run after a link has been followed."
  1026. :group 'org-link-follow
  1027. :type 'hook)
  1028. (defcustom org-tab-follows-link nil
  1029. "Non-nil means, on links TAB will follow the link.
  1030. Needs to be set before org.el is loaded.
  1031. This really should not be used, it does not make sense, and the
  1032. implementation is bad."
  1033. :group 'org-link-follow
  1034. :type 'boolean)
  1035. (defcustom org-return-follows-link nil
  1036. "Non-nil means, on links RET will follow the link.
  1037. Needs to be set before org.el is loaded."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-mouse-1-follows-link
  1041. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1042. "Non-nil means, mouse-1 on a link will follow the link.
  1043. A longer mouse click will still set point. Does not work on XEmacs.
  1044. Needs to be set before org.el is loaded."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-mark-ring-length 4
  1048. "Number of different positions to be recorded in the ring
  1049. Changing this requires a restart of Emacs to work correctly."
  1050. :group 'org-link-follow
  1051. :type 'integer)
  1052. (defcustom org-link-frame-setup
  1053. '((vm . vm-visit-folder-other-frame)
  1054. (gnus . gnus-other-frame)
  1055. (file . find-file-other-window))
  1056. "Setup the frame configuration for following links.
  1057. When following a link with Emacs, it may often be useful to display
  1058. this link in another window or frame. This variable can be used to
  1059. set this up for the different types of links.
  1060. For VM, use any of
  1061. `vm-visit-folder'
  1062. `vm-visit-folder-other-frame'
  1063. For Gnus, use any of
  1064. `gnus'
  1065. `gnus-other-frame'
  1066. `org-gnus-no-new-news'
  1067. For FILE, use any of
  1068. `find-file'
  1069. `find-file-other-window'
  1070. `find-file-other-frame'
  1071. For the calendar, use the variable `calendar-setup'.
  1072. For BBDB, it is currently only possible to display the matches in
  1073. another window."
  1074. :group 'org-link-follow
  1075. :type '(list
  1076. (cons (const vm)
  1077. (choice
  1078. (const vm-visit-folder)
  1079. (const vm-visit-folder-other-window)
  1080. (const vm-visit-folder-other-frame)))
  1081. (cons (const gnus)
  1082. (choice
  1083. (const gnus)
  1084. (const gnus-other-frame)
  1085. (const org-gnus-no-new-news)))
  1086. (cons (const file)
  1087. (choice
  1088. (const find-file)
  1089. (const find-file-other-window)
  1090. (const find-file-other-frame)))))
  1091. (defcustom org-display-internal-link-with-indirect-buffer nil
  1092. "Non-nil means, use indirect buffer to display infile links.
  1093. Activating internal links (from one location in a file to another location
  1094. in the same file) normally just jumps to the location. When the link is
  1095. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1096. another window. When this option is set, the other window actually displays
  1097. an indirect buffer clone of the current buffer, to avoid any visibility
  1098. changes to the current buffer."
  1099. :group 'org-link-follow
  1100. :type 'boolean)
  1101. (defcustom org-open-non-existing-files nil
  1102. "Non-nil means, `org-open-file' will open non-existing files.
  1103. When nil, an error will be generated."
  1104. :group 'org-link-follow
  1105. :type 'boolean)
  1106. (defcustom org-open-directory-means-index-dot-org nil
  1107. "Non-nil means, a link to a directory really means to index.org.
  1108. When nil, following a directory link will run dired or open a finder/explorer
  1109. window on that directory."
  1110. :group 'org-link-follow
  1111. :type 'boolean)
  1112. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1113. "Function and arguments to call for following mailto links.
  1114. This is a list with the first element being a lisp function, and the
  1115. remaining elements being arguments to the function. In string arguments,
  1116. %a will be replaced by the address, and %s will be replaced by the subject
  1117. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1118. :group 'org-link-follow
  1119. :type '(choice
  1120. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1121. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1122. (const :tag "message-mail" (message-mail "%a" "%s"))
  1123. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1124. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1125. "Non-nil means, ask for confirmation before executing shell links.
  1126. Shell links can be dangerous: just think about a link
  1127. [[shell:rm -rf ~/*][Google Search]]
  1128. This link would show up in your Org-mode document as \"Google Search\",
  1129. but really it would remove your entire home directory.
  1130. Therefore we advise against setting this variable to nil.
  1131. Just change it to `y-or-n-p' if you want to confirm with a
  1132. single keystroke rather than having to type \"yes\"."
  1133. :group 'org-link-follow
  1134. :type '(choice
  1135. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1136. (const :tag "with y-or-n (faster)" y-or-n-p)
  1137. (const :tag "no confirmation (dangerous)" nil)))
  1138. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1139. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1140. Elisp links can be dangerous: just think about a link
  1141. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1142. This link would show up in your Org-mode document as \"Google Search\",
  1143. but really it would remove your entire home directory.
  1144. Therefore we advise against setting this variable to nil.
  1145. Just change it to `y-or-n-p' if you want to confirm with a
  1146. single keystroke rather than having to type \"yes\"."
  1147. :group 'org-link-follow
  1148. :type '(choice
  1149. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1150. (const :tag "with y-or-n (faster)" y-or-n-p)
  1151. (const :tag "no confirmation (dangerous)" nil)))
  1152. (defconst org-file-apps-defaults-gnu
  1153. '((remote . emacs)
  1154. (system . mailcap)
  1155. (t . mailcap))
  1156. "Default file applications on a UNIX or GNU/Linux system.
  1157. See `org-file-apps'.")
  1158. (defconst org-file-apps-defaults-macosx
  1159. '((remote . emacs)
  1160. (t . "open %s")
  1161. (system . "open %s")
  1162. ("ps.gz" . "gv %s")
  1163. ("eps.gz" . "gv %s")
  1164. ("dvi" . "xdvi %s")
  1165. ("fig" . "xfig %s"))
  1166. "Default file applications on a MacOS X system.
  1167. The system \"open\" is known as a default, but we use X11 applications
  1168. for some files for which the OS does not have a good default.
  1169. See `org-file-apps'.")
  1170. (defconst org-file-apps-defaults-windowsnt
  1171. (list
  1172. '(remote . emacs)
  1173. (cons t
  1174. (list (if (featurep 'xemacs)
  1175. 'mswindows-shell-execute
  1176. 'w32-shell-execute)
  1177. "open" 'file))
  1178. (cons 'system
  1179. (list (if (featurep 'xemacs)
  1180. 'mswindows-shell-execute
  1181. 'w32-shell-execute)
  1182. "open" 'file)))
  1183. "Default file applications on a Windows NT system.
  1184. The system \"open\" is used for most files.
  1185. See `org-file-apps'.")
  1186. (defcustom org-file-apps
  1187. '(
  1188. (auto-mode . emacs)
  1189. ("\\.x?html?\\'" . default)
  1190. ("\\.pdf\\'" . default)
  1191. )
  1192. "External applications for opening `file:path' items in a document.
  1193. Org-mode uses system defaults for different file types, but
  1194. you can use this variable to set the application for a given file
  1195. extension. The entries in this list are cons cells where the car identifies
  1196. files and the cdr the corresponding command. Possible values for the
  1197. file identifier are
  1198. \"regex\" Regular expression matched against the file name. For backward
  1199. compatibility, this can also be a string with only alphanumeric
  1200. characters, which is then interpreted as an extension.
  1201. `directory' Matches a directory
  1202. `remote' Matches a remote file, accessible through tramp or efs.
  1203. Remote files most likely should be visited through Emacs
  1204. because external applications cannot handle such paths.
  1205. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1206. so all files Emacs knows how to handle. Using this with
  1207. command `emacs' will open most files in Emacs. Beware that this
  1208. will also open html files inside Emacs, unless you add
  1209. (\"html\" . default) to the list as well.
  1210. t Default for files not matched by any of the other options.
  1211. `system' The system command to open files, like `open' on Windows
  1212. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1213. that will be selected if you call `C-c C-o' with a double
  1214. `C-u C-u' prefix.
  1215. Possible values for the command are:
  1216. `emacs' The file will be visited by the current Emacs process.
  1217. `default' Use the default application for this file type, which is the
  1218. association for t in the list, most likely in the system-specific
  1219. part.
  1220. This can be used to overrule an unwanted setting in the
  1221. system-specific variable.
  1222. `system' Use the system command for opening files, like \"open\".
  1223. This command is specified by the entry whose car is `system'.
  1224. Most likely, the system-specific version of this variable
  1225. does define this command, but you can overrule/replace it
  1226. here.
  1227. string A command to be executed by a shell; %s will be replaced
  1228. by the path to the file.
  1229. sexp A Lisp form which will be evaluated. The file path will
  1230. be available in the Lisp variable `file'.
  1231. For more examples, see the system specific constants
  1232. `org-file-apps-defaults-macosx'
  1233. `org-file-apps-defaults-windowsnt'
  1234. `org-file-apps-defaults-gnu'."
  1235. :group 'org-link-follow
  1236. :type '(repeat
  1237. (cons (choice :value ""
  1238. (string :tag "Extension")
  1239. (const :tag "System command to open files" system)
  1240. (const :tag "Default for unrecognized files" t)
  1241. (const :tag "Remote file" remote)
  1242. (const :tag "Links to a directory" directory)
  1243. (const :tag "Any files that have Emacs modes"
  1244. auto-mode))
  1245. (choice :value ""
  1246. (const :tag "Visit with Emacs" emacs)
  1247. (const :tag "Use default" default)
  1248. (const :tag "Use the system command" system)
  1249. (string :tag "Command")
  1250. (sexp :tag "Lisp form")))))
  1251. (defgroup org-refile nil
  1252. "Options concerning refiling entries in Org-mode."
  1253. :tag "Org Refile"
  1254. :group 'org)
  1255. (defcustom org-directory "~/org"
  1256. "Directory with org files.
  1257. This is just a default location to look for Org files. There is no need
  1258. at all to put your files into this directory. It is only used in the
  1259. following situations:
  1260. 1. When a remember template specifies a target file that is not an
  1261. absolute path. The path will then be interpreted relative to
  1262. `org-directory'
  1263. 2. When a remember note is filed away in an interactive way (when exiting the
  1264. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1265. with `org-directory' as the default path."
  1266. :group 'org-refile
  1267. :group 'org-remember
  1268. :type 'directory)
  1269. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1270. "Default target for storing notes.
  1271. Used by the hooks for remember.el. This can be a string, or nil to mean
  1272. the value of `remember-data-file'.
  1273. You can set this on a per-template basis with the variable
  1274. `org-remember-templates'."
  1275. :group 'org-refile
  1276. :group 'org-remember
  1277. :type '(choice
  1278. (const :tag "Default from remember-data-file" nil)
  1279. file))
  1280. (defcustom org-goto-interface 'outline
  1281. "The default interface to be used for `org-goto'.
  1282. Allowed values are:
  1283. outline The interface shows an outline of the relevant file
  1284. and the correct heading is found by moving through
  1285. the outline or by searching with incremental search.
  1286. outline-path-completion Headlines in the current buffer are offered via
  1287. completion. This is the interface also used by
  1288. the refile command."
  1289. :group 'org-refile
  1290. :type '(choice
  1291. (const :tag "Outline" outline)
  1292. (const :tag "Outline-path-completion" outline-path-completion)))
  1293. (defcustom org-goto-max-level 5
  1294. "Maximum level to be considered when running org-goto with refile interface."
  1295. :group 'org-refile
  1296. :type 'integer)
  1297. (defcustom org-reverse-note-order nil
  1298. "Non-nil means, store new notes at the beginning of a file or entry.
  1299. When nil, new notes will be filed to the end of a file or entry.
  1300. This can also be a list with cons cells of regular expressions that
  1301. are matched against file names, and values."
  1302. :group 'org-remember
  1303. :group 'org-refile
  1304. :type '(choice
  1305. (const :tag "Reverse always" t)
  1306. (const :tag "Reverse never" nil)
  1307. (repeat :tag "By file name regexp"
  1308. (cons regexp boolean))))
  1309. (defcustom org-refile-targets nil
  1310. "Targets for refiling entries with \\[org-refile].
  1311. This is list of cons cells. Each cell contains:
  1312. - a specification of the files to be considered, either a list of files,
  1313. or a symbol whose function or variable value will be used to retrieve
  1314. a file name or a list of file names. If you use `org-agenda-files' for
  1315. that, all agenda files will be scanned for targets. Nil means, consider
  1316. headings in the current buffer.
  1317. - A specification of how to find candidate refile targets. This may be
  1318. any of:
  1319. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1320. This tag has to be present in all target headlines, inheritance will
  1321. not be considered.
  1322. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1323. todo keyword.
  1324. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1325. headlines that are refiling targets.
  1326. - a cons cell (:level . N). Any headline of level N is considered a target.
  1327. Note that, when `org-odd-levels-only' is set, level corresponds to
  1328. order in hierarchy, not to the number of stars.
  1329. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1330. Note that, when `org-odd-levels-only' is set, level corresponds to
  1331. order in hierarchy, not to the number of stars.
  1332. You can set the variable `org-refile-target-verify-function' to a function
  1333. to verify each headline found by the simple critery above.
  1334. When this variable is nil, all top-level headlines in the current buffer
  1335. are used, equivalent to the value `((nil . (:level . 1))'."
  1336. :group 'org-refile
  1337. :type '(repeat
  1338. (cons
  1339. (choice :value org-agenda-files
  1340. (const :tag "All agenda files" org-agenda-files)
  1341. (const :tag "Current buffer" nil)
  1342. (function) (variable) (file))
  1343. (choice :tag "Identify target headline by"
  1344. (cons :tag "Specific tag" (const :value :tag) (string))
  1345. (cons :tag "TODO keyword" (const :value :todo) (string))
  1346. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1347. (cons :tag "Level number" (const :value :level) (integer))
  1348. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1349. (defcustom org-refile-target-verify-function nil
  1350. "Function to verify if the headline at point should be a refile target.
  1351. The function will be called without arguments, with point at the
  1352. beginning of the headline. It should return t and leave point
  1353. where it is if the headline is a valid target for refiling.
  1354. If the target should not be selected, the function must return nil.
  1355. In addition to this, it may move point to a place from where the search
  1356. should be continued. For example, the function may decide that the entire
  1357. subtree of the current entry should be excluded and move point to the end
  1358. of the subtree."
  1359. :group 'org-refile
  1360. :type 'function)
  1361. (defcustom org-refile-use-outline-path nil
  1362. "Non-nil means, provide refile targets as paths.
  1363. So a level 3 headline will be available as level1/level2/level3.
  1364. When the value is `file', also include the file name (without directory)
  1365. into the path. In this case, you can also stop the completion after
  1366. the file name, to get entries inserted as top level in the file.
  1367. When `full-file-path', include the full file path."
  1368. :group 'org-refile
  1369. :type '(choice
  1370. (const :tag "Not" nil)
  1371. (const :tag "Yes" t)
  1372. (const :tag "Start with file name" file)
  1373. (const :tag "Start with full file path" full-file-path)))
  1374. (defcustom org-outline-path-complete-in-steps t
  1375. "Non-nil means, complete the outline path in hierarchical steps.
  1376. When Org-mode uses the refile interface to select an outline path
  1377. \(see variable `org-refile-use-outline-path'), the completion of
  1378. the path can be done is a single go, or if can be done in steps down
  1379. the headline hierarchy. Going in steps is probably the best if you
  1380. do not use a special completion package like `ido' or `icicles'.
  1381. However, when using these packages, going in one step can be very
  1382. fast, while still showing the whole path to the entry."
  1383. :group 'org-refile
  1384. :type 'boolean)
  1385. (defcustom org-refile-allow-creating-parent-nodes nil
  1386. "Non-nil means, allow to create new nodes as refile targets.
  1387. New nodes are then created by adding \"/new node name\" to the completion
  1388. of an existing node. When the value of this variable is `confirm',
  1389. new node creation must be confirmed by the user (recommended)
  1390. When nil, the completion must match an existing entry.
  1391. Note that, if the new heading is not seen by the criteria
  1392. listed in `org-refile-targets', multiple instances of the same
  1393. heading would be created by trying again to file under the new
  1394. heading."
  1395. :group 'org-refile
  1396. :type '(choice
  1397. (const :tag "Never" nil)
  1398. (const :tag "Always" t)
  1399. (const :tag "Prompt for confirmation" confirm)))
  1400. (defgroup org-todo nil
  1401. "Options concerning TODO items in Org-mode."
  1402. :tag "Org TODO"
  1403. :group 'org)
  1404. (defgroup org-progress nil
  1405. "Options concerning Progress logging in Org-mode."
  1406. :tag "Org Progress"
  1407. :group 'org-time)
  1408. (defvar org-todo-interpretation-widgets
  1409. '(
  1410. (:tag "Sequence (cycling hits every state)" sequence)
  1411. (:tag "Type (cycling directly to DONE)" type))
  1412. "The available interpretation symbols for customizing
  1413. `org-todo-keywords'.
  1414. Interested libraries should add to this list.")
  1415. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1416. "List of TODO entry keyword sequences and their interpretation.
  1417. \\<org-mode-map>This is a list of sequences.
  1418. Each sequence starts with a symbol, either `sequence' or `type',
  1419. indicating if the keywords should be interpreted as a sequence of
  1420. action steps, or as different types of TODO items. The first
  1421. keywords are states requiring action - these states will select a headline
  1422. for inclusion into the global TODO list Org-mode produces. If one of
  1423. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1424. signify that no further action is necessary. If \"|\" is not found,
  1425. the last keyword is treated as the only DONE state of the sequence.
  1426. The command \\[org-todo] cycles an entry through these states, and one
  1427. additional state where no keyword is present. For details about this
  1428. cycling, see the manual.
  1429. TODO keywords and interpretation can also be set on a per-file basis with
  1430. the special #+SEQ_TODO and #+TYP_TODO lines.
  1431. Each keyword can optionally specify a character for fast state selection
  1432. \(in combination with the variable `org-use-fast-todo-selection')
  1433. and specifiers for state change logging, using the same syntax
  1434. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1435. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1436. indicates to record a time stamp each time this state is selected.
  1437. Each keyword may also specify if a timestamp or a note should be
  1438. recorded when entering or leaving the state, by adding additional
  1439. characters in the parenthesis after the keyword. This looks like this:
  1440. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1441. record only the time of the state change. With X and Y being either
  1442. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1443. Y when leaving the state if and only if the *target* state does not
  1444. define X. You may omit any of the fast-selection key or X or /Y,
  1445. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1446. For backward compatibility, this variable may also be just a list
  1447. of keywords - in this case the interpretation (sequence or type) will be
  1448. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1449. :group 'org-todo
  1450. :group 'org-keywords
  1451. :type '(choice
  1452. (repeat :tag "Old syntax, just keywords"
  1453. (string :tag "Keyword"))
  1454. (repeat :tag "New syntax"
  1455. (cons
  1456. (choice
  1457. :tag "Interpretation"
  1458. ;;Quick and dirty way to see
  1459. ;;`org-todo-interpretations'. This takes the
  1460. ;;place of item arguments
  1461. :convert-widget
  1462. (lambda (widget)
  1463. (widget-put widget
  1464. :args (mapcar
  1465. #'(lambda (x)
  1466. (widget-convert
  1467. (cons 'const x)))
  1468. org-todo-interpretation-widgets))
  1469. widget))
  1470. (repeat
  1471. (string :tag "Keyword"))))))
  1472. (defvar org-todo-keywords-1 nil
  1473. "All TODO and DONE keywords active in a buffer.")
  1474. (make-variable-buffer-local 'org-todo-keywords-1)
  1475. (defvar org-todo-keywords-for-agenda nil)
  1476. (defvar org-done-keywords-for-agenda nil)
  1477. (defvar org-todo-keyword-alist-for-agenda nil)
  1478. (defvar org-tag-alist-for-agenda nil)
  1479. (defvar org-agenda-contributing-files nil)
  1480. (defvar org-not-done-keywords nil)
  1481. (make-variable-buffer-local 'org-not-done-keywords)
  1482. (defvar org-done-keywords nil)
  1483. (make-variable-buffer-local 'org-done-keywords)
  1484. (defvar org-todo-heads nil)
  1485. (make-variable-buffer-local 'org-todo-heads)
  1486. (defvar org-todo-sets nil)
  1487. (make-variable-buffer-local 'org-todo-sets)
  1488. (defvar org-todo-log-states nil)
  1489. (make-variable-buffer-local 'org-todo-log-states)
  1490. (defvar org-todo-kwd-alist nil)
  1491. (make-variable-buffer-local 'org-todo-kwd-alist)
  1492. (defvar org-todo-key-alist nil)
  1493. (make-variable-buffer-local 'org-todo-key-alist)
  1494. (defvar org-todo-key-trigger nil)
  1495. (make-variable-buffer-local 'org-todo-key-trigger)
  1496. (defcustom org-todo-interpretation 'sequence
  1497. "Controls how TODO keywords are interpreted.
  1498. This variable is in principle obsolete and is only used for
  1499. backward compatibility, if the interpretation of todo keywords is
  1500. not given already in `org-todo-keywords'. See that variable for
  1501. more information."
  1502. :group 'org-todo
  1503. :group 'org-keywords
  1504. :type '(choice (const sequence)
  1505. (const type)))
  1506. (defcustom org-use-fast-todo-selection t
  1507. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1508. This variable describes if and under what circumstances the cycling
  1509. mechanism for TODO keywords will be replaced by a single-key, direct
  1510. selection scheme.
  1511. When nil, fast selection is never used.
  1512. When the symbol `prefix', it will be used when `org-todo' is called with
  1513. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1514. in an agenda buffer.
  1515. When t, fast selection is used by default. In this case, the prefix
  1516. argument forces cycling instead.
  1517. In all cases, the special interface is only used if access keys have actually
  1518. been assigned by the user, i.e. if keywords in the configuration are followed
  1519. by a letter in parenthesis, like TODO(t)."
  1520. :group 'org-todo
  1521. :type '(choice
  1522. (const :tag "Never" nil)
  1523. (const :tag "By default" t)
  1524. (const :tag "Only with C-u C-c C-t" prefix)))
  1525. (defcustom org-provide-todo-statistics t
  1526. "Non-nil means, update todo statistics after insert and toggle.
  1527. When this is set, todo statistics is updated in the parent of the current
  1528. entry each time a todo state is changed."
  1529. :group 'org-todo
  1530. :type 'boolean)
  1531. (defcustom org-hierarchical-todo-statistics t
  1532. "Non-nil means, TODO statistics covers just direct children.
  1533. When nil, all entries in the subtree are considered.
  1534. This has only an effect if `org-provide-todo-statistics' is set."
  1535. :group 'org-todo
  1536. :type 'boolean)
  1537. (defcustom org-after-todo-state-change-hook nil
  1538. "Hook which is run after the state of a TODO item was changed.
  1539. The new state (a string with a TODO keyword, or nil) is available in the
  1540. Lisp variable `state'."
  1541. :group 'org-todo
  1542. :type 'hook)
  1543. (defvar org-blocker-hook nil
  1544. "Hook for functions that are allowed to block a state change.
  1545. Each function gets as its single argument a property list, see
  1546. `org-trigger-hook' for more information about this list.
  1547. If any of the functions in this hook returns nil, the state change
  1548. is blocked.")
  1549. (defvar org-trigger-hook nil
  1550. "Hook for functions that are triggered by a state change.
  1551. Each function gets as its single argument a property list with at least
  1552. the following elements:
  1553. (:type type-of-change :position pos-at-entry-start
  1554. :from old-state :to new-state)
  1555. Depending on the type, more properties may be present.
  1556. This mechanism is currently implemented for:
  1557. TODO state changes
  1558. ------------------
  1559. :type todo-state-change
  1560. :from previous state (keyword as a string), or nil, or a symbol
  1561. 'todo' or 'done', to indicate the general type of state.
  1562. :to new state, like in :from")
  1563. (defcustom org-enforce-todo-dependencies nil
  1564. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1565. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1566. be blocked if any prior sibling is not yet done.
  1567. Finally, if the parent is blocked because of ordered siblings of its own,
  1568. the child will also be blocked.
  1569. This variable needs to be set before org.el is loaded, and you need to
  1570. restart Emacs after a change to make the change effective. The only way
  1571. to change is while Emacs is running is through the customize interface."
  1572. :set (lambda (var val)
  1573. (set var val)
  1574. (if val
  1575. (add-hook 'org-blocker-hook
  1576. 'org-block-todo-from-children-or-siblings-or-parent)
  1577. (remove-hook 'org-blocker-hook
  1578. 'org-block-todo-from-children-or-siblings-or-parent)))
  1579. :group 'org-todo
  1580. :type 'boolean)
  1581. (defcustom org-enforce-todo-checkbox-dependencies nil
  1582. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1583. When this is nil, checkboxes have no influence on switching TODO states.
  1584. When non-nil, you first need to check off all check boxes before the TODO
  1585. entry can be switched to DONE.
  1586. This variable needs to be set before org.el is loaded, and you need to
  1587. restart Emacs after a change to make the change effective. The only way
  1588. to change is while Emacs is running is through the customize interface."
  1589. :set (lambda (var val)
  1590. (set var val)
  1591. (if val
  1592. (add-hook 'org-blocker-hook
  1593. 'org-block-todo-from-checkboxes)
  1594. (remove-hook 'org-blocker-hook
  1595. 'org-block-todo-from-checkboxes)))
  1596. :group 'org-todo
  1597. :type 'boolean)
  1598. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1599. "Non-nil means, inserting a TODO heading is treated as state change.
  1600. So when the command \\[org-insert-todo-heading] is used, state change
  1601. logging will apply if appropriate. When nil, the new TODO item will
  1602. be inserted directly, and no logging will take place."
  1603. :group 'org-todo
  1604. :type 'boolean)
  1605. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1606. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1607. This is the default behavior. However, setting this to nil allows a
  1608. convenient way to select a TODO state and bypass any logging associated
  1609. with that."
  1610. :group 'org-todo
  1611. :type 'boolean)
  1612. (defcustom org-todo-state-tags-triggers nil
  1613. "Tag changes that should be triggered by TODO state changes.
  1614. This is a list. Each entry is
  1615. (state-change (tag . flag) .......)
  1616. State-change can be a string with a state, and empty string to indicate the
  1617. state that has no TODO keyword, or it can be one of the symbols `todo'
  1618. or `done', meaning any not-done or done state, respectively."
  1619. :group 'org-todo
  1620. :group 'org-tags
  1621. :type '(repeat
  1622. (cons (choice :tag "When changing to"
  1623. (const :tag "Not-done state" todo)
  1624. (const :tag "Done state" done)
  1625. (string :tag "State"))
  1626. (repeat
  1627. (cons :tag "Tag action"
  1628. (string :tag "Tag")
  1629. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1630. (defcustom org-log-done nil
  1631. "Information to record when a task moves to the DONE state.
  1632. Possible values are:
  1633. nil Don't add anything, just change the keyword
  1634. time Add a time stamp to the task
  1635. note Prompt a closing note and add it with template `org-log-note-headings'
  1636. This option can also be set with on a per-file-basis with
  1637. #+STARTUP: nologdone
  1638. #+STARTUP: logdone
  1639. #+STARTUP: lognotedone
  1640. You can have local logging settings for a subtree by setting the LOGGING
  1641. property to one or more of these keywords."
  1642. :group 'org-todo
  1643. :group 'org-progress
  1644. :type '(choice
  1645. (const :tag "No logging" nil)
  1646. (const :tag "Record CLOSED timestamp" time)
  1647. (const :tag "Record CLOSED timestamp with closing note." note)))
  1648. ;; Normalize old uses of org-log-done.
  1649. (cond
  1650. ((eq org-log-done t) (setq org-log-done 'time))
  1651. ((and (listp org-log-done) (memq 'done org-log-done))
  1652. (setq org-log-done 'note)))
  1653. (defcustom org-log-note-clock-out nil
  1654. "Non-nil means, record a note when clocking out of an item.
  1655. This can also be configured on a per-file basis by adding one of
  1656. the following lines anywhere in the buffer:
  1657. #+STARTUP: lognoteclock-out
  1658. #+STARTUP: nolognoteclock-out"
  1659. :group 'org-todo
  1660. :group 'org-progress
  1661. :type 'boolean)
  1662. (defcustom org-log-done-with-time t
  1663. "Non-nil means, the CLOSED time stamp will contain date and time.
  1664. When nil, only the date will be recorded."
  1665. :group 'org-progress
  1666. :type 'boolean)
  1667. (defcustom org-log-note-headings
  1668. '((done . "CLOSING NOTE %t")
  1669. (state . "State %-12s from %-12S %t")
  1670. (note . "Note taken on %t")
  1671. (clock-out . ""))
  1672. "Headings for notes added to entries.
  1673. The value is an alist, with the car being a symbol indicating the note
  1674. context, and the cdr is the heading to be used. The heading may also be the
  1675. empty string.
  1676. %t in the heading will be replaced by a time stamp.
  1677. %s will be replaced by the new TODO state, in double quotes.
  1678. %S will be replaced by the old TODO state, in double quotes.
  1679. %u will be replaced by the user name.
  1680. %U will be replaced by the full user name."
  1681. :group 'org-todo
  1682. :group 'org-progress
  1683. :type '(list :greedy t
  1684. (cons (const :tag "Heading when closing an item" done) string)
  1685. (cons (const :tag
  1686. "Heading when changing todo state (todo sequence only)"
  1687. state) string)
  1688. (cons (const :tag "Heading when just taking a note" note) string)
  1689. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1690. (unless (assq 'note org-log-note-headings)
  1691. (push '(note . "%t") org-log-note-headings))
  1692. (defcustom org-log-into-drawer nil
  1693. "Non-nil means, insert state change notes and time stamps into a drawer.
  1694. When nil, state changes notes will be inserted after the headline and
  1695. any scheduling and clock lines, but not inside a drawer.
  1696. The value of this variable should be the name of the drawer to use.
  1697. LOGBOOK is proposed at the default drawer for this purpose, you can
  1698. also set this to a string to define the drawer of your choice.
  1699. A value of t is also allowed, representing \"LOGBOOK\".
  1700. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1701. will be ignored.
  1702. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1703. a subtree."
  1704. :group 'org-todo
  1705. :group 'org-progress
  1706. :type '(choice
  1707. (const :tag "Not into a drawer" nil)
  1708. (const :tag "LOGBOOK" t)
  1709. (string :tag "Other")))
  1710. (if (fboundp 'defvaralias)
  1711. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1712. (defun org-log-into-drawer ()
  1713. "Return the value of `org-log-into-drawer', but let properties overrule.
  1714. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1715. used instead of the default value."
  1716. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1717. (cond
  1718. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1719. ((equal p "t") "LOGBOOK")
  1720. (t p))))
  1721. (defcustom org-log-state-notes-insert-after-drawers nil
  1722. "Non-nil means, insert state change notes after any drawers in entry.
  1723. Only the drawers that *immediately* follow the headline and the
  1724. deadline/scheduled line are skipped.
  1725. When nil, insert notes right after the heading and perhaps the line
  1726. with deadline/scheduling if present.
  1727. This variable will have no effect if `org-log-into-drawer' is
  1728. set."
  1729. :group 'org-todo
  1730. :group 'org-progress
  1731. :type 'boolean)
  1732. (defcustom org-log-states-order-reversed t
  1733. "Non-nil means, the latest state change note will be directly after heading.
  1734. When nil, the notes will be orderer according to time."
  1735. :group 'org-todo
  1736. :group 'org-progress
  1737. :type 'boolean)
  1738. (defcustom org-log-repeat 'time
  1739. "Non-nil means, record moving through the DONE state when triggering repeat.
  1740. An auto-repeating tasks is immediately switched back to TODO when marked
  1741. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1742. the TODO keyword definition, or recording a closing note by setting
  1743. `org-log-done', there will be no record of the task moving through DONE.
  1744. This variable forces taking a note anyway. Possible values are:
  1745. nil Don't force a record
  1746. time Record a time stamp
  1747. note Record a note
  1748. This option can also be set with on a per-file-basis with
  1749. #+STARTUP: logrepeat
  1750. #+STARTUP: lognoterepeat
  1751. #+STARTUP: nologrepeat
  1752. You can have local logging settings for a subtree by setting the LOGGING
  1753. property to one or more of these keywords."
  1754. :group 'org-todo
  1755. :group 'org-progress
  1756. :type '(choice
  1757. (const :tag "Don't force a record" nil)
  1758. (const :tag "Force recording the DONE state" time)
  1759. (const :tag "Force recording a note with the DONE state" note)))
  1760. (defgroup org-priorities nil
  1761. "Priorities in Org-mode."
  1762. :tag "Org Priorities"
  1763. :group 'org-todo)
  1764. (defcustom org-enable-priority-commands t
  1765. "Non-nil means, priority commands are active.
  1766. When nil, these commands will be disabled, so that you never accidentally
  1767. set a priority."
  1768. :group 'org-priorities
  1769. :type 'boolean)
  1770. (defcustom org-highest-priority ?A
  1771. "The highest priority of TODO items. A character like ?A, ?B etc.
  1772. Must have a smaller ASCII number than `org-lowest-priority'."
  1773. :group 'org-priorities
  1774. :type 'character)
  1775. (defcustom org-lowest-priority ?C
  1776. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1777. Must have a larger ASCII number than `org-highest-priority'."
  1778. :group 'org-priorities
  1779. :type 'character)
  1780. (defcustom org-default-priority ?B
  1781. "The default priority of TODO items.
  1782. This is the priority an item get if no explicit priority is given."
  1783. :group 'org-priorities
  1784. :type 'character)
  1785. (defcustom org-priority-start-cycle-with-default t
  1786. "Non-nil means, start with default priority when starting to cycle.
  1787. When this is nil, the first step in the cycle will be (depending on the
  1788. command used) one higher or lower that the default priority."
  1789. :group 'org-priorities
  1790. :type 'boolean)
  1791. (defgroup org-time nil
  1792. "Options concerning time stamps and deadlines in Org-mode."
  1793. :tag "Org Time"
  1794. :group 'org)
  1795. (defcustom org-insert-labeled-timestamps-at-point nil
  1796. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1797. When nil, these labeled time stamps are forces into the second line of an
  1798. entry, just after the headline. When scheduling from the global TODO list,
  1799. the time stamp will always be forced into the second line."
  1800. :group 'org-time
  1801. :type 'boolean)
  1802. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1803. "Formats for `format-time-string' which are used for time stamps.
  1804. It is not recommended to change this constant.")
  1805. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1806. "Number of minutes to round time stamps to.
  1807. These are two values, the first applies when first creating a time stamp.
  1808. The second applies when changing it with the commands `S-up' and `S-down'.
  1809. When changing the time stamp, this means that it will change in steps
  1810. of N minutes, as given by the second value.
  1811. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1812. numbers should be factors of 60, so for example 5, 10, 15.
  1813. When this is larger than 1, you can still force an exact time-stamp by using
  1814. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1815. and by using a prefix arg to `S-up/down' to specify the exact number
  1816. of minutes to shift."
  1817. :group 'org-time
  1818. :get '(lambda (var) ; Make sure all entries have 5 elements
  1819. (if (integerp (default-value var))
  1820. (list (default-value var) 5)
  1821. (default-value var)))
  1822. :type '(list
  1823. (integer :tag "when inserting times")
  1824. (integer :tag "when modifying times")))
  1825. ;; Normalize old customizations of this variable.
  1826. (when (integerp org-time-stamp-rounding-minutes)
  1827. (setq org-time-stamp-rounding-minutes
  1828. (list org-time-stamp-rounding-minutes
  1829. org-time-stamp-rounding-minutes)))
  1830. (defcustom org-display-custom-times nil
  1831. "Non-nil means, overlay custom formats over all time stamps.
  1832. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1833. To turn this on on a per-file basis, insert anywhere in the file:
  1834. #+STARTUP: customtime"
  1835. :group 'org-time
  1836. :set 'set-default
  1837. :type 'sexp)
  1838. (make-variable-buffer-local 'org-display-custom-times)
  1839. (defcustom org-time-stamp-custom-formats
  1840. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1841. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1842. These are overlayed over the default ISO format if the variable
  1843. `org-display-custom-times' is set. Time like %H:%M should be at the
  1844. end of the second format. The custom formats are also honored by export
  1845. commands, if custom time display is turned on at the time of export."
  1846. :group 'org-time
  1847. :type 'sexp)
  1848. (defun org-time-stamp-format (&optional long inactive)
  1849. "Get the right format for a time string."
  1850. (let ((f (if long (cdr org-time-stamp-formats)
  1851. (car org-time-stamp-formats))))
  1852. (if inactive
  1853. (concat "[" (substring f 1 -1) "]")
  1854. f)))
  1855. (defcustom org-time-clocksum-format "%d:%02d"
  1856. "The format string used when creating CLOCKSUM lines, or when
  1857. org-mode generates a time duration."
  1858. :group 'org-time
  1859. :type 'string)
  1860. (defcustom org-deadline-warning-days 14
  1861. "No. of days before expiration during which a deadline becomes active.
  1862. This variable governs the display in sparse trees and in the agenda.
  1863. When 0 or negative, it means use this number (the absolute value of it)
  1864. even if a deadline has a different individual lead time specified.
  1865. Custom commands can set this variable in the options section."
  1866. :group 'org-time
  1867. :group 'org-agenda-daily/weekly
  1868. :type 'integer)
  1869. (defcustom org-read-date-prefer-future t
  1870. "Non-nil means, assume future for incomplete date input from user.
  1871. This affects the following situations:
  1872. 1. The user gives a day, but no month.
  1873. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1874. read this as the third of *next* month. However, if you enter \"17\",
  1875. it will be considered as *this* month.
  1876. 2. The user gives a month but not a year.
  1877. For example, if it is april and you enter \"feb 2\", this will be read
  1878. as feb 2, *next* year. \"May 5\", however, will be this year.
  1879. Currently this does not work for ISO week specifications.
  1880. When this option is nil, the current month and year will always be used
  1881. as defaults."
  1882. :group 'org-time
  1883. :type 'boolean)
  1884. (defcustom org-read-date-display-live t
  1885. "Non-nil means, display current interpretation of date prompt live.
  1886. This display will be in an overlay, in the minibuffer."
  1887. :group 'org-time
  1888. :type 'boolean)
  1889. (defcustom org-read-date-popup-calendar t
  1890. "Non-nil means, pop up a calendar when prompting for a date.
  1891. In the calendar, the date can be selected with mouse-1. However, the
  1892. minibuffer will also be active, and you can simply enter the date as well.
  1893. When nil, only the minibuffer will be available."
  1894. :group 'org-time
  1895. :type 'boolean)
  1896. (if (fboundp 'defvaralias)
  1897. (defvaralias 'org-popup-calendar-for-date-prompt
  1898. 'org-read-date-popup-calendar))
  1899. (defcustom org-read-date-minibuffer-setup-hook nil
  1900. "Hook to be used to set up keys for the date/time interface.
  1901. Add key definitions to `minibuffer-local-map', which will be a temporary
  1902. copy."
  1903. :group 'org-time
  1904. :type 'hook)
  1905. (defcustom org-extend-today-until 0
  1906. "The hour when your day really ends. Must be an integer.
  1907. This has influence for the following applications:
  1908. - When switching the agenda to \"today\". It it is still earlier than
  1909. the time given here, the day recognized as TODAY is actually yesterday.
  1910. - When a date is read from the user and it is still before the time given
  1911. here, the current date and time will be assumed to be yesterday, 23:59.
  1912. Also, timestamps inserted in remember templates follow this rule.
  1913. IMPORTANT: This is a feature whose implementation is and likely will
  1914. remain incomplete. Really, it is only here because past midnight seems to
  1915. be the favorite working time of John Wiegley :-)"
  1916. :group 'org-time
  1917. :type 'integer)
  1918. (defcustom org-edit-timestamp-down-means-later nil
  1919. "Non-nil means, S-down will increase the time in a time stamp.
  1920. When nil, S-up will increase."
  1921. :group 'org-time
  1922. :type 'boolean)
  1923. (defcustom org-calendar-follow-timestamp-change t
  1924. "Non-nil means, make the calendar window follow timestamp changes.
  1925. When a timestamp is modified and the calendar window is visible, it will be
  1926. moved to the new date."
  1927. :group 'org-time
  1928. :type 'boolean)
  1929. (defgroup org-tags nil
  1930. "Options concerning tags in Org-mode."
  1931. :tag "Org Tags"
  1932. :group 'org)
  1933. (defcustom org-tag-alist nil
  1934. "List of tags allowed in Org-mode files.
  1935. When this list is nil, Org-mode will base TAG input on what is already in the
  1936. buffer.
  1937. The value of this variable is an alist, the car of each entry must be a
  1938. keyword as a string, the cdr may be a character that is used to select
  1939. that tag through the fast-tag-selection interface.
  1940. See the manual for details."
  1941. :group 'org-tags
  1942. :type '(repeat
  1943. (choice
  1944. (cons (string :tag "Tag name")
  1945. (character :tag "Access char"))
  1946. (const :tag "Start radio group" (:startgroup))
  1947. (const :tag "End radio group" (:endgroup))
  1948. (const :tag "New line" (:newline)))))
  1949. (defcustom org-tag-persistent-alist nil
  1950. "List of tags that will always appear in all Org-mode files.
  1951. This is in addition to any in buffer settings or customizations
  1952. of `org-tag-alist'.
  1953. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1954. The value of this variable is an alist, the car of each entry must be a
  1955. keyword as a string, the cdr may be a character that is used to select
  1956. that tag through the fast-tag-selection interface.
  1957. See the manual for details.
  1958. To disable these tags on a per-file basis, insert anywhere in the file:
  1959. #+STARTUP: noptag"
  1960. :group 'org-tags
  1961. :type '(repeat
  1962. (choice
  1963. (cons (string :tag "Tag name")
  1964. (character :tag "Access char"))
  1965. (const :tag "Start radio group" (:startgroup))
  1966. (const :tag "End radio group" (:endgroup))
  1967. (const :tag "New line" (:newline)))))
  1968. (defvar org-file-tags nil
  1969. "List of tags that can be inherited by all entries in the file.
  1970. The tags will be inherited if the variable `org-use-tag-inheritance'
  1971. says they should be.
  1972. This variable is populated from #+TAG lines.")
  1973. (defcustom org-use-fast-tag-selection 'auto
  1974. "Non-nil means, use fast tag selection scheme.
  1975. This is a special interface to select and deselect tags with single keys.
  1976. When nil, fast selection is never used.
  1977. When the symbol `auto', fast selection is used if and only if selection
  1978. characters for tags have been configured, either through the variable
  1979. `org-tag-alist' or through a #+TAGS line in the buffer.
  1980. When t, fast selection is always used and selection keys are assigned
  1981. automatically if necessary."
  1982. :group 'org-tags
  1983. :type '(choice
  1984. (const :tag "Always" t)
  1985. (const :tag "Never" nil)
  1986. (const :tag "When selection characters are configured" 'auto)))
  1987. (defcustom org-fast-tag-selection-single-key nil
  1988. "Non-nil means, fast tag selection exits after first change.
  1989. When nil, you have to press RET to exit it.
  1990. During fast tag selection, you can toggle this flag with `C-c'.
  1991. This variable can also have the value `expert'. In this case, the window
  1992. displaying the tags menu is not even shown, until you press C-c again."
  1993. :group 'org-tags
  1994. :type '(choice
  1995. (const :tag "No" nil)
  1996. (const :tag "Yes" t)
  1997. (const :tag "Expert" expert)))
  1998. (defvar org-fast-tag-selection-include-todo nil
  1999. "Non-nil means, fast tags selection interface will also offer TODO states.
  2000. This is an undocumented feature, you should not rely on it.")
  2001. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2002. "The column to which tags should be indented in a headline.
  2003. If this number is positive, it specifies the column. If it is negative,
  2004. it means that the tags should be flushright to that column. For example,
  2005. -80 works well for a normal 80 character screen."
  2006. :group 'org-tags
  2007. :type 'integer)
  2008. (defcustom org-auto-align-tags t
  2009. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2010. These operations change the length of a headline and therefore shift
  2011. the tags around. With this options turned on, after each such operation
  2012. the tags are again aligned to `org-tags-column'."
  2013. :group 'org-tags
  2014. :type 'boolean)
  2015. (defcustom org-use-tag-inheritance t
  2016. "Non-nil means, tags in levels apply also for sublevels.
  2017. When nil, only the tags directly given in a specific line apply there.
  2018. This may also be a list of tags that should be inherited, or a regexp that
  2019. matches tags that should be inherited. Additional control is possible
  2020. with the variable `org-tags-exclude-from-inheritance' which gives an
  2021. explicit list of tags to be excluded from inheritance., even if the value of
  2022. `org-use-tag-inheritance' would select it for inheritance.
  2023. If this option is t, a match early-on in a tree can lead to a large
  2024. number of matches in the subtree when constructing the agenda or creating
  2025. a sparse tree. If you only want to see the first match in a tree during
  2026. a search, check out the variable `org-tags-match-list-sublevels'."
  2027. :group 'org-tags
  2028. :type '(choice
  2029. (const :tag "Not" nil)
  2030. (const :tag "Always" t)
  2031. (repeat :tag "Specific tags" (string :tag "Tag"))
  2032. (regexp :tag "Tags matched by regexp")))
  2033. (defcustom org-tags-exclude-from-inheritance nil
  2034. "List of tags that should never be inherited.
  2035. This is a way to exclude a few tags from inheritance. For way to do
  2036. the opposite, to actively allow inheritance for selected tags,
  2037. see the variable `org-use-tag-inheritance'."
  2038. :group 'org-tags
  2039. :type '(repeat (string :tag "Tag")))
  2040. (defun org-tag-inherit-p (tag)
  2041. "Check if TAG is one that should be inherited."
  2042. (cond
  2043. ((member tag org-tags-exclude-from-inheritance) nil)
  2044. ((eq org-use-tag-inheritance t) t)
  2045. ((not org-use-tag-inheritance) nil)
  2046. ((stringp org-use-tag-inheritance)
  2047. (string-match org-use-tag-inheritance tag))
  2048. ((listp org-use-tag-inheritance)
  2049. (member tag org-use-tag-inheritance))
  2050. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2051. (defcustom org-tags-match-list-sublevels t
  2052. "Non-nil means list also sublevels of headlines matching a search.
  2053. This variable applies to tags/property searches, and also to stuck
  2054. projects because this search is based on a tags match as well.
  2055. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2056. the sublevels of a headline matching a tag search often also match
  2057. the same search. Listing all of them can create very long lists.
  2058. Setting this variable to nil causes subtrees of a match to be skipped.
  2059. As a special case, if the tag search is restricted to TODO items, the
  2060. value of this variable is ignored and sublevels are always checked, to
  2061. make sure all corresponding TODO items find their way into the list.
  2062. This variable is semi-obsolete and probably should always be true. It
  2063. is better to limit inheritance to certain tags using the variables
  2064. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2065. :group 'org-tags
  2066. :type '(choice
  2067. (const :tag "No, don't list them" nil)
  2068. (const :tag "Yes, do list them" t)
  2069. (const :tag "List them, indented with leading dots" indented)))
  2070. (defvar org-tags-history nil
  2071. "History of minibuffer reads for tags.")
  2072. (defvar org-last-tags-completion-table nil
  2073. "The last used completion table for tags.")
  2074. (defvar org-after-tags-change-hook nil
  2075. "Hook that is run after the tags in a line have changed.")
  2076. (defgroup org-properties nil
  2077. "Options concerning properties in Org-mode."
  2078. :tag "Org Properties"
  2079. :group 'org)
  2080. (defcustom org-property-format "%-10s %s"
  2081. "How property key/value pairs should be formatted by `indent-line'.
  2082. When `indent-line' hits a property definition, it will format the line
  2083. according to this format, mainly to make sure that the values are
  2084. lined-up with respect to each other."
  2085. :group 'org-properties
  2086. :type 'string)
  2087. (defcustom org-use-property-inheritance nil
  2088. "Non-nil means, properties apply also for sublevels.
  2089. This setting is chiefly used during property searches. Turning it on can
  2090. cause significant overhead when doing a search, which is why it is not
  2091. on by default.
  2092. When nil, only the properties directly given in the current entry count.
  2093. When t, every property is inherited. The value may also be a list of
  2094. properties that should have inheritance, or a regular expression matching
  2095. properties that should be inherited.
  2096. However, note that some special properties use inheritance under special
  2097. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2098. and the properties ending in \"_ALL\" when they are used as descriptor
  2099. for valid values of a property.
  2100. Note for programmers:
  2101. When querying an entry with `org-entry-get', you can control if inheritance
  2102. should be used. By default, `org-entry-get' looks only at the local
  2103. properties. You can request inheritance by setting the inherit argument
  2104. to t (to force inheritance) or to `selective' (to respect the setting
  2105. in this variable)."
  2106. :group 'org-properties
  2107. :type '(choice
  2108. (const :tag "Not" nil)
  2109. (const :tag "Always" t)
  2110. (repeat :tag "Specific properties" (string :tag "Property"))
  2111. (regexp :tag "Properties matched by regexp")))
  2112. (defun org-property-inherit-p (property)
  2113. "Check if PROPERTY is one that should be inherited."
  2114. (cond
  2115. ((eq org-use-property-inheritance t) t)
  2116. ((not org-use-property-inheritance) nil)
  2117. ((stringp org-use-property-inheritance)
  2118. (string-match org-use-property-inheritance property))
  2119. ((listp org-use-property-inheritance)
  2120. (member property org-use-property-inheritance))
  2121. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2122. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2123. "The default column format, if no other format has been defined.
  2124. This variable can be set on the per-file basis by inserting a line
  2125. #+COLUMNS: %25ITEM ....."
  2126. :group 'org-properties
  2127. :type 'string)
  2128. (defcustom org-columns-ellipses ".."
  2129. "The ellipses to be used when a field in column view is truncated.
  2130. When this is the empty string, as many characters as possible are shown,
  2131. but then there will be no visual indication that the field has been truncated.
  2132. When this is a string of length N, the last N characters of a truncated
  2133. field are replaced by this string. If the column is narrower than the
  2134. ellipses string, only part of the ellipses string will be shown."
  2135. :group 'org-properties
  2136. :type 'string)
  2137. (defcustom org-columns-modify-value-for-display-function nil
  2138. "Function that modifies values for display in column view.
  2139. For example, it can be used to cut out a certain part from a time stamp.
  2140. The function must take 2 arguments:
  2141. column-title The title of the column (*not* the property name)
  2142. value The value that should be modified.
  2143. The function should return the value that should be displayed,
  2144. or nil if the normal value should be used."
  2145. :group 'org-properties
  2146. :type 'function)
  2147. (defcustom org-effort-property "Effort"
  2148. "The property that is being used to keep track of effort estimates.
  2149. Effort estimates given in this property need to have the format H:MM."
  2150. :group 'org-properties
  2151. :group 'org-progress
  2152. :type '(string :tag "Property"))
  2153. (defconst org-global-properties-fixed
  2154. '(("VISIBILITY_ALL" . "folded children content all")
  2155. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2156. "List of property/value pairs that can be inherited by any entry.
  2157. These are fixed values, for the preset properties. The user variable
  2158. that can be used to add to this list is `org-global-properties'.
  2159. The entries in this list are cons cells where the car is a property
  2160. name and cdr is a string with the value. If the value represents
  2161. multiple items like an \"_ALL\" property, separate the items by
  2162. spaces.")
  2163. (defcustom org-global-properties nil
  2164. "List of property/value pairs that can be inherited by any entry.
  2165. This list will be combined with the constant `org-global-properties-fixed'.
  2166. The entries in this list are cons cells where the car is a property
  2167. name and cdr is a string with the value.
  2168. You can set buffer-local values for the same purpose in the variable
  2169. `org-file-properties' this by adding lines like
  2170. #+PROPERTY: NAME VALUE"
  2171. :group 'org-properties
  2172. :type '(repeat
  2173. (cons (string :tag "Property")
  2174. (string :tag "Value"))))
  2175. (defvar org-file-properties nil
  2176. "List of property/value pairs that can be inherited by any entry.
  2177. Valid for the current buffer.
  2178. This variable is populated from #+PROPERTY lines.")
  2179. (make-variable-buffer-local 'org-file-properties)
  2180. (defgroup org-agenda nil
  2181. "Options concerning agenda views in Org-mode."
  2182. :tag "Org Agenda"
  2183. :group 'org)
  2184. (defvar org-category nil
  2185. "Variable used by org files to set a category for agenda display.
  2186. Such files should use a file variable to set it, for example
  2187. # -*- mode: org; org-category: \"ELisp\"
  2188. or contain a special line
  2189. #+CATEGORY: ELisp
  2190. If the file does not specify a category, then file's base name
  2191. is used instead.")
  2192. (make-variable-buffer-local 'org-category)
  2193. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2194. (defcustom org-agenda-files nil
  2195. "The files to be used for agenda display.
  2196. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2197. \\[org-remove-file]. You can also use customize to edit the list.
  2198. If an entry is a directory, all files in that directory that are matched by
  2199. `org-agenda-file-regexp' will be part of the file list.
  2200. If the value of the variable is not a list but a single file name, then
  2201. the list of agenda files is actually stored and maintained in that file, one
  2202. agenda file per line."
  2203. :group 'org-agenda
  2204. :type '(choice
  2205. (repeat :tag "List of files and directories" file)
  2206. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2207. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2208. "Regular expression to match files for `org-agenda-files'.
  2209. If any element in the list in that variable contains a directory instead
  2210. of a normal file, all files in that directory that are matched by this
  2211. regular expression will be included."
  2212. :group 'org-agenda
  2213. :type 'regexp)
  2214. (defcustom org-agenda-text-search-extra-files nil
  2215. "List of extra files to be searched by text search commands.
  2216. These files will be search in addition to the agenda files by the
  2217. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2218. Note that these files will only be searched for text search commands,
  2219. not for the other agenda views like todo lists, tag searches or the weekly
  2220. agenda. This variable is intended to list notes and possibly archive files
  2221. that should also be searched by these two commands.
  2222. In fact, if the first element in the list is the symbol `agenda-archives',
  2223. than all archive files of all agenda files will be added to the search
  2224. scope."
  2225. :group 'org-agenda
  2226. :type '(set :greedy t
  2227. (const :tag "Agenda Archives" agenda-archives)
  2228. (repeat :inline t (file))))
  2229. (if (fboundp 'defvaralias)
  2230. (defvaralias 'org-agenda-multi-occur-extra-files
  2231. 'org-agenda-text-search-extra-files))
  2232. (defcustom org-agenda-skip-unavailable-files nil
  2233. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2234. A nil value means to remove them, after a query, from the list."
  2235. :group 'org-agenda
  2236. :type 'boolean)
  2237. (defcustom org-calendar-to-agenda-key [?c]
  2238. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2239. The command `org-calendar-goto-agenda' will be bound to this key. The
  2240. default is the character `c' because then `c' can be used to switch back and
  2241. forth between agenda and calendar."
  2242. :group 'org-agenda
  2243. :type 'sexp)
  2244. (defcustom org-calendar-agenda-action-key [?k]
  2245. "The key to be installed in `calendar-mode-map' for agenda-action.
  2246. The command `org-agenda-action' will be bound to this key. The
  2247. default is the character `k' because we use the same key in the agenda."
  2248. :group 'org-agenda
  2249. :type 'sexp)
  2250. (eval-after-load "calendar"
  2251. '(progn
  2252. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2253. 'org-calendar-goto-agenda)
  2254. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2255. 'org-agenda-action)))
  2256. (defgroup org-latex nil
  2257. "Options for embedding LaTeX code into Org-mode."
  2258. :tag "Org LaTeX"
  2259. :group 'org)
  2260. (defcustom org-format-latex-options
  2261. '(:foreground default :background default :scale 1.0
  2262. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2263. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2264. "Options for creating images from LaTeX fragments.
  2265. This is a property list with the following properties:
  2266. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2267. `default' means use the foreground of the default face.
  2268. :background the background color, or \"Transparent\".
  2269. `default' means use the background of the default face.
  2270. :scale a scaling factor for the size of the images.
  2271. :html-foreground, :html-background, :html-scale
  2272. the same numbers for HTML export.
  2273. :matchers a list indicating which matchers should be used to
  2274. find LaTeX fragments. Valid members of this list are:
  2275. \"begin\" find environments
  2276. \"$1\" find single characters surrounded by $.$
  2277. \"$\" find math expressions surrounded by $...$
  2278. \"$$\" find math expressions surrounded by $$....$$
  2279. \"\\(\" find math expressions surrounded by \\(...\\)
  2280. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2281. :group 'org-latex
  2282. :type 'plist)
  2283. (defcustom org-format-latex-header "\\documentclass{article}
  2284. \\usepackage{fullpage} % do not remove
  2285. \\usepackage{amssymb}
  2286. \\usepackage[usenames]{color}
  2287. \\usepackage{amsmath}
  2288. \\usepackage{latexsym}
  2289. \\usepackage[mathscr]{eucal}
  2290. \\pagestyle{empty} % do not remove"
  2291. "The document header used for processing LaTeX fragments."
  2292. :group 'org-latex
  2293. :type 'string)
  2294. (defgroup org-font-lock nil
  2295. "Font-lock settings for highlighting in Org-mode."
  2296. :tag "Org Font Lock"
  2297. :group 'org)
  2298. (defcustom org-level-color-stars-only nil
  2299. "Non-nil means fontify only the stars in each headline.
  2300. When nil, the entire headline is fontified.
  2301. Changing it requires restart of `font-lock-mode' to become effective
  2302. also in regions already fontified."
  2303. :group 'org-font-lock
  2304. :type 'boolean)
  2305. (defcustom org-hide-leading-stars nil
  2306. "Non-nil means, hide the first N-1 stars in a headline.
  2307. This works by using the face `org-hide' for these stars. This
  2308. face is white for a light background, and black for a dark
  2309. background. You may have to customize the face `org-hide' to
  2310. make this work.
  2311. Changing it requires restart of `font-lock-mode' to become effective
  2312. also in regions already fontified.
  2313. You may also set this on a per-file basis by adding one of the following
  2314. lines to the buffer:
  2315. #+STARTUP: hidestars
  2316. #+STARTUP: showstars"
  2317. :group 'org-font-lock
  2318. :type 'boolean)
  2319. (defcustom org-fontify-done-headline nil
  2320. "Non-nil means, change the face of a headline if it is marked DONE.
  2321. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2322. When this is non-nil, the headline after the keyword is set to the
  2323. `org-headline-done' as an additional indication."
  2324. :group 'org-font-lock
  2325. :type 'boolean)
  2326. (defcustom org-fontify-emphasized-text t
  2327. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2328. Changing this variable requires a restart of Emacs to take effect."
  2329. :group 'org-font-lock
  2330. :type 'boolean)
  2331. (defcustom org-highlight-latex-fragments-and-specials nil
  2332. "Non-nil means, fontify what is treated specially by the exporters."
  2333. :group 'org-font-lock
  2334. :type 'boolean)
  2335. (defcustom org-hide-emphasis-markers nil
  2336. "Non-nil mean font-lock should hide the emphasis marker characters."
  2337. :group 'org-font-lock
  2338. :type 'boolean)
  2339. (defvar org-emph-re nil
  2340. "Regular expression for matching emphasis.")
  2341. (defvar org-verbatim-re nil
  2342. "Regular expression for matching verbatim text.")
  2343. (defvar org-emphasis-regexp-components) ; defined just below
  2344. (defvar org-emphasis-alist) ; defined just below
  2345. (defun org-set-emph-re (var val)
  2346. "Set variable and compute the emphasis regular expression."
  2347. (set var val)
  2348. (when (and (boundp 'org-emphasis-alist)
  2349. (boundp 'org-emphasis-regexp-components)
  2350. org-emphasis-alist org-emphasis-regexp-components)
  2351. (let* ((e org-emphasis-regexp-components)
  2352. (pre (car e))
  2353. (post (nth 1 e))
  2354. (border (nth 2 e))
  2355. (body (nth 3 e))
  2356. (nl (nth 4 e))
  2357. (body1 (concat body "*?"))
  2358. (markers (mapconcat 'car org-emphasis-alist ""))
  2359. (vmarkers (mapconcat
  2360. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2361. org-emphasis-alist "")))
  2362. ;; make sure special characters appear at the right position in the class
  2363. (if (string-match "\\^" markers)
  2364. (setq markers (concat (replace-match "" t t markers) "^")))
  2365. (if (string-match "-" markers)
  2366. (setq markers (concat (replace-match "" t t markers) "-")))
  2367. (if (string-match "\\^" vmarkers)
  2368. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2369. (if (string-match "-" vmarkers)
  2370. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2371. (if (> nl 0)
  2372. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2373. (int-to-string nl) "\\}")))
  2374. ;; Make the regexp
  2375. (setq org-emph-re
  2376. (concat "\\([" pre "]\\|^\\)"
  2377. "\\("
  2378. "\\([" markers "]\\)"
  2379. "\\("
  2380. "[^" border "]\\|"
  2381. "[^" border "]"
  2382. body1
  2383. "[^" border "]"
  2384. "\\)"
  2385. "\\3\\)"
  2386. "\\([" post "]\\|$\\)"))
  2387. (setq org-verbatim-re
  2388. (concat "\\([" pre "]\\|^\\)"
  2389. "\\("
  2390. "\\([" vmarkers "]\\)"
  2391. "\\("
  2392. "[^" border "]\\|"
  2393. "[^" border "]"
  2394. body1
  2395. "[^" border "]"
  2396. "\\)"
  2397. "\\3\\)"
  2398. "\\([" post "]\\|$\\)")))))
  2399. (defcustom org-emphasis-regexp-components
  2400. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2401. "Components used to build the regular expression for emphasis.
  2402. This is a list with 6 entries. Terminology: In an emphasis string
  2403. like \" *strong word* \", we call the initial space PREMATCH, the final
  2404. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2405. and \"trong wor\" is the body. The different components in this variable
  2406. specify what is allowed/forbidden in each part:
  2407. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2408. post Chars allowed as postmatch. End of line will be allowed too.
  2409. border The chars *forbidden* as border characters.
  2410. body-regexp A regexp like \".\" to match a body character. Don't use
  2411. non-shy groups here, and don't allow newline here.
  2412. newline The maximum number of newlines allowed in an emphasis exp.
  2413. Use customize to modify this, or restart Emacs after changing it."
  2414. :group 'org-font-lock
  2415. :set 'org-set-emph-re
  2416. :type '(list
  2417. (sexp :tag "Allowed chars in pre ")
  2418. (sexp :tag "Allowed chars in post ")
  2419. (sexp :tag "Forbidden chars in border ")
  2420. (sexp :tag "Regexp for body ")
  2421. (integer :tag "number of newlines allowed")
  2422. (option (boolean :tag "Please ignore this button"))))
  2423. (defcustom org-emphasis-alist
  2424. `(("*" bold "<b>" "</b>")
  2425. ("/" italic "<i>" "</i>")
  2426. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2427. ("=" org-code "<code>" "</code>" verbatim)
  2428. ("~" org-verbatim "<code>" "</code>" verbatim)
  2429. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2430. "<del>" "</del>")
  2431. )
  2432. "Special syntax for emphasized text.
  2433. Text starting and ending with a special character will be emphasized, for
  2434. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2435. characters, the face to be used by font-lock for highlighting in Org-mode
  2436. Emacs buffers, and the HTML tags to be used for this.
  2437. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2438. Use customize to modify this, or restart Emacs after changing it."
  2439. :group 'org-font-lock
  2440. :set 'org-set-emph-re
  2441. :type '(repeat
  2442. (list
  2443. (string :tag "Marker character")
  2444. (choice
  2445. (face :tag "Font-lock-face")
  2446. (plist :tag "Face property list"))
  2447. (string :tag "HTML start tag")
  2448. (string :tag "HTML end tag")
  2449. (option (const verbatim)))))
  2450. ;;; Miscellaneous options
  2451. (defgroup org-completion nil
  2452. "Completion in Org-mode."
  2453. :tag "Org Completion"
  2454. :group 'org)
  2455. (defcustom org-completion-use-ido nil
  2456. "Non-nil means, use ido completion wherever possible.
  2457. Note that `ido-mode' must be active for this variable to be relevant.
  2458. If you decide to turn this variable on, you might well want to turn off
  2459. `org-outline-path-complete-in-steps'."
  2460. :group 'org-completion
  2461. :type 'boolean)
  2462. (defcustom org-completion-fallback-command 'hippie-expand
  2463. "The expansion command called by \\[org-complete] in normal context.
  2464. Normal means, no org-mode-specific context."
  2465. :group 'org-completion
  2466. :type 'function)
  2467. ;;; Functions and variables from ther packages
  2468. ;; Declared here to avoid compiler warnings
  2469. ;; XEmacs only
  2470. (defvar outline-mode-menu-heading)
  2471. (defvar outline-mode-menu-show)
  2472. (defvar outline-mode-menu-hide)
  2473. (defvar zmacs-regions) ; XEmacs regions
  2474. ;; Emacs only
  2475. (defvar mark-active)
  2476. ;; Various packages
  2477. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2478. (declare-function calendar-forward-day "cal-move" (arg))
  2479. (declare-function calendar-goto-date "cal-move" (date))
  2480. (declare-function calendar-goto-today "cal-move" ())
  2481. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2482. (defvar calc-embedded-close-formula)
  2483. (defvar calc-embedded-open-formula)
  2484. (declare-function cdlatex-tab "ext:cdlatex" ())
  2485. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2486. (defvar font-lock-unfontify-region-function)
  2487. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2488. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2489. (defvar iswitchb-temp-buflist)
  2490. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2491. (defvar org-agenda-tags-todo-honor-ignore-options)
  2492. (declare-function org-agenda-skip "org-agenda" ())
  2493. (declare-function org-format-agenda-item "org-agenda"
  2494. (extra txt &optional category tags dotime noprefix remove-re))
  2495. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2496. (declare-function org-agenda-change-all-lines "org-agenda"
  2497. (newhead hdmarker &optional fixface just-this))
  2498. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2499. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2500. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2501. (beg end))
  2502. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2503. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2504. "org-agenda" (&optional end))
  2505. (declare-function parse-time-string "parse-time" (string))
  2506. (declare-function remember "remember" (&optional initial))
  2507. (declare-function remember-buffer-desc "remember" ())
  2508. (declare-function remember-finalize "remember" ())
  2509. (defvar remember-save-after-remembering)
  2510. (defvar remember-data-file)
  2511. (defvar remember-register)
  2512. (defvar remember-buffer)
  2513. (defvar remember-handler-functions)
  2514. (defvar remember-annotation-functions)
  2515. (defvar texmathp-why)
  2516. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2517. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2518. (defvar w3m-current-url)
  2519. (defvar w3m-current-title)
  2520. (defvar org-latex-regexps)
  2521. ;;; Autoload and prepare some org modules
  2522. ;; Some table stuff that needs to be defined here, because it is used
  2523. ;; by the functions setting up org-mode or checking for table context.
  2524. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2525. "Detects an org-type or table-type table.")
  2526. (defconst org-table-line-regexp "^[ \t]*|"
  2527. "Detects an org-type table line.")
  2528. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2529. "Detects an org-type table line.")
  2530. (defconst org-table-hline-regexp "^[ \t]*|-"
  2531. "Detects an org-type table hline.")
  2532. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2533. "Detects a table-type table hline.")
  2534. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2535. "Searching from within a table (any type) this finds the first line
  2536. outside the table.")
  2537. ;; Autoload the functions in org-table.el that are needed by functions here.
  2538. (eval-and-compile
  2539. (org-autoload "org-table"
  2540. '(org-table-align org-table-begin org-table-blank-field
  2541. org-table-convert org-table-convert-region org-table-copy-down
  2542. org-table-copy-region org-table-create
  2543. org-table-create-or-convert-from-region
  2544. org-table-create-with-table.el org-table-current-dline
  2545. org-table-cut-region org-table-delete-column org-table-edit-field
  2546. org-table-edit-formulas org-table-end org-table-eval-formula
  2547. org-table-export org-table-field-info
  2548. org-table-get-stored-formulas org-table-goto-column
  2549. org-table-hline-and-move org-table-import org-table-insert-column
  2550. org-table-insert-hline org-table-insert-row org-table-iterate
  2551. org-table-justify-field-maybe org-table-kill-row
  2552. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2553. org-table-move-column org-table-move-column-left
  2554. org-table-move-column-right org-table-move-row
  2555. org-table-move-row-down org-table-move-row-up
  2556. org-table-next-field org-table-next-row org-table-paste-rectangle
  2557. org-table-previous-field org-table-recalculate
  2558. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2559. org-table-toggle-coordinate-overlays
  2560. org-table-toggle-formula-debugger org-table-wrap-region
  2561. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2562. (defun org-at-table-p (&optional table-type)
  2563. "Return t if the cursor is inside an org-type table.
  2564. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2565. (if org-enable-table-editor
  2566. (save-excursion
  2567. (beginning-of-line 1)
  2568. (looking-at (if table-type org-table-any-line-regexp
  2569. org-table-line-regexp)))
  2570. nil))
  2571. (defsubst org-table-p () (org-at-table-p))
  2572. (defun org-at-table.el-p ()
  2573. "Return t if and only if we are at a table.el table."
  2574. (and (org-at-table-p 'any)
  2575. (save-excursion
  2576. (goto-char (org-table-begin 'any))
  2577. (looking-at org-table1-hline-regexp))))
  2578. (defun org-table-recognize-table.el ()
  2579. "If there is a table.el table nearby, recognize it and move into it."
  2580. (if org-table-tab-recognizes-table.el
  2581. (if (org-at-table.el-p)
  2582. (progn
  2583. (beginning-of-line 1)
  2584. (if (looking-at org-table-dataline-regexp)
  2585. nil
  2586. (if (looking-at org-table1-hline-regexp)
  2587. (progn
  2588. (beginning-of-line 2)
  2589. (if (looking-at org-table-any-border-regexp)
  2590. (beginning-of-line -1)))))
  2591. (if (re-search-forward "|" (org-table-end t) t)
  2592. (progn
  2593. (require 'table)
  2594. (if (table--at-cell-p (point))
  2595. t
  2596. (message "recognizing table.el table...")
  2597. (table-recognize-table)
  2598. (message "recognizing table.el table...done")))
  2599. (error "This should not happen..."))
  2600. t)
  2601. nil)
  2602. nil))
  2603. (defun org-at-table-hline-p ()
  2604. "Return t if the cursor is inside a hline in a table."
  2605. (if org-enable-table-editor
  2606. (save-excursion
  2607. (beginning-of-line 1)
  2608. (looking-at org-table-hline-regexp))
  2609. nil))
  2610. (defvar org-table-clean-did-remove-column nil)
  2611. (defun org-table-map-tables (function)
  2612. "Apply FUNCTION to the start of all tables in the buffer."
  2613. (save-excursion
  2614. (save-restriction
  2615. (widen)
  2616. (goto-char (point-min))
  2617. (while (re-search-forward org-table-any-line-regexp nil t)
  2618. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2619. (beginning-of-line 1)
  2620. (if (looking-at org-table-line-regexp)
  2621. (save-excursion (funcall function)))
  2622. (re-search-forward org-table-any-border-regexp nil 1))))
  2623. (message "Mapping tables: done"))
  2624. ;; Declare and autoload functions from org-exp.el & Co
  2625. (declare-function org-default-export-plist "org-exp")
  2626. (declare-function org-infile-export-plist "org-exp")
  2627. (declare-function org-get-current-options "org-exp")
  2628. (eval-and-compile
  2629. (org-autoload "org-exp"
  2630. '(org-export org-export-visible
  2631. org-insert-export-options-template
  2632. org-table-clean-before-export))
  2633. (org-autoload "org-ascii"
  2634. '(org-export-as-ascii org-export-ascii-preprocess
  2635. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2636. org-export-region-as-ascii))
  2637. (org-autoload "org-html"
  2638. '(org-export-as-html-and-open
  2639. org-export-as-html-batch org-export-as-html-to-buffer
  2640. org-replace-region-by-html org-export-region-as-html
  2641. org-export-as-html))
  2642. (org-autoload "org-icalendar"
  2643. '(org-export-icalendar-this-file
  2644. org-export-icalendar-all-agenda-files
  2645. org-export-icalendar-combine-agenda-files))
  2646. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2647. ;; Declare and autoload functions from org-agenda.el
  2648. (eval-and-compile
  2649. (org-autoload "org-agenda"
  2650. '(org-agenda org-agenda-list org-search-view
  2651. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2652. org-diary org-agenda-to-appt
  2653. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2654. ;; Autoload org-remember
  2655. (eval-and-compile
  2656. (org-autoload "org-remember"
  2657. '(org-remember-insinuate org-remember-annotation
  2658. org-remember-apply-template org-remember org-remember-handler)))
  2659. ;; Autoload org-clock.el
  2660. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2661. (beg end))
  2662. (declare-function org-clock-update-mode-line "org-clock" ())
  2663. (defvar org-clock-start-time)
  2664. (defvar org-clock-marker (make-marker)
  2665. "Marker recording the last clock-in.")
  2666. (eval-and-compile
  2667. (org-autoload
  2668. "org-clock"
  2669. '(org-clock-in org-clock-out org-clock-cancel
  2670. org-clock-goto org-clock-sum org-clock-display
  2671. org-clock-remove-overlays org-clock-report
  2672. org-clocktable-shift org-dblock-write:clocktable
  2673. org-get-clocktable)))
  2674. (defun org-clock-update-time-maybe ()
  2675. "If this is a CLOCK line, update it and return t.
  2676. Otherwise, return nil."
  2677. (interactive)
  2678. (save-excursion
  2679. (beginning-of-line 1)
  2680. (skip-chars-forward " \t")
  2681. (when (looking-at org-clock-string)
  2682. (let ((re (concat "[ \t]*" org-clock-string
  2683. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2684. "\\([ \t]*=>.*\\)?\\)?"))
  2685. ts te h m s neg)
  2686. (cond
  2687. ((not (looking-at re))
  2688. nil)
  2689. ((not (match-end 2))
  2690. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2691. (> org-clock-marker (point))
  2692. (<= org-clock-marker (point-at-eol)))
  2693. ;; The clock is running here
  2694. (setq org-clock-start-time
  2695. (apply 'encode-time
  2696. (org-parse-time-string (match-string 1))))
  2697. (org-clock-update-mode-line)))
  2698. (t
  2699. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2700. (end-of-line 1)
  2701. (setq ts (match-string 1)
  2702. te (match-string 3))
  2703. (setq s (- (time-to-seconds
  2704. (apply 'encode-time (org-parse-time-string te)))
  2705. (time-to-seconds
  2706. (apply 'encode-time (org-parse-time-string ts))))
  2707. neg (< s 0)
  2708. s (abs s)
  2709. h (floor (/ s 3600))
  2710. s (- s (* 3600 h))
  2711. m (floor (/ s 60))
  2712. s (- s (* 60 s)))
  2713. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2714. t))))))
  2715. (defun org-check-running-clock ()
  2716. "Check if the current buffer contains the running clock.
  2717. If yes, offer to stop it and to save the buffer with the changes."
  2718. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2719. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2720. (buffer-name))))
  2721. (org-clock-out)
  2722. (when (y-or-n-p "Save changed buffer?")
  2723. (save-buffer))))
  2724. (defun org-clocktable-try-shift (dir n)
  2725. "Check if this line starts a clock table, if yes, shift the time block."
  2726. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2727. (org-clocktable-shift dir n)))
  2728. ;; Autoload org-timer.el
  2729. (eval-and-compile
  2730. (org-autoload
  2731. "org-timer"
  2732. '(org-timer-start org-timer org-timer-item
  2733. org-timer-change-times-in-region)))
  2734. ;; Autoload org-feed.el
  2735. (eval-and-compile
  2736. (org-autoload
  2737. "org-feed"
  2738. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2739. ;; Autoload archiving code
  2740. ;; The stuff that is needed for cycling and tags has to be defined here.
  2741. (defgroup org-archive nil
  2742. "Options concerning archiving in Org-mode."
  2743. :tag "Org Archive"
  2744. :group 'org-structure)
  2745. (defcustom org-archive-location "%s_archive::"
  2746. "The location where subtrees should be archived.
  2747. The value of this variable is a string, consisting of two parts,
  2748. separated by a double-colon. The first part is a filename and
  2749. the second part is a headline.
  2750. When the filename is omitted, archiving happens in the same file.
  2751. %s in the filename will be replaced by the current file
  2752. name (without the directory part). Archiving to a different file
  2753. is useful to keep archived entries from contributing to the
  2754. Org-mode Agenda.
  2755. The archived entries will be filed as subtrees of the specified
  2756. headline. When the headline is omitted, the subtrees are simply
  2757. filed away at the end of the file, as top-level entries. Also in
  2758. the heading you can use %s to represent the file name, this can be
  2759. useful when using the same archive for a number of different files.
  2760. Here are a few examples:
  2761. \"%s_archive::\"
  2762. If the current file is Projects.org, archive in file
  2763. Projects.org_archive, as top-level trees. This is the default.
  2764. \"::* Archived Tasks\"
  2765. Archive in the current file, under the top-level headline
  2766. \"* Archived Tasks\".
  2767. \"~/org/archive.org::\"
  2768. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2769. \"~/org/archive.org::From %s\"
  2770. Archive in file ~/org/archive.org (absolute path), und headlines
  2771. \"From FILENAME\" where file name is the current file name.
  2772. \"basement::** Finished Tasks\"
  2773. Archive in file ./basement (relative path), as level 3 trees
  2774. below the level 2 heading \"** Finished Tasks\".
  2775. You may set this option on a per-file basis by adding to the buffer a
  2776. line like
  2777. #+ARCHIVE: basement::** Finished Tasks
  2778. You may also define it locally for a subtree by setting an ARCHIVE property
  2779. in the entry. If such a property is found in an entry, or anywhere up
  2780. the hierarchy, it will be used."
  2781. :group 'org-archive
  2782. :type 'string)
  2783. (defcustom org-archive-tag "ARCHIVE"
  2784. "The tag that marks a subtree as archived.
  2785. An archived subtree does not open during visibility cycling, and does
  2786. not contribute to the agenda listings.
  2787. After changing this, font-lock must be restarted in the relevant buffers to
  2788. get the proper fontification."
  2789. :group 'org-archive
  2790. :group 'org-keywords
  2791. :type 'string)
  2792. (defcustom org-agenda-skip-archived-trees t
  2793. "Non-nil means, the agenda will skip any items located in archived trees.
  2794. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2795. variable is no longer recommended, you should leave it at the value t.
  2796. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2797. :group 'org-archive
  2798. :group 'org-agenda-skip
  2799. :type 'boolean)
  2800. (defcustom org-columns-skip-arrchived-trees t
  2801. "Non-nil means, irgnore archived trees when creating column view."
  2802. :group 'org-archive
  2803. :group 'org-properties
  2804. :type 'boolean)
  2805. (defcustom org-cycle-open-archived-trees nil
  2806. "Non-nil means, `org-cycle' will open archived trees.
  2807. An archived tree is a tree marked with the tag ARCHIVE.
  2808. When nil, archived trees will stay folded. You can still open them with
  2809. normal outline commands like `show-all', but not with the cycling commands."
  2810. :group 'org-archive
  2811. :group 'org-cycle
  2812. :type 'boolean)
  2813. (defcustom org-sparse-tree-open-archived-trees nil
  2814. "Non-nil means sparse tree construction shows matches in archived trees.
  2815. When nil, matches in these trees are highlighted, but the trees are kept in
  2816. collapsed state."
  2817. :group 'org-archive
  2818. :group 'org-sparse-trees
  2819. :type 'boolean)
  2820. (defun org-cycle-hide-archived-subtrees (state)
  2821. "Re-hide all archived subtrees after a visibility state change."
  2822. (when (and (not org-cycle-open-archived-trees)
  2823. (not (memq state '(overview folded))))
  2824. (save-excursion
  2825. (let* ((globalp (memq state '(contents all)))
  2826. (beg (if globalp (point-min) (point)))
  2827. (end (if globalp (point-max) (org-end-of-subtree t))))
  2828. (org-hide-archived-subtrees beg end)
  2829. (goto-char beg)
  2830. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2831. (message "%s" (substitute-command-keys
  2832. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2833. (defun org-force-cycle-archived ()
  2834. "Cycle subtree even if it is archived."
  2835. (interactive)
  2836. (setq this-command 'org-cycle)
  2837. (let ((org-cycle-open-archived-trees t))
  2838. (call-interactively 'org-cycle)))
  2839. (defun org-hide-archived-subtrees (beg end)
  2840. "Re-hide all archived subtrees after a visibility state change."
  2841. (save-excursion
  2842. (let* ((re (concat ":" org-archive-tag ":")))
  2843. (goto-char beg)
  2844. (while (re-search-forward re end t)
  2845. (and (org-on-heading-p) (hide-subtree))
  2846. (org-end-of-subtree t)))))
  2847. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2848. (eval-and-compile
  2849. (org-autoload "org-archive"
  2850. '(org-add-archive-files org-archive-subtree
  2851. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2852. ;; Autoload Column View Code
  2853. (declare-function org-columns-number-to-string "org-colview")
  2854. (declare-function org-columns-get-format-and-top-level "org-colview")
  2855. (declare-function org-columns-compute "org-colview")
  2856. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2857. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2858. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2859. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2860. ;; Autoload ID code
  2861. (declare-function org-id-store-link "org-id")
  2862. (declare-function org-id-locations-load "org-id")
  2863. (declare-function org-id-locations-save "org-id")
  2864. (defvar org-id-track-globally)
  2865. (org-autoload "org-id"
  2866. '(org-id-get-create org-id-new org-id-copy org-id-get
  2867. org-id-get-with-outline-path-completion
  2868. org-id-get-with-outline-drilling
  2869. org-id-goto org-id-find org-id-store-link))
  2870. ;; Autoload Plotting Code
  2871. (org-autoload "org-plot"
  2872. '(org-plot/gnuplot))
  2873. ;;; Variables for pre-computed regular expressions, all buffer local
  2874. (defvar org-drawer-regexp nil
  2875. "Matches first line of a hidden block.")
  2876. (make-variable-buffer-local 'org-drawer-regexp)
  2877. (defvar org-todo-regexp nil
  2878. "Matches any of the TODO state keywords.")
  2879. (make-variable-buffer-local 'org-todo-regexp)
  2880. (defvar org-not-done-regexp nil
  2881. "Matches any of the TODO state keywords except the last one.")
  2882. (make-variable-buffer-local 'org-not-done-regexp)
  2883. (defvar org-not-done-heading-regexp nil
  2884. "Matches a TODO headline that is not done.")
  2885. (make-variable-buffer-local 'org-not-done-regexp)
  2886. (defvar org-todo-line-regexp nil
  2887. "Matches a headline and puts TODO state into group 2 if present.")
  2888. (make-variable-buffer-local 'org-todo-line-regexp)
  2889. (defvar org-complex-heading-regexp nil
  2890. "Matches a headline and puts everything into groups:
  2891. group 1: the stars
  2892. group 2: The todo keyword, maybe
  2893. group 3: Priority cookie
  2894. group 4: True headline
  2895. group 5: Tags")
  2896. (make-variable-buffer-local 'org-complex-heading-regexp)
  2897. (defvar org-todo-line-tags-regexp nil
  2898. "Matches a headline and puts TODO state into group 2 if present.
  2899. Also put tags into group 4 if tags are present.")
  2900. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2901. (defvar org-nl-done-regexp nil
  2902. "Matches newline followed by a headline with the DONE keyword.")
  2903. (make-variable-buffer-local 'org-nl-done-regexp)
  2904. (defvar org-looking-at-done-regexp nil
  2905. "Matches the DONE keyword a point.")
  2906. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2907. (defvar org-ds-keyword-length 12
  2908. "Maximum length of the Deadline and SCHEDULED keywords.")
  2909. (make-variable-buffer-local 'org-ds-keyword-length)
  2910. (defvar org-deadline-regexp nil
  2911. "Matches the DEADLINE keyword.")
  2912. (make-variable-buffer-local 'org-deadline-regexp)
  2913. (defvar org-deadline-time-regexp nil
  2914. "Matches the DEADLINE keyword together with a time stamp.")
  2915. (make-variable-buffer-local 'org-deadline-time-regexp)
  2916. (defvar org-deadline-line-regexp nil
  2917. "Matches the DEADLINE keyword and the rest of the line.")
  2918. (make-variable-buffer-local 'org-deadline-line-regexp)
  2919. (defvar org-scheduled-regexp nil
  2920. "Matches the SCHEDULED keyword.")
  2921. (make-variable-buffer-local 'org-scheduled-regexp)
  2922. (defvar org-scheduled-time-regexp nil
  2923. "Matches the SCHEDULED keyword together with a time stamp.")
  2924. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2925. (defvar org-closed-time-regexp nil
  2926. "Matches the CLOSED keyword together with a time stamp.")
  2927. (make-variable-buffer-local 'org-closed-time-regexp)
  2928. (defvar org-keyword-time-regexp nil
  2929. "Matches any of the 4 keywords, together with the time stamp.")
  2930. (make-variable-buffer-local 'org-keyword-time-regexp)
  2931. (defvar org-keyword-time-not-clock-regexp nil
  2932. "Matches any of the 3 keywords, together with the time stamp.")
  2933. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2934. (defvar org-maybe-keyword-time-regexp nil
  2935. "Matches a timestamp, possibly preceeded by a keyword.")
  2936. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2937. (defvar org-planning-or-clock-line-re nil
  2938. "Matches a line with planning or clock info.")
  2939. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2940. (defconst org-plain-time-of-day-regexp
  2941. (concat
  2942. "\\(\\<[012]?[0-9]"
  2943. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2944. "\\(--?"
  2945. "\\(\\<[012]?[0-9]"
  2946. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2947. "\\)?")
  2948. "Regular expression to match a plain time or time range.
  2949. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2950. groups carry important information:
  2951. 0 the full match
  2952. 1 the first time, range or not
  2953. 8 the second time, if it is a range.")
  2954. (defconst org-plain-time-extension-regexp
  2955. (concat
  2956. "\\(\\<[012]?[0-9]"
  2957. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2958. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2959. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2960. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2961. groups carry important information:
  2962. 0 the full match
  2963. 7 hours of duration
  2964. 9 minutes of duration")
  2965. (defconst org-stamp-time-of-day-regexp
  2966. (concat
  2967. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2968. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2969. "\\(--?"
  2970. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2971. "Regular expression to match a timestamp time or time range.
  2972. After a match, the following groups carry important information:
  2973. 0 the full match
  2974. 1 date plus weekday, for backreferencing to make sure both times on same day
  2975. 2 the first time, range or not
  2976. 4 the second time, if it is a range.")
  2977. (defconst org-startup-options
  2978. '(("fold" org-startup-folded t)
  2979. ("overview" org-startup-folded t)
  2980. ("nofold" org-startup-folded nil)
  2981. ("showall" org-startup-folded nil)
  2982. ("content" org-startup-folded content)
  2983. ("hidestars" org-hide-leading-stars t)
  2984. ("showstars" org-hide-leading-stars nil)
  2985. ("odd" org-odd-levels-only t)
  2986. ("oddeven" org-odd-levels-only nil)
  2987. ("align" org-startup-align-all-tables t)
  2988. ("noalign" org-startup-align-all-tables nil)
  2989. ("customtime" org-display-custom-times t)
  2990. ("logdone" org-log-done time)
  2991. ("lognotedone" org-log-done note)
  2992. ("nologdone" org-log-done nil)
  2993. ("lognoteclock-out" org-log-note-clock-out t)
  2994. ("nolognoteclock-out" org-log-note-clock-out nil)
  2995. ("logrepeat" org-log-repeat state)
  2996. ("lognoterepeat" org-log-repeat note)
  2997. ("nologrepeat" org-log-repeat nil)
  2998. ("fninline" org-footnote-define-inline t)
  2999. ("nofninline" org-footnote-define-inline nil)
  3000. ("fnlocal" org-footnote-section nil)
  3001. ("fnauto" org-footnote-auto-label t)
  3002. ("fnprompt" org-footnote-auto-label nil)
  3003. ("fnconfirm" org-footnote-auto-label confirm)
  3004. ("fnplain" org-footnote-auto-label plain)
  3005. ("constcgs" constants-unit-system cgs)
  3006. ("constSI" constants-unit-system SI)
  3007. ("noptag" org-tag-persistent-alist nil)
  3008. ("hideblocks" org-hide-block-startup t)
  3009. ("nohideblocks" org-hide-block-startup nil))
  3010. "Variable associated with STARTUP options for org-mode.
  3011. Each element is a list of three items: The startup options as written
  3012. in the #+STARTUP line, the corresponding variable, and the value to
  3013. set this variable to if the option is found. An optional forth element PUSH
  3014. means to push this value onto the list in the variable.")
  3015. (defun org-set-regexps-and-options ()
  3016. "Precompute regular expressions for current buffer."
  3017. (when (org-mode-p)
  3018. (org-set-local 'org-todo-kwd-alist nil)
  3019. (org-set-local 'org-todo-key-alist nil)
  3020. (org-set-local 'org-todo-key-trigger nil)
  3021. (org-set-local 'org-todo-keywords-1 nil)
  3022. (org-set-local 'org-done-keywords nil)
  3023. (org-set-local 'org-todo-heads nil)
  3024. (org-set-local 'org-todo-sets nil)
  3025. (org-set-local 'org-todo-log-states nil)
  3026. (org-set-local 'org-file-properties nil)
  3027. (org-set-local 'org-file-tags nil)
  3028. (let ((re (org-make-options-regexp
  3029. '("CATEGORY" "TODO" "COLUMNS"
  3030. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3031. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3032. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3033. (splitre "[ \t]+")
  3034. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3035. tail sep kws1 prio props ftags drawers
  3036. ext-setup-or-nil setup-contents (start 0))
  3037. (save-excursion
  3038. (save-restriction
  3039. (widen)
  3040. (goto-char (point-min))
  3041. (while (or (and ext-setup-or-nil
  3042. (string-match re ext-setup-or-nil start)
  3043. (setq start (match-end 0)))
  3044. (and (setq ext-setup-or-nil nil start 0)
  3045. (re-search-forward re nil t)))
  3046. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3047. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3048. (cond
  3049. ((equal key "CATEGORY")
  3050. (if (string-match "[ \t]+$" value)
  3051. (setq value (replace-match "" t t value)))
  3052. (setq cat value))
  3053. ((member key '("SEQ_TODO" "TODO"))
  3054. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3055. ((equal key "TYP_TODO")
  3056. (push (cons 'type (org-split-string value splitre)) kwds))
  3057. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3058. ;; general TODO-like setup
  3059. (push (cons (intern (downcase (match-string 1 key)))
  3060. (org-split-string value splitre)) kwds))
  3061. ((equal key "TAGS")
  3062. (setq tags (append tags (if tags '("\\n") nil)
  3063. (org-split-string value splitre))))
  3064. ((equal key "COLUMNS")
  3065. (org-set-local 'org-columns-default-format value))
  3066. ((equal key "LINK")
  3067. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3068. (push (cons (match-string 1 value)
  3069. (org-trim (match-string 2 value)))
  3070. links)))
  3071. ((equal key "PRIORITIES")
  3072. (setq prio (org-split-string value " +")))
  3073. ((equal key "PROPERTY")
  3074. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3075. (push (cons (match-string 1 value) (match-string 2 value))
  3076. props)))
  3077. ((equal key "FILETAGS")
  3078. (when (string-match "\\S-" value)
  3079. (setq ftags
  3080. (append
  3081. ftags
  3082. (apply 'append
  3083. (mapcar (lambda (x) (org-split-string x ":"))
  3084. (org-split-string value)))))))
  3085. ((equal key "DRAWERS")
  3086. (setq drawers (org-split-string value splitre)))
  3087. ((equal key "CONSTANTS")
  3088. (setq const (append const (org-split-string value splitre))))
  3089. ((equal key "STARTUP")
  3090. (let ((opts (org-split-string value splitre))
  3091. l var val)
  3092. (while (setq l (pop opts))
  3093. (when (setq l (assoc l org-startup-options))
  3094. (setq var (nth 1 l) val (nth 2 l))
  3095. (if (not (nth 3 l))
  3096. (set (make-local-variable var) val)
  3097. (if (not (listp (symbol-value var)))
  3098. (set (make-local-variable var) nil))
  3099. (set (make-local-variable var) (symbol-value var))
  3100. (add-to-list var val))))))
  3101. ((equal key "ARCHIVE")
  3102. (string-match " *$" value)
  3103. (setq arch (replace-match "" t t value))
  3104. (remove-text-properties 0 (length arch)
  3105. '(face t fontified t) arch))
  3106. ((equal key "SETUPFILE")
  3107. (setq setup-contents (org-file-contents
  3108. (expand-file-name
  3109. (org-remove-double-quotes value))
  3110. 'noerror))
  3111. (if (not ext-setup-or-nil)
  3112. (setq ext-setup-or-nil setup-contents start 0)
  3113. (setq ext-setup-or-nil
  3114. (concat (substring ext-setup-or-nil 0 start)
  3115. "\n" setup-contents "\n"
  3116. (substring ext-setup-or-nil start)))))
  3117. ))))
  3118. (when cat
  3119. (org-set-local 'org-category (intern cat))
  3120. (push (cons "CATEGORY" cat) props))
  3121. (when prio
  3122. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3123. (setq prio (mapcar 'string-to-char prio))
  3124. (org-set-local 'org-highest-priority (nth 0 prio))
  3125. (org-set-local 'org-lowest-priority (nth 1 prio))
  3126. (org-set-local 'org-default-priority (nth 2 prio)))
  3127. (and props (org-set-local 'org-file-properties (nreverse props)))
  3128. (and ftags (org-set-local 'org-file-tags
  3129. (mapcar 'org-add-prop-inherited ftags)))
  3130. (and drawers (org-set-local 'org-drawers drawers))
  3131. (and arch (org-set-local 'org-archive-location arch))
  3132. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3133. ;; Process the TODO keywords
  3134. (unless kwds
  3135. ;; Use the global values as if they had been given locally.
  3136. (setq kwds (default-value 'org-todo-keywords))
  3137. (if (stringp (car kwds))
  3138. (setq kwds (list (cons org-todo-interpretation
  3139. (default-value 'org-todo-keywords)))))
  3140. (setq kwds (reverse kwds)))
  3141. (setq kwds (nreverse kwds))
  3142. (let (inter kws kw)
  3143. (while (setq kws (pop kwds))
  3144. (let ((kws (or
  3145. (run-hook-with-args-until-success
  3146. 'org-todo-setup-filter-hook kws)
  3147. kws)))
  3148. (setq inter (pop kws) sep (member "|" kws)
  3149. kws0 (delete "|" (copy-sequence kws))
  3150. kwsa nil
  3151. kws1 (mapcar
  3152. (lambda (x)
  3153. ;; 1 2
  3154. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3155. (progn
  3156. (setq kw (match-string 1 x)
  3157. key (and (match-end 2) (match-string 2 x))
  3158. log (org-extract-log-state-settings x))
  3159. (push (cons kw (and key (string-to-char key))) kwsa)
  3160. (and log (push log org-todo-log-states))
  3161. kw)
  3162. (error "Invalid TODO keyword %s" x)))
  3163. kws0)
  3164. kwsa (if kwsa (append '((:startgroup))
  3165. (nreverse kwsa)
  3166. '((:endgroup))))
  3167. hw (car kws1)
  3168. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3169. tail (list inter hw (car dws) (org-last dws))))
  3170. (add-to-list 'org-todo-heads hw 'append)
  3171. (push kws1 org-todo-sets)
  3172. (setq org-done-keywords (append org-done-keywords dws nil))
  3173. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3174. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3175. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3176. (setq org-todo-sets (nreverse org-todo-sets)
  3177. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3178. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3179. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3180. ;; Process the constants
  3181. (when const
  3182. (let (e cst)
  3183. (while (setq e (pop const))
  3184. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3185. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3186. (setq org-table-formula-constants-local cst)))
  3187. ;; Process the tags.
  3188. (when tags
  3189. (let (e tgs)
  3190. (while (setq e (pop tags))
  3191. (cond
  3192. ((equal e "{") (push '(:startgroup) tgs))
  3193. ((equal e "}") (push '(:endgroup) tgs))
  3194. ((equal e "\\n") (push '(:newline) tgs))
  3195. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3196. (push (cons (match-string 1 e)
  3197. (string-to-char (match-string 2 e)))
  3198. tgs))
  3199. (t (push (list e) tgs))))
  3200. (org-set-local 'org-tag-alist nil)
  3201. (while (setq e (pop tgs))
  3202. (or (and (stringp (car e))
  3203. (assoc (car e) org-tag-alist))
  3204. (push e org-tag-alist)))))
  3205. ;; Compute the regular expressions and other local variables
  3206. (if (not org-done-keywords)
  3207. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3208. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3209. (length org-scheduled-string)
  3210. (length org-clock-string)
  3211. (length org-closed-string)))
  3212. org-drawer-regexp
  3213. (concat "^[ \t]*:\\("
  3214. (mapconcat 'regexp-quote org-drawers "\\|")
  3215. "\\):[ \t]*$")
  3216. org-not-done-keywords
  3217. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3218. org-todo-regexp
  3219. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3220. "\\|") "\\)\\>")
  3221. org-not-done-regexp
  3222. (concat "\\<\\("
  3223. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3224. "\\)\\>")
  3225. org-not-done-heading-regexp
  3226. (concat "^\\(\\*+\\)[ \t]+\\("
  3227. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3228. "\\)\\>")
  3229. org-todo-line-regexp
  3230. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3231. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3232. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3233. org-complex-heading-regexp
  3234. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3235. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3236. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3237. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3238. org-nl-done-regexp
  3239. (concat "\n\\*+[ \t]+"
  3240. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3241. "\\)" "\\>")
  3242. org-todo-line-tags-regexp
  3243. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3244. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3245. (org-re
  3246. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3247. org-looking-at-done-regexp
  3248. (concat "^" "\\(?:"
  3249. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3250. "\\>")
  3251. org-deadline-regexp (concat "\\<" org-deadline-string)
  3252. org-deadline-time-regexp
  3253. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3254. org-deadline-line-regexp
  3255. (concat "\\<\\(" org-deadline-string "\\).*")
  3256. org-scheduled-regexp
  3257. (concat "\\<" org-scheduled-string)
  3258. org-scheduled-time-regexp
  3259. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3260. org-closed-time-regexp
  3261. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3262. org-keyword-time-regexp
  3263. (concat "\\<\\(" org-scheduled-string
  3264. "\\|" org-deadline-string
  3265. "\\|" org-closed-string
  3266. "\\|" org-clock-string "\\)"
  3267. " *[[<]\\([^]>]+\\)[]>]")
  3268. org-keyword-time-not-clock-regexp
  3269. (concat "\\<\\(" org-scheduled-string
  3270. "\\|" org-deadline-string
  3271. "\\|" org-closed-string
  3272. "\\)"
  3273. " *[[<]\\([^]>]+\\)[]>]")
  3274. org-maybe-keyword-time-regexp
  3275. (concat "\\(\\<\\(" org-scheduled-string
  3276. "\\|" org-deadline-string
  3277. "\\|" org-closed-string
  3278. "\\|" org-clock-string "\\)\\)?"
  3279. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3280. org-planning-or-clock-line-re
  3281. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3282. "\\|" org-deadline-string
  3283. "\\|" org-closed-string "\\|" org-clock-string
  3284. "\\)\\>\\)")
  3285. )
  3286. (org-compute-latex-and-specials-regexp)
  3287. (org-set-font-lock-defaults))))
  3288. (defun org-file-contents (file &optional noerror)
  3289. "Return the contents of FILE, as a string."
  3290. (if (or (not file)
  3291. (not (file-readable-p file)))
  3292. (if noerror
  3293. (progn
  3294. (message "Cannot read file %s" file)
  3295. (ding) (sit-for 2)
  3296. "")
  3297. (error "Cannot read file %s" file))
  3298. (with-temp-buffer
  3299. (insert-file-contents file)
  3300. (buffer-string))))
  3301. (defun org-extract-log-state-settings (x)
  3302. "Extract the log state setting from a TODO keyword string.
  3303. This will extract info from a string like \"WAIT(w@/!)\"."
  3304. (let (kw key log1 log2)
  3305. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3306. (setq kw (match-string 1 x)
  3307. key (and (match-end 2) (match-string 2 x))
  3308. log1 (and (match-end 3) (match-string 3 x))
  3309. log2 (and (match-end 4) (match-string 4 x)))
  3310. (and (or log1 log2)
  3311. (list kw
  3312. (and log1 (if (equal log1 "!") 'time 'note))
  3313. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3314. (defun org-remove-keyword-keys (list)
  3315. "Remove a pair of parenthesis at the end of each string in LIST."
  3316. (mapcar (lambda (x)
  3317. (if (string-match "(.*)$" x)
  3318. (substring x 0 (match-beginning 0))
  3319. x))
  3320. list))
  3321. ;; FIXME: this could be done much better, using second characters etc.
  3322. (defun org-assign-fast-keys (alist)
  3323. "Assign fast keys to a keyword-key alist.
  3324. Respect keys that are already there."
  3325. (let (new e k c c1 c2 (char ?a))
  3326. (while (setq e (pop alist))
  3327. (cond
  3328. ((equal e '(:startgroup)) (push e new))
  3329. ((equal e '(:endgroup)) (push e new))
  3330. ((equal e '(:newline)) (push e new))
  3331. (t
  3332. (setq k (car e) c2 nil)
  3333. (if (cdr e)
  3334. (setq c (cdr e))
  3335. ;; automatically assign a character.
  3336. (setq c1 (string-to-char
  3337. (downcase (substring
  3338. k (if (= (string-to-char k) ?@) 1 0)))))
  3339. (if (or (rassoc c1 new) (rassoc c1 alist))
  3340. (while (or (rassoc char new) (rassoc char alist))
  3341. (setq char (1+ char)))
  3342. (setq c2 c1))
  3343. (setq c (or c2 char)))
  3344. (push (cons k c) new))))
  3345. (nreverse new)))
  3346. ;;; Some variables used in various places
  3347. (defvar org-window-configuration nil
  3348. "Used in various places to store a window configuration.")
  3349. (defvar org-finish-function nil
  3350. "Function to be called when `C-c C-c' is used.
  3351. This is for getting out of special buffers like remember.")
  3352. ;; FIXME: Occasionally check by commenting these, to make sure
  3353. ;; no other functions uses these, forgetting to let-bind them.
  3354. (defvar entry)
  3355. (defvar state)
  3356. (defvar last-state)
  3357. (defvar date)
  3358. (defvar description)
  3359. ;; Defined somewhere in this file, but used before definition.
  3360. (defvar org-html-entities)
  3361. (defvar org-struct-menu)
  3362. (defvar org-org-menu)
  3363. (defvar org-tbl-menu)
  3364. (defvar org-agenda-keymap)
  3365. ;;;; Define the Org-mode
  3366. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3367. (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."))
  3368. ;; We use a before-change function to check if a table might need
  3369. ;; an update.
  3370. (defvar org-table-may-need-update t
  3371. "Indicates that a table might need an update.
  3372. This variable is set by `org-before-change-function'.
  3373. `org-table-align' sets it back to nil.")
  3374. (defun org-before-change-function (beg end)
  3375. "Every change indicates that a table might need an update."
  3376. (setq org-table-may-need-update t))
  3377. (defvar org-mode-map)
  3378. (defvar org-mode-hook nil
  3379. "Mode hook for Org-mode, run after the mode was turned on.")
  3380. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3381. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3382. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3383. (defvar org-table-buffer-is-an nil)
  3384. (defconst org-outline-regexp "\\*+ ")
  3385. ;;;###autoload
  3386. (define-derived-mode org-mode outline-mode "Org"
  3387. "Outline-based notes management and organizer, alias
  3388. \"Carsten's outline-mode for keeping track of everything.\"
  3389. Org-mode develops organizational tasks around a NOTES file which
  3390. contains information about projects as plain text. Org-mode is
  3391. implemented on top of outline-mode, which is ideal to keep the content
  3392. of large files well structured. It supports ToDo items, deadlines and
  3393. time stamps, which magically appear in the diary listing of the Emacs
  3394. calendar. Tables are easily created with a built-in table editor.
  3395. Plain text URL-like links connect to websites, emails (VM), Usenet
  3396. messages (Gnus), BBDB entries, and any files related to the project.
  3397. For printing and sharing of notes, an Org-mode file (or a part of it)
  3398. can be exported as a structured ASCII or HTML file.
  3399. The following commands are available:
  3400. \\{org-mode-map}"
  3401. ;; Get rid of Outline menus, they are not needed
  3402. ;; Need to do this here because define-derived-mode sets up
  3403. ;; the keymap so late. Still, it is a waste to call this each time
  3404. ;; we switch another buffer into org-mode.
  3405. (if (featurep 'xemacs)
  3406. (when (boundp 'outline-mode-menu-heading)
  3407. ;; Assume this is Greg's port, it used easymenu
  3408. (easy-menu-remove outline-mode-menu-heading)
  3409. (easy-menu-remove outline-mode-menu-show)
  3410. (easy-menu-remove outline-mode-menu-hide))
  3411. (define-key org-mode-map [menu-bar headings] 'undefined)
  3412. (define-key org-mode-map [menu-bar hide] 'undefined)
  3413. (define-key org-mode-map [menu-bar show] 'undefined))
  3414. (org-load-modules-maybe)
  3415. (easy-menu-add org-org-menu)
  3416. (easy-menu-add org-tbl-menu)
  3417. (org-install-agenda-files-menu)
  3418. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3419. (org-add-to-invisibility-spec '(org-cwidth))
  3420. (org-add-to-invisibility-spec '(org-hide-block . t))
  3421. (when (featurep 'xemacs)
  3422. (org-set-local 'line-move-ignore-invisible t))
  3423. (org-set-local 'outline-regexp org-outline-regexp)
  3424. (org-set-local 'outline-level 'org-outline-level)
  3425. (when (and org-ellipsis
  3426. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3427. (fboundp 'make-glyph-code))
  3428. (unless org-display-table
  3429. (setq org-display-table (make-display-table)))
  3430. (set-display-table-slot
  3431. org-display-table 4
  3432. (vconcat (mapcar
  3433. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3434. org-ellipsis)))
  3435. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3436. (setq buffer-display-table org-display-table))
  3437. (org-set-regexps-and-options)
  3438. (when (and org-tag-faces (not org-tags-special-faces-re))
  3439. ;; tag faces set outside customize.... force initialization.
  3440. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3441. ;; Calc embedded
  3442. (org-set-local 'calc-embedded-open-mode "# ")
  3443. (modify-syntax-entry ?# "<")
  3444. (modify-syntax-entry ?@ "w")
  3445. (if org-startup-truncated (setq truncate-lines t))
  3446. (org-set-local 'font-lock-unfontify-region-function
  3447. 'org-unfontify-region)
  3448. ;; Activate before-change-function
  3449. (org-set-local 'org-table-may-need-update t)
  3450. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3451. 'local)
  3452. ;; Check for running clock before killing a buffer
  3453. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3454. ;; Paragraphs and auto-filling
  3455. (org-set-autofill-regexps)
  3456. (setq indent-line-function 'org-indent-line-function)
  3457. (org-update-radio-target-regexp)
  3458. ;; Make sure dependence stuff works reliably, even for users who set it
  3459. ;; too late :-(
  3460. (if org-enforce-todo-dependencies
  3461. (add-hook 'org-blocker-hook
  3462. 'org-block-todo-from-children-or-siblings-or-parent)
  3463. (remove-hook 'org-blocker-hook
  3464. 'org-block-todo-from-children-or-siblings-or-parent))
  3465. (if org-enforce-todo-checkbox-dependencies
  3466. (add-hook 'org-blocker-hook
  3467. 'org-block-todo-from-checkboxes)
  3468. (remove-hook 'org-blocker-hook
  3469. 'org-block-todo-from-checkboxes))
  3470. ;; Comment characters
  3471. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3472. (org-set-local 'comment-padding " ")
  3473. ;; Align options lines
  3474. (org-set-local
  3475. 'align-mode-rules-list
  3476. '((org-in-buffer-settings
  3477. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3478. (modes . '(org-mode)))))
  3479. ;; Imenu
  3480. (org-set-local 'imenu-create-index-function
  3481. 'org-imenu-get-tree)
  3482. ;; Make isearch reveal context
  3483. (if (or (featurep 'xemacs)
  3484. (not (boundp 'outline-isearch-open-invisible-function)))
  3485. ;; Emacs 21 and XEmacs make use of the hook
  3486. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3487. ;; Emacs 22 deals with this through a special variable
  3488. (org-set-local 'outline-isearch-open-invisible-function
  3489. (lambda (&rest ignore) (org-show-context 'isearch))))
  3490. ;; If empty file that did not turn on org-mode automatically, make it to.
  3491. (if (and org-insert-mode-line-in-empty-file
  3492. (interactive-p)
  3493. (= (point-min) (point-max)))
  3494. (insert "# -*- mode: org -*-\n\n"))
  3495. (unless org-inhibit-startup
  3496. (when org-startup-align-all-tables
  3497. (let ((bmp (buffer-modified-p)))
  3498. (org-table-map-tables 'org-table-align)
  3499. (set-buffer-modified-p bmp)))
  3500. (org-set-startup-visibility)))
  3501. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3502. (defun org-current-time ()
  3503. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3504. (if (> (car org-time-stamp-rounding-minutes) 1)
  3505. (let ((r (car org-time-stamp-rounding-minutes))
  3506. (time (decode-time)))
  3507. (apply 'encode-time
  3508. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3509. (nthcdr 2 time))))
  3510. (current-time)))
  3511. ;;;; Font-Lock stuff, including the activators
  3512. (defvar org-mouse-map (make-sparse-keymap))
  3513. (org-defkey org-mouse-map
  3514. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3515. (org-defkey org-mouse-map
  3516. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3517. (when org-mouse-1-follows-link
  3518. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3519. (when org-tab-follows-link
  3520. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3521. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3522. (require 'font-lock)
  3523. (defconst org-non-link-chars "]\t\n\r<>")
  3524. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3525. "shell" "elisp"))
  3526. (defvar org-link-types-re nil
  3527. "Matches a link that has a url-like prefix like \"http:\"")
  3528. (defvar org-link-re-with-space nil
  3529. "Matches a link with spaces, optional angular brackets around it.")
  3530. (defvar org-link-re-with-space2 nil
  3531. "Matches a link with spaces, optional angular brackets around it.")
  3532. (defvar org-link-re-with-space3 nil
  3533. "Matches a link with spaces, only for internal part in bracket links.")
  3534. (defvar org-angle-link-re nil
  3535. "Matches link with angular brackets, spaces are allowed.")
  3536. (defvar org-plain-link-re nil
  3537. "Matches plain link, without spaces.")
  3538. (defvar org-bracket-link-regexp nil
  3539. "Matches a link in double brackets.")
  3540. (defvar org-bracket-link-analytic-regexp nil
  3541. "Regular expression used to analyze links.
  3542. Here is what the match groups contain after a match:
  3543. 1: http:
  3544. 2: http
  3545. 3: path
  3546. 4: [desc]
  3547. 5: desc")
  3548. (defvar org-bracket-link-analytic-regexp++ nil
  3549. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3550. (defvar org-any-link-re nil
  3551. "Regular expression matching any link.")
  3552. (defun org-make-link-regexps ()
  3553. "Update the link regular expressions.
  3554. This should be called after the variable `org-link-types' has changed."
  3555. (setq org-link-types-re
  3556. (concat
  3557. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3558. org-link-re-with-space
  3559. (concat
  3560. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3561. "\\([^" org-non-link-chars " ]"
  3562. "[^" org-non-link-chars "]*"
  3563. "[^" org-non-link-chars " ]\\)>?")
  3564. org-link-re-with-space2
  3565. (concat
  3566. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3567. "\\([^" org-non-link-chars " ]"
  3568. "[^\t\n\r]*"
  3569. "[^" org-non-link-chars " ]\\)>?")
  3570. org-link-re-with-space3
  3571. (concat
  3572. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3573. "\\([^" org-non-link-chars " ]"
  3574. "[^\t\n\r]*\\)")
  3575. org-angle-link-re
  3576. (concat
  3577. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3578. "\\([^" org-non-link-chars " ]"
  3579. "[^" org-non-link-chars "]*"
  3580. "\\)>")
  3581. org-plain-link-re
  3582. (concat
  3583. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3584. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3585. org-bracket-link-regexp
  3586. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3587. org-bracket-link-analytic-regexp
  3588. (concat
  3589. "\\[\\["
  3590. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3591. "\\([^]]+\\)"
  3592. "\\]"
  3593. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3594. "\\]")
  3595. org-bracket-link-analytic-regexp++
  3596. (concat
  3597. "\\[\\["
  3598. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3599. "\\([^]]+\\)"
  3600. "\\]"
  3601. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3602. "\\]")
  3603. org-any-link-re
  3604. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3605. org-angle-link-re "\\)\\|\\("
  3606. org-plain-link-re "\\)")))
  3607. (org-make-link-regexps)
  3608. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3609. "Regular expression for fast time stamp matching.")
  3610. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3611. "Regular expression for fast time stamp matching.")
  3612. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3613. "Regular expression matching time strings for analysis.
  3614. This one does not require the space after the date, so it can be used
  3615. on a string that terminates immediately after the date.")
  3616. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3617. "Regular expression matching time strings for analysis.")
  3618. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3619. "Regular expression matching time stamps, with groups.")
  3620. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3621. "Regular expression matching time stamps (also [..]), with groups.")
  3622. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3623. "Regular expression matching a time stamp range.")
  3624. (defconst org-tr-regexp-both
  3625. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3626. "Regular expression matching a time stamp range.")
  3627. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3628. org-ts-regexp "\\)?")
  3629. "Regular expression matching a time stamp or time stamp range.")
  3630. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3631. org-ts-regexp-both "\\)?")
  3632. "Regular expression matching a time stamp or time stamp range.
  3633. The time stamps may be either active or inactive.")
  3634. (defvar org-emph-face nil)
  3635. (defun org-do-emphasis-faces (limit)
  3636. "Run through the buffer and add overlays to links."
  3637. (let (rtn)
  3638. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3639. (if (not (= (char-after (match-beginning 3))
  3640. (char-after (match-beginning 4))))
  3641. (progn
  3642. (setq rtn t)
  3643. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3644. 'face
  3645. (nth 1 (assoc (match-string 3)
  3646. org-emphasis-alist)))
  3647. (add-text-properties (match-beginning 2) (match-end 2)
  3648. '(font-lock-multiline t))
  3649. (when org-hide-emphasis-markers
  3650. (add-text-properties (match-end 4) (match-beginning 5)
  3651. '(invisible org-link))
  3652. (add-text-properties (match-beginning 3) (match-end 3)
  3653. '(invisible org-link)))))
  3654. (backward-char 1))
  3655. rtn))
  3656. (defun org-emphasize (&optional char)
  3657. "Insert or change an emphasis, i.e. a font like bold or italic.
  3658. If there is an active region, change that region to a new emphasis.
  3659. If there is no region, just insert the marker characters and position
  3660. the cursor between them.
  3661. CHAR should be either the marker character, or the first character of the
  3662. HTML tag associated with that emphasis. If CHAR is a space, the means
  3663. to remove the emphasis of the selected region.
  3664. If char is not given (for example in an interactive call) it
  3665. will be prompted for."
  3666. (interactive)
  3667. (let ((eal org-emphasis-alist) e det
  3668. (erc org-emphasis-regexp-components)
  3669. (prompt "")
  3670. (string "") beg end move tag c s)
  3671. (if (org-region-active-p)
  3672. (setq beg (region-beginning) end (region-end)
  3673. string (buffer-substring beg end))
  3674. (setq move t))
  3675. (while (setq e (pop eal))
  3676. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3677. c (aref tag 0))
  3678. (push (cons c (string-to-char (car e))) det)
  3679. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3680. (substring tag 1)))))
  3681. (setq det (nreverse det))
  3682. (unless char
  3683. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3684. (setq char (read-char-exclusive)))
  3685. (setq char (or (cdr (assoc char det)) char))
  3686. (if (equal char ?\ )
  3687. (setq s "" move nil)
  3688. (unless (assoc (char-to-string char) org-emphasis-alist)
  3689. (error "No such emphasis marker: \"%c\"" char))
  3690. (setq s (char-to-string char)))
  3691. (while (and (> (length string) 1)
  3692. (equal (substring string 0 1) (substring string -1))
  3693. (assoc (substring string 0 1) org-emphasis-alist))
  3694. (setq string (substring string 1 -1)))
  3695. (setq string (concat s string s))
  3696. (if beg (delete-region beg end))
  3697. (unless (or (bolp)
  3698. (string-match (concat "[" (nth 0 erc) "\n]")
  3699. (char-to-string (char-before (point)))))
  3700. (insert " "))
  3701. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3702. (char-to-string (char-after (point))))
  3703. (insert " ") (backward-char 1))
  3704. (insert string)
  3705. (and move (backward-char 1))))
  3706. (defconst org-nonsticky-props
  3707. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3708. (defsubst org-rear-nonsticky-at (pos)
  3709. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3710. (defun org-activate-plain-links (limit)
  3711. "Run through the buffer and add overlays to links."
  3712. (catch 'exit
  3713. (let (f)
  3714. (while (re-search-forward org-plain-link-re limit t)
  3715. (setq f (get-text-property (match-beginning 0) 'face))
  3716. (if (or (eq f 'org-tag)
  3717. (and (listp f) (memq 'org-tag f)))
  3718. nil
  3719. (add-text-properties (match-beginning 0) (match-end 0)
  3720. (list 'mouse-face 'highlight
  3721. 'keymap org-mouse-map))
  3722. (org-rear-nonsticky-at (match-end 0))
  3723. (throw 'exit t))))))
  3724. (defun org-activate-code (limit)
  3725. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3726. (progn
  3727. (remove-text-properties (match-beginning 0) (match-end 0)
  3728. '(display t invisible t intangible t))
  3729. t)))
  3730. (defvar org-protecting-blocks
  3731. '("src" "example" "latex" "ascii" "html" "docbook")
  3732. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3733. This is needed for font-lock setup.")
  3734. (defun org-fontify-meta-lines-and-blocks (limit)
  3735. "Fontify #+ lines and blocks, in the correct ways."
  3736. (let ((case-fold-search t))
  3737. (if (re-search-forward
  3738. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3739. limit t)
  3740. (let ((beg (match-beginning 0))
  3741. (beg1 (line-beginning-position 2))
  3742. (dc1 (downcase (match-string 2)))
  3743. (dc3 (downcase (match-string 3)))
  3744. end end1 quoting)
  3745. (cond
  3746. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3747. ;; a single line of backend-specific content
  3748. (remove-text-properties (match-beginning 0) (match-end 0)
  3749. '(display t invisible t intangible t))
  3750. (add-text-properties (match-beginning 1) (match-end 3)
  3751. '(font-lock-fontified t face org-meta-line))
  3752. (add-text-properties (match-beginning 6) (match-end 6)
  3753. '(font-lock-fontified t face org-block))
  3754. t)
  3755. ((and (match-end 4) (equal dc3 "begin"))
  3756. ;; Truely a block
  3757. (setq quoting (member (downcase (match-string 5))
  3758. org-protecting-blocks))
  3759. (when (re-search-forward
  3760. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3761. nil t) ;; on purpose, we look further than LIMIT
  3762. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3763. (when quoting
  3764. (remove-text-properties beg end
  3765. '(display t invisible t intangible t)))
  3766. (add-text-properties
  3767. beg end
  3768. '(font-lock-fontified t font-lock-multiline t))
  3769. (add-text-properties beg beg1 '(face org-meta-line))
  3770. (add-text-properties end1 end '(face org-meta-line))
  3771. (when quoting
  3772. (add-text-properties beg1 end1 '(face org-block)))
  3773. t))
  3774. ((not (member (char-after beg) '(?\ ?\t)))
  3775. ;; just any other in-buffer setting, but not indented
  3776. (add-text-properties
  3777. beg (match-end 0)
  3778. '(font-lock-fontified t face org-meta-line))
  3779. t)
  3780. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3781. (and (match-end 4) (equal dc3 "attr")))
  3782. (add-text-properties
  3783. beg (match-end 0)
  3784. '(font-lock-fontified t face org-meta-line))
  3785. t)
  3786. (t nil))))))
  3787. (defun org-activate-angle-links (limit)
  3788. "Run through the buffer and add overlays to links."
  3789. (if (re-search-forward org-angle-link-re limit t)
  3790. (progn
  3791. (add-text-properties (match-beginning 0) (match-end 0)
  3792. (list 'mouse-face 'highlight
  3793. 'keymap org-mouse-map))
  3794. (org-rear-nonsticky-at (match-end 0))
  3795. t)))
  3796. (defun org-activate-footnote-links (limit)
  3797. "Run through the buffer and add overlays to links."
  3798. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3799. limit t)
  3800. (progn
  3801. (add-text-properties (match-beginning 2) (match-end 2)
  3802. (list 'mouse-face 'highlight
  3803. 'keymap org-mouse-map
  3804. 'help-echo
  3805. (if (= (point-at-bol) (match-beginning 2))
  3806. "Footnote definition"
  3807. "Footnote reference")
  3808. ))
  3809. (org-rear-nonsticky-at (match-end 2))
  3810. t)))
  3811. (defun org-activate-bracket-links (limit)
  3812. "Run through the buffer and add overlays to bracketed links."
  3813. (if (re-search-forward org-bracket-link-regexp limit t)
  3814. (let* ((help (concat "LINK: "
  3815. (org-match-string-no-properties 1)))
  3816. ;; FIXME: above we should remove the escapes.
  3817. ;; but that requires another match, protecting match data,
  3818. ;; a lot of overhead for font-lock.
  3819. (ip (org-maybe-intangible
  3820. (list 'invisible 'org-link
  3821. 'keymap org-mouse-map 'mouse-face 'highlight
  3822. 'font-lock-multiline t 'help-echo help)))
  3823. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3824. 'font-lock-multiline t 'help-echo help)))
  3825. ;; We need to remove the invisible property here. Table narrowing
  3826. ;; may have made some of this invisible.
  3827. (remove-text-properties (match-beginning 0) (match-end 0)
  3828. '(invisible nil))
  3829. (if (match-end 3)
  3830. (progn
  3831. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3832. (org-rear-nonsticky-at (match-beginning 3))
  3833. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3834. (org-rear-nonsticky-at (match-end 3))
  3835. (add-text-properties (match-end 3) (match-end 0) ip)
  3836. (org-rear-nonsticky-at (match-end 0)))
  3837. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3838. (org-rear-nonsticky-at (match-beginning 1))
  3839. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3840. (org-rear-nonsticky-at (match-end 1))
  3841. (add-text-properties (match-end 1) (match-end 0) ip)
  3842. (org-rear-nonsticky-at (match-end 0)))
  3843. t)))
  3844. (defun org-activate-dates (limit)
  3845. "Run through the buffer and add overlays to dates."
  3846. (if (re-search-forward org-tsr-regexp-both limit t)
  3847. (progn
  3848. (add-text-properties (match-beginning 0) (match-end 0)
  3849. (list 'mouse-face 'highlight
  3850. 'keymap org-mouse-map))
  3851. (org-rear-nonsticky-at (match-end 0))
  3852. (when org-display-custom-times
  3853. (if (match-end 3)
  3854. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3855. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3856. t)))
  3857. (defvar org-target-link-regexp nil
  3858. "Regular expression matching radio targets in plain text.")
  3859. (make-variable-buffer-local 'org-target-link-regexp)
  3860. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3861. "Regular expression matching a link target.")
  3862. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3863. "Regular expression matching a radio target.")
  3864. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3865. "Regular expression matching any target.")
  3866. (defun org-activate-target-links (limit)
  3867. "Run through the buffer and add overlays to target matches."
  3868. (when org-target-link-regexp
  3869. (let ((case-fold-search t))
  3870. (if (re-search-forward org-target-link-regexp limit t)
  3871. (progn
  3872. (add-text-properties (match-beginning 0) (match-end 0)
  3873. (list 'mouse-face 'highlight
  3874. 'keymap org-mouse-map
  3875. 'help-echo "Radio target link"
  3876. 'org-linked-text t))
  3877. (org-rear-nonsticky-at (match-end 0))
  3878. t)))))
  3879. (defun org-update-radio-target-regexp ()
  3880. "Find all radio targets in this file and update the regular expression."
  3881. (interactive)
  3882. (when (memq 'radio org-activate-links)
  3883. (setq org-target-link-regexp
  3884. (org-make-target-link-regexp (org-all-targets 'radio)))
  3885. (org-restart-font-lock)))
  3886. (defun org-hide-wide-columns (limit)
  3887. (let (s e)
  3888. (setq s (text-property-any (point) (or limit (point-max))
  3889. 'org-cwidth t))
  3890. (when s
  3891. (setq e (next-single-property-change s 'org-cwidth))
  3892. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3893. (goto-char e)
  3894. t)))
  3895. (defvar org-latex-and-specials-regexp nil
  3896. "Regular expression for highlighting export special stuff.")
  3897. (defvar org-match-substring-regexp)
  3898. (defvar org-match-substring-with-braces-regexp)
  3899. (defvar org-export-html-special-string-regexps)
  3900. (defun org-compute-latex-and-specials-regexp ()
  3901. "Compute regular expression for stuff treated specially by exporters."
  3902. (if (not org-highlight-latex-fragments-and-specials)
  3903. (org-set-local 'org-latex-and-specials-regexp nil)
  3904. (require 'org-exp)
  3905. (let*
  3906. ((matchers (plist-get org-format-latex-options :matchers))
  3907. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3908. org-latex-regexps)))
  3909. (options (org-combine-plists (org-default-export-plist)
  3910. (org-infile-export-plist)))
  3911. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3912. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3913. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3914. (org-export-html-expand (plist-get options :expand-quoted-html))
  3915. (org-export-with-special-strings (plist-get options :special-strings))
  3916. (re-sub
  3917. (cond
  3918. ((equal org-export-with-sub-superscripts '{})
  3919. (list org-match-substring-with-braces-regexp))
  3920. (org-export-with-sub-superscripts
  3921. (list org-match-substring-regexp))
  3922. (t nil)))
  3923. (re-latex
  3924. (if org-export-with-LaTeX-fragments
  3925. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3926. (re-macros
  3927. (if org-export-with-TeX-macros
  3928. (list (concat "\\\\"
  3929. (regexp-opt
  3930. (append (mapcar 'car org-html-entities)
  3931. (if (boundp 'org-latex-entities)
  3932. (mapcar (lambda (x)
  3933. (or (car-safe x) x))
  3934. org-latex-entities)
  3935. nil))
  3936. 'words))) ; FIXME
  3937. ))
  3938. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3939. (re-special (if org-export-with-special-strings
  3940. (mapcar (lambda (x) (car x))
  3941. org-export-html-special-string-regexps)))
  3942. (re-rest
  3943. (delq nil
  3944. (list
  3945. (if org-export-html-expand "@<[^>\n]+>")
  3946. ))))
  3947. (org-set-local
  3948. 'org-latex-and-specials-regexp
  3949. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3950. re-rest) "\\|")))))
  3951. (defun org-do-latex-and-special-faces (limit)
  3952. "Run through the buffer and add overlays to links."
  3953. (when org-latex-and-specials-regexp
  3954. (let (rtn d)
  3955. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3956. limit t))
  3957. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3958. 'face))
  3959. '(org-code org-verbatim underline)))
  3960. (progn
  3961. (setq rtn t
  3962. d (cond ((member (char-after (1+ (match-beginning 0)))
  3963. '(?_ ?^)) 1)
  3964. (t 0)))
  3965. (font-lock-prepend-text-property
  3966. (+ d (match-beginning 0)) (match-end 0)
  3967. 'face 'org-latex-and-export-specials)
  3968. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3969. '(font-lock-multiline t)))))
  3970. rtn)))
  3971. (defun org-restart-font-lock ()
  3972. "Restart font-lock-mode, to force refontification."
  3973. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3974. (font-lock-mode -1)
  3975. (font-lock-mode 1)))
  3976. (defun org-all-targets (&optional radio)
  3977. "Return a list of all targets in this file.
  3978. With optional argument RADIO, only find radio targets."
  3979. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3980. rtn)
  3981. (save-excursion
  3982. (goto-char (point-min))
  3983. (while (re-search-forward re nil t)
  3984. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3985. rtn)))
  3986. (defun org-make-target-link-regexp (targets)
  3987. "Make regular expression matching all strings in TARGETS.
  3988. The regular expression finds the targets also if there is a line break
  3989. between words."
  3990. (and targets
  3991. (concat
  3992. "\\<\\("
  3993. (mapconcat
  3994. (lambda (x)
  3995. (while (string-match " +" x)
  3996. (setq x (replace-match "\\s-+" t t x)))
  3997. x)
  3998. targets
  3999. "\\|")
  4000. "\\)\\>")))
  4001. (defun org-activate-tags (limit)
  4002. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4003. (progn
  4004. (add-text-properties (match-beginning 1) (match-end 1)
  4005. (list 'mouse-face 'highlight
  4006. 'keymap org-mouse-map))
  4007. (org-rear-nonsticky-at (match-end 1))
  4008. t)))
  4009. (defun org-outline-level ()
  4010. (save-excursion
  4011. (looking-at outline-regexp)
  4012. (if (match-beginning 1)
  4013. (+ (org-get-string-indentation (match-string 1)) 1000)
  4014. (1- (- (match-end 0) (match-beginning 0))))))
  4015. (defvar org-font-lock-keywords nil)
  4016. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4017. "Regular expression matching a property line.")
  4018. (defvar org-font-lock-hook nil
  4019. "Functions to be called for special font lock stuff.")
  4020. (defun org-font-lock-hook (limit)
  4021. (run-hook-with-args 'org-font-lock-hook limit))
  4022. (defun org-set-font-lock-defaults ()
  4023. (let* ((em org-fontify-emphasized-text)
  4024. (lk org-activate-links)
  4025. (org-font-lock-extra-keywords
  4026. (list
  4027. ;; Call the hook
  4028. '(org-font-lock-hook)
  4029. ;; Headlines
  4030. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4031. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4032. ;; Table lines
  4033. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4034. (1 'org-table t))
  4035. ;; Table internals
  4036. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4037. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4038. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4039. ;; Drawers
  4040. (list org-drawer-regexp '(0 'org-special-keyword t))
  4041. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4042. ;; Properties
  4043. (list org-property-re
  4044. '(1 'org-special-keyword t)
  4045. '(3 'org-property-value t))
  4046. (if org-format-transports-properties-p
  4047. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4048. ;; Links
  4049. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4050. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4051. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4052. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4053. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4054. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4055. (if (memq 'footnote lk) '(org-activate-footnote-links
  4056. (2 'org-footnote t)))
  4057. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4058. '(org-hide-wide-columns (0 nil append))
  4059. ;; TODO lines
  4060. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4061. '(1 (org-get-todo-face 1) t))
  4062. ;; DONE
  4063. (if org-fontify-done-headline
  4064. (list (concat "^[*]+ +\\<\\("
  4065. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4066. "\\)\\(.*\\)")
  4067. '(2 'org-headline-done t))
  4068. nil)
  4069. ;; Priorities
  4070. '(org-font-lock-add-priority-faces)
  4071. ;; Tags
  4072. '(org-font-lock-add-tag-faces)
  4073. ;; Special keywords
  4074. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4075. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4076. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4077. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4078. ;; Emphasis
  4079. (if em
  4080. (if (featurep 'xemacs)
  4081. '(org-do-emphasis-faces (0 nil append))
  4082. '(org-do-emphasis-faces)))
  4083. ;; Checkboxes
  4084. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4085. 2 'org-checkbox prepend)
  4086. (if org-provide-checkbox-statistics
  4087. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4088. (0 (org-get-checkbox-statistics-face) t)))
  4089. ;; Description list items
  4090. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4091. 2 'bold prepend)
  4092. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4093. '(1 'org-archived prepend))
  4094. ;; Specials
  4095. '(org-do-latex-and-special-faces)
  4096. ;; Code
  4097. '(org-activate-code (1 'org-code t))
  4098. ;; COMMENT
  4099. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4100. "\\|" org-quote-string "\\)\\>")
  4101. '(1 'org-special-keyword t))
  4102. '("^#.*" (0 'font-lock-comment-face t))
  4103. ;; Blocks and meta lines
  4104. '(org-fontify-meta-lines-and-blocks)
  4105. )))
  4106. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4107. ;; Now set the full font-lock-keywords
  4108. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4109. (org-set-local 'font-lock-defaults
  4110. '(org-font-lock-keywords t nil nil backward-paragraph))
  4111. (kill-local-variable 'font-lock-keywords) nil))
  4112. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4113. "Fontify string S like in Org-mode"
  4114. (with-temp-buffer
  4115. (insert s)
  4116. (let ((org-odd-levels-only odd-levels))
  4117. (org-mode)
  4118. (font-lock-fontify-buffer)
  4119. (buffer-string))))
  4120. (defvar org-m nil)
  4121. (defvar org-l nil)
  4122. (defvar org-f nil)
  4123. (defun org-get-level-face (n)
  4124. "Get the right face for match N in font-lock matching of headlines."
  4125. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4126. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4127. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4128. (cond
  4129. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4130. ((eq n 2) org-f)
  4131. (t (if org-level-color-stars-only nil org-f))))
  4132. (defun org-get-todo-face (kwd)
  4133. "Get the right face for a TODO keyword KWD.
  4134. If KWD is a number, get the corresponding match group."
  4135. (if (numberp kwd) (setq kwd (match-string kwd)))
  4136. (or (cdr (assoc kwd org-todo-keyword-faces))
  4137. (and (member kwd org-done-keywords) 'org-done)
  4138. 'org-todo))
  4139. (defun org-font-lock-add-tag-faces (limit)
  4140. "Add the special tag faces."
  4141. (when (and org-tag-faces org-tags-special-faces-re)
  4142. (while (re-search-forward org-tags-special-faces-re limit t)
  4143. (add-text-properties (match-beginning 1) (match-end 1)
  4144. (list 'face (org-get-tag-face 1)
  4145. 'font-lock-fontified t))
  4146. (backward-char 1))))
  4147. (defun org-font-lock-add-priority-faces (limit)
  4148. "Add the special priority faces."
  4149. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4150. (add-text-properties
  4151. (match-beginning 0) (match-end 0)
  4152. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4153. org-priority-faces))
  4154. 'org-special-keyword)
  4155. 'font-lock-fontified t))))
  4156. (defun org-get-tag-face (kwd)
  4157. "Get the right face for a TODO keyword KWD.
  4158. If KWD is a number, get the corresponding match group."
  4159. (if (numberp kwd) (setq kwd (match-string kwd)))
  4160. (or (cdr (assoc kwd org-tag-faces))
  4161. 'org-tag))
  4162. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4163. "Remove fontification and activation overlays from links."
  4164. (font-lock-default-unfontify-region beg end)
  4165. (let* ((buffer-undo-list t)
  4166. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4167. (inhibit-modification-hooks t)
  4168. deactivate-mark buffer-file-name buffer-file-truename)
  4169. (remove-text-properties beg end
  4170. '(mouse-face t keymap t org-linked-text t
  4171. invisible t intangible t))))
  4172. ;;;; Visibility cycling, including org-goto and indirect buffer
  4173. ;;; Cycling
  4174. (defvar org-cycle-global-status nil)
  4175. (make-variable-buffer-local 'org-cycle-global-status)
  4176. (defvar org-cycle-subtree-status nil)
  4177. (make-variable-buffer-local 'org-cycle-subtree-status)
  4178. ;;;###autoload
  4179. (defvar org-inlinetask-min-level)
  4180. (defun org-cycle (&optional arg)
  4181. "TAB-action and visibility cycling for Org-mode.
  4182. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4183. is outine visibility cycling, but it also invokes other actions
  4184. in special contexts.
  4185. - When this function is called with a prefix argument, rotate the entire
  4186. buffer through 3 states (global cycling)
  4187. 1. OVERVIEW: Show only top-level headlines.
  4188. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4189. 3. SHOW ALL: Show everything.
  4190. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4191. determined by the variable `org-startup-folded', and by any VISIBILITY
  4192. properties in the buffer.
  4193. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4194. including any drawers.
  4195. - When inside a table, re-align the table and move to the next field.
  4196. - When point is at the beginning of a headline, rotate the subtree started
  4197. by this line through 3 different states (local cycling)
  4198. 1. FOLDED: Only the main headline is shown.
  4199. 2. CHILDREN: The main headline and the direct children are shown.
  4200. From this state, you can move to one of the children
  4201. and zoom in further.
  4202. 3. SUBTREE: Show the entire subtree, including body text.
  4203. - When there is a numeric prefix, go up to a heading with level ARG, do
  4204. a `show-subtree' and return to the previous cursor position. If ARG
  4205. is negative, go up that many levels.
  4206. - When point is not at the beginning of a headline, execute the global
  4207. binding for TAB, which is re-indenting the line. See the option
  4208. `org-cycle-emulate-tab' for details.
  4209. - Special case: if point is at the beginning of the buffer and there is
  4210. no headline in line 1, this function will act as if called with prefix arg.
  4211. But only if also the variable `org-cycle-global-at-bob' is t."
  4212. (interactive "P")
  4213. (org-load-modules-maybe)
  4214. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4215. (let* ((limit-level
  4216. (or org-cycle-max-level
  4217. (and (boundp 'org-inlinetask-min-level)
  4218. org-inlinetask-min-level
  4219. (1- org-inlinetask-min-level))))
  4220. (nstars (and limit-level
  4221. (if org-odd-levels-only
  4222. (and limit-level (1- (* limit-level 2)))
  4223. limit-level)))
  4224. (outline-regexp
  4225. (cond
  4226. ((not (org-mode-p)) outline-regexp)
  4227. (org-cycle-include-plain-lists
  4228. (concat "\\(?:\\*"
  4229. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4230. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4231. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4232. (bob-special (and org-cycle-global-at-bob (bobp)
  4233. (not (looking-at outline-regexp))))
  4234. (org-cycle-hook
  4235. (if bob-special
  4236. (delq 'org-optimize-window-after-visibility-change
  4237. (copy-sequence org-cycle-hook))
  4238. org-cycle-hook))
  4239. (pos (point)))
  4240. (if (or bob-special (equal arg '(4)))
  4241. ;; special case: use global cycling
  4242. (setq arg t))
  4243. (cond
  4244. ((equal arg '(16))
  4245. (org-set-startup-visibility)
  4246. (message "Startup visibility, plus VISIBILITY properties"))
  4247. ((equal arg '(64))
  4248. (show-all)
  4249. (message "Entire buffer visible, including drawers"))
  4250. ((org-at-table-p 'any)
  4251. ;; Enter the table or move to the next field in the table
  4252. (or (org-table-recognize-table.el)
  4253. (progn
  4254. (if arg (org-table-edit-field t)
  4255. (org-table-justify-field-maybe)
  4256. (call-interactively 'org-table-next-field)))))
  4257. ((run-hook-with-args-until-success
  4258. 'org-tab-after-check-for-table-hook))
  4259. ((eq arg t) ;; Global cycling
  4260. (org-cycle-internal-global))
  4261. ((and org-drawers org-drawer-regexp
  4262. (save-excursion
  4263. (beginning-of-line 1)
  4264. (looking-at org-drawer-regexp)))
  4265. ;; Toggle block visibility
  4266. (org-flag-drawer
  4267. (not (get-char-property (match-end 0) 'invisible))))
  4268. ((integerp arg)
  4269. ;; Show-subtree, ARG levels up from here.
  4270. (save-excursion
  4271. (org-back-to-heading)
  4272. (outline-up-heading (if (< arg 0) (- arg)
  4273. (- (funcall outline-level) arg)))
  4274. (org-show-subtree)))
  4275. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4276. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4277. (org-cycle-internal-local))
  4278. ;; TAB emulation and template completion
  4279. (buffer-read-only (org-back-to-heading))
  4280. ((run-hook-with-args-until-success
  4281. 'org-tab-after-check-for-cycling-hook))
  4282. ((org-try-structure-completion))
  4283. ((org-try-cdlatex-tab))
  4284. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4285. (or (not (bolp))
  4286. (not (looking-at outline-regexp))))
  4287. (call-interactively (global-key-binding "\t")))
  4288. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4289. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4290. (or (and (eq org-cycle-emulate-tab 'white)
  4291. (= (match-end 0) (point-at-eol)))
  4292. (and (eq org-cycle-emulate-tab 'whitestart)
  4293. (>= (match-end 0) pos))))
  4294. t
  4295. (eq org-cycle-emulate-tab t))
  4296. (call-interactively (global-key-binding "\t")))
  4297. (t (save-excursion
  4298. (org-back-to-heading)
  4299. (org-cycle)))))))
  4300. (defun org-cycle-internal-global ()
  4301. "Do the global cycling action."
  4302. (cond
  4303. ((and (eq last-command this-command)
  4304. (eq org-cycle-global-status 'overview))
  4305. ;; We just created the overview - now do table of contents
  4306. ;; This can be slow in very large buffers, so indicate action
  4307. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4308. (message "CONTENTS...")
  4309. (org-content)
  4310. (message "CONTENTS...done")
  4311. (setq org-cycle-global-status 'contents)
  4312. (run-hook-with-args 'org-cycle-hook 'contents))
  4313. ((and (eq last-command this-command)
  4314. (eq org-cycle-global-status 'contents))
  4315. ;; We just showed the table of contents - now show everything
  4316. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4317. (show-all)
  4318. (message "SHOW ALL")
  4319. (setq org-cycle-global-status 'all)
  4320. (run-hook-with-args 'org-cycle-hook 'all))
  4321. (t
  4322. ;; Default action: go to overview
  4323. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4324. (org-overview)
  4325. (message "OVERVIEW")
  4326. (setq org-cycle-global-status 'overview)
  4327. (run-hook-with-args 'org-cycle-hook 'overview))))
  4328. (defun org-cycle-internal-local ()
  4329. "Do the local cycling action."
  4330. (org-back-to-heading)
  4331. (let ((goal-column 0) eoh eol eos)
  4332. ;; First, some boundaries
  4333. (save-excursion
  4334. (org-back-to-heading)
  4335. (save-excursion
  4336. (beginning-of-line 2)
  4337. (while (and (not (eobp)) ;; this is like `next-line'
  4338. (get-char-property (1- (point)) 'invisible))
  4339. (beginning-of-line 2)) (setq eol (point)))
  4340. (outline-end-of-heading) (setq eoh (point))
  4341. (org-end-of-subtree t)
  4342. (unless (eobp)
  4343. (skip-chars-forward " \t\n")
  4344. (beginning-of-line 1) ; in case this is an item
  4345. )
  4346. (setq eos (1- (point))))
  4347. ;; Find out what to do next and set `this-command'
  4348. (cond
  4349. ((= eos eoh)
  4350. ;; Nothing is hidden behind this heading
  4351. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4352. (message "EMPTY ENTRY")
  4353. (setq org-cycle-subtree-status nil)
  4354. (save-excursion
  4355. (goto-char eos)
  4356. (outline-next-heading)
  4357. (if (org-invisible-p) (org-flag-heading nil))))
  4358. ((or (>= eol eos)
  4359. (not (string-match "\\S-" (buffer-substring eol eos))))
  4360. ;; Entire subtree is hidden in one line: open it
  4361. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4362. (org-show-entry)
  4363. (show-children)
  4364. (message "CHILDREN")
  4365. (save-excursion
  4366. (goto-char eos)
  4367. (outline-next-heading)
  4368. (if (org-invisible-p) (org-flag-heading nil)))
  4369. (setq org-cycle-subtree-status 'children)
  4370. (run-hook-with-args 'org-cycle-hook 'children))
  4371. ((and (eq last-command this-command)
  4372. (eq org-cycle-subtree-status 'children))
  4373. ;; We just showed the children, now show everything.
  4374. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4375. (org-show-subtree)
  4376. (message "SUBTREE")
  4377. (setq org-cycle-subtree-status 'subtree)
  4378. (run-hook-with-args 'org-cycle-hook 'subtree))
  4379. (t
  4380. ;; Default action: hide the subtree.
  4381. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4382. (hide-subtree)
  4383. (message "FOLDED")
  4384. (setq org-cycle-subtree-status 'folded)
  4385. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4386. ;;;###autoload
  4387. (defun org-global-cycle (&optional arg)
  4388. "Cycle the global visibility. For details see `org-cycle'.
  4389. With C-u prefix arg, switch to startup visibility.
  4390. With a numeric prefix, show all headlines up to that level."
  4391. (interactive "P")
  4392. (let ((org-cycle-include-plain-lists
  4393. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4394. (cond
  4395. ((integerp arg)
  4396. (show-all)
  4397. (hide-sublevels arg)
  4398. (setq org-cycle-global-status 'contents))
  4399. ((equal arg '(4))
  4400. (org-set-startup-visibility)
  4401. (message "Startup visibility, plus VISIBILITY properties."))
  4402. (t
  4403. (org-cycle '(4))))))
  4404. (defun org-set-startup-visibility ()
  4405. "Set the visibility required by startup options and properties."
  4406. (cond
  4407. ((eq org-startup-folded t)
  4408. (org-cycle '(4)))
  4409. ((eq org-startup-folded 'content)
  4410. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4411. (org-cycle '(4)) (org-cycle '(4)))))
  4412. (if org-hide-block-startup (org-hide-block-all))
  4413. (org-set-visibility-according-to-property 'no-cleanup)
  4414. (org-cycle-hide-archived-subtrees 'all)
  4415. (org-cycle-hide-drawers 'all)
  4416. (org-cycle-show-empty-lines 'all))
  4417. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4418. "Switch subtree visibilities according to :VISIBILITY: property."
  4419. (interactive)
  4420. (let (org-show-entry-below state)
  4421. (save-excursion
  4422. (goto-char (point-min))
  4423. (while (re-search-forward
  4424. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4425. nil t)
  4426. (setq state (match-string 1))
  4427. (save-excursion
  4428. (org-back-to-heading t)
  4429. (hide-subtree)
  4430. (org-reveal)
  4431. (cond
  4432. ((equal state '("fold" "folded"))
  4433. (hide-subtree))
  4434. ((equal state "children")
  4435. (org-show-hidden-entry)
  4436. (show-children))
  4437. ((equal state "content")
  4438. (save-excursion
  4439. (save-restriction
  4440. (org-narrow-to-subtree)
  4441. (org-content))))
  4442. ((member state '("all" "showall"))
  4443. (show-subtree)))))
  4444. (unless no-cleanup
  4445. (org-cycle-hide-archived-subtrees 'all)
  4446. (org-cycle-hide-drawers 'all)
  4447. (org-cycle-show-empty-lines 'all)))))
  4448. (defun org-overview ()
  4449. "Switch to overview mode, showing only top-level headlines.
  4450. Really, this shows all headlines with level equal or greater than the level
  4451. of the first headline in the buffer. This is important, because if the
  4452. first headline is not level one, then (hide-sublevels 1) gives confusing
  4453. results."
  4454. (interactive)
  4455. (let ((level (save-excursion
  4456. (goto-char (point-min))
  4457. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4458. (progn
  4459. (goto-char (match-beginning 0))
  4460. (funcall outline-level))))))
  4461. (and level (hide-sublevels level))))
  4462. (defun org-content (&optional arg)
  4463. "Show all headlines in the buffer, like a table of contents.
  4464. With numerical argument N, show content up to level N."
  4465. (interactive "P")
  4466. (save-excursion
  4467. ;; Visit all headings and show their offspring
  4468. (and (integerp arg) (org-overview))
  4469. (goto-char (point-max))
  4470. (catch 'exit
  4471. (while (and (progn (condition-case nil
  4472. (outline-previous-visible-heading 1)
  4473. (error (goto-char (point-min))))
  4474. t)
  4475. (looking-at outline-regexp))
  4476. (if (integerp arg)
  4477. (show-children (1- arg))
  4478. (show-branches))
  4479. (if (bobp) (throw 'exit nil))))))
  4480. (defun org-optimize-window-after-visibility-change (state)
  4481. "Adjust the window after a change in outline visibility.
  4482. This function is the default value of the hook `org-cycle-hook'."
  4483. (when (get-buffer-window (current-buffer))
  4484. (cond
  4485. ((eq state 'content) nil)
  4486. ((eq state 'all) nil)
  4487. ((eq state 'folded) nil)
  4488. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4489. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4490. (defun org-compact-display-after-subtree-move ()
  4491. "Show a compacter version of the tree of the entry's parent."
  4492. (save-excursion
  4493. (if (org-up-heading-safe)
  4494. (progn
  4495. (hide-subtree)
  4496. (show-entry)
  4497. (show-children)
  4498. (org-cycle-show-empty-lines 'children)
  4499. (org-cycle-hide-drawers 'children))
  4500. (org-overview))))
  4501. (defun org-cycle-show-empty-lines (state)
  4502. "Show empty lines above all visible headlines.
  4503. The region to be covered depends on STATE when called through
  4504. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4505. entire buffer covered. Note that an empty line is only shown if there
  4506. are at least `org-cycle-separator-lines' empty lines before the headline."
  4507. (when (> org-cycle-separator-lines 0)
  4508. (save-excursion
  4509. (let* ((n org-cycle-separator-lines)
  4510. (re (cond
  4511. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4512. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4513. (t (let ((ns (number-to-string (- n 2))))
  4514. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4515. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4516. beg end)
  4517. (cond
  4518. ((memq state '(overview contents t))
  4519. (setq beg (point-min) end (point-max)))
  4520. ((memq state '(children folded))
  4521. (setq beg (point) end (progn (org-end-of-subtree t t)
  4522. (beginning-of-line 2)
  4523. (point)))))
  4524. (when beg
  4525. (goto-char beg)
  4526. (while (re-search-forward re end t)
  4527. (if (not (get-char-property (match-end 1) 'invisible))
  4528. (outline-flag-region
  4529. (match-beginning 1) (match-end 1) nil)))))))
  4530. ;; Never hide empty lines at the end of the file.
  4531. (save-excursion
  4532. (goto-char (point-max))
  4533. (outline-previous-heading)
  4534. (outline-end-of-heading)
  4535. (if (and (looking-at "[ \t\n]+")
  4536. (= (match-end 0) (point-max)))
  4537. (outline-flag-region (point) (match-end 0) nil))))
  4538. (defun org-show-empty-lines-in-parent ()
  4539. "Move to the parent and re-show empty lines before visible headlines."
  4540. (save-excursion
  4541. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4542. (org-cycle-show-empty-lines context))))
  4543. (defun org-cycle-hide-drawers (state)
  4544. "Re-hide all drawers after a visibility state change."
  4545. (when (and (org-mode-p)
  4546. (not (memq state '(overview folded))))
  4547. (save-excursion
  4548. (let* ((globalp (memq state '(contents all)))
  4549. (beg (if globalp (point-min) (point)))
  4550. (end (if globalp (point-max) (org-end-of-subtree t))))
  4551. (goto-char beg)
  4552. (while (re-search-forward org-drawer-regexp end t)
  4553. (org-flag-drawer t))))))
  4554. (defun org-flag-drawer (flag)
  4555. (save-excursion
  4556. (beginning-of-line 1)
  4557. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4558. (let ((b (match-end 0))
  4559. (outline-regexp org-outline-regexp))
  4560. (if (re-search-forward
  4561. "^[ \t]*:END:"
  4562. (save-excursion (outline-next-heading) (point)) t)
  4563. (outline-flag-region b (point-at-eol) flag)
  4564. (error ":END: line missing"))))))
  4565. (defun org-subtree-end-visible-p ()
  4566. "Is the end of the current subtree visible?"
  4567. (pos-visible-in-window-p
  4568. (save-excursion (org-end-of-subtree t) (point))))
  4569. (defun org-first-headline-recenter (&optional N)
  4570. "Move cursor to the first headline and recenter the headline.
  4571. Optional argument N means, put the headline into the Nth line of the window."
  4572. (goto-char (point-min))
  4573. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4574. (beginning-of-line)
  4575. (recenter (prefix-numeric-value N))))
  4576. ;;; Folding of blocks
  4577. (defcustom org-hide-block-startup nil
  4578. "Non-nil means, , entering Org-mode will fold all blocks.
  4579. This can also be set in on a per-file basis with
  4580. #+STARTUP: hideblocks
  4581. #+STARTUP: showblocks"
  4582. :group 'org-startup
  4583. :type 'boolean)
  4584. (defconst org-block-regexp
  4585. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1"
  4586. "Regular expression for hiding blocks.")
  4587. (defvar org-hide-block-overlays nil
  4588. "Overays hiding blocks.")
  4589. (make-variable-buffer-local 'org-hide-block-overlays)
  4590. (defun org-block-map (function &optional start end)
  4591. "Call func at the head of all source blocks in the current
  4592. buffer. Optional arguments START and END can be used to limit
  4593. the range."
  4594. (let ((start (or start (point-min)))
  4595. (end (or end (point-max))))
  4596. (save-excursion
  4597. (goto-char start)
  4598. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4599. (save-excursion
  4600. (save-match-data
  4601. (goto-char (match-beginning 0))
  4602. (funcall function)))))))
  4603. (defun org-hide-block-toggle-all ()
  4604. "Toggle the visibility of all blocks in the current buffer."
  4605. (org-block-map #'org-hide-block-toggle))
  4606. (defun org-hide-block-all ()
  4607. "Fold all blocks in the current buffer."
  4608. (interactive)
  4609. (org-show-block-all)
  4610. (org-block-map #'org-hide-block-toggle-maybe))
  4611. (defun org-show-block-all ()
  4612. "Unfold all blocks in the current buffer."
  4613. (mapc 'org-delete-overlay org-hide-block-overlays)
  4614. (setq org-hide-block-overlays nil))
  4615. (defun org-hide-block-toggle-maybe ()
  4616. "Toggle visibility of block at point."
  4617. (interactive)
  4618. (let ((case-fold-search t))
  4619. (if (save-excursion
  4620. (beginning-of-line 1)
  4621. (looking-at org-block-regexp))
  4622. (progn (org-hide-block-toggle)
  4623. t) ;; to signal that we took action
  4624. nil))) ;; to signal that we did not
  4625. (defun org-hide-block-toggle (&optional force)
  4626. "Toggle the visibility of the current block."
  4627. (interactive)
  4628. (save-excursion
  4629. (beginning-of-line)
  4630. (if (re-search-forward org-block-regexp nil t)
  4631. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4632. (end (match-end 0))
  4633. ov) ;; end of entire body
  4634. (if (memq t (mapcar (lambda (overlay)
  4635. (eq (org-overlay-get overlay 'invisible)
  4636. 'org-hide-block))
  4637. (org-overlays-at start)))
  4638. (if (or (not force) (eq force 'off))
  4639. (mapc (lambda (ov)
  4640. (when (member ov org-hide-block-overlays)
  4641. (setq org-hide-block-overlays
  4642. (delq ov org-hide-block-overlays)))
  4643. (when (eq (org-overlay-get ov 'invisible)
  4644. 'org-hide-block)
  4645. (org-delete-overlay ov)))
  4646. (org-overlays-at start)))
  4647. (setq ov (make-overlay start end))
  4648. (org-overlay-put ov 'invisible 'org-hide-block)
  4649. (push ov org-hide-block-overlays)))
  4650. (error "Not looking at a source block"))))
  4651. ;; org-tab-after-check-for-cycling-hook
  4652. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4653. ;; Remove overlays when changing major mode
  4654. (add-hook 'org-mode-hook
  4655. (lambda () (org-add-hook 'change-major-mode-hook
  4656. 'org-show-block-all 'append 'local)))
  4657. ;;; Org-goto
  4658. (defvar org-goto-window-configuration nil)
  4659. (defvar org-goto-marker nil)
  4660. (defvar org-goto-map
  4661. (let ((map (make-sparse-keymap)))
  4662. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4663. (while (setq cmd (pop cmds))
  4664. (substitute-key-definition cmd cmd map global-map)))
  4665. (suppress-keymap map)
  4666. (org-defkey map "\C-m" 'org-goto-ret)
  4667. (org-defkey map [(return)] 'org-goto-ret)
  4668. (org-defkey map [(left)] 'org-goto-left)
  4669. (org-defkey map [(right)] 'org-goto-right)
  4670. (org-defkey map [(control ?g)] 'org-goto-quit)
  4671. (org-defkey map "\C-i" 'org-cycle)
  4672. (org-defkey map [(tab)] 'org-cycle)
  4673. (org-defkey map [(down)] 'outline-next-visible-heading)
  4674. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4675. (if org-goto-auto-isearch
  4676. (if (fboundp 'define-key-after)
  4677. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4678. nil)
  4679. (org-defkey map "q" 'org-goto-quit)
  4680. (org-defkey map "n" 'outline-next-visible-heading)
  4681. (org-defkey map "p" 'outline-previous-visible-heading)
  4682. (org-defkey map "f" 'outline-forward-same-level)
  4683. (org-defkey map "b" 'outline-backward-same-level)
  4684. (org-defkey map "u" 'outline-up-heading))
  4685. (org-defkey map "/" 'org-occur)
  4686. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4687. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4688. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4689. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4690. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4691. map))
  4692. (defconst org-goto-help
  4693. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4694. RET=jump to location [Q]uit and return to previous location
  4695. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4696. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4697. ;; FIXME: Docstring doe not mention both interfaces
  4698. (defun org-goto (&optional alternative-interface)
  4699. "Look up a different location in the current file, keeping current visibility.
  4700. When you want look-up or go to a different location in a document, the
  4701. fastest way is often to fold the entire buffer and then dive into the tree.
  4702. This method has the disadvantage, that the previous location will be folded,
  4703. which may not be what you want.
  4704. This command works around this by showing a copy of the current buffer
  4705. in an indirect buffer, in overview mode. You can dive into the tree in
  4706. that copy, use org-occur and incremental search to find a location.
  4707. When pressing RET or `Q', the command returns to the original buffer in
  4708. which the visibility is still unchanged. After RET is will also jump to
  4709. the location selected in the indirect buffer and expose the
  4710. the headline hierarchy above."
  4711. (interactive "P")
  4712. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4713. (org-refile-use-outline-path t)
  4714. (org-refile-target-verify-function nil)
  4715. (interface
  4716. (if (not alternative-interface)
  4717. org-goto-interface
  4718. (if (eq org-goto-interface 'outline)
  4719. 'outline-path-completion
  4720. 'outline)))
  4721. (org-goto-start-pos (point))
  4722. (selected-point
  4723. (if (eq interface 'outline)
  4724. (car (org-get-location (current-buffer) org-goto-help))
  4725. (nth 3 (org-refile-get-location "Goto: ")))))
  4726. (if selected-point
  4727. (progn
  4728. (org-mark-ring-push org-goto-start-pos)
  4729. (goto-char selected-point)
  4730. (if (or (org-invisible-p) (org-invisible-p2))
  4731. (org-show-context 'org-goto)))
  4732. (message "Quit"))))
  4733. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4734. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4735. (defvar org-goto-local-auto-isearch-map) ; defined below
  4736. (defun org-get-location (buf help)
  4737. "Let the user select a location in the Org-mode buffer BUF.
  4738. This function uses a recursive edit. It returns the selected position
  4739. or nil."
  4740. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4741. (isearch-hide-immediately nil)
  4742. (isearch-search-fun-function
  4743. (lambda () 'org-goto-local-search-headings))
  4744. (org-goto-selected-point org-goto-exit-command))
  4745. (save-excursion
  4746. (save-window-excursion
  4747. (delete-other-windows)
  4748. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4749. (switch-to-buffer
  4750. (condition-case nil
  4751. (make-indirect-buffer (current-buffer) "*org-goto*")
  4752. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4753. (with-output-to-temp-buffer "*Help*"
  4754. (princ help))
  4755. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4756. (setq buffer-read-only nil)
  4757. (let ((org-startup-truncated t)
  4758. (org-startup-folded nil)
  4759. (org-startup-align-all-tables nil))
  4760. (org-mode)
  4761. (org-overview))
  4762. (setq buffer-read-only t)
  4763. (if (and (boundp 'org-goto-start-pos)
  4764. (integer-or-marker-p org-goto-start-pos))
  4765. (let ((org-show-hierarchy-above t)
  4766. (org-show-siblings t)
  4767. (org-show-following-heading t))
  4768. (goto-char org-goto-start-pos)
  4769. (and (org-invisible-p) (org-show-context)))
  4770. (goto-char (point-min)))
  4771. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4772. (message "Select location and press RET")
  4773. (use-local-map org-goto-map)
  4774. (recursive-edit)
  4775. ))
  4776. (kill-buffer "*org-goto*")
  4777. (cons org-goto-selected-point org-goto-exit-command)))
  4778. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4779. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4780. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4781. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4782. (defun org-goto-local-search-headings (string bound noerror)
  4783. "Search and make sure that any matches are in headlines."
  4784. (catch 'return
  4785. (while (if isearch-forward
  4786. (search-forward string bound noerror)
  4787. (search-backward string bound noerror))
  4788. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4789. (and (member :headline context)
  4790. (not (member :tags context))))
  4791. (throw 'return (point))))))
  4792. (defun org-goto-local-auto-isearch ()
  4793. "Start isearch."
  4794. (interactive)
  4795. (goto-char (point-min))
  4796. (let ((keys (this-command-keys)))
  4797. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4798. (isearch-mode t)
  4799. (isearch-process-search-char (string-to-char keys)))))
  4800. (defun org-goto-ret (&optional arg)
  4801. "Finish `org-goto' by going to the new location."
  4802. (interactive "P")
  4803. (setq org-goto-selected-point (point)
  4804. org-goto-exit-command 'return)
  4805. (throw 'exit nil))
  4806. (defun org-goto-left ()
  4807. "Finish `org-goto' by going to the new location."
  4808. (interactive)
  4809. (if (org-on-heading-p)
  4810. (progn
  4811. (beginning-of-line 1)
  4812. (setq org-goto-selected-point (point)
  4813. org-goto-exit-command 'left)
  4814. (throw 'exit nil))
  4815. (error "Not on a heading")))
  4816. (defun org-goto-right ()
  4817. "Finish `org-goto' by going to the new location."
  4818. (interactive)
  4819. (if (org-on-heading-p)
  4820. (progn
  4821. (setq org-goto-selected-point (point)
  4822. org-goto-exit-command 'right)
  4823. (throw 'exit nil))
  4824. (error "Not on a heading")))
  4825. (defun org-goto-quit ()
  4826. "Finish `org-goto' without cursor motion."
  4827. (interactive)
  4828. (setq org-goto-selected-point nil)
  4829. (setq org-goto-exit-command 'quit)
  4830. (throw 'exit nil))
  4831. ;;; Indirect buffer display of subtrees
  4832. (defvar org-indirect-dedicated-frame nil
  4833. "This is the frame being used for indirect tree display.")
  4834. (defvar org-last-indirect-buffer nil)
  4835. (defun org-tree-to-indirect-buffer (&optional arg)
  4836. "Create indirect buffer and narrow it to current subtree.
  4837. With numerical prefix ARG, go up to this level and then take that tree.
  4838. If ARG is negative, go up that many levels.
  4839. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4840. indirect buffer previously made with this command, to avoid proliferation of
  4841. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4842. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4843. is kept so that you can work with several indirect buffers at the same time.
  4844. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4845. requests that a new frame be made for the new buffer, so that the dedicated
  4846. frame is not changed."
  4847. (interactive "P")
  4848. (let ((cbuf (current-buffer))
  4849. (cwin (selected-window))
  4850. (pos (point))
  4851. beg end level heading ibuf)
  4852. (save-excursion
  4853. (org-back-to-heading t)
  4854. (when (numberp arg)
  4855. (setq level (org-outline-level))
  4856. (if (< arg 0) (setq arg (+ level arg)))
  4857. (while (> (setq level (org-outline-level)) arg)
  4858. (outline-up-heading 1 t)))
  4859. (setq beg (point)
  4860. heading (org-get-heading))
  4861. (org-end-of-subtree t) (setq end (point)))
  4862. (if (and (buffer-live-p org-last-indirect-buffer)
  4863. (not (eq org-indirect-buffer-display 'new-frame))
  4864. (not arg))
  4865. (kill-buffer org-last-indirect-buffer))
  4866. (setq ibuf (org-get-indirect-buffer cbuf)
  4867. org-last-indirect-buffer ibuf)
  4868. (cond
  4869. ((or (eq org-indirect-buffer-display 'new-frame)
  4870. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4871. (select-frame (make-frame))
  4872. (delete-other-windows)
  4873. (switch-to-buffer ibuf)
  4874. (org-set-frame-title heading))
  4875. ((eq org-indirect-buffer-display 'dedicated-frame)
  4876. (raise-frame
  4877. (select-frame (or (and org-indirect-dedicated-frame
  4878. (frame-live-p org-indirect-dedicated-frame)
  4879. org-indirect-dedicated-frame)
  4880. (setq org-indirect-dedicated-frame (make-frame)))))
  4881. (delete-other-windows)
  4882. (switch-to-buffer ibuf)
  4883. (org-set-frame-title (concat "Indirect: " heading)))
  4884. ((eq org-indirect-buffer-display 'current-window)
  4885. (switch-to-buffer ibuf))
  4886. ((eq org-indirect-buffer-display 'other-window)
  4887. (pop-to-buffer ibuf))
  4888. (t (error "Invalid value.")))
  4889. (if (featurep 'xemacs)
  4890. (save-excursion (org-mode) (turn-on-font-lock)))
  4891. (narrow-to-region beg end)
  4892. (show-all)
  4893. (goto-char pos)
  4894. (and (window-live-p cwin) (select-window cwin))))
  4895. (defun org-get-indirect-buffer (&optional buffer)
  4896. (setq buffer (or buffer (current-buffer)))
  4897. (let ((n 1) (base (buffer-name buffer)) bname)
  4898. (while (buffer-live-p
  4899. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4900. (setq n (1+ n)))
  4901. (condition-case nil
  4902. (make-indirect-buffer buffer bname 'clone)
  4903. (error (make-indirect-buffer buffer bname)))))
  4904. (defun org-set-frame-title (title)
  4905. "Set the title of the current frame to the string TITLE."
  4906. ;; FIXME: how to name a single frame in XEmacs???
  4907. (unless (featurep 'xemacs)
  4908. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4909. ;;;; Structure editing
  4910. ;;; Inserting headlines
  4911. (defun org-previous-line-empty-p ()
  4912. (save-excursion
  4913. (and (not (bobp))
  4914. (or (beginning-of-line 0) t)
  4915. (save-match-data
  4916. (looking-at "[ \t]*$")))))
  4917. (defun org-insert-heading (&optional force-heading)
  4918. "Insert a new heading or item with same depth at point.
  4919. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4920. If point is at the beginning of a headline, insert a sibling before the
  4921. current headline. If point is not at the beginning, do not split the line,
  4922. but create the new headline after the current line."
  4923. (interactive "P")
  4924. (if (= (buffer-size) 0)
  4925. (insert "\n* ")
  4926. (when (or force-heading (not (org-insert-item)))
  4927. (let* ((empty-line-p nil)
  4928. (head (save-excursion
  4929. (condition-case nil
  4930. (progn
  4931. (org-back-to-heading)
  4932. (setq empty-line-p (org-previous-line-empty-p))
  4933. (match-string 0))
  4934. (error "*"))))
  4935. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4936. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4937. pos hide-previous previous-pos)
  4938. (cond
  4939. ((and (org-on-heading-p) (bolp)
  4940. (or (bobp)
  4941. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4942. ;; insert before the current line
  4943. (open-line (if blank 2 1)))
  4944. ((and (bolp)
  4945. (or (bobp)
  4946. (save-excursion
  4947. (backward-char 1) (not (org-invisible-p)))))
  4948. ;; insert right here
  4949. nil)
  4950. (t
  4951. ;; somewhere in the line
  4952. (save-excursion
  4953. (setq previous-pos (point-at-bol))
  4954. (end-of-line)
  4955. (setq hide-previous (org-invisible-p)))
  4956. (and org-insert-heading-respect-content (org-show-subtree))
  4957. (let ((split
  4958. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4959. (save-excursion
  4960. (let ((p (point)))
  4961. (goto-char (point-at-bol))
  4962. (and (looking-at org-complex-heading-regexp)
  4963. (> p (match-beginning 4)))))))
  4964. tags pos)
  4965. (cond
  4966. (org-insert-heading-respect-content
  4967. (org-end-of-subtree nil t)
  4968. (or (bolp) (newline))
  4969. (or (org-previous-line-empty-p)
  4970. (and blank (newline)))
  4971. (open-line 1))
  4972. ((org-on-heading-p)
  4973. (when hide-previous
  4974. (show-children)
  4975. (org-show-entry))
  4976. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4977. (setq tags (and (match-end 2) (match-string 2)))
  4978. (and (match-end 1)
  4979. (delete-region (match-beginning 1) (match-end 1)))
  4980. (setq pos (point-at-bol))
  4981. (or split (end-of-line 1))
  4982. (delete-horizontal-space)
  4983. (newline (if blank 2 1))
  4984. (when tags
  4985. (save-excursion
  4986. (goto-char pos)
  4987. (end-of-line 1)
  4988. (insert " " tags)
  4989. (org-set-tags nil 'align))))
  4990. (t
  4991. (or split (end-of-line 1))
  4992. (newline (if blank 2 1)))))))
  4993. (insert head) (just-one-space)
  4994. (setq pos (point))
  4995. (end-of-line 1)
  4996. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4997. (when (and org-insert-heading-respect-content hide-previous)
  4998. (save-excursion
  4999. (goto-char previous-pos)
  5000. (hide-subtree)))
  5001. (run-hooks 'org-insert-heading-hook)))))
  5002. (defun org-get-heading (&optional no-tags)
  5003. "Return the heading of the current entry, without the stars."
  5004. (save-excursion
  5005. (org-back-to-heading t)
  5006. (if (looking-at
  5007. (if no-tags
  5008. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5009. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5010. (match-string 1) "")))
  5011. (defun org-heading-components ()
  5012. "Return the components of the current heading.
  5013. This is a list with the following elements:
  5014. - the level as an integer
  5015. - the reduced level, different if `org-odd-levels-only' is set.
  5016. - the TODO keyword, or nil
  5017. - the priority character, like ?A, or nil if no priority is given
  5018. - the headline text itself, or the tags string if no headline text
  5019. - the tags string, or nil."
  5020. (save-excursion
  5021. (org-back-to-heading t)
  5022. (if (looking-at org-complex-heading-regexp)
  5023. (list (length (match-string 1))
  5024. (org-reduced-level (length (match-string 1)))
  5025. (org-match-string-no-properties 2)
  5026. (and (match-end 3) (aref (match-string 3) 2))
  5027. (org-match-string-no-properties 4)
  5028. (org-match-string-no-properties 5)))))
  5029. (defun org-get-entry ()
  5030. "Get the entry text, after heading, entire subtree."
  5031. (save-excursion
  5032. (org-back-to-heading t)
  5033. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5034. (defun org-insert-heading-after-current ()
  5035. "Insert a new heading with same level as current, after current subtree."
  5036. (interactive)
  5037. (org-back-to-heading)
  5038. (org-insert-heading)
  5039. (org-move-subtree-down)
  5040. (end-of-line 1))
  5041. (defun org-insert-heading-respect-content ()
  5042. (interactive)
  5043. (let ((org-insert-heading-respect-content t))
  5044. (org-insert-heading t)))
  5045. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5046. (interactive "P")
  5047. (let ((org-insert-heading-respect-content t))
  5048. (org-insert-todo-heading force-state t)))
  5049. (defun org-insert-todo-heading (arg &optional force-heading)
  5050. "Insert a new heading with the same level and TODO state as current heading.
  5051. If the heading has no TODO state, or if the state is DONE, use the first
  5052. state (TODO by default). Also with prefix arg, force first state."
  5053. (interactive "P")
  5054. (when (or force-heading (not (org-insert-item 'checkbox)))
  5055. (org-insert-heading force-heading)
  5056. (save-excursion
  5057. (org-back-to-heading)
  5058. (outline-previous-heading)
  5059. (looking-at org-todo-line-regexp))
  5060. (let*
  5061. ((new-mark-x
  5062. (if (or arg
  5063. (not (match-beginning 2))
  5064. (member (match-string 2) org-done-keywords))
  5065. (car org-todo-keywords-1)
  5066. (match-string 2)))
  5067. (new-mark
  5068. (or
  5069. (run-hook-with-args-until-success
  5070. 'org-todo-get-default-hook new-mark-x nil)
  5071. new-mark-x)))
  5072. (beginning-of-line 1)
  5073. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5074. (if org-treat-insert-todo-heading-as-state-change
  5075. (org-todo new-mark)
  5076. (insert new-mark " "))))
  5077. (when org-provide-todo-statistics
  5078. (org-update-parent-todo-statistics))))
  5079. (defun org-insert-subheading (arg)
  5080. "Insert a new subheading and demote it.
  5081. Works for outline headings and for plain lists alike."
  5082. (interactive "P")
  5083. (org-insert-heading arg)
  5084. (cond
  5085. ((org-on-heading-p) (org-do-demote))
  5086. ((org-at-item-p) (org-indent-item 1))))
  5087. (defun org-insert-todo-subheading (arg)
  5088. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5089. Works for outline headings and for plain lists alike."
  5090. (interactive "P")
  5091. (org-insert-todo-heading arg)
  5092. (cond
  5093. ((org-on-heading-p) (org-do-demote))
  5094. ((org-at-item-p) (org-indent-item 1))))
  5095. ;;; Promotion and Demotion
  5096. (defun org-promote-subtree ()
  5097. "Promote the entire subtree.
  5098. See also `org-promote'."
  5099. (interactive)
  5100. (save-excursion
  5101. (org-map-tree 'org-promote))
  5102. (org-fix-position-after-promote))
  5103. (defun org-demote-subtree ()
  5104. "Demote the entire subtree. See `org-demote'.
  5105. See also `org-promote'."
  5106. (interactive)
  5107. (save-excursion
  5108. (org-map-tree 'org-demote))
  5109. (org-fix-position-after-promote))
  5110. (defun org-do-promote ()
  5111. "Promote the current heading higher up the tree.
  5112. If the region is active in `transient-mark-mode', promote all headings
  5113. in the region."
  5114. (interactive)
  5115. (save-excursion
  5116. (if (org-region-active-p)
  5117. (org-map-region 'org-promote (region-beginning) (region-end))
  5118. (org-promote)))
  5119. (org-fix-position-after-promote))
  5120. (defun org-do-demote ()
  5121. "Demote the current heading lower down the tree.
  5122. If the region is active in `transient-mark-mode', demote all headings
  5123. in the region."
  5124. (interactive)
  5125. (save-excursion
  5126. (if (org-region-active-p)
  5127. (org-map-region 'org-demote (region-beginning) (region-end))
  5128. (org-demote)))
  5129. (org-fix-position-after-promote))
  5130. (defun org-fix-position-after-promote ()
  5131. "Make sure that after pro/demotion cursor position is right."
  5132. (let ((pos (point)))
  5133. (when (save-excursion
  5134. (beginning-of-line 1)
  5135. (looking-at org-todo-line-regexp)
  5136. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5137. (cond ((eobp) (insert " "))
  5138. ((eolp) (insert " "))
  5139. ((equal (char-after) ?\ ) (forward-char 1))))))
  5140. (defun org-reduced-level (l)
  5141. "Compute the effective level of a heading.
  5142. This takes into account the setting of `org-odd-levels-only'."
  5143. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5144. (defun org-get-valid-level (level &optional change)
  5145. "Rectify a level change under the influence of `org-odd-levels-only'
  5146. LEVEL is a current level, CHANGE is by how much the level should be
  5147. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5148. even level numbers will become the next higher odd number."
  5149. (if org-odd-levels-only
  5150. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5151. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5152. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5153. (max 1 (+ level (or change 0)))))
  5154. (if (boundp 'define-obsolete-function-alias)
  5155. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5156. (define-obsolete-function-alias 'org-get-legal-level
  5157. 'org-get-valid-level)
  5158. (define-obsolete-function-alias 'org-get-legal-level
  5159. 'org-get-valid-level "23.1")))
  5160. (defun org-promote ()
  5161. "Promote the current heading higher up the tree.
  5162. If the region is active in `transient-mark-mode', promote all headings
  5163. in the region."
  5164. (org-back-to-heading t)
  5165. (let* ((level (save-match-data (funcall outline-level)))
  5166. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5167. (diff (abs (- level (length up-head) -1))))
  5168. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5169. (replace-match up-head nil t)
  5170. ;; Fixup tag positioning
  5171. (and org-auto-align-tags (org-set-tags nil t))
  5172. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5173. (defun org-demote ()
  5174. "Demote the current heading lower down the tree.
  5175. If the region is active in `transient-mark-mode', demote all headings
  5176. in the region."
  5177. (org-back-to-heading t)
  5178. (let* ((level (save-match-data (funcall outline-level)))
  5179. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5180. (diff (abs (- level (length down-head) -1))))
  5181. (replace-match down-head nil t)
  5182. ;; Fixup tag positioning
  5183. (and org-auto-align-tags (org-set-tags nil t))
  5184. (if org-adapt-indentation (org-fixup-indentation diff))))
  5185. (defun org-map-tree (fun)
  5186. "Call FUN for every heading underneath the current one."
  5187. (org-back-to-heading)
  5188. (let ((level (funcall outline-level)))
  5189. (save-excursion
  5190. (funcall fun)
  5191. (while (and (progn
  5192. (outline-next-heading)
  5193. (> (funcall outline-level) level))
  5194. (not (eobp)))
  5195. (funcall fun)))))
  5196. (defun org-map-region (fun beg end)
  5197. "Call FUN for every heading between BEG and END."
  5198. (let ((org-ignore-region t))
  5199. (save-excursion
  5200. (setq end (copy-marker end))
  5201. (goto-char beg)
  5202. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5203. (< (point) end))
  5204. (funcall fun))
  5205. (while (and (progn
  5206. (outline-next-heading)
  5207. (< (point) end))
  5208. (not (eobp)))
  5209. (funcall fun)))))
  5210. (defun org-fixup-indentation (diff)
  5211. "Change the indentation in the current entry by DIFF
  5212. However, if any line in the current entry has no indentation, or if it
  5213. would end up with no indentation after the change, nothing at all is done."
  5214. (save-excursion
  5215. (let ((end (save-excursion (outline-next-heading)
  5216. (point-marker)))
  5217. (prohibit (if (> diff 0)
  5218. "^\\S-"
  5219. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5220. col)
  5221. (unless (save-excursion (end-of-line 1)
  5222. (re-search-forward prohibit end t))
  5223. (while (and (< (point) end)
  5224. (re-search-forward "^[ \t]+" end t))
  5225. (goto-char (match-end 0))
  5226. (setq col (current-column))
  5227. (if (< diff 0) (replace-match ""))
  5228. (org-indent-to-column (+ diff col))))
  5229. (move-marker end nil))))
  5230. (defun org-convert-to-odd-levels ()
  5231. "Convert an org-mode file with all levels allowed to one with odd levels.
  5232. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5233. level 5 etc."
  5234. (interactive)
  5235. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5236. (let ((org-odd-levels-only nil) n)
  5237. (save-excursion
  5238. (goto-char (point-min))
  5239. (while (re-search-forward "^\\*\\*+ " nil t)
  5240. (setq n (- (length (match-string 0)) 2))
  5241. (while (>= (setq n (1- n)) 0)
  5242. (org-demote))
  5243. (end-of-line 1))))))
  5244. (defun org-convert-to-oddeven-levels ()
  5245. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5246. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5247. section with an even level, conversion would destroy the structure of the file. An error
  5248. is signaled in this case."
  5249. (interactive)
  5250. (goto-char (point-min))
  5251. ;; First check if there are no even levels
  5252. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5253. (org-show-context t)
  5254. (error "Not all levels are odd in this file. Conversion not possible."))
  5255. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5256. (let ((org-odd-levels-only nil) n)
  5257. (save-excursion
  5258. (goto-char (point-min))
  5259. (while (re-search-forward "^\\*\\*+ " nil t)
  5260. (setq n (/ (1- (length (match-string 0))) 2))
  5261. (while (>= (setq n (1- n)) 0)
  5262. (org-promote))
  5263. (end-of-line 1))))))
  5264. (defun org-tr-level (n)
  5265. "Make N odd if required."
  5266. (if org-odd-levels-only (1+ (/ n 2)) n))
  5267. ;;; Vertical tree motion, cutting and pasting of subtrees
  5268. (defun org-move-subtree-up (&optional arg)
  5269. "Move the current subtree up past ARG headlines of the same level."
  5270. (interactive "p")
  5271. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5272. (defun org-move-subtree-down (&optional arg)
  5273. "Move the current subtree down past ARG headlines of the same level."
  5274. (interactive "p")
  5275. (setq arg (prefix-numeric-value arg))
  5276. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5277. 'outline-get-last-sibling))
  5278. (ins-point (make-marker))
  5279. (cnt (abs arg))
  5280. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5281. ;; Select the tree
  5282. (org-back-to-heading)
  5283. (setq beg0 (point))
  5284. (save-excursion
  5285. (setq ne-beg (org-back-over-empty-lines))
  5286. (setq beg (point)))
  5287. (save-match-data
  5288. (save-excursion (outline-end-of-heading)
  5289. (setq folded (org-invisible-p)))
  5290. (outline-end-of-subtree))
  5291. (outline-next-heading)
  5292. (setq ne-end (org-back-over-empty-lines))
  5293. (setq end (point))
  5294. (goto-char beg0)
  5295. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5296. ;; include less whitespace
  5297. (save-excursion
  5298. (goto-char beg)
  5299. (forward-line (- ne-beg ne-end))
  5300. (setq beg (point))))
  5301. ;; Find insertion point, with error handling
  5302. (while (> cnt 0)
  5303. (or (and (funcall movfunc) (looking-at outline-regexp))
  5304. (progn (goto-char beg0)
  5305. (error "Cannot move past superior level or buffer limit")))
  5306. (setq cnt (1- cnt)))
  5307. (if (> arg 0)
  5308. ;; Moving forward - still need to move over subtree
  5309. (progn (org-end-of-subtree t t)
  5310. (save-excursion
  5311. (org-back-over-empty-lines)
  5312. (or (bolp) (newline)))))
  5313. (setq ne-ins (org-back-over-empty-lines))
  5314. (move-marker ins-point (point))
  5315. (setq txt (buffer-substring beg end))
  5316. (org-save-markers-in-region beg end)
  5317. (delete-region beg end)
  5318. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5319. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5320. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5321. (let ((bbb (point)))
  5322. (insert-before-markers txt)
  5323. (org-reinstall-markers-in-region bbb)
  5324. (move-marker ins-point bbb))
  5325. (or (bolp) (insert "\n"))
  5326. (setq ins-end (point))
  5327. (goto-char ins-point)
  5328. (org-skip-whitespace)
  5329. (when (and (< arg 0)
  5330. (org-first-sibling-p)
  5331. (> ne-ins ne-beg))
  5332. ;; Move whitespace back to beginning
  5333. (save-excursion
  5334. (goto-char ins-end)
  5335. (let ((kill-whole-line t))
  5336. (kill-line (- ne-ins ne-beg)) (point)))
  5337. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5338. (move-marker ins-point nil)
  5339. (org-compact-display-after-subtree-move)
  5340. (org-show-empty-lines-in-parent)
  5341. (unless folded
  5342. (org-show-entry)
  5343. (show-children)
  5344. (org-cycle-hide-drawers 'children))))
  5345. (defvar org-subtree-clip ""
  5346. "Clipboard for cut and paste of subtrees.
  5347. This is actually only a copy of the kill, because we use the normal kill
  5348. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5349. (defvar org-subtree-clip-folded nil
  5350. "Was the last copied subtree folded?
  5351. This is used to fold the tree back after pasting.")
  5352. (defun org-cut-subtree (&optional n)
  5353. "Cut the current subtree into the clipboard.
  5354. With prefix arg N, cut this many sequential subtrees.
  5355. This is a short-hand for marking the subtree and then cutting it."
  5356. (interactive "p")
  5357. (org-copy-subtree n 'cut))
  5358. (defun org-copy-subtree (&optional n cut force-store-markers)
  5359. "Cut the current subtree into the clipboard.
  5360. With prefix arg N, cut this many sequential subtrees.
  5361. This is a short-hand for marking the subtree and then copying it.
  5362. If CUT is non-nil, actually cut the subtree.
  5363. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5364. of some markers in the region, even if CUT is non-nil. This is
  5365. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5366. (interactive "p")
  5367. (let (beg end folded (beg0 (point)))
  5368. (if (interactive-p)
  5369. (org-back-to-heading nil) ; take what looks like a subtree
  5370. (org-back-to-heading t)) ; take what is really there
  5371. (org-back-over-empty-lines)
  5372. (setq beg (point))
  5373. (skip-chars-forward " \t\r\n")
  5374. (save-match-data
  5375. (save-excursion (outline-end-of-heading)
  5376. (setq folded (org-invisible-p)))
  5377. (condition-case nil
  5378. (outline-forward-same-level (1- n))
  5379. (error nil))
  5380. (org-end-of-subtree t t))
  5381. (org-back-over-empty-lines)
  5382. (setq end (point))
  5383. (goto-char beg0)
  5384. (when (> end beg)
  5385. (setq org-subtree-clip-folded folded)
  5386. (when (or cut force-store-markers)
  5387. (org-save-markers-in-region beg end))
  5388. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5389. (setq org-subtree-clip (current-kill 0))
  5390. (message "%s: Subtree(s) with %d characters"
  5391. (if cut "Cut" "Copied")
  5392. (length org-subtree-clip)))))
  5393. (defun org-paste-subtree (&optional level tree for-yank)
  5394. "Paste the clipboard as a subtree, with modification of headline level.
  5395. The entire subtree is promoted or demoted in order to match a new headline
  5396. level.
  5397. If the cursor is at the beginning of a headline, the same level as
  5398. that headline is used to paste the tree
  5399. If not, the new level is derived from the *visible* headings
  5400. before and after the insertion point, and taken to be the inferior headline
  5401. level of the two. So if the previous visible heading is level 3 and the
  5402. next is level 4 (or vice versa), level 4 will be used for insertion.
  5403. This makes sure that the subtree remains an independent subtree and does
  5404. not swallow low level entries.
  5405. You can also force a different level, either by using a numeric prefix
  5406. argument, or by inserting the heading marker by hand. For example, if the
  5407. cursor is after \"*****\", then the tree will be shifted to level 5.
  5408. If optional TREE is given, use this text instead of the kill ring.
  5409. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5410. move back over whitespace before inserting, and move point to the end of
  5411. the inserted text when done."
  5412. (interactive "P")
  5413. (unless (org-kill-is-subtree-p tree)
  5414. (error "%s"
  5415. (substitute-command-keys
  5416. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5417. (let* ((visp (not (org-invisible-p)))
  5418. (txt (or tree (and kill-ring (current-kill 0))))
  5419. (^re (concat "^\\(" outline-regexp "\\)"))
  5420. (re (concat "\\(" outline-regexp "\\)"))
  5421. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5422. (old-level (if (string-match ^re txt)
  5423. (- (match-end 0) (match-beginning 0) 1)
  5424. -1))
  5425. (force-level (cond (level (prefix-numeric-value level))
  5426. ((and (looking-at "[ \t]*$")
  5427. (string-match
  5428. ^re_ (buffer-substring
  5429. (point-at-bol) (point))))
  5430. (- (match-end 1) (match-beginning 1)))
  5431. ((and (bolp)
  5432. (looking-at org-outline-regexp))
  5433. (- (match-end 0) (point) 1))
  5434. (t nil)))
  5435. (previous-level (save-excursion
  5436. (condition-case nil
  5437. (progn
  5438. (outline-previous-visible-heading 1)
  5439. (if (looking-at re)
  5440. (- (match-end 0) (match-beginning 0) 1)
  5441. 1))
  5442. (error 1))))
  5443. (next-level (save-excursion
  5444. (condition-case nil
  5445. (progn
  5446. (or (looking-at outline-regexp)
  5447. (outline-next-visible-heading 1))
  5448. (if (looking-at re)
  5449. (- (match-end 0) (match-beginning 0) 1)
  5450. 1))
  5451. (error 1))))
  5452. (new-level (or force-level (max previous-level next-level)))
  5453. (shift (if (or (= old-level -1)
  5454. (= new-level -1)
  5455. (= old-level new-level))
  5456. 0
  5457. (- new-level old-level)))
  5458. (delta (if (> shift 0) -1 1))
  5459. (func (if (> shift 0) 'org-demote 'org-promote))
  5460. (org-odd-levels-only nil)
  5461. beg end newend)
  5462. ;; Remove the forced level indicator
  5463. (if force-level
  5464. (delete-region (point-at-bol) (point)))
  5465. ;; Paste
  5466. (beginning-of-line 1)
  5467. (unless for-yank (org-back-over-empty-lines))
  5468. (setq beg (point))
  5469. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5470. (insert-before-markers txt)
  5471. (unless (string-match "\n\\'" txt) (insert "\n"))
  5472. (setq newend (point))
  5473. (org-reinstall-markers-in-region beg)
  5474. (setq end (point))
  5475. (goto-char beg)
  5476. (skip-chars-forward " \t\n\r")
  5477. (setq beg (point))
  5478. (if (and (org-invisible-p) visp)
  5479. (save-excursion (outline-show-heading)))
  5480. ;; Shift if necessary
  5481. (unless (= shift 0)
  5482. (save-restriction
  5483. (narrow-to-region beg end)
  5484. (while (not (= shift 0))
  5485. (org-map-region func (point-min) (point-max))
  5486. (setq shift (+ delta shift)))
  5487. (goto-char (point-min))
  5488. (setq newend (point-max))))
  5489. (when (or (interactive-p) for-yank)
  5490. (message "Clipboard pasted as level %d subtree" new-level))
  5491. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5492. kill-ring
  5493. (eq org-subtree-clip (current-kill 0))
  5494. org-subtree-clip-folded)
  5495. ;; The tree was folded before it was killed/copied
  5496. (hide-subtree))
  5497. (and for-yank (goto-char newend))))
  5498. (defun org-kill-is-subtree-p (&optional txt)
  5499. "Check if the current kill is an outline subtree, or a set of trees.
  5500. Returns nil if kill does not start with a headline, or if the first
  5501. headline level is not the largest headline level in the tree.
  5502. So this will actually accept several entries of equal levels as well,
  5503. which is OK for `org-paste-subtree'.
  5504. If optional TXT is given, check this string instead of the current kill."
  5505. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5506. (start-level (and kill
  5507. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5508. org-outline-regexp "\\)")
  5509. kill)
  5510. (- (match-end 2) (match-beginning 2) 1)))
  5511. (re (concat "^" org-outline-regexp))
  5512. (start (1+ (or (match-beginning 2) -1))))
  5513. (if (not start-level)
  5514. (progn
  5515. nil) ;; does not even start with a heading
  5516. (catch 'exit
  5517. (while (setq start (string-match re kill (1+ start)))
  5518. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5519. (throw 'exit nil)))
  5520. t))))
  5521. (defvar org-markers-to-move nil
  5522. "Markers that should be moved with a cut-and-paste operation.
  5523. Those markers are stored together with their positions relative to
  5524. the start of the region.")
  5525. (defun org-save-markers-in-region (beg end)
  5526. "Check markers in region.
  5527. If these markers are between BEG and END, record their position relative
  5528. to BEG, so that after moving the block of text, we can put the markers back
  5529. into place.
  5530. This function gets called just before an entry or tree gets cut from the
  5531. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5532. called immediately, to move the markers with the entries."
  5533. (setq org-markers-to-move nil)
  5534. (when (featurep 'org-clock)
  5535. (org-clock-save-markers-for-cut-and-paste beg end))
  5536. (when (featurep 'org-agenda)
  5537. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5538. (defun org-check-and-save-marker (marker beg end)
  5539. "Check if MARKER is between BEG and END.
  5540. If yes, remember the marker and the distance to BEG."
  5541. (when (and (marker-buffer marker)
  5542. (equal (marker-buffer marker) (current-buffer)))
  5543. (if (and (>= marker beg) (< marker end))
  5544. (push (cons marker (- marker beg)) org-markers-to-move))))
  5545. (defun org-reinstall-markers-in-region (beg)
  5546. "Move all remembered markers to their position relative to BEG."
  5547. (mapc (lambda (x)
  5548. (move-marker (car x) (+ beg (cdr x))))
  5549. org-markers-to-move)
  5550. (setq org-markers-to-move nil))
  5551. (defun org-narrow-to-subtree ()
  5552. "Narrow buffer to the current subtree."
  5553. (interactive)
  5554. (save-excursion
  5555. (save-match-data
  5556. (narrow-to-region
  5557. (progn (org-back-to-heading) (point))
  5558. (progn (org-end-of-subtree t) (point))))))
  5559. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5560. "Clone the task (subtree) at point N times.
  5561. The clones will be inserted as siblings.
  5562. In interactive use, the user will be prompted for the number of clones
  5563. to be produced, and for a time SHIFT, which may be a repeater as used
  5564. in time stamps, for example `+3d'.
  5565. When a valid repeater is given and the entry contains any time stamps,
  5566. the clones will become a sequence in time, with time stamps in the
  5567. subtree shifted for each clone produced. If SHIFT is nil or the
  5568. empty string, time stamps will be left alone.
  5569. If the original subtree did contain time stamps with a repeater,
  5570. the following will happen:
  5571. - the repeater will be removed in each clone
  5572. - an additional clone will be produced, with the current, unshifted
  5573. date(s) in the entry.
  5574. - the original entry will be placed *after* all the clones, with
  5575. repeater intact.
  5576. - the start days in the repeater in the original entry will be shifted
  5577. to past the last clone.
  5578. I this way you can spell out a number of instances of a repeating task,
  5579. and still retain the repeater to cover future instances of the task."
  5580. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5581. (let (beg end template task
  5582. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5583. (if (not (and (integerp n) (> n 0)))
  5584. (error "Invalid number of replications %s" n))
  5585. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5586. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5587. shift)))
  5588. (error "Invalid shift specification %s" shift))
  5589. (when doshift
  5590. (setq shift-n (string-to-number (match-string 1 shift))
  5591. shift-what (cdr (assoc (match-string 2 shift)
  5592. '(("d" . day) ("w" . week)
  5593. ("m" . month) ("y" . year))))))
  5594. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5595. (setq nmin 1 nmax n)
  5596. (org-back-to-heading t)
  5597. (setq beg (point))
  5598. (org-end-of-subtree t t)
  5599. (setq end (point))
  5600. (setq template (buffer-substring beg end))
  5601. (when (and doshift
  5602. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5603. (delete-region beg end)
  5604. (setq end beg)
  5605. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5606. (goto-char end)
  5607. (loop for n from nmin to nmax do
  5608. (if (not doshift)
  5609. (setq task template)
  5610. (with-temp-buffer
  5611. (insert template)
  5612. (org-mode)
  5613. (goto-char (point-min))
  5614. (while (re-search-forward org-ts-regexp nil t)
  5615. (org-timestamp-change (* n shift-n) shift-what))
  5616. (unless (= n n-no-remove)
  5617. (goto-char (point-min))
  5618. (while (re-search-forward org-ts-regexp nil t)
  5619. (save-excursion
  5620. (goto-char (match-beginning 0))
  5621. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5622. (delete-region (match-beginning 1) (match-end 1))))))
  5623. (setq task (buffer-string))))
  5624. (insert task))
  5625. (goto-char beg)))
  5626. ;;; Outline Sorting
  5627. (defun org-sort (with-case)
  5628. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5629. Optional argument WITH-CASE means sort case-sensitively.
  5630. With a double prefix argument, also remove duplicate entries."
  5631. (interactive "P")
  5632. (if (org-at-table-p)
  5633. (org-call-with-arg 'org-table-sort-lines with-case)
  5634. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5635. (defun org-sort-remove-invisible (s)
  5636. (remove-text-properties 0 (length s) org-rm-props s)
  5637. (while (string-match org-bracket-link-regexp s)
  5638. (setq s (replace-match (if (match-end 2)
  5639. (match-string 3 s)
  5640. (match-string 1 s)) t t s)))
  5641. s)
  5642. (defvar org-priority-regexp) ; defined later in the file
  5643. (defvar org-after-sorting-entries-or-items-hook nil
  5644. "Hook that is run after a bunch of entries or items have been sorted.
  5645. When children are sorted, the cursor is in the parent line when this
  5646. hook gets called. When a region or a plain list is sorted, the cursor
  5647. will be in the first entry of the sorted region/list.")
  5648. (defun org-sort-entries-or-items
  5649. (&optional with-case sorting-type getkey-func compare-func property)
  5650. "Sort entries on a certain level of an outline tree, or plain list items.
  5651. If there is an active region, the entries in the region are sorted.
  5652. Else, if the cursor is before the first entry, sort the top-level items.
  5653. Else, the children of the entry at point are sorted.
  5654. If the cursor is at the first item in a plain list, the list items will be
  5655. sorted.
  5656. Sorting can be alphabetically, numerically, by date/time as given by
  5657. a time stamp, by a property or by priority.
  5658. The command prompts for the sorting type unless it has been given to the
  5659. function through the SORTING-TYPE argument, which needs to a character,
  5660. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5661. precise meaning of each character:
  5662. n Numerically, by converting the beginning of the entry/item to a number.
  5663. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5664. t By date/time, either the first active time stamp in the entry, or, if
  5665. none exist, by the first inactive one.
  5666. In items, only the first line will be chekced.
  5667. s By the scheduled date/time.
  5668. d By deadline date/time.
  5669. c By creation time, which is assumed to be the first inactive time stamp
  5670. at the beginning of a line.
  5671. p By priority according to the cookie.
  5672. r By the value of a property.
  5673. Capital letters will reverse the sort order.
  5674. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5675. called with point at the beginning of the record. It must return either
  5676. a string or a number that should serve as the sorting key for that record.
  5677. Comparing entries ignores case by default. However, with an optional argument
  5678. WITH-CASE, the sorting considers case as well."
  5679. (interactive "P")
  5680. (let ((case-func (if with-case 'identity 'downcase))
  5681. start beg end stars re re2
  5682. txt what tmp plain-list-p)
  5683. ;; Find beginning and end of region to sort
  5684. (cond
  5685. ((org-region-active-p)
  5686. ;; we will sort the region
  5687. (setq end (region-end)
  5688. what "region")
  5689. (goto-char (region-beginning))
  5690. (if (not (org-on-heading-p)) (outline-next-heading))
  5691. (setq start (point)))
  5692. ((org-at-item-p)
  5693. ;; we will sort this plain list
  5694. (org-beginning-of-item-list) (setq start (point))
  5695. (org-end-of-item-list) (setq end (point))
  5696. (goto-char start)
  5697. (setq plain-list-p t
  5698. what "plain list"))
  5699. ((or (org-on-heading-p)
  5700. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5701. ;; we will sort the children of the current headline
  5702. (org-back-to-heading)
  5703. (setq start (point)
  5704. end (progn (org-end-of-subtree t t)
  5705. (org-back-over-empty-lines)
  5706. (point))
  5707. what "children")
  5708. (goto-char start)
  5709. (show-subtree)
  5710. (outline-next-heading))
  5711. (t
  5712. ;; we will sort the top-level entries in this file
  5713. (goto-char (point-min))
  5714. (or (org-on-heading-p) (outline-next-heading))
  5715. (setq start (point) end (point-max) what "top-level")
  5716. (goto-char start)
  5717. (show-all)))
  5718. (setq beg (point))
  5719. (if (>= beg end) (error "Nothing to sort"))
  5720. (unless plain-list-p
  5721. (looking-at "\\(\\*+\\)")
  5722. (setq stars (match-string 1)
  5723. re (concat "^" (regexp-quote stars) " +")
  5724. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5725. txt (buffer-substring beg end))
  5726. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5727. (if (and (not (equal stars "*")) (string-match re2 txt))
  5728. (error "Region to sort contains a level above the first entry")))
  5729. (unless sorting-type
  5730. (message
  5731. (if plain-list-p
  5732. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5733. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5734. [t]ime [s]cheduled [d]eadline [c]reated
  5735. A/N/T/S/D/C/P/O/F means reversed:")
  5736. what)
  5737. (setq sorting-type (read-char-exclusive))
  5738. (and (= (downcase sorting-type) ?f)
  5739. (setq getkey-func
  5740. (org-ido-completing-read "Sort using function: "
  5741. obarray 'fboundp t nil nil))
  5742. (setq getkey-func (intern getkey-func)))
  5743. (and (= (downcase sorting-type) ?r)
  5744. (setq property
  5745. (org-ido-completing-read "Property: "
  5746. (mapcar 'list (org-buffer-property-keys t))
  5747. nil t))))
  5748. (message "Sorting entries...")
  5749. (save-restriction
  5750. (narrow-to-region start end)
  5751. (let ((dcst (downcase sorting-type))
  5752. (case-fold-search nil)
  5753. (now (current-time)))
  5754. (sort-subr
  5755. (/= dcst sorting-type)
  5756. ;; This function moves to the beginning character of the "record" to
  5757. ;; be sorted.
  5758. (if plain-list-p
  5759. (lambda nil
  5760. (if (org-at-item-p) t (goto-char (point-max))))
  5761. (lambda nil
  5762. (if (re-search-forward re nil t)
  5763. (goto-char (match-beginning 0))
  5764. (goto-char (point-max)))))
  5765. ;; This function moves to the last character of the "record" being
  5766. ;; sorted.
  5767. (if plain-list-p
  5768. 'org-end-of-item
  5769. (lambda nil
  5770. (save-match-data
  5771. (condition-case nil
  5772. (outline-forward-same-level 1)
  5773. (error
  5774. (goto-char (point-max)))))))
  5775. ;; This function returns the value that gets sorted against.
  5776. (if plain-list-p
  5777. (lambda nil
  5778. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5779. (cond
  5780. ((= dcst ?n)
  5781. (string-to-number (buffer-substring (match-end 0)
  5782. (point-at-eol))))
  5783. ((= dcst ?a)
  5784. (buffer-substring (match-end 0) (point-at-eol)))
  5785. ((= dcst ?t)
  5786. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5787. (re-search-forward org-ts-regexp-both
  5788. (point-at-eol) t))
  5789. (org-time-string-to-seconds (match-string 0))
  5790. (time-to-seconds now)))
  5791. ((= dcst ?f)
  5792. (if getkey-func
  5793. (progn
  5794. (setq tmp (funcall getkey-func))
  5795. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5796. tmp)
  5797. (error "Invalid key function `%s'" getkey-func)))
  5798. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5799. (lambda nil
  5800. (cond
  5801. ((= dcst ?n)
  5802. (if (looking-at org-complex-heading-regexp)
  5803. (string-to-number (match-string 4))
  5804. nil))
  5805. ((= dcst ?a)
  5806. (if (looking-at org-complex-heading-regexp)
  5807. (funcall case-func (match-string 4))
  5808. nil))
  5809. ((= dcst ?t)
  5810. (let ((end (save-excursion (outline-next-heading) (point))))
  5811. (if (or (re-search-forward org-ts-regexp end t)
  5812. (re-search-forward org-ts-regexp-both end t))
  5813. (org-time-string-to-seconds (match-string 0))
  5814. (time-to-seconds now))))
  5815. ((= dcst ?c)
  5816. (let ((end (save-excursion (outline-next-heading) (point))))
  5817. (if (re-search-forward
  5818. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5819. end t)
  5820. (org-time-string-to-seconds (match-string 0))
  5821. (time-to-seconds now))))
  5822. ((= dcst ?s)
  5823. (let ((end (save-excursion (outline-next-heading) (point))))
  5824. (if (re-search-forward org-scheduled-time-regexp end t)
  5825. (org-time-string-to-seconds (match-string 1))
  5826. (time-to-seconds now))))
  5827. ((= dcst ?d)
  5828. (let ((end (save-excursion (outline-next-heading) (point))))
  5829. (if (re-search-forward org-deadline-time-regexp end t)
  5830. (org-time-string-to-seconds (match-string 1))
  5831. (time-to-seconds now))))
  5832. ((= dcst ?p)
  5833. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5834. (string-to-char (match-string 2))
  5835. org-default-priority))
  5836. ((= dcst ?r)
  5837. (or (org-entry-get nil property) ""))
  5838. ((= dcst ?o)
  5839. (if (looking-at org-complex-heading-regexp)
  5840. (- 9999 (length (member (match-string 2)
  5841. org-todo-keywords-1)))))
  5842. ((= dcst ?f)
  5843. (if getkey-func
  5844. (progn
  5845. (setq tmp (funcall getkey-func))
  5846. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5847. tmp)
  5848. (error "Invalid key function `%s'" getkey-func)))
  5849. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5850. nil
  5851. (cond
  5852. ((= dcst ?a) 'string<)
  5853. ((= dcst ?t) 'time-less-p)
  5854. ((= dcst ?f) compare-func)
  5855. ((= dcst ?p) '<)
  5856. (t nil)))))
  5857. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5858. (message "Sorting entries...done")))
  5859. (defun org-do-sort (table what &optional with-case sorting-type)
  5860. "Sort TABLE of WHAT according to SORTING-TYPE.
  5861. The user will be prompted for the SORTING-TYPE if the call to this
  5862. function does not specify it. WHAT is only for the prompt, to indicate
  5863. what is being sorted. The sorting key will be extracted from
  5864. the car of the elements of the table.
  5865. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5866. (unless sorting-type
  5867. (message
  5868. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5869. what)
  5870. (setq sorting-type (read-char-exclusive)))
  5871. (let ((dcst (downcase sorting-type))
  5872. extractfun comparefun)
  5873. ;; Define the appropriate functions
  5874. (cond
  5875. ((= dcst ?n)
  5876. (setq extractfun 'string-to-number
  5877. comparefun (if (= dcst sorting-type) '< '>)))
  5878. ((= dcst ?a)
  5879. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5880. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5881. comparefun (if (= dcst sorting-type)
  5882. 'string<
  5883. (lambda (a b) (and (not (string< a b))
  5884. (not (string= a b)))))))
  5885. ((= dcst ?t)
  5886. (setq extractfun
  5887. (lambda (x)
  5888. (if (or (string-match org-ts-regexp x)
  5889. (string-match org-ts-regexp-both x))
  5890. (time-to-seconds
  5891. (org-time-string-to-time (match-string 0 x)))
  5892. 0))
  5893. comparefun (if (= dcst sorting-type) '< '>)))
  5894. (t (error "Invalid sorting type `%c'" sorting-type)))
  5895. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5896. table)
  5897. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5898. ;;; Editing source examples
  5899. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5900. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5901. (defvar org-edit-src-force-single-line nil)
  5902. (defvar org-edit-src-from-org-mode nil)
  5903. (defvar org-edit-src-picture nil)
  5904. (defvar org-edit-src-beg-marker nil)
  5905. (defvar org-edit-src-end-marker nil)
  5906. (defvar org-edit-src-overlay nil)
  5907. (defvar org-edit-src-nindent nil)
  5908. (define-minor-mode org-exit-edit-mode
  5909. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5910. (defun org-edit-src-code ()
  5911. "Edit the source code example at point.
  5912. The example is copied to a separate buffer, and that buffer is switched
  5913. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5914. This will remove the original code in the Org buffer, and replace it with
  5915. the edited version."
  5916. (interactive)
  5917. (let ((line (org-current-line))
  5918. (case-fold-search t)
  5919. (msg (substitute-command-keys
  5920. "Edit, then exit with C-c ' (C-c and single quote)"))
  5921. (info (org-edit-src-find-region-and-lang))
  5922. (org-mode-p (eq major-mode 'org-mode))
  5923. (beg (make-marker))
  5924. (end (make-marker))
  5925. nindent ovl lang lang-f single lfmt code begline buffer)
  5926. (if (not info)
  5927. nil
  5928. (setq beg (move-marker beg (nth 0 info))
  5929. end (move-marker end (nth 1 info))
  5930. code (buffer-substring-no-properties beg end)
  5931. lang (nth 2 info)
  5932. single (nth 3 info)
  5933. lfmt (nth 4 info)
  5934. lang-f (intern (concat lang "-mode"))
  5935. begline (save-excursion (goto-char beg) (org-current-line)))
  5936. (unless (functionp lang-f)
  5937. (error "No such language mode: %s" lang-f))
  5938. (goto-line line)
  5939. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5940. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5941. (switch-to-buffer buffer)
  5942. (when buffer
  5943. (with-current-buffer buffer
  5944. (if (boundp 'org-edit-src-overlay)
  5945. (org-delete-overlay org-edit-src-overlay)))
  5946. (kill-buffer buffer))
  5947. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5948. (setq ovl (org-make-overlay beg end))
  5949. (org-overlay-put ovl 'face 'secondary-selection)
  5950. (org-overlay-put ovl 'edit-buffer buffer)
  5951. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5952. (org-overlay-put ovl 'face 'secondary-selection)
  5953. (org-overlay-put ovl
  5954. 'keymap
  5955. (let ((map (make-sparse-keymap)))
  5956. (define-key map [mouse-1] 'org-edit-src-continue)
  5957. map))
  5958. (org-overlay-put ovl :read-only "Leave me alone")
  5959. (switch-to-buffer buffer)
  5960. (insert code)
  5961. (remove-text-properties (point-min) (point-max)
  5962. '(display nil invisible nil intangible nil))
  5963. (setq nindent (org-do-remove-indentation))
  5964. (let ((org-inhibit-startup t))
  5965. (funcall lang-f))
  5966. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5967. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5968. (when lfmt
  5969. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5970. (when org-mode-p
  5971. (goto-char (point-min))
  5972. (while (re-search-forward "^," nil t)
  5973. (replace-match "")))
  5974. (goto-line (1+ (- line begline)))
  5975. (org-exit-edit-mode)
  5976. (org-set-local 'org-edit-src-beg-marker beg)
  5977. (org-set-local 'org-edit-src-end-marker end)
  5978. (org-set-local 'org-edit-src-overlay ovl)
  5979. (org-set-local 'org-edit-src-nindent nindent)
  5980. (and org-edit-src-persistent-message
  5981. (org-set-local 'header-line-format msg)))
  5982. (message "%s" msg)
  5983. t)))
  5984. (defun org-edit-src-continue (e)
  5985. (interactive "e")
  5986. (mouse-set-point e)
  5987. (let ((buf (get-char-property (point) 'edit-buffer)))
  5988. (if buf (switch-to-buffer buf)
  5989. (error "Something is wrong here"))))
  5990. (defun org-edit-src-find-buffer (beg end)
  5991. "Find a source editing buffer that is already editing the region BEG to END."
  5992. (catch 'exit
  5993. (mapc
  5994. (lambda (b)
  5995. (with-current-buffer b
  5996. (if (and (string-match "\\`*Org Edit " (buffer-name))
  5997. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  5998. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  5999. (equal beg org-edit-src-beg-marker)
  6000. (equal end org-edit-src-end-marker))
  6001. (throw 'exit (current-buffer)))))
  6002. (buffer-list))
  6003. nil))
  6004. (defun org-edit-fixed-width-region ()
  6005. "Edit the fixed-width ascii drawing at point.
  6006. This must be a region where each line starts with a colon followed by
  6007. a space character.
  6008. An new buffer is created and the fixed-width region is copied into it,
  6009. and the buffer is switched into `artist-mode' for editing. When done,
  6010. exit with \\[org-edit-src-exit]. The edited text will then replace
  6011. the fragment in the Org-mode buffer."
  6012. (interactive)
  6013. (let ((line (org-current-line))
  6014. (case-fold-search t)
  6015. (msg (substitute-command-keys
  6016. "Edit, then exit with C-c ' (C-c and single quote)"))
  6017. (org-mode-p (eq major-mode 'org-mode))
  6018. (beg (make-marker))
  6019. (end (make-marker))
  6020. nindent ovl beg1 end1 code begline buffer)
  6021. (beginning-of-line 1)
  6022. (if (looking-at "[ \t]*[^:\n \t]")
  6023. nil
  6024. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  6025. (setq beg1 (point) end1 beg1)
  6026. (save-excursion
  6027. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  6028. (setq beg1 (point-at-bol 2))
  6029. (setq beg1 (point))))
  6030. (save-excursion
  6031. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  6032. (setq end1 (1- (match-beginning 0)))
  6033. (setq end1 (point))))
  6034. (goto-line line))
  6035. (setq beg (move-marker beg beg1)
  6036. end (move-marker end end1)
  6037. code (buffer-substring-no-properties beg end)
  6038. begline (save-excursion (goto-char beg) (org-current-line)))
  6039. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  6040. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  6041. (switch-to-buffer buffer)
  6042. (when buffer
  6043. (with-current-buffer buffer
  6044. (if (boundp 'org-edit-src-overlay)
  6045. (org-delete-overlay org-edit-src-overlay)))
  6046. (kill-buffer buffer))
  6047. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  6048. (setq ovl (org-make-overlay beg end))
  6049. (org-overlay-put ovl 'face 'secondary-selection)
  6050. (org-overlay-put ovl 'edit-buffer buffer)
  6051. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  6052. (org-overlay-put ovl 'face 'secondary-selection)
  6053. (org-overlay-put ovl
  6054. 'keymap
  6055. (let ((map (make-sparse-keymap)))
  6056. (define-key map [mouse-1] 'org-edit-src-continue)
  6057. map))
  6058. (org-overlay-put ovl :read-only "Leave me alone")
  6059. (switch-to-buffer buffer)
  6060. (insert code)
  6061. (remove-text-properties (point-min) (point-max)
  6062. '(display nil invisible nil intangible nil))
  6063. (setq nindent (org-do-remove-indentation))
  6064. (cond
  6065. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  6066. (fundamental-mode)
  6067. (artist-mode 1))
  6068. (t (funcall org-edit-fixed-width-region-mode)))
  6069. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  6070. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  6071. (set (make-local-variable 'org-edit-src-picture) t)
  6072. (goto-char (point-min))
  6073. (while (re-search-forward "^[ \t]*: ?" nil t)
  6074. (replace-match ""))
  6075. (goto-line (1+ (- line begline)))
  6076. (org-exit-edit-mode)
  6077. (org-set-local 'org-edit-src-beg-marker beg)
  6078. (org-set-local 'org-edit-src-end-marker end)
  6079. (org-set-local 'org-edit-src-overlay ovl)
  6080. (org-set-local 'org-edit-src-nindent nindent)
  6081. (and org-edit-src-persistent-message
  6082. (org-set-local 'header-line-format msg)))
  6083. (message "%s" msg)
  6084. t)))
  6085. (defun org-edit-src-find-region-and-lang ()
  6086. "Find the region and language for a local edit.
  6087. Return a list with beginning and end of the region, a string representing
  6088. the language, a switch telling of the content should be in a single line."
  6089. (let ((re-list
  6090. (append
  6091. org-edit-src-region-extra
  6092. '(
  6093. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  6094. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  6095. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  6096. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  6097. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  6098. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  6099. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  6100. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  6101. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  6102. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  6103. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  6104. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  6105. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  6106. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  6107. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  6108. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  6109. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  6110. )))
  6111. (pos (point))
  6112. re1 re2 single beg end lang lfmt match-re1)
  6113. (catch 'exit
  6114. (while (setq entry (pop re-list))
  6115. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  6116. single (nth 3 entry))
  6117. (save-excursion
  6118. (if (or (looking-at re1)
  6119. (re-search-backward re1 nil t))
  6120. (progn
  6121. (setq match-re1 (match-string 0))
  6122. (setq beg (match-end 0)
  6123. lang (org-edit-src-get-lang lang)
  6124. lfmt (org-edit-src-get-label-format match-re1))
  6125. (if (and (re-search-forward re2 nil t)
  6126. (>= (match-end 0) pos))
  6127. (throw 'exit (list beg (match-beginning 0)
  6128. lang single lfmt))))
  6129. (if (or (looking-at re2)
  6130. (re-search-forward re2 nil t))
  6131. (progn
  6132. (setq end (match-beginning 0))
  6133. (if (and (re-search-backward re1 nil t)
  6134. (<= (match-beginning 0) pos))
  6135. (progn
  6136. (setq lfmt (org-edit-src-get-label-format
  6137. (match-string 0)))
  6138. (throw 'exit
  6139. (list (match-end 0) end
  6140. (org-edit-src-get-lang lang)
  6141. single lfmt))))))))))))
  6142. (defun org-edit-src-get-lang (lang)
  6143. "Extract the src language."
  6144. (let ((m (match-string 0)))
  6145. (cond
  6146. ((stringp lang) lang)
  6147. ((integerp lang) (match-string lang))
  6148. ((and (eq lang 'lang)
  6149. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  6150. (match-string 1 m))
  6151. ((and (eq lang 'style)
  6152. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  6153. (match-string 1 m))
  6154. (t "fundamental"))))
  6155. (defun org-edit-src-get-label-format (s)
  6156. "Extract the label format."
  6157. (save-match-data
  6158. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  6159. (match-string 1 s))))
  6160. (defun org-edit-src-exit ()
  6161. "Exit special edit and protect problematic lines."
  6162. (interactive)
  6163. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  6164. (error "This is not an sub-editing buffer, something is wrong..."))
  6165. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  6166. 1
  6167. (org-current-line)))
  6168. (beg org-edit-src-beg-marker)
  6169. (end org-edit-src-end-marker)
  6170. (ovl org-edit-src-overlay)
  6171. (buffer (current-buffer))
  6172. (nindent org-edit-src-nindent)
  6173. code)
  6174. (goto-char (point-min))
  6175. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  6176. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  6177. (when (org-bound-and-true-p org-edit-src-force-single-line)
  6178. (goto-char (point-min))
  6179. (while (re-search-forward "\n" nil t)
  6180. (replace-match " "))
  6181. (goto-char (point-min))
  6182. (if (looking-at "\\s-*") (replace-match " "))
  6183. (if (re-search-forward "\\s-+\\'" nil t)
  6184. (replace-match "")))
  6185. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  6186. (goto-char (point-min))
  6187. (while (re-search-forward
  6188. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  6189. (replace-match ",\\1")))
  6190. (when (org-bound-and-true-p org-edit-src-picture)
  6191. (untabify (point-min) (point-max))
  6192. (goto-char (point-min))
  6193. (while (re-search-forward "^" nil t)
  6194. (replace-match ": ")))
  6195. (when nindent
  6196. (setq nindent (make-string nindent ?\ ))
  6197. (goto-char (point-min))
  6198. (while (re-search-forward "^" nil t)
  6199. (replace-match nindent)))
  6200. (setq code (buffer-string))
  6201. (switch-to-buffer (marker-buffer beg))
  6202. (kill-buffer buffer)
  6203. (goto-char beg)
  6204. (org-delete-overlay ovl)
  6205. (delete-region beg end)
  6206. (insert code)
  6207. (goto-char beg)
  6208. (goto-line (1- (+ (org-current-line) line)))
  6209. (move-marker beg nil)
  6210. (move-marker end nil)))
  6211. ;;; The orgstruct minor mode
  6212. ;; Define a minor mode which can be used in other modes in order to
  6213. ;; integrate the org-mode structure editing commands.
  6214. ;; This is really a hack, because the org-mode structure commands use
  6215. ;; keys which normally belong to the major mode. Here is how it
  6216. ;; works: The minor mode defines all the keys necessary to operate the
  6217. ;; structure commands, but wraps the commands into a function which
  6218. ;; tests if the cursor is currently at a headline or a plain list
  6219. ;; item. If that is the case, the structure command is used,
  6220. ;; temporarily setting many Org-mode variables like regular
  6221. ;; expressions for filling etc. However, when any of those keys is
  6222. ;; used at a different location, function uses `key-binding' to look
  6223. ;; up if the key has an associated command in another currently active
  6224. ;; keymap (minor modes, major mode, global), and executes that
  6225. ;; command. There might be problems if any of the keys is otherwise
  6226. ;; used as a prefix key.
  6227. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6228. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6229. ;; addresses this by checking explicitly for both bindings.
  6230. (defvar orgstruct-mode-map (make-sparse-keymap)
  6231. "Keymap for the minor `orgstruct-mode'.")
  6232. (defvar org-local-vars nil
  6233. "List of local variables, for use by `orgstruct-mode'")
  6234. ;;;###autoload
  6235. (define-minor-mode orgstruct-mode
  6236. "Toggle the minor more `orgstruct-mode'.
  6237. This mode is for using Org-mode structure commands in other modes.
  6238. The following key behave as if Org-mode was active, if the cursor
  6239. is on a headline, or on a plain list item (both in the definition
  6240. of Org-mode).
  6241. M-up Move entry/item up
  6242. M-down Move entry/item down
  6243. M-left Promote
  6244. M-right Demote
  6245. M-S-up Move entry/item up
  6246. M-S-down Move entry/item down
  6247. M-S-left Promote subtree
  6248. M-S-right Demote subtree
  6249. M-q Fill paragraph and items like in Org-mode
  6250. C-c ^ Sort entries
  6251. C-c - Cycle list bullet
  6252. TAB Cycle item visibility
  6253. M-RET Insert new heading/item
  6254. S-M-RET Insert new TODO heading / Checkbox item
  6255. C-c C-c Set tags / toggle checkbox"
  6256. nil " OrgStruct" nil
  6257. (org-load-modules-maybe)
  6258. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6259. ;;;###autoload
  6260. (defun turn-on-orgstruct ()
  6261. "Unconditionally turn on `orgstruct-mode'."
  6262. (orgstruct-mode 1))
  6263. (defun orgstruct++-mode (&optional arg)
  6264. "Toggle `orgstruct-mode', the enhanced version of it.
  6265. In addition to setting orgstruct-mode, this also exports all indentation and
  6266. autofilling variables from org-mode into the buffer. It will also
  6267. recognize item context in multiline items.
  6268. Note that turning off orgstruct-mode will *not* remove the
  6269. indentation/paragraph settings. This can only be done by refreshing the
  6270. major mode, for example with \[normal-mode]."
  6271. (interactive "P")
  6272. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6273. (if (< arg 1)
  6274. (orgstruct-mode -1)
  6275. (orgstruct-mode 1)
  6276. (let (var val)
  6277. (mapc
  6278. (lambda (x)
  6279. (when (string-match
  6280. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6281. (symbol-name (car x)))
  6282. (setq var (car x) val (nth 1 x))
  6283. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6284. org-local-vars)
  6285. (org-set-local 'orgstruct-is-++ t))))
  6286. (defvar orgstruct-is-++ nil
  6287. "Is orgstruct-mode in ++ version in the current-buffer?")
  6288. (make-variable-buffer-local 'orgstruct-is-++)
  6289. ;;;###autoload
  6290. (defun turn-on-orgstruct++ ()
  6291. "Unconditionally turn on `orgstruct++-mode'."
  6292. (orgstruct++-mode 1))
  6293. (defun orgstruct-error ()
  6294. "Error when there is no default binding for a structure key."
  6295. (interactive)
  6296. (error "This key has no function outside structure elements"))
  6297. (defun orgstruct-setup ()
  6298. "Setup orgstruct keymaps."
  6299. (let ((nfunc 0)
  6300. (bindings
  6301. (list
  6302. '([(meta up)] org-metaup)
  6303. '([(meta down)] org-metadown)
  6304. '([(meta left)] org-metaleft)
  6305. '([(meta right)] org-metaright)
  6306. '([(meta shift up)] org-shiftmetaup)
  6307. '([(meta shift down)] org-shiftmetadown)
  6308. '([(meta shift left)] org-shiftmetaleft)
  6309. '([(meta shift right)] org-shiftmetaright)
  6310. '([?\e (up)] org-metaup)
  6311. '([?\e (down)] org-metadown)
  6312. '([?\e (left)] org-metaleft)
  6313. '([?\e (right)] org-metaright)
  6314. '([?\e (shift up)] org-shiftmetaup)
  6315. '([?\e (shift down)] org-shiftmetadown)
  6316. '([?\e (shift left)] org-shiftmetaleft)
  6317. '([?\e (shift right)] org-shiftmetaright)
  6318. '([(shift up)] org-shiftup)
  6319. '([(shift down)] org-shiftdown)
  6320. '([(shift left)] org-shiftleft)
  6321. '([(shift right)] org-shiftright)
  6322. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6323. '("\M-q" fill-paragraph)
  6324. '("\C-c^" org-sort)
  6325. '("\C-c-" org-cycle-list-bullet)))
  6326. elt key fun cmd)
  6327. (while (setq elt (pop bindings))
  6328. (setq nfunc (1+ nfunc))
  6329. (setq key (org-key (car elt))
  6330. fun (nth 1 elt)
  6331. cmd (orgstruct-make-binding fun nfunc key))
  6332. (org-defkey orgstruct-mode-map key cmd))
  6333. ;; Special treatment needed for TAB and RET
  6334. (org-defkey orgstruct-mode-map [(tab)]
  6335. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6336. (org-defkey orgstruct-mode-map "\C-i"
  6337. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6338. (org-defkey orgstruct-mode-map "\M-\C-m"
  6339. (orgstruct-make-binding 'org-insert-heading 105
  6340. "\M-\C-m" [(meta return)]))
  6341. (org-defkey orgstruct-mode-map [(meta return)]
  6342. (orgstruct-make-binding 'org-insert-heading 106
  6343. [(meta return)] "\M-\C-m"))
  6344. (org-defkey orgstruct-mode-map [(shift meta return)]
  6345. (orgstruct-make-binding 'org-insert-todo-heading 107
  6346. [(meta return)] "\M-\C-m"))
  6347. (org-defkey orgstruct-mode-map "\e\C-m"
  6348. (orgstruct-make-binding 'org-insert-heading 108
  6349. "\e\C-m" [?\e (return)]))
  6350. (org-defkey orgstruct-mode-map [?\e (return)]
  6351. (orgstruct-make-binding 'org-insert-heading 109
  6352. [?\e (return)] "\e\C-m"))
  6353. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6354. (orgstruct-make-binding 'org-insert-todo-heading 110
  6355. [?\e (return)] "\e\C-m"))
  6356. (unless org-local-vars
  6357. (setq org-local-vars (org-get-local-variables)))
  6358. t))
  6359. (defun orgstruct-make-binding (fun n &rest keys)
  6360. "Create a function for binding in the structure minor mode.
  6361. FUN is the command to call inside a table. N is used to create a unique
  6362. command name. KEYS are keys that should be checked in for a command
  6363. to execute outside of tables."
  6364. (eval
  6365. (list 'defun
  6366. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6367. '(arg)
  6368. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6369. "Outside of structure, run the binding of `"
  6370. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6371. "'.")
  6372. '(interactive "p")
  6373. (list 'if
  6374. `(org-context-p 'headline 'item
  6375. (and orgstruct-is-++
  6376. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6377. 'item-body))
  6378. (list 'org-run-like-in-org-mode (list 'quote fun))
  6379. (list 'let '(orgstruct-mode)
  6380. (list 'call-interactively
  6381. (append '(or)
  6382. (mapcar (lambda (k)
  6383. (list 'key-binding k))
  6384. keys)
  6385. '('orgstruct-error))))))))
  6386. (defun org-context-p (&rest contexts)
  6387. "Check if local context is any of CONTEXTS.
  6388. Possible values in the list of contexts are `table', `headline', and `item'."
  6389. (let ((pos (point)))
  6390. (goto-char (point-at-bol))
  6391. (prog1 (or (and (memq 'table contexts)
  6392. (looking-at "[ \t]*|"))
  6393. (and (memq 'headline contexts)
  6394. ;;????????? (looking-at "\\*+"))
  6395. (looking-at outline-regexp))
  6396. (and (memq 'item contexts)
  6397. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6398. (and (memq 'item-body contexts)
  6399. (org-in-item-p)))
  6400. (goto-char pos))))
  6401. (defun org-get-local-variables ()
  6402. "Return a list of all local variables in an org-mode buffer."
  6403. (let (varlist)
  6404. (with-current-buffer (get-buffer-create "*Org tmp*")
  6405. (erase-buffer)
  6406. (org-mode)
  6407. (setq varlist (buffer-local-variables)))
  6408. (kill-buffer "*Org tmp*")
  6409. (delq nil
  6410. (mapcar
  6411. (lambda (x)
  6412. (setq x
  6413. (if (symbolp x)
  6414. (list x)
  6415. (list (car x) (list 'quote (cdr x)))))
  6416. (if (string-match
  6417. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6418. (symbol-name (car x)))
  6419. x nil))
  6420. varlist))))
  6421. ;;;###autoload
  6422. (defun org-run-like-in-org-mode (cmd)
  6423. "Run a command, pretending that the current buffer is in Org-mode.
  6424. This will temporarily bind local variables that are typically bound in
  6425. Org-mode to the values they have in Org-mode, and then interactively
  6426. call CMD."
  6427. (org-load-modules-maybe)
  6428. (unless org-local-vars
  6429. (setq org-local-vars (org-get-local-variables)))
  6430. (eval (list 'let org-local-vars
  6431. (list 'call-interactively (list 'quote cmd)))))
  6432. ;;;; Archiving
  6433. (defun org-get-category (&optional pos)
  6434. "Get the category applying to position POS."
  6435. (get-text-property (or pos (point)) 'org-category))
  6436. (defun org-refresh-category-properties ()
  6437. "Refresh category text properties in the buffer."
  6438. (let ((def-cat (cond
  6439. ((null org-category)
  6440. (if buffer-file-name
  6441. (file-name-sans-extension
  6442. (file-name-nondirectory buffer-file-name))
  6443. "???"))
  6444. ((symbolp org-category) (symbol-name org-category))
  6445. (t org-category)))
  6446. beg end cat pos optionp)
  6447. (org-unmodified
  6448. (save-excursion
  6449. (save-restriction
  6450. (widen)
  6451. (goto-char (point-min))
  6452. (put-text-property (point) (point-max) 'org-category def-cat)
  6453. (while (re-search-forward
  6454. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6455. (setq pos (match-end 0)
  6456. optionp (equal (char-after (match-beginning 0)) ?#)
  6457. cat (org-trim (match-string 2)))
  6458. (if optionp
  6459. (setq beg (point-at-bol) end (point-max))
  6460. (org-back-to-heading t)
  6461. (setq beg (point) end (org-end-of-subtree t t)))
  6462. (put-text-property beg end 'org-category cat)
  6463. (goto-char pos)))))))
  6464. ;;;; Link Stuff
  6465. ;;; Link abbreviations
  6466. (defun org-link-expand-abbrev (link)
  6467. "Apply replacements as defined in `org-link-abbrev-alist."
  6468. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6469. (let* ((key (match-string 1 link))
  6470. (as (or (assoc key org-link-abbrev-alist-local)
  6471. (assoc key org-link-abbrev-alist)))
  6472. (tag (and (match-end 2) (match-string 3 link)))
  6473. rpl)
  6474. (if (not as)
  6475. link
  6476. (setq rpl (cdr as))
  6477. (cond
  6478. ((symbolp rpl) (funcall rpl tag))
  6479. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6480. ((string-match "%h" rpl)
  6481. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6482. (t (concat rpl tag)))))
  6483. link))
  6484. ;;; Storing and inserting links
  6485. (defvar org-insert-link-history nil
  6486. "Minibuffer history for links inserted with `org-insert-link'.")
  6487. (defvar org-stored-links nil
  6488. "Contains the links stored with `org-store-link'.")
  6489. (defvar org-store-link-plist nil
  6490. "Plist with info about the most recently link created with `org-store-link'.")
  6491. (defvar org-link-protocols nil
  6492. "Link protocols added to Org-mode using `org-add-link-type'.")
  6493. (defvar org-store-link-functions nil
  6494. "List of functions that are called to create and store a link.
  6495. Each function will be called in turn until one returns a non-nil
  6496. value. Each function should check if it is responsible for creating
  6497. this link (for example by looking at the major mode).
  6498. If not, it must exit and return nil.
  6499. If yes, it should return a non-nil value after a calling
  6500. `org-store-link-props' with a list of properties and values.
  6501. Special properties are:
  6502. :type The link prefix. like \"http\". This must be given.
  6503. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6504. This is obligatory as well.
  6505. :description Optional default description for the second pair
  6506. of brackets in an Org-mode link. The user can still change
  6507. this when inserting this link into an Org-mode buffer.
  6508. In addition to these, any additional properties can be specified
  6509. and then used in remember templates.")
  6510. (defun org-add-link-type (type &optional follow export)
  6511. "Add TYPE to the list of `org-link-types'.
  6512. Re-compute all regular expressions depending on `org-link-types'
  6513. FOLLOW and EXPORT are two functions.
  6514. FOLLOW should take the link path as the single argument and do whatever
  6515. is necessary to follow the link, for example find a file or display
  6516. a mail message.
  6517. EXPORT should format the link path for export to one of the export formats.
  6518. It should be a function accepting three arguments:
  6519. path the path of the link, the text after the prefix (like \"http:\")
  6520. desc the description of the link, if any, nil if there was no description
  6521. format the export format, a symbol like `html' or `latex'.
  6522. The function may use the FORMAT information to return different values
  6523. depending on the format. The return value will be put literally into
  6524. the exported file.
  6525. Org-mode has a built-in default for exporting links. If you are happy with
  6526. this default, there is no need to define an export function for the link
  6527. type. For a simple example of an export function, see `org-bbdb.el'."
  6528. (add-to-list 'org-link-types type t)
  6529. (org-make-link-regexps)
  6530. (if (assoc type org-link-protocols)
  6531. (setcdr (assoc type org-link-protocols) (list follow export))
  6532. (push (list type follow export) org-link-protocols)))
  6533. ;;;###autoload
  6534. (defun org-store-link (arg)
  6535. "\\<org-mode-map>Store an org-link to the current location.
  6536. This link is added to `org-stored-links' and can later be inserted
  6537. into an org-buffer with \\[org-insert-link].
  6538. For some link types, a prefix arg is interpreted:
  6539. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6540. For file links, arg negates `org-context-in-file-links'."
  6541. (interactive "P")
  6542. (org-load-modules-maybe)
  6543. (setq org-store-link-plist nil) ; reset
  6544. (let (link cpltxt desc description search txt custom-id)
  6545. (cond
  6546. ((run-hook-with-args-until-success 'org-store-link-functions)
  6547. (setq link (plist-get org-store-link-plist :link)
  6548. desc (or (plist-get org-store-link-plist :description) link)))
  6549. ((equal (buffer-name) "*Org Edit Src Example*")
  6550. (let (label gc)
  6551. (while (or (not label)
  6552. (save-excursion
  6553. (save-restriction
  6554. (widen)
  6555. (goto-char (point-min))
  6556. (re-search-forward
  6557. (regexp-quote (format org-coderef-label-format label))
  6558. nil t))))
  6559. (when label (message "Label exists already") (sit-for 2))
  6560. (setq label (read-string "Code line label: " label)))
  6561. (end-of-line 1)
  6562. (setq link (format org-coderef-label-format label))
  6563. (setq gc (- 79 (length link)))
  6564. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6565. (insert link)
  6566. (setq link (concat "(" label ")") desc nil)))
  6567. ((eq major-mode 'calendar-mode)
  6568. (let ((cd (calendar-cursor-to-date)))
  6569. (setq link
  6570. (format-time-string
  6571. (car org-time-stamp-formats)
  6572. (apply 'encode-time
  6573. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6574. nil nil nil))))
  6575. (org-store-link-props :type "calendar" :date cd)))
  6576. ((eq major-mode 'w3-mode)
  6577. (setq cpltxt (if (and (buffer-name)
  6578. (not (string-match "Untitled" (buffer-name))))
  6579. (buffer-name)
  6580. (url-view-url t))
  6581. link (org-make-link (url-view-url t)))
  6582. (org-store-link-props :type "w3" :url (url-view-url t)))
  6583. ((eq major-mode 'w3m-mode)
  6584. (setq cpltxt (or w3m-current-title w3m-current-url)
  6585. link (org-make-link w3m-current-url))
  6586. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6587. ((setq search (run-hook-with-args-until-success
  6588. 'org-create-file-search-functions))
  6589. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6590. "::" search))
  6591. (setq cpltxt (or description link)))
  6592. ((eq major-mode 'image-mode)
  6593. (setq cpltxt (concat "file:"
  6594. (abbreviate-file-name buffer-file-name))
  6595. link (org-make-link cpltxt))
  6596. (org-store-link-props :type "image" :file buffer-file-name))
  6597. ((eq major-mode 'dired-mode)
  6598. ;; link to the file in the current line
  6599. (setq cpltxt (concat "file:"
  6600. (abbreviate-file-name
  6601. (expand-file-name
  6602. (dired-get-filename nil t))))
  6603. link (org-make-link cpltxt)))
  6604. ((and buffer-file-name (org-mode-p))
  6605. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6606. (cond
  6607. ((org-in-regexp "<<\\(.*?\\)>>")
  6608. (setq cpltxt
  6609. (concat "file:"
  6610. (abbreviate-file-name buffer-file-name)
  6611. "::" (match-string 1))
  6612. link (org-make-link cpltxt)))
  6613. ((and (featurep 'org-id)
  6614. (or (eq org-link-to-org-use-id t)
  6615. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6616. (interactive-p))
  6617. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6618. (interactive-p)
  6619. (not custom-id))
  6620. (and org-link-to-org-use-id
  6621. (condition-case nil
  6622. (org-entry-get nil "ID")
  6623. (error nil)))))
  6624. ;; We can make a link using the ID.
  6625. (setq link (condition-case nil
  6626. (prog1 (org-id-store-link)
  6627. (setq desc (plist-get org-store-link-plist
  6628. :description)))
  6629. (error
  6630. ;; probably before first headline, link to file only
  6631. (concat "file:"
  6632. (abbreviate-file-name buffer-file-name))))))
  6633. (t
  6634. ;; Just link to current headline
  6635. (setq cpltxt (concat "file:"
  6636. (abbreviate-file-name buffer-file-name)))
  6637. ;; Add a context search string
  6638. (when (org-xor org-context-in-file-links arg)
  6639. (setq txt (cond
  6640. ((org-on-heading-p) nil)
  6641. ((org-region-active-p)
  6642. (buffer-substring (region-beginning) (region-end)))
  6643. (t nil)))
  6644. (when (or (null txt) (string-match "\\S-" txt))
  6645. (setq cpltxt
  6646. (concat cpltxt "::"
  6647. (condition-case nil
  6648. (org-make-org-heading-search-string txt)
  6649. (error "")))
  6650. desc "NONE")))
  6651. (if (string-match "::\\'" cpltxt)
  6652. (setq cpltxt (substring cpltxt 0 -2)))
  6653. (setq link (org-make-link cpltxt)))))
  6654. ((buffer-file-name (buffer-base-buffer))
  6655. ;; Just link to this file here.
  6656. (setq cpltxt (concat "file:"
  6657. (abbreviate-file-name
  6658. (buffer-file-name (buffer-base-buffer)))))
  6659. ;; Add a context string
  6660. (when (org-xor org-context-in-file-links arg)
  6661. (setq txt (if (org-region-active-p)
  6662. (buffer-substring (region-beginning) (region-end))
  6663. (buffer-substring (point-at-bol) (point-at-eol))))
  6664. ;; Only use search option if there is some text.
  6665. (when (string-match "\\S-" txt)
  6666. (setq cpltxt
  6667. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6668. desc "NONE")))
  6669. (setq link (org-make-link cpltxt)))
  6670. ((interactive-p)
  6671. (error "Cannot link to a buffer which is not visiting a file"))
  6672. (t (setq link nil)))
  6673. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6674. (setq link (or link cpltxt)
  6675. desc (or desc cpltxt))
  6676. (if (equal desc "NONE") (setq desc nil))
  6677. (if (and (interactive-p) link)
  6678. (progn
  6679. (setq org-stored-links
  6680. (cons (list link desc) org-stored-links))
  6681. (message "Stored: %s" (or desc link))
  6682. (when custom-id
  6683. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6684. "::#" custom-id))
  6685. (setq org-stored-links
  6686. (cons (list link desc) org-stored-links))))
  6687. (and link (org-make-link-string link desc)))))
  6688. (defun org-store-link-props (&rest plist)
  6689. "Store link properties, extract names and addresses."
  6690. (let (x adr)
  6691. (when (setq x (plist-get plist :from))
  6692. (setq adr (mail-extract-address-components x))
  6693. (setq plist (plist-put plist :fromname (car adr)))
  6694. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6695. (when (setq x (plist-get plist :to))
  6696. (setq adr (mail-extract-address-components x))
  6697. (setq plist (plist-put plist :toname (car adr)))
  6698. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6699. (let ((from (plist-get plist :from))
  6700. (to (plist-get plist :to)))
  6701. (when (and from to org-from-is-user-regexp)
  6702. (setq plist
  6703. (plist-put plist :fromto
  6704. (if (string-match org-from-is-user-regexp from)
  6705. (concat "to %t")
  6706. (concat "from %f"))))))
  6707. (setq org-store-link-plist plist))
  6708. (defun org-add-link-props (&rest plist)
  6709. "Add these properties to the link property list."
  6710. (let (key value)
  6711. (while plist
  6712. (setq key (pop plist) value (pop plist))
  6713. (setq org-store-link-plist
  6714. (plist-put org-store-link-plist key value)))))
  6715. (defun org-email-link-description (&optional fmt)
  6716. "Return the description part of an email link.
  6717. This takes information from `org-store-link-plist' and formats it
  6718. according to FMT (default from `org-email-link-description-format')."
  6719. (setq fmt (or fmt org-email-link-description-format))
  6720. (let* ((p org-store-link-plist)
  6721. (to (plist-get p :toaddress))
  6722. (from (plist-get p :fromaddress))
  6723. (table
  6724. (list
  6725. (cons "%c" (plist-get p :fromto))
  6726. (cons "%F" (plist-get p :from))
  6727. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6728. (cons "%T" (plist-get p :to))
  6729. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6730. (cons "%s" (plist-get p :subject))
  6731. (cons "%m" (plist-get p :message-id)))))
  6732. (when (string-match "%c" fmt)
  6733. ;; Check if the user wrote this message
  6734. (if (and org-from-is-user-regexp from to
  6735. (save-match-data (string-match org-from-is-user-regexp from)))
  6736. (setq fmt (replace-match "to %t" t t fmt))
  6737. (setq fmt (replace-match "from %f" t t fmt))))
  6738. (org-replace-escapes fmt table)))
  6739. (defun org-make-org-heading-search-string (&optional string heading)
  6740. "Make search string for STRING or current headline."
  6741. (interactive)
  6742. (let ((s (or string (org-get-heading))))
  6743. (unless (and string (not heading))
  6744. ;; We are using a headline, clean up garbage in there.
  6745. (if (string-match org-todo-regexp s)
  6746. (setq s (replace-match "" t t s)))
  6747. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6748. (setq s (replace-match "" t t s)))
  6749. (setq s (org-trim s))
  6750. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6751. org-comment-string "\\)") s)
  6752. (setq s (replace-match "" t t s)))
  6753. (while (string-match org-ts-regexp s)
  6754. (setq s (replace-match "" t t s))))
  6755. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6756. (setq s (replace-match " " t t s)))
  6757. (or string (setq s (concat "*" s))) ; Add * for headlines
  6758. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6759. (defun org-make-link (&rest strings)
  6760. "Concatenate STRINGS."
  6761. (apply 'concat strings))
  6762. (defun org-make-link-string (link &optional description)
  6763. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6764. (unless (string-match "\\S-" link)
  6765. (error "Empty link"))
  6766. (when (stringp description)
  6767. ;; Remove brackets from the description, they are fatal.
  6768. (while (string-match "\\[" description)
  6769. (setq description (replace-match "{" t t description)))
  6770. (while (string-match "\\]" description)
  6771. (setq description (replace-match "}" t t description))))
  6772. (when (equal (org-link-escape link) description)
  6773. ;; No description needed, it is identical
  6774. (setq description nil))
  6775. (when (and (not description)
  6776. (not (equal link (org-link-escape link))))
  6777. (setq description (org-extract-attributes link)))
  6778. (concat "[[" (org-link-escape link) "]"
  6779. (if description (concat "[" description "]") "")
  6780. "]"))
  6781. (defconst org-link-escape-chars
  6782. '((?\ . "%20")
  6783. (?\[ . "%5B")
  6784. (?\] . "%5D")
  6785. (?\340 . "%E0") ; `a
  6786. (?\342 . "%E2") ; ^a
  6787. (?\347 . "%E7") ; ,c
  6788. (?\350 . "%E8") ; `e
  6789. (?\351 . "%E9") ; 'e
  6790. (?\352 . "%EA") ; ^e
  6791. (?\356 . "%EE") ; ^i
  6792. (?\364 . "%F4") ; ^o
  6793. (?\371 . "%F9") ; `u
  6794. (?\373 . "%FB") ; ^u
  6795. (?\; . "%3B")
  6796. (?? . "%3F")
  6797. (?= . "%3D")
  6798. (?+ . "%2B")
  6799. )
  6800. "Association list of escapes for some characters problematic in links.
  6801. This is the list that is used for internal purposes.")
  6802. (defvar org-url-encoding-use-url-hexify nil)
  6803. (defconst org-link-escape-chars-browser
  6804. '((?\ . "%20")) ; 32 for the SPC char
  6805. "Association list of escapes for some characters problematic in links.
  6806. This is the list that is used before handing over to the browser.")
  6807. (defun org-link-escape (text &optional table)
  6808. "Escape characters in TEXT that are problematic for links."
  6809. (if org-url-encoding-use-url-hexify
  6810. (url-hexify-string text)
  6811. (setq table (or table org-link-escape-chars))
  6812. (when text
  6813. (let ((re (mapconcat (lambda (x) (regexp-quote
  6814. (char-to-string (car x))))
  6815. table "\\|")))
  6816. (while (string-match re text)
  6817. (setq text
  6818. (replace-match
  6819. (cdr (assoc (string-to-char (match-string 0 text))
  6820. table))
  6821. t t text)))
  6822. text))))
  6823. (defun org-link-unescape (text &optional table)
  6824. "Reverse the action of `org-link-escape'."
  6825. (if org-url-encoding-use-url-hexify
  6826. (url-unhex-string text)
  6827. (setq table (or table org-link-escape-chars))
  6828. (when text
  6829. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6830. table "\\|")))
  6831. (while (string-match re text)
  6832. (setq text
  6833. (replace-match
  6834. (char-to-string (car (rassoc (match-string 0 text) table)))
  6835. t t text)))
  6836. text))))
  6837. (defun org-xor (a b)
  6838. "Exclusive or."
  6839. (if a (not b) b))
  6840. (defun org-fixup-message-id-for-http (s)
  6841. "Replace special characters in a message id, so it can be used in an http query."
  6842. (while (string-match "<" s)
  6843. (setq s (replace-match "%3C" t t s)))
  6844. (while (string-match ">" s)
  6845. (setq s (replace-match "%3E" t t s)))
  6846. (while (string-match "@" s)
  6847. (setq s (replace-match "%40" t t s)))
  6848. s)
  6849. ;;;###autoload
  6850. (defun org-insert-link-global ()
  6851. "Insert a link like Org-mode does.
  6852. This command can be called in any mode to insert a link in Org-mode syntax."
  6853. (interactive)
  6854. (org-load-modules-maybe)
  6855. (org-run-like-in-org-mode 'org-insert-link))
  6856. (defun org-insert-link (&optional complete-file link-location)
  6857. "Insert a link. At the prompt, enter the link.
  6858. Completion can be used to insert any of the link protocol prefixes like
  6859. http or ftp in use.
  6860. The history can be used to select a link previously stored with
  6861. `org-store-link'. When the empty string is entered (i.e. if you just
  6862. press RET at the prompt), the link defaults to the most recently
  6863. stored link. As SPC triggers completion in the minibuffer, you need to
  6864. use M-SPC or C-q SPC to force the insertion of a space character.
  6865. You will also be prompted for a description, and if one is given, it will
  6866. be displayed in the buffer instead of the link.
  6867. If there is already a link at point, this command will allow you to edit link
  6868. and description parts.
  6869. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6870. be selected using completion. The path to the file will be relative to the
  6871. current directory if the file is in the current directory or a subdirectory.
  6872. Otherwise, the link will be the absolute path as completed in the minibuffer
  6873. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6874. option `org-link-file-path-type'.
  6875. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6876. the current directory or below.
  6877. With three \\[universal-argument] prefixes, negate the meaning of
  6878. `org-keep-stored-link-after-insertion'.
  6879. If `org-make-link-description-function' is non-nil, this function will be
  6880. called with the link target, and the result will be the default
  6881. link description.
  6882. If the LINK-LOCATION parameter is non-nil, this value will be
  6883. used as the link location instead of reading one interactively."
  6884. (interactive "P")
  6885. (let* ((wcf (current-window-configuration))
  6886. (region (if (org-region-active-p)
  6887. (buffer-substring (region-beginning) (region-end))))
  6888. (remove (and region (list (region-beginning) (region-end))))
  6889. (desc region)
  6890. tmphist ; byte-compile incorrectly complains about this
  6891. (link link-location)
  6892. entry file all-prefixes)
  6893. (cond
  6894. (link-location) ; specified by arg, just use it.
  6895. ((org-in-regexp org-bracket-link-regexp 1)
  6896. ;; We do have a link at point, and we are going to edit it.
  6897. (setq remove (list (match-beginning 0) (match-end 0)))
  6898. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6899. (setq link (read-string "Link: "
  6900. (org-link-unescape
  6901. (org-match-string-no-properties 1)))))
  6902. ((or (org-in-regexp org-angle-link-re)
  6903. (org-in-regexp org-plain-link-re))
  6904. ;; Convert to bracket link
  6905. (setq remove (list (match-beginning 0) (match-end 0))
  6906. link (read-string "Link: "
  6907. (org-remove-angle-brackets (match-string 0)))))
  6908. ((member complete-file '((4) (16)))
  6909. ;; Completing read for file names.
  6910. (setq link (org-file-complete-link complete-file)))
  6911. (t
  6912. ;; Read link, with completion for stored links.
  6913. (with-output-to-temp-buffer "*Org Links*"
  6914. (princ "Insert a link.
  6915. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6916. (when org-stored-links
  6917. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6918. (princ (mapconcat
  6919. (lambda (x)
  6920. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6921. (reverse org-stored-links) "\n"))))
  6922. (let ((cw (selected-window)))
  6923. (select-window (get-buffer-window "*Org Links*"))
  6924. (setq truncate-lines t)
  6925. (unless (pos-visible-in-window-p (point-max))
  6926. (org-fit-window-to-buffer))
  6927. (and (window-live-p cw) (select-window cw)))
  6928. ;; Fake a link history, containing the stored links.
  6929. (setq tmphist (append (mapcar 'car org-stored-links)
  6930. org-insert-link-history))
  6931. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6932. (mapcar 'car org-link-abbrev-alist)
  6933. org-link-types))
  6934. (unwind-protect
  6935. (progn
  6936. (setq link
  6937. (let ((org-completion-use-ido nil))
  6938. (org-completing-read
  6939. "Link: "
  6940. (mapcar (lambda (x) (list (concat x ":")))
  6941. all-prefixes)
  6942. nil nil nil
  6943. 'tmphist
  6944. (or (car (car org-stored-links))))))
  6945. (if (or (member link all-prefixes)
  6946. (and (equal ":" (substring link -1))
  6947. (member (substring link 0 -1) all-prefixes)
  6948. (setq link (substring link 0 -1))))
  6949. (setq link (org-link-try-special-completion link))))
  6950. (set-window-configuration wcf)
  6951. (kill-buffer "*Org Links*"))
  6952. (setq entry (assoc link org-stored-links))
  6953. (or entry (push link org-insert-link-history))
  6954. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6955. (not org-keep-stored-link-after-insertion))
  6956. (setq org-stored-links (delq (assoc link org-stored-links)
  6957. org-stored-links)))
  6958. (setq desc (or desc (nth 1 entry)))))
  6959. (if (string-match org-plain-link-re link)
  6960. ;; URL-like link, normalize the use of angular brackets.
  6961. (setq link (org-make-link (org-remove-angle-brackets link))))
  6962. ;; Check if we are linking to the current file with a search option
  6963. ;; If yes, simplify the link by using only the search option.
  6964. (when (and buffer-file-name
  6965. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6966. (let* ((path (match-string 1 link))
  6967. (case-fold-search nil)
  6968. (search (match-string 2 link)))
  6969. (save-match-data
  6970. (if (equal (file-truename buffer-file-name) (file-truename path))
  6971. ;; We are linking to this same file, with a search option
  6972. (setq link search)))))
  6973. ;; Check if we can/should use a relative path. If yes, simplify the link
  6974. (when (string-match "^file:\\(.*\\)" link)
  6975. (let* ((path (match-string 1 link))
  6976. (origpath path)
  6977. (case-fold-search nil))
  6978. (cond
  6979. ((or (eq org-link-file-path-type 'absolute)
  6980. (equal complete-file '(16)))
  6981. (setq path (abbreviate-file-name (expand-file-name path))))
  6982. ((eq org-link-file-path-type 'noabbrev)
  6983. (setq path (expand-file-name path)))
  6984. ((eq org-link-file-path-type 'relative)
  6985. (setq path (file-relative-name path)))
  6986. (t
  6987. (save-match-data
  6988. (if (string-match (concat "^" (regexp-quote
  6989. (file-name-as-directory
  6990. (expand-file-name "."))))
  6991. (expand-file-name path))
  6992. ;; We are linking a file with relative path name.
  6993. (setq path (substring (expand-file-name path)
  6994. (match-end 0)))
  6995. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6996. (setq link (concat "file:" path))
  6997. (if (equal desc origpath)
  6998. (setq desc path))))
  6999. (if org-make-link-description-function
  7000. (setq desc (funcall org-make-link-description-function link desc)))
  7001. (setq desc (read-string "Description: " desc))
  7002. (unless (string-match "\\S-" desc) (setq desc nil))
  7003. (if remove (apply 'delete-region remove))
  7004. (insert (org-make-link-string link desc))))
  7005. (defun org-link-try-special-completion (type)
  7006. "If there is completion support for link type TAPE, offer it."
  7007. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7008. (if (functionp fun)
  7009. (funcall fun)
  7010. (read-string "Link (no completion support): " (concat type ":")))))
  7011. (defun org-file-complete-link (&optional arg)
  7012. "Create a file link using completion."
  7013. (let (file link)
  7014. (setq file (read-file-name "File: "))
  7015. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7016. (pwd1 (file-name-as-directory (abbreviate-file-name
  7017. (expand-file-name ".")))))
  7018. (cond
  7019. ((equal arg '(16))
  7020. (setq link (org-make-link
  7021. "file:"
  7022. (abbreviate-file-name (expand-file-name file)))))
  7023. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7024. (setq link (org-make-link "file:" (match-string 1 file))))
  7025. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7026. (expand-file-name file))
  7027. (setq link (org-make-link
  7028. "file:" (match-string 1 (expand-file-name file)))))
  7029. (t (setq link (org-make-link "file:" file)))))
  7030. link))
  7031. (defun org-completing-read (&rest args)
  7032. "Completing-read with SPACE being a normal character."
  7033. (let ((minibuffer-local-completion-map
  7034. (copy-keymap minibuffer-local-completion-map)))
  7035. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7036. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7037. (apply 'org-ido-completing-read args)))
  7038. (defun org-completing-read-no-ido (&rest args)
  7039. (let (org-completion-use-ido)
  7040. (apply 'org-completing-read args)))
  7041. (defun org-ido-completing-read (&rest args)
  7042. "Completing-read using `ido-mode' speedups if available"
  7043. (if (and org-completion-use-ido
  7044. (fboundp 'ido-completing-read)
  7045. (boundp 'ido-mode) ido-mode
  7046. (listp (second args)))
  7047. (let ((ido-enter-matching-directory nil))
  7048. (apply 'ido-completing-read (concat (car args))
  7049. (mapcar (lambda (x) (car x)) (nth 1 args))
  7050. (cddr args)))
  7051. (apply 'completing-read args)))
  7052. (defun org-extract-attributes (s)
  7053. "Extract the attributes cookie from a string and set as text property."
  7054. (let (a attr (start 0) key value)
  7055. (save-match-data
  7056. (when (string-match "{{\\([^}]+\\)}}$" s)
  7057. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7058. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7059. (setq key (match-string 1 a) value (match-string 2 a)
  7060. start (match-end 0)
  7061. attr (plist-put attr (intern key) value))))
  7062. (org-add-props s nil 'org-attr attr))
  7063. s))
  7064. (defun org-extract-attributes-from-string (tag)
  7065. (let (key value attr)
  7066. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7067. (setq key (match-string 1 tag) value (match-string 2 tag)
  7068. tag (replace-match "" t t tag)
  7069. attr (plist-put attr (intern key) value)))
  7070. (cons tag attr)))
  7071. (defun org-attributes-to-string (plist)
  7072. "Format a property list into an HTML attribute list."
  7073. (let ((s "") key value)
  7074. (while plist
  7075. (setq key (pop plist) value (pop plist))
  7076. (and value
  7077. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7078. s))
  7079. ;;; Opening/following a link
  7080. (defvar org-link-search-failed nil)
  7081. (defun org-next-link ()
  7082. "Move forward to the next link.
  7083. If the link is in hidden text, expose it."
  7084. (interactive)
  7085. (when (and org-link-search-failed (eq this-command last-command))
  7086. (goto-char (point-min))
  7087. (message "Link search wrapped back to beginning of buffer"))
  7088. (setq org-link-search-failed nil)
  7089. (let* ((pos (point))
  7090. (ct (org-context))
  7091. (a (assoc :link ct)))
  7092. (if a (goto-char (nth 2 a)))
  7093. (if (re-search-forward org-any-link-re nil t)
  7094. (progn
  7095. (goto-char (match-beginning 0))
  7096. (if (org-invisible-p) (org-show-context)))
  7097. (goto-char pos)
  7098. (setq org-link-search-failed t)
  7099. (error "No further link found"))))
  7100. (defun org-previous-link ()
  7101. "Move backward to the previous link.
  7102. If the link is in hidden text, expose it."
  7103. (interactive)
  7104. (when (and org-link-search-failed (eq this-command last-command))
  7105. (goto-char (point-max))
  7106. (message "Link search wrapped back to end of buffer"))
  7107. (setq org-link-search-failed nil)
  7108. (let* ((pos (point))
  7109. (ct (org-context))
  7110. (a (assoc :link ct)))
  7111. (if a (goto-char (nth 1 a)))
  7112. (if (re-search-backward org-any-link-re nil t)
  7113. (progn
  7114. (goto-char (match-beginning 0))
  7115. (if (org-invisible-p) (org-show-context)))
  7116. (goto-char pos)
  7117. (setq org-link-search-failed t)
  7118. (error "No further link found"))))
  7119. (defun org-translate-link (s)
  7120. "Translate a link string if a translation function has been defined."
  7121. (if (and org-link-translation-function
  7122. (fboundp org-link-translation-function)
  7123. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7124. (progn
  7125. (setq s (funcall org-link-translation-function
  7126. (match-string 1) (match-string 2)))
  7127. (concat (car s) ":" (cdr s)))
  7128. s))
  7129. (defun org-translate-link-from-planner (type path)
  7130. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7131. This is still an experimental function, your mileage may vary."
  7132. (cond
  7133. ((member type '("http" "https" "news" "ftp"))
  7134. ;; standard Internet links are the same.
  7135. nil)
  7136. ((and (equal type "irc") (string-match "^//" path))
  7137. ;; Planner has two / at the beginning of an irc link, we have 1.
  7138. ;; We should have zero, actually....
  7139. (setq path (substring path 1)))
  7140. ((and (equal type "lisp") (string-match "^/" path))
  7141. ;; Planner has a slash, we do not.
  7142. (setq type "elisp" path (substring path 1)))
  7143. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7144. ;; A typical message link. Planner has the id after the fina slash,
  7145. ;; we separate it with a hash mark
  7146. (setq path (concat (match-string 1 path) "#"
  7147. (org-remove-angle-brackets (match-string 2 path)))))
  7148. )
  7149. (cons type path))
  7150. (defun org-find-file-at-mouse (ev)
  7151. "Open file link or URL at mouse."
  7152. (interactive "e")
  7153. (mouse-set-point ev)
  7154. (org-open-at-point 'in-emacs))
  7155. (defun org-open-at-mouse (ev)
  7156. "Open file link or URL at mouse."
  7157. (interactive "e")
  7158. (mouse-set-point ev)
  7159. (if (eq major-mode 'org-agenda-mode)
  7160. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7161. (org-open-at-point))
  7162. (defvar org-window-config-before-follow-link nil
  7163. "The window configuration before following a link.
  7164. This is saved in case the need arises to restore it.")
  7165. (defvar org-open-link-marker (make-marker)
  7166. "Marker pointing to the location where `org-open-at-point; was called.")
  7167. ;;;###autoload
  7168. (defun org-open-at-point-global ()
  7169. "Follow a link like Org-mode does.
  7170. This command can be called in any mode to follow a link that has
  7171. Org-mode syntax."
  7172. (interactive)
  7173. (org-run-like-in-org-mode 'org-open-at-point))
  7174. ;;;###autoload
  7175. (defun org-open-link-from-string (s &optional arg)
  7176. "Open a link in the string S, as if it was in Org-mode."
  7177. (interactive "sLink: \nP")
  7178. (let ((reference-buffer (current-buffer)))
  7179. (with-temp-buffer
  7180. (let ((org-inhibit-startup t))
  7181. (org-mode)
  7182. (insert s)
  7183. (goto-char (point-min))
  7184. (org-open-at-point arg reference-buffer)))))
  7185. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7186. "Open link at or after point.
  7187. If there is no link at point, this function will search forward up to
  7188. the end of the current line.
  7189. Normally, files will be opened by an appropriate application. If the
  7190. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7191. With a double prefix argument, try to open outside of Emacs, in the
  7192. application the system uses for this file type."
  7193. (interactive "P")
  7194. (org-load-modules-maybe)
  7195. (move-marker org-open-link-marker (point))
  7196. (setq org-window-config-before-follow-link (current-window-configuration))
  7197. (org-remove-occur-highlights nil nil t)
  7198. (cond
  7199. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7200. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7201. (org-footnote-action))
  7202. (t
  7203. (let (type path link line search (pos (point)))
  7204. (catch 'match
  7205. (save-excursion
  7206. (skip-chars-forward "^]\n\r")
  7207. (when (org-in-regexp org-bracket-link-regexp)
  7208. (setq link (org-extract-attributes
  7209. (org-link-unescape (org-match-string-no-properties 1))))
  7210. (while (string-match " *\n *" link)
  7211. (setq link (replace-match " " t t link)))
  7212. (setq link (org-link-expand-abbrev link))
  7213. (cond
  7214. ((or (file-name-absolute-p link)
  7215. (string-match "^\\.\\.?/" link))
  7216. (setq type "file" path link))
  7217. ((string-match org-link-re-with-space3 link)
  7218. (setq type (match-string 1 link) path (match-string 2 link)))
  7219. (t (setq type "thisfile" path link)))
  7220. (throw 'match t)))
  7221. (when (get-text-property (point) 'org-linked-text)
  7222. (setq type "thisfile"
  7223. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7224. (1+ (point)) (point))
  7225. path (buffer-substring
  7226. (previous-single-property-change pos 'org-linked-text)
  7227. (next-single-property-change pos 'org-linked-text)))
  7228. (throw 'match t))
  7229. (save-excursion
  7230. (when (or (org-in-regexp org-angle-link-re)
  7231. (org-in-regexp org-plain-link-re))
  7232. (setq type (match-string 1) path (match-string 2))
  7233. (throw 'match t)))
  7234. (save-excursion
  7235. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7236. (setq type "tags"
  7237. path (match-string 1))
  7238. (while (string-match ":" path)
  7239. (setq path (replace-match "+" t t path)))
  7240. (throw 'match t)))
  7241. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7242. (setq type "tree-match"
  7243. path (match-string 1))
  7244. (throw 'match t)))
  7245. (unless path
  7246. (error "No link found"))
  7247. ;; switch back to reference buffer
  7248. ;; needed when if called in a temporary buffer through
  7249. ;; org-open-link-from-string
  7250. (and reference-buffer (switch-to-buffer reference-buffer))
  7251. ;; Remove any trailing spaces in path
  7252. (if (string-match " +\\'" path)
  7253. (setq path (replace-match "" t t path)))
  7254. (if (and org-link-translation-function
  7255. (fboundp org-link-translation-function))
  7256. ;; Check if we need to translate the link
  7257. (let ((tmp (funcall org-link-translation-function type path)))
  7258. (setq type (car tmp) path (cdr tmp))))
  7259. (cond
  7260. ((assoc type org-link-protocols)
  7261. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7262. ((equal type "mailto")
  7263. (let ((cmd (car org-link-mailto-program))
  7264. (args (cdr org-link-mailto-program)) args1
  7265. (address path) (subject "") a)
  7266. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7267. (setq address (match-string 1 path)
  7268. subject (org-link-escape (match-string 2 path))))
  7269. (while args
  7270. (cond
  7271. ((not (stringp (car args))) (push (pop args) args1))
  7272. (t (setq a (pop args))
  7273. (if (string-match "%a" a)
  7274. (setq a (replace-match address t t a)))
  7275. (if (string-match "%s" a)
  7276. (setq a (replace-match subject t t a)))
  7277. (push a args1))))
  7278. (apply cmd (nreverse args1))))
  7279. ((member type '("http" "https" "ftp" "news"))
  7280. (browse-url (concat type ":" (org-link-escape
  7281. path org-link-escape-chars-browser))))
  7282. ((member type '("message"))
  7283. (browse-url (concat type ":" path)))
  7284. ((string= type "tags")
  7285. (org-tags-view in-emacs path))
  7286. ((string= type "thisfile")
  7287. (if in-emacs
  7288. (switch-to-buffer-other-window
  7289. (org-get-buffer-for-internal-link (current-buffer)))
  7290. (org-mark-ring-push))
  7291. (let ((cmd `(org-link-search
  7292. ,path
  7293. ,(cond ((equal in-emacs '(4)) 'occur)
  7294. ((equal in-emacs '(16)) 'org-occur)
  7295. (t nil))
  7296. ,pos)))
  7297. (condition-case nil (eval cmd)
  7298. (error (progn (widen) (eval cmd))))))
  7299. ((string= type "tree-match")
  7300. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7301. ((string= type "file")
  7302. (if (string-match "::\\([0-9]+\\)\\'" path)
  7303. (setq line (string-to-number (match-string 1 path))
  7304. path (substring path 0 (match-beginning 0)))
  7305. (if (string-match "::\\(.+\\)\\'" path)
  7306. (setq search (match-string 1 path)
  7307. path (substring path 0 (match-beginning 0)))))
  7308. (if (string-match "[*?{]" (file-name-nondirectory path))
  7309. (dired path)
  7310. (org-open-file path in-emacs line search)))
  7311. ((string= type "news")
  7312. (require 'org-gnus)
  7313. (org-gnus-follow-link path))
  7314. ((string= type "shell")
  7315. (let ((cmd path))
  7316. (if (or (not org-confirm-shell-link-function)
  7317. (funcall org-confirm-shell-link-function
  7318. (format "Execute \"%s\" in shell? "
  7319. (org-add-props cmd nil
  7320. 'face 'org-warning))))
  7321. (progn
  7322. (message "Executing %s" cmd)
  7323. (shell-command cmd))
  7324. (error "Abort"))))
  7325. ((string= type "elisp")
  7326. (let ((cmd path))
  7327. (if (or (not org-confirm-elisp-link-function)
  7328. (funcall org-confirm-elisp-link-function
  7329. (format "Execute \"%s\" as elisp? "
  7330. (org-add-props cmd nil
  7331. 'face 'org-warning))))
  7332. (message "%s => %s" cmd
  7333. (if (equal (string-to-char cmd) ?\()
  7334. (eval (read cmd))
  7335. (call-interactively (read cmd))))
  7336. (error "Abort"))))
  7337. (t
  7338. (browse-url-at-point))))))
  7339. (move-marker org-open-link-marker nil)
  7340. (run-hook-with-args 'org-follow-link-hook))
  7341. ;;;; Time estimates
  7342. (defun org-get-effort (&optional pom)
  7343. "Get the effort estimate for the current entry."
  7344. (org-entry-get pom org-effort-property))
  7345. ;;; File search
  7346. (defvar org-create-file-search-functions nil
  7347. "List of functions to construct the right search string for a file link.
  7348. These functions are called in turn with point at the location to
  7349. which the link should point.
  7350. A function in the hook should first test if it would like to
  7351. handle this file type, for example by checking the major-mode or
  7352. the file extension. If it decides not to handle this file, it
  7353. should just return nil to give other functions a chance. If it
  7354. does handle the file, it must return the search string to be used
  7355. when following the link. The search string will be part of the
  7356. file link, given after a double colon, and `org-open-at-point'
  7357. will automatically search for it. If special measures must be
  7358. taken to make the search successful, another function should be
  7359. added to the companion hook `org-execute-file-search-functions',
  7360. which see.
  7361. A function in this hook may also use `setq' to set the variable
  7362. `description' to provide a suggestion for the descriptive text to
  7363. be used for this link when it gets inserted into an Org-mode
  7364. buffer with \\[org-insert-link].")
  7365. (defvar org-execute-file-search-functions nil
  7366. "List of functions to execute a file search triggered by a link.
  7367. Functions added to this hook must accept a single argument, the
  7368. search string that was part of the file link, the part after the
  7369. double colon. The function must first check if it would like to
  7370. handle this search, for example by checking the major-mode or the
  7371. file extension. If it decides not to handle this search, it
  7372. should just return nil to give other functions a chance. If it
  7373. does handle the search, it must return a non-nil value to keep
  7374. other functions from trying.
  7375. Each function can access the current prefix argument through the
  7376. variable `current-prefix-argument'. Note that a single prefix is
  7377. used to force opening a link in Emacs, so it may be good to only
  7378. use a numeric or double prefix to guide the search function.
  7379. In case this is needed, a function in this hook can also restore
  7380. the window configuration before `org-open-at-point' was called using:
  7381. (set-window-configuration org-window-config-before-follow-link)")
  7382. (defun org-link-search (s &optional type avoid-pos)
  7383. "Search for a link search option.
  7384. If S is surrounded by forward slashes, it is interpreted as a
  7385. regular expression. In org-mode files, this will create an `org-occur'
  7386. sparse tree. In ordinary files, `occur' will be used to list matches.
  7387. If the current buffer is in `dired-mode', grep will be used to search
  7388. in all files. If AVOID-POS is given, ignore matches near that position."
  7389. (let ((case-fold-search t)
  7390. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7391. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7392. (append '(("") (" ") ("\t") ("\n"))
  7393. org-emphasis-alist)
  7394. "\\|") "\\)"))
  7395. (pos (point))
  7396. (pre nil) (post nil)
  7397. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7398. (cond
  7399. ;; First check if there are any special
  7400. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7401. ;; Now try the builtin stuff
  7402. ((and (equal (string-to-char s0) ?#)
  7403. (> (length s0) 1)
  7404. (save-excursion
  7405. (goto-char (point-min))
  7406. (and
  7407. (re-search-forward
  7408. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7409. (setq type 'dedicated
  7410. pos (match-beginning 0))))
  7411. ;; There is an exact target for this
  7412. (goto-char pos)
  7413. (org-back-to-heading t)))
  7414. ((save-excursion
  7415. (goto-char (point-min))
  7416. (and
  7417. (re-search-forward
  7418. (concat "<<" (regexp-quote s0) ">>") nil t)
  7419. (setq type 'dedicated
  7420. pos (match-beginning 0))))
  7421. ;; There is an exact target for this
  7422. (goto-char pos))
  7423. ((and (string-match "^(\\(.*\\))$" s0)
  7424. (save-excursion
  7425. (goto-char (point-min))
  7426. (and
  7427. (re-search-forward
  7428. (concat "[^[]" (regexp-quote
  7429. (format org-coderef-label-format
  7430. (match-string 1 s0))))
  7431. nil t)
  7432. (setq type 'dedicated
  7433. pos (1+ (match-beginning 0))))))
  7434. ;; There is a coderef target for this
  7435. (goto-char pos))
  7436. ((string-match "^/\\(.*\\)/$" s)
  7437. ;; A regular expression
  7438. (cond
  7439. ((org-mode-p)
  7440. (org-occur (match-string 1 s)))
  7441. ;;((eq major-mode 'dired-mode)
  7442. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7443. (t (org-do-occur (match-string 1 s)))))
  7444. (t
  7445. ;; A normal search strings
  7446. (when (equal (string-to-char s) ?*)
  7447. ;; Anchor on headlines, post may include tags.
  7448. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7449. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7450. s (substring s 1)))
  7451. (remove-text-properties
  7452. 0 (length s)
  7453. '(face nil mouse-face nil keymap nil fontified nil) s)
  7454. ;; Make a series of regular expressions to find a match
  7455. (setq words (org-split-string s "[ \n\r\t]+")
  7456. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7457. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7458. "\\)" markers)
  7459. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7460. re2a (concat "[ \t\r\n]" re2a_)
  7461. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7462. re4 (concat "[^a-zA-Z_]" re4_)
  7463. re1 (concat pre re2 post)
  7464. re3 (concat pre (if pre re4_ re4) post)
  7465. re5 (concat pre ".*" re4)
  7466. re2 (concat pre re2)
  7467. re2a (concat pre (if pre re2a_ re2a))
  7468. re4 (concat pre (if pre re4_ re4))
  7469. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7470. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7471. re5 "\\)"
  7472. ))
  7473. (cond
  7474. ((eq type 'org-occur) (org-occur reall))
  7475. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7476. (t (goto-char (point-min))
  7477. (setq type 'fuzzy)
  7478. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7479. (org-search-not-self 1 re1 nil t)
  7480. (org-search-not-self 1 re2 nil t)
  7481. (org-search-not-self 1 re2a nil t)
  7482. (org-search-not-self 1 re3 nil t)
  7483. (org-search-not-self 1 re4 nil t)
  7484. (org-search-not-self 1 re5 nil t)
  7485. )
  7486. (goto-char (match-beginning 1))
  7487. (goto-char pos)
  7488. (error "No match")))))
  7489. (t
  7490. ;; Normal string-search
  7491. (goto-char (point-min))
  7492. (if (search-forward s nil t)
  7493. (goto-char (match-beginning 0))
  7494. (error "No match"))))
  7495. (and (org-mode-p) (org-show-context 'link-search))
  7496. type))
  7497. (defun org-search-not-self (group &rest args)
  7498. "Execute `re-search-forward', but only accept matches that do not
  7499. enclose the position of `org-open-link-marker'."
  7500. (let ((m org-open-link-marker))
  7501. (catch 'exit
  7502. (while (apply 're-search-forward args)
  7503. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7504. (goto-char (match-end group))
  7505. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7506. (> (match-beginning 0) (marker-position m))
  7507. (< (match-end 0) (marker-position m)))
  7508. (save-match-data
  7509. (or (not (org-in-regexp
  7510. org-bracket-link-analytic-regexp 1))
  7511. (not (match-end 4)) ; no description
  7512. (and (<= (match-beginning 4) (point))
  7513. (>= (match-end 4) (point))))))
  7514. (throw 'exit (point))))))))
  7515. (defun org-get-buffer-for-internal-link (buffer)
  7516. "Return a buffer to be used for displaying the link target of internal links."
  7517. (cond
  7518. ((not org-display-internal-link-with-indirect-buffer)
  7519. buffer)
  7520. ((string-match "(Clone)$" (buffer-name buffer))
  7521. (message "Buffer is already a clone, not making another one")
  7522. ;; we also do not modify visibility in this case
  7523. buffer)
  7524. (t ; make a new indirect buffer for displaying the link
  7525. (let* ((bn (buffer-name buffer))
  7526. (ibn (concat bn "(Clone)"))
  7527. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7528. (with-current-buffer ib (org-overview))
  7529. ib))))
  7530. (defun org-do-occur (regexp &optional cleanup)
  7531. "Call the Emacs command `occur'.
  7532. If CLEANUP is non-nil, remove the printout of the regular expression
  7533. in the *Occur* buffer. This is useful if the regex is long and not useful
  7534. to read."
  7535. (occur regexp)
  7536. (when cleanup
  7537. (let ((cwin (selected-window)) win beg end)
  7538. (when (setq win (get-buffer-window "*Occur*"))
  7539. (select-window win))
  7540. (goto-char (point-min))
  7541. (when (re-search-forward "match[a-z]+" nil t)
  7542. (setq beg (match-end 0))
  7543. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7544. (setq end (1- (match-beginning 0)))))
  7545. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7546. (goto-char (point-min))
  7547. (select-window cwin))))
  7548. ;;; The mark ring for links jumps
  7549. (defvar org-mark-ring nil
  7550. "Mark ring for positions before jumps in Org-mode.")
  7551. (defvar org-mark-ring-last-goto nil
  7552. "Last position in the mark ring used to go back.")
  7553. ;; Fill and close the ring
  7554. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7555. (loop for i from 1 to org-mark-ring-length do
  7556. (push (make-marker) org-mark-ring))
  7557. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7558. org-mark-ring)
  7559. (defun org-mark-ring-push (&optional pos buffer)
  7560. "Put the current position or POS into the mark ring and rotate it."
  7561. (interactive)
  7562. (setq pos (or pos (point)))
  7563. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7564. (move-marker (car org-mark-ring)
  7565. (or pos (point))
  7566. (or buffer (current-buffer)))
  7567. (message "%s"
  7568. (substitute-command-keys
  7569. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7570. (defun org-mark-ring-goto (&optional n)
  7571. "Jump to the previous position in the mark ring.
  7572. With prefix arg N, jump back that many stored positions. When
  7573. called several times in succession, walk through the entire ring.
  7574. Org-mode commands jumping to a different position in the current file,
  7575. or to another Org-mode file, automatically push the old position
  7576. onto the ring."
  7577. (interactive "p")
  7578. (let (p m)
  7579. (if (eq last-command this-command)
  7580. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7581. (setq p org-mark-ring))
  7582. (setq org-mark-ring-last-goto p)
  7583. (setq m (car p))
  7584. (switch-to-buffer (marker-buffer m))
  7585. (goto-char m)
  7586. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7587. (defun org-remove-angle-brackets (s)
  7588. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7589. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7590. s)
  7591. (defun org-add-angle-brackets (s)
  7592. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7593. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7594. s)
  7595. (defun org-remove-double-quotes (s)
  7596. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7597. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7598. s)
  7599. ;;; Following specific links
  7600. (defun org-follow-timestamp-link ()
  7601. (cond
  7602. ((org-at-date-range-p t)
  7603. (let ((org-agenda-start-on-weekday)
  7604. (t1 (match-string 1))
  7605. (t2 (match-string 2)))
  7606. (setq t1 (time-to-days (org-time-string-to-time t1))
  7607. t2 (time-to-days (org-time-string-to-time t2)))
  7608. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7609. ((org-at-timestamp-p t)
  7610. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7611. (substring (match-string 1) 0 10)))
  7612. 1))
  7613. (t (error "This should not happen"))))
  7614. ;;; Following file links
  7615. (defvar org-wait nil)
  7616. (defun org-open-file (path &optional in-emacs line search)
  7617. "Open the file at PATH.
  7618. First, this expands any special file name abbreviations. Then the
  7619. configuration variable `org-file-apps' is checked if it contains an
  7620. entry for this file type, and if yes, the corresponding command is launched.
  7621. If no application is found, Emacs simply visits the file.
  7622. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7623. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7624. and o use an external application to visit the file.
  7625. Optional LINE specifies a line to go to, optional SEARCH a string to
  7626. search for. If LINE or SEARCH is given, the file will always be
  7627. opened in Emacs.
  7628. If the file does not exist, an error is thrown."
  7629. (setq in-emacs (or in-emacs line search))
  7630. (let* ((file (if (equal path "")
  7631. buffer-file-name
  7632. (substitute-in-file-name (expand-file-name path))))
  7633. (apps (append org-file-apps (org-default-apps)))
  7634. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7635. (dirp (if remp nil (file-directory-p file)))
  7636. (file (if (and dirp org-open-directory-means-index-dot-org)
  7637. (concat (file-name-as-directory file) "index.org")
  7638. file))
  7639. (a-m-a-p (assq 'auto-mode apps))
  7640. (dfile (downcase file))
  7641. (old-buffer (current-buffer))
  7642. (old-pos (point))
  7643. (old-mode major-mode)
  7644. ext cmd)
  7645. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7646. (setq ext (match-string 1 dfile))
  7647. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7648. (setq ext (match-string 1 dfile))))
  7649. (cond
  7650. ((equal in-emacs '(16))
  7651. (setq cmd (cdr (assoc 'system apps))))
  7652. (in-emacs (setq cmd 'emacs))
  7653. (t
  7654. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7655. (and dirp (cdr (assoc 'directory apps)))
  7656. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7657. 'string-match)
  7658. (cdr (assoc ext apps))
  7659. (cdr (assoc t apps))))))
  7660. (when (eq cmd 'system)
  7661. (setq cmd (cdr (assoc 'system apps))))
  7662. (when (eq cmd 'default)
  7663. (setq cmd (cdr (assoc t apps))))
  7664. (when (eq cmd 'mailcap)
  7665. (require 'mailcap)
  7666. (mailcap-parse-mailcaps)
  7667. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7668. (command (mailcap-mime-info mime-type)))
  7669. (if (stringp command)
  7670. (setq cmd command)
  7671. (setq cmd 'emacs))))
  7672. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7673. (not (file-exists-p file))
  7674. (not org-open-non-existing-files))
  7675. (error "No such file: %s" file))
  7676. (cond
  7677. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7678. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7679. (while (string-match "['\"]%s['\"]" cmd)
  7680. (setq cmd (replace-match "%s" t t cmd)))
  7681. (while (string-match "%s" cmd)
  7682. (setq cmd (replace-match
  7683. (save-match-data
  7684. (shell-quote-argument
  7685. (convert-standard-filename file)))
  7686. t t cmd)))
  7687. (save-window-excursion
  7688. (start-process-shell-command cmd nil cmd)
  7689. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7690. ))
  7691. ((or (stringp cmd)
  7692. (eq cmd 'emacs))
  7693. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7694. (widen)
  7695. (if line (goto-line line)
  7696. (if search (org-link-search search))))
  7697. ((consp cmd)
  7698. (let ((file (convert-standard-filename file)))
  7699. (eval cmd)))
  7700. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7701. (and (org-mode-p) (eq old-mode 'org-mode)
  7702. (or (not (equal old-buffer (current-buffer)))
  7703. (not (equal old-pos (point))))
  7704. (org-mark-ring-push old-pos old-buffer))))
  7705. (defun org-default-apps ()
  7706. "Return the default applications for this operating system."
  7707. (cond
  7708. ((eq system-type 'darwin)
  7709. org-file-apps-defaults-macosx)
  7710. ((eq system-type 'windows-nt)
  7711. org-file-apps-defaults-windowsnt)
  7712. (t org-file-apps-defaults-gnu)))
  7713. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7714. "Convert extensions to regular expressions in the cars of LIST.
  7715. Also, weed out any non-string entries, because the return value is used
  7716. only for regexp matching.
  7717. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7718. point to the symbol `emacs', indicating that the file should
  7719. be opened in Emacs."
  7720. (append
  7721. (delq nil
  7722. (mapcar (lambda (x)
  7723. (if (not (stringp (car x)))
  7724. nil
  7725. (if (string-match "\\W" (car x))
  7726. x
  7727. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7728. list))
  7729. (if add-auto-mode
  7730. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7731. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7732. (defun org-file-remote-p (file)
  7733. "Test whether FILE specifies a location on a remote system.
  7734. Return non-nil if the location is indeed remote.
  7735. For example, the filename \"/user@host:/foo\" specifies a location
  7736. on the system \"/user@host:\"."
  7737. (cond ((fboundp 'file-remote-p)
  7738. (file-remote-p file))
  7739. ((fboundp 'tramp-handle-file-remote-p)
  7740. (tramp-handle-file-remote-p file))
  7741. ((and (boundp 'ange-ftp-name-format)
  7742. (string-match (car ange-ftp-name-format) file))
  7743. t)
  7744. (t nil)))
  7745. ;;;; Refiling
  7746. (defun org-get-org-file ()
  7747. "Read a filename, with default directory `org-directory'."
  7748. (let ((default (or org-default-notes-file remember-data-file)))
  7749. (read-file-name (format "File name [%s]: " default)
  7750. (file-name-as-directory org-directory)
  7751. default)))
  7752. (defun org-notes-order-reversed-p ()
  7753. "Check if the current file should receive notes in reversed order."
  7754. (cond
  7755. ((not org-reverse-note-order) nil)
  7756. ((eq t org-reverse-note-order) t)
  7757. ((not (listp org-reverse-note-order)) nil)
  7758. (t (catch 'exit
  7759. (let ((all org-reverse-note-order)
  7760. entry)
  7761. (while (setq entry (pop all))
  7762. (if (string-match (car entry) buffer-file-name)
  7763. (throw 'exit (cdr entry))))
  7764. nil)))))
  7765. (defvar org-refile-target-table nil
  7766. "The list of refile targets, created by `org-refile'.")
  7767. (defvar org-agenda-new-buffers nil
  7768. "Buffers created to visit agenda files.")
  7769. (defun org-get-refile-targets (&optional default-buffer)
  7770. "Produce a table with refile targets."
  7771. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7772. targets txt re files f desc descre fast-path-p level pos0)
  7773. (message "Getting targets...")
  7774. (with-current-buffer (or default-buffer (current-buffer))
  7775. (while (setq entry (pop entries))
  7776. (setq files (car entry) desc (cdr entry))
  7777. (setq fast-path-p nil)
  7778. (cond
  7779. ((null files) (setq files (list (current-buffer))))
  7780. ((eq files 'org-agenda-files)
  7781. (setq files (org-agenda-files 'unrestricted)))
  7782. ((and (symbolp files) (fboundp files))
  7783. (setq files (funcall files)))
  7784. ((and (symbolp files) (boundp files))
  7785. (setq files (symbol-value files))))
  7786. (if (stringp files) (setq files (list files)))
  7787. (cond
  7788. ((eq (car desc) :tag)
  7789. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7790. ((eq (car desc) :todo)
  7791. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7792. ((eq (car desc) :regexp)
  7793. (setq descre (cdr desc)))
  7794. ((eq (car desc) :level)
  7795. (setq descre (concat "^\\*\\{" (number-to-string
  7796. (if org-odd-levels-only
  7797. (1- (* 2 (cdr desc)))
  7798. (cdr desc)))
  7799. "\\}[ \t]")))
  7800. ((eq (car desc) :maxlevel)
  7801. (setq fast-path-p t)
  7802. (setq descre (concat "^\\*\\{1," (number-to-string
  7803. (if org-odd-levels-only
  7804. (1- (* 2 (cdr desc)))
  7805. (cdr desc)))
  7806. "\\}[ \t]")))
  7807. (t (error "Bad refiling target description %s" desc)))
  7808. (while (setq f (pop files))
  7809. (save-excursion
  7810. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7811. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7812. (setq f (expand-file-name f))
  7813. (if (eq org-refile-use-outline-path 'file)
  7814. (push (list (file-name-nondirectory f) f nil nil) targets))
  7815. (save-excursion
  7816. (save-restriction
  7817. (widen)
  7818. (goto-char (point-min))
  7819. (while (re-search-forward descre nil t)
  7820. (goto-char (setq pos0 (point-at-bol)))
  7821. (catch 'next
  7822. (when org-refile-target-verify-function
  7823. (save-match-data
  7824. (or (funcall org-refile-target-verify-function)
  7825. (throw 'next t))))
  7826. (when (looking-at org-complex-heading-regexp)
  7827. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7828. txt (org-link-display-format (match-string 4))
  7829. re (concat "^" (regexp-quote
  7830. (buffer-substring (match-beginning 1)
  7831. (match-end 4)))))
  7832. (if (match-end 5) (setq re (concat re "[ \t]+"
  7833. (regexp-quote
  7834. (match-string 5)))))
  7835. (setq re (concat re "[ \t]*$"))
  7836. (when org-refile-use-outline-path
  7837. (setq txt (mapconcat 'org-protect-slash
  7838. (append
  7839. (if (eq org-refile-use-outline-path 'file)
  7840. (list (file-name-nondirectory
  7841. (buffer-file-name (buffer-base-buffer))))
  7842. (if (eq org-refile-use-outline-path 'full-file-path)
  7843. (list (buffer-file-name (buffer-base-buffer)))))
  7844. (org-get-outline-path fast-path-p level txt)
  7845. (list txt))
  7846. "/")))
  7847. (push (list txt f re (point)) targets)))
  7848. (when (= (point) pos0)
  7849. ;; verification function has not moved point
  7850. (goto-char (point-at-eol))))))))))
  7851. (message "Getting targets...done")
  7852. (nreverse targets)))
  7853. (defun org-protect-slash (s)
  7854. (while (string-match "/" s)
  7855. (setq s (replace-match "\\" t t s)))
  7856. s)
  7857. (defvar org-olpa (make-vector 20 nil))
  7858. (defun org-get-outline-path (&optional fastp level heading)
  7859. "Return the outline path to the current entry, as a list."
  7860. (if fastp
  7861. (progn
  7862. (if (> level 19)
  7863. (error "Outline path failure, more than 19 levels."))
  7864. (loop for i from level upto 19 do
  7865. (aset org-olpa i nil))
  7866. (prog1
  7867. (delq nil (append org-olpa nil))
  7868. (aset org-olpa level heading)))
  7869. (let (rtn)
  7870. (save-excursion
  7871. (while (org-up-heading-safe)
  7872. (when (looking-at org-complex-heading-regexp)
  7873. (push (org-match-string-no-properties 4) rtn)))
  7874. rtn))))
  7875. (defvar org-refile-history nil
  7876. "History for refiling operations.")
  7877. (defvar org-after-refile-insert-hook nil
  7878. "Hook run after `org-refile' has inserted its stuff at the new location.
  7879. Note that this is still *before* the stuff will be removed from
  7880. the *old* location.")
  7881. (defun org-refile (&optional goto default-buffer)
  7882. "Move the entry at point to another heading.
  7883. The list of target headings is compiled using the information in
  7884. `org-refile-targets', which see. This list is created before each use
  7885. and will therefore always be up-to-date.
  7886. At the target location, the entry is filed as a subitem of the target heading.
  7887. Depending on `org-reverse-note-order', the new subitem will either be the
  7888. first or the last subitem.
  7889. If there is an active region, all entries in that region will be moved.
  7890. However, the region must fulfil the requirement that the first heading
  7891. is the first one sets the top-level of the moved text - at most siblings
  7892. below it are allowed.
  7893. With prefix arg GOTO, the command will only visit the target location,
  7894. not actually move anything.
  7895. With a double prefix `C-u C-u', go to the location where the last refiling
  7896. operation has put the subtree.
  7897. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7898. (interactive "P")
  7899. (let* ((cbuf (current-buffer))
  7900. (regionp (org-region-active-p))
  7901. (region-start (and regionp (region-beginning)))
  7902. (region-end (and regionp (region-end)))
  7903. (region-length (and regionp (- region-end region-start)))
  7904. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7905. pos it nbuf file re level reversed)
  7906. (when regionp (goto-char region-start)
  7907. (unless (org-kill-is-subtree-p
  7908. (buffer-substring region-start region-end))
  7909. (error "The region is not a (sequence of) subtree(s)")))
  7910. (if (equal goto '(16))
  7911. (org-refile-goto-last-stored)
  7912. (when (setq it (org-refile-get-location
  7913. (if goto "Goto: " "Refile to: ") default-buffer
  7914. org-refile-allow-creating-parent-nodes))
  7915. (setq file (nth 1 it)
  7916. re (nth 2 it)
  7917. pos (nth 3 it))
  7918. (if (and (not goto)
  7919. pos
  7920. (equal (buffer-file-name) file)
  7921. (if regionp
  7922. (and (>= pos region-start)
  7923. (<= pos region-end))
  7924. (and (>= pos (point))
  7925. (< pos (save-excursion
  7926. (org-end-of-subtree t t))))))
  7927. (error "Cannot refile to position inside the tree or region"))
  7928. (setq nbuf (or (find-buffer-visiting file)
  7929. (find-file-noselect file)))
  7930. (if goto
  7931. (progn
  7932. (switch-to-buffer nbuf)
  7933. (goto-char pos)
  7934. (org-show-context 'org-goto))
  7935. (if regionp
  7936. (progn
  7937. (kill-new (buffer-substring region-start region-end))
  7938. (org-save-markers-in-region region-start region-end))
  7939. (org-copy-subtree 1 nil t))
  7940. (save-excursion
  7941. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7942. (find-file-noselect file))))
  7943. (setq reversed (org-notes-order-reversed-p))
  7944. (save-excursion
  7945. (save-restriction
  7946. (widen)
  7947. (if pos
  7948. (progn
  7949. (goto-char pos)
  7950. (looking-at outline-regexp)
  7951. (setq level (org-get-valid-level (funcall outline-level) 1))
  7952. (goto-char
  7953. (if reversed
  7954. (or (outline-next-heading) (point-max))
  7955. (or (save-excursion (outline-get-next-sibling))
  7956. (org-end-of-subtree t t)
  7957. (point-max)))))
  7958. (setq level 1)
  7959. (if (not reversed)
  7960. (goto-char (point-max))
  7961. (goto-char (point-min))
  7962. (or (outline-next-heading) (goto-char (point-max)))))
  7963. (if (not (bolp)) (newline))
  7964. (bookmark-set "org-refile-last-stored")
  7965. (org-paste-subtree level)
  7966. (run-hooks 'org-after-refile-insert-hook))))
  7967. (if regionp
  7968. (delete-region (point) (+ (point) region-length))
  7969. (org-cut-subtree))
  7970. (setq org-markers-to-move nil)
  7971. (message "Refiled to \"%s\"" (car it))))))
  7972. (org-reveal))
  7973. (defun org-refile-goto-last-stored ()
  7974. "Go to the location where the last refile was stored."
  7975. (interactive)
  7976. (bookmark-jump "org-refile-last-stored")
  7977. (message "This is the location of the last refile"))
  7978. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7979. "Prompt the user for a refile location, using PROMPT."
  7980. (let ((org-refile-targets org-refile-targets)
  7981. (org-refile-use-outline-path org-refile-use-outline-path))
  7982. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7983. (unless org-refile-target-table
  7984. (error "No refile targets"))
  7985. (let* ((cbuf (current-buffer))
  7986. (partial-completion-mode nil)
  7987. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7988. (cfunc (if (and org-refile-use-outline-path
  7989. org-outline-path-complete-in-steps)
  7990. 'org-olpath-completing-read
  7991. 'org-ido-completing-read))
  7992. (extra (if org-refile-use-outline-path "/" ""))
  7993. (filename (and cfn (expand-file-name cfn)))
  7994. (tbl (mapcar
  7995. (lambda (x)
  7996. (if (and (not (member org-refile-use-outline-path
  7997. '(file full-file-path)))
  7998. (not (equal filename (nth 1 x))))
  7999. (cons (concat (car x) extra " ("
  8000. (file-name-nondirectory (nth 1 x)) ")")
  8001. (cdr x))
  8002. (cons (concat (car x) extra) (cdr x))))
  8003. org-refile-target-table))
  8004. (completion-ignore-case t)
  8005. pa answ parent-target child parent)
  8006. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8007. nil 'org-refile-history))
  8008. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8009. (if pa
  8010. pa
  8011. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8012. (setq parent (match-string 1 answ)
  8013. child (match-string 2 answ))
  8014. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8015. (when (and parent-target
  8016. (or (eq new-nodes t)
  8017. (and (eq new-nodes 'confirm)
  8018. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8019. (org-refile-new-child parent-target child))))))
  8020. (defun org-refile-new-child (parent-target child)
  8021. "Use refile target PARENT-TARGET to add new CHILD below it."
  8022. (unless parent-target
  8023. (error "Cannot find parent for new node"))
  8024. (let ((file (nth 1 parent-target))
  8025. (pos (nth 3 parent-target))
  8026. level)
  8027. (with-current-buffer (or (find-buffer-visiting file)
  8028. (find-file-noselect file))
  8029. (save-excursion
  8030. (save-restriction
  8031. (widen)
  8032. (if pos
  8033. (goto-char pos)
  8034. (goto-char (point-max))
  8035. (if (not (bolp)) (newline)))
  8036. (when (looking-at outline-regexp)
  8037. (setq level (funcall outline-level))
  8038. (org-end-of-subtree t t))
  8039. (org-back-over-empty-lines)
  8040. (insert "\n" (make-string
  8041. (if pos (org-get-valid-level level 1) 1) ?*)
  8042. " " child "\n")
  8043. (beginning-of-line 0)
  8044. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8045. (defun org-olpath-completing-read (prompt collection &rest args)
  8046. "Read an outline path like a file name."
  8047. (let ((thetable collection)
  8048. (org-completion-use-ido nil)) ; does not work with ido.
  8049. (apply
  8050. 'org-ido-completing-read prompt
  8051. (lambda (string predicate &optional flag)
  8052. (let (rtn r f (l (length string)))
  8053. (cond
  8054. ((eq flag nil)
  8055. ;; try completion
  8056. (try-completion string thetable))
  8057. ((eq flag t)
  8058. ;; all-completions
  8059. (setq rtn (all-completions string thetable predicate))
  8060. (mapcar
  8061. (lambda (x)
  8062. (setq r (substring x l))
  8063. (if (string-match " ([^)]*)$" x)
  8064. (setq f (match-string 0 x))
  8065. (setq f ""))
  8066. (if (string-match "/" r)
  8067. (concat string (substring r 0 (match-end 0)) f)
  8068. x))
  8069. rtn))
  8070. ((eq flag 'lambda)
  8071. ;; exact match?
  8072. (assoc string thetable)))
  8073. ))
  8074. args)))
  8075. ;;;; Dynamic blocks
  8076. (defun org-find-dblock (name)
  8077. "Find the first dynamic block with name NAME in the buffer.
  8078. If not found, stay at current position and return nil."
  8079. (let (pos)
  8080. (save-excursion
  8081. (goto-char (point-min))
  8082. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8083. nil t)
  8084. (match-beginning 0))))
  8085. (if pos (goto-char pos))
  8086. pos))
  8087. (defconst org-dblock-start-re
  8088. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8089. "Matches the startline of a dynamic block, with parameters.")
  8090. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8091. "Matches the end of a dynamic block.")
  8092. (defun org-create-dblock (plist)
  8093. "Create a dynamic block section, with parameters taken from PLIST.
  8094. PLIST must contain a :name entry which is used as name of the block."
  8095. (unless (bolp) (newline))
  8096. (let ((name (plist-get plist :name)))
  8097. (insert "#+BEGIN: " name)
  8098. (while plist
  8099. (if (eq (car plist) :name)
  8100. (setq plist (cddr plist))
  8101. (insert " " (prin1-to-string (pop plist)))))
  8102. (insert "\n\n#+END:\n")
  8103. (beginning-of-line -2)))
  8104. (defun org-prepare-dblock ()
  8105. "Prepare dynamic block for refresh.
  8106. This empties the block, puts the cursor at the insert position and returns
  8107. the property list including an extra property :name with the block name."
  8108. (unless (looking-at org-dblock-start-re)
  8109. (error "Not at a dynamic block"))
  8110. (let* ((begdel (1+ (match-end 0)))
  8111. (name (org-no-properties (match-string 1)))
  8112. (params (append (list :name name)
  8113. (read (concat "(" (match-string 3) ")")))))
  8114. (unless (re-search-forward org-dblock-end-re nil t)
  8115. (error "Dynamic block not terminated"))
  8116. (setq params
  8117. (append params
  8118. (list :content (buffer-substring
  8119. begdel (match-beginning 0)))))
  8120. (delete-region begdel (match-beginning 0))
  8121. (goto-char begdel)
  8122. (open-line 1)
  8123. params))
  8124. (defun org-map-dblocks (&optional command)
  8125. "Apply COMMAND to all dynamic blocks in the current buffer.
  8126. If COMMAND is not given, use `org-update-dblock'."
  8127. (let ((cmd (or command 'org-update-dblock))
  8128. pos)
  8129. (save-excursion
  8130. (goto-char (point-min))
  8131. (while (re-search-forward org-dblock-start-re nil t)
  8132. (goto-char (setq pos (match-beginning 0)))
  8133. (condition-case nil
  8134. (funcall cmd)
  8135. (error (message "Error during update of dynamic block")))
  8136. (goto-char pos)
  8137. (unless (re-search-forward org-dblock-end-re nil t)
  8138. (error "Dynamic block not terminated"))))))
  8139. (defun org-dblock-update (&optional arg)
  8140. "User command for updating dynamic blocks.
  8141. Update the dynamic block at point. With prefix ARG, update all dynamic
  8142. blocks in the buffer."
  8143. (interactive "P")
  8144. (if arg
  8145. (org-update-all-dblocks)
  8146. (or (looking-at org-dblock-start-re)
  8147. (org-beginning-of-dblock))
  8148. (org-update-dblock)))
  8149. (defun org-update-dblock ()
  8150. "Update the dynamic block at point
  8151. This means to empty the block, parse for parameters and then call
  8152. the correct writing function."
  8153. (save-window-excursion
  8154. (let* ((pos (point))
  8155. (line (org-current-line))
  8156. (params (org-prepare-dblock))
  8157. (name (plist-get params :name))
  8158. (cmd (intern (concat "org-dblock-write:" name))))
  8159. (message "Updating dynamic block `%s' at line %d..." name line)
  8160. (funcall cmd params)
  8161. (message "Updating dynamic block `%s' at line %d...done" name line)
  8162. (goto-char pos))))
  8163. (defun org-beginning-of-dblock ()
  8164. "Find the beginning of the dynamic block at point.
  8165. Error if there is no such block at point."
  8166. (let ((pos (point))
  8167. beg)
  8168. (end-of-line 1)
  8169. (if (and (re-search-backward org-dblock-start-re nil t)
  8170. (setq beg (match-beginning 0))
  8171. (re-search-forward org-dblock-end-re nil t)
  8172. (> (match-end 0) pos))
  8173. (goto-char beg)
  8174. (goto-char pos)
  8175. (error "Not in a dynamic block"))))
  8176. (defun org-update-all-dblocks ()
  8177. "Update all dynamic blocks in the buffer.
  8178. This function can be used in a hook."
  8179. (when (org-mode-p)
  8180. (org-map-dblocks 'org-update-dblock)))
  8181. ;;;; Completion
  8182. (defconst org-additional-option-like-keywords
  8183. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  8184. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  8185. "LATEX_HEADER:"
  8186. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8187. "BEGIN_QUOTE" "END_QUOTE"
  8188. "BEGIN_VERSE" "END_VERSE"
  8189. "BEGIN_CENTER" "END_CENTER"
  8190. "BEGIN_SRC" "END_SRC"
  8191. "CATEGORY" "COLUMNS"
  8192. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  8193. (defcustom org-structure-template-alist
  8194. '(
  8195. ("s" "#+begin_src ?\n\n#+end_src"
  8196. "<src lang=\"?\">\n\n</src>")
  8197. ("e" "#+begin_example\n?\n#+end_example"
  8198. "<example>\n?\n</example>")
  8199. ("q" "#+begin_quote\n?\n#+end_quote"
  8200. "<quote>\n?\n</quote>")
  8201. ("v" "#+begin_verse\n?\n#+end_verse"
  8202. "<verse>\n?\n/verse>")
  8203. ("c" "#+begin_center\n?\n#+end_center"
  8204. "<center>\n?\n/center>")
  8205. ("l" "#+begin_latex\n?\n#+end_latex"
  8206. "<literal style=\"latex\">\n?\n</literal>")
  8207. ("L" "#+latex: "
  8208. "<literal style=\"latex\">?</literal>")
  8209. ("h" "#+begin_html\n?\n#+end_html"
  8210. "<literal style=\"html\">\n?\n</literal>")
  8211. ("H" "#+html: "
  8212. "<literal style=\"html\">?</literal>")
  8213. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8214. ("A" "#+ascii: ")
  8215. ("i" "#+include %file ?"
  8216. "<include file=%file markup=\"?\">")
  8217. )
  8218. "Structure completion elements.
  8219. This is a list of abbreviation keys and values. The value gets inserted
  8220. it you type @samp{.} followed by the key and then the completion key,
  8221. usually `M-TAB'. %file will be replaced by a file name after prompting
  8222. for the file using completion.
  8223. There are two templates for each key, the first uses the original Org syntax,
  8224. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8225. the default when the /org-mtags.el/ module has been loaded. See also the
  8226. variable `org-mtags-prefer-muse-templates'.
  8227. This is an experimental feature, it is undecided if it is going to stay in."
  8228. :group 'org-completion
  8229. :type '(repeat
  8230. (string :tag "Key")
  8231. (string :tag "Template")
  8232. (string :tag "Muse Template")))
  8233. (defun org-try-structure-completion ()
  8234. "Try to complete a structure template before point.
  8235. This looks for strings like \"<e\" on an otherwise empty line and
  8236. expands them."
  8237. (let ((l (buffer-substring (point-at-bol) (point)))
  8238. a)
  8239. (when (and (looking-at "[ \t]*$")
  8240. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8241. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8242. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8243. (match-beginning 1)) a)
  8244. t)))
  8245. (defun org-complete-expand-structure-template (start cell)
  8246. "Expand a structure template."
  8247. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8248. (rpl (nth (if musep 2 1) cell))
  8249. (ind ""))
  8250. (delete-region start (point))
  8251. (when (string-match "\\`#\\+" rpl)
  8252. (cond
  8253. ((bolp))
  8254. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8255. (setq ind (buffer-substring (point-at-bol) (point))))
  8256. (t (newline))))
  8257. (setq start (point))
  8258. (if (string-match "%file" rpl)
  8259. (setq rpl (replace-match
  8260. (concat
  8261. "\""
  8262. (save-match-data
  8263. (abbreviate-file-name (read-file-name "Include file: ")))
  8264. "\"")
  8265. t t rpl)))
  8266. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8267. (concat "\n" ind)))
  8268. (insert rpl)
  8269. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8270. (defun org-complete (&optional arg)
  8271. "Perform completion on word at point.
  8272. At the beginning of a headline, this completes TODO keywords as given in
  8273. `org-todo-keywords'.
  8274. If the current word is preceded by a backslash, completes the TeX symbols
  8275. that are supported for HTML support.
  8276. If the current word is preceded by \"#+\", completes special words for
  8277. setting file options.
  8278. In the line after \"#+STARTUP:, complete valid keywords.\"
  8279. At all other locations, this simply calls the value of
  8280. `org-completion-fallback-command'."
  8281. (interactive "P")
  8282. (org-without-partial-completion
  8283. (catch 'exit
  8284. (let* ((a nil)
  8285. (end (point))
  8286. (beg1 (save-excursion
  8287. (skip-chars-backward (org-re "[:alnum:]_@"))
  8288. (point)))
  8289. (beg (save-excursion
  8290. (skip-chars-backward "a-zA-Z0-9_:$")
  8291. (point)))
  8292. (confirm (lambda (x) (stringp (car x))))
  8293. (searchhead (equal (char-before beg) ?*))
  8294. (struct
  8295. (when (and (member (char-before beg1) '(?. ?<))
  8296. (setq a (assoc (buffer-substring beg1 (point))
  8297. org-structure-template-alist)))
  8298. (org-complete-expand-structure-template (1- beg1) a)
  8299. (throw 'exit t)))
  8300. (tag (and (equal (char-before beg1) ?:)
  8301. (equal (char-after (point-at-bol)) ?*)))
  8302. (prop (and (equal (char-before beg1) ?:)
  8303. (not (equal (char-after (point-at-bol)) ?*))))
  8304. (texp (equal (char-before beg) ?\\))
  8305. (link (equal (char-before beg) ?\[))
  8306. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8307. beg)
  8308. "#+"))
  8309. (startup (string-match "^#\\+STARTUP:.*"
  8310. (buffer-substring (point-at-bol) (point))))
  8311. (completion-ignore-case opt)
  8312. (type nil)
  8313. (tbl nil)
  8314. (table (cond
  8315. (opt
  8316. (setq type :opt)
  8317. (require 'org-exp)
  8318. (append
  8319. (mapcar
  8320. (lambda (x)
  8321. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8322. (cons (match-string 2 x) (match-string 1 x)))
  8323. (org-split-string (org-get-current-options) "\n"))
  8324. (mapcar 'list org-additional-option-like-keywords)))
  8325. (startup
  8326. (setq type :startup)
  8327. org-startup-options)
  8328. (link (append org-link-abbrev-alist-local
  8329. org-link-abbrev-alist))
  8330. (texp
  8331. (setq type :tex)
  8332. org-html-entities)
  8333. ((string-match "\\`\\*+[ \t]+\\'"
  8334. (buffer-substring (point-at-bol) beg))
  8335. (setq type :todo)
  8336. (mapcar 'list org-todo-keywords-1))
  8337. (searchhead
  8338. (setq type :searchhead)
  8339. (save-excursion
  8340. (goto-char (point-min))
  8341. (while (re-search-forward org-todo-line-regexp nil t)
  8342. (push (list
  8343. (org-make-org-heading-search-string
  8344. (match-string 3) t))
  8345. tbl)))
  8346. tbl)
  8347. (tag (setq type :tag beg beg1)
  8348. (or org-tag-alist (org-get-buffer-tags)))
  8349. (prop (setq type :prop beg beg1)
  8350. (mapcar 'list (org-buffer-property-keys nil t t)))
  8351. (t (progn
  8352. (call-interactively org-completion-fallback-command)
  8353. (throw 'exit nil)))))
  8354. (pattern (buffer-substring-no-properties beg end))
  8355. (completion (try-completion pattern table confirm)))
  8356. (cond ((eq completion t)
  8357. (if (not (assoc (upcase pattern) table))
  8358. (message "Already complete")
  8359. (if (and (equal type :opt)
  8360. (not (member (car (assoc (upcase pattern) table))
  8361. org-additional-option-like-keywords)))
  8362. (insert (substring (cdr (assoc (upcase pattern) table))
  8363. (length pattern)))
  8364. (if (memq type '(:tag :prop)) (insert ":")))))
  8365. ((null completion)
  8366. (message "Can't find completion for \"%s\"" pattern)
  8367. (ding))
  8368. ((not (string= pattern completion))
  8369. (delete-region beg end)
  8370. (if (string-match " +$" completion)
  8371. (setq completion (replace-match "" t t completion)))
  8372. (insert completion)
  8373. (if (get-buffer-window "*Completions*")
  8374. (delete-window (get-buffer-window "*Completions*")))
  8375. (if (assoc completion table)
  8376. (if (eq type :todo) (insert " ")
  8377. (if (memq type '(:tag :prop)) (insert ":"))))
  8378. (if (and (equal type :opt) (assoc completion table))
  8379. (message "%s" (substitute-command-keys
  8380. "Press \\[org-complete] again to insert example settings"))))
  8381. (t
  8382. (message "Making completion list...")
  8383. (let ((list (sort (all-completions pattern table confirm)
  8384. 'string<)))
  8385. (with-output-to-temp-buffer "*Completions*"
  8386. (condition-case nil
  8387. ;; Protection needed for XEmacs and emacs 21
  8388. (display-completion-list list pattern)
  8389. (error (display-completion-list list)))))
  8390. (message "Making completion list...%s" "done")))))))
  8391. ;;;; TODO, DEADLINE, Comments
  8392. (defun org-toggle-comment ()
  8393. "Change the COMMENT state of an entry."
  8394. (interactive)
  8395. (save-excursion
  8396. (org-back-to-heading)
  8397. (let (case-fold-search)
  8398. (if (looking-at (concat outline-regexp
  8399. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8400. (replace-match "" t t nil 1)
  8401. (if (looking-at outline-regexp)
  8402. (progn
  8403. (goto-char (match-end 0))
  8404. (insert org-comment-string " ")))))))
  8405. (defvar org-last-todo-state-is-todo nil
  8406. "This is non-nil when the last TODO state change led to a TODO state.
  8407. If the last change removed the TODO tag or switched to DONE, then
  8408. this is nil.")
  8409. (defvar org-setting-tags nil) ; dynamically skipped
  8410. (defun org-parse-local-options (string var)
  8411. "Parse STRING for startup setting relevant for variable VAR."
  8412. (let ((rtn (symbol-value var))
  8413. e opts)
  8414. (save-match-data
  8415. (if (or (not string) (not (string-match "\\S-" string)))
  8416. rtn
  8417. (setq opts (delq nil (mapcar (lambda (x)
  8418. (setq e (assoc x org-startup-options))
  8419. (if (eq (nth 1 e) var) e nil))
  8420. (org-split-string string "[ \t]+"))))
  8421. (if (not opts)
  8422. rtn
  8423. (setq rtn nil)
  8424. (while (setq e (pop opts))
  8425. (if (not (nth 3 e))
  8426. (setq rtn (nth 2 e))
  8427. (if (not (listp rtn)) (setq rtn nil))
  8428. (push (nth 2 e) rtn)))
  8429. rtn)))))
  8430. (defvar org-todo-setup-filter-hook nil
  8431. "Hook for functions that pre-filter todo specs.
  8432. Each function takes a todo spec and returns either `nil' or the spec
  8433. transformed into canonical form." )
  8434. (defvar org-todo-get-default-hook nil
  8435. "Hook for functions that get a default item for todo.
  8436. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8437. `nil' or a string to be used for the todo mark." )
  8438. (defvar org-agenda-headline-snapshot-before-repeat)
  8439. (defun org-todo (&optional arg)
  8440. "Change the TODO state of an item.
  8441. The state of an item is given by a keyword at the start of the heading,
  8442. like
  8443. *** TODO Write paper
  8444. *** DONE Call mom
  8445. The different keywords are specified in the variable `org-todo-keywords'.
  8446. By default the available states are \"TODO\" and \"DONE\".
  8447. So for this example: when the item starts with TODO, it is changed to DONE.
  8448. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8449. DONE are present, add TODO at the beginning of the heading.
  8450. With C-u prefix arg, use completion to determine the new state.
  8451. With numeric prefix arg, switch to that state.
  8452. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8453. With a tripple C-u prefix, circumvent any state blocking.
  8454. For calling through lisp, arg is also interpreted in the following way:
  8455. 'none -> empty state
  8456. \"\"(empty string) -> switch to empty state
  8457. 'done -> switch to DONE
  8458. 'nextset -> switch to the next set of keywords
  8459. 'previousset -> switch to the previous set of keywords
  8460. \"WAITING\" -> switch to the specified keyword, but only if it
  8461. really is a member of `org-todo-keywords'."
  8462. (interactive "P")
  8463. (if (equal arg '(16)) (setq arg 'nextset))
  8464. (let ((org-blocker-hook org-blocker-hook)
  8465. (case-fold-search nil))
  8466. (when (equal arg '(64))
  8467. (setq arg nil org-blocker-hook nil))
  8468. (when (and org-blocker-hook
  8469. (org-entry-get nil "NOBLOCKING"))
  8470. (setq org-blocker-hook nil))
  8471. (save-excursion
  8472. (catch 'exit
  8473. (org-back-to-heading)
  8474. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8475. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8476. (looking-at " *"))
  8477. (let* ((match-data (match-data))
  8478. (startpos (point-at-bol))
  8479. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8480. (org-log-done org-log-done)
  8481. (org-log-repeat org-log-repeat)
  8482. (org-todo-log-states org-todo-log-states)
  8483. (this (match-string 1))
  8484. (hl-pos (match-beginning 0))
  8485. (head (org-get-todo-sequence-head this))
  8486. (ass (assoc head org-todo-kwd-alist))
  8487. (interpret (nth 1 ass))
  8488. (done-word (nth 3 ass))
  8489. (final-done-word (nth 4 ass))
  8490. (last-state (or this ""))
  8491. (completion-ignore-case t)
  8492. (member (member this org-todo-keywords-1))
  8493. (tail (cdr member))
  8494. (state (cond
  8495. ((and org-todo-key-trigger
  8496. (or (and (equal arg '(4))
  8497. (eq org-use-fast-todo-selection 'prefix))
  8498. (and (not arg) org-use-fast-todo-selection
  8499. (not (eq org-use-fast-todo-selection
  8500. 'prefix)))))
  8501. ;; Use fast selection
  8502. (org-fast-todo-selection))
  8503. ((and (equal arg '(4))
  8504. (or (not org-use-fast-todo-selection)
  8505. (not org-todo-key-trigger)))
  8506. ;; Read a state with completion
  8507. (org-ido-completing-read
  8508. "State: " (mapcar (lambda(x) (list x))
  8509. org-todo-keywords-1)
  8510. nil t))
  8511. ((eq arg 'right)
  8512. (if this
  8513. (if tail (car tail) nil)
  8514. (car org-todo-keywords-1)))
  8515. ((eq arg 'left)
  8516. (if (equal member org-todo-keywords-1)
  8517. nil
  8518. (if this
  8519. (nth (- (length org-todo-keywords-1)
  8520. (length tail) 2)
  8521. org-todo-keywords-1)
  8522. (org-last org-todo-keywords-1))))
  8523. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8524. (setq arg nil))) ; hack to fall back to cycling
  8525. (arg
  8526. ;; user or caller requests a specific state
  8527. (cond
  8528. ((equal arg "") nil)
  8529. ((eq arg 'none) nil)
  8530. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8531. ((eq arg 'nextset)
  8532. (or (car (cdr (member head org-todo-heads)))
  8533. (car org-todo-heads)))
  8534. ((eq arg 'previousset)
  8535. (let ((org-todo-heads (reverse org-todo-heads)))
  8536. (or (car (cdr (member head org-todo-heads)))
  8537. (car org-todo-heads))))
  8538. ((car (member arg org-todo-keywords-1)))
  8539. ((nth (1- (prefix-numeric-value arg))
  8540. org-todo-keywords-1))))
  8541. ((null member) (or head (car org-todo-keywords-1)))
  8542. ((equal this final-done-word) nil) ;; -> make empty
  8543. ((null tail) nil) ;; -> first entry
  8544. ((memq interpret '(type priority))
  8545. (if (eq this-command last-command)
  8546. (car tail)
  8547. (if (> (length tail) 0)
  8548. (or done-word (car org-done-keywords))
  8549. nil)))
  8550. (t
  8551. (car tail))))
  8552. (state (or
  8553. (run-hook-with-args-until-success
  8554. 'org-todo-get-default-hook state last-state)
  8555. state))
  8556. (next (if state (concat " " state " ") " "))
  8557. (change-plist (list :type 'todo-state-change :from this :to state
  8558. :position startpos))
  8559. dolog now-done-p)
  8560. (when org-blocker-hook
  8561. (setq org-last-todo-state-is-todo
  8562. (not (member this org-done-keywords)))
  8563. (unless (save-excursion
  8564. (save-match-data
  8565. (run-hook-with-args-until-failure
  8566. 'org-blocker-hook change-plist)))
  8567. (if (interactive-p)
  8568. (error "TODO state change from %s to %s blocked" this state)
  8569. ;; fail silently
  8570. (message "TODO state change from %s to %s blocked" this state)
  8571. (throw 'exit nil))))
  8572. (store-match-data match-data)
  8573. (replace-match next t t)
  8574. (unless (pos-visible-in-window-p hl-pos)
  8575. (message "TODO state changed to %s" (org-trim next)))
  8576. (unless head
  8577. (setq head (org-get-todo-sequence-head state)
  8578. ass (assoc head org-todo-kwd-alist)
  8579. interpret (nth 1 ass)
  8580. done-word (nth 3 ass)
  8581. final-done-word (nth 4 ass)))
  8582. (when (memq arg '(nextset previousset))
  8583. (message "Keyword-Set %d/%d: %s"
  8584. (- (length org-todo-sets) -1
  8585. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8586. (length org-todo-sets)
  8587. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8588. (setq org-last-todo-state-is-todo
  8589. (not (member state org-done-keywords)))
  8590. (setq now-done-p (and (member state org-done-keywords)
  8591. (not (member this org-done-keywords))))
  8592. (and logging (org-local-logging logging))
  8593. (when (and (or org-todo-log-states org-log-done)
  8594. (not org-inhibit-logging)
  8595. (not (memq arg '(nextset previousset))))
  8596. ;; we need to look at recording a time and note
  8597. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8598. (nth 2 (assoc this org-todo-log-states))))
  8599. (when (and state
  8600. (member state org-not-done-keywords)
  8601. (not (member this org-not-done-keywords)))
  8602. ;; This is now a todo state and was not one before
  8603. ;; If there was a CLOSED time stamp, get rid of it.
  8604. (org-add-planning-info nil nil 'closed))
  8605. (when (and now-done-p org-log-done)
  8606. ;; It is now done, and it was not done before
  8607. (org-add-planning-info 'closed (org-current-time))
  8608. (if (and (not dolog) (eq 'note org-log-done))
  8609. (org-add-log-setup 'done state this 'findpos 'note)))
  8610. (when (and state dolog)
  8611. ;; This is a non-nil state, and we need to log it
  8612. (org-add-log-setup 'state state this 'findpos dolog)))
  8613. ;; Fixup tag positioning
  8614. (org-todo-trigger-tag-changes state)
  8615. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8616. (when org-provide-todo-statistics
  8617. (org-update-parent-todo-statistics))
  8618. (run-hooks 'org-after-todo-state-change-hook)
  8619. (if (and arg (not (member state org-done-keywords)))
  8620. (setq head (org-get-todo-sequence-head state)))
  8621. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8622. ;; Do we need to trigger a repeat?
  8623. (when now-done-p
  8624. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8625. ;; This is for the agenda, take a snapshot of the headline.
  8626. (save-match-data
  8627. (setq org-agenda-headline-snapshot-before-repeat
  8628. (org-get-heading))))
  8629. (org-auto-repeat-maybe state))
  8630. ;; Fixup cursor location if close to the keyword
  8631. (if (and (outline-on-heading-p)
  8632. (not (bolp))
  8633. (save-excursion (beginning-of-line 1)
  8634. (looking-at org-todo-line-regexp))
  8635. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8636. (progn
  8637. (goto-char (or (match-end 2) (match-end 1)))
  8638. (and (looking-at " ") (just-one-space))))
  8639. (when org-trigger-hook
  8640. (save-excursion
  8641. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8642. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8643. "Block turning an entry into a TODO, using the hierarchy.
  8644. This checks whether the current task should be blocked from state
  8645. changes. Such blocking occurs when:
  8646. 1. The task has children which are not all in a completed state.
  8647. 2. A task has a parent with the property :ORDERED:, and there
  8648. are siblings prior to the current task with incomplete
  8649. status.
  8650. 3. The parent of the task is blocked because it has siblings that should
  8651. be done first, or is child of a block grandparent TODO entry."
  8652. (catch 'dont-block
  8653. ;; If this is not a todo state change, or if this entry is already DONE,
  8654. ;; do not block
  8655. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8656. (member (plist-get change-plist :from)
  8657. (cons 'done org-done-keywords))
  8658. (member (plist-get change-plist :to)
  8659. (cons 'todo org-not-done-keywords)))
  8660. (throw 'dont-block t))
  8661. ;; If this task has children, and any are undone, it's blocked
  8662. (save-excursion
  8663. (org-back-to-heading t)
  8664. (let ((this-level (funcall outline-level)))
  8665. (outline-next-heading)
  8666. (let ((child-level (funcall outline-level)))
  8667. (while (and (not (eobp))
  8668. (> child-level this-level))
  8669. ;; this todo has children, check whether they are all
  8670. ;; completed
  8671. (if (and (not (org-entry-is-done-p))
  8672. (org-entry-is-todo-p))
  8673. (throw 'dont-block nil))
  8674. (outline-next-heading)
  8675. (setq child-level (funcall outline-level))))))
  8676. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8677. ;; any previous siblings are undone, it's blocked
  8678. (save-excursion
  8679. (org-back-to-heading t)
  8680. (let* ((pos (point))
  8681. (parent-pos (and (org-up-heading-safe) (point))))
  8682. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8683. (when (and (org-entry-get (point) "ORDERED")
  8684. (forward-line 1)
  8685. (re-search-forward org-not-done-heading-regexp pos t))
  8686. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8687. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8688. (while t
  8689. (goto-char parent-pos)
  8690. (if (not (looking-at org-not-done-heading-regexp))
  8691. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8692. (setq pos (point))
  8693. (setq parent-pos (and (org-up-heading-safe) (point)))
  8694. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8695. (when (and (org-entry-get (point) "ORDERED")
  8696. (forward-line 1)
  8697. (re-search-forward org-not-done-heading-regexp pos t))
  8698. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8699. (defcustom org-track-ordered-property-with-tag nil
  8700. "Should the ORDERED property also be shown as a tag?
  8701. The ORDERED property decides if an entry should require subtasks to be
  8702. completed in sequence. Since a property is not very visible, setting
  8703. this option means that toggling the ORDERED property with the command
  8704. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8705. not relevant for the behavior, but it makes things more visible.
  8706. Note that toggling the tag with tags commands will not change the property
  8707. and therefore not influence behavior!
  8708. This can be t, meaning the tag ORDERED should be used, It can also be a
  8709. string to select a different tag for this task."
  8710. :group 'org-todo
  8711. :type '(choice
  8712. (const :tag "No tracking" nil)
  8713. (const :tag "Track with ORDERED tag" t)
  8714. (string :tag "Use other tag")))
  8715. (defun org-toggle-ordered-property ()
  8716. "Toggle the ORDERED property of the current entry.
  8717. For better visibility, you can track the value of this property with a tag.
  8718. See variable `org-track-ordered-property-with-tag'."
  8719. (interactive)
  8720. (let* ((t1 org-track-ordered-property-with-tag)
  8721. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8722. (save-excursion
  8723. (org-back-to-heading)
  8724. (if (org-entry-get nil "ORDERED")
  8725. (progn
  8726. (org-delete-property "ORDERED")
  8727. (and tag (org-toggle-tag tag 'off))
  8728. (message "Subtasks can be completed in arbitrary order"))
  8729. (org-entry-put nil "ORDERED" "t")
  8730. (and tag (org-toggle-tag tag 'on))
  8731. (message "Subtasks must be completed in sequence")))))
  8732. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8733. (defun org-block-todo-from-checkboxes (change-plist)
  8734. "Block turning an entry into a TODO, using checkboxes.
  8735. This checks whether the current task should be blocked from state
  8736. changes because there are uncheckd boxes in this entry."
  8737. (catch 'dont-block
  8738. ;; If this is not a todo state change, or if this entry is already DONE,
  8739. ;; do not block
  8740. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8741. (member (plist-get change-plist :from)
  8742. (cons 'done org-done-keywords))
  8743. (member (plist-get change-plist :to)
  8744. (cons 'todo org-not-done-keywords)))
  8745. (throw 'dont-block t))
  8746. ;; If this task has checkboxes that are not checked, it's blocked
  8747. (save-excursion
  8748. (org-back-to-heading t)
  8749. (let ((beg (point)) end)
  8750. (outline-next-heading)
  8751. (setq end (point))
  8752. (goto-char beg)
  8753. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8754. end t)
  8755. (progn
  8756. (if (boundp 'org-blocked-by-checkboxes)
  8757. (setq org-blocked-by-checkboxes t))
  8758. (throw 'dont-block nil)))))
  8759. t)) ; do not block
  8760. (defvar org-entry-property-inherited-from) ;; defined below
  8761. (defun org-update-parent-todo-statistics ()
  8762. "Update any statistics cookie in the parent of the current headline.
  8763. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8764. the entire subtree and this will travel up the hierarchy and update
  8765. statistics everywhere."
  8766. (interactive)
  8767. (let* ((lim 0) prop
  8768. (recursive (or (not org-hierarchical-todo-statistics)
  8769. (string-match
  8770. "\\<recursive\\>"
  8771. (or (setq prop (org-entry-get
  8772. nil "COOKIE_DATA" 'inherit)) ""))))
  8773. (lim (or (and prop (marker-position
  8774. org-entry-property-inherited-from))
  8775. lim))
  8776. (first t)
  8777. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8778. level ltoggle l1
  8779. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8780. (catch 'exit
  8781. (save-excursion
  8782. (beginning-of-line 1)
  8783. (if (org-at-heading-p)
  8784. (setq ltoggle (funcall outline-level))
  8785. (error "This should not happen"))
  8786. (while (and (setq level (org-up-heading-safe))
  8787. (or recursive first)
  8788. (>= (point) lim))
  8789. (setq first nil)
  8790. (unless (and level
  8791. (not (string-match
  8792. "\\<checkbox\\>"
  8793. (downcase
  8794. (or (org-entry-get
  8795. nil "COOKIE_DATA")
  8796. "")))))
  8797. (throw 'exit nil))
  8798. (while (re-search-forward box-re (point-at-eol) t)
  8799. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8800. (setq is-percent (match-end 2))
  8801. (save-match-data
  8802. (unless (outline-next-heading) (throw 'exit nil))
  8803. (while (and (looking-at org-complex-heading-regexp)
  8804. (> (setq l1 (length (match-string 1))) level))
  8805. (setq kwd (and (or recursive (= l1 ltoggle))
  8806. (match-string 2)))
  8807. (and kwd (setq cnt-all (1+ cnt-all)))
  8808. (and (member kwd org-done-keywords)
  8809. (setq cnt-done (1+ cnt-done)))
  8810. (outline-next-heading)))
  8811. (replace-match
  8812. (if is-percent
  8813. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8814. (format "[%d/%d]" cnt-done cnt-all)))))
  8815. (when cookie-present
  8816. (run-hook-with-args 'org-after-todo-statistics-hook
  8817. cnt-done (- cnt-all cnt-done)))))
  8818. (run-hooks 'org-todo-statistics-hook)))
  8819. (defvar org-after-todo-statistics-hook nil
  8820. "Hook that is called after a TODO statistics cookie has been updated.
  8821. Each function is called with two arguments: the number of not-done entries
  8822. and the number of done entries.
  8823. For example, the following function, when added to this hook, will switch
  8824. an entry to DONE when all children are done, and back to TODO when new
  8825. entries are set to a TODO status. Note that this hook is only called
  8826. when there is a statistics cookie in the headline!
  8827. (defun org-summary-todo (n-done n-not-done)
  8828. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8829. (let (org-log-done org-log-states) ; turn off logging
  8830. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8831. ")
  8832. (defvar org-todo-statistics-hook nil
  8833. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8834. This hook runs even if there is no statisics cookie present, in which case
  8835. `org-after-todo-statistics-hook' would not run.")
  8836. (defun org-todo-trigger-tag-changes (state)
  8837. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8838. (let ((l org-todo-state-tags-triggers)
  8839. changes)
  8840. (when (or (not state) (equal state ""))
  8841. (setq changes (append changes (cdr (assoc "" l)))))
  8842. (when (and (stringp state) (> (length state) 0))
  8843. (setq changes (append changes (cdr (assoc state l)))))
  8844. (when (member state org-not-done-keywords)
  8845. (setq changes (append changes (cdr (assoc 'todo l)))))
  8846. (when (member state org-done-keywords)
  8847. (setq changes (append changes (cdr (assoc 'done l)))))
  8848. (dolist (c changes)
  8849. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8850. (defun org-local-logging (value)
  8851. "Get logging settings from a property VALUE."
  8852. (let* (words w a)
  8853. ;; directly set the variables, they are already local.
  8854. (setq org-log-done nil
  8855. org-log-repeat nil
  8856. org-todo-log-states nil)
  8857. (setq words (org-split-string value))
  8858. (while (setq w (pop words))
  8859. (cond
  8860. ((setq a (assoc w org-startup-options))
  8861. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8862. (set (nth 1 a) (nth 2 a))))
  8863. ((setq a (org-extract-log-state-settings w))
  8864. (and (member (car a) org-todo-keywords-1)
  8865. (push a org-todo-log-states)))))))
  8866. (defun org-get-todo-sequence-head (kwd)
  8867. "Return the head of the TODO sequence to which KWD belongs.
  8868. If KWD is not set, check if there is a text property remembering the
  8869. right sequence."
  8870. (let (p)
  8871. (cond
  8872. ((not kwd)
  8873. (or (get-text-property (point-at-bol) 'org-todo-head)
  8874. (progn
  8875. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8876. nil (point-at-eol)))
  8877. (get-text-property p 'org-todo-head))))
  8878. ((not (member kwd org-todo-keywords-1))
  8879. (car org-todo-keywords-1))
  8880. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8881. (defun org-fast-todo-selection ()
  8882. "Fast TODO keyword selection with single keys.
  8883. Returns the new TODO keyword, or nil if no state change should occur."
  8884. (let* ((fulltable org-todo-key-alist)
  8885. (done-keywords org-done-keywords) ;; needed for the faces.
  8886. (maxlen (apply 'max (mapcar
  8887. (lambda (x)
  8888. (if (stringp (car x)) (string-width (car x)) 0))
  8889. fulltable)))
  8890. (expert nil)
  8891. (fwidth (+ maxlen 3 1 3))
  8892. (ncol (/ (- (window-width) 4) fwidth))
  8893. tg cnt e c tbl
  8894. groups ingroup)
  8895. (save-excursion
  8896. (save-window-excursion
  8897. (if expert
  8898. (set-buffer (get-buffer-create " *Org todo*"))
  8899. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8900. (erase-buffer)
  8901. (org-set-local 'org-done-keywords done-keywords)
  8902. (setq tbl fulltable cnt 0)
  8903. (while (setq e (pop tbl))
  8904. (cond
  8905. ((equal e '(:startgroup))
  8906. (push '() groups) (setq ingroup t)
  8907. (when (not (= cnt 0))
  8908. (setq cnt 0)
  8909. (insert "\n"))
  8910. (insert "{ "))
  8911. ((equal e '(:endgroup))
  8912. (setq ingroup nil cnt 0)
  8913. (insert "}\n"))
  8914. ((equal e '(:newline))
  8915. (when (not (= cnt 0))
  8916. (setq cnt 0)
  8917. (insert "\n")
  8918. (setq e (car tbl))
  8919. (while (equal (car tbl) '(:newline))
  8920. (insert "\n")
  8921. (setq tbl (cdr tbl)))))
  8922. (t
  8923. (setq tg (car e) c (cdr e))
  8924. (if ingroup (push tg (car groups)))
  8925. (setq tg (org-add-props tg nil 'face
  8926. (org-get-todo-face tg)))
  8927. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8928. (insert "[" c "] " tg (make-string
  8929. (- fwidth 4 (length tg)) ?\ ))
  8930. (when (= (setq cnt (1+ cnt)) ncol)
  8931. (insert "\n")
  8932. (if ingroup (insert " "))
  8933. (setq cnt 0)))))
  8934. (insert "\n")
  8935. (goto-char (point-min))
  8936. (if (not expert) (org-fit-window-to-buffer))
  8937. (message "[a-z..]:Set [SPC]:clear")
  8938. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8939. (cond
  8940. ((or (= c ?\C-g)
  8941. (and (= c ?q) (not (rassoc c fulltable))))
  8942. (setq quit-flag t))
  8943. ((= c ?\ ) nil)
  8944. ((setq e (rassoc c fulltable) tg (car e))
  8945. tg)
  8946. (t (setq quit-flag t)))))))
  8947. (defun org-entry-is-todo-p ()
  8948. (member (org-get-todo-state) org-not-done-keywords))
  8949. (defun org-entry-is-done-p ()
  8950. (member (org-get-todo-state) org-done-keywords))
  8951. (defun org-get-todo-state ()
  8952. (save-excursion
  8953. (org-back-to-heading t)
  8954. (and (looking-at org-todo-line-regexp)
  8955. (match-end 2)
  8956. (match-string 2))))
  8957. (defun org-at-date-range-p (&optional inactive-ok)
  8958. "Is the cursor inside a date range?"
  8959. (interactive)
  8960. (save-excursion
  8961. (catch 'exit
  8962. (let ((pos (point)))
  8963. (skip-chars-backward "^[<\r\n")
  8964. (skip-chars-backward "<[")
  8965. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8966. (>= (match-end 0) pos)
  8967. (throw 'exit t))
  8968. (skip-chars-backward "^<[\r\n")
  8969. (skip-chars-backward "<[")
  8970. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8971. (>= (match-end 0) pos)
  8972. (throw 'exit t)))
  8973. nil)))
  8974. (defun org-get-repeat ()
  8975. "Check if there is a deadline/schedule with repeater in this entry."
  8976. (save-match-data
  8977. (save-excursion
  8978. (org-back-to-heading t)
  8979. (if (re-search-forward
  8980. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8981. (match-string 1)))))
  8982. (defvar org-last-changed-timestamp)
  8983. (defvar org-last-inserted-timestamp)
  8984. (defvar org-log-post-message)
  8985. (defvar org-log-note-purpose)
  8986. (defvar org-log-note-how)
  8987. (defvar org-log-note-extra)
  8988. (defun org-auto-repeat-maybe (done-word)
  8989. "Check if the current headline contains a repeated deadline/schedule.
  8990. If yes, set TODO state back to what it was and change the base date
  8991. of repeating deadline/scheduled time stamps to new date.
  8992. This function is run automatically after each state change to a DONE state."
  8993. ;; last-state is dynamically scoped into this function
  8994. (let* ((repeat (org-get-repeat))
  8995. (aa (assoc last-state org-todo-kwd-alist))
  8996. (interpret (nth 1 aa))
  8997. (head (nth 2 aa))
  8998. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8999. (msg "Entry repeats: ")
  9000. (org-log-done nil)
  9001. (org-todo-log-states nil)
  9002. (nshiftmax 10) (nshift 0)
  9003. re type n what ts time)
  9004. (when repeat
  9005. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9006. (org-todo (if (eq interpret 'type) last-state head))
  9007. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9008. (org-time-stamp-format t t)))
  9009. (when org-log-repeat
  9010. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9011. (memq 'org-add-log-note post-command-hook))
  9012. ;; OK, we are already setup for some record
  9013. (if (eq org-log-repeat 'note)
  9014. ;; make sure we take a note, not only a time stamp
  9015. (setq org-log-note-how 'note))
  9016. ;; Set up for taking a record
  9017. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9018. last-state
  9019. 'findpos org-log-repeat)))
  9020. (org-back-to-heading t)
  9021. (org-add-planning-info nil nil 'closed)
  9022. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9023. org-deadline-time-regexp "\\)\\|\\("
  9024. org-ts-regexp "\\)"))
  9025. (while (re-search-forward
  9026. re (save-excursion (outline-next-heading) (point)) t)
  9027. (setq type (if (match-end 1) org-scheduled-string
  9028. (if (match-end 3) org-deadline-string "Plain:"))
  9029. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9030. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9031. (setq n (string-to-number (match-string 2 ts))
  9032. what (match-string 3 ts))
  9033. (if (equal what "w") (setq n (* n 7) what "d"))
  9034. ;; Preparation, see if we need to modify the start date for the change
  9035. (when (match-end 1)
  9036. (setq time (save-match-data (org-time-string-to-time ts)))
  9037. (cond
  9038. ((equal (match-string 1 ts) ".")
  9039. ;; Shift starting date to today
  9040. (org-timestamp-change
  9041. (- (time-to-days (current-time)) (time-to-days time))
  9042. 'day))
  9043. ((equal (match-string 1 ts) "+")
  9044. (while (or (= nshift 0)
  9045. (<= (time-to-days time) (time-to-days (current-time))))
  9046. (when (= (incf nshift) nshiftmax)
  9047. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9048. (error "Abort")))
  9049. (org-timestamp-change n (cdr (assoc what whata)))
  9050. (org-at-timestamp-p t)
  9051. (setq ts (match-string 1))
  9052. (setq time (save-match-data (org-time-string-to-time ts))))
  9053. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9054. ;; rematch, so that we have everything in place for the real shift
  9055. (org-at-timestamp-p t)
  9056. (setq ts (match-string 1))
  9057. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9058. (org-timestamp-change n (cdr (assoc what whata)))
  9059. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9060. (setq org-log-post-message msg)
  9061. (message "%s" msg))))
  9062. (defun org-show-todo-tree (arg)
  9063. "Make a compact tree which shows all headlines marked with TODO.
  9064. The tree will show the lines where the regexp matches, and all higher
  9065. headlines above the match.
  9066. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9067. With a numeric prefix N, construct a sparse tree for the Nth element
  9068. of `org-todo-keywords-1'."
  9069. (interactive "P")
  9070. (let ((case-fold-search nil)
  9071. (kwd-re
  9072. (cond ((null arg) org-not-done-regexp)
  9073. ((equal arg '(4))
  9074. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  9075. (mapcar 'list org-todo-keywords-1))))
  9076. (concat "\\("
  9077. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9078. "\\)\\>")))
  9079. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9080. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9081. org-todo-keywords-1)))
  9082. (t (error "Invalid prefix argument: %s" arg)))))
  9083. (message "%d TODO entries found"
  9084. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9085. (defun org-deadline (&optional remove time)
  9086. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9087. With argument REMOVE, remove any deadline from the item.
  9088. When TIME is set, it should be an internal time specification, and the
  9089. scheduling will use the corresponding date."
  9090. (interactive "P")
  9091. (if remove
  9092. (progn
  9093. (org-remove-timestamp-with-keyword org-deadline-string)
  9094. (message "Item no longer has a deadline."))
  9095. (if (org-get-repeat)
  9096. (error "Cannot change deadline on task with repeater, please do that by hand")
  9097. (org-add-planning-info 'deadline time 'closed)
  9098. (message "Deadline on %s" org-last-inserted-timestamp))))
  9099. (defun org-schedule (&optional remove time)
  9100. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9101. With argument REMOVE, remove any scheduling date from the item.
  9102. When TIME is set, it should be an internal time specification, and the
  9103. scheduling will use the corresponding date."
  9104. (interactive "P")
  9105. (if remove
  9106. (progn
  9107. (org-remove-timestamp-with-keyword org-scheduled-string)
  9108. (message "Item is no longer scheduled."))
  9109. (if (org-get-repeat)
  9110. (error "Cannot reschedule task with repeater, please do that by hand")
  9111. (org-add-planning-info 'scheduled time 'closed)
  9112. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9113. (defun org-get-scheduled-time (pom &optional inherit)
  9114. "Get the scheduled time as a time tuple, of a format suitable
  9115. for calling org-schedule with, or if there is no scheduling,
  9116. returns nil."
  9117. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9118. (when time
  9119. (apply 'encode-time (org-parse-time-string time)))))
  9120. (defun org-get-deadline-time (pom &optional inherit)
  9121. "Get the deadine as a time tuple, of a format suitable for
  9122. calling org-deadlin with, or if there is no scheduling, returns
  9123. nil."
  9124. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9125. (when time
  9126. (apply 'encode-time (org-parse-time-string time)))))
  9127. (defun org-remove-timestamp-with-keyword (keyword)
  9128. "Remove all time stamps with KEYWORD in the current entry."
  9129. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9130. beg)
  9131. (save-excursion
  9132. (org-back-to-heading t)
  9133. (setq beg (point))
  9134. (org-end-of-subtree t t)
  9135. (while (re-search-backward re beg t)
  9136. (replace-match "")
  9137. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9138. (equal (char-before) ?\ ))
  9139. (backward-delete-char 1)
  9140. (if (string-match "^[ \t]*$" (buffer-substring
  9141. (point-at-bol) (point-at-eol)))
  9142. (delete-region (point-at-bol)
  9143. (min (point-max) (1+ (point-at-eol))))))))))
  9144. (defun org-add-planning-info (what &optional time &rest remove)
  9145. "Insert new timestamp with keyword in the line directly after the headline.
  9146. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9147. If non is given, the user is prompted for a date.
  9148. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9149. be removed."
  9150. (interactive)
  9151. (let (org-time-was-given org-end-time-was-given ts
  9152. end default-time default-input)
  9153. (catch 'exit
  9154. (when (and (not time) (memq what '(scheduled deadline)))
  9155. ;; Try to get a default date/time from existing timestamp
  9156. (save-excursion
  9157. (org-back-to-heading t)
  9158. (setq end (save-excursion (outline-next-heading) (point)))
  9159. (when (re-search-forward (if (eq what 'scheduled)
  9160. org-scheduled-time-regexp
  9161. org-deadline-time-regexp)
  9162. end t)
  9163. (setq ts (match-string 1)
  9164. default-time
  9165. (apply 'encode-time (org-parse-time-string ts))
  9166. default-input (and ts (org-get-compact-tod ts))))))
  9167. (when what
  9168. ;; If necessary, get the time from the user
  9169. (setq time (or time (org-read-date nil 'to-time nil nil
  9170. default-time default-input))))
  9171. (when (and org-insert-labeled-timestamps-at-point
  9172. (member what '(scheduled deadline)))
  9173. (insert
  9174. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9175. (org-insert-time-stamp time org-time-was-given
  9176. nil nil nil (list org-end-time-was-given))
  9177. (setq what nil))
  9178. (save-excursion
  9179. (save-restriction
  9180. (let (col list elt ts buffer-invisibility-spec)
  9181. (org-back-to-heading t)
  9182. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9183. (goto-char (match-end 1))
  9184. (setq col (current-column))
  9185. (goto-char (match-end 0))
  9186. (if (eobp) (insert "\n") (forward-char 1))
  9187. (when (and (not what)
  9188. (not (looking-at
  9189. (concat "[ \t]*"
  9190. org-keyword-time-not-clock-regexp))))
  9191. ;; Nothing to add, nothing to remove...... :-)
  9192. (throw 'exit nil))
  9193. (if (and (not (looking-at outline-regexp))
  9194. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9195. "[^\r\n]*"))
  9196. (not (equal (match-string 1) org-clock-string)))
  9197. (narrow-to-region (match-beginning 0) (match-end 0))
  9198. (insert-before-markers "\n")
  9199. (backward-char 1)
  9200. (narrow-to-region (point) (point))
  9201. (and org-adapt-indentation (org-indent-to-column col)))
  9202. ;; Check if we have to remove something.
  9203. (setq list (cons what remove))
  9204. (while list
  9205. (setq elt (pop list))
  9206. (goto-char (point-min))
  9207. (when (or (and (eq elt 'scheduled)
  9208. (re-search-forward org-scheduled-time-regexp nil t))
  9209. (and (eq elt 'deadline)
  9210. (re-search-forward org-deadline-time-regexp nil t))
  9211. (and (eq elt 'closed)
  9212. (re-search-forward org-closed-time-regexp nil t)))
  9213. (replace-match "")
  9214. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9215. (if (looking-at " +") (replace-match ""))))
  9216. (goto-char (point-max))
  9217. (when what
  9218. (insert
  9219. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9220. (cond ((eq what 'scheduled) org-scheduled-string)
  9221. ((eq what 'deadline) org-deadline-string)
  9222. ((eq what 'closed) org-closed-string))
  9223. " ")
  9224. (setq ts (org-insert-time-stamp
  9225. time
  9226. (or org-time-was-given
  9227. (and (eq what 'closed) org-log-done-with-time))
  9228. (eq what 'closed)
  9229. nil nil (list org-end-time-was-given)))
  9230. (end-of-line 1))
  9231. (goto-char (point-min))
  9232. (widen)
  9233. (if (and (looking-at "[ \t]+\n")
  9234. (equal (char-before) ?\n))
  9235. (delete-region (1- (point)) (point-at-eol)))
  9236. ts))))))
  9237. (defvar org-log-note-marker (make-marker))
  9238. (defvar org-log-note-purpose nil)
  9239. (defvar org-log-note-state nil)
  9240. (defvar org-log-note-previous-state nil)
  9241. (defvar org-log-note-how nil)
  9242. (defvar org-log-note-extra nil)
  9243. (defvar org-log-note-window-configuration nil)
  9244. (defvar org-log-note-return-to (make-marker))
  9245. (defvar org-log-post-message nil
  9246. "Message to be displayed after a log note has been stored.
  9247. The auto-repeater uses this.")
  9248. (defun org-add-note ()
  9249. "Add a note to the current entry.
  9250. This is done in the same way as adding a state change note."
  9251. (interactive)
  9252. (org-add-log-setup 'note nil nil 'findpos nil))
  9253. (defvar org-property-end-re)
  9254. (defun org-add-log-setup (&optional purpose state prev-state
  9255. findpos how &optional extra)
  9256. "Set up the post command hook to take a note.
  9257. If this is about to TODO state change, the new state is expected in STATE.
  9258. When FINDPOS is non-nil, find the correct position for the note in
  9259. the current entry. If not, assume that it can be inserted at point.
  9260. HOW is an indicator what kind of note should be created.
  9261. EXTRA is additional text that will be inserted into the notes buffer."
  9262. (let* ((org-log-into-drawer (org-log-into-drawer))
  9263. (drawer (cond ((stringp org-log-into-drawer)
  9264. org-log-into-drawer)
  9265. (org-log-into-drawer "LOGBOOK")
  9266. (t nil))))
  9267. (save-restriction
  9268. (save-excursion
  9269. (when findpos
  9270. (org-back-to-heading t)
  9271. (narrow-to-region (point) (save-excursion
  9272. (outline-next-heading) (point)))
  9273. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9274. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9275. "[^\r\n]*\\)?"))
  9276. (goto-char (match-end 0))
  9277. (cond
  9278. (drawer
  9279. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9280. nil t)
  9281. (progn
  9282. (goto-char (match-end 0))
  9283. (or org-log-states-order-reversed
  9284. (and (re-search-forward org-property-end-re nil t)
  9285. (goto-char (1- (match-beginning 0))))))
  9286. (insert "\n:" drawer ":\n:END:")
  9287. (beginning-of-line 0)
  9288. (org-indent-line-function)
  9289. (beginning-of-line 2)
  9290. (org-indent-line-function)
  9291. (end-of-line 0)))
  9292. ((and org-log-state-notes-insert-after-drawers
  9293. (save-excursion
  9294. (forward-line) (looking-at org-drawer-regexp)))
  9295. (forward-line)
  9296. (while (looking-at org-drawer-regexp)
  9297. (goto-char (match-end 0))
  9298. (re-search-forward org-property-end-re (point-max) t)
  9299. (forward-line))
  9300. (forward-line -1)))
  9301. (unless org-log-states-order-reversed
  9302. (and (= (char-after) ?\n) (forward-char 1))
  9303. (org-skip-over-state-notes)
  9304. (skip-chars-backward " \t\n\r")))
  9305. (move-marker org-log-note-marker (point))
  9306. (setq org-log-note-purpose purpose
  9307. org-log-note-state state
  9308. org-log-note-previous-state prev-state
  9309. org-log-note-how how
  9310. org-log-note-extra extra)
  9311. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9312. (defun org-skip-over-state-notes ()
  9313. "Skip past the list of State notes in an entry."
  9314. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9315. (while (looking-at "[ \t]*- State")
  9316. (condition-case nil
  9317. (org-next-item)
  9318. (error (org-end-of-item)))))
  9319. (defun org-add-log-note (&optional purpose)
  9320. "Pop up a window for taking a note, and add this note later at point."
  9321. (remove-hook 'post-command-hook 'org-add-log-note)
  9322. (setq org-log-note-window-configuration (current-window-configuration))
  9323. (delete-other-windows)
  9324. (move-marker org-log-note-return-to (point))
  9325. (switch-to-buffer (marker-buffer org-log-note-marker))
  9326. (goto-char org-log-note-marker)
  9327. (org-switch-to-buffer-other-window "*Org Note*")
  9328. (erase-buffer)
  9329. (if (memq org-log-note-how '(time state))
  9330. (let (current-prefix-arg) (org-store-log-note))
  9331. (let ((org-inhibit-startup t)) (org-mode))
  9332. (insert (format "# Insert note for %s.
  9333. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9334. (cond
  9335. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9336. ((eq org-log-note-purpose 'done) "closed todo item")
  9337. ((eq org-log-note-purpose 'state)
  9338. (format "state change from \"%s\" to \"%s\""
  9339. (or org-log-note-previous-state "")
  9340. (or org-log-note-state "")))
  9341. ((eq org-log-note-purpose 'note)
  9342. "this entry")
  9343. (t (error "This should not happen")))))
  9344. (if org-log-note-extra (insert org-log-note-extra))
  9345. (org-set-local 'org-finish-function 'org-store-log-note)))
  9346. (defvar org-note-abort nil) ; dynamically scoped
  9347. (defun org-store-log-note ()
  9348. "Finish taking a log note, and insert it to where it belongs."
  9349. (let ((txt (buffer-string))
  9350. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9351. lines ind)
  9352. (kill-buffer (current-buffer))
  9353. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9354. (setq txt (replace-match "" t t txt)))
  9355. (if (string-match "\\s-+\\'" txt)
  9356. (setq txt (replace-match "" t t txt)))
  9357. (setq lines (org-split-string txt "\n"))
  9358. (when (and note (string-match "\\S-" note))
  9359. (setq note
  9360. (org-replace-escapes
  9361. note
  9362. (list (cons "%u" (user-login-name))
  9363. (cons "%U" user-full-name)
  9364. (cons "%t" (format-time-string
  9365. (org-time-stamp-format 'long 'inactive)
  9366. (current-time)))
  9367. (cons "%s" (if org-log-note-state
  9368. (concat "\"" org-log-note-state "\"")
  9369. ""))
  9370. (cons "%S" (if org-log-note-previous-state
  9371. (concat "\"" org-log-note-previous-state "\"")
  9372. "\"\"")))))
  9373. (if lines (setq note (concat note " \\\\")))
  9374. (push note lines))
  9375. (when (or current-prefix-arg org-note-abort)
  9376. (when org-log-into-drawer
  9377. (org-remove-empty-drawer-at
  9378. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9379. org-log-note-marker))
  9380. (setq lines nil))
  9381. (when lines
  9382. (save-excursion
  9383. (set-buffer (marker-buffer org-log-note-marker))
  9384. (save-excursion
  9385. (goto-char org-log-note-marker)
  9386. (move-marker org-log-note-marker nil)
  9387. (end-of-line 1)
  9388. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9389. (insert "- " (pop lines))
  9390. (org-indent-line-function)
  9391. (beginning-of-line 1)
  9392. (looking-at "[ \t]*")
  9393. (setq ind (concat (match-string 0) " "))
  9394. (end-of-line 1)
  9395. (while lines (insert "\n" ind (pop lines)))
  9396. (message "Note stored")
  9397. (org-back-to-heading t)
  9398. (org-cycle-hide-drawers 'children)))))
  9399. (set-window-configuration org-log-note-window-configuration)
  9400. (with-current-buffer (marker-buffer org-log-note-return-to)
  9401. (goto-char org-log-note-return-to))
  9402. (move-marker org-log-note-return-to nil)
  9403. (and org-log-post-message (message "%s" org-log-post-message)))
  9404. (defun org-remove-empty-drawer-at (drawer pos)
  9405. "Remove an emptyr DARWER drawer at position POS.
  9406. POS may also be a marker."
  9407. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9408. (save-excursion
  9409. (save-restriction
  9410. (widen)
  9411. (goto-char pos)
  9412. (if (org-in-regexp
  9413. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9414. (replace-match ""))))))
  9415. (defun org-sparse-tree (&optional arg)
  9416. "Create a sparse tree, prompt for the details.
  9417. This command can create sparse trees. You first need to select the type
  9418. of match used to create the tree:
  9419. t Show entries with a specific TODO keyword.
  9420. m Show entries selected by a tags/property match.
  9421. p Enter a property name and its value (both with completion on existing
  9422. names/values) and show entries with that property.
  9423. r Show entries matching a regular expression.
  9424. d Show deadlines due within `org-deadline-warning-days'.
  9425. b Show deadlines and scheduled items before a date.
  9426. a Show deadlines and scheduled items after a date."
  9427. (interactive "P")
  9428. (let (ans kwd value)
  9429. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9430. (setq ans (read-char-exclusive))
  9431. (cond
  9432. ((equal ans ?d)
  9433. (call-interactively 'org-check-deadlines))
  9434. ((equal ans ?b)
  9435. (call-interactively 'org-check-before-date))
  9436. ((equal ans ?a)
  9437. (call-interactively 'org-check-after-date))
  9438. ((equal ans ?t)
  9439. (org-show-todo-tree '(4)))
  9440. ((member ans '(?T ?m))
  9441. (call-interactively 'org-match-sparse-tree))
  9442. ((member ans '(?p ?P))
  9443. (setq kwd (org-ido-completing-read "Property: "
  9444. (mapcar 'list (org-buffer-property-keys))))
  9445. (setq value (org-ido-completing-read "Value: "
  9446. (mapcar 'list (org-property-values kwd))))
  9447. (unless (string-match "\\`{.*}\\'" value)
  9448. (setq value (concat "\"" value "\"")))
  9449. (org-match-sparse-tree arg (concat kwd "=" value)))
  9450. ((member ans '(?r ?R ?/))
  9451. (call-interactively 'org-occur))
  9452. (t (error "No such sparse tree command \"%c\"" ans)))))
  9453. (defvar org-occur-highlights nil
  9454. "List of overlays used for occur matches.")
  9455. (make-variable-buffer-local 'org-occur-highlights)
  9456. (defvar org-occur-parameters nil
  9457. "Parameters of the active org-occur calls.
  9458. This is a list, each call to org-occur pushes as cons cell,
  9459. containing the regular expression and the callback, onto the list.
  9460. The list can contain several entries if `org-occur' has been called
  9461. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9462. will only contain one set of parameters. When the highlights are
  9463. removed (for example with `C-c C-c', or with the next edit (depending
  9464. on `org-remove-highlights-with-change'), this variable is emptied
  9465. as well.")
  9466. (make-variable-buffer-local 'org-occur-parameters)
  9467. (defun org-occur (regexp &optional keep-previous callback)
  9468. "Make a compact tree which shows all matches of REGEXP.
  9469. The tree will show the lines where the regexp matches, and all higher
  9470. headlines above the match. It will also show the heading after the match,
  9471. to make sure editing the matching entry is easy.
  9472. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9473. call to `org-occur' will be kept, to allow stacking of calls to this
  9474. command.
  9475. If CALLBACK is non-nil, it is a function which is called to confirm
  9476. that the match should indeed be shown."
  9477. (interactive "sRegexp: \nP")
  9478. (unless keep-previous
  9479. (org-remove-occur-highlights nil nil t))
  9480. (push (cons regexp callback) org-occur-parameters)
  9481. (let ((cnt 0))
  9482. (save-excursion
  9483. (goto-char (point-min))
  9484. (if (or (not keep-previous) ; do not want to keep
  9485. (not org-occur-highlights)) ; no previous matches
  9486. ;; hide everything
  9487. (org-overview))
  9488. (while (re-search-forward regexp nil t)
  9489. (when (or (not callback)
  9490. (save-match-data (funcall callback)))
  9491. (setq cnt (1+ cnt))
  9492. (when org-highlight-sparse-tree-matches
  9493. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9494. (org-show-context 'occur-tree))))
  9495. (when org-remove-highlights-with-change
  9496. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9497. nil 'local))
  9498. (unless org-sparse-tree-open-archived-trees
  9499. (org-hide-archived-subtrees (point-min) (point-max)))
  9500. (run-hooks 'org-occur-hook)
  9501. (if (interactive-p)
  9502. (message "%d match(es) for regexp %s" cnt regexp))
  9503. cnt))
  9504. (defun org-show-context (&optional key)
  9505. "Make sure point and context and visible.
  9506. How much context is shown depends upon the variables
  9507. `org-show-hierarchy-above', `org-show-following-heading'. and
  9508. `org-show-siblings'."
  9509. (let ((heading-p (org-on-heading-p t))
  9510. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9511. (following-p (org-get-alist-option org-show-following-heading key))
  9512. (entry-p (org-get-alist-option org-show-entry-below key))
  9513. (siblings-p (org-get-alist-option org-show-siblings key)))
  9514. (catch 'exit
  9515. ;; Show heading or entry text
  9516. (if (and heading-p (not entry-p))
  9517. (org-flag-heading nil) ; only show the heading
  9518. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9519. (org-show-hidden-entry))) ; show entire entry
  9520. (when following-p
  9521. ;; Show next sibling, or heading below text
  9522. (save-excursion
  9523. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9524. (org-flag-heading nil))))
  9525. (when siblings-p (org-show-siblings))
  9526. (when hierarchy-p
  9527. ;; show all higher headings, possibly with siblings
  9528. (save-excursion
  9529. (while (and (condition-case nil
  9530. (progn (org-up-heading-all 1) t)
  9531. (error nil))
  9532. (not (bobp)))
  9533. (org-flag-heading nil)
  9534. (when siblings-p (org-show-siblings))))))))
  9535. (defun org-reveal (&optional siblings)
  9536. "Show current entry, hierarchy above it, and the following headline.
  9537. This can be used to show a consistent set of context around locations
  9538. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9539. not t for the search context.
  9540. With optional argument SIBLINGS, on each level of the hierarchy all
  9541. siblings are shown. This repairs the tree structure to what it would
  9542. look like when opened with hierarchical calls to `org-cycle'."
  9543. (interactive "P")
  9544. (let ((org-show-hierarchy-above t)
  9545. (org-show-following-heading t)
  9546. (org-show-siblings (if siblings t org-show-siblings)))
  9547. (org-show-context nil)))
  9548. (defun org-highlight-new-match (beg end)
  9549. "Highlight from BEG to END and mark the highlight is an occur headline."
  9550. (let ((ov (org-make-overlay beg end)))
  9551. (org-overlay-put ov 'face 'secondary-selection)
  9552. (push ov org-occur-highlights)))
  9553. (defun org-remove-occur-highlights (&optional beg end noremove)
  9554. "Remove the occur highlights from the buffer.
  9555. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9556. from the `before-change-functions' in the current buffer."
  9557. (interactive)
  9558. (unless org-inhibit-highlight-removal
  9559. (mapc 'org-delete-overlay org-occur-highlights)
  9560. (setq org-occur-highlights nil)
  9561. (setq org-occur-parameters nil)
  9562. (unless noremove
  9563. (remove-hook 'before-change-functions
  9564. 'org-remove-occur-highlights 'local))))
  9565. ;;;; Priorities
  9566. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9567. "Regular expression matching the priority indicator.")
  9568. (defvar org-remove-priority-next-time nil)
  9569. (defun org-priority-up ()
  9570. "Increase the priority of the current item."
  9571. (interactive)
  9572. (org-priority 'up))
  9573. (defun org-priority-down ()
  9574. "Decrease the priority of the current item."
  9575. (interactive)
  9576. (org-priority 'down))
  9577. (defun org-priority (&optional action)
  9578. "Change the priority of an item by ARG.
  9579. ACTION can be `set', `up', `down', or a character."
  9580. (interactive)
  9581. (unless org-enable-priority-commands
  9582. (error "Priority commands are disabled"))
  9583. (setq action (or action 'set))
  9584. (let (current new news have remove)
  9585. (save-excursion
  9586. (org-back-to-heading t)
  9587. (if (looking-at org-priority-regexp)
  9588. (setq current (string-to-char (match-string 2))
  9589. have t)
  9590. (setq current org-default-priority))
  9591. (cond
  9592. ((or (eq action 'set)
  9593. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9594. (if (not (eq action 'set))
  9595. (setq new action)
  9596. (message "Priority %c-%c, SPC to remove: "
  9597. org-highest-priority org-lowest-priority)
  9598. (setq new (read-char-exclusive)))
  9599. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9600. (= (upcase org-lowest-priority) org-lowest-priority))
  9601. (setq new (upcase new)))
  9602. (cond ((equal new ?\ ) (setq remove t))
  9603. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9604. (error "Priority must be between `%c' and `%c'"
  9605. org-highest-priority org-lowest-priority))))
  9606. ((eq action 'up)
  9607. (if (and (not have) (eq last-command this-command))
  9608. (setq new org-lowest-priority)
  9609. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9610. org-default-priority (1- current)))))
  9611. ((eq action 'down)
  9612. (if (and (not have) (eq last-command this-command))
  9613. (setq new org-highest-priority)
  9614. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9615. org-default-priority (1+ current)))))
  9616. (t (error "Invalid action")))
  9617. (if (or (< (upcase new) org-highest-priority)
  9618. (> (upcase new) org-lowest-priority))
  9619. (setq remove t))
  9620. (setq news (format "%c" new))
  9621. (if have
  9622. (if remove
  9623. (replace-match "" t t nil 1)
  9624. (replace-match news t t nil 2))
  9625. (if remove
  9626. (error "No priority cookie found in line")
  9627. (let ((case-fold-search nil))
  9628. (looking-at org-todo-line-regexp))
  9629. (if (match-end 2)
  9630. (progn
  9631. (goto-char (match-end 2))
  9632. (insert " [#" news "]"))
  9633. (goto-char (match-beginning 3))
  9634. (insert "[#" news "] ")))))
  9635. (org-preserve-lc (org-set-tags nil 'align))
  9636. (if remove
  9637. (message "Priority removed")
  9638. (message "Priority of current item set to %s" news))))
  9639. (defun org-get-priority (s)
  9640. "Find priority cookie and return priority."
  9641. (save-match-data
  9642. (if (not (string-match org-priority-regexp s))
  9643. (* 1000 (- org-lowest-priority org-default-priority))
  9644. (* 1000 (- org-lowest-priority
  9645. (string-to-char (match-string 2 s)))))))
  9646. ;;;; Tags
  9647. (defvar org-agenda-archives-mode)
  9648. (defvar org-map-continue-from nil
  9649. "Position from where mapping should continue.
  9650. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9651. (defvar org-scanner-tags nil
  9652. "The current tag list while the tags scanner is running.")
  9653. (defvar org-trust-scanner-tags nil
  9654. "Should `org-get-tags-at' use the tags fro the scanner.
  9655. This is for internal dynamical scoping only.
  9656. When this is non-nil, the function `org-get-tags-at' will return the value
  9657. of `org-scanner-tags' instead of building the list by itself. This
  9658. can lead to large speed-ups when the tags scanner is used in a file with
  9659. many entries, and when the list of tags is retrieved, for example to
  9660. obtain a list of properties. Building the tags list for each entry in such
  9661. a file becomes an N^2 operation - but with this variable set, it scales
  9662. as N.")
  9663. (defun org-scan-tags (action matcher &optional todo-only)
  9664. "Scan headline tags with inheritance and produce output ACTION.
  9665. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9666. or `agenda' to produce an entry list for an agenda view. It can also be
  9667. a Lisp form or a function that should be called at each matched headline, in
  9668. this case the return value is a list of all return values from these calls.
  9669. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9670. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9671. only lines with a TODO keyword are included in the output."
  9672. (require 'org-agenda)
  9673. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9674. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9675. (org-re
  9676. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9677. (props (list 'face 'default
  9678. 'done-face 'org-agenda-done
  9679. 'undone-face 'default
  9680. 'mouse-face 'highlight
  9681. 'org-not-done-regexp org-not-done-regexp
  9682. 'org-todo-regexp org-todo-regexp
  9683. 'keymap org-agenda-keymap
  9684. 'help-echo
  9685. (format "mouse-2 or RET jump to org file %s"
  9686. (abbreviate-file-name
  9687. (or (buffer-file-name (buffer-base-buffer))
  9688. (buffer-name (buffer-base-buffer)))))))
  9689. (case-fold-search nil)
  9690. (org-map-continue-from nil)
  9691. lspos tags tags-list
  9692. (tags-alist (list (cons 0 org-file-tags)))
  9693. (llast 0) rtn rtn1 level category i txt
  9694. todo marker entry priority)
  9695. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9696. (setq action (list 'lambda nil action)))
  9697. (save-excursion
  9698. (goto-char (point-min))
  9699. (when (eq action 'sparse-tree)
  9700. (org-overview)
  9701. (org-remove-occur-highlights))
  9702. (while (re-search-forward re nil t)
  9703. (catch :skip
  9704. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9705. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9706. (goto-char (setq lspos (match-beginning 0)))
  9707. (setq level (org-reduced-level (funcall outline-level))
  9708. category (org-get-category))
  9709. (setq i llast llast level)
  9710. ;; remove tag lists from same and sublevels
  9711. (while (>= i level)
  9712. (when (setq entry (assoc i tags-alist))
  9713. (setq tags-alist (delete entry tags-alist)))
  9714. (setq i (1- i)))
  9715. ;; add the next tags
  9716. (when tags
  9717. (setq tags (org-split-string tags ":")
  9718. tags-alist
  9719. (cons (cons level tags) tags-alist)))
  9720. ;; compile tags for current headline
  9721. (setq tags-list
  9722. (if org-use-tag-inheritance
  9723. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9724. tags)
  9725. org-scanner-tags tags-list)
  9726. (when org-use-tag-inheritance
  9727. (setcdr (car tags-alist)
  9728. (mapcar (lambda (x)
  9729. (setq x (copy-sequence x))
  9730. (org-add-prop-inherited x))
  9731. (cdar tags-alist))))
  9732. (when (and tags org-use-tag-inheritance
  9733. (or (not (eq t org-use-tag-inheritance))
  9734. org-tags-exclude-from-inheritance))
  9735. ;; selective inheritance, remove uninherited ones
  9736. (setcdr (car tags-alist)
  9737. (org-remove-uniherited-tags (cdar tags-alist))))
  9738. (when (and (or (not todo-only)
  9739. (and (member todo org-not-done-keywords)
  9740. (or (not org-agenda-tags-todo-honor-ignore-options)
  9741. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9742. (let ((case-fold-search t)) (eval matcher))
  9743. (or
  9744. (not (member org-archive-tag tags-list))
  9745. ;; we have an archive tag, should we use this anyway?
  9746. (or (not org-agenda-skip-archived-trees)
  9747. (and (eq action 'agenda) org-agenda-archives-mode))))
  9748. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9749. ;; select this headline
  9750. (cond
  9751. ((eq action 'sparse-tree)
  9752. (and org-highlight-sparse-tree-matches
  9753. (org-get-heading) (match-end 0)
  9754. (org-highlight-new-match
  9755. (match-beginning 0) (match-beginning 1)))
  9756. (org-show-context 'tags-tree))
  9757. ((eq action 'agenda)
  9758. (setq txt (org-format-agenda-item
  9759. ""
  9760. (concat
  9761. (if (eq org-tags-match-list-sublevels 'indented)
  9762. (make-string (1- level) ?.) "")
  9763. (org-get-heading))
  9764. category
  9765. tags-list
  9766. )
  9767. priority (org-get-priority txt))
  9768. (goto-char lspos)
  9769. (setq marker (org-agenda-new-marker))
  9770. (org-add-props txt props
  9771. 'org-marker marker 'org-hd-marker marker 'org-category category
  9772. 'todo-state todo
  9773. 'priority priority 'type "tagsmatch")
  9774. (push txt rtn))
  9775. ((functionp action)
  9776. (setq org-map-continue-from nil)
  9777. (save-excursion
  9778. (setq rtn1 (funcall action))
  9779. (push rtn1 rtn)))
  9780. (t (error "Invalid action")))
  9781. ;; if we are to skip sublevels, jump to end of subtree
  9782. (unless org-tags-match-list-sublevels
  9783. (org-end-of-subtree t)
  9784. (backward-char 1))))
  9785. ;; Get the correct position from where to continue
  9786. (if org-map-continue-from
  9787. (goto-char org-map-continue-from)
  9788. (and (= (point) lspos) (end-of-line 1)))))
  9789. (when (and (eq action 'sparse-tree)
  9790. (not org-sparse-tree-open-archived-trees))
  9791. (org-hide-archived-subtrees (point-min) (point-max)))
  9792. (nreverse rtn)))
  9793. (defun org-remove-uniherited-tags (tags)
  9794. "Remove all tags that are not inherited from the list TAGS."
  9795. (cond
  9796. ((eq org-use-tag-inheritance t)
  9797. (if org-tags-exclude-from-inheritance
  9798. (org-delete-all org-tags-exclude-from-inheritance tags)
  9799. tags))
  9800. ((not org-use-tag-inheritance) nil)
  9801. ((stringp org-use-tag-inheritance)
  9802. (delq nil (mapcar
  9803. (lambda (x)
  9804. (if (and (string-match org-use-tag-inheritance x)
  9805. (not (member x org-tags-exclude-from-inheritance)))
  9806. x nil))
  9807. tags)))
  9808. ((listp org-use-tag-inheritance)
  9809. (delq nil (mapcar
  9810. (lambda (x)
  9811. (if (member x org-use-tag-inheritance) x nil))
  9812. tags)))))
  9813. (defvar todo-only) ;; dynamically scoped
  9814. (defun org-match-sparse-tree (&optional todo-only match)
  9815. "Create a sparse tree according to tags string MATCH.
  9816. MATCH can contain positive and negative selection of tags, like
  9817. \"+WORK+URGENT-WITHBOSS\".
  9818. If optional argument TODO-ONLY is non-nil, only select lines that are
  9819. also TODO lines."
  9820. (interactive "P")
  9821. (org-prepare-agenda-buffers (list (current-buffer)))
  9822. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9823. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9824. (defvar org-cached-props nil)
  9825. (defun org-cached-entry-get (pom property)
  9826. (if (or (eq t org-use-property-inheritance)
  9827. (and (stringp org-use-property-inheritance)
  9828. (string-match org-use-property-inheritance property))
  9829. (and (listp org-use-property-inheritance)
  9830. (member property org-use-property-inheritance)))
  9831. ;; Caching is not possible, check it directly
  9832. (org-entry-get pom property 'inherit)
  9833. ;; Get all properties, so that we can do complicated checks easily
  9834. (cdr (assoc property (or org-cached-props
  9835. (setq org-cached-props
  9836. (org-entry-properties pom)))))))
  9837. (defun org-global-tags-completion-table (&optional files)
  9838. "Return the list of all tags in all agenda buffer/files."
  9839. (save-excursion
  9840. (org-uniquify
  9841. (delq nil
  9842. (apply 'append
  9843. (mapcar
  9844. (lambda (file)
  9845. (set-buffer (find-file-noselect file))
  9846. (append (org-get-buffer-tags)
  9847. (mapcar (lambda (x) (if (stringp (car-safe x))
  9848. (list (car-safe x)) nil))
  9849. org-tag-alist)))
  9850. (if (and files (car files))
  9851. files
  9852. (org-agenda-files))))))))
  9853. (defun org-make-tags-matcher (match)
  9854. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9855. ;; todo-only is scoped dynamically into this function, and the function
  9856. ;; may change it if the matcher asks for it.
  9857. (unless match
  9858. ;; Get a new match request, with completion
  9859. (let ((org-last-tags-completion-table
  9860. (org-global-tags-completion-table)))
  9861. (setq match (org-completing-read-no-ido
  9862. "Match: " 'org-tags-completion-function nil nil nil
  9863. 'org-tags-history))))
  9864. ;; Parse the string and create a lisp form
  9865. (let ((match0 match)
  9866. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9867. minus tag mm
  9868. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9869. orterms term orlist re-p str-p level-p level-op time-p
  9870. prop-p pn pv po cat-p gv rest)
  9871. (if (string-match "/+" match)
  9872. ;; match contains also a todo-matching request
  9873. (progn
  9874. (setq tagsmatch (substring match 0 (match-beginning 0))
  9875. todomatch (substring match (match-end 0)))
  9876. (if (string-match "^!" todomatch)
  9877. (setq todo-only t todomatch (substring todomatch 1)))
  9878. (if (string-match "^\\s-*$" todomatch)
  9879. (setq todomatch nil)))
  9880. ;; only matching tags
  9881. (setq tagsmatch match todomatch nil))
  9882. ;; Make the tags matcher
  9883. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9884. (setq tagsmatcher t)
  9885. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9886. (while (setq term (pop orterms))
  9887. (while (and (equal (substring term -1) "\\") orterms)
  9888. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9889. (while (string-match re term)
  9890. (setq rest (substring term (match-end 0))
  9891. minus (and (match-end 1)
  9892. (equal (match-string 1 term) "-"))
  9893. tag (match-string 2 term)
  9894. re-p (equal (string-to-char tag) ?{)
  9895. level-p (match-end 4)
  9896. prop-p (match-end 5)
  9897. mm (cond
  9898. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9899. (level-p
  9900. (setq level-op (org-op-to-function (match-string 3 term)))
  9901. `(,level-op level ,(string-to-number
  9902. (match-string 4 term))))
  9903. (prop-p
  9904. (setq pn (match-string 5 term)
  9905. po (match-string 6 term)
  9906. pv (match-string 7 term)
  9907. cat-p (equal pn "CATEGORY")
  9908. re-p (equal (string-to-char pv) ?{)
  9909. str-p (equal (string-to-char pv) ?\")
  9910. time-p (save-match-data
  9911. (string-match "^\"[[<].*[]>]\"$" pv))
  9912. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9913. (if time-p (setq pv (org-matcher-time pv)))
  9914. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9915. (cond
  9916. ((equal pn "CATEGORY")
  9917. (setq gv '(get-text-property (point) 'org-category)))
  9918. ((equal pn "TODO")
  9919. (setq gv 'todo))
  9920. (t
  9921. (setq gv `(org-cached-entry-get nil ,pn))))
  9922. (if re-p
  9923. (if (eq po 'org<>)
  9924. `(not (string-match ,pv (or ,gv "")))
  9925. `(string-match ,pv (or ,gv "")))
  9926. (if str-p
  9927. `(,po (or ,gv "") ,pv)
  9928. `(,po (string-to-number (or ,gv ""))
  9929. ,(string-to-number pv) ))))
  9930. (t `(member ,tag tags-list)))
  9931. mm (if minus (list 'not mm) mm)
  9932. term rest)
  9933. (push mm tagsmatcher))
  9934. (push (if (> (length tagsmatcher) 1)
  9935. (cons 'and tagsmatcher)
  9936. (car tagsmatcher))
  9937. orlist)
  9938. (setq tagsmatcher nil))
  9939. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9940. (setq tagsmatcher
  9941. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9942. ;; Make the todo matcher
  9943. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9944. (setq todomatcher t)
  9945. (setq orterms (org-split-string todomatch "|") orlist nil)
  9946. (while (setq term (pop orterms))
  9947. (while (string-match re term)
  9948. (setq minus (and (match-end 1)
  9949. (equal (match-string 1 term) "-"))
  9950. kwd (match-string 2 term)
  9951. re-p (equal (string-to-char kwd) ?{)
  9952. term (substring term (match-end 0))
  9953. mm (if re-p
  9954. `(string-match ,(substring kwd 1 -1) todo)
  9955. (list 'equal 'todo kwd))
  9956. mm (if minus (list 'not mm) mm))
  9957. (push mm todomatcher))
  9958. (push (if (> (length todomatcher) 1)
  9959. (cons 'and todomatcher)
  9960. (car todomatcher))
  9961. orlist)
  9962. (setq todomatcher nil))
  9963. (setq todomatcher (if (> (length orlist) 1)
  9964. (cons 'or orlist) (car orlist))))
  9965. ;; Return the string and lisp forms of the matcher
  9966. (setq matcher (if todomatcher
  9967. (list 'and tagsmatcher todomatcher)
  9968. tagsmatcher))
  9969. (cons match0 matcher)))
  9970. (defun org-op-to-function (op &optional stringp)
  9971. "Turn an operator into the appropriate function."
  9972. (setq op
  9973. (cond
  9974. ((equal op "<" ) '(< string< org-time<))
  9975. ((equal op ">" ) '(> org-string> org-time>))
  9976. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9977. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9978. ((member op '("=" "==")) '(= string= org-time=))
  9979. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9980. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9981. (defun org<> (a b) (not (= a b)))
  9982. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9983. (defun org-string>= (a b) (not (string< a b)))
  9984. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9985. (defun org-string<> (a b) (not (string= a b)))
  9986. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9987. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9988. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9989. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9990. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9991. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9992. (defun org-2ft (s)
  9993. "Convert S to a floating point time.
  9994. If S is already a number, just return it. If it is a string, parse
  9995. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9996. (cond
  9997. ((numberp s) s)
  9998. ((stringp s)
  9999. (condition-case nil
  10000. (float-time (apply 'encode-time (org-parse-time-string s)))
  10001. (error 0.)))
  10002. (t 0.)))
  10003. (defun org-time-today ()
  10004. "Time in seconds today at 0:00.
  10005. Returns the float number of seconds since the beginning of the
  10006. epoch to the beginning of today (00:00)."
  10007. (float-time (apply 'encode-time
  10008. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10009. (defun org-matcher-time (s)
  10010. "Interpret a time comparison value."
  10011. (save-match-data
  10012. (cond
  10013. ((string= s "<now>") (float-time))
  10014. ((string= s "<today>") (org-time-today))
  10015. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10016. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10017. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10018. (+ (org-time-today)
  10019. (* (string-to-number (match-string 1 s))
  10020. (cdr (assoc (match-string 2 s)
  10021. '(("d" . 86400.0) ("w" . 604800.0)
  10022. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10023. (t (org-2ft s)))))
  10024. (defun org-match-any-p (re list)
  10025. "Does re match any element of list?"
  10026. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10027. (delq nil list))
  10028. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10029. (defvar org-tags-overlay (org-make-overlay 1 1))
  10030. (org-detach-overlay org-tags-overlay)
  10031. (defun org-get-local-tags-at (&optional pos)
  10032. "Get a list of tags defined in the current headline."
  10033. (org-get-tags-at pos 'local))
  10034. (defun org-get-local-tags ()
  10035. "Get a list of tags defined in the current headline."
  10036. (org-get-tags-at nil 'local))
  10037. (defun org-get-tags-at (&optional pos local)
  10038. "Get a list of all headline tags applicable at POS.
  10039. POS defaults to point. If tags are inherited, the list contains
  10040. the targets in the same sequence as the headlines appear, i.e.
  10041. the tags of the current headline come last.
  10042. When LOCAL is non-nil, only return tags from the current headline,
  10043. ignore inherited ones."
  10044. (interactive)
  10045. (if (and org-trust-scanner-tags
  10046. (or (not pos) (equal pos (point)))
  10047. (not local))
  10048. org-scanner-tags
  10049. (let (tags ltags lastpos parent)
  10050. (save-excursion
  10051. (save-restriction
  10052. (widen)
  10053. (goto-char (or pos (point)))
  10054. (save-match-data
  10055. (catch 'done
  10056. (condition-case nil
  10057. (progn
  10058. (org-back-to-heading t)
  10059. (while (not (equal lastpos (point)))
  10060. (setq lastpos (point))
  10061. (when (looking-at
  10062. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10063. (setq ltags (org-split-string
  10064. (org-match-string-no-properties 1) ":"))
  10065. (when parent
  10066. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10067. (setq tags (append
  10068. (if parent
  10069. (org-remove-uniherited-tags ltags)
  10070. ltags)
  10071. tags)))
  10072. (or org-use-tag-inheritance (throw 'done t))
  10073. (if local (throw 'done t))
  10074. (or (org-up-heading-safe) (error nil))
  10075. (setq parent t)))
  10076. (error nil)))))
  10077. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10078. (defun org-add-prop-inherited (s)
  10079. (add-text-properties 0 (length s) '(inherited t) s)
  10080. s)
  10081. (defun org-toggle-tag (tag &optional onoff)
  10082. "Toggle the tag TAG for the current line.
  10083. If ONOFF is `on' or `off', don't toggle but set to this state."
  10084. (let (res current)
  10085. (save-excursion
  10086. (org-back-to-heading t)
  10087. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10088. (point-at-eol) t)
  10089. (progn
  10090. (setq current (match-string 1))
  10091. (replace-match ""))
  10092. (setq current ""))
  10093. (setq current (nreverse (org-split-string current ":")))
  10094. (cond
  10095. ((eq onoff 'on)
  10096. (setq res t)
  10097. (or (member tag current) (push tag current)))
  10098. ((eq onoff 'off)
  10099. (or (not (member tag current)) (setq current (delete tag current))))
  10100. (t (if (member tag current)
  10101. (setq current (delete tag current))
  10102. (setq res t)
  10103. (push tag current))))
  10104. (end-of-line 1)
  10105. (if current
  10106. (progn
  10107. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10108. (org-set-tags nil t))
  10109. (delete-horizontal-space))
  10110. (run-hooks 'org-after-tags-change-hook))
  10111. res))
  10112. (defun org-align-tags-here (to-col)
  10113. ;; Assumes that this is a headline
  10114. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10115. (beginning-of-line 1)
  10116. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10117. (< pos (match-beginning 2)))
  10118. (progn
  10119. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10120. (goto-char (match-beginning 1))
  10121. (insert " ")
  10122. (delete-region (point) (1+ (match-beginning 2)))
  10123. (setq ncol (max (1+ (current-column))
  10124. (1+ col)
  10125. (if (> to-col 0)
  10126. to-col
  10127. (- (abs to-col) tags-l))))
  10128. (setq p (point))
  10129. (insert (make-string (- ncol (current-column)) ?\ ))
  10130. (setq ncol (current-column))
  10131. (when indent-tabs-mode (tabify p (point-at-eol)))
  10132. (org-move-to-column (min ncol col) t))
  10133. (goto-char pos))))
  10134. (defun org-set-tags-command (&optional arg just-align)
  10135. "Call the set-tags command for the current entry."
  10136. (interactive "P")
  10137. (if (org-on-heading-p)
  10138. (org-set-tags arg just-align)
  10139. (save-excursion
  10140. (org-back-to-heading t)
  10141. (org-set-tags arg just-align))))
  10142. (defun org-set-tags (&optional arg just-align)
  10143. "Set the tags for the current headline.
  10144. With prefix ARG, realign all tags in headings in the current buffer."
  10145. (interactive "P")
  10146. (let* ((re (concat "^" outline-regexp))
  10147. (current (org-get-tags-string))
  10148. (col (current-column))
  10149. (org-setting-tags t)
  10150. table current-tags inherited-tags ; computed below when needed
  10151. tags p0 c0 c1 rpl)
  10152. (if arg
  10153. (save-excursion
  10154. (goto-char (point-min))
  10155. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10156. (while (re-search-forward re nil t)
  10157. (org-set-tags nil t)
  10158. (end-of-line 1)))
  10159. (message "All tags realigned to column %d" org-tags-column))
  10160. (if just-align
  10161. (setq tags current)
  10162. ;; Get a new set of tags from the user
  10163. (save-excursion
  10164. (setq table (append org-tag-persistent-alist
  10165. (or org-tag-alist (org-get-buffer-tags)))
  10166. org-last-tags-completion-table table
  10167. current-tags (org-split-string current ":")
  10168. inherited-tags (nreverse
  10169. (nthcdr (length current-tags)
  10170. (nreverse (org-get-tags-at))))
  10171. tags
  10172. (if (or (eq t org-use-fast-tag-selection)
  10173. (and org-use-fast-tag-selection
  10174. (delq nil (mapcar 'cdr table))))
  10175. (org-fast-tag-selection
  10176. current-tags inherited-tags table
  10177. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10178. (let ((org-add-colon-after-tag-completion t))
  10179. (org-trim
  10180. (org-without-partial-completion
  10181. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10182. nil nil current 'org-tags-history)))))))
  10183. (while (string-match "[-+&]+" tags)
  10184. ;; No boolean logic, just a list
  10185. (setq tags (replace-match ":" t t tags))))
  10186. (if (string-match "\\`[\t ]*\\'" tags)
  10187. (setq tags "")
  10188. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10189. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10190. ;; Insert new tags at the correct column
  10191. (beginning-of-line 1)
  10192. (cond
  10193. ((and (equal current "") (equal tags "")))
  10194. ((re-search-forward
  10195. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10196. (point-at-eol) t)
  10197. (if (equal tags "")
  10198. (setq rpl "")
  10199. (goto-char (match-beginning 0))
  10200. (setq c0 (current-column) p0 (point)
  10201. c1 (max (1+ c0) (if (> org-tags-column 0)
  10202. org-tags-column
  10203. (- (- org-tags-column) (length tags))))
  10204. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10205. (replace-match rpl t t)
  10206. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10207. tags)
  10208. (t (error "Tags alignment failed")))
  10209. (org-move-to-column col)
  10210. (unless just-align
  10211. (run-hooks 'org-after-tags-change-hook)))))
  10212. (defun org-change-tag-in-region (beg end tag off)
  10213. "Add or remove TAG for each entry in the region.
  10214. This works in the agenda, and also in an org-mode buffer."
  10215. (interactive
  10216. (list (region-beginning) (region-end)
  10217. (let ((org-last-tags-completion-table
  10218. (if (org-mode-p)
  10219. (org-get-buffer-tags)
  10220. (org-global-tags-completion-table))))
  10221. (org-ido-completing-read
  10222. "Tag: " 'org-tags-completion-function nil nil nil
  10223. 'org-tags-history))
  10224. (progn
  10225. (message "[s]et or [r]emove? ")
  10226. (equal (read-char-exclusive) ?r))))
  10227. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10228. (let ((agendap (equal major-mode 'org-agenda-mode))
  10229. l1 l2 m buf pos newhead (cnt 0))
  10230. (goto-char end)
  10231. (setq l2 (1- (org-current-line)))
  10232. (goto-char beg)
  10233. (setq l1 (org-current-line))
  10234. (loop for l from l1 to l2 do
  10235. (goto-line l)
  10236. (setq m (get-text-property (point) 'org-hd-marker))
  10237. (when (or (and (org-mode-p) (org-on-heading-p))
  10238. (and agendap m))
  10239. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10240. pos (if agendap m (point)))
  10241. (with-current-buffer buf
  10242. (save-excursion
  10243. (save-restriction
  10244. (goto-char pos)
  10245. (setq cnt (1+ cnt))
  10246. (org-toggle-tag tag (if off 'off 'on))
  10247. (setq newhead (org-get-heading)))))
  10248. (and agendap (org-agenda-change-all-lines newhead m))))
  10249. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10250. (defun org-tags-completion-function (string predicate &optional flag)
  10251. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10252. (confirm (lambda (x) (stringp (car x)))))
  10253. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10254. (setq s1 (match-string 1 string)
  10255. s2 (match-string 2 string))
  10256. (setq s1 "" s2 string))
  10257. (cond
  10258. ((eq flag nil)
  10259. ;; try completion
  10260. (setq rtn (try-completion s2 ctable confirm))
  10261. (if (stringp rtn)
  10262. (setq rtn
  10263. (concat s1 s2 (substring rtn (length s2))
  10264. (if (and org-add-colon-after-tag-completion
  10265. (assoc rtn ctable))
  10266. ":" ""))))
  10267. rtn)
  10268. ((eq flag t)
  10269. ;; all-completions
  10270. (all-completions s2 ctable confirm)
  10271. )
  10272. ((eq flag 'lambda)
  10273. ;; exact match?
  10274. (assoc s2 ctable)))
  10275. ))
  10276. (defun org-fast-tag-insert (kwd tags face &optional end)
  10277. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10278. (insert (format "%-12s" (concat kwd ":"))
  10279. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10280. (or end "")))
  10281. (defun org-fast-tag-show-exit (flag)
  10282. (save-excursion
  10283. (goto-line 3)
  10284. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10285. (replace-match ""))
  10286. (when flag
  10287. (end-of-line 1)
  10288. (org-move-to-column (- (window-width) 19) t)
  10289. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10290. (defun org-set-current-tags-overlay (current prefix)
  10291. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10292. (if (featurep 'xemacs)
  10293. (org-overlay-display org-tags-overlay (concat prefix s)
  10294. 'secondary-selection)
  10295. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10296. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10297. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10298. "Fast tag selection with single keys.
  10299. CURRENT is the current list of tags in the headline, INHERITED is the
  10300. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10301. possibly with grouping information. TODO-TABLE is a similar table with
  10302. TODO keywords, should these have keys assigned to them.
  10303. If the keys are nil, a-z are automatically assigned.
  10304. Returns the new tags string, or nil to not change the current settings."
  10305. (let* ((fulltable (append table todo-table))
  10306. (maxlen (apply 'max (mapcar
  10307. (lambda (x)
  10308. (if (stringp (car x)) (string-width (car x)) 0))
  10309. fulltable)))
  10310. (buf (current-buffer))
  10311. (expert (eq org-fast-tag-selection-single-key 'expert))
  10312. (buffer-tags nil)
  10313. (fwidth (+ maxlen 3 1 3))
  10314. (ncol (/ (- (window-width) 4) fwidth))
  10315. (i-face 'org-done)
  10316. (c-face 'org-todo)
  10317. tg cnt e c char c1 c2 ntable tbl rtn
  10318. ov-start ov-end ov-prefix
  10319. (exit-after-next org-fast-tag-selection-single-key)
  10320. (done-keywords org-done-keywords)
  10321. groups ingroup)
  10322. (save-excursion
  10323. (beginning-of-line 1)
  10324. (if (looking-at
  10325. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10326. (setq ov-start (match-beginning 1)
  10327. ov-end (match-end 1)
  10328. ov-prefix "")
  10329. (setq ov-start (1- (point-at-eol))
  10330. ov-end (1+ ov-start))
  10331. (skip-chars-forward "^\n\r")
  10332. (setq ov-prefix
  10333. (concat
  10334. (buffer-substring (1- (point)) (point))
  10335. (if (> (current-column) org-tags-column)
  10336. " "
  10337. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10338. (org-move-overlay org-tags-overlay ov-start ov-end)
  10339. (save-window-excursion
  10340. (if expert
  10341. (set-buffer (get-buffer-create " *Org tags*"))
  10342. (delete-other-windows)
  10343. (split-window-vertically)
  10344. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10345. (erase-buffer)
  10346. (org-set-local 'org-done-keywords done-keywords)
  10347. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10348. (org-fast-tag-insert "Current" current c-face "\n\n")
  10349. (org-fast-tag-show-exit exit-after-next)
  10350. (org-set-current-tags-overlay current ov-prefix)
  10351. (setq tbl fulltable char ?a cnt 0)
  10352. (while (setq e (pop tbl))
  10353. (cond
  10354. ((equal e '(:startgroup))
  10355. (push '() groups) (setq ingroup t)
  10356. (when (not (= cnt 0))
  10357. (setq cnt 0)
  10358. (insert "\n"))
  10359. (insert "{ "))
  10360. ((equal e '(:endgroup))
  10361. (setq ingroup nil cnt 0)
  10362. (insert "}\n"))
  10363. ((equal e '(:newline))
  10364. (when (not (= cnt 0))
  10365. (setq cnt 0)
  10366. (insert "\n")
  10367. (setq e (car tbl))
  10368. (while (equal (car tbl) '(:newline))
  10369. (insert "\n")
  10370. (setq tbl (cdr tbl)))))
  10371. (t
  10372. (setq tg (car e) c2 nil)
  10373. (if (cdr e)
  10374. (setq c (cdr e))
  10375. ;; automatically assign a character.
  10376. (setq c1 (string-to-char
  10377. (downcase (substring
  10378. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10379. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10380. (while (or (rassoc char ntable) (rassoc char table))
  10381. (setq char (1+ char)))
  10382. (setq c2 c1))
  10383. (setq c (or c2 char)))
  10384. (if ingroup (push tg (car groups)))
  10385. (setq tg (org-add-props tg nil 'face
  10386. (cond
  10387. ((not (assoc tg table))
  10388. (org-get-todo-face tg))
  10389. ((member tg current) c-face)
  10390. ((member tg inherited) i-face)
  10391. (t nil))))
  10392. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10393. (insert "[" c "] " tg (make-string
  10394. (- fwidth 4 (length tg)) ?\ ))
  10395. (push (cons tg c) ntable)
  10396. (when (= (setq cnt (1+ cnt)) ncol)
  10397. (insert "\n")
  10398. (if ingroup (insert " "))
  10399. (setq cnt 0)))))
  10400. (setq ntable (nreverse ntable))
  10401. (insert "\n")
  10402. (goto-char (point-min))
  10403. (if (not expert) (org-fit-window-to-buffer))
  10404. (setq rtn
  10405. (catch 'exit
  10406. (while t
  10407. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10408. (if groups " [!] no groups" " [!]groups")
  10409. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10410. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10411. (cond
  10412. ((= c ?\r) (throw 'exit t))
  10413. ((= c ?!)
  10414. (setq groups (not groups))
  10415. (goto-char (point-min))
  10416. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10417. ((= c ?\C-c)
  10418. (if (not expert)
  10419. (org-fast-tag-show-exit
  10420. (setq exit-after-next (not exit-after-next)))
  10421. (setq expert nil)
  10422. (delete-other-windows)
  10423. (split-window-vertically)
  10424. (org-switch-to-buffer-other-window " *Org tags*")
  10425. (org-fit-window-to-buffer)))
  10426. ((or (= c ?\C-g)
  10427. (and (= c ?q) (not (rassoc c ntable))))
  10428. (org-detach-overlay org-tags-overlay)
  10429. (setq quit-flag t))
  10430. ((= c ?\ )
  10431. (setq current nil)
  10432. (if exit-after-next (setq exit-after-next 'now)))
  10433. ((= c ?\t)
  10434. (condition-case nil
  10435. (setq tg (org-ido-completing-read
  10436. "Tag: "
  10437. (or buffer-tags
  10438. (with-current-buffer buf
  10439. (org-get-buffer-tags)))))
  10440. (quit (setq tg "")))
  10441. (when (string-match "\\S-" tg)
  10442. (add-to-list 'buffer-tags (list tg))
  10443. (if (member tg current)
  10444. (setq current (delete tg current))
  10445. (push tg current)))
  10446. (if exit-after-next (setq exit-after-next 'now)))
  10447. ((setq e (rassoc c todo-table) tg (car e))
  10448. (with-current-buffer buf
  10449. (save-excursion (org-todo tg)))
  10450. (if exit-after-next (setq exit-after-next 'now)))
  10451. ((setq e (rassoc c ntable) tg (car e))
  10452. (if (member tg current)
  10453. (setq current (delete tg current))
  10454. (loop for g in groups do
  10455. (if (member tg g)
  10456. (mapc (lambda (x)
  10457. (setq current (delete x current)))
  10458. g)))
  10459. (push tg current))
  10460. (if exit-after-next (setq exit-after-next 'now))))
  10461. ;; Create a sorted list
  10462. (setq current
  10463. (sort current
  10464. (lambda (a b)
  10465. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10466. (if (eq exit-after-next 'now) (throw 'exit t))
  10467. (goto-char (point-min))
  10468. (beginning-of-line 2)
  10469. (delete-region (point) (point-at-eol))
  10470. (org-fast-tag-insert "Current" current c-face)
  10471. (org-set-current-tags-overlay current ov-prefix)
  10472. (while (re-search-forward
  10473. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10474. (setq tg (match-string 1))
  10475. (add-text-properties
  10476. (match-beginning 1) (match-end 1)
  10477. (list 'face
  10478. (cond
  10479. ((member tg current) c-face)
  10480. ((member tg inherited) i-face)
  10481. (t (get-text-property (match-beginning 1) 'face))))))
  10482. (goto-char (point-min)))))
  10483. (org-detach-overlay org-tags-overlay)
  10484. (if rtn
  10485. (mapconcat 'identity current ":")
  10486. nil))))
  10487. (defun org-get-tags-string ()
  10488. "Get the TAGS string in the current headline."
  10489. (unless (org-on-heading-p t)
  10490. (error "Not on a heading"))
  10491. (save-excursion
  10492. (beginning-of-line 1)
  10493. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10494. (org-match-string-no-properties 1)
  10495. "")))
  10496. (defun org-get-tags ()
  10497. "Get the list of tags specified in the current headline."
  10498. (org-split-string (org-get-tags-string) ":"))
  10499. (defun org-get-buffer-tags ()
  10500. "Get a table of all tags used in the buffer, for completion."
  10501. (let (tags)
  10502. (save-excursion
  10503. (goto-char (point-min))
  10504. (while (re-search-forward
  10505. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10506. (when (equal (char-after (point-at-bol 0)) ?*)
  10507. (mapc (lambda (x) (add-to-list 'tags x))
  10508. (org-split-string (org-match-string-no-properties 1) ":")))))
  10509. (mapcar 'list tags)))
  10510. ;;;; The mapping API
  10511. ;;;###autoload
  10512. (defun org-map-entries (func &optional match scope &rest skip)
  10513. "Call FUNC at each headline selected by MATCH in SCOPE.
  10514. FUNC is a function or a lisp form. The function will be called without
  10515. arguments, with the cursor positioned at the beginning of the headline.
  10516. The return values of all calls to the function will be collected and
  10517. returned as a list.
  10518. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10519. does not need to preserve point. After evaluaton, the cursor will be
  10520. moved to the end of the line (presumably of the headline of the
  10521. processed entry) and search continues from there. Under some
  10522. circumstances, this may not produce the wanted results. For example,
  10523. if you have removed (e.g. archived) the current (sub)tree it could
  10524. mean that the next entry will be skipped entirely. In such cases, you
  10525. can specify the position from where search should continue by making
  10526. FUNC set the variable `org-map-continue-from' to the desired buffer
  10527. position.
  10528. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10529. Only headlines that are matched by this query will be considered during
  10530. the iteration. When MATCH is nil or t, all headlines will be
  10531. visited by the iteration.
  10532. SCOPE determines the scope of this command. It can be any of:
  10533. nil The current buffer, respecting the restriction if any
  10534. tree The subtree started with the entry at point
  10535. file The current buffer, without restriction
  10536. file-with-archives
  10537. The current buffer, and any archives associated with it
  10538. agenda All agenda files
  10539. agenda-with-archives
  10540. All agenda files with any archive files associated with them
  10541. \(file1 file2 ...)
  10542. If this is a list, all files in the list will be scanned
  10543. The remaining args are treated as settings for the skipping facilities of
  10544. the scanner. The following items can be given here:
  10545. archive skip trees with the archive tag.
  10546. comment skip trees with the COMMENT keyword
  10547. function or Emacs Lisp form:
  10548. will be used as value for `org-agenda-skip-function', so whenever
  10549. the the function returns t, FUNC will not be called for that
  10550. entry and search will continue from the point where the
  10551. function leaves it.
  10552. If your function needs to retrieve the tags including inherited tags
  10553. at the *current* entry, you can use the value of the variable
  10554. `org-scanner-tags' which will be much faster than getting the value
  10555. with `org-get-tags-at'. If your function gets properties with
  10556. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10557. to t around the call to `org-entry-properties' to get the same speedup.
  10558. Note that if your function moves around to retrieve tags and properties at
  10559. a *different* entry, you cannot use these techniques."
  10560. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10561. (org-agenda-skip-archived-trees (memq 'archive skip))
  10562. (org-agenda-skip-comment-trees (memq 'comment skip))
  10563. (org-agenda-skip-function
  10564. (car (org-delete-all '(comment archive) skip)))
  10565. (org-tags-match-list-sublevels t)
  10566. matcher file res
  10567. org-todo-keywords-for-agenda
  10568. org-done-keywords-for-agenda
  10569. org-todo-keyword-alist-for-agenda
  10570. org-tag-alist-for-agenda)
  10571. (cond
  10572. ((eq match t) (setq matcher t))
  10573. ((eq match nil) (setq matcher t))
  10574. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10575. (save-excursion
  10576. (save-restriction
  10577. (when (eq scope 'tree)
  10578. (org-back-to-heading t)
  10579. (org-narrow-to-subtree)
  10580. (setq scope nil))
  10581. (if (not scope)
  10582. (progn
  10583. (org-prepare-agenda-buffers
  10584. (list (buffer-file-name (current-buffer))))
  10585. (setq res (org-scan-tags func matcher)))
  10586. ;; Get the right scope
  10587. (cond
  10588. ((and scope (listp scope) (symbolp (car scope)))
  10589. (setq scope (eval scope)))
  10590. ((eq scope 'agenda)
  10591. (setq scope (org-agenda-files t)))
  10592. ((eq scope 'agenda-with-archives)
  10593. (setq scope (org-agenda-files t))
  10594. (setq scope (org-add-archive-files scope)))
  10595. ((eq scope 'file)
  10596. (setq scope (list (buffer-file-name))))
  10597. ((eq scope 'file-with-archives)
  10598. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10599. (org-prepare-agenda-buffers scope)
  10600. (while (setq file (pop scope))
  10601. (with-current-buffer (org-find-base-buffer-visiting file)
  10602. (save-excursion
  10603. (save-restriction
  10604. (widen)
  10605. (goto-char (point-min))
  10606. (setq res (append res (org-scan-tags func matcher))))))))))
  10607. res))
  10608. ;;;; Properties
  10609. ;;; Setting and retrieving properties
  10610. (defconst org-special-properties
  10611. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10612. "TIMESTAMP" "TIMESTAMP_IA")
  10613. "The special properties valid in Org-mode.
  10614. These are properties that are not defined in the property drawer,
  10615. but in some other way.")
  10616. (defconst org-default-properties
  10617. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10618. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10619. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10620. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10621. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10622. "CLOCK_MODELINE_TOTAL")
  10623. "Some properties that are used by Org-mode for various purposes.
  10624. Being in this list makes sure that they are offered for completion.")
  10625. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10626. "Regular expression matching the first line of a property drawer.")
  10627. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10628. "Regular expression matching the first line of a property drawer.")
  10629. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10630. "Regular expression matching the first line of a property drawer.")
  10631. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10632. "Regular expression matching the first line of a property drawer.")
  10633. (defconst org-property-drawer-re
  10634. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10635. org-property-end-re "\\)\n?")
  10636. "Matches an entire property drawer.")
  10637. (defconst org-clock-drawer-re
  10638. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10639. org-property-end-re "\\)\n?")
  10640. "Matches an entire clock drawer.")
  10641. (defun org-property-action ()
  10642. "Do an action on properties."
  10643. (interactive)
  10644. (let (c)
  10645. (org-at-property-p)
  10646. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10647. (setq c (read-char-exclusive))
  10648. (cond
  10649. ((equal c ?s)
  10650. (call-interactively 'org-set-property))
  10651. ((equal c ?d)
  10652. (call-interactively 'org-delete-property))
  10653. ((equal c ?D)
  10654. (call-interactively 'org-delete-property-globally))
  10655. ((equal c ?c)
  10656. (call-interactively 'org-compute-property-at-point))
  10657. (t (error "No such property action %c" c)))))
  10658. (defun org-at-property-p ()
  10659. "Is the cursor in a property line?"
  10660. ;; FIXME: Does not check if we are actually in the drawer.
  10661. ;; FIXME: also returns true on any drawers.....
  10662. ;; This is used by C-c C-c for property action.
  10663. (save-excursion
  10664. (beginning-of-line 1)
  10665. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10666. (defun org-get-property-block (&optional beg end force)
  10667. "Return the (beg . end) range of the body of the property drawer.
  10668. BEG and END can be beginning and end of subtree, if not given
  10669. they will be found.
  10670. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10671. (catch 'exit
  10672. (save-excursion
  10673. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10674. (end (or end (progn (outline-next-heading) (point)))))
  10675. (goto-char beg)
  10676. (if (re-search-forward org-property-start-re end t)
  10677. (setq beg (1+ (match-end 0)))
  10678. (if force
  10679. (save-excursion
  10680. (org-insert-property-drawer)
  10681. (setq end (progn (outline-next-heading) (point))))
  10682. (throw 'exit nil))
  10683. (goto-char beg)
  10684. (if (re-search-forward org-property-start-re end t)
  10685. (setq beg (1+ (match-end 0)))))
  10686. (if (re-search-forward org-property-end-re end t)
  10687. (setq end (match-beginning 0))
  10688. (or force (throw 'exit nil))
  10689. (goto-char beg)
  10690. (setq end beg)
  10691. (org-indent-line-function)
  10692. (insert ":END:\n"))
  10693. (cons beg end)))))
  10694. (defun org-entry-properties (&optional pom which)
  10695. "Get all properties of the entry at point-or-marker POM.
  10696. This includes the TODO keyword, the tags, time strings for deadline,
  10697. scheduled, and clocking, and any additional properties defined in the
  10698. entry. The return value is an alist, keys may occur multiple times
  10699. if the property key was used several times.
  10700. POM may also be nil, in which case the current entry is used.
  10701. If WHICH is nil or `all', get all properties. If WHICH is
  10702. `special' or `standard', only get that subclass."
  10703. (setq which (or which 'all))
  10704. (org-with-point-at pom
  10705. (let ((clockstr (substring org-clock-string 0 -1))
  10706. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10707. beg end range props sum-props key value string clocksum)
  10708. (save-excursion
  10709. (when (condition-case nil
  10710. (and (org-mode-p) (org-back-to-heading t))
  10711. (error nil))
  10712. (setq beg (point))
  10713. (setq sum-props (get-text-property (point) 'org-summaries))
  10714. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10715. (outline-next-heading)
  10716. (setq end (point))
  10717. (when (memq which '(all special))
  10718. ;; Get the special properties, like TODO and tags
  10719. (goto-char beg)
  10720. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10721. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10722. (when (looking-at org-priority-regexp)
  10723. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10724. (when (and (setq value (org-get-tags-string))
  10725. (string-match "\\S-" value))
  10726. (push (cons "TAGS" value) props))
  10727. (when (setq value (org-get-tags-at))
  10728. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10729. props))
  10730. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10731. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10732. string (if (equal key clockstr)
  10733. (org-no-properties
  10734. (org-trim
  10735. (buffer-substring
  10736. (match-beginning 3) (goto-char (point-at-eol)))))
  10737. (substring (org-match-string-no-properties 3) 1 -1)))
  10738. (unless key
  10739. (if (= (char-after (match-beginning 3)) ?\[)
  10740. (setq key "TIMESTAMP_IA")
  10741. (setq key "TIMESTAMP")))
  10742. (when (or (equal key clockstr) (not (assoc key props)))
  10743. (push (cons key string) props)))
  10744. )
  10745. (when (memq which '(all standard))
  10746. ;; Get the standard properties, like :PROP: ...
  10747. (setq range (org-get-property-block beg end))
  10748. (when range
  10749. (goto-char (car range))
  10750. (while (re-search-forward
  10751. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10752. (cdr range) t)
  10753. (setq key (org-match-string-no-properties 1)
  10754. value (org-trim (or (org-match-string-no-properties 2) "")))
  10755. (unless (member key excluded)
  10756. (push (cons key (or value "")) props)))))
  10757. (if clocksum
  10758. (push (cons "CLOCKSUM"
  10759. (org-columns-number-to-string (/ (float clocksum) 60.)
  10760. 'add_times))
  10761. props))
  10762. (unless (assoc "CATEGORY" props)
  10763. (setq value (or (org-get-category)
  10764. (progn (org-refresh-category-properties)
  10765. (org-get-category))))
  10766. (push (cons "CATEGORY" value) props))
  10767. (append sum-props (nreverse props)))))))
  10768. (defun org-entry-get (pom property &optional inherit)
  10769. "Get value of PROPERTY for entry at point-or-marker POM.
  10770. If INHERIT is non-nil and the entry does not have the property,
  10771. then also check higher levels of the hierarchy.
  10772. If INHERIT is the symbol `selective', use inheritance only if the setting
  10773. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10774. If the property is present but empty, the return value is the empty string.
  10775. If the property is not present at all, nil is returned."
  10776. (org-with-point-at pom
  10777. (if (and inherit (if (eq inherit 'selective)
  10778. (org-property-inherit-p property)
  10779. t))
  10780. (org-entry-get-with-inheritance property)
  10781. (if (member property org-special-properties)
  10782. ;; We need a special property. Use brute force, get all properties.
  10783. (cdr (assoc property (org-entry-properties nil 'special)))
  10784. (let ((range (org-get-property-block)))
  10785. (if (and range
  10786. (goto-char (car range))
  10787. (re-search-forward
  10788. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10789. (cdr range) t))
  10790. ;; Found the property, return it.
  10791. (if (match-end 1)
  10792. (org-match-string-no-properties 1)
  10793. "")))))))
  10794. (defun org-property-or-variable-value (var &optional inherit)
  10795. "Check if there is a property fixing the value of VAR.
  10796. If yes, return this value. If not, return the current value of the variable."
  10797. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10798. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10799. (read prop)
  10800. (symbol-value var))))
  10801. (defun org-entry-delete (pom property)
  10802. "Delete the property PROPERTY from entry at point-or-marker POM."
  10803. (org-with-point-at pom
  10804. (if (member property org-special-properties)
  10805. nil ; cannot delete these properties.
  10806. (let ((range (org-get-property-block)))
  10807. (if (and range
  10808. (goto-char (car range))
  10809. (re-search-forward
  10810. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10811. (cdr range) t))
  10812. (progn
  10813. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10814. t)
  10815. nil)))))
  10816. ;; Multi-values properties are properties that contain multiple values
  10817. ;; These values are assumed to be single words, separated by whitespace.
  10818. (defun org-entry-add-to-multivalued-property (pom property value)
  10819. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10820. (let* ((old (org-entry-get pom property))
  10821. (values (and old (org-split-string old "[ \t]"))))
  10822. (setq value (org-entry-protect-space value))
  10823. (unless (member value values)
  10824. (setq values (cons value values))
  10825. (org-entry-put pom property
  10826. (mapconcat 'identity values " ")))))
  10827. (defun org-entry-remove-from-multivalued-property (pom property value)
  10828. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10829. (let* ((old (org-entry-get pom property))
  10830. (values (and old (org-split-string old "[ \t]"))))
  10831. (setq value (org-entry-protect-space value))
  10832. (when (member value values)
  10833. (setq values (delete value values))
  10834. (org-entry-put pom property
  10835. (mapconcat 'identity values " ")))))
  10836. (defun org-entry-member-in-multivalued-property (pom property value)
  10837. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10838. (let* ((old (org-entry-get pom property))
  10839. (values (and old (org-split-string old "[ \t]"))))
  10840. (setq value (org-entry-protect-space value))
  10841. (member value values)))
  10842. (defun org-entry-get-multivalued-property (pom property)
  10843. "Return a list of values in a multivalued property."
  10844. (let* ((value (org-entry-get pom property))
  10845. (values (and value (org-split-string value "[ \t]"))))
  10846. (mapcar 'org-entry-restore-space values)))
  10847. (defun org-entry-put-multivalued-property (pom property &rest values)
  10848. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10849. VALUES should be a list of strings. Spaces will be protected."
  10850. (org-entry-put pom property
  10851. (mapconcat 'org-entry-protect-space values " "))
  10852. (let* ((value (org-entry-get pom property))
  10853. (values (and value (org-split-string value "[ \t]"))))
  10854. (mapcar 'org-entry-restore-space values)))
  10855. (defun org-entry-protect-space (s)
  10856. "Protect spaces and newline in string S."
  10857. (while (string-match " " s)
  10858. (setq s (replace-match "%20" t t s)))
  10859. (while (string-match "\n" s)
  10860. (setq s (replace-match "%0A" t t s)))
  10861. s)
  10862. (defun org-entry-restore-space (s)
  10863. "Restore spaces and newline in string S."
  10864. (while (string-match "%20" s)
  10865. (setq s (replace-match " " t t s)))
  10866. (while (string-match "%0A" s)
  10867. (setq s (replace-match "\n" t t s)))
  10868. s)
  10869. (defvar org-entry-property-inherited-from (make-marker)
  10870. "Marker pointing to the entry from where a property was inherited.
  10871. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10872. location of the entry where the inheritance search matched. If there was
  10873. no match, the marker will point nowhere.
  10874. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10875. is set.")
  10876. (defun org-entry-get-with-inheritance (property)
  10877. "Get entry property, and search higher levels if not present."
  10878. (move-marker org-entry-property-inherited-from nil)
  10879. (let (tmp)
  10880. (save-excursion
  10881. (save-restriction
  10882. (widen)
  10883. (catch 'ex
  10884. (while t
  10885. (when (setq tmp (org-entry-get nil property))
  10886. (org-back-to-heading t)
  10887. (move-marker org-entry-property-inherited-from (point))
  10888. (throw 'ex tmp))
  10889. (or (org-up-heading-safe) (throw 'ex nil)))))
  10890. (or tmp
  10891. (cdr (assoc property org-file-properties))
  10892. (cdr (assoc property org-global-properties))
  10893. (cdr (assoc property org-global-properties-fixed))))))
  10894. (defun org-entry-put (pom property value)
  10895. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10896. (org-with-point-at pom
  10897. (org-back-to-heading t)
  10898. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10899. range)
  10900. (cond
  10901. ((equal property "TODO")
  10902. (when (and (stringp value) (string-match "\\S-" value)
  10903. (not (member value org-todo-keywords-1)))
  10904. (error "\"%s\" is not a valid TODO state" value))
  10905. (if (or (not value)
  10906. (not (string-match "\\S-" value)))
  10907. (setq value 'none))
  10908. (org-todo value)
  10909. (org-set-tags nil 'align))
  10910. ((equal property "PRIORITY")
  10911. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10912. (string-to-char value) ?\ ))
  10913. (org-set-tags nil 'align))
  10914. ((equal property "SCHEDULED")
  10915. (if (re-search-forward org-scheduled-time-regexp end t)
  10916. (cond
  10917. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10918. ((eq value 'later) (org-timestamp-change 1 'day))
  10919. (t (call-interactively 'org-schedule)))
  10920. (call-interactively 'org-schedule)))
  10921. ((equal property "DEADLINE")
  10922. (if (re-search-forward org-deadline-time-regexp end t)
  10923. (cond
  10924. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10925. ((eq value 'later) (org-timestamp-change 1 'day))
  10926. (t (call-interactively 'org-deadline)))
  10927. (call-interactively 'org-deadline)))
  10928. ((member property org-special-properties)
  10929. (error "The %s property can not yet be set with `org-entry-put'"
  10930. property))
  10931. (t ; a non-special property
  10932. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10933. (setq range (org-get-property-block beg end 'force))
  10934. (goto-char (car range))
  10935. (if (re-search-forward
  10936. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10937. (progn
  10938. (delete-region (match-beginning 1) (match-end 1))
  10939. (goto-char (match-beginning 1)))
  10940. (goto-char (cdr range))
  10941. (insert "\n")
  10942. (backward-char 1)
  10943. (org-indent-line-function)
  10944. (insert ":" property ":"))
  10945. (and value (insert " " value))
  10946. (org-indent-line-function)))))))
  10947. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10948. "Get all property keys in the current buffer.
  10949. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10950. like tags and TODO state.
  10951. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10952. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10953. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10954. formats in the current buffer."
  10955. (let (rtn range cfmt s p)
  10956. (save-excursion
  10957. (save-restriction
  10958. (widen)
  10959. (goto-char (point-min))
  10960. (while (re-search-forward org-property-start-re nil t)
  10961. (setq range (org-get-property-block))
  10962. (goto-char (car range))
  10963. (while (re-search-forward
  10964. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10965. (cdr range) t)
  10966. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10967. (outline-next-heading))))
  10968. (when include-specials
  10969. (setq rtn (append org-special-properties rtn)))
  10970. (when include-defaults
  10971. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10972. (add-to-list 'rtn org-effort-property))
  10973. (when include-columns
  10974. (save-excursion
  10975. (save-restriction
  10976. (widen)
  10977. (goto-char (point-min))
  10978. (while (re-search-forward
  10979. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10980. nil t)
  10981. (setq cfmt (match-string 2) s 0)
  10982. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10983. cfmt s)
  10984. (setq s (match-end 0)
  10985. p (match-string 1 cfmt))
  10986. (unless (or (equal p "ITEM")
  10987. (member p org-special-properties))
  10988. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10989. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10990. (defun org-property-values (key)
  10991. "Return a list of all values of property KEY."
  10992. (save-excursion
  10993. (save-restriction
  10994. (widen)
  10995. (goto-char (point-min))
  10996. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10997. values)
  10998. (while (re-search-forward re nil t)
  10999. (add-to-list 'values (org-trim (match-string 1))))
  11000. (delete "" values)))))
  11001. (defun org-insert-property-drawer ()
  11002. "Insert a property drawer into the current entry."
  11003. (interactive)
  11004. (org-back-to-heading t)
  11005. (looking-at outline-regexp)
  11006. (let ((indent (- (match-end 0)(match-beginning 0)))
  11007. (beg (point))
  11008. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11009. end hiddenp)
  11010. (outline-next-heading)
  11011. (setq end (point))
  11012. (goto-char beg)
  11013. (while (re-search-forward re end t))
  11014. (setq hiddenp (org-invisible-p))
  11015. (end-of-line 1)
  11016. (and (equal (char-after) ?\n) (forward-char 1))
  11017. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11018. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11019. ;; just skip this line
  11020. (beginning-of-line 2)
  11021. ;; Drawer start, find the end
  11022. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11023. (beginning-of-line 1)))
  11024. (org-skip-over-state-notes)
  11025. (skip-chars-backward " \t\n\r")
  11026. (if (eq (char-before) ?*) (forward-char 1))
  11027. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11028. (beginning-of-line 0)
  11029. (org-indent-to-column indent)
  11030. (beginning-of-line 2)
  11031. (org-indent-to-column indent)
  11032. (beginning-of-line 0)
  11033. (if hiddenp
  11034. (save-excursion
  11035. (org-back-to-heading t)
  11036. (hide-entry))
  11037. (org-flag-drawer t))))
  11038. (defun org-set-property (property value)
  11039. "In the current entry, set PROPERTY to VALUE.
  11040. When called interactively, this will prompt for a property name, offering
  11041. completion on existing and default properties. And then it will prompt
  11042. for a value, offering completion either on allowed values (via an inherited
  11043. xxx_ALL property) or on existing values in other instances of this property
  11044. in the current file."
  11045. (interactive
  11046. (let* ((completion-ignore-case t)
  11047. (keys (org-buffer-property-keys nil t t))
  11048. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  11049. (prop (if (member prop0 keys)
  11050. prop0
  11051. (or (cdr (assoc (downcase prop0)
  11052. (mapcar (lambda (x) (cons (downcase x) x))
  11053. keys)))
  11054. prop0)))
  11055. (cur (org-entry-get nil prop))
  11056. (allowed (org-property-get-allowed-values nil prop 'table))
  11057. (existing (mapcar 'list (org-property-values prop)))
  11058. (val (if allowed
  11059. (org-completing-read "Value: " allowed nil 'req-match)
  11060. (let (org-completion-use-ido)
  11061. (org-completing-read
  11062. (concat "Value" (if (and cur (string-match "\\S-" cur))
  11063. (concat "[" cur "]") "")
  11064. ": ")
  11065. existing nil nil "" nil cur)))))
  11066. (list prop (if (equal val "") cur val))))
  11067. (unless (equal (org-entry-get nil property) value)
  11068. (org-entry-put nil property value)))
  11069. (defun org-delete-property (property)
  11070. "In the current entry, delete PROPERTY."
  11071. (interactive
  11072. (let* ((completion-ignore-case t)
  11073. (prop (org-ido-completing-read
  11074. "Property: " (org-entry-properties nil 'standard))))
  11075. (list prop)))
  11076. (message "Property %s %s" property
  11077. (if (org-entry-delete nil property)
  11078. "deleted"
  11079. "was not present in the entry")))
  11080. (defun org-delete-property-globally (property)
  11081. "Remove PROPERTY globally, from all entries."
  11082. (interactive
  11083. (let* ((completion-ignore-case t)
  11084. (prop (org-ido-completing-read
  11085. "Globally remove property: "
  11086. (mapcar 'list (org-buffer-property-keys)))))
  11087. (list prop)))
  11088. (save-excursion
  11089. (save-restriction
  11090. (widen)
  11091. (goto-char (point-min))
  11092. (let ((cnt 0))
  11093. (while (re-search-forward
  11094. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11095. nil t)
  11096. (setq cnt (1+ cnt))
  11097. (replace-match ""))
  11098. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11099. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11100. (defun org-compute-property-at-point ()
  11101. "Compute the property at point.
  11102. This looks for an enclosing column format, extracts the operator and
  11103. then applies it to the property in the column format's scope."
  11104. (interactive)
  11105. (unless (org-at-property-p)
  11106. (error "Not at a property"))
  11107. (let ((prop (org-match-string-no-properties 2)))
  11108. (org-columns-get-format-and-top-level)
  11109. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11110. (error "No operator defined for property %s" prop))
  11111. (org-columns-compute prop)))
  11112. (defun org-property-get-allowed-values (pom property &optional table)
  11113. "Get allowed values for the property PROPERTY.
  11114. When TABLE is non-nil, return an alist that can directly be used for
  11115. completion."
  11116. (let (vals)
  11117. (cond
  11118. ((equal property "TODO")
  11119. (setq vals (org-with-point-at pom
  11120. (append org-todo-keywords-1 '("")))))
  11121. ((equal property "PRIORITY")
  11122. (let ((n org-lowest-priority))
  11123. (while (>= n org-highest-priority)
  11124. (push (char-to-string n) vals)
  11125. (setq n (1- n)))))
  11126. ((member property org-special-properties))
  11127. (t
  11128. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11129. (when (and vals (string-match "\\S-" vals))
  11130. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11131. (setq vals (mapcar (lambda (x)
  11132. (cond ((stringp x) x)
  11133. ((numberp x) (number-to-string x))
  11134. ((symbolp x) (symbol-name x))
  11135. (t "???")))
  11136. vals)))))
  11137. (if table (mapcar 'list vals) vals)))
  11138. (defun org-property-previous-allowed-value (&optional previous)
  11139. "Switch to the next allowed value for this property."
  11140. (interactive)
  11141. (org-property-next-allowed-value t))
  11142. (defun org-property-next-allowed-value (&optional previous)
  11143. "Switch to the next allowed value for this property."
  11144. (interactive)
  11145. (unless (org-at-property-p)
  11146. (error "Not at a property"))
  11147. (let* ((key (match-string 2))
  11148. (value (match-string 3))
  11149. (allowed (or (org-property-get-allowed-values (point) key)
  11150. (and (member value '("[ ]" "[-]" "[X]"))
  11151. '("[ ]" "[X]"))))
  11152. nval)
  11153. (unless allowed
  11154. (error "Allowed values for this property have not been defined"))
  11155. (if previous (setq allowed (reverse allowed)))
  11156. (if (member value allowed)
  11157. (setq nval (car (cdr (member value allowed)))))
  11158. (setq nval (or nval (car allowed)))
  11159. (if (equal nval value)
  11160. (error "Only one allowed value for this property"))
  11161. (org-at-property-p)
  11162. (replace-match (concat " :" key ": " nval) t t)
  11163. (org-indent-line-function)
  11164. (beginning-of-line 1)
  11165. (skip-chars-forward " \t")))
  11166. (defun org-find-entry-with-id (ident)
  11167. "Locate the entry that contains the ID property with exact value IDENT.
  11168. IDENT can be a string, a symbol or a number, this function will search for
  11169. the string representation of it.
  11170. Return the position where this entry starts, or nil if there is no such entry."
  11171. (interactive "sID: ")
  11172. (let ((id (cond
  11173. ((stringp ident) ident)
  11174. ((symbol-name ident) (symbol-name ident))
  11175. ((numberp ident) (number-to-string ident))
  11176. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11177. (case-fold-search nil))
  11178. (save-excursion
  11179. (save-restriction
  11180. (widen)
  11181. (goto-char (point-min))
  11182. (when (re-search-forward
  11183. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11184. nil t)
  11185. (org-back-to-heading)
  11186. (point))))))
  11187. ;;;; Timestamps
  11188. (defvar org-last-changed-timestamp nil)
  11189. (defvar org-last-inserted-timestamp nil
  11190. "The last time stamp inserted with `org-insert-time-stamp'.")
  11191. (defvar org-time-was-given) ; dynamically scoped parameter
  11192. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11193. (defvar org-ts-what) ; dynamically scoped parameter
  11194. (defun org-time-stamp (arg &optional inactive)
  11195. "Prompt for a date/time and insert a time stamp.
  11196. If the user specifies a time like HH:MM, or if this command is called
  11197. with a prefix argument, the time stamp will contain date and time.
  11198. Otherwise, only the date will be included. All parts of a date not
  11199. specified by the user will be filled in from the current date/time.
  11200. So if you press just return without typing anything, the time stamp
  11201. will represent the current date/time. If there is already a timestamp
  11202. at the cursor, it will be modified."
  11203. (interactive "P")
  11204. (let* ((ts nil)
  11205. (default-time
  11206. ;; Default time is either today, or, when entering a range,
  11207. ;; the range start.
  11208. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11209. (save-excursion
  11210. (re-search-backward
  11211. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11212. (- (point) 20) t)))
  11213. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11214. (current-time)))
  11215. (default-input (and ts (org-get-compact-tod ts)))
  11216. org-time-was-given org-end-time-was-given time)
  11217. (cond
  11218. ((and (org-at-timestamp-p t)
  11219. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11220. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11221. (insert "--")
  11222. (setq time (let ((this-command this-command))
  11223. (org-read-date arg 'totime nil nil
  11224. default-time default-input)))
  11225. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11226. ((org-at-timestamp-p t)
  11227. (setq time (let ((this-command this-command))
  11228. (org-read-date arg 'totime nil nil default-time default-input)))
  11229. (when (org-at-timestamp-p t) ; just to get the match data
  11230. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11231. (replace-match "")
  11232. (setq org-last-changed-timestamp
  11233. (org-insert-time-stamp
  11234. time (or org-time-was-given arg)
  11235. inactive nil nil (list org-end-time-was-given))))
  11236. (message "Timestamp updated"))
  11237. (t
  11238. (setq time (let ((this-command this-command))
  11239. (org-read-date arg 'totime nil nil default-time default-input)))
  11240. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11241. nil nil (list org-end-time-was-given))))))
  11242. ;; FIXME: can we use this for something else, like computing time differences?
  11243. (defun org-get-compact-tod (s)
  11244. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11245. (let* ((t1 (match-string 1 s))
  11246. (h1 (string-to-number (match-string 2 s)))
  11247. (m1 (string-to-number (match-string 3 s)))
  11248. (t2 (and (match-end 4) (match-string 5 s)))
  11249. (h2 (and t2 (string-to-number (match-string 6 s))))
  11250. (m2 (and t2 (string-to-number (match-string 7 s))))
  11251. dh dm)
  11252. (if (not t2)
  11253. t1
  11254. (setq dh (- h2 h1) dm (- m2 m1))
  11255. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11256. (concat t1 "+" (number-to-string dh)
  11257. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11258. (defun org-time-stamp-inactive (&optional arg)
  11259. "Insert an inactive time stamp.
  11260. An inactive time stamp is enclosed in square brackets instead of angle
  11261. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11262. does not link to the calendar and cannot be changed with the S-cursor keys.
  11263. So these are more for recording a certain time/date."
  11264. (interactive "P")
  11265. (org-time-stamp arg 'inactive))
  11266. (defvar org-date-ovl (org-make-overlay 1 1))
  11267. (org-overlay-put org-date-ovl 'face 'org-warning)
  11268. (org-detach-overlay org-date-ovl)
  11269. (defvar org-ans1) ; dynamically scoped parameter
  11270. (defvar org-ans2) ; dynamically scoped parameter
  11271. (defvar org-plain-time-of-day-regexp) ; defined below
  11272. (defvar org-overriding-default-time nil) ; dynamically scoped
  11273. (defvar org-read-date-overlay nil)
  11274. (defvar org-dcst nil) ; dynamically scoped
  11275. (defvar org-read-date-history nil)
  11276. (defun org-read-date (&optional with-time to-time from-string prompt
  11277. default-time default-input)
  11278. "Read a date, possibly a time, and make things smooth for the user.
  11279. The prompt will suggest to enter an ISO date, but you can also enter anything
  11280. which will at least partially be understood by `parse-time-string'.
  11281. Unrecognized parts of the date will default to the current day, month, year,
  11282. hour and minute. If this command is called to replace a timestamp at point,
  11283. of to enter the second timestamp of a range, the default time is taken from the
  11284. existing stamp. For example,
  11285. 3-2-5 --> 2003-02-05
  11286. feb 15 --> currentyear-02-15
  11287. sep 12 9 --> 2009-09-12
  11288. 12:45 --> today 12:45
  11289. 22 sept 0:34 --> currentyear-09-22 0:34
  11290. 12 --> currentyear-currentmonth-12
  11291. Fri --> nearest Friday (today or later)
  11292. etc.
  11293. Furthermore you can specify a relative date by giving, as the *first* thing
  11294. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11295. change in days weeks, months, years.
  11296. With a single plus or minus, the date is relative to today. With a double
  11297. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11298. +4d --> four days from today
  11299. +4 --> same as above
  11300. +2w --> two weeks from today
  11301. ++5 --> five days from default date
  11302. The function understands only English month and weekday abbreviations,
  11303. but this can be configured with the variables `parse-time-months' and
  11304. `parse-time-weekdays'.
  11305. While prompting, a calendar is popped up - you can also select the
  11306. date with the mouse (button 1). The calendar shows a period of three
  11307. months. To scroll it to other months, use the keys `>' and `<'.
  11308. If you don't like the calendar, turn it off with
  11309. \(setq org-read-date-popup-calendar nil)
  11310. With optional argument TO-TIME, the date will immediately be converted
  11311. to an internal time.
  11312. With an optional argument WITH-TIME, the prompt will suggest to also
  11313. insert a time. Note that when WITH-TIME is not set, you can still
  11314. enter a time, and this function will inform the calling routine about
  11315. this change. The calling routine may then choose to change the format
  11316. used to insert the time stamp into the buffer to include the time.
  11317. With optional argument FROM-STRING, read from this string instead from
  11318. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11319. the time/date that is used for everything that is not specified by the
  11320. user."
  11321. (require 'parse-time)
  11322. (let* ((org-time-stamp-rounding-minutes
  11323. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11324. (org-dcst org-display-custom-times)
  11325. (ct (org-current-time))
  11326. (def (or org-overriding-default-time default-time ct))
  11327. (defdecode (decode-time def))
  11328. (dummy (progn
  11329. (when (< (nth 2 defdecode) org-extend-today-until)
  11330. (setcar (nthcdr 2 defdecode) -1)
  11331. (setcar (nthcdr 1 defdecode) 59)
  11332. (setq def (apply 'encode-time defdecode)
  11333. defdecode (decode-time def)))))
  11334. (calendar-move-hook nil)
  11335. (calendar-view-diary-initially-flag nil)
  11336. (view-diary-entries-initially nil)
  11337. (calendar-view-holidays-initially-flag nil)
  11338. (view-calendar-holidays-initially nil)
  11339. (timestr (format-time-string
  11340. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11341. (prompt (concat (if prompt (concat prompt " ") "")
  11342. (format "Date+time [%s]: " timestr)))
  11343. ans (org-ans0 "") org-ans1 org-ans2 final)
  11344. (cond
  11345. (from-string (setq ans from-string))
  11346. (org-read-date-popup-calendar
  11347. (save-excursion
  11348. (save-window-excursion
  11349. (calendar)
  11350. (calendar-forward-day (- (time-to-days def)
  11351. (calendar-absolute-from-gregorian
  11352. (calendar-current-date))))
  11353. (org-eval-in-calendar nil t)
  11354. (let* ((old-map (current-local-map))
  11355. (map (copy-keymap calendar-mode-map))
  11356. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11357. (org-defkey map (kbd "RET") 'org-calendar-select)
  11358. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11359. 'org-calendar-select-mouse)
  11360. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11361. 'org-calendar-select-mouse)
  11362. (org-defkey minibuffer-local-map [(meta shift left)]
  11363. (lambda () (interactive)
  11364. (org-eval-in-calendar '(calendar-backward-month 1))))
  11365. (org-defkey minibuffer-local-map [(meta shift right)]
  11366. (lambda () (interactive)
  11367. (org-eval-in-calendar '(calendar-forward-month 1))))
  11368. (org-defkey minibuffer-local-map [(meta shift up)]
  11369. (lambda () (interactive)
  11370. (org-eval-in-calendar '(calendar-backward-year 1))))
  11371. (org-defkey minibuffer-local-map [(meta shift down)]
  11372. (lambda () (interactive)
  11373. (org-eval-in-calendar '(calendar-forward-year 1))))
  11374. (org-defkey minibuffer-local-map [?\e (shift left)]
  11375. (lambda () (interactive)
  11376. (org-eval-in-calendar '(calendar-backward-month 1))))
  11377. (org-defkey minibuffer-local-map [?\e (shift right)]
  11378. (lambda () (interactive)
  11379. (org-eval-in-calendar '(calendar-forward-month 1))))
  11380. (org-defkey minibuffer-local-map [?\e (shift up)]
  11381. (lambda () (interactive)
  11382. (org-eval-in-calendar '(calendar-backward-year 1))))
  11383. (org-defkey minibuffer-local-map [?\e (shift down)]
  11384. (lambda () (interactive)
  11385. (org-eval-in-calendar '(calendar-forward-year 1))))
  11386. (org-defkey minibuffer-local-map [(shift up)]
  11387. (lambda () (interactive)
  11388. (org-eval-in-calendar '(calendar-backward-week 1))))
  11389. (org-defkey minibuffer-local-map [(shift down)]
  11390. (lambda () (interactive)
  11391. (org-eval-in-calendar '(calendar-forward-week 1))))
  11392. (org-defkey minibuffer-local-map [(shift left)]
  11393. (lambda () (interactive)
  11394. (org-eval-in-calendar '(calendar-backward-day 1))))
  11395. (org-defkey minibuffer-local-map [(shift right)]
  11396. (lambda () (interactive)
  11397. (org-eval-in-calendar '(calendar-forward-day 1))))
  11398. (org-defkey minibuffer-local-map ">"
  11399. (lambda () (interactive)
  11400. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11401. (org-defkey minibuffer-local-map "<"
  11402. (lambda () (interactive)
  11403. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11404. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11405. (unwind-protect
  11406. (progn
  11407. (use-local-map map)
  11408. (add-hook 'post-command-hook 'org-read-date-display)
  11409. (setq org-ans0 (read-string prompt default-input
  11410. 'org-read-date-history nil))
  11411. ;; org-ans0: from prompt
  11412. ;; org-ans1: from mouse click
  11413. ;; org-ans2: from calendar motion
  11414. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11415. (remove-hook 'post-command-hook 'org-read-date-display)
  11416. (use-local-map old-map)
  11417. (when org-read-date-overlay
  11418. (org-delete-overlay org-read-date-overlay)
  11419. (setq org-read-date-overlay nil)))))))
  11420. (t ; Naked prompt only
  11421. (unwind-protect
  11422. (setq ans (read-string prompt default-input
  11423. 'org-read-date-history timestr))
  11424. (when org-read-date-overlay
  11425. (org-delete-overlay org-read-date-overlay)
  11426. (setq org-read-date-overlay nil)))))
  11427. (setq final (org-read-date-analyze ans def defdecode))
  11428. (if to-time
  11429. (apply 'encode-time final)
  11430. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11431. (format "%04d-%02d-%02d %02d:%02d"
  11432. (nth 5 final) (nth 4 final) (nth 3 final)
  11433. (nth 2 final) (nth 1 final))
  11434. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11435. (defvar def)
  11436. (defvar defdecode)
  11437. (defvar with-time)
  11438. (defun org-read-date-display ()
  11439. "Display the current date prompt interpretation in the minibuffer."
  11440. (when org-read-date-display-live
  11441. (when org-read-date-overlay
  11442. (org-delete-overlay org-read-date-overlay))
  11443. (let ((p (point)))
  11444. (end-of-line 1)
  11445. (while (not (equal (buffer-substring
  11446. (max (point-min) (- (point) 4)) (point))
  11447. " "))
  11448. (insert " "))
  11449. (goto-char p))
  11450. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11451. " " (or org-ans1 org-ans2)))
  11452. (org-end-time-was-given nil)
  11453. (f (org-read-date-analyze ans def defdecode))
  11454. (fmts (if org-dcst
  11455. org-time-stamp-custom-formats
  11456. org-time-stamp-formats))
  11457. (fmt (if (or with-time
  11458. (and (boundp 'org-time-was-given) org-time-was-given))
  11459. (cdr fmts)
  11460. (car fmts)))
  11461. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11462. (when (and org-end-time-was-given
  11463. (string-match org-plain-time-of-day-regexp txt))
  11464. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11465. org-end-time-was-given
  11466. (substring txt (match-end 0)))))
  11467. (setq org-read-date-overlay
  11468. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11469. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11470. (defun org-read-date-analyze (ans def defdecode)
  11471. "Analyse the combined answer of the date prompt."
  11472. ;; FIXME: cleanup and comment
  11473. (let (delta deltan deltaw deltadef year month day
  11474. hour minute second wday pm h2 m2 tl wday1
  11475. iso-year iso-weekday iso-week iso-year iso-date)
  11476. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11477. (setq ans "+0"))
  11478. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11479. (setq ans (replace-match "" t t ans)
  11480. deltan (car delta)
  11481. deltaw (nth 1 delta)
  11482. deltadef (nth 2 delta)))
  11483. ;; Check if there is an iso week date in there
  11484. ;; If yes, sore the info and postpone interpreting it until the rest
  11485. ;; of the parsing is done
  11486. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11487. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11488. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11489. iso-week (string-to-number (match-string 2 ans)))
  11490. (setq ans (replace-match "" t t ans)))
  11491. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11492. (when (string-match
  11493. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11494. (setq year (if (match-end 2)
  11495. (string-to-number (match-string 2 ans))
  11496. (string-to-number (format-time-string "%Y")))
  11497. month (string-to-number (match-string 3 ans))
  11498. day (string-to-number (match-string 4 ans)))
  11499. (if (< year 100) (setq year (+ 2000 year)))
  11500. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11501. t nil ans)))
  11502. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11503. ;; If there is a time with am/pm, and *no* time without it, we convert
  11504. ;; so that matching will be successful.
  11505. (loop for i from 1 to 2 do ; twice, for end time as well
  11506. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11507. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11508. (setq hour (string-to-number (match-string 1 ans))
  11509. minute (if (match-end 3)
  11510. (string-to-number (match-string 3 ans))
  11511. 0)
  11512. pm (equal ?p
  11513. (string-to-char (downcase (match-string 4 ans)))))
  11514. (if (and (= hour 12) (not pm))
  11515. (setq hour 0)
  11516. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11517. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11518. t t ans))))
  11519. ;; Check if a time range is given as a duration
  11520. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11521. (setq hour (string-to-number (match-string 1 ans))
  11522. h2 (+ hour (string-to-number (match-string 3 ans)))
  11523. minute (string-to-number (match-string 2 ans))
  11524. m2 (+ minute (if (match-end 5) (string-to-number
  11525. (match-string 5 ans))0)))
  11526. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11527. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11528. t t ans)))
  11529. ;; Check if there is a time range
  11530. (when (boundp 'org-end-time-was-given)
  11531. (setq org-time-was-given nil)
  11532. (when (and (string-match org-plain-time-of-day-regexp ans)
  11533. (match-end 8))
  11534. (setq org-end-time-was-given (match-string 8 ans))
  11535. (setq ans (concat (substring ans 0 (match-beginning 7))
  11536. (substring ans (match-end 7))))))
  11537. (setq tl (parse-time-string ans)
  11538. day (or (nth 3 tl) (nth 3 defdecode))
  11539. month (or (nth 4 tl)
  11540. (if (and org-read-date-prefer-future
  11541. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11542. (1+ (nth 4 defdecode))
  11543. (nth 4 defdecode)))
  11544. year (or (nth 5 tl)
  11545. (if (and org-read-date-prefer-future
  11546. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11547. (1+ (nth 5 defdecode))
  11548. (nth 5 defdecode)))
  11549. hour (or (nth 2 tl) (nth 2 defdecode))
  11550. minute (or (nth 1 tl) (nth 1 defdecode))
  11551. second (or (nth 0 tl) 0)
  11552. wday (nth 6 tl))
  11553. ;; Special date definitions below
  11554. (cond
  11555. (iso-week
  11556. ;; There was an iso week
  11557. (setq year (or iso-year year)
  11558. day (or iso-weekday wday 1)
  11559. wday nil ; to make sure that the trigger below does not match
  11560. iso-date (calendar-gregorian-from-absolute
  11561. (calendar-absolute-from-iso
  11562. (list iso-week day year))))
  11563. ; FIXME: Should we also push ISO weeks into the future?
  11564. ; (when (and org-read-date-prefer-future
  11565. ; (not iso-year)
  11566. ; (< (calendar-absolute-from-gregorian iso-date)
  11567. ; (time-to-days (current-time))))
  11568. ; (setq year (1+ year)
  11569. ; iso-date (calendar-gregorian-from-absolute
  11570. ; (calendar-absolute-from-iso
  11571. ; (list iso-week day year)))))
  11572. (setq month (car iso-date)
  11573. year (nth 2 iso-date)
  11574. day (nth 1 iso-date)))
  11575. (deltan
  11576. (unless deltadef
  11577. (let ((now (decode-time (current-time))))
  11578. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11579. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11580. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11581. ((equal deltaw "m") (setq month (+ month deltan)))
  11582. ((equal deltaw "y") (setq year (+ year deltan)))))
  11583. ((and wday (not (nth 3 tl)))
  11584. ;; Weekday was given, but no day, so pick that day in the week
  11585. ;; on or after the derived date.
  11586. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11587. (unless (equal wday wday1)
  11588. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11589. (if (and (boundp 'org-time-was-given)
  11590. (nth 2 tl))
  11591. (setq org-time-was-given t))
  11592. (if (< year 100) (setq year (+ 2000 year)))
  11593. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11594. (list second minute hour day month year)))
  11595. (defvar parse-time-weekdays)
  11596. (defun org-read-date-get-relative (s today default)
  11597. "Check string S for special relative date string.
  11598. TODAY and DEFAULT are internal times, for today and for a default.
  11599. Return shift list (N what def-flag)
  11600. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11601. N is the number of WHATs to shift.
  11602. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11603. the DEFAULT date rather than TODAY."
  11604. (when (and
  11605. (string-match
  11606. (concat
  11607. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11608. "\\([0-9]+\\)?"
  11609. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11610. "\\([ \t]\\|$\\)") s)
  11611. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11612. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11613. (string-to-char (substring (match-string 1 s) -1))
  11614. ?+))
  11615. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11616. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11617. (what (if (match-end 3) (match-string 3 s) "d"))
  11618. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11619. (date (if rel default today))
  11620. (wday (nth 6 (decode-time date)))
  11621. delta)
  11622. (if wday1
  11623. (progn
  11624. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11625. (if (= dir ?-) (setq delta (- delta 7)))
  11626. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11627. (list delta "d" rel))
  11628. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11629. (defun org-eval-in-calendar (form &optional keepdate)
  11630. "Eval FORM in the calendar window and return to current window.
  11631. Also, store the cursor date in variable org-ans2."
  11632. (let ((sw (selected-window)))
  11633. (select-window (get-buffer-window "*Calendar*"))
  11634. (eval form)
  11635. (when (and (not keepdate) (calendar-cursor-to-date))
  11636. (let* ((date (calendar-cursor-to-date))
  11637. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11638. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11639. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11640. (select-window sw)))
  11641. (defun org-calendar-select ()
  11642. "Return to `org-read-date' with the date currently selected.
  11643. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11644. (interactive)
  11645. (when (calendar-cursor-to-date)
  11646. (let* ((date (calendar-cursor-to-date))
  11647. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11648. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11649. (if (active-minibuffer-window) (exit-minibuffer))))
  11650. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11651. "Insert a date stamp for the date given by the internal TIME.
  11652. WITH-HM means, use the stamp format that includes the time of the day.
  11653. INACTIVE means use square brackets instead of angular ones, so that the
  11654. stamp will not contribute to the agenda.
  11655. PRE and POST are optional strings to be inserted before and after the
  11656. stamp.
  11657. The command returns the inserted time stamp."
  11658. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11659. stamp)
  11660. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11661. (insert-before-markers (or pre ""))
  11662. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11663. (when (listp extra)
  11664. (setq extra (car extra))
  11665. (if (and (stringp extra)
  11666. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11667. (setq extra (format "-%02d:%02d"
  11668. (string-to-number (match-string 1 extra))
  11669. (string-to-number (match-string 2 extra))))
  11670. (setq extra nil)))
  11671. (when extra
  11672. (backward-char 1)
  11673. (insert-before-markers extra)
  11674. (forward-char 1))
  11675. (insert-before-markers (or post ""))
  11676. (setq org-last-inserted-timestamp stamp)))
  11677. (defun org-toggle-time-stamp-overlays ()
  11678. "Toggle the use of custom time stamp formats."
  11679. (interactive)
  11680. (setq org-display-custom-times (not org-display-custom-times))
  11681. (unless org-display-custom-times
  11682. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11683. (while (setq p (next-single-property-change p 'display))
  11684. (if (and (get-text-property p 'display)
  11685. (eq (get-text-property p 'face) 'org-date))
  11686. (remove-text-properties
  11687. p (setq p (next-single-property-change p 'display))
  11688. '(display t))))
  11689. (set-buffer-modified-p bmp)))
  11690. (if (featurep 'xemacs)
  11691. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11692. (org-restart-font-lock)
  11693. (setq org-table-may-need-update t)
  11694. (if org-display-custom-times
  11695. (message "Time stamps are overlayed with custom format")
  11696. (message "Time stamp overlays removed")))
  11697. (defun org-display-custom-time (beg end)
  11698. "Overlay modified time stamp format over timestamp between BEG and END."
  11699. (let* ((ts (buffer-substring beg end))
  11700. t1 w1 with-hm tf time str w2 (off 0))
  11701. (save-match-data
  11702. (setq t1 (org-parse-time-string ts t))
  11703. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11704. (setq off (- (match-end 0) (match-beginning 0)))))
  11705. (setq end (- end off))
  11706. (setq w1 (- end beg)
  11707. with-hm (and (nth 1 t1) (nth 2 t1))
  11708. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11709. time (org-fix-decoded-time t1)
  11710. str (org-add-props
  11711. (format-time-string
  11712. (substring tf 1 -1) (apply 'encode-time time))
  11713. nil 'mouse-face 'highlight)
  11714. w2 (length str))
  11715. (if (not (= w2 w1))
  11716. (add-text-properties (1+ beg) (+ 2 beg)
  11717. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11718. (if (featurep 'xemacs)
  11719. (progn
  11720. (put-text-property beg end 'invisible t)
  11721. (put-text-property beg end 'end-glyph (make-glyph str)))
  11722. (put-text-property beg end 'display str))))
  11723. (defun org-translate-time (string)
  11724. "Translate all timestamps in STRING to custom format.
  11725. But do this only if the variable `org-display-custom-times' is set."
  11726. (when org-display-custom-times
  11727. (save-match-data
  11728. (let* ((start 0)
  11729. (re org-ts-regexp-both)
  11730. t1 with-hm inactive tf time str beg end)
  11731. (while (setq start (string-match re string start))
  11732. (setq beg (match-beginning 0)
  11733. end (match-end 0)
  11734. t1 (save-match-data
  11735. (org-parse-time-string (substring string beg end) t))
  11736. with-hm (and (nth 1 t1) (nth 2 t1))
  11737. inactive (equal (substring string beg (1+ beg)) "[")
  11738. tf (funcall (if with-hm 'cdr 'car)
  11739. org-time-stamp-custom-formats)
  11740. time (org-fix-decoded-time t1)
  11741. str (format-time-string
  11742. (concat
  11743. (if inactive "[" "<") (substring tf 1 -1)
  11744. (if inactive "]" ">"))
  11745. (apply 'encode-time time))
  11746. string (replace-match str t t string)
  11747. start (+ start (length str)))))))
  11748. string)
  11749. (defun org-fix-decoded-time (time)
  11750. "Set 0 instead of nil for the first 6 elements of time.
  11751. Don't touch the rest."
  11752. (let ((n 0))
  11753. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11754. (defun org-days-to-time (timestamp-string)
  11755. "Difference between TIMESTAMP-STRING and now in days."
  11756. (- (time-to-days (org-time-string-to-time timestamp-string))
  11757. (time-to-days (current-time))))
  11758. (defun org-deadline-close (timestamp-string &optional ndays)
  11759. "Is the time in TIMESTAMP-STRING close to the current date?"
  11760. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11761. (and (< (org-days-to-time timestamp-string) ndays)
  11762. (not (org-entry-is-done-p))))
  11763. (defun org-get-wdays (ts)
  11764. "Get the deadline lead time appropriate for timestring TS."
  11765. (cond
  11766. ((<= org-deadline-warning-days 0)
  11767. ;; 0 or negative, enforce this value no matter what
  11768. (- org-deadline-warning-days))
  11769. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11770. ;; lead time is specified.
  11771. (floor (* (string-to-number (match-string 1 ts))
  11772. (cdr (assoc (match-string 2 ts)
  11773. '(("d" . 1) ("w" . 7)
  11774. ("m" . 30.4) ("y" . 365.25)))))))
  11775. ;; go for the default.
  11776. (t org-deadline-warning-days)))
  11777. (defun org-calendar-select-mouse (ev)
  11778. "Return to `org-read-date' with the date currently selected.
  11779. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11780. (interactive "e")
  11781. (mouse-set-point ev)
  11782. (when (calendar-cursor-to-date)
  11783. (let* ((date (calendar-cursor-to-date))
  11784. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11785. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11786. (if (active-minibuffer-window) (exit-minibuffer))))
  11787. (defun org-check-deadlines (ndays)
  11788. "Check if there are any deadlines due or past due.
  11789. A deadline is considered due if it happens within `org-deadline-warning-days'
  11790. days from today's date. If the deadline appears in an entry marked DONE,
  11791. it is not shown. The prefix arg NDAYS can be used to test that many
  11792. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11793. (interactive "P")
  11794. (let* ((org-warn-days
  11795. (cond
  11796. ((equal ndays '(4)) 100000)
  11797. (ndays (prefix-numeric-value ndays))
  11798. (t (abs org-deadline-warning-days))))
  11799. (case-fold-search nil)
  11800. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11801. (callback
  11802. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11803. (message "%d deadlines past-due or due within %d days"
  11804. (org-occur regexp nil callback)
  11805. org-warn-days)))
  11806. (defun org-check-before-date (date)
  11807. "Check if there are deadlines or scheduled entries before DATE."
  11808. (interactive (list (org-read-date)))
  11809. (let ((case-fold-search nil)
  11810. (regexp (concat "\\<\\(" org-deadline-string
  11811. "\\|" org-scheduled-string
  11812. "\\) *<\\([^>]+\\)>"))
  11813. (callback
  11814. (lambda () (time-less-p
  11815. (org-time-string-to-time (match-string 2))
  11816. (org-time-string-to-time date)))))
  11817. (message "%d entries before %s"
  11818. (org-occur regexp nil callback) date)))
  11819. (defun org-check-after-date (date)
  11820. "Check if there are deadlines or scheduled entries after DATE."
  11821. (interactive (list (org-read-date)))
  11822. (let ((case-fold-search nil)
  11823. (regexp (concat "\\<\\(" org-deadline-string
  11824. "\\|" org-scheduled-string
  11825. "\\) *<\\([^>]+\\)>"))
  11826. (callback
  11827. (lambda () (not
  11828. (time-less-p
  11829. (org-time-string-to-time (match-string 2))
  11830. (org-time-string-to-time date))))))
  11831. (message "%d entries after %s"
  11832. (org-occur regexp nil callback) date)))
  11833. (defun org-evaluate-time-range (&optional to-buffer)
  11834. "Evaluate a time range by computing the difference between start and end.
  11835. Normally the result is just printed in the echo area, but with prefix arg
  11836. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11837. If the time range is actually in a table, the result is inserted into the
  11838. next column.
  11839. For time difference computation, a year is assumed to be exactly 365
  11840. days in order to avoid rounding problems."
  11841. (interactive "P")
  11842. (or
  11843. (org-clock-update-time-maybe)
  11844. (save-excursion
  11845. (unless (org-at-date-range-p t)
  11846. (goto-char (point-at-bol))
  11847. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11848. (if (not (org-at-date-range-p t))
  11849. (error "Not at a time-stamp range, and none found in current line")))
  11850. (let* ((ts1 (match-string 1))
  11851. (ts2 (match-string 2))
  11852. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11853. (match-end (match-end 0))
  11854. (time1 (org-time-string-to-time ts1))
  11855. (time2 (org-time-string-to-time ts2))
  11856. (t1 (time-to-seconds time1))
  11857. (t2 (time-to-seconds time2))
  11858. (diff (abs (- t2 t1)))
  11859. (negative (< (- t2 t1) 0))
  11860. ;; (ys (floor (* 365 24 60 60)))
  11861. (ds (* 24 60 60))
  11862. (hs (* 60 60))
  11863. (fy "%dy %dd %02d:%02d")
  11864. (fy1 "%dy %dd")
  11865. (fd "%dd %02d:%02d")
  11866. (fd1 "%dd")
  11867. (fh "%02d:%02d")
  11868. y d h m align)
  11869. (if havetime
  11870. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11871. y 0
  11872. d (floor (/ diff ds)) diff (mod diff ds)
  11873. h (floor (/ diff hs)) diff (mod diff hs)
  11874. m (floor (/ diff 60)))
  11875. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11876. y 0
  11877. d (floor (+ (/ diff ds) 0.5))
  11878. h 0 m 0))
  11879. (if (not to-buffer)
  11880. (message "%s" (org-make-tdiff-string y d h m))
  11881. (if (org-at-table-p)
  11882. (progn
  11883. (goto-char match-end)
  11884. (setq align t)
  11885. (and (looking-at " *|") (goto-char (match-end 0))))
  11886. (goto-char match-end))
  11887. (if (looking-at
  11888. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11889. (replace-match ""))
  11890. (if negative (insert " -"))
  11891. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11892. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11893. (insert " " (format fh h m))))
  11894. (if align (org-table-align))
  11895. (message "Time difference inserted")))))
  11896. (defun org-make-tdiff-string (y d h m)
  11897. (let ((fmt "")
  11898. (l nil))
  11899. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11900. l (push y l)))
  11901. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11902. l (push d l)))
  11903. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11904. l (push h l)))
  11905. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11906. l (push m l)))
  11907. (apply 'format fmt (nreverse l))))
  11908. (defun org-time-string-to-time (s)
  11909. (apply 'encode-time (org-parse-time-string s)))
  11910. (defun org-time-string-to-seconds (s)
  11911. (time-to-seconds (org-time-string-to-time s)))
  11912. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11913. "Convert a time stamp to an absolute day number.
  11914. If there is a specifyer for a cyclic time stamp, get the closest date to
  11915. DAYNR.
  11916. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11917. (cond
  11918. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11919. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11920. daynr
  11921. (+ daynr 1000)))
  11922. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11923. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11924. (time-to-days (current-time))) (match-string 0 s)
  11925. prefer show-all))
  11926. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11927. (defun org-days-to-iso-week (days)
  11928. "Return the iso week number."
  11929. (require 'cal-iso)
  11930. (car (calendar-iso-from-absolute days)))
  11931. (defun org-small-year-to-year (year)
  11932. "Convert 2-digit years into 4-digit years.
  11933. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11934. The year 2000 cannot be abbreviated. Any year larger than 99
  11935. is returned unchanged."
  11936. (if (< year 38)
  11937. (setq year (+ 2000 year))
  11938. (if (< year 100)
  11939. (setq year (+ 1900 year))))
  11940. year)
  11941. (defun org-time-from-absolute (d)
  11942. "Return the time corresponding to date D.
  11943. D may be an absolute day number, or a calendar-type list (month day year)."
  11944. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11945. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11946. (defun org-calendar-holiday ()
  11947. "List of holidays, for Diary display in Org-mode."
  11948. (require 'holidays)
  11949. (let ((hl (funcall
  11950. (if (fboundp 'calendar-check-holidays)
  11951. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11952. (if hl (mapconcat 'identity hl "; "))))
  11953. (defun org-diary-sexp-entry (sexp entry date)
  11954. "Process a SEXP diary ENTRY for DATE."
  11955. (require 'diary-lib)
  11956. (let ((result (if calendar-debug-sexp
  11957. (let ((stack-trace-on-error t))
  11958. (eval (car (read-from-string sexp))))
  11959. (condition-case nil
  11960. (eval (car (read-from-string sexp)))
  11961. (error
  11962. (beep)
  11963. (message "Bad sexp at line %d in %s: %s"
  11964. (org-current-line)
  11965. (buffer-file-name) sexp)
  11966. (sleep-for 2))))))
  11967. (cond ((stringp result) result)
  11968. ((and (consp result)
  11969. (stringp (cdr result))) (cdr result))
  11970. (result entry)
  11971. (t nil))))
  11972. (defun org-diary-to-ical-string (frombuf)
  11973. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11974. This uses the icalendar.el library."
  11975. (let* ((tmpdir (if (featurep 'xemacs)
  11976. (temp-directory)
  11977. temporary-file-directory))
  11978. (tmpfile (make-temp-name
  11979. (expand-file-name "orgics" tmpdir)))
  11980. buf rtn b e)
  11981. (save-excursion
  11982. (set-buffer frombuf)
  11983. (icalendar-export-region (point-min) (point-max) tmpfile)
  11984. (setq buf (find-buffer-visiting tmpfile))
  11985. (set-buffer buf)
  11986. (goto-char (point-min))
  11987. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11988. (setq b (match-beginning 0)))
  11989. (goto-char (point-max))
  11990. (if (re-search-backward "^END:VEVENT" nil t)
  11991. (setq e (match-end 0)))
  11992. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11993. (kill-buffer buf)
  11994. (delete-file tmpfile)
  11995. rtn))
  11996. (defun org-closest-date (start current change prefer show-all)
  11997. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11998. When PREFER is `past' return a date that is either CURRENT or past.
  11999. When PREFER is `future', return a date that is either CURRENT or future.
  12000. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12001. ;; Make the proper lists from the dates
  12002. (catch 'exit
  12003. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12004. dn dw sday cday n1 n2 n0
  12005. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12006. (setq start (org-date-to-gregorian start)
  12007. current (org-date-to-gregorian
  12008. (if show-all
  12009. current
  12010. (time-to-days (current-time))))
  12011. sday (calendar-absolute-from-gregorian start)
  12012. cday (calendar-absolute-from-gregorian current))
  12013. (if (<= cday sday) (throw 'exit sday))
  12014. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12015. (setq dn (string-to-number (match-string 1 change))
  12016. dw (cdr (assoc (match-string 2 change) a1)))
  12017. (error "Invalid change specifyer: %s" change))
  12018. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12019. (cond
  12020. ((eq dw 'day)
  12021. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12022. n2 (+ n1 dn)))
  12023. ((eq dw 'year)
  12024. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12025. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12026. (setq date1 (list m d y1)
  12027. n1 (calendar-absolute-from-gregorian date1)
  12028. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12029. n2 (calendar-absolute-from-gregorian date2)))
  12030. ((eq dw 'month)
  12031. ;; approx number of month between the two dates
  12032. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12033. ;; How often does dn fit in there?
  12034. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12035. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12036. m (+ m nm)
  12037. ny (floor (/ m 12))
  12038. y (+ y ny)
  12039. m (- m (* ny 12)))
  12040. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12041. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12042. (setq m2 (+ m dn) y2 y)
  12043. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12044. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12045. (while (<= n2 cday)
  12046. (setq n1 n2 m m2 y y2)
  12047. (setq m2 (+ m dn) y2 y)
  12048. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12049. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12050. ;; Make sure n1 is the earlier date
  12051. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12052. (if show-all
  12053. (cond
  12054. ((eq prefer 'past) n1)
  12055. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12056. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12057. (cond
  12058. ((eq prefer 'past) n1)
  12059. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12060. (t (if (= cday n1) n1 n2)))))))
  12061. (defun org-date-to-gregorian (date)
  12062. "Turn any specification of DATE into a gregorian date for the calendar."
  12063. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12064. ((and (listp date) (= (length date) 3)) date)
  12065. ((stringp date)
  12066. (setq date (org-parse-time-string date))
  12067. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12068. ((listp date)
  12069. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12070. (defun org-parse-time-string (s &optional nodefault)
  12071. "Parse the standard Org-mode time string.
  12072. This should be a lot faster than the normal `parse-time-string'.
  12073. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12074. hour and minute fields will be nil if not given."
  12075. (if (string-match org-ts-regexp0 s)
  12076. (list 0
  12077. (if (or (match-beginning 8) (not nodefault))
  12078. (string-to-number (or (match-string 8 s) "0")))
  12079. (if (or (match-beginning 7) (not nodefault))
  12080. (string-to-number (or (match-string 7 s) "0")))
  12081. (string-to-number (match-string 4 s))
  12082. (string-to-number (match-string 3 s))
  12083. (string-to-number (match-string 2 s))
  12084. nil nil nil)
  12085. (make-list 9 0)))
  12086. (defun org-timestamp-up (&optional arg)
  12087. "Increase the date item at the cursor by one.
  12088. If the cursor is on the year, change the year. If it is on the month or
  12089. the day, change that.
  12090. With prefix ARG, change by that many units."
  12091. (interactive "p")
  12092. (org-timestamp-change (prefix-numeric-value arg)))
  12093. (defun org-timestamp-down (&optional arg)
  12094. "Decrease the date item at the cursor by one.
  12095. If the cursor is on the year, change the year. If it is on the month or
  12096. the day, change that.
  12097. With prefix ARG, change by that many units."
  12098. (interactive "p")
  12099. (org-timestamp-change (- (prefix-numeric-value arg))))
  12100. (defun org-timestamp-up-day (&optional arg)
  12101. "Increase the date in the time stamp by one day.
  12102. With prefix ARG, change that many days."
  12103. (interactive "p")
  12104. (if (and (not (org-at-timestamp-p t))
  12105. (org-on-heading-p))
  12106. (org-todo 'up)
  12107. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12108. (defun org-timestamp-down-day (&optional arg)
  12109. "Decrease the date in the time stamp by one day.
  12110. With prefix ARG, change that many days."
  12111. (interactive "p")
  12112. (if (and (not (org-at-timestamp-p t))
  12113. (org-on-heading-p))
  12114. (org-todo 'down)
  12115. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12116. (defun org-at-timestamp-p (&optional inactive-ok)
  12117. "Determine if the cursor is in or at a timestamp."
  12118. (interactive)
  12119. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12120. (pos (point))
  12121. (ans (or (looking-at tsr)
  12122. (save-excursion
  12123. (skip-chars-backward "^[<\n\r\t")
  12124. (if (> (point) (point-min)) (backward-char 1))
  12125. (and (looking-at tsr)
  12126. (> (- (match-end 0) pos) -1))))))
  12127. (and ans
  12128. (boundp 'org-ts-what)
  12129. (setq org-ts-what
  12130. (cond
  12131. ((= pos (match-beginning 0)) 'bracket)
  12132. ((= pos (1- (match-end 0))) 'bracket)
  12133. ((org-pos-in-match-range pos 2) 'year)
  12134. ((org-pos-in-match-range pos 3) 'month)
  12135. ((org-pos-in-match-range pos 7) 'hour)
  12136. ((org-pos-in-match-range pos 8) 'minute)
  12137. ((or (org-pos-in-match-range pos 4)
  12138. (org-pos-in-match-range pos 5)) 'day)
  12139. ((and (> pos (or (match-end 8) (match-end 5)))
  12140. (< pos (match-end 0)))
  12141. (- pos (or (match-end 8) (match-end 5))))
  12142. (t 'day))))
  12143. ans))
  12144. (defun org-toggle-timestamp-type ()
  12145. "Toggle the type (<active> or [inactive]) of a time stamp."
  12146. (interactive)
  12147. (when (org-at-timestamp-p t)
  12148. (let ((beg (match-beginning 0)) (end (match-end 0))
  12149. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12150. (save-excursion
  12151. (goto-char beg)
  12152. (while (re-search-forward "[][<>]" end t)
  12153. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12154. t t)))
  12155. (message "Timestamp is now %sactive"
  12156. (if (equal (char-after beg) ?<) "" "in")))))
  12157. (defun org-timestamp-change (n &optional what)
  12158. "Change the date in the time stamp at point.
  12159. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12160. `year', `minute', `second'. If WHAT is not given, the cursor position
  12161. in the timestamp determines what will be changed."
  12162. (let ((pos (point))
  12163. with-hm inactive
  12164. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12165. org-ts-what
  12166. extra rem
  12167. ts time time0)
  12168. (if (not (org-at-timestamp-p t))
  12169. (error "Not at a timestamp"))
  12170. (if (and (not what) (eq org-ts-what 'bracket))
  12171. (org-toggle-timestamp-type)
  12172. (if (and (not what) (not (eq org-ts-what 'day))
  12173. org-display-custom-times
  12174. (get-text-property (point) 'display)
  12175. (not (get-text-property (1- (point)) 'display)))
  12176. (setq org-ts-what 'day))
  12177. (setq org-ts-what (or what org-ts-what)
  12178. inactive (= (char-after (match-beginning 0)) ?\[)
  12179. ts (match-string 0))
  12180. (replace-match "")
  12181. (if (string-match
  12182. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12183. ts)
  12184. (setq extra (match-string 1 ts)))
  12185. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12186. (setq with-hm t))
  12187. (setq time0 (org-parse-time-string ts))
  12188. (when (and (eq org-ts-what 'minute)
  12189. (eq current-prefix-arg nil))
  12190. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12191. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12192. (setcar (cdr time0) (+ (nth 1 time0)
  12193. (if (> n 0) (- rem) (- dm rem))))))
  12194. (setq time
  12195. (encode-time (or (car time0) 0)
  12196. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12197. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12198. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12199. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12200. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12201. (nthcdr 6 time0)))
  12202. (when (and (member org-ts-what '(hour minute))
  12203. extra
  12204. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12205. (setq extra (org-modify-ts-extra
  12206. extra
  12207. (if (eq org-ts-what 'hour) 2 5)
  12208. n dm)))
  12209. (when (integerp org-ts-what)
  12210. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12211. (if (eq what 'calendar)
  12212. (let ((cal-date (org-get-date-from-calendar)))
  12213. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12214. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12215. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12216. (setcar time0 (or (car time0) 0))
  12217. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12218. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12219. (setq time (apply 'encode-time time0))))
  12220. (setq org-last-changed-timestamp
  12221. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12222. (org-clock-update-time-maybe)
  12223. (goto-char pos)
  12224. ;; Try to recenter the calendar window, if any
  12225. (if (and org-calendar-follow-timestamp-change
  12226. (get-buffer-window "*Calendar*" t)
  12227. (memq org-ts-what '(day month year)))
  12228. (org-recenter-calendar (time-to-days time))))))
  12229. (defun org-modify-ts-extra (s pos n dm)
  12230. "Change the different parts of the lead-time and repeat fields in timestamp."
  12231. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12232. ng h m new rem)
  12233. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12234. (cond
  12235. ((or (org-pos-in-match-range pos 2)
  12236. (org-pos-in-match-range pos 3))
  12237. (setq m (string-to-number (match-string 3 s))
  12238. h (string-to-number (match-string 2 s)))
  12239. (if (org-pos-in-match-range pos 2)
  12240. (setq h (+ h n))
  12241. (setq n (* dm (org-no-warnings (signum n))))
  12242. (when (not (= 0 (setq rem (% m dm))))
  12243. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12244. (setq m (+ m n)))
  12245. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12246. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12247. (setq h (min 24 (max 0 h)))
  12248. (setq ng 1 new (format "-%02d:%02d" h m)))
  12249. ((org-pos-in-match-range pos 6)
  12250. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12251. ((org-pos-in-match-range pos 5)
  12252. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12253. ((org-pos-in-match-range pos 9)
  12254. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12255. ((org-pos-in-match-range pos 8)
  12256. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12257. (when ng
  12258. (setq s (concat
  12259. (substring s 0 (match-beginning ng))
  12260. new
  12261. (substring s (match-end ng))))))
  12262. s))
  12263. (defun org-recenter-calendar (date)
  12264. "If the calendar is visible, recenter it to DATE."
  12265. (let* ((win (selected-window))
  12266. (cwin (get-buffer-window "*Calendar*" t))
  12267. (calendar-move-hook nil))
  12268. (when cwin
  12269. (select-window cwin)
  12270. (calendar-goto-date (if (listp date) date
  12271. (calendar-gregorian-from-absolute date)))
  12272. (select-window win))))
  12273. (defun org-goto-calendar (&optional arg)
  12274. "Go to the Emacs calendar at the current date.
  12275. If there is a time stamp in the current line, go to that date.
  12276. A prefix ARG can be used to force the current date."
  12277. (interactive "P")
  12278. (let ((tsr org-ts-regexp) diff
  12279. (calendar-move-hook nil)
  12280. (calendar-view-holidays-initially-flag nil)
  12281. (view-calendar-holidays-initially nil)
  12282. (calendar-view-diary-initially-flag nil)
  12283. (view-diary-entries-initially nil))
  12284. (if (or (org-at-timestamp-p)
  12285. (save-excursion
  12286. (beginning-of-line 1)
  12287. (looking-at (concat ".*" tsr))))
  12288. (let ((d1 (time-to-days (current-time)))
  12289. (d2 (time-to-days
  12290. (org-time-string-to-time (match-string 1)))))
  12291. (setq diff (- d2 d1))))
  12292. (calendar)
  12293. (calendar-goto-today)
  12294. (if (and diff (not arg)) (calendar-forward-day diff))))
  12295. (defun org-get-date-from-calendar ()
  12296. "Return a list (month day year) of date at point in calendar."
  12297. (with-current-buffer "*Calendar*"
  12298. (save-match-data
  12299. (calendar-cursor-to-date))))
  12300. (defun org-date-from-calendar ()
  12301. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12302. If there is already a time stamp at the cursor position, update it."
  12303. (interactive)
  12304. (if (org-at-timestamp-p t)
  12305. (org-timestamp-change 0 'calendar)
  12306. (let ((cal-date (org-get-date-from-calendar)))
  12307. (org-insert-time-stamp
  12308. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12309. (defun org-minutes-to-hh:mm-string (m)
  12310. "Compute H:MM from a number of minutes."
  12311. (let ((h (/ m 60)))
  12312. (setq m (- m (* 60 h)))
  12313. (format org-time-clocksum-format h m)))
  12314. (defun org-hh:mm-string-to-minutes (s)
  12315. "Convert a string H:MM to a number of minutes."
  12316. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12317. (+ (* (string-to-number (match-string 1 s)) 60)
  12318. (string-to-number (match-string 2 s)))
  12319. 0))
  12320. ;;;; Files
  12321. (defun org-save-all-org-buffers ()
  12322. "Save all Org-mode buffers without user confirmation."
  12323. (interactive)
  12324. (message "Saving all Org-mode buffers...")
  12325. (save-some-buffers t 'org-mode-p)
  12326. (when (featurep 'org-id) (org-id-locations-save))
  12327. (message "Saving all Org-mode buffers... done"))
  12328. (defun org-revert-all-org-buffers ()
  12329. "Revert all Org-mode buffers.
  12330. Prompt for confirmation when there are unsaved changes.
  12331. Be sure you know what you are doing before letting this function
  12332. overwrite your changes.
  12333. This function is useful in a setup where one tracks org files
  12334. with a version control system, to revert on one machine after pulling
  12335. changes from another. I believe the procedure must be like this:
  12336. 1. M-x org-save-all-org-buffers
  12337. 2. Pull changes from the other machine, resolve conflicts
  12338. 3. M-x org-revert-all-org-buffers"
  12339. (interactive)
  12340. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12341. (error "Abort"))
  12342. (save-excursion
  12343. (save-window-excursion
  12344. (mapc
  12345. (lambda (b)
  12346. (when (and (with-current-buffer b (org-mode-p))
  12347. (with-current-buffer b buffer-file-name))
  12348. (switch-to-buffer b)
  12349. (revert-buffer t 'no-confirm)))
  12350. (buffer-list))
  12351. (when (and (featurep 'org-id) org-id-track-globally)
  12352. (org-id-locations-load)))))
  12353. ;;;; Agenda files
  12354. ;;;###autoload
  12355. (defun org-iswitchb (&optional arg)
  12356. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12357. With a prefix argument, restrict available to files.
  12358. With two prefix arguments, restrict available buffers to agenda files.
  12359. Due to some yet unresolved reason, the global function
  12360. `iswitchb-mode' needs to be active for this function to work."
  12361. (interactive "P")
  12362. (require 'iswitchb)
  12363. (let ((enabled iswitchb-mode) blist)
  12364. (or enabled (iswitchb-mode 1))
  12365. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12366. ((equal arg '(16)) (org-buffer-list 'agenda))
  12367. (t (org-buffer-list))))
  12368. (unwind-protect
  12369. (let ((iswitchb-make-buflist-hook
  12370. (lambda ()
  12371. (setq iswitchb-temp-buflist
  12372. (mapcar 'buffer-name blist)))))
  12373. (switch-to-buffer
  12374. (iswitchb-read-buffer
  12375. "Switch-to: " nil t))
  12376. (or enabled (iswitchb-mode -1))))))
  12377. ;;;###autoload
  12378. (defun org-ido-switchb (&optional arg)
  12379. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12380. With a prefix argument, restrict available to files.
  12381. With two prefix arguments, restrict available buffers to agenda files."
  12382. (interactive "P")
  12383. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12384. ((equal arg '(16)) (org-buffer-list 'agenda))
  12385. (t (org-buffer-list)))))
  12386. (switch-to-buffer
  12387. (org-ido-completing-read "Org buffer: "
  12388. (mapcar 'list (mapcar 'buffer-name blist))
  12389. nil t))))
  12390. (defun org-buffer-list (&optional predicate exclude-tmp)
  12391. "Return a list of Org buffers.
  12392. PREDICATE can be `export', `files' or `agenda'.
  12393. export restrict the list to Export buffers.
  12394. files restrict the list to buffers visiting Org files.
  12395. agenda restrict the list to buffers visiting agenda files.
  12396. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12397. (let* ((bfn nil)
  12398. (agenda-files (and (eq predicate 'agenda)
  12399. (mapcar 'file-truename (org-agenda-files t))))
  12400. (filter
  12401. (cond
  12402. ((eq predicate 'files)
  12403. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12404. ((eq predicate 'export)
  12405. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12406. ((eq predicate 'agenda)
  12407. (lambda (b)
  12408. (with-current-buffer b
  12409. (and (eq major-mode 'org-mode)
  12410. (setq bfn (buffer-file-name b))
  12411. (member (file-truename bfn) agenda-files)))))
  12412. (t (lambda (b) (with-current-buffer b
  12413. (or (eq major-mode 'org-mode)
  12414. (string-match "\*Org .*Export"
  12415. (buffer-name b)))))))))
  12416. (delq nil
  12417. (mapcar
  12418. (lambda(b)
  12419. (if (and (funcall filter b)
  12420. (or (not exclude-tmp)
  12421. (not (string-match "tmp" (buffer-name b)))))
  12422. b
  12423. nil))
  12424. (buffer-list)))))
  12425. (defun org-agenda-files (&optional unrestricted archives)
  12426. "Get the list of agenda files.
  12427. Optional UNRESTRICTED means return the full list even if a restriction
  12428. is currently in place.
  12429. When ARCHIVES is t, include all archive files hat are really being
  12430. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12431. `org-agenda-archives-mode' is t."
  12432. (let ((files
  12433. (cond
  12434. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12435. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12436. ((listp org-agenda-files) org-agenda-files)
  12437. (t (error "Invalid value of `org-agenda-files'")))))
  12438. (setq files (apply 'append
  12439. (mapcar (lambda (f)
  12440. (if (file-directory-p f)
  12441. (directory-files
  12442. f t org-agenda-file-regexp)
  12443. (list f)))
  12444. files)))
  12445. (when org-agenda-skip-unavailable-files
  12446. (setq files (delq nil
  12447. (mapcar (function
  12448. (lambda (file)
  12449. (and (file-readable-p file) file)))
  12450. files))))
  12451. (when (or (eq archives t)
  12452. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12453. (setq files (org-add-archive-files files)))
  12454. files))
  12455. (defun org-edit-agenda-file-list ()
  12456. "Edit the list of agenda files.
  12457. Depending on setup, this either uses customize to edit the variable
  12458. `org-agenda-files', or it visits the file that is holding the list. In the
  12459. latter case, the buffer is set up in a way that saving it automatically kills
  12460. the buffer and restores the previous window configuration."
  12461. (interactive)
  12462. (if (stringp org-agenda-files)
  12463. (let ((cw (current-window-configuration)))
  12464. (find-file org-agenda-files)
  12465. (org-set-local 'org-window-configuration cw)
  12466. (org-add-hook 'after-save-hook
  12467. (lambda ()
  12468. (set-window-configuration
  12469. (prog1 org-window-configuration
  12470. (kill-buffer (current-buffer))))
  12471. (org-install-agenda-files-menu)
  12472. (message "New agenda file list installed"))
  12473. nil 'local)
  12474. (message "%s" (substitute-command-keys
  12475. "Edit list and finish with \\[save-buffer]")))
  12476. (customize-variable 'org-agenda-files)))
  12477. (defun org-store-new-agenda-file-list (list)
  12478. "Set new value for the agenda file list and save it correctly."
  12479. (if (stringp org-agenda-files)
  12480. (let ((f org-agenda-files) b)
  12481. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12482. (with-temp-file f
  12483. (insert (mapconcat 'identity list "\n") "\n")))
  12484. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12485. (setq org-agenda-files list)
  12486. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12487. (defun org-read-agenda-file-list ()
  12488. "Read the list of agenda files from a file."
  12489. (when (file-directory-p org-agenda-files)
  12490. (error "`org-agenda-files' cannot be a single directory"))
  12491. (when (stringp org-agenda-files)
  12492. (with-temp-buffer
  12493. (insert-file-contents org-agenda-files)
  12494. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12495. ;;;###autoload
  12496. (defun org-cycle-agenda-files ()
  12497. "Cycle through the files in `org-agenda-files'.
  12498. If the current buffer visits an agenda file, find the next one in the list.
  12499. If the current buffer does not, find the first agenda file."
  12500. (interactive)
  12501. (let* ((fs (org-agenda-files t))
  12502. (files (append fs (list (car fs))))
  12503. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12504. file)
  12505. (unless files (error "No agenda files"))
  12506. (catch 'exit
  12507. (while (setq file (pop files))
  12508. (if (equal (file-truename file) tcf)
  12509. (when (car files)
  12510. (find-file (car files))
  12511. (throw 'exit t))))
  12512. (find-file (car fs)))
  12513. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12514. (defun org-agenda-file-to-front (&optional to-end)
  12515. "Move/add the current file to the top of the agenda file list.
  12516. If the file is not present in the list, it is added to the front. If it is
  12517. present, it is moved there. With optional argument TO-END, add/move to the
  12518. end of the list."
  12519. (interactive "P")
  12520. (let ((org-agenda-skip-unavailable-files nil)
  12521. (file-alist (mapcar (lambda (x)
  12522. (cons (file-truename x) x))
  12523. (org-agenda-files t)))
  12524. (ctf (file-truename buffer-file-name))
  12525. x had)
  12526. (setq x (assoc ctf file-alist) had x)
  12527. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12528. (if to-end
  12529. (setq file-alist (append (delq x file-alist) (list x)))
  12530. (setq file-alist (cons x (delq x file-alist))))
  12531. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12532. (org-install-agenda-files-menu)
  12533. (message "File %s to %s of agenda file list"
  12534. (if had "moved" "added") (if to-end "end" "front"))))
  12535. (defun org-remove-file (&optional file)
  12536. "Remove current file from the list of files in variable `org-agenda-files'.
  12537. These are the files which are being checked for agenda entries.
  12538. Optional argument FILE means, use this file instead of the current."
  12539. (interactive)
  12540. (let* ((org-agenda-skip-unavailable-files nil)
  12541. (file (or file buffer-file-name))
  12542. (true-file (file-truename file))
  12543. (afile (abbreviate-file-name file))
  12544. (files (delq nil (mapcar
  12545. (lambda (x)
  12546. (if (equal true-file
  12547. (file-truename x))
  12548. nil x))
  12549. (org-agenda-files t)))))
  12550. (if (not (= (length files) (length (org-agenda-files t))))
  12551. (progn
  12552. (org-store-new-agenda-file-list files)
  12553. (org-install-agenda-files-menu)
  12554. (message "Removed file: %s" afile))
  12555. (message "File was not in list: %s (not removed)" afile))))
  12556. (defun org-file-menu-entry (file)
  12557. (vector file (list 'find-file file) t))
  12558. (defun org-check-agenda-file (file)
  12559. "Make sure FILE exists. If not, ask user what to do."
  12560. (when (not (file-exists-p file))
  12561. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12562. (abbreviate-file-name file))
  12563. (let ((r (downcase (read-char-exclusive))))
  12564. (cond
  12565. ((equal r ?r)
  12566. (org-remove-file file)
  12567. (throw 'nextfile t))
  12568. (t (error "Abort"))))))
  12569. (defun org-get-agenda-file-buffer (file)
  12570. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12571. it to the list of buffers which might be released later."
  12572. (let ((buf (org-find-base-buffer-visiting file)))
  12573. (if buf
  12574. buf ; just return it
  12575. ;; Make a new buffer and remember it
  12576. (setq buf (find-file-noselect file))
  12577. (if buf (push buf org-agenda-new-buffers))
  12578. buf)))
  12579. (defun org-release-buffers (blist)
  12580. "Release all buffers in list, asking the user for confirmation when needed.
  12581. When a buffer is unmodified, it is just killed. When modified, it is saved
  12582. \(if the user agrees) and then killed."
  12583. (let (buf file)
  12584. (while (setq buf (pop blist))
  12585. (setq file (buffer-file-name buf))
  12586. (when (and (buffer-modified-p buf)
  12587. file
  12588. (y-or-n-p (format "Save file %s? " file)))
  12589. (with-current-buffer buf (save-buffer)))
  12590. (kill-buffer buf))))
  12591. (defun org-prepare-agenda-buffers (files)
  12592. "Create buffers for all agenda files, protect archived trees and comments."
  12593. (interactive)
  12594. (let ((pa '(:org-archived t))
  12595. (pc '(:org-comment t))
  12596. (pall '(:org-archived t :org-comment t))
  12597. (inhibit-read-only t)
  12598. (rea (concat ":" org-archive-tag ":"))
  12599. bmp file re)
  12600. (save-excursion
  12601. (save-restriction
  12602. (while (setq file (pop files))
  12603. (catch 'nextfile
  12604. (if (bufferp file)
  12605. (set-buffer file)
  12606. (org-check-agenda-file file)
  12607. (set-buffer (org-get-agenda-file-buffer file)))
  12608. (widen)
  12609. (setq bmp (buffer-modified-p))
  12610. (org-refresh-category-properties)
  12611. (setq org-todo-keywords-for-agenda
  12612. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12613. (setq org-done-keywords-for-agenda
  12614. (append org-done-keywords-for-agenda org-done-keywords))
  12615. (setq org-todo-keyword-alist-for-agenda
  12616. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12617. (setq org-tag-alist-for-agenda
  12618. (append org-tag-alist-for-agenda org-tag-alist))
  12619. (save-excursion
  12620. (remove-text-properties (point-min) (point-max) pall)
  12621. (when org-agenda-skip-archived-trees
  12622. (goto-char (point-min))
  12623. (while (re-search-forward rea nil t)
  12624. (if (org-on-heading-p t)
  12625. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12626. (goto-char (point-min))
  12627. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12628. (while (re-search-forward re nil t)
  12629. (add-text-properties
  12630. (match-beginning 0) (org-end-of-subtree t) pc)))
  12631. (set-buffer-modified-p bmp)))))
  12632. (setq org-todo-keyword-alist-for-agenda
  12633. (org-uniquify org-todo-keyword-alist-for-agenda)
  12634. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12635. ;;;; Embedded LaTeX
  12636. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12637. "Keymap for the minor `org-cdlatex-mode'.")
  12638. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12639. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12640. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12641. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12642. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12643. (defvar org-cdlatex-texmathp-advice-is-done nil
  12644. "Flag remembering if we have applied the advice to texmathp already.")
  12645. (define-minor-mode org-cdlatex-mode
  12646. "Toggle the minor `org-cdlatex-mode'.
  12647. This mode supports entering LaTeX environment and math in LaTeX fragments
  12648. in Org-mode.
  12649. \\{org-cdlatex-mode-map}"
  12650. nil " OCDL" nil
  12651. (when org-cdlatex-mode (require 'cdlatex))
  12652. (unless org-cdlatex-texmathp-advice-is-done
  12653. (setq org-cdlatex-texmathp-advice-is-done t)
  12654. (defadvice texmathp (around org-math-always-on activate)
  12655. "Always return t in org-mode buffers.
  12656. This is because we want to insert math symbols without dollars even outside
  12657. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12658. an embedded LaTeX fragment, let texmathp do its job.
  12659. \\[org-cdlatex-mode-map]"
  12660. (interactive)
  12661. (let (p)
  12662. (cond
  12663. ((not (org-mode-p)) ad-do-it)
  12664. ((eq this-command 'cdlatex-math-symbol)
  12665. (setq ad-return-value t
  12666. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12667. (t
  12668. (let ((p (org-inside-LaTeX-fragment-p)))
  12669. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12670. (setq ad-return-value t
  12671. texmathp-why '("Org-mode embedded math" . 0))
  12672. (if p ad-do-it)))))))))
  12673. (defun turn-on-org-cdlatex ()
  12674. "Unconditionally turn on `org-cdlatex-mode'."
  12675. (org-cdlatex-mode 1))
  12676. (defun org-inside-LaTeX-fragment-p ()
  12677. "Test if point is inside a LaTeX fragment.
  12678. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12679. sequence appearing also before point.
  12680. Even though the matchers for math are configurable, this function assumes
  12681. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12682. delimiters are skipped when they have been removed by customization.
  12683. The return value is nil, or a cons cell with the delimiter and
  12684. and the position of this delimiter.
  12685. This function does a reasonably good job, but can locally be fooled by
  12686. for example currency specifications. For example it will assume being in
  12687. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12688. fragments that are properly closed, but during editing, we have to live
  12689. with the uncertainty caused by missing closing delimiters. This function
  12690. looks only before point, not after."
  12691. (catch 'exit
  12692. (let ((pos (point))
  12693. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12694. (lim (progn
  12695. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12696. (point)))
  12697. dd-on str (start 0) m re)
  12698. (goto-char pos)
  12699. (when dodollar
  12700. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12701. re (nth 1 (assoc "$" org-latex-regexps)))
  12702. (while (string-match re str start)
  12703. (cond
  12704. ((= (match-end 0) (length str))
  12705. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12706. ((= (match-end 0) (- (length str) 5))
  12707. (throw 'exit nil))
  12708. (t (setq start (match-end 0))))))
  12709. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12710. (goto-char pos)
  12711. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12712. (and (match-beginning 2) (throw 'exit nil))
  12713. ;; count $$
  12714. (while (re-search-backward "\\$\\$" lim t)
  12715. (setq dd-on (not dd-on)))
  12716. (goto-char pos)
  12717. (if dd-on (cons "$$" m))))))
  12718. (defun org-try-cdlatex-tab ()
  12719. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12720. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12721. - inside a LaTeX fragment, or
  12722. - after the first word in a line, where an abbreviation expansion could
  12723. insert a LaTeX environment."
  12724. (when org-cdlatex-mode
  12725. (cond
  12726. ((save-excursion
  12727. (skip-chars-backward "a-zA-Z0-9*")
  12728. (skip-chars-backward " \t")
  12729. (bolp))
  12730. (cdlatex-tab) t)
  12731. ((org-inside-LaTeX-fragment-p)
  12732. (cdlatex-tab) t)
  12733. (t nil))))
  12734. (defun org-cdlatex-underscore-caret (&optional arg)
  12735. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12736. Revert to the normal definition outside of these fragments."
  12737. (interactive "P")
  12738. (if (org-inside-LaTeX-fragment-p)
  12739. (call-interactively 'cdlatex-sub-superscript)
  12740. (let (org-cdlatex-mode)
  12741. (call-interactively (key-binding (vector last-input-event))))))
  12742. (defun org-cdlatex-math-modify (&optional arg)
  12743. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12744. Revert to the normal definition outside of these fragments."
  12745. (interactive "P")
  12746. (if (org-inside-LaTeX-fragment-p)
  12747. (call-interactively 'cdlatex-math-modify)
  12748. (let (org-cdlatex-mode)
  12749. (call-interactively (key-binding (vector last-input-event))))))
  12750. (defvar org-latex-fragment-image-overlays nil
  12751. "List of overlays carrying the images of latex fragments.")
  12752. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12753. (defun org-remove-latex-fragment-image-overlays ()
  12754. "Remove all overlays with LaTeX fragment images in current buffer."
  12755. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12756. (setq org-latex-fragment-image-overlays nil))
  12757. (defun org-preview-latex-fragment (&optional subtree)
  12758. "Preview the LaTeX fragment at point, or all locally or globally.
  12759. If the cursor is in a LaTeX fragment, create the image and overlay
  12760. it over the source code. If there is no fragment at point, display
  12761. all fragments in the current text, from one headline to the next. With
  12762. prefix SUBTREE, display all fragments in the current subtree. With a
  12763. double prefix `C-u C-u', or when the cursor is before the first headline,
  12764. display all fragments in the buffer.
  12765. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12766. (interactive "P")
  12767. (org-remove-latex-fragment-image-overlays)
  12768. (save-excursion
  12769. (save-restriction
  12770. (let (beg end at msg)
  12771. (cond
  12772. ((or (equal subtree '(16))
  12773. (not (save-excursion
  12774. (re-search-backward (concat "^" outline-regexp) nil t))))
  12775. (setq beg (point-min) end (point-max)
  12776. msg "Creating images for buffer...%s"))
  12777. ((equal subtree '(4))
  12778. (org-back-to-heading)
  12779. (setq beg (point) end (org-end-of-subtree t)
  12780. msg "Creating images for subtree...%s"))
  12781. (t
  12782. (if (setq at (org-inside-LaTeX-fragment-p))
  12783. (goto-char (max (point-min) (- (cdr at) 2)))
  12784. (org-back-to-heading))
  12785. (setq beg (point) end (progn (outline-next-heading) (point))
  12786. msg (if at "Creating image...%s"
  12787. "Creating images for entry...%s"))))
  12788. (message msg "")
  12789. (narrow-to-region beg end)
  12790. (goto-char beg)
  12791. (org-format-latex
  12792. (concat "ltxpng/" (file-name-sans-extension
  12793. (file-name-nondirectory
  12794. buffer-file-name)))
  12795. default-directory 'overlays msg at 'forbuffer)
  12796. (message msg "done. Use `C-c C-c' to remove images.")))))
  12797. (defvar org-latex-regexps
  12798. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12799. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12800. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12801. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12802. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12803. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12804. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12805. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12806. "Regular expressions for matching embedded LaTeX.")
  12807. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12808. "Replace LaTeX fragments with links to an image, and produce images."
  12809. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12810. (let* ((prefixnodir (file-name-nondirectory prefix))
  12811. (absprefix (expand-file-name prefix dir))
  12812. (todir (file-name-directory absprefix))
  12813. (opt org-format-latex-options)
  12814. (matchers (plist-get opt :matchers))
  12815. (re-list org-latex-regexps)
  12816. (cnt 0) txt link beg end re e checkdir
  12817. executables-checked
  12818. m n block linkfile movefile ov)
  12819. ;; Check if there are old images files with this prefix, and remove them
  12820. (when (file-directory-p todir)
  12821. (mapc 'delete-file
  12822. (directory-files
  12823. todir 'full
  12824. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12825. ;; Check the different regular expressions
  12826. (while (setq e (pop re-list))
  12827. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12828. block (if (nth 3 e) "\n\n" ""))
  12829. (when (member m matchers)
  12830. (goto-char (point-min))
  12831. (while (re-search-forward re nil t)
  12832. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12833. (not (get-text-property (match-beginning n)
  12834. 'org-protected)))
  12835. (setq txt (match-string n)
  12836. beg (match-beginning n) end (match-end n)
  12837. cnt (1+ cnt)
  12838. linkfile (format "%s_%04d.png" prefix cnt)
  12839. movefile (format "%s_%04d.png" absprefix cnt)
  12840. link (concat block "[[file:" linkfile "]]" block))
  12841. (if msg (message msg cnt))
  12842. (goto-char beg)
  12843. (unless checkdir ; make sure the directory exists
  12844. (setq checkdir t)
  12845. (or (file-directory-p todir) (make-directory todir)))
  12846. (unless executables-checked
  12847. (org-check-external-command
  12848. "latex" "needed to convert LaTeX fragments to images")
  12849. (org-check-external-command
  12850. "dvipng" "needed to convert LaTeX fragments to images")
  12851. (setq executables-checked t))
  12852. (org-create-formula-image
  12853. txt movefile opt forbuffer)
  12854. (if overlays
  12855. (progn
  12856. (setq ov (org-make-overlay beg end))
  12857. (if (featurep 'xemacs)
  12858. (progn
  12859. (org-overlay-put ov 'invisible t)
  12860. (org-overlay-put
  12861. ov 'end-glyph
  12862. (make-glyph (vector 'png :file movefile))))
  12863. (org-overlay-put
  12864. ov 'display
  12865. (list 'image :type 'png :file movefile :ascent 'center)))
  12866. (push ov org-latex-fragment-image-overlays)
  12867. (goto-char end))
  12868. (delete-region beg end)
  12869. (insert link))))))))
  12870. ;; This function borrows from Ganesh Swami's latex2png.el
  12871. (defun org-create-formula-image (string tofile options buffer)
  12872. (let* ((tmpdir (if (featurep 'xemacs)
  12873. (temp-directory)
  12874. temporary-file-directory))
  12875. (texfilebase (make-temp-name
  12876. (expand-file-name "orgtex" tmpdir)))
  12877. (texfile (concat texfilebase ".tex"))
  12878. (dvifile (concat texfilebase ".dvi"))
  12879. (pngfile (concat texfilebase ".png"))
  12880. (fnh (if (featurep 'xemacs)
  12881. (font-height (get-face-font 'default))
  12882. (face-attribute 'default :height nil)))
  12883. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12884. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12885. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12886. "Black"))
  12887. (bg (or (plist-get options (if buffer :background :html-background))
  12888. "Transparent")))
  12889. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12890. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12891. (with-temp-file texfile
  12892. (insert org-format-latex-header
  12893. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12894. (let ((dir default-directory))
  12895. (condition-case nil
  12896. (progn
  12897. (cd tmpdir)
  12898. (call-process "latex" nil nil nil texfile))
  12899. (error nil))
  12900. (cd dir))
  12901. (if (not (file-exists-p dvifile))
  12902. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12903. (condition-case nil
  12904. (call-process "dvipng" nil nil nil
  12905. "-E" "-fg" fg "-bg" bg
  12906. "-D" dpi
  12907. ;;"-x" scale "-y" scale
  12908. "-T" "tight"
  12909. "-o" pngfile
  12910. dvifile)
  12911. (error nil))
  12912. (if (not (file-exists-p pngfile))
  12913. (progn (message "Failed to create png file from %s" texfile) nil)
  12914. ;; Use the requested file name and clean up
  12915. (copy-file pngfile tofile 'replace)
  12916. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12917. (delete-file (concat texfilebase e)))
  12918. pngfile))))
  12919. (defun org-dvipng-color (attr)
  12920. "Return an rgb color specification for dvipng."
  12921. (apply 'format "rgb %s %s %s"
  12922. (mapcar 'org-normalize-color
  12923. (color-values (face-attribute 'default attr nil)))))
  12924. (defun org-normalize-color (value)
  12925. "Return string to be used as color value for an RGB component."
  12926. (format "%g" (/ value 65535.0)))
  12927. ;;;; Key bindings
  12928. ;; Make `C-c C-x' a prefix key
  12929. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12930. ;; TAB key with modifiers
  12931. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12932. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12933. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12934. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12935. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12936. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12937. ;; The following line is necessary under Suse GNU/Linux
  12938. (unless (featurep 'xemacs)
  12939. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12940. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12941. (define-key org-mode-map [backtab] 'org-shifttab)
  12942. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12943. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12944. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12945. ;; Cursor keys with modifiers
  12946. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12947. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12948. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12949. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12950. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12951. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12952. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12953. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12954. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12955. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12956. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12957. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12958. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12959. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12960. ;;; Extra keys for tty access.
  12961. ;; We only set them when really needed because otherwise the
  12962. ;; menus don't show the simple keys
  12963. (when (or org-use-extra-keys
  12964. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12965. (not window-system))
  12966. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12967. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12968. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12969. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12970. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12971. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12972. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12973. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12974. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12975. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12976. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12977. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12978. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12979. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12980. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12981. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12982. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12983. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12984. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12985. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12986. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12987. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12988. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12989. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12990. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12991. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12992. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12993. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12994. ;; All the other keys
  12995. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12996. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12997. (if (boundp 'narrow-map)
  12998. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12999. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13000. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13001. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13002. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13003. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13004. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13005. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13006. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13007. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13008. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13009. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13010. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13011. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13012. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13013. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13014. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13015. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13016. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13017. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13018. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13019. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13020. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13021. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13022. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13023. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13024. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13025. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13026. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13027. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13028. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13029. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13030. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13031. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13032. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13033. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13034. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13035. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13036. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13037. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13038. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13039. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13040. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13041. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13042. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13043. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13044. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13045. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13046. (org-defkey org-mode-map "\C-m" 'org-return)
  13047. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13048. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13049. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13050. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13051. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13052. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13053. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13054. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13055. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13056. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13057. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13058. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13059. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13060. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13061. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13062. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13063. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13064. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13065. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13066. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13067. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13068. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13069. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13070. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13071. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13072. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13073. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13074. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13075. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13076. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13077. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13078. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13079. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13080. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13081. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13082. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13083. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13084. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13085. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13086. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13087. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13088. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13089. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13090. (when (featurep 'xemacs)
  13091. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13092. (defvar org-self-insert-command-undo-counter 0)
  13093. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13094. (defun org-self-insert-command (N)
  13095. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13096. If the cursor is in a table looking at whitespace, the whitespace is
  13097. overwritten, and the table is not marked as requiring realignment."
  13098. (interactive "p")
  13099. (if (and
  13100. (org-table-p)
  13101. (progn
  13102. ;; check if we blank the field, and if that triggers align
  13103. (and (featurep 'org-table) org-table-auto-blank-field
  13104. (member last-command
  13105. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  13106. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13107. ;; got extra space, this field does not determine column width
  13108. (let (org-table-may-need-update) (org-table-blank-field))
  13109. ;; no extra space, this field may determine column width
  13110. (org-table-blank-field)))
  13111. t)
  13112. (eq N 1)
  13113. (looking-at "[^|\n]* |"))
  13114. (let (org-table-may-need-update)
  13115. (goto-char (1- (match-end 0)))
  13116. (delete-backward-char 1)
  13117. (goto-char (match-beginning 0))
  13118. (self-insert-command N))
  13119. (setq org-table-may-need-update t)
  13120. (self-insert-command N)
  13121. (org-fix-tags-on-the-fly)
  13122. (if org-self-insert-cluster-for-undo
  13123. (if (not (eq last-command 'org-self-insert-command))
  13124. (setq org-self-insert-command-undo-counter 1)
  13125. (if (>= org-self-insert-command-undo-counter 20)
  13126. (setq org-self-insert-command-undo-counter 1)
  13127. (and (> org-self-insert-command-undo-counter 0)
  13128. buffer-undo-list
  13129. (not (cadr buffer-undo-list)) ; remove nil entry
  13130. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13131. (setq org-self-insert-command-undo-counter
  13132. (1+ org-self-insert-command-undo-counter)))))))
  13133. (defun org-fix-tags-on-the-fly ()
  13134. (when (and (equal (char-after (point-at-bol)) ?*)
  13135. (org-on-heading-p))
  13136. (org-align-tags-here org-tags-column)))
  13137. (defun org-delete-backward-char (N)
  13138. "Like `delete-backward-char', insert whitespace at field end in tables.
  13139. When deleting backwards, in tables this function will insert whitespace in
  13140. front of the next \"|\" separator, to keep the table aligned. The table will
  13141. still be marked for re-alignment if the field did fill the entire column,
  13142. because, in this case the deletion might narrow the column."
  13143. (interactive "p")
  13144. (if (and (org-table-p)
  13145. (eq N 1)
  13146. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13147. (looking-at ".*?|"))
  13148. (let ((pos (point))
  13149. (noalign (looking-at "[^|\n\r]* |"))
  13150. (c org-table-may-need-update))
  13151. (backward-delete-char N)
  13152. (skip-chars-forward "^|")
  13153. (insert " ")
  13154. (goto-char (1- pos))
  13155. ;; noalign: if there were two spaces at the end, this field
  13156. ;; does not determine the width of the column.
  13157. (if noalign (setq org-table-may-need-update c)))
  13158. (backward-delete-char N)
  13159. (org-fix-tags-on-the-fly)))
  13160. (defun org-delete-char (N)
  13161. "Like `delete-char', but insert whitespace at field end in tables.
  13162. When deleting characters, in tables this function will insert whitespace in
  13163. front of the next \"|\" separator, to keep the table aligned. The table will
  13164. still be marked for re-alignment if the field did fill the entire column,
  13165. because, in this case the deletion might narrow the column."
  13166. (interactive "p")
  13167. (if (and (org-table-p)
  13168. (not (bolp))
  13169. (not (= (char-after) ?|))
  13170. (eq N 1))
  13171. (if (looking-at ".*?|")
  13172. (let ((pos (point))
  13173. (noalign (looking-at "[^|\n\r]* |"))
  13174. (c org-table-may-need-update))
  13175. (replace-match (concat
  13176. (substring (match-string 0) 1 -1)
  13177. " |"))
  13178. (goto-char pos)
  13179. ;; noalign: if there were two spaces at the end, this field
  13180. ;; does not determine the width of the column.
  13181. (if noalign (setq org-table-may-need-update c)))
  13182. (delete-char N))
  13183. (delete-char N)
  13184. (org-fix-tags-on-the-fly)))
  13185. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13186. (put 'org-self-insert-command 'delete-selection t)
  13187. (put 'orgtbl-self-insert-command 'delete-selection t)
  13188. (put 'org-delete-char 'delete-selection 'supersede)
  13189. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13190. (put 'org-yank 'delete-selection 'yank)
  13191. ;; Make `flyspell-mode' delay after some commands
  13192. (put 'org-self-insert-command 'flyspell-delayed t)
  13193. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13194. (put 'org-delete-char 'flyspell-delayed t)
  13195. (put 'org-delete-backward-char 'flyspell-delayed t)
  13196. ;; Make pabbrev-mode expand after org-mode commands
  13197. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13198. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13199. ;; How to do this: Measure non-white length of current string
  13200. ;; If equal to column width, we should realign.
  13201. (defun org-remap (map &rest commands)
  13202. "In MAP, remap the functions given in COMMANDS.
  13203. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13204. (let (new old)
  13205. (while commands
  13206. (setq old (pop commands) new (pop commands))
  13207. (if (fboundp 'command-remapping)
  13208. (org-defkey map (vector 'remap old) new)
  13209. (substitute-key-definition old new map global-map)))))
  13210. (when (eq org-enable-table-editor 'optimized)
  13211. ;; If the user wants maximum table support, we need to hijack
  13212. ;; some standard editing functions
  13213. (org-remap org-mode-map
  13214. 'self-insert-command 'org-self-insert-command
  13215. 'delete-char 'org-delete-char
  13216. 'delete-backward-char 'org-delete-backward-char)
  13217. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13218. (defvar org-ctrl-c-ctrl-c-hook nil
  13219. "Hook for functions attaching themselves to `C-c C-c'.
  13220. This can be used to add additional functionality to the C-c C-c key which
  13221. executes context-dependent commands.
  13222. Each function will be called with no arguments. The function must check
  13223. if the context is appropriate for it to act. If yes, it should do its
  13224. thing and then return a non-nil value. If the context is wrong,
  13225. just do nothing and return nil.")
  13226. (defvar org-tab-first-hook nil
  13227. "Hook for functions to attach themselves to TAB.
  13228. See `org-ctrl-c-ctrl-c-hook' for more information.
  13229. This hook runs as the first action when TAB is pressed, even before
  13230. `org-cycle' messes around with the `outline-regexp' to cater for
  13231. inline tasks and plain list item folding.
  13232. If any function in this hook returns t, not other actions like table
  13233. field motion visibility cycling will be done.")
  13234. (defvar org-tab-after-check-for-table-hook nil
  13235. "Hook for functions to attach themselves to TAB.
  13236. See `org-ctrl-c-ctrl-c-hook' for more information.
  13237. This hook runs after it has been established that the cursor is not in a
  13238. table, but before checking if the cursor is in a headline or if global cycling
  13239. should be done.
  13240. If any function in this hook returns t, not other actions like visibility
  13241. cycling will be done.")
  13242. (defvar org-tab-after-check-for-cycling-hook nil
  13243. "Hook for functions to attach themselves to TAB.
  13244. See `org-ctrl-c-ctrl-c-hook' for more information.
  13245. This hook runs after it has been established that not table field motion and
  13246. not visibility should be done because of current context. This is probably
  13247. the place where a package like yasnippets can hook in.")
  13248. (defvar org-metaleft-hook nil
  13249. "Hook for functions attaching themselves to `M-left'.
  13250. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13251. (defvar org-metaright-hook nil
  13252. "Hook for functions attaching themselves to `M-right'.
  13253. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13254. (defvar org-metaup-hook nil
  13255. "Hook for functions attaching themselves to `M-up'.
  13256. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13257. (defvar org-metadown-hook nil
  13258. "Hook for functions attaching themselves to `M-down'.
  13259. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13260. (defvar org-shiftmetaleft-hook nil
  13261. "Hook for functions attaching themselves to `M-S-left'.
  13262. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13263. (defvar org-shiftmetaright-hook nil
  13264. "Hook for functions attaching themselves to `M-S-right'.
  13265. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13266. (defvar org-shiftmetaup-hook nil
  13267. "Hook for functions attaching themselves to `M-S-up'.
  13268. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13269. (defvar org-shiftmetadown-hook nil
  13270. "Hook for functions attaching themselves to `M-S-down'.
  13271. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13272. (defvar org-metareturn-hook nil
  13273. "Hook for functions attaching themselves to `M-RET'.
  13274. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13275. (defun org-modifier-cursor-error ()
  13276. "Throw an error, a modified cursor command was applied in wrong context."
  13277. (error "This command is active in special context like tables, headlines or items"))
  13278. (defun org-shiftselect-error ()
  13279. "Throw an error because Shift-Cursor command was applied in wrong context."
  13280. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13281. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13282. (error "This command works only in special context like headlines or timestamps.")))
  13283. (defun org-call-for-shift-select (cmd)
  13284. (let ((this-command-keys-shift-translated t))
  13285. (call-interactively cmd)))
  13286. (defun org-shifttab (&optional arg)
  13287. "Global visibility cycling or move to previous table field.
  13288. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13289. on context.
  13290. See the individual commands for more information."
  13291. (interactive "P")
  13292. (cond
  13293. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13294. ((integerp arg)
  13295. (message "Content view to level: %d" arg)
  13296. (org-content (prefix-numeric-value arg))
  13297. (setq org-cycle-global-status 'overview))
  13298. (t (call-interactively 'org-global-cycle))))
  13299. (defun org-shiftmetaleft ()
  13300. "Promote subtree or delete table column.
  13301. Calls `org-promote-subtree', `org-outdent-item',
  13302. or `org-table-delete-column', depending on context.
  13303. See the individual commands for more information."
  13304. (interactive)
  13305. (cond
  13306. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13307. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13308. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13309. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13310. (t (org-modifier-cursor-error))))
  13311. (defun org-shiftmetaright ()
  13312. "Demote subtree or insert table column.
  13313. Calls `org-demote-subtree', `org-indent-item',
  13314. or `org-table-insert-column', depending on context.
  13315. See the individual commands for more information."
  13316. (interactive)
  13317. (cond
  13318. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13319. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13320. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13321. ((org-at-item-p) (call-interactively 'org-indent-item))
  13322. (t (org-modifier-cursor-error))))
  13323. (defun org-shiftmetaup (&optional arg)
  13324. "Move subtree up or kill table row.
  13325. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13326. `org-move-item-up' depending on context. See the individual commands
  13327. for more information."
  13328. (interactive "P")
  13329. (cond
  13330. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13331. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13332. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13333. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13334. (t (org-modifier-cursor-error))))
  13335. (defun org-shiftmetadown (&optional arg)
  13336. "Move subtree down or insert table row.
  13337. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13338. `org-move-item-down', depending on context. See the individual
  13339. commands for more information."
  13340. (interactive "P")
  13341. (cond
  13342. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13343. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13344. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13345. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13346. (t (org-modifier-cursor-error))))
  13347. (defun org-metaleft (&optional arg)
  13348. "Promote heading or move table column to left.
  13349. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13350. With no specific context, calls the Emacs default `backward-word'.
  13351. See the individual commands for more information."
  13352. (interactive "P")
  13353. (cond
  13354. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13355. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13356. ((or (org-on-heading-p)
  13357. (and (org-region-active-p)
  13358. (save-excursion
  13359. (goto-char (region-beginning))
  13360. (org-on-heading-p))))
  13361. (call-interactively 'org-do-promote))
  13362. ((or (org-at-item-p)
  13363. (and (org-region-active-p)
  13364. (save-excursion
  13365. (goto-char (region-beginning))
  13366. (org-at-item-p))))
  13367. (call-interactively 'org-outdent-item))
  13368. (t (call-interactively 'backward-word))))
  13369. (defun org-metaright (&optional arg)
  13370. "Demote subtree or move table column to right.
  13371. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13372. With no specific context, calls the Emacs default `forward-word'.
  13373. See the individual commands for more information."
  13374. (interactive "P")
  13375. (cond
  13376. ((run-hook-with-args-until-success 'org-metaright-hook))
  13377. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13378. ((or (org-on-heading-p)
  13379. (and (org-region-active-p)
  13380. (save-excursion
  13381. (goto-char (region-beginning))
  13382. (org-on-heading-p))))
  13383. (call-interactively 'org-do-demote))
  13384. ((or (org-at-item-p)
  13385. (and (org-region-active-p)
  13386. (save-excursion
  13387. (goto-char (region-beginning))
  13388. (org-at-item-p))))
  13389. (call-interactively 'org-indent-item))
  13390. (t (call-interactively 'forward-word))))
  13391. (defun org-metaup (&optional arg)
  13392. "Move subtree up or move table row up.
  13393. Calls `org-move-subtree-up' or `org-table-move-row' or
  13394. `org-move-item-up', depending on context. See the individual commands
  13395. for more information."
  13396. (interactive "P")
  13397. (cond
  13398. ((run-hook-with-args-until-success 'org-metaup-hook))
  13399. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13400. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13401. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13402. (t (transpose-lines 1) (beginning-of-line -1))))
  13403. (defun org-metadown (&optional arg)
  13404. "Move subtree down or move table row down.
  13405. Calls `org-move-subtree-down' or `org-table-move-row' or
  13406. `org-move-item-down', depending on context. See the individual
  13407. commands for more information."
  13408. (interactive "P")
  13409. (cond
  13410. ((run-hook-with-args-until-success 'org-metadown-hook))
  13411. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13412. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13413. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13414. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13415. (defun org-shiftup (&optional arg)
  13416. "Increase item in timestamp or increase priority of current headline.
  13417. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13418. depending on context. See the individual commands for more information."
  13419. (interactive "P")
  13420. (cond
  13421. ((and org-support-shift-select (org-region-active-p))
  13422. (org-call-for-shift-select 'previous-line))
  13423. ((org-at-timestamp-p t)
  13424. (call-interactively (if org-edit-timestamp-down-means-later
  13425. 'org-timestamp-down 'org-timestamp-up)))
  13426. ((and (not (eq org-support-shift-select 'always))
  13427. org-enable-priority-commands
  13428. (org-on-heading-p))
  13429. (call-interactively 'org-priority-up))
  13430. ((and (not org-support-shift-select) (org-at-item-p))
  13431. (call-interactively 'org-previous-item))
  13432. ((org-clocktable-try-shift 'up arg))
  13433. (org-support-shift-select
  13434. (org-call-for-shift-select 'previous-line))
  13435. (t (org-shiftselect-error))))
  13436. (defun org-shiftdown (&optional arg)
  13437. "Decrease item in timestamp or decrease priority of current headline.
  13438. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13439. depending on context. See the individual commands for more information."
  13440. (interactive "P")
  13441. (cond
  13442. ((and org-support-shift-select (org-region-active-p))
  13443. (org-call-for-shift-select 'next-line))
  13444. ((org-at-timestamp-p t)
  13445. (call-interactively (if org-edit-timestamp-down-means-later
  13446. 'org-timestamp-up 'org-timestamp-down)))
  13447. ((and (not (eq org-support-shift-select 'always))
  13448. org-enable-priority-commands
  13449. (org-on-heading-p))
  13450. (call-interactively 'org-priority-down))
  13451. ((and (not org-support-shift-select) (org-at-item-p))
  13452. (call-interactively 'org-next-item))
  13453. ((org-clocktable-try-shift 'down arg))
  13454. (org-support-shift-select
  13455. (org-call-for-shift-select 'next-line))
  13456. (t (org-shiftselect-error))))
  13457. (defun org-shiftright (&optional arg)
  13458. "Cycle the thing at point or in the current line, depending on context.
  13459. Depending on context, this does one of the following:
  13460. - switch a timestamp at point one day into the future
  13461. - on a headline, switch to the next TODO keyword.
  13462. - on an item, switch entire list to the next bullet type
  13463. - on a property line, switch to the next allowed value
  13464. - on a clocktable definition line, move time block into the future"
  13465. (interactive "P")
  13466. (cond
  13467. ((and org-support-shift-select (org-region-active-p))
  13468. (org-call-for-shift-select 'forward-char))
  13469. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13470. ((and (not (eq org-support-shift-select 'always))
  13471. (org-on-heading-p))
  13472. (let ((org-inhibit-logging
  13473. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13474. (org-call-with-arg 'org-todo 'right)))
  13475. ((or (and org-support-shift-select
  13476. (not (eq org-support-shift-select 'always))
  13477. (org-at-item-bullet-p))
  13478. (and (not org-support-shift-select) (org-at-item-p)))
  13479. (org-call-with-arg 'org-cycle-list-bullet nil))
  13480. ((and (not (eq org-support-shift-select 'always))
  13481. (org-at-property-p))
  13482. (call-interactively 'org-property-next-allowed-value))
  13483. ((org-clocktable-try-shift 'right arg))
  13484. (org-support-shift-select
  13485. (org-call-for-shift-select 'forward-char))
  13486. (t (org-shiftselect-error))))
  13487. (defun org-shiftleft (&optional arg)
  13488. "Cycle the thing at point or in the current line, depending on context.
  13489. Depending on context, this does one of the following:
  13490. - switch a timestamp at point one day into the past
  13491. - on a headline, switch to the previous TODO keyword.
  13492. - on an item, switch entire list to the previous bullet type
  13493. - on a property line, switch to the previous allowed value
  13494. - on a clocktable definition line, move time block into the past"
  13495. (interactive "P")
  13496. (cond
  13497. ((and org-support-shift-select (org-region-active-p))
  13498. (org-call-for-shift-select 'backward-char))
  13499. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13500. ((and (not (eq org-support-shift-select 'always))
  13501. (org-on-heading-p))
  13502. (let ((org-inhibit-logging
  13503. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13504. (org-call-with-arg 'org-todo 'left)))
  13505. ((or (and org-support-shift-select
  13506. (not (eq org-support-shift-select 'always))
  13507. (org-at-item-bullet-p))
  13508. (and (not org-support-shift-select) (org-at-item-p)))
  13509. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13510. ((and (not (eq org-support-shift-select 'always))
  13511. (org-at-property-p))
  13512. (call-interactively 'org-property-previous-allowed-value))
  13513. ((org-clocktable-try-shift 'left arg))
  13514. (org-support-shift-select
  13515. (org-call-for-shift-select 'backward-char))
  13516. (t (org-shiftselect-error))))
  13517. (defun org-shiftcontrolright ()
  13518. "Switch to next TODO set."
  13519. (interactive)
  13520. (cond
  13521. ((and org-support-shift-select (org-region-active-p))
  13522. (org-call-for-shift-select 'forward-word))
  13523. ((and (not (eq org-support-shift-select 'always))
  13524. (org-on-heading-p))
  13525. (org-call-with-arg 'org-todo 'nextset))
  13526. (org-support-shift-select
  13527. (org-call-for-shift-select 'forward-word))
  13528. (t (org-shiftselect-error))))
  13529. (defun org-shiftcontrolleft ()
  13530. "Switch to previous TODO set."
  13531. (interactive)
  13532. (cond
  13533. ((and org-support-shift-select (org-region-active-p))
  13534. (org-call-for-shift-select 'backward-word))
  13535. ((and (not (eq org-support-shift-select 'always))
  13536. (org-on-heading-p))
  13537. (org-call-with-arg 'org-todo 'previousset))
  13538. (org-support-shift-select
  13539. (org-call-for-shift-select 'backward-word))
  13540. (t (org-shiftselect-error))))
  13541. (defun org-ctrl-c-ret ()
  13542. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13543. (interactive)
  13544. (cond
  13545. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13546. (t (call-interactively 'org-insert-heading))))
  13547. (defun org-copy-special ()
  13548. "Copy region in table or copy current subtree.
  13549. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13550. See the individual commands for more information."
  13551. (interactive)
  13552. (call-interactively
  13553. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13554. (defun org-cut-special ()
  13555. "Cut region in table or cut current subtree.
  13556. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13557. See the individual commands for more information."
  13558. (interactive)
  13559. (call-interactively
  13560. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13561. (defun org-paste-special (arg)
  13562. "Paste rectangular region into table, or past subtree relative to level.
  13563. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13564. See the individual commands for more information."
  13565. (interactive "P")
  13566. (if (org-at-table-p)
  13567. (org-table-paste-rectangle)
  13568. (org-paste-subtree arg)))
  13569. (defun org-edit-special ()
  13570. "Call a special editor for the stuff at point.
  13571. When at a table, call the formula editor with `org-table-edit-formulas'.
  13572. When at the first line of an src example, call `org-edit-src-code'.
  13573. When in an #+include line, visit the include file. Otherwise call
  13574. `ffap' to visit the file at point."
  13575. (interactive)
  13576. (cond
  13577. ((org-at-table-p)
  13578. (call-interactively 'org-table-edit-formulas))
  13579. ((save-excursion
  13580. (beginning-of-line 1)
  13581. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13582. (find-file (org-trim (match-string 1))))
  13583. ((org-edit-src-code))
  13584. ((org-edit-fixed-width-region))
  13585. (t (call-interactively 'ffap))))
  13586. (defun org-ctrl-c-ctrl-c (&optional arg)
  13587. "Set tags in headline, or update according to changed information at point.
  13588. This command does many different things, depending on context:
  13589. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13590. this is what we do.
  13591. - If the cursor is in a headline, prompt for tags and insert them
  13592. into the current line, aligned to `org-tags-column'. When called
  13593. with prefix arg, realign all tags in the current buffer.
  13594. - If the cursor is in one of the special #+KEYWORD lines, this
  13595. triggers scanning the buffer for these lines and updating the
  13596. information.
  13597. - If the cursor is inside a table, realign the table. This command
  13598. works even if the automatic table editor has been turned off.
  13599. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13600. the entire table.
  13601. - If the cursor is at a footnote reference or definition, jump to
  13602. the corresponding definition or references, respectively.
  13603. - If the cursor is a the beginning of a dynamic block, update it.
  13604. - If the cursor is inside a table created by the table.el package,
  13605. activate that table.
  13606. - If the current buffer is a remember buffer, close note and file
  13607. it. A prefix argument of 1 files to the default location
  13608. without further interaction. A prefix argument of 2 files to
  13609. the currently clocking task.
  13610. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13611. links in this buffer.
  13612. - If the cursor is on a numbered item in a plain list, renumber the
  13613. ordered list.
  13614. - If the cursor is on a checkbox, toggle it."
  13615. (interactive "P")
  13616. (let ((org-enable-table-editor t))
  13617. (cond
  13618. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13619. org-occur-highlights
  13620. org-latex-fragment-image-overlays)
  13621. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13622. (org-remove-occur-highlights)
  13623. (org-remove-latex-fragment-image-overlays)
  13624. (message "Temporary highlights/overlays removed from current buffer"))
  13625. ((and (local-variable-p 'org-finish-function (current-buffer))
  13626. (fboundp org-finish-function))
  13627. (funcall org-finish-function))
  13628. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13629. ((org-at-property-p)
  13630. (call-interactively 'org-property-action))
  13631. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13632. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13633. ((org-at-table.el-p)
  13634. (require 'table)
  13635. (beginning-of-line 1)
  13636. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13637. (call-interactively 'table-recognize-table))
  13638. ((org-at-table-p)
  13639. (org-table-maybe-eval-formula)
  13640. (if arg
  13641. (call-interactively 'org-table-recalculate)
  13642. (org-table-maybe-recalculate-line))
  13643. (call-interactively 'org-table-align))
  13644. ((or (org-footnote-at-reference-p)
  13645. (org-footnote-at-definition-p))
  13646. (call-interactively 'org-footnote-action))
  13647. ((org-at-item-checkbox-p)
  13648. (call-interactively 'org-toggle-checkbox))
  13649. ((org-at-item-p)
  13650. (if arg
  13651. (call-interactively 'org-toggle-checkbox)
  13652. (call-interactively 'org-maybe-renumber-ordered-list)))
  13653. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13654. ;; Dynamic block
  13655. (beginning-of-line 1)
  13656. (save-excursion (org-update-dblock)))
  13657. ((save-excursion
  13658. (beginning-of-line 1)
  13659. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13660. (cond
  13661. ((equal (match-string 1) "TBLFM")
  13662. ;; Recalculate the table before this line
  13663. (save-excursion
  13664. (beginning-of-line 1)
  13665. (skip-chars-backward " \r\n\t")
  13666. (if (org-at-table-p)
  13667. (org-call-with-arg 'org-table-recalculate t))))
  13668. (t
  13669. ; (org-set-regexps-and-options)
  13670. ; (org-restart-font-lock)
  13671. (let ((org-inhibit-startup t)) (org-mode-restart))
  13672. (message "Local setup has been refreshed"))))
  13673. (t (error "C-c C-c can do nothing useful at this location.")))))
  13674. (defun org-mode-restart ()
  13675. "Restart Org-mode, to scan again for special lines.
  13676. Also updates the keyword regular expressions."
  13677. (interactive)
  13678. (org-mode)
  13679. (message "Org-mode restarted"))
  13680. (defun org-kill-note-or-show-branches ()
  13681. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13682. (interactive)
  13683. (if (not org-finish-function)
  13684. (call-interactively 'show-branches)
  13685. (let ((org-note-abort t))
  13686. (funcall org-finish-function))))
  13687. (defun org-return (&optional indent)
  13688. "Goto next table row or insert a newline.
  13689. Calls `org-table-next-row' or `newline', depending on context.
  13690. See the individual commands for more information."
  13691. (interactive)
  13692. (cond
  13693. ((bobp) (if indent (newline-and-indent) (newline)))
  13694. ((org-at-table-p)
  13695. (org-table-justify-field-maybe)
  13696. (call-interactively 'org-table-next-row))
  13697. ((and org-return-follows-link
  13698. (eq (get-text-property (point) 'face) 'org-link))
  13699. (call-interactively 'org-open-at-point))
  13700. ((and (org-at-heading-p)
  13701. (looking-at
  13702. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13703. (org-show-entry)
  13704. (end-of-line 1)
  13705. (newline))
  13706. (t (if indent (newline-and-indent) (newline)))))
  13707. (defun org-return-indent ()
  13708. "Goto next table row or insert a newline and indent.
  13709. Calls `org-table-next-row' or `newline-and-indent', depending on
  13710. context. See the individual commands for more information."
  13711. (interactive)
  13712. (org-return t))
  13713. (defun org-ctrl-c-star ()
  13714. "Compute table, or change heading status of lines.
  13715. Calls `org-table-recalculate' or `org-toggle-heading',
  13716. depending on context."
  13717. (interactive)
  13718. (cond
  13719. ((org-at-table-p)
  13720. (call-interactively 'org-table-recalculate))
  13721. (t
  13722. ;; Convert all lines in region to list items
  13723. (call-interactively 'org-toggle-heading))))
  13724. (defun org-ctrl-c-minus ()
  13725. "Insert separator line in table or modify bullet status of line.
  13726. Also turns a plain line or a region of lines into list items.
  13727. Calls `org-table-insert-hline', `org-toggle-item', or
  13728. `org-cycle-list-bullet', depending on context."
  13729. (interactive)
  13730. (cond
  13731. ((org-at-table-p)
  13732. (call-interactively 'org-table-insert-hline))
  13733. ((org-region-active-p)
  13734. (call-interactively 'org-toggle-item))
  13735. ((org-in-item-p)
  13736. (call-interactively 'org-cycle-list-bullet))
  13737. (t
  13738. (call-interactively 'org-toggle-item))))
  13739. (defun org-toggle-item ()
  13740. "Convert headings or normal lines to items, items to normal lines.
  13741. If there is no active region, only the current line is considered.
  13742. If the first line in the region is a headline, convert all headlines to items.
  13743. If the first line in the region is an item, convert all items to normal lines.
  13744. If the first line is normal text, add an item bullet to each line."
  13745. (interactive)
  13746. (let (l2 l beg end)
  13747. (if (org-region-active-p)
  13748. (setq beg (region-beginning) end (region-end))
  13749. (setq beg (point-at-bol)
  13750. end (min (1+ (point-at-eol)) (point-max))))
  13751. (save-excursion
  13752. (goto-char end)
  13753. (setq l2 (org-current-line))
  13754. (goto-char beg)
  13755. (beginning-of-line 1)
  13756. (setq l (1- (org-current-line)))
  13757. (if (org-at-item-p)
  13758. ;; We already have items, de-itemize
  13759. (while (< (setq l (1+ l)) l2)
  13760. (when (org-at-item-p)
  13761. (goto-char (match-beginning 2))
  13762. (delete-region (match-beginning 2) (match-end 2))
  13763. (and (looking-at "[ \t]+") (replace-match "")))
  13764. (beginning-of-line 2))
  13765. (if (org-on-heading-p)
  13766. ;; Headings, convert to items
  13767. (while (< (setq l (1+ l)) l2)
  13768. (if (looking-at org-outline-regexp)
  13769. (replace-match "- " t t))
  13770. (beginning-of-line 2))
  13771. ;; normal lines, turn them into items
  13772. (while (< (setq l (1+ l)) l2)
  13773. (unless (org-at-item-p)
  13774. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13775. (replace-match "\\1- \\2")))
  13776. (beginning-of-line 2)))))))
  13777. (defun org-toggle-heading (&optional nstars)
  13778. "Convert headings to normal text, or items or text to headings.
  13779. If there is no active region, only the current line is considered.
  13780. If the first line is a heading, remove the stars from all headlines
  13781. in the region.
  13782. If the first line is a plain list item, turn all plain list items into
  13783. headings.
  13784. If the first line is a normal line, turn each and every line in the region
  13785. into a heading.
  13786. When converting a line into a heading, the number of stars is chosen
  13787. such that the lines become children of the current entry. However, when
  13788. a prefix argument is given, its value determines the number of stars to add."
  13789. (interactive "P")
  13790. (let (l2 l itemp beg end)
  13791. (if (org-region-active-p)
  13792. (setq beg (region-beginning) end (region-end))
  13793. (setq beg (point-at-bol)
  13794. end (min (1+ (point-at-eol)) (point-max))))
  13795. (save-excursion
  13796. (goto-char end)
  13797. (setq l2 (org-current-line))
  13798. (goto-char beg)
  13799. (beginning-of-line 1)
  13800. (setq l (1- (org-current-line)))
  13801. (if (org-on-heading-p)
  13802. ;; We already have headlines, de-star them
  13803. (while (< (setq l (1+ l)) l2)
  13804. (when (org-on-heading-p t)
  13805. (and (looking-at outline-regexp) (replace-match "")))
  13806. (beginning-of-line 2))
  13807. (setq itemp (org-at-item-p))
  13808. (let* ((stars
  13809. (if nstars
  13810. (make-string (prefix-numeric-value current-prefix-arg)
  13811. ?*)
  13812. (save-excursion
  13813. (re-search-backward org-complex-heading-regexp nil t)
  13814. (or (match-string 1) ""))))
  13815. (add-stars (cond (nstars "")
  13816. ((equal stars "") "*")
  13817. (org-odd-levels-only "**")
  13818. (t "*")))
  13819. (rpl (concat stars add-stars " ")))
  13820. (while (< (setq l (1+ l)) l2)
  13821. (if itemp
  13822. (and (org-at-item-p) (replace-match rpl t t))
  13823. (unless (org-on-heading-p)
  13824. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13825. (replace-match (concat rpl (match-string 2))))))
  13826. (beginning-of-line 2)))))))
  13827. (defun org-meta-return (&optional arg)
  13828. "Insert a new heading or wrap a region in a table.
  13829. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13830. See the individual commands for more information."
  13831. (interactive "P")
  13832. (cond
  13833. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13834. ((org-at-table-p)
  13835. (call-interactively 'org-table-wrap-region))
  13836. (t (call-interactively 'org-insert-heading))))
  13837. ;;; Menu entries
  13838. ;; Define the Org-mode menus
  13839. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13840. '("Tbl"
  13841. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13842. ["Next Field" org-cycle (org-at-table-p)]
  13843. ["Previous Field" org-shifttab (org-at-table-p)]
  13844. ["Next Row" org-return (org-at-table-p)]
  13845. "--"
  13846. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13847. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13848. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13849. "--"
  13850. ("Column"
  13851. ["Move Column Left" org-metaleft (org-at-table-p)]
  13852. ["Move Column Right" org-metaright (org-at-table-p)]
  13853. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13854. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13855. ("Row"
  13856. ["Move Row Up" org-metaup (org-at-table-p)]
  13857. ["Move Row Down" org-metadown (org-at-table-p)]
  13858. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13859. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13860. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13861. "--"
  13862. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13863. ("Rectangle"
  13864. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13865. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13866. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13867. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13868. "--"
  13869. ("Calculate"
  13870. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13871. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13872. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13873. "--"
  13874. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13875. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13876. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13877. "--"
  13878. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13879. "--"
  13880. ["Sum Column/Rectangle" org-table-sum
  13881. (or (org-at-table-p) (org-region-active-p))]
  13882. ["Which Column?" org-table-current-column (org-at-table-p)])
  13883. ["Debug Formulas"
  13884. org-table-toggle-formula-debugger
  13885. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13886. ["Show Col/Row Numbers"
  13887. org-table-toggle-coordinate-overlays
  13888. :style toggle
  13889. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13890. "--"
  13891. ["Create" org-table-create (and (not (org-at-table-p))
  13892. org-enable-table-editor)]
  13893. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13894. ["Import from File" org-table-import (not (org-at-table-p))]
  13895. ["Export to File" org-table-export (org-at-table-p)]
  13896. "--"
  13897. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13898. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13899. '("Org"
  13900. ("Show/Hide"
  13901. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13902. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13903. ["Sparse Tree..." org-sparse-tree t]
  13904. ["Reveal Context" org-reveal t]
  13905. ["Show All" show-all t]
  13906. "--"
  13907. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13908. "--"
  13909. ["New Heading" org-insert-heading t]
  13910. ("Navigate Headings"
  13911. ["Up" outline-up-heading t]
  13912. ["Next" outline-next-visible-heading t]
  13913. ["Previous" outline-previous-visible-heading t]
  13914. ["Next Same Level" outline-forward-same-level t]
  13915. ["Previous Same Level" outline-backward-same-level t]
  13916. "--"
  13917. ["Jump" org-goto t])
  13918. ("Edit Structure"
  13919. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13920. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13921. "--"
  13922. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13923. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13924. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13925. "--"
  13926. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13927. "--"
  13928. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13929. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13930. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13931. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13932. "--"
  13933. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13934. "--"
  13935. ["Convert to odd levels" org-convert-to-odd-levels t]
  13936. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13937. ("Editing"
  13938. ["Emphasis..." org-emphasize t]
  13939. ["Edit Source Example" org-edit-special t]
  13940. "--"
  13941. ["Footnote new/jump" org-footnote-action t]
  13942. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13943. ("Archive"
  13944. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13945. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13946. ; :active t :keys "C-u C-c C-x C-a"]
  13947. ["Sparse trees open ARCHIVE trees"
  13948. (setq org-sparse-tree-open-archived-trees
  13949. (not org-sparse-tree-open-archived-trees))
  13950. :style toggle :selected org-sparse-tree-open-archived-trees]
  13951. ["Cycling opens ARCHIVE trees"
  13952. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13953. :style toggle :selected org-cycle-open-archived-trees]
  13954. "--"
  13955. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13956. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13957. ; ["Check and Move Children" (org-archive-subtree '(4))
  13958. ; :active t :keys "C-u C-c C-x C-s"]
  13959. )
  13960. "--"
  13961. ("Hyperlinks"
  13962. ["Store Link (Global)" org-store-link t]
  13963. ["Insert Link" org-insert-link t]
  13964. ["Follow Link" org-open-at-point t]
  13965. "--"
  13966. ["Next link" org-next-link t]
  13967. ["Previous link" org-previous-link t]
  13968. "--"
  13969. ["Descriptive Links"
  13970. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13971. :style radio
  13972. :selected (member '(org-link) buffer-invisibility-spec)]
  13973. ["Literal Links"
  13974. (progn
  13975. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13976. :style radio
  13977. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13978. "--"
  13979. ("TODO Lists"
  13980. ["TODO/DONE/-" org-todo t]
  13981. ("Select keyword"
  13982. ["Next keyword" org-shiftright (org-on-heading-p)]
  13983. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13984. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13985. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13986. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13987. ["Show TODO Tree" org-show-todo-tree t]
  13988. ["Global TODO list" org-todo-list t]
  13989. "--"
  13990. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13991. :selected org-enforce-todo-dependencies :style toggle :active t]
  13992. "Settings for tree at point"
  13993. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13994. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13995. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13996. ["Do Children parallel" org-toggle-ordered-property :style radio
  13997. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13998. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13999. "--"
  14000. ["Set Priority" org-priority t]
  14001. ["Priority Up" org-shiftup t]
  14002. ["Priority Down" org-shiftdown t]
  14003. "--"
  14004. ["Get news from all feeds" org-feed-update-all t]
  14005. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14006. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14007. ("TAGS and Properties"
  14008. ["Set Tags" org-set-tags-command t]
  14009. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14010. "--"
  14011. ["Set property" org-set-property t]
  14012. ["Column view of properties" org-columns t]
  14013. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14014. ("Dates and Scheduling"
  14015. ["Timestamp" org-time-stamp t]
  14016. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14017. ("Change Date"
  14018. ["1 Day Later" org-shiftright t]
  14019. ["1 Day Earlier" org-shiftleft t]
  14020. ["1 ... Later" org-shiftup t]
  14021. ["1 ... Earlier" org-shiftdown t])
  14022. ["Compute Time Range" org-evaluate-time-range t]
  14023. ["Schedule Item" org-schedule t]
  14024. ["Deadline" org-deadline t]
  14025. "--"
  14026. ["Custom time format" org-toggle-time-stamp-overlays
  14027. :style radio :selected org-display-custom-times]
  14028. "--"
  14029. ["Goto Calendar" org-goto-calendar t]
  14030. ["Date from Calendar" org-date-from-calendar t]
  14031. "--"
  14032. ["Start/Restart Timer" org-timer-start t]
  14033. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14034. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14035. ["Insert Timer String" org-timer t]
  14036. ["Insert Timer Item" org-timer-item t])
  14037. ("Logging work"
  14038. ["Clock in" org-clock-in t]
  14039. ["Clock out" org-clock-out t]
  14040. ["Clock cancel" org-clock-cancel t]
  14041. ["Goto running clock" org-clock-goto t]
  14042. ["Display times" org-clock-display t]
  14043. ["Create clock table" org-clock-report t]
  14044. "--"
  14045. ["Record DONE time"
  14046. (progn (setq org-log-done (not org-log-done))
  14047. (message "Switching to %s will %s record a timestamp"
  14048. (car org-done-keywords)
  14049. (if org-log-done "automatically" "not")))
  14050. :style toggle :selected org-log-done])
  14051. "--"
  14052. ["Agenda Command..." org-agenda t]
  14053. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14054. ("File List for Agenda")
  14055. ("Special views current file"
  14056. ["TODO Tree" org-show-todo-tree t]
  14057. ["Check Deadlines" org-check-deadlines t]
  14058. ["Timeline" org-timeline t]
  14059. ["Tags/Property tree" org-match-sparse-tree t])
  14060. "--"
  14061. ["Export/Publish..." org-export t]
  14062. ("LaTeX"
  14063. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14064. :selected org-cdlatex-mode]
  14065. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14066. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14067. ["Modify math symbol" org-cdlatex-math-modify
  14068. (org-inside-LaTeX-fragment-p)]
  14069. ["Insert citation" org-reftex-citation t]
  14070. "--"
  14071. ["Export LaTeX fragments as images"
  14072. (if (featurep 'org-exp)
  14073. (setq org-export-with-LaTeX-fragments
  14074. (not org-export-with-LaTeX-fragments))
  14075. (require 'org-exp))
  14076. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14077. org-export-with-LaTeX-fragments)])
  14078. "--"
  14079. ("Documentation"
  14080. ["Show Version" org-version t]
  14081. ["Info Documentation" org-info t])
  14082. ("Customize"
  14083. ["Browse Org Group" org-customize t]
  14084. "--"
  14085. ["Expand This Menu" org-create-customize-menu
  14086. (fboundp 'customize-menu-create)])
  14087. "--"
  14088. ("Refresh/Reload"
  14089. ["Refresh setup current buffer" org-mode-restart t]
  14090. ["Reload Org (after update)" org-reload t]
  14091. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14092. ))
  14093. (defun org-info (&optional node)
  14094. "Read documentation for Org-mode in the info system.
  14095. With optional NODE, go directly to that node."
  14096. (interactive)
  14097. (info (format "(org)%s" (or node ""))))
  14098. (defun org-install-agenda-files-menu ()
  14099. (let ((bl (buffer-list)))
  14100. (save-excursion
  14101. (while bl
  14102. (set-buffer (pop bl))
  14103. (if (org-mode-p) (setq bl nil)))
  14104. (when (org-mode-p)
  14105. (easy-menu-change
  14106. '("Org") "File List for Agenda"
  14107. (append
  14108. (list
  14109. ["Edit File List" (org-edit-agenda-file-list) t]
  14110. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14111. ["Remove Current File from List" org-remove-file t]
  14112. ["Cycle through agenda files" org-cycle-agenda-files t]
  14113. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14114. "--")
  14115. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14116. ;;;; Documentation
  14117. ;;;###autoload
  14118. (defun org-require-autoloaded-modules ()
  14119. (interactive)
  14120. (mapc 'require
  14121. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14122. org-docbook org-exp org-html org-icalendar
  14123. org-id org-latex
  14124. org-publish org-remember org-table
  14125. org-timer org-xoxo)))
  14126. ;;;###autoload
  14127. (defun org-reload (&optional uncompiled)
  14128. "Reload all org lisp files.
  14129. With prefix arg UNCOMPILED, load the uncompiled versions."
  14130. (interactive "P")
  14131. (require 'find-func)
  14132. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14133. (dir-org (file-name-directory (org-find-library-name "org")))
  14134. (dir-org-contrib (ignore-errors
  14135. (file-name-directory
  14136. (org-find-library-name "org-contribdir"))))
  14137. (files
  14138. (append (directory-files dir-org t file-re)
  14139. (and dir-org-contrib
  14140. (directory-files dir-org-contrib t file-re))))
  14141. (remove-re (concat (if (featurep 'xemacs)
  14142. "org-colview" "org-colview-xemacs")
  14143. "\\'")))
  14144. (setq files (mapcar 'file-name-sans-extension files))
  14145. (setq files (mapcar
  14146. (lambda (x) (if (string-match remove-re x) nil x))
  14147. files))
  14148. (setq files (delq nil files))
  14149. (mapc
  14150. (lambda (f)
  14151. (when (featurep (intern (file-name-nondirectory f)))
  14152. (if (and (not uncompiled)
  14153. (file-exists-p (concat f ".elc")))
  14154. (load (concat f ".elc") nil nil t)
  14155. (load (concat f ".el") nil nil t))))
  14156. files))
  14157. (org-version))
  14158. ;;;###autoload
  14159. (defun org-customize ()
  14160. "Call the customize function with org as argument."
  14161. (interactive)
  14162. (org-load-modules-maybe)
  14163. (org-require-autoloaded-modules)
  14164. (customize-browse 'org))
  14165. (defun org-create-customize-menu ()
  14166. "Create a full customization menu for Org-mode, insert it into the menu."
  14167. (interactive)
  14168. (org-load-modules-maybe)
  14169. (org-require-autoloaded-modules)
  14170. (if (fboundp 'customize-menu-create)
  14171. (progn
  14172. (easy-menu-change
  14173. '("Org") "Customize"
  14174. `(["Browse Org group" org-customize t]
  14175. "--"
  14176. ,(customize-menu-create 'org)
  14177. ["Set" Custom-set t]
  14178. ["Save" Custom-save t]
  14179. ["Reset to Current" Custom-reset-current t]
  14180. ["Reset to Saved" Custom-reset-saved t]
  14181. ["Reset to Standard Settings" Custom-reset-standard t]))
  14182. (message "\"Org\"-menu now contains full customization menu"))
  14183. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14184. ;;;; Miscellaneous stuff
  14185. ;;; Generally useful functions
  14186. (defun org-find-text-property-in-string (prop s)
  14187. "Return the first non-nil value of property PROP in string S."
  14188. (or (get-text-property 0 prop s)
  14189. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14190. prop s)))
  14191. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14192. "Display the given MESSAGE as a warning."
  14193. (if (fboundp 'display-warning)
  14194. (display-warning 'org message
  14195. (if (featurep 'xemacs)
  14196. 'warning
  14197. :warning))
  14198. (let ((buf (get-buffer-create "*Org warnings*")))
  14199. (with-current-buffer buf
  14200. (goto-char (point-max))
  14201. (insert "Warning (Org): " message)
  14202. (unless (bolp)
  14203. (newline)))
  14204. (display-buffer buf)
  14205. (sit-for 0))))
  14206. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14207. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14208. (if (and marker (marker-buffer marker)
  14209. (buffer-live-p (marker-buffer marker)))
  14210. (progn
  14211. (switch-to-buffer (marker-buffer marker))
  14212. (if (or (> marker (point-max)) (< marker (point-min)))
  14213. (widen))
  14214. (goto-char marker)
  14215. (org-show-context 'org-goto))
  14216. (if bookmark
  14217. (bookmark-jump bookmark)
  14218. (error "Cannot find location"))))
  14219. (defun org-quote-csv-field (s)
  14220. "Quote field for inclusion in CSV material."
  14221. (if (string-match "[\",]" s)
  14222. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14223. s))
  14224. (defun org-plist-delete (plist property)
  14225. "Delete PROPERTY from PLIST.
  14226. This is in contrast to merely setting it to 0."
  14227. (let (p)
  14228. (while plist
  14229. (if (not (eq property (car plist)))
  14230. (setq p (plist-put p (car plist) (nth 1 plist))))
  14231. (setq plist (cddr plist)))
  14232. p))
  14233. (defun org-force-self-insert (N)
  14234. "Needed to enforce self-insert under remapping."
  14235. (interactive "p")
  14236. (self-insert-command N))
  14237. (defun org-string-width (s)
  14238. "Compute width of string, ignoring invisible characters.
  14239. This ignores character with invisibility property `org-link', and also
  14240. characters with property `org-cwidth', because these will become invisible
  14241. upon the next fontification round."
  14242. (let (b l)
  14243. (when (or (eq t buffer-invisibility-spec)
  14244. (assq 'org-link buffer-invisibility-spec))
  14245. (while (setq b (text-property-any 0 (length s)
  14246. 'invisible 'org-link s))
  14247. (setq s (concat (substring s 0 b)
  14248. (substring s (or (next-single-property-change
  14249. b 'invisible s) (length s)))))))
  14250. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14251. (setq s (concat (substring s 0 b)
  14252. (substring s (or (next-single-property-change
  14253. b 'org-cwidth s) (length s))))))
  14254. (setq l (string-width s) b -1)
  14255. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14256. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14257. l))
  14258. (defun org-get-indentation (&optional line)
  14259. "Get the indentation of the current line, interpreting tabs.
  14260. When LINE is given, assume it represents a line and compute its indentation."
  14261. (if line
  14262. (if (string-match "^ *" (org-remove-tabs line))
  14263. (match-end 0))
  14264. (save-excursion
  14265. (beginning-of-line 1)
  14266. (skip-chars-forward " \t")
  14267. (current-column))))
  14268. (defun org-remove-tabs (s &optional width)
  14269. "Replace tabulators in S with spaces.
  14270. Assumes that s is a single line, starting in column 0."
  14271. (setq width (or width tab-width))
  14272. (while (string-match "\t" s)
  14273. (setq s (replace-match
  14274. (make-string
  14275. (- (* width (/ (+ (match-beginning 0) width) width))
  14276. (match-beginning 0)) ?\ )
  14277. t t s)))
  14278. s)
  14279. (defun org-fix-indentation (line ind)
  14280. "Fix indentation in LINE.
  14281. IND is a cons cell with target and minimum indentation.
  14282. If the current indentation in LINE is smaller than the minimum,
  14283. leave it alone. If it is larger than ind, set it to the target."
  14284. (let* ((l (org-remove-tabs line))
  14285. (i (org-get-indentation l))
  14286. (i1 (car ind)) (i2 (cdr ind)))
  14287. (if (>= i i2) (setq l (substring line i2)))
  14288. (if (> i1 0)
  14289. (concat (make-string i1 ?\ ) l)
  14290. l)))
  14291. (defun org-remove-indentation (code &optional n)
  14292. "Remove the maximum common indentation from the lines in CODE.
  14293. N may optionally be the number of spaces to remove."
  14294. (with-temp-buffer
  14295. (insert code)
  14296. (org-do-remove-indentation n)
  14297. (buffer-string)))
  14298. (defun org-do-remove-indentation (&optional n)
  14299. "Remove the maximum common indentation from the buffer."
  14300. (untabify (point-min) (point-max))
  14301. (let ((min 10000) re)
  14302. (if n
  14303. (setq min n)
  14304. (goto-char (point-min))
  14305. (while (re-search-forward "^ *[^ \n]" nil t)
  14306. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14307. (unless (or (= min 0) (= min 10000))
  14308. (setq re (format "^ \\{%d\\}" min))
  14309. (goto-char (point-min))
  14310. (while (re-search-forward re nil t)
  14311. (replace-match "")
  14312. (end-of-line 1))
  14313. min)))
  14314. (defun org-base-buffer (buffer)
  14315. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14316. (if (not buffer)
  14317. buffer
  14318. (or (buffer-base-buffer buffer)
  14319. buffer)))
  14320. (defun org-trim (s)
  14321. "Remove whitespace at beginning and end of string."
  14322. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14323. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14324. s)
  14325. (defun org-wrap (string &optional width lines)
  14326. "Wrap string to either a number of lines, or a width in characters.
  14327. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14328. that costs. If there is a word longer than WIDTH, the text is actually
  14329. wrapped to the length of that word.
  14330. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14331. many lines, whatever width that takes.
  14332. The return value is a list of lines, without newlines at the end."
  14333. (let* ((words (org-split-string string "[ \t\n]+"))
  14334. (maxword (apply 'max (mapcar 'org-string-width words)))
  14335. w ll)
  14336. (cond (width
  14337. (org-do-wrap words (max maxword width)))
  14338. (lines
  14339. (setq w maxword)
  14340. (setq ll (org-do-wrap words maxword))
  14341. (if (<= (length ll) lines)
  14342. ll
  14343. (setq ll words)
  14344. (while (> (length ll) lines)
  14345. (setq w (1+ w))
  14346. (setq ll (org-do-wrap words w)))
  14347. ll))
  14348. (t (error "Cannot wrap this")))))
  14349. (defun org-do-wrap (words width)
  14350. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14351. (let (lines line)
  14352. (while words
  14353. (setq line (pop words))
  14354. (while (and words (< (+ (length line) (length (car words))) width))
  14355. (setq line (concat line " " (pop words))))
  14356. (setq lines (push line lines)))
  14357. (nreverse lines)))
  14358. (defun org-split-string (string &optional separators)
  14359. "Splits STRING into substrings at SEPARATORS.
  14360. No empty strings are returned if there are matches at the beginning
  14361. and end of string."
  14362. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14363. (start 0)
  14364. notfirst
  14365. (list nil))
  14366. (while (and (string-match rexp string
  14367. (if (and notfirst
  14368. (= start (match-beginning 0))
  14369. (< start (length string)))
  14370. (1+ start) start))
  14371. (< (match-beginning 0) (length string)))
  14372. (setq notfirst t)
  14373. (or (eq (match-beginning 0) 0)
  14374. (and (eq (match-beginning 0) (match-end 0))
  14375. (eq (match-beginning 0) start))
  14376. (setq list
  14377. (cons (substring string start (match-beginning 0))
  14378. list)))
  14379. (setq start (match-end 0)))
  14380. (or (eq start (length string))
  14381. (setq list
  14382. (cons (substring string start)
  14383. list)))
  14384. (nreverse list)))
  14385. (defun org-uuidgen-p (s)
  14386. "Is S an ID created by UUIDGEN?"
  14387. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14388. (defun org-context ()
  14389. "Return a list of contexts of the current cursor position.
  14390. If several contexts apply, all are returned.
  14391. Each context entry is a list with a symbol naming the context, and
  14392. two positions indicating start and end of the context. Possible
  14393. contexts are:
  14394. :headline anywhere in a headline
  14395. :headline-stars on the leading stars in a headline
  14396. :todo-keyword on a TODO keyword (including DONE) in a headline
  14397. :tags on the TAGS in a headline
  14398. :priority on the priority cookie in a headline
  14399. :item on the first line of a plain list item
  14400. :item-bullet on the bullet/number of a plain list item
  14401. :checkbox on the checkbox in a plain list item
  14402. :table in an org-mode table
  14403. :table-special on a special filed in a table
  14404. :table-table in a table.el table
  14405. :link on a hyperlink
  14406. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14407. :target on a <<target>>
  14408. :radio-target on a <<<radio-target>>>
  14409. :latex-fragment on a LaTeX fragment
  14410. :latex-preview on a LaTeX fragment with overlayed preview image
  14411. This function expects the position to be visible because it uses font-lock
  14412. faces as a help to recognize the following contexts: :table-special, :link,
  14413. and :keyword."
  14414. (let* ((f (get-text-property (point) 'face))
  14415. (faces (if (listp f) f (list f)))
  14416. (p (point)) clist o)
  14417. ;; First the large context
  14418. (cond
  14419. ((org-on-heading-p t)
  14420. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14421. (when (progn
  14422. (beginning-of-line 1)
  14423. (looking-at org-todo-line-tags-regexp))
  14424. (push (org-point-in-group p 1 :headline-stars) clist)
  14425. (push (org-point-in-group p 2 :todo-keyword) clist)
  14426. (push (org-point-in-group p 4 :tags) clist))
  14427. (goto-char p)
  14428. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14429. (if (looking-at "\\[#[A-Z0-9]\\]")
  14430. (push (org-point-in-group p 0 :priority) clist)))
  14431. ((org-at-item-p)
  14432. (push (org-point-in-group p 2 :item-bullet) clist)
  14433. (push (list :item (point-at-bol)
  14434. (save-excursion (org-end-of-item) (point)))
  14435. clist)
  14436. (and (org-at-item-checkbox-p)
  14437. (push (org-point-in-group p 0 :checkbox) clist)))
  14438. ((org-at-table-p)
  14439. (push (list :table (org-table-begin) (org-table-end)) clist)
  14440. (if (memq 'org-formula faces)
  14441. (push (list :table-special
  14442. (previous-single-property-change p 'face)
  14443. (next-single-property-change p 'face)) clist)))
  14444. ((org-at-table-p 'any)
  14445. (push (list :table-table) clist)))
  14446. (goto-char p)
  14447. ;; Now the small context
  14448. (cond
  14449. ((org-at-timestamp-p)
  14450. (push (org-point-in-group p 0 :timestamp) clist))
  14451. ((memq 'org-link faces)
  14452. (push (list :link
  14453. (previous-single-property-change p 'face)
  14454. (next-single-property-change p 'face)) clist))
  14455. ((memq 'org-special-keyword faces)
  14456. (push (list :keyword
  14457. (previous-single-property-change p 'face)
  14458. (next-single-property-change p 'face)) clist))
  14459. ((org-on-target-p)
  14460. (push (org-point-in-group p 0 :target) clist)
  14461. (goto-char (1- (match-beginning 0)))
  14462. (if (looking-at org-radio-target-regexp)
  14463. (push (org-point-in-group p 0 :radio-target) clist))
  14464. (goto-char p))
  14465. ((setq o (car (delq nil
  14466. (mapcar
  14467. (lambda (x)
  14468. (if (memq x org-latex-fragment-image-overlays) x))
  14469. (org-overlays-at (point))))))
  14470. (push (list :latex-fragment
  14471. (org-overlay-start o) (org-overlay-end o)) clist)
  14472. (push (list :latex-preview
  14473. (org-overlay-start o) (org-overlay-end o)) clist))
  14474. ((org-inside-LaTeX-fragment-p)
  14475. ;; FIXME: positions wrong.
  14476. (push (list :latex-fragment (point) (point)) clist)))
  14477. (setq clist (nreverse (delq nil clist)))
  14478. clist))
  14479. ;; FIXME: Compare with at-regexp-p Do we need both?
  14480. (defun org-in-regexp (re &optional nlines visually)
  14481. "Check if point is inside a match of regexp.
  14482. Normally only the current line is checked, but you can include NLINES extra
  14483. lines both before and after point into the search.
  14484. If VISUALLY is set, require that the cursor is not after the match but
  14485. really on, so that the block visually is on the match."
  14486. (catch 'exit
  14487. (let ((pos (point))
  14488. (eol (point-at-eol (+ 1 (or nlines 0))))
  14489. (inc (if visually 1 0)))
  14490. (save-excursion
  14491. (beginning-of-line (- 1 (or nlines 0)))
  14492. (while (re-search-forward re eol t)
  14493. (if (and (<= (match-beginning 0) pos)
  14494. (>= (+ inc (match-end 0)) pos))
  14495. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14496. (defun org-at-regexp-p (regexp)
  14497. "Is point inside a match of REGEXP in the current line?"
  14498. (catch 'exit
  14499. (save-excursion
  14500. (let ((pos (point)) (end (point-at-eol)))
  14501. (beginning-of-line 1)
  14502. (while (re-search-forward regexp end t)
  14503. (if (and (<= (match-beginning 0) pos)
  14504. (>= (match-end 0) pos))
  14505. (throw 'exit t)))
  14506. nil))))
  14507. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14508. "Call `multi-occur' with buffers for all agenda files."
  14509. (interactive "sOrg-files matching: \np")
  14510. (let* ((files (org-agenda-files))
  14511. (tnames (mapcar 'file-truename files))
  14512. (extra org-agenda-text-search-extra-files)
  14513. f)
  14514. (when (eq (car extra) 'agenda-archives)
  14515. (setq extra (cdr extra))
  14516. (setq files (org-add-archive-files files)))
  14517. (while (setq f (pop extra))
  14518. (unless (member (file-truename f) tnames)
  14519. (add-to-list 'files f 'append)
  14520. (add-to-list 'tnames (file-truename f) 'append)))
  14521. (multi-occur
  14522. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14523. regexp)))
  14524. (if (boundp 'occur-mode-find-occurrence-hook)
  14525. ;; Emacs 23
  14526. (add-hook 'occur-mode-find-occurrence-hook
  14527. (lambda ()
  14528. (when (org-mode-p)
  14529. (org-reveal))))
  14530. ;; Emacs 22
  14531. (defadvice occur-mode-goto-occurrence
  14532. (after org-occur-reveal activate)
  14533. (and (org-mode-p) (org-reveal)))
  14534. (defadvice occur-mode-goto-occurrence-other-window
  14535. (after org-occur-reveal activate)
  14536. (and (org-mode-p) (org-reveal)))
  14537. (defadvice occur-mode-display-occurrence
  14538. (after org-occur-reveal activate)
  14539. (when (org-mode-p)
  14540. (let ((pos (occur-mode-find-occurrence)))
  14541. (with-current-buffer (marker-buffer pos)
  14542. (save-excursion
  14543. (goto-char pos)
  14544. (org-reveal)))))))
  14545. (defun org-uniquify (list)
  14546. "Remove duplicate elements from LIST."
  14547. (let (res)
  14548. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14549. res))
  14550. (defun org-delete-all (elts list)
  14551. "Remove all elements in ELTS from LIST."
  14552. (while elts
  14553. (setq list (delete (pop elts) list)))
  14554. list)
  14555. (defun org-back-over-empty-lines ()
  14556. "Move backwards over whitespace, to the beginning of the first empty line.
  14557. Returns the number of empty lines passed."
  14558. (let ((pos (point)))
  14559. (skip-chars-backward " \t\n\r")
  14560. (beginning-of-line 2)
  14561. (goto-char (min (point) pos))
  14562. (count-lines (point) pos)))
  14563. (defun org-skip-whitespace ()
  14564. (skip-chars-forward " \t\n\r"))
  14565. (defun org-point-in-group (point group &optional context)
  14566. "Check if POINT is in match-group GROUP.
  14567. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14568. match. If the match group does ot exist or point is not inside it,
  14569. return nil."
  14570. (and (match-beginning group)
  14571. (>= point (match-beginning group))
  14572. (<= point (match-end group))
  14573. (if context
  14574. (list context (match-beginning group) (match-end group))
  14575. t)))
  14576. (defun org-switch-to-buffer-other-window (&rest args)
  14577. "Switch to buffer in a second window on the current frame.
  14578. In particular, do not allow pop-up frames."
  14579. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14580. special-display-function)
  14581. (apply 'switch-to-buffer-other-window args)))
  14582. (defun org-combine-plists (&rest plists)
  14583. "Create a single property list from all plists in PLISTS.
  14584. The process starts by copying the first list, and then setting properties
  14585. from the other lists. Settings in the last list are the most significant
  14586. ones and overrule settings in the other lists."
  14587. (let ((rtn (copy-sequence (pop plists)))
  14588. p v ls)
  14589. (while plists
  14590. (setq ls (pop plists))
  14591. (while ls
  14592. (setq p (pop ls) v (pop ls))
  14593. (setq rtn (plist-put rtn p v))))
  14594. rtn))
  14595. (defun org-move-line-down (arg)
  14596. "Move the current line down. With prefix argument, move it past ARG lines."
  14597. (interactive "p")
  14598. (let ((col (current-column))
  14599. beg end pos)
  14600. (beginning-of-line 1) (setq beg (point))
  14601. (beginning-of-line 2) (setq end (point))
  14602. (beginning-of-line (+ 1 arg))
  14603. (setq pos (move-marker (make-marker) (point)))
  14604. (insert (delete-and-extract-region beg end))
  14605. (goto-char pos)
  14606. (org-move-to-column col)))
  14607. (defun org-move-line-up (arg)
  14608. "Move the current line up. With prefix argument, move it past ARG lines."
  14609. (interactive "p")
  14610. (let ((col (current-column))
  14611. beg end pos)
  14612. (beginning-of-line 1) (setq beg (point))
  14613. (beginning-of-line 2) (setq end (point))
  14614. (beginning-of-line (- arg))
  14615. (setq pos (move-marker (make-marker) (point)))
  14616. (insert (delete-and-extract-region beg end))
  14617. (goto-char pos)
  14618. (org-move-to-column col)))
  14619. (defun org-replace-escapes (string table)
  14620. "Replace %-escapes in STRING with values in TABLE.
  14621. TABLE is an association list with keys like \"%a\" and string values.
  14622. The sequences in STRING may contain normal field width and padding information,
  14623. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14624. so values can contain further %-escapes if they are define later in TABLE."
  14625. (let ((case-fold-search nil)
  14626. e re rpl)
  14627. (while (setq e (pop table))
  14628. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14629. (while (string-match re string)
  14630. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14631. (cdr e)))
  14632. (setq string (replace-match rpl t t string))))
  14633. string))
  14634. (defun org-sublist (list start end)
  14635. "Return a section of LIST, from START to END.
  14636. Counting starts at 1."
  14637. (let (rtn (c start))
  14638. (setq list (nthcdr (1- start) list))
  14639. (while (and list (<= c end))
  14640. (push (pop list) rtn)
  14641. (setq c (1+ c)))
  14642. (nreverse rtn)))
  14643. (defun org-find-base-buffer-visiting (file)
  14644. "Like `find-buffer-visiting' but always return the base buffer and
  14645. not an indirect buffer."
  14646. (let ((buf (or (get-file-buffer file)
  14647. (find-buffer-visiting file))))
  14648. (if buf
  14649. (or (buffer-base-buffer buf) buf)
  14650. nil)))
  14651. (defun org-image-file-name-regexp (&optional extensions)
  14652. "Return regexp matching the file names of images.
  14653. If EXTENSIONS is given, only match these."
  14654. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14655. (image-file-name-regexp)
  14656. (let ((image-file-name-extensions
  14657. (or extensions
  14658. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14659. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14660. (concat "\\."
  14661. (regexp-opt (nconc (mapcar 'upcase
  14662. image-file-name-extensions)
  14663. image-file-name-extensions)
  14664. t)
  14665. "\\'"))))
  14666. (defun org-file-image-p (file &optional extensions)
  14667. "Return non-nil if FILE is an image."
  14668. (save-match-data
  14669. (string-match (org-image-file-name-regexp extensions) file)))
  14670. (defun org-get-cursor-date ()
  14671. "Return the date at cursor in as a time.
  14672. This works in the calendar and in the agenda, anywhere else it just
  14673. returns the current time."
  14674. (let (date day defd)
  14675. (cond
  14676. ((eq major-mode 'calendar-mode)
  14677. (setq date (calendar-cursor-to-date)
  14678. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14679. ((eq major-mode 'org-agenda-mode)
  14680. (setq day (get-text-property (point) 'day))
  14681. (if day
  14682. (setq date (calendar-gregorian-from-absolute day)
  14683. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14684. (nth 2 date))))))
  14685. (or defd (current-time))))
  14686. (defvar org-agenda-action-marker (make-marker)
  14687. "Marker pointing to the entry for the next agenda action.")
  14688. (defun org-mark-entry-for-agenda-action ()
  14689. "Mark the current entry as target of an agenda action.
  14690. Agenda actions are actions executed from the agenda with the key `k',
  14691. which make use of the date at the cursor."
  14692. (interactive)
  14693. (move-marker org-agenda-action-marker
  14694. (save-excursion (org-back-to-heading t) (point))
  14695. (current-buffer))
  14696. (message
  14697. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14698. ;;; Paragraph filling stuff.
  14699. ;; We want this to be just right, so use the full arsenal.
  14700. (defun org-indent-line-function ()
  14701. "Indent line like previous, but further if previous was headline or item."
  14702. (interactive)
  14703. (let* ((pos (point))
  14704. (itemp (org-at-item-p))
  14705. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14706. column bpos bcol tpos tcol bullet btype bullet-type)
  14707. ;; Find the previous relevant line
  14708. (beginning-of-line 1)
  14709. (cond
  14710. ((looking-at "#") (setq column 0))
  14711. ((looking-at "\\*+ ") (setq column 0))
  14712. ((and (looking-at "[ \t]*:END:")
  14713. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14714. (save-excursion
  14715. (goto-char (1- (match-beginning 1)))
  14716. (setq column (current-column))))
  14717. (t
  14718. (beginning-of-line 0)
  14719. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14720. (not (looking-at "[ \t]*:END:"))
  14721. (not (looking-at org-drawer-regexp)))
  14722. (beginning-of-line 0))
  14723. (cond
  14724. ((looking-at "\\*+[ \t]+")
  14725. (if (not org-adapt-indentation)
  14726. (setq column 0)
  14727. (goto-char (match-end 0))
  14728. (setq column (current-column))))
  14729. ((looking-at org-drawer-regexp)
  14730. (goto-char (1- (match-beginning 1)))
  14731. (setq column (current-column)))
  14732. ((looking-at "\\([ \t]*\\):END:")
  14733. (goto-char (match-end 1))
  14734. (setq column (current-column)))
  14735. ((org-in-item-p)
  14736. (org-beginning-of-item)
  14737. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14738. (setq bpos (match-beginning 1) tpos (match-end 0)
  14739. bcol (progn (goto-char bpos) (current-column))
  14740. tcol (progn (goto-char tpos) (current-column))
  14741. bullet (match-string 1)
  14742. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14743. (if (> tcol (+ bcol org-description-max-indent))
  14744. (setq tcol (+ bcol 5)))
  14745. (if (not itemp)
  14746. (setq column tcol)
  14747. (goto-char pos)
  14748. (beginning-of-line 1)
  14749. (if (looking-at "\\S-")
  14750. (progn
  14751. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14752. (setq bullet (match-string 1)
  14753. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14754. (setq column (if (equal btype bullet-type) bcol tcol)))
  14755. (setq column (org-get-indentation)))))
  14756. (t (setq column (org-get-indentation))))))
  14757. (goto-char pos)
  14758. (if (<= (current-column) (current-indentation))
  14759. (org-indent-line-to column)
  14760. (save-excursion (org-indent-line-to column)))
  14761. (setq column (current-column))
  14762. (beginning-of-line 1)
  14763. (if (looking-at
  14764. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14765. (replace-match (concat "\\1" (format org-property-format
  14766. (match-string 2) (match-string 3)))
  14767. t nil))
  14768. (org-move-to-column column)))
  14769. (defun org-set-autofill-regexps ()
  14770. (interactive)
  14771. ;; In the paragraph separator we include headlines, because filling
  14772. ;; text in a line directly attached to a headline would otherwise
  14773. ;; fill the headline as well.
  14774. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14775. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14776. ;; The paragraph starter includes hand-formatted lists.
  14777. (org-set-local 'paragraph-start
  14778. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14779. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14780. ;; But only if the user has not turned off tables or fixed-width regions
  14781. (org-set-local
  14782. 'auto-fill-inhibit-regexp
  14783. (concat "\\*+ \\|#\\+"
  14784. "\\|[ \t]*" org-keyword-time-regexp
  14785. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14786. (concat
  14787. "\\|[ \t]*["
  14788. (if org-enable-table-editor "|" "")
  14789. (if org-enable-fixed-width-editor ":" "")
  14790. "]"))))
  14791. ;; We use our own fill-paragraph function, to make sure that tables
  14792. ;; and fixed-width regions are not wrapped. That function will pass
  14793. ;; through to `fill-paragraph' when appropriate.
  14794. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14795. ; Adaptive filling: To get full control, first make sure that
  14796. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14797. (org-set-local 'adaptive-fill-regexp "\000")
  14798. (org-set-local 'adaptive-fill-function
  14799. 'org-adaptive-fill-function)
  14800. (org-set-local
  14801. 'align-mode-rules-list
  14802. '((org-in-buffer-settings
  14803. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14804. (modes . '(org-mode))))))
  14805. (defun org-fill-paragraph (&optional justify)
  14806. "Re-align a table, pass through to fill-paragraph if no table."
  14807. (let ((table-p (org-at-table-p))
  14808. (table.el-p (org-at-table.el-p)))
  14809. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14810. (save-excursion (goto-char (point-at-bol))
  14811. (looking-at outline-regexp)))
  14812. t) ; skip headlines
  14813. (table.el-p t) ; skip table.el tables
  14814. (table-p (org-table-align) t) ; align org-mode tables
  14815. (t nil)))) ; call paragraph-fill
  14816. ;; For reference, this is the default value of adaptive-fill-regexp
  14817. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14818. (defun org-adaptive-fill-function ()
  14819. "Return a fill prefix for org-mode files.
  14820. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14821. work correctly."
  14822. (cond ((looking-at "#[ \t]+")
  14823. (match-string 0))
  14824. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14825. (save-excursion
  14826. (if (> (match-end 1) (+ (match-beginning 1)
  14827. org-description-max-indent))
  14828. (goto-char (+ (match-beginning 1) 5))
  14829. (goto-char (match-end 0)))
  14830. (make-string (current-column) ?\ )))
  14831. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14832. (save-excursion
  14833. (goto-char (match-end 0))
  14834. (make-string (current-column) ?\ )))
  14835. (t nil)))
  14836. ;;; Other stuff.
  14837. (defun org-toggle-fixed-width-section (arg)
  14838. "Toggle the fixed-width export.
  14839. If there is no active region, the QUOTE keyword at the current headline is
  14840. inserted or removed. When present, it causes the text between this headline
  14841. and the next to be exported as fixed-width text, and unmodified.
  14842. If there is an active region, this command adds or removes a colon as the
  14843. first character of this line. If the first character of a line is a colon,
  14844. this line is also exported in fixed-width font."
  14845. (interactive "P")
  14846. (let* ((cc 0)
  14847. (regionp (org-region-active-p))
  14848. (beg (if regionp (region-beginning) (point)))
  14849. (end (if regionp (region-end)))
  14850. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14851. (case-fold-search nil)
  14852. (re "[ \t]*\\(: \\)")
  14853. off)
  14854. (if regionp
  14855. (save-excursion
  14856. (goto-char beg)
  14857. (setq cc (current-column))
  14858. (beginning-of-line 1)
  14859. (setq off (looking-at re))
  14860. (while (> nlines 0)
  14861. (setq nlines (1- nlines))
  14862. (beginning-of-line 1)
  14863. (cond
  14864. (arg
  14865. (org-move-to-column cc t)
  14866. (insert ": \n")
  14867. (forward-line -1))
  14868. ((and off (looking-at re))
  14869. (replace-match "" t t nil 1))
  14870. ((not off) (org-move-to-column cc t) (insert ": ")))
  14871. (forward-line 1)))
  14872. (save-excursion
  14873. (org-back-to-heading)
  14874. (if (looking-at (concat outline-regexp
  14875. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14876. (replace-match "" t t nil 1)
  14877. (if (looking-at outline-regexp)
  14878. (progn
  14879. (goto-char (match-end 0))
  14880. (insert org-quote-string " "))))))))
  14881. (defun org-reftex-citation ()
  14882. "Use reftex-citation to insert a citation into the buffer.
  14883. This looks for a line like
  14884. #+BIBLIOGRAPHY: foo plain option:-d
  14885. and derives from it that foo.bib is the bbliography file relevant
  14886. for this document. It then installs the necessary environment for RefTeX
  14887. to work in this buffer and calls `reftex-citation' to insert a citation
  14888. into the buffer.
  14889. Export of such citations to both LaTeX and HTML is handled by the contributed
  14890. package org-exp-bibtex by Taru Karttunen."
  14891. (interactive)
  14892. (let ((reftex-docstruct-symbol 'rds)
  14893. (reftex-cite-format "\\cite{%l}")
  14894. rds bib)
  14895. (save-excursion
  14896. (save-restriction
  14897. (widen)
  14898. (let ((case-fold-search t)
  14899. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14900. (if (not (save-excursion
  14901. (or (re-search-forward re nil t)
  14902. (re-search-backward re nil t))))
  14903. (error "No bibliography defined in file")
  14904. (setq bib (concat (match-string 1) ".bib")
  14905. rds (list (list 'bib bib)))))))
  14906. (call-interactively 'reftex-citation)))
  14907. ;;;; Functions extending outline functionality
  14908. (defun org-beginning-of-line (&optional arg)
  14909. "Go to the beginning of the current line. If that is invisible, continue
  14910. to a visible line beginning. This makes the function of C-a more intuitive.
  14911. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14912. first attempt, and only move to after the tags when the cursor is already
  14913. beyond the end of the headline."
  14914. (interactive "P")
  14915. (let ((pos (point))
  14916. (special (if (consp org-special-ctrl-a/e)
  14917. (car org-special-ctrl-a/e)
  14918. org-special-ctrl-a/e))
  14919. refpos)
  14920. (beginning-of-line 1)
  14921. (if (and arg (fboundp 'move-beginning-of-line))
  14922. (call-interactively 'move-beginning-of-line)
  14923. (if (bobp)
  14924. nil
  14925. (backward-char 1)
  14926. (if (org-invisible-p)
  14927. (while (and (not (bobp)) (org-invisible-p))
  14928. (backward-char 1)
  14929. (beginning-of-line 1))
  14930. (forward-char 1))))
  14931. (when special
  14932. (cond
  14933. ((and (looking-at org-complex-heading-regexp)
  14934. (= (char-after (match-end 1)) ?\ ))
  14935. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14936. (point-at-eol)))
  14937. (goto-char
  14938. (if (eq special t)
  14939. (cond ((> pos refpos) refpos)
  14940. ((= pos (point)) refpos)
  14941. (t (point)))
  14942. (cond ((> pos (point)) (point))
  14943. ((not (eq last-command this-command)) (point))
  14944. (t refpos)))))
  14945. ((org-at-item-p)
  14946. (goto-char
  14947. (if (eq special t)
  14948. (cond ((> pos (match-end 4)) (match-end 4))
  14949. ((= pos (point)) (match-end 4))
  14950. (t (point)))
  14951. (cond ((> pos (point)) (point))
  14952. ((not (eq last-command this-command)) (point))
  14953. (t (match-end 4))))))))
  14954. (org-no-warnings
  14955. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14956. (defun org-end-of-line (&optional arg)
  14957. "Go to the end of the line.
  14958. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14959. first attempt, and only move to after the tags when the cursor is already
  14960. beyond the end of the headline."
  14961. (interactive "P")
  14962. (let ((special (if (consp org-special-ctrl-a/e)
  14963. (cdr org-special-ctrl-a/e)
  14964. org-special-ctrl-a/e)))
  14965. (if (or (not special)
  14966. (not (org-on-heading-p))
  14967. arg)
  14968. (call-interactively (if (fboundp 'move-end-of-line)
  14969. 'move-end-of-line
  14970. 'end-of-line))
  14971. (let ((pos (point)))
  14972. (beginning-of-line 1)
  14973. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14974. (if (eq special t)
  14975. (if (or (< pos (match-beginning 1))
  14976. (= pos (match-end 0)))
  14977. (goto-char (match-beginning 1))
  14978. (goto-char (match-end 0)))
  14979. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14980. (goto-char (match-end 0))
  14981. (goto-char (match-beginning 1))))
  14982. (call-interactively (if (fboundp 'move-end-of-line)
  14983. 'move-end-of-line
  14984. 'end-of-line)))))
  14985. (org-no-warnings
  14986. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14987. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14988. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14989. (defun org-backward-sentence (&optional arg)
  14990. "Go to beginning of sentence, or beginning of table field.
  14991. This will call `backward-sentence' or `org-table-beginning-of-field',
  14992. depending on context."
  14993. (interactive "P")
  14994. (cond
  14995. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14996. (t (call-interactively 'backward-sentence))))
  14997. (defun org-forward-sentence (&optional arg)
  14998. "Go to end of sentence, or end of table field.
  14999. This will call `forward-sentence' or `org-table-end-of-field',
  15000. depending on context."
  15001. (interactive "P")
  15002. (cond
  15003. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15004. (t (call-interactively 'forward-sentence))))
  15005. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15006. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15007. (defun org-kill-line (&optional arg)
  15008. "Kill line, to tags or end of line."
  15009. (interactive "P")
  15010. (cond
  15011. ((or (not org-special-ctrl-k)
  15012. (bolp)
  15013. (not (org-on-heading-p)))
  15014. (call-interactively 'kill-line))
  15015. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15016. (kill-region (point) (match-beginning 1))
  15017. (org-set-tags nil t))
  15018. (t (kill-region (point) (point-at-eol)))))
  15019. (define-key org-mode-map "\C-k" 'org-kill-line)
  15020. (defun org-yank (&optional arg)
  15021. "Yank. If the kill is a subtree, treat it specially.
  15022. This command will look at the current kill and check if is a single
  15023. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15024. cursor is at the beginning of a line or after the stars of a currently
  15025. empty headline, then the yank is handled specially. How exactly depends
  15026. on the value of the following variables, both set by default.
  15027. org-yank-folded-subtrees
  15028. When set, the subtree(s) will be folded after insertion, but only
  15029. if doing so would now swallow text after the yanked text.
  15030. org-yank-adjusted-subtrees
  15031. When set, the subtree will be promoted or demoted in order to
  15032. fit into the local outline tree structure, which means that the level
  15033. will be adjusted so that it becomes the smaller one of the two
  15034. *visible* surrounding headings.
  15035. Any prefix to this command will cause `yank' to be called directly with
  15036. no special treatment. In particular, a simple `C-u' prefix will just
  15037. plainly yank the text as it is.
  15038. \[1] The test checks if the first non-white line is a heading
  15039. and if there are no other headings with fewer stars."
  15040. (interactive "P")
  15041. (org-yank-generic 'yank arg))
  15042. (defun org-yank-generic (command arg)
  15043. "Perform some yank-like command.
  15044. This function implements the behavior described in the `org-yank'
  15045. documentation. However, it has been generalized to work for any
  15046. interactive command with similar behavior."
  15047. ;; pretend to be command COMMAND
  15048. (setq this-command command)
  15049. (if arg
  15050. (call-interactively command)
  15051. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15052. (and (org-kill-is-subtree-p)
  15053. (or (bolp)
  15054. (and (looking-at "[ \t]*$")
  15055. (string-match
  15056. "\\`\\*+\\'"
  15057. (buffer-substring (point-at-bol) (point)))))))
  15058. swallowp)
  15059. (cond
  15060. ((and subtreep org-yank-folded-subtrees)
  15061. (let ((beg (point))
  15062. end)
  15063. (if (and subtreep org-yank-adjusted-subtrees)
  15064. (org-paste-subtree nil nil 'for-yank)
  15065. (call-interactively command))
  15066. (setq end (point))
  15067. (goto-char beg)
  15068. (when (and (bolp) subtreep
  15069. (not (setq swallowp
  15070. (org-yank-folding-would-swallow-text beg end))))
  15071. (or (looking-at outline-regexp)
  15072. (re-search-forward (concat "^" outline-regexp) end t))
  15073. (while (and (< (point) end) (looking-at outline-regexp))
  15074. (hide-subtree)
  15075. (org-cycle-show-empty-lines 'folded)
  15076. (condition-case nil
  15077. (outline-forward-same-level 1)
  15078. (error (goto-char end)))))
  15079. (when swallowp
  15080. (message
  15081. "Inserted text not folded because that would swallow text"))
  15082. (goto-char end)
  15083. (skip-chars-forward " \t\n\r")
  15084. (beginning-of-line 1)
  15085. (push-mark beg 'nomsg)))
  15086. ((and subtreep org-yank-adjusted-subtrees)
  15087. (let ((beg (point-at-bol)))
  15088. (org-paste-subtree nil nil 'for-yank)
  15089. (push-mark beg 'nomsg)))
  15090. (t
  15091. (call-interactively command))))))
  15092. (defun org-yank-folding-would-swallow-text (beg end)
  15093. "Would hide-subtree at BEG swallow any text after END?"
  15094. (let (level)
  15095. (save-excursion
  15096. (goto-char beg)
  15097. (when (or (looking-at outline-regexp)
  15098. (re-search-forward (concat "^" outline-regexp) end t))
  15099. (setq level (org-outline-level)))
  15100. (goto-char end)
  15101. (skip-chars-forward " \t\r\n\v\f")
  15102. (if (or (eobp)
  15103. (and (bolp) (looking-at org-outline-regexp)
  15104. (<= (org-outline-level) level)))
  15105. nil ; Nothing would be swallowed
  15106. t)))) ; something would swallow
  15107. (define-key org-mode-map "\C-y" 'org-yank)
  15108. (defun org-invisible-p ()
  15109. "Check if point is at a character currently not visible."
  15110. ;; Early versions of noutline don't have `outline-invisible-p'.
  15111. (if (fboundp 'outline-invisible-p)
  15112. (outline-invisible-p)
  15113. (get-char-property (point) 'invisible)))
  15114. (defun org-invisible-p2 ()
  15115. "Check if point is at a character currently not visible."
  15116. (save-excursion
  15117. (if (and (eolp) (not (bobp))) (backward-char 1))
  15118. ;; Early versions of noutline don't have `outline-invisible-p'.
  15119. (if (fboundp 'outline-invisible-p)
  15120. (outline-invisible-p)
  15121. (get-char-property (point) 'invisible))))
  15122. (defun org-back-to-heading (&optional invisible-ok)
  15123. "Call `outline-back-to-heading', but provide a better error message."
  15124. (condition-case nil
  15125. (outline-back-to-heading invisible-ok)
  15126. (error (error "Before first headline at position %d in buffer %s"
  15127. (point) (current-buffer)))))
  15128. (defun org-before-first-heading-p ()
  15129. "Before first heading?"
  15130. (save-excursion
  15131. (null (re-search-backward "^\\*+ " nil t))))
  15132. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15133. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15134. (defun org-at-heading-or-item-p ()
  15135. (or (org-on-heading-p) (org-at-item-p)))
  15136. (defun org-on-target-p ()
  15137. (or (org-in-regexp org-radio-target-regexp)
  15138. (org-in-regexp org-target-regexp)))
  15139. (defun org-up-heading-all (arg)
  15140. "Move to the heading line of which the present line is a subheading.
  15141. This function considers both visible and invisible heading lines.
  15142. With argument, move up ARG levels."
  15143. (if (fboundp 'outline-up-heading-all)
  15144. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15145. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15146. (defun org-up-heading-safe ()
  15147. "Move to the heading line of which the present line is a subheading.
  15148. This version will not throw an error. It will return the level of the
  15149. headline found, or nil if no higher level is found.
  15150. Also, this function will be a lot faster than `outline-up-heading',
  15151. because it relies on stars being the outline starters. This can really
  15152. make a significant difference in outlines with very many siblings."
  15153. (let (start-level re)
  15154. (org-back-to-heading t)
  15155. (setq start-level (funcall outline-level))
  15156. (if (equal start-level 1)
  15157. nil
  15158. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15159. (if (re-search-backward re nil t)
  15160. (funcall outline-level)))))
  15161. (defun org-first-sibling-p ()
  15162. "Is this heading the first child of its parents?"
  15163. (interactive)
  15164. (let ((re (concat "^" outline-regexp))
  15165. level l)
  15166. (unless (org-at-heading-p t)
  15167. (error "Not at a heading"))
  15168. (setq level (funcall outline-level))
  15169. (save-excursion
  15170. (if (not (re-search-backward re nil t))
  15171. t
  15172. (setq l (funcall outline-level))
  15173. (< l level)))))
  15174. (defun org-goto-sibling (&optional previous)
  15175. "Goto the next sibling, even if it is invisible.
  15176. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15177. when a sibling was found. When none is found, return nil and don't
  15178. move point."
  15179. (let ((fun (if previous 're-search-backward 're-search-forward))
  15180. (pos (point))
  15181. (re (concat "^" outline-regexp))
  15182. level l)
  15183. (when (condition-case nil (org-back-to-heading t) (error nil))
  15184. (setq level (funcall outline-level))
  15185. (catch 'exit
  15186. (or previous (forward-char 1))
  15187. (while (funcall fun re nil t)
  15188. (setq l (funcall outline-level))
  15189. (when (< l level) (goto-char pos) (throw 'exit nil))
  15190. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15191. (goto-char pos)
  15192. nil))))
  15193. (defun org-show-siblings ()
  15194. "Show all siblings of the current headline."
  15195. (save-excursion
  15196. (while (org-goto-sibling) (org-flag-heading nil)))
  15197. (save-excursion
  15198. (while (org-goto-sibling 'previous)
  15199. (org-flag-heading nil))))
  15200. (defun org-show-hidden-entry ()
  15201. "Show an entry where even the heading is hidden."
  15202. (save-excursion
  15203. (org-show-entry)))
  15204. (defun org-flag-heading (flag &optional entry)
  15205. "Flag the current heading. FLAG non-nil means make invisible.
  15206. When ENTRY is non-nil, show the entire entry."
  15207. (save-excursion
  15208. (org-back-to-heading t)
  15209. ;; Check if we should show the entire entry
  15210. (if entry
  15211. (progn
  15212. (org-show-entry)
  15213. (save-excursion
  15214. (and (outline-next-heading)
  15215. (org-flag-heading nil))))
  15216. (outline-flag-region (max (point-min) (1- (point)))
  15217. (save-excursion (outline-end-of-heading) (point))
  15218. flag))))
  15219. (defun org-forward-same-level (arg)
  15220. "Move forward to the ARG'th subheading at same level as this one.
  15221. Stop at the first and last subheadings of a superior heading.
  15222. This is like outline-forward-same-level, but invisible headings are ok."
  15223. (interactive "p")
  15224. (org-back-to-heading t)
  15225. (while (> arg 0)
  15226. (let ((point-to-move-to (save-excursion
  15227. (org-get-next-sibling))))
  15228. (if point-to-move-to
  15229. (progn
  15230. (goto-char point-to-move-to)
  15231. (setq arg (1- arg)))
  15232. (progn
  15233. (setq arg 0)
  15234. (error "No following same-level heading"))))))
  15235. (defun org-get-next-sibling ()
  15236. "Move to next heading of the same level, and return point.
  15237. If there is no such heading, return nil.
  15238. This is like outline-next-sibling, but invisible headings are ok."
  15239. (let ((level (funcall outline-level)))
  15240. (outline-next-heading)
  15241. (while (and (not (eobp)) (> (funcall outline-level) level))
  15242. (outline-next-heading))
  15243. (if (or (eobp) (< (funcall outline-level) level))
  15244. nil
  15245. (point))))
  15246. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15247. ;; This contains an exact copy of the original function, but it uses
  15248. ;; `org-back-to-heading', to make it work also in invisible
  15249. ;; trees. And is uses an invisible-OK argument.
  15250. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15251. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15252. ;; with many children and grandchildren etc, this can be much faster
  15253. ;; than the outline version.
  15254. (org-back-to-heading invisible-OK)
  15255. (let ((first t)
  15256. (level (funcall outline-level)))
  15257. (if (and (org-mode-p) (< level 1000))
  15258. ;; A true heading (not a plain list item), in Org-mode
  15259. ;; This means we can easily find the end by looking
  15260. ;; only for the right number of stars. Using a regexp to do
  15261. ;; this is so much faster than using a Lisp loop.
  15262. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15263. (forward-char 1)
  15264. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15265. ;; something else, do it the slow way
  15266. (while (and (not (eobp))
  15267. (or first (> (funcall outline-level) level)))
  15268. (setq first nil)
  15269. (outline-next-heading)))
  15270. (unless to-heading
  15271. (if (memq (preceding-char) '(?\n ?\^M))
  15272. (progn
  15273. ;; Go to end of line before heading
  15274. (forward-char -1)
  15275. (if (memq (preceding-char) '(?\n ?\^M))
  15276. ;; leave blank line before heading
  15277. (forward-char -1))))))
  15278. (point))
  15279. (defun org-show-subtree ()
  15280. "Show everything after this heading at deeper levels."
  15281. (outline-flag-region
  15282. (point)
  15283. (save-excursion
  15284. (outline-end-of-subtree) (outline-next-heading) (point))
  15285. nil))
  15286. (defun org-show-entry ()
  15287. "Show the body directly following this heading.
  15288. Show the heading too, if it is currently invisible."
  15289. (interactive)
  15290. (save-excursion
  15291. (condition-case nil
  15292. (progn
  15293. (org-back-to-heading t)
  15294. (outline-flag-region
  15295. (max (point-min) (1- (point)))
  15296. (save-excursion
  15297. (if (re-search-forward
  15298. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15299. (match-beginning 1)
  15300. (point-max)))
  15301. nil))
  15302. (error nil))))
  15303. (defun org-make-options-regexp (kwds &optional extra)
  15304. "Make a regular expression for keyword lines."
  15305. (concat
  15306. "^"
  15307. "#?[ \t]*\\+\\("
  15308. (mapconcat 'regexp-quote kwds "\\|")
  15309. (if extra (concat "\\|" extra))
  15310. "\\):[ \t]*"
  15311. "\\(.+\\)"))
  15312. ;; Make isearch reveal the necessary context
  15313. (defun org-isearch-end ()
  15314. "Reveal context after isearch exits."
  15315. (when isearch-success ; only if search was successful
  15316. (if (featurep 'xemacs)
  15317. ;; Under XEmacs, the hook is run in the correct place,
  15318. ;; we directly show the context.
  15319. (org-show-context 'isearch)
  15320. ;; In Emacs the hook runs *before* restoring the overlays.
  15321. ;; So we have to use a one-time post-command-hook to do this.
  15322. ;; (Emacs 22 has a special variable, see function `org-mode')
  15323. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15324. isearch-mode-end-hook-quit)
  15325. ;; Only when the isearch was not quitted.
  15326. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15327. 'append 'local)))))
  15328. (defun org-isearch-post-command ()
  15329. "Remove self from hook, and show context."
  15330. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15331. (org-show-context 'isearch))
  15332. ;;;; Integration with and fixes for other packages
  15333. ;;; Imenu support
  15334. (defvar org-imenu-markers nil
  15335. "All markers currently used by Imenu.")
  15336. (make-variable-buffer-local 'org-imenu-markers)
  15337. (defun org-imenu-new-marker (&optional pos)
  15338. "Return a new marker for use by Imenu, and remember the marker."
  15339. (let ((m (make-marker)))
  15340. (move-marker m (or pos (point)))
  15341. (push m org-imenu-markers)
  15342. m))
  15343. (defun org-imenu-get-tree ()
  15344. "Produce the index for Imenu."
  15345. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15346. (setq org-imenu-markers nil)
  15347. (let* ((n org-imenu-depth)
  15348. (re (concat "^" outline-regexp))
  15349. (subs (make-vector (1+ n) nil))
  15350. (last-level 0)
  15351. m level head)
  15352. (save-excursion
  15353. (save-restriction
  15354. (widen)
  15355. (goto-char (point-max))
  15356. (while (re-search-backward re nil t)
  15357. (setq level (org-reduced-level (funcall outline-level)))
  15358. (when (<= level n)
  15359. (looking-at org-complex-heading-regexp)
  15360. (setq head (org-link-display-format
  15361. (org-match-string-no-properties 4))
  15362. m (org-imenu-new-marker))
  15363. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15364. (if (>= level last-level)
  15365. (push (cons head m) (aref subs level))
  15366. (push (cons head (aref subs (1+ level))) (aref subs level))
  15367. (loop for i from (1+ level) to n do (aset subs i nil)))
  15368. (setq last-level level)))))
  15369. (aref subs 1)))
  15370. (eval-after-load "imenu"
  15371. '(progn
  15372. (add-hook 'imenu-after-jump-hook
  15373. (lambda ()
  15374. (if (eq major-mode 'org-mode)
  15375. (org-show-context 'org-goto))))))
  15376. (defun org-link-display-format (link)
  15377. "Replace a link with either the description, or the link target
  15378. if no description is present"
  15379. (save-match-data
  15380. (if (string-match org-bracket-link-analytic-regexp link)
  15381. (replace-match (or (match-string 5 link)
  15382. (concat (match-string 1 link)
  15383. (match-string 3 link)))
  15384. nil nil link)
  15385. link)))
  15386. ;; Speedbar support
  15387. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15388. "Overlay marking the agenda restriction line in speedbar.")
  15389. (org-overlay-put org-speedbar-restriction-lock-overlay
  15390. 'face 'org-agenda-restriction-lock)
  15391. (org-overlay-put org-speedbar-restriction-lock-overlay
  15392. 'help-echo "Agendas are currently limited to this item.")
  15393. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15394. (defun org-speedbar-set-agenda-restriction ()
  15395. "Restrict future agenda commands to the location at point in speedbar.
  15396. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15397. (interactive)
  15398. (require 'org-agenda)
  15399. (let (p m tp np dir txt)
  15400. (cond
  15401. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15402. 'org-imenu t))
  15403. (setq m (get-text-property p 'org-imenu-marker))
  15404. (save-excursion
  15405. (save-restriction
  15406. (set-buffer (marker-buffer m))
  15407. (goto-char m)
  15408. (org-agenda-set-restriction-lock 'subtree))))
  15409. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15410. 'speedbar-function 'speedbar-find-file))
  15411. (setq tp (previous-single-property-change
  15412. (1+ p) 'speedbar-function)
  15413. np (next-single-property-change
  15414. tp 'speedbar-function)
  15415. dir (speedbar-line-directory)
  15416. txt (buffer-substring-no-properties (or tp (point-min))
  15417. (or np (point-max))))
  15418. (save-excursion
  15419. (save-restriction
  15420. (set-buffer (find-file-noselect
  15421. (let ((default-directory dir))
  15422. (expand-file-name txt))))
  15423. (unless (org-mode-p)
  15424. (error "Cannot restrict to non-Org-mode file"))
  15425. (org-agenda-set-restriction-lock 'file))))
  15426. (t (error "Don't know how to restrict Org-mode's agenda")))
  15427. (org-move-overlay org-speedbar-restriction-lock-overlay
  15428. (point-at-bol) (point-at-eol))
  15429. (setq current-prefix-arg nil)
  15430. (org-agenda-maybe-redo)))
  15431. (eval-after-load "speedbar"
  15432. '(progn
  15433. (speedbar-add-supported-extension ".org")
  15434. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15435. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15436. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15437. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15438. (add-hook 'speedbar-visiting-tag-hook
  15439. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15440. ;;; Fixes and Hacks for problems with other packages
  15441. ;; Make flyspell not check words in links, to not mess up our keymap
  15442. (defun org-mode-flyspell-verify ()
  15443. "Don't let flyspell put overlays at active buttons."
  15444. (not (get-text-property (point) 'keymap)))
  15445. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15446. (eval-after-load "bookmark"
  15447. '(if (boundp 'bookmark-after-jump-hook)
  15448. ;; We can use the hook
  15449. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15450. ;; Hook not available, use advice
  15451. (defadvice bookmark-jump (after org-make-visible activate)
  15452. "Make the position visible."
  15453. (org-bookmark-jump-unhide))))
  15454. ;; Make sure saveplace show the location if it was hidden
  15455. (eval-after-load "saveplace"
  15456. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15457. "Make the position visible."
  15458. (org-bookmark-jump-unhide)))
  15459. (defun org-bookmark-jump-unhide ()
  15460. "Unhide the current position, to show the bookmark location."
  15461. (and (org-mode-p)
  15462. (or (org-invisible-p)
  15463. (save-excursion (goto-char (max (point-min) (1- (point))))
  15464. (org-invisible-p)))
  15465. (org-show-context 'bookmark-jump)))
  15466. ;; Make session.el ignore our circular variable
  15467. (eval-after-load "session"
  15468. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15469. ;;;; Experimental code
  15470. (defun org-closed-in-range ()
  15471. "Sparse tree of items closed in a certain time range.
  15472. Still experimental, may disappear in the future."
  15473. (interactive)
  15474. ;; Get the time interval from the user.
  15475. (let* ((time1 (time-to-seconds
  15476. (org-read-date nil 'to-time nil "Starting date: ")))
  15477. (time2 (time-to-seconds
  15478. (org-read-date nil 'to-time nil "End date:")))
  15479. ;; callback function
  15480. (callback (lambda ()
  15481. (let ((time
  15482. (time-to-seconds
  15483. (apply 'encode-time
  15484. (org-parse-time-string
  15485. (match-string 1))))))
  15486. ;; check if time in interval
  15487. (and (>= time time1) (<= time time2))))))
  15488. ;; make tree, check each match with the callback
  15489. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15490. ;;;; Finish up
  15491. (provide 'org)
  15492. (run-hooks 'org-load-hook)
  15493. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15494. ;;; org.el ends here