org.el 630 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913
  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. "List of property/value pairs that can be inherited by any entry.
  2156. These are fixed values, for the preset properties. The user variable
  2157. that can be used to add to this list is `org-global-properties'.
  2158. The entries in this list are cons cells where the car is a property
  2159. name and cdr is a string with the value. If the value represents
  2160. multiple items like an \"_ALL\" property, separate the items by
  2161. spaces.")
  2162. (defcustom org-global-properties nil
  2163. "List of property/value pairs that can be inherited by any entry.
  2164. This list will be combined with the constant `org-global-properties-fixed'.
  2165. The entries in this list are cons cells where the car is a property
  2166. name and cdr is a string with the value.
  2167. You can set buffer-local values for the same purpose in the variable
  2168. `org-file-properties' this by adding lines like
  2169. #+PROPERTY: NAME VALUE"
  2170. :group 'org-properties
  2171. :type '(repeat
  2172. (cons (string :tag "Property")
  2173. (string :tag "Value"))))
  2174. (defvar org-file-properties nil
  2175. "List of property/value pairs that can be inherited by any entry.
  2176. Valid for the current buffer.
  2177. This variable is populated from #+PROPERTY lines.")
  2178. (make-variable-buffer-local 'org-file-properties)
  2179. (defgroup org-agenda nil
  2180. "Options concerning agenda views in Org-mode."
  2181. :tag "Org Agenda"
  2182. :group 'org)
  2183. (defvar org-category nil
  2184. "Variable used by org files to set a category for agenda display.
  2185. Such files should use a file variable to set it, for example
  2186. # -*- mode: org; org-category: \"ELisp\"
  2187. or contain a special line
  2188. #+CATEGORY: ELisp
  2189. If the file does not specify a category, then file's base name
  2190. is used instead.")
  2191. (make-variable-buffer-local 'org-category)
  2192. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2193. (defcustom org-agenda-files nil
  2194. "The files to be used for agenda display.
  2195. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2196. \\[org-remove-file]. You can also use customize to edit the list.
  2197. If an entry is a directory, all files in that directory that are matched by
  2198. `org-agenda-file-regexp' will be part of the file list.
  2199. If the value of the variable is not a list but a single file name, then
  2200. the list of agenda files is actually stored and maintained in that file, one
  2201. agenda file per line."
  2202. :group 'org-agenda
  2203. :type '(choice
  2204. (repeat :tag "List of files and directories" file)
  2205. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2206. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2207. "Regular expression to match files for `org-agenda-files'.
  2208. If any element in the list in that variable contains a directory instead
  2209. of a normal file, all files in that directory that are matched by this
  2210. regular expression will be included."
  2211. :group 'org-agenda
  2212. :type 'regexp)
  2213. (defcustom org-agenda-text-search-extra-files nil
  2214. "List of extra files to be searched by text search commands.
  2215. These files will be search in addition to the agenda files by the
  2216. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2217. Note that these files will only be searched for text search commands,
  2218. not for the other agenda views like todo lists, tag searches or the weekly
  2219. agenda. This variable is intended to list notes and possibly archive files
  2220. that should also be searched by these two commands.
  2221. In fact, if the first element in the list is the symbol `agenda-archives',
  2222. than all archive files of all agenda files will be added to the search
  2223. scope."
  2224. :group 'org-agenda
  2225. :type '(set :greedy t
  2226. (const :tag "Agenda Archives" agenda-archives)
  2227. (repeat :inline t (file))))
  2228. (if (fboundp 'defvaralias)
  2229. (defvaralias 'org-agenda-multi-occur-extra-files
  2230. 'org-agenda-text-search-extra-files))
  2231. (defcustom org-agenda-skip-unavailable-files nil
  2232. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2233. A nil value means to remove them, after a query, from the list."
  2234. :group 'org-agenda
  2235. :type 'boolean)
  2236. (defcustom org-calendar-to-agenda-key [?c]
  2237. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2238. The command `org-calendar-goto-agenda' will be bound to this key. The
  2239. default is the character `c' because then `c' can be used to switch back and
  2240. forth between agenda and calendar."
  2241. :group 'org-agenda
  2242. :type 'sexp)
  2243. (defcustom org-calendar-agenda-action-key [?k]
  2244. "The key to be installed in `calendar-mode-map' for agenda-action.
  2245. The command `org-agenda-action' will be bound to this key. The
  2246. default is the character `k' because we use the same key in the agenda."
  2247. :group 'org-agenda
  2248. :type 'sexp)
  2249. (eval-after-load "calendar"
  2250. '(progn
  2251. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2252. 'org-calendar-goto-agenda)
  2253. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2254. 'org-agenda-action)))
  2255. (defgroup org-latex nil
  2256. "Options for embedding LaTeX code into Org-mode."
  2257. :tag "Org LaTeX"
  2258. :group 'org)
  2259. (defcustom org-format-latex-options
  2260. '(:foreground default :background default :scale 1.0
  2261. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2262. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2263. "Options for creating images from LaTeX fragments.
  2264. This is a property list with the following properties:
  2265. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2266. `default' means use the foreground of the default face.
  2267. :background the background color, or \"Transparent\".
  2268. `default' means use the background of the default face.
  2269. :scale a scaling factor for the size of the images.
  2270. :html-foreground, :html-background, :html-scale
  2271. the same numbers for HTML export.
  2272. :matchers a list indicating which matchers should be used to
  2273. find LaTeX fragments. Valid members of this list are:
  2274. \"begin\" find environments
  2275. \"$1\" find single characters surrounded by $.$
  2276. \"$\" find math expressions surrounded by $...$
  2277. \"$$\" find math expressions surrounded by $$....$$
  2278. \"\\(\" find math expressions surrounded by \\(...\\)
  2279. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2280. :group 'org-latex
  2281. :type 'plist)
  2282. (defcustom org-format-latex-header "\\documentclass{article}
  2283. \\usepackage{fullpage} % do not remove
  2284. \\usepackage{amssymb}
  2285. \\usepackage[usenames]{color}
  2286. \\usepackage{amsmath}
  2287. \\usepackage{latexsym}
  2288. \\usepackage[mathscr]{eucal}
  2289. \\pagestyle{empty} % do not remove"
  2290. "The document header used for processing LaTeX fragments."
  2291. :group 'org-latex
  2292. :type 'string)
  2293. (defgroup org-font-lock nil
  2294. "Font-lock settings for highlighting in Org-mode."
  2295. :tag "Org Font Lock"
  2296. :group 'org)
  2297. (defcustom org-level-color-stars-only nil
  2298. "Non-nil means fontify only the stars in each headline.
  2299. When nil, the entire headline is fontified.
  2300. Changing it requires restart of `font-lock-mode' to become effective
  2301. also in regions already fontified."
  2302. :group 'org-font-lock
  2303. :type 'boolean)
  2304. (defcustom org-hide-leading-stars nil
  2305. "Non-nil means, hide the first N-1 stars in a headline.
  2306. This works by using the face `org-hide' for these stars. This
  2307. face is white for a light background, and black for a dark
  2308. background. You may have to customize the face `org-hide' to
  2309. make this work.
  2310. Changing it requires restart of `font-lock-mode' to become effective
  2311. also in regions already fontified.
  2312. You may also set this on a per-file basis by adding one of the following
  2313. lines to the buffer:
  2314. #+STARTUP: hidestars
  2315. #+STARTUP: showstars"
  2316. :group 'org-font-lock
  2317. :type 'boolean)
  2318. (defcustom org-fontify-done-headline nil
  2319. "Non-nil means, change the face of a headline if it is marked DONE.
  2320. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2321. When this is non-nil, the headline after the keyword is set to the
  2322. `org-headline-done' as an additional indication."
  2323. :group 'org-font-lock
  2324. :type 'boolean)
  2325. (defcustom org-fontify-emphasized-text t
  2326. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2327. Changing this variable requires a restart of Emacs to take effect."
  2328. :group 'org-font-lock
  2329. :type 'boolean)
  2330. (defcustom org-highlight-latex-fragments-and-specials nil
  2331. "Non-nil means, fontify what is treated specially by the exporters."
  2332. :group 'org-font-lock
  2333. :type 'boolean)
  2334. (defcustom org-hide-emphasis-markers nil
  2335. "Non-nil mean font-lock should hide the emphasis marker characters."
  2336. :group 'org-font-lock
  2337. :type 'boolean)
  2338. (defvar org-emph-re nil
  2339. "Regular expression for matching emphasis.")
  2340. (defvar org-verbatim-re nil
  2341. "Regular expression for matching verbatim text.")
  2342. (defvar org-emphasis-regexp-components) ; defined just below
  2343. (defvar org-emphasis-alist) ; defined just below
  2344. (defun org-set-emph-re (var val)
  2345. "Set variable and compute the emphasis regular expression."
  2346. (set var val)
  2347. (when (and (boundp 'org-emphasis-alist)
  2348. (boundp 'org-emphasis-regexp-components)
  2349. org-emphasis-alist org-emphasis-regexp-components)
  2350. (let* ((e org-emphasis-regexp-components)
  2351. (pre (car e))
  2352. (post (nth 1 e))
  2353. (border (nth 2 e))
  2354. (body (nth 3 e))
  2355. (nl (nth 4 e))
  2356. (body1 (concat body "*?"))
  2357. (markers (mapconcat 'car org-emphasis-alist ""))
  2358. (vmarkers (mapconcat
  2359. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2360. org-emphasis-alist "")))
  2361. ;; make sure special characters appear at the right position in the class
  2362. (if (string-match "\\^" markers)
  2363. (setq markers (concat (replace-match "" t t markers) "^")))
  2364. (if (string-match "-" markers)
  2365. (setq markers (concat (replace-match "" t t markers) "-")))
  2366. (if (string-match "\\^" vmarkers)
  2367. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2368. (if (string-match "-" vmarkers)
  2369. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2370. (if (> nl 0)
  2371. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2372. (int-to-string nl) "\\}")))
  2373. ;; Make the regexp
  2374. (setq org-emph-re
  2375. (concat "\\([" pre "]\\|^\\)"
  2376. "\\("
  2377. "\\([" markers "]\\)"
  2378. "\\("
  2379. "[^" border "]\\|"
  2380. "[^" border "]"
  2381. body1
  2382. "[^" border "]"
  2383. "\\)"
  2384. "\\3\\)"
  2385. "\\([" post "]\\|$\\)"))
  2386. (setq org-verbatim-re
  2387. (concat "\\([" pre "]\\|^\\)"
  2388. "\\("
  2389. "\\([" vmarkers "]\\)"
  2390. "\\("
  2391. "[^" border "]\\|"
  2392. "[^" border "]"
  2393. body1
  2394. "[^" border "]"
  2395. "\\)"
  2396. "\\3\\)"
  2397. "\\([" post "]\\|$\\)")))))
  2398. (defcustom org-emphasis-regexp-components
  2399. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2400. "Components used to build the regular expression for emphasis.
  2401. This is a list with 6 entries. Terminology: In an emphasis string
  2402. like \" *strong word* \", we call the initial space PREMATCH, the final
  2403. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2404. and \"trong wor\" is the body. The different components in this variable
  2405. specify what is allowed/forbidden in each part:
  2406. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2407. post Chars allowed as postmatch. End of line will be allowed too.
  2408. border The chars *forbidden* as border characters.
  2409. body-regexp A regexp like \".\" to match a body character. Don't use
  2410. non-shy groups here, and don't allow newline here.
  2411. newline The maximum number of newlines allowed in an emphasis exp.
  2412. Use customize to modify this, or restart Emacs after changing it."
  2413. :group 'org-font-lock
  2414. :set 'org-set-emph-re
  2415. :type '(list
  2416. (sexp :tag "Allowed chars in pre ")
  2417. (sexp :tag "Allowed chars in post ")
  2418. (sexp :tag "Forbidden chars in border ")
  2419. (sexp :tag "Regexp for body ")
  2420. (integer :tag "number of newlines allowed")
  2421. (option (boolean :tag "Please ignore this button"))))
  2422. (defcustom org-emphasis-alist
  2423. `(("*" bold "<b>" "</b>")
  2424. ("/" italic "<i>" "</i>")
  2425. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2426. ("=" org-code "<code>" "</code>" verbatim)
  2427. ("~" org-verbatim "<code>" "</code>" verbatim)
  2428. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2429. "<del>" "</del>")
  2430. )
  2431. "Special syntax for emphasized text.
  2432. Text starting and ending with a special character will be emphasized, for
  2433. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2434. characters, the face to be used by font-lock for highlighting in Org-mode
  2435. Emacs buffers, and the HTML tags to be used for this.
  2436. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2437. Use customize to modify this, or restart Emacs after changing it."
  2438. :group 'org-font-lock
  2439. :set 'org-set-emph-re
  2440. :type '(repeat
  2441. (list
  2442. (string :tag "Marker character")
  2443. (choice
  2444. (face :tag "Font-lock-face")
  2445. (plist :tag "Face property list"))
  2446. (string :tag "HTML start tag")
  2447. (string :tag "HTML end tag")
  2448. (option (const verbatim)))))
  2449. ;;; Miscellaneous options
  2450. (defgroup org-completion nil
  2451. "Completion in Org-mode."
  2452. :tag "Org Completion"
  2453. :group 'org)
  2454. (defcustom org-completion-use-ido nil
  2455. "Non-nil means, use ido completion wherever possible.
  2456. Note that `ido-mode' must be active for this variable to be relevant.
  2457. If you decide to turn this variable on, you might well want to turn off
  2458. `org-outline-path-complete-in-steps'."
  2459. :group 'org-completion
  2460. :type 'boolean)
  2461. (defcustom org-completion-fallback-command 'hippie-expand
  2462. "The expansion command called by \\[org-complete] in normal context.
  2463. Normal means, no org-mode-specific context."
  2464. :group 'org-completion
  2465. :type 'function)
  2466. ;;; Functions and variables from ther packages
  2467. ;; Declared here to avoid compiler warnings
  2468. ;; XEmacs only
  2469. (defvar outline-mode-menu-heading)
  2470. (defvar outline-mode-menu-show)
  2471. (defvar outline-mode-menu-hide)
  2472. (defvar zmacs-regions) ; XEmacs regions
  2473. ;; Emacs only
  2474. (defvar mark-active)
  2475. ;; Various packages
  2476. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2477. (declare-function calendar-forward-day "cal-move" (arg))
  2478. (declare-function calendar-goto-date "cal-move" (date))
  2479. (declare-function calendar-goto-today "cal-move" ())
  2480. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2481. (defvar calc-embedded-close-formula)
  2482. (defvar calc-embedded-open-formula)
  2483. (declare-function cdlatex-tab "ext:cdlatex" ())
  2484. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2485. (defvar font-lock-unfontify-region-function)
  2486. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2487. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2488. (defvar iswitchb-temp-buflist)
  2489. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2490. (defvar org-agenda-tags-todo-honor-ignore-options)
  2491. (declare-function org-agenda-skip "org-agenda" ())
  2492. (declare-function org-format-agenda-item "org-agenda"
  2493. (extra txt &optional category tags dotime noprefix remove-re))
  2494. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2495. (declare-function org-agenda-change-all-lines "org-agenda"
  2496. (newhead hdmarker &optional fixface just-this))
  2497. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2498. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2499. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2500. (beg end))
  2501. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2502. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2503. "org-agenda" (&optional end))
  2504. (declare-function parse-time-string "parse-time" (string))
  2505. (declare-function remember "remember" (&optional initial))
  2506. (declare-function remember-buffer-desc "remember" ())
  2507. (declare-function remember-finalize "remember" ())
  2508. (defvar remember-save-after-remembering)
  2509. (defvar remember-data-file)
  2510. (defvar remember-register)
  2511. (defvar remember-buffer)
  2512. (defvar remember-handler-functions)
  2513. (defvar remember-annotation-functions)
  2514. (defvar texmathp-why)
  2515. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2516. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2517. (defvar w3m-current-url)
  2518. (defvar w3m-current-title)
  2519. (defvar org-latex-regexps)
  2520. ;;; Autoload and prepare some org modules
  2521. ;; Some table stuff that needs to be defined here, because it is used
  2522. ;; by the functions setting up org-mode or checking for table context.
  2523. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2524. "Detects an org-type or table-type table.")
  2525. (defconst org-table-line-regexp "^[ \t]*|"
  2526. "Detects an org-type table line.")
  2527. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2528. "Detects an org-type table line.")
  2529. (defconst org-table-hline-regexp "^[ \t]*|-"
  2530. "Detects an org-type table hline.")
  2531. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2532. "Detects a table-type table hline.")
  2533. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2534. "Searching from within a table (any type) this finds the first line
  2535. outside the table.")
  2536. ;; Autoload the functions in org-table.el that are needed by functions here.
  2537. (eval-and-compile
  2538. (org-autoload "org-table"
  2539. '(org-table-align org-table-begin org-table-blank-field
  2540. org-table-convert org-table-convert-region org-table-copy-down
  2541. org-table-copy-region org-table-create
  2542. org-table-create-or-convert-from-region
  2543. org-table-create-with-table.el org-table-current-dline
  2544. org-table-cut-region org-table-delete-column org-table-edit-field
  2545. org-table-edit-formulas org-table-end org-table-eval-formula
  2546. org-table-export org-table-field-info
  2547. org-table-get-stored-formulas org-table-goto-column
  2548. org-table-hline-and-move org-table-import org-table-insert-column
  2549. org-table-insert-hline org-table-insert-row org-table-iterate
  2550. org-table-justify-field-maybe org-table-kill-row
  2551. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2552. org-table-move-column org-table-move-column-left
  2553. org-table-move-column-right org-table-move-row
  2554. org-table-move-row-down org-table-move-row-up
  2555. org-table-next-field org-table-next-row org-table-paste-rectangle
  2556. org-table-previous-field org-table-recalculate
  2557. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2558. org-table-toggle-coordinate-overlays
  2559. org-table-toggle-formula-debugger org-table-wrap-region
  2560. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2561. (defun org-at-table-p (&optional table-type)
  2562. "Return t if the cursor is inside an org-type table.
  2563. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2564. (if org-enable-table-editor
  2565. (save-excursion
  2566. (beginning-of-line 1)
  2567. (looking-at (if table-type org-table-any-line-regexp
  2568. org-table-line-regexp)))
  2569. nil))
  2570. (defsubst org-table-p () (org-at-table-p))
  2571. (defun org-at-table.el-p ()
  2572. "Return t if and only if we are at a table.el table."
  2573. (and (org-at-table-p 'any)
  2574. (save-excursion
  2575. (goto-char (org-table-begin 'any))
  2576. (looking-at org-table1-hline-regexp))))
  2577. (defun org-table-recognize-table.el ()
  2578. "If there is a table.el table nearby, recognize it and move into it."
  2579. (if org-table-tab-recognizes-table.el
  2580. (if (org-at-table.el-p)
  2581. (progn
  2582. (beginning-of-line 1)
  2583. (if (looking-at org-table-dataline-regexp)
  2584. nil
  2585. (if (looking-at org-table1-hline-regexp)
  2586. (progn
  2587. (beginning-of-line 2)
  2588. (if (looking-at org-table-any-border-regexp)
  2589. (beginning-of-line -1)))))
  2590. (if (re-search-forward "|" (org-table-end t) t)
  2591. (progn
  2592. (require 'table)
  2593. (if (table--at-cell-p (point))
  2594. t
  2595. (message "recognizing table.el table...")
  2596. (table-recognize-table)
  2597. (message "recognizing table.el table...done")))
  2598. (error "This should not happen..."))
  2599. t)
  2600. nil)
  2601. nil))
  2602. (defun org-at-table-hline-p ()
  2603. "Return t if the cursor is inside a hline in a table."
  2604. (if org-enable-table-editor
  2605. (save-excursion
  2606. (beginning-of-line 1)
  2607. (looking-at org-table-hline-regexp))
  2608. nil))
  2609. (defvar org-table-clean-did-remove-column nil)
  2610. (defun org-table-map-tables (function)
  2611. "Apply FUNCTION to the start of all tables in the buffer."
  2612. (save-excursion
  2613. (save-restriction
  2614. (widen)
  2615. (goto-char (point-min))
  2616. (while (re-search-forward org-table-any-line-regexp nil t)
  2617. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2618. (beginning-of-line 1)
  2619. (if (looking-at org-table-line-regexp)
  2620. (save-excursion (funcall function)))
  2621. (re-search-forward org-table-any-border-regexp nil 1))))
  2622. (message "Mapping tables: done"))
  2623. ;; Declare and autoload functions from org-exp.el & Co
  2624. (declare-function org-default-export-plist "org-exp")
  2625. (declare-function org-infile-export-plist "org-exp")
  2626. (declare-function org-get-current-options "org-exp")
  2627. (eval-and-compile
  2628. (org-autoload "org-exp"
  2629. '(org-export org-export-visible
  2630. org-insert-export-options-template
  2631. org-table-clean-before-export))
  2632. (org-autoload "org-ascii"
  2633. '(org-export-as-ascii org-export-ascii-preprocess
  2634. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2635. org-export-region-as-ascii))
  2636. (org-autoload "org-html"
  2637. '(org-export-as-html-and-open
  2638. org-export-as-html-batch org-export-as-html-to-buffer
  2639. org-replace-region-by-html org-export-region-as-html
  2640. org-export-as-html))
  2641. (org-autoload "org-icalendar"
  2642. '(org-export-icalendar-this-file
  2643. org-export-icalendar-all-agenda-files
  2644. org-export-icalendar-combine-agenda-files))
  2645. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2646. ;; Declare and autoload functions from org-agenda.el
  2647. (eval-and-compile
  2648. (org-autoload "org-agenda"
  2649. '(org-agenda org-agenda-list org-search-view
  2650. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2651. org-diary org-agenda-to-appt
  2652. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2653. ;; Autoload org-remember
  2654. (eval-and-compile
  2655. (org-autoload "org-remember"
  2656. '(org-remember-insinuate org-remember-annotation
  2657. org-remember-apply-template org-remember org-remember-handler)))
  2658. ;; Autoload org-clock.el
  2659. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2660. (beg end))
  2661. (declare-function org-clock-update-mode-line "org-clock" ())
  2662. (defvar org-clock-start-time)
  2663. (defvar org-clock-marker (make-marker)
  2664. "Marker recording the last clock-in.")
  2665. (eval-and-compile
  2666. (org-autoload
  2667. "org-clock"
  2668. '(org-clock-in org-clock-out org-clock-cancel
  2669. org-clock-goto org-clock-sum org-clock-display
  2670. org-clock-remove-overlays org-clock-report
  2671. org-clocktable-shift org-dblock-write:clocktable
  2672. org-get-clocktable)))
  2673. (defun org-clock-update-time-maybe ()
  2674. "If this is a CLOCK line, update it and return t.
  2675. Otherwise, return nil."
  2676. (interactive)
  2677. (save-excursion
  2678. (beginning-of-line 1)
  2679. (skip-chars-forward " \t")
  2680. (when (looking-at org-clock-string)
  2681. (let ((re (concat "[ \t]*" org-clock-string
  2682. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2683. "\\([ \t]*=>.*\\)?\\)?"))
  2684. ts te h m s neg)
  2685. (cond
  2686. ((not (looking-at re))
  2687. nil)
  2688. ((not (match-end 2))
  2689. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2690. (> org-clock-marker (point))
  2691. (<= org-clock-marker (point-at-eol)))
  2692. ;; The clock is running here
  2693. (setq org-clock-start-time
  2694. (apply 'encode-time
  2695. (org-parse-time-string (match-string 1))))
  2696. (org-clock-update-mode-line)))
  2697. (t
  2698. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2699. (end-of-line 1)
  2700. (setq ts (match-string 1)
  2701. te (match-string 3))
  2702. (setq s (- (time-to-seconds
  2703. (apply 'encode-time (org-parse-time-string te)))
  2704. (time-to-seconds
  2705. (apply 'encode-time (org-parse-time-string ts))))
  2706. neg (< s 0)
  2707. s (abs s)
  2708. h (floor (/ s 3600))
  2709. s (- s (* 3600 h))
  2710. m (floor (/ s 60))
  2711. s (- s (* 60 s)))
  2712. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2713. t))))))
  2714. (defun org-check-running-clock ()
  2715. "Check if the current buffer contains the running clock.
  2716. If yes, offer to stop it and to save the buffer with the changes."
  2717. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2718. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2719. (buffer-name))))
  2720. (org-clock-out)
  2721. (when (y-or-n-p "Save changed buffer?")
  2722. (save-buffer))))
  2723. (defun org-clocktable-try-shift (dir n)
  2724. "Check if this line starts a clock table, if yes, shift the time block."
  2725. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2726. (org-clocktable-shift dir n)))
  2727. ;; Autoload org-timer.el
  2728. (eval-and-compile
  2729. (org-autoload
  2730. "org-timer"
  2731. '(org-timer-start org-timer org-timer-item
  2732. org-timer-change-times-in-region)))
  2733. ;; Autoload org-feed.el
  2734. (eval-and-compile
  2735. (org-autoload
  2736. "org-feed"
  2737. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2738. ;; Autoload archiving code
  2739. ;; The stuff that is needed for cycling and tags has to be defined here.
  2740. (defgroup org-archive nil
  2741. "Options concerning archiving in Org-mode."
  2742. :tag "Org Archive"
  2743. :group 'org-structure)
  2744. (defcustom org-archive-location "%s_archive::"
  2745. "The location where subtrees should be archived.
  2746. The value of this variable is a string, consisting of two parts,
  2747. separated by a double-colon. The first part is a filename and
  2748. the second part is a headline.
  2749. When the filename is omitted, archiving happens in the same file.
  2750. %s in the filename will be replaced by the current file
  2751. name (without the directory part). Archiving to a different file
  2752. is useful to keep archived entries from contributing to the
  2753. Org-mode Agenda.
  2754. The archived entries will be filed as subtrees of the specified
  2755. headline. When the headline is omitted, the subtrees are simply
  2756. filed away at the end of the file, as top-level entries. Also in
  2757. the heading you can use %s to represent the file name, this can be
  2758. useful when using the same archive for a number of different files.
  2759. Here are a few examples:
  2760. \"%s_archive::\"
  2761. If the current file is Projects.org, archive in file
  2762. Projects.org_archive, as top-level trees. This is the default.
  2763. \"::* Archived Tasks\"
  2764. Archive in the current file, under the top-level headline
  2765. \"* Archived Tasks\".
  2766. \"~/org/archive.org::\"
  2767. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2768. \"~/org/archive.org::From %s\"
  2769. Archive in file ~/org/archive.org (absolute path), und headlines
  2770. \"From FILENAME\" where file name is the current file name.
  2771. \"basement::** Finished Tasks\"
  2772. Archive in file ./basement (relative path), as level 3 trees
  2773. below the level 2 heading \"** Finished Tasks\".
  2774. You may set this option on a per-file basis by adding to the buffer a
  2775. line like
  2776. #+ARCHIVE: basement::** Finished Tasks
  2777. You may also define it locally for a subtree by setting an ARCHIVE property
  2778. in the entry. If such a property is found in an entry, or anywhere up
  2779. the hierarchy, it will be used."
  2780. :group 'org-archive
  2781. :type 'string)
  2782. (defcustom org-archive-tag "ARCHIVE"
  2783. "The tag that marks a subtree as archived.
  2784. An archived subtree does not open during visibility cycling, and does
  2785. not contribute to the agenda listings.
  2786. After changing this, font-lock must be restarted in the relevant buffers to
  2787. get the proper fontification."
  2788. :group 'org-archive
  2789. :group 'org-keywords
  2790. :type 'string)
  2791. (defcustom org-agenda-skip-archived-trees t
  2792. "Non-nil means, the agenda will skip any items located in archived trees.
  2793. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2794. variable is no longer recommended, you should leave it at the value t.
  2795. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2796. :group 'org-archive
  2797. :group 'org-agenda-skip
  2798. :type 'boolean)
  2799. (defcustom org-columns-skip-arrchived-trees t
  2800. "Non-nil means, irgnore archived trees when creating column view."
  2801. :group 'org-archive
  2802. :group 'org-properties
  2803. :type 'boolean)
  2804. (defcustom org-cycle-open-archived-trees nil
  2805. "Non-nil means, `org-cycle' will open archived trees.
  2806. An archived tree is a tree marked with the tag ARCHIVE.
  2807. When nil, archived trees will stay folded. You can still open them with
  2808. normal outline commands like `show-all', but not with the cycling commands."
  2809. :group 'org-archive
  2810. :group 'org-cycle
  2811. :type 'boolean)
  2812. (defcustom org-sparse-tree-open-archived-trees nil
  2813. "Non-nil means sparse tree construction shows matches in archived trees.
  2814. When nil, matches in these trees are highlighted, but the trees are kept in
  2815. collapsed state."
  2816. :group 'org-archive
  2817. :group 'org-sparse-trees
  2818. :type 'boolean)
  2819. (defun org-cycle-hide-archived-subtrees (state)
  2820. "Re-hide all archived subtrees after a visibility state change."
  2821. (when (and (not org-cycle-open-archived-trees)
  2822. (not (memq state '(overview folded))))
  2823. (save-excursion
  2824. (let* ((globalp (memq state '(contents all)))
  2825. (beg (if globalp (point-min) (point)))
  2826. (end (if globalp (point-max) (org-end-of-subtree t))))
  2827. (org-hide-archived-subtrees beg end)
  2828. (goto-char beg)
  2829. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2830. (message "%s" (substitute-command-keys
  2831. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2832. (defun org-force-cycle-archived ()
  2833. "Cycle subtree even if it is archived."
  2834. (interactive)
  2835. (setq this-command 'org-cycle)
  2836. (let ((org-cycle-open-archived-trees t))
  2837. (call-interactively 'org-cycle)))
  2838. (defun org-hide-archived-subtrees (beg end)
  2839. "Re-hide all archived subtrees after a visibility state change."
  2840. (save-excursion
  2841. (let* ((re (concat ":" org-archive-tag ":")))
  2842. (goto-char beg)
  2843. (while (re-search-forward re end t)
  2844. (and (org-on-heading-p) (hide-subtree))
  2845. (org-end-of-subtree t)))))
  2846. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2847. (eval-and-compile
  2848. (org-autoload "org-archive"
  2849. '(org-add-archive-files org-archive-subtree
  2850. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2851. ;; Autoload Column View Code
  2852. (declare-function org-columns-number-to-string "org-colview")
  2853. (declare-function org-columns-get-format-and-top-level "org-colview")
  2854. (declare-function org-columns-compute "org-colview")
  2855. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2856. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2857. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2858. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2859. ;; Autoload ID code
  2860. (declare-function org-id-store-link "org-id")
  2861. (declare-function org-id-locations-load "org-id")
  2862. (declare-function org-id-locations-save "org-id")
  2863. (defvar org-id-track-globally)
  2864. (org-autoload "org-id"
  2865. '(org-id-get-create org-id-new org-id-copy org-id-get
  2866. org-id-get-with-outline-path-completion
  2867. org-id-get-with-outline-drilling
  2868. org-id-goto org-id-find org-id-store-link))
  2869. ;; Autoload Plotting Code
  2870. (org-autoload "org-plot"
  2871. '(org-plot/gnuplot))
  2872. ;;; Variables for pre-computed regular expressions, all buffer local
  2873. (defvar org-drawer-regexp nil
  2874. "Matches first line of a hidden block.")
  2875. (make-variable-buffer-local 'org-drawer-regexp)
  2876. (defvar org-todo-regexp nil
  2877. "Matches any of the TODO state keywords.")
  2878. (make-variable-buffer-local 'org-todo-regexp)
  2879. (defvar org-not-done-regexp nil
  2880. "Matches any of the TODO state keywords except the last one.")
  2881. (make-variable-buffer-local 'org-not-done-regexp)
  2882. (defvar org-not-done-heading-regexp nil
  2883. "Matches a TODO headline that is not done.")
  2884. (make-variable-buffer-local 'org-not-done-regexp)
  2885. (defvar org-todo-line-regexp nil
  2886. "Matches a headline and puts TODO state into group 2 if present.")
  2887. (make-variable-buffer-local 'org-todo-line-regexp)
  2888. (defvar org-complex-heading-regexp nil
  2889. "Matches a headline and puts everything into groups:
  2890. group 1: the stars
  2891. group 2: The todo keyword, maybe
  2892. group 3: Priority cookie
  2893. group 4: True headline
  2894. group 5: Tags")
  2895. (make-variable-buffer-local 'org-complex-heading-regexp)
  2896. (defvar org-todo-line-tags-regexp nil
  2897. "Matches a headline and puts TODO state into group 2 if present.
  2898. Also put tags into group 4 if tags are present.")
  2899. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2900. (defvar org-nl-done-regexp nil
  2901. "Matches newline followed by a headline with the DONE keyword.")
  2902. (make-variable-buffer-local 'org-nl-done-regexp)
  2903. (defvar org-looking-at-done-regexp nil
  2904. "Matches the DONE keyword a point.")
  2905. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2906. (defvar org-ds-keyword-length 12
  2907. "Maximum length of the Deadline and SCHEDULED keywords.")
  2908. (make-variable-buffer-local 'org-ds-keyword-length)
  2909. (defvar org-deadline-regexp nil
  2910. "Matches the DEADLINE keyword.")
  2911. (make-variable-buffer-local 'org-deadline-regexp)
  2912. (defvar org-deadline-time-regexp nil
  2913. "Matches the DEADLINE keyword together with a time stamp.")
  2914. (make-variable-buffer-local 'org-deadline-time-regexp)
  2915. (defvar org-deadline-line-regexp nil
  2916. "Matches the DEADLINE keyword and the rest of the line.")
  2917. (make-variable-buffer-local 'org-deadline-line-regexp)
  2918. (defvar org-scheduled-regexp nil
  2919. "Matches the SCHEDULED keyword.")
  2920. (make-variable-buffer-local 'org-scheduled-regexp)
  2921. (defvar org-scheduled-time-regexp nil
  2922. "Matches the SCHEDULED keyword together with a time stamp.")
  2923. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2924. (defvar org-closed-time-regexp nil
  2925. "Matches the CLOSED keyword together with a time stamp.")
  2926. (make-variable-buffer-local 'org-closed-time-regexp)
  2927. (defvar org-keyword-time-regexp nil
  2928. "Matches any of the 4 keywords, together with the time stamp.")
  2929. (make-variable-buffer-local 'org-keyword-time-regexp)
  2930. (defvar org-keyword-time-not-clock-regexp nil
  2931. "Matches any of the 3 keywords, together with the time stamp.")
  2932. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2933. (defvar org-maybe-keyword-time-regexp nil
  2934. "Matches a timestamp, possibly preceeded by a keyword.")
  2935. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2936. (defvar org-planning-or-clock-line-re nil
  2937. "Matches a line with planning or clock info.")
  2938. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2939. (defconst org-plain-time-of-day-regexp
  2940. (concat
  2941. "\\(\\<[012]?[0-9]"
  2942. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2943. "\\(--?"
  2944. "\\(\\<[012]?[0-9]"
  2945. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2946. "\\)?")
  2947. "Regular expression to match a plain time or time range.
  2948. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2949. groups carry important information:
  2950. 0 the full match
  2951. 1 the first time, range or not
  2952. 8 the second time, if it is a range.")
  2953. (defconst org-plain-time-extension-regexp
  2954. (concat
  2955. "\\(\\<[012]?[0-9]"
  2956. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2957. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2958. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2959. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2960. groups carry important information:
  2961. 0 the full match
  2962. 7 hours of duration
  2963. 9 minutes of duration")
  2964. (defconst org-stamp-time-of-day-regexp
  2965. (concat
  2966. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2967. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2968. "\\(--?"
  2969. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2970. "Regular expression to match a timestamp time or time range.
  2971. After a match, the following groups carry important information:
  2972. 0 the full match
  2973. 1 date plus weekday, for backreferencing to make sure both times on same day
  2974. 2 the first time, range or not
  2975. 4 the second time, if it is a range.")
  2976. (defconst org-startup-options
  2977. '(("fold" org-startup-folded t)
  2978. ("overview" org-startup-folded t)
  2979. ("nofold" org-startup-folded nil)
  2980. ("showall" org-startup-folded nil)
  2981. ("content" org-startup-folded content)
  2982. ("hidestars" org-hide-leading-stars t)
  2983. ("showstars" org-hide-leading-stars nil)
  2984. ("odd" org-odd-levels-only t)
  2985. ("oddeven" org-odd-levels-only nil)
  2986. ("align" org-startup-align-all-tables t)
  2987. ("noalign" org-startup-align-all-tables nil)
  2988. ("customtime" org-display-custom-times t)
  2989. ("logdone" org-log-done time)
  2990. ("lognotedone" org-log-done note)
  2991. ("nologdone" org-log-done nil)
  2992. ("lognoteclock-out" org-log-note-clock-out t)
  2993. ("nolognoteclock-out" org-log-note-clock-out nil)
  2994. ("logrepeat" org-log-repeat state)
  2995. ("lognoterepeat" org-log-repeat note)
  2996. ("nologrepeat" org-log-repeat nil)
  2997. ("fninline" org-footnote-define-inline t)
  2998. ("nofninline" org-footnote-define-inline nil)
  2999. ("fnlocal" org-footnote-section nil)
  3000. ("fnauto" org-footnote-auto-label t)
  3001. ("fnprompt" org-footnote-auto-label nil)
  3002. ("fnconfirm" org-footnote-auto-label confirm)
  3003. ("fnplain" org-footnote-auto-label plain)
  3004. ("constcgs" constants-unit-system cgs)
  3005. ("constSI" constants-unit-system SI)
  3006. ("noptag" org-tag-persistent-alist nil)
  3007. ("hideblocks" org-hide-block-startup t)
  3008. ("nohideblocks" org-hide-block-startup nil))
  3009. "Variable associated with STARTUP options for org-mode.
  3010. Each element is a list of three items: The startup options as written
  3011. in the #+STARTUP line, the corresponding variable, and the value to
  3012. set this variable to if the option is found. An optional forth element PUSH
  3013. means to push this value onto the list in the variable.")
  3014. (defun org-set-regexps-and-options ()
  3015. "Precompute regular expressions for current buffer."
  3016. (when (org-mode-p)
  3017. (org-set-local 'org-todo-kwd-alist nil)
  3018. (org-set-local 'org-todo-key-alist nil)
  3019. (org-set-local 'org-todo-key-trigger nil)
  3020. (org-set-local 'org-todo-keywords-1 nil)
  3021. (org-set-local 'org-done-keywords nil)
  3022. (org-set-local 'org-todo-heads nil)
  3023. (org-set-local 'org-todo-sets nil)
  3024. (org-set-local 'org-todo-log-states nil)
  3025. (org-set-local 'org-file-properties nil)
  3026. (org-set-local 'org-file-tags nil)
  3027. (let ((re (org-make-options-regexp
  3028. '("CATEGORY" "TODO" "COLUMNS"
  3029. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3030. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3031. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3032. (splitre "[ \t]+")
  3033. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3034. tail sep kws1 prio props ftags drawers
  3035. ext-setup-or-nil setup-contents (start 0))
  3036. (save-excursion
  3037. (save-restriction
  3038. (widen)
  3039. (goto-char (point-min))
  3040. (while (or (and ext-setup-or-nil
  3041. (string-match re ext-setup-or-nil start)
  3042. (setq start (match-end 0)))
  3043. (and (setq ext-setup-or-nil nil start 0)
  3044. (re-search-forward re nil t)))
  3045. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3046. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3047. (cond
  3048. ((equal key "CATEGORY")
  3049. (if (string-match "[ \t]+$" value)
  3050. (setq value (replace-match "" t t value)))
  3051. (setq cat value))
  3052. ((member key '("SEQ_TODO" "TODO"))
  3053. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3054. ((equal key "TYP_TODO")
  3055. (push (cons 'type (org-split-string value splitre)) kwds))
  3056. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3057. ;; general TODO-like setup
  3058. (push (cons (intern (downcase (match-string 1 key)))
  3059. (org-split-string value splitre)) kwds))
  3060. ((equal key "TAGS")
  3061. (setq tags (append tags (if tags '("\\n") nil)
  3062. (org-split-string value splitre))))
  3063. ((equal key "COLUMNS")
  3064. (org-set-local 'org-columns-default-format value))
  3065. ((equal key "LINK")
  3066. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3067. (push (cons (match-string 1 value)
  3068. (org-trim (match-string 2 value)))
  3069. links)))
  3070. ((equal key "PRIORITIES")
  3071. (setq prio (org-split-string value " +")))
  3072. ((equal key "PROPERTY")
  3073. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3074. (push (cons (match-string 1 value) (match-string 2 value))
  3075. props)))
  3076. ((equal key "FILETAGS")
  3077. (when (string-match "\\S-" value)
  3078. (setq ftags
  3079. (append
  3080. ftags
  3081. (apply 'append
  3082. (mapcar (lambda (x) (org-split-string x ":"))
  3083. (org-split-string value)))))))
  3084. ((equal key "DRAWERS")
  3085. (setq drawers (org-split-string value splitre)))
  3086. ((equal key "CONSTANTS")
  3087. (setq const (append const (org-split-string value splitre))))
  3088. ((equal key "STARTUP")
  3089. (let ((opts (org-split-string value splitre))
  3090. l var val)
  3091. (while (setq l (pop opts))
  3092. (when (setq l (assoc l org-startup-options))
  3093. (setq var (nth 1 l) val (nth 2 l))
  3094. (if (not (nth 3 l))
  3095. (set (make-local-variable var) val)
  3096. (if (not (listp (symbol-value var)))
  3097. (set (make-local-variable var) nil))
  3098. (set (make-local-variable var) (symbol-value var))
  3099. (add-to-list var val))))))
  3100. ((equal key "ARCHIVE")
  3101. (string-match " *$" value)
  3102. (setq arch (replace-match "" t t value))
  3103. (remove-text-properties 0 (length arch)
  3104. '(face t fontified t) arch))
  3105. ((equal key "SETUPFILE")
  3106. (setq setup-contents (org-file-contents
  3107. (expand-file-name
  3108. (org-remove-double-quotes value))
  3109. 'noerror))
  3110. (if (not ext-setup-or-nil)
  3111. (setq ext-setup-or-nil setup-contents start 0)
  3112. (setq ext-setup-or-nil
  3113. (concat (substring ext-setup-or-nil 0 start)
  3114. "\n" setup-contents "\n"
  3115. (substring ext-setup-or-nil start)))))
  3116. ))))
  3117. (when cat
  3118. (org-set-local 'org-category (intern cat))
  3119. (push (cons "CATEGORY" cat) props))
  3120. (when prio
  3121. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3122. (setq prio (mapcar 'string-to-char prio))
  3123. (org-set-local 'org-highest-priority (nth 0 prio))
  3124. (org-set-local 'org-lowest-priority (nth 1 prio))
  3125. (org-set-local 'org-default-priority (nth 2 prio)))
  3126. (and props (org-set-local 'org-file-properties (nreverse props)))
  3127. (and ftags (org-set-local 'org-file-tags
  3128. (mapcar 'org-add-prop-inherited ftags)))
  3129. (and drawers (org-set-local 'org-drawers drawers))
  3130. (and arch (org-set-local 'org-archive-location arch))
  3131. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3132. ;; Process the TODO keywords
  3133. (unless kwds
  3134. ;; Use the global values as if they had been given locally.
  3135. (setq kwds (default-value 'org-todo-keywords))
  3136. (if (stringp (car kwds))
  3137. (setq kwds (list (cons org-todo-interpretation
  3138. (default-value 'org-todo-keywords)))))
  3139. (setq kwds (reverse kwds)))
  3140. (setq kwds (nreverse kwds))
  3141. (let (inter kws kw)
  3142. (while (setq kws (pop kwds))
  3143. (let ((kws (or
  3144. (run-hook-with-args-until-success
  3145. 'org-todo-setup-filter-hook kws)
  3146. kws)))
  3147. (setq inter (pop kws) sep (member "|" kws)
  3148. kws0 (delete "|" (copy-sequence kws))
  3149. kwsa nil
  3150. kws1 (mapcar
  3151. (lambda (x)
  3152. ;; 1 2
  3153. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3154. (progn
  3155. (setq kw (match-string 1 x)
  3156. key (and (match-end 2) (match-string 2 x))
  3157. log (org-extract-log-state-settings x))
  3158. (push (cons kw (and key (string-to-char key))) kwsa)
  3159. (and log (push log org-todo-log-states))
  3160. kw)
  3161. (error "Invalid TODO keyword %s" x)))
  3162. kws0)
  3163. kwsa (if kwsa (append '((:startgroup))
  3164. (nreverse kwsa)
  3165. '((:endgroup))))
  3166. hw (car kws1)
  3167. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3168. tail (list inter hw (car dws) (org-last dws))))
  3169. (add-to-list 'org-todo-heads hw 'append)
  3170. (push kws1 org-todo-sets)
  3171. (setq org-done-keywords (append org-done-keywords dws nil))
  3172. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3173. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3174. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3175. (setq org-todo-sets (nreverse org-todo-sets)
  3176. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3177. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3178. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3179. ;; Process the constants
  3180. (when const
  3181. (let (e cst)
  3182. (while (setq e (pop const))
  3183. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3184. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3185. (setq org-table-formula-constants-local cst)))
  3186. ;; Process the tags.
  3187. (when tags
  3188. (let (e tgs)
  3189. (while (setq e (pop tags))
  3190. (cond
  3191. ((equal e "{") (push '(:startgroup) tgs))
  3192. ((equal e "}") (push '(:endgroup) tgs))
  3193. ((equal e "\\n") (push '(:newline) tgs))
  3194. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3195. (push (cons (match-string 1 e)
  3196. (string-to-char (match-string 2 e)))
  3197. tgs))
  3198. (t (push (list e) tgs))))
  3199. (org-set-local 'org-tag-alist nil)
  3200. (while (setq e (pop tgs))
  3201. (or (and (stringp (car e))
  3202. (assoc (car e) org-tag-alist))
  3203. (push e org-tag-alist)))))
  3204. ;; Compute the regular expressions and other local variables
  3205. (if (not org-done-keywords)
  3206. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3207. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3208. (length org-scheduled-string)
  3209. (length org-clock-string)
  3210. (length org-closed-string)))
  3211. org-drawer-regexp
  3212. (concat "^[ \t]*:\\("
  3213. (mapconcat 'regexp-quote org-drawers "\\|")
  3214. "\\):[ \t]*$")
  3215. org-not-done-keywords
  3216. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3217. org-todo-regexp
  3218. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3219. "\\|") "\\)\\>")
  3220. org-not-done-regexp
  3221. (concat "\\<\\("
  3222. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3223. "\\)\\>")
  3224. org-not-done-heading-regexp
  3225. (concat "^\\(\\*+\\)[ \t]+\\("
  3226. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3227. "\\)\\>")
  3228. org-todo-line-regexp
  3229. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3230. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3231. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3232. org-complex-heading-regexp
  3233. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3234. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3235. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3236. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3237. org-nl-done-regexp
  3238. (concat "\n\\*+[ \t]+"
  3239. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3240. "\\)" "\\>")
  3241. org-todo-line-tags-regexp
  3242. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3243. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3244. (org-re
  3245. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3246. org-looking-at-done-regexp
  3247. (concat "^" "\\(?:"
  3248. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3249. "\\>")
  3250. org-deadline-regexp (concat "\\<" org-deadline-string)
  3251. org-deadline-time-regexp
  3252. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3253. org-deadline-line-regexp
  3254. (concat "\\<\\(" org-deadline-string "\\).*")
  3255. org-scheduled-regexp
  3256. (concat "\\<" org-scheduled-string)
  3257. org-scheduled-time-regexp
  3258. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3259. org-closed-time-regexp
  3260. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3261. org-keyword-time-regexp
  3262. (concat "\\<\\(" org-scheduled-string
  3263. "\\|" org-deadline-string
  3264. "\\|" org-closed-string
  3265. "\\|" org-clock-string "\\)"
  3266. " *[[<]\\([^]>]+\\)[]>]")
  3267. org-keyword-time-not-clock-regexp
  3268. (concat "\\<\\(" org-scheduled-string
  3269. "\\|" org-deadline-string
  3270. "\\|" org-closed-string
  3271. "\\)"
  3272. " *[[<]\\([^]>]+\\)[]>]")
  3273. org-maybe-keyword-time-regexp
  3274. (concat "\\(\\<\\(" org-scheduled-string
  3275. "\\|" org-deadline-string
  3276. "\\|" org-closed-string
  3277. "\\|" org-clock-string "\\)\\)?"
  3278. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3279. org-planning-or-clock-line-re
  3280. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3281. "\\|" org-deadline-string
  3282. "\\|" org-closed-string "\\|" org-clock-string
  3283. "\\)\\>\\)")
  3284. )
  3285. (org-compute-latex-and-specials-regexp)
  3286. (org-set-font-lock-defaults))))
  3287. (defun org-file-contents (file &optional noerror)
  3288. "Return the contents of FILE, as a string."
  3289. (if (or (not file)
  3290. (not (file-readable-p file)))
  3291. (if noerror
  3292. (progn
  3293. (message "Cannot read file %s" file)
  3294. (ding) (sit-for 2)
  3295. "")
  3296. (error "Cannot read file %s" file))
  3297. (with-temp-buffer
  3298. (insert-file-contents file)
  3299. (buffer-string))))
  3300. (defun org-extract-log-state-settings (x)
  3301. "Extract the log state setting from a TODO keyword string.
  3302. This will extract info from a string like \"WAIT(w@/!)\"."
  3303. (let (kw key log1 log2)
  3304. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3305. (setq kw (match-string 1 x)
  3306. key (and (match-end 2) (match-string 2 x))
  3307. log1 (and (match-end 3) (match-string 3 x))
  3308. log2 (and (match-end 4) (match-string 4 x)))
  3309. (and (or log1 log2)
  3310. (list kw
  3311. (and log1 (if (equal log1 "!") 'time 'note))
  3312. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3313. (defun org-remove-keyword-keys (list)
  3314. "Remove a pair of parenthesis at the end of each string in LIST."
  3315. (mapcar (lambda (x)
  3316. (if (string-match "(.*)$" x)
  3317. (substring x 0 (match-beginning 0))
  3318. x))
  3319. list))
  3320. ;; FIXME: this could be done much better, using second characters etc.
  3321. (defun org-assign-fast-keys (alist)
  3322. "Assign fast keys to a keyword-key alist.
  3323. Respect keys that are already there."
  3324. (let (new e k c c1 c2 (char ?a))
  3325. (while (setq e (pop alist))
  3326. (cond
  3327. ((equal e '(:startgroup)) (push e new))
  3328. ((equal e '(:endgroup)) (push e new))
  3329. ((equal e '(:newline)) (push e new))
  3330. (t
  3331. (setq k (car e) c2 nil)
  3332. (if (cdr e)
  3333. (setq c (cdr e))
  3334. ;; automatically assign a character.
  3335. (setq c1 (string-to-char
  3336. (downcase (substring
  3337. k (if (= (string-to-char k) ?@) 1 0)))))
  3338. (if (or (rassoc c1 new) (rassoc c1 alist))
  3339. (while (or (rassoc char new) (rassoc char alist))
  3340. (setq char (1+ char)))
  3341. (setq c2 c1))
  3342. (setq c (or c2 char)))
  3343. (push (cons k c) new))))
  3344. (nreverse new)))
  3345. ;;; Some variables used in various places
  3346. (defvar org-window-configuration nil
  3347. "Used in various places to store a window configuration.")
  3348. (defvar org-finish-function nil
  3349. "Function to be called when `C-c C-c' is used.
  3350. This is for getting out of special buffers like remember.")
  3351. ;; FIXME: Occasionally check by commenting these, to make sure
  3352. ;; no other functions uses these, forgetting to let-bind them.
  3353. (defvar entry)
  3354. (defvar state)
  3355. (defvar last-state)
  3356. (defvar date)
  3357. (defvar description)
  3358. ;; Defined somewhere in this file, but used before definition.
  3359. (defvar org-html-entities)
  3360. (defvar org-struct-menu)
  3361. (defvar org-org-menu)
  3362. (defvar org-tbl-menu)
  3363. (defvar org-agenda-keymap)
  3364. ;;;; Define the Org-mode
  3365. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3366. (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."))
  3367. ;; We use a before-change function to check if a table might need
  3368. ;; an update.
  3369. (defvar org-table-may-need-update t
  3370. "Indicates that a table might need an update.
  3371. This variable is set by `org-before-change-function'.
  3372. `org-table-align' sets it back to nil.")
  3373. (defun org-before-change-function (beg end)
  3374. "Every change indicates that a table might need an update."
  3375. (setq org-table-may-need-update t))
  3376. (defvar org-mode-map)
  3377. (defvar org-mode-hook nil
  3378. "Mode hook for Org-mode, run after the mode was turned on.")
  3379. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3380. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3381. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3382. (defvar org-table-buffer-is-an nil)
  3383. (defconst org-outline-regexp "\\*+ ")
  3384. ;;;###autoload
  3385. (define-derived-mode org-mode outline-mode "Org"
  3386. "Outline-based notes management and organizer, alias
  3387. \"Carsten's outline-mode for keeping track of everything.\"
  3388. Org-mode develops organizational tasks around a NOTES file which
  3389. contains information about projects as plain text. Org-mode is
  3390. implemented on top of outline-mode, which is ideal to keep the content
  3391. of large files well structured. It supports ToDo items, deadlines and
  3392. time stamps, which magically appear in the diary listing of the Emacs
  3393. calendar. Tables are easily created with a built-in table editor.
  3394. Plain text URL-like links connect to websites, emails (VM), Usenet
  3395. messages (Gnus), BBDB entries, and any files related to the project.
  3396. For printing and sharing of notes, an Org-mode file (or a part of it)
  3397. can be exported as a structured ASCII or HTML file.
  3398. The following commands are available:
  3399. \\{org-mode-map}"
  3400. ;; Get rid of Outline menus, they are not needed
  3401. ;; Need to do this here because define-derived-mode sets up
  3402. ;; the keymap so late. Still, it is a waste to call this each time
  3403. ;; we switch another buffer into org-mode.
  3404. (if (featurep 'xemacs)
  3405. (when (boundp 'outline-mode-menu-heading)
  3406. ;; Assume this is Greg's port, it used easymenu
  3407. (easy-menu-remove outline-mode-menu-heading)
  3408. (easy-menu-remove outline-mode-menu-show)
  3409. (easy-menu-remove outline-mode-menu-hide))
  3410. (define-key org-mode-map [menu-bar headings] 'undefined)
  3411. (define-key org-mode-map [menu-bar hide] 'undefined)
  3412. (define-key org-mode-map [menu-bar show] 'undefined))
  3413. (org-load-modules-maybe)
  3414. (easy-menu-add org-org-menu)
  3415. (easy-menu-add org-tbl-menu)
  3416. (org-install-agenda-files-menu)
  3417. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3418. (org-add-to-invisibility-spec '(org-cwidth))
  3419. (org-add-to-invisibility-spec '(org-hide-block . t))
  3420. (when (featurep 'xemacs)
  3421. (org-set-local 'line-move-ignore-invisible t))
  3422. (org-set-local 'outline-regexp org-outline-regexp)
  3423. (org-set-local 'outline-level 'org-outline-level)
  3424. (when (and org-ellipsis
  3425. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3426. (fboundp 'make-glyph-code))
  3427. (unless org-display-table
  3428. (setq org-display-table (make-display-table)))
  3429. (set-display-table-slot
  3430. org-display-table 4
  3431. (vconcat (mapcar
  3432. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3433. org-ellipsis)))
  3434. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3435. (setq buffer-display-table org-display-table))
  3436. (org-set-regexps-and-options)
  3437. (when (and org-tag-faces (not org-tags-special-faces-re))
  3438. ;; tag faces set outside customize.... force initialization.
  3439. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3440. ;; Calc embedded
  3441. (org-set-local 'calc-embedded-open-mode "# ")
  3442. (modify-syntax-entry ?# "<")
  3443. (modify-syntax-entry ?@ "w")
  3444. (if org-startup-truncated (setq truncate-lines t))
  3445. (org-set-local 'font-lock-unfontify-region-function
  3446. 'org-unfontify-region)
  3447. ;; Activate before-change-function
  3448. (org-set-local 'org-table-may-need-update t)
  3449. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3450. 'local)
  3451. ;; Check for running clock before killing a buffer
  3452. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3453. ;; Paragraphs and auto-filling
  3454. (org-set-autofill-regexps)
  3455. (setq indent-line-function 'org-indent-line-function)
  3456. (org-update-radio-target-regexp)
  3457. ;; Make sure dependence stuff works reliably, even for users who set it
  3458. ;; too late :-(
  3459. (if org-enforce-todo-dependencies
  3460. (add-hook 'org-blocker-hook
  3461. 'org-block-todo-from-children-or-siblings-or-parent)
  3462. (remove-hook 'org-blocker-hook
  3463. 'org-block-todo-from-children-or-siblings-or-parent))
  3464. (if org-enforce-todo-checkbox-dependencies
  3465. (add-hook 'org-blocker-hook
  3466. 'org-block-todo-from-checkboxes)
  3467. (remove-hook 'org-blocker-hook
  3468. 'org-block-todo-from-checkboxes))
  3469. ;; Comment characters
  3470. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3471. (org-set-local 'comment-padding " ")
  3472. ;; Align options lines
  3473. (org-set-local
  3474. 'align-mode-rules-list
  3475. '((org-in-buffer-settings
  3476. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3477. (modes . '(org-mode)))))
  3478. ;; Imenu
  3479. (org-set-local 'imenu-create-index-function
  3480. 'org-imenu-get-tree)
  3481. ;; Make isearch reveal context
  3482. (if (or (featurep 'xemacs)
  3483. (not (boundp 'outline-isearch-open-invisible-function)))
  3484. ;; Emacs 21 and XEmacs make use of the hook
  3485. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3486. ;; Emacs 22 deals with this through a special variable
  3487. (org-set-local 'outline-isearch-open-invisible-function
  3488. (lambda (&rest ignore) (org-show-context 'isearch))))
  3489. ;; If empty file that did not turn on org-mode automatically, make it to.
  3490. (if (and org-insert-mode-line-in-empty-file
  3491. (interactive-p)
  3492. (= (point-min) (point-max)))
  3493. (insert "# -*- mode: org -*-\n\n"))
  3494. (unless org-inhibit-startup
  3495. (when org-startup-align-all-tables
  3496. (let ((bmp (buffer-modified-p)))
  3497. (org-table-map-tables 'org-table-align)
  3498. (set-buffer-modified-p bmp)))
  3499. (org-set-startup-visibility)))
  3500. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3501. (defun org-current-time ()
  3502. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3503. (if (> (car org-time-stamp-rounding-minutes) 1)
  3504. (let ((r (car org-time-stamp-rounding-minutes))
  3505. (time (decode-time)))
  3506. (apply 'encode-time
  3507. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3508. (nthcdr 2 time))))
  3509. (current-time)))
  3510. ;;;; Font-Lock stuff, including the activators
  3511. (defvar org-mouse-map (make-sparse-keymap))
  3512. (org-defkey org-mouse-map
  3513. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3514. (org-defkey org-mouse-map
  3515. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3516. (when org-mouse-1-follows-link
  3517. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3518. (when org-tab-follows-link
  3519. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3520. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3521. (require 'font-lock)
  3522. (defconst org-non-link-chars "]\t\n\r<>")
  3523. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3524. "shell" "elisp"))
  3525. (defvar org-link-types-re nil
  3526. "Matches a link that has a url-like prefix like \"http:\"")
  3527. (defvar org-link-re-with-space nil
  3528. "Matches a link with spaces, optional angular brackets around it.")
  3529. (defvar org-link-re-with-space2 nil
  3530. "Matches a link with spaces, optional angular brackets around it.")
  3531. (defvar org-link-re-with-space3 nil
  3532. "Matches a link with spaces, only for internal part in bracket links.")
  3533. (defvar org-angle-link-re nil
  3534. "Matches link with angular brackets, spaces are allowed.")
  3535. (defvar org-plain-link-re nil
  3536. "Matches plain link, without spaces.")
  3537. (defvar org-bracket-link-regexp nil
  3538. "Matches a link in double brackets.")
  3539. (defvar org-bracket-link-analytic-regexp nil
  3540. "Regular expression used to analyze links.
  3541. Here is what the match groups contain after a match:
  3542. 1: http:
  3543. 2: http
  3544. 3: path
  3545. 4: [desc]
  3546. 5: desc")
  3547. (defvar org-bracket-link-analytic-regexp++ nil
  3548. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3549. (defvar org-any-link-re nil
  3550. "Regular expression matching any link.")
  3551. (defun org-make-link-regexps ()
  3552. "Update the link regular expressions.
  3553. This should be called after the variable `org-link-types' has changed."
  3554. (setq org-link-types-re
  3555. (concat
  3556. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3557. org-link-re-with-space
  3558. (concat
  3559. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3560. "\\([^" org-non-link-chars " ]"
  3561. "[^" org-non-link-chars "]*"
  3562. "[^" org-non-link-chars " ]\\)>?")
  3563. org-link-re-with-space2
  3564. (concat
  3565. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3566. "\\([^" org-non-link-chars " ]"
  3567. "[^\t\n\r]*"
  3568. "[^" org-non-link-chars " ]\\)>?")
  3569. org-link-re-with-space3
  3570. (concat
  3571. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3572. "\\([^" org-non-link-chars " ]"
  3573. "[^\t\n\r]*\\)")
  3574. org-angle-link-re
  3575. (concat
  3576. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3577. "\\([^" org-non-link-chars " ]"
  3578. "[^" org-non-link-chars "]*"
  3579. "\\)>")
  3580. org-plain-link-re
  3581. (concat
  3582. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3583. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3584. org-bracket-link-regexp
  3585. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3586. org-bracket-link-analytic-regexp
  3587. (concat
  3588. "\\[\\["
  3589. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3590. "\\([^]]+\\)"
  3591. "\\]"
  3592. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3593. "\\]")
  3594. org-bracket-link-analytic-regexp++
  3595. (concat
  3596. "\\[\\["
  3597. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3598. "\\([^]]+\\)"
  3599. "\\]"
  3600. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3601. "\\]")
  3602. org-any-link-re
  3603. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3604. org-angle-link-re "\\)\\|\\("
  3605. org-plain-link-re "\\)")))
  3606. (org-make-link-regexps)
  3607. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3608. "Regular expression for fast time stamp matching.")
  3609. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3610. "Regular expression for fast time stamp matching.")
  3611. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3612. "Regular expression matching time strings for analysis.
  3613. This one does not require the space after the date, so it can be used
  3614. on a string that terminates immediately after the date.")
  3615. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3616. "Regular expression matching time strings for analysis.")
  3617. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3618. "Regular expression matching time stamps, with groups.")
  3619. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3620. "Regular expression matching time stamps (also [..]), with groups.")
  3621. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3622. "Regular expression matching a time stamp range.")
  3623. (defconst org-tr-regexp-both
  3624. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3625. "Regular expression matching a time stamp range.")
  3626. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3627. org-ts-regexp "\\)?")
  3628. "Regular expression matching a time stamp or time stamp range.")
  3629. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3630. org-ts-regexp-both "\\)?")
  3631. "Regular expression matching a time stamp or time stamp range.
  3632. The time stamps may be either active or inactive.")
  3633. (defvar org-emph-face nil)
  3634. (defun org-do-emphasis-faces (limit)
  3635. "Run through the buffer and add overlays to links."
  3636. (let (rtn)
  3637. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3638. (if (not (= (char-after (match-beginning 3))
  3639. (char-after (match-beginning 4))))
  3640. (progn
  3641. (setq rtn t)
  3642. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3643. 'face
  3644. (nth 1 (assoc (match-string 3)
  3645. org-emphasis-alist)))
  3646. (add-text-properties (match-beginning 2) (match-end 2)
  3647. '(font-lock-multiline t))
  3648. (when org-hide-emphasis-markers
  3649. (add-text-properties (match-end 4) (match-beginning 5)
  3650. '(invisible org-link))
  3651. (add-text-properties (match-beginning 3) (match-end 3)
  3652. '(invisible org-link)))))
  3653. (backward-char 1))
  3654. rtn))
  3655. (defun org-emphasize (&optional char)
  3656. "Insert or change an emphasis, i.e. a font like bold or italic.
  3657. If there is an active region, change that region to a new emphasis.
  3658. If there is no region, just insert the marker characters and position
  3659. the cursor between them.
  3660. CHAR should be either the marker character, or the first character of the
  3661. HTML tag associated with that emphasis. If CHAR is a space, the means
  3662. to remove the emphasis of the selected region.
  3663. If char is not given (for example in an interactive call) it
  3664. will be prompted for."
  3665. (interactive)
  3666. (let ((eal org-emphasis-alist) e det
  3667. (erc org-emphasis-regexp-components)
  3668. (prompt "")
  3669. (string "") beg end move tag c s)
  3670. (if (org-region-active-p)
  3671. (setq beg (region-beginning) end (region-end)
  3672. string (buffer-substring beg end))
  3673. (setq move t))
  3674. (while (setq e (pop eal))
  3675. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3676. c (aref tag 0))
  3677. (push (cons c (string-to-char (car e))) det)
  3678. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3679. (substring tag 1)))))
  3680. (setq det (nreverse det))
  3681. (unless char
  3682. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3683. (setq char (read-char-exclusive)))
  3684. (setq char (or (cdr (assoc char det)) char))
  3685. (if (equal char ?\ )
  3686. (setq s "" move nil)
  3687. (unless (assoc (char-to-string char) org-emphasis-alist)
  3688. (error "No such emphasis marker: \"%c\"" char))
  3689. (setq s (char-to-string char)))
  3690. (while (and (> (length string) 1)
  3691. (equal (substring string 0 1) (substring string -1))
  3692. (assoc (substring string 0 1) org-emphasis-alist))
  3693. (setq string (substring string 1 -1)))
  3694. (setq string (concat s string s))
  3695. (if beg (delete-region beg end))
  3696. (unless (or (bolp)
  3697. (string-match (concat "[" (nth 0 erc) "\n]")
  3698. (char-to-string (char-before (point)))))
  3699. (insert " "))
  3700. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3701. (char-to-string (char-after (point))))
  3702. (insert " ") (backward-char 1))
  3703. (insert string)
  3704. (and move (backward-char 1))))
  3705. (defconst org-nonsticky-props
  3706. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3707. (defsubst org-rear-nonsticky-at (pos)
  3708. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3709. (defun org-activate-plain-links (limit)
  3710. "Run through the buffer and add overlays to links."
  3711. (catch 'exit
  3712. (let (f)
  3713. (while (re-search-forward org-plain-link-re limit t)
  3714. (setq f (get-text-property (match-beginning 0) 'face))
  3715. (if (or (eq f 'org-tag)
  3716. (and (listp f) (memq 'org-tag f)))
  3717. nil
  3718. (add-text-properties (match-beginning 0) (match-end 0)
  3719. (list 'mouse-face 'highlight
  3720. 'keymap org-mouse-map))
  3721. (org-rear-nonsticky-at (match-end 0))
  3722. (throw 'exit t))))))
  3723. (defun org-activate-code (limit)
  3724. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3725. (progn
  3726. (remove-text-properties (match-beginning 0) (match-end 0)
  3727. '(display t invisible t intangible t))
  3728. t)))
  3729. (defvar org-protecting-blocks
  3730. '("src" "example" "latex" "ascii" "html" "docbook")
  3731. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3732. This is needed for font-lock setup.")
  3733. (defun org-fontify-meta-lines-and-blocks (limit)
  3734. "Fontify #+ lines and blocks, in the correct ways."
  3735. (let ((case-fold-search t))
  3736. (if (re-search-forward
  3737. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3738. limit t)
  3739. (let ((beg (match-beginning 0))
  3740. (beg1 (line-beginning-position 2))
  3741. (dc1 (downcase (match-string 2)))
  3742. (dc3 (downcase (match-string 3)))
  3743. end end1 quoting)
  3744. (cond
  3745. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3746. ;; a single line of backend-specific content
  3747. (remove-text-properties (match-beginning 0) (match-end 0)
  3748. '(display t invisible t intangible t))
  3749. (add-text-properties (match-beginning 1) (match-end 3)
  3750. '(font-lock-fontified t face org-meta-line))
  3751. (add-text-properties (match-beginning 6) (match-end 6)
  3752. '(font-lock-fontified t face org-block))
  3753. t)
  3754. ((and (match-end 4) (equal dc3 "begin"))
  3755. ;; Truely a block
  3756. (setq quoting (member (downcase (match-string 5))
  3757. org-protecting-blocks))
  3758. (when (re-search-forward
  3759. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3760. nil t) ;; on purpose, we look further than LIMIT
  3761. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3762. (when quoting
  3763. (remove-text-properties beg end
  3764. '(display t invisible t intangible t)))
  3765. (add-text-properties
  3766. beg end
  3767. '(font-lock-fontified t font-lock-multiline t))
  3768. (add-text-properties beg beg1 '(face org-meta-line))
  3769. (add-text-properties end1 end '(face org-meta-line))
  3770. (when quoting
  3771. (add-text-properties beg1 end1 '(face org-block)))
  3772. t))
  3773. ((not (member (char-after beg) '(?\ ?\t)))
  3774. ;; just any other in-buffer setting, but not indented
  3775. (add-text-properties
  3776. beg (match-end 0)
  3777. '(font-lock-fontified t face org-meta-line))
  3778. t)
  3779. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3780. (and (match-end 4) (equal dc3 "attr")))
  3781. (add-text-properties
  3782. beg (match-end 0)
  3783. '(font-lock-fontified t face org-meta-line))
  3784. t)
  3785. (t nil))))))
  3786. (defun org-activate-angle-links (limit)
  3787. "Run through the buffer and add overlays to links."
  3788. (if (re-search-forward org-angle-link-re limit t)
  3789. (progn
  3790. (add-text-properties (match-beginning 0) (match-end 0)
  3791. (list 'mouse-face 'highlight
  3792. 'keymap org-mouse-map))
  3793. (org-rear-nonsticky-at (match-end 0))
  3794. t)))
  3795. (defun org-activate-footnote-links (limit)
  3796. "Run through the buffer and add overlays to links."
  3797. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3798. limit t)
  3799. (progn
  3800. (add-text-properties (match-beginning 2) (match-end 2)
  3801. (list 'mouse-face 'highlight
  3802. 'keymap org-mouse-map
  3803. 'help-echo
  3804. (if (= (point-at-bol) (match-beginning 2))
  3805. "Footnote definition"
  3806. "Footnote reference")
  3807. ))
  3808. (org-rear-nonsticky-at (match-end 2))
  3809. t)))
  3810. (defun org-activate-bracket-links (limit)
  3811. "Run through the buffer and add overlays to bracketed links."
  3812. (if (re-search-forward org-bracket-link-regexp limit t)
  3813. (let* ((help (concat "LINK: "
  3814. (org-match-string-no-properties 1)))
  3815. ;; FIXME: above we should remove the escapes.
  3816. ;; but that requires another match, protecting match data,
  3817. ;; a lot of overhead for font-lock.
  3818. (ip (org-maybe-intangible
  3819. (list 'invisible 'org-link
  3820. 'keymap org-mouse-map 'mouse-face 'highlight
  3821. 'font-lock-multiline t 'help-echo help)))
  3822. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3823. 'font-lock-multiline t 'help-echo help)))
  3824. ;; We need to remove the invisible property here. Table narrowing
  3825. ;; may have made some of this invisible.
  3826. (remove-text-properties (match-beginning 0) (match-end 0)
  3827. '(invisible nil))
  3828. (if (match-end 3)
  3829. (progn
  3830. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3831. (org-rear-nonsticky-at (match-beginning 3))
  3832. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3833. (org-rear-nonsticky-at (match-end 3))
  3834. (add-text-properties (match-end 3) (match-end 0) ip)
  3835. (org-rear-nonsticky-at (match-end 0)))
  3836. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3837. (org-rear-nonsticky-at (match-beginning 1))
  3838. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3839. (org-rear-nonsticky-at (match-end 1))
  3840. (add-text-properties (match-end 1) (match-end 0) ip)
  3841. (org-rear-nonsticky-at (match-end 0)))
  3842. t)))
  3843. (defun org-activate-dates (limit)
  3844. "Run through the buffer and add overlays to dates."
  3845. (if (re-search-forward org-tsr-regexp-both limit t)
  3846. (progn
  3847. (add-text-properties (match-beginning 0) (match-end 0)
  3848. (list 'mouse-face 'highlight
  3849. 'keymap org-mouse-map))
  3850. (org-rear-nonsticky-at (match-end 0))
  3851. (when org-display-custom-times
  3852. (if (match-end 3)
  3853. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3854. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3855. t)))
  3856. (defvar org-target-link-regexp nil
  3857. "Regular expression matching radio targets in plain text.")
  3858. (make-variable-buffer-local 'org-target-link-regexp)
  3859. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3860. "Regular expression matching a link target.")
  3861. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3862. "Regular expression matching a radio target.")
  3863. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3864. "Regular expression matching any target.")
  3865. (defun org-activate-target-links (limit)
  3866. "Run through the buffer and add overlays to target matches."
  3867. (when org-target-link-regexp
  3868. (let ((case-fold-search t))
  3869. (if (re-search-forward org-target-link-regexp limit t)
  3870. (progn
  3871. (add-text-properties (match-beginning 0) (match-end 0)
  3872. (list 'mouse-face 'highlight
  3873. 'keymap org-mouse-map
  3874. 'help-echo "Radio target link"
  3875. 'org-linked-text t))
  3876. (org-rear-nonsticky-at (match-end 0))
  3877. t)))))
  3878. (defun org-update-radio-target-regexp ()
  3879. "Find all radio targets in this file and update the regular expression."
  3880. (interactive)
  3881. (when (memq 'radio org-activate-links)
  3882. (setq org-target-link-regexp
  3883. (org-make-target-link-regexp (org-all-targets 'radio)))
  3884. (org-restart-font-lock)))
  3885. (defun org-hide-wide-columns (limit)
  3886. (let (s e)
  3887. (setq s (text-property-any (point) (or limit (point-max))
  3888. 'org-cwidth t))
  3889. (when s
  3890. (setq e (next-single-property-change s 'org-cwidth))
  3891. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3892. (goto-char e)
  3893. t)))
  3894. (defvar org-latex-and-specials-regexp nil
  3895. "Regular expression for highlighting export special stuff.")
  3896. (defvar org-match-substring-regexp)
  3897. (defvar org-match-substring-with-braces-regexp)
  3898. (defvar org-export-html-special-string-regexps)
  3899. (defun org-compute-latex-and-specials-regexp ()
  3900. "Compute regular expression for stuff treated specially by exporters."
  3901. (if (not org-highlight-latex-fragments-and-specials)
  3902. (org-set-local 'org-latex-and-specials-regexp nil)
  3903. (require 'org-exp)
  3904. (let*
  3905. ((matchers (plist-get org-format-latex-options :matchers))
  3906. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3907. org-latex-regexps)))
  3908. (options (org-combine-plists (org-default-export-plist)
  3909. (org-infile-export-plist)))
  3910. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3911. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3912. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3913. (org-export-html-expand (plist-get options :expand-quoted-html))
  3914. (org-export-with-special-strings (plist-get options :special-strings))
  3915. (re-sub
  3916. (cond
  3917. ((equal org-export-with-sub-superscripts '{})
  3918. (list org-match-substring-with-braces-regexp))
  3919. (org-export-with-sub-superscripts
  3920. (list org-match-substring-regexp))
  3921. (t nil)))
  3922. (re-latex
  3923. (if org-export-with-LaTeX-fragments
  3924. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3925. (re-macros
  3926. (if org-export-with-TeX-macros
  3927. (list (concat "\\\\"
  3928. (regexp-opt
  3929. (append (mapcar 'car org-html-entities)
  3930. (if (boundp 'org-latex-entities)
  3931. (mapcar (lambda (x)
  3932. (or (car-safe x) x))
  3933. org-latex-entities)
  3934. nil))
  3935. 'words))) ; FIXME
  3936. ))
  3937. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3938. (re-special (if org-export-with-special-strings
  3939. (mapcar (lambda (x) (car x))
  3940. org-export-html-special-string-regexps)))
  3941. (re-rest
  3942. (delq nil
  3943. (list
  3944. (if org-export-html-expand "@<[^>\n]+>")
  3945. ))))
  3946. (org-set-local
  3947. 'org-latex-and-specials-regexp
  3948. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3949. re-rest) "\\|")))))
  3950. (defun org-do-latex-and-special-faces (limit)
  3951. "Run through the buffer and add overlays to links."
  3952. (when org-latex-and-specials-regexp
  3953. (let (rtn d)
  3954. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3955. limit t))
  3956. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3957. 'face))
  3958. '(org-code org-verbatim underline)))
  3959. (progn
  3960. (setq rtn t
  3961. d (cond ((member (char-after (1+ (match-beginning 0)))
  3962. '(?_ ?^)) 1)
  3963. (t 0)))
  3964. (font-lock-prepend-text-property
  3965. (+ d (match-beginning 0)) (match-end 0)
  3966. 'face 'org-latex-and-export-specials)
  3967. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3968. '(font-lock-multiline t)))))
  3969. rtn)))
  3970. (defun org-restart-font-lock ()
  3971. "Restart font-lock-mode, to force refontification."
  3972. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3973. (font-lock-mode -1)
  3974. (font-lock-mode 1)))
  3975. (defun org-all-targets (&optional radio)
  3976. "Return a list of all targets in this file.
  3977. With optional argument RADIO, only find radio targets."
  3978. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3979. rtn)
  3980. (save-excursion
  3981. (goto-char (point-min))
  3982. (while (re-search-forward re nil t)
  3983. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3984. rtn)))
  3985. (defun org-make-target-link-regexp (targets)
  3986. "Make regular expression matching all strings in TARGETS.
  3987. The regular expression finds the targets also if there is a line break
  3988. between words."
  3989. (and targets
  3990. (concat
  3991. "\\<\\("
  3992. (mapconcat
  3993. (lambda (x)
  3994. (while (string-match " +" x)
  3995. (setq x (replace-match "\\s-+" t t x)))
  3996. x)
  3997. targets
  3998. "\\|")
  3999. "\\)\\>")))
  4000. (defun org-activate-tags (limit)
  4001. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4002. (progn
  4003. (add-text-properties (match-beginning 1) (match-end 1)
  4004. (list 'mouse-face 'highlight
  4005. 'keymap org-mouse-map))
  4006. (org-rear-nonsticky-at (match-end 1))
  4007. t)))
  4008. (defun org-outline-level ()
  4009. (save-excursion
  4010. (looking-at outline-regexp)
  4011. (if (match-beginning 1)
  4012. (+ (org-get-string-indentation (match-string 1)) 1000)
  4013. (1- (- (match-end 0) (match-beginning 0))))))
  4014. (defvar org-font-lock-keywords nil)
  4015. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4016. "Regular expression matching a property line.")
  4017. (defvar org-font-lock-hook nil
  4018. "Functions to be called for special font lock stuff.")
  4019. (defun org-font-lock-hook (limit)
  4020. (run-hook-with-args 'org-font-lock-hook limit))
  4021. (defun org-set-font-lock-defaults ()
  4022. (let* ((em org-fontify-emphasized-text)
  4023. (lk org-activate-links)
  4024. (org-font-lock-extra-keywords
  4025. (list
  4026. ;; Call the hook
  4027. '(org-font-lock-hook)
  4028. ;; Headlines
  4029. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4030. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4031. ;; Table lines
  4032. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4033. (1 'org-table t))
  4034. ;; Table internals
  4035. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4036. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4037. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4038. ;; Drawers
  4039. (list org-drawer-regexp '(0 'org-special-keyword t))
  4040. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4041. ;; Properties
  4042. (list org-property-re
  4043. '(1 'org-special-keyword t)
  4044. '(3 'org-property-value t))
  4045. (if org-format-transports-properties-p
  4046. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4047. ;; Links
  4048. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4049. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4050. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4051. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4052. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4053. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4054. (if (memq 'footnote lk) '(org-activate-footnote-links
  4055. (2 'org-footnote t)))
  4056. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4057. '(org-hide-wide-columns (0 nil append))
  4058. ;; TODO lines
  4059. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4060. '(1 (org-get-todo-face 1) t))
  4061. ;; DONE
  4062. (if org-fontify-done-headline
  4063. (list (concat "^[*]+ +\\<\\("
  4064. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4065. "\\)\\(.*\\)")
  4066. '(2 'org-headline-done t))
  4067. nil)
  4068. ;; Priorities
  4069. '(org-font-lock-add-priority-faces)
  4070. ;; Tags
  4071. '(org-font-lock-add-tag-faces)
  4072. ;; Special keywords
  4073. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4074. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4075. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4076. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4077. ;; Emphasis
  4078. (if em
  4079. (if (featurep 'xemacs)
  4080. '(org-do-emphasis-faces (0 nil append))
  4081. '(org-do-emphasis-faces)))
  4082. ;; Checkboxes
  4083. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4084. 2 'org-checkbox prepend)
  4085. (if org-provide-checkbox-statistics
  4086. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4087. (0 (org-get-checkbox-statistics-face) t)))
  4088. ;; Description list items
  4089. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4090. 2 'bold prepend)
  4091. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4092. '(1 'org-archived prepend))
  4093. ;; Specials
  4094. '(org-do-latex-and-special-faces)
  4095. ;; Code
  4096. '(org-activate-code (1 'org-code t))
  4097. ;; COMMENT
  4098. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4099. "\\|" org-quote-string "\\)\\>")
  4100. '(1 'org-special-keyword t))
  4101. '("^#.*" (0 'font-lock-comment-face t))
  4102. ;; Blocks and meta lines
  4103. '(org-fontify-meta-lines-and-blocks)
  4104. )))
  4105. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4106. ;; Now set the full font-lock-keywords
  4107. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4108. (org-set-local 'font-lock-defaults
  4109. '(org-font-lock-keywords t nil nil backward-paragraph))
  4110. (kill-local-variable 'font-lock-keywords) nil))
  4111. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4112. "Fontify string S like in Org-mode"
  4113. (with-temp-buffer
  4114. (insert s)
  4115. (let ((org-odd-levels-only odd-levels))
  4116. (org-mode)
  4117. (font-lock-fontify-buffer)
  4118. (buffer-string))))
  4119. (defvar org-m nil)
  4120. (defvar org-l nil)
  4121. (defvar org-f nil)
  4122. (defun org-get-level-face (n)
  4123. "Get the right face for match N in font-lock matching of headlines."
  4124. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4125. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4126. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4127. (cond
  4128. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4129. ((eq n 2) org-f)
  4130. (t (if org-level-color-stars-only nil org-f))))
  4131. (defun org-get-todo-face (kwd)
  4132. "Get the right face for a TODO keyword KWD.
  4133. If KWD is a number, get the corresponding match group."
  4134. (if (numberp kwd) (setq kwd (match-string kwd)))
  4135. (or (cdr (assoc kwd org-todo-keyword-faces))
  4136. (and (member kwd org-done-keywords) 'org-done)
  4137. 'org-todo))
  4138. (defun org-font-lock-add-tag-faces (limit)
  4139. "Add the special tag faces."
  4140. (when (and org-tag-faces org-tags-special-faces-re)
  4141. (while (re-search-forward org-tags-special-faces-re limit t)
  4142. (add-text-properties (match-beginning 1) (match-end 1)
  4143. (list 'face (org-get-tag-face 1)
  4144. 'font-lock-fontified t))
  4145. (backward-char 1))))
  4146. (defun org-font-lock-add-priority-faces (limit)
  4147. "Add the special priority faces."
  4148. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4149. (add-text-properties
  4150. (match-beginning 0) (match-end 0)
  4151. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4152. org-priority-faces))
  4153. 'org-special-keyword)
  4154. 'font-lock-fontified t))))
  4155. (defun org-get-tag-face (kwd)
  4156. "Get the right face for a TODO keyword KWD.
  4157. If KWD is a number, get the corresponding match group."
  4158. (if (numberp kwd) (setq kwd (match-string kwd)))
  4159. (or (cdr (assoc kwd org-tag-faces))
  4160. 'org-tag))
  4161. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4162. "Remove fontification and activation overlays from links."
  4163. (font-lock-default-unfontify-region beg end)
  4164. (let* ((buffer-undo-list t)
  4165. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4166. (inhibit-modification-hooks t)
  4167. deactivate-mark buffer-file-name buffer-file-truename)
  4168. (remove-text-properties beg end
  4169. '(mouse-face t keymap t org-linked-text t
  4170. invisible t intangible t))))
  4171. ;;;; Visibility cycling, including org-goto and indirect buffer
  4172. ;;; Cycling
  4173. (defvar org-cycle-global-status nil)
  4174. (make-variable-buffer-local 'org-cycle-global-status)
  4175. (defvar org-cycle-subtree-status nil)
  4176. (make-variable-buffer-local 'org-cycle-subtree-status)
  4177. ;;;###autoload
  4178. (defvar org-inlinetask-min-level)
  4179. (defun org-cycle (&optional arg)
  4180. "TAB-action and visibility cycling for Org-mode.
  4181. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4182. is outine visibility cycling, but it also invokes other actions
  4183. in special contexts.
  4184. - When this function is called with a prefix argument, rotate the entire
  4185. buffer through 3 states (global cycling)
  4186. 1. OVERVIEW: Show only top-level headlines.
  4187. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4188. 3. SHOW ALL: Show everything.
  4189. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4190. determined by the variable `org-startup-folded', and by any VISIBILITY
  4191. properties in the buffer.
  4192. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4193. including any drawers.
  4194. - When inside a table, re-align the table and move to the next field.
  4195. - When point is at the beginning of a headline, rotate the subtree started
  4196. by this line through 3 different states (local cycling)
  4197. 1. FOLDED: Only the main headline is shown.
  4198. 2. CHILDREN: The main headline and the direct children are shown.
  4199. From this state, you can move to one of the children
  4200. and zoom in further.
  4201. 3. SUBTREE: Show the entire subtree, including body text.
  4202. - When there is a numeric prefix, go up to a heading with level ARG, do
  4203. a `show-subtree' and return to the previous cursor position. If ARG
  4204. is negative, go up that many levels.
  4205. - When point is not at the beginning of a headline, execute the global
  4206. binding for TAB, which is re-indenting the line. See the option
  4207. `org-cycle-emulate-tab' for details.
  4208. - Special case: if point is at the beginning of the buffer and there is
  4209. no headline in line 1, this function will act as if called with prefix arg.
  4210. But only if also the variable `org-cycle-global-at-bob' is t."
  4211. (interactive "P")
  4212. (org-load-modules-maybe)
  4213. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4214. (let* ((limit-level
  4215. (or org-cycle-max-level
  4216. (and (boundp 'org-inlinetask-min-level)
  4217. org-inlinetask-min-level
  4218. (1- org-inlinetask-min-level))))
  4219. (nstars (and limit-level
  4220. (if org-odd-levels-only
  4221. (and limit-level (1- (* limit-level 2)))
  4222. limit-level)))
  4223. (outline-regexp
  4224. (cond
  4225. ((not (org-mode-p)) outline-regexp)
  4226. (org-cycle-include-plain-lists
  4227. (concat "\\(?:\\*"
  4228. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4229. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4230. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4231. (bob-special (and org-cycle-global-at-bob (bobp)
  4232. (not (looking-at outline-regexp))))
  4233. (org-cycle-hook
  4234. (if bob-special
  4235. (delq 'org-optimize-window-after-visibility-change
  4236. (copy-sequence org-cycle-hook))
  4237. org-cycle-hook))
  4238. (pos (point)))
  4239. (if (or bob-special (equal arg '(4)))
  4240. ;; special case: use global cycling
  4241. (setq arg t))
  4242. (cond
  4243. ((equal arg '(16))
  4244. (org-set-startup-visibility)
  4245. (message "Startup visibility, plus VISIBILITY properties"))
  4246. ((equal arg '(64))
  4247. (show-all)
  4248. (message "Entire buffer visible, including drawers"))
  4249. ((org-at-table-p 'any)
  4250. ;; Enter the table or move to the next field in the table
  4251. (or (org-table-recognize-table.el)
  4252. (progn
  4253. (if arg (org-table-edit-field t)
  4254. (org-table-justify-field-maybe)
  4255. (call-interactively 'org-table-next-field)))))
  4256. ((run-hook-with-args-until-success
  4257. 'org-tab-after-check-for-table-hook))
  4258. ((eq arg t) ;; Global cycling
  4259. (org-cycle-internal-global))
  4260. ((and org-drawers org-drawer-regexp
  4261. (save-excursion
  4262. (beginning-of-line 1)
  4263. (looking-at org-drawer-regexp)))
  4264. ;; Toggle block visibility
  4265. (org-flag-drawer
  4266. (not (get-char-property (match-end 0) 'invisible))))
  4267. ((integerp arg)
  4268. ;; Show-subtree, ARG levels up from here.
  4269. (save-excursion
  4270. (org-back-to-heading)
  4271. (outline-up-heading (if (< arg 0) (- arg)
  4272. (- (funcall outline-level) arg)))
  4273. (org-show-subtree)))
  4274. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4275. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4276. (org-cycle-internal-local))
  4277. ;; TAB emulation and template completion
  4278. (buffer-read-only (org-back-to-heading))
  4279. ((run-hook-with-args-until-success
  4280. 'org-tab-after-check-for-cycling-hook))
  4281. ((org-try-structure-completion))
  4282. ((org-try-cdlatex-tab))
  4283. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4284. (or (not (bolp))
  4285. (not (looking-at outline-regexp))))
  4286. (call-interactively (global-key-binding "\t")))
  4287. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4288. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4289. (or (and (eq org-cycle-emulate-tab 'white)
  4290. (= (match-end 0) (point-at-eol)))
  4291. (and (eq org-cycle-emulate-tab 'whitestart)
  4292. (>= (match-end 0) pos))))
  4293. t
  4294. (eq org-cycle-emulate-tab t))
  4295. (call-interactively (global-key-binding "\t")))
  4296. (t (save-excursion
  4297. (org-back-to-heading)
  4298. (org-cycle)))))))
  4299. (defun org-cycle-internal-global ()
  4300. "Do the global cycling action."
  4301. (cond
  4302. ((and (eq last-command this-command)
  4303. (eq org-cycle-global-status 'overview))
  4304. ;; We just created the overview - now do table of contents
  4305. ;; This can be slow in very large buffers, so indicate action
  4306. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4307. (message "CONTENTS...")
  4308. (org-content)
  4309. (message "CONTENTS...done")
  4310. (setq org-cycle-global-status 'contents)
  4311. (run-hook-with-args 'org-cycle-hook 'contents))
  4312. ((and (eq last-command this-command)
  4313. (eq org-cycle-global-status 'contents))
  4314. ;; We just showed the table of contents - now show everything
  4315. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4316. (show-all)
  4317. (message "SHOW ALL")
  4318. (setq org-cycle-global-status 'all)
  4319. (run-hook-with-args 'org-cycle-hook 'all))
  4320. (t
  4321. ;; Default action: go to overview
  4322. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4323. (org-overview)
  4324. (message "OVERVIEW")
  4325. (setq org-cycle-global-status 'overview)
  4326. (run-hook-with-args 'org-cycle-hook 'overview))))
  4327. (defun org-cycle-internal-local ()
  4328. "Do the local cycling action."
  4329. (org-back-to-heading)
  4330. (let ((goal-column 0) eoh eol eos)
  4331. ;; First, some boundaries
  4332. (save-excursion
  4333. (org-back-to-heading)
  4334. (save-excursion
  4335. (beginning-of-line 2)
  4336. (while (and (not (eobp)) ;; this is like `next-line'
  4337. (get-char-property (1- (point)) 'invisible))
  4338. (beginning-of-line 2)) (setq eol (point)))
  4339. (outline-end-of-heading) (setq eoh (point))
  4340. (org-end-of-subtree t)
  4341. (unless (eobp)
  4342. (skip-chars-forward " \t\n")
  4343. (beginning-of-line 1) ; in case this is an item
  4344. )
  4345. (setq eos (1- (point))))
  4346. ;; Find out what to do next and set `this-command'
  4347. (cond
  4348. ((= eos eoh)
  4349. ;; Nothing is hidden behind this heading
  4350. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4351. (message "EMPTY ENTRY")
  4352. (setq org-cycle-subtree-status nil)
  4353. (save-excursion
  4354. (goto-char eos)
  4355. (outline-next-heading)
  4356. (if (org-invisible-p) (org-flag-heading nil))))
  4357. ((or (>= eol eos)
  4358. (not (string-match "\\S-" (buffer-substring eol eos))))
  4359. ;; Entire subtree is hidden in one line: open it
  4360. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4361. (org-show-entry)
  4362. (show-children)
  4363. (message "CHILDREN")
  4364. (save-excursion
  4365. (goto-char eos)
  4366. (outline-next-heading)
  4367. (if (org-invisible-p) (org-flag-heading nil)))
  4368. (setq org-cycle-subtree-status 'children)
  4369. (run-hook-with-args 'org-cycle-hook 'children))
  4370. ((and (eq last-command this-command)
  4371. (eq org-cycle-subtree-status 'children))
  4372. ;; We just showed the children, now show everything.
  4373. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4374. (org-show-subtree)
  4375. (message "SUBTREE")
  4376. (setq org-cycle-subtree-status 'subtree)
  4377. (run-hook-with-args 'org-cycle-hook 'subtree))
  4378. (t
  4379. ;; Default action: hide the subtree.
  4380. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4381. (hide-subtree)
  4382. (message "FOLDED")
  4383. (setq org-cycle-subtree-status 'folded)
  4384. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4385. ;;;###autoload
  4386. (defun org-global-cycle (&optional arg)
  4387. "Cycle the global visibility. For details see `org-cycle'.
  4388. With C-u prefix arg, switch to startup visibility.
  4389. With a numeric prefix, show all headlines up to that level."
  4390. (interactive "P")
  4391. (let ((org-cycle-include-plain-lists
  4392. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4393. (cond
  4394. ((integerp arg)
  4395. (show-all)
  4396. (hide-sublevels arg)
  4397. (setq org-cycle-global-status 'contents))
  4398. ((equal arg '(4))
  4399. (org-set-startup-visibility)
  4400. (message "Startup visibility, plus VISIBILITY properties."))
  4401. (t
  4402. (org-cycle '(4))))))
  4403. (defun org-set-startup-visibility ()
  4404. "Set the visibility required by startup options and properties."
  4405. (cond
  4406. ((eq org-startup-folded t)
  4407. (org-cycle '(4)))
  4408. ((eq org-startup-folded 'content)
  4409. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4410. (org-cycle '(4)) (org-cycle '(4)))))
  4411. (if org-hide-block-startup (org-hide-block-all))
  4412. (org-set-visibility-according-to-property 'no-cleanup)
  4413. (org-cycle-hide-archived-subtrees 'all)
  4414. (org-cycle-hide-drawers 'all)
  4415. (org-cycle-show-empty-lines 'all))
  4416. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4417. "Switch subtree visibilities according to :VISIBILITY: property."
  4418. (interactive)
  4419. (let (org-show-entry-below state)
  4420. (save-excursion
  4421. (goto-char (point-min))
  4422. (while (re-search-forward
  4423. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4424. nil t)
  4425. (setq state (match-string 1))
  4426. (save-excursion
  4427. (org-back-to-heading t)
  4428. (hide-subtree)
  4429. (org-reveal)
  4430. (cond
  4431. ((equal state '("fold" "folded"))
  4432. (hide-subtree))
  4433. ((equal state "children")
  4434. (org-show-hidden-entry)
  4435. (show-children))
  4436. ((equal state "content")
  4437. (save-excursion
  4438. (save-restriction
  4439. (org-narrow-to-subtree)
  4440. (org-content))))
  4441. ((member state '("all" "showall"))
  4442. (show-subtree)))))
  4443. (unless no-cleanup
  4444. (org-cycle-hide-archived-subtrees 'all)
  4445. (org-cycle-hide-drawers 'all)
  4446. (org-cycle-show-empty-lines 'all)))))
  4447. (defun org-overview ()
  4448. "Switch to overview mode, showing only top-level headlines.
  4449. Really, this shows all headlines with level equal or greater than the level
  4450. of the first headline in the buffer. This is important, because if the
  4451. first headline is not level one, then (hide-sublevels 1) gives confusing
  4452. results."
  4453. (interactive)
  4454. (let ((level (save-excursion
  4455. (goto-char (point-min))
  4456. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4457. (progn
  4458. (goto-char (match-beginning 0))
  4459. (funcall outline-level))))))
  4460. (and level (hide-sublevels level))))
  4461. (defun org-content (&optional arg)
  4462. "Show all headlines in the buffer, like a table of contents.
  4463. With numerical argument N, show content up to level N."
  4464. (interactive "P")
  4465. (save-excursion
  4466. ;; Visit all headings and show their offspring
  4467. (and (integerp arg) (org-overview))
  4468. (goto-char (point-max))
  4469. (catch 'exit
  4470. (while (and (progn (condition-case nil
  4471. (outline-previous-visible-heading 1)
  4472. (error (goto-char (point-min))))
  4473. t)
  4474. (looking-at outline-regexp))
  4475. (if (integerp arg)
  4476. (show-children (1- arg))
  4477. (show-branches))
  4478. (if (bobp) (throw 'exit nil))))))
  4479. (defun org-optimize-window-after-visibility-change (state)
  4480. "Adjust the window after a change in outline visibility.
  4481. This function is the default value of the hook `org-cycle-hook'."
  4482. (when (get-buffer-window (current-buffer))
  4483. (cond
  4484. ((eq state 'content) nil)
  4485. ((eq state 'all) nil)
  4486. ((eq state 'folded) nil)
  4487. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4488. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4489. (defun org-compact-display-after-subtree-move ()
  4490. "Show a compacter version of the tree of the entry's parent."
  4491. (save-excursion
  4492. (if (org-up-heading-safe)
  4493. (progn
  4494. (hide-subtree)
  4495. (show-entry)
  4496. (show-children)
  4497. (org-cycle-show-empty-lines 'children)
  4498. (org-cycle-hide-drawers 'children))
  4499. (org-overview))))
  4500. (defun org-cycle-show-empty-lines (state)
  4501. "Show empty lines above all visible headlines.
  4502. The region to be covered depends on STATE when called through
  4503. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4504. entire buffer covered. Note that an empty line is only shown if there
  4505. are at least `org-cycle-separator-lines' empty lines before the headline."
  4506. (when (> org-cycle-separator-lines 0)
  4507. (save-excursion
  4508. (let* ((n org-cycle-separator-lines)
  4509. (re (cond
  4510. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4511. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4512. (t (let ((ns (number-to-string (- n 2))))
  4513. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4514. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4515. beg end)
  4516. (cond
  4517. ((memq state '(overview contents t))
  4518. (setq beg (point-min) end (point-max)))
  4519. ((memq state '(children folded))
  4520. (setq beg (point) end (progn (org-end-of-subtree t t)
  4521. (beginning-of-line 2)
  4522. (point)))))
  4523. (when beg
  4524. (goto-char beg)
  4525. (while (re-search-forward re end t)
  4526. (if (not (get-char-property (match-end 1) 'invisible))
  4527. (outline-flag-region
  4528. (match-beginning 1) (match-end 1) nil)))))))
  4529. ;; Never hide empty lines at the end of the file.
  4530. (save-excursion
  4531. (goto-char (point-max))
  4532. (outline-previous-heading)
  4533. (outline-end-of-heading)
  4534. (if (and (looking-at "[ \t\n]+")
  4535. (= (match-end 0) (point-max)))
  4536. (outline-flag-region (point) (match-end 0) nil))))
  4537. (defun org-show-empty-lines-in-parent ()
  4538. "Move to the parent and re-show empty lines before visible headlines."
  4539. (save-excursion
  4540. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4541. (org-cycle-show-empty-lines context))))
  4542. (defun org-cycle-hide-drawers (state)
  4543. "Re-hide all drawers after a visibility state change."
  4544. (when (and (org-mode-p)
  4545. (not (memq state '(overview folded))))
  4546. (save-excursion
  4547. (let* ((globalp (memq state '(contents all)))
  4548. (beg (if globalp (point-min) (point)))
  4549. (end (if globalp (point-max) (org-end-of-subtree t))))
  4550. (goto-char beg)
  4551. (while (re-search-forward org-drawer-regexp end t)
  4552. (org-flag-drawer t))))))
  4553. (defun org-flag-drawer (flag)
  4554. (save-excursion
  4555. (beginning-of-line 1)
  4556. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4557. (let ((b (match-end 0))
  4558. (outline-regexp org-outline-regexp))
  4559. (if (re-search-forward
  4560. "^[ \t]*:END:"
  4561. (save-excursion (outline-next-heading) (point)) t)
  4562. (outline-flag-region b (point-at-eol) flag)
  4563. (error ":END: line missing"))))))
  4564. (defun org-subtree-end-visible-p ()
  4565. "Is the end of the current subtree visible?"
  4566. (pos-visible-in-window-p
  4567. (save-excursion (org-end-of-subtree t) (point))))
  4568. (defun org-first-headline-recenter (&optional N)
  4569. "Move cursor to the first headline and recenter the headline.
  4570. Optional argument N means, put the headline into the Nth line of the window."
  4571. (goto-char (point-min))
  4572. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4573. (beginning-of-line)
  4574. (recenter (prefix-numeric-value N))))
  4575. ;;; Folding of blocks
  4576. (defcustom org-hide-block-startup nil
  4577. "Non-nil means, , entering Org-mode will fold all blocks.
  4578. This can also be set in on a per-file basis with
  4579. #+STARTUP: hideblocks
  4580. #+STARTUP: showblocks"
  4581. :group 'org-startup
  4582. :type 'boolean)
  4583. (defconst org-block-regexp
  4584. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1"
  4585. "Regular expression for hiding blocks.")
  4586. (defvar org-hide-block-overlays nil
  4587. "Overays hiding blocks.")
  4588. (make-variable-buffer-local 'org-hide-block-overlays)
  4589. (defun org-block-map (function &optional start end)
  4590. "Call func at the head of all source blocks in the current
  4591. buffer. Optional arguments START and END can be used to limit
  4592. the range."
  4593. (let ((start (or start (point-min)))
  4594. (end (or end (point-max))))
  4595. (save-excursion
  4596. (goto-char start)
  4597. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4598. (save-excursion
  4599. (save-match-data
  4600. (goto-char (match-beginning 0))
  4601. (funcall function)))))))
  4602. (defun org-hide-block-toggle-all ()
  4603. "Toggle the visibility of all blocks in the current buffer."
  4604. (org-block-map #'org-hide-block-toggle))
  4605. (defun org-hide-block-all ()
  4606. "Fold all blocks in the current buffer."
  4607. (interactive)
  4608. (org-show-block-all)
  4609. (org-block-map #'org-hide-block-toggle-maybe))
  4610. (defun org-show-block-all ()
  4611. "Unfold all blocks in the current buffer."
  4612. (mapc 'org-delete-overlay org-hide-block-overlays)
  4613. (setq org-hide-block-overlays nil))
  4614. (defun org-hide-block-toggle-maybe ()
  4615. "Toggle visibility of block at point."
  4616. (interactive)
  4617. (let ((case-fold-search t))
  4618. (if (save-excursion
  4619. (beginning-of-line 1)
  4620. (looking-at org-block-regexp))
  4621. (progn (org-hide-block-toggle)
  4622. t) ;; to signal that we took action
  4623. nil))) ;; to signal that we did not
  4624. (defun org-hide-block-toggle (&optional force)
  4625. "Toggle the visibility of the current block."
  4626. (interactive)
  4627. (save-excursion
  4628. (beginning-of-line)
  4629. (if (re-search-forward org-block-regexp nil t)
  4630. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4631. (end (match-end 0))
  4632. ov) ;; end of entire body
  4633. (if (memq t (mapcar (lambda (overlay)
  4634. (eq (org-overlay-get overlay 'invisible)
  4635. 'org-hide-block))
  4636. (org-overlays-at start)))
  4637. (if (or (not force) (eq force 'off))
  4638. (mapc (lambda (ov)
  4639. (when (member ov org-hide-block-overlays)
  4640. (setq org-hide-block-overlays
  4641. (delq ov org-hide-block-overlays)))
  4642. (when (eq (org-overlay-get ov 'invisible)
  4643. 'org-hide-block)
  4644. (org-delete-overlay ov)))
  4645. (org-overlays-at start)))
  4646. (setq ov (make-overlay start end))
  4647. (org-overlay-put ov 'invisible 'org-hide-block)
  4648. (push ov org-hide-block-overlays)))
  4649. (error "Not looking at a source block"))))
  4650. ;; org-tab-after-check-for-cycling-hook
  4651. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4652. ;; Remove overlays when changing major mode
  4653. (add-hook 'org-mode-hook
  4654. (lambda () (org-add-hook 'change-major-mode-hook
  4655. 'org-show-block-all 'append 'local)))
  4656. ;;; Org-goto
  4657. (defvar org-goto-window-configuration nil)
  4658. (defvar org-goto-marker nil)
  4659. (defvar org-goto-map
  4660. (let ((map (make-sparse-keymap)))
  4661. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4662. (while (setq cmd (pop cmds))
  4663. (substitute-key-definition cmd cmd map global-map)))
  4664. (suppress-keymap map)
  4665. (org-defkey map "\C-m" 'org-goto-ret)
  4666. (org-defkey map [(return)] 'org-goto-ret)
  4667. (org-defkey map [(left)] 'org-goto-left)
  4668. (org-defkey map [(right)] 'org-goto-right)
  4669. (org-defkey map [(control ?g)] 'org-goto-quit)
  4670. (org-defkey map "\C-i" 'org-cycle)
  4671. (org-defkey map [(tab)] 'org-cycle)
  4672. (org-defkey map [(down)] 'outline-next-visible-heading)
  4673. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4674. (if org-goto-auto-isearch
  4675. (if (fboundp 'define-key-after)
  4676. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4677. nil)
  4678. (org-defkey map "q" 'org-goto-quit)
  4679. (org-defkey map "n" 'outline-next-visible-heading)
  4680. (org-defkey map "p" 'outline-previous-visible-heading)
  4681. (org-defkey map "f" 'outline-forward-same-level)
  4682. (org-defkey map "b" 'outline-backward-same-level)
  4683. (org-defkey map "u" 'outline-up-heading))
  4684. (org-defkey map "/" 'org-occur)
  4685. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4686. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4687. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4688. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4689. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4690. map))
  4691. (defconst org-goto-help
  4692. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4693. RET=jump to location [Q]uit and return to previous location
  4694. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4695. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4696. ;; FIXME: Docstring doe not mention both interfaces
  4697. (defun org-goto (&optional alternative-interface)
  4698. "Look up a different location in the current file, keeping current visibility.
  4699. When you want look-up or go to a different location in a document, the
  4700. fastest way is often to fold the entire buffer and then dive into the tree.
  4701. This method has the disadvantage, that the previous location will be folded,
  4702. which may not be what you want.
  4703. This command works around this by showing a copy of the current buffer
  4704. in an indirect buffer, in overview mode. You can dive into the tree in
  4705. that copy, use org-occur and incremental search to find a location.
  4706. When pressing RET or `Q', the command returns to the original buffer in
  4707. which the visibility is still unchanged. After RET is will also jump to
  4708. the location selected in the indirect buffer and expose the
  4709. the headline hierarchy above."
  4710. (interactive "P")
  4711. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4712. (org-refile-use-outline-path t)
  4713. (org-refile-target-verify-function nil)
  4714. (interface
  4715. (if (not alternative-interface)
  4716. org-goto-interface
  4717. (if (eq org-goto-interface 'outline)
  4718. 'outline-path-completion
  4719. 'outline)))
  4720. (org-goto-start-pos (point))
  4721. (selected-point
  4722. (if (eq interface 'outline)
  4723. (car (org-get-location (current-buffer) org-goto-help))
  4724. (nth 3 (org-refile-get-location "Goto: ")))))
  4725. (if selected-point
  4726. (progn
  4727. (org-mark-ring-push org-goto-start-pos)
  4728. (goto-char selected-point)
  4729. (if (or (org-invisible-p) (org-invisible-p2))
  4730. (org-show-context 'org-goto)))
  4731. (message "Quit"))))
  4732. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4733. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4734. (defvar org-goto-local-auto-isearch-map) ; defined below
  4735. (defun org-get-location (buf help)
  4736. "Let the user select a location in the Org-mode buffer BUF.
  4737. This function uses a recursive edit. It returns the selected position
  4738. or nil."
  4739. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4740. (isearch-hide-immediately nil)
  4741. (isearch-search-fun-function
  4742. (lambda () 'org-goto-local-search-headings))
  4743. (org-goto-selected-point org-goto-exit-command))
  4744. (save-excursion
  4745. (save-window-excursion
  4746. (delete-other-windows)
  4747. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4748. (switch-to-buffer
  4749. (condition-case nil
  4750. (make-indirect-buffer (current-buffer) "*org-goto*")
  4751. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4752. (with-output-to-temp-buffer "*Help*"
  4753. (princ help))
  4754. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4755. (setq buffer-read-only nil)
  4756. (let ((org-startup-truncated t)
  4757. (org-startup-folded nil)
  4758. (org-startup-align-all-tables nil))
  4759. (org-mode)
  4760. (org-overview))
  4761. (setq buffer-read-only t)
  4762. (if (and (boundp 'org-goto-start-pos)
  4763. (integer-or-marker-p org-goto-start-pos))
  4764. (let ((org-show-hierarchy-above t)
  4765. (org-show-siblings t)
  4766. (org-show-following-heading t))
  4767. (goto-char org-goto-start-pos)
  4768. (and (org-invisible-p) (org-show-context)))
  4769. (goto-char (point-min)))
  4770. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4771. (message "Select location and press RET")
  4772. (use-local-map org-goto-map)
  4773. (recursive-edit)
  4774. ))
  4775. (kill-buffer "*org-goto*")
  4776. (cons org-goto-selected-point org-goto-exit-command)))
  4777. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4778. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4779. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4780. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4781. (defun org-goto-local-search-headings (string bound noerror)
  4782. "Search and make sure that any matches are in headlines."
  4783. (catch 'return
  4784. (while (if isearch-forward
  4785. (search-forward string bound noerror)
  4786. (search-backward string bound noerror))
  4787. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4788. (and (member :headline context)
  4789. (not (member :tags context))))
  4790. (throw 'return (point))))))
  4791. (defun org-goto-local-auto-isearch ()
  4792. "Start isearch."
  4793. (interactive)
  4794. (goto-char (point-min))
  4795. (let ((keys (this-command-keys)))
  4796. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4797. (isearch-mode t)
  4798. (isearch-process-search-char (string-to-char keys)))))
  4799. (defun org-goto-ret (&optional arg)
  4800. "Finish `org-goto' by going to the new location."
  4801. (interactive "P")
  4802. (setq org-goto-selected-point (point)
  4803. org-goto-exit-command 'return)
  4804. (throw 'exit nil))
  4805. (defun org-goto-left ()
  4806. "Finish `org-goto' by going to the new location."
  4807. (interactive)
  4808. (if (org-on-heading-p)
  4809. (progn
  4810. (beginning-of-line 1)
  4811. (setq org-goto-selected-point (point)
  4812. org-goto-exit-command 'left)
  4813. (throw 'exit nil))
  4814. (error "Not on a heading")))
  4815. (defun org-goto-right ()
  4816. "Finish `org-goto' by going to the new location."
  4817. (interactive)
  4818. (if (org-on-heading-p)
  4819. (progn
  4820. (setq org-goto-selected-point (point)
  4821. org-goto-exit-command 'right)
  4822. (throw 'exit nil))
  4823. (error "Not on a heading")))
  4824. (defun org-goto-quit ()
  4825. "Finish `org-goto' without cursor motion."
  4826. (interactive)
  4827. (setq org-goto-selected-point nil)
  4828. (setq org-goto-exit-command 'quit)
  4829. (throw 'exit nil))
  4830. ;;; Indirect buffer display of subtrees
  4831. (defvar org-indirect-dedicated-frame nil
  4832. "This is the frame being used for indirect tree display.")
  4833. (defvar org-last-indirect-buffer nil)
  4834. (defun org-tree-to-indirect-buffer (&optional arg)
  4835. "Create indirect buffer and narrow it to current subtree.
  4836. With numerical prefix ARG, go up to this level and then take that tree.
  4837. If ARG is negative, go up that many levels.
  4838. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4839. indirect buffer previously made with this command, to avoid proliferation of
  4840. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4841. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4842. is kept so that you can work with several indirect buffers at the same time.
  4843. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4844. requests that a new frame be made for the new buffer, so that the dedicated
  4845. frame is not changed."
  4846. (interactive "P")
  4847. (let ((cbuf (current-buffer))
  4848. (cwin (selected-window))
  4849. (pos (point))
  4850. beg end level heading ibuf)
  4851. (save-excursion
  4852. (org-back-to-heading t)
  4853. (when (numberp arg)
  4854. (setq level (org-outline-level))
  4855. (if (< arg 0) (setq arg (+ level arg)))
  4856. (while (> (setq level (org-outline-level)) arg)
  4857. (outline-up-heading 1 t)))
  4858. (setq beg (point)
  4859. heading (org-get-heading))
  4860. (org-end-of-subtree t) (setq end (point)))
  4861. (if (and (buffer-live-p org-last-indirect-buffer)
  4862. (not (eq org-indirect-buffer-display 'new-frame))
  4863. (not arg))
  4864. (kill-buffer org-last-indirect-buffer))
  4865. (setq ibuf (org-get-indirect-buffer cbuf)
  4866. org-last-indirect-buffer ibuf)
  4867. (cond
  4868. ((or (eq org-indirect-buffer-display 'new-frame)
  4869. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4870. (select-frame (make-frame))
  4871. (delete-other-windows)
  4872. (switch-to-buffer ibuf)
  4873. (org-set-frame-title heading))
  4874. ((eq org-indirect-buffer-display 'dedicated-frame)
  4875. (raise-frame
  4876. (select-frame (or (and org-indirect-dedicated-frame
  4877. (frame-live-p org-indirect-dedicated-frame)
  4878. org-indirect-dedicated-frame)
  4879. (setq org-indirect-dedicated-frame (make-frame)))))
  4880. (delete-other-windows)
  4881. (switch-to-buffer ibuf)
  4882. (org-set-frame-title (concat "Indirect: " heading)))
  4883. ((eq org-indirect-buffer-display 'current-window)
  4884. (switch-to-buffer ibuf))
  4885. ((eq org-indirect-buffer-display 'other-window)
  4886. (pop-to-buffer ibuf))
  4887. (t (error "Invalid value.")))
  4888. (if (featurep 'xemacs)
  4889. (save-excursion (org-mode) (turn-on-font-lock)))
  4890. (narrow-to-region beg end)
  4891. (show-all)
  4892. (goto-char pos)
  4893. (and (window-live-p cwin) (select-window cwin))))
  4894. (defun org-get-indirect-buffer (&optional buffer)
  4895. (setq buffer (or buffer (current-buffer)))
  4896. (let ((n 1) (base (buffer-name buffer)) bname)
  4897. (while (buffer-live-p
  4898. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4899. (setq n (1+ n)))
  4900. (condition-case nil
  4901. (make-indirect-buffer buffer bname 'clone)
  4902. (error (make-indirect-buffer buffer bname)))))
  4903. (defun org-set-frame-title (title)
  4904. "Set the title of the current frame to the string TITLE."
  4905. ;; FIXME: how to name a single frame in XEmacs???
  4906. (unless (featurep 'xemacs)
  4907. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4908. ;;;; Structure editing
  4909. ;;; Inserting headlines
  4910. (defun org-previous-line-empty-p ()
  4911. (save-excursion
  4912. (and (not (bobp))
  4913. (or (beginning-of-line 0) t)
  4914. (save-match-data
  4915. (looking-at "[ \t]*$")))))
  4916. (defun org-insert-heading (&optional force-heading)
  4917. "Insert a new heading or item with same depth at point.
  4918. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4919. If point is at the beginning of a headline, insert a sibling before the
  4920. current headline. If point is not at the beginning, do not split the line,
  4921. but create the new headline after the current line."
  4922. (interactive "P")
  4923. (if (= (buffer-size) 0)
  4924. (insert "\n* ")
  4925. (when (or force-heading (not (org-insert-item)))
  4926. (let* ((empty-line-p nil)
  4927. (head (save-excursion
  4928. (condition-case nil
  4929. (progn
  4930. (org-back-to-heading)
  4931. (setq empty-line-p (org-previous-line-empty-p))
  4932. (match-string 0))
  4933. (error "*"))))
  4934. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4935. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4936. pos hide-previous previous-pos)
  4937. (cond
  4938. ((and (org-on-heading-p) (bolp)
  4939. (or (bobp)
  4940. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4941. ;; insert before the current line
  4942. (open-line (if blank 2 1)))
  4943. ((and (bolp)
  4944. (or (bobp)
  4945. (save-excursion
  4946. (backward-char 1) (not (org-invisible-p)))))
  4947. ;; insert right here
  4948. nil)
  4949. (t
  4950. ;; somewhere in the line
  4951. (save-excursion
  4952. (setq previous-pos (point-at-bol))
  4953. (end-of-line)
  4954. (setq hide-previous (org-invisible-p)))
  4955. (and org-insert-heading-respect-content (org-show-subtree))
  4956. (let ((split
  4957. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4958. (save-excursion
  4959. (let ((p (point)))
  4960. (goto-char (point-at-bol))
  4961. (and (looking-at org-complex-heading-regexp)
  4962. (> p (match-beginning 4)))))))
  4963. tags pos)
  4964. (cond
  4965. (org-insert-heading-respect-content
  4966. (org-end-of-subtree nil t)
  4967. (or (bolp) (newline))
  4968. (or (org-previous-line-empty-p)
  4969. (and blank (newline)))
  4970. (open-line 1))
  4971. ((org-on-heading-p)
  4972. (when hide-previous
  4973. (show-children)
  4974. (org-show-entry))
  4975. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4976. (setq tags (and (match-end 2) (match-string 2)))
  4977. (and (match-end 1)
  4978. (delete-region (match-beginning 1) (match-end 1)))
  4979. (setq pos (point-at-bol))
  4980. (or split (end-of-line 1))
  4981. (delete-horizontal-space)
  4982. (newline (if blank 2 1))
  4983. (when tags
  4984. (save-excursion
  4985. (goto-char pos)
  4986. (end-of-line 1)
  4987. (insert " " tags)
  4988. (org-set-tags nil 'align))))
  4989. (t
  4990. (or split (end-of-line 1))
  4991. (newline (if blank 2 1)))))))
  4992. (insert head) (just-one-space)
  4993. (setq pos (point))
  4994. (end-of-line 1)
  4995. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4996. (when (and org-insert-heading-respect-content hide-previous)
  4997. (save-excursion
  4998. (goto-char previous-pos)
  4999. (hide-subtree)))
  5000. (run-hooks 'org-insert-heading-hook)))))
  5001. (defun org-get-heading (&optional no-tags)
  5002. "Return the heading of the current entry, without the stars."
  5003. (save-excursion
  5004. (org-back-to-heading t)
  5005. (if (looking-at
  5006. (if no-tags
  5007. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5008. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5009. (match-string 1) "")))
  5010. (defun org-heading-components ()
  5011. "Return the components of the current heading.
  5012. This is a list with the following elements:
  5013. - the level as an integer
  5014. - the reduced level, different if `org-odd-levels-only' is set.
  5015. - the TODO keyword, or nil
  5016. - the priority character, like ?A, or nil if no priority is given
  5017. - the headline text itself, or the tags string if no headline text
  5018. - the tags string, or nil."
  5019. (save-excursion
  5020. (org-back-to-heading t)
  5021. (if (looking-at org-complex-heading-regexp)
  5022. (list (length (match-string 1))
  5023. (org-reduced-level (length (match-string 1)))
  5024. (org-match-string-no-properties 2)
  5025. (and (match-end 3) (aref (match-string 3) 2))
  5026. (org-match-string-no-properties 4)
  5027. (org-match-string-no-properties 5)))))
  5028. (defun org-get-entry ()
  5029. "Get the entry text, after heading, entire subtree."
  5030. (save-excursion
  5031. (org-back-to-heading t)
  5032. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5033. (defun org-insert-heading-after-current ()
  5034. "Insert a new heading with same level as current, after current subtree."
  5035. (interactive)
  5036. (org-back-to-heading)
  5037. (org-insert-heading)
  5038. (org-move-subtree-down)
  5039. (end-of-line 1))
  5040. (defun org-insert-heading-respect-content ()
  5041. (interactive)
  5042. (let ((org-insert-heading-respect-content t))
  5043. (org-insert-heading t)))
  5044. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5045. (interactive "P")
  5046. (let ((org-insert-heading-respect-content t))
  5047. (org-insert-todo-heading force-state t)))
  5048. (defun org-insert-todo-heading (arg &optional force-heading)
  5049. "Insert a new heading with the same level and TODO state as current heading.
  5050. If the heading has no TODO state, or if the state is DONE, use the first
  5051. state (TODO by default). Also with prefix arg, force first state."
  5052. (interactive "P")
  5053. (when (or force-heading (not (org-insert-item 'checkbox)))
  5054. (org-insert-heading force-heading)
  5055. (save-excursion
  5056. (org-back-to-heading)
  5057. (outline-previous-heading)
  5058. (looking-at org-todo-line-regexp))
  5059. (let*
  5060. ((new-mark-x
  5061. (if (or arg
  5062. (not (match-beginning 2))
  5063. (member (match-string 2) org-done-keywords))
  5064. (car org-todo-keywords-1)
  5065. (match-string 2)))
  5066. (new-mark
  5067. (or
  5068. (run-hook-with-args-until-success
  5069. 'org-todo-get-default-hook new-mark-x nil)
  5070. new-mark-x)))
  5071. (beginning-of-line 1)
  5072. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5073. (if org-treat-insert-todo-heading-as-state-change
  5074. (org-todo new-mark)
  5075. (insert new-mark " "))))
  5076. (when org-provide-todo-statistics
  5077. (org-update-parent-todo-statistics))))
  5078. (defun org-insert-subheading (arg)
  5079. "Insert a new subheading and demote it.
  5080. Works for outline headings and for plain lists alike."
  5081. (interactive "P")
  5082. (org-insert-heading arg)
  5083. (cond
  5084. ((org-on-heading-p) (org-do-demote))
  5085. ((org-at-item-p) (org-indent-item 1))))
  5086. (defun org-insert-todo-subheading (arg)
  5087. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5088. Works for outline headings and for plain lists alike."
  5089. (interactive "P")
  5090. (org-insert-todo-heading arg)
  5091. (cond
  5092. ((org-on-heading-p) (org-do-demote))
  5093. ((org-at-item-p) (org-indent-item 1))))
  5094. ;;; Promotion and Demotion
  5095. (defun org-promote-subtree ()
  5096. "Promote the entire subtree.
  5097. See also `org-promote'."
  5098. (interactive)
  5099. (save-excursion
  5100. (org-map-tree 'org-promote))
  5101. (org-fix-position-after-promote))
  5102. (defun org-demote-subtree ()
  5103. "Demote the entire subtree. See `org-demote'.
  5104. See also `org-promote'."
  5105. (interactive)
  5106. (save-excursion
  5107. (org-map-tree 'org-demote))
  5108. (org-fix-position-after-promote))
  5109. (defun org-do-promote ()
  5110. "Promote the current heading higher up the tree.
  5111. If the region is active in `transient-mark-mode', promote all headings
  5112. in the region."
  5113. (interactive)
  5114. (save-excursion
  5115. (if (org-region-active-p)
  5116. (org-map-region 'org-promote (region-beginning) (region-end))
  5117. (org-promote)))
  5118. (org-fix-position-after-promote))
  5119. (defun org-do-demote ()
  5120. "Demote the current heading lower down the tree.
  5121. If the region is active in `transient-mark-mode', demote all headings
  5122. in the region."
  5123. (interactive)
  5124. (save-excursion
  5125. (if (org-region-active-p)
  5126. (org-map-region 'org-demote (region-beginning) (region-end))
  5127. (org-demote)))
  5128. (org-fix-position-after-promote))
  5129. (defun org-fix-position-after-promote ()
  5130. "Make sure that after pro/demotion cursor position is right."
  5131. (let ((pos (point)))
  5132. (when (save-excursion
  5133. (beginning-of-line 1)
  5134. (looking-at org-todo-line-regexp)
  5135. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5136. (cond ((eobp) (insert " "))
  5137. ((eolp) (insert " "))
  5138. ((equal (char-after) ?\ ) (forward-char 1))))))
  5139. (defun org-reduced-level (l)
  5140. "Compute the effective level of a heading.
  5141. This takes into account the setting of `org-odd-levels-only'."
  5142. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5143. (defun org-get-valid-level (level &optional change)
  5144. "Rectify a level change under the influence of `org-odd-levels-only'
  5145. LEVEL is a current level, CHANGE is by how much the level should be
  5146. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5147. even level numbers will become the next higher odd number."
  5148. (if org-odd-levels-only
  5149. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5150. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5151. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5152. (max 1 (+ level (or change 0)))))
  5153. (if (boundp 'define-obsolete-function-alias)
  5154. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5155. (define-obsolete-function-alias 'org-get-legal-level
  5156. 'org-get-valid-level)
  5157. (define-obsolete-function-alias 'org-get-legal-level
  5158. 'org-get-valid-level "23.1")))
  5159. (defun org-promote ()
  5160. "Promote the current heading higher up the tree.
  5161. If the region is active in `transient-mark-mode', promote all headings
  5162. in the region."
  5163. (org-back-to-heading t)
  5164. (let* ((level (save-match-data (funcall outline-level)))
  5165. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5166. (diff (abs (- level (length up-head) -1))))
  5167. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5168. (replace-match up-head nil t)
  5169. ;; Fixup tag positioning
  5170. (and org-auto-align-tags (org-set-tags nil t))
  5171. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5172. (defun org-demote ()
  5173. "Demote the current heading lower down the tree.
  5174. If the region is active in `transient-mark-mode', demote all headings
  5175. in the region."
  5176. (org-back-to-heading t)
  5177. (let* ((level (save-match-data (funcall outline-level)))
  5178. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5179. (diff (abs (- level (length down-head) -1))))
  5180. (replace-match down-head nil t)
  5181. ;; Fixup tag positioning
  5182. (and org-auto-align-tags (org-set-tags nil t))
  5183. (if org-adapt-indentation (org-fixup-indentation diff))))
  5184. (defun org-map-tree (fun)
  5185. "Call FUN for every heading underneath the current one."
  5186. (org-back-to-heading)
  5187. (let ((level (funcall outline-level)))
  5188. (save-excursion
  5189. (funcall fun)
  5190. (while (and (progn
  5191. (outline-next-heading)
  5192. (> (funcall outline-level) level))
  5193. (not (eobp)))
  5194. (funcall fun)))))
  5195. (defun org-map-region (fun beg end)
  5196. "Call FUN for every heading between BEG and END."
  5197. (let ((org-ignore-region t))
  5198. (save-excursion
  5199. (setq end (copy-marker end))
  5200. (goto-char beg)
  5201. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5202. (< (point) end))
  5203. (funcall fun))
  5204. (while (and (progn
  5205. (outline-next-heading)
  5206. (< (point) end))
  5207. (not (eobp)))
  5208. (funcall fun)))))
  5209. (defun org-fixup-indentation (diff)
  5210. "Change the indentation in the current entry by DIFF
  5211. However, if any line in the current entry has no indentation, or if it
  5212. would end up with no indentation after the change, nothing at all is done."
  5213. (save-excursion
  5214. (let ((end (save-excursion (outline-next-heading)
  5215. (point-marker)))
  5216. (prohibit (if (> diff 0)
  5217. "^\\S-"
  5218. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5219. col)
  5220. (unless (save-excursion (end-of-line 1)
  5221. (re-search-forward prohibit end t))
  5222. (while (and (< (point) end)
  5223. (re-search-forward "^[ \t]+" end t))
  5224. (goto-char (match-end 0))
  5225. (setq col (current-column))
  5226. (if (< diff 0) (replace-match ""))
  5227. (org-indent-to-column (+ diff col))))
  5228. (move-marker end nil))))
  5229. (defun org-convert-to-odd-levels ()
  5230. "Convert an org-mode file with all levels allowed to one with odd levels.
  5231. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5232. level 5 etc."
  5233. (interactive)
  5234. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5235. (let ((org-odd-levels-only nil) n)
  5236. (save-excursion
  5237. (goto-char (point-min))
  5238. (while (re-search-forward "^\\*\\*+ " nil t)
  5239. (setq n (- (length (match-string 0)) 2))
  5240. (while (>= (setq n (1- n)) 0)
  5241. (org-demote))
  5242. (end-of-line 1))))))
  5243. (defun org-convert-to-oddeven-levels ()
  5244. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5245. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5246. section with an even level, conversion would destroy the structure of the file. An error
  5247. is signaled in this case."
  5248. (interactive)
  5249. (goto-char (point-min))
  5250. ;; First check if there are no even levels
  5251. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5252. (org-show-context t)
  5253. (error "Not all levels are odd in this file. Conversion not possible."))
  5254. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5255. (let ((org-odd-levels-only nil) n)
  5256. (save-excursion
  5257. (goto-char (point-min))
  5258. (while (re-search-forward "^\\*\\*+ " nil t)
  5259. (setq n (/ (1- (length (match-string 0))) 2))
  5260. (while (>= (setq n (1- n)) 0)
  5261. (org-promote))
  5262. (end-of-line 1))))))
  5263. (defun org-tr-level (n)
  5264. "Make N odd if required."
  5265. (if org-odd-levels-only (1+ (/ n 2)) n))
  5266. ;;; Vertical tree motion, cutting and pasting of subtrees
  5267. (defun org-move-subtree-up (&optional arg)
  5268. "Move the current subtree up past ARG headlines of the same level."
  5269. (interactive "p")
  5270. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5271. (defun org-move-subtree-down (&optional arg)
  5272. "Move the current subtree down past ARG headlines of the same level."
  5273. (interactive "p")
  5274. (setq arg (prefix-numeric-value arg))
  5275. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5276. 'outline-get-last-sibling))
  5277. (ins-point (make-marker))
  5278. (cnt (abs arg))
  5279. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5280. ;; Select the tree
  5281. (org-back-to-heading)
  5282. (setq beg0 (point))
  5283. (save-excursion
  5284. (setq ne-beg (org-back-over-empty-lines))
  5285. (setq beg (point)))
  5286. (save-match-data
  5287. (save-excursion (outline-end-of-heading)
  5288. (setq folded (org-invisible-p)))
  5289. (outline-end-of-subtree))
  5290. (outline-next-heading)
  5291. (setq ne-end (org-back-over-empty-lines))
  5292. (setq end (point))
  5293. (goto-char beg0)
  5294. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5295. ;; include less whitespace
  5296. (save-excursion
  5297. (goto-char beg)
  5298. (forward-line (- ne-beg ne-end))
  5299. (setq beg (point))))
  5300. ;; Find insertion point, with error handling
  5301. (while (> cnt 0)
  5302. (or (and (funcall movfunc) (looking-at outline-regexp))
  5303. (progn (goto-char beg0)
  5304. (error "Cannot move past superior level or buffer limit")))
  5305. (setq cnt (1- cnt)))
  5306. (if (> arg 0)
  5307. ;; Moving forward - still need to move over subtree
  5308. (progn (org-end-of-subtree t t)
  5309. (save-excursion
  5310. (org-back-over-empty-lines)
  5311. (or (bolp) (newline)))))
  5312. (setq ne-ins (org-back-over-empty-lines))
  5313. (move-marker ins-point (point))
  5314. (setq txt (buffer-substring beg end))
  5315. (org-save-markers-in-region beg end)
  5316. (delete-region beg end)
  5317. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5318. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5319. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5320. (let ((bbb (point)))
  5321. (insert-before-markers txt)
  5322. (org-reinstall-markers-in-region bbb)
  5323. (move-marker ins-point bbb))
  5324. (or (bolp) (insert "\n"))
  5325. (setq ins-end (point))
  5326. (goto-char ins-point)
  5327. (org-skip-whitespace)
  5328. (when (and (< arg 0)
  5329. (org-first-sibling-p)
  5330. (> ne-ins ne-beg))
  5331. ;; Move whitespace back to beginning
  5332. (save-excursion
  5333. (goto-char ins-end)
  5334. (let ((kill-whole-line t))
  5335. (kill-line (- ne-ins ne-beg)) (point)))
  5336. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5337. (move-marker ins-point nil)
  5338. (org-compact-display-after-subtree-move)
  5339. (org-show-empty-lines-in-parent)
  5340. (unless folded
  5341. (org-show-entry)
  5342. (show-children)
  5343. (org-cycle-hide-drawers 'children))))
  5344. (defvar org-subtree-clip ""
  5345. "Clipboard for cut and paste of subtrees.
  5346. This is actually only a copy of the kill, because we use the normal kill
  5347. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5348. (defvar org-subtree-clip-folded nil
  5349. "Was the last copied subtree folded?
  5350. This is used to fold the tree back after pasting.")
  5351. (defun org-cut-subtree (&optional n)
  5352. "Cut the current subtree into the clipboard.
  5353. With prefix arg N, cut this many sequential subtrees.
  5354. This is a short-hand for marking the subtree and then cutting it."
  5355. (interactive "p")
  5356. (org-copy-subtree n 'cut))
  5357. (defun org-copy-subtree (&optional n cut force-store-markers)
  5358. "Cut the current subtree into the clipboard.
  5359. With prefix arg N, cut this many sequential subtrees.
  5360. This is a short-hand for marking the subtree and then copying it.
  5361. If CUT is non-nil, actually cut the subtree.
  5362. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5363. of some markers in the region, even if CUT is non-nil. This is
  5364. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5365. (interactive "p")
  5366. (let (beg end folded (beg0 (point)))
  5367. (if (interactive-p)
  5368. (org-back-to-heading nil) ; take what looks like a subtree
  5369. (org-back-to-heading t)) ; take what is really there
  5370. (org-back-over-empty-lines)
  5371. (setq beg (point))
  5372. (skip-chars-forward " \t\r\n")
  5373. (save-match-data
  5374. (save-excursion (outline-end-of-heading)
  5375. (setq folded (org-invisible-p)))
  5376. (condition-case nil
  5377. (outline-forward-same-level (1- n))
  5378. (error nil))
  5379. (org-end-of-subtree t t))
  5380. (org-back-over-empty-lines)
  5381. (setq end (point))
  5382. (goto-char beg0)
  5383. (when (> end beg)
  5384. (setq org-subtree-clip-folded folded)
  5385. (when (or cut force-store-markers)
  5386. (org-save-markers-in-region beg end))
  5387. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5388. (setq org-subtree-clip (current-kill 0))
  5389. (message "%s: Subtree(s) with %d characters"
  5390. (if cut "Cut" "Copied")
  5391. (length org-subtree-clip)))))
  5392. (defun org-paste-subtree (&optional level tree for-yank)
  5393. "Paste the clipboard as a subtree, with modification of headline level.
  5394. The entire subtree is promoted or demoted in order to match a new headline
  5395. level.
  5396. If the cursor is at the beginning of a headline, the same level as
  5397. that headline is used to paste the tree
  5398. If not, the new level is derived from the *visible* headings
  5399. before and after the insertion point, and taken to be the inferior headline
  5400. level of the two. So if the previous visible heading is level 3 and the
  5401. next is level 4 (or vice versa), level 4 will be used for insertion.
  5402. This makes sure that the subtree remains an independent subtree and does
  5403. not swallow low level entries.
  5404. You can also force a different level, either by using a numeric prefix
  5405. argument, or by inserting the heading marker by hand. For example, if the
  5406. cursor is after \"*****\", then the tree will be shifted to level 5.
  5407. If optional TREE is given, use this text instead of the kill ring.
  5408. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5409. move back over whitespace before inserting, and move point to the end of
  5410. the inserted text when done."
  5411. (interactive "P")
  5412. (unless (org-kill-is-subtree-p tree)
  5413. (error "%s"
  5414. (substitute-command-keys
  5415. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5416. (let* ((visp (not (org-invisible-p)))
  5417. (txt (or tree (and kill-ring (current-kill 0))))
  5418. (^re (concat "^\\(" outline-regexp "\\)"))
  5419. (re (concat "\\(" outline-regexp "\\)"))
  5420. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5421. (old-level (if (string-match ^re txt)
  5422. (- (match-end 0) (match-beginning 0) 1)
  5423. -1))
  5424. (force-level (cond (level (prefix-numeric-value level))
  5425. ((and (looking-at "[ \t]*$")
  5426. (string-match
  5427. ^re_ (buffer-substring
  5428. (point-at-bol) (point))))
  5429. (- (match-end 1) (match-beginning 1)))
  5430. ((and (bolp)
  5431. (looking-at org-outline-regexp))
  5432. (- (match-end 0) (point) 1))
  5433. (t nil)))
  5434. (previous-level (save-excursion
  5435. (condition-case nil
  5436. (progn
  5437. (outline-previous-visible-heading 1)
  5438. (if (looking-at re)
  5439. (- (match-end 0) (match-beginning 0) 1)
  5440. 1))
  5441. (error 1))))
  5442. (next-level (save-excursion
  5443. (condition-case nil
  5444. (progn
  5445. (or (looking-at outline-regexp)
  5446. (outline-next-visible-heading 1))
  5447. (if (looking-at re)
  5448. (- (match-end 0) (match-beginning 0) 1)
  5449. 1))
  5450. (error 1))))
  5451. (new-level (or force-level (max previous-level next-level)))
  5452. (shift (if (or (= old-level -1)
  5453. (= new-level -1)
  5454. (= old-level new-level))
  5455. 0
  5456. (- new-level old-level)))
  5457. (delta (if (> shift 0) -1 1))
  5458. (func (if (> shift 0) 'org-demote 'org-promote))
  5459. (org-odd-levels-only nil)
  5460. beg end newend)
  5461. ;; Remove the forced level indicator
  5462. (if force-level
  5463. (delete-region (point-at-bol) (point)))
  5464. ;; Paste
  5465. (beginning-of-line 1)
  5466. (unless for-yank (org-back-over-empty-lines))
  5467. (setq beg (point))
  5468. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5469. (insert-before-markers txt)
  5470. (unless (string-match "\n\\'" txt) (insert "\n"))
  5471. (setq newend (point))
  5472. (org-reinstall-markers-in-region beg)
  5473. (setq end (point))
  5474. (goto-char beg)
  5475. (skip-chars-forward " \t\n\r")
  5476. (setq beg (point))
  5477. (if (and (org-invisible-p) visp)
  5478. (save-excursion (outline-show-heading)))
  5479. ;; Shift if necessary
  5480. (unless (= shift 0)
  5481. (save-restriction
  5482. (narrow-to-region beg end)
  5483. (while (not (= shift 0))
  5484. (org-map-region func (point-min) (point-max))
  5485. (setq shift (+ delta shift)))
  5486. (goto-char (point-min))
  5487. (setq newend (point-max))))
  5488. (when (or (interactive-p) for-yank)
  5489. (message "Clipboard pasted as level %d subtree" new-level))
  5490. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5491. kill-ring
  5492. (eq org-subtree-clip (current-kill 0))
  5493. org-subtree-clip-folded)
  5494. ;; The tree was folded before it was killed/copied
  5495. (hide-subtree))
  5496. (and for-yank (goto-char newend))))
  5497. (defun org-kill-is-subtree-p (&optional txt)
  5498. "Check if the current kill is an outline subtree, or a set of trees.
  5499. Returns nil if kill does not start with a headline, or if the first
  5500. headline level is not the largest headline level in the tree.
  5501. So this will actually accept several entries of equal levels as well,
  5502. which is OK for `org-paste-subtree'.
  5503. If optional TXT is given, check this string instead of the current kill."
  5504. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5505. (start-level (and kill
  5506. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5507. org-outline-regexp "\\)")
  5508. kill)
  5509. (- (match-end 2) (match-beginning 2) 1)))
  5510. (re (concat "^" org-outline-regexp))
  5511. (start (1+ (or (match-beginning 2) -1))))
  5512. (if (not start-level)
  5513. (progn
  5514. nil) ;; does not even start with a heading
  5515. (catch 'exit
  5516. (while (setq start (string-match re kill (1+ start)))
  5517. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5518. (throw 'exit nil)))
  5519. t))))
  5520. (defvar org-markers-to-move nil
  5521. "Markers that should be moved with a cut-and-paste operation.
  5522. Those markers are stored together with their positions relative to
  5523. the start of the region.")
  5524. (defun org-save-markers-in-region (beg end)
  5525. "Check markers in region.
  5526. If these markers are between BEG and END, record their position relative
  5527. to BEG, so that after moving the block of text, we can put the markers back
  5528. into place.
  5529. This function gets called just before an entry or tree gets cut from the
  5530. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5531. called immediately, to move the markers with the entries."
  5532. (setq org-markers-to-move nil)
  5533. (when (featurep 'org-clock)
  5534. (org-clock-save-markers-for-cut-and-paste beg end))
  5535. (when (featurep 'org-agenda)
  5536. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5537. (defun org-check-and-save-marker (marker beg end)
  5538. "Check if MARKER is between BEG and END.
  5539. If yes, remember the marker and the distance to BEG."
  5540. (when (and (marker-buffer marker)
  5541. (equal (marker-buffer marker) (current-buffer)))
  5542. (if (and (>= marker beg) (< marker end))
  5543. (push (cons marker (- marker beg)) org-markers-to-move))))
  5544. (defun org-reinstall-markers-in-region (beg)
  5545. "Move all remembered markers to their position relative to BEG."
  5546. (mapc (lambda (x)
  5547. (move-marker (car x) (+ beg (cdr x))))
  5548. org-markers-to-move)
  5549. (setq org-markers-to-move nil))
  5550. (defun org-narrow-to-subtree ()
  5551. "Narrow buffer to the current subtree."
  5552. (interactive)
  5553. (save-excursion
  5554. (save-match-data
  5555. (narrow-to-region
  5556. (progn (org-back-to-heading) (point))
  5557. (progn (org-end-of-subtree t) (point))))))
  5558. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5559. "Clone the task (subtree) at point N times.
  5560. The clones will be inserted as siblings.
  5561. In interactive use, the user will be prompted for the number of clones
  5562. to be produced, and for a time SHIFT, which may be a repeater as used
  5563. in time stamps, for example `+3d'.
  5564. When a valid repeater is given and the entry contains any time stamps,
  5565. the clones will become a sequence in time, with time stamps in the
  5566. subtree shifted for each clone produced. If SHIFT is nil or the
  5567. empty string, time stamps will be left alone.
  5568. If the original subtree did contain time stamps with a repeater,
  5569. the following will happen:
  5570. - the repeater will be removed in each clone
  5571. - an additional clone will be produced, with the current, unshifted
  5572. date(s) in the entry.
  5573. - the original entry will be placed *after* all the clones, with
  5574. repeater intact.
  5575. - the start days in the repeater in the original entry will be shifted
  5576. to past the last clone.
  5577. I this way you can spell out a number of instances of a repeating task,
  5578. and still retain the repeater to cover future instances of the task."
  5579. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5580. (let (beg end template task
  5581. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5582. (if (not (and (integerp n) (> n 0)))
  5583. (error "Invalid number of replications %s" n))
  5584. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5585. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5586. shift)))
  5587. (error "Invalid shift specification %s" shift))
  5588. (when doshift
  5589. (setq shift-n (string-to-number (match-string 1 shift))
  5590. shift-what (cdr (assoc (match-string 2 shift)
  5591. '(("d" . day) ("w" . week)
  5592. ("m" . month) ("y" . year))))))
  5593. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5594. (setq nmin 1 nmax n)
  5595. (org-back-to-heading t)
  5596. (setq beg (point))
  5597. (org-end-of-subtree t t)
  5598. (setq end (point))
  5599. (setq template (buffer-substring beg end))
  5600. (when (and doshift
  5601. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5602. (delete-region beg end)
  5603. (setq end beg)
  5604. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5605. (goto-char end)
  5606. (loop for n from nmin to nmax do
  5607. (if (not doshift)
  5608. (setq task template)
  5609. (with-temp-buffer
  5610. (insert template)
  5611. (org-mode)
  5612. (goto-char (point-min))
  5613. (while (re-search-forward org-ts-regexp nil t)
  5614. (org-timestamp-change (* n shift-n) shift-what))
  5615. (unless (= n n-no-remove)
  5616. (goto-char (point-min))
  5617. (while (re-search-forward org-ts-regexp nil t)
  5618. (save-excursion
  5619. (goto-char (match-beginning 0))
  5620. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5621. (delete-region (match-beginning 1) (match-end 1))))))
  5622. (setq task (buffer-string))))
  5623. (insert task))
  5624. (goto-char beg)))
  5625. ;;; Outline Sorting
  5626. (defun org-sort (with-case)
  5627. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5628. Optional argument WITH-CASE means sort case-sensitively.
  5629. With a double prefix argument, also remove duplicate entries."
  5630. (interactive "P")
  5631. (if (org-at-table-p)
  5632. (org-call-with-arg 'org-table-sort-lines with-case)
  5633. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5634. (defun org-sort-remove-invisible (s)
  5635. (remove-text-properties 0 (length s) org-rm-props s)
  5636. (while (string-match org-bracket-link-regexp s)
  5637. (setq s (replace-match (if (match-end 2)
  5638. (match-string 3 s)
  5639. (match-string 1 s)) t t s)))
  5640. s)
  5641. (defvar org-priority-regexp) ; defined later in the file
  5642. (defvar org-after-sorting-entries-or-items-hook nil
  5643. "Hook that is run after a bunch of entries or items have been sorted.
  5644. When children are sorted, the cursor is in the parent line when this
  5645. hook gets called. When a region or a plain list is sorted, the cursor
  5646. will be in the first entry of the sorted region/list.")
  5647. (defun org-sort-entries-or-items
  5648. (&optional with-case sorting-type getkey-func compare-func property)
  5649. "Sort entries on a certain level of an outline tree, or plain list items.
  5650. If there is an active region, the entries in the region are sorted.
  5651. Else, if the cursor is before the first entry, sort the top-level items.
  5652. Else, the children of the entry at point are sorted.
  5653. If the cursor is at the first item in a plain list, the list items will be
  5654. sorted.
  5655. Sorting can be alphabetically, numerically, by date/time as given by
  5656. a time stamp, by a property or by priority.
  5657. The command prompts for the sorting type unless it has been given to the
  5658. function through the SORTING-TYPE argument, which needs to a character,
  5659. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5660. precise meaning of each character:
  5661. n Numerically, by converting the beginning of the entry/item to a number.
  5662. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5663. t By date/time, either the first active time stamp in the entry, or, if
  5664. none exist, by the first inactive one.
  5665. In items, only the first line will be chekced.
  5666. s By the scheduled date/time.
  5667. d By deadline date/time.
  5668. c By creation time, which is assumed to be the first inactive time stamp
  5669. at the beginning of a line.
  5670. p By priority according to the cookie.
  5671. r By the value of a property.
  5672. Capital letters will reverse the sort order.
  5673. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5674. called with point at the beginning of the record. It must return either
  5675. a string or a number that should serve as the sorting key for that record.
  5676. Comparing entries ignores case by default. However, with an optional argument
  5677. WITH-CASE, the sorting considers case as well."
  5678. (interactive "P")
  5679. (let ((case-func (if with-case 'identity 'downcase))
  5680. start beg end stars re re2
  5681. txt what tmp plain-list-p)
  5682. ;; Find beginning and end of region to sort
  5683. (cond
  5684. ((org-region-active-p)
  5685. ;; we will sort the region
  5686. (setq end (region-end)
  5687. what "region")
  5688. (goto-char (region-beginning))
  5689. (if (not (org-on-heading-p)) (outline-next-heading))
  5690. (setq start (point)))
  5691. ((org-at-item-p)
  5692. ;; we will sort this plain list
  5693. (org-beginning-of-item-list) (setq start (point))
  5694. (org-end-of-item-list) (setq end (point))
  5695. (goto-char start)
  5696. (setq plain-list-p t
  5697. what "plain list"))
  5698. ((or (org-on-heading-p)
  5699. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5700. ;; we will sort the children of the current headline
  5701. (org-back-to-heading)
  5702. (setq start (point)
  5703. end (progn (org-end-of-subtree t t)
  5704. (org-back-over-empty-lines)
  5705. (point))
  5706. what "children")
  5707. (goto-char start)
  5708. (show-subtree)
  5709. (outline-next-heading))
  5710. (t
  5711. ;; we will sort the top-level entries in this file
  5712. (goto-char (point-min))
  5713. (or (org-on-heading-p) (outline-next-heading))
  5714. (setq start (point) end (point-max) what "top-level")
  5715. (goto-char start)
  5716. (show-all)))
  5717. (setq beg (point))
  5718. (if (>= beg end) (error "Nothing to sort"))
  5719. (unless plain-list-p
  5720. (looking-at "\\(\\*+\\)")
  5721. (setq stars (match-string 1)
  5722. re (concat "^" (regexp-quote stars) " +")
  5723. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5724. txt (buffer-substring beg end))
  5725. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5726. (if (and (not (equal stars "*")) (string-match re2 txt))
  5727. (error "Region to sort contains a level above the first entry")))
  5728. (unless sorting-type
  5729. (message
  5730. (if plain-list-p
  5731. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5732. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5733. [t]ime [s]cheduled [d]eadline [c]reated
  5734. A/N/T/S/D/C/P/O/F means reversed:")
  5735. what)
  5736. (setq sorting-type (read-char-exclusive))
  5737. (and (= (downcase sorting-type) ?f)
  5738. (setq getkey-func
  5739. (org-ido-completing-read "Sort using function: "
  5740. obarray 'fboundp t nil nil))
  5741. (setq getkey-func (intern getkey-func)))
  5742. (and (= (downcase sorting-type) ?r)
  5743. (setq property
  5744. (org-ido-completing-read "Property: "
  5745. (mapcar 'list (org-buffer-property-keys t))
  5746. nil t))))
  5747. (message "Sorting entries...")
  5748. (save-restriction
  5749. (narrow-to-region start end)
  5750. (let ((dcst (downcase sorting-type))
  5751. (case-fold-search nil)
  5752. (now (current-time)))
  5753. (sort-subr
  5754. (/= dcst sorting-type)
  5755. ;; This function moves to the beginning character of the "record" to
  5756. ;; be sorted.
  5757. (if plain-list-p
  5758. (lambda nil
  5759. (if (org-at-item-p) t (goto-char (point-max))))
  5760. (lambda nil
  5761. (if (re-search-forward re nil t)
  5762. (goto-char (match-beginning 0))
  5763. (goto-char (point-max)))))
  5764. ;; This function moves to the last character of the "record" being
  5765. ;; sorted.
  5766. (if plain-list-p
  5767. 'org-end-of-item
  5768. (lambda nil
  5769. (save-match-data
  5770. (condition-case nil
  5771. (outline-forward-same-level 1)
  5772. (error
  5773. (goto-char (point-max)))))))
  5774. ;; This function returns the value that gets sorted against.
  5775. (if plain-list-p
  5776. (lambda nil
  5777. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5778. (cond
  5779. ((= dcst ?n)
  5780. (string-to-number (buffer-substring (match-end 0)
  5781. (point-at-eol))))
  5782. ((= dcst ?a)
  5783. (buffer-substring (match-end 0) (point-at-eol)))
  5784. ((= dcst ?t)
  5785. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5786. (re-search-forward org-ts-regexp-both
  5787. (point-at-eol) t))
  5788. (org-time-string-to-seconds (match-string 0))
  5789. (time-to-seconds now)))
  5790. ((= dcst ?f)
  5791. (if getkey-func
  5792. (progn
  5793. (setq tmp (funcall getkey-func))
  5794. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5795. tmp)
  5796. (error "Invalid key function `%s'" getkey-func)))
  5797. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5798. (lambda nil
  5799. (cond
  5800. ((= dcst ?n)
  5801. (if (looking-at org-complex-heading-regexp)
  5802. (string-to-number (match-string 4))
  5803. nil))
  5804. ((= dcst ?a)
  5805. (if (looking-at org-complex-heading-regexp)
  5806. (funcall case-func (match-string 4))
  5807. nil))
  5808. ((= dcst ?t)
  5809. (let ((end (save-excursion (outline-next-heading) (point))))
  5810. (if (or (re-search-forward org-ts-regexp end t)
  5811. (re-search-forward org-ts-regexp-both end t))
  5812. (org-time-string-to-seconds (match-string 0))
  5813. (time-to-seconds now))))
  5814. ((= dcst ?c)
  5815. (let ((end (save-excursion (outline-next-heading) (point))))
  5816. (if (re-search-forward
  5817. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5818. end t)
  5819. (org-time-string-to-seconds (match-string 0))
  5820. (time-to-seconds now))))
  5821. ((= dcst ?s)
  5822. (let ((end (save-excursion (outline-next-heading) (point))))
  5823. (if (re-search-forward org-scheduled-time-regexp end t)
  5824. (org-time-string-to-seconds (match-string 1))
  5825. (time-to-seconds now))))
  5826. ((= dcst ?d)
  5827. (let ((end (save-excursion (outline-next-heading) (point))))
  5828. (if (re-search-forward org-deadline-time-regexp end t)
  5829. (org-time-string-to-seconds (match-string 1))
  5830. (time-to-seconds now))))
  5831. ((= dcst ?p)
  5832. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5833. (string-to-char (match-string 2))
  5834. org-default-priority))
  5835. ((= dcst ?r)
  5836. (or (org-entry-get nil property) ""))
  5837. ((= dcst ?o)
  5838. (if (looking-at org-complex-heading-regexp)
  5839. (- 9999 (length (member (match-string 2)
  5840. org-todo-keywords-1)))))
  5841. ((= dcst ?f)
  5842. (if getkey-func
  5843. (progn
  5844. (setq tmp (funcall getkey-func))
  5845. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5846. tmp)
  5847. (error "Invalid key function `%s'" getkey-func)))
  5848. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5849. nil
  5850. (cond
  5851. ((= dcst ?a) 'string<)
  5852. ((= dcst ?t) 'time-less-p)
  5853. ((= dcst ?f) compare-func)
  5854. ((= dcst ?p) '<)
  5855. (t nil)))))
  5856. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5857. (message "Sorting entries...done")))
  5858. (defun org-do-sort (table what &optional with-case sorting-type)
  5859. "Sort TABLE of WHAT according to SORTING-TYPE.
  5860. The user will be prompted for the SORTING-TYPE if the call to this
  5861. function does not specify it. WHAT is only for the prompt, to indicate
  5862. what is being sorted. The sorting key will be extracted from
  5863. the car of the elements of the table.
  5864. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5865. (unless sorting-type
  5866. (message
  5867. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5868. what)
  5869. (setq sorting-type (read-char-exclusive)))
  5870. (let ((dcst (downcase sorting-type))
  5871. extractfun comparefun)
  5872. ;; Define the appropriate functions
  5873. (cond
  5874. ((= dcst ?n)
  5875. (setq extractfun 'string-to-number
  5876. comparefun (if (= dcst sorting-type) '< '>)))
  5877. ((= dcst ?a)
  5878. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5879. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5880. comparefun (if (= dcst sorting-type)
  5881. 'string<
  5882. (lambda (a b) (and (not (string< a b))
  5883. (not (string= a b)))))))
  5884. ((= dcst ?t)
  5885. (setq extractfun
  5886. (lambda (x)
  5887. (if (or (string-match org-ts-regexp x)
  5888. (string-match org-ts-regexp-both x))
  5889. (time-to-seconds
  5890. (org-time-string-to-time (match-string 0 x)))
  5891. 0))
  5892. comparefun (if (= dcst sorting-type) '< '>)))
  5893. (t (error "Invalid sorting type `%c'" sorting-type)))
  5894. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5895. table)
  5896. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5897. ;;; Editing source examples
  5898. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5899. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5900. (defvar org-edit-src-force-single-line nil)
  5901. (defvar org-edit-src-from-org-mode nil)
  5902. (defvar org-edit-src-picture nil)
  5903. (defvar org-edit-src-beg-marker nil)
  5904. (defvar org-edit-src-end-marker nil)
  5905. (defvar org-edit-src-overlay nil)
  5906. (defvar org-edit-src-nindent nil)
  5907. (define-minor-mode org-exit-edit-mode
  5908. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5909. (defun org-edit-src-code ()
  5910. "Edit the source code example at point.
  5911. The example is copied to a separate buffer, and that buffer is switched
  5912. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5913. This will remove the original code in the Org buffer, and replace it with
  5914. the edited version."
  5915. (interactive)
  5916. (let ((line (org-current-line))
  5917. (case-fold-search t)
  5918. (msg (substitute-command-keys
  5919. "Edit, then exit with C-c ' (C-c and single quote)"))
  5920. (info (org-edit-src-find-region-and-lang))
  5921. (org-mode-p (eq major-mode 'org-mode))
  5922. (beg (make-marker))
  5923. (end (make-marker))
  5924. nindent ovl lang lang-f single lfmt code begline buffer)
  5925. (if (not info)
  5926. nil
  5927. (setq beg (move-marker beg (nth 0 info))
  5928. end (move-marker end (nth 1 info))
  5929. code (buffer-substring-no-properties beg end)
  5930. lang (nth 2 info)
  5931. single (nth 3 info)
  5932. lfmt (nth 4 info)
  5933. lang-f (intern (concat lang "-mode"))
  5934. begline (save-excursion (goto-char beg) (org-current-line)))
  5935. (unless (functionp lang-f)
  5936. (error "No such language mode: %s" lang-f))
  5937. (goto-line line)
  5938. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5939. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5940. (switch-to-buffer buffer)
  5941. (when buffer
  5942. (with-current-buffer buffer
  5943. (if (boundp 'org-edit-src-overlay)
  5944. (org-delete-overlay org-edit-src-overlay)))
  5945. (kill-buffer buffer))
  5946. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5947. (setq ovl (org-make-overlay beg end))
  5948. (org-overlay-put ovl 'face 'secondary-selection)
  5949. (org-overlay-put ovl 'edit-buffer buffer)
  5950. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5951. (org-overlay-put ovl 'face 'secondary-selection)
  5952. (org-overlay-put ovl
  5953. 'keymap
  5954. (let ((map (make-sparse-keymap)))
  5955. (define-key map [mouse-1] 'org-edit-src-continue)
  5956. map))
  5957. (org-overlay-put ovl :read-only "Leave me alone")
  5958. (switch-to-buffer buffer)
  5959. (insert code)
  5960. (remove-text-properties (point-min) (point-max)
  5961. '(display nil invisible nil intangible nil))
  5962. (setq nindent (org-do-remove-indentation))
  5963. (let ((org-inhibit-startup t))
  5964. (funcall lang-f))
  5965. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5966. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5967. (when lfmt
  5968. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5969. (when org-mode-p
  5970. (goto-char (point-min))
  5971. (while (re-search-forward "^," nil t)
  5972. (replace-match "")))
  5973. (goto-line (1+ (- line begline)))
  5974. (org-exit-edit-mode)
  5975. (org-set-local 'org-edit-src-beg-marker beg)
  5976. (org-set-local 'org-edit-src-end-marker end)
  5977. (org-set-local 'org-edit-src-overlay ovl)
  5978. (org-set-local 'org-edit-src-nindent nindent)
  5979. (and org-edit-src-persistent-message
  5980. (org-set-local 'header-line-format msg)))
  5981. (message "%s" msg)
  5982. t)))
  5983. (defun org-edit-src-continue (e)
  5984. (interactive "e")
  5985. (mouse-set-point e)
  5986. (let ((buf (get-char-property (point) 'edit-buffer)))
  5987. (if buf (switch-to-buffer buf)
  5988. (error "Something is wrong here"))))
  5989. (defun org-edit-src-find-buffer (beg end)
  5990. "Find a source editing buffer that is already editing the region BEG to END."
  5991. (catch 'exit
  5992. (mapc
  5993. (lambda (b)
  5994. (with-current-buffer b
  5995. (if (and (string-match "\\`*Org Edit " (buffer-name))
  5996. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  5997. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  5998. (equal beg org-edit-src-beg-marker)
  5999. (equal end org-edit-src-end-marker))
  6000. (throw 'exit (current-buffer)))))
  6001. (buffer-list))
  6002. nil))
  6003. (defun org-edit-fixed-width-region ()
  6004. "Edit the fixed-width ascii drawing at point.
  6005. This must be a region where each line starts with a colon followed by
  6006. a space character.
  6007. An new buffer is created and the fixed-width region is copied into it,
  6008. and the buffer is switched into `artist-mode' for editing. When done,
  6009. exit with \\[org-edit-src-exit]. The edited text will then replace
  6010. the fragment in the Org-mode buffer."
  6011. (interactive)
  6012. (let ((line (org-current-line))
  6013. (case-fold-search t)
  6014. (msg (substitute-command-keys
  6015. "Edit, then exit with C-c ' (C-c and single quote)"))
  6016. (org-mode-p (eq major-mode 'org-mode))
  6017. (beg (make-marker))
  6018. (end (make-marker))
  6019. nindent ovl beg1 end1 code begline buffer)
  6020. (beginning-of-line 1)
  6021. (if (looking-at "[ \t]*[^:\n \t]")
  6022. nil
  6023. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  6024. (setq beg1 (point) end1 beg1)
  6025. (save-excursion
  6026. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  6027. (setq beg1 (point-at-bol 2))
  6028. (setq beg1 (point))))
  6029. (save-excursion
  6030. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  6031. (setq end1 (1- (match-beginning 0)))
  6032. (setq end1 (point))))
  6033. (goto-line line))
  6034. (setq beg (move-marker beg beg1)
  6035. end (move-marker end end1)
  6036. code (buffer-substring-no-properties beg end)
  6037. begline (save-excursion (goto-char beg) (org-current-line)))
  6038. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  6039. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  6040. (switch-to-buffer buffer)
  6041. (when buffer
  6042. (with-current-buffer buffer
  6043. (if (boundp 'org-edit-src-overlay)
  6044. (org-delete-overlay org-edit-src-overlay)))
  6045. (kill-buffer buffer))
  6046. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  6047. (setq ovl (org-make-overlay beg end))
  6048. (org-overlay-put ovl 'face 'secondary-selection)
  6049. (org-overlay-put ovl 'edit-buffer buffer)
  6050. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  6051. (org-overlay-put ovl 'face 'secondary-selection)
  6052. (org-overlay-put ovl
  6053. 'keymap
  6054. (let ((map (make-sparse-keymap)))
  6055. (define-key map [mouse-1] 'org-edit-src-continue)
  6056. map))
  6057. (org-overlay-put ovl :read-only "Leave me alone")
  6058. (switch-to-buffer buffer)
  6059. (insert code)
  6060. (remove-text-properties (point-min) (point-max)
  6061. '(display nil invisible nil intangible nil))
  6062. (setq nindent (org-do-remove-indentation))
  6063. (cond
  6064. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  6065. (fundamental-mode)
  6066. (artist-mode 1))
  6067. (t (funcall org-edit-fixed-width-region-mode)))
  6068. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  6069. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  6070. (set (make-local-variable 'org-edit-src-picture) t)
  6071. (goto-char (point-min))
  6072. (while (re-search-forward "^[ \t]*: ?" nil t)
  6073. (replace-match ""))
  6074. (goto-line (1+ (- line begline)))
  6075. (org-exit-edit-mode)
  6076. (org-set-local 'org-edit-src-beg-marker beg)
  6077. (org-set-local 'org-edit-src-end-marker end)
  6078. (org-set-local 'org-edit-src-overlay ovl)
  6079. (org-set-local 'org-edit-src-nindent nindent)
  6080. (and org-edit-src-persistent-message
  6081. (org-set-local 'header-line-format msg)))
  6082. (message "%s" msg)
  6083. t)))
  6084. (defun org-edit-src-find-region-and-lang ()
  6085. "Find the region and language for a local edit.
  6086. Return a list with beginning and end of the region, a string representing
  6087. the language, a switch telling of the content should be in a single line."
  6088. (let ((re-list
  6089. (append
  6090. org-edit-src-region-extra
  6091. '(
  6092. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  6093. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  6094. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  6095. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  6096. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  6097. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  6098. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  6099. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  6100. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  6101. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  6102. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  6103. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  6104. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  6105. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  6106. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  6107. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  6108. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  6109. )))
  6110. (pos (point))
  6111. re1 re2 single beg end lang lfmt match-re1)
  6112. (catch 'exit
  6113. (while (setq entry (pop re-list))
  6114. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  6115. single (nth 3 entry))
  6116. (save-excursion
  6117. (if (or (looking-at re1)
  6118. (re-search-backward re1 nil t))
  6119. (progn
  6120. (setq match-re1 (match-string 0))
  6121. (setq beg (match-end 0)
  6122. lang (org-edit-src-get-lang lang)
  6123. lfmt (org-edit-src-get-label-format match-re1))
  6124. (if (and (re-search-forward re2 nil t)
  6125. (>= (match-end 0) pos))
  6126. (throw 'exit (list beg (match-beginning 0)
  6127. lang single lfmt))))
  6128. (if (or (looking-at re2)
  6129. (re-search-forward re2 nil t))
  6130. (progn
  6131. (setq end (match-beginning 0))
  6132. (if (and (re-search-backward re1 nil t)
  6133. (<= (match-beginning 0) pos))
  6134. (progn
  6135. (setq lfmt (org-edit-src-get-label-format
  6136. (match-string 0)))
  6137. (throw 'exit
  6138. (list (match-end 0) end
  6139. (org-edit-src-get-lang lang)
  6140. single lfmt))))))))))))
  6141. (defun org-edit-src-get-lang (lang)
  6142. "Extract the src language."
  6143. (let ((m (match-string 0)))
  6144. (cond
  6145. ((stringp lang) lang)
  6146. ((integerp lang) (match-string lang))
  6147. ((and (eq lang 'lang)
  6148. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  6149. (match-string 1 m))
  6150. ((and (eq lang 'style)
  6151. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  6152. (match-string 1 m))
  6153. (t "fundamental"))))
  6154. (defun org-edit-src-get-label-format (s)
  6155. "Extract the label format."
  6156. (save-match-data
  6157. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  6158. (match-string 1 s))))
  6159. (defun org-edit-src-exit ()
  6160. "Exit special edit and protect problematic lines."
  6161. (interactive)
  6162. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  6163. (error "This is not an sub-editing buffer, something is wrong..."))
  6164. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  6165. 1
  6166. (org-current-line)))
  6167. (beg org-edit-src-beg-marker)
  6168. (end org-edit-src-end-marker)
  6169. (ovl org-edit-src-overlay)
  6170. (buffer (current-buffer))
  6171. (nindent org-edit-src-nindent)
  6172. code)
  6173. (goto-char (point-min))
  6174. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  6175. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  6176. (when (org-bound-and-true-p org-edit-src-force-single-line)
  6177. (goto-char (point-min))
  6178. (while (re-search-forward "\n" nil t)
  6179. (replace-match " "))
  6180. (goto-char (point-min))
  6181. (if (looking-at "\\s-*") (replace-match " "))
  6182. (if (re-search-forward "\\s-+\\'" nil t)
  6183. (replace-match "")))
  6184. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  6185. (goto-char (point-min))
  6186. (while (re-search-forward
  6187. (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\|[ \t]*#\\+\\)") nil t)
  6188. (replace-match ",\\1")))
  6189. (when (org-bound-and-true-p org-edit-src-picture)
  6190. (untabify (point-min) (point-max))
  6191. (goto-char (point-min))
  6192. (while (re-search-forward "^" nil t)
  6193. (replace-match ": ")))
  6194. (when nindent
  6195. (setq nindent (make-string nindent ?\ ))
  6196. (goto-char (point-min))
  6197. (while (re-search-forward "^" nil t)
  6198. (replace-match nindent)))
  6199. (setq code (buffer-string))
  6200. (switch-to-buffer (marker-buffer beg))
  6201. (kill-buffer buffer)
  6202. (goto-char beg)
  6203. (org-delete-overlay ovl)
  6204. (delete-region beg end)
  6205. (insert code)
  6206. (goto-char beg)
  6207. (goto-line (1- (+ (org-current-line) line)))
  6208. (move-marker beg nil)
  6209. (move-marker end nil)))
  6210. ;;; The orgstruct minor mode
  6211. ;; Define a minor mode which can be used in other modes in order to
  6212. ;; integrate the org-mode structure editing commands.
  6213. ;; This is really a hack, because the org-mode structure commands use
  6214. ;; keys which normally belong to the major mode. Here is how it
  6215. ;; works: The minor mode defines all the keys necessary to operate the
  6216. ;; structure commands, but wraps the commands into a function which
  6217. ;; tests if the cursor is currently at a headline or a plain list
  6218. ;; item. If that is the case, the structure command is used,
  6219. ;; temporarily setting many Org-mode variables like regular
  6220. ;; expressions for filling etc. However, when any of those keys is
  6221. ;; used at a different location, function uses `key-binding' to look
  6222. ;; up if the key has an associated command in another currently active
  6223. ;; keymap (minor modes, major mode, global), and executes that
  6224. ;; command. There might be problems if any of the keys is otherwise
  6225. ;; used as a prefix key.
  6226. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6227. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6228. ;; addresses this by checking explicitly for both bindings.
  6229. (defvar orgstruct-mode-map (make-sparse-keymap)
  6230. "Keymap for the minor `orgstruct-mode'.")
  6231. (defvar org-local-vars nil
  6232. "List of local variables, for use by `orgstruct-mode'")
  6233. ;;;###autoload
  6234. (define-minor-mode orgstruct-mode
  6235. "Toggle the minor more `orgstruct-mode'.
  6236. This mode is for using Org-mode structure commands in other modes.
  6237. The following key behave as if Org-mode was active, if the cursor
  6238. is on a headline, or on a plain list item (both in the definition
  6239. of Org-mode).
  6240. M-up Move entry/item up
  6241. M-down Move entry/item down
  6242. M-left Promote
  6243. M-right Demote
  6244. M-S-up Move entry/item up
  6245. M-S-down Move entry/item down
  6246. M-S-left Promote subtree
  6247. M-S-right Demote subtree
  6248. M-q Fill paragraph and items like in Org-mode
  6249. C-c ^ Sort entries
  6250. C-c - Cycle list bullet
  6251. TAB Cycle item visibility
  6252. M-RET Insert new heading/item
  6253. S-M-RET Insert new TODO heading / Checkbox item
  6254. C-c C-c Set tags / toggle checkbox"
  6255. nil " OrgStruct" nil
  6256. (org-load-modules-maybe)
  6257. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6258. ;;;###autoload
  6259. (defun turn-on-orgstruct ()
  6260. "Unconditionally turn on `orgstruct-mode'."
  6261. (orgstruct-mode 1))
  6262. (defun orgstruct++-mode (&optional arg)
  6263. "Toggle `orgstruct-mode', the enhanced version of it.
  6264. In addition to setting orgstruct-mode, this also exports all indentation and
  6265. autofilling variables from org-mode into the buffer. It will also
  6266. recognize item context in multiline items.
  6267. Note that turning off orgstruct-mode will *not* remove the
  6268. indentation/paragraph settings. This can only be done by refreshing the
  6269. major mode, for example with \[normal-mode]."
  6270. (interactive "P")
  6271. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6272. (if (< arg 1)
  6273. (orgstruct-mode -1)
  6274. (orgstruct-mode 1)
  6275. (let (var val)
  6276. (mapc
  6277. (lambda (x)
  6278. (when (string-match
  6279. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6280. (symbol-name (car x)))
  6281. (setq var (car x) val (nth 1 x))
  6282. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6283. org-local-vars)
  6284. (org-set-local 'orgstruct-is-++ t))))
  6285. (defvar orgstruct-is-++ nil
  6286. "Is orgstruct-mode in ++ version in the current-buffer?")
  6287. (make-variable-buffer-local 'orgstruct-is-++)
  6288. ;;;###autoload
  6289. (defun turn-on-orgstruct++ ()
  6290. "Unconditionally turn on `orgstruct++-mode'."
  6291. (orgstruct++-mode 1))
  6292. (defun orgstruct-error ()
  6293. "Error when there is no default binding for a structure key."
  6294. (interactive)
  6295. (error "This key has no function outside structure elements"))
  6296. (defun orgstruct-setup ()
  6297. "Setup orgstruct keymaps."
  6298. (let ((nfunc 0)
  6299. (bindings
  6300. (list
  6301. '([(meta up)] org-metaup)
  6302. '([(meta down)] org-metadown)
  6303. '([(meta left)] org-metaleft)
  6304. '([(meta right)] org-metaright)
  6305. '([(meta shift up)] org-shiftmetaup)
  6306. '([(meta shift down)] org-shiftmetadown)
  6307. '([(meta shift left)] org-shiftmetaleft)
  6308. '([(meta shift right)] org-shiftmetaright)
  6309. '([?\e (up)] org-metaup)
  6310. '([?\e (down)] org-metadown)
  6311. '([?\e (left)] org-metaleft)
  6312. '([?\e (right)] org-metaright)
  6313. '([?\e (shift up)] org-shiftmetaup)
  6314. '([?\e (shift down)] org-shiftmetadown)
  6315. '([?\e (shift left)] org-shiftmetaleft)
  6316. '([?\e (shift right)] org-shiftmetaright)
  6317. '([(shift up)] org-shiftup)
  6318. '([(shift down)] org-shiftdown)
  6319. '([(shift left)] org-shiftleft)
  6320. '([(shift right)] org-shiftright)
  6321. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6322. '("\M-q" fill-paragraph)
  6323. '("\C-c^" org-sort)
  6324. '("\C-c-" org-cycle-list-bullet)))
  6325. elt key fun cmd)
  6326. (while (setq elt (pop bindings))
  6327. (setq nfunc (1+ nfunc))
  6328. (setq key (org-key (car elt))
  6329. fun (nth 1 elt)
  6330. cmd (orgstruct-make-binding fun nfunc key))
  6331. (org-defkey orgstruct-mode-map key cmd))
  6332. ;; Special treatment needed for TAB and RET
  6333. (org-defkey orgstruct-mode-map [(tab)]
  6334. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6335. (org-defkey orgstruct-mode-map "\C-i"
  6336. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6337. (org-defkey orgstruct-mode-map "\M-\C-m"
  6338. (orgstruct-make-binding 'org-insert-heading 105
  6339. "\M-\C-m" [(meta return)]))
  6340. (org-defkey orgstruct-mode-map [(meta return)]
  6341. (orgstruct-make-binding 'org-insert-heading 106
  6342. [(meta return)] "\M-\C-m"))
  6343. (org-defkey orgstruct-mode-map [(shift meta return)]
  6344. (orgstruct-make-binding 'org-insert-todo-heading 107
  6345. [(meta return)] "\M-\C-m"))
  6346. (org-defkey orgstruct-mode-map "\e\C-m"
  6347. (orgstruct-make-binding 'org-insert-heading 108
  6348. "\e\C-m" [?\e (return)]))
  6349. (org-defkey orgstruct-mode-map [?\e (return)]
  6350. (orgstruct-make-binding 'org-insert-heading 109
  6351. [?\e (return)] "\e\C-m"))
  6352. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6353. (orgstruct-make-binding 'org-insert-todo-heading 110
  6354. [?\e (return)] "\e\C-m"))
  6355. (unless org-local-vars
  6356. (setq org-local-vars (org-get-local-variables)))
  6357. t))
  6358. (defun orgstruct-make-binding (fun n &rest keys)
  6359. "Create a function for binding in the structure minor mode.
  6360. FUN is the command to call inside a table. N is used to create a unique
  6361. command name. KEYS are keys that should be checked in for a command
  6362. to execute outside of tables."
  6363. (eval
  6364. (list 'defun
  6365. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6366. '(arg)
  6367. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6368. "Outside of structure, run the binding of `"
  6369. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6370. "'.")
  6371. '(interactive "p")
  6372. (list 'if
  6373. `(org-context-p 'headline 'item
  6374. (and orgstruct-is-++
  6375. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6376. 'item-body))
  6377. (list 'org-run-like-in-org-mode (list 'quote fun))
  6378. (list 'let '(orgstruct-mode)
  6379. (list 'call-interactively
  6380. (append '(or)
  6381. (mapcar (lambda (k)
  6382. (list 'key-binding k))
  6383. keys)
  6384. '('orgstruct-error))))))))
  6385. (defun org-context-p (&rest contexts)
  6386. "Check if local context is any of CONTEXTS.
  6387. Possible values in the list of contexts are `table', `headline', and `item'."
  6388. (let ((pos (point)))
  6389. (goto-char (point-at-bol))
  6390. (prog1 (or (and (memq 'table contexts)
  6391. (looking-at "[ \t]*|"))
  6392. (and (memq 'headline contexts)
  6393. ;;????????? (looking-at "\\*+"))
  6394. (looking-at outline-regexp))
  6395. (and (memq 'item contexts)
  6396. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6397. (and (memq 'item-body contexts)
  6398. (org-in-item-p)))
  6399. (goto-char pos))))
  6400. (defun org-get-local-variables ()
  6401. "Return a list of all local variables in an org-mode buffer."
  6402. (let (varlist)
  6403. (with-current-buffer (get-buffer-create "*Org tmp*")
  6404. (erase-buffer)
  6405. (org-mode)
  6406. (setq varlist (buffer-local-variables)))
  6407. (kill-buffer "*Org tmp*")
  6408. (delq nil
  6409. (mapcar
  6410. (lambda (x)
  6411. (setq x
  6412. (if (symbolp x)
  6413. (list x)
  6414. (list (car x) (list 'quote (cdr x)))))
  6415. (if (string-match
  6416. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6417. (symbol-name (car x)))
  6418. x nil))
  6419. varlist))))
  6420. ;;;###autoload
  6421. (defun org-run-like-in-org-mode (cmd)
  6422. "Run a command, pretending that the current buffer is in Org-mode.
  6423. This will temporarily bind local variables that are typically bound in
  6424. Org-mode to the values they have in Org-mode, and then interactively
  6425. call CMD."
  6426. (org-load-modules-maybe)
  6427. (unless org-local-vars
  6428. (setq org-local-vars (org-get-local-variables)))
  6429. (eval (list 'let org-local-vars
  6430. (list 'call-interactively (list 'quote cmd)))))
  6431. ;;;; Archiving
  6432. (defun org-get-category (&optional pos)
  6433. "Get the category applying to position POS."
  6434. (get-text-property (or pos (point)) 'org-category))
  6435. (defun org-refresh-category-properties ()
  6436. "Refresh category text properties in the buffer."
  6437. (let ((def-cat (cond
  6438. ((null org-category)
  6439. (if buffer-file-name
  6440. (file-name-sans-extension
  6441. (file-name-nondirectory buffer-file-name))
  6442. "???"))
  6443. ((symbolp org-category) (symbol-name org-category))
  6444. (t org-category)))
  6445. beg end cat pos optionp)
  6446. (org-unmodified
  6447. (save-excursion
  6448. (save-restriction
  6449. (widen)
  6450. (goto-char (point-min))
  6451. (put-text-property (point) (point-max) 'org-category def-cat)
  6452. (while (re-search-forward
  6453. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6454. (setq pos (match-end 0)
  6455. optionp (equal (char-after (match-beginning 0)) ?#)
  6456. cat (org-trim (match-string 2)))
  6457. (if optionp
  6458. (setq beg (point-at-bol) end (point-max))
  6459. (org-back-to-heading t)
  6460. (setq beg (point) end (org-end-of-subtree t t)))
  6461. (put-text-property beg end 'org-category cat)
  6462. (goto-char pos)))))))
  6463. ;;;; Link Stuff
  6464. ;;; Link abbreviations
  6465. (defun org-link-expand-abbrev (link)
  6466. "Apply replacements as defined in `org-link-abbrev-alist."
  6467. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6468. (let* ((key (match-string 1 link))
  6469. (as (or (assoc key org-link-abbrev-alist-local)
  6470. (assoc key org-link-abbrev-alist)))
  6471. (tag (and (match-end 2) (match-string 3 link)))
  6472. rpl)
  6473. (if (not as)
  6474. link
  6475. (setq rpl (cdr as))
  6476. (cond
  6477. ((symbolp rpl) (funcall rpl tag))
  6478. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6479. ((string-match "%h" rpl)
  6480. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6481. (t (concat rpl tag)))))
  6482. link))
  6483. ;;; Storing and inserting links
  6484. (defvar org-insert-link-history nil
  6485. "Minibuffer history for links inserted with `org-insert-link'.")
  6486. (defvar org-stored-links nil
  6487. "Contains the links stored with `org-store-link'.")
  6488. (defvar org-store-link-plist nil
  6489. "Plist with info about the most recently link created with `org-store-link'.")
  6490. (defvar org-link-protocols nil
  6491. "Link protocols added to Org-mode using `org-add-link-type'.")
  6492. (defvar org-store-link-functions nil
  6493. "List of functions that are called to create and store a link.
  6494. Each function will be called in turn until one returns a non-nil
  6495. value. Each function should check if it is responsible for creating
  6496. this link (for example by looking at the major mode).
  6497. If not, it must exit and return nil.
  6498. If yes, it should return a non-nil value after a calling
  6499. `org-store-link-props' with a list of properties and values.
  6500. Special properties are:
  6501. :type The link prefix. like \"http\". This must be given.
  6502. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6503. This is obligatory as well.
  6504. :description Optional default description for the second pair
  6505. of brackets in an Org-mode link. The user can still change
  6506. this when inserting this link into an Org-mode buffer.
  6507. In addition to these, any additional properties can be specified
  6508. and then used in remember templates.")
  6509. (defun org-add-link-type (type &optional follow export)
  6510. "Add TYPE to the list of `org-link-types'.
  6511. Re-compute all regular expressions depending on `org-link-types'
  6512. FOLLOW and EXPORT are two functions.
  6513. FOLLOW should take the link path as the single argument and do whatever
  6514. is necessary to follow the link, for example find a file or display
  6515. a mail message.
  6516. EXPORT should format the link path for export to one of the export formats.
  6517. It should be a function accepting three arguments:
  6518. path the path of the link, the text after the prefix (like \"http:\")
  6519. desc the description of the link, if any, nil if there was no description
  6520. format the export format, a symbol like `html' or `latex'.
  6521. The function may use the FORMAT information to return different values
  6522. depending on the format. The return value will be put literally into
  6523. the exported file.
  6524. Org-mode has a built-in default for exporting links. If you are happy with
  6525. this default, there is no need to define an export function for the link
  6526. type. For a simple example of an export function, see `org-bbdb.el'."
  6527. (add-to-list 'org-link-types type t)
  6528. (org-make-link-regexps)
  6529. (if (assoc type org-link-protocols)
  6530. (setcdr (assoc type org-link-protocols) (list follow export))
  6531. (push (list type follow export) org-link-protocols)))
  6532. ;;;###autoload
  6533. (defun org-store-link (arg)
  6534. "\\<org-mode-map>Store an org-link to the current location.
  6535. This link is added to `org-stored-links' and can later be inserted
  6536. into an org-buffer with \\[org-insert-link].
  6537. For some link types, a prefix arg is interpreted:
  6538. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6539. For file links, arg negates `org-context-in-file-links'."
  6540. (interactive "P")
  6541. (org-load-modules-maybe)
  6542. (setq org-store-link-plist nil) ; reset
  6543. (let (link cpltxt desc description search txt custom-id)
  6544. (cond
  6545. ((run-hook-with-args-until-success 'org-store-link-functions)
  6546. (setq link (plist-get org-store-link-plist :link)
  6547. desc (or (plist-get org-store-link-plist :description) link)))
  6548. ((equal (buffer-name) "*Org Edit Src Example*")
  6549. (let (label gc)
  6550. (while (or (not label)
  6551. (save-excursion
  6552. (save-restriction
  6553. (widen)
  6554. (goto-char (point-min))
  6555. (re-search-forward
  6556. (regexp-quote (format org-coderef-label-format label))
  6557. nil t))))
  6558. (when label (message "Label exists already") (sit-for 2))
  6559. (setq label (read-string "Code line label: " label)))
  6560. (end-of-line 1)
  6561. (setq link (format org-coderef-label-format label))
  6562. (setq gc (- 79 (length link)))
  6563. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6564. (insert link)
  6565. (setq link (concat "(" label ")") desc nil)))
  6566. ((eq major-mode 'calendar-mode)
  6567. (let ((cd (calendar-cursor-to-date)))
  6568. (setq link
  6569. (format-time-string
  6570. (car org-time-stamp-formats)
  6571. (apply 'encode-time
  6572. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6573. nil nil nil))))
  6574. (org-store-link-props :type "calendar" :date cd)))
  6575. ((eq major-mode 'w3-mode)
  6576. (setq cpltxt (if (and (buffer-name)
  6577. (not (string-match "Untitled" (buffer-name))))
  6578. (buffer-name)
  6579. (url-view-url t))
  6580. link (org-make-link (url-view-url t)))
  6581. (org-store-link-props :type "w3" :url (url-view-url t)))
  6582. ((eq major-mode 'w3m-mode)
  6583. (setq cpltxt (or w3m-current-title w3m-current-url)
  6584. link (org-make-link w3m-current-url))
  6585. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6586. ((setq search (run-hook-with-args-until-success
  6587. 'org-create-file-search-functions))
  6588. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6589. "::" search))
  6590. (setq cpltxt (or description link)))
  6591. ((eq major-mode 'image-mode)
  6592. (setq cpltxt (concat "file:"
  6593. (abbreviate-file-name buffer-file-name))
  6594. link (org-make-link cpltxt))
  6595. (org-store-link-props :type "image" :file buffer-file-name))
  6596. ((eq major-mode 'dired-mode)
  6597. ;; link to the file in the current line
  6598. (setq cpltxt (concat "file:"
  6599. (abbreviate-file-name
  6600. (expand-file-name
  6601. (dired-get-filename nil t))))
  6602. link (org-make-link cpltxt)))
  6603. ((and buffer-file-name (org-mode-p))
  6604. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6605. (cond
  6606. ((org-in-regexp "<<\\(.*?\\)>>")
  6607. (setq cpltxt
  6608. (concat "file:"
  6609. (abbreviate-file-name buffer-file-name)
  6610. "::" (match-string 1))
  6611. link (org-make-link cpltxt)))
  6612. ((and (featurep 'org-id)
  6613. (or (eq org-link-to-org-use-id t)
  6614. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6615. (interactive-p))
  6616. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6617. (interactive-p)
  6618. (not custom-id))
  6619. (and org-link-to-org-use-id
  6620. (condition-case nil
  6621. (org-entry-get nil "ID")
  6622. (error nil)))))
  6623. ;; We can make a link using the ID.
  6624. (setq link (condition-case nil
  6625. (prog1 (org-id-store-link)
  6626. (setq desc (plist-get org-store-link-plist
  6627. :description)))
  6628. (error
  6629. ;; probably before first headline, link to file only
  6630. (concat "file:"
  6631. (abbreviate-file-name buffer-file-name))))))
  6632. (t
  6633. ;; Just link to current headline
  6634. (setq cpltxt (concat "file:"
  6635. (abbreviate-file-name buffer-file-name)))
  6636. ;; Add a context search string
  6637. (when (org-xor org-context-in-file-links arg)
  6638. (setq txt (cond
  6639. ((org-on-heading-p) nil)
  6640. ((org-region-active-p)
  6641. (buffer-substring (region-beginning) (region-end)))
  6642. (t nil)))
  6643. (when (or (null txt) (string-match "\\S-" txt))
  6644. (setq cpltxt
  6645. (concat cpltxt "::"
  6646. (condition-case nil
  6647. (org-make-org-heading-search-string txt)
  6648. (error "")))
  6649. desc "NONE")))
  6650. (if (string-match "::\\'" cpltxt)
  6651. (setq cpltxt (substring cpltxt 0 -2)))
  6652. (setq link (org-make-link cpltxt)))))
  6653. ((buffer-file-name (buffer-base-buffer))
  6654. ;; Just link to this file here.
  6655. (setq cpltxt (concat "file:"
  6656. (abbreviate-file-name
  6657. (buffer-file-name (buffer-base-buffer)))))
  6658. ;; Add a context string
  6659. (when (org-xor org-context-in-file-links arg)
  6660. (setq txt (if (org-region-active-p)
  6661. (buffer-substring (region-beginning) (region-end))
  6662. (buffer-substring (point-at-bol) (point-at-eol))))
  6663. ;; Only use search option if there is some text.
  6664. (when (string-match "\\S-" txt)
  6665. (setq cpltxt
  6666. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6667. desc "NONE")))
  6668. (setq link (org-make-link cpltxt)))
  6669. ((interactive-p)
  6670. (error "Cannot link to a buffer which is not visiting a file"))
  6671. (t (setq link nil)))
  6672. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6673. (setq link (or link cpltxt)
  6674. desc (or desc cpltxt))
  6675. (if (equal desc "NONE") (setq desc nil))
  6676. (if (and (interactive-p) link)
  6677. (progn
  6678. (setq org-stored-links
  6679. (cons (list link desc) org-stored-links))
  6680. (message "Stored: %s" (or desc link))
  6681. (when custom-id
  6682. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6683. "::#" custom-id))
  6684. (setq org-stored-links
  6685. (cons (list link desc) org-stored-links))))
  6686. (and link (org-make-link-string link desc)))))
  6687. (defun org-store-link-props (&rest plist)
  6688. "Store link properties, extract names and addresses."
  6689. (let (x adr)
  6690. (when (setq x (plist-get plist :from))
  6691. (setq adr (mail-extract-address-components x))
  6692. (setq plist (plist-put plist :fromname (car adr)))
  6693. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6694. (when (setq x (plist-get plist :to))
  6695. (setq adr (mail-extract-address-components x))
  6696. (setq plist (plist-put plist :toname (car adr)))
  6697. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6698. (let ((from (plist-get plist :from))
  6699. (to (plist-get plist :to)))
  6700. (when (and from to org-from-is-user-regexp)
  6701. (setq plist
  6702. (plist-put plist :fromto
  6703. (if (string-match org-from-is-user-regexp from)
  6704. (concat "to %t")
  6705. (concat "from %f"))))))
  6706. (setq org-store-link-plist plist))
  6707. (defun org-add-link-props (&rest plist)
  6708. "Add these properties to the link property list."
  6709. (let (key value)
  6710. (while plist
  6711. (setq key (pop plist) value (pop plist))
  6712. (setq org-store-link-plist
  6713. (plist-put org-store-link-plist key value)))))
  6714. (defun org-email-link-description (&optional fmt)
  6715. "Return the description part of an email link.
  6716. This takes information from `org-store-link-plist' and formats it
  6717. according to FMT (default from `org-email-link-description-format')."
  6718. (setq fmt (or fmt org-email-link-description-format))
  6719. (let* ((p org-store-link-plist)
  6720. (to (plist-get p :toaddress))
  6721. (from (plist-get p :fromaddress))
  6722. (table
  6723. (list
  6724. (cons "%c" (plist-get p :fromto))
  6725. (cons "%F" (plist-get p :from))
  6726. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6727. (cons "%T" (plist-get p :to))
  6728. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6729. (cons "%s" (plist-get p :subject))
  6730. (cons "%m" (plist-get p :message-id)))))
  6731. (when (string-match "%c" fmt)
  6732. ;; Check if the user wrote this message
  6733. (if (and org-from-is-user-regexp from to
  6734. (save-match-data (string-match org-from-is-user-regexp from)))
  6735. (setq fmt (replace-match "to %t" t t fmt))
  6736. (setq fmt (replace-match "from %f" t t fmt))))
  6737. (org-replace-escapes fmt table)))
  6738. (defun org-make-org-heading-search-string (&optional string heading)
  6739. "Make search string for STRING or current headline."
  6740. (interactive)
  6741. (let ((s (or string (org-get-heading))))
  6742. (unless (and string (not heading))
  6743. ;; We are using a headline, clean up garbage in there.
  6744. (if (string-match org-todo-regexp s)
  6745. (setq s (replace-match "" t t s)))
  6746. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6747. (setq s (replace-match "" t t s)))
  6748. (setq s (org-trim s))
  6749. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6750. org-comment-string "\\)") s)
  6751. (setq s (replace-match "" t t s)))
  6752. (while (string-match org-ts-regexp s)
  6753. (setq s (replace-match "" t t s))))
  6754. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6755. (setq s (replace-match " " t t s)))
  6756. (or string (setq s (concat "*" s))) ; Add * for headlines
  6757. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6758. (defun org-make-link (&rest strings)
  6759. "Concatenate STRINGS."
  6760. (apply 'concat strings))
  6761. (defun org-make-link-string (link &optional description)
  6762. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6763. (unless (string-match "\\S-" link)
  6764. (error "Empty link"))
  6765. (when (stringp description)
  6766. ;; Remove brackets from the description, they are fatal.
  6767. (while (string-match "\\[" description)
  6768. (setq description (replace-match "{" t t description)))
  6769. (while (string-match "\\]" description)
  6770. (setq description (replace-match "}" t t description))))
  6771. (when (equal (org-link-escape link) description)
  6772. ;; No description needed, it is identical
  6773. (setq description nil))
  6774. (when (and (not description)
  6775. (not (equal link (org-link-escape link))))
  6776. (setq description (org-extract-attributes link)))
  6777. (concat "[[" (org-link-escape link) "]"
  6778. (if description (concat "[" description "]") "")
  6779. "]"))
  6780. (defconst org-link-escape-chars
  6781. '((?\ . "%20")
  6782. (?\[ . "%5B")
  6783. (?\] . "%5D")
  6784. (?\340 . "%E0") ; `a
  6785. (?\342 . "%E2") ; ^a
  6786. (?\347 . "%E7") ; ,c
  6787. (?\350 . "%E8") ; `e
  6788. (?\351 . "%E9") ; 'e
  6789. (?\352 . "%EA") ; ^e
  6790. (?\356 . "%EE") ; ^i
  6791. (?\364 . "%F4") ; ^o
  6792. (?\371 . "%F9") ; `u
  6793. (?\373 . "%FB") ; ^u
  6794. (?\; . "%3B")
  6795. (?? . "%3F")
  6796. (?= . "%3D")
  6797. (?+ . "%2B")
  6798. )
  6799. "Association list of escapes for some characters problematic in links.
  6800. This is the list that is used for internal purposes.")
  6801. (defvar org-url-encoding-use-url-hexify nil)
  6802. (defconst org-link-escape-chars-browser
  6803. '((?\ . "%20")) ; 32 for the SPC char
  6804. "Association list of escapes for some characters problematic in links.
  6805. This is the list that is used before handing over to the browser.")
  6806. (defun org-link-escape (text &optional table)
  6807. "Escape characters in TEXT that are problematic for links."
  6808. (if org-url-encoding-use-url-hexify
  6809. (url-hexify-string text)
  6810. (setq table (or table org-link-escape-chars))
  6811. (when text
  6812. (let ((re (mapconcat (lambda (x) (regexp-quote
  6813. (char-to-string (car x))))
  6814. table "\\|")))
  6815. (while (string-match re text)
  6816. (setq text
  6817. (replace-match
  6818. (cdr (assoc (string-to-char (match-string 0 text))
  6819. table))
  6820. t t text)))
  6821. text))))
  6822. (defun org-link-unescape (text &optional table)
  6823. "Reverse the action of `org-link-escape'."
  6824. (if org-url-encoding-use-url-hexify
  6825. (url-unhex-string text)
  6826. (setq table (or table org-link-escape-chars))
  6827. (when text
  6828. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6829. table "\\|")))
  6830. (while (string-match re text)
  6831. (setq text
  6832. (replace-match
  6833. (char-to-string (car (rassoc (match-string 0 text) table)))
  6834. t t text)))
  6835. text))))
  6836. (defun org-xor (a b)
  6837. "Exclusive or."
  6838. (if a (not b) b))
  6839. (defun org-fixup-message-id-for-http (s)
  6840. "Replace special characters in a message id, so it can be used in an http query."
  6841. (while (string-match "<" s)
  6842. (setq s (replace-match "%3C" t t s)))
  6843. (while (string-match ">" s)
  6844. (setq s (replace-match "%3E" t t s)))
  6845. (while (string-match "@" s)
  6846. (setq s (replace-match "%40" t t s)))
  6847. s)
  6848. ;;;###autoload
  6849. (defun org-insert-link-global ()
  6850. "Insert a link like Org-mode does.
  6851. This command can be called in any mode to insert a link in Org-mode syntax."
  6852. (interactive)
  6853. (org-load-modules-maybe)
  6854. (org-run-like-in-org-mode 'org-insert-link))
  6855. (defun org-insert-link (&optional complete-file link-location)
  6856. "Insert a link. At the prompt, enter the link.
  6857. Completion can be used to insert any of the link protocol prefixes like
  6858. http or ftp in use.
  6859. The history can be used to select a link previously stored with
  6860. `org-store-link'. When the empty string is entered (i.e. if you just
  6861. press RET at the prompt), the link defaults to the most recently
  6862. stored link. As SPC triggers completion in the minibuffer, you need to
  6863. use M-SPC or C-q SPC to force the insertion of a space character.
  6864. You will also be prompted for a description, and if one is given, it will
  6865. be displayed in the buffer instead of the link.
  6866. If there is already a link at point, this command will allow you to edit link
  6867. and description parts.
  6868. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6869. be selected using completion. The path to the file will be relative to the
  6870. current directory if the file is in the current directory or a subdirectory.
  6871. Otherwise, the link will be the absolute path as completed in the minibuffer
  6872. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6873. option `org-link-file-path-type'.
  6874. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6875. the current directory or below.
  6876. With three \\[universal-argument] prefixes, negate the meaning of
  6877. `org-keep-stored-link-after-insertion'.
  6878. If `org-make-link-description-function' is non-nil, this function will be
  6879. called with the link target, and the result will be the default
  6880. link description.
  6881. If the LINK-LOCATION parameter is non-nil, this value will be
  6882. used as the link location instead of reading one interactively."
  6883. (interactive "P")
  6884. (let* ((wcf (current-window-configuration))
  6885. (region (if (org-region-active-p)
  6886. (buffer-substring (region-beginning) (region-end))))
  6887. (remove (and region (list (region-beginning) (region-end))))
  6888. (desc region)
  6889. tmphist ; byte-compile incorrectly complains about this
  6890. (link link-location)
  6891. entry file all-prefixes)
  6892. (cond
  6893. (link-location) ; specified by arg, just use it.
  6894. ((org-in-regexp org-bracket-link-regexp 1)
  6895. ;; We do have a link at point, and we are going to edit it.
  6896. (setq remove (list (match-beginning 0) (match-end 0)))
  6897. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6898. (setq link (read-string "Link: "
  6899. (org-link-unescape
  6900. (org-match-string-no-properties 1)))))
  6901. ((or (org-in-regexp org-angle-link-re)
  6902. (org-in-regexp org-plain-link-re))
  6903. ;; Convert to bracket link
  6904. (setq remove (list (match-beginning 0) (match-end 0))
  6905. link (read-string "Link: "
  6906. (org-remove-angle-brackets (match-string 0)))))
  6907. ((member complete-file '((4) (16)))
  6908. ;; Completing read for file names.
  6909. (setq link (org-file-complete-link complete-file)))
  6910. (t
  6911. ;; Read link, with completion for stored links.
  6912. (with-output-to-temp-buffer "*Org Links*"
  6913. (princ "Insert a link.
  6914. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6915. (when org-stored-links
  6916. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6917. (princ (mapconcat
  6918. (lambda (x)
  6919. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6920. (reverse org-stored-links) "\n"))))
  6921. (let ((cw (selected-window)))
  6922. (select-window (get-buffer-window "*Org Links*"))
  6923. (setq truncate-lines t)
  6924. (unless (pos-visible-in-window-p (point-max))
  6925. (org-fit-window-to-buffer))
  6926. (and (window-live-p cw) (select-window cw)))
  6927. ;; Fake a link history, containing the stored links.
  6928. (setq tmphist (append (mapcar 'car org-stored-links)
  6929. org-insert-link-history))
  6930. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6931. (mapcar 'car org-link-abbrev-alist)
  6932. org-link-types))
  6933. (unwind-protect
  6934. (progn
  6935. (setq link
  6936. (let ((org-completion-use-ido nil))
  6937. (org-completing-read
  6938. "Link: "
  6939. (mapcar (lambda (x) (list (concat x ":")))
  6940. all-prefixes)
  6941. nil nil nil
  6942. 'tmphist
  6943. (or (car (car org-stored-links))))))
  6944. (if (or (member link all-prefixes)
  6945. (and (equal ":" (substring link -1))
  6946. (member (substring link 0 -1) all-prefixes)
  6947. (setq link (substring link 0 -1))))
  6948. (setq link (org-link-try-special-completion link))))
  6949. (set-window-configuration wcf)
  6950. (kill-buffer "*Org Links*"))
  6951. (setq entry (assoc link org-stored-links))
  6952. (or entry (push link org-insert-link-history))
  6953. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6954. (not org-keep-stored-link-after-insertion))
  6955. (setq org-stored-links (delq (assoc link org-stored-links)
  6956. org-stored-links)))
  6957. (setq desc (or desc (nth 1 entry)))))
  6958. (if (string-match org-plain-link-re link)
  6959. ;; URL-like link, normalize the use of angular brackets.
  6960. (setq link (org-make-link (org-remove-angle-brackets link))))
  6961. ;; Check if we are linking to the current file with a search option
  6962. ;; If yes, simplify the link by using only the search option.
  6963. (when (and buffer-file-name
  6964. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6965. (let* ((path (match-string 1 link))
  6966. (case-fold-search nil)
  6967. (search (match-string 2 link)))
  6968. (save-match-data
  6969. (if (equal (file-truename buffer-file-name) (file-truename path))
  6970. ;; We are linking to this same file, with a search option
  6971. (setq link search)))))
  6972. ;; Check if we can/should use a relative path. If yes, simplify the link
  6973. (when (string-match "^file:\\(.*\\)" link)
  6974. (let* ((path (match-string 1 link))
  6975. (origpath path)
  6976. (case-fold-search nil))
  6977. (cond
  6978. ((or (eq org-link-file-path-type 'absolute)
  6979. (equal complete-file '(16)))
  6980. (setq path (abbreviate-file-name (expand-file-name path))))
  6981. ((eq org-link-file-path-type 'noabbrev)
  6982. (setq path (expand-file-name path)))
  6983. ((eq org-link-file-path-type 'relative)
  6984. (setq path (file-relative-name path)))
  6985. (t
  6986. (save-match-data
  6987. (if (string-match (concat "^" (regexp-quote
  6988. (file-name-as-directory
  6989. (expand-file-name "."))))
  6990. (expand-file-name path))
  6991. ;; We are linking a file with relative path name.
  6992. (setq path (substring (expand-file-name path)
  6993. (match-end 0)))
  6994. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6995. (setq link (concat "file:" path))
  6996. (if (equal desc origpath)
  6997. (setq desc path))))
  6998. (if org-make-link-description-function
  6999. (setq desc (funcall org-make-link-description-function link desc)))
  7000. (setq desc (read-string "Description: " desc))
  7001. (unless (string-match "\\S-" desc) (setq desc nil))
  7002. (if remove (apply 'delete-region remove))
  7003. (insert (org-make-link-string link desc))))
  7004. (defun org-link-try-special-completion (type)
  7005. "If there is completion support for link type TAPE, offer it."
  7006. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7007. (if (functionp fun)
  7008. (funcall fun)
  7009. (read-string "Link (no completion support): " (concat type ":")))))
  7010. (defun org-file-complete-link (&optional arg)
  7011. "Create a file link using completion."
  7012. (let (file link)
  7013. (setq file (read-file-name "File: "))
  7014. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7015. (pwd1 (file-name-as-directory (abbreviate-file-name
  7016. (expand-file-name ".")))))
  7017. (cond
  7018. ((equal arg '(16))
  7019. (setq link (org-make-link
  7020. "file:"
  7021. (abbreviate-file-name (expand-file-name file)))))
  7022. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7023. (setq link (org-make-link "file:" (match-string 1 file))))
  7024. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7025. (expand-file-name file))
  7026. (setq link (org-make-link
  7027. "file:" (match-string 1 (expand-file-name file)))))
  7028. (t (setq link (org-make-link "file:" file)))))
  7029. link))
  7030. (defun org-completing-read (&rest args)
  7031. "Completing-read with SPACE being a normal character."
  7032. (let ((minibuffer-local-completion-map
  7033. (copy-keymap minibuffer-local-completion-map)))
  7034. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7035. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7036. (apply 'org-ido-completing-read args)))
  7037. (defun org-completing-read-no-ido (&rest args)
  7038. (let (org-completion-use-ido)
  7039. (apply 'org-completing-read args)))
  7040. (defun org-ido-completing-read (&rest args)
  7041. "Completing-read using `ido-mode' speedups if available"
  7042. (if (and org-completion-use-ido
  7043. (fboundp 'ido-completing-read)
  7044. (boundp 'ido-mode) ido-mode
  7045. (listp (second args)))
  7046. (let ((ido-enter-matching-directory nil))
  7047. (apply 'ido-completing-read (concat (car args))
  7048. (mapcar (lambda (x) (car x)) (nth 1 args))
  7049. (cddr args)))
  7050. (apply 'completing-read args)))
  7051. (defun org-extract-attributes (s)
  7052. "Extract the attributes cookie from a string and set as text property."
  7053. (let (a attr (start 0) key value)
  7054. (save-match-data
  7055. (when (string-match "{{\\([^}]+\\)}}$" s)
  7056. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7057. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7058. (setq key (match-string 1 a) value (match-string 2 a)
  7059. start (match-end 0)
  7060. attr (plist-put attr (intern key) value))))
  7061. (org-add-props s nil 'org-attr attr))
  7062. s))
  7063. (defun org-extract-attributes-from-string (tag)
  7064. (let (key value attr)
  7065. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7066. (setq key (match-string 1 tag) value (match-string 2 tag)
  7067. tag (replace-match "" t t tag)
  7068. attr (plist-put attr (intern key) value)))
  7069. (cons tag attr)))
  7070. (defun org-attributes-to-string (plist)
  7071. "Format a property list into an HTML attribute list."
  7072. (let ((s "") key value)
  7073. (while plist
  7074. (setq key (pop plist) value (pop plist))
  7075. (and value
  7076. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7077. s))
  7078. ;;; Opening/following a link
  7079. (defvar org-link-search-failed nil)
  7080. (defun org-next-link ()
  7081. "Move forward to the next link.
  7082. If the link is in hidden text, expose it."
  7083. (interactive)
  7084. (when (and org-link-search-failed (eq this-command last-command))
  7085. (goto-char (point-min))
  7086. (message "Link search wrapped back to beginning of buffer"))
  7087. (setq org-link-search-failed nil)
  7088. (let* ((pos (point))
  7089. (ct (org-context))
  7090. (a (assoc :link ct)))
  7091. (if a (goto-char (nth 2 a)))
  7092. (if (re-search-forward org-any-link-re nil t)
  7093. (progn
  7094. (goto-char (match-beginning 0))
  7095. (if (org-invisible-p) (org-show-context)))
  7096. (goto-char pos)
  7097. (setq org-link-search-failed t)
  7098. (error "No further link found"))))
  7099. (defun org-previous-link ()
  7100. "Move backward to the previous link.
  7101. If the link is in hidden text, expose it."
  7102. (interactive)
  7103. (when (and org-link-search-failed (eq this-command last-command))
  7104. (goto-char (point-max))
  7105. (message "Link search wrapped back to end of buffer"))
  7106. (setq org-link-search-failed nil)
  7107. (let* ((pos (point))
  7108. (ct (org-context))
  7109. (a (assoc :link ct)))
  7110. (if a (goto-char (nth 1 a)))
  7111. (if (re-search-backward org-any-link-re nil t)
  7112. (progn
  7113. (goto-char (match-beginning 0))
  7114. (if (org-invisible-p) (org-show-context)))
  7115. (goto-char pos)
  7116. (setq org-link-search-failed t)
  7117. (error "No further link found"))))
  7118. (defun org-translate-link (s)
  7119. "Translate a link string if a translation function has been defined."
  7120. (if (and org-link-translation-function
  7121. (fboundp org-link-translation-function)
  7122. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7123. (progn
  7124. (setq s (funcall org-link-translation-function
  7125. (match-string 1) (match-string 2)))
  7126. (concat (car s) ":" (cdr s)))
  7127. s))
  7128. (defun org-translate-link-from-planner (type path)
  7129. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7130. This is still an experimental function, your mileage may vary."
  7131. (cond
  7132. ((member type '("http" "https" "news" "ftp"))
  7133. ;; standard Internet links are the same.
  7134. nil)
  7135. ((and (equal type "irc") (string-match "^//" path))
  7136. ;; Planner has two / at the beginning of an irc link, we have 1.
  7137. ;; We should have zero, actually....
  7138. (setq path (substring path 1)))
  7139. ((and (equal type "lisp") (string-match "^/" path))
  7140. ;; Planner has a slash, we do not.
  7141. (setq type "elisp" path (substring path 1)))
  7142. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7143. ;; A typical message link. Planner has the id after the fina slash,
  7144. ;; we separate it with a hash mark
  7145. (setq path (concat (match-string 1 path) "#"
  7146. (org-remove-angle-brackets (match-string 2 path)))))
  7147. )
  7148. (cons type path))
  7149. (defun org-find-file-at-mouse (ev)
  7150. "Open file link or URL at mouse."
  7151. (interactive "e")
  7152. (mouse-set-point ev)
  7153. (org-open-at-point 'in-emacs))
  7154. (defun org-open-at-mouse (ev)
  7155. "Open file link or URL at mouse."
  7156. (interactive "e")
  7157. (mouse-set-point ev)
  7158. (if (eq major-mode 'org-agenda-mode)
  7159. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7160. (org-open-at-point))
  7161. (defvar org-window-config-before-follow-link nil
  7162. "The window configuration before following a link.
  7163. This is saved in case the need arises to restore it.")
  7164. (defvar org-open-link-marker (make-marker)
  7165. "Marker pointing to the location where `org-open-at-point; was called.")
  7166. ;;;###autoload
  7167. (defun org-open-at-point-global ()
  7168. "Follow a link like Org-mode does.
  7169. This command can be called in any mode to follow a link that has
  7170. Org-mode syntax."
  7171. (interactive)
  7172. (org-run-like-in-org-mode 'org-open-at-point))
  7173. ;;;###autoload
  7174. (defun org-open-link-from-string (s &optional arg)
  7175. "Open a link in the string S, as if it was in Org-mode."
  7176. (interactive "sLink: \nP")
  7177. (let ((reference-buffer (current-buffer)))
  7178. (with-temp-buffer
  7179. (let ((org-inhibit-startup t))
  7180. (org-mode)
  7181. (insert s)
  7182. (goto-char (point-min))
  7183. (org-open-at-point arg reference-buffer)))))
  7184. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7185. "Open link at or after point.
  7186. If there is no link at point, this function will search forward up to
  7187. the end of the current line.
  7188. Normally, files will be opened by an appropriate application. If the
  7189. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7190. With a double prefix argument, try to open outside of Emacs, in the
  7191. application the system uses for this file type."
  7192. (interactive "P")
  7193. (org-load-modules-maybe)
  7194. (move-marker org-open-link-marker (point))
  7195. (setq org-window-config-before-follow-link (current-window-configuration))
  7196. (org-remove-occur-highlights nil nil t)
  7197. (cond
  7198. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7199. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7200. (org-footnote-action))
  7201. (t
  7202. (let (type path link line search (pos (point)))
  7203. (catch 'match
  7204. (save-excursion
  7205. (skip-chars-forward "^]\n\r")
  7206. (when (org-in-regexp org-bracket-link-regexp)
  7207. (setq link (org-extract-attributes
  7208. (org-link-unescape (org-match-string-no-properties 1))))
  7209. (while (string-match " *\n *" link)
  7210. (setq link (replace-match " " t t link)))
  7211. (setq link (org-link-expand-abbrev link))
  7212. (cond
  7213. ((or (file-name-absolute-p link)
  7214. (string-match "^\\.\\.?/" link))
  7215. (setq type "file" path link))
  7216. ((string-match org-link-re-with-space3 link)
  7217. (setq type (match-string 1 link) path (match-string 2 link)))
  7218. (t (setq type "thisfile" path link)))
  7219. (throw 'match t)))
  7220. (when (get-text-property (point) 'org-linked-text)
  7221. (setq type "thisfile"
  7222. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7223. (1+ (point)) (point))
  7224. path (buffer-substring
  7225. (previous-single-property-change pos 'org-linked-text)
  7226. (next-single-property-change pos 'org-linked-text)))
  7227. (throw 'match t))
  7228. (save-excursion
  7229. (when (or (org-in-regexp org-angle-link-re)
  7230. (org-in-regexp org-plain-link-re))
  7231. (setq type (match-string 1) path (match-string 2))
  7232. (throw 'match t)))
  7233. (save-excursion
  7234. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7235. (setq type "tags"
  7236. path (match-string 1))
  7237. (while (string-match ":" path)
  7238. (setq path (replace-match "+" t t path)))
  7239. (throw 'match t)))
  7240. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7241. (setq type "tree-match"
  7242. path (match-string 1))
  7243. (throw 'match t)))
  7244. (unless path
  7245. (error "No link found"))
  7246. ;; switch back to reference buffer
  7247. ;; needed when if called in a temporary buffer through
  7248. ;; org-open-link-from-string
  7249. (and reference-buffer (switch-to-buffer reference-buffer))
  7250. ;; Remove any trailing spaces in path
  7251. (if (string-match " +\\'" path)
  7252. (setq path (replace-match "" t t path)))
  7253. (if (and org-link-translation-function
  7254. (fboundp org-link-translation-function))
  7255. ;; Check if we need to translate the link
  7256. (let ((tmp (funcall org-link-translation-function type path)))
  7257. (setq type (car tmp) path (cdr tmp))))
  7258. (cond
  7259. ((assoc type org-link-protocols)
  7260. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7261. ((equal type "mailto")
  7262. (let ((cmd (car org-link-mailto-program))
  7263. (args (cdr org-link-mailto-program)) args1
  7264. (address path) (subject "") a)
  7265. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7266. (setq address (match-string 1 path)
  7267. subject (org-link-escape (match-string 2 path))))
  7268. (while args
  7269. (cond
  7270. ((not (stringp (car args))) (push (pop args) args1))
  7271. (t (setq a (pop args))
  7272. (if (string-match "%a" a)
  7273. (setq a (replace-match address t t a)))
  7274. (if (string-match "%s" a)
  7275. (setq a (replace-match subject t t a)))
  7276. (push a args1))))
  7277. (apply cmd (nreverse args1))))
  7278. ((member type '("http" "https" "ftp" "news"))
  7279. (browse-url (concat type ":" (org-link-escape
  7280. path org-link-escape-chars-browser))))
  7281. ((member type '("message"))
  7282. (browse-url (concat type ":" path)))
  7283. ((string= type "tags")
  7284. (org-tags-view in-emacs path))
  7285. ((string= type "thisfile")
  7286. (if in-emacs
  7287. (switch-to-buffer-other-window
  7288. (org-get-buffer-for-internal-link (current-buffer)))
  7289. (org-mark-ring-push))
  7290. (let ((cmd `(org-link-search
  7291. ,path
  7292. ,(cond ((equal in-emacs '(4)) 'occur)
  7293. ((equal in-emacs '(16)) 'org-occur)
  7294. (t nil))
  7295. ,pos)))
  7296. (condition-case nil (eval cmd)
  7297. (error (progn (widen) (eval cmd))))))
  7298. ((string= type "tree-match")
  7299. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7300. ((string= type "file")
  7301. (if (string-match "::\\([0-9]+\\)\\'" path)
  7302. (setq line (string-to-number (match-string 1 path))
  7303. path (substring path 0 (match-beginning 0)))
  7304. (if (string-match "::\\(.+\\)\\'" path)
  7305. (setq search (match-string 1 path)
  7306. path (substring path 0 (match-beginning 0)))))
  7307. (if (string-match "[*?{]" (file-name-nondirectory path))
  7308. (dired path)
  7309. (org-open-file path in-emacs line search)))
  7310. ((string= type "news")
  7311. (require 'org-gnus)
  7312. (org-gnus-follow-link path))
  7313. ((string= type "shell")
  7314. (let ((cmd path))
  7315. (if (or (not org-confirm-shell-link-function)
  7316. (funcall org-confirm-shell-link-function
  7317. (format "Execute \"%s\" in shell? "
  7318. (org-add-props cmd nil
  7319. 'face 'org-warning))))
  7320. (progn
  7321. (message "Executing %s" cmd)
  7322. (shell-command cmd))
  7323. (error "Abort"))))
  7324. ((string= type "elisp")
  7325. (let ((cmd path))
  7326. (if (or (not org-confirm-elisp-link-function)
  7327. (funcall org-confirm-elisp-link-function
  7328. (format "Execute \"%s\" as elisp? "
  7329. (org-add-props cmd nil
  7330. 'face 'org-warning))))
  7331. (message "%s => %s" cmd
  7332. (if (equal (string-to-char cmd) ?\()
  7333. (eval (read cmd))
  7334. (call-interactively (read cmd))))
  7335. (error "Abort"))))
  7336. (t
  7337. (browse-url-at-point))))))
  7338. (move-marker org-open-link-marker nil)
  7339. (run-hook-with-args 'org-follow-link-hook))
  7340. ;;;; Time estimates
  7341. (defun org-get-effort (&optional pom)
  7342. "Get the effort estimate for the current entry."
  7343. (org-entry-get pom org-effort-property))
  7344. ;;; File search
  7345. (defvar org-create-file-search-functions nil
  7346. "List of functions to construct the right search string for a file link.
  7347. These functions are called in turn with point at the location to
  7348. which the link should point.
  7349. A function in the hook should first test if it would like to
  7350. handle this file type, for example by checking the major-mode or
  7351. the file extension. If it decides not to handle this file, it
  7352. should just return nil to give other functions a chance. If it
  7353. does handle the file, it must return the search string to be used
  7354. when following the link. The search string will be part of the
  7355. file link, given after a double colon, and `org-open-at-point'
  7356. will automatically search for it. If special measures must be
  7357. taken to make the search successful, another function should be
  7358. added to the companion hook `org-execute-file-search-functions',
  7359. which see.
  7360. A function in this hook may also use `setq' to set the variable
  7361. `description' to provide a suggestion for the descriptive text to
  7362. be used for this link when it gets inserted into an Org-mode
  7363. buffer with \\[org-insert-link].")
  7364. (defvar org-execute-file-search-functions nil
  7365. "List of functions to execute a file search triggered by a link.
  7366. Functions added to this hook must accept a single argument, the
  7367. search string that was part of the file link, the part after the
  7368. double colon. The function must first check if it would like to
  7369. handle this search, for example by checking the major-mode or the
  7370. file extension. If it decides not to handle this search, it
  7371. should just return nil to give other functions a chance. If it
  7372. does handle the search, it must return a non-nil value to keep
  7373. other functions from trying.
  7374. Each function can access the current prefix argument through the
  7375. variable `current-prefix-argument'. Note that a single prefix is
  7376. used to force opening a link in Emacs, so it may be good to only
  7377. use a numeric or double prefix to guide the search function.
  7378. In case this is needed, a function in this hook can also restore
  7379. the window configuration before `org-open-at-point' was called using:
  7380. (set-window-configuration org-window-config-before-follow-link)")
  7381. (defun org-link-search (s &optional type avoid-pos)
  7382. "Search for a link search option.
  7383. If S is surrounded by forward slashes, it is interpreted as a
  7384. regular expression. In org-mode files, this will create an `org-occur'
  7385. sparse tree. In ordinary files, `occur' will be used to list matches.
  7386. If the current buffer is in `dired-mode', grep will be used to search
  7387. in all files. If AVOID-POS is given, ignore matches near that position."
  7388. (let ((case-fold-search t)
  7389. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7390. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7391. (append '(("") (" ") ("\t") ("\n"))
  7392. org-emphasis-alist)
  7393. "\\|") "\\)"))
  7394. (pos (point))
  7395. (pre nil) (post nil)
  7396. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7397. (cond
  7398. ;; First check if there are any special
  7399. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7400. ;; Now try the builtin stuff
  7401. ((and (equal (string-to-char s0) ?#)
  7402. (> (length s0) 1)
  7403. (save-excursion
  7404. (goto-char (point-min))
  7405. (and
  7406. (re-search-forward
  7407. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7408. (setq type 'dedicated
  7409. pos (match-beginning 0))))
  7410. ;; There is an exact target for this
  7411. (goto-char pos)
  7412. (org-back-to-heading t)))
  7413. ((save-excursion
  7414. (goto-char (point-min))
  7415. (and
  7416. (re-search-forward
  7417. (concat "<<" (regexp-quote s0) ">>") nil t)
  7418. (setq type 'dedicated
  7419. pos (match-beginning 0))))
  7420. ;; There is an exact target for this
  7421. (goto-char pos))
  7422. ((and (string-match "^(\\(.*\\))$" s0)
  7423. (save-excursion
  7424. (goto-char (point-min))
  7425. (and
  7426. (re-search-forward
  7427. (concat "[^[]" (regexp-quote
  7428. (format org-coderef-label-format
  7429. (match-string 1 s0))))
  7430. nil t)
  7431. (setq type 'dedicated
  7432. pos (1+ (match-beginning 0))))))
  7433. ;; There is a coderef target for this
  7434. (goto-char pos))
  7435. ((string-match "^/\\(.*\\)/$" s)
  7436. ;; A regular expression
  7437. (cond
  7438. ((org-mode-p)
  7439. (org-occur (match-string 1 s)))
  7440. ;;((eq major-mode 'dired-mode)
  7441. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7442. (t (org-do-occur (match-string 1 s)))))
  7443. (t
  7444. ;; A normal search strings
  7445. (when (equal (string-to-char s) ?*)
  7446. ;; Anchor on headlines, post may include tags.
  7447. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7448. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7449. s (substring s 1)))
  7450. (remove-text-properties
  7451. 0 (length s)
  7452. '(face nil mouse-face nil keymap nil fontified nil) s)
  7453. ;; Make a series of regular expressions to find a match
  7454. (setq words (org-split-string s "[ \n\r\t]+")
  7455. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7456. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7457. "\\)" markers)
  7458. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7459. re2a (concat "[ \t\r\n]" re2a_)
  7460. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7461. re4 (concat "[^a-zA-Z_]" re4_)
  7462. re1 (concat pre re2 post)
  7463. re3 (concat pre (if pre re4_ re4) post)
  7464. re5 (concat pre ".*" re4)
  7465. re2 (concat pre re2)
  7466. re2a (concat pre (if pre re2a_ re2a))
  7467. re4 (concat pre (if pre re4_ re4))
  7468. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7469. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7470. re5 "\\)"
  7471. ))
  7472. (cond
  7473. ((eq type 'org-occur) (org-occur reall))
  7474. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7475. (t (goto-char (point-min))
  7476. (setq type 'fuzzy)
  7477. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7478. (org-search-not-self 1 re1 nil t)
  7479. (org-search-not-self 1 re2 nil t)
  7480. (org-search-not-self 1 re2a nil t)
  7481. (org-search-not-self 1 re3 nil t)
  7482. (org-search-not-self 1 re4 nil t)
  7483. (org-search-not-self 1 re5 nil t)
  7484. )
  7485. (goto-char (match-beginning 1))
  7486. (goto-char pos)
  7487. (error "No match")))))
  7488. (t
  7489. ;; Normal string-search
  7490. (goto-char (point-min))
  7491. (if (search-forward s nil t)
  7492. (goto-char (match-beginning 0))
  7493. (error "No match"))))
  7494. (and (org-mode-p) (org-show-context 'link-search))
  7495. type))
  7496. (defun org-search-not-self (group &rest args)
  7497. "Execute `re-search-forward', but only accept matches that do not
  7498. enclose the position of `org-open-link-marker'."
  7499. (let ((m org-open-link-marker))
  7500. (catch 'exit
  7501. (while (apply 're-search-forward args)
  7502. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7503. (goto-char (match-end group))
  7504. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7505. (> (match-beginning 0) (marker-position m))
  7506. (< (match-end 0) (marker-position m)))
  7507. (save-match-data
  7508. (or (not (org-in-regexp
  7509. org-bracket-link-analytic-regexp 1))
  7510. (not (match-end 4)) ; no description
  7511. (and (<= (match-beginning 4) (point))
  7512. (>= (match-end 4) (point))))))
  7513. (throw 'exit (point))))))))
  7514. (defun org-get-buffer-for-internal-link (buffer)
  7515. "Return a buffer to be used for displaying the link target of internal links."
  7516. (cond
  7517. ((not org-display-internal-link-with-indirect-buffer)
  7518. buffer)
  7519. ((string-match "(Clone)$" (buffer-name buffer))
  7520. (message "Buffer is already a clone, not making another one")
  7521. ;; we also do not modify visibility in this case
  7522. buffer)
  7523. (t ; make a new indirect buffer for displaying the link
  7524. (let* ((bn (buffer-name buffer))
  7525. (ibn (concat bn "(Clone)"))
  7526. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7527. (with-current-buffer ib (org-overview))
  7528. ib))))
  7529. (defun org-do-occur (regexp &optional cleanup)
  7530. "Call the Emacs command `occur'.
  7531. If CLEANUP is non-nil, remove the printout of the regular expression
  7532. in the *Occur* buffer. This is useful if the regex is long and not useful
  7533. to read."
  7534. (occur regexp)
  7535. (when cleanup
  7536. (let ((cwin (selected-window)) win beg end)
  7537. (when (setq win (get-buffer-window "*Occur*"))
  7538. (select-window win))
  7539. (goto-char (point-min))
  7540. (when (re-search-forward "match[a-z]+" nil t)
  7541. (setq beg (match-end 0))
  7542. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7543. (setq end (1- (match-beginning 0)))))
  7544. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7545. (goto-char (point-min))
  7546. (select-window cwin))))
  7547. ;;; The mark ring for links jumps
  7548. (defvar org-mark-ring nil
  7549. "Mark ring for positions before jumps in Org-mode.")
  7550. (defvar org-mark-ring-last-goto nil
  7551. "Last position in the mark ring used to go back.")
  7552. ;; Fill and close the ring
  7553. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7554. (loop for i from 1 to org-mark-ring-length do
  7555. (push (make-marker) org-mark-ring))
  7556. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7557. org-mark-ring)
  7558. (defun org-mark-ring-push (&optional pos buffer)
  7559. "Put the current position or POS into the mark ring and rotate it."
  7560. (interactive)
  7561. (setq pos (or pos (point)))
  7562. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7563. (move-marker (car org-mark-ring)
  7564. (or pos (point))
  7565. (or buffer (current-buffer)))
  7566. (message "%s"
  7567. (substitute-command-keys
  7568. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7569. (defun org-mark-ring-goto (&optional n)
  7570. "Jump to the previous position in the mark ring.
  7571. With prefix arg N, jump back that many stored positions. When
  7572. called several times in succession, walk through the entire ring.
  7573. Org-mode commands jumping to a different position in the current file,
  7574. or to another Org-mode file, automatically push the old position
  7575. onto the ring."
  7576. (interactive "p")
  7577. (let (p m)
  7578. (if (eq last-command this-command)
  7579. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7580. (setq p org-mark-ring))
  7581. (setq org-mark-ring-last-goto p)
  7582. (setq m (car p))
  7583. (switch-to-buffer (marker-buffer m))
  7584. (goto-char m)
  7585. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7586. (defun org-remove-angle-brackets (s)
  7587. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7588. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7589. s)
  7590. (defun org-add-angle-brackets (s)
  7591. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7592. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7593. s)
  7594. (defun org-remove-double-quotes (s)
  7595. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7596. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7597. s)
  7598. ;;; Following specific links
  7599. (defun org-follow-timestamp-link ()
  7600. (cond
  7601. ((org-at-date-range-p t)
  7602. (let ((org-agenda-start-on-weekday)
  7603. (t1 (match-string 1))
  7604. (t2 (match-string 2)))
  7605. (setq t1 (time-to-days (org-time-string-to-time t1))
  7606. t2 (time-to-days (org-time-string-to-time t2)))
  7607. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7608. ((org-at-timestamp-p t)
  7609. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7610. (substring (match-string 1) 0 10)))
  7611. 1))
  7612. (t (error "This should not happen"))))
  7613. ;;; Following file links
  7614. (defvar org-wait nil)
  7615. (defun org-open-file (path &optional in-emacs line search)
  7616. "Open the file at PATH.
  7617. First, this expands any special file name abbreviations. Then the
  7618. configuration variable `org-file-apps' is checked if it contains an
  7619. entry for this file type, and if yes, the corresponding command is launched.
  7620. If no application is found, Emacs simply visits the file.
  7621. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7622. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7623. and o use an external application to visit the file.
  7624. Optional LINE specifies a line to go to, optional SEARCH a string to
  7625. search for. If LINE or SEARCH is given, the file will always be
  7626. opened in Emacs.
  7627. If the file does not exist, an error is thrown."
  7628. (setq in-emacs (or in-emacs line search))
  7629. (let* ((file (if (equal path "")
  7630. buffer-file-name
  7631. (substitute-in-file-name (expand-file-name path))))
  7632. (apps (append org-file-apps (org-default-apps)))
  7633. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7634. (dirp (if remp nil (file-directory-p file)))
  7635. (file (if (and dirp org-open-directory-means-index-dot-org)
  7636. (concat (file-name-as-directory file) "index.org")
  7637. file))
  7638. (a-m-a-p (assq 'auto-mode apps))
  7639. (dfile (downcase file))
  7640. (old-buffer (current-buffer))
  7641. (old-pos (point))
  7642. (old-mode major-mode)
  7643. ext cmd)
  7644. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7645. (setq ext (match-string 1 dfile))
  7646. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7647. (setq ext (match-string 1 dfile))))
  7648. (cond
  7649. ((equal in-emacs '(16))
  7650. (setq cmd (cdr (assoc 'system apps))))
  7651. (in-emacs (setq cmd 'emacs))
  7652. (t
  7653. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7654. (and dirp (cdr (assoc 'directory apps)))
  7655. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7656. 'string-match)
  7657. (cdr (assoc ext apps))
  7658. (cdr (assoc t apps))))))
  7659. (when (eq cmd 'system)
  7660. (setq cmd (cdr (assoc 'system apps))))
  7661. (when (eq cmd 'default)
  7662. (setq cmd (cdr (assoc t apps))))
  7663. (when (eq cmd 'mailcap)
  7664. (require 'mailcap)
  7665. (mailcap-parse-mailcaps)
  7666. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7667. (command (mailcap-mime-info mime-type)))
  7668. (if (stringp command)
  7669. (setq cmd command)
  7670. (setq cmd 'emacs))))
  7671. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7672. (not (file-exists-p file))
  7673. (not org-open-non-existing-files))
  7674. (error "No such file: %s" file))
  7675. (cond
  7676. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7677. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7678. (while (string-match "['\"]%s['\"]" cmd)
  7679. (setq cmd (replace-match "%s" t t cmd)))
  7680. (while (string-match "%s" cmd)
  7681. (setq cmd (replace-match
  7682. (save-match-data
  7683. (shell-quote-argument
  7684. (convert-standard-filename file)))
  7685. t t cmd)))
  7686. (save-window-excursion
  7687. (start-process-shell-command cmd nil cmd)
  7688. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7689. ))
  7690. ((or (stringp cmd)
  7691. (eq cmd 'emacs))
  7692. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7693. (widen)
  7694. (if line (goto-line line)
  7695. (if search (org-link-search search))))
  7696. ((consp cmd)
  7697. (let ((file (convert-standard-filename file)))
  7698. (eval cmd)))
  7699. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7700. (and (org-mode-p) (eq old-mode 'org-mode)
  7701. (or (not (equal old-buffer (current-buffer)))
  7702. (not (equal old-pos (point))))
  7703. (org-mark-ring-push old-pos old-buffer))))
  7704. (defun org-default-apps ()
  7705. "Return the default applications for this operating system."
  7706. (cond
  7707. ((eq system-type 'darwin)
  7708. org-file-apps-defaults-macosx)
  7709. ((eq system-type 'windows-nt)
  7710. org-file-apps-defaults-windowsnt)
  7711. (t org-file-apps-defaults-gnu)))
  7712. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7713. "Convert extensions to regular expressions in the cars of LIST.
  7714. Also, weed out any non-string entries, because the return value is used
  7715. only for regexp matching.
  7716. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7717. point to the symbol `emacs', indicating that the file should
  7718. be opened in Emacs."
  7719. (append
  7720. (delq nil
  7721. (mapcar (lambda (x)
  7722. (if (not (stringp (car x)))
  7723. nil
  7724. (if (string-match "\\W" (car x))
  7725. x
  7726. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7727. list))
  7728. (if add-auto-mode
  7729. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7730. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7731. (defun org-file-remote-p (file)
  7732. "Test whether FILE specifies a location on a remote system.
  7733. Return non-nil if the location is indeed remote.
  7734. For example, the filename \"/user@host:/foo\" specifies a location
  7735. on the system \"/user@host:\"."
  7736. (cond ((fboundp 'file-remote-p)
  7737. (file-remote-p file))
  7738. ((fboundp 'tramp-handle-file-remote-p)
  7739. (tramp-handle-file-remote-p file))
  7740. ((and (boundp 'ange-ftp-name-format)
  7741. (string-match (car ange-ftp-name-format) file))
  7742. t)
  7743. (t nil)))
  7744. ;;;; Refiling
  7745. (defun org-get-org-file ()
  7746. "Read a filename, with default directory `org-directory'."
  7747. (let ((default (or org-default-notes-file remember-data-file)))
  7748. (read-file-name (format "File name [%s]: " default)
  7749. (file-name-as-directory org-directory)
  7750. default)))
  7751. (defun org-notes-order-reversed-p ()
  7752. "Check if the current file should receive notes in reversed order."
  7753. (cond
  7754. ((not org-reverse-note-order) nil)
  7755. ((eq t org-reverse-note-order) t)
  7756. ((not (listp org-reverse-note-order)) nil)
  7757. (t (catch 'exit
  7758. (let ((all org-reverse-note-order)
  7759. entry)
  7760. (while (setq entry (pop all))
  7761. (if (string-match (car entry) buffer-file-name)
  7762. (throw 'exit (cdr entry))))
  7763. nil)))))
  7764. (defvar org-refile-target-table nil
  7765. "The list of refile targets, created by `org-refile'.")
  7766. (defvar org-agenda-new-buffers nil
  7767. "Buffers created to visit agenda files.")
  7768. (defun org-get-refile-targets (&optional default-buffer)
  7769. "Produce a table with refile targets."
  7770. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7771. targets txt re files f desc descre fast-path-p level pos0)
  7772. (message "Getting targets...")
  7773. (with-current-buffer (or default-buffer (current-buffer))
  7774. (while (setq entry (pop entries))
  7775. (setq files (car entry) desc (cdr entry))
  7776. (setq fast-path-p nil)
  7777. (cond
  7778. ((null files) (setq files (list (current-buffer))))
  7779. ((eq files 'org-agenda-files)
  7780. (setq files (org-agenda-files 'unrestricted)))
  7781. ((and (symbolp files) (fboundp files))
  7782. (setq files (funcall files)))
  7783. ((and (symbolp files) (boundp files))
  7784. (setq files (symbol-value files))))
  7785. (if (stringp files) (setq files (list files)))
  7786. (cond
  7787. ((eq (car desc) :tag)
  7788. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7789. ((eq (car desc) :todo)
  7790. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7791. ((eq (car desc) :regexp)
  7792. (setq descre (cdr desc)))
  7793. ((eq (car desc) :level)
  7794. (setq descre (concat "^\\*\\{" (number-to-string
  7795. (if org-odd-levels-only
  7796. (1- (* 2 (cdr desc)))
  7797. (cdr desc)))
  7798. "\\}[ \t]")))
  7799. ((eq (car desc) :maxlevel)
  7800. (setq fast-path-p t)
  7801. (setq descre (concat "^\\*\\{1," (number-to-string
  7802. (if org-odd-levels-only
  7803. (1- (* 2 (cdr desc)))
  7804. (cdr desc)))
  7805. "\\}[ \t]")))
  7806. (t (error "Bad refiling target description %s" desc)))
  7807. (while (setq f (pop files))
  7808. (save-excursion
  7809. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7810. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7811. (setq f (expand-file-name f))
  7812. (if (eq org-refile-use-outline-path 'file)
  7813. (push (list (file-name-nondirectory f) f nil nil) targets))
  7814. (save-excursion
  7815. (save-restriction
  7816. (widen)
  7817. (goto-char (point-min))
  7818. (while (re-search-forward descre nil t)
  7819. (goto-char (setq pos0 (point-at-bol)))
  7820. (catch 'next
  7821. (when org-refile-target-verify-function
  7822. (save-match-data
  7823. (or (funcall org-refile-target-verify-function)
  7824. (throw 'next t))))
  7825. (when (looking-at org-complex-heading-regexp)
  7826. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7827. txt (org-link-display-format (match-string 4))
  7828. re (concat "^" (regexp-quote
  7829. (buffer-substring (match-beginning 1)
  7830. (match-end 4)))))
  7831. (if (match-end 5) (setq re (concat re "[ \t]+"
  7832. (regexp-quote
  7833. (match-string 5)))))
  7834. (setq re (concat re "[ \t]*$"))
  7835. (when org-refile-use-outline-path
  7836. (setq txt (mapconcat 'org-protect-slash
  7837. (append
  7838. (if (eq org-refile-use-outline-path 'file)
  7839. (list (file-name-nondirectory
  7840. (buffer-file-name (buffer-base-buffer))))
  7841. (if (eq org-refile-use-outline-path 'full-file-path)
  7842. (list (buffer-file-name (buffer-base-buffer)))))
  7843. (org-get-outline-path fast-path-p level txt)
  7844. (list txt))
  7845. "/")))
  7846. (push (list txt f re (point)) targets)))
  7847. (when (= (point) pos0)
  7848. ;; verification function has not moved point
  7849. (goto-char (point-at-eol))))))))))
  7850. (message "Getting targets...done")
  7851. (nreverse targets)))
  7852. (defun org-protect-slash (s)
  7853. (while (string-match "/" s)
  7854. (setq s (replace-match "\\" t t s)))
  7855. s)
  7856. (defvar org-olpa (make-vector 20 nil))
  7857. (defun org-get-outline-path (&optional fastp level heading)
  7858. "Return the outline path to the current entry, as a list."
  7859. (if fastp
  7860. (progn
  7861. (if (> level 19)
  7862. (error "Outline path failure, more than 19 levels."))
  7863. (loop for i from level upto 19 do
  7864. (aset org-olpa i nil))
  7865. (prog1
  7866. (delq nil (append org-olpa nil))
  7867. (aset org-olpa level heading)))
  7868. (let (rtn)
  7869. (save-excursion
  7870. (while (org-up-heading-safe)
  7871. (when (looking-at org-complex-heading-regexp)
  7872. (push (org-match-string-no-properties 4) rtn)))
  7873. rtn))))
  7874. (defvar org-refile-history nil
  7875. "History for refiling operations.")
  7876. (defvar org-after-refile-insert-hook nil
  7877. "Hook run after `org-refile' has inserted its stuff at the new location.
  7878. Note that this is still *before* the stuff will be removed from
  7879. the *old* location.")
  7880. (defun org-refile (&optional goto default-buffer)
  7881. "Move the entry at point to another heading.
  7882. The list of target headings is compiled using the information in
  7883. `org-refile-targets', which see. This list is created before each use
  7884. and will therefore always be up-to-date.
  7885. At the target location, the entry is filed as a subitem of the target heading.
  7886. Depending on `org-reverse-note-order', the new subitem will either be the
  7887. first or the last subitem.
  7888. If there is an active region, all entries in that region will be moved.
  7889. However, the region must fulfil the requirement that the first heading
  7890. is the first one sets the top-level of the moved text - at most siblings
  7891. below it are allowed.
  7892. With prefix arg GOTO, the command will only visit the target location,
  7893. not actually move anything.
  7894. With a double prefix `C-u C-u', go to the location where the last refiling
  7895. operation has put the subtree.
  7896. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7897. (interactive "P")
  7898. (let* ((cbuf (current-buffer))
  7899. (regionp (org-region-active-p))
  7900. (region-start (and regionp (region-beginning)))
  7901. (region-end (and regionp (region-end)))
  7902. (region-length (and regionp (- region-end region-start)))
  7903. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7904. pos it nbuf file re level reversed)
  7905. (when regionp (goto-char region-start)
  7906. (unless (org-kill-is-subtree-p
  7907. (buffer-substring region-start region-end))
  7908. (error "The region is not a (sequence of) subtree(s)")))
  7909. (if (equal goto '(16))
  7910. (org-refile-goto-last-stored)
  7911. (when (setq it (org-refile-get-location
  7912. (if goto "Goto: " "Refile to: ") default-buffer
  7913. org-refile-allow-creating-parent-nodes))
  7914. (setq file (nth 1 it)
  7915. re (nth 2 it)
  7916. pos (nth 3 it))
  7917. (if (and (not goto)
  7918. pos
  7919. (equal (buffer-file-name) file)
  7920. (if regionp
  7921. (and (>= pos region-start)
  7922. (<= pos region-end))
  7923. (and (>= pos (point))
  7924. (< pos (save-excursion
  7925. (org-end-of-subtree t t))))))
  7926. (error "Cannot refile to position inside the tree or region"))
  7927. (setq nbuf (or (find-buffer-visiting file)
  7928. (find-file-noselect file)))
  7929. (if goto
  7930. (progn
  7931. (switch-to-buffer nbuf)
  7932. (goto-char pos)
  7933. (org-show-context 'org-goto))
  7934. (if regionp
  7935. (progn
  7936. (kill-new (buffer-substring region-start region-end))
  7937. (org-save-markers-in-region region-start region-end))
  7938. (org-copy-subtree 1 nil t))
  7939. (save-excursion
  7940. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7941. (find-file-noselect file))))
  7942. (setq reversed (org-notes-order-reversed-p))
  7943. (save-excursion
  7944. (save-restriction
  7945. (widen)
  7946. (if pos
  7947. (progn
  7948. (goto-char pos)
  7949. (looking-at outline-regexp)
  7950. (setq level (org-get-valid-level (funcall outline-level) 1))
  7951. (goto-char
  7952. (if reversed
  7953. (or (outline-next-heading) (point-max))
  7954. (or (save-excursion (outline-get-next-sibling))
  7955. (org-end-of-subtree t t)
  7956. (point-max)))))
  7957. (setq level 1)
  7958. (if (not reversed)
  7959. (goto-char (point-max))
  7960. (goto-char (point-min))
  7961. (or (outline-next-heading) (goto-char (point-max)))))
  7962. (if (not (bolp)) (newline))
  7963. (bookmark-set "org-refile-last-stored")
  7964. (org-paste-subtree level)
  7965. (run-hooks 'org-after-refile-insert-hook))))
  7966. (if regionp
  7967. (delete-region (point) (+ (point) region-length))
  7968. (org-cut-subtree))
  7969. (setq org-markers-to-move nil)
  7970. (message "Refiled to \"%s\"" (car it))))))
  7971. (org-reveal))
  7972. (defun org-refile-goto-last-stored ()
  7973. "Go to the location where the last refile was stored."
  7974. (interactive)
  7975. (bookmark-jump "org-refile-last-stored")
  7976. (message "This is the location of the last refile"))
  7977. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7978. "Prompt the user for a refile location, using PROMPT."
  7979. (let ((org-refile-targets org-refile-targets)
  7980. (org-refile-use-outline-path org-refile-use-outline-path))
  7981. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7982. (unless org-refile-target-table
  7983. (error "No refile targets"))
  7984. (let* ((cbuf (current-buffer))
  7985. (partial-completion-mode nil)
  7986. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7987. (cfunc (if (and org-refile-use-outline-path
  7988. org-outline-path-complete-in-steps)
  7989. 'org-olpath-completing-read
  7990. 'org-ido-completing-read))
  7991. (extra (if org-refile-use-outline-path "/" ""))
  7992. (filename (and cfn (expand-file-name cfn)))
  7993. (tbl (mapcar
  7994. (lambda (x)
  7995. (if (and (not (member org-refile-use-outline-path
  7996. '(file full-file-path)))
  7997. (not (equal filename (nth 1 x))))
  7998. (cons (concat (car x) extra " ("
  7999. (file-name-nondirectory (nth 1 x)) ")")
  8000. (cdr x))
  8001. (cons (concat (car x) extra) (cdr x))))
  8002. org-refile-target-table))
  8003. (completion-ignore-case t)
  8004. pa answ parent-target child parent)
  8005. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8006. nil 'org-refile-history))
  8007. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8008. (if pa
  8009. pa
  8010. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8011. (setq parent (match-string 1 answ)
  8012. child (match-string 2 answ))
  8013. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8014. (when (and parent-target
  8015. (or (eq new-nodes t)
  8016. (and (eq new-nodes 'confirm)
  8017. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8018. (org-refile-new-child parent-target child))))))
  8019. (defun org-refile-new-child (parent-target child)
  8020. "Use refile target PARENT-TARGET to add new CHILD below it."
  8021. (unless parent-target
  8022. (error "Cannot find parent for new node"))
  8023. (let ((file (nth 1 parent-target))
  8024. (pos (nth 3 parent-target))
  8025. level)
  8026. (with-current-buffer (or (find-buffer-visiting file)
  8027. (find-file-noselect file))
  8028. (save-excursion
  8029. (save-restriction
  8030. (widen)
  8031. (if pos
  8032. (goto-char pos)
  8033. (goto-char (point-max))
  8034. (if (not (bolp)) (newline)))
  8035. (when (looking-at outline-regexp)
  8036. (setq level (funcall outline-level))
  8037. (org-end-of-subtree t t))
  8038. (org-back-over-empty-lines)
  8039. (insert "\n" (make-string
  8040. (if pos (org-get-valid-level level 1) 1) ?*)
  8041. " " child "\n")
  8042. (beginning-of-line 0)
  8043. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8044. (defun org-olpath-completing-read (prompt collection &rest args)
  8045. "Read an outline path like a file name."
  8046. (let ((thetable collection)
  8047. (org-completion-use-ido nil)) ; does not work with ido.
  8048. (apply
  8049. 'org-ido-completing-read prompt
  8050. (lambda (string predicate &optional flag)
  8051. (let (rtn r f (l (length string)))
  8052. (cond
  8053. ((eq flag nil)
  8054. ;; try completion
  8055. (try-completion string thetable))
  8056. ((eq flag t)
  8057. ;; all-completions
  8058. (setq rtn (all-completions string thetable predicate))
  8059. (mapcar
  8060. (lambda (x)
  8061. (setq r (substring x l))
  8062. (if (string-match " ([^)]*)$" x)
  8063. (setq f (match-string 0 x))
  8064. (setq f ""))
  8065. (if (string-match "/" r)
  8066. (concat string (substring r 0 (match-end 0)) f)
  8067. x))
  8068. rtn))
  8069. ((eq flag 'lambda)
  8070. ;; exact match?
  8071. (assoc string thetable)))
  8072. ))
  8073. args)))
  8074. ;;;; Dynamic blocks
  8075. (defun org-find-dblock (name)
  8076. "Find the first dynamic block with name NAME in the buffer.
  8077. If not found, stay at current position and return nil."
  8078. (let (pos)
  8079. (save-excursion
  8080. (goto-char (point-min))
  8081. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8082. nil t)
  8083. (match-beginning 0))))
  8084. (if pos (goto-char pos))
  8085. pos))
  8086. (defconst org-dblock-start-re
  8087. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8088. "Matches the startline of a dynamic block, with parameters.")
  8089. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8090. "Matches the end of a dynamic block.")
  8091. (defun org-create-dblock (plist)
  8092. "Create a dynamic block section, with parameters taken from PLIST.
  8093. PLIST must contain a :name entry which is used as name of the block."
  8094. (unless (bolp) (newline))
  8095. (let ((name (plist-get plist :name)))
  8096. (insert "#+BEGIN: " name)
  8097. (while plist
  8098. (if (eq (car plist) :name)
  8099. (setq plist (cddr plist))
  8100. (insert " " (prin1-to-string (pop plist)))))
  8101. (insert "\n\n#+END:\n")
  8102. (beginning-of-line -2)))
  8103. (defun org-prepare-dblock ()
  8104. "Prepare dynamic block for refresh.
  8105. This empties the block, puts the cursor at the insert position and returns
  8106. the property list including an extra property :name with the block name."
  8107. (unless (looking-at org-dblock-start-re)
  8108. (error "Not at a dynamic block"))
  8109. (let* ((begdel (1+ (match-end 0)))
  8110. (name (org-no-properties (match-string 1)))
  8111. (params (append (list :name name)
  8112. (read (concat "(" (match-string 3) ")")))))
  8113. (unless (re-search-forward org-dblock-end-re nil t)
  8114. (error "Dynamic block not terminated"))
  8115. (setq params
  8116. (append params
  8117. (list :content (buffer-substring
  8118. begdel (match-beginning 0)))))
  8119. (delete-region begdel (match-beginning 0))
  8120. (goto-char begdel)
  8121. (open-line 1)
  8122. params))
  8123. (defun org-map-dblocks (&optional command)
  8124. "Apply COMMAND to all dynamic blocks in the current buffer.
  8125. If COMMAND is not given, use `org-update-dblock'."
  8126. (let ((cmd (or command 'org-update-dblock))
  8127. pos)
  8128. (save-excursion
  8129. (goto-char (point-min))
  8130. (while (re-search-forward org-dblock-start-re nil t)
  8131. (goto-char (setq pos (match-beginning 0)))
  8132. (condition-case nil
  8133. (funcall cmd)
  8134. (error (message "Error during update of dynamic block")))
  8135. (goto-char pos)
  8136. (unless (re-search-forward org-dblock-end-re nil t)
  8137. (error "Dynamic block not terminated"))))))
  8138. (defun org-dblock-update (&optional arg)
  8139. "User command for updating dynamic blocks.
  8140. Update the dynamic block at point. With prefix ARG, update all dynamic
  8141. blocks in the buffer."
  8142. (interactive "P")
  8143. (if arg
  8144. (org-update-all-dblocks)
  8145. (or (looking-at org-dblock-start-re)
  8146. (org-beginning-of-dblock))
  8147. (org-update-dblock)))
  8148. (defun org-update-dblock ()
  8149. "Update the dynamic block at point
  8150. This means to empty the block, parse for parameters and then call
  8151. the correct writing function."
  8152. (save-window-excursion
  8153. (let* ((pos (point))
  8154. (line (org-current-line))
  8155. (params (org-prepare-dblock))
  8156. (name (plist-get params :name))
  8157. (cmd (intern (concat "org-dblock-write:" name))))
  8158. (message "Updating dynamic block `%s' at line %d..." name line)
  8159. (funcall cmd params)
  8160. (message "Updating dynamic block `%s' at line %d...done" name line)
  8161. (goto-char pos))))
  8162. (defun org-beginning-of-dblock ()
  8163. "Find the beginning of the dynamic block at point.
  8164. Error if there is no such block at point."
  8165. (let ((pos (point))
  8166. beg)
  8167. (end-of-line 1)
  8168. (if (and (re-search-backward org-dblock-start-re nil t)
  8169. (setq beg (match-beginning 0))
  8170. (re-search-forward org-dblock-end-re nil t)
  8171. (> (match-end 0) pos))
  8172. (goto-char beg)
  8173. (goto-char pos)
  8174. (error "Not in a dynamic block"))))
  8175. (defun org-update-all-dblocks ()
  8176. "Update all dynamic blocks in the buffer.
  8177. This function can be used in a hook."
  8178. (when (org-mode-p)
  8179. (org-map-dblocks 'org-update-dblock)))
  8180. ;;;; Completion
  8181. (defconst org-additional-option-like-keywords
  8182. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  8183. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  8184. "LATEX_HEADER:"
  8185. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8186. "BEGIN_QUOTE" "END_QUOTE"
  8187. "BEGIN_VERSE" "END_VERSE"
  8188. "BEGIN_CENTER" "END_CENTER"
  8189. "BEGIN_SRC" "END_SRC"
  8190. "CATEGORY" "COLUMNS"
  8191. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  8192. (defcustom org-structure-template-alist
  8193. '(
  8194. ("s" "#+begin_src ?\n\n#+end_src"
  8195. "<src lang=\"?\">\n\n</src>")
  8196. ("e" "#+begin_example\n?\n#+end_example"
  8197. "<example>\n?\n</example>")
  8198. ("q" "#+begin_quote\n?\n#+end_quote"
  8199. "<quote>\n?\n</quote>")
  8200. ("v" "#+begin_verse\n?\n#+end_verse"
  8201. "<verse>\n?\n/verse>")
  8202. ("c" "#+begin_center\n?\n#+end_center"
  8203. "<center>\n?\n/center>")
  8204. ("l" "#+begin_latex\n?\n#+end_latex"
  8205. "<literal style=\"latex\">\n?\n</literal>")
  8206. ("L" "#+latex: "
  8207. "<literal style=\"latex\">?</literal>")
  8208. ("h" "#+begin_html\n?\n#+end_html"
  8209. "<literal style=\"html\">\n?\n</literal>")
  8210. ("H" "#+html: "
  8211. "<literal style=\"html\">?</literal>")
  8212. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8213. ("A" "#+ascii: ")
  8214. ("i" "#+include %file ?"
  8215. "<include file=%file markup=\"?\">")
  8216. )
  8217. "Structure completion elements.
  8218. This is a list of abbreviation keys and values. The value gets inserted
  8219. it you type @samp{.} followed by the key and then the completion key,
  8220. usually `M-TAB'. %file will be replaced by a file name after prompting
  8221. for the file using completion.
  8222. There are two templates for each key, the first uses the original Org syntax,
  8223. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8224. the default when the /org-mtags.el/ module has been loaded. See also the
  8225. variable `org-mtags-prefer-muse-templates'.
  8226. This is an experimental feature, it is undecided if it is going to stay in."
  8227. :group 'org-completion
  8228. :type '(repeat
  8229. (string :tag "Key")
  8230. (string :tag "Template")
  8231. (string :tag "Muse Template")))
  8232. (defun org-try-structure-completion ()
  8233. "Try to complete a structure template before point.
  8234. This looks for strings like \"<e\" on an otherwise empty line and
  8235. expands them."
  8236. (let ((l (buffer-substring (point-at-bol) (point)))
  8237. a)
  8238. (when (and (looking-at "[ \t]*$")
  8239. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8240. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8241. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8242. (match-beginning 1)) a)
  8243. t)))
  8244. (defun org-complete-expand-structure-template (start cell)
  8245. "Expand a structure template."
  8246. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8247. (rpl (nth (if musep 2 1) cell))
  8248. (ind ""))
  8249. (delete-region start (point))
  8250. (when (string-match "\\`#\\+" rpl)
  8251. (cond
  8252. ((bolp))
  8253. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8254. (setq ind (buffer-substring (point-at-bol) (point))))
  8255. (t (newline))))
  8256. (setq start (point))
  8257. (if (string-match "%file" rpl)
  8258. (setq rpl (replace-match
  8259. (concat
  8260. "\""
  8261. (save-match-data
  8262. (abbreviate-file-name (read-file-name "Include file: ")))
  8263. "\"")
  8264. t t rpl)))
  8265. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8266. (concat "\n" ind)))
  8267. (insert rpl)
  8268. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8269. (defun org-complete (&optional arg)
  8270. "Perform completion on word at point.
  8271. At the beginning of a headline, this completes TODO keywords as given in
  8272. `org-todo-keywords'.
  8273. If the current word is preceded by a backslash, completes the TeX symbols
  8274. that are supported for HTML support.
  8275. If the current word is preceded by \"#+\", completes special words for
  8276. setting file options.
  8277. In the line after \"#+STARTUP:, complete valid keywords.\"
  8278. At all other locations, this simply calls the value of
  8279. `org-completion-fallback-command'."
  8280. (interactive "P")
  8281. (org-without-partial-completion
  8282. (catch 'exit
  8283. (let* ((a nil)
  8284. (end (point))
  8285. (beg1 (save-excursion
  8286. (skip-chars-backward (org-re "[:alnum:]_@"))
  8287. (point)))
  8288. (beg (save-excursion
  8289. (skip-chars-backward "a-zA-Z0-9_:$")
  8290. (point)))
  8291. (confirm (lambda (x) (stringp (car x))))
  8292. (searchhead (equal (char-before beg) ?*))
  8293. (struct
  8294. (when (and (member (char-before beg1) '(?. ?<))
  8295. (setq a (assoc (buffer-substring beg1 (point))
  8296. org-structure-template-alist)))
  8297. (org-complete-expand-structure-template (1- beg1) a)
  8298. (throw 'exit t)))
  8299. (tag (and (equal (char-before beg1) ?:)
  8300. (equal (char-after (point-at-bol)) ?*)))
  8301. (prop (and (equal (char-before beg1) ?:)
  8302. (not (equal (char-after (point-at-bol)) ?*))))
  8303. (texp (equal (char-before beg) ?\\))
  8304. (link (equal (char-before beg) ?\[))
  8305. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8306. beg)
  8307. "#+"))
  8308. (startup (string-match "^#\\+STARTUP:.*"
  8309. (buffer-substring (point-at-bol) (point))))
  8310. (completion-ignore-case opt)
  8311. (type nil)
  8312. (tbl nil)
  8313. (table (cond
  8314. (opt
  8315. (setq type :opt)
  8316. (require 'org-exp)
  8317. (append
  8318. (mapcar
  8319. (lambda (x)
  8320. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8321. (cons (match-string 2 x) (match-string 1 x)))
  8322. (org-split-string (org-get-current-options) "\n"))
  8323. (mapcar 'list org-additional-option-like-keywords)))
  8324. (startup
  8325. (setq type :startup)
  8326. org-startup-options)
  8327. (link (append org-link-abbrev-alist-local
  8328. org-link-abbrev-alist))
  8329. (texp
  8330. (setq type :tex)
  8331. org-html-entities)
  8332. ((string-match "\\`\\*+[ \t]+\\'"
  8333. (buffer-substring (point-at-bol) beg))
  8334. (setq type :todo)
  8335. (mapcar 'list org-todo-keywords-1))
  8336. (searchhead
  8337. (setq type :searchhead)
  8338. (save-excursion
  8339. (goto-char (point-min))
  8340. (while (re-search-forward org-todo-line-regexp nil t)
  8341. (push (list
  8342. (org-make-org-heading-search-string
  8343. (match-string 3) t))
  8344. tbl)))
  8345. tbl)
  8346. (tag (setq type :tag beg beg1)
  8347. (or org-tag-alist (org-get-buffer-tags)))
  8348. (prop (setq type :prop beg beg1)
  8349. (mapcar 'list (org-buffer-property-keys nil t t)))
  8350. (t (progn
  8351. (call-interactively org-completion-fallback-command)
  8352. (throw 'exit nil)))))
  8353. (pattern (buffer-substring-no-properties beg end))
  8354. (completion (try-completion pattern table confirm)))
  8355. (cond ((eq completion t)
  8356. (if (not (assoc (upcase pattern) table))
  8357. (message "Already complete")
  8358. (if (and (equal type :opt)
  8359. (not (member (car (assoc (upcase pattern) table))
  8360. org-additional-option-like-keywords)))
  8361. (insert (substring (cdr (assoc (upcase pattern) table))
  8362. (length pattern)))
  8363. (if (memq type '(:tag :prop)) (insert ":")))))
  8364. ((null completion)
  8365. (message "Can't find completion for \"%s\"" pattern)
  8366. (ding))
  8367. ((not (string= pattern completion))
  8368. (delete-region beg end)
  8369. (if (string-match " +$" completion)
  8370. (setq completion (replace-match "" t t completion)))
  8371. (insert completion)
  8372. (if (get-buffer-window "*Completions*")
  8373. (delete-window (get-buffer-window "*Completions*")))
  8374. (if (assoc completion table)
  8375. (if (eq type :todo) (insert " ")
  8376. (if (memq type '(:tag :prop)) (insert ":"))))
  8377. (if (and (equal type :opt) (assoc completion table))
  8378. (message "%s" (substitute-command-keys
  8379. "Press \\[org-complete] again to insert example settings"))))
  8380. (t
  8381. (message "Making completion list...")
  8382. (let ((list (sort (all-completions pattern table confirm)
  8383. 'string<)))
  8384. (with-output-to-temp-buffer "*Completions*"
  8385. (condition-case nil
  8386. ;; Protection needed for XEmacs and emacs 21
  8387. (display-completion-list list pattern)
  8388. (error (display-completion-list list)))))
  8389. (message "Making completion list...%s" "done")))))))
  8390. ;;;; TODO, DEADLINE, Comments
  8391. (defun org-toggle-comment ()
  8392. "Change the COMMENT state of an entry."
  8393. (interactive)
  8394. (save-excursion
  8395. (org-back-to-heading)
  8396. (let (case-fold-search)
  8397. (if (looking-at (concat outline-regexp
  8398. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8399. (replace-match "" t t nil 1)
  8400. (if (looking-at outline-regexp)
  8401. (progn
  8402. (goto-char (match-end 0))
  8403. (insert org-comment-string " ")))))))
  8404. (defvar org-last-todo-state-is-todo nil
  8405. "This is non-nil when the last TODO state change led to a TODO state.
  8406. If the last change removed the TODO tag or switched to DONE, then
  8407. this is nil.")
  8408. (defvar org-setting-tags nil) ; dynamically skipped
  8409. (defun org-parse-local-options (string var)
  8410. "Parse STRING for startup setting relevant for variable VAR."
  8411. (let ((rtn (symbol-value var))
  8412. e opts)
  8413. (save-match-data
  8414. (if (or (not string) (not (string-match "\\S-" string)))
  8415. rtn
  8416. (setq opts (delq nil (mapcar (lambda (x)
  8417. (setq e (assoc x org-startup-options))
  8418. (if (eq (nth 1 e) var) e nil))
  8419. (org-split-string string "[ \t]+"))))
  8420. (if (not opts)
  8421. rtn
  8422. (setq rtn nil)
  8423. (while (setq e (pop opts))
  8424. (if (not (nth 3 e))
  8425. (setq rtn (nth 2 e))
  8426. (if (not (listp rtn)) (setq rtn nil))
  8427. (push (nth 2 e) rtn)))
  8428. rtn)))))
  8429. (defvar org-todo-setup-filter-hook nil
  8430. "Hook for functions that pre-filter todo specs.
  8431. Each function takes a todo spec and returns either `nil' or the spec
  8432. transformed into canonical form." )
  8433. (defvar org-todo-get-default-hook nil
  8434. "Hook for functions that get a default item for todo.
  8435. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8436. `nil' or a string to be used for the todo mark." )
  8437. (defvar org-agenda-headline-snapshot-before-repeat)
  8438. (defun org-todo (&optional arg)
  8439. "Change the TODO state of an item.
  8440. The state of an item is given by a keyword at the start of the heading,
  8441. like
  8442. *** TODO Write paper
  8443. *** DONE Call mom
  8444. The different keywords are specified in the variable `org-todo-keywords'.
  8445. By default the available states are \"TODO\" and \"DONE\".
  8446. So for this example: when the item starts with TODO, it is changed to DONE.
  8447. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8448. DONE are present, add TODO at the beginning of the heading.
  8449. With C-u prefix arg, use completion to determine the new state.
  8450. With numeric prefix arg, switch to that state.
  8451. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8452. With a tripple C-u prefix, circumvent any state blocking.
  8453. For calling through lisp, arg is also interpreted in the following way:
  8454. 'none -> empty state
  8455. \"\"(empty string) -> switch to empty state
  8456. 'done -> switch to DONE
  8457. 'nextset -> switch to the next set of keywords
  8458. 'previousset -> switch to the previous set of keywords
  8459. \"WAITING\" -> switch to the specified keyword, but only if it
  8460. really is a member of `org-todo-keywords'."
  8461. (interactive "P")
  8462. (if (equal arg '(16)) (setq arg 'nextset))
  8463. (let ((org-blocker-hook org-blocker-hook)
  8464. (case-fold-search nil))
  8465. (when (equal arg '(64))
  8466. (setq arg nil org-blocker-hook nil))
  8467. (when (and org-blocker-hook
  8468. (org-entry-get nil "NOBLOCKING"))
  8469. (setq org-blocker-hook nil))
  8470. (save-excursion
  8471. (catch 'exit
  8472. (org-back-to-heading)
  8473. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8474. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8475. (looking-at " *"))
  8476. (let* ((match-data (match-data))
  8477. (startpos (point-at-bol))
  8478. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8479. (org-log-done org-log-done)
  8480. (org-log-repeat org-log-repeat)
  8481. (org-todo-log-states org-todo-log-states)
  8482. (this (match-string 1))
  8483. (hl-pos (match-beginning 0))
  8484. (head (org-get-todo-sequence-head this))
  8485. (ass (assoc head org-todo-kwd-alist))
  8486. (interpret (nth 1 ass))
  8487. (done-word (nth 3 ass))
  8488. (final-done-word (nth 4 ass))
  8489. (last-state (or this ""))
  8490. (completion-ignore-case t)
  8491. (member (member this org-todo-keywords-1))
  8492. (tail (cdr member))
  8493. (state (cond
  8494. ((and org-todo-key-trigger
  8495. (or (and (equal arg '(4))
  8496. (eq org-use-fast-todo-selection 'prefix))
  8497. (and (not arg) org-use-fast-todo-selection
  8498. (not (eq org-use-fast-todo-selection
  8499. 'prefix)))))
  8500. ;; Use fast selection
  8501. (org-fast-todo-selection))
  8502. ((and (equal arg '(4))
  8503. (or (not org-use-fast-todo-selection)
  8504. (not org-todo-key-trigger)))
  8505. ;; Read a state with completion
  8506. (org-ido-completing-read
  8507. "State: " (mapcar (lambda(x) (list x))
  8508. org-todo-keywords-1)
  8509. nil t))
  8510. ((eq arg 'right)
  8511. (if this
  8512. (if tail (car tail) nil)
  8513. (car org-todo-keywords-1)))
  8514. ((eq arg 'left)
  8515. (if (equal member org-todo-keywords-1)
  8516. nil
  8517. (if this
  8518. (nth (- (length org-todo-keywords-1)
  8519. (length tail) 2)
  8520. org-todo-keywords-1)
  8521. (org-last org-todo-keywords-1))))
  8522. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8523. (setq arg nil))) ; hack to fall back to cycling
  8524. (arg
  8525. ;; user or caller requests a specific state
  8526. (cond
  8527. ((equal arg "") nil)
  8528. ((eq arg 'none) nil)
  8529. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8530. ((eq arg 'nextset)
  8531. (or (car (cdr (member head org-todo-heads)))
  8532. (car org-todo-heads)))
  8533. ((eq arg 'previousset)
  8534. (let ((org-todo-heads (reverse org-todo-heads)))
  8535. (or (car (cdr (member head org-todo-heads)))
  8536. (car org-todo-heads))))
  8537. ((car (member arg org-todo-keywords-1)))
  8538. ((nth (1- (prefix-numeric-value arg))
  8539. org-todo-keywords-1))))
  8540. ((null member) (or head (car org-todo-keywords-1)))
  8541. ((equal this final-done-word) nil) ;; -> make empty
  8542. ((null tail) nil) ;; -> first entry
  8543. ((memq interpret '(type priority))
  8544. (if (eq this-command last-command)
  8545. (car tail)
  8546. (if (> (length tail) 0)
  8547. (or done-word (car org-done-keywords))
  8548. nil)))
  8549. (t
  8550. (car tail))))
  8551. (state (or
  8552. (run-hook-with-args-until-success
  8553. 'org-todo-get-default-hook state last-state)
  8554. state))
  8555. (next (if state (concat " " state " ") " "))
  8556. (change-plist (list :type 'todo-state-change :from this :to state
  8557. :position startpos))
  8558. dolog now-done-p)
  8559. (when org-blocker-hook
  8560. (setq org-last-todo-state-is-todo
  8561. (not (member this org-done-keywords)))
  8562. (unless (save-excursion
  8563. (save-match-data
  8564. (run-hook-with-args-until-failure
  8565. 'org-blocker-hook change-plist)))
  8566. (if (interactive-p)
  8567. (error "TODO state change from %s to %s blocked" this state)
  8568. ;; fail silently
  8569. (message "TODO state change from %s to %s blocked" this state)
  8570. (throw 'exit nil))))
  8571. (store-match-data match-data)
  8572. (replace-match next t t)
  8573. (unless (pos-visible-in-window-p hl-pos)
  8574. (message "TODO state changed to %s" (org-trim next)))
  8575. (unless head
  8576. (setq head (org-get-todo-sequence-head state)
  8577. ass (assoc head org-todo-kwd-alist)
  8578. interpret (nth 1 ass)
  8579. done-word (nth 3 ass)
  8580. final-done-word (nth 4 ass)))
  8581. (when (memq arg '(nextset previousset))
  8582. (message "Keyword-Set %d/%d: %s"
  8583. (- (length org-todo-sets) -1
  8584. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8585. (length org-todo-sets)
  8586. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8587. (setq org-last-todo-state-is-todo
  8588. (not (member state org-done-keywords)))
  8589. (setq now-done-p (and (member state org-done-keywords)
  8590. (not (member this org-done-keywords))))
  8591. (and logging (org-local-logging logging))
  8592. (when (and (or org-todo-log-states org-log-done)
  8593. (not org-inhibit-logging)
  8594. (not (memq arg '(nextset previousset))))
  8595. ;; we need to look at recording a time and note
  8596. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8597. (nth 2 (assoc this org-todo-log-states))))
  8598. (when (and state
  8599. (member state org-not-done-keywords)
  8600. (not (member this org-not-done-keywords)))
  8601. ;; This is now a todo state and was not one before
  8602. ;; If there was a CLOSED time stamp, get rid of it.
  8603. (org-add-planning-info nil nil 'closed))
  8604. (when (and now-done-p org-log-done)
  8605. ;; It is now done, and it was not done before
  8606. (org-add-planning-info 'closed (org-current-time))
  8607. (if (and (not dolog) (eq 'note org-log-done))
  8608. (org-add-log-setup 'done state this 'findpos 'note)))
  8609. (when (and state dolog)
  8610. ;; This is a non-nil state, and we need to log it
  8611. (org-add-log-setup 'state state this 'findpos dolog)))
  8612. ;; Fixup tag positioning
  8613. (org-todo-trigger-tag-changes state)
  8614. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8615. (when org-provide-todo-statistics
  8616. (org-update-parent-todo-statistics))
  8617. (run-hooks 'org-after-todo-state-change-hook)
  8618. (if (and arg (not (member state org-done-keywords)))
  8619. (setq head (org-get-todo-sequence-head state)))
  8620. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8621. ;; Do we need to trigger a repeat?
  8622. (when now-done-p
  8623. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8624. ;; This is for the agenda, take a snapshot of the headline.
  8625. (save-match-data
  8626. (setq org-agenda-headline-snapshot-before-repeat
  8627. (org-get-heading))))
  8628. (org-auto-repeat-maybe state))
  8629. ;; Fixup cursor location if close to the keyword
  8630. (if (and (outline-on-heading-p)
  8631. (not (bolp))
  8632. (save-excursion (beginning-of-line 1)
  8633. (looking-at org-todo-line-regexp))
  8634. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8635. (progn
  8636. (goto-char (or (match-end 2) (match-end 1)))
  8637. (and (looking-at " ") (just-one-space))))
  8638. (when org-trigger-hook
  8639. (save-excursion
  8640. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8641. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8642. "Block turning an entry into a TODO, using the hierarchy.
  8643. This checks whether the current task should be blocked from state
  8644. changes. Such blocking occurs when:
  8645. 1. The task has children which are not all in a completed state.
  8646. 2. A task has a parent with the property :ORDERED:, and there
  8647. are siblings prior to the current task with incomplete
  8648. status.
  8649. 3. The parent of the task is blocked because it has siblings that should
  8650. be done first, or is child of a block grandparent TODO entry."
  8651. (catch 'dont-block
  8652. ;; If this is not a todo state change, or if this entry is already DONE,
  8653. ;; do not block
  8654. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8655. (member (plist-get change-plist :from)
  8656. (cons 'done org-done-keywords))
  8657. (member (plist-get change-plist :to)
  8658. (cons 'todo org-not-done-keywords)))
  8659. (throw 'dont-block t))
  8660. ;; If this task has children, and any are undone, it's blocked
  8661. (save-excursion
  8662. (org-back-to-heading t)
  8663. (let ((this-level (funcall outline-level)))
  8664. (outline-next-heading)
  8665. (let ((child-level (funcall outline-level)))
  8666. (while (and (not (eobp))
  8667. (> child-level this-level))
  8668. ;; this todo has children, check whether they are all
  8669. ;; completed
  8670. (if (and (not (org-entry-is-done-p))
  8671. (org-entry-is-todo-p))
  8672. (throw 'dont-block nil))
  8673. (outline-next-heading)
  8674. (setq child-level (funcall outline-level))))))
  8675. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8676. ;; any previous siblings are undone, it's blocked
  8677. (save-excursion
  8678. (org-back-to-heading t)
  8679. (let* ((pos (point))
  8680. (parent-pos (and (org-up-heading-safe) (point))))
  8681. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8682. (when (and (org-entry-get (point) "ORDERED")
  8683. (forward-line 1)
  8684. (re-search-forward org-not-done-heading-regexp pos t))
  8685. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8686. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8687. (while t
  8688. (goto-char parent-pos)
  8689. (if (not (looking-at org-not-done-heading-regexp))
  8690. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8691. (setq pos (point))
  8692. (setq parent-pos (and (org-up-heading-safe) (point)))
  8693. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8694. (when (and (org-entry-get (point) "ORDERED")
  8695. (forward-line 1)
  8696. (re-search-forward org-not-done-heading-regexp pos t))
  8697. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8698. (defcustom org-track-ordered-property-with-tag nil
  8699. "Should the ORDERED property also be shown as a tag?
  8700. The ORDERED property decides if an entry should require subtasks to be
  8701. completed in sequence. Since a property is not very visible, setting
  8702. this option means that toggling the ORDERED property with the command
  8703. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8704. not relevant for the behavior, but it makes things more visible.
  8705. Note that toggling the tag with tags commands will not change the property
  8706. and therefore not influence behavior!
  8707. This can be t, meaning the tag ORDERED should be used, It can also be a
  8708. string to select a different tag for this task."
  8709. :group 'org-todo
  8710. :type '(choice
  8711. (const :tag "No tracking" nil)
  8712. (const :tag "Track with ORDERED tag" t)
  8713. (string :tag "Use other tag")))
  8714. (defun org-toggle-ordered-property ()
  8715. "Toggle the ORDERED property of the current entry.
  8716. For better visibility, you can track the value of this property with a tag.
  8717. See variable `org-track-ordered-property-with-tag'."
  8718. (interactive)
  8719. (let* ((t1 org-track-ordered-property-with-tag)
  8720. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8721. (save-excursion
  8722. (org-back-to-heading)
  8723. (if (org-entry-get nil "ORDERED")
  8724. (progn
  8725. (org-delete-property "ORDERED")
  8726. (and tag (org-toggle-tag tag 'off))
  8727. (message "Subtasks can be completed in arbitrary order"))
  8728. (org-entry-put nil "ORDERED" "t")
  8729. (and tag (org-toggle-tag tag 'on))
  8730. (message "Subtasks must be completed in sequence")))))
  8731. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8732. (defun org-block-todo-from-checkboxes (change-plist)
  8733. "Block turning an entry into a TODO, using checkboxes.
  8734. This checks whether the current task should be blocked from state
  8735. changes because there are uncheckd boxes in this entry."
  8736. (catch 'dont-block
  8737. ;; If this is not a todo state change, or if this entry is already DONE,
  8738. ;; do not block
  8739. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8740. (member (plist-get change-plist :from)
  8741. (cons 'done org-done-keywords))
  8742. (member (plist-get change-plist :to)
  8743. (cons 'todo org-not-done-keywords)))
  8744. (throw 'dont-block t))
  8745. ;; If this task has checkboxes that are not checked, it's blocked
  8746. (save-excursion
  8747. (org-back-to-heading t)
  8748. (let ((beg (point)) end)
  8749. (outline-next-heading)
  8750. (setq end (point))
  8751. (goto-char beg)
  8752. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8753. end t)
  8754. (progn
  8755. (if (boundp 'org-blocked-by-checkboxes)
  8756. (setq org-blocked-by-checkboxes t))
  8757. (throw 'dont-block nil)))))
  8758. t)) ; do not block
  8759. (defvar org-entry-property-inherited-from) ;; defined below
  8760. (defun org-update-parent-todo-statistics ()
  8761. "Update any statistics cookie in the parent of the current headline.
  8762. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8763. the entire subtree and this will travel up the hierarchy and update
  8764. statistics everywhere."
  8765. (interactive)
  8766. (let* ((lim 0) prop
  8767. (recursive (or (not org-hierarchical-todo-statistics)
  8768. (string-match
  8769. "\\<recursive\\>"
  8770. (or (setq prop (org-entry-get
  8771. nil "COOKIE_DATA" 'inherit)) ""))))
  8772. (lim (or (and prop (marker-position
  8773. org-entry-property-inherited-from))
  8774. lim))
  8775. (first t)
  8776. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8777. level ltoggle l1
  8778. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8779. (catch 'exit
  8780. (save-excursion
  8781. (beginning-of-line 1)
  8782. (if (org-at-heading-p)
  8783. (setq ltoggle (funcall outline-level))
  8784. (error "This should not happen"))
  8785. (while (and (setq level (org-up-heading-safe))
  8786. (or recursive first)
  8787. (>= (point) lim))
  8788. (setq first nil)
  8789. (unless (and level
  8790. (not (string-match
  8791. "\\<checkbox\\>"
  8792. (downcase
  8793. (or (org-entry-get
  8794. nil "COOKIE_DATA")
  8795. "")))))
  8796. (throw 'exit nil))
  8797. (while (re-search-forward box-re (point-at-eol) t)
  8798. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8799. (setq is-percent (match-end 2))
  8800. (save-match-data
  8801. (unless (outline-next-heading) (throw 'exit nil))
  8802. (while (and (looking-at org-complex-heading-regexp)
  8803. (> (setq l1 (length (match-string 1))) level))
  8804. (setq kwd (and (or recursive (= l1 ltoggle))
  8805. (match-string 2)))
  8806. (and kwd (setq cnt-all (1+ cnt-all)))
  8807. (and (member kwd org-done-keywords)
  8808. (setq cnt-done (1+ cnt-done)))
  8809. (outline-next-heading)))
  8810. (replace-match
  8811. (if is-percent
  8812. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8813. (format "[%d/%d]" cnt-done cnt-all)))))
  8814. (when cookie-present
  8815. (run-hook-with-args 'org-after-todo-statistics-hook
  8816. cnt-done (- cnt-all cnt-done)))))
  8817. (run-hooks 'org-todo-statistics-hook)))
  8818. (defvar org-after-todo-statistics-hook nil
  8819. "Hook that is called after a TODO statistics cookie has been updated.
  8820. Each function is called with two arguments: the number of not-done entries
  8821. and the number of done entries.
  8822. For example, the following function, when added to this hook, will switch
  8823. an entry to DONE when all children are done, and back to TODO when new
  8824. entries are set to a TODO status. Note that this hook is only called
  8825. when there is a statistics cookie in the headline!
  8826. (defun org-summary-todo (n-done n-not-done)
  8827. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8828. (let (org-log-done org-log-states) ; turn off logging
  8829. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8830. ")
  8831. (defvar org-todo-statistics-hook nil
  8832. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8833. This hook runs even if there is no statisics cookie present, in which case
  8834. `org-after-todo-statistics-hook' would not run.")
  8835. (defun org-todo-trigger-tag-changes (state)
  8836. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8837. (let ((l org-todo-state-tags-triggers)
  8838. changes)
  8839. (when (or (not state) (equal state ""))
  8840. (setq changes (append changes (cdr (assoc "" l)))))
  8841. (when (and (stringp state) (> (length state) 0))
  8842. (setq changes (append changes (cdr (assoc state l)))))
  8843. (when (member state org-not-done-keywords)
  8844. (setq changes (append changes (cdr (assoc 'todo l)))))
  8845. (when (member state org-done-keywords)
  8846. (setq changes (append changes (cdr (assoc 'done l)))))
  8847. (dolist (c changes)
  8848. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8849. (defun org-local-logging (value)
  8850. "Get logging settings from a property VALUE."
  8851. (let* (words w a)
  8852. ;; directly set the variables, they are already local.
  8853. (setq org-log-done nil
  8854. org-log-repeat nil
  8855. org-todo-log-states nil)
  8856. (setq words (org-split-string value))
  8857. (while (setq w (pop words))
  8858. (cond
  8859. ((setq a (assoc w org-startup-options))
  8860. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8861. (set (nth 1 a) (nth 2 a))))
  8862. ((setq a (org-extract-log-state-settings w))
  8863. (and (member (car a) org-todo-keywords-1)
  8864. (push a org-todo-log-states)))))))
  8865. (defun org-get-todo-sequence-head (kwd)
  8866. "Return the head of the TODO sequence to which KWD belongs.
  8867. If KWD is not set, check if there is a text property remembering the
  8868. right sequence."
  8869. (let (p)
  8870. (cond
  8871. ((not kwd)
  8872. (or (get-text-property (point-at-bol) 'org-todo-head)
  8873. (progn
  8874. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8875. nil (point-at-eol)))
  8876. (get-text-property p 'org-todo-head))))
  8877. ((not (member kwd org-todo-keywords-1))
  8878. (car org-todo-keywords-1))
  8879. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8880. (defun org-fast-todo-selection ()
  8881. "Fast TODO keyword selection with single keys.
  8882. Returns the new TODO keyword, or nil if no state change should occur."
  8883. (let* ((fulltable org-todo-key-alist)
  8884. (done-keywords org-done-keywords) ;; needed for the faces.
  8885. (maxlen (apply 'max (mapcar
  8886. (lambda (x)
  8887. (if (stringp (car x)) (string-width (car x)) 0))
  8888. fulltable)))
  8889. (expert nil)
  8890. (fwidth (+ maxlen 3 1 3))
  8891. (ncol (/ (- (window-width) 4) fwidth))
  8892. tg cnt e c tbl
  8893. groups ingroup)
  8894. (save-excursion
  8895. (save-window-excursion
  8896. (if expert
  8897. (set-buffer (get-buffer-create " *Org todo*"))
  8898. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8899. (erase-buffer)
  8900. (org-set-local 'org-done-keywords done-keywords)
  8901. (setq tbl fulltable cnt 0)
  8902. (while (setq e (pop tbl))
  8903. (cond
  8904. ((equal e '(:startgroup))
  8905. (push '() groups) (setq ingroup t)
  8906. (when (not (= cnt 0))
  8907. (setq cnt 0)
  8908. (insert "\n"))
  8909. (insert "{ "))
  8910. ((equal e '(:endgroup))
  8911. (setq ingroup nil cnt 0)
  8912. (insert "}\n"))
  8913. ((equal e '(:newline))
  8914. (when (not (= cnt 0))
  8915. (setq cnt 0)
  8916. (insert "\n")
  8917. (setq e (car tbl))
  8918. (while (equal (car tbl) '(:newline))
  8919. (insert "\n")
  8920. (setq tbl (cdr tbl)))))
  8921. (t
  8922. (setq tg (car e) c (cdr e))
  8923. (if ingroup (push tg (car groups)))
  8924. (setq tg (org-add-props tg nil 'face
  8925. (org-get-todo-face tg)))
  8926. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8927. (insert "[" c "] " tg (make-string
  8928. (- fwidth 4 (length tg)) ?\ ))
  8929. (when (= (setq cnt (1+ cnt)) ncol)
  8930. (insert "\n")
  8931. (if ingroup (insert " "))
  8932. (setq cnt 0)))))
  8933. (insert "\n")
  8934. (goto-char (point-min))
  8935. (if (not expert) (org-fit-window-to-buffer))
  8936. (message "[a-z..]:Set [SPC]:clear")
  8937. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8938. (cond
  8939. ((or (= c ?\C-g)
  8940. (and (= c ?q) (not (rassoc c fulltable))))
  8941. (setq quit-flag t))
  8942. ((= c ?\ ) nil)
  8943. ((setq e (rassoc c fulltable) tg (car e))
  8944. tg)
  8945. (t (setq quit-flag t)))))))
  8946. (defun org-entry-is-todo-p ()
  8947. (member (org-get-todo-state) org-not-done-keywords))
  8948. (defun org-entry-is-done-p ()
  8949. (member (org-get-todo-state) org-done-keywords))
  8950. (defun org-get-todo-state ()
  8951. (save-excursion
  8952. (org-back-to-heading t)
  8953. (and (looking-at org-todo-line-regexp)
  8954. (match-end 2)
  8955. (match-string 2))))
  8956. (defun org-at-date-range-p (&optional inactive-ok)
  8957. "Is the cursor inside a date range?"
  8958. (interactive)
  8959. (save-excursion
  8960. (catch 'exit
  8961. (let ((pos (point)))
  8962. (skip-chars-backward "^[<\r\n")
  8963. (skip-chars-backward "<[")
  8964. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8965. (>= (match-end 0) pos)
  8966. (throw 'exit t))
  8967. (skip-chars-backward "^<[\r\n")
  8968. (skip-chars-backward "<[")
  8969. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8970. (>= (match-end 0) pos)
  8971. (throw 'exit t)))
  8972. nil)))
  8973. (defun org-get-repeat ()
  8974. "Check if there is a deadline/schedule with repeater in this entry."
  8975. (save-match-data
  8976. (save-excursion
  8977. (org-back-to-heading t)
  8978. (if (re-search-forward
  8979. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8980. (match-string 1)))))
  8981. (defvar org-last-changed-timestamp)
  8982. (defvar org-last-inserted-timestamp)
  8983. (defvar org-log-post-message)
  8984. (defvar org-log-note-purpose)
  8985. (defvar org-log-note-how)
  8986. (defvar org-log-note-extra)
  8987. (defun org-auto-repeat-maybe (done-word)
  8988. "Check if the current headline contains a repeated deadline/schedule.
  8989. If yes, set TODO state back to what it was and change the base date
  8990. of repeating deadline/scheduled time stamps to new date.
  8991. This function is run automatically after each state change to a DONE state."
  8992. ;; last-state is dynamically scoped into this function
  8993. (let* ((repeat (org-get-repeat))
  8994. (aa (assoc last-state org-todo-kwd-alist))
  8995. (interpret (nth 1 aa))
  8996. (head (nth 2 aa))
  8997. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8998. (msg "Entry repeats: ")
  8999. (org-log-done nil)
  9000. (org-todo-log-states nil)
  9001. (nshiftmax 10) (nshift 0)
  9002. re type n what ts time)
  9003. (when repeat
  9004. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9005. (org-todo (if (eq interpret 'type) last-state head))
  9006. (when org-log-repeat
  9007. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9008. (memq 'org-add-log-note post-command-hook))
  9009. ;; OK, we are already setup for some record
  9010. (if (eq org-log-repeat 'note)
  9011. ;; make sure we take a note, not only a time stamp
  9012. (setq org-log-note-how 'note))
  9013. ;; Set up for taking a record
  9014. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9015. last-state
  9016. 'findpos org-log-repeat)))
  9017. (org-back-to-heading t)
  9018. (org-add-planning-info nil nil 'closed)
  9019. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9020. org-deadline-time-regexp "\\)\\|\\("
  9021. org-ts-regexp "\\)"))
  9022. (while (re-search-forward
  9023. re (save-excursion (outline-next-heading) (point)) t)
  9024. (setq type (if (match-end 1) org-scheduled-string
  9025. (if (match-end 3) org-deadline-string "Plain:"))
  9026. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9027. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9028. (setq n (string-to-number (match-string 2 ts))
  9029. what (match-string 3 ts))
  9030. (if (equal what "w") (setq n (* n 7) what "d"))
  9031. ;; Preparation, see if we need to modify the start date for the change
  9032. (when (match-end 1)
  9033. (setq time (save-match-data (org-time-string-to-time ts)))
  9034. (cond
  9035. ((equal (match-string 1 ts) ".")
  9036. ;; Shift starting date to today
  9037. (org-timestamp-change
  9038. (- (time-to-days (current-time)) (time-to-days time))
  9039. 'day))
  9040. ((equal (match-string 1 ts) "+")
  9041. (while (or (= nshift 0)
  9042. (<= (time-to-days time) (time-to-days (current-time))))
  9043. (when (= (incf nshift) nshiftmax)
  9044. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9045. (error "Abort")))
  9046. (org-timestamp-change n (cdr (assoc what whata)))
  9047. (org-at-timestamp-p t)
  9048. (setq ts (match-string 1))
  9049. (setq time (save-match-data (org-time-string-to-time ts))))
  9050. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9051. ;; rematch, so that we have everything in place for the real shift
  9052. (org-at-timestamp-p t)
  9053. (setq ts (match-string 1))
  9054. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9055. (org-timestamp-change n (cdr (assoc what whata)))
  9056. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9057. (setq org-log-post-message msg)
  9058. (message "%s" msg))))
  9059. (defun org-show-todo-tree (arg)
  9060. "Make a compact tree which shows all headlines marked with TODO.
  9061. The tree will show the lines where the regexp matches, and all higher
  9062. headlines above the match.
  9063. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9064. With a numeric prefix N, construct a sparse tree for the Nth element
  9065. of `org-todo-keywords-1'."
  9066. (interactive "P")
  9067. (let ((case-fold-search nil)
  9068. (kwd-re
  9069. (cond ((null arg) org-not-done-regexp)
  9070. ((equal arg '(4))
  9071. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  9072. (mapcar 'list org-todo-keywords-1))))
  9073. (concat "\\("
  9074. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9075. "\\)\\>")))
  9076. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9077. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9078. org-todo-keywords-1)))
  9079. (t (error "Invalid prefix argument: %s" arg)))))
  9080. (message "%d TODO entries found"
  9081. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9082. (defun org-deadline (&optional remove time)
  9083. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9084. With argument REMOVE, remove any deadline from the item.
  9085. When TIME is set, it should be an internal time specification, and the
  9086. scheduling will use the corresponding date."
  9087. (interactive "P")
  9088. (if remove
  9089. (progn
  9090. (org-remove-timestamp-with-keyword org-deadline-string)
  9091. (message "Item no longer has a deadline."))
  9092. (if (org-get-repeat)
  9093. (error "Cannot change deadline on task with repeater, please do that by hand")
  9094. (org-add-planning-info 'deadline time 'closed)
  9095. (message "Deadline on %s" org-last-inserted-timestamp))))
  9096. (defun org-schedule (&optional remove time)
  9097. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9098. With argument REMOVE, remove any scheduling date from the item.
  9099. When TIME is set, it should be an internal time specification, and the
  9100. scheduling will use the corresponding date."
  9101. (interactive "P")
  9102. (if remove
  9103. (progn
  9104. (org-remove-timestamp-with-keyword org-scheduled-string)
  9105. (message "Item is no longer scheduled."))
  9106. (if (org-get-repeat)
  9107. (error "Cannot reschedule task with repeater, please do that by hand")
  9108. (org-add-planning-info 'scheduled time 'closed)
  9109. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9110. (defun org-get-scheduled-time (pom &optional inherit)
  9111. "Get the scheduled time as a time tuple, of a format suitable
  9112. for calling org-schedule with, or if there is no scheduling,
  9113. returns nil."
  9114. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9115. (when time
  9116. (apply 'encode-time (org-parse-time-string time)))))
  9117. (defun org-get-deadline-time (pom &optional inherit)
  9118. "Get the deadine as a time tuple, of a format suitable for
  9119. calling org-deadlin with, or if there is no scheduling, returns
  9120. nil."
  9121. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9122. (when time
  9123. (apply 'encode-time (org-parse-time-string time)))))
  9124. (defun org-remove-timestamp-with-keyword (keyword)
  9125. "Remove all time stamps with KEYWORD in the current entry."
  9126. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9127. beg)
  9128. (save-excursion
  9129. (org-back-to-heading t)
  9130. (setq beg (point))
  9131. (org-end-of-subtree t t)
  9132. (while (re-search-backward re beg t)
  9133. (replace-match "")
  9134. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9135. (equal (char-before) ?\ ))
  9136. (backward-delete-char 1)
  9137. (if (string-match "^[ \t]*$" (buffer-substring
  9138. (point-at-bol) (point-at-eol)))
  9139. (delete-region (point-at-bol)
  9140. (min (point-max) (1+ (point-at-eol))))))))))
  9141. (defun org-add-planning-info (what &optional time &rest remove)
  9142. "Insert new timestamp with keyword in the line directly after the headline.
  9143. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9144. If non is given, the user is prompted for a date.
  9145. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9146. be removed."
  9147. (interactive)
  9148. (let (org-time-was-given org-end-time-was-given ts
  9149. end default-time default-input)
  9150. (catch 'exit
  9151. (when (and (not time) (memq what '(scheduled deadline)))
  9152. ;; Try to get a default date/time from existing timestamp
  9153. (save-excursion
  9154. (org-back-to-heading t)
  9155. (setq end (save-excursion (outline-next-heading) (point)))
  9156. (when (re-search-forward (if (eq what 'scheduled)
  9157. org-scheduled-time-regexp
  9158. org-deadline-time-regexp)
  9159. end t)
  9160. (setq ts (match-string 1)
  9161. default-time
  9162. (apply 'encode-time (org-parse-time-string ts))
  9163. default-input (and ts (org-get-compact-tod ts))))))
  9164. (when what
  9165. ;; If necessary, get the time from the user
  9166. (setq time (or time (org-read-date nil 'to-time nil nil
  9167. default-time default-input))))
  9168. (when (and org-insert-labeled-timestamps-at-point
  9169. (member what '(scheduled deadline)))
  9170. (insert
  9171. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9172. (org-insert-time-stamp time org-time-was-given
  9173. nil nil nil (list org-end-time-was-given))
  9174. (setq what nil))
  9175. (save-excursion
  9176. (save-restriction
  9177. (let (col list elt ts buffer-invisibility-spec)
  9178. (org-back-to-heading t)
  9179. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9180. (goto-char (match-end 1))
  9181. (setq col (current-column))
  9182. (goto-char (match-end 0))
  9183. (if (eobp) (insert "\n") (forward-char 1))
  9184. (when (and (not what)
  9185. (not (looking-at
  9186. (concat "[ \t]*"
  9187. org-keyword-time-not-clock-regexp))))
  9188. ;; Nothing to add, nothing to remove...... :-)
  9189. (throw 'exit nil))
  9190. (if (and (not (looking-at outline-regexp))
  9191. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9192. "[^\r\n]*"))
  9193. (not (equal (match-string 1) org-clock-string)))
  9194. (narrow-to-region (match-beginning 0) (match-end 0))
  9195. (insert-before-markers "\n")
  9196. (backward-char 1)
  9197. (narrow-to-region (point) (point))
  9198. (and org-adapt-indentation (org-indent-to-column col)))
  9199. ;; Check if we have to remove something.
  9200. (setq list (cons what remove))
  9201. (while list
  9202. (setq elt (pop list))
  9203. (goto-char (point-min))
  9204. (when (or (and (eq elt 'scheduled)
  9205. (re-search-forward org-scheduled-time-regexp nil t))
  9206. (and (eq elt 'deadline)
  9207. (re-search-forward org-deadline-time-regexp nil t))
  9208. (and (eq elt 'closed)
  9209. (re-search-forward org-closed-time-regexp nil t)))
  9210. (replace-match "")
  9211. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9212. (if (looking-at " +") (replace-match ""))))
  9213. (goto-char (point-max))
  9214. (when what
  9215. (insert
  9216. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9217. (cond ((eq what 'scheduled) org-scheduled-string)
  9218. ((eq what 'deadline) org-deadline-string)
  9219. ((eq what 'closed) org-closed-string))
  9220. " ")
  9221. (setq ts (org-insert-time-stamp
  9222. time
  9223. (or org-time-was-given
  9224. (and (eq what 'closed) org-log-done-with-time))
  9225. (eq what 'closed)
  9226. nil nil (list org-end-time-was-given)))
  9227. (end-of-line 1))
  9228. (goto-char (point-min))
  9229. (widen)
  9230. (if (and (looking-at "[ \t]+\n")
  9231. (equal (char-before) ?\n))
  9232. (delete-region (1- (point)) (point-at-eol)))
  9233. ts))))))
  9234. (defvar org-log-note-marker (make-marker))
  9235. (defvar org-log-note-purpose nil)
  9236. (defvar org-log-note-state nil)
  9237. (defvar org-log-note-previous-state nil)
  9238. (defvar org-log-note-how nil)
  9239. (defvar org-log-note-extra nil)
  9240. (defvar org-log-note-window-configuration nil)
  9241. (defvar org-log-note-return-to (make-marker))
  9242. (defvar org-log-post-message nil
  9243. "Message to be displayed after a log note has been stored.
  9244. The auto-repeater uses this.")
  9245. (defun org-add-note ()
  9246. "Add a note to the current entry.
  9247. This is done in the same way as adding a state change note."
  9248. (interactive)
  9249. (org-add-log-setup 'note nil nil 'findpos nil))
  9250. (defvar org-property-end-re)
  9251. (defun org-add-log-setup (&optional purpose state prev-state
  9252. findpos how &optional extra)
  9253. "Set up the post command hook to take a note.
  9254. If this is about to TODO state change, the new state is expected in STATE.
  9255. When FINDPOS is non-nil, find the correct position for the note in
  9256. the current entry. If not, assume that it can be inserted at point.
  9257. HOW is an indicator what kind of note should be created.
  9258. EXTRA is additional text that will be inserted into the notes buffer."
  9259. (let* ((org-log-into-drawer (org-log-into-drawer))
  9260. (drawer (cond ((stringp org-log-into-drawer)
  9261. org-log-into-drawer)
  9262. (org-log-into-drawer "LOGBOOK")
  9263. (t nil))))
  9264. (save-restriction
  9265. (save-excursion
  9266. (when findpos
  9267. (org-back-to-heading t)
  9268. (narrow-to-region (point) (save-excursion
  9269. (outline-next-heading) (point)))
  9270. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9271. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9272. "[^\r\n]*\\)?"))
  9273. (goto-char (match-end 0))
  9274. (cond
  9275. (drawer
  9276. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9277. nil t)
  9278. (progn
  9279. (goto-char (match-end 0))
  9280. (or org-log-states-order-reversed
  9281. (and (re-search-forward org-property-end-re nil t)
  9282. (goto-char (1- (match-beginning 0))))))
  9283. (insert "\n:" drawer ":\n:END:")
  9284. (beginning-of-line 0)
  9285. (org-indent-line-function)
  9286. (beginning-of-line 2)
  9287. (org-indent-line-function)
  9288. (end-of-line 0)))
  9289. ((and org-log-state-notes-insert-after-drawers
  9290. (save-excursion
  9291. (forward-line) (looking-at org-drawer-regexp)))
  9292. (forward-line)
  9293. (while (looking-at org-drawer-regexp)
  9294. (goto-char (match-end 0))
  9295. (re-search-forward org-property-end-re (point-max) t)
  9296. (forward-line))
  9297. (forward-line -1)))
  9298. (unless org-log-states-order-reversed
  9299. (and (= (char-after) ?\n) (forward-char 1))
  9300. (org-skip-over-state-notes)
  9301. (skip-chars-backward " \t\n\r")))
  9302. (move-marker org-log-note-marker (point))
  9303. (setq org-log-note-purpose purpose
  9304. org-log-note-state state
  9305. org-log-note-previous-state prev-state
  9306. org-log-note-how how
  9307. org-log-note-extra extra)
  9308. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9309. (defun org-skip-over-state-notes ()
  9310. "Skip past the list of State notes in an entry."
  9311. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9312. (while (looking-at "[ \t]*- State")
  9313. (condition-case nil
  9314. (org-next-item)
  9315. (error (org-end-of-item)))))
  9316. (defun org-add-log-note (&optional purpose)
  9317. "Pop up a window for taking a note, and add this note later at point."
  9318. (remove-hook 'post-command-hook 'org-add-log-note)
  9319. (setq org-log-note-window-configuration (current-window-configuration))
  9320. (delete-other-windows)
  9321. (move-marker org-log-note-return-to (point))
  9322. (switch-to-buffer (marker-buffer org-log-note-marker))
  9323. (goto-char org-log-note-marker)
  9324. (org-switch-to-buffer-other-window "*Org Note*")
  9325. (erase-buffer)
  9326. (if (memq org-log-note-how '(time state))
  9327. (let (current-prefix-arg) (org-store-log-note))
  9328. (let ((org-inhibit-startup t)) (org-mode))
  9329. (insert (format "# Insert note for %s.
  9330. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9331. (cond
  9332. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9333. ((eq org-log-note-purpose 'done) "closed todo item")
  9334. ((eq org-log-note-purpose 'state)
  9335. (format "state change from \"%s\" to \"%s\""
  9336. (or org-log-note-previous-state "")
  9337. (or org-log-note-state "")))
  9338. ((eq org-log-note-purpose 'note)
  9339. "this entry")
  9340. (t (error "This should not happen")))))
  9341. (if org-log-note-extra (insert org-log-note-extra))
  9342. (org-set-local 'org-finish-function 'org-store-log-note)))
  9343. (defvar org-note-abort nil) ; dynamically scoped
  9344. (defun org-store-log-note ()
  9345. "Finish taking a log note, and insert it to where it belongs."
  9346. (let ((txt (buffer-string))
  9347. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9348. lines ind)
  9349. (kill-buffer (current-buffer))
  9350. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9351. (setq txt (replace-match "" t t txt)))
  9352. (if (string-match "\\s-+\\'" txt)
  9353. (setq txt (replace-match "" t t txt)))
  9354. (setq lines (org-split-string txt "\n"))
  9355. (when (and note (string-match "\\S-" note))
  9356. (setq note
  9357. (org-replace-escapes
  9358. note
  9359. (list (cons "%u" (user-login-name))
  9360. (cons "%U" user-full-name)
  9361. (cons "%t" (format-time-string
  9362. (org-time-stamp-format 'long 'inactive)
  9363. (current-time)))
  9364. (cons "%s" (if org-log-note-state
  9365. (concat "\"" org-log-note-state "\"")
  9366. ""))
  9367. (cons "%S" (if org-log-note-previous-state
  9368. (concat "\"" org-log-note-previous-state "\"")
  9369. "\"\"")))))
  9370. (if lines (setq note (concat note " \\\\")))
  9371. (push note lines))
  9372. (when (or current-prefix-arg org-note-abort)
  9373. (when org-log-into-drawer
  9374. (org-remove-empty-drawer-at
  9375. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9376. org-log-note-marker))
  9377. (setq lines nil))
  9378. (when lines
  9379. (save-excursion
  9380. (set-buffer (marker-buffer org-log-note-marker))
  9381. (save-excursion
  9382. (goto-char org-log-note-marker)
  9383. (move-marker org-log-note-marker nil)
  9384. (end-of-line 1)
  9385. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9386. (insert "- " (pop lines))
  9387. (org-indent-line-function)
  9388. (beginning-of-line 1)
  9389. (looking-at "[ \t]*")
  9390. (setq ind (concat (match-string 0) " "))
  9391. (end-of-line 1)
  9392. (while lines (insert "\n" ind (pop lines)))
  9393. (message "Note stored")
  9394. (org-back-to-heading t)
  9395. (org-cycle-hide-drawers 'children)))))
  9396. (set-window-configuration org-log-note-window-configuration)
  9397. (with-current-buffer (marker-buffer org-log-note-return-to)
  9398. (goto-char org-log-note-return-to))
  9399. (move-marker org-log-note-return-to nil)
  9400. (and org-log-post-message (message "%s" org-log-post-message)))
  9401. (defun org-remove-empty-drawer-at (drawer pos)
  9402. "Remove an emptyr DARWER drawer at position POS.
  9403. POS may also be a marker."
  9404. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9405. (save-excursion
  9406. (save-restriction
  9407. (widen)
  9408. (goto-char pos)
  9409. (if (org-in-regexp
  9410. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9411. (replace-match ""))))))
  9412. (defun org-sparse-tree (&optional arg)
  9413. "Create a sparse tree, prompt for the details.
  9414. This command can create sparse trees. You first need to select the type
  9415. of match used to create the tree:
  9416. t Show entries with a specific TODO keyword.
  9417. m Show entries selected by a tags/property match.
  9418. p Enter a property name and its value (both with completion on existing
  9419. names/values) and show entries with that property.
  9420. r Show entries matching a regular expression.
  9421. d Show deadlines due within `org-deadline-warning-days'.
  9422. b Show deadlines and scheduled items before a date.
  9423. a Show deadlines and scheduled items after a date."
  9424. (interactive "P")
  9425. (let (ans kwd value)
  9426. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9427. (setq ans (read-char-exclusive))
  9428. (cond
  9429. ((equal ans ?d)
  9430. (call-interactively 'org-check-deadlines))
  9431. ((equal ans ?b)
  9432. (call-interactively 'org-check-before-date))
  9433. ((equal ans ?a)
  9434. (call-interactively 'org-check-after-date))
  9435. ((equal ans ?t)
  9436. (org-show-todo-tree '(4)))
  9437. ((member ans '(?T ?m))
  9438. (call-interactively 'org-match-sparse-tree))
  9439. ((member ans '(?p ?P))
  9440. (setq kwd (org-ido-completing-read "Property: "
  9441. (mapcar 'list (org-buffer-property-keys))))
  9442. (setq value (org-ido-completing-read "Value: "
  9443. (mapcar 'list (org-property-values kwd))))
  9444. (unless (string-match "\\`{.*}\\'" value)
  9445. (setq value (concat "\"" value "\"")))
  9446. (org-match-sparse-tree arg (concat kwd "=" value)))
  9447. ((member ans '(?r ?R ?/))
  9448. (call-interactively 'org-occur))
  9449. (t (error "No such sparse tree command \"%c\"" ans)))))
  9450. (defvar org-occur-highlights nil
  9451. "List of overlays used for occur matches.")
  9452. (make-variable-buffer-local 'org-occur-highlights)
  9453. (defvar org-occur-parameters nil
  9454. "Parameters of the active org-occur calls.
  9455. This is a list, each call to org-occur pushes as cons cell,
  9456. containing the regular expression and the callback, onto the list.
  9457. The list can contain several entries if `org-occur' has been called
  9458. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9459. will only contain one set of parameters. When the highlights are
  9460. removed (for example with `C-c C-c', or with the next edit (depending
  9461. on `org-remove-highlights-with-change'), this variable is emptied
  9462. as well.")
  9463. (make-variable-buffer-local 'org-occur-parameters)
  9464. (defun org-occur (regexp &optional keep-previous callback)
  9465. "Make a compact tree which shows all matches of REGEXP.
  9466. The tree will show the lines where the regexp matches, and all higher
  9467. headlines above the match. It will also show the heading after the match,
  9468. to make sure editing the matching entry is easy.
  9469. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9470. call to `org-occur' will be kept, to allow stacking of calls to this
  9471. command.
  9472. If CALLBACK is non-nil, it is a function which is called to confirm
  9473. that the match should indeed be shown."
  9474. (interactive "sRegexp: \nP")
  9475. (unless keep-previous
  9476. (org-remove-occur-highlights nil nil t))
  9477. (push (cons regexp callback) org-occur-parameters)
  9478. (let ((cnt 0))
  9479. (save-excursion
  9480. (goto-char (point-min))
  9481. (if (or (not keep-previous) ; do not want to keep
  9482. (not org-occur-highlights)) ; no previous matches
  9483. ;; hide everything
  9484. (org-overview))
  9485. (while (re-search-forward regexp nil t)
  9486. (when (or (not callback)
  9487. (save-match-data (funcall callback)))
  9488. (setq cnt (1+ cnt))
  9489. (when org-highlight-sparse-tree-matches
  9490. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9491. (org-show-context 'occur-tree))))
  9492. (when org-remove-highlights-with-change
  9493. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9494. nil 'local))
  9495. (unless org-sparse-tree-open-archived-trees
  9496. (org-hide-archived-subtrees (point-min) (point-max)))
  9497. (run-hooks 'org-occur-hook)
  9498. (if (interactive-p)
  9499. (message "%d match(es) for regexp %s" cnt regexp))
  9500. cnt))
  9501. (defun org-show-context (&optional key)
  9502. "Make sure point and context and visible.
  9503. How much context is shown depends upon the variables
  9504. `org-show-hierarchy-above', `org-show-following-heading'. and
  9505. `org-show-siblings'."
  9506. (let ((heading-p (org-on-heading-p t))
  9507. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9508. (following-p (org-get-alist-option org-show-following-heading key))
  9509. (entry-p (org-get-alist-option org-show-entry-below key))
  9510. (siblings-p (org-get-alist-option org-show-siblings key)))
  9511. (catch 'exit
  9512. ;; Show heading or entry text
  9513. (if (and heading-p (not entry-p))
  9514. (org-flag-heading nil) ; only show the heading
  9515. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9516. (org-show-hidden-entry))) ; show entire entry
  9517. (when following-p
  9518. ;; Show next sibling, or heading below text
  9519. (save-excursion
  9520. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9521. (org-flag-heading nil))))
  9522. (when siblings-p (org-show-siblings))
  9523. (when hierarchy-p
  9524. ;; show all higher headings, possibly with siblings
  9525. (save-excursion
  9526. (while (and (condition-case nil
  9527. (progn (org-up-heading-all 1) t)
  9528. (error nil))
  9529. (not (bobp)))
  9530. (org-flag-heading nil)
  9531. (when siblings-p (org-show-siblings))))))))
  9532. (defun org-reveal (&optional siblings)
  9533. "Show current entry, hierarchy above it, and the following headline.
  9534. This can be used to show a consistent set of context around locations
  9535. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9536. not t for the search context.
  9537. With optional argument SIBLINGS, on each level of the hierarchy all
  9538. siblings are shown. This repairs the tree structure to what it would
  9539. look like when opened with hierarchical calls to `org-cycle'."
  9540. (interactive "P")
  9541. (let ((org-show-hierarchy-above t)
  9542. (org-show-following-heading t)
  9543. (org-show-siblings (if siblings t org-show-siblings)))
  9544. (org-show-context nil)))
  9545. (defun org-highlight-new-match (beg end)
  9546. "Highlight from BEG to END and mark the highlight is an occur headline."
  9547. (let ((ov (org-make-overlay beg end)))
  9548. (org-overlay-put ov 'face 'secondary-selection)
  9549. (push ov org-occur-highlights)))
  9550. (defun org-remove-occur-highlights (&optional beg end noremove)
  9551. "Remove the occur highlights from the buffer.
  9552. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9553. from the `before-change-functions' in the current buffer."
  9554. (interactive)
  9555. (unless org-inhibit-highlight-removal
  9556. (mapc 'org-delete-overlay org-occur-highlights)
  9557. (setq org-occur-highlights nil)
  9558. (setq org-occur-parameters nil)
  9559. (unless noremove
  9560. (remove-hook 'before-change-functions
  9561. 'org-remove-occur-highlights 'local))))
  9562. ;;;; Priorities
  9563. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9564. "Regular expression matching the priority indicator.")
  9565. (defvar org-remove-priority-next-time nil)
  9566. (defun org-priority-up ()
  9567. "Increase the priority of the current item."
  9568. (interactive)
  9569. (org-priority 'up))
  9570. (defun org-priority-down ()
  9571. "Decrease the priority of the current item."
  9572. (interactive)
  9573. (org-priority 'down))
  9574. (defun org-priority (&optional action)
  9575. "Change the priority of an item by ARG.
  9576. ACTION can be `set', `up', `down', or a character."
  9577. (interactive)
  9578. (unless org-enable-priority-commands
  9579. (error "Priority commands are disabled"))
  9580. (setq action (or action 'set))
  9581. (let (current new news have remove)
  9582. (save-excursion
  9583. (org-back-to-heading t)
  9584. (if (looking-at org-priority-regexp)
  9585. (setq current (string-to-char (match-string 2))
  9586. have t)
  9587. (setq current org-default-priority))
  9588. (cond
  9589. ((or (eq action 'set)
  9590. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9591. (if (not (eq action 'set))
  9592. (setq new action)
  9593. (message "Priority %c-%c, SPC to remove: "
  9594. org-highest-priority org-lowest-priority)
  9595. (setq new (read-char-exclusive)))
  9596. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9597. (= (upcase org-lowest-priority) org-lowest-priority))
  9598. (setq new (upcase new)))
  9599. (cond ((equal new ?\ ) (setq remove t))
  9600. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9601. (error "Priority must be between `%c' and `%c'"
  9602. org-highest-priority org-lowest-priority))))
  9603. ((eq action 'up)
  9604. (if (and (not have) (eq last-command this-command))
  9605. (setq new org-lowest-priority)
  9606. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9607. org-default-priority (1- current)))))
  9608. ((eq action 'down)
  9609. (if (and (not have) (eq last-command this-command))
  9610. (setq new org-highest-priority)
  9611. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9612. org-default-priority (1+ current)))))
  9613. (t (error "Invalid action")))
  9614. (if (or (< (upcase new) org-highest-priority)
  9615. (> (upcase new) org-lowest-priority))
  9616. (setq remove t))
  9617. (setq news (format "%c" new))
  9618. (if have
  9619. (if remove
  9620. (replace-match "" t t nil 1)
  9621. (replace-match news t t nil 2))
  9622. (if remove
  9623. (error "No priority cookie found in line")
  9624. (let ((case-fold-search nil))
  9625. (looking-at org-todo-line-regexp))
  9626. (if (match-end 2)
  9627. (progn
  9628. (goto-char (match-end 2))
  9629. (insert " [#" news "]"))
  9630. (goto-char (match-beginning 3))
  9631. (insert "[#" news "] ")))))
  9632. (org-preserve-lc (org-set-tags nil 'align))
  9633. (if remove
  9634. (message "Priority removed")
  9635. (message "Priority of current item set to %s" news))))
  9636. (defun org-get-priority (s)
  9637. "Find priority cookie and return priority."
  9638. (save-match-data
  9639. (if (not (string-match org-priority-regexp s))
  9640. (* 1000 (- org-lowest-priority org-default-priority))
  9641. (* 1000 (- org-lowest-priority
  9642. (string-to-char (match-string 2 s)))))))
  9643. ;;;; Tags
  9644. (defvar org-agenda-archives-mode)
  9645. (defvar org-map-continue-from nil
  9646. "Position from where mapping should continue.
  9647. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9648. (defvar org-scanner-tags nil
  9649. "The current tag list while the tags scanner is running.")
  9650. (defvar org-trust-scanner-tags nil
  9651. "Should `org-get-tags-at' use the tags fro the scanner.
  9652. This is for internal dynamical scoping only.
  9653. When this is non-nil, the function `org-get-tags-at' will return the value
  9654. of `org-scanner-tags' instead of building the list by itself. This
  9655. can lead to large speed-ups when the tags scanner is used in a file with
  9656. many entries, and when the list of tags is retrieved, for example to
  9657. obtain a list of properties. Building the tags list for each entry in such
  9658. a file becomes an N^2 operation - but with this variable set, it scales
  9659. as N.")
  9660. (defun org-scan-tags (action matcher &optional todo-only)
  9661. "Scan headline tags with inheritance and produce output ACTION.
  9662. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9663. or `agenda' to produce an entry list for an agenda view. It can also be
  9664. a Lisp form or a function that should be called at each matched headline, in
  9665. this case the return value is a list of all return values from these calls.
  9666. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9667. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9668. only lines with a TODO keyword are included in the output."
  9669. (require 'org-agenda)
  9670. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9671. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9672. (org-re
  9673. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9674. (props (list 'face 'default
  9675. 'done-face 'org-agenda-done
  9676. 'undone-face 'default
  9677. 'mouse-face 'highlight
  9678. 'org-not-done-regexp org-not-done-regexp
  9679. 'org-todo-regexp org-todo-regexp
  9680. 'keymap org-agenda-keymap
  9681. 'help-echo
  9682. (format "mouse-2 or RET jump to org file %s"
  9683. (abbreviate-file-name
  9684. (or (buffer-file-name (buffer-base-buffer))
  9685. (buffer-name (buffer-base-buffer)))))))
  9686. (case-fold-search nil)
  9687. (org-map-continue-from nil)
  9688. lspos tags tags-list
  9689. (tags-alist (list (cons 0 org-file-tags)))
  9690. (llast 0) rtn rtn1 level category i txt
  9691. todo marker entry priority)
  9692. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9693. (setq action (list 'lambda nil action)))
  9694. (save-excursion
  9695. (goto-char (point-min))
  9696. (when (eq action 'sparse-tree)
  9697. (org-overview)
  9698. (org-remove-occur-highlights))
  9699. (while (re-search-forward re nil t)
  9700. (catch :skip
  9701. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9702. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9703. (goto-char (setq lspos (match-beginning 0)))
  9704. (setq level (org-reduced-level (funcall outline-level))
  9705. category (org-get-category))
  9706. (setq i llast llast level)
  9707. ;; remove tag lists from same and sublevels
  9708. (while (>= i level)
  9709. (when (setq entry (assoc i tags-alist))
  9710. (setq tags-alist (delete entry tags-alist)))
  9711. (setq i (1- i)))
  9712. ;; add the next tags
  9713. (when tags
  9714. (setq tags (org-split-string tags ":")
  9715. tags-alist
  9716. (cons (cons level tags) tags-alist)))
  9717. ;; compile tags for current headline
  9718. (setq tags-list
  9719. (if org-use-tag-inheritance
  9720. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9721. tags)
  9722. org-scanner-tags tags-list)
  9723. (when org-use-tag-inheritance
  9724. (setcdr (car tags-alist)
  9725. (mapcar (lambda (x)
  9726. (setq x (copy-sequence x))
  9727. (org-add-prop-inherited x))
  9728. (cdar tags-alist))))
  9729. (when (and tags org-use-tag-inheritance
  9730. (or (not (eq t org-use-tag-inheritance))
  9731. org-tags-exclude-from-inheritance))
  9732. ;; selective inheritance, remove uninherited ones
  9733. (setcdr (car tags-alist)
  9734. (org-remove-uniherited-tags (cdar tags-alist))))
  9735. (when (and (or (not todo-only)
  9736. (and (member todo org-not-done-keywords)
  9737. (or (not org-agenda-tags-todo-honor-ignore-options)
  9738. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9739. (let ((case-fold-search t)) (eval matcher))
  9740. (or
  9741. (not (member org-archive-tag tags-list))
  9742. ;; we have an archive tag, should we use this anyway?
  9743. (or (not org-agenda-skip-archived-trees)
  9744. (and (eq action 'agenda) org-agenda-archives-mode))))
  9745. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9746. ;; select this headline
  9747. (cond
  9748. ((eq action 'sparse-tree)
  9749. (and org-highlight-sparse-tree-matches
  9750. (org-get-heading) (match-end 0)
  9751. (org-highlight-new-match
  9752. (match-beginning 0) (match-beginning 1)))
  9753. (org-show-context 'tags-tree))
  9754. ((eq action 'agenda)
  9755. (setq txt (org-format-agenda-item
  9756. ""
  9757. (concat
  9758. (if (eq org-tags-match-list-sublevels 'indented)
  9759. (make-string (1- level) ?.) "")
  9760. (org-get-heading))
  9761. category
  9762. tags-list
  9763. )
  9764. priority (org-get-priority txt))
  9765. (goto-char lspos)
  9766. (setq marker (org-agenda-new-marker))
  9767. (org-add-props txt props
  9768. 'org-marker marker 'org-hd-marker marker 'org-category category
  9769. 'todo-state todo
  9770. 'priority priority 'type "tagsmatch")
  9771. (push txt rtn))
  9772. ((functionp action)
  9773. (setq org-map-continue-from nil)
  9774. (save-excursion
  9775. (setq rtn1 (funcall action))
  9776. (push rtn1 rtn)))
  9777. (t (error "Invalid action")))
  9778. ;; if we are to skip sublevels, jump to end of subtree
  9779. (unless org-tags-match-list-sublevels
  9780. (org-end-of-subtree t)
  9781. (backward-char 1))))
  9782. ;; Get the correct position from where to continue
  9783. (if org-map-continue-from
  9784. (goto-char org-map-continue-from)
  9785. (and (= (point) lspos) (end-of-line 1)))))
  9786. (when (and (eq action 'sparse-tree)
  9787. (not org-sparse-tree-open-archived-trees))
  9788. (org-hide-archived-subtrees (point-min) (point-max)))
  9789. (nreverse rtn)))
  9790. (defun org-remove-uniherited-tags (tags)
  9791. "Remove all tags that are not inherited from the list TAGS."
  9792. (cond
  9793. ((eq org-use-tag-inheritance t)
  9794. (if org-tags-exclude-from-inheritance
  9795. (org-delete-all org-tags-exclude-from-inheritance tags)
  9796. tags))
  9797. ((not org-use-tag-inheritance) nil)
  9798. ((stringp org-use-tag-inheritance)
  9799. (delq nil (mapcar
  9800. (lambda (x)
  9801. (if (and (string-match org-use-tag-inheritance x)
  9802. (not (member x org-tags-exclude-from-inheritance)))
  9803. x nil))
  9804. tags)))
  9805. ((listp org-use-tag-inheritance)
  9806. (delq nil (mapcar
  9807. (lambda (x)
  9808. (if (member x org-use-tag-inheritance) x nil))
  9809. tags)))))
  9810. (defvar todo-only) ;; dynamically scoped
  9811. (defun org-match-sparse-tree (&optional todo-only match)
  9812. "Create a sparse tree according to tags string MATCH.
  9813. MATCH can contain positive and negative selection of tags, like
  9814. \"+WORK+URGENT-WITHBOSS\".
  9815. If optional argument TODO-ONLY is non-nil, only select lines that are
  9816. also TODO lines."
  9817. (interactive "P")
  9818. (org-prepare-agenda-buffers (list (current-buffer)))
  9819. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9820. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9821. (defvar org-cached-props nil)
  9822. (defun org-cached-entry-get (pom property)
  9823. (if (or (eq t org-use-property-inheritance)
  9824. (and (stringp org-use-property-inheritance)
  9825. (string-match org-use-property-inheritance property))
  9826. (and (listp org-use-property-inheritance)
  9827. (member property org-use-property-inheritance)))
  9828. ;; Caching is not possible, check it directly
  9829. (org-entry-get pom property 'inherit)
  9830. ;; Get all properties, so that we can do complicated checks easily
  9831. (cdr (assoc property (or org-cached-props
  9832. (setq org-cached-props
  9833. (org-entry-properties pom)))))))
  9834. (defun org-global-tags-completion-table (&optional files)
  9835. "Return the list of all tags in all agenda buffer/files."
  9836. (save-excursion
  9837. (org-uniquify
  9838. (delq nil
  9839. (apply 'append
  9840. (mapcar
  9841. (lambda (file)
  9842. (set-buffer (find-file-noselect file))
  9843. (append (org-get-buffer-tags)
  9844. (mapcar (lambda (x) (if (stringp (car-safe x))
  9845. (list (car-safe x)) nil))
  9846. org-tag-alist)))
  9847. (if (and files (car files))
  9848. files
  9849. (org-agenda-files))))))))
  9850. (defun org-make-tags-matcher (match)
  9851. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9852. ;; todo-only is scoped dynamically into this function, and the function
  9853. ;; may change it if the matcher asks for it.
  9854. (unless match
  9855. ;; Get a new match request, with completion
  9856. (let ((org-last-tags-completion-table
  9857. (org-global-tags-completion-table)))
  9858. (setq match (org-completing-read-no-ido
  9859. "Match: " 'org-tags-completion-function nil nil nil
  9860. 'org-tags-history))))
  9861. ;; Parse the string and create a lisp form
  9862. (let ((match0 match)
  9863. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9864. minus tag mm
  9865. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9866. orterms term orlist re-p str-p level-p level-op time-p
  9867. prop-p pn pv po cat-p gv rest)
  9868. (if (string-match "/+" match)
  9869. ;; match contains also a todo-matching request
  9870. (progn
  9871. (setq tagsmatch (substring match 0 (match-beginning 0))
  9872. todomatch (substring match (match-end 0)))
  9873. (if (string-match "^!" todomatch)
  9874. (setq todo-only t todomatch (substring todomatch 1)))
  9875. (if (string-match "^\\s-*$" todomatch)
  9876. (setq todomatch nil)))
  9877. ;; only matching tags
  9878. (setq tagsmatch match todomatch nil))
  9879. ;; Make the tags matcher
  9880. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9881. (setq tagsmatcher t)
  9882. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9883. (while (setq term (pop orterms))
  9884. (while (and (equal (substring term -1) "\\") orterms)
  9885. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9886. (while (string-match re term)
  9887. (setq rest (substring term (match-end 0))
  9888. minus (and (match-end 1)
  9889. (equal (match-string 1 term) "-"))
  9890. tag (match-string 2 term)
  9891. re-p (equal (string-to-char tag) ?{)
  9892. level-p (match-end 4)
  9893. prop-p (match-end 5)
  9894. mm (cond
  9895. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9896. (level-p
  9897. (setq level-op (org-op-to-function (match-string 3 term)))
  9898. `(,level-op level ,(string-to-number
  9899. (match-string 4 term))))
  9900. (prop-p
  9901. (setq pn (match-string 5 term)
  9902. po (match-string 6 term)
  9903. pv (match-string 7 term)
  9904. cat-p (equal pn "CATEGORY")
  9905. re-p (equal (string-to-char pv) ?{)
  9906. str-p (equal (string-to-char pv) ?\")
  9907. time-p (save-match-data
  9908. (string-match "^\"[[<].*[]>]\"$" pv))
  9909. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9910. (if time-p (setq pv (org-matcher-time pv)))
  9911. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9912. (cond
  9913. ((equal pn "CATEGORY")
  9914. (setq gv '(get-text-property (point) 'org-category)))
  9915. ((equal pn "TODO")
  9916. (setq gv 'todo))
  9917. (t
  9918. (setq gv `(org-cached-entry-get nil ,pn))))
  9919. (if re-p
  9920. (if (eq po 'org<>)
  9921. `(not (string-match ,pv (or ,gv "")))
  9922. `(string-match ,pv (or ,gv "")))
  9923. (if str-p
  9924. `(,po (or ,gv "") ,pv)
  9925. `(,po (string-to-number (or ,gv ""))
  9926. ,(string-to-number pv) ))))
  9927. (t `(member ,tag tags-list)))
  9928. mm (if minus (list 'not mm) mm)
  9929. term rest)
  9930. (push mm tagsmatcher))
  9931. (push (if (> (length tagsmatcher) 1)
  9932. (cons 'and tagsmatcher)
  9933. (car tagsmatcher))
  9934. orlist)
  9935. (setq tagsmatcher nil))
  9936. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9937. (setq tagsmatcher
  9938. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9939. ;; Make the todo matcher
  9940. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9941. (setq todomatcher t)
  9942. (setq orterms (org-split-string todomatch "|") orlist nil)
  9943. (while (setq term (pop orterms))
  9944. (while (string-match re term)
  9945. (setq minus (and (match-end 1)
  9946. (equal (match-string 1 term) "-"))
  9947. kwd (match-string 2 term)
  9948. re-p (equal (string-to-char kwd) ?{)
  9949. term (substring term (match-end 0))
  9950. mm (if re-p
  9951. `(string-match ,(substring kwd 1 -1) todo)
  9952. (list 'equal 'todo kwd))
  9953. mm (if minus (list 'not mm) mm))
  9954. (push mm todomatcher))
  9955. (push (if (> (length todomatcher) 1)
  9956. (cons 'and todomatcher)
  9957. (car todomatcher))
  9958. orlist)
  9959. (setq todomatcher nil))
  9960. (setq todomatcher (if (> (length orlist) 1)
  9961. (cons 'or orlist) (car orlist))))
  9962. ;; Return the string and lisp forms of the matcher
  9963. (setq matcher (if todomatcher
  9964. (list 'and tagsmatcher todomatcher)
  9965. tagsmatcher))
  9966. (cons match0 matcher)))
  9967. (defun org-op-to-function (op &optional stringp)
  9968. "Turn an operator into the appropriate function."
  9969. (setq op
  9970. (cond
  9971. ((equal op "<" ) '(< string< org-time<))
  9972. ((equal op ">" ) '(> org-string> org-time>))
  9973. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9974. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9975. ((member op '("=" "==")) '(= string= org-time=))
  9976. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9977. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9978. (defun org<> (a b) (not (= a b)))
  9979. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9980. (defun org-string>= (a b) (not (string< a b)))
  9981. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9982. (defun org-string<> (a b) (not (string= a b)))
  9983. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9984. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9985. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= 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) (org<> a b)))
  9989. (defun org-2ft (s)
  9990. "Convert S to a floating point time.
  9991. If S is already a number, just return it. If it is a string, parse
  9992. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9993. (cond
  9994. ((numberp s) s)
  9995. ((stringp s)
  9996. (condition-case nil
  9997. (float-time (apply 'encode-time (org-parse-time-string s)))
  9998. (error 0.)))
  9999. (t 0.)))
  10000. (defun org-time-today ()
  10001. "Time in seconds today at 0:00.
  10002. Returns the float number of seconds since the beginning of the
  10003. epoch to the beginning of today (00:00)."
  10004. (float-time (apply 'encode-time
  10005. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10006. (defun org-matcher-time (s)
  10007. "Interpret a time comparison value."
  10008. (save-match-data
  10009. (cond
  10010. ((string= s "<now>") (float-time))
  10011. ((string= s "<today>") (org-time-today))
  10012. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10013. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10014. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10015. (+ (org-time-today)
  10016. (* (string-to-number (match-string 1 s))
  10017. (cdr (assoc (match-string 2 s)
  10018. '(("d" . 86400.0) ("w" . 604800.0)
  10019. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10020. (t (org-2ft s)))))
  10021. (defun org-match-any-p (re list)
  10022. "Does re match any element of list?"
  10023. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10024. (delq nil list))
  10025. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10026. (defvar org-tags-overlay (org-make-overlay 1 1))
  10027. (org-detach-overlay org-tags-overlay)
  10028. (defun org-get-local-tags-at (&optional pos)
  10029. "Get a list of tags defined in the current headline."
  10030. (org-get-tags-at pos 'local))
  10031. (defun org-get-local-tags ()
  10032. "Get a list of tags defined in the current headline."
  10033. (org-get-tags-at nil 'local))
  10034. (defun org-get-tags-at (&optional pos local)
  10035. "Get a list of all headline tags applicable at POS.
  10036. POS defaults to point. If tags are inherited, the list contains
  10037. the targets in the same sequence as the headlines appear, i.e.
  10038. the tags of the current headline come last.
  10039. When LOCAL is non-nil, only return tags from the current headline,
  10040. ignore inherited ones."
  10041. (interactive)
  10042. (if (and org-trust-scanner-tags
  10043. (or (not pos) (equal pos (point)))
  10044. (not local))
  10045. org-scanner-tags
  10046. (let (tags ltags lastpos parent)
  10047. (save-excursion
  10048. (save-restriction
  10049. (widen)
  10050. (goto-char (or pos (point)))
  10051. (save-match-data
  10052. (catch 'done
  10053. (condition-case nil
  10054. (progn
  10055. (org-back-to-heading t)
  10056. (while (not (equal lastpos (point)))
  10057. (setq lastpos (point))
  10058. (when (looking-at
  10059. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10060. (setq ltags (org-split-string
  10061. (org-match-string-no-properties 1) ":"))
  10062. (when parent
  10063. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10064. (setq tags (append
  10065. (if parent
  10066. (org-remove-uniherited-tags ltags)
  10067. ltags)
  10068. tags)))
  10069. (or org-use-tag-inheritance (throw 'done t))
  10070. (if local (throw 'done t))
  10071. (or (org-up-heading-safe) (error nil))
  10072. (setq parent t)))
  10073. (error nil)))))
  10074. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10075. (defun org-add-prop-inherited (s)
  10076. (add-text-properties 0 (length s) '(inherited t) s)
  10077. s)
  10078. (defun org-toggle-tag (tag &optional onoff)
  10079. "Toggle the tag TAG for the current line.
  10080. If ONOFF is `on' or `off', don't toggle but set to this state."
  10081. (let (res current)
  10082. (save-excursion
  10083. (org-back-to-heading t)
  10084. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10085. (point-at-eol) t)
  10086. (progn
  10087. (setq current (match-string 1))
  10088. (replace-match ""))
  10089. (setq current ""))
  10090. (setq current (nreverse (org-split-string current ":")))
  10091. (cond
  10092. ((eq onoff 'on)
  10093. (setq res t)
  10094. (or (member tag current) (push tag current)))
  10095. ((eq onoff 'off)
  10096. (or (not (member tag current)) (setq current (delete tag current))))
  10097. (t (if (member tag current)
  10098. (setq current (delete tag current))
  10099. (setq res t)
  10100. (push tag current))))
  10101. (end-of-line 1)
  10102. (if current
  10103. (progn
  10104. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10105. (org-set-tags nil t))
  10106. (delete-horizontal-space))
  10107. (run-hooks 'org-after-tags-change-hook))
  10108. res))
  10109. (defun org-align-tags-here (to-col)
  10110. ;; Assumes that this is a headline
  10111. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10112. (beginning-of-line 1)
  10113. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10114. (< pos (match-beginning 2)))
  10115. (progn
  10116. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10117. (goto-char (match-beginning 1))
  10118. (insert " ")
  10119. (delete-region (point) (1+ (match-beginning 2)))
  10120. (setq ncol (max (1+ (current-column))
  10121. (1+ col)
  10122. (if (> to-col 0)
  10123. to-col
  10124. (- (abs to-col) tags-l))))
  10125. (setq p (point))
  10126. (insert (make-string (- ncol (current-column)) ?\ ))
  10127. (setq ncol (current-column))
  10128. (when indent-tabs-mode (tabify p (point-at-eol)))
  10129. (org-move-to-column (min ncol col) t))
  10130. (goto-char pos))))
  10131. (defun org-set-tags-command (&optional arg just-align)
  10132. "Call the set-tags command for the current entry."
  10133. (interactive "P")
  10134. (if (org-on-heading-p)
  10135. (org-set-tags arg just-align)
  10136. (save-excursion
  10137. (org-back-to-heading t)
  10138. (org-set-tags arg just-align))))
  10139. (defun org-set-tags (&optional arg just-align)
  10140. "Set the tags for the current headline.
  10141. With prefix ARG, realign all tags in headings in the current buffer."
  10142. (interactive "P")
  10143. (let* ((re (concat "^" outline-regexp))
  10144. (current (org-get-tags-string))
  10145. (col (current-column))
  10146. (org-setting-tags t)
  10147. table current-tags inherited-tags ; computed below when needed
  10148. tags p0 c0 c1 rpl)
  10149. (if arg
  10150. (save-excursion
  10151. (goto-char (point-min))
  10152. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10153. (while (re-search-forward re nil t)
  10154. (org-set-tags nil t)
  10155. (end-of-line 1)))
  10156. (message "All tags realigned to column %d" org-tags-column))
  10157. (if just-align
  10158. (setq tags current)
  10159. ;; Get a new set of tags from the user
  10160. (save-excursion
  10161. (setq table (append org-tag-persistent-alist
  10162. (or org-tag-alist (org-get-buffer-tags)))
  10163. org-last-tags-completion-table table
  10164. current-tags (org-split-string current ":")
  10165. inherited-tags (nreverse
  10166. (nthcdr (length current-tags)
  10167. (nreverse (org-get-tags-at))))
  10168. tags
  10169. (if (or (eq t org-use-fast-tag-selection)
  10170. (and org-use-fast-tag-selection
  10171. (delq nil (mapcar 'cdr table))))
  10172. (org-fast-tag-selection
  10173. current-tags inherited-tags table
  10174. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10175. (let ((org-add-colon-after-tag-completion t))
  10176. (org-trim
  10177. (org-without-partial-completion
  10178. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10179. nil nil current 'org-tags-history)))))))
  10180. (while (string-match "[-+&]+" tags)
  10181. ;; No boolean logic, just a list
  10182. (setq tags (replace-match ":" t t tags))))
  10183. (if (string-match "\\`[\t ]*\\'" tags)
  10184. (setq tags "")
  10185. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10186. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10187. ;; Insert new tags at the correct column
  10188. (beginning-of-line 1)
  10189. (cond
  10190. ((and (equal current "") (equal tags "")))
  10191. ((re-search-forward
  10192. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10193. (point-at-eol) t)
  10194. (if (equal tags "")
  10195. (setq rpl "")
  10196. (goto-char (match-beginning 0))
  10197. (setq c0 (current-column) p0 (point)
  10198. c1 (max (1+ c0) (if (> org-tags-column 0)
  10199. org-tags-column
  10200. (- (- org-tags-column) (length tags))))
  10201. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10202. (replace-match rpl t t)
  10203. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10204. tags)
  10205. (t (error "Tags alignment failed")))
  10206. (org-move-to-column col)
  10207. (unless just-align
  10208. (run-hooks 'org-after-tags-change-hook)))))
  10209. (defun org-change-tag-in-region (beg end tag off)
  10210. "Add or remove TAG for each entry in the region.
  10211. This works in the agenda, and also in an org-mode buffer."
  10212. (interactive
  10213. (list (region-beginning) (region-end)
  10214. (let ((org-last-tags-completion-table
  10215. (if (org-mode-p)
  10216. (org-get-buffer-tags)
  10217. (org-global-tags-completion-table))))
  10218. (org-ido-completing-read
  10219. "Tag: " 'org-tags-completion-function nil nil nil
  10220. 'org-tags-history))
  10221. (progn
  10222. (message "[s]et or [r]emove? ")
  10223. (equal (read-char-exclusive) ?r))))
  10224. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10225. (let ((agendap (equal major-mode 'org-agenda-mode))
  10226. l1 l2 m buf pos newhead (cnt 0))
  10227. (goto-char end)
  10228. (setq l2 (1- (org-current-line)))
  10229. (goto-char beg)
  10230. (setq l1 (org-current-line))
  10231. (loop for l from l1 to l2 do
  10232. (goto-line l)
  10233. (setq m (get-text-property (point) 'org-hd-marker))
  10234. (when (or (and (org-mode-p) (org-on-heading-p))
  10235. (and agendap m))
  10236. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10237. pos (if agendap m (point)))
  10238. (with-current-buffer buf
  10239. (save-excursion
  10240. (save-restriction
  10241. (goto-char pos)
  10242. (setq cnt (1+ cnt))
  10243. (org-toggle-tag tag (if off 'off 'on))
  10244. (setq newhead (org-get-heading)))))
  10245. (and agendap (org-agenda-change-all-lines newhead m))))
  10246. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10247. (defun org-tags-completion-function (string predicate &optional flag)
  10248. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10249. (confirm (lambda (x) (stringp (car x)))))
  10250. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10251. (setq s1 (match-string 1 string)
  10252. s2 (match-string 2 string))
  10253. (setq s1 "" s2 string))
  10254. (cond
  10255. ((eq flag nil)
  10256. ;; try completion
  10257. (setq rtn (try-completion s2 ctable confirm))
  10258. (if (stringp rtn)
  10259. (setq rtn
  10260. (concat s1 s2 (substring rtn (length s2))
  10261. (if (and org-add-colon-after-tag-completion
  10262. (assoc rtn ctable))
  10263. ":" ""))))
  10264. rtn)
  10265. ((eq flag t)
  10266. ;; all-completions
  10267. (all-completions s2 ctable confirm)
  10268. )
  10269. ((eq flag 'lambda)
  10270. ;; exact match?
  10271. (assoc s2 ctable)))
  10272. ))
  10273. (defun org-fast-tag-insert (kwd tags face &optional end)
  10274. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10275. (insert (format "%-12s" (concat kwd ":"))
  10276. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10277. (or end "")))
  10278. (defun org-fast-tag-show-exit (flag)
  10279. (save-excursion
  10280. (goto-line 3)
  10281. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10282. (replace-match ""))
  10283. (when flag
  10284. (end-of-line 1)
  10285. (org-move-to-column (- (window-width) 19) t)
  10286. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10287. (defun org-set-current-tags-overlay (current prefix)
  10288. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10289. (if (featurep 'xemacs)
  10290. (org-overlay-display org-tags-overlay (concat prefix s)
  10291. 'secondary-selection)
  10292. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10293. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10294. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10295. "Fast tag selection with single keys.
  10296. CURRENT is the current list of tags in the headline, INHERITED is the
  10297. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10298. possibly with grouping information. TODO-TABLE is a similar table with
  10299. TODO keywords, should these have keys assigned to them.
  10300. If the keys are nil, a-z are automatically assigned.
  10301. Returns the new tags string, or nil to not change the current settings."
  10302. (let* ((fulltable (append table todo-table))
  10303. (maxlen (apply 'max (mapcar
  10304. (lambda (x)
  10305. (if (stringp (car x)) (string-width (car x)) 0))
  10306. fulltable)))
  10307. (buf (current-buffer))
  10308. (expert (eq org-fast-tag-selection-single-key 'expert))
  10309. (buffer-tags nil)
  10310. (fwidth (+ maxlen 3 1 3))
  10311. (ncol (/ (- (window-width) 4) fwidth))
  10312. (i-face 'org-done)
  10313. (c-face 'org-todo)
  10314. tg cnt e c char c1 c2 ntable tbl rtn
  10315. ov-start ov-end ov-prefix
  10316. (exit-after-next org-fast-tag-selection-single-key)
  10317. (done-keywords org-done-keywords)
  10318. groups ingroup)
  10319. (save-excursion
  10320. (beginning-of-line 1)
  10321. (if (looking-at
  10322. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10323. (setq ov-start (match-beginning 1)
  10324. ov-end (match-end 1)
  10325. ov-prefix "")
  10326. (setq ov-start (1- (point-at-eol))
  10327. ov-end (1+ ov-start))
  10328. (skip-chars-forward "^\n\r")
  10329. (setq ov-prefix
  10330. (concat
  10331. (buffer-substring (1- (point)) (point))
  10332. (if (> (current-column) org-tags-column)
  10333. " "
  10334. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10335. (org-move-overlay org-tags-overlay ov-start ov-end)
  10336. (save-window-excursion
  10337. (if expert
  10338. (set-buffer (get-buffer-create " *Org tags*"))
  10339. (delete-other-windows)
  10340. (split-window-vertically)
  10341. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10342. (erase-buffer)
  10343. (org-set-local 'org-done-keywords done-keywords)
  10344. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10345. (org-fast-tag-insert "Current" current c-face "\n\n")
  10346. (org-fast-tag-show-exit exit-after-next)
  10347. (org-set-current-tags-overlay current ov-prefix)
  10348. (setq tbl fulltable char ?a cnt 0)
  10349. (while (setq e (pop tbl))
  10350. (cond
  10351. ((equal e '(:startgroup))
  10352. (push '() groups) (setq ingroup t)
  10353. (when (not (= cnt 0))
  10354. (setq cnt 0)
  10355. (insert "\n"))
  10356. (insert "{ "))
  10357. ((equal e '(:endgroup))
  10358. (setq ingroup nil cnt 0)
  10359. (insert "}\n"))
  10360. ((equal e '(:newline))
  10361. (when (not (= cnt 0))
  10362. (setq cnt 0)
  10363. (insert "\n")
  10364. (setq e (car tbl))
  10365. (while (equal (car tbl) '(:newline))
  10366. (insert "\n")
  10367. (setq tbl (cdr tbl)))))
  10368. (t
  10369. (setq tg (car e) c2 nil)
  10370. (if (cdr e)
  10371. (setq c (cdr e))
  10372. ;; automatically assign a character.
  10373. (setq c1 (string-to-char
  10374. (downcase (substring
  10375. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10376. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10377. (while (or (rassoc char ntable) (rassoc char table))
  10378. (setq char (1+ char)))
  10379. (setq c2 c1))
  10380. (setq c (or c2 char)))
  10381. (if ingroup (push tg (car groups)))
  10382. (setq tg (org-add-props tg nil 'face
  10383. (cond
  10384. ((not (assoc tg table))
  10385. (org-get-todo-face tg))
  10386. ((member tg current) c-face)
  10387. ((member tg inherited) i-face)
  10388. (t nil))))
  10389. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10390. (insert "[" c "] " tg (make-string
  10391. (- fwidth 4 (length tg)) ?\ ))
  10392. (push (cons tg c) ntable)
  10393. (when (= (setq cnt (1+ cnt)) ncol)
  10394. (insert "\n")
  10395. (if ingroup (insert " "))
  10396. (setq cnt 0)))))
  10397. (setq ntable (nreverse ntable))
  10398. (insert "\n")
  10399. (goto-char (point-min))
  10400. (if (not expert) (org-fit-window-to-buffer))
  10401. (setq rtn
  10402. (catch 'exit
  10403. (while t
  10404. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10405. (if groups " [!] no groups" " [!]groups")
  10406. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10407. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10408. (cond
  10409. ((= c ?\r) (throw 'exit t))
  10410. ((= c ?!)
  10411. (setq groups (not groups))
  10412. (goto-char (point-min))
  10413. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10414. ((= c ?\C-c)
  10415. (if (not expert)
  10416. (org-fast-tag-show-exit
  10417. (setq exit-after-next (not exit-after-next)))
  10418. (setq expert nil)
  10419. (delete-other-windows)
  10420. (split-window-vertically)
  10421. (org-switch-to-buffer-other-window " *Org tags*")
  10422. (org-fit-window-to-buffer)))
  10423. ((or (= c ?\C-g)
  10424. (and (= c ?q) (not (rassoc c ntable))))
  10425. (org-detach-overlay org-tags-overlay)
  10426. (setq quit-flag t))
  10427. ((= c ?\ )
  10428. (setq current nil)
  10429. (if exit-after-next (setq exit-after-next 'now)))
  10430. ((= c ?\t)
  10431. (condition-case nil
  10432. (setq tg (org-ido-completing-read
  10433. "Tag: "
  10434. (or buffer-tags
  10435. (with-current-buffer buf
  10436. (org-get-buffer-tags)))))
  10437. (quit (setq tg "")))
  10438. (when (string-match "\\S-" tg)
  10439. (add-to-list 'buffer-tags (list tg))
  10440. (if (member tg current)
  10441. (setq current (delete tg current))
  10442. (push tg current)))
  10443. (if exit-after-next (setq exit-after-next 'now)))
  10444. ((setq e (rassoc c todo-table) tg (car e))
  10445. (with-current-buffer buf
  10446. (save-excursion (org-todo tg)))
  10447. (if exit-after-next (setq exit-after-next 'now)))
  10448. ((setq e (rassoc c ntable) tg (car e))
  10449. (if (member tg current)
  10450. (setq current (delete tg current))
  10451. (loop for g in groups do
  10452. (if (member tg g)
  10453. (mapc (lambda (x)
  10454. (setq current (delete x current)))
  10455. g)))
  10456. (push tg current))
  10457. (if exit-after-next (setq exit-after-next 'now))))
  10458. ;; Create a sorted list
  10459. (setq current
  10460. (sort current
  10461. (lambda (a b)
  10462. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10463. (if (eq exit-after-next 'now) (throw 'exit t))
  10464. (goto-char (point-min))
  10465. (beginning-of-line 2)
  10466. (delete-region (point) (point-at-eol))
  10467. (org-fast-tag-insert "Current" current c-face)
  10468. (org-set-current-tags-overlay current ov-prefix)
  10469. (while (re-search-forward
  10470. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10471. (setq tg (match-string 1))
  10472. (add-text-properties
  10473. (match-beginning 1) (match-end 1)
  10474. (list 'face
  10475. (cond
  10476. ((member tg current) c-face)
  10477. ((member tg inherited) i-face)
  10478. (t (get-text-property (match-beginning 1) 'face))))))
  10479. (goto-char (point-min)))))
  10480. (org-detach-overlay org-tags-overlay)
  10481. (if rtn
  10482. (mapconcat 'identity current ":")
  10483. nil))))
  10484. (defun org-get-tags-string ()
  10485. "Get the TAGS string in the current headline."
  10486. (unless (org-on-heading-p t)
  10487. (error "Not on a heading"))
  10488. (save-excursion
  10489. (beginning-of-line 1)
  10490. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10491. (org-match-string-no-properties 1)
  10492. "")))
  10493. (defun org-get-tags ()
  10494. "Get the list of tags specified in the current headline."
  10495. (org-split-string (org-get-tags-string) ":"))
  10496. (defun org-get-buffer-tags ()
  10497. "Get a table of all tags used in the buffer, for completion."
  10498. (let (tags)
  10499. (save-excursion
  10500. (goto-char (point-min))
  10501. (while (re-search-forward
  10502. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10503. (when (equal (char-after (point-at-bol 0)) ?*)
  10504. (mapc (lambda (x) (add-to-list 'tags x))
  10505. (org-split-string (org-match-string-no-properties 1) ":")))))
  10506. (mapcar 'list tags)))
  10507. ;;;; The mapping API
  10508. ;;;###autoload
  10509. (defun org-map-entries (func &optional match scope &rest skip)
  10510. "Call FUNC at each headline selected by MATCH in SCOPE.
  10511. FUNC is a function or a lisp form. The function will be called without
  10512. arguments, with the cursor positioned at the beginning of the headline.
  10513. The return values of all calls to the function will be collected and
  10514. returned as a list.
  10515. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10516. does not need to preserve point. After evaluaton, the cursor will be
  10517. moved to the end of the line (presumably of the headline of the
  10518. processed entry) and search continues from there. Under some
  10519. circumstances, this may not produce the wanted results. For example,
  10520. if you have removed (e.g. archived) the current (sub)tree it could
  10521. mean that the next entry will be skipped entirely. In such cases, you
  10522. can specify the position from where search should continue by making
  10523. FUNC set the variable `org-map-continue-from' to the desired buffer
  10524. position.
  10525. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10526. Only headlines that are matched by this query will be considered during
  10527. the iteration. When MATCH is nil or t, all headlines will be
  10528. visited by the iteration.
  10529. SCOPE determines the scope of this command. It can be any of:
  10530. nil The current buffer, respecting the restriction if any
  10531. tree The subtree started with the entry at point
  10532. file The current buffer, without restriction
  10533. file-with-archives
  10534. The current buffer, and any archives associated with it
  10535. agenda All agenda files
  10536. agenda-with-archives
  10537. All agenda files with any archive files associated with them
  10538. \(file1 file2 ...)
  10539. If this is a list, all files in the list will be scanned
  10540. The remaining args are treated as settings for the skipping facilities of
  10541. the scanner. The following items can be given here:
  10542. archive skip trees with the archive tag.
  10543. comment skip trees with the COMMENT keyword
  10544. function or Emacs Lisp form:
  10545. will be used as value for `org-agenda-skip-function', so whenever
  10546. the the function returns t, FUNC will not be called for that
  10547. entry and search will continue from the point where the
  10548. function leaves it.
  10549. If your function needs to retrieve the tags including inherited tags
  10550. at the *current* entry, you can use the value of the variable
  10551. `org-scanner-tags' which will be much faster than getting the value
  10552. with `org-get-tags-at'. If your function gets properties with
  10553. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10554. to t around the call to `org-entry-properties' to get the same speedup.
  10555. Note that if your function moves around to retrieve tags and properties at
  10556. a *different* entry, you cannot use these techniques."
  10557. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10558. (org-agenda-skip-archived-trees (memq 'archive skip))
  10559. (org-agenda-skip-comment-trees (memq 'comment skip))
  10560. (org-agenda-skip-function
  10561. (car (org-delete-all '(comment archive) skip)))
  10562. (org-tags-match-list-sublevels t)
  10563. matcher file res
  10564. org-todo-keywords-for-agenda
  10565. org-done-keywords-for-agenda
  10566. org-todo-keyword-alist-for-agenda
  10567. org-tag-alist-for-agenda)
  10568. (cond
  10569. ((eq match t) (setq matcher t))
  10570. ((eq match nil) (setq matcher t))
  10571. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10572. (save-excursion
  10573. (save-restriction
  10574. (when (eq scope 'tree)
  10575. (org-back-to-heading t)
  10576. (org-narrow-to-subtree)
  10577. (setq scope nil))
  10578. (if (not scope)
  10579. (progn
  10580. (org-prepare-agenda-buffers
  10581. (list (buffer-file-name (current-buffer))))
  10582. (setq res (org-scan-tags func matcher)))
  10583. ;; Get the right scope
  10584. (cond
  10585. ((and scope (listp scope) (symbolp (car scope)))
  10586. (setq scope (eval scope)))
  10587. ((eq scope 'agenda)
  10588. (setq scope (org-agenda-files t)))
  10589. ((eq scope 'agenda-with-archives)
  10590. (setq scope (org-agenda-files t))
  10591. (setq scope (org-add-archive-files scope)))
  10592. ((eq scope 'file)
  10593. (setq scope (list (buffer-file-name))))
  10594. ((eq scope 'file-with-archives)
  10595. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10596. (org-prepare-agenda-buffers scope)
  10597. (while (setq file (pop scope))
  10598. (with-current-buffer (org-find-base-buffer-visiting file)
  10599. (save-excursion
  10600. (save-restriction
  10601. (widen)
  10602. (goto-char (point-min))
  10603. (setq res (append res (org-scan-tags func matcher))))))))))
  10604. res))
  10605. ;;;; Properties
  10606. ;;; Setting and retrieving properties
  10607. (defconst org-special-properties
  10608. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10609. "TIMESTAMP" "TIMESTAMP_IA")
  10610. "The special properties valid in Org-mode.
  10611. These are properties that are not defined in the property drawer,
  10612. but in some other way.")
  10613. (defconst org-default-properties
  10614. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10615. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10616. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10617. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10618. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER")
  10619. "Some properties that are used by Org-mode for various purposes.
  10620. Being in this list makes sure that they are offered for completion.")
  10621. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10622. "Regular expression matching the first line of a property drawer.")
  10623. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10624. "Regular expression matching the first line of a property drawer.")
  10625. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10626. "Regular expression matching the first line of a property drawer.")
  10627. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10628. "Regular expression matching the first line of a property drawer.")
  10629. (defconst org-property-drawer-re
  10630. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10631. org-property-end-re "\\)\n?")
  10632. "Matches an entire property drawer.")
  10633. (defconst org-clock-drawer-re
  10634. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10635. org-property-end-re "\\)\n?")
  10636. "Matches an entire clock drawer.")
  10637. (defun org-property-action ()
  10638. "Do an action on properties."
  10639. (interactive)
  10640. (let (c)
  10641. (org-at-property-p)
  10642. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10643. (setq c (read-char-exclusive))
  10644. (cond
  10645. ((equal c ?s)
  10646. (call-interactively 'org-set-property))
  10647. ((equal c ?d)
  10648. (call-interactively 'org-delete-property))
  10649. ((equal c ?D)
  10650. (call-interactively 'org-delete-property-globally))
  10651. ((equal c ?c)
  10652. (call-interactively 'org-compute-property-at-point))
  10653. (t (error "No such property action %c" c)))))
  10654. (defun org-at-property-p ()
  10655. "Is the cursor in a property line?"
  10656. ;; FIXME: Does not check if we are actually in the drawer.
  10657. ;; FIXME: also returns true on any drawers.....
  10658. ;; This is used by C-c C-c for property action.
  10659. (save-excursion
  10660. (beginning-of-line 1)
  10661. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10662. (defun org-get-property-block (&optional beg end force)
  10663. "Return the (beg . end) range of the body of the property drawer.
  10664. BEG and END can be beginning and end of subtree, if not given
  10665. they will be found.
  10666. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10667. (catch 'exit
  10668. (save-excursion
  10669. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10670. (end (or end (progn (outline-next-heading) (point)))))
  10671. (goto-char beg)
  10672. (if (re-search-forward org-property-start-re end t)
  10673. (setq beg (1+ (match-end 0)))
  10674. (if force
  10675. (save-excursion
  10676. (org-insert-property-drawer)
  10677. (setq end (progn (outline-next-heading) (point))))
  10678. (throw 'exit nil))
  10679. (goto-char beg)
  10680. (if (re-search-forward org-property-start-re end t)
  10681. (setq beg (1+ (match-end 0)))))
  10682. (if (re-search-forward org-property-end-re end t)
  10683. (setq end (match-beginning 0))
  10684. (or force (throw 'exit nil))
  10685. (goto-char beg)
  10686. (setq end beg)
  10687. (org-indent-line-function)
  10688. (insert ":END:\n"))
  10689. (cons beg end)))))
  10690. (defun org-entry-properties (&optional pom which)
  10691. "Get all properties of the entry at point-or-marker POM.
  10692. This includes the TODO keyword, the tags, time strings for deadline,
  10693. scheduled, and clocking, and any additional properties defined in the
  10694. entry. The return value is an alist, keys may occur multiple times
  10695. if the property key was used several times.
  10696. POM may also be nil, in which case the current entry is used.
  10697. If WHICH is nil or `all', get all properties. If WHICH is
  10698. `special' or `standard', only get that subclass."
  10699. (setq which (or which 'all))
  10700. (org-with-point-at pom
  10701. (let ((clockstr (substring org-clock-string 0 -1))
  10702. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10703. beg end range props sum-props key value string clocksum)
  10704. (save-excursion
  10705. (when (condition-case nil
  10706. (and (org-mode-p) (org-back-to-heading t))
  10707. (error nil))
  10708. (setq beg (point))
  10709. (setq sum-props (get-text-property (point) 'org-summaries))
  10710. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10711. (outline-next-heading)
  10712. (setq end (point))
  10713. (when (memq which '(all special))
  10714. ;; Get the special properties, like TODO and tags
  10715. (goto-char beg)
  10716. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10717. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10718. (when (looking-at org-priority-regexp)
  10719. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10720. (when (and (setq value (org-get-tags-string))
  10721. (string-match "\\S-" value))
  10722. (push (cons "TAGS" value) props))
  10723. (when (setq value (org-get-tags-at))
  10724. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10725. props))
  10726. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10727. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10728. string (if (equal key clockstr)
  10729. (org-no-properties
  10730. (org-trim
  10731. (buffer-substring
  10732. (match-beginning 3) (goto-char (point-at-eol)))))
  10733. (substring (org-match-string-no-properties 3) 1 -1)))
  10734. (unless key
  10735. (if (= (char-after (match-beginning 3)) ?\[)
  10736. (setq key "TIMESTAMP_IA")
  10737. (setq key "TIMESTAMP")))
  10738. (when (or (equal key clockstr) (not (assoc key props)))
  10739. (push (cons key string) props)))
  10740. )
  10741. (when (memq which '(all standard))
  10742. ;; Get the standard properties, like :PROP: ...
  10743. (setq range (org-get-property-block beg end))
  10744. (when range
  10745. (goto-char (car range))
  10746. (while (re-search-forward
  10747. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10748. (cdr range) t)
  10749. (setq key (org-match-string-no-properties 1)
  10750. value (org-trim (or (org-match-string-no-properties 2) "")))
  10751. (unless (member key excluded)
  10752. (push (cons key (or value "")) props)))))
  10753. (if clocksum
  10754. (push (cons "CLOCKSUM"
  10755. (org-columns-number-to-string (/ (float clocksum) 60.)
  10756. 'add_times))
  10757. props))
  10758. (unless (assoc "CATEGORY" props)
  10759. (setq value (or (org-get-category)
  10760. (progn (org-refresh-category-properties)
  10761. (org-get-category))))
  10762. (push (cons "CATEGORY" value) props))
  10763. (append sum-props (nreverse props)))))))
  10764. (defun org-entry-get (pom property &optional inherit)
  10765. "Get value of PROPERTY for entry at point-or-marker POM.
  10766. If INHERIT is non-nil and the entry does not have the property,
  10767. then also check higher levels of the hierarchy.
  10768. If INHERIT is the symbol `selective', use inheritance only if the setting
  10769. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10770. If the property is present but empty, the return value is the empty string.
  10771. If the property is not present at all, nil is returned."
  10772. (org-with-point-at pom
  10773. (if (and inherit (if (eq inherit 'selective)
  10774. (org-property-inherit-p property)
  10775. t))
  10776. (org-entry-get-with-inheritance property)
  10777. (if (member property org-special-properties)
  10778. ;; We need a special property. Use brute force, get all properties.
  10779. (cdr (assoc property (org-entry-properties nil 'special)))
  10780. (let ((range (org-get-property-block)))
  10781. (if (and range
  10782. (goto-char (car range))
  10783. (re-search-forward
  10784. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10785. (cdr range) t))
  10786. ;; Found the property, return it.
  10787. (if (match-end 1)
  10788. (org-match-string-no-properties 1)
  10789. "")))))))
  10790. (defun org-property-or-variable-value (var &optional inherit)
  10791. "Check if there is a property fixing the value of VAR.
  10792. If yes, return this value. If not, return the current value of the variable."
  10793. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10794. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10795. (read prop)
  10796. (symbol-value var))))
  10797. (defun org-entry-delete (pom property)
  10798. "Delete the property PROPERTY from entry at point-or-marker POM."
  10799. (org-with-point-at pom
  10800. (if (member property org-special-properties)
  10801. nil ; cannot delete these properties.
  10802. (let ((range (org-get-property-block)))
  10803. (if (and range
  10804. (goto-char (car range))
  10805. (re-search-forward
  10806. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10807. (cdr range) t))
  10808. (progn
  10809. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10810. t)
  10811. nil)))))
  10812. ;; Multi-values properties are properties that contain multiple values
  10813. ;; These values are assumed to be single words, separated by whitespace.
  10814. (defun org-entry-add-to-multivalued-property (pom property value)
  10815. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10816. (let* ((old (org-entry-get pom property))
  10817. (values (and old (org-split-string old "[ \t]"))))
  10818. (setq value (org-entry-protect-space value))
  10819. (unless (member value values)
  10820. (setq values (cons value values))
  10821. (org-entry-put pom property
  10822. (mapconcat 'identity values " ")))))
  10823. (defun org-entry-remove-from-multivalued-property (pom property value)
  10824. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10825. (let* ((old (org-entry-get pom property))
  10826. (values (and old (org-split-string old "[ \t]"))))
  10827. (setq value (org-entry-protect-space value))
  10828. (when (member value values)
  10829. (setq values (delete value values))
  10830. (org-entry-put pom property
  10831. (mapconcat 'identity values " ")))))
  10832. (defun org-entry-member-in-multivalued-property (pom property value)
  10833. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10834. (let* ((old (org-entry-get pom property))
  10835. (values (and old (org-split-string old "[ \t]"))))
  10836. (setq value (org-entry-protect-space value))
  10837. (member value values)))
  10838. (defun org-entry-get-multivalued-property (pom property)
  10839. "Return a list of values in a multivalued property."
  10840. (let* ((value (org-entry-get pom property))
  10841. (values (and value (org-split-string value "[ \t]"))))
  10842. (mapcar 'org-entry-restore-space values)))
  10843. (defun org-entry-put-multivalued-property (pom property &rest values)
  10844. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10845. VALUES should be a list of strings. Spaces will be protected."
  10846. (org-entry-put pom property
  10847. (mapconcat 'org-entry-protect-space values " "))
  10848. (let* ((value (org-entry-get pom property))
  10849. (values (and value (org-split-string value "[ \t]"))))
  10850. (mapcar 'org-entry-restore-space values)))
  10851. (defun org-entry-protect-space (s)
  10852. "Protect spaces and newline in string S."
  10853. (while (string-match " " s)
  10854. (setq s (replace-match "%20" t t s)))
  10855. (while (string-match "\n" s)
  10856. (setq s (replace-match "%0A" t t s)))
  10857. s)
  10858. (defun org-entry-restore-space (s)
  10859. "Restore spaces and newline in string S."
  10860. (while (string-match "%20" s)
  10861. (setq s (replace-match " " t t s)))
  10862. (while (string-match "%0A" s)
  10863. (setq s (replace-match "\n" t t s)))
  10864. s)
  10865. (defvar org-entry-property-inherited-from (make-marker)
  10866. "Marker pointing to the entry from where a property was inherited.
  10867. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10868. location of the entry where the inheritance search matched. If there was
  10869. no match, the marker will point nowhere.
  10870. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10871. is set.")
  10872. (defun org-entry-get-with-inheritance (property)
  10873. "Get entry property, and search higher levels if not present."
  10874. (move-marker org-entry-property-inherited-from nil)
  10875. (let (tmp)
  10876. (save-excursion
  10877. (save-restriction
  10878. (widen)
  10879. (catch 'ex
  10880. (while t
  10881. (when (setq tmp (org-entry-get nil property))
  10882. (org-back-to-heading t)
  10883. (move-marker org-entry-property-inherited-from (point))
  10884. (throw 'ex tmp))
  10885. (or (org-up-heading-safe) (throw 'ex nil)))))
  10886. (or tmp
  10887. (cdr (assoc property org-file-properties))
  10888. (cdr (assoc property org-global-properties))
  10889. (cdr (assoc property org-global-properties-fixed))))))
  10890. (defun org-entry-put (pom property value)
  10891. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10892. (org-with-point-at pom
  10893. (org-back-to-heading t)
  10894. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10895. range)
  10896. (cond
  10897. ((equal property "TODO")
  10898. (when (and (stringp value) (string-match "\\S-" value)
  10899. (not (member value org-todo-keywords-1)))
  10900. (error "\"%s\" is not a valid TODO state" value))
  10901. (if (or (not value)
  10902. (not (string-match "\\S-" value)))
  10903. (setq value 'none))
  10904. (org-todo value)
  10905. (org-set-tags nil 'align))
  10906. ((equal property "PRIORITY")
  10907. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10908. (string-to-char value) ?\ ))
  10909. (org-set-tags nil 'align))
  10910. ((equal property "SCHEDULED")
  10911. (if (re-search-forward org-scheduled-time-regexp end t)
  10912. (cond
  10913. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10914. ((eq value 'later) (org-timestamp-change 1 'day))
  10915. (t (call-interactively 'org-schedule)))
  10916. (call-interactively 'org-schedule)))
  10917. ((equal property "DEADLINE")
  10918. (if (re-search-forward org-deadline-time-regexp end t)
  10919. (cond
  10920. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10921. ((eq value 'later) (org-timestamp-change 1 'day))
  10922. (t (call-interactively 'org-deadline)))
  10923. (call-interactively 'org-deadline)))
  10924. ((member property org-special-properties)
  10925. (error "The %s property can not yet be set with `org-entry-put'"
  10926. property))
  10927. (t ; a non-special property
  10928. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10929. (setq range (org-get-property-block beg end 'force))
  10930. (goto-char (car range))
  10931. (if (re-search-forward
  10932. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10933. (progn
  10934. (delete-region (match-beginning 1) (match-end 1))
  10935. (goto-char (match-beginning 1)))
  10936. (goto-char (cdr range))
  10937. (insert "\n")
  10938. (backward-char 1)
  10939. (org-indent-line-function)
  10940. (insert ":" property ":"))
  10941. (and value (insert " " value))
  10942. (org-indent-line-function)))))))
  10943. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10944. "Get all property keys in the current buffer.
  10945. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10946. like tags and TODO state.
  10947. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10948. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10949. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10950. formats in the current buffer."
  10951. (let (rtn range cfmt s p)
  10952. (save-excursion
  10953. (save-restriction
  10954. (widen)
  10955. (goto-char (point-min))
  10956. (while (re-search-forward org-property-start-re nil t)
  10957. (setq range (org-get-property-block))
  10958. (goto-char (car range))
  10959. (while (re-search-forward
  10960. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10961. (cdr range) t)
  10962. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10963. (outline-next-heading))))
  10964. (when include-specials
  10965. (setq rtn (append org-special-properties rtn)))
  10966. (when include-defaults
  10967. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10968. (add-to-list 'rtn org-effort-property))
  10969. (when include-columns
  10970. (save-excursion
  10971. (save-restriction
  10972. (widen)
  10973. (goto-char (point-min))
  10974. (while (re-search-forward
  10975. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10976. nil t)
  10977. (setq cfmt (match-string 2) s 0)
  10978. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10979. cfmt s)
  10980. (setq s (match-end 0)
  10981. p (match-string 1 cfmt))
  10982. (unless (or (equal p "ITEM")
  10983. (member p org-special-properties))
  10984. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10985. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10986. (defun org-property-values (key)
  10987. "Return a list of all values of property KEY."
  10988. (save-excursion
  10989. (save-restriction
  10990. (widen)
  10991. (goto-char (point-min))
  10992. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10993. values)
  10994. (while (re-search-forward re nil t)
  10995. (add-to-list 'values (org-trim (match-string 1))))
  10996. (delete "" values)))))
  10997. (defun org-insert-property-drawer ()
  10998. "Insert a property drawer into the current entry."
  10999. (interactive)
  11000. (org-back-to-heading t)
  11001. (looking-at outline-regexp)
  11002. (let ((indent (- (match-end 0)(match-beginning 0)))
  11003. (beg (point))
  11004. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11005. end hiddenp)
  11006. (outline-next-heading)
  11007. (setq end (point))
  11008. (goto-char beg)
  11009. (while (re-search-forward re end t))
  11010. (setq hiddenp (org-invisible-p))
  11011. (end-of-line 1)
  11012. (and (equal (char-after) ?\n) (forward-char 1))
  11013. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11014. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11015. ;; just skip this line
  11016. (beginning-of-line 2)
  11017. ;; Drawer start, find the end
  11018. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11019. (beginning-of-line 1)))
  11020. (org-skip-over-state-notes)
  11021. (skip-chars-backward " \t\n\r")
  11022. (if (eq (char-before) ?*) (forward-char 1))
  11023. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11024. (beginning-of-line 0)
  11025. (org-indent-to-column indent)
  11026. (beginning-of-line 2)
  11027. (org-indent-to-column indent)
  11028. (beginning-of-line 0)
  11029. (if hiddenp
  11030. (save-excursion
  11031. (org-back-to-heading t)
  11032. (hide-entry))
  11033. (org-flag-drawer t))))
  11034. (defun org-set-property (property value)
  11035. "In the current entry, set PROPERTY to VALUE.
  11036. When called interactively, this will prompt for a property name, offering
  11037. completion on existing and default properties. And then it will prompt
  11038. for a value, offering completion either on allowed values (via an inherited
  11039. xxx_ALL property) or on existing values in other instances of this property
  11040. in the current file."
  11041. (interactive
  11042. (let* ((completion-ignore-case t)
  11043. (keys (org-buffer-property-keys nil t t))
  11044. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  11045. (prop (if (member prop0 keys)
  11046. prop0
  11047. (or (cdr (assoc (downcase prop0)
  11048. (mapcar (lambda (x) (cons (downcase x) x))
  11049. keys)))
  11050. prop0)))
  11051. (cur (org-entry-get nil prop))
  11052. (allowed (org-property-get-allowed-values nil prop 'table))
  11053. (existing (mapcar 'list (org-property-values prop)))
  11054. (val (if allowed
  11055. (org-completing-read "Value: " allowed nil 'req-match)
  11056. (let (org-completion-use-ido)
  11057. (org-completing-read
  11058. (concat "Value" (if (and cur (string-match "\\S-" cur))
  11059. (concat "[" cur "]") "")
  11060. ": ")
  11061. existing nil nil "" nil cur)))))
  11062. (list prop (if (equal val "") cur val))))
  11063. (unless (equal (org-entry-get nil property) value)
  11064. (org-entry-put nil property value)))
  11065. (defun org-delete-property (property)
  11066. "In the current entry, delete PROPERTY."
  11067. (interactive
  11068. (let* ((completion-ignore-case t)
  11069. (prop (org-ido-completing-read
  11070. "Property: " (org-entry-properties nil 'standard))))
  11071. (list prop)))
  11072. (message "Property %s %s" property
  11073. (if (org-entry-delete nil property)
  11074. "deleted"
  11075. "was not present in the entry")))
  11076. (defun org-delete-property-globally (property)
  11077. "Remove PROPERTY globally, from all entries."
  11078. (interactive
  11079. (let* ((completion-ignore-case t)
  11080. (prop (org-ido-completing-read
  11081. "Globally remove property: "
  11082. (mapcar 'list (org-buffer-property-keys)))))
  11083. (list prop)))
  11084. (save-excursion
  11085. (save-restriction
  11086. (widen)
  11087. (goto-char (point-min))
  11088. (let ((cnt 0))
  11089. (while (re-search-forward
  11090. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11091. nil t)
  11092. (setq cnt (1+ cnt))
  11093. (replace-match ""))
  11094. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11095. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11096. (defun org-compute-property-at-point ()
  11097. "Compute the property at point.
  11098. This looks for an enclosing column format, extracts the operator and
  11099. then applies it to the property in the column format's scope."
  11100. (interactive)
  11101. (unless (org-at-property-p)
  11102. (error "Not at a property"))
  11103. (let ((prop (org-match-string-no-properties 2)))
  11104. (org-columns-get-format-and-top-level)
  11105. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11106. (error "No operator defined for property %s" prop))
  11107. (org-columns-compute prop)))
  11108. (defun org-property-get-allowed-values (pom property &optional table)
  11109. "Get allowed values for the property PROPERTY.
  11110. When TABLE is non-nil, return an alist that can directly be used for
  11111. completion."
  11112. (let (vals)
  11113. (cond
  11114. ((equal property "TODO")
  11115. (setq vals (org-with-point-at pom
  11116. (append org-todo-keywords-1 '("")))))
  11117. ((equal property "PRIORITY")
  11118. (let ((n org-lowest-priority))
  11119. (while (>= n org-highest-priority)
  11120. (push (char-to-string n) vals)
  11121. (setq n (1- n)))))
  11122. ((member property org-special-properties))
  11123. (t
  11124. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11125. (when (and vals (string-match "\\S-" vals))
  11126. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11127. (setq vals (mapcar (lambda (x)
  11128. (cond ((stringp x) x)
  11129. ((numberp x) (number-to-string x))
  11130. ((symbolp x) (symbol-name x))
  11131. (t "???")))
  11132. vals)))))
  11133. (if table (mapcar 'list vals) vals)))
  11134. (defun org-property-previous-allowed-value (&optional previous)
  11135. "Switch to the next allowed value for this property."
  11136. (interactive)
  11137. (org-property-next-allowed-value t))
  11138. (defun org-property-next-allowed-value (&optional previous)
  11139. "Switch to the next allowed value for this property."
  11140. (interactive)
  11141. (unless (org-at-property-p)
  11142. (error "Not at a property"))
  11143. (let* ((key (match-string 2))
  11144. (value (match-string 3))
  11145. (allowed (or (org-property-get-allowed-values (point) key)
  11146. (and (member value '("[ ]" "[-]" "[X]"))
  11147. '("[ ]" "[X]"))))
  11148. nval)
  11149. (unless allowed
  11150. (error "Allowed values for this property have not been defined"))
  11151. (if previous (setq allowed (reverse allowed)))
  11152. (if (member value allowed)
  11153. (setq nval (car (cdr (member value allowed)))))
  11154. (setq nval (or nval (car allowed)))
  11155. (if (equal nval value)
  11156. (error "Only one allowed value for this property"))
  11157. (org-at-property-p)
  11158. (replace-match (concat " :" key ": " nval) t t)
  11159. (org-indent-line-function)
  11160. (beginning-of-line 1)
  11161. (skip-chars-forward " \t")))
  11162. (defun org-find-entry-with-id (ident)
  11163. "Locate the entry that contains the ID property with exact value IDENT.
  11164. IDENT can be a string, a symbol or a number, this function will search for
  11165. the string representation of it.
  11166. Return the position where this entry starts, or nil if there is no such entry."
  11167. (interactive "sID: ")
  11168. (let ((id (cond
  11169. ((stringp ident) ident)
  11170. ((symbol-name ident) (symbol-name ident))
  11171. ((numberp ident) (number-to-string ident))
  11172. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11173. (case-fold-search nil))
  11174. (save-excursion
  11175. (save-restriction
  11176. (widen)
  11177. (goto-char (point-min))
  11178. (when (re-search-forward
  11179. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11180. nil t)
  11181. (org-back-to-heading)
  11182. (point))))))
  11183. ;;;; Timestamps
  11184. (defvar org-last-changed-timestamp nil)
  11185. (defvar org-last-inserted-timestamp nil
  11186. "The last time stamp inserted with `org-insert-time-stamp'.")
  11187. (defvar org-time-was-given) ; dynamically scoped parameter
  11188. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11189. (defvar org-ts-what) ; dynamically scoped parameter
  11190. (defun org-time-stamp (arg &optional inactive)
  11191. "Prompt for a date/time and insert a time stamp.
  11192. If the user specifies a time like HH:MM, or if this command is called
  11193. with a prefix argument, the time stamp will contain date and time.
  11194. Otherwise, only the date will be included. All parts of a date not
  11195. specified by the user will be filled in from the current date/time.
  11196. So if you press just return without typing anything, the time stamp
  11197. will represent the current date/time. If there is already a timestamp
  11198. at the cursor, it will be modified."
  11199. (interactive "P")
  11200. (let* ((ts nil)
  11201. (default-time
  11202. ;; Default time is either today, or, when entering a range,
  11203. ;; the range start.
  11204. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11205. (save-excursion
  11206. (re-search-backward
  11207. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11208. (- (point) 20) t)))
  11209. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11210. (current-time)))
  11211. (default-input (and ts (org-get-compact-tod ts)))
  11212. org-time-was-given org-end-time-was-given time)
  11213. (cond
  11214. ((and (org-at-timestamp-p t)
  11215. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11216. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11217. (insert "--")
  11218. (setq time (let ((this-command this-command))
  11219. (org-read-date arg 'totime nil nil
  11220. default-time default-input)))
  11221. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11222. ((org-at-timestamp-p t)
  11223. (setq time (let ((this-command this-command))
  11224. (org-read-date arg 'totime nil nil default-time default-input)))
  11225. (when (org-at-timestamp-p t) ; just to get the match data
  11226. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11227. (replace-match "")
  11228. (setq org-last-changed-timestamp
  11229. (org-insert-time-stamp
  11230. time (or org-time-was-given arg)
  11231. inactive nil nil (list org-end-time-was-given))))
  11232. (message "Timestamp updated"))
  11233. (t
  11234. (setq time (let ((this-command this-command))
  11235. (org-read-date arg 'totime nil nil default-time default-input)))
  11236. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11237. nil nil (list org-end-time-was-given))))))
  11238. ;; FIXME: can we use this for something else, like computing time differences?
  11239. (defun org-get-compact-tod (s)
  11240. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11241. (let* ((t1 (match-string 1 s))
  11242. (h1 (string-to-number (match-string 2 s)))
  11243. (m1 (string-to-number (match-string 3 s)))
  11244. (t2 (and (match-end 4) (match-string 5 s)))
  11245. (h2 (and t2 (string-to-number (match-string 6 s))))
  11246. (m2 (and t2 (string-to-number (match-string 7 s))))
  11247. dh dm)
  11248. (if (not t2)
  11249. t1
  11250. (setq dh (- h2 h1) dm (- m2 m1))
  11251. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11252. (concat t1 "+" (number-to-string dh)
  11253. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11254. (defun org-time-stamp-inactive (&optional arg)
  11255. "Insert an inactive time stamp.
  11256. An inactive time stamp is enclosed in square brackets instead of angle
  11257. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11258. does not link to the calendar and cannot be changed with the S-cursor keys.
  11259. So these are more for recording a certain time/date."
  11260. (interactive "P")
  11261. (org-time-stamp arg 'inactive))
  11262. (defvar org-date-ovl (org-make-overlay 1 1))
  11263. (org-overlay-put org-date-ovl 'face 'org-warning)
  11264. (org-detach-overlay org-date-ovl)
  11265. (defvar org-ans1) ; dynamically scoped parameter
  11266. (defvar org-ans2) ; dynamically scoped parameter
  11267. (defvar org-plain-time-of-day-regexp) ; defined below
  11268. (defvar org-overriding-default-time nil) ; dynamically scoped
  11269. (defvar org-read-date-overlay nil)
  11270. (defvar org-dcst nil) ; dynamically scoped
  11271. (defvar org-read-date-history nil)
  11272. (defun org-read-date (&optional with-time to-time from-string prompt
  11273. default-time default-input)
  11274. "Read a date, possibly a time, and make things smooth for the user.
  11275. The prompt will suggest to enter an ISO date, but you can also enter anything
  11276. which will at least partially be understood by `parse-time-string'.
  11277. Unrecognized parts of the date will default to the current day, month, year,
  11278. hour and minute. If this command is called to replace a timestamp at point,
  11279. of to enter the second timestamp of a range, the default time is taken from the
  11280. existing stamp. For example,
  11281. 3-2-5 --> 2003-02-05
  11282. feb 15 --> currentyear-02-15
  11283. sep 12 9 --> 2009-09-12
  11284. 12:45 --> today 12:45
  11285. 22 sept 0:34 --> currentyear-09-22 0:34
  11286. 12 --> currentyear-currentmonth-12
  11287. Fri --> nearest Friday (today or later)
  11288. etc.
  11289. Furthermore you can specify a relative date by giving, as the *first* thing
  11290. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11291. change in days weeks, months, years.
  11292. With a single plus or minus, the date is relative to today. With a double
  11293. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11294. +4d --> four days from today
  11295. +4 --> same as above
  11296. +2w --> two weeks from today
  11297. ++5 --> five days from default date
  11298. The function understands only English month and weekday abbreviations,
  11299. but this can be configured with the variables `parse-time-months' and
  11300. `parse-time-weekdays'.
  11301. While prompting, a calendar is popped up - you can also select the
  11302. date with the mouse (button 1). The calendar shows a period of three
  11303. months. To scroll it to other months, use the keys `>' and `<'.
  11304. If you don't like the calendar, turn it off with
  11305. \(setq org-read-date-popup-calendar nil)
  11306. With optional argument TO-TIME, the date will immediately be converted
  11307. to an internal time.
  11308. With an optional argument WITH-TIME, the prompt will suggest to also
  11309. insert a time. Note that when WITH-TIME is not set, you can still
  11310. enter a time, and this function will inform the calling routine about
  11311. this change. The calling routine may then choose to change the format
  11312. used to insert the time stamp into the buffer to include the time.
  11313. With optional argument FROM-STRING, read from this string instead from
  11314. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11315. the time/date that is used for everything that is not specified by the
  11316. user."
  11317. (require 'parse-time)
  11318. (let* ((org-time-stamp-rounding-minutes
  11319. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11320. (org-dcst org-display-custom-times)
  11321. (ct (org-current-time))
  11322. (def (or org-overriding-default-time default-time ct))
  11323. (defdecode (decode-time def))
  11324. (dummy (progn
  11325. (when (< (nth 2 defdecode) org-extend-today-until)
  11326. (setcar (nthcdr 2 defdecode) -1)
  11327. (setcar (nthcdr 1 defdecode) 59)
  11328. (setq def (apply 'encode-time defdecode)
  11329. defdecode (decode-time def)))))
  11330. (calendar-move-hook nil)
  11331. (calendar-view-diary-initially-flag nil)
  11332. (view-diary-entries-initially nil)
  11333. (calendar-view-holidays-initially-flag nil)
  11334. (view-calendar-holidays-initially nil)
  11335. (timestr (format-time-string
  11336. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11337. (prompt (concat (if prompt (concat prompt " ") "")
  11338. (format "Date+time [%s]: " timestr)))
  11339. ans (org-ans0 "") org-ans1 org-ans2 final)
  11340. (cond
  11341. (from-string (setq ans from-string))
  11342. (org-read-date-popup-calendar
  11343. (save-excursion
  11344. (save-window-excursion
  11345. (calendar)
  11346. (calendar-forward-day (- (time-to-days def)
  11347. (calendar-absolute-from-gregorian
  11348. (calendar-current-date))))
  11349. (org-eval-in-calendar nil t)
  11350. (let* ((old-map (current-local-map))
  11351. (map (copy-keymap calendar-mode-map))
  11352. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11353. (org-defkey map (kbd "RET") 'org-calendar-select)
  11354. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11355. 'org-calendar-select-mouse)
  11356. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11357. 'org-calendar-select-mouse)
  11358. (org-defkey minibuffer-local-map [(meta shift left)]
  11359. (lambda () (interactive)
  11360. (org-eval-in-calendar '(calendar-backward-month 1))))
  11361. (org-defkey minibuffer-local-map [(meta shift right)]
  11362. (lambda () (interactive)
  11363. (org-eval-in-calendar '(calendar-forward-month 1))))
  11364. (org-defkey minibuffer-local-map [(meta shift up)]
  11365. (lambda () (interactive)
  11366. (org-eval-in-calendar '(calendar-backward-year 1))))
  11367. (org-defkey minibuffer-local-map [(meta shift down)]
  11368. (lambda () (interactive)
  11369. (org-eval-in-calendar '(calendar-forward-year 1))))
  11370. (org-defkey minibuffer-local-map [?\e (shift left)]
  11371. (lambda () (interactive)
  11372. (org-eval-in-calendar '(calendar-backward-month 1))))
  11373. (org-defkey minibuffer-local-map [?\e (shift right)]
  11374. (lambda () (interactive)
  11375. (org-eval-in-calendar '(calendar-forward-month 1))))
  11376. (org-defkey minibuffer-local-map [?\e (shift up)]
  11377. (lambda () (interactive)
  11378. (org-eval-in-calendar '(calendar-backward-year 1))))
  11379. (org-defkey minibuffer-local-map [?\e (shift down)]
  11380. (lambda () (interactive)
  11381. (org-eval-in-calendar '(calendar-forward-year 1))))
  11382. (org-defkey minibuffer-local-map [(shift up)]
  11383. (lambda () (interactive)
  11384. (org-eval-in-calendar '(calendar-backward-week 1))))
  11385. (org-defkey minibuffer-local-map [(shift down)]
  11386. (lambda () (interactive)
  11387. (org-eval-in-calendar '(calendar-forward-week 1))))
  11388. (org-defkey minibuffer-local-map [(shift left)]
  11389. (lambda () (interactive)
  11390. (org-eval-in-calendar '(calendar-backward-day 1))))
  11391. (org-defkey minibuffer-local-map [(shift right)]
  11392. (lambda () (interactive)
  11393. (org-eval-in-calendar '(calendar-forward-day 1))))
  11394. (org-defkey minibuffer-local-map ">"
  11395. (lambda () (interactive)
  11396. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11397. (org-defkey minibuffer-local-map "<"
  11398. (lambda () (interactive)
  11399. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11400. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11401. (unwind-protect
  11402. (progn
  11403. (use-local-map map)
  11404. (add-hook 'post-command-hook 'org-read-date-display)
  11405. (setq org-ans0 (read-string prompt default-input
  11406. 'org-read-date-history nil))
  11407. ;; org-ans0: from prompt
  11408. ;; org-ans1: from mouse click
  11409. ;; org-ans2: from calendar motion
  11410. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11411. (remove-hook 'post-command-hook 'org-read-date-display)
  11412. (use-local-map old-map)
  11413. (when org-read-date-overlay
  11414. (org-delete-overlay org-read-date-overlay)
  11415. (setq org-read-date-overlay nil)))))))
  11416. (t ; Naked prompt only
  11417. (unwind-protect
  11418. (setq ans (read-string prompt default-input
  11419. 'org-read-date-history timestr))
  11420. (when org-read-date-overlay
  11421. (org-delete-overlay org-read-date-overlay)
  11422. (setq org-read-date-overlay nil)))))
  11423. (setq final (org-read-date-analyze ans def defdecode))
  11424. (if to-time
  11425. (apply 'encode-time final)
  11426. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11427. (format "%04d-%02d-%02d %02d:%02d"
  11428. (nth 5 final) (nth 4 final) (nth 3 final)
  11429. (nth 2 final) (nth 1 final))
  11430. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11431. (defvar def)
  11432. (defvar defdecode)
  11433. (defvar with-time)
  11434. (defun org-read-date-display ()
  11435. "Display the current date prompt interpretation in the minibuffer."
  11436. (when org-read-date-display-live
  11437. (when org-read-date-overlay
  11438. (org-delete-overlay org-read-date-overlay))
  11439. (let ((p (point)))
  11440. (end-of-line 1)
  11441. (while (not (equal (buffer-substring
  11442. (max (point-min) (- (point) 4)) (point))
  11443. " "))
  11444. (insert " "))
  11445. (goto-char p))
  11446. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11447. " " (or org-ans1 org-ans2)))
  11448. (org-end-time-was-given nil)
  11449. (f (org-read-date-analyze ans def defdecode))
  11450. (fmts (if org-dcst
  11451. org-time-stamp-custom-formats
  11452. org-time-stamp-formats))
  11453. (fmt (if (or with-time
  11454. (and (boundp 'org-time-was-given) org-time-was-given))
  11455. (cdr fmts)
  11456. (car fmts)))
  11457. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11458. (when (and org-end-time-was-given
  11459. (string-match org-plain-time-of-day-regexp txt))
  11460. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11461. org-end-time-was-given
  11462. (substring txt (match-end 0)))))
  11463. (setq org-read-date-overlay
  11464. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11465. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11466. (defun org-read-date-analyze (ans def defdecode)
  11467. "Analyse the combined answer of the date prompt."
  11468. ;; FIXME: cleanup and comment
  11469. (let (delta deltan deltaw deltadef year month day
  11470. hour minute second wday pm h2 m2 tl wday1
  11471. iso-year iso-weekday iso-week iso-year iso-date)
  11472. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11473. (setq ans "+0"))
  11474. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11475. (setq ans (replace-match "" t t ans)
  11476. deltan (car delta)
  11477. deltaw (nth 1 delta)
  11478. deltadef (nth 2 delta)))
  11479. ;; Check if there is an iso week date in there
  11480. ;; If yes, sore the info and postpone interpreting it until the rest
  11481. ;; of the parsing is done
  11482. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11483. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11484. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11485. iso-week (string-to-number (match-string 2 ans)))
  11486. (setq ans (replace-match "" t t ans)))
  11487. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11488. (when (string-match
  11489. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11490. (setq year (if (match-end 2)
  11491. (string-to-number (match-string 2 ans))
  11492. (string-to-number (format-time-string "%Y")))
  11493. month (string-to-number (match-string 3 ans))
  11494. day (string-to-number (match-string 4 ans)))
  11495. (if (< year 100) (setq year (+ 2000 year)))
  11496. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11497. t nil ans)))
  11498. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11499. ;; If there is a time with am/pm, and *no* time without it, we convert
  11500. ;; so that matching will be successful.
  11501. (loop for i from 1 to 2 do ; twice, for end time as well
  11502. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11503. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11504. (setq hour (string-to-number (match-string 1 ans))
  11505. minute (if (match-end 3)
  11506. (string-to-number (match-string 3 ans))
  11507. 0)
  11508. pm (equal ?p
  11509. (string-to-char (downcase (match-string 4 ans)))))
  11510. (if (and (= hour 12) (not pm))
  11511. (setq hour 0)
  11512. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11513. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11514. t t ans))))
  11515. ;; Check if a time range is given as a duration
  11516. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11517. (setq hour (string-to-number (match-string 1 ans))
  11518. h2 (+ hour (string-to-number (match-string 3 ans)))
  11519. minute (string-to-number (match-string 2 ans))
  11520. m2 (+ minute (if (match-end 5) (string-to-number
  11521. (match-string 5 ans))0)))
  11522. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11523. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11524. t t ans)))
  11525. ;; Check if there is a time range
  11526. (when (boundp 'org-end-time-was-given)
  11527. (setq org-time-was-given nil)
  11528. (when (and (string-match org-plain-time-of-day-regexp ans)
  11529. (match-end 8))
  11530. (setq org-end-time-was-given (match-string 8 ans))
  11531. (setq ans (concat (substring ans 0 (match-beginning 7))
  11532. (substring ans (match-end 7))))))
  11533. (setq tl (parse-time-string ans)
  11534. day (or (nth 3 tl) (nth 3 defdecode))
  11535. month (or (nth 4 tl)
  11536. (if (and org-read-date-prefer-future
  11537. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11538. (1+ (nth 4 defdecode))
  11539. (nth 4 defdecode)))
  11540. year (or (nth 5 tl)
  11541. (if (and org-read-date-prefer-future
  11542. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11543. (1+ (nth 5 defdecode))
  11544. (nth 5 defdecode)))
  11545. hour (or (nth 2 tl) (nth 2 defdecode))
  11546. minute (or (nth 1 tl) (nth 1 defdecode))
  11547. second (or (nth 0 tl) 0)
  11548. wday (nth 6 tl))
  11549. ;; Special date definitions below
  11550. (cond
  11551. (iso-week
  11552. ;; There was an iso week
  11553. (setq year (or iso-year year)
  11554. day (or iso-weekday wday 1)
  11555. wday nil ; to make sure that the trigger below does not match
  11556. iso-date (calendar-gregorian-from-absolute
  11557. (calendar-absolute-from-iso
  11558. (list iso-week day year))))
  11559. ; FIXME: Should we also push ISO weeks into the future?
  11560. ; (when (and org-read-date-prefer-future
  11561. ; (not iso-year)
  11562. ; (< (calendar-absolute-from-gregorian iso-date)
  11563. ; (time-to-days (current-time))))
  11564. ; (setq year (1+ year)
  11565. ; iso-date (calendar-gregorian-from-absolute
  11566. ; (calendar-absolute-from-iso
  11567. ; (list iso-week day year)))))
  11568. (setq month (car iso-date)
  11569. year (nth 2 iso-date)
  11570. day (nth 1 iso-date)))
  11571. (deltan
  11572. (unless deltadef
  11573. (let ((now (decode-time (current-time))))
  11574. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11575. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11576. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11577. ((equal deltaw "m") (setq month (+ month deltan)))
  11578. ((equal deltaw "y") (setq year (+ year deltan)))))
  11579. ((and wday (not (nth 3 tl)))
  11580. ;; Weekday was given, but no day, so pick that day in the week
  11581. ;; on or after the derived date.
  11582. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11583. (unless (equal wday wday1)
  11584. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11585. (if (and (boundp 'org-time-was-given)
  11586. (nth 2 tl))
  11587. (setq org-time-was-given t))
  11588. (if (< year 100) (setq year (+ 2000 year)))
  11589. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11590. (list second minute hour day month year)))
  11591. (defvar parse-time-weekdays)
  11592. (defun org-read-date-get-relative (s today default)
  11593. "Check string S for special relative date string.
  11594. TODAY and DEFAULT are internal times, for today and for a default.
  11595. Return shift list (N what def-flag)
  11596. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11597. N is the number of WHATs to shift.
  11598. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11599. the DEFAULT date rather than TODAY."
  11600. (when (and
  11601. (string-match
  11602. (concat
  11603. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11604. "\\([0-9]+\\)?"
  11605. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11606. "\\([ \t]\\|$\\)") s)
  11607. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11608. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11609. (string-to-char (substring (match-string 1 s) -1))
  11610. ?+))
  11611. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11612. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11613. (what (if (match-end 3) (match-string 3 s) "d"))
  11614. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11615. (date (if rel default today))
  11616. (wday (nth 6 (decode-time date)))
  11617. delta)
  11618. (if wday1
  11619. (progn
  11620. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11621. (if (= dir ?-) (setq delta (- delta 7)))
  11622. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11623. (list delta "d" rel))
  11624. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11625. (defun org-eval-in-calendar (form &optional keepdate)
  11626. "Eval FORM in the calendar window and return to current window.
  11627. Also, store the cursor date in variable org-ans2."
  11628. (let ((sw (selected-window)))
  11629. (select-window (get-buffer-window "*Calendar*"))
  11630. (eval form)
  11631. (when (and (not keepdate) (calendar-cursor-to-date))
  11632. (let* ((date (calendar-cursor-to-date))
  11633. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11634. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11635. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11636. (select-window sw)))
  11637. (defun org-calendar-select ()
  11638. "Return to `org-read-date' with the date currently selected.
  11639. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11640. (interactive)
  11641. (when (calendar-cursor-to-date)
  11642. (let* ((date (calendar-cursor-to-date))
  11643. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11644. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11645. (if (active-minibuffer-window) (exit-minibuffer))))
  11646. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11647. "Insert a date stamp for the date given by the internal TIME.
  11648. WITH-HM means, use the stamp format that includes the time of the day.
  11649. INACTIVE means use square brackets instead of angular ones, so that the
  11650. stamp will not contribute to the agenda.
  11651. PRE and POST are optional strings to be inserted before and after the
  11652. stamp.
  11653. The command returns the inserted time stamp."
  11654. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11655. stamp)
  11656. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11657. (insert-before-markers (or pre ""))
  11658. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11659. (when (listp extra)
  11660. (setq extra (car extra))
  11661. (if (and (stringp extra)
  11662. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11663. (setq extra (format "-%02d:%02d"
  11664. (string-to-number (match-string 1 extra))
  11665. (string-to-number (match-string 2 extra))))
  11666. (setq extra nil)))
  11667. (when extra
  11668. (backward-char 1)
  11669. (insert-before-markers extra)
  11670. (forward-char 1))
  11671. (insert-before-markers (or post ""))
  11672. (setq org-last-inserted-timestamp stamp)))
  11673. (defun org-toggle-time-stamp-overlays ()
  11674. "Toggle the use of custom time stamp formats."
  11675. (interactive)
  11676. (setq org-display-custom-times (not org-display-custom-times))
  11677. (unless org-display-custom-times
  11678. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11679. (while (setq p (next-single-property-change p 'display))
  11680. (if (and (get-text-property p 'display)
  11681. (eq (get-text-property p 'face) 'org-date))
  11682. (remove-text-properties
  11683. p (setq p (next-single-property-change p 'display))
  11684. '(display t))))
  11685. (set-buffer-modified-p bmp)))
  11686. (if (featurep 'xemacs)
  11687. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11688. (org-restart-font-lock)
  11689. (setq org-table-may-need-update t)
  11690. (if org-display-custom-times
  11691. (message "Time stamps are overlayed with custom format")
  11692. (message "Time stamp overlays removed")))
  11693. (defun org-display-custom-time (beg end)
  11694. "Overlay modified time stamp format over timestamp between BEG and END."
  11695. (let* ((ts (buffer-substring beg end))
  11696. t1 w1 with-hm tf time str w2 (off 0))
  11697. (save-match-data
  11698. (setq t1 (org-parse-time-string ts t))
  11699. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11700. (setq off (- (match-end 0) (match-beginning 0)))))
  11701. (setq end (- end off))
  11702. (setq w1 (- end beg)
  11703. with-hm (and (nth 1 t1) (nth 2 t1))
  11704. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11705. time (org-fix-decoded-time t1)
  11706. str (org-add-props
  11707. (format-time-string
  11708. (substring tf 1 -1) (apply 'encode-time time))
  11709. nil 'mouse-face 'highlight)
  11710. w2 (length str))
  11711. (if (not (= w2 w1))
  11712. (add-text-properties (1+ beg) (+ 2 beg)
  11713. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11714. (if (featurep 'xemacs)
  11715. (progn
  11716. (put-text-property beg end 'invisible t)
  11717. (put-text-property beg end 'end-glyph (make-glyph str)))
  11718. (put-text-property beg end 'display str))))
  11719. (defun org-translate-time (string)
  11720. "Translate all timestamps in STRING to custom format.
  11721. But do this only if the variable `org-display-custom-times' is set."
  11722. (when org-display-custom-times
  11723. (save-match-data
  11724. (let* ((start 0)
  11725. (re org-ts-regexp-both)
  11726. t1 with-hm inactive tf time str beg end)
  11727. (while (setq start (string-match re string start))
  11728. (setq beg (match-beginning 0)
  11729. end (match-end 0)
  11730. t1 (save-match-data
  11731. (org-parse-time-string (substring string beg end) t))
  11732. with-hm (and (nth 1 t1) (nth 2 t1))
  11733. inactive (equal (substring string beg (1+ beg)) "[")
  11734. tf (funcall (if with-hm 'cdr 'car)
  11735. org-time-stamp-custom-formats)
  11736. time (org-fix-decoded-time t1)
  11737. str (format-time-string
  11738. (concat
  11739. (if inactive "[" "<") (substring tf 1 -1)
  11740. (if inactive "]" ">"))
  11741. (apply 'encode-time time))
  11742. string (replace-match str t t string)
  11743. start (+ start (length str)))))))
  11744. string)
  11745. (defun org-fix-decoded-time (time)
  11746. "Set 0 instead of nil for the first 6 elements of time.
  11747. Don't touch the rest."
  11748. (let ((n 0))
  11749. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11750. (defun org-days-to-time (timestamp-string)
  11751. "Difference between TIMESTAMP-STRING and now in days."
  11752. (- (time-to-days (org-time-string-to-time timestamp-string))
  11753. (time-to-days (current-time))))
  11754. (defun org-deadline-close (timestamp-string &optional ndays)
  11755. "Is the time in TIMESTAMP-STRING close to the current date?"
  11756. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11757. (and (< (org-days-to-time timestamp-string) ndays)
  11758. (not (org-entry-is-done-p))))
  11759. (defun org-get-wdays (ts)
  11760. "Get the deadline lead time appropriate for timestring TS."
  11761. (cond
  11762. ((<= org-deadline-warning-days 0)
  11763. ;; 0 or negative, enforce this value no matter what
  11764. (- org-deadline-warning-days))
  11765. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11766. ;; lead time is specified.
  11767. (floor (* (string-to-number (match-string 1 ts))
  11768. (cdr (assoc (match-string 2 ts)
  11769. '(("d" . 1) ("w" . 7)
  11770. ("m" . 30.4) ("y" . 365.25)))))))
  11771. ;; go for the default.
  11772. (t org-deadline-warning-days)))
  11773. (defun org-calendar-select-mouse (ev)
  11774. "Return to `org-read-date' with the date currently selected.
  11775. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11776. (interactive "e")
  11777. (mouse-set-point ev)
  11778. (when (calendar-cursor-to-date)
  11779. (let* ((date (calendar-cursor-to-date))
  11780. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11781. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11782. (if (active-minibuffer-window) (exit-minibuffer))))
  11783. (defun org-check-deadlines (ndays)
  11784. "Check if there are any deadlines due or past due.
  11785. A deadline is considered due if it happens within `org-deadline-warning-days'
  11786. days from today's date. If the deadline appears in an entry marked DONE,
  11787. it is not shown. The prefix arg NDAYS can be used to test that many
  11788. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11789. (interactive "P")
  11790. (let* ((org-warn-days
  11791. (cond
  11792. ((equal ndays '(4)) 100000)
  11793. (ndays (prefix-numeric-value ndays))
  11794. (t (abs org-deadline-warning-days))))
  11795. (case-fold-search nil)
  11796. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11797. (callback
  11798. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11799. (message "%d deadlines past-due or due within %d days"
  11800. (org-occur regexp nil callback)
  11801. org-warn-days)))
  11802. (defun org-check-before-date (date)
  11803. "Check if there are deadlines or scheduled entries before DATE."
  11804. (interactive (list (org-read-date)))
  11805. (let ((case-fold-search nil)
  11806. (regexp (concat "\\<\\(" org-deadline-string
  11807. "\\|" org-scheduled-string
  11808. "\\) *<\\([^>]+\\)>"))
  11809. (callback
  11810. (lambda () (time-less-p
  11811. (org-time-string-to-time (match-string 2))
  11812. (org-time-string-to-time date)))))
  11813. (message "%d entries before %s"
  11814. (org-occur regexp nil callback) date)))
  11815. (defun org-check-after-date (date)
  11816. "Check if there are deadlines or scheduled entries after DATE."
  11817. (interactive (list (org-read-date)))
  11818. (let ((case-fold-search nil)
  11819. (regexp (concat "\\<\\(" org-deadline-string
  11820. "\\|" org-scheduled-string
  11821. "\\) *<\\([^>]+\\)>"))
  11822. (callback
  11823. (lambda () (not
  11824. (time-less-p
  11825. (org-time-string-to-time (match-string 2))
  11826. (org-time-string-to-time date))))))
  11827. (message "%d entries after %s"
  11828. (org-occur regexp nil callback) date)))
  11829. (defun org-evaluate-time-range (&optional to-buffer)
  11830. "Evaluate a time range by computing the difference between start and end.
  11831. Normally the result is just printed in the echo area, but with prefix arg
  11832. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11833. If the time range is actually in a table, the result is inserted into the
  11834. next column.
  11835. For time difference computation, a year is assumed to be exactly 365
  11836. days in order to avoid rounding problems."
  11837. (interactive "P")
  11838. (or
  11839. (org-clock-update-time-maybe)
  11840. (save-excursion
  11841. (unless (org-at-date-range-p t)
  11842. (goto-char (point-at-bol))
  11843. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11844. (if (not (org-at-date-range-p t))
  11845. (error "Not at a time-stamp range, and none found in current line")))
  11846. (let* ((ts1 (match-string 1))
  11847. (ts2 (match-string 2))
  11848. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11849. (match-end (match-end 0))
  11850. (time1 (org-time-string-to-time ts1))
  11851. (time2 (org-time-string-to-time ts2))
  11852. (t1 (time-to-seconds time1))
  11853. (t2 (time-to-seconds time2))
  11854. (diff (abs (- t2 t1)))
  11855. (negative (< (- t2 t1) 0))
  11856. ;; (ys (floor (* 365 24 60 60)))
  11857. (ds (* 24 60 60))
  11858. (hs (* 60 60))
  11859. (fy "%dy %dd %02d:%02d")
  11860. (fy1 "%dy %dd")
  11861. (fd "%dd %02d:%02d")
  11862. (fd1 "%dd")
  11863. (fh "%02d:%02d")
  11864. y d h m align)
  11865. (if havetime
  11866. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11867. y 0
  11868. d (floor (/ diff ds)) diff (mod diff ds)
  11869. h (floor (/ diff hs)) diff (mod diff hs)
  11870. m (floor (/ diff 60)))
  11871. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11872. y 0
  11873. d (floor (+ (/ diff ds) 0.5))
  11874. h 0 m 0))
  11875. (if (not to-buffer)
  11876. (message "%s" (org-make-tdiff-string y d h m))
  11877. (if (org-at-table-p)
  11878. (progn
  11879. (goto-char match-end)
  11880. (setq align t)
  11881. (and (looking-at " *|") (goto-char (match-end 0))))
  11882. (goto-char match-end))
  11883. (if (looking-at
  11884. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11885. (replace-match ""))
  11886. (if negative (insert " -"))
  11887. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11888. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11889. (insert " " (format fh h m))))
  11890. (if align (org-table-align))
  11891. (message "Time difference inserted")))))
  11892. (defun org-make-tdiff-string (y d h m)
  11893. (let ((fmt "")
  11894. (l nil))
  11895. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11896. l (push y l)))
  11897. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11898. l (push d l)))
  11899. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11900. l (push h l)))
  11901. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11902. l (push m l)))
  11903. (apply 'format fmt (nreverse l))))
  11904. (defun org-time-string-to-time (s)
  11905. (apply 'encode-time (org-parse-time-string s)))
  11906. (defun org-time-string-to-seconds (s)
  11907. (time-to-seconds (org-time-string-to-time s)))
  11908. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11909. "Convert a time stamp to an absolute day number.
  11910. If there is a specifyer for a cyclic time stamp, get the closest date to
  11911. DAYNR.
  11912. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11913. (cond
  11914. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11915. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11916. daynr
  11917. (+ daynr 1000)))
  11918. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11919. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11920. (time-to-days (current-time))) (match-string 0 s)
  11921. prefer show-all))
  11922. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11923. (defun org-days-to-iso-week (days)
  11924. "Return the iso week number."
  11925. (require 'cal-iso)
  11926. (car (calendar-iso-from-absolute days)))
  11927. (defun org-small-year-to-year (year)
  11928. "Convert 2-digit years into 4-digit years.
  11929. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11930. The year 2000 cannot be abbreviated. Any year larger than 99
  11931. is returned unchanged."
  11932. (if (< year 38)
  11933. (setq year (+ 2000 year))
  11934. (if (< year 100)
  11935. (setq year (+ 1900 year))))
  11936. year)
  11937. (defun org-time-from-absolute (d)
  11938. "Return the time corresponding to date D.
  11939. D may be an absolute day number, or a calendar-type list (month day year)."
  11940. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11941. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11942. (defun org-calendar-holiday ()
  11943. "List of holidays, for Diary display in Org-mode."
  11944. (require 'holidays)
  11945. (let ((hl (funcall
  11946. (if (fboundp 'calendar-check-holidays)
  11947. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11948. (if hl (mapconcat 'identity hl "; "))))
  11949. (defun org-diary-sexp-entry (sexp entry date)
  11950. "Process a SEXP diary ENTRY for DATE."
  11951. (require 'diary-lib)
  11952. (let ((result (if calendar-debug-sexp
  11953. (let ((stack-trace-on-error t))
  11954. (eval (car (read-from-string sexp))))
  11955. (condition-case nil
  11956. (eval (car (read-from-string sexp)))
  11957. (error
  11958. (beep)
  11959. (message "Bad sexp at line %d in %s: %s"
  11960. (org-current-line)
  11961. (buffer-file-name) sexp)
  11962. (sleep-for 2))))))
  11963. (cond ((stringp result) result)
  11964. ((and (consp result)
  11965. (stringp (cdr result))) (cdr result))
  11966. (result entry)
  11967. (t nil))))
  11968. (defun org-diary-to-ical-string (frombuf)
  11969. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11970. This uses the icalendar.el library."
  11971. (let* ((tmpdir (if (featurep 'xemacs)
  11972. (temp-directory)
  11973. temporary-file-directory))
  11974. (tmpfile (make-temp-name
  11975. (expand-file-name "orgics" tmpdir)))
  11976. buf rtn b e)
  11977. (save-excursion
  11978. (set-buffer frombuf)
  11979. (icalendar-export-region (point-min) (point-max) tmpfile)
  11980. (setq buf (find-buffer-visiting tmpfile))
  11981. (set-buffer buf)
  11982. (goto-char (point-min))
  11983. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11984. (setq b (match-beginning 0)))
  11985. (goto-char (point-max))
  11986. (if (re-search-backward "^END:VEVENT" nil t)
  11987. (setq e (match-end 0)))
  11988. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11989. (kill-buffer buf)
  11990. (delete-file tmpfile)
  11991. rtn))
  11992. (defun org-closest-date (start current change prefer show-all)
  11993. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11994. When PREFER is `past' return a date that is either CURRENT or past.
  11995. When PREFER is `future', return a date that is either CURRENT or future.
  11996. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11997. ;; Make the proper lists from the dates
  11998. (catch 'exit
  11999. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12000. dn dw sday cday n1 n2 n0
  12001. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12002. (setq start (org-date-to-gregorian start)
  12003. current (org-date-to-gregorian
  12004. (if show-all
  12005. current
  12006. (time-to-days (current-time))))
  12007. sday (calendar-absolute-from-gregorian start)
  12008. cday (calendar-absolute-from-gregorian current))
  12009. (if (<= cday sday) (throw 'exit sday))
  12010. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12011. (setq dn (string-to-number (match-string 1 change))
  12012. dw (cdr (assoc (match-string 2 change) a1)))
  12013. (error "Invalid change specifyer: %s" change))
  12014. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12015. (cond
  12016. ((eq dw 'day)
  12017. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12018. n2 (+ n1 dn)))
  12019. ((eq dw 'year)
  12020. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12021. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12022. (setq date1 (list m d y1)
  12023. n1 (calendar-absolute-from-gregorian date1)
  12024. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12025. n2 (calendar-absolute-from-gregorian date2)))
  12026. ((eq dw 'month)
  12027. ;; approx number of month between the two dates
  12028. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12029. ;; How often does dn fit in there?
  12030. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12031. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12032. m (+ m nm)
  12033. ny (floor (/ m 12))
  12034. y (+ y ny)
  12035. m (- m (* ny 12)))
  12036. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12037. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12038. (setq m2 (+ m dn) y2 y)
  12039. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12040. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12041. (while (<= n2 cday)
  12042. (setq n1 n2 m m2 y y2)
  12043. (setq m2 (+ m dn) y2 y)
  12044. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12045. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12046. ;; Make sure n1 is the earlier date
  12047. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12048. (if show-all
  12049. (cond
  12050. ((eq prefer 'past) n1)
  12051. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12052. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12053. (cond
  12054. ((eq prefer 'past) n1)
  12055. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12056. (t (if (= cday n1) n1 n2)))))))
  12057. (defun org-date-to-gregorian (date)
  12058. "Turn any specification of DATE into a gregorian date for the calendar."
  12059. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12060. ((and (listp date) (= (length date) 3)) date)
  12061. ((stringp date)
  12062. (setq date (org-parse-time-string date))
  12063. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12064. ((listp date)
  12065. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12066. (defun org-parse-time-string (s &optional nodefault)
  12067. "Parse the standard Org-mode time string.
  12068. This should be a lot faster than the normal `parse-time-string'.
  12069. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12070. hour and minute fields will be nil if not given."
  12071. (if (string-match org-ts-regexp0 s)
  12072. (list 0
  12073. (if (or (match-beginning 8) (not nodefault))
  12074. (string-to-number (or (match-string 8 s) "0")))
  12075. (if (or (match-beginning 7) (not nodefault))
  12076. (string-to-number (or (match-string 7 s) "0")))
  12077. (string-to-number (match-string 4 s))
  12078. (string-to-number (match-string 3 s))
  12079. (string-to-number (match-string 2 s))
  12080. nil nil nil)
  12081. (make-list 9 0)))
  12082. (defun org-timestamp-up (&optional arg)
  12083. "Increase the date item at the cursor by one.
  12084. If the cursor is on the year, change the year. If it is on the month or
  12085. the day, change that.
  12086. With prefix ARG, change by that many units."
  12087. (interactive "p")
  12088. (org-timestamp-change (prefix-numeric-value arg)))
  12089. (defun org-timestamp-down (&optional arg)
  12090. "Decrease the date item at the cursor by one.
  12091. If the cursor is on the year, change the year. If it is on the month or
  12092. the day, change that.
  12093. With prefix ARG, change by that many units."
  12094. (interactive "p")
  12095. (org-timestamp-change (- (prefix-numeric-value arg))))
  12096. (defun org-timestamp-up-day (&optional arg)
  12097. "Increase the date in the time stamp by one day.
  12098. With prefix ARG, change that many days."
  12099. (interactive "p")
  12100. (if (and (not (org-at-timestamp-p t))
  12101. (org-on-heading-p))
  12102. (org-todo 'up)
  12103. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12104. (defun org-timestamp-down-day (&optional arg)
  12105. "Decrease the date in the time stamp by one day.
  12106. With prefix ARG, change that many days."
  12107. (interactive "p")
  12108. (if (and (not (org-at-timestamp-p t))
  12109. (org-on-heading-p))
  12110. (org-todo 'down)
  12111. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12112. (defun org-at-timestamp-p (&optional inactive-ok)
  12113. "Determine if the cursor is in or at a timestamp."
  12114. (interactive)
  12115. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12116. (pos (point))
  12117. (ans (or (looking-at tsr)
  12118. (save-excursion
  12119. (skip-chars-backward "^[<\n\r\t")
  12120. (if (> (point) (point-min)) (backward-char 1))
  12121. (and (looking-at tsr)
  12122. (> (- (match-end 0) pos) -1))))))
  12123. (and ans
  12124. (boundp 'org-ts-what)
  12125. (setq org-ts-what
  12126. (cond
  12127. ((= pos (match-beginning 0)) 'bracket)
  12128. ((= pos (1- (match-end 0))) 'bracket)
  12129. ((org-pos-in-match-range pos 2) 'year)
  12130. ((org-pos-in-match-range pos 3) 'month)
  12131. ((org-pos-in-match-range pos 7) 'hour)
  12132. ((org-pos-in-match-range pos 8) 'minute)
  12133. ((or (org-pos-in-match-range pos 4)
  12134. (org-pos-in-match-range pos 5)) 'day)
  12135. ((and (> pos (or (match-end 8) (match-end 5)))
  12136. (< pos (match-end 0)))
  12137. (- pos (or (match-end 8) (match-end 5))))
  12138. (t 'day))))
  12139. ans))
  12140. (defun org-toggle-timestamp-type ()
  12141. "Toggle the type (<active> or [inactive]) of a time stamp."
  12142. (interactive)
  12143. (when (org-at-timestamp-p t)
  12144. (let ((beg (match-beginning 0)) (end (match-end 0))
  12145. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12146. (save-excursion
  12147. (goto-char beg)
  12148. (while (re-search-forward "[][<>]" end t)
  12149. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12150. t t)))
  12151. (message "Timestamp is now %sactive"
  12152. (if (equal (char-after beg) ?<) "" "in")))))
  12153. (defun org-timestamp-change (n &optional what)
  12154. "Change the date in the time stamp at point.
  12155. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12156. `year', `minute', `second'. If WHAT is not given, the cursor position
  12157. in the timestamp determines what will be changed."
  12158. (let ((pos (point))
  12159. with-hm inactive
  12160. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12161. org-ts-what
  12162. extra rem
  12163. ts time time0)
  12164. (if (not (org-at-timestamp-p t))
  12165. (error "Not at a timestamp"))
  12166. (if (and (not what) (eq org-ts-what 'bracket))
  12167. (org-toggle-timestamp-type)
  12168. (if (and (not what) (not (eq org-ts-what 'day))
  12169. org-display-custom-times
  12170. (get-text-property (point) 'display)
  12171. (not (get-text-property (1- (point)) 'display)))
  12172. (setq org-ts-what 'day))
  12173. (setq org-ts-what (or what org-ts-what)
  12174. inactive (= (char-after (match-beginning 0)) ?\[)
  12175. ts (match-string 0))
  12176. (replace-match "")
  12177. (if (string-match
  12178. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12179. ts)
  12180. (setq extra (match-string 1 ts)))
  12181. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12182. (setq with-hm t))
  12183. (setq time0 (org-parse-time-string ts))
  12184. (when (and (eq org-ts-what 'minute)
  12185. (eq current-prefix-arg nil))
  12186. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12187. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12188. (setcar (cdr time0) (+ (nth 1 time0)
  12189. (if (> n 0) (- rem) (- dm rem))))))
  12190. (setq time
  12191. (encode-time (or (car time0) 0)
  12192. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12193. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12194. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12195. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12196. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12197. (nthcdr 6 time0)))
  12198. (when (and (member org-ts-what '(hour minute))
  12199. extra
  12200. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12201. (setq extra (org-modify-ts-extra
  12202. extra
  12203. (if (eq org-ts-what 'hour) 2 5)
  12204. n dm)))
  12205. (when (integerp org-ts-what)
  12206. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12207. (if (eq what 'calendar)
  12208. (let ((cal-date (org-get-date-from-calendar)))
  12209. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12210. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12211. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12212. (setcar time0 (or (car time0) 0))
  12213. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12214. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12215. (setq time (apply 'encode-time time0))))
  12216. (setq org-last-changed-timestamp
  12217. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12218. (org-clock-update-time-maybe)
  12219. (goto-char pos)
  12220. ;; Try to recenter the calendar window, if any
  12221. (if (and org-calendar-follow-timestamp-change
  12222. (get-buffer-window "*Calendar*" t)
  12223. (memq org-ts-what '(day month year)))
  12224. (org-recenter-calendar (time-to-days time))))))
  12225. (defun org-modify-ts-extra (s pos n dm)
  12226. "Change the different parts of the lead-time and repeat fields in timestamp."
  12227. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12228. ng h m new rem)
  12229. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12230. (cond
  12231. ((or (org-pos-in-match-range pos 2)
  12232. (org-pos-in-match-range pos 3))
  12233. (setq m (string-to-number (match-string 3 s))
  12234. h (string-to-number (match-string 2 s)))
  12235. (if (org-pos-in-match-range pos 2)
  12236. (setq h (+ h n))
  12237. (setq n (* dm (org-no-warnings (signum n))))
  12238. (when (not (= 0 (setq rem (% m dm))))
  12239. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12240. (setq m (+ m n)))
  12241. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12242. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12243. (setq h (min 24 (max 0 h)))
  12244. (setq ng 1 new (format "-%02d:%02d" h m)))
  12245. ((org-pos-in-match-range pos 6)
  12246. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12247. ((org-pos-in-match-range pos 5)
  12248. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12249. ((org-pos-in-match-range pos 9)
  12250. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12251. ((org-pos-in-match-range pos 8)
  12252. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12253. (when ng
  12254. (setq s (concat
  12255. (substring s 0 (match-beginning ng))
  12256. new
  12257. (substring s (match-end ng))))))
  12258. s))
  12259. (defun org-recenter-calendar (date)
  12260. "If the calendar is visible, recenter it to DATE."
  12261. (let* ((win (selected-window))
  12262. (cwin (get-buffer-window "*Calendar*" t))
  12263. (calendar-move-hook nil))
  12264. (when cwin
  12265. (select-window cwin)
  12266. (calendar-goto-date (if (listp date) date
  12267. (calendar-gregorian-from-absolute date)))
  12268. (select-window win))))
  12269. (defun org-goto-calendar (&optional arg)
  12270. "Go to the Emacs calendar at the current date.
  12271. If there is a time stamp in the current line, go to that date.
  12272. A prefix ARG can be used to force the current date."
  12273. (interactive "P")
  12274. (let ((tsr org-ts-regexp) diff
  12275. (calendar-move-hook nil)
  12276. (calendar-view-holidays-initially-flag nil)
  12277. (view-calendar-holidays-initially nil)
  12278. (calendar-view-diary-initially-flag nil)
  12279. (view-diary-entries-initially nil))
  12280. (if (or (org-at-timestamp-p)
  12281. (save-excursion
  12282. (beginning-of-line 1)
  12283. (looking-at (concat ".*" tsr))))
  12284. (let ((d1 (time-to-days (current-time)))
  12285. (d2 (time-to-days
  12286. (org-time-string-to-time (match-string 1)))))
  12287. (setq diff (- d2 d1))))
  12288. (calendar)
  12289. (calendar-goto-today)
  12290. (if (and diff (not arg)) (calendar-forward-day diff))))
  12291. (defun org-get-date-from-calendar ()
  12292. "Return a list (month day year) of date at point in calendar."
  12293. (with-current-buffer "*Calendar*"
  12294. (save-match-data
  12295. (calendar-cursor-to-date))))
  12296. (defun org-date-from-calendar ()
  12297. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12298. If there is already a time stamp at the cursor position, update it."
  12299. (interactive)
  12300. (if (org-at-timestamp-p t)
  12301. (org-timestamp-change 0 'calendar)
  12302. (let ((cal-date (org-get-date-from-calendar)))
  12303. (org-insert-time-stamp
  12304. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12305. (defun org-minutes-to-hh:mm-string (m)
  12306. "Compute H:MM from a number of minutes."
  12307. (let ((h (/ m 60)))
  12308. (setq m (- m (* 60 h)))
  12309. (format org-time-clocksum-format h m)))
  12310. (defun org-hh:mm-string-to-minutes (s)
  12311. "Convert a string H:MM to a number of minutes."
  12312. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12313. (+ (* (string-to-number (match-string 1 s)) 60)
  12314. (string-to-number (match-string 2 s)))
  12315. 0))
  12316. ;;;; Files
  12317. (defun org-save-all-org-buffers ()
  12318. "Save all Org-mode buffers without user confirmation."
  12319. (interactive)
  12320. (message "Saving all Org-mode buffers...")
  12321. (save-some-buffers t 'org-mode-p)
  12322. (when (featurep 'org-id) (org-id-locations-save))
  12323. (message "Saving all Org-mode buffers... done"))
  12324. (defun org-revert-all-org-buffers ()
  12325. "Revert all Org-mode buffers.
  12326. Prompt for confirmation when there are unsaved changes.
  12327. Be sure you know what you are doing before letting this function
  12328. overwrite your changes.
  12329. This function is useful in a setup where one tracks org files
  12330. with a version control system, to revert on one machine after pulling
  12331. changes from another. I believe the procedure must be like this:
  12332. 1. M-x org-save-all-org-buffers
  12333. 2. Pull changes from the other machine, resolve conflicts
  12334. 3. M-x org-revert-all-org-buffers"
  12335. (interactive)
  12336. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12337. (error "Abort"))
  12338. (save-excursion
  12339. (save-window-excursion
  12340. (mapc
  12341. (lambda (b)
  12342. (when (and (with-current-buffer b (org-mode-p))
  12343. (with-current-buffer b buffer-file-name))
  12344. (switch-to-buffer b)
  12345. (revert-buffer t 'no-confirm)))
  12346. (buffer-list))
  12347. (when (and (featurep 'org-id) org-id-track-globally)
  12348. (org-id-locations-load)))))
  12349. ;;;; Agenda files
  12350. ;;;###autoload
  12351. (defun org-iswitchb (&optional arg)
  12352. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12353. With a prefix argument, restrict available to files.
  12354. With two prefix arguments, restrict available buffers to agenda files.
  12355. Due to some yet unresolved reason, the global function
  12356. `iswitchb-mode' needs to be active for this function to work."
  12357. (interactive "P")
  12358. (require 'iswitchb)
  12359. (let ((enabled iswitchb-mode) blist)
  12360. (or enabled (iswitchb-mode 1))
  12361. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12362. ((equal arg '(16)) (org-buffer-list 'agenda))
  12363. (t (org-buffer-list))))
  12364. (unwind-protect
  12365. (let ((iswitchb-make-buflist-hook
  12366. (lambda ()
  12367. (setq iswitchb-temp-buflist
  12368. (mapcar 'buffer-name blist)))))
  12369. (switch-to-buffer
  12370. (iswitchb-read-buffer
  12371. "Switch-to: " nil t))
  12372. (or enabled (iswitchb-mode -1))))))
  12373. ;;;###autoload
  12374. (defun org-ido-switchb (&optional arg)
  12375. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12376. With a prefix argument, restrict available to files.
  12377. With two prefix arguments, restrict available buffers to agenda files."
  12378. (interactive "P")
  12379. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12380. ((equal arg '(16)) (org-buffer-list 'agenda))
  12381. (t (org-buffer-list)))))
  12382. (switch-to-buffer
  12383. (org-ido-completing-read "Org buffer: "
  12384. (mapcar 'list (mapcar 'buffer-name blist))
  12385. nil t))))
  12386. (defun org-buffer-list (&optional predicate exclude-tmp)
  12387. "Return a list of Org buffers.
  12388. PREDICATE can be `export', `files' or `agenda'.
  12389. export restrict the list to Export buffers.
  12390. files restrict the list to buffers visiting Org files.
  12391. agenda restrict the list to buffers visiting agenda files.
  12392. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12393. (let* ((bfn nil)
  12394. (agenda-files (and (eq predicate 'agenda)
  12395. (mapcar 'file-truename (org-agenda-files t))))
  12396. (filter
  12397. (cond
  12398. ((eq predicate 'files)
  12399. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12400. ((eq predicate 'export)
  12401. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12402. ((eq predicate 'agenda)
  12403. (lambda (b)
  12404. (with-current-buffer b
  12405. (and (eq major-mode 'org-mode)
  12406. (setq bfn (buffer-file-name b))
  12407. (member (file-truename bfn) agenda-files)))))
  12408. (t (lambda (b) (with-current-buffer b
  12409. (or (eq major-mode 'org-mode)
  12410. (string-match "\*Org .*Export"
  12411. (buffer-name b)))))))))
  12412. (delq nil
  12413. (mapcar
  12414. (lambda(b)
  12415. (if (and (funcall filter b)
  12416. (or (not exclude-tmp)
  12417. (not (string-match "tmp" (buffer-name b)))))
  12418. b
  12419. nil))
  12420. (buffer-list)))))
  12421. (defun org-agenda-files (&optional unrestricted archives)
  12422. "Get the list of agenda files.
  12423. Optional UNRESTRICTED means return the full list even if a restriction
  12424. is currently in place.
  12425. When ARCHIVES is t, include all archive files hat are really being
  12426. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12427. `org-agenda-archives-mode' is t."
  12428. (let ((files
  12429. (cond
  12430. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12431. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12432. ((listp org-agenda-files) org-agenda-files)
  12433. (t (error "Invalid value of `org-agenda-files'")))))
  12434. (setq files (apply 'append
  12435. (mapcar (lambda (f)
  12436. (if (file-directory-p f)
  12437. (directory-files
  12438. f t org-agenda-file-regexp)
  12439. (list f)))
  12440. files)))
  12441. (when org-agenda-skip-unavailable-files
  12442. (setq files (delq nil
  12443. (mapcar (function
  12444. (lambda (file)
  12445. (and (file-readable-p file) file)))
  12446. files))))
  12447. (when (or (eq archives t)
  12448. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12449. (setq files (org-add-archive-files files)))
  12450. files))
  12451. (defun org-edit-agenda-file-list ()
  12452. "Edit the list of agenda files.
  12453. Depending on setup, this either uses customize to edit the variable
  12454. `org-agenda-files', or it visits the file that is holding the list. In the
  12455. latter case, the buffer is set up in a way that saving it automatically kills
  12456. the buffer and restores the previous window configuration."
  12457. (interactive)
  12458. (if (stringp org-agenda-files)
  12459. (let ((cw (current-window-configuration)))
  12460. (find-file org-agenda-files)
  12461. (org-set-local 'org-window-configuration cw)
  12462. (org-add-hook 'after-save-hook
  12463. (lambda ()
  12464. (set-window-configuration
  12465. (prog1 org-window-configuration
  12466. (kill-buffer (current-buffer))))
  12467. (org-install-agenda-files-menu)
  12468. (message "New agenda file list installed"))
  12469. nil 'local)
  12470. (message "%s" (substitute-command-keys
  12471. "Edit list and finish with \\[save-buffer]")))
  12472. (customize-variable 'org-agenda-files)))
  12473. (defun org-store-new-agenda-file-list (list)
  12474. "Set new value for the agenda file list and save it correctly."
  12475. (if (stringp org-agenda-files)
  12476. (let ((f org-agenda-files) b)
  12477. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12478. (with-temp-file f
  12479. (insert (mapconcat 'identity list "\n") "\n")))
  12480. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12481. (setq org-agenda-files list)
  12482. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12483. (defun org-read-agenda-file-list ()
  12484. "Read the list of agenda files from a file."
  12485. (when (file-directory-p org-agenda-files)
  12486. (error "`org-agenda-files' cannot be a single directory"))
  12487. (when (stringp org-agenda-files)
  12488. (with-temp-buffer
  12489. (insert-file-contents org-agenda-files)
  12490. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12491. ;;;###autoload
  12492. (defun org-cycle-agenda-files ()
  12493. "Cycle through the files in `org-agenda-files'.
  12494. If the current buffer visits an agenda file, find the next one in the list.
  12495. If the current buffer does not, find the first agenda file."
  12496. (interactive)
  12497. (let* ((fs (org-agenda-files t))
  12498. (files (append fs (list (car fs))))
  12499. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12500. file)
  12501. (unless files (error "No agenda files"))
  12502. (catch 'exit
  12503. (while (setq file (pop files))
  12504. (if (equal (file-truename file) tcf)
  12505. (when (car files)
  12506. (find-file (car files))
  12507. (throw 'exit t))))
  12508. (find-file (car fs)))
  12509. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12510. (defun org-agenda-file-to-front (&optional to-end)
  12511. "Move/add the current file to the top of the agenda file list.
  12512. If the file is not present in the list, it is added to the front. If it is
  12513. present, it is moved there. With optional argument TO-END, add/move to the
  12514. end of the list."
  12515. (interactive "P")
  12516. (let ((org-agenda-skip-unavailable-files nil)
  12517. (file-alist (mapcar (lambda (x)
  12518. (cons (file-truename x) x))
  12519. (org-agenda-files t)))
  12520. (ctf (file-truename buffer-file-name))
  12521. x had)
  12522. (setq x (assoc ctf file-alist) had x)
  12523. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12524. (if to-end
  12525. (setq file-alist (append (delq x file-alist) (list x)))
  12526. (setq file-alist (cons x (delq x file-alist))))
  12527. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12528. (org-install-agenda-files-menu)
  12529. (message "File %s to %s of agenda file list"
  12530. (if had "moved" "added") (if to-end "end" "front"))))
  12531. (defun org-remove-file (&optional file)
  12532. "Remove current file from the list of files in variable `org-agenda-files'.
  12533. These are the files which are being checked for agenda entries.
  12534. Optional argument FILE means, use this file instead of the current."
  12535. (interactive)
  12536. (let* ((org-agenda-skip-unavailable-files nil)
  12537. (file (or file buffer-file-name))
  12538. (true-file (file-truename file))
  12539. (afile (abbreviate-file-name file))
  12540. (files (delq nil (mapcar
  12541. (lambda (x)
  12542. (if (equal true-file
  12543. (file-truename x))
  12544. nil x))
  12545. (org-agenda-files t)))))
  12546. (if (not (= (length files) (length (org-agenda-files t))))
  12547. (progn
  12548. (org-store-new-agenda-file-list files)
  12549. (org-install-agenda-files-menu)
  12550. (message "Removed file: %s" afile))
  12551. (message "File was not in list: %s (not removed)" afile))))
  12552. (defun org-file-menu-entry (file)
  12553. (vector file (list 'find-file file) t))
  12554. (defun org-check-agenda-file (file)
  12555. "Make sure FILE exists. If not, ask user what to do."
  12556. (when (not (file-exists-p file))
  12557. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12558. (abbreviate-file-name file))
  12559. (let ((r (downcase (read-char-exclusive))))
  12560. (cond
  12561. ((equal r ?r)
  12562. (org-remove-file file)
  12563. (throw 'nextfile t))
  12564. (t (error "Abort"))))))
  12565. (defun org-get-agenda-file-buffer (file)
  12566. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12567. it to the list of buffers which might be released later."
  12568. (let ((buf (org-find-base-buffer-visiting file)))
  12569. (if buf
  12570. buf ; just return it
  12571. ;; Make a new buffer and remember it
  12572. (setq buf (find-file-noselect file))
  12573. (if buf (push buf org-agenda-new-buffers))
  12574. buf)))
  12575. (defun org-release-buffers (blist)
  12576. "Release all buffers in list, asking the user for confirmation when needed.
  12577. When a buffer is unmodified, it is just killed. When modified, it is saved
  12578. \(if the user agrees) and then killed."
  12579. (let (buf file)
  12580. (while (setq buf (pop blist))
  12581. (setq file (buffer-file-name buf))
  12582. (when (and (buffer-modified-p buf)
  12583. file
  12584. (y-or-n-p (format "Save file %s? " file)))
  12585. (with-current-buffer buf (save-buffer)))
  12586. (kill-buffer buf))))
  12587. (defun org-prepare-agenda-buffers (files)
  12588. "Create buffers for all agenda files, protect archived trees and comments."
  12589. (interactive)
  12590. (let ((pa '(:org-archived t))
  12591. (pc '(:org-comment t))
  12592. (pall '(:org-archived t :org-comment t))
  12593. (inhibit-read-only t)
  12594. (rea (concat ":" org-archive-tag ":"))
  12595. bmp file re)
  12596. (save-excursion
  12597. (save-restriction
  12598. (while (setq file (pop files))
  12599. (catch 'nextfile
  12600. (if (bufferp file)
  12601. (set-buffer file)
  12602. (org-check-agenda-file file)
  12603. (set-buffer (org-get-agenda-file-buffer file)))
  12604. (widen)
  12605. (setq bmp (buffer-modified-p))
  12606. (org-refresh-category-properties)
  12607. (setq org-todo-keywords-for-agenda
  12608. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12609. (setq org-done-keywords-for-agenda
  12610. (append org-done-keywords-for-agenda org-done-keywords))
  12611. (setq org-todo-keyword-alist-for-agenda
  12612. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12613. (setq org-tag-alist-for-agenda
  12614. (append org-tag-alist-for-agenda org-tag-alist))
  12615. (save-excursion
  12616. (remove-text-properties (point-min) (point-max) pall)
  12617. (when org-agenda-skip-archived-trees
  12618. (goto-char (point-min))
  12619. (while (re-search-forward rea nil t)
  12620. (if (org-on-heading-p t)
  12621. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12622. (goto-char (point-min))
  12623. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12624. (while (re-search-forward re nil t)
  12625. (add-text-properties
  12626. (match-beginning 0) (org-end-of-subtree t) pc)))
  12627. (set-buffer-modified-p bmp)))))
  12628. (setq org-todo-keyword-alist-for-agenda
  12629. (org-uniquify org-todo-keyword-alist-for-agenda)
  12630. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12631. ;;;; Embedded LaTeX
  12632. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12633. "Keymap for the minor `org-cdlatex-mode'.")
  12634. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12635. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12636. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12637. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12638. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12639. (defvar org-cdlatex-texmathp-advice-is-done nil
  12640. "Flag remembering if we have applied the advice to texmathp already.")
  12641. (define-minor-mode org-cdlatex-mode
  12642. "Toggle the minor `org-cdlatex-mode'.
  12643. This mode supports entering LaTeX environment and math in LaTeX fragments
  12644. in Org-mode.
  12645. \\{org-cdlatex-mode-map}"
  12646. nil " OCDL" nil
  12647. (when org-cdlatex-mode (require 'cdlatex))
  12648. (unless org-cdlatex-texmathp-advice-is-done
  12649. (setq org-cdlatex-texmathp-advice-is-done t)
  12650. (defadvice texmathp (around org-math-always-on activate)
  12651. "Always return t in org-mode buffers.
  12652. This is because we want to insert math symbols without dollars even outside
  12653. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12654. an embedded LaTeX fragment, let texmathp do its job.
  12655. \\[org-cdlatex-mode-map]"
  12656. (interactive)
  12657. (let (p)
  12658. (cond
  12659. ((not (org-mode-p)) ad-do-it)
  12660. ((eq this-command 'cdlatex-math-symbol)
  12661. (setq ad-return-value t
  12662. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12663. (t
  12664. (let ((p (org-inside-LaTeX-fragment-p)))
  12665. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12666. (setq ad-return-value t
  12667. texmathp-why '("Org-mode embedded math" . 0))
  12668. (if p ad-do-it)))))))))
  12669. (defun turn-on-org-cdlatex ()
  12670. "Unconditionally turn on `org-cdlatex-mode'."
  12671. (org-cdlatex-mode 1))
  12672. (defun org-inside-LaTeX-fragment-p ()
  12673. "Test if point is inside a LaTeX fragment.
  12674. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12675. sequence appearing also before point.
  12676. Even though the matchers for math are configurable, this function assumes
  12677. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12678. delimiters are skipped when they have been removed by customization.
  12679. The return value is nil, or a cons cell with the delimiter and
  12680. and the position of this delimiter.
  12681. This function does a reasonably good job, but can locally be fooled by
  12682. for example currency specifications. For example it will assume being in
  12683. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12684. fragments that are properly closed, but during editing, we have to live
  12685. with the uncertainty caused by missing closing delimiters. This function
  12686. looks only before point, not after."
  12687. (catch 'exit
  12688. (let ((pos (point))
  12689. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12690. (lim (progn
  12691. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12692. (point)))
  12693. dd-on str (start 0) m re)
  12694. (goto-char pos)
  12695. (when dodollar
  12696. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12697. re (nth 1 (assoc "$" org-latex-regexps)))
  12698. (while (string-match re str start)
  12699. (cond
  12700. ((= (match-end 0) (length str))
  12701. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12702. ((= (match-end 0) (- (length str) 5))
  12703. (throw 'exit nil))
  12704. (t (setq start (match-end 0))))))
  12705. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12706. (goto-char pos)
  12707. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12708. (and (match-beginning 2) (throw 'exit nil))
  12709. ;; count $$
  12710. (while (re-search-backward "\\$\\$" lim t)
  12711. (setq dd-on (not dd-on)))
  12712. (goto-char pos)
  12713. (if dd-on (cons "$$" m))))))
  12714. (defun org-try-cdlatex-tab ()
  12715. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12716. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12717. - inside a LaTeX fragment, or
  12718. - after the first word in a line, where an abbreviation expansion could
  12719. insert a LaTeX environment."
  12720. (when org-cdlatex-mode
  12721. (cond
  12722. ((save-excursion
  12723. (skip-chars-backward "a-zA-Z0-9*")
  12724. (skip-chars-backward " \t")
  12725. (bolp))
  12726. (cdlatex-tab) t)
  12727. ((org-inside-LaTeX-fragment-p)
  12728. (cdlatex-tab) t)
  12729. (t nil))))
  12730. (defun org-cdlatex-underscore-caret (&optional arg)
  12731. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12732. Revert to the normal definition outside of these fragments."
  12733. (interactive "P")
  12734. (if (org-inside-LaTeX-fragment-p)
  12735. (call-interactively 'cdlatex-sub-superscript)
  12736. (let (org-cdlatex-mode)
  12737. (call-interactively (key-binding (vector last-input-event))))))
  12738. (defun org-cdlatex-math-modify (&optional arg)
  12739. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12740. Revert to the normal definition outside of these fragments."
  12741. (interactive "P")
  12742. (if (org-inside-LaTeX-fragment-p)
  12743. (call-interactively 'cdlatex-math-modify)
  12744. (let (org-cdlatex-mode)
  12745. (call-interactively (key-binding (vector last-input-event))))))
  12746. (defvar org-latex-fragment-image-overlays nil
  12747. "List of overlays carrying the images of latex fragments.")
  12748. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12749. (defun org-remove-latex-fragment-image-overlays ()
  12750. "Remove all overlays with LaTeX fragment images in current buffer."
  12751. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12752. (setq org-latex-fragment-image-overlays nil))
  12753. (defun org-preview-latex-fragment (&optional subtree)
  12754. "Preview the LaTeX fragment at point, or all locally or globally.
  12755. If the cursor is in a LaTeX fragment, create the image and overlay
  12756. it over the source code. If there is no fragment at point, display
  12757. all fragments in the current text, from one headline to the next. With
  12758. prefix SUBTREE, display all fragments in the current subtree. With a
  12759. double prefix `C-u C-u', or when the cursor is before the first headline,
  12760. display all fragments in the buffer.
  12761. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12762. (interactive "P")
  12763. (org-remove-latex-fragment-image-overlays)
  12764. (save-excursion
  12765. (save-restriction
  12766. (let (beg end at msg)
  12767. (cond
  12768. ((or (equal subtree '(16))
  12769. (not (save-excursion
  12770. (re-search-backward (concat "^" outline-regexp) nil t))))
  12771. (setq beg (point-min) end (point-max)
  12772. msg "Creating images for buffer...%s"))
  12773. ((equal subtree '(4))
  12774. (org-back-to-heading)
  12775. (setq beg (point) end (org-end-of-subtree t)
  12776. msg "Creating images for subtree...%s"))
  12777. (t
  12778. (if (setq at (org-inside-LaTeX-fragment-p))
  12779. (goto-char (max (point-min) (- (cdr at) 2)))
  12780. (org-back-to-heading))
  12781. (setq beg (point) end (progn (outline-next-heading) (point))
  12782. msg (if at "Creating image...%s"
  12783. "Creating images for entry...%s"))))
  12784. (message msg "")
  12785. (narrow-to-region beg end)
  12786. (goto-char beg)
  12787. (org-format-latex
  12788. (concat "ltxpng/" (file-name-sans-extension
  12789. (file-name-nondirectory
  12790. buffer-file-name)))
  12791. default-directory 'overlays msg at 'forbuffer)
  12792. (message msg "done. Use `C-c C-c' to remove images.")))))
  12793. (defvar org-latex-regexps
  12794. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12795. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12796. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12797. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12798. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12799. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12800. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12801. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12802. "Regular expressions for matching embedded LaTeX.")
  12803. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12804. "Replace LaTeX fragments with links to an image, and produce images."
  12805. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12806. (let* ((prefixnodir (file-name-nondirectory prefix))
  12807. (absprefix (expand-file-name prefix dir))
  12808. (todir (file-name-directory absprefix))
  12809. (opt org-format-latex-options)
  12810. (matchers (plist-get opt :matchers))
  12811. (re-list org-latex-regexps)
  12812. (cnt 0) txt link beg end re e checkdir
  12813. executables-checked
  12814. m n block linkfile movefile ov)
  12815. ;; Check if there are old images files with this prefix, and remove them
  12816. (when (file-directory-p todir)
  12817. (mapc 'delete-file
  12818. (directory-files
  12819. todir 'full
  12820. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12821. ;; Check the different regular expressions
  12822. (while (setq e (pop re-list))
  12823. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12824. block (if (nth 3 e) "\n\n" ""))
  12825. (when (member m matchers)
  12826. (goto-char (point-min))
  12827. (while (re-search-forward re nil t)
  12828. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12829. (not (get-text-property (match-beginning n)
  12830. 'org-protected)))
  12831. (setq txt (match-string n)
  12832. beg (match-beginning n) end (match-end n)
  12833. cnt (1+ cnt)
  12834. linkfile (format "%s_%04d.png" prefix cnt)
  12835. movefile (format "%s_%04d.png" absprefix cnt)
  12836. link (concat block "[[file:" linkfile "]]" block))
  12837. (if msg (message msg cnt))
  12838. (goto-char beg)
  12839. (unless checkdir ; make sure the directory exists
  12840. (setq checkdir t)
  12841. (or (file-directory-p todir) (make-directory todir)))
  12842. (unless executables-checked
  12843. (org-check-external-command
  12844. "latex" "needed to convert LaTeX fragments to images")
  12845. (org-check-external-command
  12846. "dvipng" "needed to convert LaTeX fragments to images")
  12847. (setq executables-checked t))
  12848. (org-create-formula-image
  12849. txt movefile opt forbuffer)
  12850. (if overlays
  12851. (progn
  12852. (setq ov (org-make-overlay beg end))
  12853. (if (featurep 'xemacs)
  12854. (progn
  12855. (org-overlay-put ov 'invisible t)
  12856. (org-overlay-put
  12857. ov 'end-glyph
  12858. (make-glyph (vector 'png :file movefile))))
  12859. (org-overlay-put
  12860. ov 'display
  12861. (list 'image :type 'png :file movefile :ascent 'center)))
  12862. (push ov org-latex-fragment-image-overlays)
  12863. (goto-char end))
  12864. (delete-region beg end)
  12865. (insert link))))))))
  12866. ;; This function borrows from Ganesh Swami's latex2png.el
  12867. (defun org-create-formula-image (string tofile options buffer)
  12868. (let* ((tmpdir (if (featurep 'xemacs)
  12869. (temp-directory)
  12870. temporary-file-directory))
  12871. (texfilebase (make-temp-name
  12872. (expand-file-name "orgtex" tmpdir)))
  12873. (texfile (concat texfilebase ".tex"))
  12874. (dvifile (concat texfilebase ".dvi"))
  12875. (pngfile (concat texfilebase ".png"))
  12876. (fnh (if (featurep 'xemacs)
  12877. (font-height (get-face-font 'default))
  12878. (face-attribute 'default :height nil)))
  12879. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12880. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12881. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12882. "Black"))
  12883. (bg (or (plist-get options (if buffer :background :html-background))
  12884. "Transparent")))
  12885. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12886. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12887. (with-temp-file texfile
  12888. (insert org-format-latex-header
  12889. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12890. (let ((dir default-directory))
  12891. (condition-case nil
  12892. (progn
  12893. (cd tmpdir)
  12894. (call-process "latex" nil nil nil texfile))
  12895. (error nil))
  12896. (cd dir))
  12897. (if (not (file-exists-p dvifile))
  12898. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12899. (condition-case nil
  12900. (call-process "dvipng" nil nil nil
  12901. "-E" "-fg" fg "-bg" bg
  12902. "-D" dpi
  12903. ;;"-x" scale "-y" scale
  12904. "-T" "tight"
  12905. "-o" pngfile
  12906. dvifile)
  12907. (error nil))
  12908. (if (not (file-exists-p pngfile))
  12909. (progn (message "Failed to create png file from %s" texfile) nil)
  12910. ;; Use the requested file name and clean up
  12911. (copy-file pngfile tofile 'replace)
  12912. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12913. (delete-file (concat texfilebase e)))
  12914. pngfile))))
  12915. (defun org-dvipng-color (attr)
  12916. "Return an rgb color specification for dvipng."
  12917. (apply 'format "rgb %s %s %s"
  12918. (mapcar 'org-normalize-color
  12919. (color-values (face-attribute 'default attr nil)))))
  12920. (defun org-normalize-color (value)
  12921. "Return string to be used as color value for an RGB component."
  12922. (format "%g" (/ value 65535.0)))
  12923. ;;;; Key bindings
  12924. ;; Make `C-c C-x' a prefix key
  12925. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12926. ;; TAB key with modifiers
  12927. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12928. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12929. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12930. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12931. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12932. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12933. ;; The following line is necessary under Suse GNU/Linux
  12934. (unless (featurep 'xemacs)
  12935. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12936. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12937. (define-key org-mode-map [backtab] 'org-shifttab)
  12938. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12939. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12940. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12941. ;; Cursor keys with modifiers
  12942. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12943. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12944. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12945. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12946. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12947. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12948. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12949. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12950. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12951. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12952. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12953. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12954. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12955. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12956. ;;; Extra keys for tty access.
  12957. ;; We only set them when really needed because otherwise the
  12958. ;; menus don't show the simple keys
  12959. (when (or org-use-extra-keys
  12960. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12961. (not window-system))
  12962. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12963. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12964. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12965. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12966. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12967. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12968. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12969. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12970. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12971. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12972. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12973. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12974. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12975. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12976. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12977. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12978. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12979. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12980. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12981. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12982. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12983. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12984. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12985. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12986. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12987. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12988. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12989. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12990. ;; All the other keys
  12991. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12992. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12993. (if (boundp 'narrow-map)
  12994. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12995. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12996. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12997. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12998. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12999. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13000. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13001. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13002. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13003. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13004. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13005. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13006. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13007. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13008. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13009. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13010. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13011. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13012. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13013. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13014. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13015. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13016. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13017. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13018. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13019. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13020. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13021. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13022. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13023. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13024. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13025. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13026. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13027. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13028. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13029. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13030. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13031. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13032. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13033. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13034. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13035. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13036. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13037. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13038. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13039. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13040. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13041. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13042. (org-defkey org-mode-map "\C-m" 'org-return)
  13043. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13044. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13045. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13046. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13047. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13048. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13049. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13050. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13051. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13052. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13053. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13054. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13055. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13056. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13057. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13058. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13059. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13060. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13061. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13062. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13063. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13064. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13065. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13066. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13067. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13068. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13069. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13070. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13071. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13072. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13073. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13074. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13075. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13076. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13077. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13078. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13079. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13080. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13081. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13082. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13083. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13084. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13085. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13086. (when (featurep 'xemacs)
  13087. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13088. (defvar org-self-insert-command-undo-counter 0)
  13089. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13090. (defun org-self-insert-command (N)
  13091. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13092. If the cursor is in a table looking at whitespace, the whitespace is
  13093. overwritten, and the table is not marked as requiring realignment."
  13094. (interactive "p")
  13095. (if (and
  13096. (org-table-p)
  13097. (progn
  13098. ;; check if we blank the field, and if that triggers align
  13099. (and (featurep 'org-table) org-table-auto-blank-field
  13100. (member last-command
  13101. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  13102. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13103. ;; got extra space, this field does not determine column width
  13104. (let (org-table-may-need-update) (org-table-blank-field))
  13105. ;; no extra space, this field may determine column width
  13106. (org-table-blank-field)))
  13107. t)
  13108. (eq N 1)
  13109. (looking-at "[^|\n]* |"))
  13110. (let (org-table-may-need-update)
  13111. (goto-char (1- (match-end 0)))
  13112. (delete-backward-char 1)
  13113. (goto-char (match-beginning 0))
  13114. (self-insert-command N))
  13115. (setq org-table-may-need-update t)
  13116. (self-insert-command N)
  13117. (org-fix-tags-on-the-fly)
  13118. (if org-self-insert-cluster-for-undo
  13119. (if (not (eq last-command 'org-self-insert-command))
  13120. (setq org-self-insert-command-undo-counter 1)
  13121. (if (>= org-self-insert-command-undo-counter 20)
  13122. (setq org-self-insert-command-undo-counter 1)
  13123. (and (> org-self-insert-command-undo-counter 0)
  13124. buffer-undo-list
  13125. (not (cadr buffer-undo-list)) ; remove nil entry
  13126. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13127. (setq org-self-insert-command-undo-counter
  13128. (1+ org-self-insert-command-undo-counter)))))))
  13129. (defun org-fix-tags-on-the-fly ()
  13130. (when (and (equal (char-after (point-at-bol)) ?*)
  13131. (org-on-heading-p))
  13132. (org-align-tags-here org-tags-column)))
  13133. (defun org-delete-backward-char (N)
  13134. "Like `delete-backward-char', insert whitespace at field end in tables.
  13135. When deleting backwards, in tables this function will insert whitespace in
  13136. front of the next \"|\" separator, to keep the table aligned. The table will
  13137. still be marked for re-alignment if the field did fill the entire column,
  13138. because, in this case the deletion might narrow the column."
  13139. (interactive "p")
  13140. (if (and (org-table-p)
  13141. (eq N 1)
  13142. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13143. (looking-at ".*?|"))
  13144. (let ((pos (point))
  13145. (noalign (looking-at "[^|\n\r]* |"))
  13146. (c org-table-may-need-update))
  13147. (backward-delete-char N)
  13148. (skip-chars-forward "^|")
  13149. (insert " ")
  13150. (goto-char (1- pos))
  13151. ;; noalign: if there were two spaces at the end, this field
  13152. ;; does not determine the width of the column.
  13153. (if noalign (setq org-table-may-need-update c)))
  13154. (backward-delete-char N)
  13155. (org-fix-tags-on-the-fly)))
  13156. (defun org-delete-char (N)
  13157. "Like `delete-char', but insert whitespace at field end in tables.
  13158. When deleting characters, in tables this function will insert whitespace in
  13159. front of the next \"|\" separator, to keep the table aligned. The table will
  13160. still be marked for re-alignment if the field did fill the entire column,
  13161. because, in this case the deletion might narrow the column."
  13162. (interactive "p")
  13163. (if (and (org-table-p)
  13164. (not (bolp))
  13165. (not (= (char-after) ?|))
  13166. (eq N 1))
  13167. (if (looking-at ".*?|")
  13168. (let ((pos (point))
  13169. (noalign (looking-at "[^|\n\r]* |"))
  13170. (c org-table-may-need-update))
  13171. (replace-match (concat
  13172. (substring (match-string 0) 1 -1)
  13173. " |"))
  13174. (goto-char pos)
  13175. ;; noalign: if there were two spaces at the end, this field
  13176. ;; does not determine the width of the column.
  13177. (if noalign (setq org-table-may-need-update c)))
  13178. (delete-char N))
  13179. (delete-char N)
  13180. (org-fix-tags-on-the-fly)))
  13181. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13182. (put 'org-self-insert-command 'delete-selection t)
  13183. (put 'orgtbl-self-insert-command 'delete-selection t)
  13184. (put 'org-delete-char 'delete-selection 'supersede)
  13185. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13186. (put 'org-yank 'delete-selection 'yank)
  13187. ;; Make `flyspell-mode' delay after some commands
  13188. (put 'org-self-insert-command 'flyspell-delayed t)
  13189. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13190. (put 'org-delete-char 'flyspell-delayed t)
  13191. (put 'org-delete-backward-char 'flyspell-delayed t)
  13192. ;; Make pabbrev-mode expand after org-mode commands
  13193. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13194. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13195. ;; How to do this: Measure non-white length of current string
  13196. ;; If equal to column width, we should realign.
  13197. (defun org-remap (map &rest commands)
  13198. "In MAP, remap the functions given in COMMANDS.
  13199. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13200. (let (new old)
  13201. (while commands
  13202. (setq old (pop commands) new (pop commands))
  13203. (if (fboundp 'command-remapping)
  13204. (org-defkey map (vector 'remap old) new)
  13205. (substitute-key-definition old new map global-map)))))
  13206. (when (eq org-enable-table-editor 'optimized)
  13207. ;; If the user wants maximum table support, we need to hijack
  13208. ;; some standard editing functions
  13209. (org-remap org-mode-map
  13210. 'self-insert-command 'org-self-insert-command
  13211. 'delete-char 'org-delete-char
  13212. 'delete-backward-char 'org-delete-backward-char)
  13213. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13214. (defvar org-ctrl-c-ctrl-c-hook nil
  13215. "Hook for functions attaching themselves to `C-c C-c'.
  13216. This can be used to add additional functionality to the C-c C-c key which
  13217. executes context-dependent commands.
  13218. Each function will be called with no arguments. The function must check
  13219. if the context is appropriate for it to act. If yes, it should do its
  13220. thing and then return a non-nil value. If the context is wrong,
  13221. just do nothing and return nil.")
  13222. (defvar org-tab-first-hook nil
  13223. "Hook for functions to attach themselves to TAB.
  13224. See `org-ctrl-c-ctrl-c-hook' for more information.
  13225. This hook runs as the first action when TAB is pressed, even before
  13226. `org-cycle' messes around with the `outline-regexp' to cater for
  13227. inline tasks and plain list item folding.
  13228. If any function in this hook returns t, not other actions like table
  13229. field motion visibility cycling will be done.")
  13230. (defvar org-tab-after-check-for-table-hook nil
  13231. "Hook for functions to attach themselves to TAB.
  13232. See `org-ctrl-c-ctrl-c-hook' for more information.
  13233. This hook runs after it has been established that the cursor is not in a
  13234. table, but before checking if the cursor is in a headline or if global cycling
  13235. should be done.
  13236. If any function in this hook returns t, not other actions like visibility
  13237. cycling will be done.")
  13238. (defvar org-tab-after-check-for-cycling-hook nil
  13239. "Hook for functions to attach themselves to TAB.
  13240. See `org-ctrl-c-ctrl-c-hook' for more information.
  13241. This hook runs after it has been established that not table field motion and
  13242. not visibility should be done because of current context. This is probably
  13243. the place where a package like yasnippets can hook in.")
  13244. (defvar org-metaleft-hook nil
  13245. "Hook for functions attaching themselves to `M-left'.
  13246. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13247. (defvar org-metaright-hook nil
  13248. "Hook for functions attaching themselves to `M-right'.
  13249. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13250. (defvar org-metaup-hook nil
  13251. "Hook for functions attaching themselves to `M-up'.
  13252. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13253. (defvar org-metadown-hook nil
  13254. "Hook for functions attaching themselves to `M-down'.
  13255. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13256. (defvar org-shiftmetaleft-hook nil
  13257. "Hook for functions attaching themselves to `M-S-left'.
  13258. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13259. (defvar org-shiftmetaright-hook nil
  13260. "Hook for functions attaching themselves to `M-S-right'.
  13261. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13262. (defvar org-shiftmetaup-hook nil
  13263. "Hook for functions attaching themselves to `M-S-up'.
  13264. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13265. (defvar org-shiftmetadown-hook nil
  13266. "Hook for functions attaching themselves to `M-S-down'.
  13267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13268. (defvar org-metareturn-hook nil
  13269. "Hook for functions attaching themselves to `M-RET'.
  13270. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13271. (defun org-modifier-cursor-error ()
  13272. "Throw an error, a modified cursor command was applied in wrong context."
  13273. (error "This command is active in special context like tables, headlines or items"))
  13274. (defun org-shiftselect-error ()
  13275. "Throw an error because Shift-Cursor command was applied in wrong context."
  13276. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13277. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13278. (error "This command works only in special context like headlines or timestamps.")))
  13279. (defun org-call-for-shift-select (cmd)
  13280. (let ((this-command-keys-shift-translated t))
  13281. (call-interactively cmd)))
  13282. (defun org-shifttab (&optional arg)
  13283. "Global visibility cycling or move to previous table field.
  13284. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13285. on context.
  13286. See the individual commands for more information."
  13287. (interactive "P")
  13288. (cond
  13289. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13290. ((integerp arg)
  13291. (message "Content view to level: %d" arg)
  13292. (org-content (prefix-numeric-value arg))
  13293. (setq org-cycle-global-status 'overview))
  13294. (t (call-interactively 'org-global-cycle))))
  13295. (defun org-shiftmetaleft ()
  13296. "Promote subtree or delete table column.
  13297. Calls `org-promote-subtree', `org-outdent-item',
  13298. or `org-table-delete-column', depending on context.
  13299. See the individual commands for more information."
  13300. (interactive)
  13301. (cond
  13302. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13303. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13304. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13305. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13306. (t (org-modifier-cursor-error))))
  13307. (defun org-shiftmetaright ()
  13308. "Demote subtree or insert table column.
  13309. Calls `org-demote-subtree', `org-indent-item',
  13310. or `org-table-insert-column', depending on context.
  13311. See the individual commands for more information."
  13312. (interactive)
  13313. (cond
  13314. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13315. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13316. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13317. ((org-at-item-p) (call-interactively 'org-indent-item))
  13318. (t (org-modifier-cursor-error))))
  13319. (defun org-shiftmetaup (&optional arg)
  13320. "Move subtree up or kill table row.
  13321. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13322. `org-move-item-up' depending on context. See the individual commands
  13323. for more information."
  13324. (interactive "P")
  13325. (cond
  13326. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13327. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13328. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13329. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13330. (t (org-modifier-cursor-error))))
  13331. (defun org-shiftmetadown (&optional arg)
  13332. "Move subtree down or insert table row.
  13333. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13334. `org-move-item-down', depending on context. See the individual
  13335. commands for more information."
  13336. (interactive "P")
  13337. (cond
  13338. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13339. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13340. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13341. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13342. (t (org-modifier-cursor-error))))
  13343. (defun org-metaleft (&optional arg)
  13344. "Promote heading or move table column to left.
  13345. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13346. With no specific context, calls the Emacs default `backward-word'.
  13347. See the individual commands for more information."
  13348. (interactive "P")
  13349. (cond
  13350. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13351. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13352. ((or (org-on-heading-p)
  13353. (and (org-region-active-p)
  13354. (save-excursion
  13355. (goto-char (region-beginning))
  13356. (org-on-heading-p))))
  13357. (call-interactively 'org-do-promote))
  13358. ((or (org-at-item-p)
  13359. (and (org-region-active-p)
  13360. (save-excursion
  13361. (goto-char (region-beginning))
  13362. (org-at-item-p))))
  13363. (call-interactively 'org-outdent-item))
  13364. (t (call-interactively 'backward-word))))
  13365. (defun org-metaright (&optional arg)
  13366. "Demote subtree or move table column to right.
  13367. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13368. With no specific context, calls the Emacs default `forward-word'.
  13369. See the individual commands for more information."
  13370. (interactive "P")
  13371. (cond
  13372. ((run-hook-with-args-until-success 'org-metaright-hook))
  13373. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13374. ((or (org-on-heading-p)
  13375. (and (org-region-active-p)
  13376. (save-excursion
  13377. (goto-char (region-beginning))
  13378. (org-on-heading-p))))
  13379. (call-interactively 'org-do-demote))
  13380. ((or (org-at-item-p)
  13381. (and (org-region-active-p)
  13382. (save-excursion
  13383. (goto-char (region-beginning))
  13384. (org-at-item-p))))
  13385. (call-interactively 'org-indent-item))
  13386. (t (call-interactively 'forward-word))))
  13387. (defun org-metaup (&optional arg)
  13388. "Move subtree up or move table row up.
  13389. Calls `org-move-subtree-up' or `org-table-move-row' or
  13390. `org-move-item-up', depending on context. See the individual commands
  13391. for more information."
  13392. (interactive "P")
  13393. (cond
  13394. ((run-hook-with-args-until-success 'org-metaup-hook))
  13395. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13396. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13397. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13398. (t (transpose-lines 1) (beginning-of-line -1))))
  13399. (defun org-metadown (&optional arg)
  13400. "Move subtree down or move table row down.
  13401. Calls `org-move-subtree-down' or `org-table-move-row' or
  13402. `org-move-item-down', depending on context. See the individual
  13403. commands for more information."
  13404. (interactive "P")
  13405. (cond
  13406. ((run-hook-with-args-until-success 'org-metadown-hook))
  13407. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13408. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13409. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13410. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13411. (defun org-shiftup (&optional arg)
  13412. "Increase item in timestamp or increase priority of current headline.
  13413. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13414. depending on context. See the individual commands for more information."
  13415. (interactive "P")
  13416. (cond
  13417. ((and org-support-shift-select (org-region-active-p))
  13418. (org-call-for-shift-select 'previous-line))
  13419. ((org-at-timestamp-p t)
  13420. (call-interactively (if org-edit-timestamp-down-means-later
  13421. 'org-timestamp-down 'org-timestamp-up)))
  13422. ((and (not (eq org-support-shift-select 'always))
  13423. org-enable-priority-commands
  13424. (org-on-heading-p))
  13425. (call-interactively 'org-priority-up))
  13426. ((and (not org-support-shift-select) (org-at-item-p))
  13427. (call-interactively 'org-previous-item))
  13428. ((org-clocktable-try-shift 'up arg))
  13429. (org-support-shift-select
  13430. (org-call-for-shift-select 'previous-line))
  13431. (t (org-shiftselect-error))))
  13432. (defun org-shiftdown (&optional arg)
  13433. "Decrease item in timestamp or decrease priority of current headline.
  13434. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13435. depending on context. See the individual commands for more information."
  13436. (interactive "P")
  13437. (cond
  13438. ((and org-support-shift-select (org-region-active-p))
  13439. (org-call-for-shift-select 'next-line))
  13440. ((org-at-timestamp-p t)
  13441. (call-interactively (if org-edit-timestamp-down-means-later
  13442. 'org-timestamp-up 'org-timestamp-down)))
  13443. ((and (not (eq org-support-shift-select 'always))
  13444. org-enable-priority-commands
  13445. (org-on-heading-p))
  13446. (call-interactively 'org-priority-down))
  13447. ((and (not org-support-shift-select) (org-at-item-p))
  13448. (call-interactively 'org-next-item))
  13449. ((org-clocktable-try-shift 'down arg))
  13450. (org-support-shift-select
  13451. (org-call-for-shift-select 'next-line))
  13452. (t (org-shiftselect-error))))
  13453. (defun org-shiftright (&optional arg)
  13454. "Cycle the thing at point or in the current line, depending on context.
  13455. Depending on context, this does one of the following:
  13456. - switch a timestamp at point one day into the future
  13457. - on a headline, switch to the next TODO keyword.
  13458. - on an item, switch entire list to the next bullet type
  13459. - on a property line, switch to the next allowed value
  13460. - on a clocktable definition line, move time block into the future"
  13461. (interactive "P")
  13462. (cond
  13463. ((and org-support-shift-select (org-region-active-p))
  13464. (org-call-for-shift-select 'forward-char))
  13465. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13466. ((and (not (eq org-support-shift-select 'always))
  13467. (org-on-heading-p))
  13468. (let ((org-inhibit-logging
  13469. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13470. (org-call-with-arg 'org-todo 'right)))
  13471. ((or (and org-support-shift-select
  13472. (not (eq org-support-shift-select 'always))
  13473. (org-at-item-bullet-p))
  13474. (and (not org-support-shift-select) (org-at-item-p)))
  13475. (org-call-with-arg 'org-cycle-list-bullet nil))
  13476. ((and (not (eq org-support-shift-select 'always))
  13477. (org-at-property-p))
  13478. (call-interactively 'org-property-next-allowed-value))
  13479. ((org-clocktable-try-shift 'right arg))
  13480. (org-support-shift-select
  13481. (org-call-for-shift-select 'forward-char))
  13482. (t (org-shiftselect-error))))
  13483. (defun org-shiftleft (&optional arg)
  13484. "Cycle the thing at point or in the current line, depending on context.
  13485. Depending on context, this does one of the following:
  13486. - switch a timestamp at point one day into the past
  13487. - on a headline, switch to the previous TODO keyword.
  13488. - on an item, switch entire list to the previous bullet type
  13489. - on a property line, switch to the previous allowed value
  13490. - on a clocktable definition line, move time block into the past"
  13491. (interactive "P")
  13492. (cond
  13493. ((and org-support-shift-select (org-region-active-p))
  13494. (org-call-for-shift-select 'backward-char))
  13495. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13496. ((and (not (eq org-support-shift-select 'always))
  13497. (org-on-heading-p))
  13498. (let ((org-inhibit-logging
  13499. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13500. (org-call-with-arg 'org-todo 'left)))
  13501. ((or (and org-support-shift-select
  13502. (not (eq org-support-shift-select 'always))
  13503. (org-at-item-bullet-p))
  13504. (and (not org-support-shift-select) (org-at-item-p)))
  13505. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13506. ((and (not (eq org-support-shift-select 'always))
  13507. (org-at-property-p))
  13508. (call-interactively 'org-property-previous-allowed-value))
  13509. ((org-clocktable-try-shift 'left arg))
  13510. (org-support-shift-select
  13511. (org-call-for-shift-select 'backward-char))
  13512. (t (org-shiftselect-error))))
  13513. (defun org-shiftcontrolright ()
  13514. "Switch to next TODO set."
  13515. (interactive)
  13516. (cond
  13517. ((and org-support-shift-select (org-region-active-p))
  13518. (org-call-for-shift-select 'forward-word))
  13519. ((and (not (eq org-support-shift-select 'always))
  13520. (org-on-heading-p))
  13521. (org-call-with-arg 'org-todo 'nextset))
  13522. (org-support-shift-select
  13523. (org-call-for-shift-select 'forward-word))
  13524. (t (org-shiftselect-error))))
  13525. (defun org-shiftcontrolleft ()
  13526. "Switch to previous TODO set."
  13527. (interactive)
  13528. (cond
  13529. ((and org-support-shift-select (org-region-active-p))
  13530. (org-call-for-shift-select 'backward-word))
  13531. ((and (not (eq org-support-shift-select 'always))
  13532. (org-on-heading-p))
  13533. (org-call-with-arg 'org-todo 'previousset))
  13534. (org-support-shift-select
  13535. (org-call-for-shift-select 'backward-word))
  13536. (t (org-shiftselect-error))))
  13537. (defun org-ctrl-c-ret ()
  13538. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13539. (interactive)
  13540. (cond
  13541. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13542. (t (call-interactively 'org-insert-heading))))
  13543. (defun org-copy-special ()
  13544. "Copy region in table or copy current subtree.
  13545. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13546. See the individual commands for more information."
  13547. (interactive)
  13548. (call-interactively
  13549. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13550. (defun org-cut-special ()
  13551. "Cut region in table or cut current subtree.
  13552. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13553. See the individual commands for more information."
  13554. (interactive)
  13555. (call-interactively
  13556. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13557. (defun org-paste-special (arg)
  13558. "Paste rectangular region into table, or past subtree relative to level.
  13559. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13560. See the individual commands for more information."
  13561. (interactive "P")
  13562. (if (org-at-table-p)
  13563. (org-table-paste-rectangle)
  13564. (org-paste-subtree arg)))
  13565. (defun org-edit-special ()
  13566. "Call a special editor for the stuff at point.
  13567. When at a table, call the formula editor with `org-table-edit-formulas'.
  13568. When at the first line of an src example, call `org-edit-src-code'.
  13569. When in an #+include line, visit the include file. Otherwise call
  13570. `ffap' to visit the file at point."
  13571. (interactive)
  13572. (cond
  13573. ((org-at-table-p)
  13574. (call-interactively 'org-table-edit-formulas))
  13575. ((save-excursion
  13576. (beginning-of-line 1)
  13577. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13578. (find-file (org-trim (match-string 1))))
  13579. ((org-edit-src-code))
  13580. ((org-edit-fixed-width-region))
  13581. (t (call-interactively 'ffap))))
  13582. (defun org-ctrl-c-ctrl-c (&optional arg)
  13583. "Set tags in headline, or update according to changed information at point.
  13584. This command does many different things, depending on context:
  13585. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13586. this is what we do.
  13587. - If the cursor is in a headline, prompt for tags and insert them
  13588. into the current line, aligned to `org-tags-column'. When called
  13589. with prefix arg, realign all tags in the current buffer.
  13590. - If the cursor is in one of the special #+KEYWORD lines, this
  13591. triggers scanning the buffer for these lines and updating the
  13592. information.
  13593. - If the cursor is inside a table, realign the table. This command
  13594. works even if the automatic table editor has been turned off.
  13595. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13596. the entire table.
  13597. - If the cursor is at a footnote reference or definition, jump to
  13598. the corresponding definition or references, respectively.
  13599. - If the cursor is a the beginning of a dynamic block, update it.
  13600. - If the cursor is inside a table created by the table.el package,
  13601. activate that table.
  13602. - If the current buffer is a remember buffer, close note and file
  13603. it. A prefix argument of 1 files to the default location
  13604. without further interaction. A prefix argument of 2 files to
  13605. the currently clocking task.
  13606. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13607. links in this buffer.
  13608. - If the cursor is on a numbered item in a plain list, renumber the
  13609. ordered list.
  13610. - If the cursor is on a checkbox, toggle it."
  13611. (interactive "P")
  13612. (let ((org-enable-table-editor t))
  13613. (cond
  13614. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13615. org-occur-highlights
  13616. org-latex-fragment-image-overlays)
  13617. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13618. (org-remove-occur-highlights)
  13619. (org-remove-latex-fragment-image-overlays)
  13620. (message "Temporary highlights/overlays removed from current buffer"))
  13621. ((and (local-variable-p 'org-finish-function (current-buffer))
  13622. (fboundp org-finish-function))
  13623. (funcall org-finish-function))
  13624. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13625. ((org-at-property-p)
  13626. (call-interactively 'org-property-action))
  13627. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13628. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13629. ((org-at-table.el-p)
  13630. (require 'table)
  13631. (beginning-of-line 1)
  13632. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13633. (call-interactively 'table-recognize-table))
  13634. ((org-at-table-p)
  13635. (org-table-maybe-eval-formula)
  13636. (if arg
  13637. (call-interactively 'org-table-recalculate)
  13638. (org-table-maybe-recalculate-line))
  13639. (call-interactively 'org-table-align))
  13640. ((or (org-footnote-at-reference-p)
  13641. (org-footnote-at-definition-p))
  13642. (call-interactively 'org-footnote-action))
  13643. ((org-at-item-checkbox-p)
  13644. (call-interactively 'org-toggle-checkbox))
  13645. ((org-at-item-p)
  13646. (if arg
  13647. (call-interactively 'org-toggle-checkbox)
  13648. (call-interactively 'org-maybe-renumber-ordered-list)))
  13649. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13650. ;; Dynamic block
  13651. (beginning-of-line 1)
  13652. (save-excursion (org-update-dblock)))
  13653. ((save-excursion
  13654. (beginning-of-line 1)
  13655. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13656. (cond
  13657. ((equal (match-string 1) "TBLFM")
  13658. ;; Recalculate the table before this line
  13659. (save-excursion
  13660. (beginning-of-line 1)
  13661. (skip-chars-backward " \r\n\t")
  13662. (if (org-at-table-p)
  13663. (org-call-with-arg 'org-table-recalculate t))))
  13664. (t
  13665. ; (org-set-regexps-and-options)
  13666. ; (org-restart-font-lock)
  13667. (let ((org-inhibit-startup t)) (org-mode-restart))
  13668. (message "Local setup has been refreshed"))))
  13669. (t (error "C-c C-c can do nothing useful at this location.")))))
  13670. (defun org-mode-restart ()
  13671. "Restart Org-mode, to scan again for special lines.
  13672. Also updates the keyword regular expressions."
  13673. (interactive)
  13674. (org-mode)
  13675. (message "Org-mode restarted"))
  13676. (defun org-kill-note-or-show-branches ()
  13677. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13678. (interactive)
  13679. (if (not org-finish-function)
  13680. (call-interactively 'show-branches)
  13681. (let ((org-note-abort t))
  13682. (funcall org-finish-function))))
  13683. (defun org-return (&optional indent)
  13684. "Goto next table row or insert a newline.
  13685. Calls `org-table-next-row' or `newline', depending on context.
  13686. See the individual commands for more information."
  13687. (interactive)
  13688. (cond
  13689. ((bobp) (if indent (newline-and-indent) (newline)))
  13690. ((org-at-table-p)
  13691. (org-table-justify-field-maybe)
  13692. (call-interactively 'org-table-next-row))
  13693. ((and org-return-follows-link
  13694. (eq (get-text-property (point) 'face) 'org-link))
  13695. (call-interactively 'org-open-at-point))
  13696. ((and (org-at-heading-p)
  13697. (looking-at
  13698. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13699. (org-show-entry)
  13700. (end-of-line 1)
  13701. (newline))
  13702. (t (if indent (newline-and-indent) (newline)))))
  13703. (defun org-return-indent ()
  13704. "Goto next table row or insert a newline and indent.
  13705. Calls `org-table-next-row' or `newline-and-indent', depending on
  13706. context. See the individual commands for more information."
  13707. (interactive)
  13708. (org-return t))
  13709. (defun org-ctrl-c-star ()
  13710. "Compute table, or change heading status of lines.
  13711. Calls `org-table-recalculate' or `org-toggle-heading',
  13712. depending on context."
  13713. (interactive)
  13714. (cond
  13715. ((org-at-table-p)
  13716. (call-interactively 'org-table-recalculate))
  13717. (t
  13718. ;; Convert all lines in region to list items
  13719. (call-interactively 'org-toggle-heading))))
  13720. (defun org-ctrl-c-minus ()
  13721. "Insert separator line in table or modify bullet status of line.
  13722. Also turns a plain line or a region of lines into list items.
  13723. Calls `org-table-insert-hline', `org-toggle-item', or
  13724. `org-cycle-list-bullet', depending on context."
  13725. (interactive)
  13726. (cond
  13727. ((org-at-table-p)
  13728. (call-interactively 'org-table-insert-hline))
  13729. ((org-region-active-p)
  13730. (call-interactively 'org-toggle-item))
  13731. ((org-in-item-p)
  13732. (call-interactively 'org-cycle-list-bullet))
  13733. (t
  13734. (call-interactively 'org-toggle-item))))
  13735. (defun org-toggle-item ()
  13736. "Convert headings or normal lines to items, items to normal lines.
  13737. If there is no active region, only the current line is considered.
  13738. If the first line in the region is a headline, convert all headlines to items.
  13739. If the first line in the region is an item, convert all items to normal lines.
  13740. If the first line is normal text, add an item bullet to each line."
  13741. (interactive)
  13742. (let (l2 l beg end)
  13743. (if (org-region-active-p)
  13744. (setq beg (region-beginning) end (region-end))
  13745. (setq beg (point-at-bol)
  13746. end (min (1+ (point-at-eol)) (point-max))))
  13747. (save-excursion
  13748. (goto-char end)
  13749. (setq l2 (org-current-line))
  13750. (goto-char beg)
  13751. (beginning-of-line 1)
  13752. (setq l (1- (org-current-line)))
  13753. (if (org-at-item-p)
  13754. ;; We already have items, de-itemize
  13755. (while (< (setq l (1+ l)) l2)
  13756. (when (org-at-item-p)
  13757. (goto-char (match-beginning 2))
  13758. (delete-region (match-beginning 2) (match-end 2))
  13759. (and (looking-at "[ \t]+") (replace-match "")))
  13760. (beginning-of-line 2))
  13761. (if (org-on-heading-p)
  13762. ;; Headings, convert to items
  13763. (while (< (setq l (1+ l)) l2)
  13764. (if (looking-at org-outline-regexp)
  13765. (replace-match "- " t t))
  13766. (beginning-of-line 2))
  13767. ;; normal lines, turn them into items
  13768. (while (< (setq l (1+ l)) l2)
  13769. (unless (org-at-item-p)
  13770. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13771. (replace-match "\\1- \\2")))
  13772. (beginning-of-line 2)))))))
  13773. (defun org-toggle-heading (&optional nstars)
  13774. "Convert headings to normal text, or items or text to headings.
  13775. If there is no active region, only the current line is considered.
  13776. If the first line is a heading, remove the stars from all headlines
  13777. in the region.
  13778. If the first line is a plain list item, turn all plain list items into
  13779. headings.
  13780. If the first line is a normal line, turn each and every line in the region
  13781. into a heading.
  13782. When converting a line into a heading, the number of stars is chosen
  13783. such that the lines become children of the current entry. However, when
  13784. a prefix argument is given, its value determines the number of stars to add."
  13785. (interactive "P")
  13786. (let (l2 l itemp beg end)
  13787. (if (org-region-active-p)
  13788. (setq beg (region-beginning) end (region-end))
  13789. (setq beg (point-at-bol)
  13790. end (min (1+ (point-at-eol)) (point-max))))
  13791. (save-excursion
  13792. (goto-char end)
  13793. (setq l2 (org-current-line))
  13794. (goto-char beg)
  13795. (beginning-of-line 1)
  13796. (setq l (1- (org-current-line)))
  13797. (if (org-on-heading-p)
  13798. ;; We already have headlines, de-star them
  13799. (while (< (setq l (1+ l)) l2)
  13800. (when (org-on-heading-p t)
  13801. (and (looking-at outline-regexp) (replace-match "")))
  13802. (beginning-of-line 2))
  13803. (setq itemp (org-at-item-p))
  13804. (let* ((stars
  13805. (if nstars
  13806. (make-string (prefix-numeric-value current-prefix-arg)
  13807. ?*)
  13808. (save-excursion
  13809. (re-search-backward org-complex-heading-regexp nil t)
  13810. (or (match-string 1) ""))))
  13811. (add-stars (cond (nstars "")
  13812. ((equal stars "") "*")
  13813. (org-odd-levels-only "**")
  13814. (t "*")))
  13815. (rpl (concat stars add-stars " ")))
  13816. (while (< (setq l (1+ l)) l2)
  13817. (if itemp
  13818. (and (org-at-item-p) (replace-match rpl t t))
  13819. (unless (org-on-heading-p)
  13820. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13821. (replace-match (concat rpl (match-string 2))))))
  13822. (beginning-of-line 2)))))))
  13823. (defun org-meta-return (&optional arg)
  13824. "Insert a new heading or wrap a region in a table.
  13825. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13826. See the individual commands for more information."
  13827. (interactive "P")
  13828. (cond
  13829. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13830. ((org-at-table-p)
  13831. (call-interactively 'org-table-wrap-region))
  13832. (t (call-interactively 'org-insert-heading))))
  13833. ;;; Menu entries
  13834. ;; Define the Org-mode menus
  13835. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13836. '("Tbl"
  13837. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13838. ["Next Field" org-cycle (org-at-table-p)]
  13839. ["Previous Field" org-shifttab (org-at-table-p)]
  13840. ["Next Row" org-return (org-at-table-p)]
  13841. "--"
  13842. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13843. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13844. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13845. "--"
  13846. ("Column"
  13847. ["Move Column Left" org-metaleft (org-at-table-p)]
  13848. ["Move Column Right" org-metaright (org-at-table-p)]
  13849. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13850. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13851. ("Row"
  13852. ["Move Row Up" org-metaup (org-at-table-p)]
  13853. ["Move Row Down" org-metadown (org-at-table-p)]
  13854. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13855. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13856. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13857. "--"
  13858. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13859. ("Rectangle"
  13860. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13861. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13862. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13863. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13864. "--"
  13865. ("Calculate"
  13866. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13867. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13868. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13869. "--"
  13870. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13871. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13872. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13873. "--"
  13874. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13875. "--"
  13876. ["Sum Column/Rectangle" org-table-sum
  13877. (or (org-at-table-p) (org-region-active-p))]
  13878. ["Which Column?" org-table-current-column (org-at-table-p)])
  13879. ["Debug Formulas"
  13880. org-table-toggle-formula-debugger
  13881. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13882. ["Show Col/Row Numbers"
  13883. org-table-toggle-coordinate-overlays
  13884. :style toggle
  13885. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13886. "--"
  13887. ["Create" org-table-create (and (not (org-at-table-p))
  13888. org-enable-table-editor)]
  13889. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13890. ["Import from File" org-table-import (not (org-at-table-p))]
  13891. ["Export to File" org-table-export (org-at-table-p)]
  13892. "--"
  13893. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13894. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13895. '("Org"
  13896. ("Show/Hide"
  13897. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13898. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13899. ["Sparse Tree..." org-sparse-tree t]
  13900. ["Reveal Context" org-reveal t]
  13901. ["Show All" show-all t]
  13902. "--"
  13903. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13904. "--"
  13905. ["New Heading" org-insert-heading t]
  13906. ("Navigate Headings"
  13907. ["Up" outline-up-heading t]
  13908. ["Next" outline-next-visible-heading t]
  13909. ["Previous" outline-previous-visible-heading t]
  13910. ["Next Same Level" outline-forward-same-level t]
  13911. ["Previous Same Level" outline-backward-same-level t]
  13912. "--"
  13913. ["Jump" org-goto t])
  13914. ("Edit Structure"
  13915. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13916. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13917. "--"
  13918. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13919. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13920. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13921. "--"
  13922. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13923. "--"
  13924. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13925. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13926. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13927. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13928. "--"
  13929. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13930. "--"
  13931. ["Convert to odd levels" org-convert-to-odd-levels t]
  13932. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13933. ("Editing"
  13934. ["Emphasis..." org-emphasize t]
  13935. ["Edit Source Example" org-edit-special t]
  13936. "--"
  13937. ["Footnote new/jump" org-footnote-action t]
  13938. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13939. ("Archive"
  13940. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13941. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13942. ; :active t :keys "C-u C-c C-x C-a"]
  13943. ["Sparse trees open ARCHIVE trees"
  13944. (setq org-sparse-tree-open-archived-trees
  13945. (not org-sparse-tree-open-archived-trees))
  13946. :style toggle :selected org-sparse-tree-open-archived-trees]
  13947. ["Cycling opens ARCHIVE trees"
  13948. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13949. :style toggle :selected org-cycle-open-archived-trees]
  13950. "--"
  13951. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13952. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13953. ; ["Check and Move Children" (org-archive-subtree '(4))
  13954. ; :active t :keys "C-u C-c C-x C-s"]
  13955. )
  13956. "--"
  13957. ("Hyperlinks"
  13958. ["Store Link (Global)" org-store-link t]
  13959. ["Insert Link" org-insert-link t]
  13960. ["Follow Link" org-open-at-point t]
  13961. "--"
  13962. ["Next link" org-next-link t]
  13963. ["Previous link" org-previous-link t]
  13964. "--"
  13965. ["Descriptive Links"
  13966. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13967. :style radio
  13968. :selected (member '(org-link) buffer-invisibility-spec)]
  13969. ["Literal Links"
  13970. (progn
  13971. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13972. :style radio
  13973. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13974. "--"
  13975. ("TODO Lists"
  13976. ["TODO/DONE/-" org-todo t]
  13977. ("Select keyword"
  13978. ["Next keyword" org-shiftright (org-on-heading-p)]
  13979. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13980. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13981. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13982. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13983. ["Show TODO Tree" org-show-todo-tree t]
  13984. ["Global TODO list" org-todo-list t]
  13985. "--"
  13986. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13987. :selected org-enforce-todo-dependencies :style toggle :active t]
  13988. "Settings for tree at point"
  13989. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13990. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13991. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13992. ["Do Children parallel" org-toggle-ordered-property :style radio
  13993. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13994. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13995. "--"
  13996. ["Set Priority" org-priority t]
  13997. ["Priority Up" org-shiftup t]
  13998. ["Priority Down" org-shiftdown t]
  13999. "--"
  14000. ["Get news from all feeds" org-feed-update-all t]
  14001. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14002. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14003. ("TAGS and Properties"
  14004. ["Set Tags" org-set-tags-command t]
  14005. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14006. "--"
  14007. ["Set property" org-set-property t]
  14008. ["Column view of properties" org-columns t]
  14009. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14010. ("Dates and Scheduling"
  14011. ["Timestamp" org-time-stamp t]
  14012. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14013. ("Change Date"
  14014. ["1 Day Later" org-shiftright t]
  14015. ["1 Day Earlier" org-shiftleft t]
  14016. ["1 ... Later" org-shiftup t]
  14017. ["1 ... Earlier" org-shiftdown t])
  14018. ["Compute Time Range" org-evaluate-time-range t]
  14019. ["Schedule Item" org-schedule t]
  14020. ["Deadline" org-deadline t]
  14021. "--"
  14022. ["Custom time format" org-toggle-time-stamp-overlays
  14023. :style radio :selected org-display-custom-times]
  14024. "--"
  14025. ["Goto Calendar" org-goto-calendar t]
  14026. ["Date from Calendar" org-date-from-calendar t]
  14027. "--"
  14028. ["Start/Restart Timer" org-timer-start t]
  14029. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14030. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14031. ["Insert Timer String" org-timer t]
  14032. ["Insert Timer Item" org-timer-item t])
  14033. ("Logging work"
  14034. ["Clock in" org-clock-in t]
  14035. ["Clock out" org-clock-out t]
  14036. ["Clock cancel" org-clock-cancel t]
  14037. ["Goto running clock" org-clock-goto t]
  14038. ["Display times" org-clock-display t]
  14039. ["Create clock table" org-clock-report t]
  14040. "--"
  14041. ["Record DONE time"
  14042. (progn (setq org-log-done (not org-log-done))
  14043. (message "Switching to %s will %s record a timestamp"
  14044. (car org-done-keywords)
  14045. (if org-log-done "automatically" "not")))
  14046. :style toggle :selected org-log-done])
  14047. "--"
  14048. ["Agenda Command..." org-agenda t]
  14049. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14050. ("File List for Agenda")
  14051. ("Special views current file"
  14052. ["TODO Tree" org-show-todo-tree t]
  14053. ["Check Deadlines" org-check-deadlines t]
  14054. ["Timeline" org-timeline t]
  14055. ["Tags/Property tree" org-match-sparse-tree t])
  14056. "--"
  14057. ["Export/Publish..." org-export t]
  14058. ("LaTeX"
  14059. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14060. :selected org-cdlatex-mode]
  14061. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14062. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14063. ["Modify math symbol" org-cdlatex-math-modify
  14064. (org-inside-LaTeX-fragment-p)]
  14065. ["Insert citation" org-reftex-citation t]
  14066. "--"
  14067. ["Export LaTeX fragments as images"
  14068. (if (featurep 'org-exp)
  14069. (setq org-export-with-LaTeX-fragments
  14070. (not org-export-with-LaTeX-fragments))
  14071. (require 'org-exp))
  14072. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14073. org-export-with-LaTeX-fragments)])
  14074. "--"
  14075. ("Documentation"
  14076. ["Show Version" org-version t]
  14077. ["Info Documentation" org-info t])
  14078. ("Customize"
  14079. ["Browse Org Group" org-customize t]
  14080. "--"
  14081. ["Expand This Menu" org-create-customize-menu
  14082. (fboundp 'customize-menu-create)])
  14083. "--"
  14084. ("Refresh/Reload"
  14085. ["Refresh setup current buffer" org-mode-restart t]
  14086. ["Reload Org (after update)" org-reload t]
  14087. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14088. ))
  14089. (defun org-info (&optional node)
  14090. "Read documentation for Org-mode in the info system.
  14091. With optional NODE, go directly to that node."
  14092. (interactive)
  14093. (info (format "(org)%s" (or node ""))))
  14094. (defun org-install-agenda-files-menu ()
  14095. (let ((bl (buffer-list)))
  14096. (save-excursion
  14097. (while bl
  14098. (set-buffer (pop bl))
  14099. (if (org-mode-p) (setq bl nil)))
  14100. (when (org-mode-p)
  14101. (easy-menu-change
  14102. '("Org") "File List for Agenda"
  14103. (append
  14104. (list
  14105. ["Edit File List" (org-edit-agenda-file-list) t]
  14106. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14107. ["Remove Current File from List" org-remove-file t]
  14108. ["Cycle through agenda files" org-cycle-agenda-files t]
  14109. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14110. "--")
  14111. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14112. ;;;; Documentation
  14113. ;;;###autoload
  14114. (defun org-require-autoloaded-modules ()
  14115. (interactive)
  14116. (mapc 'require
  14117. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14118. org-docbook org-exp org-html org-icalendar
  14119. org-id org-latex
  14120. org-publish org-remember org-table
  14121. org-timer org-xoxo)))
  14122. ;;;###autoload
  14123. (defun org-reload (&optional uncompiled)
  14124. "Reload all org lisp files.
  14125. With prefix arg UNCOMPILED, load the uncompiled versions."
  14126. (interactive "P")
  14127. (require 'find-func)
  14128. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14129. (dir-org (file-name-directory (org-find-library-name "org")))
  14130. (dir-org-contrib (ignore-errors
  14131. (file-name-directory
  14132. (org-find-library-name "org-contribdir"))))
  14133. (files
  14134. (append (directory-files dir-org t file-re)
  14135. (and dir-org-contrib
  14136. (directory-files dir-org-contrib t file-re))))
  14137. (remove-re (concat (if (featurep 'xemacs)
  14138. "org-colview" "org-colview-xemacs")
  14139. "\\'")))
  14140. (setq files (mapcar 'file-name-sans-extension files))
  14141. (setq files (mapcar
  14142. (lambda (x) (if (string-match remove-re x) nil x))
  14143. files))
  14144. (setq files (delq nil files))
  14145. (mapc
  14146. (lambda (f)
  14147. (when (featurep (intern (file-name-nondirectory f)))
  14148. (if (and (not uncompiled)
  14149. (file-exists-p (concat f ".elc")))
  14150. (load (concat f ".elc") nil nil t)
  14151. (load (concat f ".el") nil nil t))))
  14152. files))
  14153. (org-version))
  14154. ;;;###autoload
  14155. (defun org-customize ()
  14156. "Call the customize function with org as argument."
  14157. (interactive)
  14158. (org-load-modules-maybe)
  14159. (org-require-autoloaded-modules)
  14160. (customize-browse 'org))
  14161. (defun org-create-customize-menu ()
  14162. "Create a full customization menu for Org-mode, insert it into the menu."
  14163. (interactive)
  14164. (org-load-modules-maybe)
  14165. (org-require-autoloaded-modules)
  14166. (if (fboundp 'customize-menu-create)
  14167. (progn
  14168. (easy-menu-change
  14169. '("Org") "Customize"
  14170. `(["Browse Org group" org-customize t]
  14171. "--"
  14172. ,(customize-menu-create 'org)
  14173. ["Set" Custom-set t]
  14174. ["Save" Custom-save t]
  14175. ["Reset to Current" Custom-reset-current t]
  14176. ["Reset to Saved" Custom-reset-saved t]
  14177. ["Reset to Standard Settings" Custom-reset-standard t]))
  14178. (message "\"Org\"-menu now contains full customization menu"))
  14179. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14180. ;;;; Miscellaneous stuff
  14181. ;;; Generally useful functions
  14182. (defun org-find-text-property-in-string (prop s)
  14183. "Return the first non-nil value of property PROP in string S."
  14184. (or (get-text-property 0 prop s)
  14185. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14186. prop s)))
  14187. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14188. "Display the given MESSAGE as a warning."
  14189. (if (fboundp 'display-warning)
  14190. (display-warning 'org message
  14191. (if (featurep 'xemacs)
  14192. 'warning
  14193. :warning))
  14194. (let ((buf (get-buffer-create "*Org warnings*")))
  14195. (with-current-buffer buf
  14196. (goto-char (point-max))
  14197. (insert "Warning (Org): " message)
  14198. (unless (bolp)
  14199. (newline)))
  14200. (display-buffer buf)
  14201. (sit-for 0))))
  14202. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14203. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14204. (if (and marker (marker-buffer marker)
  14205. (buffer-live-p (marker-buffer marker)))
  14206. (progn
  14207. (switch-to-buffer (marker-buffer marker))
  14208. (if (or (> marker (point-max)) (< marker (point-min)))
  14209. (widen))
  14210. (goto-char marker)
  14211. (org-show-context 'org-goto))
  14212. (if bookmark
  14213. (bookmark-jump bookmark)
  14214. (error "Cannot find location"))))
  14215. (defun org-quote-csv-field (s)
  14216. "Quote field for inclusion in CSV material."
  14217. (if (string-match "[\",]" s)
  14218. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14219. s))
  14220. (defun org-plist-delete (plist property)
  14221. "Delete PROPERTY from PLIST.
  14222. This is in contrast to merely setting it to 0."
  14223. (let (p)
  14224. (while plist
  14225. (if (not (eq property (car plist)))
  14226. (setq p (plist-put p (car plist) (nth 1 plist))))
  14227. (setq plist (cddr plist)))
  14228. p))
  14229. (defun org-force-self-insert (N)
  14230. "Needed to enforce self-insert under remapping."
  14231. (interactive "p")
  14232. (self-insert-command N))
  14233. (defun org-string-width (s)
  14234. "Compute width of string, ignoring invisible characters.
  14235. This ignores character with invisibility property `org-link', and also
  14236. characters with property `org-cwidth', because these will become invisible
  14237. upon the next fontification round."
  14238. (let (b l)
  14239. (when (or (eq t buffer-invisibility-spec)
  14240. (assq 'org-link buffer-invisibility-spec))
  14241. (while (setq b (text-property-any 0 (length s)
  14242. 'invisible 'org-link s))
  14243. (setq s (concat (substring s 0 b)
  14244. (substring s (or (next-single-property-change
  14245. b 'invisible s) (length s)))))))
  14246. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14247. (setq s (concat (substring s 0 b)
  14248. (substring s (or (next-single-property-change
  14249. b 'org-cwidth s) (length s))))))
  14250. (setq l (string-width s) b -1)
  14251. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14252. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14253. l))
  14254. (defun org-get-indentation (&optional line)
  14255. "Get the indentation of the current line, interpreting tabs.
  14256. When LINE is given, assume it represents a line and compute its indentation."
  14257. (if line
  14258. (if (string-match "^ *" (org-remove-tabs line))
  14259. (match-end 0))
  14260. (save-excursion
  14261. (beginning-of-line 1)
  14262. (skip-chars-forward " \t")
  14263. (current-column))))
  14264. (defun org-remove-tabs (s &optional width)
  14265. "Replace tabulators in S with spaces.
  14266. Assumes that s is a single line, starting in column 0."
  14267. (setq width (or width tab-width))
  14268. (while (string-match "\t" s)
  14269. (setq s (replace-match
  14270. (make-string
  14271. (- (* width (/ (+ (match-beginning 0) width) width))
  14272. (match-beginning 0)) ?\ )
  14273. t t s)))
  14274. s)
  14275. (defun org-fix-indentation (line ind)
  14276. "Fix indentation in LINE.
  14277. IND is a cons cell with target and minimum indentation.
  14278. If the current indentation in LINE is smaller than the minimum,
  14279. leave it alone. If it is larger than ind, set it to the target."
  14280. (let* ((l (org-remove-tabs line))
  14281. (i (org-get-indentation l))
  14282. (i1 (car ind)) (i2 (cdr ind)))
  14283. (if (>= i i2) (setq l (substring line i2)))
  14284. (if (> i1 0)
  14285. (concat (make-string i1 ?\ ) l)
  14286. l)))
  14287. (defun org-remove-indentation (code &optional n)
  14288. "Remove the maximum common indentation from the lines in CODE.
  14289. N may optionally be the number of spaces to remove."
  14290. (with-temp-buffer
  14291. (insert code)
  14292. (org-do-remove-indentation n)
  14293. (buffer-string)))
  14294. (defun org-do-remove-indentation (&optional n)
  14295. "Remove the maximum common indentation from the buffer."
  14296. (untabify (point-min) (point-max))
  14297. (let ((min 10000) re)
  14298. (if n
  14299. (setq min n)
  14300. (goto-char (point-min))
  14301. (while (re-search-forward "^ *[^ \n]" nil t)
  14302. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14303. (unless (or (= min 0) (= min 10000))
  14304. (setq re (format "^ \\{%d\\}" min))
  14305. (goto-char (point-min))
  14306. (while (re-search-forward re nil t)
  14307. (replace-match "")
  14308. (end-of-line 1))
  14309. min)))
  14310. (defun org-base-buffer (buffer)
  14311. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14312. (if (not buffer)
  14313. buffer
  14314. (or (buffer-base-buffer buffer)
  14315. buffer)))
  14316. (defun org-trim (s)
  14317. "Remove whitespace at beginning and end of string."
  14318. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14319. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14320. s)
  14321. (defun org-wrap (string &optional width lines)
  14322. "Wrap string to either a number of lines, or a width in characters.
  14323. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14324. that costs. If there is a word longer than WIDTH, the text is actually
  14325. wrapped to the length of that word.
  14326. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14327. many lines, whatever width that takes.
  14328. The return value is a list of lines, without newlines at the end."
  14329. (let* ((words (org-split-string string "[ \t\n]+"))
  14330. (maxword (apply 'max (mapcar 'org-string-width words)))
  14331. w ll)
  14332. (cond (width
  14333. (org-do-wrap words (max maxword width)))
  14334. (lines
  14335. (setq w maxword)
  14336. (setq ll (org-do-wrap words maxword))
  14337. (if (<= (length ll) lines)
  14338. ll
  14339. (setq ll words)
  14340. (while (> (length ll) lines)
  14341. (setq w (1+ w))
  14342. (setq ll (org-do-wrap words w)))
  14343. ll))
  14344. (t (error "Cannot wrap this")))))
  14345. (defun org-do-wrap (words width)
  14346. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14347. (let (lines line)
  14348. (while words
  14349. (setq line (pop words))
  14350. (while (and words (< (+ (length line) (length (car words))) width))
  14351. (setq line (concat line " " (pop words))))
  14352. (setq lines (push line lines)))
  14353. (nreverse lines)))
  14354. (defun org-split-string (string &optional separators)
  14355. "Splits STRING into substrings at SEPARATORS.
  14356. No empty strings are returned if there are matches at the beginning
  14357. and end of string."
  14358. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14359. (start 0)
  14360. notfirst
  14361. (list nil))
  14362. (while (and (string-match rexp string
  14363. (if (and notfirst
  14364. (= start (match-beginning 0))
  14365. (< start (length string)))
  14366. (1+ start) start))
  14367. (< (match-beginning 0) (length string)))
  14368. (setq notfirst t)
  14369. (or (eq (match-beginning 0) 0)
  14370. (and (eq (match-beginning 0) (match-end 0))
  14371. (eq (match-beginning 0) start))
  14372. (setq list
  14373. (cons (substring string start (match-beginning 0))
  14374. list)))
  14375. (setq start (match-end 0)))
  14376. (or (eq start (length string))
  14377. (setq list
  14378. (cons (substring string start)
  14379. list)))
  14380. (nreverse list)))
  14381. (defun org-uuidgen-p (s)
  14382. "Is S an ID created by UUIDGEN?"
  14383. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14384. (defun org-context ()
  14385. "Return a list of contexts of the current cursor position.
  14386. If several contexts apply, all are returned.
  14387. Each context entry is a list with a symbol naming the context, and
  14388. two positions indicating start and end of the context. Possible
  14389. contexts are:
  14390. :headline anywhere in a headline
  14391. :headline-stars on the leading stars in a headline
  14392. :todo-keyword on a TODO keyword (including DONE) in a headline
  14393. :tags on the TAGS in a headline
  14394. :priority on the priority cookie in a headline
  14395. :item on the first line of a plain list item
  14396. :item-bullet on the bullet/number of a plain list item
  14397. :checkbox on the checkbox in a plain list item
  14398. :table in an org-mode table
  14399. :table-special on a special filed in a table
  14400. :table-table in a table.el table
  14401. :link on a hyperlink
  14402. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14403. :target on a <<target>>
  14404. :radio-target on a <<<radio-target>>>
  14405. :latex-fragment on a LaTeX fragment
  14406. :latex-preview on a LaTeX fragment with overlayed preview image
  14407. This function expects the position to be visible because it uses font-lock
  14408. faces as a help to recognize the following contexts: :table-special, :link,
  14409. and :keyword."
  14410. (let* ((f (get-text-property (point) 'face))
  14411. (faces (if (listp f) f (list f)))
  14412. (p (point)) clist o)
  14413. ;; First the large context
  14414. (cond
  14415. ((org-on-heading-p t)
  14416. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14417. (when (progn
  14418. (beginning-of-line 1)
  14419. (looking-at org-todo-line-tags-regexp))
  14420. (push (org-point-in-group p 1 :headline-stars) clist)
  14421. (push (org-point-in-group p 2 :todo-keyword) clist)
  14422. (push (org-point-in-group p 4 :tags) clist))
  14423. (goto-char p)
  14424. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14425. (if (looking-at "\\[#[A-Z0-9]\\]")
  14426. (push (org-point-in-group p 0 :priority) clist)))
  14427. ((org-at-item-p)
  14428. (push (org-point-in-group p 2 :item-bullet) clist)
  14429. (push (list :item (point-at-bol)
  14430. (save-excursion (org-end-of-item) (point)))
  14431. clist)
  14432. (and (org-at-item-checkbox-p)
  14433. (push (org-point-in-group p 0 :checkbox) clist)))
  14434. ((org-at-table-p)
  14435. (push (list :table (org-table-begin) (org-table-end)) clist)
  14436. (if (memq 'org-formula faces)
  14437. (push (list :table-special
  14438. (previous-single-property-change p 'face)
  14439. (next-single-property-change p 'face)) clist)))
  14440. ((org-at-table-p 'any)
  14441. (push (list :table-table) clist)))
  14442. (goto-char p)
  14443. ;; Now the small context
  14444. (cond
  14445. ((org-at-timestamp-p)
  14446. (push (org-point-in-group p 0 :timestamp) clist))
  14447. ((memq 'org-link faces)
  14448. (push (list :link
  14449. (previous-single-property-change p 'face)
  14450. (next-single-property-change p 'face)) clist))
  14451. ((memq 'org-special-keyword faces)
  14452. (push (list :keyword
  14453. (previous-single-property-change p 'face)
  14454. (next-single-property-change p 'face)) clist))
  14455. ((org-on-target-p)
  14456. (push (org-point-in-group p 0 :target) clist)
  14457. (goto-char (1- (match-beginning 0)))
  14458. (if (looking-at org-radio-target-regexp)
  14459. (push (org-point-in-group p 0 :radio-target) clist))
  14460. (goto-char p))
  14461. ((setq o (car (delq nil
  14462. (mapcar
  14463. (lambda (x)
  14464. (if (memq x org-latex-fragment-image-overlays) x))
  14465. (org-overlays-at (point))))))
  14466. (push (list :latex-fragment
  14467. (org-overlay-start o) (org-overlay-end o)) clist)
  14468. (push (list :latex-preview
  14469. (org-overlay-start o) (org-overlay-end o)) clist))
  14470. ((org-inside-LaTeX-fragment-p)
  14471. ;; FIXME: positions wrong.
  14472. (push (list :latex-fragment (point) (point)) clist)))
  14473. (setq clist (nreverse (delq nil clist)))
  14474. clist))
  14475. ;; FIXME: Compare with at-regexp-p Do we need both?
  14476. (defun org-in-regexp (re &optional nlines visually)
  14477. "Check if point is inside a match of regexp.
  14478. Normally only the current line is checked, but you can include NLINES extra
  14479. lines both before and after point into the search.
  14480. If VISUALLY is set, require that the cursor is not after the match but
  14481. really on, so that the block visually is on the match."
  14482. (catch 'exit
  14483. (let ((pos (point))
  14484. (eol (point-at-eol (+ 1 (or nlines 0))))
  14485. (inc (if visually 1 0)))
  14486. (save-excursion
  14487. (beginning-of-line (- 1 (or nlines 0)))
  14488. (while (re-search-forward re eol t)
  14489. (if (and (<= (match-beginning 0) pos)
  14490. (>= (+ inc (match-end 0)) pos))
  14491. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14492. (defun org-at-regexp-p (regexp)
  14493. "Is point inside a match of REGEXP in the current line?"
  14494. (catch 'exit
  14495. (save-excursion
  14496. (let ((pos (point)) (end (point-at-eol)))
  14497. (beginning-of-line 1)
  14498. (while (re-search-forward regexp end t)
  14499. (if (and (<= (match-beginning 0) pos)
  14500. (>= (match-end 0) pos))
  14501. (throw 'exit t)))
  14502. nil))))
  14503. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14504. "Call `multi-occur' with buffers for all agenda files."
  14505. (interactive "sOrg-files matching: \np")
  14506. (let* ((files (org-agenda-files))
  14507. (tnames (mapcar 'file-truename files))
  14508. (extra org-agenda-text-search-extra-files)
  14509. f)
  14510. (when (eq (car extra) 'agenda-archives)
  14511. (setq extra (cdr extra))
  14512. (setq files (org-add-archive-files files)))
  14513. (while (setq f (pop extra))
  14514. (unless (member (file-truename f) tnames)
  14515. (add-to-list 'files f 'append)
  14516. (add-to-list 'tnames (file-truename f) 'append)))
  14517. (multi-occur
  14518. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14519. regexp)))
  14520. (if (boundp 'occur-mode-find-occurrence-hook)
  14521. ;; Emacs 23
  14522. (add-hook 'occur-mode-find-occurrence-hook
  14523. (lambda ()
  14524. (when (org-mode-p)
  14525. (org-reveal))))
  14526. ;; Emacs 22
  14527. (defadvice occur-mode-goto-occurrence
  14528. (after org-occur-reveal activate)
  14529. (and (org-mode-p) (org-reveal)))
  14530. (defadvice occur-mode-goto-occurrence-other-window
  14531. (after org-occur-reveal activate)
  14532. (and (org-mode-p) (org-reveal)))
  14533. (defadvice occur-mode-display-occurrence
  14534. (after org-occur-reveal activate)
  14535. (when (org-mode-p)
  14536. (let ((pos (occur-mode-find-occurrence)))
  14537. (with-current-buffer (marker-buffer pos)
  14538. (save-excursion
  14539. (goto-char pos)
  14540. (org-reveal)))))))
  14541. (defun org-uniquify (list)
  14542. "Remove duplicate elements from LIST."
  14543. (let (res)
  14544. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14545. res))
  14546. (defun org-delete-all (elts list)
  14547. "Remove all elements in ELTS from LIST."
  14548. (while elts
  14549. (setq list (delete (pop elts) list)))
  14550. list)
  14551. (defun org-back-over-empty-lines ()
  14552. "Move backwards over whitespace, to the beginning of the first empty line.
  14553. Returns the number of empty lines passed."
  14554. (let ((pos (point)))
  14555. (skip-chars-backward " \t\n\r")
  14556. (beginning-of-line 2)
  14557. (goto-char (min (point) pos))
  14558. (count-lines (point) pos)))
  14559. (defun org-skip-whitespace ()
  14560. (skip-chars-forward " \t\n\r"))
  14561. (defun org-point-in-group (point group &optional context)
  14562. "Check if POINT is in match-group GROUP.
  14563. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14564. match. If the match group does ot exist or point is not inside it,
  14565. return nil."
  14566. (and (match-beginning group)
  14567. (>= point (match-beginning group))
  14568. (<= point (match-end group))
  14569. (if context
  14570. (list context (match-beginning group) (match-end group))
  14571. t)))
  14572. (defun org-switch-to-buffer-other-window (&rest args)
  14573. "Switch to buffer in a second window on the current frame.
  14574. In particular, do not allow pop-up frames."
  14575. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14576. special-display-function)
  14577. (apply 'switch-to-buffer-other-window args)))
  14578. (defun org-combine-plists (&rest plists)
  14579. "Create a single property list from all plists in PLISTS.
  14580. The process starts by copying the first list, and then setting properties
  14581. from the other lists. Settings in the last list are the most significant
  14582. ones and overrule settings in the other lists."
  14583. (let ((rtn (copy-sequence (pop plists)))
  14584. p v ls)
  14585. (while plists
  14586. (setq ls (pop plists))
  14587. (while ls
  14588. (setq p (pop ls) v (pop ls))
  14589. (setq rtn (plist-put rtn p v))))
  14590. rtn))
  14591. (defun org-move-line-down (arg)
  14592. "Move the current line down. With prefix argument, move it past ARG lines."
  14593. (interactive "p")
  14594. (let ((col (current-column))
  14595. beg end pos)
  14596. (beginning-of-line 1) (setq beg (point))
  14597. (beginning-of-line 2) (setq end (point))
  14598. (beginning-of-line (+ 1 arg))
  14599. (setq pos (move-marker (make-marker) (point)))
  14600. (insert (delete-and-extract-region beg end))
  14601. (goto-char pos)
  14602. (org-move-to-column col)))
  14603. (defun org-move-line-up (arg)
  14604. "Move the current line up. With prefix argument, move it past ARG lines."
  14605. (interactive "p")
  14606. (let ((col (current-column))
  14607. beg end pos)
  14608. (beginning-of-line 1) (setq beg (point))
  14609. (beginning-of-line 2) (setq end (point))
  14610. (beginning-of-line (- arg))
  14611. (setq pos (move-marker (make-marker) (point)))
  14612. (insert (delete-and-extract-region beg end))
  14613. (goto-char pos)
  14614. (org-move-to-column col)))
  14615. (defun org-replace-escapes (string table)
  14616. "Replace %-escapes in STRING with values in TABLE.
  14617. TABLE is an association list with keys like \"%a\" and string values.
  14618. The sequences in STRING may contain normal field width and padding information,
  14619. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14620. so values can contain further %-escapes if they are define later in TABLE."
  14621. (let ((case-fold-search nil)
  14622. e re rpl)
  14623. (while (setq e (pop table))
  14624. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14625. (while (string-match re string)
  14626. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14627. (cdr e)))
  14628. (setq string (replace-match rpl t t string))))
  14629. string))
  14630. (defun org-sublist (list start end)
  14631. "Return a section of LIST, from START to END.
  14632. Counting starts at 1."
  14633. (let (rtn (c start))
  14634. (setq list (nthcdr (1- start) list))
  14635. (while (and list (<= c end))
  14636. (push (pop list) rtn)
  14637. (setq c (1+ c)))
  14638. (nreverse rtn)))
  14639. (defun org-find-base-buffer-visiting (file)
  14640. "Like `find-buffer-visiting' but always return the base buffer and
  14641. not an indirect buffer."
  14642. (let ((buf (or (get-file-buffer file)
  14643. (find-buffer-visiting file))))
  14644. (if buf
  14645. (or (buffer-base-buffer buf) buf)
  14646. nil)))
  14647. (defun org-image-file-name-regexp (&optional extensions)
  14648. "Return regexp matching the file names of images.
  14649. If EXTENSIONS is given, only match these."
  14650. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14651. (image-file-name-regexp)
  14652. (let ((image-file-name-extensions
  14653. (or extensions
  14654. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14655. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14656. (concat "\\."
  14657. (regexp-opt (nconc (mapcar 'upcase
  14658. image-file-name-extensions)
  14659. image-file-name-extensions)
  14660. t)
  14661. "\\'"))))
  14662. (defun org-file-image-p (file &optional extensions)
  14663. "Return non-nil if FILE is an image."
  14664. (save-match-data
  14665. (string-match (org-image-file-name-regexp extensions) file)))
  14666. (defun org-get-cursor-date ()
  14667. "Return the date at cursor in as a time.
  14668. This works in the calendar and in the agenda, anywhere else it just
  14669. returns the current time."
  14670. (let (date day defd)
  14671. (cond
  14672. ((eq major-mode 'calendar-mode)
  14673. (setq date (calendar-cursor-to-date)
  14674. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14675. ((eq major-mode 'org-agenda-mode)
  14676. (setq day (get-text-property (point) 'day))
  14677. (if day
  14678. (setq date (calendar-gregorian-from-absolute day)
  14679. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14680. (nth 2 date))))))
  14681. (or defd (current-time))))
  14682. (defvar org-agenda-action-marker (make-marker)
  14683. "Marker pointing to the entry for the next agenda action.")
  14684. (defun org-mark-entry-for-agenda-action ()
  14685. "Mark the current entry as target of an agenda action.
  14686. Agenda actions are actions executed from the agenda with the key `k',
  14687. which make use of the date at the cursor."
  14688. (interactive)
  14689. (move-marker org-agenda-action-marker
  14690. (save-excursion (org-back-to-heading t) (point))
  14691. (current-buffer))
  14692. (message
  14693. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14694. ;;; Paragraph filling stuff.
  14695. ;; We want this to be just right, so use the full arsenal.
  14696. (defun org-indent-line-function ()
  14697. "Indent line like previous, but further if previous was headline or item."
  14698. (interactive)
  14699. (let* ((pos (point))
  14700. (itemp (org-at-item-p))
  14701. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14702. column bpos bcol tpos tcol bullet btype bullet-type)
  14703. ;; Find the previous relevant line
  14704. (beginning-of-line 1)
  14705. (cond
  14706. ((looking-at "#") (setq column 0))
  14707. ((looking-at "\\*+ ") (setq column 0))
  14708. ((and (looking-at "[ \t]*:END:")
  14709. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14710. (save-excursion
  14711. (goto-char (1- (match-beginning 1)))
  14712. (setq column (current-column))))
  14713. (t
  14714. (beginning-of-line 0)
  14715. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14716. (not (looking-at "[ \t]*:END:"))
  14717. (not (looking-at org-drawer-regexp)))
  14718. (beginning-of-line 0))
  14719. (cond
  14720. ((looking-at "\\*+[ \t]+")
  14721. (if (not org-adapt-indentation)
  14722. (setq column 0)
  14723. (goto-char (match-end 0))
  14724. (setq column (current-column))))
  14725. ((looking-at org-drawer-regexp)
  14726. (goto-char (1- (match-beginning 1)))
  14727. (setq column (current-column)))
  14728. ((looking-at "\\([ \t]*\\):END:")
  14729. (goto-char (match-end 1))
  14730. (setq column (current-column)))
  14731. ((org-in-item-p)
  14732. (org-beginning-of-item)
  14733. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14734. (setq bpos (match-beginning 1) tpos (match-end 0)
  14735. bcol (progn (goto-char bpos) (current-column))
  14736. tcol (progn (goto-char tpos) (current-column))
  14737. bullet (match-string 1)
  14738. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14739. (if (> tcol (+ bcol org-description-max-indent))
  14740. (setq tcol (+ bcol 5)))
  14741. (if (not itemp)
  14742. (setq column tcol)
  14743. (goto-char pos)
  14744. (beginning-of-line 1)
  14745. (if (looking-at "\\S-")
  14746. (progn
  14747. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14748. (setq bullet (match-string 1)
  14749. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14750. (setq column (if (equal btype bullet-type) bcol tcol)))
  14751. (setq column (org-get-indentation)))))
  14752. (t (setq column (org-get-indentation))))))
  14753. (goto-char pos)
  14754. (if (<= (current-column) (current-indentation))
  14755. (org-indent-line-to column)
  14756. (save-excursion (org-indent-line-to column)))
  14757. (setq column (current-column))
  14758. (beginning-of-line 1)
  14759. (if (looking-at
  14760. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14761. (replace-match (concat "\\1" (format org-property-format
  14762. (match-string 2) (match-string 3)))
  14763. t nil))
  14764. (org-move-to-column column)))
  14765. (defun org-set-autofill-regexps ()
  14766. (interactive)
  14767. ;; In the paragraph separator we include headlines, because filling
  14768. ;; text in a line directly attached to a headline would otherwise
  14769. ;; fill the headline as well.
  14770. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14771. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14772. ;; The paragraph starter includes hand-formatted lists.
  14773. (org-set-local 'paragraph-start
  14774. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14775. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14776. ;; But only if the user has not turned off tables or fixed-width regions
  14777. (org-set-local
  14778. 'auto-fill-inhibit-regexp
  14779. (concat "\\*+ \\|#\\+"
  14780. "\\|[ \t]*" org-keyword-time-regexp
  14781. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14782. (concat
  14783. "\\|[ \t]*["
  14784. (if org-enable-table-editor "|" "")
  14785. (if org-enable-fixed-width-editor ":" "")
  14786. "]"))))
  14787. ;; We use our own fill-paragraph function, to make sure that tables
  14788. ;; and fixed-width regions are not wrapped. That function will pass
  14789. ;; through to `fill-paragraph' when appropriate.
  14790. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14791. ; Adaptive filling: To get full control, first make sure that
  14792. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14793. (org-set-local 'adaptive-fill-regexp "\000")
  14794. (org-set-local 'adaptive-fill-function
  14795. 'org-adaptive-fill-function)
  14796. (org-set-local
  14797. 'align-mode-rules-list
  14798. '((org-in-buffer-settings
  14799. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14800. (modes . '(org-mode))))))
  14801. (defun org-fill-paragraph (&optional justify)
  14802. "Re-align a table, pass through to fill-paragraph if no table."
  14803. (let ((table-p (org-at-table-p))
  14804. (table.el-p (org-at-table.el-p)))
  14805. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14806. (save-excursion (goto-char (point-at-bol))
  14807. (looking-at outline-regexp)))
  14808. t) ; skip headlines
  14809. (table.el-p t) ; skip table.el tables
  14810. (table-p (org-table-align) t) ; align org-mode tables
  14811. (t nil)))) ; call paragraph-fill
  14812. ;; For reference, this is the default value of adaptive-fill-regexp
  14813. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14814. (defun org-adaptive-fill-function ()
  14815. "Return a fill prefix for org-mode files.
  14816. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14817. work correctly."
  14818. (cond ((looking-at "#[ \t]+")
  14819. (match-string 0))
  14820. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14821. (save-excursion
  14822. (if (> (match-end 1) (+ (match-beginning 1)
  14823. org-description-max-indent))
  14824. (goto-char (+ (match-beginning 1) 5))
  14825. (goto-char (match-end 0)))
  14826. (make-string (current-column) ?\ )))
  14827. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14828. (save-excursion
  14829. (goto-char (match-end 0))
  14830. (make-string (current-column) ?\ )))
  14831. (t nil)))
  14832. ;;; Other stuff.
  14833. (defun org-toggle-fixed-width-section (arg)
  14834. "Toggle the fixed-width export.
  14835. If there is no active region, the QUOTE keyword at the current headline is
  14836. inserted or removed. When present, it causes the text between this headline
  14837. and the next to be exported as fixed-width text, and unmodified.
  14838. If there is an active region, this command adds or removes a colon as the
  14839. first character of this line. If the first character of a line is a colon,
  14840. this line is also exported in fixed-width font."
  14841. (interactive "P")
  14842. (let* ((cc 0)
  14843. (regionp (org-region-active-p))
  14844. (beg (if regionp (region-beginning) (point)))
  14845. (end (if regionp (region-end)))
  14846. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14847. (case-fold-search nil)
  14848. (re "[ \t]*\\(: \\)")
  14849. off)
  14850. (if regionp
  14851. (save-excursion
  14852. (goto-char beg)
  14853. (setq cc (current-column))
  14854. (beginning-of-line 1)
  14855. (setq off (looking-at re))
  14856. (while (> nlines 0)
  14857. (setq nlines (1- nlines))
  14858. (beginning-of-line 1)
  14859. (cond
  14860. (arg
  14861. (org-move-to-column cc t)
  14862. (insert ": \n")
  14863. (forward-line -1))
  14864. ((and off (looking-at re))
  14865. (replace-match "" t t nil 1))
  14866. ((not off) (org-move-to-column cc t) (insert ": ")))
  14867. (forward-line 1)))
  14868. (save-excursion
  14869. (org-back-to-heading)
  14870. (if (looking-at (concat outline-regexp
  14871. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14872. (replace-match "" t t nil 1)
  14873. (if (looking-at outline-regexp)
  14874. (progn
  14875. (goto-char (match-end 0))
  14876. (insert org-quote-string " "))))))))
  14877. (defun org-reftex-citation ()
  14878. "Use reftex-citation to insert a citation into the buffer.
  14879. This looks for a line like
  14880. #+BIBLIOGRAPHY: foo plain option:-d
  14881. and derives from it that foo.bib is the bbliography file relevant
  14882. for this document. It then installs the necessary environment for RefTeX
  14883. to work in this buffer and calls `reftex-citation' to insert a citation
  14884. into the buffer.
  14885. Export of such citations to both LaTeX and HTML is handled by the contributed
  14886. package org-exp-bibtex by Taru Karttunen."
  14887. (interactive)
  14888. (let ((reftex-docstruct-symbol 'rds)
  14889. (reftex-cite-format "\\cite{%l}")
  14890. rds bib)
  14891. (save-excursion
  14892. (save-restriction
  14893. (widen)
  14894. (let ((case-fold-search t)
  14895. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14896. (if (not (save-excursion
  14897. (or (re-search-forward re nil t)
  14898. (re-search-backward re nil t))))
  14899. (error "No bibliography defined in file")
  14900. (setq bib (concat (match-string 1) ".bib")
  14901. rds (list (list 'bib bib)))))))
  14902. (call-interactively 'reftex-citation)))
  14903. ;;;; Functions extending outline functionality
  14904. (defun org-beginning-of-line (&optional arg)
  14905. "Go to the beginning of the current line. If that is invisible, continue
  14906. to a visible line beginning. This makes the function of C-a more intuitive.
  14907. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14908. first attempt, and only move to after the tags when the cursor is already
  14909. beyond the end of the headline."
  14910. (interactive "P")
  14911. (let ((pos (point))
  14912. (special (if (consp org-special-ctrl-a/e)
  14913. (car org-special-ctrl-a/e)
  14914. org-special-ctrl-a/e))
  14915. refpos)
  14916. (beginning-of-line 1)
  14917. (if (and arg (fboundp 'move-beginning-of-line))
  14918. (call-interactively 'move-beginning-of-line)
  14919. (if (bobp)
  14920. nil
  14921. (backward-char 1)
  14922. (if (org-invisible-p)
  14923. (while (and (not (bobp)) (org-invisible-p))
  14924. (backward-char 1)
  14925. (beginning-of-line 1))
  14926. (forward-char 1))))
  14927. (when special
  14928. (cond
  14929. ((and (looking-at org-complex-heading-regexp)
  14930. (= (char-after (match-end 1)) ?\ ))
  14931. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14932. (point-at-eol)))
  14933. (goto-char
  14934. (if (eq special t)
  14935. (cond ((> pos refpos) refpos)
  14936. ((= pos (point)) refpos)
  14937. (t (point)))
  14938. (cond ((> pos (point)) (point))
  14939. ((not (eq last-command this-command)) (point))
  14940. (t refpos)))))
  14941. ((org-at-item-p)
  14942. (goto-char
  14943. (if (eq special t)
  14944. (cond ((> pos (match-end 4)) (match-end 4))
  14945. ((= pos (point)) (match-end 4))
  14946. (t (point)))
  14947. (cond ((> pos (point)) (point))
  14948. ((not (eq last-command this-command)) (point))
  14949. (t (match-end 4))))))))
  14950. (org-no-warnings
  14951. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14952. (defun org-end-of-line (&optional arg)
  14953. "Go to the end of the line.
  14954. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14955. first attempt, and only move to after the tags when the cursor is already
  14956. beyond the end of the headline."
  14957. (interactive "P")
  14958. (let ((special (if (consp org-special-ctrl-a/e)
  14959. (cdr org-special-ctrl-a/e)
  14960. org-special-ctrl-a/e)))
  14961. (if (or (not special)
  14962. (not (org-on-heading-p))
  14963. arg)
  14964. (call-interactively (if (fboundp 'move-end-of-line)
  14965. 'move-end-of-line
  14966. 'end-of-line))
  14967. (let ((pos (point)))
  14968. (beginning-of-line 1)
  14969. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14970. (if (eq special t)
  14971. (if (or (< pos (match-beginning 1))
  14972. (= pos (match-end 0)))
  14973. (goto-char (match-beginning 1))
  14974. (goto-char (match-end 0)))
  14975. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14976. (goto-char (match-end 0))
  14977. (goto-char (match-beginning 1))))
  14978. (call-interactively (if (fboundp 'move-end-of-line)
  14979. 'move-end-of-line
  14980. 'end-of-line)))))
  14981. (org-no-warnings
  14982. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14983. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14984. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14985. (defun org-backward-sentence (&optional arg)
  14986. "Go to beginning of sentence, or beginning of table field.
  14987. This will call `backward-sentence' or `org-table-beginning-of-field',
  14988. depending on context."
  14989. (interactive "P")
  14990. (cond
  14991. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14992. (t (call-interactively 'backward-sentence))))
  14993. (defun org-forward-sentence (&optional arg)
  14994. "Go to end of sentence, or end of table field.
  14995. This will call `forward-sentence' or `org-table-end-of-field',
  14996. depending on context."
  14997. (interactive "P")
  14998. (cond
  14999. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15000. (t (call-interactively 'forward-sentence))))
  15001. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15002. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15003. (defun org-kill-line (&optional arg)
  15004. "Kill line, to tags or end of line."
  15005. (interactive "P")
  15006. (cond
  15007. ((or (not org-special-ctrl-k)
  15008. (bolp)
  15009. (not (org-on-heading-p)))
  15010. (call-interactively 'kill-line))
  15011. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15012. (kill-region (point) (match-beginning 1))
  15013. (org-set-tags nil t))
  15014. (t (kill-region (point) (point-at-eol)))))
  15015. (define-key org-mode-map "\C-k" 'org-kill-line)
  15016. (defun org-yank (&optional arg)
  15017. "Yank. If the kill is a subtree, treat it specially.
  15018. This command will look at the current kill and check if is a single
  15019. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15020. cursor is at the beginning of a line or after the stars of a currently
  15021. empty headline, then the yank is handled specially. How exactly depends
  15022. on the value of the following variables, both set by default.
  15023. org-yank-folded-subtrees
  15024. When set, the subtree(s) will be folded after insertion, but only
  15025. if doing so would now swallow text after the yanked text.
  15026. org-yank-adjusted-subtrees
  15027. When set, the subtree will be promoted or demoted in order to
  15028. fit into the local outline tree structure, which means that the level
  15029. will be adjusted so that it becomes the smaller one of the two
  15030. *visible* surrounding headings.
  15031. Any prefix to this command will cause `yank' to be called directly with
  15032. no special treatment. In particular, a simple `C-u' prefix will just
  15033. plainly yank the text as it is.
  15034. \[1] The test checks if the first non-white line is a heading
  15035. and if there are no other headings with fewer stars."
  15036. (interactive "P")
  15037. (org-yank-generic 'yank arg))
  15038. (defun org-yank-generic (command arg)
  15039. "Perform some yank-like command.
  15040. This function implements the behavior described in the `org-yank'
  15041. documentation. However, it has been generalized to work for any
  15042. interactive command with similar behavior."
  15043. ;; pretend to be command COMMAND
  15044. (setq this-command command)
  15045. (if arg
  15046. (call-interactively command)
  15047. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15048. (and (org-kill-is-subtree-p)
  15049. (or (bolp)
  15050. (and (looking-at "[ \t]*$")
  15051. (string-match
  15052. "\\`\\*+\\'"
  15053. (buffer-substring (point-at-bol) (point)))))))
  15054. swallowp)
  15055. (cond
  15056. ((and subtreep org-yank-folded-subtrees)
  15057. (let ((beg (point))
  15058. end)
  15059. (if (and subtreep org-yank-adjusted-subtrees)
  15060. (org-paste-subtree nil nil 'for-yank)
  15061. (call-interactively command))
  15062. (setq end (point))
  15063. (goto-char beg)
  15064. (when (and (bolp) subtreep
  15065. (not (setq swallowp
  15066. (org-yank-folding-would-swallow-text beg end))))
  15067. (or (looking-at outline-regexp)
  15068. (re-search-forward (concat "^" outline-regexp) end t))
  15069. (while (and (< (point) end) (looking-at outline-regexp))
  15070. (hide-subtree)
  15071. (org-cycle-show-empty-lines 'folded)
  15072. (condition-case nil
  15073. (outline-forward-same-level 1)
  15074. (error (goto-char end)))))
  15075. (when swallowp
  15076. (message
  15077. "Inserted text not folded because that would swallow text"))
  15078. (goto-char end)
  15079. (skip-chars-forward " \t\n\r")
  15080. (beginning-of-line 1)
  15081. (push-mark beg 'nomsg)))
  15082. ((and subtreep org-yank-adjusted-subtrees)
  15083. (let ((beg (point-at-bol)))
  15084. (org-paste-subtree nil nil 'for-yank)
  15085. (push-mark beg 'nomsg)))
  15086. (t
  15087. (call-interactively command))))))
  15088. (defun org-yank-folding-would-swallow-text (beg end)
  15089. "Would hide-subtree at BEG swallow any text after END?"
  15090. (let (level)
  15091. (save-excursion
  15092. (goto-char beg)
  15093. (when (or (looking-at outline-regexp)
  15094. (re-search-forward (concat "^" outline-regexp) end t))
  15095. (setq level (org-outline-level)))
  15096. (goto-char end)
  15097. (skip-chars-forward " \t\r\n\v\f")
  15098. (if (or (eobp)
  15099. (and (bolp) (looking-at org-outline-regexp)
  15100. (<= (org-outline-level) level)))
  15101. nil ; Nothing would be swallowed
  15102. t)))) ; something would swallow
  15103. (define-key org-mode-map "\C-y" 'org-yank)
  15104. (defun org-invisible-p ()
  15105. "Check if point is at a character currently not visible."
  15106. ;; Early versions of noutline don't have `outline-invisible-p'.
  15107. (if (fboundp 'outline-invisible-p)
  15108. (outline-invisible-p)
  15109. (get-char-property (point) 'invisible)))
  15110. (defun org-invisible-p2 ()
  15111. "Check if point is at a character currently not visible."
  15112. (save-excursion
  15113. (if (and (eolp) (not (bobp))) (backward-char 1))
  15114. ;; Early versions of noutline don't have `outline-invisible-p'.
  15115. (if (fboundp 'outline-invisible-p)
  15116. (outline-invisible-p)
  15117. (get-char-property (point) 'invisible))))
  15118. (defun org-back-to-heading (&optional invisible-ok)
  15119. "Call `outline-back-to-heading', but provide a better error message."
  15120. (condition-case nil
  15121. (outline-back-to-heading invisible-ok)
  15122. (error (error "Before first headline at position %d in buffer %s"
  15123. (point) (current-buffer)))))
  15124. (defun org-before-first-heading-p ()
  15125. "Before first heading?"
  15126. (save-excursion
  15127. (null (re-search-backward "^\\*+ " nil t))))
  15128. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15129. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15130. (defun org-at-heading-or-item-p ()
  15131. (or (org-on-heading-p) (org-at-item-p)))
  15132. (defun org-on-target-p ()
  15133. (or (org-in-regexp org-radio-target-regexp)
  15134. (org-in-regexp org-target-regexp)))
  15135. (defun org-up-heading-all (arg)
  15136. "Move to the heading line of which the present line is a subheading.
  15137. This function considers both visible and invisible heading lines.
  15138. With argument, move up ARG levels."
  15139. (if (fboundp 'outline-up-heading-all)
  15140. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15141. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15142. (defun org-up-heading-safe ()
  15143. "Move to the heading line of which the present line is a subheading.
  15144. This version will not throw an error. It will return the level of the
  15145. headline found, or nil if no higher level is found.
  15146. Also, this function will be a lot faster than `outline-up-heading',
  15147. because it relies on stars being the outline starters. This can really
  15148. make a significant difference in outlines with very many siblings."
  15149. (let (start-level re)
  15150. (org-back-to-heading t)
  15151. (setq start-level (funcall outline-level))
  15152. (if (equal start-level 1)
  15153. nil
  15154. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15155. (if (re-search-backward re nil t)
  15156. (funcall outline-level)))))
  15157. (defun org-first-sibling-p ()
  15158. "Is this heading the first child of its parents?"
  15159. (interactive)
  15160. (let ((re (concat "^" outline-regexp))
  15161. level l)
  15162. (unless (org-at-heading-p t)
  15163. (error "Not at a heading"))
  15164. (setq level (funcall outline-level))
  15165. (save-excursion
  15166. (if (not (re-search-backward re nil t))
  15167. t
  15168. (setq l (funcall outline-level))
  15169. (< l level)))))
  15170. (defun org-goto-sibling (&optional previous)
  15171. "Goto the next sibling, even if it is invisible.
  15172. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15173. when a sibling was found. When none is found, return nil and don't
  15174. move point."
  15175. (let ((fun (if previous 're-search-backward 're-search-forward))
  15176. (pos (point))
  15177. (re (concat "^" outline-regexp))
  15178. level l)
  15179. (when (condition-case nil (org-back-to-heading t) (error nil))
  15180. (setq level (funcall outline-level))
  15181. (catch 'exit
  15182. (or previous (forward-char 1))
  15183. (while (funcall fun re nil t)
  15184. (setq l (funcall outline-level))
  15185. (when (< l level) (goto-char pos) (throw 'exit nil))
  15186. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15187. (goto-char pos)
  15188. nil))))
  15189. (defun org-show-siblings ()
  15190. "Show all siblings of the current headline."
  15191. (save-excursion
  15192. (while (org-goto-sibling) (org-flag-heading nil)))
  15193. (save-excursion
  15194. (while (org-goto-sibling 'previous)
  15195. (org-flag-heading nil))))
  15196. (defun org-show-hidden-entry ()
  15197. "Show an entry where even the heading is hidden."
  15198. (save-excursion
  15199. (org-show-entry)))
  15200. (defun org-flag-heading (flag &optional entry)
  15201. "Flag the current heading. FLAG non-nil means make invisible.
  15202. When ENTRY is non-nil, show the entire entry."
  15203. (save-excursion
  15204. (org-back-to-heading t)
  15205. ;; Check if we should show the entire entry
  15206. (if entry
  15207. (progn
  15208. (org-show-entry)
  15209. (save-excursion
  15210. (and (outline-next-heading)
  15211. (org-flag-heading nil))))
  15212. (outline-flag-region (max (point-min) (1- (point)))
  15213. (save-excursion (outline-end-of-heading) (point))
  15214. flag))))
  15215. (defun org-forward-same-level (arg)
  15216. "Move forward to the ARG'th subheading at same level as this one.
  15217. Stop at the first and last subheadings of a superior heading.
  15218. This is like outline-forward-same-level, but invisible headings are ok."
  15219. (interactive "p")
  15220. (org-back-to-heading t)
  15221. (while (> arg 0)
  15222. (let ((point-to-move-to (save-excursion
  15223. (org-get-next-sibling))))
  15224. (if point-to-move-to
  15225. (progn
  15226. (goto-char point-to-move-to)
  15227. (setq arg (1- arg)))
  15228. (progn
  15229. (setq arg 0)
  15230. (error "No following same-level heading"))))))
  15231. (defun org-get-next-sibling ()
  15232. "Move to next heading of the same level, and return point.
  15233. If there is no such heading, return nil.
  15234. This is like outline-next-sibling, but invisible headings are ok."
  15235. (let ((level (funcall outline-level)))
  15236. (outline-next-heading)
  15237. (while (and (not (eobp)) (> (funcall outline-level) level))
  15238. (outline-next-heading))
  15239. (if (or (eobp) (< (funcall outline-level) level))
  15240. nil
  15241. (point))))
  15242. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15243. ;; This contains an exact copy of the original function, but it uses
  15244. ;; `org-back-to-heading', to make it work also in invisible
  15245. ;; trees. And is uses an invisible-OK argument.
  15246. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15247. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15248. ;; with many children and grandchildren etc, this can be much faster
  15249. ;; than the outline version.
  15250. (org-back-to-heading invisible-OK)
  15251. (let ((first t)
  15252. (level (funcall outline-level)))
  15253. (if (and (org-mode-p) (< level 1000))
  15254. ;; A true heading (not a plain list item), in Org-mode
  15255. ;; This means we can easily find the end by looking
  15256. ;; only for the right number of stars. Using a regexp to do
  15257. ;; this is so much faster than using a Lisp loop.
  15258. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15259. (forward-char 1)
  15260. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15261. ;; something else, do it the slow way
  15262. (while (and (not (eobp))
  15263. (or first (> (funcall outline-level) level)))
  15264. (setq first nil)
  15265. (outline-next-heading)))
  15266. (unless to-heading
  15267. (if (memq (preceding-char) '(?\n ?\^M))
  15268. (progn
  15269. ;; Go to end of line before heading
  15270. (forward-char -1)
  15271. (if (memq (preceding-char) '(?\n ?\^M))
  15272. ;; leave blank line before heading
  15273. (forward-char -1))))))
  15274. (point))
  15275. (defun org-show-subtree ()
  15276. "Show everything after this heading at deeper levels."
  15277. (outline-flag-region
  15278. (point)
  15279. (save-excursion
  15280. (outline-end-of-subtree) (outline-next-heading) (point))
  15281. nil))
  15282. (defun org-show-entry ()
  15283. "Show the body directly following this heading.
  15284. Show the heading too, if it is currently invisible."
  15285. (interactive)
  15286. (save-excursion
  15287. (condition-case nil
  15288. (progn
  15289. (org-back-to-heading t)
  15290. (outline-flag-region
  15291. (max (point-min) (1- (point)))
  15292. (save-excursion
  15293. (if (re-search-forward
  15294. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15295. (match-beginning 1)
  15296. (point-max)))
  15297. nil))
  15298. (error nil))))
  15299. (defun org-make-options-regexp (kwds &optional extra)
  15300. "Make a regular expression for keyword lines."
  15301. (concat
  15302. "^"
  15303. "#?[ \t]*\\+\\("
  15304. (mapconcat 'regexp-quote kwds "\\|")
  15305. (if extra (concat "\\|" extra))
  15306. "\\):[ \t]*"
  15307. "\\(.+\\)"))
  15308. ;; Make isearch reveal the necessary context
  15309. (defun org-isearch-end ()
  15310. "Reveal context after isearch exits."
  15311. (when isearch-success ; only if search was successful
  15312. (if (featurep 'xemacs)
  15313. ;; Under XEmacs, the hook is run in the correct place,
  15314. ;; we directly show the context.
  15315. (org-show-context 'isearch)
  15316. ;; In Emacs the hook runs *before* restoring the overlays.
  15317. ;; So we have to use a one-time post-command-hook to do this.
  15318. ;; (Emacs 22 has a special variable, see function `org-mode')
  15319. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15320. isearch-mode-end-hook-quit)
  15321. ;; Only when the isearch was not quitted.
  15322. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15323. 'append 'local)))))
  15324. (defun org-isearch-post-command ()
  15325. "Remove self from hook, and show context."
  15326. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15327. (org-show-context 'isearch))
  15328. ;;;; Integration with and fixes for other packages
  15329. ;;; Imenu support
  15330. (defvar org-imenu-markers nil
  15331. "All markers currently used by Imenu.")
  15332. (make-variable-buffer-local 'org-imenu-markers)
  15333. (defun org-imenu-new-marker (&optional pos)
  15334. "Return a new marker for use by Imenu, and remember the marker."
  15335. (let ((m (make-marker)))
  15336. (move-marker m (or pos (point)))
  15337. (push m org-imenu-markers)
  15338. m))
  15339. (defun org-imenu-get-tree ()
  15340. "Produce the index for Imenu."
  15341. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15342. (setq org-imenu-markers nil)
  15343. (let* ((n org-imenu-depth)
  15344. (re (concat "^" outline-regexp))
  15345. (subs (make-vector (1+ n) nil))
  15346. (last-level 0)
  15347. m level head)
  15348. (save-excursion
  15349. (save-restriction
  15350. (widen)
  15351. (goto-char (point-max))
  15352. (while (re-search-backward re nil t)
  15353. (setq level (org-reduced-level (funcall outline-level)))
  15354. (when (<= level n)
  15355. (looking-at org-complex-heading-regexp)
  15356. (setq head (org-link-display-format
  15357. (org-match-string-no-properties 4))
  15358. m (org-imenu-new-marker))
  15359. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15360. (if (>= level last-level)
  15361. (push (cons head m) (aref subs level))
  15362. (push (cons head (aref subs (1+ level))) (aref subs level))
  15363. (loop for i from (1+ level) to n do (aset subs i nil)))
  15364. (setq last-level level)))))
  15365. (aref subs 1)))
  15366. (eval-after-load "imenu"
  15367. '(progn
  15368. (add-hook 'imenu-after-jump-hook
  15369. (lambda ()
  15370. (if (eq major-mode 'org-mode)
  15371. (org-show-context 'org-goto))))))
  15372. (defun org-link-display-format (link)
  15373. "Replace a link with either the description, or the link target
  15374. if no description is present"
  15375. (save-match-data
  15376. (if (string-match org-bracket-link-analytic-regexp link)
  15377. (replace-match (or (match-string 5 link)
  15378. (concat (match-string 1 link)
  15379. (match-string 3 link)))
  15380. nil nil link)
  15381. link)))
  15382. ;; Speedbar support
  15383. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15384. "Overlay marking the agenda restriction line in speedbar.")
  15385. (org-overlay-put org-speedbar-restriction-lock-overlay
  15386. 'face 'org-agenda-restriction-lock)
  15387. (org-overlay-put org-speedbar-restriction-lock-overlay
  15388. 'help-echo "Agendas are currently limited to this item.")
  15389. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15390. (defun org-speedbar-set-agenda-restriction ()
  15391. "Restrict future agenda commands to the location at point in speedbar.
  15392. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15393. (interactive)
  15394. (require 'org-agenda)
  15395. (let (p m tp np dir txt)
  15396. (cond
  15397. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15398. 'org-imenu t))
  15399. (setq m (get-text-property p 'org-imenu-marker))
  15400. (save-excursion
  15401. (save-restriction
  15402. (set-buffer (marker-buffer m))
  15403. (goto-char m)
  15404. (org-agenda-set-restriction-lock 'subtree))))
  15405. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15406. 'speedbar-function 'speedbar-find-file))
  15407. (setq tp (previous-single-property-change
  15408. (1+ p) 'speedbar-function)
  15409. np (next-single-property-change
  15410. tp 'speedbar-function)
  15411. dir (speedbar-line-directory)
  15412. txt (buffer-substring-no-properties (or tp (point-min))
  15413. (or np (point-max))))
  15414. (save-excursion
  15415. (save-restriction
  15416. (set-buffer (find-file-noselect
  15417. (let ((default-directory dir))
  15418. (expand-file-name txt))))
  15419. (unless (org-mode-p)
  15420. (error "Cannot restrict to non-Org-mode file"))
  15421. (org-agenda-set-restriction-lock 'file))))
  15422. (t (error "Don't know how to restrict Org-mode's agenda")))
  15423. (org-move-overlay org-speedbar-restriction-lock-overlay
  15424. (point-at-bol) (point-at-eol))
  15425. (setq current-prefix-arg nil)
  15426. (org-agenda-maybe-redo)))
  15427. (eval-after-load "speedbar"
  15428. '(progn
  15429. (speedbar-add-supported-extension ".org")
  15430. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15431. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15432. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15433. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15434. (add-hook 'speedbar-visiting-tag-hook
  15435. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15436. ;;; Fixes and Hacks for problems with other packages
  15437. ;; Make flyspell not check words in links, to not mess up our keymap
  15438. (defun org-mode-flyspell-verify ()
  15439. "Don't let flyspell put overlays at active buttons."
  15440. (not (get-text-property (point) 'keymap)))
  15441. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15442. (eval-after-load "bookmark"
  15443. '(if (boundp 'bookmark-after-jump-hook)
  15444. ;; We can use the hook
  15445. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15446. ;; Hook not available, use advice
  15447. (defadvice bookmark-jump (after org-make-visible activate)
  15448. "Make the position visible."
  15449. (org-bookmark-jump-unhide))))
  15450. ;; Make sure saveplace show the location if it was hidden
  15451. (eval-after-load "saveplace"
  15452. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15453. "Make the position visible."
  15454. (org-bookmark-jump-unhide)))
  15455. (defun org-bookmark-jump-unhide ()
  15456. "Unhide the current position, to show the bookmark location."
  15457. (and (org-mode-p)
  15458. (or (org-invisible-p)
  15459. (save-excursion (goto-char (max (point-min) (1- (point))))
  15460. (org-invisible-p)))
  15461. (org-show-context 'bookmark-jump)))
  15462. ;; Make session.el ignore our circular variable
  15463. (eval-after-load "session"
  15464. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15465. ;;;; Experimental code
  15466. (defun org-closed-in-range ()
  15467. "Sparse tree of items closed in a certain time range.
  15468. Still experimental, may disappear in the future."
  15469. (interactive)
  15470. ;; Get the time interval from the user.
  15471. (let* ((time1 (time-to-seconds
  15472. (org-read-date nil 'to-time nil "Starting date: ")))
  15473. (time2 (time-to-seconds
  15474. (org-read-date nil 'to-time nil "End date:")))
  15475. ;; callback function
  15476. (callback (lambda ()
  15477. (let ((time
  15478. (time-to-seconds
  15479. (apply 'encode-time
  15480. (org-parse-time-string
  15481. (match-string 1))))))
  15482. ;; check if time in interval
  15483. (and (>= time time1) (<= time time2))))))
  15484. ;; make tree, check each match with the callback
  15485. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15486. ;;;; Finish up
  15487. (provide 'org)
  15488. (run-hooks 'org-load-hook)
  15489. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15490. ;;; org.el ends here