org.el 628 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.28trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.28trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let ((version (format "Org-mode version %s" org-version)))
  96. (message version)
  97. (if here
  98. (insert version))))
  99. ;;; Compatibility constants
  100. ;;; The custom variables
  101. (defgroup org nil
  102. "Outline-based notes management and organizer."
  103. :tag "Org"
  104. :group 'outlines
  105. :group 'hypermedia
  106. :group 'calendar)
  107. (defcustom org-load-hook nil
  108. "Hook that is run after org.el has been loaded."
  109. :group 'org
  110. :type 'hook)
  111. (defvar org-modules) ; defined below
  112. (defvar org-modules-loaded nil
  113. "Have the modules been loaded already?")
  114. (defun org-load-modules-maybe (&optional force)
  115. "Load all extensions listed in `org-modules'."
  116. (when (or force (not org-modules-loaded))
  117. (mapc (lambda (ext)
  118. (condition-case nil (require ext)
  119. (error (message "Problems while trying to load feature `%s'" ext))))
  120. org-modules)
  121. (setq org-modules-loaded t)))
  122. (defun org-set-modules (var value)
  123. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  124. (set var value)
  125. (when (featurep 'org)
  126. (org-load-modules-maybe 'force)))
  127. (when (org-bound-and-true-p org-modules)
  128. (let ((a (member 'org-infojs org-modules)))
  129. (and a (setcar a 'org-jsinfo))))
  130. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  131. "Modules that should always be loaded together with org.el.
  132. If a description starts with <C>, the file is not part of Emacs
  133. and loading it will require that you have downloaded and properly installed
  134. the org-mode distribution.
  135. You can also use this system to load external packages (i.e. neither Org
  136. core modules, not modules from the CONTRIB directory). Just add symbols
  137. to the end of the list. If the package is called org-xyz.el, then you need
  138. to add the symbol `xyz', and the package must have a call to
  139. (provide 'org-xyz)"
  140. :group 'org
  141. :set 'org-set-modules
  142. :type
  143. '(set :greedy t
  144. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  145. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  146. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  147. (const :tag " id: Global IDs for identifying entries" org-id)
  148. (const :tag " info: Links to Info nodes" org-info)
  149. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  150. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  151. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  152. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  153. (const :tag " mew Links to Mew folders/messages" org-mew)
  154. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  155. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  156. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  157. (const :tag " vm: Links to VM folders/messages" org-vm)
  158. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  159. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  160. (const :tag " mouse: Additional mouse support" org-mouse)
  161. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  162. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  163. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  164. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  165. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  166. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  167. (const :tag "C collector: Collect properties into tables" org-collector)
  168. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  169. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  170. (const :tag "C eval: Include command output as text" org-eval)
  171. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  172. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  173. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  174. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  175. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  176. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  177. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  178. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  179. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  180. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  181. (const :tag "C R: Computation using the R language" org-R)
  182. (const :tag "C registry: A registry for Org links" org-registry)
  183. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  184. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  185. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  186. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  187. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  188. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  189. (defcustom org-support-shift-select nil
  190. "Non-nil means, make shift-cursor commands select text when possible.
  191. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  192. selecting a region, or enlarge thusly regions started in this way.
  193. In Org-mode, in special contexts, these same keys are used for other
  194. purposes, important enough to compete with shift selection. Org tries
  195. to balance these needs by supporting `shift-select-mode' outside these
  196. special contexts, under control of this variable.
  197. The default of this variable is nil, to avoid confusing behavior. Shifted
  198. cursor keys will then execute Org commands in the following contexts:
  199. - on a headline, changing TODO state (left/right) and priority (up/down)
  200. - on a time stamp, changing the time
  201. - in a plain list item, changing the bullet type
  202. - in a property definition line, switching between allowed values
  203. - in the BEGIN line of a clock table (changing the time block).
  204. Outside these contexts, the commands will throw an error.
  205. When this variable is t and the cursor is not in a special context,
  206. Org-mode will support shift-selection for making and enlarging regions.
  207. To make this more effective, the bullet cycling will no longer happen
  208. anywhere in an item line, but only if the cursor is exactly on the bullet.
  209. If you set this variable to the symbol `always', then the keys
  210. will not be special in headlines, property lines, and item lines, to make
  211. shift selection work there as well. If this is what you want, you can
  212. use the following alternative commands: `C-c C-t' and `C-c ,' to
  213. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  214. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  215. edited by hand or in column view.
  216. However, when the cursor is on a timestamp, shift-cursor commands
  217. will still edit the time stamp - this is just too good to give up.
  218. XEmacs user should have this variable set to nil, because shift-select-mode
  219. is Emacs 23 only."
  220. :group 'org
  221. :type '(choice
  222. (const :tag "Never" nil)
  223. (const :tag "When outside special context" t)
  224. (const :tag "Everywhere except timestamps" always)))
  225. (defgroup org-startup nil
  226. "Options concerning startup of Org-mode."
  227. :tag "Org Startup"
  228. :group 'org)
  229. (defcustom org-startup-folded t
  230. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  231. This can also be configured on a per-file basis by adding one of
  232. the following lines anywhere in the buffer:
  233. #+STARTUP: fold
  234. #+STARTUP: nofold
  235. #+STARTUP: content"
  236. :group 'org-startup
  237. :type '(choice
  238. (const :tag "nofold: show all" nil)
  239. (const :tag "fold: overview" t)
  240. (const :tag "content: all headlines" content)))
  241. (defcustom org-startup-truncated t
  242. "Non-nil means, entering Org-mode will set `truncate-lines'.
  243. This is useful since some lines containing links can be very long and
  244. uninteresting. Also tables look terrible when wrapped."
  245. :group 'org-startup
  246. :type 'boolean)
  247. (defcustom org-startup-indented nil
  248. "Non-nil means, turn on `org-indent-mode' on startup.
  249. This can also be configured on a per-file basis by adding one of
  250. the following lines anywhere in the buffer:
  251. #+STARTUP: indent
  252. #+STARTUP: noindent"
  253. :group 'org-structure
  254. :type '(choice
  255. (const :tag "Not" nil)
  256. (const :tag "Globally (slow on startup in large files)" t)))
  257. (defcustom org-startup-align-all-tables nil
  258. "Non-nil means, align all tables when visiting a file.
  259. This is useful when the column width in tables is forced with <N> cookies
  260. in table fields. Such tables will look correct only after the first re-align.
  261. This can also be configured on a per-file basis by adding one of
  262. the following lines anywhere in the buffer:
  263. #+STARTUP: align
  264. #+STARTUP: noalign"
  265. :group 'org-startup
  266. :type 'boolean)
  267. (defcustom org-insert-mode-line-in-empty-file nil
  268. "Non-nil means insert the first line setting Org-mode in empty files.
  269. When the function `org-mode' is called interactively in an empty file, this
  270. normally means that the file name does not automatically trigger Org-mode.
  271. To ensure that the file will always be in Org-mode in the future, a
  272. line enforcing Org-mode will be inserted into the buffer, if this option
  273. has been set."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-replace-disputed-keys nil
  277. "Non-nil means use alternative key bindings for some keys.
  278. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  279. These keys are also used by other packages like shift-selection-mode'
  280. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  281. If you want to use Org-mode together with one of these other modes,
  282. or more generally if you would like to move some Org-mode commands to
  283. other keys, set this variable and configure the keys with the variable
  284. `org-disputed-keys'.
  285. This option is only relevant at load-time of Org-mode, and must be set
  286. *before* org.el is loaded. Changing it requires a restart of Emacs to
  287. become effective."
  288. :group 'org-startup
  289. :type 'boolean)
  290. (defcustom org-use-extra-keys nil
  291. "Non-nil means use extra key sequence definitions for certain
  292. commands. This happens automatically if you run XEmacs or if
  293. window-system is nil. This variable lets you do the same
  294. manually. You must set it before loading org.
  295. Example: on Carbon Emacs 22 running graphically, with an external
  296. keyboard on a Powerbook, the default way of setting M-left might
  297. not work for either Alt or ESC. Setting this variable will make
  298. it work for ESC."
  299. :group 'org-startup
  300. :type 'boolean)
  301. (if (fboundp 'defvaralias)
  302. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  303. (defcustom org-disputed-keys
  304. '(([(shift up)] . [(meta p)])
  305. ([(shift down)] . [(meta n)])
  306. ([(shift left)] . [(meta -)])
  307. ([(shift right)] . [(meta +)])
  308. ([(control shift right)] . [(meta shift +)])
  309. ([(control shift left)] . [(meta shift -)]))
  310. "Keys for which Org-mode and other modes compete.
  311. This is an alist, cars are the default keys, second element specifies
  312. the alternative to use when `org-replace-disputed-keys' is t.
  313. Keys can be specified in any syntax supported by `define-key'.
  314. The value of this option takes effect only at Org-mode's startup,
  315. therefore you'll have to restart Emacs to apply it after changing."
  316. :group 'org-startup
  317. :type 'alist)
  318. (defun org-key (key)
  319. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  320. Or return the original if not disputed."
  321. (if org-replace-disputed-keys
  322. (let* ((nkey (key-description key))
  323. (x (org-find-if (lambda (x)
  324. (equal (key-description (car x)) nkey))
  325. org-disputed-keys)))
  326. (if x (cdr x) key))
  327. key))
  328. (defun org-find-if (predicate seq)
  329. (catch 'exit
  330. (while seq
  331. (if (funcall predicate (car seq))
  332. (throw 'exit (car seq))
  333. (pop seq)))))
  334. (defun org-defkey (keymap key def)
  335. "Define a key, possibly translated, as returned by `org-key'."
  336. (define-key keymap (org-key key) def))
  337. (defcustom org-ellipsis nil
  338. "The ellipsis to use in the Org-mode outline.
  339. When nil, just use the standard three dots. When a string, use that instead,
  340. When a face, use the standard 3 dots, but with the specified face.
  341. The change affects only Org-mode (which will then use its own display table).
  342. Changing this requires executing `M-x org-mode' in a buffer to become
  343. effective."
  344. :group 'org-startup
  345. :type '(choice (const :tag "Default" nil)
  346. (face :tag "Face" :value org-warning)
  347. (string :tag "String" :value "...#")))
  348. (defvar org-display-table nil
  349. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  350. (defgroup org-keywords nil
  351. "Keywords in Org-mode."
  352. :tag "Org Keywords"
  353. :group 'org)
  354. (defcustom org-deadline-string "DEADLINE:"
  355. "String to mark deadline entries.
  356. A deadline is this string, followed by a time stamp. Should be a word,
  357. terminated by a colon. You can insert a schedule keyword and
  358. a timestamp with \\[org-deadline].
  359. Changes become only effective after restarting Emacs."
  360. :group 'org-keywords
  361. :type 'string)
  362. (defcustom org-scheduled-string "SCHEDULED:"
  363. "String to mark scheduled TODO entries.
  364. A schedule is this string, followed by a time stamp. Should be a word,
  365. terminated by a colon. You can insert a schedule keyword and
  366. a timestamp with \\[org-schedule].
  367. Changes become only effective after restarting Emacs."
  368. :group 'org-keywords
  369. :type 'string)
  370. (defcustom org-closed-string "CLOSED:"
  371. "String used as the prefix for timestamps logging closing a TODO entry."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defcustom org-clock-string "CLOCK:"
  375. "String used as prefix for timestamps clocking work hours on an item."
  376. :group 'org-keywords
  377. :type 'string)
  378. (defcustom org-comment-string "COMMENT"
  379. "Entries starting with this keyword will never be exported.
  380. An entry can be toggled between COMMENT and normal with
  381. \\[org-toggle-comment].
  382. Changes become only effective after restarting Emacs."
  383. :group 'org-keywords
  384. :type 'string)
  385. (defcustom org-quote-string "QUOTE"
  386. "Entries starting with this keyword will be exported in fixed-width font.
  387. Quoting applies only to the text in the entry following the headline, and does
  388. not extend beyond the next headline, even if that is lower level.
  389. An entry can be toggled between QUOTE and normal with
  390. \\[org-toggle-fixed-width-section]."
  391. :group 'org-keywords
  392. :type 'string)
  393. (defconst org-repeat-re
  394. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  395. "Regular expression for specifying repeated events.
  396. After a match, group 1 contains the repeat expression.")
  397. (defgroup org-structure nil
  398. "Options concerning the general structure of Org-mode files."
  399. :tag "Org Structure"
  400. :group 'org)
  401. (defgroup org-reveal-location nil
  402. "Options about how to make context of a location visible."
  403. :tag "Org Reveal Location"
  404. :group 'org-structure)
  405. (defconst org-context-choice
  406. '(choice
  407. (const :tag "Always" t)
  408. (const :tag "Never" nil)
  409. (repeat :greedy t :tag "Individual contexts"
  410. (cons
  411. (choice :tag "Context"
  412. (const agenda)
  413. (const org-goto)
  414. (const occur-tree)
  415. (const tags-tree)
  416. (const link-search)
  417. (const mark-goto)
  418. (const bookmark-jump)
  419. (const isearch)
  420. (const default))
  421. (boolean))))
  422. "Contexts for the reveal options.")
  423. (defcustom org-show-hierarchy-above '((default . t))
  424. "Non-nil means, show full hierarchy when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the hierarchy of headings
  427. above the exposed location is shown.
  428. Turning this off for example for sparse trees makes them very compact.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. Valid contexts are
  431. agenda when exposing an entry from the agenda
  432. org-goto when using the command `org-goto' on key C-c C-j
  433. occur-tree when using the command `org-occur' on key C-c /
  434. tags-tree when constructing a sparse tree based on tags matches
  435. link-search when exposing search matches associated with a link
  436. mark-goto when exposing the jump goal of a mark
  437. bookmark-jump when exposing a bookmark location
  438. isearch when exiting from an incremental search
  439. default default for all contexts not set explicitly"
  440. :group 'org-reveal-location
  441. :type org-context-choice)
  442. (defcustom org-show-following-heading '((default . nil))
  443. "Non-nil means, show following heading when revealing a location.
  444. Org-mode often shows locations in an org-mode file which might have
  445. been invisible before. When this is set, the heading following the
  446. match is shown.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-siblings '((default . nil) (isearch t))
  455. "Non-nil means, show all sibling heading when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the sibling of the current entry
  458. heading are all made visible. If `org-show-hierarchy-above' is t,
  459. the same happens on each level of the hierarchy above the current entry.
  460. By default this is on for the isearch context, off for all other contexts.
  461. Turning this off for example for sparse trees makes them very compact,
  462. but makes it harder to edit the location of the match. In such a case,
  463. use the command \\[org-reveal] to show more context.
  464. Instead of t, this can also be an alist specifying this option for different
  465. contexts. See `org-show-hierarchy-above' for valid contexts."
  466. :group 'org-reveal-location
  467. :type org-context-choice)
  468. (defcustom org-show-entry-below '((default . nil))
  469. "Non-nil means, show the entry below a headline when revealing a location.
  470. Org-mode often shows locations in an org-mode file which might have
  471. been invisible before. When this is set, the text below the headline that is
  472. exposed is also shown.
  473. By default this is off for all contexts.
  474. Instead of t, this can also be an alist specifying this option for different
  475. contexts. See `org-show-hierarchy-above' for valid contexts."
  476. :group 'org-reveal-location
  477. :type org-context-choice)
  478. (defcustom org-indirect-buffer-display 'other-window
  479. "How should indirect tree buffers be displayed?
  480. This applies to indirect buffers created with the commands
  481. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  482. Valid values are:
  483. current-window Display in the current window
  484. other-window Just display in another window.
  485. dedicated-frame Create one new frame, and re-use it each time.
  486. new-frame Make a new frame each time. Note that in this case
  487. previously-made indirect buffers are kept, and you need to
  488. kill these buffers yourself."
  489. :group 'org-structure
  490. :group 'org-agenda-windows
  491. :type '(choice
  492. (const :tag "In current window" current-window)
  493. (const :tag "In current frame, other window" other-window)
  494. (const :tag "Each time a new frame" new-frame)
  495. (const :tag "One dedicated frame" dedicated-frame)))
  496. (defgroup org-cycle nil
  497. "Options concerning visibility cycling in Org-mode."
  498. :tag "Org Cycle"
  499. :group 'org-structure)
  500. (defcustom org-cycle-skip-children-state-if-no-children t
  501. "Non-nil means, skip CHILDREN state in entries that don't have any."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-max-level nil
  505. "Maximum level which should still be subject to visibility cycling.
  506. Levels higher than this will, for cycling, be treated as text, not a headline.
  507. When `org-odd-levels-only' is set, a value of N in this variable actually
  508. means 2N-1 stars as the limiting headline.
  509. When nil, cycle all levels.
  510. Note that the limiting level of cycling is also influenced by
  511. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  512. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  513. than its value."
  514. :group 'org-cycle
  515. :type '(choice
  516. (const :tag "No limit" nil)
  517. (integer :tag "Maximum level")))
  518. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  519. "Names of drawers. Drawers are not opened by cycling on the headline above.
  520. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  521. this:
  522. :DRAWERNAME:
  523. .....
  524. :END:
  525. The drawer \"PROPERTIES\" is special for capturing properties through
  526. the property API.
  527. Drawers can be defined on the per-file basis with a line like:
  528. #+DRAWERS: HIDDEN STATE PROPERTIES"
  529. :group 'org-structure
  530. :group 'org-cycle
  531. :type '(repeat (string :tag "Drawer Name")))
  532. (defcustom org-hide-block-startup nil
  533. "Non-nil means, , entering Org-mode will fold all blocks.
  534. This can also be set in on a per-file basis with
  535. #+STARTUP: hideblocks
  536. #+STARTUP: showblocks"
  537. :group 'org-startup
  538. :group 'org-cycle
  539. :type 'boolean)
  540. (defcustom org-cycle-global-at-bob nil
  541. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  542. This makes it possible to do global cycling without having to use S-TAB or
  543. C-u TAB. For this special case to work, the first line of the buffer
  544. must not be a headline - it may be empty or some other text. When used in
  545. this way, `org-cycle-hook' is disables temporarily, to make sure the
  546. cursor stays at the beginning of the buffer.
  547. When this option is nil, don't do anything special at the beginning
  548. of the buffer."
  549. :group 'org-cycle
  550. :type 'boolean)
  551. (defcustom org-cycle-emulate-tab t
  552. "Where should `org-cycle' emulate TAB.
  553. nil Never
  554. white Only in completely white lines
  555. whitestart Only at the beginning of lines, before the first non-white char
  556. t Everywhere except in headlines
  557. exc-hl-bol Everywhere except at the start of a headline
  558. If TAB is used in a place where it does not emulate TAB, the current subtree
  559. visibility is cycled."
  560. :group 'org-cycle
  561. :type '(choice (const :tag "Never" nil)
  562. (const :tag "Only in completely white lines" white)
  563. (const :tag "Before first char in a line" whitestart)
  564. (const :tag "Everywhere except in headlines" t)
  565. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  566. ))
  567. (defcustom org-cycle-separator-lines 2
  568. "Number of empty lines needed to keep an empty line between collapsed trees.
  569. If you leave an empty line between the end of a subtree and the following
  570. headline, this empty line is hidden when the subtree is folded.
  571. Org-mode will leave (exactly) one empty line visible if the number of
  572. empty lines is equal or larger to the number given in this variable.
  573. So the default 2 means, at least 2 empty lines after the end of a subtree
  574. are needed to produce free space between a collapsed subtree and the
  575. following headline.
  576. Special case: when 0, never leave empty lines in collapsed view."
  577. :group 'org-cycle
  578. :type 'integer)
  579. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  580. (defcustom org-pre-cycle-hook nil
  581. "Hook that is run before visibility cycling is happening.
  582. The function(s) in this hook must accept a single argument which indicates
  583. the new state that will be set right after running this hook. The
  584. argument is a symbol. Before a global state change, it can have the values
  585. `overview', `content', or `all'. Before a local state change, it can have
  586. the values `folded', `children', or `subtree'."
  587. :group 'org-cycle
  588. :type 'hook)
  589. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  590. org-cycle-hide-drawers
  591. org-cycle-show-empty-lines
  592. org-optimize-window-after-visibility-change)
  593. "Hook that is run after `org-cycle' has changed the buffer visibility.
  594. The function(s) in this hook must accept a single argument which indicates
  595. the new state that was set by the most recent `org-cycle' command. The
  596. argument is a symbol. After a global state change, it can have the values
  597. `overview', `content', or `all'. After a local state change, it can have
  598. the values `folded', `children', or `subtree'."
  599. :group 'org-cycle
  600. :type 'hook)
  601. (defgroup org-edit-structure nil
  602. "Options concerning structure editing in Org-mode."
  603. :tag "Org Edit Structure"
  604. :group 'org-structure)
  605. (defcustom org-odd-levels-only nil
  606. "Non-nil means, skip even levels and only use odd levels for the outline.
  607. This has the effect that two stars are being added/taken away in
  608. promotion/demotion commands. It also influences how levels are
  609. handled by the exporters.
  610. Changing it requires restart of `font-lock-mode' to become effective
  611. for fontification also in regions already fontified.
  612. You may also set this on a per-file basis by adding one of the following
  613. lines to the buffer:
  614. #+STARTUP: odd
  615. #+STARTUP: oddeven"
  616. :group 'org-edit-structure
  617. :group 'org-font-lock
  618. :type 'boolean)
  619. (defcustom org-adapt-indentation t
  620. "Non-nil means, adapt indentation to outline node level.
  621. When this variable is set, Org assumes that you write outlines by
  622. indenting text in each node to align with the headline (after the stars).
  623. The following issues are influenced by this variable:
  624. - When this is set and the *entire* text in an entry is indented, the
  625. indentation is increased by one space in a demotion command, and
  626. decreased by one in a promotion command. If any line in the entry
  627. body starts with text at column 0, indentation is not changed at all.
  628. - Property drawers and planning information is inserted indented when
  629. this variable s set. When nil, they will not be indented.
  630. - TAB indents a line relative to context. The lines below a headline
  631. will be indented when this variable is set.
  632. Note that this is all about true indentation, by adding and removing
  633. space characters. See also `org-indent.el' which does level-dependent
  634. indentation in a virtual way, i.e. at display time in Emacs."
  635. :group 'org-edit-structure
  636. :type 'boolean)
  637. (defcustom org-special-ctrl-a/e nil
  638. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  639. When t, `C-a' will bring back the cursor to the beginning of the
  640. headline text, i.e. after the stars and after a possible TODO keyword.
  641. In an item, this will be the position after the bullet.
  642. When the cursor is already at that position, another `C-a' will bring
  643. it to the beginning of the line.
  644. `C-e' will jump to the end of the headline, ignoring the presence of tags
  645. in the headline. A second `C-e' will then jump to the true end of the
  646. line, after any tags.
  647. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  648. going to the true line boundary first. Only a directly following, identical
  649. keypress will bring the cursor to the special positions.
  650. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  651. set separately."
  652. :group 'org-edit-structure
  653. :type '(choice
  654. (const :tag "off" nil)
  655. (const :tag "after stars/bullet and before tags first" t)
  656. (const :tag "true line boundary first" reversed)
  657. (cons :tag "Set C-a and C-e separately"
  658. (choice :tag "Special C-a"
  659. (const :tag "off" nil)
  660. (const :tag "after stars/bullet first" t)
  661. (const :tag "before stars/bullet first" reversed))
  662. (choice :tag "Special C-e"
  663. (const :tag "off" nil)
  664. (const :tag "before tags first" t)
  665. (const :tag "after tags first" reversed)))))
  666. (if (fboundp 'defvaralias)
  667. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  668. (defcustom org-special-ctrl-k nil
  669. "Non-nil means `C-k' will behave specially in headlines.
  670. When nil, `C-k' will call the default `kill-line' command.
  671. When t, the following will happen while the cursor is in the headline:
  672. - When the cursor is at the beginning of a headline, kill the entire
  673. line and possible the folded subtree below the line.
  674. - When in the middle of the headline text, kill the headline up to the tags.
  675. - When after the headline text, kill the tags."
  676. :group 'org-edit-structure
  677. :type 'boolean)
  678. (defcustom org-yank-folded-subtrees t
  679. "Non-nil means, when yanking subtrees, fold them.
  680. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  681. it starts with a heading and all other headings in it are either children
  682. or siblings, then fold all the subtrees. However, do this only if no
  683. text after the yank would be swallowed into a folded tree by this action."
  684. :group 'org-edit-structure
  685. :type 'boolean)
  686. (defcustom org-yank-adjusted-subtrees nil
  687. "Non-nil means, when yanking subtrees, adjust the level.
  688. With this setting, `org-paste-subtree' is used to insert the subtree, see
  689. this function for details."
  690. :group 'org-edit-structure
  691. :type 'boolean)
  692. (defcustom org-M-RET-may-split-line '((default . t))
  693. "Non-nil means, M-RET will split the line at the cursor position.
  694. When nil, it will go to the end of the line before making a
  695. new line.
  696. You may also set this option in a different way for different
  697. contexts. Valid contexts are:
  698. headline when creating a new headline
  699. item when creating a new item
  700. table in a table field
  701. default the value to be used for all contexts not explicitly
  702. customized"
  703. :group 'org-structure
  704. :group 'org-table
  705. :type '(choice
  706. (const :tag "Always" t)
  707. (const :tag "Never" nil)
  708. (repeat :greedy t :tag "Individual contexts"
  709. (cons
  710. (choice :tag "Context"
  711. (const headline)
  712. (const item)
  713. (const table)
  714. (const default))
  715. (boolean)))))
  716. (defcustom org-insert-heading-respect-content nil
  717. "Non-nil means, insert new headings after the current subtree.
  718. When nil, the new heading is created directly after the current line.
  719. The commands \\[org-insert-heading-respect-content] and
  720. \\[org-insert-todo-heading-respect-content] turn this variable on
  721. for the duration of the command."
  722. :group 'org-structure
  723. :type 'boolean)
  724. (defcustom org-blank-before-new-entry '((heading . auto)
  725. (plain-list-item . auto))
  726. "Should `org-insert-heading' leave a blank line before new heading/item?
  727. The value is an alist, with `heading' and `plain-list-item' as car,
  728. and a boolean flag as cdr. For plain lists, if the variable
  729. `org-empty-line-terminates-plain-lists' is set, the setting here
  730. is ignored and no empty line is inserted, to keep the list in tact."
  731. :group 'org-edit-structure
  732. :type '(list
  733. (cons (const heading)
  734. (choice (const :tag "Never" nil)
  735. (const :tag "Always" t)
  736. (const :tag "Auto" auto)))
  737. (cons (const plain-list-item)
  738. (choice (const :tag "Never" nil)
  739. (const :tag "Always" t)
  740. (const :tag "Auto" auto)))))
  741. (defcustom org-insert-heading-hook nil
  742. "Hook being run after inserting a new heading."
  743. :group 'org-edit-structure
  744. :type 'hook)
  745. (defcustom org-enable-fixed-width-editor t
  746. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  747. This currently only means, they are never auto-wrapped.
  748. When nil, such lines will be treated like ordinary lines.
  749. See also the QUOTE keyword."
  750. :group 'org-edit-structure
  751. :type 'boolean)
  752. (defcustom org-goto-auto-isearch t
  753. "Non-nil means, typing characters in org-goto starts incremental search."
  754. :group 'org-edit-structure
  755. :type 'boolean)
  756. (defgroup org-sparse-trees nil
  757. "Options concerning sparse trees in Org-mode."
  758. :tag "Org Sparse Trees"
  759. :group 'org-structure)
  760. (defcustom org-highlight-sparse-tree-matches t
  761. "Non-nil means, highlight all matches that define a sparse tree.
  762. The highlights will automatically disappear the next time the buffer is
  763. changed by an edit command."
  764. :group 'org-sparse-trees
  765. :type 'boolean)
  766. (defcustom org-remove-highlights-with-change t
  767. "Non-nil means, any change to the buffer will remove temporary highlights.
  768. Such highlights are created by `org-occur' and `org-clock-display'.
  769. When nil, `C-c C-c needs to be used to get rid of the highlights.
  770. The highlights created by `org-preview-latex-fragment' always need
  771. `C-c C-c' to be removed."
  772. :group 'org-sparse-trees
  773. :group 'org-time
  774. :type 'boolean)
  775. (defcustom org-occur-hook '(org-first-headline-recenter)
  776. "Hook that is run after `org-occur' has constructed a sparse tree.
  777. This can be used to recenter the window to show as much of the structure
  778. as possible."
  779. :group 'org-sparse-trees
  780. :type 'hook)
  781. (defgroup org-imenu-and-speedbar nil
  782. "Options concerning imenu and speedbar in Org-mode."
  783. :tag "Org Imenu and Speedbar"
  784. :group 'org-structure)
  785. (defcustom org-imenu-depth 2
  786. "The maximum level for Imenu access to Org-mode headlines.
  787. This also applied for speedbar access."
  788. :group 'org-imenu-and-speedbar
  789. :type 'integer)
  790. (defgroup org-table nil
  791. "Options concerning tables in Org-mode."
  792. :tag "Org Table"
  793. :group 'org)
  794. (defcustom org-enable-table-editor 'optimized
  795. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  796. When nil, such lines will be treated like ordinary lines.
  797. When equal to the symbol `optimized', the table editor will be optimized to
  798. do the following:
  799. - Automatic overwrite mode in front of whitespace in table fields.
  800. This makes the structure of the table stay in tact as long as the edited
  801. field does not exceed the column width.
  802. - Minimize the number of realigns. Normally, the table is aligned each time
  803. TAB or RET are pressed to move to another field. With optimization this
  804. happens only if changes to a field might have changed the column width.
  805. Optimization requires replacing the functions `self-insert-command',
  806. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  807. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  808. very good at guessing when a re-align will be necessary, but you can always
  809. force one with \\[org-ctrl-c-ctrl-c].
  810. If you would like to use the optimized version in Org-mode, but the
  811. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  812. This variable can be used to turn on and off the table editor during a session,
  813. but in order to toggle optimization, a restart is required.
  814. See also the variable `org-table-auto-blank-field'."
  815. :group 'org-table
  816. :type '(choice
  817. (const :tag "off" nil)
  818. (const :tag "on" t)
  819. (const :tag "on, optimized" optimized)))
  820. (defcustom org-self-insert-cluster-for-undo t
  821. "Non-nil means cluster self-insert commands for undo when possible.
  822. If this is set, then, like in the Emacs command loop, 20 consequtive
  823. characters will be undone together.
  824. This is configurable, because there is some impact on typing performance."
  825. :group 'org-table
  826. :type 'boolean)
  827. (defcustom org-table-tab-recognizes-table.el t
  828. "Non-nil means, TAB will automatically notice a table.el table.
  829. When it sees such a table, it moves point into it and - if necessary -
  830. calls `table-recognize-table'."
  831. :group 'org-table-editing
  832. :type 'boolean)
  833. (defgroup org-link nil
  834. "Options concerning links in Org-mode."
  835. :tag "Org Link"
  836. :group 'org)
  837. (defvar org-link-abbrev-alist-local nil
  838. "Buffer-local version of `org-link-abbrev-alist', which see.
  839. The value of this is taken from the #+LINK lines.")
  840. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  841. (defcustom org-link-abbrev-alist nil
  842. "Alist of link abbreviations.
  843. The car of each element is a string, to be replaced at the start of a link.
  844. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  845. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  846. [[linkkey:tag][description]]
  847. The 'linkkey' must be a word word, starting with a letter, followed
  848. by letters, numbers, '-' or '_'.
  849. If REPLACE is a string, the tag will simply be appended to create the link.
  850. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  851. the placeholder \"%h\" will cause a url-encoded version of the tag to
  852. be inserted at that point (see the function `url-hexify-string').
  853. REPLACE may also be a function that will be called with the tag as the
  854. only argument to create the link, which should be returned as a string.
  855. See the manual for examples."
  856. :group 'org-link
  857. :type '(repeat
  858. (cons
  859. (string :tag "Protocol")
  860. (choice
  861. (string :tag "Format")
  862. (function)))))
  863. (defcustom org-descriptive-links t
  864. "Non-nil means, hide link part and only show description of bracket links.
  865. Bracket links are like [[link][description]]. This variable sets the initial
  866. state in new org-mode buffers. The setting can then be toggled on a
  867. per-buffer basis from the Org->Hyperlinks menu."
  868. :group 'org-link
  869. :type 'boolean)
  870. (defcustom org-link-file-path-type 'adaptive
  871. "How the path name in file links should be stored.
  872. Valid values are:
  873. relative Relative to the current directory, i.e. the directory of the file
  874. into which the link is being inserted.
  875. absolute Absolute path, if possible with ~ for home directory.
  876. noabbrev Absolute path, no abbreviation of home directory.
  877. adaptive Use relative path for files in the current directory and sub-
  878. directories of it. For other files, use an absolute path."
  879. :group 'org-link
  880. :type '(choice
  881. (const relative)
  882. (const absolute)
  883. (const noabbrev)
  884. (const adaptive)))
  885. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  886. "Types of links that should be activated in Org-mode files.
  887. This is a list of symbols, each leading to the activation of a certain link
  888. type. In principle, it does not hurt to turn on most link types - there may
  889. be a small gain when turning off unused link types. The types are:
  890. bracket The recommended [[link][description]] or [[link]] links with hiding.
  891. angular Links in angular brackets that may contain whitespace like
  892. <bbdb:Carsten Dominik>.
  893. plain Plain links in normal text, no whitespace, like http://google.com.
  894. radio Text that is matched by a radio target, see manual for details.
  895. tag Tag settings in a headline (link to tag search).
  896. date Time stamps (link to calendar).
  897. footnote Footnote labels.
  898. Changing this variable requires a restart of Emacs to become effective."
  899. :group 'org-link
  900. :type '(set :greedy t
  901. (const :tag "Double bracket links (new style)" bracket)
  902. (const :tag "Angular bracket links (old style)" angular)
  903. (const :tag "Plain text links" plain)
  904. (const :tag "Radio target matches" radio)
  905. (const :tag "Tags" tag)
  906. (const :tag "Timestamps" date)
  907. (const :tag "Footnotes" footnote)))
  908. (defcustom org-make-link-description-function nil
  909. "Function to use to generate link descriptions from links. If
  910. nil the link location will be used. This function must take two
  911. parameters; the first is the link and the second the description
  912. org-insert-link has generated, and should return the description
  913. to use."
  914. :group 'org-link
  915. :type 'function)
  916. (defgroup org-link-store nil
  917. "Options concerning storing links in Org-mode."
  918. :tag "Org Store Link"
  919. :group 'org-link)
  920. (defcustom org-email-link-description-format "Email %c: %.30s"
  921. "Format of the description part of a link to an email or usenet message.
  922. The following %-escapes will be replaced by corresponding information:
  923. %F full \"From\" field
  924. %f name, taken from \"From\" field, address if no name
  925. %T full \"To\" field
  926. %t first name in \"To\" field, address if no name
  927. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  928. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  929. %s subject
  930. %m message-id.
  931. You may use normal field width specification between the % and the letter.
  932. This is for example useful to limit the length of the subject.
  933. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  934. :group 'org-link-store
  935. :type 'string)
  936. (defcustom org-from-is-user-regexp
  937. (let (r1 r2)
  938. (when (and user-mail-address (not (string= user-mail-address "")))
  939. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  940. (when (and user-full-name (not (string= user-full-name "")))
  941. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  942. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  943. "Regexp matched against the \"From:\" header of an email or usenet message.
  944. It should match if the message is from the user him/herself."
  945. :group 'org-link-store
  946. :type 'regexp)
  947. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  948. "Non-nil means, storing a link to an Org file will use entry IDs.
  949. Note that before this variable is even considered, org-id must be loaded,
  950. so please customize `org-modules' and turn it on.
  951. The variable can have the following values:
  952. t Create an ID if needed to make a link to the current entry.
  953. create-if-interactive
  954. If `org-store-link' is called directly (interactively, as a user
  955. command), do create an ID to support the link. But when doing the
  956. job for remember, only use the ID if it already exists. The
  957. purpose of this setting is to avoid proliferation of unwanted
  958. IDs, just because you happen to be in an Org file when you
  959. call `org-remember' that automatically and preemptively
  960. creates a link. If you do want to get an ID link in a remember
  961. template to an entry not having an ID, create it first by
  962. explicitly creating a link to it, using `C-c C-l' first.
  963. create-if-interactive-and-no-custom-id
  964. Like create-if-interactive, but do not create an ID if there is
  965. a CUSTOM_ID property defined in the entry. This is the default.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if storing link interactively"
  974. create-if-interactive)
  975. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  976. create-if-interactive-and-no-custom-id)
  977. (const :tag "Only use existing" use-existing)
  978. (const :tag "Do not use ID to create link" nil)))
  979. (defcustom org-context-in-file-links t
  980. "Non-nil means, file links from `org-store-link' contain context.
  981. A search string will be added to the file name with :: as separator and
  982. used to find the context when the link is activated by the command
  983. `org-open-at-point'.
  984. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  985. negates this setting for the duration of the command."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defcustom org-keep-stored-link-after-insertion nil
  989. "Non-nil means, keep link in list for entire session.
  990. The command `org-store-link' adds a link pointing to the current
  991. location to an internal list. These links accumulate during a session.
  992. The command `org-insert-link' can be used to insert links into any
  993. Org-mode file (offering completion for all stored links). When this
  994. option is nil, every link which has been inserted once using \\[org-insert-link]
  995. will be removed from the list, to make completing the unused links
  996. more efficient."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defgroup org-link-follow nil
  1000. "Options concerning following links in Org-mode."
  1001. :tag "Org Follow Link"
  1002. :group 'org-link)
  1003. (defcustom org-link-translation-function nil
  1004. "Function to translate links with different syntax to Org syntax.
  1005. This can be used to translate links created for example by the Planner
  1006. or emacs-wiki packages to Org syntax.
  1007. The function must accept two parameters, a TYPE containing the link
  1008. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1009. which is everything after the link protocol. It should return a cons
  1010. with possibly modified values of type and path.
  1011. Org contains a function for this, so if you set this variable to
  1012. `org-translate-link-from-planner', you should be able follow many
  1013. links created by planner."
  1014. :group 'org-link-follow
  1015. :type 'function)
  1016. (defcustom org-follow-link-hook nil
  1017. "Hook that is run after a link has been followed."
  1018. :group 'org-link-follow
  1019. :type 'hook)
  1020. (defcustom org-tab-follows-link nil
  1021. "Non-nil means, on links TAB will follow the link.
  1022. Needs to be set before org.el is loaded.
  1023. This really should not be used, it does not make sense, and the
  1024. implementation is bad."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-return-follows-link nil
  1028. "Non-nil means, on links RET will follow the link.
  1029. Needs to be set before org.el is loaded."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-mouse-1-follows-link
  1033. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1034. "Non-nil means, mouse-1 on a link will follow the link.
  1035. A longer mouse click will still set point. Does not work on XEmacs.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mark-ring-length 4
  1040. "Number of different positions to be recorded in the ring
  1041. Changing this requires a restart of Emacs to work correctly."
  1042. :group 'org-link-follow
  1043. :type 'integer)
  1044. (defcustom org-link-frame-setup
  1045. '((vm . vm-visit-folder-other-frame)
  1046. (gnus . gnus-other-frame)
  1047. (file . find-file-other-window))
  1048. "Setup the frame configuration for following links.
  1049. When following a link with Emacs, it may often be useful to display
  1050. this link in another window or frame. This variable can be used to
  1051. set this up for the different types of links.
  1052. For VM, use any of
  1053. `vm-visit-folder'
  1054. `vm-visit-folder-other-frame'
  1055. For Gnus, use any of
  1056. `gnus'
  1057. `gnus-other-frame'
  1058. `org-gnus-no-new-news'
  1059. For FILE, use any of
  1060. `find-file'
  1061. `find-file-other-window'
  1062. `find-file-other-frame'
  1063. For the calendar, use the variable `calendar-setup'.
  1064. For BBDB, it is currently only possible to display the matches in
  1065. another window."
  1066. :group 'org-link-follow
  1067. :type '(list
  1068. (cons (const vm)
  1069. (choice
  1070. (const vm-visit-folder)
  1071. (const vm-visit-folder-other-window)
  1072. (const vm-visit-folder-other-frame)))
  1073. (cons (const gnus)
  1074. (choice
  1075. (const gnus)
  1076. (const gnus-other-frame)
  1077. (const org-gnus-no-new-news)))
  1078. (cons (const file)
  1079. (choice
  1080. (const find-file)
  1081. (const find-file-other-window)
  1082. (const find-file-other-frame)))))
  1083. (defcustom org-display-internal-link-with-indirect-buffer nil
  1084. "Non-nil means, use indirect buffer to display infile links.
  1085. Activating internal links (from one location in a file to another location
  1086. in the same file) normally just jumps to the location. When the link is
  1087. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1088. another window. When this option is set, the other window actually displays
  1089. an indirect buffer clone of the current buffer, to avoid any visibility
  1090. changes to the current buffer."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-open-non-existing-files nil
  1094. "Non-nil means, `org-open-file' will open non-existing files.
  1095. When nil, an error will be generated.
  1096. This variable applies only to external applications because they
  1097. might choke on non-existing files. If the link is to a file that
  1098. will be openend in Emacs, the variable is ignored."
  1099. :group 'org-link-follow
  1100. :type 'boolean)
  1101. (defcustom org-open-directory-means-index-dot-org nil
  1102. "Non-nil means, a link to a directory really means to index.org.
  1103. When nil, following a directory link will run dired or open a finder/explorer
  1104. window on that directory."
  1105. :group 'org-link-follow
  1106. :type 'boolean)
  1107. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1108. "Function and arguments to call for following mailto links.
  1109. This is a list with the first element being a lisp function, and the
  1110. remaining elements being arguments to the function. In string arguments,
  1111. %a will be replaced by the address, and %s will be replaced by the subject
  1112. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1113. :group 'org-link-follow
  1114. :type '(choice
  1115. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1116. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1117. (const :tag "message-mail" (message-mail "%a" "%s"))
  1118. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1119. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1120. "Non-nil means, ask for confirmation before executing shell links.
  1121. Shell links can be dangerous: just think about a link
  1122. [[shell:rm -rf ~/*][Google Search]]
  1123. This link would show up in your Org-mode document as \"Google Search\",
  1124. but really it would remove your entire home directory.
  1125. Therefore we advise against setting this variable to nil.
  1126. Just change it to `y-or-n-p' if you want to confirm with a
  1127. single keystroke rather than having to type \"yes\"."
  1128. :group 'org-link-follow
  1129. :type '(choice
  1130. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1131. (const :tag "with y-or-n (faster)" y-or-n-p)
  1132. (const :tag "no confirmation (dangerous)" nil)))
  1133. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1134. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1135. Elisp links can be dangerous: just think about a link
  1136. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1137. This link would show up in your Org-mode document as \"Google Search\",
  1138. but really it would remove your entire home directory.
  1139. Therefore we advise against setting this variable to nil.
  1140. Just change it to `y-or-n-p' if you want to confirm with a
  1141. single keystroke rather than having to type \"yes\"."
  1142. :group 'org-link-follow
  1143. :type '(choice
  1144. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1145. (const :tag "with y-or-n (faster)" y-or-n-p)
  1146. (const :tag "no confirmation (dangerous)" nil)))
  1147. (defconst org-file-apps-defaults-gnu
  1148. '((remote . emacs)
  1149. (system . mailcap)
  1150. (t . mailcap))
  1151. "Default file applications on a UNIX or GNU/Linux system.
  1152. See `org-file-apps'.")
  1153. (defconst org-file-apps-defaults-macosx
  1154. '((remote . emacs)
  1155. (t . "open %s")
  1156. (system . "open %s")
  1157. ("ps.gz" . "gv %s")
  1158. ("eps.gz" . "gv %s")
  1159. ("dvi" . "xdvi %s")
  1160. ("fig" . "xfig %s"))
  1161. "Default file applications on a MacOS X system.
  1162. The system \"open\" is known as a default, but we use X11 applications
  1163. for some files for which the OS does not have a good default.
  1164. See `org-file-apps'.")
  1165. (defconst org-file-apps-defaults-windowsnt
  1166. (list
  1167. '(remote . emacs)
  1168. (cons t
  1169. (list (if (featurep 'xemacs)
  1170. 'mswindows-shell-execute
  1171. 'w32-shell-execute)
  1172. "open" 'file))
  1173. (cons 'system
  1174. (list (if (featurep 'xemacs)
  1175. 'mswindows-shell-execute
  1176. 'w32-shell-execute)
  1177. "open" 'file)))
  1178. "Default file applications on a Windows NT system.
  1179. The system \"open\" is used for most files.
  1180. See `org-file-apps'.")
  1181. (defcustom org-file-apps
  1182. '(
  1183. (auto-mode . emacs)
  1184. ("\\.x?html?\\'" . default)
  1185. ("\\.pdf\\'" . default)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"regex\" Regular expression matched against the file name. For backward
  1194. compatibility, this can also be a string with only alphanumeric
  1195. characters, which is then interpreted as an extension.
  1196. `directory' Matches a directory
  1197. `remote' Matches a remote file, accessible through tramp or efs.
  1198. Remote files most likely should be visited through Emacs
  1199. because external applications cannot handle such paths.
  1200. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1201. so all files Emacs knows how to handle. Using this with
  1202. command `emacs' will open most files in Emacs. Beware that this
  1203. will also open html files inside Emacs, unless you add
  1204. (\"html\" . default) to the list as well.
  1205. t Default for files not matched by any of the other options.
  1206. `system' The system command to open files, like `open' on Windows
  1207. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1208. that will be selected if you call `C-c C-o' with a double
  1209. `C-u C-u' prefix.
  1210. Possible values for the command are:
  1211. `emacs' The file will be visited by the current Emacs process.
  1212. `default' Use the default application for this file type, which is the
  1213. association for t in the list, most likely in the system-specific
  1214. part.
  1215. This can be used to overrule an unwanted setting in the
  1216. system-specific variable.
  1217. `system' Use the system command for opening files, like \"open\".
  1218. This command is specified by the entry whose car is `system'.
  1219. Most likely, the system-specific version of this variable
  1220. does define this command, but you can overrule/replace it
  1221. here.
  1222. string A command to be executed by a shell; %s will be replaced
  1223. by the path to the file.
  1224. sexp A Lisp form which will be evaluated. The file path will
  1225. be available in the Lisp variable `file'.
  1226. For more examples, see the system specific constants
  1227. `org-file-apps-defaults-macosx'
  1228. `org-file-apps-defaults-windowsnt'
  1229. `org-file-apps-defaults-gnu'."
  1230. :group 'org-link-follow
  1231. :type '(repeat
  1232. (cons (choice :value ""
  1233. (string :tag "Extension")
  1234. (const :tag "System command to open files" system)
  1235. (const :tag "Default for unrecognized files" t)
  1236. (const :tag "Remote file" remote)
  1237. (const :tag "Links to a directory" directory)
  1238. (const :tag "Any files that have Emacs modes"
  1239. auto-mode))
  1240. (choice :value ""
  1241. (const :tag "Visit with Emacs" emacs)
  1242. (const :tag "Use default" default)
  1243. (const :tag "Use the system command" system)
  1244. (string :tag "Command")
  1245. (sexp :tag "Lisp form")))))
  1246. (defgroup org-refile nil
  1247. "Options concerning refiling entries in Org-mode."
  1248. :tag "Org Refile"
  1249. :group 'org)
  1250. (defcustom org-directory "~/org"
  1251. "Directory with org files.
  1252. This is just a default location to look for Org files. There is no need
  1253. at all to put your files into this directory. It is only used in the
  1254. following situations:
  1255. 1. When a remember template specifies a target file that is not an
  1256. absolute path. The path will then be interpreted relative to
  1257. `org-directory'
  1258. 2. When a remember note is filed away in an interactive way (when exiting the
  1259. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1260. with `org-directory' as the default path."
  1261. :group 'org-refile
  1262. :group 'org-remember
  1263. :type 'directory)
  1264. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1265. "Default target for storing notes.
  1266. Used by the hooks for remember.el. This can be a string, or nil to mean
  1267. the value of `remember-data-file'.
  1268. You can set this on a per-template basis with the variable
  1269. `org-remember-templates'."
  1270. :group 'org-refile
  1271. :group 'org-remember
  1272. :type '(choice
  1273. (const :tag "Default from remember-data-file" nil)
  1274. file))
  1275. (defcustom org-goto-interface 'outline
  1276. "The default interface to be used for `org-goto'.
  1277. Allowed values are:
  1278. outline The interface shows an outline of the relevant file
  1279. and the correct heading is found by moving through
  1280. the outline or by searching with incremental search.
  1281. outline-path-completion Headlines in the current buffer are offered via
  1282. completion. This is the interface also used by
  1283. the refile command."
  1284. :group 'org-refile
  1285. :type '(choice
  1286. (const :tag "Outline" outline)
  1287. (const :tag "Outline-path-completion" outline-path-completion)))
  1288. (defcustom org-goto-max-level 5
  1289. "Maximum level to be considered when running org-goto with refile interface."
  1290. :group 'org-refile
  1291. :type 'integer)
  1292. (defcustom org-reverse-note-order nil
  1293. "Non-nil means, store new notes at the beginning of a file or entry.
  1294. When nil, new notes will be filed to the end of a file or entry.
  1295. This can also be a list with cons cells of regular expressions that
  1296. are matched against file names, and values."
  1297. :group 'org-remember
  1298. :group 'org-refile
  1299. :type '(choice
  1300. (const :tag "Reverse always" t)
  1301. (const :tag "Reverse never" nil)
  1302. (repeat :tag "By file name regexp"
  1303. (cons regexp boolean))))
  1304. (defcustom org-refile-targets nil
  1305. "Targets for refiling entries with \\[org-refile].
  1306. This is list of cons cells. Each cell contains:
  1307. - a specification of the files to be considered, either a list of files,
  1308. or a symbol whose function or variable value will be used to retrieve
  1309. a file name or a list of file names. If you use `org-agenda-files' for
  1310. that, all agenda files will be scanned for targets. Nil means, consider
  1311. headings in the current buffer.
  1312. - A specification of how to find candidate refile targets. This may be
  1313. any of:
  1314. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1315. This tag has to be present in all target headlines, inheritance will
  1316. not be considered.
  1317. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1318. todo keyword.
  1319. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1320. headlines that are refiling targets.
  1321. - a cons cell (:level . N). Any headline of level N is considered a target.
  1322. Note that, when `org-odd-levels-only' is set, level corresponds to
  1323. order in hierarchy, not to the number of stars.
  1324. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1325. Note that, when `org-odd-levels-only' is set, level corresponds to
  1326. order in hierarchy, not to the number of stars.
  1327. You can set the variable `org-refile-target-verify-function' to a function
  1328. to verify each headline found by the simple critery above.
  1329. When this variable is nil, all top-level headlines in the current buffer
  1330. are used, equivalent to the value `((nil . (:level . 1))'."
  1331. :group 'org-refile
  1332. :type '(repeat
  1333. (cons
  1334. (choice :value org-agenda-files
  1335. (const :tag "All agenda files" org-agenda-files)
  1336. (const :tag "Current buffer" nil)
  1337. (function) (variable) (file))
  1338. (choice :tag "Identify target headline by"
  1339. (cons :tag "Specific tag" (const :value :tag) (string))
  1340. (cons :tag "TODO keyword" (const :value :todo) (string))
  1341. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1342. (cons :tag "Level number" (const :value :level) (integer))
  1343. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1344. (defcustom org-refile-target-verify-function nil
  1345. "Function to verify if the headline at point should be a refile target.
  1346. The function will be called without arguments, with point at the
  1347. beginning of the headline. It should return t and leave point
  1348. where it is if the headline is a valid target for refiling.
  1349. If the target should not be selected, the function must return nil.
  1350. In addition to this, it may move point to a place from where the search
  1351. should be continued. For example, the function may decide that the entire
  1352. subtree of the current entry should be excluded and move point to the end
  1353. of the subtree."
  1354. :group 'org-refile
  1355. :type 'function)
  1356. (defcustom org-refile-use-outline-path nil
  1357. "Non-nil means, provide refile targets as paths.
  1358. So a level 3 headline will be available as level1/level2/level3.
  1359. When the value is `file', also include the file name (without directory)
  1360. into the path. In this case, you can also stop the completion after
  1361. the file name, to get entries inserted as top level in the file.
  1362. When `full-file-path', include the full file path."
  1363. :group 'org-refile
  1364. :type '(choice
  1365. (const :tag "Not" nil)
  1366. (const :tag "Yes" t)
  1367. (const :tag "Start with file name" file)
  1368. (const :tag "Start with full file path" full-file-path)))
  1369. (defcustom org-outline-path-complete-in-steps t
  1370. "Non-nil means, complete the outline path in hierarchical steps.
  1371. When Org-mode uses the refile interface to select an outline path
  1372. \(see variable `org-refile-use-outline-path'), the completion of
  1373. the path can be done is a single go, or if can be done in steps down
  1374. the headline hierarchy. Going in steps is probably the best if you
  1375. do not use a special completion package like `ido' or `icicles'.
  1376. However, when using these packages, going in one step can be very
  1377. fast, while still showing the whole path to the entry."
  1378. :group 'org-refile
  1379. :type 'boolean)
  1380. (defcustom org-refile-allow-creating-parent-nodes nil
  1381. "Non-nil means, allow to create new nodes as refile targets.
  1382. New nodes are then created by adding \"/new node name\" to the completion
  1383. of an existing node. When the value of this variable is `confirm',
  1384. new node creation must be confirmed by the user (recommended)
  1385. When nil, the completion must match an existing entry.
  1386. Note that, if the new heading is not seen by the criteria
  1387. listed in `org-refile-targets', multiple instances of the same
  1388. heading would be created by trying again to file under the new
  1389. heading."
  1390. :group 'org-refile
  1391. :type '(choice
  1392. (const :tag "Never" nil)
  1393. (const :tag "Always" t)
  1394. (const :tag "Prompt for confirmation" confirm)))
  1395. (defgroup org-todo nil
  1396. "Options concerning TODO items in Org-mode."
  1397. :tag "Org TODO"
  1398. :group 'org)
  1399. (defgroup org-progress nil
  1400. "Options concerning Progress logging in Org-mode."
  1401. :tag "Org Progress"
  1402. :group 'org-time)
  1403. (defvar org-todo-interpretation-widgets
  1404. '(
  1405. (:tag "Sequence (cycling hits every state)" sequence)
  1406. (:tag "Type (cycling directly to DONE)" type))
  1407. "The available interpretation symbols for customizing
  1408. `org-todo-keywords'.
  1409. Interested libraries should add to this list.")
  1410. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1411. "List of TODO entry keyword sequences and their interpretation.
  1412. \\<org-mode-map>This is a list of sequences.
  1413. Each sequence starts with a symbol, either `sequence' or `type',
  1414. indicating if the keywords should be interpreted as a sequence of
  1415. action steps, or as different types of TODO items. The first
  1416. keywords are states requiring action - these states will select a headline
  1417. for inclusion into the global TODO list Org-mode produces. If one of
  1418. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1419. signify that no further action is necessary. If \"|\" is not found,
  1420. the last keyword is treated as the only DONE state of the sequence.
  1421. The command \\[org-todo] cycles an entry through these states, and one
  1422. additional state where no keyword is present. For details about this
  1423. cycling, see the manual.
  1424. TODO keywords and interpretation can also be set on a per-file basis with
  1425. the special #+SEQ_TODO and #+TYP_TODO lines.
  1426. Each keyword can optionally specify a character for fast state selection
  1427. \(in combination with the variable `org-use-fast-todo-selection')
  1428. and specifiers for state change logging, using the same syntax
  1429. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1430. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1431. indicates to record a time stamp each time this state is selected.
  1432. Each keyword may also specify if a timestamp or a note should be
  1433. recorded when entering or leaving the state, by adding additional
  1434. characters in the parenthesis after the keyword. This looks like this:
  1435. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1436. record only the time of the state change. With X and Y being either
  1437. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1438. Y when leaving the state if and only if the *target* state does not
  1439. define X. You may omit any of the fast-selection key or X or /Y,
  1440. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1441. For backward compatibility, this variable may also be just a list
  1442. of keywords - in this case the interpretation (sequence or type) will be
  1443. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1444. :group 'org-todo
  1445. :group 'org-keywords
  1446. :type '(choice
  1447. (repeat :tag "Old syntax, just keywords"
  1448. (string :tag "Keyword"))
  1449. (repeat :tag "New syntax"
  1450. (cons
  1451. (choice
  1452. :tag "Interpretation"
  1453. ;;Quick and dirty way to see
  1454. ;;`org-todo-interpretations'. This takes the
  1455. ;;place of item arguments
  1456. :convert-widget
  1457. (lambda (widget)
  1458. (widget-put widget
  1459. :args (mapcar
  1460. #'(lambda (x)
  1461. (widget-convert
  1462. (cons 'const x)))
  1463. org-todo-interpretation-widgets))
  1464. widget))
  1465. (repeat
  1466. (string :tag "Keyword"))))))
  1467. (defvar org-todo-keywords-1 nil
  1468. "All TODO and DONE keywords active in a buffer.")
  1469. (make-variable-buffer-local 'org-todo-keywords-1)
  1470. (defvar org-todo-keywords-for-agenda nil)
  1471. (defvar org-done-keywords-for-agenda nil)
  1472. (defvar org-todo-keyword-alist-for-agenda nil)
  1473. (defvar org-tag-alist-for-agenda nil)
  1474. (defvar org-agenda-contributing-files nil)
  1475. (defvar org-not-done-keywords nil)
  1476. (make-variable-buffer-local 'org-not-done-keywords)
  1477. (defvar org-done-keywords nil)
  1478. (make-variable-buffer-local 'org-done-keywords)
  1479. (defvar org-todo-heads nil)
  1480. (make-variable-buffer-local 'org-todo-heads)
  1481. (defvar org-todo-sets nil)
  1482. (make-variable-buffer-local 'org-todo-sets)
  1483. (defvar org-todo-log-states nil)
  1484. (make-variable-buffer-local 'org-todo-log-states)
  1485. (defvar org-todo-kwd-alist nil)
  1486. (make-variable-buffer-local 'org-todo-kwd-alist)
  1487. (defvar org-todo-key-alist nil)
  1488. (make-variable-buffer-local 'org-todo-key-alist)
  1489. (defvar org-todo-key-trigger nil)
  1490. (make-variable-buffer-local 'org-todo-key-trigger)
  1491. (defcustom org-todo-interpretation 'sequence
  1492. "Controls how TODO keywords are interpreted.
  1493. This variable is in principle obsolete and is only used for
  1494. backward compatibility, if the interpretation of todo keywords is
  1495. not given already in `org-todo-keywords'. See that variable for
  1496. more information."
  1497. :group 'org-todo
  1498. :group 'org-keywords
  1499. :type '(choice (const sequence)
  1500. (const type)))
  1501. (defcustom org-use-fast-todo-selection t
  1502. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1503. This variable describes if and under what circumstances the cycling
  1504. mechanism for TODO keywords will be replaced by a single-key, direct
  1505. selection scheme.
  1506. When nil, fast selection is never used.
  1507. When the symbol `prefix', it will be used when `org-todo' is called with
  1508. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1509. in an agenda buffer.
  1510. When t, fast selection is used by default. In this case, the prefix
  1511. argument forces cycling instead.
  1512. In all cases, the special interface is only used if access keys have actually
  1513. been assigned by the user, i.e. if keywords in the configuration are followed
  1514. by a letter in parenthesis, like TODO(t)."
  1515. :group 'org-todo
  1516. :type '(choice
  1517. (const :tag "Never" nil)
  1518. (const :tag "By default" t)
  1519. (const :tag "Only with C-u C-c C-t" prefix)))
  1520. (defcustom org-provide-todo-statistics t
  1521. "Non-nil means, update todo statistics after insert and toggle.
  1522. ALL-HEADLINES means update todo statistics by including headlines
  1523. with no TODO keyword as well, counting them as not done.
  1524. A list of TODO keywords means the same, but skip keywords that are
  1525. not in this list.
  1526. When this is set, todo statistics is updated in the parent of the
  1527. current entry each time a todo state is changed."
  1528. :group 'org-todo
  1529. :type '(choice
  1530. (const :tag "Yes, only for TODO entries" t)
  1531. (const :tag "Yes, including all entries" 'all-headlines)
  1532. (repeat :tag "Yes, for TODOs in this list"
  1533. (string :tag "TODO keyword"))
  1534. (other :tag "No TODO statistics" nil)))
  1535. (defcustom org-hierarchical-todo-statistics t
  1536. "Non-nil means, TODO statistics covers just direct children.
  1537. When nil, all entries in the subtree are considered.
  1538. This has only an effect if `org-provide-todo-statistics' is set."
  1539. :group 'org-todo
  1540. :type 'boolean)
  1541. (defcustom org-after-todo-state-change-hook nil
  1542. "Hook which is run after the state of a TODO item was changed.
  1543. The new state (a string with a TODO keyword, or nil) is available in the
  1544. Lisp variable `state'."
  1545. :group 'org-todo
  1546. :type 'hook)
  1547. (defvar org-blocker-hook nil
  1548. "Hook for functions that are allowed to block a state change.
  1549. Each function gets as its single argument a property list, see
  1550. `org-trigger-hook' for more information about this list.
  1551. If any of the functions in this hook returns nil, the state change
  1552. is blocked.")
  1553. (defvar org-trigger-hook nil
  1554. "Hook for functions that are triggered by a state change.
  1555. Each function gets as its single argument a property list with at least
  1556. the following elements:
  1557. (:type type-of-change :position pos-at-entry-start
  1558. :from old-state :to new-state)
  1559. Depending on the type, more properties may be present.
  1560. This mechanism is currently implemented for:
  1561. TODO state changes
  1562. ------------------
  1563. :type todo-state-change
  1564. :from previous state (keyword as a string), or nil, or a symbol
  1565. 'todo' or 'done', to indicate the general type of state.
  1566. :to new state, like in :from")
  1567. (defcustom org-enforce-todo-dependencies nil
  1568. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1569. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1570. be blocked if any prior sibling is not yet done.
  1571. Finally, if the parent is blocked because of ordered siblings of its own,
  1572. the child will also be blocked.
  1573. This variable needs to be set before org.el is loaded, and you need to
  1574. restart Emacs after a change to make the change effective. The only way
  1575. to change is while Emacs is running is through the customize interface."
  1576. :set (lambda (var val)
  1577. (set var val)
  1578. (if val
  1579. (add-hook 'org-blocker-hook
  1580. 'org-block-todo-from-children-or-siblings-or-parent)
  1581. (remove-hook 'org-blocker-hook
  1582. 'org-block-todo-from-children-or-siblings-or-parent)))
  1583. :group 'org-todo
  1584. :type 'boolean)
  1585. (defcustom org-enforce-todo-checkbox-dependencies nil
  1586. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1587. When this is nil, checkboxes have no influence on switching TODO states.
  1588. When non-nil, you first need to check off all check boxes before the TODO
  1589. entry can be switched to DONE.
  1590. This variable needs to be set before org.el is loaded, and you need to
  1591. restart Emacs after a change to make the change effective. The only way
  1592. to change is while Emacs is running is through the customize interface."
  1593. :set (lambda (var val)
  1594. (set var val)
  1595. (if val
  1596. (add-hook 'org-blocker-hook
  1597. 'org-block-todo-from-checkboxes)
  1598. (remove-hook 'org-blocker-hook
  1599. 'org-block-todo-from-checkboxes)))
  1600. :group 'org-todo
  1601. :type 'boolean)
  1602. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1603. "Non-nil means, inserting a TODO heading is treated as state change.
  1604. So when the command \\[org-insert-todo-heading] is used, state change
  1605. logging will apply if appropriate. When nil, the new TODO item will
  1606. be inserted directly, and no logging will take place."
  1607. :group 'org-todo
  1608. :type 'boolean)
  1609. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1610. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1611. This is the default behavior. However, setting this to nil allows a
  1612. convenient way to select a TODO state and bypass any logging associated
  1613. with that."
  1614. :group 'org-todo
  1615. :type 'boolean)
  1616. (defcustom org-todo-state-tags-triggers nil
  1617. "Tag changes that should be triggered by TODO state changes.
  1618. This is a list. Each entry is
  1619. (state-change (tag . flag) .......)
  1620. State-change can be a string with a state, and empty string to indicate the
  1621. state that has no TODO keyword, or it can be one of the symbols `todo'
  1622. or `done', meaning any not-done or done state, respectively."
  1623. :group 'org-todo
  1624. :group 'org-tags
  1625. :type '(repeat
  1626. (cons (choice :tag "When changing to"
  1627. (const :tag "Not-done state" todo)
  1628. (const :tag "Done state" done)
  1629. (string :tag "State"))
  1630. (repeat
  1631. (cons :tag "Tag action"
  1632. (string :tag "Tag")
  1633. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1634. (defcustom org-log-done nil
  1635. "Information to record when a task moves to the DONE state.
  1636. Possible values are:
  1637. nil Don't add anything, just change the keyword
  1638. time Add a time stamp to the task
  1639. note Prompt a closing note and add it with template `org-log-note-headings'
  1640. This option can also be set with on a per-file-basis with
  1641. #+STARTUP: nologdone
  1642. #+STARTUP: logdone
  1643. #+STARTUP: lognotedone
  1644. You can have local logging settings for a subtree by setting the LOGGING
  1645. property to one or more of these keywords."
  1646. :group 'org-todo
  1647. :group 'org-progress
  1648. :type '(choice
  1649. (const :tag "No logging" nil)
  1650. (const :tag "Record CLOSED timestamp" time)
  1651. (const :tag "Record CLOSED timestamp with closing note." note)))
  1652. ;; Normalize old uses of org-log-done.
  1653. (cond
  1654. ((eq org-log-done t) (setq org-log-done 'time))
  1655. ((and (listp org-log-done) (memq 'done org-log-done))
  1656. (setq org-log-done 'note)))
  1657. (defcustom org-log-note-clock-out nil
  1658. "Non-nil means, record a note when clocking out of an item.
  1659. This can also be configured on a per-file basis by adding one of
  1660. the following lines anywhere in the buffer:
  1661. #+STARTUP: lognoteclock-out
  1662. #+STARTUP: nolognoteclock-out"
  1663. :group 'org-todo
  1664. :group 'org-progress
  1665. :type 'boolean)
  1666. (defcustom org-log-done-with-time t
  1667. "Non-nil means, the CLOSED time stamp will contain date and time.
  1668. When nil, only the date will be recorded."
  1669. :group 'org-progress
  1670. :type 'boolean)
  1671. (defcustom org-log-note-headings
  1672. '((done . "CLOSING NOTE %t")
  1673. (state . "State %-12s from %-12S %t")
  1674. (note . "Note taken on %t")
  1675. (clock-out . ""))
  1676. "Headings for notes added to entries.
  1677. The value is an alist, with the car being a symbol indicating the note
  1678. context, and the cdr is the heading to be used. The heading may also be the
  1679. empty string.
  1680. %t in the heading will be replaced by a time stamp.
  1681. %s will be replaced by the new TODO state, in double quotes.
  1682. %S will be replaced by the old TODO state, in double quotes.
  1683. %u will be replaced by the user name.
  1684. %U will be replaced by the full user name."
  1685. :group 'org-todo
  1686. :group 'org-progress
  1687. :type '(list :greedy t
  1688. (cons (const :tag "Heading when closing an item" done) string)
  1689. (cons (const :tag
  1690. "Heading when changing todo state (todo sequence only)"
  1691. state) string)
  1692. (cons (const :tag "Heading when just taking a note" note) string)
  1693. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1694. (unless (assq 'note org-log-note-headings)
  1695. (push '(note . "%t") org-log-note-headings))
  1696. (defcustom org-log-into-drawer nil
  1697. "Non-nil means, insert state change notes and time stamps into a drawer.
  1698. When nil, state changes notes will be inserted after the headline and
  1699. any scheduling and clock lines, but not inside a drawer.
  1700. The value of this variable should be the name of the drawer to use.
  1701. LOGBOOK is proposed at the default drawer for this purpose, you can
  1702. also set this to a string to define the drawer of your choice.
  1703. A value of t is also allowed, representing \"LOGBOOK\".
  1704. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1705. will be ignored.
  1706. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1707. a subtree."
  1708. :group 'org-todo
  1709. :group 'org-progress
  1710. :type '(choice
  1711. (const :tag "Not into a drawer" nil)
  1712. (const :tag "LOGBOOK" t)
  1713. (string :tag "Other")))
  1714. (if (fboundp 'defvaralias)
  1715. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1716. (defun org-log-into-drawer ()
  1717. "Return the value of `org-log-into-drawer', but let properties overrule.
  1718. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1719. used instead of the default value."
  1720. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1721. (cond
  1722. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1723. ((equal p "t") "LOGBOOK")
  1724. (t p))))
  1725. (defcustom org-log-state-notes-insert-after-drawers nil
  1726. "Non-nil means, insert state change notes after any drawers in entry.
  1727. Only the drawers that *immediately* follow the headline and the
  1728. deadline/scheduled line are skipped.
  1729. When nil, insert notes right after the heading and perhaps the line
  1730. with deadline/scheduling if present.
  1731. This variable will have no effect if `org-log-into-drawer' is
  1732. set."
  1733. :group 'org-todo
  1734. :group 'org-progress
  1735. :type 'boolean)
  1736. (defcustom org-log-states-order-reversed t
  1737. "Non-nil means, the latest state change note will be directly after heading.
  1738. When nil, the notes will be orderer according to time."
  1739. :group 'org-todo
  1740. :group 'org-progress
  1741. :type 'boolean)
  1742. (defcustom org-log-repeat 'time
  1743. "Non-nil means, record moving through the DONE state when triggering repeat.
  1744. An auto-repeating tasks is immediately switched back to TODO when marked
  1745. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1746. the TODO keyword definition, or recording a closing note by setting
  1747. `org-log-done', there will be no record of the task moving through DONE.
  1748. This variable forces taking a note anyway. Possible values are:
  1749. nil Don't force a record
  1750. time Record a time stamp
  1751. note Record a note
  1752. This option can also be set with on a per-file-basis with
  1753. #+STARTUP: logrepeat
  1754. #+STARTUP: lognoterepeat
  1755. #+STARTUP: nologrepeat
  1756. You can have local logging settings for a subtree by setting the LOGGING
  1757. property to one or more of these keywords."
  1758. :group 'org-todo
  1759. :group 'org-progress
  1760. :type '(choice
  1761. (const :tag "Don't force a record" nil)
  1762. (const :tag "Force recording the DONE state" time)
  1763. (const :tag "Force recording a note with the DONE state" note)))
  1764. (defgroup org-priorities nil
  1765. "Priorities in Org-mode."
  1766. :tag "Org Priorities"
  1767. :group 'org-todo)
  1768. (defcustom org-enable-priority-commands t
  1769. "Non-nil means, priority commands are active.
  1770. When nil, these commands will be disabled, so that you never accidentally
  1771. set a priority."
  1772. :group 'org-priorities
  1773. :type 'boolean)
  1774. (defcustom org-highest-priority ?A
  1775. "The highest priority of TODO items. A character like ?A, ?B etc.
  1776. Must have a smaller ASCII number than `org-lowest-priority'."
  1777. :group 'org-priorities
  1778. :type 'character)
  1779. (defcustom org-lowest-priority ?C
  1780. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1781. Must have a larger ASCII number than `org-highest-priority'."
  1782. :group 'org-priorities
  1783. :type 'character)
  1784. (defcustom org-default-priority ?B
  1785. "The default priority of TODO items.
  1786. This is the priority an item get if no explicit priority is given."
  1787. :group 'org-priorities
  1788. :type 'character)
  1789. (defcustom org-priority-start-cycle-with-default t
  1790. "Non-nil means, start with default priority when starting to cycle.
  1791. When this is nil, the first step in the cycle will be (depending on the
  1792. command used) one higher or lower that the default priority."
  1793. :group 'org-priorities
  1794. :type 'boolean)
  1795. (defgroup org-time nil
  1796. "Options concerning time stamps and deadlines in Org-mode."
  1797. :tag "Org Time"
  1798. :group 'org)
  1799. (defcustom org-insert-labeled-timestamps-at-point nil
  1800. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1801. When nil, these labeled time stamps are forces into the second line of an
  1802. entry, just after the headline. When scheduling from the global TODO list,
  1803. the time stamp will always be forced into the second line."
  1804. :group 'org-time
  1805. :type 'boolean)
  1806. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1807. "Formats for `format-time-string' which are used for time stamps.
  1808. It is not recommended to change this constant.")
  1809. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1810. "Number of minutes to round time stamps to.
  1811. These are two values, the first applies when first creating a time stamp.
  1812. The second applies when changing it with the commands `S-up' and `S-down'.
  1813. When changing the time stamp, this means that it will change in steps
  1814. of N minutes, as given by the second value.
  1815. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1816. numbers should be factors of 60, so for example 5, 10, 15.
  1817. When this is larger than 1, you can still force an exact time-stamp by using
  1818. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1819. and by using a prefix arg to `S-up/down' to specify the exact number
  1820. of minutes to shift."
  1821. :group 'org-time
  1822. :get '(lambda (var) ; Make sure all entries have 5 elements
  1823. (if (integerp (default-value var))
  1824. (list (default-value var) 5)
  1825. (default-value var)))
  1826. :type '(list
  1827. (integer :tag "when inserting times")
  1828. (integer :tag "when modifying times")))
  1829. ;; Normalize old customizations of this variable.
  1830. (when (integerp org-time-stamp-rounding-minutes)
  1831. (setq org-time-stamp-rounding-minutes
  1832. (list org-time-stamp-rounding-minutes
  1833. org-time-stamp-rounding-minutes)))
  1834. (defcustom org-display-custom-times nil
  1835. "Non-nil means, overlay custom formats over all time stamps.
  1836. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1837. To turn this on on a per-file basis, insert anywhere in the file:
  1838. #+STARTUP: customtime"
  1839. :group 'org-time
  1840. :set 'set-default
  1841. :type 'sexp)
  1842. (make-variable-buffer-local 'org-display-custom-times)
  1843. (defcustom org-time-stamp-custom-formats
  1844. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1845. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1846. These are overlayed over the default ISO format if the variable
  1847. `org-display-custom-times' is set. Time like %H:%M should be at the
  1848. end of the second format. The custom formats are also honored by export
  1849. commands, if custom time display is turned on at the time of export."
  1850. :group 'org-time
  1851. :type 'sexp)
  1852. (defun org-time-stamp-format (&optional long inactive)
  1853. "Get the right format for a time string."
  1854. (let ((f (if long (cdr org-time-stamp-formats)
  1855. (car org-time-stamp-formats))))
  1856. (if inactive
  1857. (concat "[" (substring f 1 -1) "]")
  1858. f)))
  1859. (defcustom org-time-clocksum-format "%d:%02d"
  1860. "The format string used when creating CLOCKSUM lines, or when
  1861. org-mode generates a time duration."
  1862. :group 'org-time
  1863. :type 'string)
  1864. (defcustom org-deadline-warning-days 14
  1865. "No. of days before expiration during which a deadline becomes active.
  1866. This variable governs the display in sparse trees and in the agenda.
  1867. When 0 or negative, it means use this number (the absolute value of it)
  1868. even if a deadline has a different individual lead time specified.
  1869. Custom commands can set this variable in the options section."
  1870. :group 'org-time
  1871. :group 'org-agenda-daily/weekly
  1872. :type 'integer)
  1873. (defcustom org-read-date-prefer-future t
  1874. "Non-nil means, assume future for incomplete date input from user.
  1875. This affects the following situations:
  1876. 1. The user gives a day, but no month.
  1877. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1878. read this as the third of *next* month. However, if you enter \"17\",
  1879. it will be considered as *this* month.
  1880. 2. The user gives a month but not a year.
  1881. For example, if it is april and you enter \"feb 2\", this will be read
  1882. as feb 2, *next* year. \"May 5\", however, will be this year.
  1883. Currently this does not work for ISO week specifications.
  1884. When this option is nil, the current month and year will always be used
  1885. as defaults."
  1886. :group 'org-time
  1887. :type 'boolean)
  1888. (defcustom org-read-date-display-live t
  1889. "Non-nil means, display current interpretation of date prompt live.
  1890. This display will be in an overlay, in the minibuffer."
  1891. :group 'org-time
  1892. :type 'boolean)
  1893. (defcustom org-read-date-popup-calendar t
  1894. "Non-nil means, pop up a calendar when prompting for a date.
  1895. In the calendar, the date can be selected with mouse-1. However, the
  1896. minibuffer will also be active, and you can simply enter the date as well.
  1897. When nil, only the minibuffer will be available."
  1898. :group 'org-time
  1899. :type 'boolean)
  1900. (if (fboundp 'defvaralias)
  1901. (defvaralias 'org-popup-calendar-for-date-prompt
  1902. 'org-read-date-popup-calendar))
  1903. (defcustom org-read-date-minibuffer-setup-hook nil
  1904. "Hook to be used to set up keys for the date/time interface.
  1905. Add key definitions to `minibuffer-local-map', which will be a temporary
  1906. copy."
  1907. :group 'org-time
  1908. :type 'hook)
  1909. (defcustom org-extend-today-until 0
  1910. "The hour when your day really ends. Must be an integer.
  1911. This has influence for the following applications:
  1912. - When switching the agenda to \"today\". It it is still earlier than
  1913. the time given here, the day recognized as TODAY is actually yesterday.
  1914. - When a date is read from the user and it is still before the time given
  1915. here, the current date and time will be assumed to be yesterday, 23:59.
  1916. Also, timestamps inserted in remember templates follow this rule.
  1917. IMPORTANT: This is a feature whose implementation is and likely will
  1918. remain incomplete. Really, it is only here because past midnight seems to
  1919. be the favorite working time of John Wiegley :-)"
  1920. :group 'org-time
  1921. :type 'integer)
  1922. (defcustom org-edit-timestamp-down-means-later nil
  1923. "Non-nil means, S-down will increase the time in a time stamp.
  1924. When nil, S-up will increase."
  1925. :group 'org-time
  1926. :type 'boolean)
  1927. (defcustom org-calendar-follow-timestamp-change t
  1928. "Non-nil means, make the calendar window follow timestamp changes.
  1929. When a timestamp is modified and the calendar window is visible, it will be
  1930. moved to the new date."
  1931. :group 'org-time
  1932. :type 'boolean)
  1933. (defgroup org-tags nil
  1934. "Options concerning tags in Org-mode."
  1935. :tag "Org Tags"
  1936. :group 'org)
  1937. (defcustom org-tag-alist nil
  1938. "List of tags allowed in Org-mode files.
  1939. When this list is nil, Org-mode will base TAG input on what is already in the
  1940. buffer.
  1941. The value of this variable is an alist, the car of each entry must be a
  1942. keyword as a string, the cdr may be a character that is used to select
  1943. that tag through the fast-tag-selection interface.
  1944. See the manual for details."
  1945. :group 'org-tags
  1946. :type '(repeat
  1947. (choice
  1948. (cons (string :tag "Tag name")
  1949. (character :tag "Access char"))
  1950. (const :tag "Start radio group" (:startgroup))
  1951. (const :tag "End radio group" (:endgroup))
  1952. (const :tag "New line" (:newline)))))
  1953. (defcustom org-tag-persistent-alist nil
  1954. "List of tags that will always appear in all Org-mode files.
  1955. This is in addition to any in buffer settings or customizations
  1956. of `org-tag-alist'.
  1957. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1958. The value of this variable is an alist, the car of each entry must be a
  1959. keyword as a string, the cdr may be a character that is used to select
  1960. that tag through the fast-tag-selection interface.
  1961. See the manual for details.
  1962. To disable these tags on a per-file basis, insert anywhere in the file:
  1963. #+STARTUP: noptag"
  1964. :group 'org-tags
  1965. :type '(repeat
  1966. (choice
  1967. (cons (string :tag "Tag name")
  1968. (character :tag "Access char"))
  1969. (const :tag "Start radio group" (:startgroup))
  1970. (const :tag "End radio group" (:endgroup))
  1971. (const :tag "New line" (:newline)))))
  1972. (defvar org-file-tags nil
  1973. "List of tags that can be inherited by all entries in the file.
  1974. The tags will be inherited if the variable `org-use-tag-inheritance'
  1975. says they should be.
  1976. This variable is populated from #+TAG lines.")
  1977. (defcustom org-use-fast-tag-selection 'auto
  1978. "Non-nil means, use fast tag selection scheme.
  1979. This is a special interface to select and deselect tags with single keys.
  1980. When nil, fast selection is never used.
  1981. When the symbol `auto', fast selection is used if and only if selection
  1982. characters for tags have been configured, either through the variable
  1983. `org-tag-alist' or through a #+TAGS line in the buffer.
  1984. When t, fast selection is always used and selection keys are assigned
  1985. automatically if necessary."
  1986. :group 'org-tags
  1987. :type '(choice
  1988. (const :tag "Always" t)
  1989. (const :tag "Never" nil)
  1990. (const :tag "When selection characters are configured" 'auto)))
  1991. (defcustom org-fast-tag-selection-single-key nil
  1992. "Non-nil means, fast tag selection exits after first change.
  1993. When nil, you have to press RET to exit it.
  1994. During fast tag selection, you can toggle this flag with `C-c'.
  1995. This variable can also have the value `expert'. In this case, the window
  1996. displaying the tags menu is not even shown, until you press C-c again."
  1997. :group 'org-tags
  1998. :type '(choice
  1999. (const :tag "No" nil)
  2000. (const :tag "Yes" t)
  2001. (const :tag "Expert" expert)))
  2002. (defvar org-fast-tag-selection-include-todo nil
  2003. "Non-nil means, fast tags selection interface will also offer TODO states.
  2004. This is an undocumented feature, you should not rely on it.")
  2005. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2006. "The column to which tags should be indented in a headline.
  2007. If this number is positive, it specifies the column. If it is negative,
  2008. it means that the tags should be flushright to that column. For example,
  2009. -80 works well for a normal 80 character screen."
  2010. :group 'org-tags
  2011. :type 'integer)
  2012. (defcustom org-auto-align-tags t
  2013. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2014. These operations change the length of a headline and therefore shift
  2015. the tags around. With this options turned on, after each such operation
  2016. the tags are again aligned to `org-tags-column'."
  2017. :group 'org-tags
  2018. :type 'boolean)
  2019. (defcustom org-use-tag-inheritance t
  2020. "Non-nil means, tags in levels apply also for sublevels.
  2021. When nil, only the tags directly given in a specific line apply there.
  2022. This may also be a list of tags that should be inherited, or a regexp that
  2023. matches tags that should be inherited. Additional control is possible
  2024. with the variable `org-tags-exclude-from-inheritance' which gives an
  2025. explicit list of tags to be excluded from inheritance., even if the value of
  2026. `org-use-tag-inheritance' would select it for inheritance.
  2027. If this option is t, a match early-on in a tree can lead to a large
  2028. number of matches in the subtree when constructing the agenda or creating
  2029. a sparse tree. If you only want to see the first match in a tree during
  2030. a search, check out the variable `org-tags-match-list-sublevels'."
  2031. :group 'org-tags
  2032. :type '(choice
  2033. (const :tag "Not" nil)
  2034. (const :tag "Always" t)
  2035. (repeat :tag "Specific tags" (string :tag "Tag"))
  2036. (regexp :tag "Tags matched by regexp")))
  2037. (defcustom org-tags-exclude-from-inheritance nil
  2038. "List of tags that should never be inherited.
  2039. This is a way to exclude a few tags from inheritance. For way to do
  2040. the opposite, to actively allow inheritance for selected tags,
  2041. see the variable `org-use-tag-inheritance'."
  2042. :group 'org-tags
  2043. :type '(repeat (string :tag "Tag")))
  2044. (defun org-tag-inherit-p (tag)
  2045. "Check if TAG is one that should be inherited."
  2046. (cond
  2047. ((member tag org-tags-exclude-from-inheritance) nil)
  2048. ((eq org-use-tag-inheritance t) t)
  2049. ((not org-use-tag-inheritance) nil)
  2050. ((stringp org-use-tag-inheritance)
  2051. (string-match org-use-tag-inheritance tag))
  2052. ((listp org-use-tag-inheritance)
  2053. (member tag org-use-tag-inheritance))
  2054. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2055. (defcustom org-tags-match-list-sublevels t
  2056. "Non-nil means list also sublevels of headlines matching a search.
  2057. This variable applies to tags/property searches, and also to stuck
  2058. projects because this search is based on a tags match as well.
  2059. When set to the symbol `indented', sublevels are indented with
  2060. leading dots.
  2061. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2062. the sublevels of a headline matching a tag search often also match
  2063. the same search. Listing all of them can create very long lists.
  2064. Setting this variable to nil causes subtrees of a match to be skipped.
  2065. This variable is semi-obsolete and probably should always be true. It
  2066. is better to limit inheritance to certain tags using the variables
  2067. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2068. :group 'org-tags
  2069. :type '(choice
  2070. (const :tag "No, don't list them" nil)
  2071. (const :tag "Yes, do list them" t)
  2072. (const :tag "List them, indented with leading dots" indented)))
  2073. (defcustom org-tags-sort-function nil
  2074. "When set, tags are sorted using this function as a comparator"
  2075. :group 'org-tags
  2076. :type '(choice
  2077. (const :tag "No sorting" nil)
  2078. (const :tag "Alphabetical" string<)
  2079. (const :tag "Reverse alphabetical" string>)
  2080. (function :tag "Custom function" nil)))
  2081. (defvar org-tags-history nil
  2082. "History of minibuffer reads for tags.")
  2083. (defvar org-last-tags-completion-table nil
  2084. "The last used completion table for tags.")
  2085. (defvar org-after-tags-change-hook nil
  2086. "Hook that is run after the tags in a line have changed.")
  2087. (defgroup org-properties nil
  2088. "Options concerning properties in Org-mode."
  2089. :tag "Org Properties"
  2090. :group 'org)
  2091. (defcustom org-property-format "%-10s %s"
  2092. "How property key/value pairs should be formatted by `indent-line'.
  2093. When `indent-line' hits a property definition, it will format the line
  2094. according to this format, mainly to make sure that the values are
  2095. lined-up with respect to each other."
  2096. :group 'org-properties
  2097. :type 'string)
  2098. (defcustom org-use-property-inheritance nil
  2099. "Non-nil means, properties apply also for sublevels.
  2100. This setting is chiefly used during property searches. Turning it on can
  2101. cause significant overhead when doing a search, which is why it is not
  2102. on by default.
  2103. When nil, only the properties directly given in the current entry count.
  2104. When t, every property is inherited. The value may also be a list of
  2105. properties that should have inheritance, or a regular expression matching
  2106. properties that should be inherited.
  2107. However, note that some special properties use inheritance under special
  2108. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2109. and the properties ending in \"_ALL\" when they are used as descriptor
  2110. for valid values of a property.
  2111. Note for programmers:
  2112. When querying an entry with `org-entry-get', you can control if inheritance
  2113. should be used. By default, `org-entry-get' looks only at the local
  2114. properties. You can request inheritance by setting the inherit argument
  2115. to t (to force inheritance) or to `selective' (to respect the setting
  2116. in this variable)."
  2117. :group 'org-properties
  2118. :type '(choice
  2119. (const :tag "Not" nil)
  2120. (const :tag "Always" t)
  2121. (repeat :tag "Specific properties" (string :tag "Property"))
  2122. (regexp :tag "Properties matched by regexp")))
  2123. (defun org-property-inherit-p (property)
  2124. "Check if PROPERTY is one that should be inherited."
  2125. (cond
  2126. ((eq org-use-property-inheritance t) t)
  2127. ((not org-use-property-inheritance) nil)
  2128. ((stringp org-use-property-inheritance)
  2129. (string-match org-use-property-inheritance property))
  2130. ((listp org-use-property-inheritance)
  2131. (member property org-use-property-inheritance))
  2132. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2133. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2134. "The default column format, if no other format has been defined.
  2135. This variable can be set on the per-file basis by inserting a line
  2136. #+COLUMNS: %25ITEM ....."
  2137. :group 'org-properties
  2138. :type 'string)
  2139. (defcustom org-columns-ellipses ".."
  2140. "The ellipses to be used when a field in column view is truncated.
  2141. When this is the empty string, as many characters as possible are shown,
  2142. but then there will be no visual indication that the field has been truncated.
  2143. When this is a string of length N, the last N characters of a truncated
  2144. field are replaced by this string. If the column is narrower than the
  2145. ellipses string, only part of the ellipses string will be shown."
  2146. :group 'org-properties
  2147. :type 'string)
  2148. (defcustom org-columns-modify-value-for-display-function nil
  2149. "Function that modifies values for display in column view.
  2150. For example, it can be used to cut out a certain part from a time stamp.
  2151. The function must take 2 arguments:
  2152. column-title The title of the column (*not* the property name)
  2153. value The value that should be modified.
  2154. The function should return the value that should be displayed,
  2155. or nil if the normal value should be used."
  2156. :group 'org-properties
  2157. :type 'function)
  2158. (defcustom org-effort-property "Effort"
  2159. "The property that is being used to keep track of effort estimates.
  2160. Effort estimates given in this property need to have the format H:MM."
  2161. :group 'org-properties
  2162. :group 'org-progress
  2163. :type '(string :tag "Property"))
  2164. (defconst org-global-properties-fixed
  2165. '(("VISIBILITY_ALL" . "folded children content all")
  2166. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2167. "List of property/value pairs that can be inherited by any entry.
  2168. These are fixed values, for the preset properties. The user variable
  2169. that can be used to add to this list is `org-global-properties'.
  2170. The entries in this list are cons cells where the car is a property
  2171. name and cdr is a string with the value. If the value represents
  2172. multiple items like an \"_ALL\" property, separate the items by
  2173. spaces.")
  2174. (defcustom org-global-properties nil
  2175. "List of property/value pairs that can be inherited by any entry.
  2176. This list will be combined with the constant `org-global-properties-fixed'.
  2177. The entries in this list are cons cells where the car is a property
  2178. name and cdr is a string with the value.
  2179. You can set buffer-local values for the same purpose in the variable
  2180. `org-file-properties' this by adding lines like
  2181. #+PROPERTY: NAME VALUE"
  2182. :group 'org-properties
  2183. :type '(repeat
  2184. (cons (string :tag "Property")
  2185. (string :tag "Value"))))
  2186. (defvar org-file-properties nil
  2187. "List of property/value pairs that can be inherited by any entry.
  2188. Valid for the current buffer.
  2189. This variable is populated from #+PROPERTY lines.")
  2190. (make-variable-buffer-local 'org-file-properties)
  2191. (defgroup org-agenda nil
  2192. "Options concerning agenda views in Org-mode."
  2193. :tag "Org Agenda"
  2194. :group 'org)
  2195. (defvar org-category nil
  2196. "Variable used by org files to set a category for agenda display.
  2197. Such files should use a file variable to set it, for example
  2198. # -*- mode: org; org-category: \"ELisp\"
  2199. or contain a special line
  2200. #+CATEGORY: ELisp
  2201. If the file does not specify a category, then file's base name
  2202. is used instead.")
  2203. (make-variable-buffer-local 'org-category)
  2204. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2205. (defcustom org-agenda-files nil
  2206. "The files to be used for agenda display.
  2207. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2208. \\[org-remove-file]. You can also use customize to edit the list.
  2209. If an entry is a directory, all files in that directory that are matched by
  2210. `org-agenda-file-regexp' will be part of the file list.
  2211. If the value of the variable is not a list but a single file name, then
  2212. the list of agenda files is actually stored and maintained in that file, one
  2213. agenda file per line."
  2214. :group 'org-agenda
  2215. :type '(choice
  2216. (repeat :tag "List of files and directories" file)
  2217. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2218. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2219. "Regular expression to match files for `org-agenda-files'.
  2220. If any element in the list in that variable contains a directory instead
  2221. of a normal file, all files in that directory that are matched by this
  2222. regular expression will be included."
  2223. :group 'org-agenda
  2224. :type 'regexp)
  2225. (defcustom org-agenda-text-search-extra-files nil
  2226. "List of extra files to be searched by text search commands.
  2227. These files will be search in addition to the agenda files by the
  2228. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2229. Note that these files will only be searched for text search commands,
  2230. not for the other agenda views like todo lists, tag searches or the weekly
  2231. agenda. This variable is intended to list notes and possibly archive files
  2232. that should also be searched by these two commands.
  2233. In fact, if the first element in the list is the symbol `agenda-archives',
  2234. than all archive files of all agenda files will be added to the search
  2235. scope."
  2236. :group 'org-agenda
  2237. :type '(set :greedy t
  2238. (const :tag "Agenda Archives" agenda-archives)
  2239. (repeat :inline t (file))))
  2240. (if (fboundp 'defvaralias)
  2241. (defvaralias 'org-agenda-multi-occur-extra-files
  2242. 'org-agenda-text-search-extra-files))
  2243. (defcustom org-agenda-skip-unavailable-files nil
  2244. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2245. A nil value means to remove them, after a query, from the list."
  2246. :group 'org-agenda
  2247. :type 'boolean)
  2248. (defcustom org-calendar-to-agenda-key [?c]
  2249. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2250. The command `org-calendar-goto-agenda' will be bound to this key. The
  2251. default is the character `c' because then `c' can be used to switch back and
  2252. forth between agenda and calendar."
  2253. :group 'org-agenda
  2254. :type 'sexp)
  2255. (defcustom org-calendar-agenda-action-key [?k]
  2256. "The key to be installed in `calendar-mode-map' for agenda-action.
  2257. The command `org-agenda-action' will be bound to this key. The
  2258. default is the character `k' because we use the same key in the agenda."
  2259. :group 'org-agenda
  2260. :type 'sexp)
  2261. (eval-after-load "calendar"
  2262. '(progn
  2263. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2264. 'org-calendar-goto-agenda)
  2265. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2266. 'org-agenda-action)))
  2267. (defgroup org-latex nil
  2268. "Options for embedding LaTeX code into Org-mode."
  2269. :tag "Org LaTeX"
  2270. :group 'org)
  2271. (defcustom org-format-latex-options
  2272. '(:foreground default :background default :scale 1.0
  2273. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2274. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2275. "Options for creating images from LaTeX fragments.
  2276. This is a property list with the following properties:
  2277. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2278. `default' means use the foreground of the default face.
  2279. :background the background color, or \"Transparent\".
  2280. `default' means use the background of the default face.
  2281. :scale a scaling factor for the size of the images.
  2282. :html-foreground, :html-background, :html-scale
  2283. the same numbers for HTML export.
  2284. :matchers a list indicating which matchers should be used to
  2285. find LaTeX fragments. Valid members of this list are:
  2286. \"begin\" find environments
  2287. \"$1\" find single characters surrounded by $.$
  2288. \"$\" find math expressions surrounded by $...$
  2289. \"$$\" find math expressions surrounded by $$....$$
  2290. \"\\(\" find math expressions surrounded by \\(...\\)
  2291. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2292. :group 'org-latex
  2293. :type 'plist)
  2294. (defcustom org-format-latex-header "\\documentclass{article}
  2295. \\usepackage{fullpage} % do not remove
  2296. \\usepackage{amssymb}
  2297. \\usepackage[usenames]{color}
  2298. \\usepackage{amsmath}
  2299. \\usepackage{latexsym}
  2300. \\usepackage[mathscr]{eucal}
  2301. \\pagestyle{empty} % do not remove"
  2302. "The document header used for processing LaTeX fragments."
  2303. :group 'org-latex
  2304. :type 'string)
  2305. (defgroup org-font-lock nil
  2306. "Font-lock settings for highlighting in Org-mode."
  2307. :tag "Org Font Lock"
  2308. :group 'org)
  2309. (defcustom org-level-color-stars-only nil
  2310. "Non-nil means fontify only the stars in each headline.
  2311. When nil, the entire headline is fontified.
  2312. Changing it requires restart of `font-lock-mode' to become effective
  2313. also in regions already fontified."
  2314. :group 'org-font-lock
  2315. :type 'boolean)
  2316. (defcustom org-hide-leading-stars nil
  2317. "Non-nil means, hide the first N-1 stars in a headline.
  2318. This works by using the face `org-hide' for these stars. This
  2319. face is white for a light background, and black for a dark
  2320. background. You may have to customize the face `org-hide' to
  2321. make this work.
  2322. Changing it requires restart of `font-lock-mode' to become effective
  2323. also in regions already fontified.
  2324. You may also set this on a per-file basis by adding one of the following
  2325. lines to the buffer:
  2326. #+STARTUP: hidestars
  2327. #+STARTUP: showstars"
  2328. :group 'org-font-lock
  2329. :type 'boolean)
  2330. (defcustom org-fontify-done-headline nil
  2331. "Non-nil means, change the face of a headline if it is marked DONE.
  2332. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2333. When this is non-nil, the headline after the keyword is set to the
  2334. `org-headline-done' as an additional indication."
  2335. :group 'org-font-lock
  2336. :type 'boolean)
  2337. (defcustom org-fontify-emphasized-text t
  2338. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2339. Changing this variable requires a restart of Emacs to take effect."
  2340. :group 'org-font-lock
  2341. :type 'boolean)
  2342. (defcustom org-fontify-whole-heading-line nil
  2343. "Non-nil means fontify the whole line for headings.
  2344. This is useful when setting a background color for the
  2345. org-leve-* faces."
  2346. :group 'org-font-lock
  2347. :type 'boolean)
  2348. (defcustom org-highlight-latex-fragments-and-specials nil
  2349. "Non-nil means, fontify what is treated specially by the exporters."
  2350. :group 'org-font-lock
  2351. :type 'boolean)
  2352. (defcustom org-hide-emphasis-markers nil
  2353. "Non-nil mean font-lock should hide the emphasis marker characters."
  2354. :group 'org-font-lock
  2355. :type 'boolean)
  2356. (defvar org-emph-re nil
  2357. "Regular expression for matching emphasis.")
  2358. (defvar org-verbatim-re nil
  2359. "Regular expression for matching verbatim text.")
  2360. (defvar org-emphasis-regexp-components) ; defined just below
  2361. (defvar org-emphasis-alist) ; defined just below
  2362. (defun org-set-emph-re (var val)
  2363. "Set variable and compute the emphasis regular expression."
  2364. (set var val)
  2365. (when (and (boundp 'org-emphasis-alist)
  2366. (boundp 'org-emphasis-regexp-components)
  2367. org-emphasis-alist org-emphasis-regexp-components)
  2368. (let* ((e org-emphasis-regexp-components)
  2369. (pre (car e))
  2370. (post (nth 1 e))
  2371. (border (nth 2 e))
  2372. (body (nth 3 e))
  2373. (nl (nth 4 e))
  2374. (body1 (concat body "*?"))
  2375. (markers (mapconcat 'car org-emphasis-alist ""))
  2376. (vmarkers (mapconcat
  2377. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2378. org-emphasis-alist "")))
  2379. ;; make sure special characters appear at the right position in the class
  2380. (if (string-match "\\^" markers)
  2381. (setq markers (concat (replace-match "" t t markers) "^")))
  2382. (if (string-match "-" markers)
  2383. (setq markers (concat (replace-match "" t t markers) "-")))
  2384. (if (string-match "\\^" vmarkers)
  2385. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2386. (if (string-match "-" vmarkers)
  2387. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2388. (if (> nl 0)
  2389. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2390. (int-to-string nl) "\\}")))
  2391. ;; Make the regexp
  2392. (setq org-emph-re
  2393. (concat "\\([" pre "]\\|^\\)"
  2394. "\\("
  2395. "\\([" markers "]\\)"
  2396. "\\("
  2397. "[^" border "]\\|"
  2398. "[^" border "]"
  2399. body1
  2400. "[^" border "]"
  2401. "\\)"
  2402. "\\3\\)"
  2403. "\\([" post "]\\|$\\)"))
  2404. (setq org-verbatim-re
  2405. (concat "\\([" pre "]\\|^\\)"
  2406. "\\("
  2407. "\\([" vmarkers "]\\)"
  2408. "\\("
  2409. "[^" border "]\\|"
  2410. "[^" border "]"
  2411. body1
  2412. "[^" border "]"
  2413. "\\)"
  2414. "\\3\\)"
  2415. "\\([" post "]\\|$\\)")))))
  2416. (defcustom org-emphasis-regexp-components
  2417. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2418. "Components used to build the regular expression for emphasis.
  2419. This is a list with 6 entries. Terminology: In an emphasis string
  2420. like \" *strong word* \", we call the initial space PREMATCH, the final
  2421. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2422. and \"trong wor\" is the body. The different components in this variable
  2423. specify what is allowed/forbidden in each part:
  2424. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2425. post Chars allowed as postmatch. End of line will be allowed too.
  2426. border The chars *forbidden* as border characters.
  2427. body-regexp A regexp like \".\" to match a body character. Don't use
  2428. non-shy groups here, and don't allow newline here.
  2429. newline The maximum number of newlines allowed in an emphasis exp.
  2430. Use customize to modify this, or restart Emacs after changing it."
  2431. :group 'org-font-lock
  2432. :set 'org-set-emph-re
  2433. :type '(list
  2434. (sexp :tag "Allowed chars in pre ")
  2435. (sexp :tag "Allowed chars in post ")
  2436. (sexp :tag "Forbidden chars in border ")
  2437. (sexp :tag "Regexp for body ")
  2438. (integer :tag "number of newlines allowed")
  2439. (option (boolean :tag "Please ignore this button"))))
  2440. (defcustom org-emphasis-alist
  2441. `(("*" bold "<b>" "</b>")
  2442. ("/" italic "<i>" "</i>")
  2443. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2444. ("=" org-code "<code>" "</code>" verbatim)
  2445. ("~" org-verbatim "<code>" "</code>" verbatim)
  2446. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2447. "<del>" "</del>")
  2448. )
  2449. "Special syntax for emphasized text.
  2450. Text starting and ending with a special character will be emphasized, for
  2451. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2452. characters, the face to be used by font-lock for highlighting in Org-mode
  2453. Emacs buffers, and the HTML tags to be used for this.
  2454. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2455. Use customize to modify this, or restart Emacs after changing it."
  2456. :group 'org-font-lock
  2457. :set 'org-set-emph-re
  2458. :type '(repeat
  2459. (list
  2460. (string :tag "Marker character")
  2461. (choice
  2462. (face :tag "Font-lock-face")
  2463. (plist :tag "Face property list"))
  2464. (string :tag "HTML start tag")
  2465. (string :tag "HTML end tag")
  2466. (option (const verbatim)))))
  2467. (defvar org-protecting-blocks
  2468. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2469. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2470. This is needed for font-lock setup.")
  2471. ;;; Miscellaneous options
  2472. (defgroup org-completion nil
  2473. "Completion in Org-mode."
  2474. :tag "Org Completion"
  2475. :group 'org)
  2476. (defcustom org-completion-use-ido nil
  2477. "Non-nil means, use ido completion wherever possible.
  2478. Note that `ido-mode' must be active for this variable to be relevant.
  2479. If you decide to turn this variable on, you might well want to turn off
  2480. `org-outline-path-complete-in-steps'."
  2481. :group 'org-completion
  2482. :type 'boolean)
  2483. (defcustom org-completion-fallback-command 'hippie-expand
  2484. "The expansion command called by \\[org-complete] in normal context.
  2485. Normal means, no org-mode-specific context."
  2486. :group 'org-completion
  2487. :type 'function)
  2488. ;;; Functions and variables from ther packages
  2489. ;; Declared here to avoid compiler warnings
  2490. ;; XEmacs only
  2491. (defvar outline-mode-menu-heading)
  2492. (defvar outline-mode-menu-show)
  2493. (defvar outline-mode-menu-hide)
  2494. (defvar zmacs-regions) ; XEmacs regions
  2495. ;; Emacs only
  2496. (defvar mark-active)
  2497. ;; Various packages
  2498. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2499. (declare-function calendar-forward-day "cal-move" (arg))
  2500. (declare-function calendar-goto-date "cal-move" (date))
  2501. (declare-function calendar-goto-today "cal-move" ())
  2502. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2503. (defvar calc-embedded-close-formula)
  2504. (defvar calc-embedded-open-formula)
  2505. (declare-function cdlatex-tab "ext:cdlatex" ())
  2506. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2507. (defvar font-lock-unfontify-region-function)
  2508. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2509. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2510. (defvar iswitchb-temp-buflist)
  2511. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2512. (defvar org-agenda-tags-todo-honor-ignore-options)
  2513. (declare-function org-agenda-skip "org-agenda" ())
  2514. (declare-function org-format-agenda-item "org-agenda"
  2515. (extra txt &optional category tags dotime noprefix remove-re))
  2516. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2517. (declare-function org-agenda-change-all-lines "org-agenda"
  2518. (newhead hdmarker &optional fixface just-this))
  2519. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2520. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2521. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2522. (beg end))
  2523. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2524. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2525. "org-agenda" (&optional end))
  2526. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2527. (declare-function org-indent-mode "org-indent" (arg))
  2528. (declare-function parse-time-string "parse-time" (string))
  2529. (declare-function remember "remember" (&optional initial))
  2530. (declare-function remember-buffer-desc "remember" ())
  2531. (declare-function remember-finalize "remember" ())
  2532. (defvar remember-save-after-remembering)
  2533. (defvar remember-data-file)
  2534. (defvar remember-register)
  2535. (defvar remember-buffer)
  2536. (defvar remember-handler-functions)
  2537. (defvar remember-annotation-functions)
  2538. (defvar texmathp-why)
  2539. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2540. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2541. (defvar w3m-current-url)
  2542. (defvar w3m-current-title)
  2543. (defvar org-latex-regexps)
  2544. ;;; Autoload and prepare some org modules
  2545. ;; Some table stuff that needs to be defined here, because it is used
  2546. ;; by the functions setting up org-mode or checking for table context.
  2547. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2548. "Detects an org-type or table-type table.")
  2549. (defconst org-table-line-regexp "^[ \t]*|"
  2550. "Detects an org-type table line.")
  2551. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2552. "Detects an org-type table line.")
  2553. (defconst org-table-hline-regexp "^[ \t]*|-"
  2554. "Detects an org-type table hline.")
  2555. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2556. "Detects a table-type table hline.")
  2557. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2558. "Searching from within a table (any type) this finds the first line
  2559. outside the table.")
  2560. ;; Autoload the functions in org-table.el that are needed by functions here.
  2561. (eval-and-compile
  2562. (org-autoload "org-table"
  2563. '(org-table-align org-table-begin org-table-blank-field
  2564. org-table-convert org-table-convert-region org-table-copy-down
  2565. org-table-copy-region org-table-create
  2566. org-table-create-or-convert-from-region
  2567. org-table-create-with-table.el org-table-current-dline
  2568. org-table-cut-region org-table-delete-column org-table-edit-field
  2569. org-table-edit-formulas org-table-end org-table-eval-formula
  2570. org-table-export org-table-field-info
  2571. org-table-get-stored-formulas org-table-goto-column
  2572. org-table-hline-and-move org-table-import org-table-insert-column
  2573. org-table-insert-hline org-table-insert-row org-table-iterate
  2574. org-table-justify-field-maybe org-table-kill-row
  2575. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2576. org-table-move-column org-table-move-column-left
  2577. org-table-move-column-right org-table-move-row
  2578. org-table-move-row-down org-table-move-row-up
  2579. org-table-next-field org-table-next-row org-table-paste-rectangle
  2580. org-table-previous-field org-table-recalculate
  2581. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2582. org-table-toggle-coordinate-overlays
  2583. org-table-toggle-formula-debugger org-table-wrap-region
  2584. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2585. (defun org-at-table-p (&optional table-type)
  2586. "Return t if the cursor is inside an org-type table.
  2587. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2588. (if org-enable-table-editor
  2589. (save-excursion
  2590. (beginning-of-line 1)
  2591. (looking-at (if table-type org-table-any-line-regexp
  2592. org-table-line-regexp)))
  2593. nil))
  2594. (defsubst org-table-p () (org-at-table-p))
  2595. (defun org-at-table.el-p ()
  2596. "Return t if and only if we are at a table.el table."
  2597. (and (org-at-table-p 'any)
  2598. (save-excursion
  2599. (goto-char (org-table-begin 'any))
  2600. (looking-at org-table1-hline-regexp))))
  2601. (defun org-table-recognize-table.el ()
  2602. "If there is a table.el table nearby, recognize it and move into it."
  2603. (if org-table-tab-recognizes-table.el
  2604. (if (org-at-table.el-p)
  2605. (progn
  2606. (beginning-of-line 1)
  2607. (if (looking-at org-table-dataline-regexp)
  2608. nil
  2609. (if (looking-at org-table1-hline-regexp)
  2610. (progn
  2611. (beginning-of-line 2)
  2612. (if (looking-at org-table-any-border-regexp)
  2613. (beginning-of-line -1)))))
  2614. (if (re-search-forward "|" (org-table-end t) t)
  2615. (progn
  2616. (require 'table)
  2617. (if (table--at-cell-p (point))
  2618. t
  2619. (message "recognizing table.el table...")
  2620. (table-recognize-table)
  2621. (message "recognizing table.el table...done")))
  2622. (error "This should not happen..."))
  2623. t)
  2624. nil)
  2625. nil))
  2626. (defun org-at-table-hline-p ()
  2627. "Return t if the cursor is inside a hline in a table."
  2628. (if org-enable-table-editor
  2629. (save-excursion
  2630. (beginning-of-line 1)
  2631. (looking-at org-table-hline-regexp))
  2632. nil))
  2633. (defvar org-table-clean-did-remove-column nil)
  2634. (defun org-table-map-tables (function)
  2635. "Apply FUNCTION to the start of all tables in the buffer."
  2636. (save-excursion
  2637. (save-restriction
  2638. (widen)
  2639. (goto-char (point-min))
  2640. (while (re-search-forward org-table-any-line-regexp nil t)
  2641. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2642. (beginning-of-line 1)
  2643. (if (looking-at org-table-line-regexp)
  2644. (save-excursion (funcall function)))
  2645. (re-search-forward org-table-any-border-regexp nil 1))))
  2646. (message "Mapping tables: done"))
  2647. ;; Declare and autoload functions from org-exp.el & Co
  2648. (declare-function org-default-export-plist "org-exp")
  2649. (declare-function org-infile-export-plist "org-exp")
  2650. (declare-function org-get-current-options "org-exp")
  2651. (eval-and-compile
  2652. (org-autoload "org-exp"
  2653. '(org-export org-export-visible
  2654. org-insert-export-options-template
  2655. org-table-clean-before-export))
  2656. (org-autoload "org-ascii"
  2657. '(org-export-as-ascii org-export-ascii-preprocess
  2658. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2659. org-export-region-as-ascii))
  2660. (org-autoload "org-html"
  2661. '(org-export-as-html-and-open
  2662. org-export-as-html-batch org-export-as-html-to-buffer
  2663. org-replace-region-by-html org-export-region-as-html
  2664. org-export-as-html))
  2665. (org-autoload "org-icalendar"
  2666. '(org-export-icalendar-this-file
  2667. org-export-icalendar-all-agenda-files
  2668. org-export-icalendar-combine-agenda-files))
  2669. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2670. ;; Declare and autoload functions from org-agenda.el
  2671. (eval-and-compile
  2672. (org-autoload "org-agenda"
  2673. '(org-agenda org-agenda-list org-search-view
  2674. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2675. org-diary org-agenda-to-appt
  2676. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2677. ;; Autoload org-remember
  2678. (eval-and-compile
  2679. (org-autoload "org-remember"
  2680. '(org-remember-insinuate org-remember-annotation
  2681. org-remember-apply-template org-remember org-remember-handler)))
  2682. ;; Autoload org-clock.el
  2683. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2684. (beg end))
  2685. (declare-function org-clock-update-mode-line "org-clock" ())
  2686. (defvar org-clock-start-time)
  2687. (defvar org-clock-marker (make-marker)
  2688. "Marker recording the last clock-in.")
  2689. (defun org-clock-is-active ()
  2690. "Return non-nil if clock is currently running.
  2691. The return value is actually the clock marker."
  2692. (marker-buffer org-clock-marker))
  2693. (eval-and-compile
  2694. (org-autoload
  2695. "org-clock"
  2696. '(org-clock-in org-clock-out org-clock-cancel
  2697. org-clock-goto org-clock-sum org-clock-display
  2698. org-clock-remove-overlays org-clock-report
  2699. org-clocktable-shift org-dblock-write:clocktable
  2700. org-get-clocktable)))
  2701. (defun org-clock-update-time-maybe ()
  2702. "If this is a CLOCK line, update it and return t.
  2703. Otherwise, return nil."
  2704. (interactive)
  2705. (save-excursion
  2706. (beginning-of-line 1)
  2707. (skip-chars-forward " \t")
  2708. (when (looking-at org-clock-string)
  2709. (let ((re (concat "[ \t]*" org-clock-string
  2710. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2711. "\\([ \t]*=>.*\\)?\\)?"))
  2712. ts te h m s neg)
  2713. (cond
  2714. ((not (looking-at re))
  2715. nil)
  2716. ((not (match-end 2))
  2717. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2718. (> org-clock-marker (point))
  2719. (<= org-clock-marker (point-at-eol)))
  2720. ;; The clock is running here
  2721. (setq org-clock-start-time
  2722. (apply 'encode-time
  2723. (org-parse-time-string (match-string 1))))
  2724. (org-clock-update-mode-line)))
  2725. (t
  2726. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2727. (end-of-line 1)
  2728. (setq ts (match-string 1)
  2729. te (match-string 3))
  2730. (setq s (- (time-to-seconds
  2731. (apply 'encode-time (org-parse-time-string te)))
  2732. (time-to-seconds
  2733. (apply 'encode-time (org-parse-time-string ts))))
  2734. neg (< s 0)
  2735. s (abs s)
  2736. h (floor (/ s 3600))
  2737. s (- s (* 3600 h))
  2738. m (floor (/ s 60))
  2739. s (- s (* 60 s)))
  2740. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2741. t))))))
  2742. (defun org-check-running-clock ()
  2743. "Check if the current buffer contains the running clock.
  2744. If yes, offer to stop it and to save the buffer with the changes."
  2745. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2746. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2747. (buffer-name))))
  2748. (org-clock-out)
  2749. (when (y-or-n-p "Save changed buffer?")
  2750. (save-buffer))))
  2751. (defun org-clocktable-try-shift (dir n)
  2752. "Check if this line starts a clock table, if yes, shift the time block."
  2753. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2754. (org-clocktable-shift dir n)))
  2755. ;; Autoload org-timer.el
  2756. (eval-and-compile
  2757. (org-autoload
  2758. "org-timer"
  2759. '(org-timer-start org-timer org-timer-item
  2760. org-timer-change-times-in-region
  2761. org-timer-set-timer
  2762. org-timer-reset-timers
  2763. org-timer-show-remaining-time)))
  2764. ;; Autoload org-feed.el
  2765. (eval-and-compile
  2766. (org-autoload
  2767. "org-feed"
  2768. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2769. ;; Autoload org-indent.el
  2770. (eval-and-compile
  2771. (org-autoload
  2772. "org-indent"
  2773. '(org-indent-mode)))
  2774. ;; Autoload archiving code
  2775. ;; The stuff that is needed for cycling and tags has to be defined here.
  2776. (defgroup org-archive nil
  2777. "Options concerning archiving in Org-mode."
  2778. :tag "Org Archive"
  2779. :group 'org-structure)
  2780. (defcustom org-archive-location "%s_archive::"
  2781. "The location where subtrees should be archived.
  2782. The value of this variable is a string, consisting of two parts,
  2783. separated by a double-colon. The first part is a filename and
  2784. the second part is a headline.
  2785. When the filename is omitted, archiving happens in the same file.
  2786. %s in the filename will be replaced by the current file
  2787. name (without the directory part). Archiving to a different file
  2788. is useful to keep archived entries from contributing to the
  2789. Org-mode Agenda.
  2790. The archived entries will be filed as subtrees of the specified
  2791. headline. When the headline is omitted, the subtrees are simply
  2792. filed away at the end of the file, as top-level entries. Also in
  2793. the heading you can use %s to represent the file name, this can be
  2794. useful when using the same archive for a number of different files.
  2795. Here are a few examples:
  2796. \"%s_archive::\"
  2797. If the current file is Projects.org, archive in file
  2798. Projects.org_archive, as top-level trees. This is the default.
  2799. \"::* Archived Tasks\"
  2800. Archive in the current file, under the top-level headline
  2801. \"* Archived Tasks\".
  2802. \"~/org/archive.org::\"
  2803. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2804. \"~/org/archive.org::From %s\"
  2805. Archive in file ~/org/archive.org (absolute path), und headlines
  2806. \"From FILENAME\" where file name is the current file name.
  2807. \"basement::** Finished Tasks\"
  2808. Archive in file ./basement (relative path), as level 3 trees
  2809. below the level 2 heading \"** Finished Tasks\".
  2810. You may set this option on a per-file basis by adding to the buffer a
  2811. line like
  2812. #+ARCHIVE: basement::** Finished Tasks
  2813. You may also define it locally for a subtree by setting an ARCHIVE property
  2814. in the entry. If such a property is found in an entry, or anywhere up
  2815. the hierarchy, it will be used."
  2816. :group 'org-archive
  2817. :type 'string)
  2818. (defcustom org-archive-tag "ARCHIVE"
  2819. "The tag that marks a subtree as archived.
  2820. An archived subtree does not open during visibility cycling, and does
  2821. not contribute to the agenda listings.
  2822. After changing this, font-lock must be restarted in the relevant buffers to
  2823. get the proper fontification."
  2824. :group 'org-archive
  2825. :group 'org-keywords
  2826. :type 'string)
  2827. (defcustom org-agenda-skip-archived-trees t
  2828. "Non-nil means, the agenda will skip any items located in archived trees.
  2829. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2830. variable is no longer recommended, you should leave it at the value t.
  2831. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2832. :group 'org-archive
  2833. :group 'org-agenda-skip
  2834. :type 'boolean)
  2835. (defcustom org-columns-skip-arrchived-trees t
  2836. "Non-nil means, irgnore archived trees when creating column view."
  2837. :group 'org-archive
  2838. :group 'org-properties
  2839. :type 'boolean)
  2840. (defcustom org-cycle-open-archived-trees nil
  2841. "Non-nil means, `org-cycle' will open archived trees.
  2842. An archived tree is a tree marked with the tag ARCHIVE.
  2843. When nil, archived trees will stay folded. You can still open them with
  2844. normal outline commands like `show-all', but not with the cycling commands."
  2845. :group 'org-archive
  2846. :group 'org-cycle
  2847. :type 'boolean)
  2848. (defcustom org-sparse-tree-open-archived-trees nil
  2849. "Non-nil means sparse tree construction shows matches in archived trees.
  2850. When nil, matches in these trees are highlighted, but the trees are kept in
  2851. collapsed state."
  2852. :group 'org-archive
  2853. :group 'org-sparse-trees
  2854. :type 'boolean)
  2855. (defun org-cycle-hide-archived-subtrees (state)
  2856. "Re-hide all archived subtrees after a visibility state change."
  2857. (when (and (not org-cycle-open-archived-trees)
  2858. (not (memq state '(overview folded))))
  2859. (save-excursion
  2860. (let* ((globalp (memq state '(contents all)))
  2861. (beg (if globalp (point-min) (point)))
  2862. (end (if globalp (point-max) (org-end-of-subtree t))))
  2863. (org-hide-archived-subtrees beg end)
  2864. (goto-char beg)
  2865. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2866. (message "%s" (substitute-command-keys
  2867. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2868. (defun org-force-cycle-archived ()
  2869. "Cycle subtree even if it is archived."
  2870. (interactive)
  2871. (setq this-command 'org-cycle)
  2872. (let ((org-cycle-open-archived-trees t))
  2873. (call-interactively 'org-cycle)))
  2874. (defun org-hide-archived-subtrees (beg end)
  2875. "Re-hide all archived subtrees after a visibility state change."
  2876. (save-excursion
  2877. (let* ((re (concat ":" org-archive-tag ":")))
  2878. (goto-char beg)
  2879. (while (re-search-forward re end t)
  2880. (and (org-on-heading-p) (hide-subtree))
  2881. (org-end-of-subtree t)))))
  2882. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2883. (eval-and-compile
  2884. (org-autoload "org-archive"
  2885. '(org-add-archive-files org-archive-subtree
  2886. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2887. ;; Autoload Column View Code
  2888. (declare-function org-columns-number-to-string "org-colview")
  2889. (declare-function org-columns-get-format-and-top-level "org-colview")
  2890. (declare-function org-columns-compute "org-colview")
  2891. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2892. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2893. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2894. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2895. ;; Autoload ID code
  2896. (declare-function org-id-store-link "org-id")
  2897. (declare-function org-id-locations-load "org-id")
  2898. (declare-function org-id-locations-save "org-id")
  2899. (defvar org-id-track-globally)
  2900. (org-autoload "org-id"
  2901. '(org-id-get-create org-id-new org-id-copy org-id-get
  2902. org-id-get-with-outline-path-completion
  2903. org-id-get-with-outline-drilling
  2904. org-id-goto org-id-find org-id-store-link))
  2905. ;; Autoload Plotting Code
  2906. (org-autoload "org-plot"
  2907. '(org-plot/gnuplot))
  2908. ;;; Variables for pre-computed regular expressions, all buffer local
  2909. (defvar org-drawer-regexp nil
  2910. "Matches first line of a hidden block.")
  2911. (make-variable-buffer-local 'org-drawer-regexp)
  2912. (defvar org-todo-regexp nil
  2913. "Matches any of the TODO state keywords.")
  2914. (make-variable-buffer-local 'org-todo-regexp)
  2915. (defvar org-not-done-regexp nil
  2916. "Matches any of the TODO state keywords except the last one.")
  2917. (make-variable-buffer-local 'org-not-done-regexp)
  2918. (defvar org-not-done-heading-regexp nil
  2919. "Matches a TODO headline that is not done.")
  2920. (make-variable-buffer-local 'org-not-done-regexp)
  2921. (defvar org-todo-line-regexp nil
  2922. "Matches a headline and puts TODO state into group 2 if present.")
  2923. (make-variable-buffer-local 'org-todo-line-regexp)
  2924. (defvar org-complex-heading-regexp nil
  2925. "Matches a headline and puts everything into groups:
  2926. group 1: the stars
  2927. group 2: The todo keyword, maybe
  2928. group 3: Priority cookie
  2929. group 4: True headline
  2930. group 5: Tags")
  2931. (make-variable-buffer-local 'org-complex-heading-regexp)
  2932. (defvar org-todo-line-tags-regexp nil
  2933. "Matches a headline and puts TODO state into group 2 if present.
  2934. Also put tags into group 4 if tags are present.")
  2935. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2936. (defvar org-nl-done-regexp nil
  2937. "Matches newline followed by a headline with the DONE keyword.")
  2938. (make-variable-buffer-local 'org-nl-done-regexp)
  2939. (defvar org-looking-at-done-regexp nil
  2940. "Matches the DONE keyword a point.")
  2941. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2942. (defvar org-ds-keyword-length 12
  2943. "Maximum length of the Deadline and SCHEDULED keywords.")
  2944. (make-variable-buffer-local 'org-ds-keyword-length)
  2945. (defvar org-deadline-regexp nil
  2946. "Matches the DEADLINE keyword.")
  2947. (make-variable-buffer-local 'org-deadline-regexp)
  2948. (defvar org-deadline-time-regexp nil
  2949. "Matches the DEADLINE keyword together with a time stamp.")
  2950. (make-variable-buffer-local 'org-deadline-time-regexp)
  2951. (defvar org-deadline-line-regexp nil
  2952. "Matches the DEADLINE keyword and the rest of the line.")
  2953. (make-variable-buffer-local 'org-deadline-line-regexp)
  2954. (defvar org-scheduled-regexp nil
  2955. "Matches the SCHEDULED keyword.")
  2956. (make-variable-buffer-local 'org-scheduled-regexp)
  2957. (defvar org-scheduled-time-regexp nil
  2958. "Matches the SCHEDULED keyword together with a time stamp.")
  2959. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2960. (defvar org-closed-time-regexp nil
  2961. "Matches the CLOSED keyword together with a time stamp.")
  2962. (make-variable-buffer-local 'org-closed-time-regexp)
  2963. (defvar org-keyword-time-regexp nil
  2964. "Matches any of the 4 keywords, together with the time stamp.")
  2965. (make-variable-buffer-local 'org-keyword-time-regexp)
  2966. (defvar org-keyword-time-not-clock-regexp nil
  2967. "Matches any of the 3 keywords, together with the time stamp.")
  2968. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2969. (defvar org-maybe-keyword-time-regexp nil
  2970. "Matches a timestamp, possibly preceeded by a keyword.")
  2971. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2972. (defvar org-planning-or-clock-line-re nil
  2973. "Matches a line with planning or clock info.")
  2974. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2975. (defconst org-plain-time-of-day-regexp
  2976. (concat
  2977. "\\(\\<[012]?[0-9]"
  2978. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2979. "\\(--?"
  2980. "\\(\\<[012]?[0-9]"
  2981. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2982. "\\)?")
  2983. "Regular expression to match a plain time or time range.
  2984. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2985. groups carry important information:
  2986. 0 the full match
  2987. 1 the first time, range or not
  2988. 8 the second time, if it is a range.")
  2989. (defconst org-plain-time-extension-regexp
  2990. (concat
  2991. "\\(\\<[012]?[0-9]"
  2992. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2993. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2994. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2995. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2996. groups carry important information:
  2997. 0 the full match
  2998. 7 hours of duration
  2999. 9 minutes of duration")
  3000. (defconst org-stamp-time-of-day-regexp
  3001. (concat
  3002. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3003. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3004. "\\(--?"
  3005. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3006. "Regular expression to match a timestamp time or time range.
  3007. After a match, the following groups carry important information:
  3008. 0 the full match
  3009. 1 date plus weekday, for backreferencing to make sure both times on same day
  3010. 2 the first time, range or not
  3011. 4 the second time, if it is a range.")
  3012. (defconst org-startup-options
  3013. '(("fold" org-startup-folded t)
  3014. ("overview" org-startup-folded t)
  3015. ("nofold" org-startup-folded nil)
  3016. ("showall" org-startup-folded nil)
  3017. ("content" org-startup-folded content)
  3018. ("indent" org-startup-indented t)
  3019. ("noindent" org-startup-indented nil)
  3020. ("hidestars" org-hide-leading-stars t)
  3021. ("showstars" org-hide-leading-stars nil)
  3022. ("odd" org-odd-levels-only t)
  3023. ("oddeven" org-odd-levels-only nil)
  3024. ("align" org-startup-align-all-tables t)
  3025. ("noalign" org-startup-align-all-tables nil)
  3026. ("customtime" org-display-custom-times t)
  3027. ("logdone" org-log-done time)
  3028. ("lognotedone" org-log-done note)
  3029. ("nologdone" org-log-done nil)
  3030. ("lognoteclock-out" org-log-note-clock-out t)
  3031. ("nolognoteclock-out" org-log-note-clock-out nil)
  3032. ("logrepeat" org-log-repeat state)
  3033. ("lognoterepeat" org-log-repeat note)
  3034. ("nologrepeat" org-log-repeat nil)
  3035. ("fninline" org-footnote-define-inline t)
  3036. ("nofninline" org-footnote-define-inline nil)
  3037. ("fnlocal" org-footnote-section nil)
  3038. ("fnauto" org-footnote-auto-label t)
  3039. ("fnprompt" org-footnote-auto-label nil)
  3040. ("fnconfirm" org-footnote-auto-label confirm)
  3041. ("fnplain" org-footnote-auto-label plain)
  3042. ("fnadjust" org-footnote-auto-adjust t)
  3043. ("nofnadjust" org-footnote-auto-adjust nil)
  3044. ("constcgs" constants-unit-system cgs)
  3045. ("constSI" constants-unit-system SI)
  3046. ("noptag" org-tag-persistent-alist nil)
  3047. ("hideblocks" org-hide-block-startup t)
  3048. ("nohideblocks" org-hide-block-startup nil))
  3049. "Variable associated with STARTUP options for org-mode.
  3050. Each element is a list of three items: The startup options as written
  3051. in the #+STARTUP line, the corresponding variable, and the value to
  3052. set this variable to if the option is found. An optional forth element PUSH
  3053. means to push this value onto the list in the variable.")
  3054. (defun org-set-regexps-and-options ()
  3055. "Precompute regular expressions for current buffer."
  3056. (when (org-mode-p)
  3057. (org-set-local 'org-todo-kwd-alist nil)
  3058. (org-set-local 'org-todo-key-alist nil)
  3059. (org-set-local 'org-todo-key-trigger nil)
  3060. (org-set-local 'org-todo-keywords-1 nil)
  3061. (org-set-local 'org-done-keywords nil)
  3062. (org-set-local 'org-todo-heads nil)
  3063. (org-set-local 'org-todo-sets nil)
  3064. (org-set-local 'org-todo-log-states nil)
  3065. (org-set-local 'org-file-properties nil)
  3066. (org-set-local 'org-file-tags nil)
  3067. (let ((re (org-make-options-regexp
  3068. '("CATEGORY" "TODO" "COLUMNS"
  3069. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3070. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3071. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3072. (splitre "[ \t]+")
  3073. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3074. tail sep kws1 prio props ftags drawers
  3075. ext-setup-or-nil setup-contents (start 0))
  3076. (save-excursion
  3077. (save-restriction
  3078. (widen)
  3079. (goto-char (point-min))
  3080. (while (or (and ext-setup-or-nil
  3081. (string-match re ext-setup-or-nil start)
  3082. (setq start (match-end 0)))
  3083. (and (setq ext-setup-or-nil nil start 0)
  3084. (re-search-forward re nil t)))
  3085. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3086. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3087. (cond
  3088. ((equal key "CATEGORY")
  3089. (if (string-match "[ \t]+$" value)
  3090. (setq value (replace-match "" t t value)))
  3091. (setq cat value))
  3092. ((member key '("SEQ_TODO" "TODO"))
  3093. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3094. ((equal key "TYP_TODO")
  3095. (push (cons 'type (org-split-string value splitre)) kwds))
  3096. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3097. ;; general TODO-like setup
  3098. (push (cons (intern (downcase (match-string 1 key)))
  3099. (org-split-string value splitre)) kwds))
  3100. ((equal key "TAGS")
  3101. (setq tags (append tags (if tags '("\\n") nil)
  3102. (org-split-string value splitre))))
  3103. ((equal key "COLUMNS")
  3104. (org-set-local 'org-columns-default-format value))
  3105. ((equal key "LINK")
  3106. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3107. (push (cons (match-string 1 value)
  3108. (org-trim (match-string 2 value)))
  3109. links)))
  3110. ((equal key "PRIORITIES")
  3111. (setq prio (org-split-string value " +")))
  3112. ((equal key "PROPERTY")
  3113. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3114. (push (cons (match-string 1 value) (match-string 2 value))
  3115. props)))
  3116. ((equal key "FILETAGS")
  3117. (when (string-match "\\S-" value)
  3118. (setq ftags
  3119. (append
  3120. ftags
  3121. (apply 'append
  3122. (mapcar (lambda (x) (org-split-string x ":"))
  3123. (org-split-string value)))))))
  3124. ((equal key "DRAWERS")
  3125. (setq drawers (org-split-string value splitre)))
  3126. ((equal key "CONSTANTS")
  3127. (setq const (append const (org-split-string value splitre))))
  3128. ((equal key "STARTUP")
  3129. (let ((opts (org-split-string value splitre))
  3130. l var val)
  3131. (while (setq l (pop opts))
  3132. (when (setq l (assoc l org-startup-options))
  3133. (setq var (nth 1 l) val (nth 2 l))
  3134. (if (not (nth 3 l))
  3135. (set (make-local-variable var) val)
  3136. (if (not (listp (symbol-value var)))
  3137. (set (make-local-variable var) nil))
  3138. (set (make-local-variable var) (symbol-value var))
  3139. (add-to-list var val))))))
  3140. ((equal key "ARCHIVE")
  3141. (string-match " *$" value)
  3142. (setq arch (replace-match "" t t value))
  3143. (remove-text-properties 0 (length arch)
  3144. '(face t fontified t) arch))
  3145. ((equal key "SETUPFILE")
  3146. (setq setup-contents (org-file-contents
  3147. (expand-file-name
  3148. (org-remove-double-quotes value))
  3149. 'noerror))
  3150. (if (not ext-setup-or-nil)
  3151. (setq ext-setup-or-nil setup-contents start 0)
  3152. (setq ext-setup-or-nil
  3153. (concat (substring ext-setup-or-nil 0 start)
  3154. "\n" setup-contents "\n"
  3155. (substring ext-setup-or-nil start)))))
  3156. ))))
  3157. (when cat
  3158. (org-set-local 'org-category (intern cat))
  3159. (push (cons "CATEGORY" cat) props))
  3160. (when prio
  3161. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3162. (setq prio (mapcar 'string-to-char prio))
  3163. (org-set-local 'org-highest-priority (nth 0 prio))
  3164. (org-set-local 'org-lowest-priority (nth 1 prio))
  3165. (org-set-local 'org-default-priority (nth 2 prio)))
  3166. (and props (org-set-local 'org-file-properties (nreverse props)))
  3167. (and ftags (org-set-local 'org-file-tags
  3168. (mapcar 'org-add-prop-inherited ftags)))
  3169. (and drawers (org-set-local 'org-drawers drawers))
  3170. (and arch (org-set-local 'org-archive-location arch))
  3171. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3172. ;; Process the TODO keywords
  3173. (unless kwds
  3174. ;; Use the global values as if they had been given locally.
  3175. (setq kwds (default-value 'org-todo-keywords))
  3176. (if (stringp (car kwds))
  3177. (setq kwds (list (cons org-todo-interpretation
  3178. (default-value 'org-todo-keywords)))))
  3179. (setq kwds (reverse kwds)))
  3180. (setq kwds (nreverse kwds))
  3181. (let (inter kws kw)
  3182. (while (setq kws (pop kwds))
  3183. (let ((kws (or
  3184. (run-hook-with-args-until-success
  3185. 'org-todo-setup-filter-hook kws)
  3186. kws)))
  3187. (setq inter (pop kws) sep (member "|" kws)
  3188. kws0 (delete "|" (copy-sequence kws))
  3189. kwsa nil
  3190. kws1 (mapcar
  3191. (lambda (x)
  3192. ;; 1 2
  3193. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3194. (progn
  3195. (setq kw (match-string 1 x)
  3196. key (and (match-end 2) (match-string 2 x))
  3197. log (org-extract-log-state-settings x))
  3198. (push (cons kw (and key (string-to-char key))) kwsa)
  3199. (and log (push log org-todo-log-states))
  3200. kw)
  3201. (error "Invalid TODO keyword %s" x)))
  3202. kws0)
  3203. kwsa (if kwsa (append '((:startgroup))
  3204. (nreverse kwsa)
  3205. '((:endgroup))))
  3206. hw (car kws1)
  3207. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3208. tail (list inter hw (car dws) (org-last dws))))
  3209. (add-to-list 'org-todo-heads hw 'append)
  3210. (push kws1 org-todo-sets)
  3211. (setq org-done-keywords (append org-done-keywords dws nil))
  3212. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3213. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3214. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3215. (setq org-todo-sets (nreverse org-todo-sets)
  3216. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3217. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3218. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3219. ;; Process the constants
  3220. (when const
  3221. (let (e cst)
  3222. (while (setq e (pop const))
  3223. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3224. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3225. (setq org-table-formula-constants-local cst)))
  3226. ;; Process the tags.
  3227. (when tags
  3228. (let (e tgs)
  3229. (while (setq e (pop tags))
  3230. (cond
  3231. ((equal e "{") (push '(:startgroup) tgs))
  3232. ((equal e "}") (push '(:endgroup) tgs))
  3233. ((equal e "\\n") (push '(:newline) tgs))
  3234. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3235. (push (cons (match-string 1 e)
  3236. (string-to-char (match-string 2 e)))
  3237. tgs))
  3238. (t (push (list e) tgs))))
  3239. (org-set-local 'org-tag-alist nil)
  3240. (while (setq e (pop tgs))
  3241. (or (and (stringp (car e))
  3242. (assoc (car e) org-tag-alist))
  3243. (push e org-tag-alist)))))
  3244. ;; Compute the regular expressions and other local variables
  3245. (if (not org-done-keywords)
  3246. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3247. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3248. (length org-scheduled-string)
  3249. (length org-clock-string)
  3250. (length org-closed-string)))
  3251. org-drawer-regexp
  3252. (concat "^[ \t]*:\\("
  3253. (mapconcat 'regexp-quote org-drawers "\\|")
  3254. "\\):[ \t]*$")
  3255. org-not-done-keywords
  3256. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3257. org-todo-regexp
  3258. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3259. "\\|") "\\)\\>")
  3260. org-not-done-regexp
  3261. (concat "\\<\\("
  3262. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3263. "\\)\\>")
  3264. org-not-done-heading-regexp
  3265. (concat "^\\(\\*+\\)[ \t]+\\("
  3266. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3267. "\\)\\>")
  3268. org-todo-line-regexp
  3269. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3270. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3271. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3272. org-complex-heading-regexp
  3273. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3274. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3275. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3276. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3277. org-nl-done-regexp
  3278. (concat "\n\\*+[ \t]+"
  3279. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3280. "\\)" "\\>")
  3281. org-todo-line-tags-regexp
  3282. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3283. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3284. (org-re
  3285. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3286. org-looking-at-done-regexp
  3287. (concat "^" "\\(?:"
  3288. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3289. "\\>")
  3290. org-deadline-regexp (concat "\\<" org-deadline-string)
  3291. org-deadline-time-regexp
  3292. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3293. org-deadline-line-regexp
  3294. (concat "\\<\\(" org-deadline-string "\\).*")
  3295. org-scheduled-regexp
  3296. (concat "\\<" org-scheduled-string)
  3297. org-scheduled-time-regexp
  3298. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3299. org-closed-time-regexp
  3300. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3301. org-keyword-time-regexp
  3302. (concat "\\<\\(" org-scheduled-string
  3303. "\\|" org-deadline-string
  3304. "\\|" org-closed-string
  3305. "\\|" org-clock-string "\\)"
  3306. " *[[<]\\([^]>]+\\)[]>]")
  3307. org-keyword-time-not-clock-regexp
  3308. (concat "\\<\\(" org-scheduled-string
  3309. "\\|" org-deadline-string
  3310. "\\|" org-closed-string
  3311. "\\)"
  3312. " *[[<]\\([^]>]+\\)[]>]")
  3313. org-maybe-keyword-time-regexp
  3314. (concat "\\(\\<\\(" org-scheduled-string
  3315. "\\|" org-deadline-string
  3316. "\\|" org-closed-string
  3317. "\\|" org-clock-string "\\)\\)?"
  3318. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3319. org-planning-or-clock-line-re
  3320. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3321. "\\|" org-deadline-string
  3322. "\\|" org-closed-string "\\|" org-clock-string
  3323. "\\)\\>\\)")
  3324. )
  3325. (org-compute-latex-and-specials-regexp)
  3326. (org-set-font-lock-defaults))))
  3327. (defun org-file-contents (file &optional noerror)
  3328. "Return the contents of FILE, as a string."
  3329. (if (or (not file)
  3330. (not (file-readable-p file)))
  3331. (if noerror
  3332. (progn
  3333. (message "Cannot read file %s" file)
  3334. (ding) (sit-for 2)
  3335. "")
  3336. (error "Cannot read file %s" file))
  3337. (with-temp-buffer
  3338. (insert-file-contents file)
  3339. (buffer-string))))
  3340. (defun org-extract-log-state-settings (x)
  3341. "Extract the log state setting from a TODO keyword string.
  3342. This will extract info from a string like \"WAIT(w@/!)\"."
  3343. (let (kw key log1 log2)
  3344. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3345. (setq kw (match-string 1 x)
  3346. key (and (match-end 2) (match-string 2 x))
  3347. log1 (and (match-end 3) (match-string 3 x))
  3348. log2 (and (match-end 4) (match-string 4 x)))
  3349. (and (or log1 log2)
  3350. (list kw
  3351. (and log1 (if (equal log1 "!") 'time 'note))
  3352. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3353. (defun org-remove-keyword-keys (list)
  3354. "Remove a pair of parenthesis at the end of each string in LIST."
  3355. (mapcar (lambda (x)
  3356. (if (string-match "(.*)$" x)
  3357. (substring x 0 (match-beginning 0))
  3358. x))
  3359. list))
  3360. ;; FIXME: this could be done much better, using second characters etc.
  3361. (defun org-assign-fast-keys (alist)
  3362. "Assign fast keys to a keyword-key alist.
  3363. Respect keys that are already there."
  3364. (let (new e k c c1 c2 (char ?a))
  3365. (while (setq e (pop alist))
  3366. (cond
  3367. ((equal e '(:startgroup)) (push e new))
  3368. ((equal e '(:endgroup)) (push e new))
  3369. ((equal e '(:newline)) (push e new))
  3370. (t
  3371. (setq k (car e) c2 nil)
  3372. (if (cdr e)
  3373. (setq c (cdr e))
  3374. ;; automatically assign a character.
  3375. (setq c1 (string-to-char
  3376. (downcase (substring
  3377. k (if (= (string-to-char k) ?@) 1 0)))))
  3378. (if (or (rassoc c1 new) (rassoc c1 alist))
  3379. (while (or (rassoc char new) (rassoc char alist))
  3380. (setq char (1+ char)))
  3381. (setq c2 c1))
  3382. (setq c (or c2 char)))
  3383. (push (cons k c) new))))
  3384. (nreverse new)))
  3385. ;;; Some variables used in various places
  3386. (defvar org-window-configuration nil
  3387. "Used in various places to store a window configuration.")
  3388. (defvar org-finish-function nil
  3389. "Function to be called when `C-c C-c' is used.
  3390. This is for getting out of special buffers like remember.")
  3391. ;; FIXME: Occasionally check by commenting these, to make sure
  3392. ;; no other functions uses these, forgetting to let-bind them.
  3393. (defvar entry)
  3394. (defvar last-state)
  3395. (defvar date)
  3396. ;; Defined somewhere in this file, but used before definition.
  3397. (defvar org-html-entities)
  3398. (defvar org-struct-menu)
  3399. (defvar org-org-menu)
  3400. (defvar org-tbl-menu)
  3401. (defvar org-agenda-keymap)
  3402. ;;;; Define the Org-mode
  3403. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3404. (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."))
  3405. ;; We use a before-change function to check if a table might need
  3406. ;; an update.
  3407. (defvar org-table-may-need-update t
  3408. "Indicates that a table might need an update.
  3409. This variable is set by `org-before-change-function'.
  3410. `org-table-align' sets it back to nil.")
  3411. (defun org-before-change-function (beg end)
  3412. "Every change indicates that a table might need an update."
  3413. (setq org-table-may-need-update t))
  3414. (defvar org-mode-map)
  3415. (defvar org-mode-hook nil
  3416. "Mode hook for Org-mode, run after the mode was turned on.")
  3417. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3418. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3419. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3420. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3421. (defvar org-table-buffer-is-an nil)
  3422. (defconst org-outline-regexp "\\*+ ")
  3423. ;;;###autoload
  3424. (define-derived-mode org-mode outline-mode "Org"
  3425. "Outline-based notes management and organizer, alias
  3426. \"Carsten's outline-mode for keeping track of everything.\"
  3427. Org-mode develops organizational tasks around a NOTES file which
  3428. contains information about projects as plain text. Org-mode is
  3429. implemented on top of outline-mode, which is ideal to keep the content
  3430. of large files well structured. It supports ToDo items, deadlines and
  3431. time stamps, which magically appear in the diary listing of the Emacs
  3432. calendar. Tables are easily created with a built-in table editor.
  3433. Plain text URL-like links connect to websites, emails (VM), Usenet
  3434. messages (Gnus), BBDB entries, and any files related to the project.
  3435. For printing and sharing of notes, an Org-mode file (or a part of it)
  3436. can be exported as a structured ASCII or HTML file.
  3437. The following commands are available:
  3438. \\{org-mode-map}"
  3439. ;; Get rid of Outline menus, they are not needed
  3440. ;; Need to do this here because define-derived-mode sets up
  3441. ;; the keymap so late. Still, it is a waste to call this each time
  3442. ;; we switch another buffer into org-mode.
  3443. (if (featurep 'xemacs)
  3444. (when (boundp 'outline-mode-menu-heading)
  3445. ;; Assume this is Greg's port, it used easymenu
  3446. (easy-menu-remove outline-mode-menu-heading)
  3447. (easy-menu-remove outline-mode-menu-show)
  3448. (easy-menu-remove outline-mode-menu-hide))
  3449. (define-key org-mode-map [menu-bar headings] 'undefined)
  3450. (define-key org-mode-map [menu-bar hide] 'undefined)
  3451. (define-key org-mode-map [menu-bar show] 'undefined))
  3452. (org-load-modules-maybe)
  3453. (easy-menu-add org-org-menu)
  3454. (easy-menu-add org-tbl-menu)
  3455. (org-install-agenda-files-menu)
  3456. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3457. (org-add-to-invisibility-spec '(org-cwidth))
  3458. (org-add-to-invisibility-spec '(org-hide-block . t))
  3459. (when (featurep 'xemacs)
  3460. (org-set-local 'line-move-ignore-invisible t))
  3461. (org-set-local 'outline-regexp org-outline-regexp)
  3462. (org-set-local 'outline-level 'org-outline-level)
  3463. (when (and org-ellipsis
  3464. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3465. (fboundp 'make-glyph-code))
  3466. (unless org-display-table
  3467. (setq org-display-table (make-display-table)))
  3468. (set-display-table-slot
  3469. org-display-table 4
  3470. (vconcat (mapcar
  3471. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3472. org-ellipsis)))
  3473. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3474. (setq buffer-display-table org-display-table))
  3475. (org-set-regexps-and-options)
  3476. (when (and org-tag-faces (not org-tags-special-faces-re))
  3477. ;; tag faces set outside customize.... force initialization.
  3478. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3479. ;; Calc embedded
  3480. (org-set-local 'calc-embedded-open-mode "# ")
  3481. (modify-syntax-entry ?# "<")
  3482. (modify-syntax-entry ?@ "w")
  3483. (if org-startup-truncated (setq truncate-lines t))
  3484. (org-set-local 'font-lock-unfontify-region-function
  3485. 'org-unfontify-region)
  3486. ;; Activate before-change-function
  3487. (org-set-local 'org-table-may-need-update t)
  3488. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3489. 'local)
  3490. ;; Check for running clock before killing a buffer
  3491. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3492. ;; Paragraphs and auto-filling
  3493. (org-set-autofill-regexps)
  3494. (setq indent-line-function 'org-indent-line-function)
  3495. (org-update-radio-target-regexp)
  3496. ;; Make sure dependence stuff works reliably, even for users who set it
  3497. ;; too late :-(
  3498. (if org-enforce-todo-dependencies
  3499. (add-hook 'org-blocker-hook
  3500. 'org-block-todo-from-children-or-siblings-or-parent)
  3501. (remove-hook 'org-blocker-hook
  3502. 'org-block-todo-from-children-or-siblings-or-parent))
  3503. (if org-enforce-todo-checkbox-dependencies
  3504. (add-hook 'org-blocker-hook
  3505. 'org-block-todo-from-checkboxes)
  3506. (remove-hook 'org-blocker-hook
  3507. 'org-block-todo-from-checkboxes))
  3508. ;; Comment characters
  3509. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3510. (org-set-local 'comment-padding " ")
  3511. ;; Align options lines
  3512. (org-set-local
  3513. 'align-mode-rules-list
  3514. '((org-in-buffer-settings
  3515. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3516. (modes . '(org-mode)))))
  3517. ;; Imenu
  3518. (org-set-local 'imenu-create-index-function
  3519. 'org-imenu-get-tree)
  3520. ;; Make isearch reveal context
  3521. (if (or (featurep 'xemacs)
  3522. (not (boundp 'outline-isearch-open-invisible-function)))
  3523. ;; Emacs 21 and XEmacs make use of the hook
  3524. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3525. ;; Emacs 22 deals with this through a special variable
  3526. (org-set-local 'outline-isearch-open-invisible-function
  3527. (lambda (&rest ignore) (org-show-context 'isearch))))
  3528. ;; If empty file that did not turn on org-mode automatically, make it to.
  3529. (if (and org-insert-mode-line-in-empty-file
  3530. (interactive-p)
  3531. (= (point-min) (point-max)))
  3532. (insert "# -*- mode: org -*-\n\n"))
  3533. (unless org-inhibit-startup
  3534. (when org-startup-align-all-tables
  3535. (let ((bmp (buffer-modified-p)))
  3536. (org-table-map-tables 'org-table-align)
  3537. (set-buffer-modified-p bmp)))
  3538. (when org-startup-indented
  3539. (require 'org-indent)
  3540. (org-indent-mode 1))
  3541. (org-set-startup-visibility)))
  3542. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3543. (defun org-current-time ()
  3544. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3545. (if (> (car org-time-stamp-rounding-minutes) 1)
  3546. (let ((r (car org-time-stamp-rounding-minutes))
  3547. (time (decode-time)))
  3548. (apply 'encode-time
  3549. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3550. (nthcdr 2 time))))
  3551. (current-time)))
  3552. ;;;; Font-Lock stuff, including the activators
  3553. (defvar org-mouse-map (make-sparse-keymap))
  3554. (org-defkey org-mouse-map
  3555. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3556. (org-defkey org-mouse-map
  3557. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3558. (when org-mouse-1-follows-link
  3559. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3560. (when org-tab-follows-link
  3561. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3562. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3563. (require 'font-lock)
  3564. (defconst org-non-link-chars "]\t\n\r<>")
  3565. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3566. "shell" "elisp"))
  3567. (defvar org-link-types-re nil
  3568. "Matches a link that has a url-like prefix like \"http:\"")
  3569. (defvar org-link-re-with-space nil
  3570. "Matches a link with spaces, optional angular brackets around it.")
  3571. (defvar org-link-re-with-space2 nil
  3572. "Matches a link with spaces, optional angular brackets around it.")
  3573. (defvar org-link-re-with-space3 nil
  3574. "Matches a link with spaces, only for internal part in bracket links.")
  3575. (defvar org-angle-link-re nil
  3576. "Matches link with angular brackets, spaces are allowed.")
  3577. (defvar org-plain-link-re nil
  3578. "Matches plain link, without spaces.")
  3579. (defvar org-bracket-link-regexp nil
  3580. "Matches a link in double brackets.")
  3581. (defvar org-bracket-link-analytic-regexp nil
  3582. "Regular expression used to analyze links.
  3583. Here is what the match groups contain after a match:
  3584. 1: http:
  3585. 2: http
  3586. 3: path
  3587. 4: [desc]
  3588. 5: desc")
  3589. (defvar org-bracket-link-analytic-regexp++ nil
  3590. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3591. (defvar org-any-link-re nil
  3592. "Regular expression matching any link.")
  3593. (defun org-make-link-regexps ()
  3594. "Update the link regular expressions.
  3595. This should be called after the variable `org-link-types' has changed."
  3596. (setq org-link-types-re
  3597. (concat
  3598. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3599. org-link-re-with-space
  3600. (concat
  3601. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3602. "\\([^" org-non-link-chars " ]"
  3603. "[^" org-non-link-chars "]*"
  3604. "[^" org-non-link-chars " ]\\)>?")
  3605. org-link-re-with-space2
  3606. (concat
  3607. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3608. "\\([^" org-non-link-chars " ]"
  3609. "[^\t\n\r]*"
  3610. "[^" org-non-link-chars " ]\\)>?")
  3611. org-link-re-with-space3
  3612. (concat
  3613. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3614. "\\([^" org-non-link-chars " ]"
  3615. "[^\t\n\r]*\\)")
  3616. org-angle-link-re
  3617. (concat
  3618. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3619. "\\([^" org-non-link-chars " ]"
  3620. "[^" org-non-link-chars "]*"
  3621. "\\)>")
  3622. org-plain-link-re
  3623. (concat
  3624. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3625. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3626. org-bracket-link-regexp
  3627. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3628. org-bracket-link-analytic-regexp
  3629. (concat
  3630. "\\[\\["
  3631. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3632. "\\([^]]+\\)"
  3633. "\\]"
  3634. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3635. "\\]")
  3636. org-bracket-link-analytic-regexp++
  3637. (concat
  3638. "\\[\\["
  3639. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3640. "\\([^]]+\\)"
  3641. "\\]"
  3642. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3643. "\\]")
  3644. org-any-link-re
  3645. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3646. org-angle-link-re "\\)\\|\\("
  3647. org-plain-link-re "\\)")))
  3648. (org-make-link-regexps)
  3649. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3650. "Regular expression for fast time stamp matching.")
  3651. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3652. "Regular expression for fast time stamp matching.")
  3653. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3654. "Regular expression matching time strings for analysis.
  3655. This one does not require the space after the date, so it can be used
  3656. on a string that terminates immediately after the date.")
  3657. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3658. "Regular expression matching time strings for analysis.")
  3659. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3660. "Regular expression matching time stamps, with groups.")
  3661. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3662. "Regular expression matching time stamps (also [..]), with groups.")
  3663. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3664. "Regular expression matching a time stamp range.")
  3665. (defconst org-tr-regexp-both
  3666. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3667. "Regular expression matching a time stamp range.")
  3668. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3669. org-ts-regexp "\\)?")
  3670. "Regular expression matching a time stamp or time stamp range.")
  3671. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3672. org-ts-regexp-both "\\)?")
  3673. "Regular expression matching a time stamp or time stamp range.
  3674. The time stamps may be either active or inactive.")
  3675. (defvar org-emph-face nil)
  3676. (defun org-do-emphasis-faces (limit)
  3677. "Run through the buffer and add overlays to links."
  3678. (let (rtn a)
  3679. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3680. (if (not (= (char-after (match-beginning 3))
  3681. (char-after (match-beginning 4))))
  3682. (progn
  3683. (setq rtn t)
  3684. (setq a (assoc (match-string 3) org-emphasis-alist))
  3685. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3686. 'face
  3687. (nth 1 a))
  3688. (and (nth 4 a)
  3689. (org-remove-flyspell-overlays-in
  3690. (match-beginning 0) (match-end 0)))
  3691. (add-text-properties (match-beginning 2) (match-end 2)
  3692. '(font-lock-multiline t))
  3693. (when org-hide-emphasis-markers
  3694. (add-text-properties (match-end 4) (match-beginning 5)
  3695. '(invisible org-link))
  3696. (add-text-properties (match-beginning 3) (match-end 3)
  3697. '(invisible org-link)))))
  3698. (backward-char 1))
  3699. rtn))
  3700. (defun org-emphasize (&optional char)
  3701. "Insert or change an emphasis, i.e. a font like bold or italic.
  3702. If there is an active region, change that region to a new emphasis.
  3703. If there is no region, just insert the marker characters and position
  3704. the cursor between them.
  3705. CHAR should be either the marker character, or the first character of the
  3706. HTML tag associated with that emphasis. If CHAR is a space, the means
  3707. to remove the emphasis of the selected region.
  3708. If char is not given (for example in an interactive call) it
  3709. will be prompted for."
  3710. (interactive)
  3711. (let ((eal org-emphasis-alist) e det
  3712. (erc org-emphasis-regexp-components)
  3713. (prompt "")
  3714. (string "") beg end move tag c s)
  3715. (if (org-region-active-p)
  3716. (setq beg (region-beginning) end (region-end)
  3717. string (buffer-substring beg end))
  3718. (setq move t))
  3719. (while (setq e (pop eal))
  3720. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3721. c (aref tag 0))
  3722. (push (cons c (string-to-char (car e))) det)
  3723. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3724. (substring tag 1)))))
  3725. (setq det (nreverse det))
  3726. (unless char
  3727. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3728. (setq char (read-char-exclusive)))
  3729. (setq char (or (cdr (assoc char det)) char))
  3730. (if (equal char ?\ )
  3731. (setq s "" move nil)
  3732. (unless (assoc (char-to-string char) org-emphasis-alist)
  3733. (error "No such emphasis marker: \"%c\"" char))
  3734. (setq s (char-to-string char)))
  3735. (while (and (> (length string) 1)
  3736. (equal (substring string 0 1) (substring string -1))
  3737. (assoc (substring string 0 1) org-emphasis-alist))
  3738. (setq string (substring string 1 -1)))
  3739. (setq string (concat s string s))
  3740. (if beg (delete-region beg end))
  3741. (unless (or (bolp)
  3742. (string-match (concat "[" (nth 0 erc) "\n]")
  3743. (char-to-string (char-before (point)))))
  3744. (insert " "))
  3745. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3746. (char-to-string (char-after (point))))
  3747. (insert " ") (backward-char 1))
  3748. (insert string)
  3749. (and move (backward-char 1))))
  3750. (defconst org-nonsticky-props
  3751. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3752. (defsubst org-rear-nonsticky-at (pos)
  3753. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3754. (defun org-activate-plain-links (limit)
  3755. "Run through the buffer and add overlays to links."
  3756. (catch 'exit
  3757. (let (f)
  3758. (if (re-search-forward org-plain-link-re limit t)
  3759. (progn
  3760. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3761. (setq f (get-text-property (match-beginning 0) 'face))
  3762. (if (or (eq f 'org-tag)
  3763. (and (listp f) (memq 'org-tag f)))
  3764. nil
  3765. (add-text-properties (match-beginning 0) (match-end 0)
  3766. (list 'mouse-face 'highlight
  3767. 'keymap org-mouse-map))
  3768. (org-rear-nonsticky-at (match-end 0)))
  3769. t)))))
  3770. (defun org-activate-code (limit)
  3771. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3772. (progn
  3773. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3774. (remove-text-properties (match-beginning 0) (match-end 0)
  3775. '(display t invisible t intangible t))
  3776. t)))
  3777. (defun org-fontify-meta-lines-and-blocks (limit)
  3778. "Fontify #+ lines and blocks, in the correct ways."
  3779. (let ((case-fold-search t))
  3780. (if (re-search-forward
  3781. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3782. limit t)
  3783. (let ((beg (match-beginning 0))
  3784. (beg1 (line-beginning-position 2))
  3785. (dc1 (downcase (match-string 2)))
  3786. (dc3 (downcase (match-string 3)))
  3787. end end1 quoting)
  3788. (cond
  3789. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3790. ;; a single line of backend-specific content
  3791. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3792. (remove-text-properties (match-beginning 0) (match-end 0)
  3793. '(display t invisible t intangible t))
  3794. (add-text-properties (match-beginning 1) (match-end 3)
  3795. '(font-lock-fontified t face org-meta-line))
  3796. (add-text-properties (match-beginning 6) (match-end 6)
  3797. '(font-lock-fontified t face org-block))
  3798. t)
  3799. ((and (match-end 4) (equal dc3 "begin"))
  3800. ;; Truely a block
  3801. (setq quoting (member (downcase (match-string 5))
  3802. org-protecting-blocks))
  3803. (when (re-search-forward
  3804. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3805. nil t) ;; on purpose, we look further than LIMIT
  3806. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3807. (when quoting
  3808. (remove-text-properties beg end
  3809. '(display t invisible t intangible t)))
  3810. (add-text-properties
  3811. beg end
  3812. '(font-lock-fontified t font-lock-multiline t))
  3813. (add-text-properties beg beg1 '(face org-meta-line))
  3814. (add-text-properties end1 end '(face org-meta-line))
  3815. (when quoting
  3816. (add-text-properties beg1 end1 '(face org-block)))
  3817. t))
  3818. ((not (member (char-after beg) '(?\ ?\t)))
  3819. ;; just any other in-buffer setting, but not indented
  3820. (add-text-properties
  3821. beg (match-end 0)
  3822. '(font-lock-fontified t face org-meta-line))
  3823. t)
  3824. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3825. (and (match-end 4) (equal dc3 "attr")))
  3826. (add-text-properties
  3827. beg (match-end 0)
  3828. '(font-lock-fontified t face org-meta-line))
  3829. t)
  3830. (t nil))))))
  3831. (defun org-activate-angle-links (limit)
  3832. "Run through the buffer and add overlays to links."
  3833. (if (re-search-forward org-angle-link-re limit t)
  3834. (progn
  3835. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3836. (add-text-properties (match-beginning 0) (match-end 0)
  3837. (list 'mouse-face 'highlight
  3838. 'keymap org-mouse-map))
  3839. (org-rear-nonsticky-at (match-end 0))
  3840. t)))
  3841. (defun org-activate-footnote-links (limit)
  3842. "Run through the buffer and add overlays to links."
  3843. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3844. limit t)
  3845. (progn
  3846. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3847. (add-text-properties (match-beginning 2) (match-end 2)
  3848. (list 'mouse-face 'highlight
  3849. 'keymap org-mouse-map
  3850. 'help-echo
  3851. (if (= (point-at-bol) (match-beginning 2))
  3852. "Footnote definition"
  3853. "Footnote reference")
  3854. ))
  3855. (org-rear-nonsticky-at (match-end 2))
  3856. t)))
  3857. (defun org-activate-bracket-links (limit)
  3858. "Run through the buffer and add overlays to bracketed links."
  3859. (if (re-search-forward org-bracket-link-regexp limit t)
  3860. (let* ((help (concat "LINK: "
  3861. (org-match-string-no-properties 1)))
  3862. ;; FIXME: above we should remove the escapes.
  3863. ;; but that requires another match, protecting match data,
  3864. ;; a lot of overhead for font-lock.
  3865. (ip (org-maybe-intangible
  3866. (list 'invisible 'org-link
  3867. 'keymap org-mouse-map 'mouse-face 'highlight
  3868. 'font-lock-multiline t 'help-echo help)))
  3869. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3870. 'font-lock-multiline t 'help-echo help)))
  3871. ;; We need to remove the invisible property here. Table narrowing
  3872. ;; may have made some of this invisible.
  3873. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3874. (remove-text-properties (match-beginning 0) (match-end 0)
  3875. '(invisible nil))
  3876. (if (match-end 3)
  3877. (progn
  3878. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3879. (org-rear-nonsticky-at (match-beginning 3))
  3880. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3881. (org-rear-nonsticky-at (match-end 3))
  3882. (add-text-properties (match-end 3) (match-end 0) ip)
  3883. (org-rear-nonsticky-at (match-end 0)))
  3884. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3885. (org-rear-nonsticky-at (match-beginning 1))
  3886. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3887. (org-rear-nonsticky-at (match-end 1))
  3888. (add-text-properties (match-end 1) (match-end 0) ip)
  3889. (org-rear-nonsticky-at (match-end 0)))
  3890. t)))
  3891. (defun org-activate-dates (limit)
  3892. "Run through the buffer and add overlays to dates."
  3893. (if (re-search-forward org-tsr-regexp-both limit t)
  3894. (progn
  3895. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3896. (add-text-properties (match-beginning 0) (match-end 0)
  3897. (list 'mouse-face 'highlight
  3898. 'keymap org-mouse-map))
  3899. (org-rear-nonsticky-at (match-end 0))
  3900. (when org-display-custom-times
  3901. (if (match-end 3)
  3902. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3903. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3904. t)))
  3905. (defvar org-target-link-regexp nil
  3906. "Regular expression matching radio targets in plain text.")
  3907. (make-variable-buffer-local 'org-target-link-regexp)
  3908. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3909. "Regular expression matching a link target.")
  3910. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3911. "Regular expression matching a radio target.")
  3912. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3913. "Regular expression matching any target.")
  3914. (defun org-activate-target-links (limit)
  3915. "Run through the buffer and add overlays to target matches."
  3916. (when org-target-link-regexp
  3917. (let ((case-fold-search t))
  3918. (if (re-search-forward org-target-link-regexp limit t)
  3919. (progn
  3920. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3921. (add-text-properties (match-beginning 0) (match-end 0)
  3922. (list 'mouse-face 'highlight
  3923. 'keymap org-mouse-map
  3924. 'help-echo "Radio target link"
  3925. 'org-linked-text t))
  3926. (org-rear-nonsticky-at (match-end 0))
  3927. t)))))
  3928. (defun org-update-radio-target-regexp ()
  3929. "Find all radio targets in this file and update the regular expression."
  3930. (interactive)
  3931. (when (memq 'radio org-activate-links)
  3932. (setq org-target-link-regexp
  3933. (org-make-target-link-regexp (org-all-targets 'radio)))
  3934. (org-restart-font-lock)))
  3935. (defun org-hide-wide-columns (limit)
  3936. (let (s e)
  3937. (setq s (text-property-any (point) (or limit (point-max))
  3938. 'org-cwidth t))
  3939. (when s
  3940. (setq e (next-single-property-change s 'org-cwidth))
  3941. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3942. (goto-char e)
  3943. t)))
  3944. (defvar org-latex-and-specials-regexp nil
  3945. "Regular expression for highlighting export special stuff.")
  3946. (defvar org-match-substring-regexp)
  3947. (defvar org-match-substring-with-braces-regexp)
  3948. (defvar org-export-html-special-string-regexps)
  3949. (defun org-compute-latex-and-specials-regexp ()
  3950. "Compute regular expression for stuff treated specially by exporters."
  3951. (if (not org-highlight-latex-fragments-and-specials)
  3952. (org-set-local 'org-latex-and-specials-regexp nil)
  3953. (require 'org-exp)
  3954. (let*
  3955. ((matchers (plist-get org-format-latex-options :matchers))
  3956. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3957. org-latex-regexps)))
  3958. (options (org-combine-plists (org-default-export-plist)
  3959. (org-infile-export-plist)))
  3960. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3961. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3962. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3963. (org-export-html-expand (plist-get options :expand-quoted-html))
  3964. (org-export-with-special-strings (plist-get options :special-strings))
  3965. (re-sub
  3966. (cond
  3967. ((equal org-export-with-sub-superscripts '{})
  3968. (list org-match-substring-with-braces-regexp))
  3969. (org-export-with-sub-superscripts
  3970. (list org-match-substring-regexp))
  3971. (t nil)))
  3972. (re-latex
  3973. (if org-export-with-LaTeX-fragments
  3974. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3975. (re-macros
  3976. (if org-export-with-TeX-macros
  3977. (list (concat "\\\\"
  3978. (regexp-opt
  3979. (append (mapcar 'car org-html-entities)
  3980. (if (boundp 'org-latex-entities)
  3981. (mapcar (lambda (x)
  3982. (or (car-safe x) x))
  3983. org-latex-entities)
  3984. nil))
  3985. 'words))) ; FIXME
  3986. ))
  3987. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3988. (re-special (if org-export-with-special-strings
  3989. (mapcar (lambda (x) (car x))
  3990. org-export-html-special-string-regexps)))
  3991. (re-rest
  3992. (delq nil
  3993. (list
  3994. (if org-export-html-expand "@<[^>\n]+>")
  3995. ))))
  3996. (org-set-local
  3997. 'org-latex-and-specials-regexp
  3998. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3999. re-rest) "\\|")))))
  4000. (defun org-do-latex-and-special-faces (limit)
  4001. "Run through the buffer and add overlays to links."
  4002. (when org-latex-and-specials-regexp
  4003. (let (rtn d)
  4004. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4005. limit t))
  4006. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4007. 'face))
  4008. '(org-code org-verbatim underline)))
  4009. (progn
  4010. (setq rtn t
  4011. d (cond ((member (char-after (1+ (match-beginning 0)))
  4012. '(?_ ?^)) 1)
  4013. (t 0)))
  4014. (font-lock-prepend-text-property
  4015. (+ d (match-beginning 0)) (match-end 0)
  4016. 'face 'org-latex-and-export-specials)
  4017. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4018. '(font-lock-multiline t)))))
  4019. rtn)))
  4020. (defun org-restart-font-lock ()
  4021. "Restart font-lock-mode, to force refontification."
  4022. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4023. (font-lock-mode -1)
  4024. (font-lock-mode 1)))
  4025. (defun org-all-targets (&optional radio)
  4026. "Return a list of all targets in this file.
  4027. With optional argument RADIO, only find radio targets."
  4028. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4029. rtn)
  4030. (save-excursion
  4031. (goto-char (point-min))
  4032. (while (re-search-forward re nil t)
  4033. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4034. rtn)))
  4035. (defun org-make-target-link-regexp (targets)
  4036. "Make regular expression matching all strings in TARGETS.
  4037. The regular expression finds the targets also if there is a line break
  4038. between words."
  4039. (and targets
  4040. (concat
  4041. "\\<\\("
  4042. (mapconcat
  4043. (lambda (x)
  4044. (while (string-match " +" x)
  4045. (setq x (replace-match "\\s-+" t t x)))
  4046. x)
  4047. targets
  4048. "\\|")
  4049. "\\)\\>")))
  4050. (defun org-activate-tags (limit)
  4051. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4052. (progn
  4053. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4054. (add-text-properties (match-beginning 1) (match-end 1)
  4055. (list 'mouse-face 'highlight
  4056. 'keymap org-mouse-map))
  4057. (org-rear-nonsticky-at (match-end 1))
  4058. t)))
  4059. (defun org-outline-level ()
  4060. (save-excursion
  4061. (looking-at outline-regexp)
  4062. (if (match-beginning 1)
  4063. (+ (org-get-string-indentation (match-string 1)) 1000)
  4064. (1- (- (match-end 0) (match-beginning 0))))))
  4065. (defvar org-font-lock-keywords nil)
  4066. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4067. "Regular expression matching a property line.")
  4068. (defvar org-font-lock-hook nil
  4069. "Functions to be called for special font lock stuff.")
  4070. (defun org-font-lock-hook (limit)
  4071. (run-hook-with-args 'org-font-lock-hook limit))
  4072. (defun org-set-font-lock-defaults ()
  4073. (let* ((em org-fontify-emphasized-text)
  4074. (lk org-activate-links)
  4075. (org-font-lock-extra-keywords
  4076. (list
  4077. ;; Call the hook
  4078. '(org-font-lock-hook)
  4079. ;; Headlines
  4080. `(,(if org-fontify-whole-heading-line
  4081. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4082. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4083. (1 (org-get-level-face 1))
  4084. (2 (org-get-level-face 2))
  4085. (3 (org-get-level-face 3)))
  4086. ;; Table lines
  4087. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4088. (1 'org-table t))
  4089. ;; Table internals
  4090. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4091. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4092. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4093. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4094. ;; Drawers
  4095. (list org-drawer-regexp '(0 'org-special-keyword t))
  4096. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4097. ;; Properties
  4098. (list org-property-re
  4099. '(1 'org-special-keyword t)
  4100. '(3 'org-property-value t))
  4101. ;; Links
  4102. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4103. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4104. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4105. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4106. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4107. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4108. (if (memq 'footnote lk) '(org-activate-footnote-links
  4109. (2 'org-footnote t)))
  4110. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4111. '(org-hide-wide-columns (0 nil append))
  4112. ;; TODO lines
  4113. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4114. '(1 (org-get-todo-face 1) t))
  4115. ;; DONE
  4116. (if org-fontify-done-headline
  4117. (list (concat "^[*]+ +\\<\\("
  4118. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4119. "\\)\\(.*\\)")
  4120. '(2 'org-headline-done t))
  4121. nil)
  4122. ;; Priorities
  4123. '(org-font-lock-add-priority-faces)
  4124. ;; Tags
  4125. '(org-font-lock-add-tag-faces)
  4126. ;; Special keywords
  4127. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4128. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4129. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4130. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4131. ;; Emphasis
  4132. (if em
  4133. (if (featurep 'xemacs)
  4134. '(org-do-emphasis-faces (0 nil append))
  4135. '(org-do-emphasis-faces)))
  4136. ;; Checkboxes
  4137. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4138. 2 'org-checkbox prepend)
  4139. (if org-provide-checkbox-statistics
  4140. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4141. (0 (org-get-checkbox-statistics-face) t)))
  4142. ;; Description list items
  4143. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4144. 2 'bold prepend)
  4145. ;; ARCHIVEd headings
  4146. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4147. '(1 'org-archived prepend))
  4148. ;; Specials
  4149. '(org-do-latex-and-special-faces)
  4150. ;; Code
  4151. '(org-activate-code (1 'org-code t))
  4152. ;; COMMENT
  4153. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4154. "\\|" org-quote-string "\\)\\>")
  4155. '(1 'org-special-keyword t))
  4156. '("^#.*" (0 'font-lock-comment-face t))
  4157. ;; Blocks and meta lines
  4158. '(org-fontify-meta-lines-and-blocks)
  4159. )))
  4160. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4161. ;; Now set the full font-lock-keywords
  4162. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4163. (org-set-local 'font-lock-defaults
  4164. '(org-font-lock-keywords t nil nil backward-paragraph))
  4165. (kill-local-variable 'font-lock-keywords) nil))
  4166. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4167. "Fontify string S like in Org-mode"
  4168. (with-temp-buffer
  4169. (insert s)
  4170. (let ((org-odd-levels-only odd-levels))
  4171. (org-mode)
  4172. (font-lock-fontify-buffer)
  4173. (buffer-string))))
  4174. (defvar org-m nil)
  4175. (defvar org-l nil)
  4176. (defvar org-f nil)
  4177. (defun org-get-level-face (n)
  4178. "Get the right face for match N in font-lock matching of headlines."
  4179. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4180. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4181. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4182. (cond
  4183. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4184. ((eq n 2) org-f)
  4185. (t (if org-level-color-stars-only nil org-f))))
  4186. (defun org-get-todo-face (kwd)
  4187. "Get the right face for a TODO keyword KWD.
  4188. If KWD is a number, get the corresponding match group."
  4189. (if (numberp kwd) (setq kwd (match-string kwd)))
  4190. (or (cdr (assoc kwd org-todo-keyword-faces))
  4191. (and (member kwd org-done-keywords) 'org-done)
  4192. 'org-todo))
  4193. (defun org-font-lock-add-tag-faces (limit)
  4194. "Add the special tag faces."
  4195. (when (and org-tag-faces org-tags-special-faces-re)
  4196. (while (re-search-forward org-tags-special-faces-re limit t)
  4197. (add-text-properties (match-beginning 1) (match-end 1)
  4198. (list 'face (org-get-tag-face 1)
  4199. 'font-lock-fontified t))
  4200. (backward-char 1))))
  4201. (defun org-font-lock-add-priority-faces (limit)
  4202. "Add the special priority faces."
  4203. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4204. (add-text-properties
  4205. (match-beginning 0) (match-end 0)
  4206. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4207. org-priority-faces))
  4208. 'org-special-keyword)
  4209. 'font-lock-fontified t))))
  4210. (defun org-get-tag-face (kwd)
  4211. "Get the right face for a TODO keyword KWD.
  4212. If KWD is a number, get the corresponding match group."
  4213. (if (numberp kwd) (setq kwd (match-string kwd)))
  4214. (or (cdr (assoc kwd org-tag-faces))
  4215. 'org-tag))
  4216. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4217. "Remove fontification and activation overlays from links."
  4218. (font-lock-default-unfontify-region beg end)
  4219. (let* ((buffer-undo-list t)
  4220. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4221. (inhibit-modification-hooks t)
  4222. deactivate-mark buffer-file-name buffer-file-truename)
  4223. (remove-text-properties beg end
  4224. '(mouse-face t keymap t org-linked-text t
  4225. invisible t intangible t
  4226. line-prefix t wrap-prefix t
  4227. org-no-flyspell t))))
  4228. ;;;; Visibility cycling, including org-goto and indirect buffer
  4229. ;;; Cycling
  4230. (defvar org-cycle-global-status nil)
  4231. (make-variable-buffer-local 'org-cycle-global-status)
  4232. (defvar org-cycle-subtree-status nil)
  4233. (make-variable-buffer-local 'org-cycle-subtree-status)
  4234. ;;;###autoload
  4235. (defvar org-inlinetask-min-level)
  4236. (defun org-cycle (&optional arg)
  4237. "TAB-action and visibility cycling for Org-mode.
  4238. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4239. is outine visibility cycling, but it also invokes other actions
  4240. in special contexts.
  4241. - When this function is called with a prefix argument, rotate the entire
  4242. buffer through 3 states (global cycling)
  4243. 1. OVERVIEW: Show only top-level headlines.
  4244. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4245. 3. SHOW ALL: Show everything.
  4246. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4247. determined by the variable `org-startup-folded', and by any VISIBILITY
  4248. properties in the buffer.
  4249. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4250. including any drawers.
  4251. - When inside a table, re-align the table and move to the next field.
  4252. - When point is at the beginning of a headline, rotate the subtree started
  4253. by this line through 3 different states (local cycling)
  4254. 1. FOLDED: Only the main headline is shown.
  4255. 2. CHILDREN: The main headline and the direct children are shown.
  4256. From this state, you can move to one of the children
  4257. and zoom in further.
  4258. 3. SUBTREE: Show the entire subtree, including body text.
  4259. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4260. - When there is a numeric prefix, go up to a heading with level ARG, do
  4261. a `show-subtree' and return to the previous cursor position. If ARG
  4262. is negative, go up that many levels.
  4263. - When point is not at the beginning of a headline, execute the global
  4264. binding for TAB, which is re-indenting the line. See the option
  4265. `org-cycle-emulate-tab' for details.
  4266. - Special case: if point is at the beginning of the buffer and there is
  4267. no headline in line 1, this function will act as if called with prefix arg.
  4268. But only if also the variable `org-cycle-global-at-bob' is t."
  4269. (interactive "P")
  4270. (org-load-modules-maybe)
  4271. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4272. (let* ((limit-level
  4273. (or org-cycle-max-level
  4274. (and (boundp 'org-inlinetask-min-level)
  4275. org-inlinetask-min-level
  4276. (1- org-inlinetask-min-level))))
  4277. (nstars (and limit-level
  4278. (if org-odd-levels-only
  4279. (and limit-level (1- (* limit-level 2)))
  4280. limit-level)))
  4281. (outline-regexp
  4282. (cond
  4283. ((not (org-mode-p)) outline-regexp)
  4284. ((or (eq org-cycle-include-plain-lists 'integrate)
  4285. (and org-cycle-include-plain-lists (org-at-item-p)))
  4286. (concat "\\(?:\\*"
  4287. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4288. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4289. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4290. (bob-special (and org-cycle-global-at-bob (bobp)
  4291. (not (looking-at outline-regexp))))
  4292. (org-cycle-hook
  4293. (if bob-special
  4294. (delq 'org-optimize-window-after-visibility-change
  4295. (copy-sequence org-cycle-hook))
  4296. org-cycle-hook))
  4297. (pos (point)))
  4298. (if (or bob-special (equal arg '(4)))
  4299. ;; special case: use global cycling
  4300. (setq arg t))
  4301. (cond
  4302. ((equal arg '(16))
  4303. (org-set-startup-visibility)
  4304. (message "Startup visibility, plus VISIBILITY properties"))
  4305. ((equal arg '(64))
  4306. (show-all)
  4307. (message "Entire buffer visible, including drawers"))
  4308. ((org-at-table-p 'any)
  4309. ;; Enter the table or move to the next field in the table
  4310. (or (org-table-recognize-table.el)
  4311. (progn
  4312. (if arg (org-table-edit-field t)
  4313. (org-table-justify-field-maybe)
  4314. (call-interactively 'org-table-next-field)))))
  4315. ((run-hook-with-args-until-success
  4316. 'org-tab-after-check-for-table-hook))
  4317. ((eq arg t) ;; Global cycling
  4318. (org-cycle-internal-global))
  4319. ((and org-drawers org-drawer-regexp
  4320. (save-excursion
  4321. (beginning-of-line 1)
  4322. (looking-at org-drawer-regexp)))
  4323. ;; Toggle block visibility
  4324. (org-flag-drawer
  4325. (not (get-char-property (match-end 0) 'invisible))))
  4326. ((integerp arg)
  4327. ;; Show-subtree, ARG levels up from here.
  4328. (save-excursion
  4329. (org-back-to-heading)
  4330. (outline-up-heading (if (< arg 0) (- arg)
  4331. (- (funcall outline-level) arg)))
  4332. (org-show-subtree)))
  4333. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4334. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4335. (org-cycle-internal-local))
  4336. ;; TAB emulation and template completion
  4337. (buffer-read-only (org-back-to-heading))
  4338. ((run-hook-with-args-until-success
  4339. 'org-tab-after-check-for-cycling-hook))
  4340. ((org-try-structure-completion))
  4341. ((org-try-cdlatex-tab))
  4342. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4343. (or (not (bolp))
  4344. (not (looking-at outline-regexp))))
  4345. (call-interactively (global-key-binding "\t")))
  4346. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4347. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4348. (or (and (eq org-cycle-emulate-tab 'white)
  4349. (= (match-end 0) (point-at-eol)))
  4350. (and (eq org-cycle-emulate-tab 'whitestart)
  4351. (>= (match-end 0) pos))))
  4352. t
  4353. (eq org-cycle-emulate-tab t))
  4354. (call-interactively (global-key-binding "\t")))
  4355. (t (save-excursion
  4356. (org-back-to-heading)
  4357. (org-cycle)))))))
  4358. (defun org-cycle-internal-global ()
  4359. "Do the global cycling action."
  4360. (cond
  4361. ((and (eq last-command this-command)
  4362. (eq org-cycle-global-status 'overview))
  4363. ;; We just created the overview - now do table of contents
  4364. ;; This can be slow in very large buffers, so indicate action
  4365. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4366. (message "CONTENTS...")
  4367. (org-content)
  4368. (message "CONTENTS...done")
  4369. (setq org-cycle-global-status 'contents)
  4370. (run-hook-with-args 'org-cycle-hook 'contents))
  4371. ((and (eq last-command this-command)
  4372. (eq org-cycle-global-status 'contents))
  4373. ;; We just showed the table of contents - now show everything
  4374. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4375. (show-all)
  4376. (message "SHOW ALL")
  4377. (setq org-cycle-global-status 'all)
  4378. (run-hook-with-args 'org-cycle-hook 'all))
  4379. (t
  4380. ;; Default action: go to overview
  4381. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4382. (org-overview)
  4383. (message "OVERVIEW")
  4384. (setq org-cycle-global-status 'overview)
  4385. (run-hook-with-args 'org-cycle-hook 'overview))))
  4386. (defun org-cycle-internal-local ()
  4387. "Do the local cycling action."
  4388. (org-back-to-heading)
  4389. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4390. ;; First, some boundaries
  4391. (save-excursion
  4392. (org-back-to-heading)
  4393. (setq level (funcall outline-level))
  4394. (save-excursion
  4395. (beginning-of-line 2)
  4396. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4397. ; XEmacs does not have `next-single-char-property-change'
  4398. ; I'm not sure about Emacs 21.
  4399. (while (and (not (eobp)) ;; this is like `next-line'
  4400. (get-char-property (1- (point)) 'invisible))
  4401. (beginning-of-line 2))
  4402. (while (and (not (eobp)) ;; this is like `next-line'
  4403. (get-char-property (1- (point)) 'invisible))
  4404. (goto-char (next-single-char-property-change (point) 'invisible))
  4405. (or (bolp) (beginning-of-line 2))))
  4406. (setq eol (point)))
  4407. (outline-end-of-heading) (setq eoh (point))
  4408. (save-excursion
  4409. (outline-next-heading)
  4410. (setq has-children (and (org-at-heading-p t)
  4411. (> (funcall outline-level) level))))
  4412. (org-end-of-subtree t)
  4413. (unless (eobp)
  4414. (skip-chars-forward " \t\n")
  4415. (beginning-of-line 1) ; in case this is an item
  4416. )
  4417. (setq eos (1- (point))))
  4418. ;; Find out what to do next and set `this-command'
  4419. (cond
  4420. ((= eos eoh)
  4421. ;; Nothing is hidden behind this heading
  4422. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4423. (message "EMPTY ENTRY")
  4424. (setq org-cycle-subtree-status nil)
  4425. (save-excursion
  4426. (goto-char eos)
  4427. (outline-next-heading)
  4428. (if (org-invisible-p) (org-flag-heading nil))))
  4429. ((and (or (>= eol eos)
  4430. (not (string-match "\\S-" (buffer-substring eol eos))))
  4431. (or has-children
  4432. (not (setq children-skipped
  4433. org-cycle-skip-children-state-if-no-children))))
  4434. ;; Entire subtree is hidden in one line: children view
  4435. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4436. (org-show-entry)
  4437. (show-children)
  4438. (message "CHILDREN")
  4439. (save-excursion
  4440. (goto-char eos)
  4441. (outline-next-heading)
  4442. (if (org-invisible-p) (org-flag-heading nil)))
  4443. (setq org-cycle-subtree-status 'children)
  4444. (run-hook-with-args 'org-cycle-hook 'children))
  4445. ((and (eq last-command this-command)
  4446. (or children-skipped
  4447. (eq org-cycle-subtree-status 'children)))
  4448. ;; We just showed the children, now show everything.
  4449. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4450. (org-show-subtree)
  4451. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4452. (setq org-cycle-subtree-status 'subtree)
  4453. (run-hook-with-args 'org-cycle-hook 'subtree))
  4454. (t
  4455. ;; Default action: hide the subtree.
  4456. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4457. (hide-subtree)
  4458. (message "FOLDED")
  4459. (setq org-cycle-subtree-status 'folded)
  4460. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4461. ;;;###autoload
  4462. (defun org-global-cycle (&optional arg)
  4463. "Cycle the global visibility. For details see `org-cycle'.
  4464. With C-u prefix arg, switch to startup visibility.
  4465. With a numeric prefix, show all headlines up to that level."
  4466. (interactive "P")
  4467. (let ((org-cycle-include-plain-lists
  4468. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4469. (cond
  4470. ((integerp arg)
  4471. (show-all)
  4472. (hide-sublevels arg)
  4473. (setq org-cycle-global-status 'contents))
  4474. ((equal arg '(4))
  4475. (org-set-startup-visibility)
  4476. (message "Startup visibility, plus VISIBILITY properties."))
  4477. (t
  4478. (org-cycle '(4))))))
  4479. (defun org-set-startup-visibility ()
  4480. "Set the visibility required by startup options and properties."
  4481. (cond
  4482. ((eq org-startup-folded t)
  4483. (org-cycle '(4)))
  4484. ((eq org-startup-folded 'content)
  4485. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4486. (org-cycle '(4)) (org-cycle '(4)))))
  4487. (if org-hide-block-startup (org-hide-block-all))
  4488. (org-set-visibility-according-to-property 'no-cleanup)
  4489. (org-cycle-hide-archived-subtrees 'all)
  4490. (org-cycle-hide-drawers 'all)
  4491. (org-cycle-show-empty-lines 'all))
  4492. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4493. "Switch subtree visibilities according to :VISIBILITY: property."
  4494. (interactive)
  4495. (let (org-show-entry-below state)
  4496. (save-excursion
  4497. (goto-char (point-min))
  4498. (while (re-search-forward
  4499. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4500. nil t)
  4501. (setq state (match-string 1))
  4502. (save-excursion
  4503. (org-back-to-heading t)
  4504. (hide-subtree)
  4505. (org-reveal)
  4506. (cond
  4507. ((equal state '("fold" "folded"))
  4508. (hide-subtree))
  4509. ((equal state "children")
  4510. (org-show-hidden-entry)
  4511. (show-children))
  4512. ((equal state "content")
  4513. (save-excursion
  4514. (save-restriction
  4515. (org-narrow-to-subtree)
  4516. (org-content))))
  4517. ((member state '("all" "showall"))
  4518. (show-subtree)))))
  4519. (unless no-cleanup
  4520. (org-cycle-hide-archived-subtrees 'all)
  4521. (org-cycle-hide-drawers 'all)
  4522. (org-cycle-show-empty-lines 'all)))))
  4523. (defun org-overview ()
  4524. "Switch to overview mode, showing only top-level headlines.
  4525. Really, this shows all headlines with level equal or greater than the level
  4526. of the first headline in the buffer. This is important, because if the
  4527. first headline is not level one, then (hide-sublevels 1) gives confusing
  4528. results."
  4529. (interactive)
  4530. (let ((level (save-excursion
  4531. (goto-char (point-min))
  4532. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4533. (progn
  4534. (goto-char (match-beginning 0))
  4535. (funcall outline-level))))))
  4536. (and level (hide-sublevels level))))
  4537. (defun org-content (&optional arg)
  4538. "Show all headlines in the buffer, like a table of contents.
  4539. With numerical argument N, show content up to level N."
  4540. (interactive "P")
  4541. (save-excursion
  4542. ;; Visit all headings and show their offspring
  4543. (and (integerp arg) (org-overview))
  4544. (goto-char (point-max))
  4545. (catch 'exit
  4546. (while (and (progn (condition-case nil
  4547. (outline-previous-visible-heading 1)
  4548. (error (goto-char (point-min))))
  4549. t)
  4550. (looking-at outline-regexp))
  4551. (if (integerp arg)
  4552. (show-children (1- arg))
  4553. (show-branches))
  4554. (if (bobp) (throw 'exit nil))))))
  4555. (defun org-optimize-window-after-visibility-change (state)
  4556. "Adjust the window after a change in outline visibility.
  4557. This function is the default value of the hook `org-cycle-hook'."
  4558. (when (get-buffer-window (current-buffer))
  4559. (cond
  4560. ((eq state 'content) nil)
  4561. ((eq state 'all) nil)
  4562. ((eq state 'folded) nil)
  4563. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4564. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4565. ;; FIXME: no longer in use
  4566. (defun org-compact-display-after-subtree-move ()
  4567. "Show a compacter version of the tree of the entry's parent."
  4568. (save-excursion
  4569. (if (org-up-heading-safe)
  4570. (progn
  4571. (hide-subtree)
  4572. (show-entry)
  4573. (show-children)
  4574. (org-cycle-show-empty-lines 'children)
  4575. (org-cycle-hide-drawers 'children))
  4576. (org-overview))))
  4577. (defun org-remove-empty-overlays-at (pos)
  4578. "Remove outline overlays that do not contain non-white stuff."
  4579. (mapc
  4580. (lambda (o)
  4581. (and (eq 'outline (org-overlay-get o 'invisible))
  4582. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4583. (org-overlay-end o))))
  4584. (org-delete-overlay o)))
  4585. (org-overlays-at pos)))
  4586. (defun org-clean-visibility-after-subtree-move ()
  4587. "Fix visibility issues after moving a subtree."
  4588. ;; First, find a reasonable region to look at:
  4589. ;; Start two siblings above, end three below
  4590. (let* ((beg (save-excursion
  4591. (and (outline-get-last-sibling)
  4592. (outline-get-last-sibling))
  4593. (point)))
  4594. (end (save-excursion
  4595. (and (outline-get-next-sibling)
  4596. (outline-get-next-sibling)
  4597. (outline-get-next-sibling))
  4598. (if (org-at-heading-p)
  4599. (point-at-eol)
  4600. (point))))
  4601. (level (looking-at "\\*+"))
  4602. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4603. (save-excursion
  4604. (save-restriction
  4605. (narrow-to-region beg end)
  4606. (when re
  4607. ;; Properly fold already folded siblings
  4608. (goto-char (point-min))
  4609. (while (re-search-forward re nil t)
  4610. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4611. (hide-entry))))
  4612. (org-cycle-show-empty-lines 'overview)
  4613. (org-cycle-hide-drawers 'overview)))))
  4614. (defun org-cycle-show-empty-lines (state)
  4615. "Show empty lines above all visible headlines.
  4616. The region to be covered depends on STATE when called through
  4617. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4618. entire buffer covered. Note that an empty line is only shown if there
  4619. are at least `org-cycle-separator-lines' empty lines before the headline."
  4620. (when (> org-cycle-separator-lines 0)
  4621. (save-excursion
  4622. (let* ((n org-cycle-separator-lines)
  4623. (re (cond
  4624. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4625. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4626. (t (let ((ns (number-to-string (- n 2))))
  4627. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4628. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4629. beg end)
  4630. (cond
  4631. ((memq state '(overview contents t))
  4632. (setq beg (point-min) end (point-max)))
  4633. ((memq state '(children folded))
  4634. (setq beg (point) end (progn (org-end-of-subtree t t)
  4635. (beginning-of-line 2)
  4636. (point)))))
  4637. (when beg
  4638. (goto-char beg)
  4639. (while (re-search-forward re end t)
  4640. (if (not (get-char-property (match-end 1) 'invisible))
  4641. (outline-flag-region
  4642. (match-beginning 1) (match-end 1) nil)))))))
  4643. ;; Never hide empty lines at the end of the file.
  4644. (save-excursion
  4645. (goto-char (point-max))
  4646. (outline-previous-heading)
  4647. (outline-end-of-heading)
  4648. (if (and (looking-at "[ \t\n]+")
  4649. (= (match-end 0) (point-max)))
  4650. (outline-flag-region (point) (match-end 0) nil))))
  4651. (defun org-show-empty-lines-in-parent ()
  4652. "Move to the parent and re-show empty lines before visible headlines."
  4653. (save-excursion
  4654. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4655. (org-cycle-show-empty-lines context))))
  4656. (defun org-cycle-hide-drawers (state)
  4657. "Re-hide all drawers after a visibility state change."
  4658. (when (and (org-mode-p)
  4659. (not (memq state '(overview folded contents))))
  4660. (save-excursion
  4661. (let* ((globalp (memq state '(contents all)))
  4662. (beg (if globalp (point-min) (point)))
  4663. (end (if globalp (point-max)
  4664. (if (eq state 'children)
  4665. (save-excursion (outline-next-heading) (point))
  4666. (org-end-of-subtree t)))))
  4667. (goto-char beg)
  4668. (while (re-search-forward org-drawer-regexp end t)
  4669. (org-flag-drawer t))))))
  4670. (defun org-flag-drawer (flag)
  4671. (save-excursion
  4672. (beginning-of-line 1)
  4673. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4674. (let ((b (match-end 0))
  4675. (outline-regexp org-outline-regexp))
  4676. (if (re-search-forward
  4677. "^[ \t]*:END:"
  4678. (save-excursion (outline-next-heading) (point)) t)
  4679. (outline-flag-region b (point-at-eol) flag)
  4680. (error ":END: line missing"))))))
  4681. (defun org-subtree-end-visible-p ()
  4682. "Is the end of the current subtree visible?"
  4683. (pos-visible-in-window-p
  4684. (save-excursion (org-end-of-subtree t) (point))))
  4685. (defun org-first-headline-recenter (&optional N)
  4686. "Move cursor to the first headline and recenter the headline.
  4687. Optional argument N means, put the headline into the Nth line of the window."
  4688. (goto-char (point-min))
  4689. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4690. (beginning-of-line)
  4691. (recenter (prefix-numeric-value N))))
  4692. ;;; Folding of blocks
  4693. (defconst org-block-regexp
  4694. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4695. "Regular expression for hiding blocks.")
  4696. (defvar org-hide-block-overlays nil
  4697. "Overays hiding blocks.")
  4698. (make-variable-buffer-local 'org-hide-block-overlays)
  4699. (defun org-block-map (function &optional start end)
  4700. "Call func at the head of all source blocks in the current
  4701. buffer. Optional arguments START and END can be used to limit
  4702. the range."
  4703. (let ((start (or start (point-min)))
  4704. (end (or end (point-max))))
  4705. (save-excursion
  4706. (goto-char start)
  4707. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4708. (save-excursion
  4709. (save-match-data
  4710. (goto-char (match-beginning 0))
  4711. (funcall function)))))))
  4712. (defun org-hide-block-toggle-all ()
  4713. "Toggle the visibility of all blocks in the current buffer."
  4714. (org-block-map #'org-hide-block-toggle))
  4715. (defun org-hide-block-all ()
  4716. "Fold all blocks in the current buffer."
  4717. (interactive)
  4718. (org-show-block-all)
  4719. (org-block-map #'org-hide-block-toggle-maybe))
  4720. (defun org-show-block-all ()
  4721. "Unfold all blocks in the current buffer."
  4722. (mapc 'org-delete-overlay org-hide-block-overlays)
  4723. (setq org-hide-block-overlays nil))
  4724. (defun org-hide-block-toggle-maybe ()
  4725. "Toggle visibility of block at point."
  4726. (interactive)
  4727. (let ((case-fold-search t))
  4728. (if (save-excursion
  4729. (beginning-of-line 1)
  4730. (looking-at org-block-regexp))
  4731. (progn (org-hide-block-toggle)
  4732. t) ;; to signal that we took action
  4733. nil))) ;; to signal that we did not
  4734. (defun org-hide-block-toggle (&optional force)
  4735. "Toggle the visibility of the current block."
  4736. (interactive)
  4737. (save-excursion
  4738. (beginning-of-line)
  4739. (if (re-search-forward org-block-regexp nil t)
  4740. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4741. (end (match-end 0))
  4742. ov) ;; end of entire body
  4743. (if (memq t (mapcar (lambda (overlay)
  4744. (eq (org-overlay-get overlay 'invisible)
  4745. 'org-hide-block))
  4746. (org-overlays-at start)))
  4747. (if (or (not force) (eq force 'off))
  4748. (mapc (lambda (ov)
  4749. (when (member ov org-hide-block-overlays)
  4750. (setq org-hide-block-overlays
  4751. (delq ov org-hide-block-overlays)))
  4752. (when (eq (org-overlay-get ov 'invisible)
  4753. 'org-hide-block)
  4754. (org-delete-overlay ov)))
  4755. (org-overlays-at start)))
  4756. (setq ov (org-make-overlay start end))
  4757. (org-overlay-put ov 'invisible 'org-hide-block)
  4758. (push ov org-hide-block-overlays)))
  4759. (error "Not looking at a source block"))))
  4760. ;; org-tab-after-check-for-cycling-hook
  4761. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4762. ;; Remove overlays when changing major mode
  4763. (add-hook 'org-mode-hook
  4764. (lambda () (org-add-hook 'change-major-mode-hook
  4765. 'org-show-block-all 'append 'local)))
  4766. ;;; Org-goto
  4767. (defvar org-goto-window-configuration nil)
  4768. (defvar org-goto-marker nil)
  4769. (defvar org-goto-map
  4770. (let ((map (make-sparse-keymap)))
  4771. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4772. (while (setq cmd (pop cmds))
  4773. (substitute-key-definition cmd cmd map global-map)))
  4774. (suppress-keymap map)
  4775. (org-defkey map "\C-m" 'org-goto-ret)
  4776. (org-defkey map [(return)] 'org-goto-ret)
  4777. (org-defkey map [(left)] 'org-goto-left)
  4778. (org-defkey map [(right)] 'org-goto-right)
  4779. (org-defkey map [(control ?g)] 'org-goto-quit)
  4780. (org-defkey map "\C-i" 'org-cycle)
  4781. (org-defkey map [(tab)] 'org-cycle)
  4782. (org-defkey map [(down)] 'outline-next-visible-heading)
  4783. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4784. (if org-goto-auto-isearch
  4785. (if (fboundp 'define-key-after)
  4786. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4787. nil)
  4788. (org-defkey map "q" 'org-goto-quit)
  4789. (org-defkey map "n" 'outline-next-visible-heading)
  4790. (org-defkey map "p" 'outline-previous-visible-heading)
  4791. (org-defkey map "f" 'outline-forward-same-level)
  4792. (org-defkey map "b" 'outline-backward-same-level)
  4793. (org-defkey map "u" 'outline-up-heading))
  4794. (org-defkey map "/" 'org-occur)
  4795. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4796. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4797. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4798. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4799. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4800. map))
  4801. (defconst org-goto-help
  4802. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4803. RET=jump to location [Q]uit and return to previous location
  4804. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4805. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4806. ;; FIXME: Docstring doe not mention both interfaces
  4807. (defun org-goto (&optional alternative-interface)
  4808. "Look up a different location in the current file, keeping current visibility.
  4809. When you want look-up or go to a different location in a document, the
  4810. fastest way is often to fold the entire buffer and then dive into the tree.
  4811. This method has the disadvantage, that the previous location will be folded,
  4812. which may not be what you want.
  4813. This command works around this by showing a copy of the current buffer
  4814. in an indirect buffer, in overview mode. You can dive into the tree in
  4815. that copy, use org-occur and incremental search to find a location.
  4816. When pressing RET or `Q', the command returns to the original buffer in
  4817. which the visibility is still unchanged. After RET is will also jump to
  4818. the location selected in the indirect buffer and expose the
  4819. the headline hierarchy above."
  4820. (interactive "P")
  4821. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4822. (org-refile-use-outline-path t)
  4823. (org-refile-target-verify-function nil)
  4824. (interface
  4825. (if (not alternative-interface)
  4826. org-goto-interface
  4827. (if (eq org-goto-interface 'outline)
  4828. 'outline-path-completion
  4829. 'outline)))
  4830. (org-goto-start-pos (point))
  4831. (selected-point
  4832. (if (eq interface 'outline)
  4833. (car (org-get-location (current-buffer) org-goto-help))
  4834. (nth 3 (org-refile-get-location "Goto: ")))))
  4835. (if selected-point
  4836. (progn
  4837. (org-mark-ring-push org-goto-start-pos)
  4838. (goto-char selected-point)
  4839. (if (or (org-invisible-p) (org-invisible-p2))
  4840. (org-show-context 'org-goto)))
  4841. (message "Quit"))))
  4842. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4843. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4844. (defvar org-goto-local-auto-isearch-map) ; defined below
  4845. (defun org-get-location (buf help)
  4846. "Let the user select a location in the Org-mode buffer BUF.
  4847. This function uses a recursive edit. It returns the selected position
  4848. or nil."
  4849. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4850. (isearch-hide-immediately nil)
  4851. (isearch-search-fun-function
  4852. (lambda () 'org-goto-local-search-headings))
  4853. (org-goto-selected-point org-goto-exit-command))
  4854. (save-excursion
  4855. (save-window-excursion
  4856. (delete-other-windows)
  4857. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4858. (switch-to-buffer
  4859. (condition-case nil
  4860. (make-indirect-buffer (current-buffer) "*org-goto*")
  4861. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4862. (with-output-to-temp-buffer "*Help*"
  4863. (princ help))
  4864. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4865. (setq buffer-read-only nil)
  4866. (let ((org-startup-truncated t)
  4867. (org-startup-folded nil)
  4868. (org-startup-align-all-tables nil))
  4869. (org-mode)
  4870. (org-overview))
  4871. (setq buffer-read-only t)
  4872. (if (and (boundp 'org-goto-start-pos)
  4873. (integer-or-marker-p org-goto-start-pos))
  4874. (let ((org-show-hierarchy-above t)
  4875. (org-show-siblings t)
  4876. (org-show-following-heading t))
  4877. (goto-char org-goto-start-pos)
  4878. (and (org-invisible-p) (org-show-context)))
  4879. (goto-char (point-min)))
  4880. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4881. (message "Select location and press RET")
  4882. (use-local-map org-goto-map)
  4883. (recursive-edit)
  4884. ))
  4885. (kill-buffer "*org-goto*")
  4886. (cons org-goto-selected-point org-goto-exit-command)))
  4887. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4888. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4889. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4890. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4891. (defun org-goto-local-search-headings (string bound noerror)
  4892. "Search and make sure that any matches are in headlines."
  4893. (catch 'return
  4894. (while (if isearch-forward
  4895. (search-forward string bound noerror)
  4896. (search-backward string bound noerror))
  4897. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4898. (and (member :headline context)
  4899. (not (member :tags context))))
  4900. (throw 'return (point))))))
  4901. (defun org-goto-local-auto-isearch ()
  4902. "Start isearch."
  4903. (interactive)
  4904. (goto-char (point-min))
  4905. (let ((keys (this-command-keys)))
  4906. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4907. (isearch-mode t)
  4908. (isearch-process-search-char (string-to-char keys)))))
  4909. (defun org-goto-ret (&optional arg)
  4910. "Finish `org-goto' by going to the new location."
  4911. (interactive "P")
  4912. (setq org-goto-selected-point (point)
  4913. org-goto-exit-command 'return)
  4914. (throw 'exit nil))
  4915. (defun org-goto-left ()
  4916. "Finish `org-goto' by going to the new location."
  4917. (interactive)
  4918. (if (org-on-heading-p)
  4919. (progn
  4920. (beginning-of-line 1)
  4921. (setq org-goto-selected-point (point)
  4922. org-goto-exit-command 'left)
  4923. (throw 'exit nil))
  4924. (error "Not on a heading")))
  4925. (defun org-goto-right ()
  4926. "Finish `org-goto' by going to the new location."
  4927. (interactive)
  4928. (if (org-on-heading-p)
  4929. (progn
  4930. (setq org-goto-selected-point (point)
  4931. org-goto-exit-command 'right)
  4932. (throw 'exit nil))
  4933. (error "Not on a heading")))
  4934. (defun org-goto-quit ()
  4935. "Finish `org-goto' without cursor motion."
  4936. (interactive)
  4937. (setq org-goto-selected-point nil)
  4938. (setq org-goto-exit-command 'quit)
  4939. (throw 'exit nil))
  4940. ;;; Indirect buffer display of subtrees
  4941. (defvar org-indirect-dedicated-frame nil
  4942. "This is the frame being used for indirect tree display.")
  4943. (defvar org-last-indirect-buffer nil)
  4944. (defun org-tree-to-indirect-buffer (&optional arg)
  4945. "Create indirect buffer and narrow it to current subtree.
  4946. With numerical prefix ARG, go up to this level and then take that tree.
  4947. If ARG is negative, go up that many levels.
  4948. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4949. indirect buffer previously made with this command, to avoid proliferation of
  4950. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4951. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4952. is kept so that you can work with several indirect buffers at the same time.
  4953. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4954. requests that a new frame be made for the new buffer, so that the dedicated
  4955. frame is not changed."
  4956. (interactive "P")
  4957. (let ((cbuf (current-buffer))
  4958. (cwin (selected-window))
  4959. (pos (point))
  4960. beg end level heading ibuf)
  4961. (save-excursion
  4962. (org-back-to-heading t)
  4963. (when (numberp arg)
  4964. (setq level (org-outline-level))
  4965. (if (< arg 0) (setq arg (+ level arg)))
  4966. (while (> (setq level (org-outline-level)) arg)
  4967. (outline-up-heading 1 t)))
  4968. (setq beg (point)
  4969. heading (org-get-heading))
  4970. (org-end-of-subtree t) (setq end (point)))
  4971. (if (and (buffer-live-p org-last-indirect-buffer)
  4972. (not (eq org-indirect-buffer-display 'new-frame))
  4973. (not arg))
  4974. (kill-buffer org-last-indirect-buffer))
  4975. (setq ibuf (org-get-indirect-buffer cbuf)
  4976. org-last-indirect-buffer ibuf)
  4977. (cond
  4978. ((or (eq org-indirect-buffer-display 'new-frame)
  4979. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4980. (select-frame (make-frame))
  4981. (delete-other-windows)
  4982. (switch-to-buffer ibuf)
  4983. (org-set-frame-title heading))
  4984. ((eq org-indirect-buffer-display 'dedicated-frame)
  4985. (raise-frame
  4986. (select-frame (or (and org-indirect-dedicated-frame
  4987. (frame-live-p org-indirect-dedicated-frame)
  4988. org-indirect-dedicated-frame)
  4989. (setq org-indirect-dedicated-frame (make-frame)))))
  4990. (delete-other-windows)
  4991. (switch-to-buffer ibuf)
  4992. (org-set-frame-title (concat "Indirect: " heading)))
  4993. ((eq org-indirect-buffer-display 'current-window)
  4994. (switch-to-buffer ibuf))
  4995. ((eq org-indirect-buffer-display 'other-window)
  4996. (pop-to-buffer ibuf))
  4997. (t (error "Invalid value.")))
  4998. (if (featurep 'xemacs)
  4999. (save-excursion (org-mode) (turn-on-font-lock)))
  5000. (narrow-to-region beg end)
  5001. (show-all)
  5002. (goto-char pos)
  5003. (and (window-live-p cwin) (select-window cwin))))
  5004. (defun org-get-indirect-buffer (&optional buffer)
  5005. (setq buffer (or buffer (current-buffer)))
  5006. (let ((n 1) (base (buffer-name buffer)) bname)
  5007. (while (buffer-live-p
  5008. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5009. (setq n (1+ n)))
  5010. (condition-case nil
  5011. (make-indirect-buffer buffer bname 'clone)
  5012. (error (make-indirect-buffer buffer bname)))))
  5013. (defun org-set-frame-title (title)
  5014. "Set the title of the current frame to the string TITLE."
  5015. ;; FIXME: how to name a single frame in XEmacs???
  5016. (unless (featurep 'xemacs)
  5017. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5018. ;;;; Structure editing
  5019. ;;; Inserting headlines
  5020. (defun org-previous-line-empty-p ()
  5021. (save-excursion
  5022. (and (not (bobp))
  5023. (or (beginning-of-line 0) t)
  5024. (save-match-data
  5025. (looking-at "[ \t]*$")))))
  5026. (defun org-insert-heading (&optional force-heading)
  5027. "Insert a new heading or item with same depth at point.
  5028. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5029. If point is at the beginning of a headline, insert a sibling before the
  5030. current headline. If point is not at the beginning, do not split the line,
  5031. but create the new headline after the current line."
  5032. (interactive "P")
  5033. (if (= (buffer-size) 0)
  5034. (insert "\n* ")
  5035. (when (or force-heading (not (org-insert-item)))
  5036. (let* ((empty-line-p nil)
  5037. (head (save-excursion
  5038. (condition-case nil
  5039. (progn
  5040. (org-back-to-heading)
  5041. (setq empty-line-p (org-previous-line-empty-p))
  5042. (match-string 0))
  5043. (error "*"))))
  5044. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5045. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5046. pos hide-previous previous-pos)
  5047. (cond
  5048. ((and (org-on-heading-p) (bolp)
  5049. (or (bobp)
  5050. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5051. ;; insert before the current line
  5052. (open-line (if blank 2 1)))
  5053. ((and (bolp)
  5054. (or (bobp)
  5055. (save-excursion
  5056. (backward-char 1) (not (org-invisible-p)))))
  5057. ;; insert right here
  5058. nil)
  5059. (t
  5060. ;; somewhere in the line
  5061. (save-excursion
  5062. (setq previous-pos (point-at-bol))
  5063. (end-of-line)
  5064. (setq hide-previous (org-invisible-p)))
  5065. (and org-insert-heading-respect-content (org-show-subtree))
  5066. (let ((split
  5067. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5068. (save-excursion
  5069. (let ((p (point)))
  5070. (goto-char (point-at-bol))
  5071. (and (looking-at org-complex-heading-regexp)
  5072. (> p (match-beginning 4)))))))
  5073. tags pos)
  5074. (cond
  5075. (org-insert-heading-respect-content
  5076. (org-end-of-subtree nil t)
  5077. (or (bolp) (newline))
  5078. (or (org-previous-line-empty-p)
  5079. (and blank (newline)))
  5080. (open-line 1))
  5081. ((org-on-heading-p)
  5082. (when hide-previous
  5083. (show-children)
  5084. (org-show-entry))
  5085. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5086. (setq tags (and (match-end 2) (match-string 2)))
  5087. (and (match-end 1)
  5088. (delete-region (match-beginning 1) (match-end 1)))
  5089. (setq pos (point-at-bol))
  5090. (or split (end-of-line 1))
  5091. (delete-horizontal-space)
  5092. (newline (if blank 2 1))
  5093. (when tags
  5094. (save-excursion
  5095. (goto-char pos)
  5096. (end-of-line 1)
  5097. (insert " " tags)
  5098. (org-set-tags nil 'align))))
  5099. (t
  5100. (or split (end-of-line 1))
  5101. (newline (if blank 2 1)))))))
  5102. (insert head) (just-one-space)
  5103. (setq pos (point))
  5104. (end-of-line 1)
  5105. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5106. (when (and org-insert-heading-respect-content hide-previous)
  5107. (save-excursion
  5108. (goto-char previous-pos)
  5109. (hide-subtree)))
  5110. (run-hooks 'org-insert-heading-hook)))))
  5111. (defun org-get-heading (&optional no-tags)
  5112. "Return the heading of the current entry, without the stars."
  5113. (save-excursion
  5114. (org-back-to-heading t)
  5115. (if (looking-at
  5116. (if no-tags
  5117. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5118. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5119. (match-string 1) "")))
  5120. (defun org-heading-components ()
  5121. "Return the components of the current heading.
  5122. This is a list with the following elements:
  5123. - the level as an integer
  5124. - the reduced level, different if `org-odd-levels-only' is set.
  5125. - the TODO keyword, or nil
  5126. - the priority character, like ?A, or nil if no priority is given
  5127. - the headline text itself, or the tags string if no headline text
  5128. - the tags string, or nil."
  5129. (save-excursion
  5130. (org-back-to-heading t)
  5131. (if (looking-at org-complex-heading-regexp)
  5132. (list (length (match-string 1))
  5133. (org-reduced-level (length (match-string 1)))
  5134. (org-match-string-no-properties 2)
  5135. (and (match-end 3) (aref (match-string 3) 2))
  5136. (org-match-string-no-properties 4)
  5137. (org-match-string-no-properties 5)))))
  5138. (defun org-get-entry ()
  5139. "Get the entry text, after heading, entire subtree."
  5140. (save-excursion
  5141. (org-back-to-heading t)
  5142. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5143. (defun org-insert-heading-after-current ()
  5144. "Insert a new heading with same level as current, after current subtree."
  5145. (interactive)
  5146. (org-back-to-heading)
  5147. (org-insert-heading)
  5148. (org-move-subtree-down)
  5149. (end-of-line 1))
  5150. (defun org-insert-heading-respect-content ()
  5151. (interactive)
  5152. (let ((org-insert-heading-respect-content t))
  5153. (org-insert-heading t)))
  5154. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5155. (interactive "P")
  5156. (let ((org-insert-heading-respect-content t))
  5157. (org-insert-todo-heading force-state t)))
  5158. (defun org-insert-todo-heading (arg &optional force-heading)
  5159. "Insert a new heading with the same level and TODO state as current heading.
  5160. If the heading has no TODO state, or if the state is DONE, use the first
  5161. state (TODO by default). Also with prefix arg, force first state."
  5162. (interactive "P")
  5163. (when (or force-heading (not (org-insert-item 'checkbox)))
  5164. (org-insert-heading force-heading)
  5165. (save-excursion
  5166. (org-back-to-heading)
  5167. (outline-previous-heading)
  5168. (looking-at org-todo-line-regexp))
  5169. (let*
  5170. ((new-mark-x
  5171. (if (or arg
  5172. (not (match-beginning 2))
  5173. (member (match-string 2) org-done-keywords))
  5174. (car org-todo-keywords-1)
  5175. (match-string 2)))
  5176. (new-mark
  5177. (or
  5178. (run-hook-with-args-until-success
  5179. 'org-todo-get-default-hook new-mark-x nil)
  5180. new-mark-x)))
  5181. (beginning-of-line 1)
  5182. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5183. (if org-treat-insert-todo-heading-as-state-change
  5184. (org-todo new-mark)
  5185. (insert new-mark " "))))
  5186. (when org-provide-todo-statistics
  5187. (org-update-parent-todo-statistics))))
  5188. (defun org-insert-subheading (arg)
  5189. "Insert a new subheading and demote it.
  5190. Works for outline headings and for plain lists alike."
  5191. (interactive "P")
  5192. (org-insert-heading arg)
  5193. (cond
  5194. ((org-on-heading-p) (org-do-demote))
  5195. ((org-at-item-p) (org-indent-item 1))))
  5196. (defun org-insert-todo-subheading (arg)
  5197. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5198. Works for outline headings and for plain lists alike."
  5199. (interactive "P")
  5200. (org-insert-todo-heading arg)
  5201. (cond
  5202. ((org-on-heading-p) (org-do-demote))
  5203. ((org-at-item-p) (org-indent-item 1))))
  5204. ;;; Promotion and Demotion
  5205. (defvar org-after-demote-entry-hook nil
  5206. "Hook run after an entry has been demoted.
  5207. The cursor will be at the beginning of the entry.
  5208. When a subtree is being demoted, the hook will be called for each node.")
  5209. (defvar org-after-promote-entry-hook nil
  5210. "Hook run after an entry has been promoted.
  5211. The cursor will be at the beginning of the entry.
  5212. When a subtree is being promoted, the hook will be called for each node.")
  5213. (defun org-promote-subtree ()
  5214. "Promote the entire subtree.
  5215. See also `org-promote'."
  5216. (interactive)
  5217. (save-excursion
  5218. (org-map-tree 'org-promote))
  5219. (org-fix-position-after-promote))
  5220. (defun org-demote-subtree ()
  5221. "Demote the entire subtree. See `org-demote'.
  5222. See also `org-promote'."
  5223. (interactive)
  5224. (save-excursion
  5225. (org-map-tree 'org-demote))
  5226. (org-fix-position-after-promote))
  5227. (defun org-do-promote ()
  5228. "Promote the current heading higher up the tree.
  5229. If the region is active in `transient-mark-mode', promote all headings
  5230. in the region."
  5231. (interactive)
  5232. (save-excursion
  5233. (if (org-region-active-p)
  5234. (org-map-region 'org-promote (region-beginning) (region-end))
  5235. (org-promote)))
  5236. (org-fix-position-after-promote))
  5237. (defun org-do-demote ()
  5238. "Demote the current heading lower down the tree.
  5239. If the region is active in `transient-mark-mode', demote all headings
  5240. in the region."
  5241. (interactive)
  5242. (save-excursion
  5243. (if (org-region-active-p)
  5244. (org-map-region 'org-demote (region-beginning) (region-end))
  5245. (org-demote)))
  5246. (org-fix-position-after-promote))
  5247. (defun org-fix-position-after-promote ()
  5248. "Make sure that after pro/demotion cursor position is right."
  5249. (let ((pos (point)))
  5250. (when (save-excursion
  5251. (beginning-of-line 1)
  5252. (looking-at org-todo-line-regexp)
  5253. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5254. (cond ((eobp) (insert " "))
  5255. ((eolp) (insert " "))
  5256. ((equal (char-after) ?\ ) (forward-char 1))))))
  5257. (defun org-reduced-level (l)
  5258. "Compute the effective level of a heading.
  5259. This takes into account the setting of `org-odd-levels-only'."
  5260. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5261. (defun org-get-valid-level (level &optional change)
  5262. "Rectify a level change under the influence of `org-odd-levels-only'
  5263. LEVEL is a current level, CHANGE is by how much the level should be
  5264. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5265. even level numbers will become the next higher odd number."
  5266. (if org-odd-levels-only
  5267. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5268. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5269. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5270. (max 1 (+ level (or change 0)))))
  5271. (if (boundp 'define-obsolete-function-alias)
  5272. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5273. (define-obsolete-function-alias 'org-get-legal-level
  5274. 'org-get-valid-level)
  5275. (define-obsolete-function-alias 'org-get-legal-level
  5276. 'org-get-valid-level "23.1")))
  5277. (defun org-promote ()
  5278. "Promote the current heading higher up the tree.
  5279. If the region is active in `transient-mark-mode', promote all headings
  5280. in the region."
  5281. (org-back-to-heading t)
  5282. (let* ((level (save-match-data (funcall outline-level)))
  5283. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5284. (diff (abs (- level (length up-head) -1))))
  5285. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5286. (replace-match up-head nil t)
  5287. ;; Fixup tag positioning
  5288. (and org-auto-align-tags (org-set-tags nil t))
  5289. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5290. (run-hooks 'org-after-promote-entry-hook)))
  5291. (defun org-demote ()
  5292. "Demote the current heading lower down the tree.
  5293. If the region is active in `transient-mark-mode', demote all headings
  5294. in the region."
  5295. (org-back-to-heading t)
  5296. (let* ((level (save-match-data (funcall outline-level)))
  5297. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5298. (diff (abs (- level (length down-head) -1))))
  5299. (replace-match down-head nil t)
  5300. ;; Fixup tag positioning
  5301. (and org-auto-align-tags (org-set-tags nil t))
  5302. (if org-adapt-indentation (org-fixup-indentation diff))
  5303. (run-hooks 'org-after-demote-entry-hook)))
  5304. (defun org-map-tree (fun)
  5305. "Call FUN for every heading underneath the current one."
  5306. (org-back-to-heading)
  5307. (let ((level (funcall outline-level)))
  5308. (save-excursion
  5309. (funcall fun)
  5310. (while (and (progn
  5311. (outline-next-heading)
  5312. (> (funcall outline-level) level))
  5313. (not (eobp)))
  5314. (funcall fun)))))
  5315. (defun org-map-region (fun beg end)
  5316. "Call FUN for every heading between BEG and END."
  5317. (let ((org-ignore-region t))
  5318. (save-excursion
  5319. (setq end (copy-marker end))
  5320. (goto-char beg)
  5321. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5322. (< (point) end))
  5323. (funcall fun))
  5324. (while (and (progn
  5325. (outline-next-heading)
  5326. (< (point) end))
  5327. (not (eobp)))
  5328. (funcall fun)))))
  5329. (defun org-fixup-indentation (diff)
  5330. "Change the indentation in the current entry by DIFF
  5331. However, if any line in the current entry has no indentation, or if it
  5332. would end up with no indentation after the change, nothing at all is done."
  5333. (save-excursion
  5334. (let ((end (save-excursion (outline-next-heading)
  5335. (point-marker)))
  5336. (prohibit (if (> diff 0)
  5337. "^\\S-"
  5338. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5339. col)
  5340. (unless (save-excursion (end-of-line 1)
  5341. (re-search-forward prohibit end t))
  5342. (while (and (< (point) end)
  5343. (re-search-forward "^[ \t]+" end t))
  5344. (goto-char (match-end 0))
  5345. (setq col (current-column))
  5346. (if (< diff 0) (replace-match ""))
  5347. (org-indent-to-column (+ diff col))))
  5348. (move-marker end nil))))
  5349. (defun org-convert-to-odd-levels ()
  5350. "Convert an org-mode file with all levels allowed to one with odd levels.
  5351. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5352. level 5 etc."
  5353. (interactive)
  5354. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5355. (let ((org-odd-levels-only nil) n)
  5356. (save-excursion
  5357. (goto-char (point-min))
  5358. (while (re-search-forward "^\\*\\*+ " nil t)
  5359. (setq n (- (length (match-string 0)) 2))
  5360. (while (>= (setq n (1- n)) 0)
  5361. (org-demote))
  5362. (end-of-line 1))))))
  5363. (defun org-convert-to-oddeven-levels ()
  5364. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5365. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5366. section with an even level, conversion would destroy the structure of the file. An error
  5367. is signaled in this case."
  5368. (interactive)
  5369. (goto-char (point-min))
  5370. ;; First check if there are no even levels
  5371. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5372. (org-show-context t)
  5373. (error "Not all levels are odd in this file. Conversion not possible."))
  5374. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5375. (let ((org-odd-levels-only nil) n)
  5376. (save-excursion
  5377. (goto-char (point-min))
  5378. (while (re-search-forward "^\\*\\*+ " nil t)
  5379. (setq n (/ (1- (length (match-string 0))) 2))
  5380. (while (>= (setq n (1- n)) 0)
  5381. (org-promote))
  5382. (end-of-line 1))))))
  5383. (defun org-tr-level (n)
  5384. "Make N odd if required."
  5385. (if org-odd-levels-only (1+ (/ n 2)) n))
  5386. ;;; Vertical tree motion, cutting and pasting of subtrees
  5387. (defun org-move-subtree-up (&optional arg)
  5388. "Move the current subtree up past ARG headlines of the same level."
  5389. (interactive "p")
  5390. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5391. (defun org-move-subtree-down (&optional arg)
  5392. "Move the current subtree down past ARG headlines of the same level."
  5393. (interactive "p")
  5394. (setq arg (prefix-numeric-value arg))
  5395. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5396. 'outline-get-last-sibling))
  5397. (ins-point (make-marker))
  5398. (cnt (abs arg))
  5399. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5400. ;; Select the tree
  5401. (org-back-to-heading)
  5402. (setq beg0 (point))
  5403. (save-excursion
  5404. (setq ne-beg (org-back-over-empty-lines))
  5405. (setq beg (point)))
  5406. (save-match-data
  5407. (save-excursion (outline-end-of-heading)
  5408. (setq folded (org-invisible-p)))
  5409. (outline-end-of-subtree))
  5410. (outline-next-heading)
  5411. (setq ne-end (org-back-over-empty-lines))
  5412. (setq end (point))
  5413. (goto-char beg0)
  5414. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5415. ;; include less whitespace
  5416. (save-excursion
  5417. (goto-char beg)
  5418. (forward-line (- ne-beg ne-end))
  5419. (setq beg (point))))
  5420. ;; Find insertion point, with error handling
  5421. (while (> cnt 0)
  5422. (or (and (funcall movfunc) (looking-at outline-regexp))
  5423. (progn (goto-char beg0)
  5424. (error "Cannot move past superior level or buffer limit")))
  5425. (setq cnt (1- cnt)))
  5426. (if (> arg 0)
  5427. ;; Moving forward - still need to move over subtree
  5428. (progn (org-end-of-subtree t t)
  5429. (save-excursion
  5430. (org-back-over-empty-lines)
  5431. (or (bolp) (newline)))))
  5432. (setq ne-ins (org-back-over-empty-lines))
  5433. (move-marker ins-point (point))
  5434. (setq txt (buffer-substring beg end))
  5435. (org-save-markers-in-region beg end)
  5436. (delete-region beg end)
  5437. (org-remove-empty-overlays-at beg)
  5438. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5439. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5440. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5441. (let ((bbb (point)))
  5442. (insert-before-markers txt)
  5443. (org-reinstall-markers-in-region bbb)
  5444. (move-marker ins-point bbb))
  5445. (or (bolp) (insert "\n"))
  5446. (setq ins-end (point))
  5447. (goto-char ins-point)
  5448. (org-skip-whitespace)
  5449. (when (and (< arg 0)
  5450. (org-first-sibling-p)
  5451. (> ne-ins ne-beg))
  5452. ;; Move whitespace back to beginning
  5453. (save-excursion
  5454. (goto-char ins-end)
  5455. (let ((kill-whole-line t))
  5456. (kill-line (- ne-ins ne-beg)) (point)))
  5457. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5458. (move-marker ins-point nil)
  5459. (if folded
  5460. (hide-entry)
  5461. (org-show-entry)
  5462. (show-children)
  5463. (org-cycle-hide-drawers 'children))
  5464. (org-clean-visibility-after-subtree-move)))
  5465. (defvar org-subtree-clip ""
  5466. "Clipboard for cut and paste of subtrees.
  5467. This is actually only a copy of the kill, because we use the normal kill
  5468. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5469. (defvar org-subtree-clip-folded nil
  5470. "Was the last copied subtree folded?
  5471. This is used to fold the tree back after pasting.")
  5472. (defun org-cut-subtree (&optional n)
  5473. "Cut the current subtree into the clipboard.
  5474. With prefix arg N, cut this many sequential subtrees.
  5475. This is a short-hand for marking the subtree and then cutting it."
  5476. (interactive "p")
  5477. (org-copy-subtree n 'cut))
  5478. (defun org-copy-subtree (&optional n cut force-store-markers)
  5479. "Cut the current subtree into the clipboard.
  5480. With prefix arg N, cut this many sequential subtrees.
  5481. This is a short-hand for marking the subtree and then copying it.
  5482. If CUT is non-nil, actually cut the subtree.
  5483. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5484. of some markers in the region, even if CUT is non-nil. This is
  5485. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5486. (interactive "p")
  5487. (let (beg end folded (beg0 (point)))
  5488. (if (interactive-p)
  5489. (org-back-to-heading nil) ; take what looks like a subtree
  5490. (org-back-to-heading t)) ; take what is really there
  5491. (org-back-over-empty-lines)
  5492. (setq beg (point))
  5493. (skip-chars-forward " \t\r\n")
  5494. (save-match-data
  5495. (save-excursion (outline-end-of-heading)
  5496. (setq folded (org-invisible-p)))
  5497. (condition-case nil
  5498. (outline-forward-same-level (1- n))
  5499. (error nil))
  5500. (org-end-of-subtree t t))
  5501. (org-back-over-empty-lines)
  5502. (setq end (point))
  5503. (goto-char beg0)
  5504. (when (> end beg)
  5505. (setq org-subtree-clip-folded folded)
  5506. (when (or cut force-store-markers)
  5507. (org-save-markers-in-region beg end))
  5508. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5509. (setq org-subtree-clip (current-kill 0))
  5510. (message "%s: Subtree(s) with %d characters"
  5511. (if cut "Cut" "Copied")
  5512. (length org-subtree-clip)))))
  5513. (defun org-paste-subtree (&optional level tree for-yank)
  5514. "Paste the clipboard as a subtree, with modification of headline level.
  5515. The entire subtree is promoted or demoted in order to match a new headline
  5516. level.
  5517. If the cursor is at the beginning of a headline, the same level as
  5518. that headline is used to paste the tree
  5519. If not, the new level is derived from the *visible* headings
  5520. before and after the insertion point, and taken to be the inferior headline
  5521. level of the two. So if the previous visible heading is level 3 and the
  5522. next is level 4 (or vice versa), level 4 will be used for insertion.
  5523. This makes sure that the subtree remains an independent subtree and does
  5524. not swallow low level entries.
  5525. You can also force a different level, either by using a numeric prefix
  5526. argument, or by inserting the heading marker by hand. For example, if the
  5527. cursor is after \"*****\", then the tree will be shifted to level 5.
  5528. If optional TREE is given, use this text instead of the kill ring.
  5529. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5530. move back over whitespace before inserting, and move point to the end of
  5531. the inserted text when done."
  5532. (interactive "P")
  5533. (unless (org-kill-is-subtree-p tree)
  5534. (error "%s"
  5535. (substitute-command-keys
  5536. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5537. (let* ((visp (not (org-invisible-p)))
  5538. (txt (or tree (and kill-ring (current-kill 0))))
  5539. (^re (concat "^\\(" outline-regexp "\\)"))
  5540. (re (concat "\\(" outline-regexp "\\)"))
  5541. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5542. (old-level (if (string-match ^re txt)
  5543. (- (match-end 0) (match-beginning 0) 1)
  5544. -1))
  5545. (force-level (cond (level (prefix-numeric-value level))
  5546. ((and (looking-at "[ \t]*$")
  5547. (string-match
  5548. ^re_ (buffer-substring
  5549. (point-at-bol) (point))))
  5550. (- (match-end 1) (match-beginning 1)))
  5551. ((and (bolp)
  5552. (looking-at org-outline-regexp))
  5553. (- (match-end 0) (point) 1))
  5554. (t nil)))
  5555. (previous-level (save-excursion
  5556. (condition-case nil
  5557. (progn
  5558. (outline-previous-visible-heading 1)
  5559. (if (looking-at re)
  5560. (- (match-end 0) (match-beginning 0) 1)
  5561. 1))
  5562. (error 1))))
  5563. (next-level (save-excursion
  5564. (condition-case nil
  5565. (progn
  5566. (or (looking-at outline-regexp)
  5567. (outline-next-visible-heading 1))
  5568. (if (looking-at re)
  5569. (- (match-end 0) (match-beginning 0) 1)
  5570. 1))
  5571. (error 1))))
  5572. (new-level (or force-level (max previous-level next-level)))
  5573. (shift (if (or (= old-level -1)
  5574. (= new-level -1)
  5575. (= old-level new-level))
  5576. 0
  5577. (- new-level old-level)))
  5578. (delta (if (> shift 0) -1 1))
  5579. (func (if (> shift 0) 'org-demote 'org-promote))
  5580. (org-odd-levels-only nil)
  5581. beg end newend)
  5582. ;; Remove the forced level indicator
  5583. (if force-level
  5584. (delete-region (point-at-bol) (point)))
  5585. ;; Paste
  5586. (beginning-of-line 1)
  5587. (unless for-yank (org-back-over-empty-lines))
  5588. (setq beg (point))
  5589. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5590. (insert-before-markers txt)
  5591. (unless (string-match "\n\\'" txt) (insert "\n"))
  5592. (setq newend (point))
  5593. (org-reinstall-markers-in-region beg)
  5594. (setq end (point))
  5595. (goto-char beg)
  5596. (skip-chars-forward " \t\n\r")
  5597. (setq beg (point))
  5598. (if (and (org-invisible-p) visp)
  5599. (save-excursion (outline-show-heading)))
  5600. ;; Shift if necessary
  5601. (unless (= shift 0)
  5602. (save-restriction
  5603. (narrow-to-region beg end)
  5604. (while (not (= shift 0))
  5605. (org-map-region func (point-min) (point-max))
  5606. (setq shift (+ delta shift)))
  5607. (goto-char (point-min))
  5608. (setq newend (point-max))))
  5609. (when (or (interactive-p) for-yank)
  5610. (message "Clipboard pasted as level %d subtree" new-level))
  5611. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5612. kill-ring
  5613. (eq org-subtree-clip (current-kill 0))
  5614. org-subtree-clip-folded)
  5615. ;; The tree was folded before it was killed/copied
  5616. (hide-subtree))
  5617. (and for-yank (goto-char newend))))
  5618. (defun org-kill-is-subtree-p (&optional txt)
  5619. "Check if the current kill is an outline subtree, or a set of trees.
  5620. Returns nil if kill does not start with a headline, or if the first
  5621. headline level is not the largest headline level in the tree.
  5622. So this will actually accept several entries of equal levels as well,
  5623. which is OK for `org-paste-subtree'.
  5624. If optional TXT is given, check this string instead of the current kill."
  5625. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5626. (start-level (and kill
  5627. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5628. org-outline-regexp "\\)")
  5629. kill)
  5630. (- (match-end 2) (match-beginning 2) 1)))
  5631. (re (concat "^" org-outline-regexp))
  5632. (start (1+ (or (match-beginning 2) -1))))
  5633. (if (not start-level)
  5634. (progn
  5635. nil) ;; does not even start with a heading
  5636. (catch 'exit
  5637. (while (setq start (string-match re kill (1+ start)))
  5638. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5639. (throw 'exit nil)))
  5640. t))))
  5641. (defvar org-markers-to-move nil
  5642. "Markers that should be moved with a cut-and-paste operation.
  5643. Those markers are stored together with their positions relative to
  5644. the start of the region.")
  5645. (defun org-save-markers-in-region (beg end)
  5646. "Check markers in region.
  5647. If these markers are between BEG and END, record their position relative
  5648. to BEG, so that after moving the block of text, we can put the markers back
  5649. into place.
  5650. This function gets called just before an entry or tree gets cut from the
  5651. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5652. called immediately, to move the markers with the entries."
  5653. (setq org-markers-to-move nil)
  5654. (when (featurep 'org-clock)
  5655. (org-clock-save-markers-for-cut-and-paste beg end))
  5656. (when (featurep 'org-agenda)
  5657. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5658. (defun org-check-and-save-marker (marker beg end)
  5659. "Check if MARKER is between BEG and END.
  5660. If yes, remember the marker and the distance to BEG."
  5661. (when (and (marker-buffer marker)
  5662. (equal (marker-buffer marker) (current-buffer)))
  5663. (if (and (>= marker beg) (< marker end))
  5664. (push (cons marker (- marker beg)) org-markers-to-move))))
  5665. (defun org-reinstall-markers-in-region (beg)
  5666. "Move all remembered markers to their position relative to BEG."
  5667. (mapc (lambda (x)
  5668. (move-marker (car x) (+ beg (cdr x))))
  5669. org-markers-to-move)
  5670. (setq org-markers-to-move nil))
  5671. (defun org-narrow-to-subtree ()
  5672. "Narrow buffer to the current subtree."
  5673. (interactive)
  5674. (save-excursion
  5675. (save-match-data
  5676. (narrow-to-region
  5677. (progn (org-back-to-heading t) (point))
  5678. (progn (org-end-of-subtree t) (point))))))
  5679. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5680. "Clone the task (subtree) at point N times.
  5681. The clones will be inserted as siblings.
  5682. In interactive use, the user will be prompted for the number of clones
  5683. to be produced, and for a time SHIFT, which may be a repeater as used
  5684. in time stamps, for example `+3d'.
  5685. When a valid repeater is given and the entry contains any time stamps,
  5686. the clones will become a sequence in time, with time stamps in the
  5687. subtree shifted for each clone produced. If SHIFT is nil or the
  5688. empty string, time stamps will be left alone.
  5689. If the original subtree did contain time stamps with a repeater,
  5690. the following will happen:
  5691. - the repeater will be removed in each clone
  5692. - an additional clone will be produced, with the current, unshifted
  5693. date(s) in the entry.
  5694. - the original entry will be placed *after* all the clones, with
  5695. repeater intact.
  5696. - the start days in the repeater in the original entry will be shifted
  5697. to past the last clone.
  5698. I this way you can spell out a number of instances of a repeating task,
  5699. and still retain the repeater to cover future instances of the task."
  5700. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5701. (let (beg end template task
  5702. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5703. (if (not (and (integerp n) (> n 0)))
  5704. (error "Invalid number of replications %s" n))
  5705. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5706. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5707. shift)))
  5708. (error "Invalid shift specification %s" shift))
  5709. (when doshift
  5710. (setq shift-n (string-to-number (match-string 1 shift))
  5711. shift-what (cdr (assoc (match-string 2 shift)
  5712. '(("d" . day) ("w" . week)
  5713. ("m" . month) ("y" . year))))))
  5714. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5715. (setq nmin 1 nmax n)
  5716. (org-back-to-heading t)
  5717. (setq beg (point))
  5718. (org-end-of-subtree t t)
  5719. (setq end (point))
  5720. (setq template (buffer-substring beg end))
  5721. (when (and doshift
  5722. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5723. (delete-region beg end)
  5724. (setq end beg)
  5725. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5726. (goto-char end)
  5727. (loop for n from nmin to nmax do
  5728. (if (not doshift)
  5729. (setq task template)
  5730. (with-temp-buffer
  5731. (insert template)
  5732. (org-mode)
  5733. (goto-char (point-min))
  5734. (while (re-search-forward org-ts-regexp-both nil t)
  5735. (org-timestamp-change (* n shift-n) shift-what))
  5736. (unless (= n n-no-remove)
  5737. (goto-char (point-min))
  5738. (while (re-search-forward org-ts-regexp nil t)
  5739. (save-excursion
  5740. (goto-char (match-beginning 0))
  5741. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5742. (delete-region (match-beginning 1) (match-end 1))))))
  5743. (setq task (buffer-string))))
  5744. (insert task))
  5745. (goto-char beg)))
  5746. ;;; Outline Sorting
  5747. (defun org-sort (with-case)
  5748. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5749. Optional argument WITH-CASE means sort case-sensitively.
  5750. With a double prefix argument, also remove duplicate entries."
  5751. (interactive "P")
  5752. (if (org-at-table-p)
  5753. (org-call-with-arg 'org-table-sort-lines with-case)
  5754. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5755. (defun org-sort-remove-invisible (s)
  5756. (remove-text-properties 0 (length s) org-rm-props s)
  5757. (while (string-match org-bracket-link-regexp s)
  5758. (setq s (replace-match (if (match-end 2)
  5759. (match-string 3 s)
  5760. (match-string 1 s)) t t s)))
  5761. s)
  5762. (defvar org-priority-regexp) ; defined later in the file
  5763. (defvar org-after-sorting-entries-or-items-hook nil
  5764. "Hook that is run after a bunch of entries or items have been sorted.
  5765. When children are sorted, the cursor is in the parent line when this
  5766. hook gets called. When a region or a plain list is sorted, the cursor
  5767. will be in the first entry of the sorted region/list.")
  5768. (defun org-sort-entries-or-items
  5769. (&optional with-case sorting-type getkey-func compare-func property)
  5770. "Sort entries on a certain level of an outline tree, or plain list items.
  5771. If there is an active region, the entries in the region are sorted.
  5772. Else, if the cursor is before the first entry, sort the top-level items.
  5773. Else, the children of the entry at point are sorted.
  5774. If the cursor is at the first item in a plain list, the list items will be
  5775. sorted.
  5776. Sorting can be alphabetically, numerically, by date/time as given by
  5777. a time stamp, by a property or by priority.
  5778. The command prompts for the sorting type unless it has been given to the
  5779. function through the SORTING-TYPE argument, which needs to a character,
  5780. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5781. precise meaning of each character:
  5782. n Numerically, by converting the beginning of the entry/item to a number.
  5783. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5784. t By date/time, either the first active time stamp in the entry, or, if
  5785. none exist, by the first inactive one.
  5786. In items, only the first line will be chekced.
  5787. s By the scheduled date/time.
  5788. d By deadline date/time.
  5789. c By creation time, which is assumed to be the first inactive time stamp
  5790. at the beginning of a line.
  5791. p By priority according to the cookie.
  5792. r By the value of a property.
  5793. Capital letters will reverse the sort order.
  5794. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5795. called with point at the beginning of the record. It must return either
  5796. a string or a number that should serve as the sorting key for that record.
  5797. Comparing entries ignores case by default. However, with an optional argument
  5798. WITH-CASE, the sorting considers case as well."
  5799. (interactive "P")
  5800. (let ((case-func (if with-case 'identity 'downcase))
  5801. start beg end stars re re2
  5802. txt what tmp plain-list-p)
  5803. ;; Find beginning and end of region to sort
  5804. (cond
  5805. ((org-region-active-p)
  5806. ;; we will sort the region
  5807. (setq end (region-end)
  5808. what "region")
  5809. (goto-char (region-beginning))
  5810. (if (not (org-on-heading-p)) (outline-next-heading))
  5811. (setq start (point)))
  5812. ((org-at-item-p)
  5813. ;; we will sort this plain list
  5814. (org-beginning-of-item-list) (setq start (point))
  5815. (org-end-of-item-list) (setq end (point))
  5816. (goto-char start)
  5817. (setq plain-list-p t
  5818. what "plain list"))
  5819. ((or (org-on-heading-p)
  5820. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5821. ;; we will sort the children of the current headline
  5822. (org-back-to-heading)
  5823. (setq start (point)
  5824. end (progn (org-end-of-subtree t t)
  5825. (org-back-over-empty-lines)
  5826. (point))
  5827. what "children")
  5828. (goto-char start)
  5829. (show-subtree)
  5830. (outline-next-heading))
  5831. (t
  5832. ;; we will sort the top-level entries in this file
  5833. (goto-char (point-min))
  5834. (or (org-on-heading-p) (outline-next-heading))
  5835. (setq start (point) end (point-max) what "top-level")
  5836. (goto-char start)
  5837. (show-all)))
  5838. (setq beg (point))
  5839. (if (>= beg end) (error "Nothing to sort"))
  5840. (unless plain-list-p
  5841. (looking-at "\\(\\*+\\)")
  5842. (setq stars (match-string 1)
  5843. re (concat "^" (regexp-quote stars) " +")
  5844. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5845. txt (buffer-substring beg end))
  5846. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5847. (if (and (not (equal stars "*")) (string-match re2 txt))
  5848. (error "Region to sort contains a level above the first entry")))
  5849. (unless sorting-type
  5850. (message
  5851. (if plain-list-p
  5852. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5853. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5854. [t]ime [s]cheduled [d]eadline [c]reated
  5855. A/N/T/S/D/C/P/O/F means reversed:")
  5856. what)
  5857. (setq sorting-type (read-char-exclusive))
  5858. (and (= (downcase sorting-type) ?f)
  5859. (setq getkey-func
  5860. (org-ido-completing-read "Sort using function: "
  5861. obarray 'fboundp t nil nil))
  5862. (setq getkey-func (intern getkey-func)))
  5863. (and (= (downcase sorting-type) ?r)
  5864. (setq property
  5865. (org-ido-completing-read "Property: "
  5866. (mapcar 'list (org-buffer-property-keys t))
  5867. nil t))))
  5868. (message "Sorting entries...")
  5869. (save-restriction
  5870. (narrow-to-region start end)
  5871. (let ((dcst (downcase sorting-type))
  5872. (case-fold-search nil)
  5873. (now (current-time)))
  5874. (sort-subr
  5875. (/= dcst sorting-type)
  5876. ;; This function moves to the beginning character of the "record" to
  5877. ;; be sorted.
  5878. (if plain-list-p
  5879. (lambda nil
  5880. (if (org-at-item-p) t (goto-char (point-max))))
  5881. (lambda nil
  5882. (if (re-search-forward re nil t)
  5883. (goto-char (match-beginning 0))
  5884. (goto-char (point-max)))))
  5885. ;; This function moves to the last character of the "record" being
  5886. ;; sorted.
  5887. (if plain-list-p
  5888. 'org-end-of-item
  5889. (lambda nil
  5890. (save-match-data
  5891. (condition-case nil
  5892. (outline-forward-same-level 1)
  5893. (error
  5894. (goto-char (point-max)))))))
  5895. ;; This function returns the value that gets sorted against.
  5896. (if plain-list-p
  5897. (lambda nil
  5898. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5899. (cond
  5900. ((= dcst ?n)
  5901. (string-to-number (buffer-substring (match-end 0)
  5902. (point-at-eol))))
  5903. ((= dcst ?a)
  5904. (buffer-substring (match-end 0) (point-at-eol)))
  5905. ((= dcst ?t)
  5906. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5907. (re-search-forward org-ts-regexp-both
  5908. (point-at-eol) t))
  5909. (org-time-string-to-seconds (match-string 0))
  5910. (time-to-seconds now)))
  5911. ((= dcst ?f)
  5912. (if getkey-func
  5913. (progn
  5914. (setq tmp (funcall getkey-func))
  5915. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5916. tmp)
  5917. (error "Invalid key function `%s'" getkey-func)))
  5918. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5919. (lambda nil
  5920. (cond
  5921. ((= dcst ?n)
  5922. (if (looking-at org-complex-heading-regexp)
  5923. (string-to-number (match-string 4))
  5924. nil))
  5925. ((= dcst ?a)
  5926. (if (looking-at org-complex-heading-regexp)
  5927. (funcall case-func (match-string 4))
  5928. nil))
  5929. ((= dcst ?t)
  5930. (let ((end (save-excursion (outline-next-heading) (point))))
  5931. (if (or (re-search-forward org-ts-regexp end t)
  5932. (re-search-forward org-ts-regexp-both end t))
  5933. (org-time-string-to-seconds (match-string 0))
  5934. (time-to-seconds now))))
  5935. ((= dcst ?c)
  5936. (let ((end (save-excursion (outline-next-heading) (point))))
  5937. (if (re-search-forward
  5938. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5939. end t)
  5940. (org-time-string-to-seconds (match-string 0))
  5941. (time-to-seconds now))))
  5942. ((= dcst ?s)
  5943. (let ((end (save-excursion (outline-next-heading) (point))))
  5944. (if (re-search-forward org-scheduled-time-regexp end t)
  5945. (org-time-string-to-seconds (match-string 1))
  5946. (time-to-seconds now))))
  5947. ((= dcst ?d)
  5948. (let ((end (save-excursion (outline-next-heading) (point))))
  5949. (if (re-search-forward org-deadline-time-regexp end t)
  5950. (org-time-string-to-seconds (match-string 1))
  5951. (time-to-seconds now))))
  5952. ((= dcst ?p)
  5953. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5954. (string-to-char (match-string 2))
  5955. org-default-priority))
  5956. ((= dcst ?r)
  5957. (or (org-entry-get nil property) ""))
  5958. ((= dcst ?o)
  5959. (if (looking-at org-complex-heading-regexp)
  5960. (- 9999 (length (member (match-string 2)
  5961. org-todo-keywords-1)))))
  5962. ((= dcst ?f)
  5963. (if getkey-func
  5964. (progn
  5965. (setq tmp (funcall getkey-func))
  5966. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5967. tmp)
  5968. (error "Invalid key function `%s'" getkey-func)))
  5969. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5970. nil
  5971. (cond
  5972. ((= dcst ?a) 'string<)
  5973. ((= dcst ?f) compare-func)
  5974. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5975. (t nil)))))
  5976. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5977. (message "Sorting entries...done")))
  5978. (defun org-do-sort (table what &optional with-case sorting-type)
  5979. "Sort TABLE of WHAT according to SORTING-TYPE.
  5980. The user will be prompted for the SORTING-TYPE if the call to this
  5981. function does not specify it. WHAT is only for the prompt, to indicate
  5982. what is being sorted. The sorting key will be extracted from
  5983. the car of the elements of the table.
  5984. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5985. (unless sorting-type
  5986. (message
  5987. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5988. what)
  5989. (setq sorting-type (read-char-exclusive)))
  5990. (let ((dcst (downcase sorting-type))
  5991. extractfun comparefun)
  5992. ;; Define the appropriate functions
  5993. (cond
  5994. ((= dcst ?n)
  5995. (setq extractfun 'string-to-number
  5996. comparefun (if (= dcst sorting-type) '< '>)))
  5997. ((= dcst ?a)
  5998. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5999. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6000. comparefun (if (= dcst sorting-type)
  6001. 'string<
  6002. (lambda (a b) (and (not (string< a b))
  6003. (not (string= a b)))))))
  6004. ((= dcst ?t)
  6005. (setq extractfun
  6006. (lambda (x)
  6007. (if (or (string-match org-ts-regexp x)
  6008. (string-match org-ts-regexp-both x))
  6009. (time-to-seconds
  6010. (org-time-string-to-time (match-string 0 x)))
  6011. 0))
  6012. comparefun (if (= dcst sorting-type) '< '>)))
  6013. (t (error "Invalid sorting type `%c'" sorting-type)))
  6014. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6015. table)
  6016. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6017. ;;; The orgstruct minor mode
  6018. ;; Define a minor mode which can be used in other modes in order to
  6019. ;; integrate the org-mode structure editing commands.
  6020. ;; This is really a hack, because the org-mode structure commands use
  6021. ;; keys which normally belong to the major mode. Here is how it
  6022. ;; works: The minor mode defines all the keys necessary to operate the
  6023. ;; structure commands, but wraps the commands into a function which
  6024. ;; tests if the cursor is currently at a headline or a plain list
  6025. ;; item. If that is the case, the structure command is used,
  6026. ;; temporarily setting many Org-mode variables like regular
  6027. ;; expressions for filling etc. However, when any of those keys is
  6028. ;; used at a different location, function uses `key-binding' to look
  6029. ;; up if the key has an associated command in another currently active
  6030. ;; keymap (minor modes, major mode, global), and executes that
  6031. ;; command. There might be problems if any of the keys is otherwise
  6032. ;; used as a prefix key.
  6033. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6034. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6035. ;; addresses this by checking explicitly for both bindings.
  6036. (defvar orgstruct-mode-map (make-sparse-keymap)
  6037. "Keymap for the minor `orgstruct-mode'.")
  6038. (defvar org-local-vars nil
  6039. "List of local variables, for use by `orgstruct-mode'")
  6040. ;;;###autoload
  6041. (define-minor-mode orgstruct-mode
  6042. "Toggle the minor more `orgstruct-mode'.
  6043. This mode is for using Org-mode structure commands in other modes.
  6044. The following key behave as if Org-mode was active, if the cursor
  6045. is on a headline, or on a plain list item (both in the definition
  6046. of Org-mode).
  6047. M-up Move entry/item up
  6048. M-down Move entry/item down
  6049. M-left Promote
  6050. M-right Demote
  6051. M-S-up Move entry/item up
  6052. M-S-down Move entry/item down
  6053. M-S-left Promote subtree
  6054. M-S-right Demote subtree
  6055. M-q Fill paragraph and items like in Org-mode
  6056. C-c ^ Sort entries
  6057. C-c - Cycle list bullet
  6058. TAB Cycle item visibility
  6059. M-RET Insert new heading/item
  6060. S-M-RET Insert new TODO heading / Checkbox item
  6061. C-c C-c Set tags / toggle checkbox"
  6062. nil " OrgStruct" nil
  6063. (org-load-modules-maybe)
  6064. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6065. ;;;###autoload
  6066. (defun turn-on-orgstruct ()
  6067. "Unconditionally turn on `orgstruct-mode'."
  6068. (orgstruct-mode 1))
  6069. (defun orgstruct++-mode (&optional arg)
  6070. "Toggle `orgstruct-mode', the enhanced version of it.
  6071. In addition to setting orgstruct-mode, this also exports all indentation
  6072. and autofilling variables from org-mode into the buffer. It will also
  6073. recognize item context in multiline items.
  6074. Note that turning off orgstruct-mode will *not* remove the
  6075. indentation/paragraph settings. This can only be done by refreshing the
  6076. major mode, for example with \\[normal-mode]."
  6077. (interactive "P")
  6078. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6079. (if (< arg 1)
  6080. (orgstruct-mode -1)
  6081. (orgstruct-mode 1)
  6082. (let (var val)
  6083. (mapc
  6084. (lambda (x)
  6085. (when (string-match
  6086. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6087. (symbol-name (car x)))
  6088. (setq var (car x) val (nth 1 x))
  6089. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6090. org-local-vars)
  6091. (org-set-local 'orgstruct-is-++ t))))
  6092. (defvar orgstruct-is-++ nil
  6093. "Is orgstruct-mode in ++ version in the current-buffer?")
  6094. (make-variable-buffer-local 'orgstruct-is-++)
  6095. ;;;###autoload
  6096. (defun turn-on-orgstruct++ ()
  6097. "Unconditionally turn on `orgstruct++-mode'."
  6098. (orgstruct++-mode 1))
  6099. (defun orgstruct-error ()
  6100. "Error when there is no default binding for a structure key."
  6101. (interactive)
  6102. (error "This key has no function outside structure elements"))
  6103. (defun orgstruct-setup ()
  6104. "Setup orgstruct keymaps."
  6105. (let ((nfunc 0)
  6106. (bindings
  6107. (list
  6108. '([(meta up)] org-metaup)
  6109. '([(meta down)] org-metadown)
  6110. '([(meta left)] org-metaleft)
  6111. '([(meta right)] org-metaright)
  6112. '([(meta shift up)] org-shiftmetaup)
  6113. '([(meta shift down)] org-shiftmetadown)
  6114. '([(meta shift left)] org-shiftmetaleft)
  6115. '([(meta shift right)] org-shiftmetaright)
  6116. '([?\e (up)] org-metaup)
  6117. '([?\e (down)] org-metadown)
  6118. '([?\e (left)] org-metaleft)
  6119. '([?\e (right)] org-metaright)
  6120. '([?\e (shift up)] org-shiftmetaup)
  6121. '([?\e (shift down)] org-shiftmetadown)
  6122. '([?\e (shift left)] org-shiftmetaleft)
  6123. '([?\e (shift right)] org-shiftmetaright)
  6124. '([(shift up)] org-shiftup)
  6125. '([(shift down)] org-shiftdown)
  6126. '([(shift left)] org-shiftleft)
  6127. '([(shift right)] org-shiftright)
  6128. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6129. '("\M-q" fill-paragraph)
  6130. '("\C-c^" org-sort)
  6131. '("\C-c-" org-cycle-list-bullet)))
  6132. elt key fun cmd)
  6133. (while (setq elt (pop bindings))
  6134. (setq nfunc (1+ nfunc))
  6135. (setq key (org-key (car elt))
  6136. fun (nth 1 elt)
  6137. cmd (orgstruct-make-binding fun nfunc key))
  6138. (org-defkey orgstruct-mode-map key cmd))
  6139. ;; Special treatment needed for TAB and RET
  6140. (org-defkey orgstruct-mode-map [(tab)]
  6141. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6142. (org-defkey orgstruct-mode-map "\C-i"
  6143. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6144. (org-defkey orgstruct-mode-map "\M-\C-m"
  6145. (orgstruct-make-binding 'org-insert-heading 105
  6146. "\M-\C-m" [(meta return)]))
  6147. (org-defkey orgstruct-mode-map [(meta return)]
  6148. (orgstruct-make-binding 'org-insert-heading 106
  6149. [(meta return)] "\M-\C-m"))
  6150. (org-defkey orgstruct-mode-map [(shift meta return)]
  6151. (orgstruct-make-binding 'org-insert-todo-heading 107
  6152. [(meta return)] "\M-\C-m"))
  6153. (org-defkey orgstruct-mode-map "\e\C-m"
  6154. (orgstruct-make-binding 'org-insert-heading 108
  6155. "\e\C-m" [?\e (return)]))
  6156. (org-defkey orgstruct-mode-map [?\e (return)]
  6157. (orgstruct-make-binding 'org-insert-heading 109
  6158. [?\e (return)] "\e\C-m"))
  6159. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6160. (orgstruct-make-binding 'org-insert-todo-heading 110
  6161. [?\e (return)] "\e\C-m"))
  6162. (unless org-local-vars
  6163. (setq org-local-vars (org-get-local-variables)))
  6164. t))
  6165. (defun orgstruct-make-binding (fun n &rest keys)
  6166. "Create a function for binding in the structure minor mode.
  6167. FUN is the command to call inside a table. N is used to create a unique
  6168. command name. KEYS are keys that should be checked in for a command
  6169. to execute outside of tables."
  6170. (eval
  6171. (list 'defun
  6172. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6173. '(arg)
  6174. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6175. "Outside of structure, run the binding of `"
  6176. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6177. "'.")
  6178. '(interactive "p")
  6179. (list 'if
  6180. `(org-context-p 'headline 'item
  6181. (and orgstruct-is-++
  6182. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6183. 'item-body))
  6184. (list 'org-run-like-in-org-mode (list 'quote fun))
  6185. (list 'let '(orgstruct-mode)
  6186. (list 'call-interactively
  6187. (append '(or)
  6188. (mapcar (lambda (k)
  6189. (list 'key-binding k))
  6190. keys)
  6191. '('orgstruct-error))))))))
  6192. (defun org-context-p (&rest contexts)
  6193. "Check if local context is any of CONTEXTS.
  6194. Possible values in the list of contexts are `table', `headline', and `item'."
  6195. (let ((pos (point)))
  6196. (goto-char (point-at-bol))
  6197. (prog1 (or (and (memq 'table contexts)
  6198. (looking-at "[ \t]*|"))
  6199. (and (memq 'headline contexts)
  6200. ;;????????? (looking-at "\\*+"))
  6201. (looking-at outline-regexp))
  6202. (and (memq 'item contexts)
  6203. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6204. (and (memq 'item-body contexts)
  6205. (org-in-item-p)))
  6206. (goto-char pos))))
  6207. (defun org-get-local-variables ()
  6208. "Return a list of all local variables in an org-mode buffer."
  6209. (let (varlist)
  6210. (with-current-buffer (get-buffer-create "*Org tmp*")
  6211. (erase-buffer)
  6212. (org-mode)
  6213. (setq varlist (buffer-local-variables)))
  6214. (kill-buffer "*Org tmp*")
  6215. (delq nil
  6216. (mapcar
  6217. (lambda (x)
  6218. (setq x
  6219. (if (symbolp x)
  6220. (list x)
  6221. (list (car x) (list 'quote (cdr x)))))
  6222. (if (string-match
  6223. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6224. (symbol-name (car x)))
  6225. x nil))
  6226. varlist))))
  6227. ;;;###autoload
  6228. (defun org-run-like-in-org-mode (cmd)
  6229. "Run a command, pretending that the current buffer is in Org-mode.
  6230. This will temporarily bind local variables that are typically bound in
  6231. Org-mode to the values they have in Org-mode, and then interactively
  6232. call CMD."
  6233. (org-load-modules-maybe)
  6234. (unless org-local-vars
  6235. (setq org-local-vars (org-get-local-variables)))
  6236. (eval (list 'let org-local-vars
  6237. (list 'call-interactively (list 'quote cmd)))))
  6238. ;;;; Archiving
  6239. (defun org-get-category (&optional pos)
  6240. "Get the category applying to position POS."
  6241. (get-text-property (or pos (point)) 'org-category))
  6242. (defun org-refresh-category-properties ()
  6243. "Refresh category text properties in the buffer."
  6244. (let ((def-cat (cond
  6245. ((null org-category)
  6246. (if buffer-file-name
  6247. (file-name-sans-extension
  6248. (file-name-nondirectory buffer-file-name))
  6249. "???"))
  6250. ((symbolp org-category) (symbol-name org-category))
  6251. (t org-category)))
  6252. beg end cat pos optionp)
  6253. (org-unmodified
  6254. (save-excursion
  6255. (save-restriction
  6256. (widen)
  6257. (goto-char (point-min))
  6258. (put-text-property (point) (point-max) 'org-category def-cat)
  6259. (while (re-search-forward
  6260. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6261. (setq pos (match-end 0)
  6262. optionp (equal (char-after (match-beginning 0)) ?#)
  6263. cat (org-trim (match-string 2)))
  6264. (if optionp
  6265. (setq beg (point-at-bol) end (point-max))
  6266. (org-back-to-heading t)
  6267. (setq beg (point) end (org-end-of-subtree t t)))
  6268. (put-text-property beg end 'org-category cat)
  6269. (goto-char pos)))))))
  6270. ;;;; Link Stuff
  6271. ;;; Link abbreviations
  6272. (defun org-link-expand-abbrev (link)
  6273. "Apply replacements as defined in `org-link-abbrev-alist."
  6274. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6275. (let* ((key (match-string 1 link))
  6276. (as (or (assoc key org-link-abbrev-alist-local)
  6277. (assoc key org-link-abbrev-alist)))
  6278. (tag (and (match-end 2) (match-string 3 link)))
  6279. rpl)
  6280. (if (not as)
  6281. link
  6282. (setq rpl (cdr as))
  6283. (cond
  6284. ((symbolp rpl) (funcall rpl tag))
  6285. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6286. ((string-match "%h" rpl)
  6287. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6288. (t (concat rpl tag)))))
  6289. link))
  6290. ;;; Storing and inserting links
  6291. (defvar org-insert-link-history nil
  6292. "Minibuffer history for links inserted with `org-insert-link'.")
  6293. (defvar org-stored-links nil
  6294. "Contains the links stored with `org-store-link'.")
  6295. (defvar org-store-link-plist nil
  6296. "Plist with info about the most recently link created with `org-store-link'.")
  6297. (defvar org-link-protocols nil
  6298. "Link protocols added to Org-mode using `org-add-link-type'.")
  6299. (defvar org-store-link-functions nil
  6300. "List of functions that are called to create and store a link.
  6301. Each function will be called in turn until one returns a non-nil
  6302. value. Each function should check if it is responsible for creating
  6303. this link (for example by looking at the major mode).
  6304. If not, it must exit and return nil.
  6305. If yes, it should return a non-nil value after a calling
  6306. `org-store-link-props' with a list of properties and values.
  6307. Special properties are:
  6308. :type The link prefix. like \"http\". This must be given.
  6309. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6310. This is obligatory as well.
  6311. :description Optional default description for the second pair
  6312. of brackets in an Org-mode link. The user can still change
  6313. this when inserting this link into an Org-mode buffer.
  6314. In addition to these, any additional properties can be specified
  6315. and then used in remember templates.")
  6316. (defun org-add-link-type (type &optional follow export)
  6317. "Add TYPE to the list of `org-link-types'.
  6318. Re-compute all regular expressions depending on `org-link-types'
  6319. FOLLOW and EXPORT are two functions.
  6320. FOLLOW should take the link path as the single argument and do whatever
  6321. is necessary to follow the link, for example find a file or display
  6322. a mail message.
  6323. EXPORT should format the link path for export to one of the export formats.
  6324. It should be a function accepting three arguments:
  6325. path the path of the link, the text after the prefix (like \"http:\")
  6326. desc the description of the link, if any, nil if there was no description
  6327. format the export format, a symbol like `html' or `latex'.
  6328. The function may use the FORMAT information to return different values
  6329. depending on the format. The return value will be put literally into
  6330. the exported file.
  6331. Org-mode has a built-in default for exporting links. If you are happy with
  6332. this default, there is no need to define an export function for the link
  6333. type. For a simple example of an export function, see `org-bbdb.el'."
  6334. (add-to-list 'org-link-types type t)
  6335. (org-make-link-regexps)
  6336. (if (assoc type org-link-protocols)
  6337. (setcdr (assoc type org-link-protocols) (list follow export))
  6338. (push (list type follow export) org-link-protocols)))
  6339. ;;;###autoload
  6340. (defun org-store-link (arg)
  6341. "\\<org-mode-map>Store an org-link to the current location.
  6342. This link is added to `org-stored-links' and can later be inserted
  6343. into an org-buffer with \\[org-insert-link].
  6344. For some link types, a prefix arg is interpreted:
  6345. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6346. For file links, arg negates `org-context-in-file-links'."
  6347. (interactive "P")
  6348. (org-load-modules-maybe)
  6349. (setq org-store-link-plist nil) ; reset
  6350. (let ((outline-regexp (org-get-limited-outline-regexp))
  6351. link cpltxt desc description search txt custom-id)
  6352. (cond
  6353. ((run-hook-with-args-until-success 'org-store-link-functions)
  6354. (setq link (plist-get org-store-link-plist :link)
  6355. desc (or (plist-get org-store-link-plist :description) link)))
  6356. ((equal (buffer-name) "*Org Edit Src Example*")
  6357. (let (label gc)
  6358. (while (or (not label)
  6359. (save-excursion
  6360. (save-restriction
  6361. (widen)
  6362. (goto-char (point-min))
  6363. (re-search-forward
  6364. (regexp-quote (format org-coderef-label-format label))
  6365. nil t))))
  6366. (when label (message "Label exists already") (sit-for 2))
  6367. (setq label (read-string "Code line label: " label)))
  6368. (end-of-line 1)
  6369. (setq link (format org-coderef-label-format label))
  6370. (setq gc (- 79 (length link)))
  6371. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6372. (insert link)
  6373. (setq link (concat "(" label ")") desc nil)))
  6374. ((eq major-mode 'calendar-mode)
  6375. (let ((cd (calendar-cursor-to-date)))
  6376. (setq link
  6377. (format-time-string
  6378. (car org-time-stamp-formats)
  6379. (apply 'encode-time
  6380. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6381. nil nil nil))))
  6382. (org-store-link-props :type "calendar" :date cd)))
  6383. ((eq major-mode 'w3-mode)
  6384. (setq cpltxt (if (and (buffer-name)
  6385. (not (string-match "Untitled" (buffer-name))))
  6386. (buffer-name)
  6387. (url-view-url t))
  6388. link (org-make-link (url-view-url t)))
  6389. (org-store-link-props :type "w3" :url (url-view-url t)))
  6390. ((eq major-mode 'w3m-mode)
  6391. (setq cpltxt (or w3m-current-title w3m-current-url)
  6392. link (org-make-link w3m-current-url))
  6393. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6394. ((setq search (run-hook-with-args-until-success
  6395. 'org-create-file-search-functions))
  6396. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6397. "::" search))
  6398. (setq cpltxt (or description link)))
  6399. ((eq major-mode 'image-mode)
  6400. (setq cpltxt (concat "file:"
  6401. (abbreviate-file-name buffer-file-name))
  6402. link (org-make-link cpltxt))
  6403. (org-store-link-props :type "image" :file buffer-file-name))
  6404. ((eq major-mode 'dired-mode)
  6405. ;; link to the file in the current line
  6406. (setq cpltxt (concat "file:"
  6407. (abbreviate-file-name
  6408. (expand-file-name
  6409. (dired-get-filename nil t))))
  6410. link (org-make-link cpltxt)))
  6411. ((and buffer-file-name (org-mode-p))
  6412. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6413. (cond
  6414. ((org-in-regexp "<<\\(.*?\\)>>")
  6415. (setq cpltxt
  6416. (concat "file:"
  6417. (abbreviate-file-name buffer-file-name)
  6418. "::" (match-string 1))
  6419. link (org-make-link cpltxt)))
  6420. ((and (featurep 'org-id)
  6421. (or (eq org-link-to-org-use-id t)
  6422. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6423. (interactive-p))
  6424. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6425. (interactive-p)
  6426. (not custom-id))
  6427. (and org-link-to-org-use-id
  6428. (condition-case nil
  6429. (org-entry-get nil "ID")
  6430. (error nil)))))
  6431. ;; We can make a link using the ID.
  6432. (setq link (condition-case nil
  6433. (prog1 (org-id-store-link)
  6434. (setq desc (plist-get org-store-link-plist
  6435. :description)))
  6436. (error
  6437. ;; probably before first headline, link to file only
  6438. (concat "file:"
  6439. (abbreviate-file-name buffer-file-name))))))
  6440. (t
  6441. ;; Just link to current headline
  6442. (setq cpltxt (concat "file:"
  6443. (abbreviate-file-name buffer-file-name)))
  6444. ;; Add a context search string
  6445. (when (org-xor org-context-in-file-links arg)
  6446. (setq txt (cond
  6447. ((org-on-heading-p) nil)
  6448. ((org-region-active-p)
  6449. (buffer-substring (region-beginning) (region-end)))
  6450. (t nil)))
  6451. (when (or (null txt) (string-match "\\S-" txt))
  6452. (setq cpltxt
  6453. (concat cpltxt "::"
  6454. (condition-case nil
  6455. (org-make-org-heading-search-string txt)
  6456. (error "")))
  6457. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6458. (if (string-match "::\\'" cpltxt)
  6459. (setq cpltxt (substring cpltxt 0 -2)))
  6460. (setq link (org-make-link cpltxt)))))
  6461. ((buffer-file-name (buffer-base-buffer))
  6462. ;; Just link to this file here.
  6463. (setq cpltxt (concat "file:"
  6464. (abbreviate-file-name
  6465. (buffer-file-name (buffer-base-buffer)))))
  6466. ;; Add a context string
  6467. (when (org-xor org-context-in-file-links arg)
  6468. (setq txt (if (org-region-active-p)
  6469. (buffer-substring (region-beginning) (region-end))
  6470. (buffer-substring (point-at-bol) (point-at-eol))))
  6471. ;; Only use search option if there is some text.
  6472. (when (string-match "\\S-" txt)
  6473. (setq cpltxt
  6474. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6475. desc "NONE")))
  6476. (setq link (org-make-link cpltxt)))
  6477. ((interactive-p)
  6478. (error "Cannot link to a buffer which is not visiting a file"))
  6479. (t (setq link nil)))
  6480. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6481. (setq link (or link cpltxt)
  6482. desc (or desc cpltxt))
  6483. (if (equal desc "NONE") (setq desc nil))
  6484. (if (and (or (interactive-p) executing-kbd-macro) link)
  6485. (progn
  6486. (setq org-stored-links
  6487. (cons (list link desc) org-stored-links))
  6488. (message "Stored: %s" (or desc link))
  6489. (when custom-id
  6490. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6491. "::#" custom-id))
  6492. (setq org-stored-links
  6493. (cons (list link desc) org-stored-links))))
  6494. (and link (org-make-link-string link desc)))))
  6495. (defun org-store-link-props (&rest plist)
  6496. "Store link properties, extract names and addresses."
  6497. (let (x adr)
  6498. (when (setq x (plist-get plist :from))
  6499. (setq adr (mail-extract-address-components x))
  6500. (setq plist (plist-put plist :fromname (car adr)))
  6501. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6502. (when (setq x (plist-get plist :to))
  6503. (setq adr (mail-extract-address-components x))
  6504. (setq plist (plist-put plist :toname (car adr)))
  6505. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6506. (let ((from (plist-get plist :from))
  6507. (to (plist-get plist :to)))
  6508. (when (and from to org-from-is-user-regexp)
  6509. (setq plist
  6510. (plist-put plist :fromto
  6511. (if (string-match org-from-is-user-regexp from)
  6512. (concat "to %t")
  6513. (concat "from %f"))))))
  6514. (setq org-store-link-plist plist))
  6515. (defun org-add-link-props (&rest plist)
  6516. "Add these properties to the link property list."
  6517. (let (key value)
  6518. (while plist
  6519. (setq key (pop plist) value (pop plist))
  6520. (setq org-store-link-plist
  6521. (plist-put org-store-link-plist key value)))))
  6522. (defun org-email-link-description (&optional fmt)
  6523. "Return the description part of an email link.
  6524. This takes information from `org-store-link-plist' and formats it
  6525. according to FMT (default from `org-email-link-description-format')."
  6526. (setq fmt (or fmt org-email-link-description-format))
  6527. (let* ((p org-store-link-plist)
  6528. (to (plist-get p :toaddress))
  6529. (from (plist-get p :fromaddress))
  6530. (table
  6531. (list
  6532. (cons "%c" (plist-get p :fromto))
  6533. (cons "%F" (plist-get p :from))
  6534. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6535. (cons "%T" (plist-get p :to))
  6536. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6537. (cons "%s" (plist-get p :subject))
  6538. (cons "%m" (plist-get p :message-id)))))
  6539. (when (string-match "%c" fmt)
  6540. ;; Check if the user wrote this message
  6541. (if (and org-from-is-user-regexp from to
  6542. (save-match-data (string-match org-from-is-user-regexp from)))
  6543. (setq fmt (replace-match "to %t" t t fmt))
  6544. (setq fmt (replace-match "from %f" t t fmt))))
  6545. (org-replace-escapes fmt table)))
  6546. (defun org-make-org-heading-search-string (&optional string heading)
  6547. "Make search string for STRING or current headline."
  6548. (interactive)
  6549. (let ((s (or string (org-get-heading))))
  6550. (unless (and string (not heading))
  6551. ;; We are using a headline, clean up garbage in there.
  6552. (if (string-match org-todo-regexp s)
  6553. (setq s (replace-match "" t t s)))
  6554. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6555. (setq s (replace-match "" t t s)))
  6556. (setq s (org-trim s))
  6557. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6558. org-comment-string "\\)") s)
  6559. (setq s (replace-match "" t t s)))
  6560. (while (string-match org-ts-regexp s)
  6561. (setq s (replace-match "" t t s))))
  6562. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6563. (setq s (replace-match " " t t s)))
  6564. (or string (setq s (concat "*" s))) ; Add * for headlines
  6565. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6566. (defun org-make-link (&rest strings)
  6567. "Concatenate STRINGS."
  6568. (apply 'concat strings))
  6569. (defun org-make-link-string (link &optional description)
  6570. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6571. (unless (string-match "\\S-" link)
  6572. (error "Empty link"))
  6573. (when (stringp description)
  6574. ;; Remove brackets from the description, they are fatal.
  6575. (while (string-match "\\[" description)
  6576. (setq description (replace-match "{" t t description)))
  6577. (while (string-match "\\]" description)
  6578. (setq description (replace-match "}" t t description))))
  6579. (when (equal (org-link-escape link) description)
  6580. ;; No description needed, it is identical
  6581. (setq description nil))
  6582. (when (and (not description)
  6583. (not (equal link (org-link-escape link))))
  6584. (setq description (org-extract-attributes link)))
  6585. (concat "[[" (org-link-escape link) "]"
  6586. (if description (concat "[" description "]") "")
  6587. "]"))
  6588. (defconst org-link-escape-chars
  6589. '((?\ . "%20")
  6590. (?\[ . "%5B")
  6591. (?\] . "%5D")
  6592. (?\340 . "%E0") ; `a
  6593. (?\342 . "%E2") ; ^a
  6594. (?\347 . "%E7") ; ,c
  6595. (?\350 . "%E8") ; `e
  6596. (?\351 . "%E9") ; 'e
  6597. (?\352 . "%EA") ; ^e
  6598. (?\356 . "%EE") ; ^i
  6599. (?\364 . "%F4") ; ^o
  6600. (?\371 . "%F9") ; `u
  6601. (?\373 . "%FB") ; ^u
  6602. (?\; . "%3B")
  6603. (?? . "%3F")
  6604. (?= . "%3D")
  6605. (?+ . "%2B")
  6606. )
  6607. "Association list of escapes for some characters problematic in links.
  6608. This is the list that is used for internal purposes.")
  6609. (defvar org-url-encoding-use-url-hexify nil)
  6610. (defconst org-link-escape-chars-browser
  6611. '((?\ . "%20")) ; 32 for the SPC char
  6612. "Association list of escapes for some characters problematic in links.
  6613. This is the list that is used before handing over to the browser.")
  6614. (defun org-link-escape (text &optional table)
  6615. "Escape characters in TEXT that are problematic for links."
  6616. (if org-url-encoding-use-url-hexify
  6617. (url-hexify-string text)
  6618. (setq table (or table org-link-escape-chars))
  6619. (when text
  6620. (let ((re (mapconcat (lambda (x) (regexp-quote
  6621. (char-to-string (car x))))
  6622. table "\\|")))
  6623. (while (string-match re text)
  6624. (setq text
  6625. (replace-match
  6626. (cdr (assoc (string-to-char (match-string 0 text))
  6627. table))
  6628. t t text)))
  6629. text))))
  6630. (defun org-link-unescape (text &optional table)
  6631. "Reverse the action of `org-link-escape'."
  6632. (if org-url-encoding-use-url-hexify
  6633. (url-unhex-string text)
  6634. (setq table (or table org-link-escape-chars))
  6635. (when text
  6636. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6637. table "\\|")))
  6638. (while (string-match re text)
  6639. (setq text
  6640. (replace-match
  6641. (char-to-string (car (rassoc (match-string 0 text) table)))
  6642. t t text)))
  6643. text))))
  6644. (defun org-xor (a b)
  6645. "Exclusive or."
  6646. (if a (not b) b))
  6647. (defun org-fixup-message-id-for-http (s)
  6648. "Replace special characters in a message id, so it can be used in an http query."
  6649. (while (string-match "<" s)
  6650. (setq s (replace-match "%3C" t t s)))
  6651. (while (string-match ">" s)
  6652. (setq s (replace-match "%3E" t t s)))
  6653. (while (string-match "@" s)
  6654. (setq s (replace-match "%40" t t s)))
  6655. s)
  6656. ;;;###autoload
  6657. (defun org-insert-link-global ()
  6658. "Insert a link like Org-mode does.
  6659. This command can be called in any mode to insert a link in Org-mode syntax."
  6660. (interactive)
  6661. (org-load-modules-maybe)
  6662. (org-run-like-in-org-mode 'org-insert-link))
  6663. (defun org-insert-link (&optional complete-file link-location)
  6664. "Insert a link. At the prompt, enter the link.
  6665. Completion can be used to insert any of the link protocol prefixes like
  6666. http or ftp in use.
  6667. The history can be used to select a link previously stored with
  6668. `org-store-link'. When the empty string is entered (i.e. if you just
  6669. press RET at the prompt), the link defaults to the most recently
  6670. stored link. As SPC triggers completion in the minibuffer, you need to
  6671. use M-SPC or C-q SPC to force the insertion of a space character.
  6672. You will also be prompted for a description, and if one is given, it will
  6673. be displayed in the buffer instead of the link.
  6674. If there is already a link at point, this command will allow you to edit link
  6675. and description parts.
  6676. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6677. be selected using completion. The path to the file will be relative to the
  6678. current directory if the file is in the current directory or a subdirectory.
  6679. Otherwise, the link will be the absolute path as completed in the minibuffer
  6680. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6681. option `org-link-file-path-type'.
  6682. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6683. the current directory or below.
  6684. With three \\[universal-argument] prefixes, negate the meaning of
  6685. `org-keep-stored-link-after-insertion'.
  6686. If `org-make-link-description-function' is non-nil, this function will be
  6687. called with the link target, and the result will be the default
  6688. link description.
  6689. If the LINK-LOCATION parameter is non-nil, this value will be
  6690. used as the link location instead of reading one interactively."
  6691. (interactive "P")
  6692. (let* ((wcf (current-window-configuration))
  6693. (region (if (org-region-active-p)
  6694. (buffer-substring (region-beginning) (region-end))))
  6695. (remove (and region (list (region-beginning) (region-end))))
  6696. (desc region)
  6697. tmphist ; byte-compile incorrectly complains about this
  6698. (link link-location)
  6699. entry file all-prefixes)
  6700. (cond
  6701. (link-location) ; specified by arg, just use it.
  6702. ((org-in-regexp org-bracket-link-regexp 1)
  6703. ;; We do have a link at point, and we are going to edit it.
  6704. (setq remove (list (match-beginning 0) (match-end 0)))
  6705. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6706. (setq link (read-string "Link: "
  6707. (org-link-unescape
  6708. (org-match-string-no-properties 1)))))
  6709. ((or (org-in-regexp org-angle-link-re)
  6710. (org-in-regexp org-plain-link-re))
  6711. ;; Convert to bracket link
  6712. (setq remove (list (match-beginning 0) (match-end 0))
  6713. link (read-string "Link: "
  6714. (org-remove-angle-brackets (match-string 0)))))
  6715. ((member complete-file '((4) (16)))
  6716. ;; Completing read for file names.
  6717. (setq link (org-file-complete-link complete-file)))
  6718. (t
  6719. ;; Read link, with completion for stored links.
  6720. (with-output-to-temp-buffer "*Org Links*"
  6721. (princ "Insert a link.
  6722. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6723. (when org-stored-links
  6724. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6725. (princ (mapconcat
  6726. (lambda (x)
  6727. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6728. (reverse org-stored-links) "\n"))))
  6729. (let ((cw (selected-window)))
  6730. (select-window (get-buffer-window "*Org Links*"))
  6731. (setq truncate-lines t)
  6732. (unless (pos-visible-in-window-p (point-max))
  6733. (org-fit-window-to-buffer))
  6734. (and (window-live-p cw) (select-window cw)))
  6735. ;; Fake a link history, containing the stored links.
  6736. (setq tmphist (append (mapcar 'car org-stored-links)
  6737. org-insert-link-history))
  6738. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6739. (mapcar 'car org-link-abbrev-alist)
  6740. org-link-types))
  6741. (unwind-protect
  6742. (progn
  6743. (setq link
  6744. (let ((org-completion-use-ido nil))
  6745. (org-completing-read
  6746. "Link: "
  6747. (append
  6748. (mapcar (lambda (x) (list (concat x ":")))
  6749. all-prefixes)
  6750. (mapcar 'car org-stored-links))
  6751. nil nil nil
  6752. 'tmphist
  6753. (car (car org-stored-links)))))
  6754. (if (or (member link all-prefixes)
  6755. (and (equal ":" (substring link -1))
  6756. (member (substring link 0 -1) all-prefixes)
  6757. (setq link (substring link 0 -1))))
  6758. (setq link (org-link-try-special-completion link))))
  6759. (set-window-configuration wcf)
  6760. (kill-buffer "*Org Links*"))
  6761. (setq entry (assoc link org-stored-links))
  6762. (or entry (push link org-insert-link-history))
  6763. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6764. (not org-keep-stored-link-after-insertion))
  6765. (setq org-stored-links (delq (assoc link org-stored-links)
  6766. org-stored-links)))
  6767. (setq desc (or desc (nth 1 entry)))))
  6768. (if (string-match org-plain-link-re link)
  6769. ;; URL-like link, normalize the use of angular brackets.
  6770. (setq link (org-make-link (org-remove-angle-brackets link))))
  6771. ;; Check if we are linking to the current file with a search option
  6772. ;; If yes, simplify the link by using only the search option.
  6773. (when (and buffer-file-name
  6774. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6775. (let* ((path (match-string 1 link))
  6776. (case-fold-search nil)
  6777. (search (match-string 2 link)))
  6778. (save-match-data
  6779. (if (equal (file-truename buffer-file-name) (file-truename path))
  6780. ;; We are linking to this same file, with a search option
  6781. (setq link search)))))
  6782. ;; Check if we can/should use a relative path. If yes, simplify the link
  6783. (when (string-match "^file:\\(.*\\)" link)
  6784. (let* ((path (match-string 1 link))
  6785. (origpath path)
  6786. (case-fold-search nil))
  6787. (cond
  6788. ((or (eq org-link-file-path-type 'absolute)
  6789. (equal complete-file '(16)))
  6790. (setq path (abbreviate-file-name (expand-file-name path))))
  6791. ((eq org-link-file-path-type 'noabbrev)
  6792. (setq path (expand-file-name path)))
  6793. ((eq org-link-file-path-type 'relative)
  6794. (setq path (file-relative-name path)))
  6795. (t
  6796. (save-match-data
  6797. (if (string-match (concat "^" (regexp-quote
  6798. (file-name-as-directory
  6799. (expand-file-name "."))))
  6800. (expand-file-name path))
  6801. ;; We are linking a file with relative path name.
  6802. (setq path (substring (expand-file-name path)
  6803. (match-end 0)))
  6804. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6805. (setq link (concat "file:" path))
  6806. (if (equal desc origpath)
  6807. (setq desc path))))
  6808. (if org-make-link-description-function
  6809. (setq desc (funcall org-make-link-description-function link desc)))
  6810. (setq desc (read-string "Description: " desc))
  6811. (unless (string-match "\\S-" desc) (setq desc nil))
  6812. (if remove (apply 'delete-region remove))
  6813. (insert (org-make-link-string link desc))))
  6814. (defun org-link-try-special-completion (type)
  6815. "If there is completion support for link type TYPE, offer it."
  6816. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6817. (if (functionp fun)
  6818. (funcall fun)
  6819. (read-string "Link (no completion support): " (concat type ":")))))
  6820. (defun org-file-complete-link (&optional arg)
  6821. "Create a file link using completion."
  6822. (let (file link)
  6823. (setq file (read-file-name "File: "))
  6824. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6825. (pwd1 (file-name-as-directory (abbreviate-file-name
  6826. (expand-file-name ".")))))
  6827. (cond
  6828. ((equal arg '(16))
  6829. (setq link (org-make-link
  6830. "file:"
  6831. (abbreviate-file-name (expand-file-name file)))))
  6832. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6833. (setq link (org-make-link "file:" (match-string 1 file))))
  6834. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6835. (expand-file-name file))
  6836. (setq link (org-make-link
  6837. "file:" (match-string 1 (expand-file-name file)))))
  6838. (t (setq link (org-make-link "file:" file)))))
  6839. link))
  6840. (defun org-completing-read (&rest args)
  6841. "Completing-read with SPACE being a normal character."
  6842. (let ((minibuffer-local-completion-map
  6843. (copy-keymap minibuffer-local-completion-map)))
  6844. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6845. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6846. (apply 'org-ido-completing-read args)))
  6847. (defun org-completing-read-no-ido (&rest args)
  6848. (let (org-completion-use-ido)
  6849. (apply 'org-completing-read args)))
  6850. (defun org-ido-completing-read (&rest args)
  6851. "Completing-read using `ido-mode' speedups if available"
  6852. (if (and org-completion-use-ido
  6853. (fboundp 'ido-completing-read)
  6854. (boundp 'ido-mode) ido-mode
  6855. (listp (second args)))
  6856. (let ((ido-enter-matching-directory nil))
  6857. (apply 'ido-completing-read (concat (car args))
  6858. (mapcar (lambda (x) (car x)) (nth 1 args))
  6859. (cddr args)))
  6860. (apply 'completing-read args)))
  6861. (defun org-extract-attributes (s)
  6862. "Extract the attributes cookie from a string and set as text property."
  6863. (let (a attr (start 0) key value)
  6864. (save-match-data
  6865. (when (string-match "{{\\([^}]+\\)}}$" s)
  6866. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6867. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6868. (setq key (match-string 1 a) value (match-string 2 a)
  6869. start (match-end 0)
  6870. attr (plist-put attr (intern key) value))))
  6871. (org-add-props s nil 'org-attr attr))
  6872. s))
  6873. (defun org-extract-attributes-from-string (tag)
  6874. (let (key value attr)
  6875. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6876. (setq key (match-string 1 tag) value (match-string 2 tag)
  6877. tag (replace-match "" t t tag)
  6878. attr (plist-put attr (intern key) value)))
  6879. (cons tag attr)))
  6880. (defun org-attributes-to-string (plist)
  6881. "Format a property list into an HTML attribute list."
  6882. (let ((s "") key value)
  6883. (while plist
  6884. (setq key (pop plist) value (pop plist))
  6885. (and value
  6886. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6887. s))
  6888. ;;; Opening/following a link
  6889. (defvar org-link-search-failed nil)
  6890. (defun org-next-link ()
  6891. "Move forward to the next link.
  6892. If the link is in hidden text, expose it."
  6893. (interactive)
  6894. (when (and org-link-search-failed (eq this-command last-command))
  6895. (goto-char (point-min))
  6896. (message "Link search wrapped back to beginning of buffer"))
  6897. (setq org-link-search-failed nil)
  6898. (let* ((pos (point))
  6899. (ct (org-context))
  6900. (a (assoc :link ct)))
  6901. (if a (goto-char (nth 2 a)))
  6902. (if (re-search-forward org-any-link-re nil t)
  6903. (progn
  6904. (goto-char (match-beginning 0))
  6905. (if (org-invisible-p) (org-show-context)))
  6906. (goto-char pos)
  6907. (setq org-link-search-failed t)
  6908. (error "No further link found"))))
  6909. (defun org-previous-link ()
  6910. "Move backward to the previous link.
  6911. If the link is in hidden text, expose it."
  6912. (interactive)
  6913. (when (and org-link-search-failed (eq this-command last-command))
  6914. (goto-char (point-max))
  6915. (message "Link search wrapped back to end of buffer"))
  6916. (setq org-link-search-failed nil)
  6917. (let* ((pos (point))
  6918. (ct (org-context))
  6919. (a (assoc :link ct)))
  6920. (if a (goto-char (nth 1 a)))
  6921. (if (re-search-backward org-any-link-re nil t)
  6922. (progn
  6923. (goto-char (match-beginning 0))
  6924. (if (org-invisible-p) (org-show-context)))
  6925. (goto-char pos)
  6926. (setq org-link-search-failed t)
  6927. (error "No further link found"))))
  6928. (defun org-translate-link (s)
  6929. "Translate a link string if a translation function has been defined."
  6930. (if (and org-link-translation-function
  6931. (fboundp org-link-translation-function)
  6932. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6933. (progn
  6934. (setq s (funcall org-link-translation-function
  6935. (match-string 1) (match-string 2)))
  6936. (concat (car s) ":" (cdr s)))
  6937. s))
  6938. (defun org-translate-link-from-planner (type path)
  6939. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6940. This is still an experimental function, your mileage may vary."
  6941. (cond
  6942. ((member type '("http" "https" "news" "ftp"))
  6943. ;; standard Internet links are the same.
  6944. nil)
  6945. ((and (equal type "irc") (string-match "^//" path))
  6946. ;; Planner has two / at the beginning of an irc link, we have 1.
  6947. ;; We should have zero, actually....
  6948. (setq path (substring path 1)))
  6949. ((and (equal type "lisp") (string-match "^/" path))
  6950. ;; Planner has a slash, we do not.
  6951. (setq type "elisp" path (substring path 1)))
  6952. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6953. ;; A typical message link. Planner has the id after the fina slash,
  6954. ;; we separate it with a hash mark
  6955. (setq path (concat (match-string 1 path) "#"
  6956. (org-remove-angle-brackets (match-string 2 path)))))
  6957. )
  6958. (cons type path))
  6959. (defun org-find-file-at-mouse (ev)
  6960. "Open file link or URL at mouse."
  6961. (interactive "e")
  6962. (mouse-set-point ev)
  6963. (org-open-at-point 'in-emacs))
  6964. (defun org-open-at-mouse (ev)
  6965. "Open file link or URL at mouse."
  6966. (interactive "e")
  6967. (mouse-set-point ev)
  6968. (if (eq major-mode 'org-agenda-mode)
  6969. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6970. (org-open-at-point))
  6971. (defvar org-window-config-before-follow-link nil
  6972. "The window configuration before following a link.
  6973. This is saved in case the need arises to restore it.")
  6974. (defvar org-open-link-marker (make-marker)
  6975. "Marker pointing to the location where `org-open-at-point; was called.")
  6976. ;;;###autoload
  6977. (defun org-open-at-point-global ()
  6978. "Follow a link like Org-mode does.
  6979. This command can be called in any mode to follow a link that has
  6980. Org-mode syntax."
  6981. (interactive)
  6982. (org-run-like-in-org-mode 'org-open-at-point))
  6983. ;;;###autoload
  6984. (defun org-open-link-from-string (s &optional arg)
  6985. "Open a link in the string S, as if it was in Org-mode."
  6986. (interactive "sLink: \nP")
  6987. (let ((reference-buffer (current-buffer)))
  6988. (with-temp-buffer
  6989. (let ((org-inhibit-startup t))
  6990. (org-mode)
  6991. (insert s)
  6992. (goto-char (point-min))
  6993. (org-open-at-point arg reference-buffer)))))
  6994. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6995. "Open link at or after point.
  6996. If there is no link at point, this function will search forward up to
  6997. the end of the current line.
  6998. Normally, files will be opened by an appropriate application. If the
  6999. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7000. With a double prefix argument, try to open outside of Emacs, in the
  7001. application the system uses for this file type."
  7002. (interactive "P")
  7003. (org-load-modules-maybe)
  7004. (move-marker org-open-link-marker (point))
  7005. (setq org-window-config-before-follow-link (current-window-configuration))
  7006. (org-remove-occur-highlights nil nil t)
  7007. (cond
  7008. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7009. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7010. (org-footnote-action))
  7011. (t
  7012. (let (type path link line search (pos (point)))
  7013. (catch 'match
  7014. (save-excursion
  7015. (skip-chars-forward "^]\n\r")
  7016. (when (org-in-regexp org-bracket-link-regexp)
  7017. (setq link (org-extract-attributes
  7018. (org-link-unescape (org-match-string-no-properties 1))))
  7019. (while (string-match " *\n *" link)
  7020. (setq link (replace-match " " t t link)))
  7021. (setq link (org-link-expand-abbrev link))
  7022. (cond
  7023. ((or (file-name-absolute-p link)
  7024. (string-match "^\\.\\.?/" link))
  7025. (setq type "file" path link))
  7026. ((string-match org-link-re-with-space3 link)
  7027. (setq type (match-string 1 link) path (match-string 2 link)))
  7028. (t (setq type "thisfile" path link)))
  7029. (throw 'match t)))
  7030. (when (get-text-property (point) 'org-linked-text)
  7031. (setq type "thisfile"
  7032. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7033. (1+ (point)) (point))
  7034. path (buffer-substring
  7035. (previous-single-property-change pos 'org-linked-text)
  7036. (next-single-property-change pos 'org-linked-text)))
  7037. (throw 'match t))
  7038. (save-excursion
  7039. (when (or (org-in-regexp org-angle-link-re)
  7040. (org-in-regexp org-plain-link-re))
  7041. (setq type (match-string 1) path (match-string 2))
  7042. (throw 'match t)))
  7043. (save-excursion
  7044. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7045. (setq type "tags"
  7046. path (match-string 1))
  7047. (while (string-match ":" path)
  7048. (setq path (replace-match "+" t t path)))
  7049. (throw 'match t)))
  7050. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7051. (setq type "tree-match"
  7052. path (match-string 1))
  7053. (throw 'match t)))
  7054. (unless path
  7055. (error "No link found"))
  7056. ;; switch back to reference buffer
  7057. ;; needed when if called in a temporary buffer through
  7058. ;; org-open-link-from-string
  7059. (and reference-buffer (switch-to-buffer reference-buffer))
  7060. ;; Remove any trailing spaces in path
  7061. (if (string-match " +\\'" path)
  7062. (setq path (replace-match "" t t path)))
  7063. (if (and org-link-translation-function
  7064. (fboundp org-link-translation-function))
  7065. ;; Check if we need to translate the link
  7066. (let ((tmp (funcall org-link-translation-function type path)))
  7067. (setq type (car tmp) path (cdr tmp))))
  7068. (cond
  7069. ((assoc type org-link-protocols)
  7070. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7071. ((equal type "mailto")
  7072. (let ((cmd (car org-link-mailto-program))
  7073. (args (cdr org-link-mailto-program)) args1
  7074. (address path) (subject "") a)
  7075. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7076. (setq address (match-string 1 path)
  7077. subject (org-link-escape (match-string 2 path))))
  7078. (while args
  7079. (cond
  7080. ((not (stringp (car args))) (push (pop args) args1))
  7081. (t (setq a (pop args))
  7082. (if (string-match "%a" a)
  7083. (setq a (replace-match address t t a)))
  7084. (if (string-match "%s" a)
  7085. (setq a (replace-match subject t t a)))
  7086. (push a args1))))
  7087. (apply cmd (nreverse args1))))
  7088. ((member type '("http" "https" "ftp" "news"))
  7089. (browse-url (concat type ":" (org-link-escape
  7090. path org-link-escape-chars-browser))))
  7091. ((member type '("message"))
  7092. (browse-url (concat type ":" path)))
  7093. ((string= type "tags")
  7094. (org-tags-view in-emacs path))
  7095. ((string= type "thisfile")
  7096. (if in-emacs
  7097. (switch-to-buffer-other-window
  7098. (org-get-buffer-for-internal-link (current-buffer)))
  7099. (org-mark-ring-push))
  7100. (let ((cmd `(org-link-search
  7101. ,path
  7102. ,(cond ((equal in-emacs '(4)) 'occur)
  7103. ((equal in-emacs '(16)) 'org-occur)
  7104. (t nil))
  7105. ,pos)))
  7106. (condition-case nil (eval cmd)
  7107. (error (progn (widen) (eval cmd))))))
  7108. ((string= type "tree-match")
  7109. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7110. ((string= type "file")
  7111. (if (string-match "::\\([0-9]+\\)\\'" path)
  7112. (setq line (string-to-number (match-string 1 path))
  7113. path (substring path 0 (match-beginning 0)))
  7114. (if (string-match "::\\(.+\\)\\'" path)
  7115. (setq search (match-string 1 path)
  7116. path (substring path 0 (match-beginning 0)))))
  7117. (if (string-match "[*?{]" (file-name-nondirectory path))
  7118. (dired path)
  7119. (org-open-file path in-emacs line search)))
  7120. ((string= type "news")
  7121. (require 'org-gnus)
  7122. (org-gnus-follow-link path))
  7123. ((string= type "shell")
  7124. (let ((cmd path))
  7125. (if (or (not org-confirm-shell-link-function)
  7126. (funcall org-confirm-shell-link-function
  7127. (format "Execute \"%s\" in shell? "
  7128. (org-add-props cmd nil
  7129. 'face 'org-warning))))
  7130. (progn
  7131. (message "Executing %s" cmd)
  7132. (shell-command cmd))
  7133. (error "Abort"))))
  7134. ((string= type "elisp")
  7135. (let ((cmd path))
  7136. (if (or (not org-confirm-elisp-link-function)
  7137. (funcall org-confirm-elisp-link-function
  7138. (format "Execute \"%s\" as elisp? "
  7139. (org-add-props cmd nil
  7140. 'face 'org-warning))))
  7141. (message "%s => %s" cmd
  7142. (if (equal (string-to-char cmd) ?\()
  7143. (eval (read cmd))
  7144. (call-interactively (read cmd))))
  7145. (error "Abort"))))
  7146. (t
  7147. (browse-url-at-point))))))
  7148. (move-marker org-open-link-marker nil)
  7149. (run-hook-with-args 'org-follow-link-hook))
  7150. ;;;; Time estimates
  7151. (defun org-get-effort (&optional pom)
  7152. "Get the effort estimate for the current entry."
  7153. (org-entry-get pom org-effort-property))
  7154. ;;; File search
  7155. (defvar org-create-file-search-functions nil
  7156. "List of functions to construct the right search string for a file link.
  7157. These functions are called in turn with point at the location to
  7158. which the link should point.
  7159. A function in the hook should first test if it would like to
  7160. handle this file type, for example by checking the major-mode or
  7161. the file extension. If it decides not to handle this file, it
  7162. should just return nil to give other functions a chance. If it
  7163. does handle the file, it must return the search string to be used
  7164. when following the link. The search string will be part of the
  7165. file link, given after a double colon, and `org-open-at-point'
  7166. will automatically search for it. If special measures must be
  7167. taken to make the search successful, another function should be
  7168. added to the companion hook `org-execute-file-search-functions',
  7169. which see.
  7170. A function in this hook may also use `setq' to set the variable
  7171. `description' to provide a suggestion for the descriptive text to
  7172. be used for this link when it gets inserted into an Org-mode
  7173. buffer with \\[org-insert-link].")
  7174. (defvar org-execute-file-search-functions nil
  7175. "List of functions to execute a file search triggered by a link.
  7176. Functions added to this hook must accept a single argument, the
  7177. search string that was part of the file link, the part after the
  7178. double colon. The function must first check if it would like to
  7179. handle this search, for example by checking the major-mode or the
  7180. file extension. If it decides not to handle this search, it
  7181. should just return nil to give other functions a chance. If it
  7182. does handle the search, it must return a non-nil value to keep
  7183. other functions from trying.
  7184. Each function can access the current prefix argument through the
  7185. variable `current-prefix-argument'. Note that a single prefix is
  7186. used to force opening a link in Emacs, so it may be good to only
  7187. use a numeric or double prefix to guide the search function.
  7188. In case this is needed, a function in this hook can also restore
  7189. the window configuration before `org-open-at-point' was called using:
  7190. (set-window-configuration org-window-config-before-follow-link)")
  7191. (defun org-link-search (s &optional type avoid-pos)
  7192. "Search for a link search option.
  7193. If S is surrounded by forward slashes, it is interpreted as a
  7194. regular expression. In org-mode files, this will create an `org-occur'
  7195. sparse tree. In ordinary files, `occur' will be used to list matches.
  7196. If the current buffer is in `dired-mode', grep will be used to search
  7197. in all files. If AVOID-POS is given, ignore matches near that position."
  7198. (let ((case-fold-search t)
  7199. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7200. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7201. (append '(("") (" ") ("\t") ("\n"))
  7202. org-emphasis-alist)
  7203. "\\|") "\\)"))
  7204. (pos (point))
  7205. (pre nil) (post nil)
  7206. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7207. (cond
  7208. ;; First check if there are any special
  7209. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7210. ;; Now try the builtin stuff
  7211. ((and (equal (string-to-char s0) ?#)
  7212. (> (length s0) 1)
  7213. (save-excursion
  7214. (goto-char (point-min))
  7215. (and
  7216. (re-search-forward
  7217. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7218. (setq type 'dedicated
  7219. pos (match-beginning 0))))
  7220. ;; There is an exact target for this
  7221. (goto-char pos)
  7222. (org-back-to-heading t)))
  7223. ((save-excursion
  7224. (goto-char (point-min))
  7225. (and
  7226. (re-search-forward
  7227. (concat "<<" (regexp-quote s0) ">>") nil t)
  7228. (setq type 'dedicated
  7229. pos (match-beginning 0))))
  7230. ;; There is an exact target for this
  7231. (goto-char pos))
  7232. ((and (string-match "^(\\(.*\\))$" s0)
  7233. (save-excursion
  7234. (goto-char (point-min))
  7235. (and
  7236. (re-search-forward
  7237. (concat "[^[]" (regexp-quote
  7238. (format org-coderef-label-format
  7239. (match-string 1 s0))))
  7240. nil t)
  7241. (setq type 'dedicated
  7242. pos (1+ (match-beginning 0))))))
  7243. ;; There is a coderef target for this
  7244. (goto-char pos))
  7245. ((string-match "^/\\(.*\\)/$" s)
  7246. ;; A regular expression
  7247. (cond
  7248. ((org-mode-p)
  7249. (org-occur (match-string 1 s)))
  7250. ;;((eq major-mode 'dired-mode)
  7251. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7252. (t (org-do-occur (match-string 1 s)))))
  7253. (t
  7254. ;; A normal search strings
  7255. (when (equal (string-to-char s) ?*)
  7256. ;; Anchor on headlines, post may include tags.
  7257. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7258. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7259. s (substring s 1)))
  7260. (remove-text-properties
  7261. 0 (length s)
  7262. '(face nil mouse-face nil keymap nil fontified nil) s)
  7263. ;; Make a series of regular expressions to find a match
  7264. (setq words (org-split-string s "[ \n\r\t]+")
  7265. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7266. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7267. "\\)" markers)
  7268. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7269. re2a (concat "[ \t\r\n]" re2a_)
  7270. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7271. re4 (concat "[^a-zA-Z_]" re4_)
  7272. re1 (concat pre re2 post)
  7273. re3 (concat pre (if pre re4_ re4) post)
  7274. re5 (concat pre ".*" re4)
  7275. re2 (concat pre re2)
  7276. re2a (concat pre (if pre re2a_ re2a))
  7277. re4 (concat pre (if pre re4_ re4))
  7278. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7279. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7280. re5 "\\)"
  7281. ))
  7282. (cond
  7283. ((eq type 'org-occur) (org-occur reall))
  7284. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7285. (t (goto-char (point-min))
  7286. (setq type 'fuzzy)
  7287. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7288. (org-search-not-self 1 re1 nil t)
  7289. (org-search-not-self 1 re2 nil t)
  7290. (org-search-not-self 1 re2a nil t)
  7291. (org-search-not-self 1 re3 nil t)
  7292. (org-search-not-self 1 re4 nil t)
  7293. (org-search-not-self 1 re5 nil t)
  7294. )
  7295. (goto-char (match-beginning 1))
  7296. (goto-char pos)
  7297. (error "No match")))))
  7298. (t
  7299. ;; Normal string-search
  7300. (goto-char (point-min))
  7301. (if (search-forward s nil t)
  7302. (goto-char (match-beginning 0))
  7303. (error "No match"))))
  7304. (and (org-mode-p) (org-show-context 'link-search))
  7305. type))
  7306. (defun org-search-not-self (group &rest args)
  7307. "Execute `re-search-forward', but only accept matches that do not
  7308. enclose the position of `org-open-link-marker'."
  7309. (let ((m org-open-link-marker))
  7310. (catch 'exit
  7311. (while (apply 're-search-forward args)
  7312. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7313. (goto-char (match-end group))
  7314. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7315. (> (match-beginning 0) (marker-position m))
  7316. (< (match-end 0) (marker-position m)))
  7317. (save-match-data
  7318. (or (not (org-in-regexp
  7319. org-bracket-link-analytic-regexp 1))
  7320. (not (match-end 4)) ; no description
  7321. (and (<= (match-beginning 4) (point))
  7322. (>= (match-end 4) (point))))))
  7323. (throw 'exit (point))))))))
  7324. (defun org-get-buffer-for-internal-link (buffer)
  7325. "Return a buffer to be used for displaying the link target of internal links."
  7326. (cond
  7327. ((not org-display-internal-link-with-indirect-buffer)
  7328. buffer)
  7329. ((string-match "(Clone)$" (buffer-name buffer))
  7330. (message "Buffer is already a clone, not making another one")
  7331. ;; we also do not modify visibility in this case
  7332. buffer)
  7333. (t ; make a new indirect buffer for displaying the link
  7334. (let* ((bn (buffer-name buffer))
  7335. (ibn (concat bn "(Clone)"))
  7336. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7337. (with-current-buffer ib (org-overview))
  7338. ib))))
  7339. (defun org-do-occur (regexp &optional cleanup)
  7340. "Call the Emacs command `occur'.
  7341. If CLEANUP is non-nil, remove the printout of the regular expression
  7342. in the *Occur* buffer. This is useful if the regex is long and not useful
  7343. to read."
  7344. (occur regexp)
  7345. (when cleanup
  7346. (let ((cwin (selected-window)) win beg end)
  7347. (when (setq win (get-buffer-window "*Occur*"))
  7348. (select-window win))
  7349. (goto-char (point-min))
  7350. (when (re-search-forward "match[a-z]+" nil t)
  7351. (setq beg (match-end 0))
  7352. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7353. (setq end (1- (match-beginning 0)))))
  7354. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7355. (goto-char (point-min))
  7356. (select-window cwin))))
  7357. ;;; The mark ring for links jumps
  7358. (defvar org-mark-ring nil
  7359. "Mark ring for positions before jumps in Org-mode.")
  7360. (defvar org-mark-ring-last-goto nil
  7361. "Last position in the mark ring used to go back.")
  7362. ;; Fill and close the ring
  7363. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7364. (loop for i from 1 to org-mark-ring-length do
  7365. (push (make-marker) org-mark-ring))
  7366. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7367. org-mark-ring)
  7368. (defun org-mark-ring-push (&optional pos buffer)
  7369. "Put the current position or POS into the mark ring and rotate it."
  7370. (interactive)
  7371. (setq pos (or pos (point)))
  7372. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7373. (move-marker (car org-mark-ring)
  7374. (or pos (point))
  7375. (or buffer (current-buffer)))
  7376. (message "%s"
  7377. (substitute-command-keys
  7378. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7379. (defun org-mark-ring-goto (&optional n)
  7380. "Jump to the previous position in the mark ring.
  7381. With prefix arg N, jump back that many stored positions. When
  7382. called several times in succession, walk through the entire ring.
  7383. Org-mode commands jumping to a different position in the current file,
  7384. or to another Org-mode file, automatically push the old position
  7385. onto the ring."
  7386. (interactive "p")
  7387. (let (p m)
  7388. (if (eq last-command this-command)
  7389. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7390. (setq p org-mark-ring))
  7391. (setq org-mark-ring-last-goto p)
  7392. (setq m (car p))
  7393. (switch-to-buffer (marker-buffer m))
  7394. (goto-char m)
  7395. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7396. (defun org-remove-angle-brackets (s)
  7397. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7398. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7399. s)
  7400. (defun org-add-angle-brackets (s)
  7401. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7402. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7403. s)
  7404. (defun org-remove-double-quotes (s)
  7405. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7406. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7407. s)
  7408. ;;; Following specific links
  7409. (defun org-follow-timestamp-link ()
  7410. (cond
  7411. ((org-at-date-range-p t)
  7412. (let ((org-agenda-start-on-weekday)
  7413. (t1 (match-string 1))
  7414. (t2 (match-string 2)))
  7415. (setq t1 (time-to-days (org-time-string-to-time t1))
  7416. t2 (time-to-days (org-time-string-to-time t2)))
  7417. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7418. ((org-at-timestamp-p t)
  7419. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7420. (substring (match-string 1) 0 10)))
  7421. 1))
  7422. (t (error "This should not happen"))))
  7423. ;;; Following file links
  7424. (defvar org-wait nil)
  7425. (defun org-open-file (path &optional in-emacs line search)
  7426. "Open the file at PATH.
  7427. First, this expands any special file name abbreviations. Then the
  7428. configuration variable `org-file-apps' is checked if it contains an
  7429. entry for this file type, and if yes, the corresponding command is launched.
  7430. If no application is found, Emacs simply visits the file.
  7431. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7432. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7433. and o use an external application to visit the file.
  7434. Optional LINE specifies a line to go to, optional SEARCH a string to
  7435. search for. If LINE or SEARCH is given, the file will always be
  7436. opened in Emacs.
  7437. If the file does not exist, an error is thrown."
  7438. (setq in-emacs (or in-emacs line search))
  7439. (let* ((file (if (equal path "")
  7440. buffer-file-name
  7441. (substitute-in-file-name (expand-file-name path))))
  7442. (apps (append org-file-apps (org-default-apps)))
  7443. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7444. (dirp (if remp nil (file-directory-p file)))
  7445. (file (if (and dirp org-open-directory-means-index-dot-org)
  7446. (concat (file-name-as-directory file) "index.org")
  7447. file))
  7448. (a-m-a-p (assq 'auto-mode apps))
  7449. (dfile (downcase file))
  7450. (old-buffer (current-buffer))
  7451. (old-pos (point))
  7452. (old-mode major-mode)
  7453. ext cmd)
  7454. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7455. (setq ext (match-string 1 dfile))
  7456. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7457. (setq ext (match-string 1 dfile))))
  7458. (cond
  7459. ((equal in-emacs '(16))
  7460. (setq cmd (cdr (assoc 'system apps))))
  7461. (in-emacs (setq cmd 'emacs))
  7462. (t
  7463. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7464. (and dirp (cdr (assoc 'directory apps)))
  7465. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7466. 'string-match)
  7467. (cdr (assoc ext apps))
  7468. (cdr (assoc t apps))))))
  7469. (when (eq cmd 'system)
  7470. (setq cmd (cdr (assoc 'system apps))))
  7471. (when (eq cmd 'default)
  7472. (setq cmd (cdr (assoc t apps))))
  7473. (when (eq cmd 'mailcap)
  7474. (require 'mailcap)
  7475. (mailcap-parse-mailcaps)
  7476. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7477. (command (mailcap-mime-info mime-type)))
  7478. (if (stringp command)
  7479. (setq cmd command)
  7480. (setq cmd 'emacs))))
  7481. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7482. (not (file-exists-p file))
  7483. (not org-open-non-existing-files))
  7484. (error "No such file: %s" file))
  7485. (cond
  7486. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7487. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7488. (while (string-match "['\"]%s['\"]" cmd)
  7489. (setq cmd (replace-match "%s" t t cmd)))
  7490. (while (string-match "%s" cmd)
  7491. (setq cmd (replace-match
  7492. (save-match-data
  7493. (shell-quote-argument
  7494. (convert-standard-filename file)))
  7495. t t cmd)))
  7496. (save-window-excursion
  7497. (start-process-shell-command cmd nil cmd)
  7498. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7499. ))
  7500. ((or (stringp cmd)
  7501. (eq cmd 'emacs))
  7502. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7503. (widen)
  7504. (if line (goto-line line)
  7505. (if search (org-link-search search))))
  7506. ((consp cmd)
  7507. (let ((file (convert-standard-filename file)))
  7508. (eval cmd)))
  7509. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7510. (and (org-mode-p) (eq old-mode 'org-mode)
  7511. (or (not (equal old-buffer (current-buffer)))
  7512. (not (equal old-pos (point))))
  7513. (org-mark-ring-push old-pos old-buffer))))
  7514. (defun org-default-apps ()
  7515. "Return the default applications for this operating system."
  7516. (cond
  7517. ((eq system-type 'darwin)
  7518. org-file-apps-defaults-macosx)
  7519. ((eq system-type 'windows-nt)
  7520. org-file-apps-defaults-windowsnt)
  7521. (t org-file-apps-defaults-gnu)))
  7522. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7523. "Convert extensions to regular expressions in the cars of LIST.
  7524. Also, weed out any non-string entries, because the return value is used
  7525. only for regexp matching.
  7526. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7527. point to the symbol `emacs', indicating that the file should
  7528. be opened in Emacs."
  7529. (append
  7530. (delq nil
  7531. (mapcar (lambda (x)
  7532. (if (not (stringp (car x)))
  7533. nil
  7534. (if (string-match "\\W" (car x))
  7535. x
  7536. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7537. list))
  7538. (if add-auto-mode
  7539. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7540. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7541. (defun org-file-remote-p (file)
  7542. "Test whether FILE specifies a location on a remote system.
  7543. Return non-nil if the location is indeed remote.
  7544. For example, the filename \"/user@host:/foo\" specifies a location
  7545. on the system \"/user@host:\"."
  7546. (cond ((fboundp 'file-remote-p)
  7547. (file-remote-p file))
  7548. ((fboundp 'tramp-handle-file-remote-p)
  7549. (tramp-handle-file-remote-p file))
  7550. ((and (boundp 'ange-ftp-name-format)
  7551. (string-match (car ange-ftp-name-format) file))
  7552. t)
  7553. (t nil)))
  7554. ;;;; Refiling
  7555. (defun org-get-org-file ()
  7556. "Read a filename, with default directory `org-directory'."
  7557. (let ((default (or org-default-notes-file remember-data-file)))
  7558. (read-file-name (format "File name [%s]: " default)
  7559. (file-name-as-directory org-directory)
  7560. default)))
  7561. (defun org-notes-order-reversed-p ()
  7562. "Check if the current file should receive notes in reversed order."
  7563. (cond
  7564. ((not org-reverse-note-order) nil)
  7565. ((eq t org-reverse-note-order) t)
  7566. ((not (listp org-reverse-note-order)) nil)
  7567. (t (catch 'exit
  7568. (let ((all org-reverse-note-order)
  7569. entry)
  7570. (while (setq entry (pop all))
  7571. (if (string-match (car entry) buffer-file-name)
  7572. (throw 'exit (cdr entry))))
  7573. nil)))))
  7574. (defvar org-refile-target-table nil
  7575. "The list of refile targets, created by `org-refile'.")
  7576. (defvar org-agenda-new-buffers nil
  7577. "Buffers created to visit agenda files.")
  7578. (defun org-get-refile-targets (&optional default-buffer)
  7579. "Produce a table with refile targets."
  7580. (let ((case-fold-search nil)
  7581. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7582. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7583. targets txt re files f desc descre fast-path-p level pos0)
  7584. (message "Getting targets...")
  7585. (with-current-buffer (or default-buffer (current-buffer))
  7586. (while (setq entry (pop entries))
  7587. (setq files (car entry) desc (cdr entry))
  7588. (setq fast-path-p nil)
  7589. (cond
  7590. ((null files) (setq files (list (current-buffer))))
  7591. ((eq files 'org-agenda-files)
  7592. (setq files (org-agenda-files 'unrestricted)))
  7593. ((and (symbolp files) (fboundp files))
  7594. (setq files (funcall files)))
  7595. ((and (symbolp files) (boundp files))
  7596. (setq files (symbol-value files))))
  7597. (if (stringp files) (setq files (list files)))
  7598. (cond
  7599. ((eq (car desc) :tag)
  7600. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7601. ((eq (car desc) :todo)
  7602. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7603. ((eq (car desc) :regexp)
  7604. (setq descre (cdr desc)))
  7605. ((eq (car desc) :level)
  7606. (setq descre (concat "^\\*\\{" (number-to-string
  7607. (if org-odd-levels-only
  7608. (1- (* 2 (cdr desc)))
  7609. (cdr desc)))
  7610. "\\}[ \t]")))
  7611. ((eq (car desc) :maxlevel)
  7612. (setq fast-path-p t)
  7613. (setq descre (concat "^\\*\\{1," (number-to-string
  7614. (if org-odd-levels-only
  7615. (1- (* 2 (cdr desc)))
  7616. (cdr desc)))
  7617. "\\}[ \t]")))
  7618. (t (error "Bad refiling target description %s" desc)))
  7619. (while (setq f (pop files))
  7620. (save-excursion
  7621. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7622. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7623. (setq f (expand-file-name f))
  7624. (if (eq org-refile-use-outline-path 'file)
  7625. (push (list (file-name-nondirectory f) f nil nil) targets))
  7626. (save-excursion
  7627. (save-restriction
  7628. (widen)
  7629. (goto-char (point-min))
  7630. (while (re-search-forward descre nil t)
  7631. (goto-char (setq pos0 (point-at-bol)))
  7632. (catch 'next
  7633. (when org-refile-target-verify-function
  7634. (save-match-data
  7635. (or (funcall org-refile-target-verify-function)
  7636. (throw 'next t))))
  7637. (when (looking-at org-complex-heading-regexp)
  7638. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7639. txt (org-link-display-format (match-string 4))
  7640. re (concat "^" (regexp-quote
  7641. (buffer-substring (match-beginning 1)
  7642. (match-end 4)))))
  7643. (if (match-end 5) (setq re (concat re "[ \t]+"
  7644. (regexp-quote
  7645. (match-string 5)))))
  7646. (setq re (concat re "[ \t]*$"))
  7647. (when org-refile-use-outline-path
  7648. (setq txt (mapconcat 'org-protect-slash
  7649. (append
  7650. (if (eq org-refile-use-outline-path 'file)
  7651. (list (file-name-nondirectory
  7652. (buffer-file-name (buffer-base-buffer))))
  7653. (if (eq org-refile-use-outline-path 'full-file-path)
  7654. (list (buffer-file-name (buffer-base-buffer)))))
  7655. (org-get-outline-path fast-path-p level txt)
  7656. (list txt))
  7657. "/")))
  7658. (push (list txt f re (point)) targets)))
  7659. (when (= (point) pos0)
  7660. ;; verification function has not moved point
  7661. (goto-char (point-at-eol))))))))))
  7662. (message "Getting targets...done")
  7663. (nreverse targets)))
  7664. (defun org-protect-slash (s)
  7665. (while (string-match "/" s)
  7666. (setq s (replace-match "\\" t t s)))
  7667. s)
  7668. (defvar org-olpa (make-vector 20 nil))
  7669. (defun org-get-outline-path (&optional fastp level heading)
  7670. "Return the outline path to the current entry, as a list."
  7671. (if fastp
  7672. (progn
  7673. (if (> level 19)
  7674. (error "Outline path failure, more than 19 levels."))
  7675. (loop for i from level upto 19 do
  7676. (aset org-olpa i nil))
  7677. (prog1
  7678. (delq nil (append org-olpa nil))
  7679. (aset org-olpa level heading)))
  7680. (let (rtn)
  7681. (save-excursion
  7682. (while (org-up-heading-safe)
  7683. (when (looking-at org-complex-heading-regexp)
  7684. (push (org-match-string-no-properties 4) rtn)))
  7685. rtn))))
  7686. (defvar org-refile-history nil
  7687. "History for refiling operations.")
  7688. (defvar org-after-refile-insert-hook nil
  7689. "Hook run after `org-refile' has inserted its stuff at the new location.
  7690. Note that this is still *before* the stuff will be removed from
  7691. the *old* location.")
  7692. (defun org-refile (&optional goto default-buffer rfloc)
  7693. "Move the entry at point to another heading.
  7694. The list of target headings is compiled using the information in
  7695. `org-refile-targets', which see. This list is created before each use
  7696. and will therefore always be up-to-date.
  7697. At the target location, the entry is filed as a subitem of the target heading.
  7698. Depending on `org-reverse-note-order', the new subitem will either be the
  7699. first or the last subitem.
  7700. If there is an active region, all entries in that region will be moved.
  7701. However, the region must fulfil the requirement that the first heading
  7702. is the first one sets the top-level of the moved text - at most siblings
  7703. below it are allowed.
  7704. With prefix arg GOTO, the command will only visit the target location,
  7705. not actually move anything.
  7706. With a double prefix `C-u C-u', go to the location where the last refiling
  7707. operation has put the subtree.
  7708. RFLOC can be a refile location obtained in a different way.
  7709. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7710. (interactive "P")
  7711. (let* ((cbuf (current-buffer))
  7712. (regionp (org-region-active-p))
  7713. (region-start (and regionp (region-beginning)))
  7714. (region-end (and regionp (region-end)))
  7715. (region-length (and regionp (- region-end region-start)))
  7716. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7717. pos it nbuf file re level reversed)
  7718. (when regionp (goto-char region-start)
  7719. (unless (org-kill-is-subtree-p
  7720. (buffer-substring region-start region-end))
  7721. (error "The region is not a (sequence of) subtree(s)")))
  7722. (if (equal goto '(16))
  7723. (org-refile-goto-last-stored)
  7724. (when (setq it (or rfloc
  7725. (save-excursion
  7726. (org-refile-get-location
  7727. (if goto "Goto: " "Refile to: ") default-buffer
  7728. org-refile-allow-creating-parent-nodes))))
  7729. (setq file (nth 1 it)
  7730. re (nth 2 it)
  7731. pos (nth 3 it))
  7732. (if (and (not goto)
  7733. pos
  7734. (equal (buffer-file-name) file)
  7735. (if regionp
  7736. (and (>= pos region-start)
  7737. (<= pos region-end))
  7738. (and (>= pos (point))
  7739. (< pos (save-excursion
  7740. (org-end-of-subtree t t))))))
  7741. (error "Cannot refile to position inside the tree or region"))
  7742. (setq nbuf (or (find-buffer-visiting file)
  7743. (find-file-noselect file)))
  7744. (if goto
  7745. (progn
  7746. (switch-to-buffer nbuf)
  7747. (goto-char pos)
  7748. (org-show-context 'org-goto))
  7749. (if regionp
  7750. (progn
  7751. (org-kill-new (buffer-substring region-start region-end))
  7752. (org-save-markers-in-region region-start region-end))
  7753. (org-copy-subtree 1 nil t))
  7754. (save-excursion
  7755. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7756. (find-file-noselect file))))
  7757. (setq reversed (org-notes-order-reversed-p))
  7758. (save-excursion
  7759. (save-restriction
  7760. (widen)
  7761. (if pos
  7762. (progn
  7763. (goto-char pos)
  7764. (looking-at outline-regexp)
  7765. (setq level (org-get-valid-level (funcall outline-level) 1))
  7766. (goto-char
  7767. (if reversed
  7768. (or (outline-next-heading) (point-max))
  7769. (or (save-excursion (outline-get-next-sibling))
  7770. (org-end-of-subtree t t)
  7771. (point-max)))))
  7772. (setq level 1)
  7773. (if (not reversed)
  7774. (goto-char (point-max))
  7775. (goto-char (point-min))
  7776. (or (outline-next-heading) (goto-char (point-max)))))
  7777. (if (not (bolp)) (newline))
  7778. (bookmark-set "org-refile-last-stored")
  7779. (org-paste-subtree level)
  7780. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7781. (run-hooks 'org-after-refile-insert-hook))))
  7782. (if regionp
  7783. (delete-region (point) (+ (point) region-length))
  7784. (org-cut-subtree))
  7785. (when (featurep 'org-inlinetask)
  7786. (org-inlinetask-remove-END-maybe))
  7787. (setq org-markers-to-move nil)
  7788. (message "Refiled to \"%s\"" (car it))))))
  7789. (org-reveal))
  7790. (defun org-refile-goto-last-stored ()
  7791. "Go to the location where the last refile was stored."
  7792. (interactive)
  7793. (bookmark-jump "org-refile-last-stored")
  7794. (message "This is the location of the last refile"))
  7795. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7796. "Prompt the user for a refile location, using PROMPT."
  7797. (let ((org-refile-targets org-refile-targets)
  7798. (org-refile-use-outline-path org-refile-use-outline-path))
  7799. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7800. (unless org-refile-target-table
  7801. (error "No refile targets"))
  7802. (let* ((cbuf (current-buffer))
  7803. (partial-completion-mode nil)
  7804. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7805. (cfunc (if (and org-refile-use-outline-path
  7806. org-outline-path-complete-in-steps)
  7807. 'org-olpath-completing-read
  7808. 'org-ido-completing-read))
  7809. (extra (if org-refile-use-outline-path "/" ""))
  7810. (filename (and cfn (expand-file-name cfn)))
  7811. (tbl (mapcar
  7812. (lambda (x)
  7813. (if (and (not (member org-refile-use-outline-path
  7814. '(file full-file-path)))
  7815. (not (equal filename (nth 1 x))))
  7816. (cons (concat (car x) extra " ("
  7817. (file-name-nondirectory (nth 1 x)) ")")
  7818. (cdr x))
  7819. (cons (concat (car x) extra) (cdr x))))
  7820. org-refile-target-table))
  7821. (completion-ignore-case t)
  7822. pa answ parent-target child parent)
  7823. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7824. nil 'org-refile-history))
  7825. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7826. (if pa
  7827. (progn
  7828. (setcar org-refile-history (car pa))
  7829. pa)
  7830. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7831. (setq parent (match-string 1 answ)
  7832. child (match-string 2 answ))
  7833. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7834. (when (and parent-target
  7835. (or (eq new-nodes t)
  7836. (and (eq new-nodes 'confirm)
  7837. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7838. (org-refile-new-child parent-target child))))))
  7839. (defun org-refile-new-child (parent-target child)
  7840. "Use refile target PARENT-TARGET to add new CHILD below it."
  7841. (unless parent-target
  7842. (error "Cannot find parent for new node"))
  7843. (let ((file (nth 1 parent-target))
  7844. (pos (nth 3 parent-target))
  7845. level)
  7846. (with-current-buffer (or (find-buffer-visiting file)
  7847. (find-file-noselect file))
  7848. (save-excursion
  7849. (save-restriction
  7850. (widen)
  7851. (if pos
  7852. (goto-char pos)
  7853. (goto-char (point-max))
  7854. (if (not (bolp)) (newline)))
  7855. (when (looking-at outline-regexp)
  7856. (setq level (funcall outline-level))
  7857. (org-end-of-subtree t t))
  7858. (org-back-over-empty-lines)
  7859. (insert "\n" (make-string
  7860. (if pos (org-get-valid-level level 1) 1) ?*)
  7861. " " child "\n")
  7862. (beginning-of-line 0)
  7863. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7864. (defun org-olpath-completing-read (prompt collection &rest args)
  7865. "Read an outline path like a file name."
  7866. (let ((thetable collection)
  7867. (org-completion-use-ido nil)) ; does not work with ido.
  7868. (apply
  7869. 'org-ido-completing-read prompt
  7870. (lambda (string predicate &optional flag)
  7871. (let (rtn r f (l (length string)))
  7872. (cond
  7873. ((eq flag nil)
  7874. ;; try completion
  7875. (try-completion string thetable))
  7876. ((eq flag t)
  7877. ;; all-completions
  7878. (setq rtn (all-completions string thetable predicate))
  7879. (mapcar
  7880. (lambda (x)
  7881. (setq r (substring x l))
  7882. (if (string-match " ([^)]*)$" x)
  7883. (setq f (match-string 0 x))
  7884. (setq f ""))
  7885. (if (string-match "/" r)
  7886. (concat string (substring r 0 (match-end 0)) f)
  7887. x))
  7888. rtn))
  7889. ((eq flag 'lambda)
  7890. ;; exact match?
  7891. (assoc string thetable)))
  7892. ))
  7893. args)))
  7894. ;;;; Dynamic blocks
  7895. (defun org-find-dblock (name)
  7896. "Find the first dynamic block with name NAME in the buffer.
  7897. If not found, stay at current position and return nil."
  7898. (let (pos)
  7899. (save-excursion
  7900. (goto-char (point-min))
  7901. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7902. nil t)
  7903. (match-beginning 0))))
  7904. (if pos (goto-char pos))
  7905. pos))
  7906. (defconst org-dblock-start-re
  7907. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7908. "Matches the startline of a dynamic block, with parameters.")
  7909. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7910. "Matches the end of a dynamic block.")
  7911. (defun org-create-dblock (plist)
  7912. "Create a dynamic block section, with parameters taken from PLIST.
  7913. PLIST must contain a :name entry which is used as name of the block."
  7914. (unless (bolp) (newline))
  7915. (let ((name (plist-get plist :name)))
  7916. (insert "#+BEGIN: " name)
  7917. (while plist
  7918. (if (eq (car plist) :name)
  7919. (setq plist (cddr plist))
  7920. (insert " " (prin1-to-string (pop plist)))))
  7921. (insert "\n\n#+END:\n")
  7922. (beginning-of-line -2)))
  7923. (defun org-prepare-dblock ()
  7924. "Prepare dynamic block for refresh.
  7925. This empties the block, puts the cursor at the insert position and returns
  7926. the property list including an extra property :name with the block name."
  7927. (unless (looking-at org-dblock-start-re)
  7928. (error "Not at a dynamic block"))
  7929. (let* ((begdel (1+ (match-end 0)))
  7930. (name (org-no-properties (match-string 1)))
  7931. (params (append (list :name name)
  7932. (read (concat "(" (match-string 3) ")")))))
  7933. (unless (re-search-forward org-dblock-end-re nil t)
  7934. (error "Dynamic block not terminated"))
  7935. (setq params
  7936. (append params
  7937. (list :content (buffer-substring
  7938. begdel (match-beginning 0)))))
  7939. (delete-region begdel (match-beginning 0))
  7940. (goto-char begdel)
  7941. (open-line 1)
  7942. params))
  7943. (defun org-map-dblocks (&optional command)
  7944. "Apply COMMAND to all dynamic blocks in the current buffer.
  7945. If COMMAND is not given, use `org-update-dblock'."
  7946. (let ((cmd (or command 'org-update-dblock))
  7947. pos)
  7948. (save-excursion
  7949. (goto-char (point-min))
  7950. (while (re-search-forward org-dblock-start-re nil t)
  7951. (goto-char (setq pos (match-beginning 0)))
  7952. (condition-case nil
  7953. (funcall cmd)
  7954. (error (message "Error during update of dynamic block")))
  7955. (goto-char pos)
  7956. (unless (re-search-forward org-dblock-end-re nil t)
  7957. (error "Dynamic block not terminated"))))))
  7958. (defun org-dblock-update (&optional arg)
  7959. "User command for updating dynamic blocks.
  7960. Update the dynamic block at point. With prefix ARG, update all dynamic
  7961. blocks in the buffer."
  7962. (interactive "P")
  7963. (if arg
  7964. (org-update-all-dblocks)
  7965. (or (looking-at org-dblock-start-re)
  7966. (org-beginning-of-dblock))
  7967. (org-update-dblock)))
  7968. (defun org-update-dblock ()
  7969. "Update the dynamic block at point
  7970. This means to empty the block, parse for parameters and then call
  7971. the correct writing function."
  7972. (save-window-excursion
  7973. (let* ((pos (point))
  7974. (line (org-current-line))
  7975. (params (org-prepare-dblock))
  7976. (name (plist-get params :name))
  7977. (cmd (intern (concat "org-dblock-write:" name))))
  7978. (message "Updating dynamic block `%s' at line %d..." name line)
  7979. (funcall cmd params)
  7980. (message "Updating dynamic block `%s' at line %d...done" name line)
  7981. (goto-char pos))))
  7982. (defun org-beginning-of-dblock ()
  7983. "Find the beginning of the dynamic block at point.
  7984. Error if there is no such block at point."
  7985. (let ((pos (point))
  7986. beg)
  7987. (end-of-line 1)
  7988. (if (and (re-search-backward org-dblock-start-re nil t)
  7989. (setq beg (match-beginning 0))
  7990. (re-search-forward org-dblock-end-re nil t)
  7991. (> (match-end 0) pos))
  7992. (goto-char beg)
  7993. (goto-char pos)
  7994. (error "Not in a dynamic block"))))
  7995. (defun org-update-all-dblocks ()
  7996. "Update all dynamic blocks in the buffer.
  7997. This function can be used in a hook."
  7998. (when (org-mode-p)
  7999. (org-map-dblocks 'org-update-dblock)))
  8000. ;;;; Completion
  8001. (defconst org-additional-option-like-keywords
  8002. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8003. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8004. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8005. "BEGIN:" "END:"
  8006. "ORGTBL" "TBLFM:" "TBLNAME:"
  8007. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8008. "BEGIN_QUOTE" "END_QUOTE"
  8009. "BEGIN_VERSE" "END_VERSE"
  8010. "BEGIN_CENTER" "END_CENTER"
  8011. "BEGIN_SRC" "END_SRC"
  8012. "CATEGORY" "COLUMNS"
  8013. "CAPTION" "LABEL"
  8014. "BIND"))
  8015. (defcustom org-structure-template-alist
  8016. '(
  8017. ("s" "#+begin_src ?\n\n#+end_src"
  8018. "<src lang=\"?\">\n\n</src>")
  8019. ("e" "#+begin_example\n?\n#+end_example"
  8020. "<example>\n?\n</example>")
  8021. ("q" "#+begin_quote\n?\n#+end_quote"
  8022. "<quote>\n?\n</quote>")
  8023. ("v" "#+begin_verse\n?\n#+end_verse"
  8024. "<verse>\n?\n/verse>")
  8025. ("c" "#+begin_center\n?\n#+end_center"
  8026. "<center>\n?\n/center>")
  8027. ("l" "#+begin_latex\n?\n#+end_latex"
  8028. "<literal style=\"latex\">\n?\n</literal>")
  8029. ("L" "#+latex: "
  8030. "<literal style=\"latex\">?</literal>")
  8031. ("h" "#+begin_html\n?\n#+end_html"
  8032. "<literal style=\"html\">\n?\n</literal>")
  8033. ("H" "#+html: "
  8034. "<literal style=\"html\">?</literal>")
  8035. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8036. ("A" "#+ascii: ")
  8037. ("i" "#+include %file ?"
  8038. "<include file=%file markup=\"?\">")
  8039. )
  8040. "Structure completion elements.
  8041. This is a list of abbreviation keys and values. The value gets inserted
  8042. it you type @samp{.} followed by the key and then the completion key,
  8043. usually `M-TAB'. %file will be replaced by a file name after prompting
  8044. for the file using completion.
  8045. There are two templates for each key, the first uses the original Org syntax,
  8046. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8047. the default when the /org-mtags.el/ module has been loaded. See also the
  8048. variable `org-mtags-prefer-muse-templates'.
  8049. This is an experimental feature, it is undecided if it is going to stay in."
  8050. :group 'org-completion
  8051. :type '(repeat
  8052. (string :tag "Key")
  8053. (string :tag "Template")
  8054. (string :tag "Muse Template")))
  8055. (defun org-try-structure-completion ()
  8056. "Try to complete a structure template before point.
  8057. This looks for strings like \"<e\" on an otherwise empty line and
  8058. expands them."
  8059. (let ((l (buffer-substring (point-at-bol) (point)))
  8060. a)
  8061. (when (and (looking-at "[ \t]*$")
  8062. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8063. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8064. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8065. (match-beginning 1)) a)
  8066. t)))
  8067. (defun org-complete-expand-structure-template (start cell)
  8068. "Expand a structure template."
  8069. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8070. (rpl (nth (if musep 2 1) cell))
  8071. (ind ""))
  8072. (delete-region start (point))
  8073. (when (string-match "\\`#\\+" rpl)
  8074. (cond
  8075. ((bolp))
  8076. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8077. (setq ind (buffer-substring (point-at-bol) (point))))
  8078. (t (newline))))
  8079. (setq start (point))
  8080. (if (string-match "%file" rpl)
  8081. (setq rpl (replace-match
  8082. (concat
  8083. "\""
  8084. (save-match-data
  8085. (abbreviate-file-name (read-file-name "Include file: ")))
  8086. "\"")
  8087. t t rpl)))
  8088. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8089. (concat "\n" ind)))
  8090. (insert rpl)
  8091. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8092. (defun org-complete (&optional arg)
  8093. "Perform completion on word at point.
  8094. At the beginning of a headline, this completes TODO keywords as given in
  8095. `org-todo-keywords'.
  8096. If the current word is preceded by a backslash, completes the TeX symbols
  8097. that are supported for HTML support.
  8098. If the current word is preceded by \"#+\", completes special words for
  8099. setting file options.
  8100. In the line after \"#+STARTUP:, complete valid keywords.\"
  8101. At all other locations, this simply calls the value of
  8102. `org-completion-fallback-command'."
  8103. (interactive "P")
  8104. (org-without-partial-completion
  8105. (catch 'exit
  8106. (let* ((a nil)
  8107. (end (point))
  8108. (beg1 (save-excursion
  8109. (skip-chars-backward (org-re "[:alnum:]_@"))
  8110. (point)))
  8111. (beg (save-excursion
  8112. (skip-chars-backward "a-zA-Z0-9_:$")
  8113. (point)))
  8114. (confirm (lambda (x) (stringp (car x))))
  8115. (searchhead (equal (char-before beg) ?*))
  8116. (struct
  8117. (when (and (member (char-before beg1) '(?. ?<))
  8118. (setq a (assoc (buffer-substring beg1 (point))
  8119. org-structure-template-alist)))
  8120. (org-complete-expand-structure-template (1- beg1) a)
  8121. (throw 'exit t)))
  8122. (tag (and (equal (char-before beg1) ?:)
  8123. (equal (char-after (point-at-bol)) ?*)))
  8124. (prop (and (equal (char-before beg1) ?:)
  8125. (not (equal (char-after (point-at-bol)) ?*))))
  8126. (texp (equal (char-before beg) ?\\))
  8127. (link (equal (char-before beg) ?\[))
  8128. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8129. beg)
  8130. "#+"))
  8131. (startup (string-match "^#\\+STARTUP:.*"
  8132. (buffer-substring (point-at-bol) (point))))
  8133. (completion-ignore-case opt)
  8134. (type nil)
  8135. (tbl nil)
  8136. (table (cond
  8137. (opt
  8138. (setq type :opt)
  8139. (require 'org-exp)
  8140. (append
  8141. (mapcar
  8142. (lambda (x)
  8143. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8144. (cons (match-string 2 x) (match-string 1 x)))
  8145. (org-split-string (org-get-current-options) "\n"))
  8146. (mapcar 'list org-additional-option-like-keywords)))
  8147. (startup
  8148. (setq type :startup)
  8149. org-startup-options)
  8150. (link (append org-link-abbrev-alist-local
  8151. org-link-abbrev-alist))
  8152. (texp
  8153. (setq type :tex)
  8154. org-html-entities)
  8155. ((string-match "\\`\\*+[ \t]+\\'"
  8156. (buffer-substring (point-at-bol) beg))
  8157. (setq type :todo)
  8158. (mapcar 'list org-todo-keywords-1))
  8159. (searchhead
  8160. (setq type :searchhead)
  8161. (save-excursion
  8162. (goto-char (point-min))
  8163. (while (re-search-forward org-todo-line-regexp nil t)
  8164. (push (list
  8165. (org-make-org-heading-search-string
  8166. (match-string 3) t))
  8167. tbl)))
  8168. tbl)
  8169. (tag (setq type :tag beg beg1)
  8170. (or org-tag-alist (org-get-buffer-tags)))
  8171. (prop (setq type :prop beg beg1)
  8172. (mapcar 'list (org-buffer-property-keys nil t t)))
  8173. (t (progn
  8174. (call-interactively org-completion-fallback-command)
  8175. (throw 'exit nil)))))
  8176. (pattern (buffer-substring-no-properties beg end))
  8177. (completion (try-completion pattern table confirm)))
  8178. (cond ((eq completion t)
  8179. (if (not (assoc (upcase pattern) table))
  8180. (message "Already complete")
  8181. (if (and (equal type :opt)
  8182. (not (member (car (assoc (upcase pattern) table))
  8183. org-additional-option-like-keywords)))
  8184. (insert (substring (cdr (assoc (upcase pattern) table))
  8185. (length pattern)))
  8186. (if (memq type '(:tag :prop)) (insert ":")))))
  8187. ((null completion)
  8188. (message "Can't find completion for \"%s\"" pattern)
  8189. (ding))
  8190. ((not (string= pattern completion))
  8191. (delete-region beg end)
  8192. (if (string-match " +$" completion)
  8193. (setq completion (replace-match "" t t completion)))
  8194. (insert completion)
  8195. (if (get-buffer-window "*Completions*")
  8196. (delete-window (get-buffer-window "*Completions*")))
  8197. (if (assoc completion table)
  8198. (if (eq type :todo) (insert " ")
  8199. (if (memq type '(:tag :prop)) (insert ":"))))
  8200. (if (and (equal type :opt) (assoc completion table))
  8201. (message "%s" (substitute-command-keys
  8202. "Press \\[org-complete] again to insert example settings"))))
  8203. (t
  8204. (message "Making completion list...")
  8205. (let ((list (sort (all-completions pattern table confirm)
  8206. 'string<)))
  8207. (with-output-to-temp-buffer "*Completions*"
  8208. (condition-case nil
  8209. ;; Protection needed for XEmacs and emacs 21
  8210. (display-completion-list list pattern)
  8211. (error (display-completion-list list)))))
  8212. (message "Making completion list...%s" "done")))))))
  8213. ;;;; TODO, DEADLINE, Comments
  8214. (defun org-toggle-comment ()
  8215. "Change the COMMENT state of an entry."
  8216. (interactive)
  8217. (save-excursion
  8218. (org-back-to-heading)
  8219. (let (case-fold-search)
  8220. (if (looking-at (concat outline-regexp
  8221. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8222. (replace-match "" t t nil 1)
  8223. (if (looking-at outline-regexp)
  8224. (progn
  8225. (goto-char (match-end 0))
  8226. (insert org-comment-string " ")))))))
  8227. (defvar org-last-todo-state-is-todo nil
  8228. "This is non-nil when the last TODO state change led to a TODO state.
  8229. If the last change removed the TODO tag or switched to DONE, then
  8230. this is nil.")
  8231. (defvar org-setting-tags nil) ; dynamically skipped
  8232. (defun org-parse-local-options (string var)
  8233. "Parse STRING for startup setting relevant for variable VAR."
  8234. (let ((rtn (symbol-value var))
  8235. e opts)
  8236. (save-match-data
  8237. (if (or (not string) (not (string-match "\\S-" string)))
  8238. rtn
  8239. (setq opts (delq nil (mapcar (lambda (x)
  8240. (setq e (assoc x org-startup-options))
  8241. (if (eq (nth 1 e) var) e nil))
  8242. (org-split-string string "[ \t]+"))))
  8243. (if (not opts)
  8244. rtn
  8245. (setq rtn nil)
  8246. (while (setq e (pop opts))
  8247. (if (not (nth 3 e))
  8248. (setq rtn (nth 2 e))
  8249. (if (not (listp rtn)) (setq rtn nil))
  8250. (push (nth 2 e) rtn)))
  8251. rtn)))))
  8252. (defvar org-todo-setup-filter-hook nil
  8253. "Hook for functions that pre-filter todo specs.
  8254. Each function takes a todo spec and returns either `nil' or the spec
  8255. transformed into canonical form." )
  8256. (defvar org-todo-get-default-hook nil
  8257. "Hook for functions that get a default item for todo.
  8258. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8259. `nil' or a string to be used for the todo mark." )
  8260. (defvar org-agenda-headline-snapshot-before-repeat)
  8261. (defun org-todo (&optional arg)
  8262. "Change the TODO state of an item.
  8263. The state of an item is given by a keyword at the start of the heading,
  8264. like
  8265. *** TODO Write paper
  8266. *** DONE Call mom
  8267. The different keywords are specified in the variable `org-todo-keywords'.
  8268. By default the available states are \"TODO\" and \"DONE\".
  8269. So for this example: when the item starts with TODO, it is changed to DONE.
  8270. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8271. DONE are present, add TODO at the beginning of the heading.
  8272. With C-u prefix arg, use completion to determine the new state.
  8273. With numeric prefix arg, switch to that state.
  8274. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8275. With a tripple C-u prefix, circumvent any state blocking.
  8276. For calling through lisp, arg is also interpreted in the following way:
  8277. 'none -> empty state
  8278. \"\"(empty string) -> switch to empty state
  8279. 'done -> switch to DONE
  8280. 'nextset -> switch to the next set of keywords
  8281. 'previousset -> switch to the previous set of keywords
  8282. \"WAITING\" -> switch to the specified keyword, but only if it
  8283. really is a member of `org-todo-keywords'."
  8284. (interactive "P")
  8285. (if (equal arg '(16)) (setq arg 'nextset))
  8286. (let ((org-blocker-hook org-blocker-hook)
  8287. (case-fold-search nil))
  8288. (when (equal arg '(64))
  8289. (setq arg nil org-blocker-hook nil))
  8290. (when (and org-blocker-hook
  8291. (or org-inhibit-blocking
  8292. (org-entry-get nil "NOBLOCKING")))
  8293. (setq org-blocker-hook nil))
  8294. (save-excursion
  8295. (catch 'exit
  8296. (org-back-to-heading)
  8297. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8298. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8299. (looking-at " *"))
  8300. (let* ((match-data (match-data))
  8301. (startpos (point-at-bol))
  8302. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8303. (org-log-done org-log-done)
  8304. (org-log-repeat org-log-repeat)
  8305. (org-todo-log-states org-todo-log-states)
  8306. (this (match-string 1))
  8307. (hl-pos (match-beginning 0))
  8308. (head (org-get-todo-sequence-head this))
  8309. (ass (assoc head org-todo-kwd-alist))
  8310. (interpret (nth 1 ass))
  8311. (done-word (nth 3 ass))
  8312. (final-done-word (nth 4 ass))
  8313. (last-state (or this ""))
  8314. (completion-ignore-case t)
  8315. (member (member this org-todo-keywords-1))
  8316. (tail (cdr member))
  8317. (state (cond
  8318. ((and org-todo-key-trigger
  8319. (or (and (equal arg '(4))
  8320. (eq org-use-fast-todo-selection 'prefix))
  8321. (and (not arg) org-use-fast-todo-selection
  8322. (not (eq org-use-fast-todo-selection
  8323. 'prefix)))))
  8324. ;; Use fast selection
  8325. (org-fast-todo-selection))
  8326. ((and (equal arg '(4))
  8327. (or (not org-use-fast-todo-selection)
  8328. (not org-todo-key-trigger)))
  8329. ;; Read a state with completion
  8330. (org-ido-completing-read
  8331. "State: " (mapcar (lambda(x) (list x))
  8332. org-todo-keywords-1)
  8333. nil t))
  8334. ((eq arg 'right)
  8335. (if this
  8336. (if tail (car tail) nil)
  8337. (car org-todo-keywords-1)))
  8338. ((eq arg 'left)
  8339. (if (equal member org-todo-keywords-1)
  8340. nil
  8341. (if this
  8342. (nth (- (length org-todo-keywords-1)
  8343. (length tail) 2)
  8344. org-todo-keywords-1)
  8345. (org-last org-todo-keywords-1))))
  8346. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8347. (setq arg nil))) ; hack to fall back to cycling
  8348. (arg
  8349. ;; user or caller requests a specific state
  8350. (cond
  8351. ((equal arg "") nil)
  8352. ((eq arg 'none) nil)
  8353. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8354. ((eq arg 'nextset)
  8355. (or (car (cdr (member head org-todo-heads)))
  8356. (car org-todo-heads)))
  8357. ((eq arg 'previousset)
  8358. (let ((org-todo-heads (reverse org-todo-heads)))
  8359. (or (car (cdr (member head org-todo-heads)))
  8360. (car org-todo-heads))))
  8361. ((car (member arg org-todo-keywords-1)))
  8362. ((nth (1- (prefix-numeric-value arg))
  8363. org-todo-keywords-1))))
  8364. ((null member) (or head (car org-todo-keywords-1)))
  8365. ((equal this final-done-word) nil) ;; -> make empty
  8366. ((null tail) nil) ;; -> first entry
  8367. ((memq interpret '(type priority))
  8368. (if (eq this-command last-command)
  8369. (car tail)
  8370. (if (> (length tail) 0)
  8371. (or done-word (car org-done-keywords))
  8372. nil)))
  8373. (t
  8374. (car tail))))
  8375. (state (or
  8376. (run-hook-with-args-until-success
  8377. 'org-todo-get-default-hook state last-state)
  8378. state))
  8379. (next (if state (concat " " state " ") " "))
  8380. (change-plist (list :type 'todo-state-change :from this :to state
  8381. :position startpos))
  8382. dolog now-done-p)
  8383. (when org-blocker-hook
  8384. (setq org-last-todo-state-is-todo
  8385. (not (member this org-done-keywords)))
  8386. (unless (save-excursion
  8387. (save-match-data
  8388. (run-hook-with-args-until-failure
  8389. 'org-blocker-hook change-plist)))
  8390. (if (interactive-p)
  8391. (error "TODO state change from %s to %s blocked" this state)
  8392. ;; fail silently
  8393. (message "TODO state change from %s to %s blocked" this state)
  8394. (throw 'exit nil))))
  8395. (store-match-data match-data)
  8396. (replace-match next t t)
  8397. (unless (pos-visible-in-window-p hl-pos)
  8398. (message "TODO state changed to %s" (org-trim next)))
  8399. (unless head
  8400. (setq head (org-get-todo-sequence-head state)
  8401. ass (assoc head org-todo-kwd-alist)
  8402. interpret (nth 1 ass)
  8403. done-word (nth 3 ass)
  8404. final-done-word (nth 4 ass)))
  8405. (when (memq arg '(nextset previousset))
  8406. (message "Keyword-Set %d/%d: %s"
  8407. (- (length org-todo-sets) -1
  8408. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8409. (length org-todo-sets)
  8410. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8411. (setq org-last-todo-state-is-todo
  8412. (not (member state org-done-keywords)))
  8413. (setq now-done-p (and (member state org-done-keywords)
  8414. (not (member this org-done-keywords))))
  8415. (and logging (org-local-logging logging))
  8416. (when (and (or org-todo-log-states org-log-done)
  8417. (not (eq org-inhibit-logging t))
  8418. (not (memq arg '(nextset previousset))))
  8419. ;; we need to look at recording a time and note
  8420. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8421. (nth 2 (assoc this org-todo-log-states))))
  8422. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8423. (setq dolog 'time))
  8424. (when (and state
  8425. (member state org-not-done-keywords)
  8426. (not (member this org-not-done-keywords)))
  8427. ;; This is now a todo state and was not one before
  8428. ;; If there was a CLOSED time stamp, get rid of it.
  8429. (org-add-planning-info nil nil 'closed))
  8430. (when (and now-done-p org-log-done)
  8431. ;; It is now done, and it was not done before
  8432. (org-add-planning-info 'closed (org-current-time))
  8433. (if (and (not dolog) (eq 'note org-log-done))
  8434. (org-add-log-setup 'done state this 'findpos 'note)))
  8435. (when (and state dolog)
  8436. ;; This is a non-nil state, and we need to log it
  8437. (org-add-log-setup 'state state this 'findpos dolog)))
  8438. ;; Fixup tag positioning
  8439. (org-todo-trigger-tag-changes state)
  8440. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8441. (when org-provide-todo-statistics
  8442. (org-update-parent-todo-statistics))
  8443. (run-hooks 'org-after-todo-state-change-hook)
  8444. (if (and arg (not (member state org-done-keywords)))
  8445. (setq head (org-get-todo-sequence-head state)))
  8446. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8447. ;; Do we need to trigger a repeat?
  8448. (when now-done-p
  8449. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8450. ;; This is for the agenda, take a snapshot of the headline.
  8451. (save-match-data
  8452. (setq org-agenda-headline-snapshot-before-repeat
  8453. (org-get-heading))))
  8454. (org-auto-repeat-maybe state))
  8455. ;; Fixup cursor location if close to the keyword
  8456. (if (and (outline-on-heading-p)
  8457. (not (bolp))
  8458. (save-excursion (beginning-of-line 1)
  8459. (looking-at org-todo-line-regexp))
  8460. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8461. (progn
  8462. (goto-char (or (match-end 2) (match-end 1)))
  8463. (and (looking-at " ") (just-one-space))))
  8464. (when org-trigger-hook
  8465. (save-excursion
  8466. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8467. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8468. "Block turning an entry into a TODO, using the hierarchy.
  8469. This checks whether the current task should be blocked from state
  8470. changes. Such blocking occurs when:
  8471. 1. The task has children which are not all in a completed state.
  8472. 2. A task has a parent with the property :ORDERED:, and there
  8473. are siblings prior to the current task with incomplete
  8474. status.
  8475. 3. The parent of the task is blocked because it has siblings that should
  8476. be done first, or is child of a block grandparent TODO entry."
  8477. (catch 'dont-block
  8478. ;; If this is not a todo state change, or if this entry is already DONE,
  8479. ;; do not block
  8480. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8481. (member (plist-get change-plist :from)
  8482. (cons 'done org-done-keywords))
  8483. (member (plist-get change-plist :to)
  8484. (cons 'todo org-not-done-keywords)))
  8485. (throw 'dont-block t))
  8486. ;; If this task has children, and any are undone, it's blocked
  8487. (save-excursion
  8488. (org-back-to-heading t)
  8489. (let ((this-level (funcall outline-level)))
  8490. (outline-next-heading)
  8491. (let ((child-level (funcall outline-level)))
  8492. (while (and (not (eobp))
  8493. (> child-level this-level))
  8494. ;; this todo has children, check whether they are all
  8495. ;; completed
  8496. (if (and (not (org-entry-is-done-p))
  8497. (org-entry-is-todo-p))
  8498. (throw 'dont-block nil))
  8499. (outline-next-heading)
  8500. (setq child-level (funcall outline-level))))))
  8501. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8502. ;; any previous siblings are undone, it's blocked
  8503. (save-excursion
  8504. (org-back-to-heading t)
  8505. (let* ((pos (point))
  8506. (parent-pos (and (org-up-heading-safe) (point))))
  8507. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8508. (when (and (org-entry-get (point) "ORDERED")
  8509. (forward-line 1)
  8510. (re-search-forward org-not-done-heading-regexp pos t))
  8511. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8512. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8513. (while t
  8514. (goto-char parent-pos)
  8515. (if (not (looking-at org-not-done-heading-regexp))
  8516. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8517. (setq pos (point))
  8518. (setq parent-pos (and (org-up-heading-safe) (point)))
  8519. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8520. (when (and (org-entry-get (point) "ORDERED")
  8521. (forward-line 1)
  8522. (re-search-forward org-not-done-heading-regexp pos t))
  8523. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8524. (defcustom org-track-ordered-property-with-tag nil
  8525. "Should the ORDERED property also be shown as a tag?
  8526. The ORDERED property decides if an entry should require subtasks to be
  8527. completed in sequence. Since a property is not very visible, setting
  8528. this option means that toggling the ORDERED property with the command
  8529. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8530. not relevant for the behavior, but it makes things more visible.
  8531. Note that toggling the tag with tags commands will not change the property
  8532. and therefore not influence behavior!
  8533. This can be t, meaning the tag ORDERED should be used, It can also be a
  8534. string to select a different tag for this task."
  8535. :group 'org-todo
  8536. :type '(choice
  8537. (const :tag "No tracking" nil)
  8538. (const :tag "Track with ORDERED tag" t)
  8539. (string :tag "Use other tag")))
  8540. (defun org-toggle-ordered-property ()
  8541. "Toggle the ORDERED property of the current entry.
  8542. For better visibility, you can track the value of this property with a tag.
  8543. See variable `org-track-ordered-property-with-tag'."
  8544. (interactive)
  8545. (let* ((t1 org-track-ordered-property-with-tag)
  8546. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8547. (save-excursion
  8548. (org-back-to-heading)
  8549. (if (org-entry-get nil "ORDERED")
  8550. (progn
  8551. (org-delete-property "ORDERED")
  8552. (and tag (org-toggle-tag tag 'off))
  8553. (message "Subtasks can be completed in arbitrary order"))
  8554. (org-entry-put nil "ORDERED" "t")
  8555. (and tag (org-toggle-tag tag 'on))
  8556. (message "Subtasks must be completed in sequence")))))
  8557. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8558. (defun org-block-todo-from-checkboxes (change-plist)
  8559. "Block turning an entry into a TODO, using checkboxes.
  8560. This checks whether the current task should be blocked from state
  8561. changes because there are uncheckd boxes in this entry."
  8562. (catch 'dont-block
  8563. ;; If this is not a todo state change, or if this entry is already DONE,
  8564. ;; do not block
  8565. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8566. (member (plist-get change-plist :from)
  8567. (cons 'done org-done-keywords))
  8568. (member (plist-get change-plist :to)
  8569. (cons 'todo org-not-done-keywords)))
  8570. (throw 'dont-block t))
  8571. ;; If this task has checkboxes that are not checked, it's blocked
  8572. (save-excursion
  8573. (org-back-to-heading t)
  8574. (let ((beg (point)) end)
  8575. (outline-next-heading)
  8576. (setq end (point))
  8577. (goto-char beg)
  8578. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8579. end t)
  8580. (progn
  8581. (if (boundp 'org-blocked-by-checkboxes)
  8582. (setq org-blocked-by-checkboxes t))
  8583. (throw 'dont-block nil)))))
  8584. t)) ; do not block
  8585. (defvar org-entry-property-inherited-from) ;; defined below
  8586. (defun org-update-parent-todo-statistics ()
  8587. "Update any statistics cookie in the parent of the current headline.
  8588. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8589. the entire subtree and this will travel up the hierarchy and update
  8590. statistics everywhere."
  8591. (interactive)
  8592. (let* ((lim 0) prop
  8593. (recursive (or (not org-hierarchical-todo-statistics)
  8594. (string-match
  8595. "\\<recursive\\>"
  8596. (or (setq prop (org-entry-get
  8597. nil "COOKIE_DATA" 'inherit)) ""))))
  8598. (lim (or (and prop (marker-position
  8599. org-entry-property-inherited-from))
  8600. lim))
  8601. (first t)
  8602. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8603. level ltoggle l1
  8604. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8605. (catch 'exit
  8606. (save-excursion
  8607. (beginning-of-line 1)
  8608. (if (org-at-heading-p)
  8609. (setq ltoggle (funcall outline-level))
  8610. (error "This should not happen"))
  8611. (while (and (setq level (org-up-heading-safe))
  8612. (or recursive first)
  8613. (>= (point) lim))
  8614. (setq first nil)
  8615. (unless (and level
  8616. (not (string-match
  8617. "\\<checkbox\\>"
  8618. (downcase
  8619. (or (org-entry-get
  8620. nil "COOKIE_DATA")
  8621. "")))))
  8622. (throw 'exit nil))
  8623. (while (re-search-forward box-re (point-at-eol) t)
  8624. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8625. (setq is-percent (match-end 2))
  8626. (save-match-data
  8627. (unless (outline-next-heading) (throw 'exit nil))
  8628. (while (and (looking-at org-complex-heading-regexp)
  8629. (> (setq l1 (length (match-string 1))) level))
  8630. (setq kwd (and (or recursive (= l1 ltoggle))
  8631. (match-string 2)))
  8632. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8633. (and (listp org-provide-todo-statistics)
  8634. (or (member kwd org-provide-todo-statistics)
  8635. (member kwd org-done-keywords))))
  8636. (setq cnt-all (1+ cnt-all))
  8637. (if (eq org-provide-todo-statistics t)
  8638. (and kwd (setq cnt-all (1+ cnt-all)))))
  8639. (and (member kwd org-done-keywords)
  8640. (setq cnt-done (1+ cnt-done)))
  8641. (outline-next-heading)))
  8642. (replace-match
  8643. (if is-percent
  8644. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8645. (format "[%d/%d]" cnt-done cnt-all)))))
  8646. (when cookie-present
  8647. (run-hook-with-args 'org-after-todo-statistics-hook
  8648. cnt-done (- cnt-all cnt-done)))))
  8649. (run-hooks 'org-todo-statistics-hook)))
  8650. (defvar org-after-todo-statistics-hook nil
  8651. "Hook that is called after a TODO statistics cookie has been updated.
  8652. Each function is called with two arguments: the number of not-done entries
  8653. and the number of done entries.
  8654. For example, the following function, when added to this hook, will switch
  8655. an entry to DONE when all children are done, and back to TODO when new
  8656. entries are set to a TODO status. Note that this hook is only called
  8657. when there is a statistics cookie in the headline!
  8658. (defun org-summary-todo (n-done n-not-done)
  8659. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8660. (let (org-log-done org-log-states) ; turn off logging
  8661. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8662. ")
  8663. (defvar org-todo-statistics-hook nil
  8664. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8665. This hook runs even if there is no statisics cookie present, in which case
  8666. `org-after-todo-statistics-hook' would not run.")
  8667. (defun org-todo-trigger-tag-changes (state)
  8668. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8669. (let ((l org-todo-state-tags-triggers)
  8670. changes)
  8671. (when (or (not state) (equal state ""))
  8672. (setq changes (append changes (cdr (assoc "" l)))))
  8673. (when (and (stringp state) (> (length state) 0))
  8674. (setq changes (append changes (cdr (assoc state l)))))
  8675. (when (member state org-not-done-keywords)
  8676. (setq changes (append changes (cdr (assoc 'todo l)))))
  8677. (when (member state org-done-keywords)
  8678. (setq changes (append changes (cdr (assoc 'done l)))))
  8679. (dolist (c changes)
  8680. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8681. (defun org-local-logging (value)
  8682. "Get logging settings from a property VALUE."
  8683. (let* (words w a)
  8684. ;; directly set the variables, they are already local.
  8685. (setq org-log-done nil
  8686. org-log-repeat nil
  8687. org-todo-log-states nil)
  8688. (setq words (org-split-string value))
  8689. (while (setq w (pop words))
  8690. (cond
  8691. ((setq a (assoc w org-startup-options))
  8692. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8693. (set (nth 1 a) (nth 2 a))))
  8694. ((setq a (org-extract-log-state-settings w))
  8695. (and (member (car a) org-todo-keywords-1)
  8696. (push a org-todo-log-states)))))))
  8697. (defun org-get-todo-sequence-head (kwd)
  8698. "Return the head of the TODO sequence to which KWD belongs.
  8699. If KWD is not set, check if there is a text property remembering the
  8700. right sequence."
  8701. (let (p)
  8702. (cond
  8703. ((not kwd)
  8704. (or (get-text-property (point-at-bol) 'org-todo-head)
  8705. (progn
  8706. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8707. nil (point-at-eol)))
  8708. (get-text-property p 'org-todo-head))))
  8709. ((not (member kwd org-todo-keywords-1))
  8710. (car org-todo-keywords-1))
  8711. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8712. (defun org-fast-todo-selection ()
  8713. "Fast TODO keyword selection with single keys.
  8714. Returns the new TODO keyword, or nil if no state change should occur."
  8715. (let* ((fulltable org-todo-key-alist)
  8716. (done-keywords org-done-keywords) ;; needed for the faces.
  8717. (maxlen (apply 'max (mapcar
  8718. (lambda (x)
  8719. (if (stringp (car x)) (string-width (car x)) 0))
  8720. fulltable)))
  8721. (expert nil)
  8722. (fwidth (+ maxlen 3 1 3))
  8723. (ncol (/ (- (window-width) 4) fwidth))
  8724. tg cnt e c tbl
  8725. groups ingroup)
  8726. (save-excursion
  8727. (save-window-excursion
  8728. (if expert
  8729. (set-buffer (get-buffer-create " *Org todo*"))
  8730. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8731. (erase-buffer)
  8732. (org-set-local 'org-done-keywords done-keywords)
  8733. (setq tbl fulltable cnt 0)
  8734. (while (setq e (pop tbl))
  8735. (cond
  8736. ((equal e '(:startgroup))
  8737. (push '() groups) (setq ingroup t)
  8738. (when (not (= cnt 0))
  8739. (setq cnt 0)
  8740. (insert "\n"))
  8741. (insert "{ "))
  8742. ((equal e '(:endgroup))
  8743. (setq ingroup nil cnt 0)
  8744. (insert "}\n"))
  8745. ((equal e '(:newline))
  8746. (when (not (= cnt 0))
  8747. (setq cnt 0)
  8748. (insert "\n")
  8749. (setq e (car tbl))
  8750. (while (equal (car tbl) '(:newline))
  8751. (insert "\n")
  8752. (setq tbl (cdr tbl)))))
  8753. (t
  8754. (setq tg (car e) c (cdr e))
  8755. (if ingroup (push tg (car groups)))
  8756. (setq tg (org-add-props tg nil 'face
  8757. (org-get-todo-face tg)))
  8758. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8759. (insert "[" c "] " tg (make-string
  8760. (- fwidth 4 (length tg)) ?\ ))
  8761. (when (= (setq cnt (1+ cnt)) ncol)
  8762. (insert "\n")
  8763. (if ingroup (insert " "))
  8764. (setq cnt 0)))))
  8765. (insert "\n")
  8766. (goto-char (point-min))
  8767. (if (not expert) (org-fit-window-to-buffer))
  8768. (message "[a-z..]:Set [SPC]:clear")
  8769. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8770. (cond
  8771. ((or (= c ?\C-g)
  8772. (and (= c ?q) (not (rassoc c fulltable))))
  8773. (setq quit-flag t))
  8774. ((= c ?\ ) nil)
  8775. ((setq e (rassoc c fulltable) tg (car e))
  8776. tg)
  8777. (t (setq quit-flag t)))))))
  8778. (defun org-entry-is-todo-p ()
  8779. (member (org-get-todo-state) org-not-done-keywords))
  8780. (defun org-entry-is-done-p ()
  8781. (member (org-get-todo-state) org-done-keywords))
  8782. (defun org-get-todo-state ()
  8783. (save-excursion
  8784. (org-back-to-heading t)
  8785. (and (looking-at org-todo-line-regexp)
  8786. (match-end 2)
  8787. (match-string 2))))
  8788. (defun org-at-date-range-p (&optional inactive-ok)
  8789. "Is the cursor inside a date range?"
  8790. (interactive)
  8791. (save-excursion
  8792. (catch 'exit
  8793. (let ((pos (point)))
  8794. (skip-chars-backward "^[<\r\n")
  8795. (skip-chars-backward "<[")
  8796. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8797. (>= (match-end 0) pos)
  8798. (throw 'exit t))
  8799. (skip-chars-backward "^<[\r\n")
  8800. (skip-chars-backward "<[")
  8801. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8802. (>= (match-end 0) pos)
  8803. (throw 'exit t)))
  8804. nil)))
  8805. (defun org-get-repeat ()
  8806. "Check if there is a deadline/schedule with repeater in this entry."
  8807. (save-match-data
  8808. (save-excursion
  8809. (org-back-to-heading t)
  8810. (if (re-search-forward
  8811. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8812. (match-string 1)))))
  8813. (defvar org-last-changed-timestamp)
  8814. (defvar org-last-inserted-timestamp)
  8815. (defvar org-log-post-message)
  8816. (defvar org-log-note-purpose)
  8817. (defvar org-log-note-how)
  8818. (defvar org-log-note-extra)
  8819. (defun org-auto-repeat-maybe (done-word)
  8820. "Check if the current headline contains a repeated deadline/schedule.
  8821. If yes, set TODO state back to what it was and change the base date
  8822. of repeating deadline/scheduled time stamps to new date.
  8823. This function is run automatically after each state change to a DONE state."
  8824. ;; last-state is dynamically scoped into this function
  8825. (let* ((repeat (org-get-repeat))
  8826. (aa (assoc last-state org-todo-kwd-alist))
  8827. (interpret (nth 1 aa))
  8828. (head (nth 2 aa))
  8829. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8830. (msg "Entry repeats: ")
  8831. (org-log-done nil)
  8832. (org-todo-log-states nil)
  8833. (nshiftmax 10) (nshift 0)
  8834. re type n what ts time)
  8835. (when repeat
  8836. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8837. (org-todo (if (eq interpret 'type) last-state head))
  8838. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8839. (org-time-stamp-format t t)))
  8840. (when org-log-repeat
  8841. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8842. (memq 'org-add-log-note post-command-hook))
  8843. ;; OK, we are already setup for some record
  8844. (if (eq org-log-repeat 'note)
  8845. ;; make sure we take a note, not only a time stamp
  8846. (setq org-log-note-how 'note))
  8847. ;; Set up for taking a record
  8848. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8849. last-state
  8850. 'findpos org-log-repeat)))
  8851. (org-back-to-heading t)
  8852. (org-add-planning-info nil nil 'closed)
  8853. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8854. org-deadline-time-regexp "\\)\\|\\("
  8855. org-ts-regexp "\\)"))
  8856. (while (re-search-forward
  8857. re (save-excursion (outline-next-heading) (point)) t)
  8858. (setq type (if (match-end 1) org-scheduled-string
  8859. (if (match-end 3) org-deadline-string "Plain:"))
  8860. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8861. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8862. (setq n (string-to-number (match-string 2 ts))
  8863. what (match-string 3 ts))
  8864. (if (equal what "w") (setq n (* n 7) what "d"))
  8865. ;; Preparation, see if we need to modify the start date for the change
  8866. (when (match-end 1)
  8867. (setq time (save-match-data (org-time-string-to-time ts)))
  8868. (cond
  8869. ((equal (match-string 1 ts) ".")
  8870. ;; Shift starting date to today
  8871. (org-timestamp-change
  8872. (- (time-to-days (current-time)) (time-to-days time))
  8873. 'day))
  8874. ((equal (match-string 1 ts) "+")
  8875. (while (or (= nshift 0)
  8876. (<= (time-to-days time) (time-to-days (current-time))))
  8877. (when (= (incf nshift) nshiftmax)
  8878. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8879. (error "Abort")))
  8880. (org-timestamp-change n (cdr (assoc what whata)))
  8881. (org-at-timestamp-p t)
  8882. (setq ts (match-string 1))
  8883. (setq time (save-match-data (org-time-string-to-time ts))))
  8884. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8885. ;; rematch, so that we have everything in place for the real shift
  8886. (org-at-timestamp-p t)
  8887. (setq ts (match-string 1))
  8888. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8889. (org-timestamp-change n (cdr (assoc what whata)))
  8890. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8891. (setq org-log-post-message msg)
  8892. (message "%s" msg))))
  8893. (defun org-show-todo-tree (arg)
  8894. "Make a compact tree which shows all headlines marked with TODO.
  8895. The tree will show the lines where the regexp matches, and all higher
  8896. headlines above the match.
  8897. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8898. With a numeric prefix N, construct a sparse tree for the Nth element
  8899. of `org-todo-keywords-1'."
  8900. (interactive "P")
  8901. (let ((case-fold-search nil)
  8902. (kwd-re
  8903. (cond ((null arg) org-not-done-regexp)
  8904. ((equal arg '(4))
  8905. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8906. (mapcar 'list org-todo-keywords-1))))
  8907. (concat "\\("
  8908. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8909. "\\)\\>")))
  8910. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8911. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8912. org-todo-keywords-1)))
  8913. (t (error "Invalid prefix argument: %s" arg)))))
  8914. (message "%d TODO entries found"
  8915. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8916. (defun org-deadline (&optional remove time)
  8917. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8918. With argument REMOVE, remove any deadline from the item.
  8919. When TIME is set, it should be an internal time specification, and the
  8920. scheduling will use the corresponding date."
  8921. (interactive "P")
  8922. (if remove
  8923. (progn
  8924. (org-remove-timestamp-with-keyword org-deadline-string)
  8925. (message "Item no longer has a deadline."))
  8926. (if (org-get-repeat)
  8927. (error "Cannot change deadline on task with repeater, please do that by hand")
  8928. (org-add-planning-info 'deadline time 'closed)
  8929. (message "Deadline on %s" org-last-inserted-timestamp))))
  8930. (defun org-schedule (&optional remove time)
  8931. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8932. With argument REMOVE, remove any scheduling date from the item.
  8933. When TIME is set, it should be an internal time specification, and the
  8934. scheduling will use the corresponding date."
  8935. (interactive "P")
  8936. (if remove
  8937. (progn
  8938. (org-remove-timestamp-with-keyword org-scheduled-string)
  8939. (message "Item is no longer scheduled."))
  8940. (if (org-get-repeat)
  8941. (error "Cannot reschedule task with repeater, please do that by hand")
  8942. (org-add-planning-info 'scheduled time 'closed)
  8943. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8944. (defun org-get-scheduled-time (pom &optional inherit)
  8945. "Get the scheduled time as a time tuple, of a format suitable
  8946. for calling org-schedule with, or if there is no scheduling,
  8947. returns nil."
  8948. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8949. (when time
  8950. (apply 'encode-time (org-parse-time-string time)))))
  8951. (defun org-get-deadline-time (pom &optional inherit)
  8952. "Get the deadine as a time tuple, of a format suitable for
  8953. calling org-deadlin with, or if there is no scheduling, returns
  8954. nil."
  8955. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8956. (when time
  8957. (apply 'encode-time (org-parse-time-string time)))))
  8958. (defun org-remove-timestamp-with-keyword (keyword)
  8959. "Remove all time stamps with KEYWORD in the current entry."
  8960. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8961. beg)
  8962. (save-excursion
  8963. (org-back-to-heading t)
  8964. (setq beg (point))
  8965. (org-end-of-subtree t t)
  8966. (while (re-search-backward re beg t)
  8967. (replace-match "")
  8968. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8969. (equal (char-before) ?\ ))
  8970. (backward-delete-char 1)
  8971. (if (string-match "^[ \t]*$" (buffer-substring
  8972. (point-at-bol) (point-at-eol)))
  8973. (delete-region (point-at-bol)
  8974. (min (point-max) (1+ (point-at-eol))))))))))
  8975. (defun org-add-planning-info (what &optional time &rest remove)
  8976. "Insert new timestamp with keyword in the line directly after the headline.
  8977. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8978. If non is given, the user is prompted for a date.
  8979. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8980. be removed."
  8981. (interactive)
  8982. (let (org-time-was-given org-end-time-was-given ts
  8983. end default-time default-input)
  8984. (catch 'exit
  8985. (when (and (not time) (memq what '(scheduled deadline)))
  8986. ;; Try to get a default date/time from existing timestamp
  8987. (save-excursion
  8988. (org-back-to-heading t)
  8989. (setq end (save-excursion (outline-next-heading) (point)))
  8990. (when (re-search-forward (if (eq what 'scheduled)
  8991. org-scheduled-time-regexp
  8992. org-deadline-time-regexp)
  8993. end t)
  8994. (setq ts (match-string 1)
  8995. default-time
  8996. (apply 'encode-time (org-parse-time-string ts))
  8997. default-input (and ts (org-get-compact-tod ts))))))
  8998. (when what
  8999. ;; If necessary, get the time from the user
  9000. (setq time (or time (org-read-date nil 'to-time nil nil
  9001. default-time default-input))))
  9002. (when (and org-insert-labeled-timestamps-at-point
  9003. (member what '(scheduled deadline)))
  9004. (insert
  9005. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9006. (org-insert-time-stamp time org-time-was-given
  9007. nil nil nil (list org-end-time-was-given))
  9008. (setq what nil))
  9009. (save-excursion
  9010. (save-restriction
  9011. (let (col list elt ts buffer-invisibility-spec)
  9012. (org-back-to-heading t)
  9013. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9014. (goto-char (match-end 1))
  9015. (setq col (current-column))
  9016. (goto-char (match-end 0))
  9017. (if (eobp) (insert "\n") (forward-char 1))
  9018. (when (and (not what)
  9019. (not (looking-at
  9020. (concat "[ \t]*"
  9021. org-keyword-time-not-clock-regexp))))
  9022. ;; Nothing to add, nothing to remove...... :-)
  9023. (throw 'exit nil))
  9024. (if (and (not (looking-at outline-regexp))
  9025. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9026. "[^\r\n]*"))
  9027. (not (equal (match-string 1) org-clock-string)))
  9028. (narrow-to-region (match-beginning 0) (match-end 0))
  9029. (insert-before-markers "\n")
  9030. (backward-char 1)
  9031. (narrow-to-region (point) (point))
  9032. (and org-adapt-indentation (org-indent-to-column col)))
  9033. ;; Check if we have to remove something.
  9034. (setq list (cons what remove))
  9035. (while list
  9036. (setq elt (pop list))
  9037. (goto-char (point-min))
  9038. (when (or (and (eq elt 'scheduled)
  9039. (re-search-forward org-scheduled-time-regexp nil t))
  9040. (and (eq elt 'deadline)
  9041. (re-search-forward org-deadline-time-regexp nil t))
  9042. (and (eq elt 'closed)
  9043. (re-search-forward org-closed-time-regexp nil t)))
  9044. (replace-match "")
  9045. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9046. (if (looking-at " +") (replace-match ""))))
  9047. (goto-char (point-max))
  9048. (when what
  9049. (insert
  9050. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9051. (cond ((eq what 'scheduled) org-scheduled-string)
  9052. ((eq what 'deadline) org-deadline-string)
  9053. ((eq what 'closed) org-closed-string))
  9054. " ")
  9055. (setq ts (org-insert-time-stamp
  9056. time
  9057. (or org-time-was-given
  9058. (and (eq what 'closed) org-log-done-with-time))
  9059. (eq what 'closed)
  9060. nil nil (list org-end-time-was-given)))
  9061. (end-of-line 1))
  9062. (goto-char (point-min))
  9063. (widen)
  9064. (if (and (looking-at "[ \t]+\n")
  9065. (equal (char-before) ?\n))
  9066. (delete-region (1- (point)) (point-at-eol)))
  9067. ts))))))
  9068. (defvar org-log-note-marker (make-marker))
  9069. (defvar org-log-note-purpose nil)
  9070. (defvar org-log-note-state nil)
  9071. (defvar org-log-note-previous-state nil)
  9072. (defvar org-log-note-how nil)
  9073. (defvar org-log-note-extra nil)
  9074. (defvar org-log-note-window-configuration nil)
  9075. (defvar org-log-note-return-to (make-marker))
  9076. (defvar org-log-post-message nil
  9077. "Message to be displayed after a log note has been stored.
  9078. The auto-repeater uses this.")
  9079. (defun org-add-note ()
  9080. "Add a note to the current entry.
  9081. This is done in the same way as adding a state change note."
  9082. (interactive)
  9083. (org-add-log-setup 'note nil nil 'findpos nil))
  9084. (defvar org-property-end-re)
  9085. (defun org-add-log-setup (&optional purpose state prev-state
  9086. findpos how &optional extra)
  9087. "Set up the post command hook to take a note.
  9088. If this is about to TODO state change, the new state is expected in STATE.
  9089. When FINDPOS is non-nil, find the correct position for the note in
  9090. the current entry. If not, assume that it can be inserted at point.
  9091. HOW is an indicator what kind of note should be created.
  9092. EXTRA is additional text that will be inserted into the notes buffer."
  9093. (let* ((org-log-into-drawer (org-log-into-drawer))
  9094. (drawer (cond ((stringp org-log-into-drawer)
  9095. org-log-into-drawer)
  9096. (org-log-into-drawer "LOGBOOK")
  9097. (t nil))))
  9098. (save-restriction
  9099. (save-excursion
  9100. (when findpos
  9101. (org-back-to-heading t)
  9102. (narrow-to-region (point) (save-excursion
  9103. (outline-next-heading) (point)))
  9104. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9105. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9106. "[^\r\n]*\\)?"))
  9107. (goto-char (match-end 0))
  9108. (cond
  9109. (drawer
  9110. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9111. nil t)
  9112. (progn
  9113. (goto-char (match-end 0))
  9114. (or org-log-states-order-reversed
  9115. (and (re-search-forward org-property-end-re nil t)
  9116. (goto-char (1- (match-beginning 0))))))
  9117. (insert "\n:" drawer ":\n:END:")
  9118. (beginning-of-line 0)
  9119. (org-indent-line-function)
  9120. (beginning-of-line 2)
  9121. (org-indent-line-function)
  9122. (end-of-line 0)))
  9123. ((and org-log-state-notes-insert-after-drawers
  9124. (save-excursion
  9125. (forward-line) (looking-at org-drawer-regexp)))
  9126. (forward-line)
  9127. (while (looking-at org-drawer-regexp)
  9128. (goto-char (match-end 0))
  9129. (re-search-forward org-property-end-re (point-max) t)
  9130. (forward-line))
  9131. (forward-line -1)))
  9132. (unless org-log-states-order-reversed
  9133. (and (= (char-after) ?\n) (forward-char 1))
  9134. (org-skip-over-state-notes)
  9135. (skip-chars-backward " \t\n\r")))
  9136. (move-marker org-log-note-marker (point))
  9137. (setq org-log-note-purpose purpose
  9138. org-log-note-state state
  9139. org-log-note-previous-state prev-state
  9140. org-log-note-how how
  9141. org-log-note-extra extra)
  9142. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9143. (defun org-skip-over-state-notes ()
  9144. "Skip past the list of State notes in an entry."
  9145. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9146. (while (looking-at "[ \t]*- State")
  9147. (condition-case nil
  9148. (org-next-item)
  9149. (error (org-end-of-item)))))
  9150. (defun org-add-log-note (&optional purpose)
  9151. "Pop up a window for taking a note, and add this note later at point."
  9152. (remove-hook 'post-command-hook 'org-add-log-note)
  9153. (setq org-log-note-window-configuration (current-window-configuration))
  9154. (delete-other-windows)
  9155. (move-marker org-log-note-return-to (point))
  9156. (switch-to-buffer (marker-buffer org-log-note-marker))
  9157. (goto-char org-log-note-marker)
  9158. (org-switch-to-buffer-other-window "*Org Note*")
  9159. (erase-buffer)
  9160. (if (memq org-log-note-how '(time state))
  9161. (let (current-prefix-arg) (org-store-log-note))
  9162. (let ((org-inhibit-startup t)) (org-mode))
  9163. (insert (format "# Insert note for %s.
  9164. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9165. (cond
  9166. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9167. ((eq org-log-note-purpose 'done) "closed todo item")
  9168. ((eq org-log-note-purpose 'state)
  9169. (format "state change from \"%s\" to \"%s\""
  9170. (or org-log-note-previous-state "")
  9171. (or org-log-note-state "")))
  9172. ((eq org-log-note-purpose 'note)
  9173. "this entry")
  9174. (t (error "This should not happen")))))
  9175. (if org-log-note-extra (insert org-log-note-extra))
  9176. (org-set-local 'org-finish-function 'org-store-log-note)))
  9177. (defvar org-note-abort nil) ; dynamically scoped
  9178. (defun org-store-log-note ()
  9179. "Finish taking a log note, and insert it to where it belongs."
  9180. (let ((txt (buffer-string))
  9181. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9182. lines ind)
  9183. (kill-buffer (current-buffer))
  9184. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9185. (setq txt (replace-match "" t t txt)))
  9186. (if (string-match "\\s-+\\'" txt)
  9187. (setq txt (replace-match "" t t txt)))
  9188. (setq lines (org-split-string txt "\n"))
  9189. (when (and note (string-match "\\S-" note))
  9190. (setq note
  9191. (org-replace-escapes
  9192. note
  9193. (list (cons "%u" (user-login-name))
  9194. (cons "%U" user-full-name)
  9195. (cons "%t" (format-time-string
  9196. (org-time-stamp-format 'long 'inactive)
  9197. (current-time)))
  9198. (cons "%s" (if org-log-note-state
  9199. (concat "\"" org-log-note-state "\"")
  9200. ""))
  9201. (cons "%S" (if org-log-note-previous-state
  9202. (concat "\"" org-log-note-previous-state "\"")
  9203. "\"\"")))))
  9204. (if lines (setq note (concat note " \\\\")))
  9205. (push note lines))
  9206. (when (or current-prefix-arg org-note-abort)
  9207. (when org-log-into-drawer
  9208. (org-remove-empty-drawer-at
  9209. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9210. org-log-note-marker))
  9211. (setq lines nil))
  9212. (when lines
  9213. (save-excursion
  9214. (set-buffer (marker-buffer org-log-note-marker))
  9215. (save-excursion
  9216. (goto-char org-log-note-marker)
  9217. (move-marker org-log-note-marker nil)
  9218. (end-of-line 1)
  9219. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9220. (insert "- " (pop lines))
  9221. (org-indent-line-function)
  9222. (beginning-of-line 1)
  9223. (looking-at "[ \t]*")
  9224. (setq ind (concat (match-string 0) " "))
  9225. (end-of-line 1)
  9226. (while lines (insert "\n" ind (pop lines)))
  9227. (message "Note stored")
  9228. (org-back-to-heading t)
  9229. (org-cycle-hide-drawers 'children)))))
  9230. (set-window-configuration org-log-note-window-configuration)
  9231. (with-current-buffer (marker-buffer org-log-note-return-to)
  9232. (goto-char org-log-note-return-to))
  9233. (move-marker org-log-note-return-to nil)
  9234. (and org-log-post-message (message "%s" org-log-post-message)))
  9235. (defun org-remove-empty-drawer-at (drawer pos)
  9236. "Remove an emptyr DARWER drawer at position POS.
  9237. POS may also be a marker."
  9238. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9239. (save-excursion
  9240. (save-restriction
  9241. (widen)
  9242. (goto-char pos)
  9243. (if (org-in-regexp
  9244. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9245. (replace-match ""))))))
  9246. (defun org-sparse-tree (&optional arg)
  9247. "Create a sparse tree, prompt for the details.
  9248. This command can create sparse trees. You first need to select the type
  9249. of match used to create the tree:
  9250. t Show entries with a specific TODO keyword.
  9251. m Show entries selected by a tags/property match.
  9252. p Enter a property name and its value (both with completion on existing
  9253. names/values) and show entries with that property.
  9254. r Show entries matching a regular expression.
  9255. d Show deadlines due within `org-deadline-warning-days'.
  9256. b Show deadlines and scheduled items before a date.
  9257. a Show deadlines and scheduled items after a date."
  9258. (interactive "P")
  9259. (let (ans kwd value)
  9260. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9261. (setq ans (read-char-exclusive))
  9262. (cond
  9263. ((equal ans ?d)
  9264. (call-interactively 'org-check-deadlines))
  9265. ((equal ans ?b)
  9266. (call-interactively 'org-check-before-date))
  9267. ((equal ans ?a)
  9268. (call-interactively 'org-check-after-date))
  9269. ((equal ans ?t)
  9270. (org-show-todo-tree '(4)))
  9271. ((member ans '(?T ?m))
  9272. (call-interactively 'org-match-sparse-tree))
  9273. ((member ans '(?p ?P))
  9274. (setq kwd (org-ido-completing-read "Property: "
  9275. (mapcar 'list (org-buffer-property-keys))))
  9276. (setq value (org-ido-completing-read "Value: "
  9277. (mapcar 'list (org-property-values kwd))))
  9278. (unless (string-match "\\`{.*}\\'" value)
  9279. (setq value (concat "\"" value "\"")))
  9280. (org-match-sparse-tree arg (concat kwd "=" value)))
  9281. ((member ans '(?r ?R ?/))
  9282. (call-interactively 'org-occur))
  9283. (t (error "No such sparse tree command \"%c\"" ans)))))
  9284. (defvar org-occur-highlights nil
  9285. "List of overlays used for occur matches.")
  9286. (make-variable-buffer-local 'org-occur-highlights)
  9287. (defvar org-occur-parameters nil
  9288. "Parameters of the active org-occur calls.
  9289. This is a list, each call to org-occur pushes as cons cell,
  9290. containing the regular expression and the callback, onto the list.
  9291. The list can contain several entries if `org-occur' has been called
  9292. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9293. will only contain one set of parameters. When the highlights are
  9294. removed (for example with `C-c C-c', or with the next edit (depending
  9295. on `org-remove-highlights-with-change'), this variable is emptied
  9296. as well.")
  9297. (make-variable-buffer-local 'org-occur-parameters)
  9298. (defun org-occur (regexp &optional keep-previous callback)
  9299. "Make a compact tree which shows all matches of REGEXP.
  9300. The tree will show the lines where the regexp matches, and all higher
  9301. headlines above the match. It will also show the heading after the match,
  9302. to make sure editing the matching entry is easy.
  9303. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9304. call to `org-occur' will be kept, to allow stacking of calls to this
  9305. command.
  9306. If CALLBACK is non-nil, it is a function which is called to confirm
  9307. that the match should indeed be shown."
  9308. (interactive "sRegexp: \nP")
  9309. (when (equal regexp "")
  9310. (error "Regexp cannot be empty"))
  9311. (unless keep-previous
  9312. (org-remove-occur-highlights nil nil t))
  9313. (push (cons regexp callback) org-occur-parameters)
  9314. (let ((cnt 0))
  9315. (save-excursion
  9316. (goto-char (point-min))
  9317. (if (or (not keep-previous) ; do not want to keep
  9318. (not org-occur-highlights)) ; no previous matches
  9319. ;; hide everything
  9320. (org-overview))
  9321. (while (re-search-forward regexp nil t)
  9322. (when (or (not callback)
  9323. (save-match-data (funcall callback)))
  9324. (setq cnt (1+ cnt))
  9325. (when org-highlight-sparse-tree-matches
  9326. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9327. (org-show-context 'occur-tree))))
  9328. (when org-remove-highlights-with-change
  9329. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9330. nil 'local))
  9331. (unless org-sparse-tree-open-archived-trees
  9332. (org-hide-archived-subtrees (point-min) (point-max)))
  9333. (run-hooks 'org-occur-hook)
  9334. (if (interactive-p)
  9335. (message "%d match(es) for regexp %s" cnt regexp))
  9336. cnt))
  9337. (defun org-show-context (&optional key)
  9338. "Make sure point and context and visible.
  9339. How much context is shown depends upon the variables
  9340. `org-show-hierarchy-above', `org-show-following-heading'. and
  9341. `org-show-siblings'."
  9342. (let ((heading-p (org-on-heading-p t))
  9343. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9344. (following-p (org-get-alist-option org-show-following-heading key))
  9345. (entry-p (org-get-alist-option org-show-entry-below key))
  9346. (siblings-p (org-get-alist-option org-show-siblings key)))
  9347. (catch 'exit
  9348. ;; Show heading or entry text
  9349. (if (and heading-p (not entry-p))
  9350. (org-flag-heading nil) ; only show the heading
  9351. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9352. (org-show-hidden-entry))) ; show entire entry
  9353. (when following-p
  9354. ;; Show next sibling, or heading below text
  9355. (save-excursion
  9356. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9357. (org-flag-heading nil))))
  9358. (when siblings-p (org-show-siblings))
  9359. (when hierarchy-p
  9360. ;; show all higher headings, possibly with siblings
  9361. (save-excursion
  9362. (while (and (condition-case nil
  9363. (progn (org-up-heading-all 1) t)
  9364. (error nil))
  9365. (not (bobp)))
  9366. (org-flag-heading nil)
  9367. (when siblings-p (org-show-siblings))))))))
  9368. (defun org-reveal (&optional siblings)
  9369. "Show current entry, hierarchy above it, and the following headline.
  9370. This can be used to show a consistent set of context around locations
  9371. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9372. not t for the search context.
  9373. With optional argument SIBLINGS, on each level of the hierarchy all
  9374. siblings are shown. This repairs the tree structure to what it would
  9375. look like when opened with hierarchical calls to `org-cycle'."
  9376. (interactive "P")
  9377. (let ((org-show-hierarchy-above t)
  9378. (org-show-following-heading t)
  9379. (org-show-siblings (if siblings t org-show-siblings)))
  9380. (org-show-context nil)))
  9381. (defun org-highlight-new-match (beg end)
  9382. "Highlight from BEG to END and mark the highlight is an occur headline."
  9383. (let ((ov (org-make-overlay beg end)))
  9384. (org-overlay-put ov 'face 'secondary-selection)
  9385. (push ov org-occur-highlights)))
  9386. (defun org-remove-occur-highlights (&optional beg end noremove)
  9387. "Remove the occur highlights from the buffer.
  9388. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9389. from the `before-change-functions' in the current buffer."
  9390. (interactive)
  9391. (unless org-inhibit-highlight-removal
  9392. (mapc 'org-delete-overlay org-occur-highlights)
  9393. (setq org-occur-highlights nil)
  9394. (setq org-occur-parameters nil)
  9395. (unless noremove
  9396. (remove-hook 'before-change-functions
  9397. 'org-remove-occur-highlights 'local))))
  9398. ;;;; Priorities
  9399. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9400. "Regular expression matching the priority indicator.")
  9401. (defvar org-remove-priority-next-time nil)
  9402. (defun org-priority-up ()
  9403. "Increase the priority of the current item."
  9404. (interactive)
  9405. (org-priority 'up))
  9406. (defun org-priority-down ()
  9407. "Decrease the priority of the current item."
  9408. (interactive)
  9409. (org-priority 'down))
  9410. (defun org-priority (&optional action)
  9411. "Change the priority of an item by ARG.
  9412. ACTION can be `set', `up', `down', or a character."
  9413. (interactive)
  9414. (unless org-enable-priority-commands
  9415. (error "Priority commands are disabled"))
  9416. (setq action (or action 'set))
  9417. (let (current new news have remove)
  9418. (save-excursion
  9419. (org-back-to-heading t)
  9420. (if (looking-at org-priority-regexp)
  9421. (setq current (string-to-char (match-string 2))
  9422. have t)
  9423. (setq current org-default-priority))
  9424. (cond
  9425. ((or (eq action 'set)
  9426. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9427. (if (not (eq action 'set))
  9428. (setq new action)
  9429. (message "Priority %c-%c, SPC to remove: "
  9430. org-highest-priority org-lowest-priority)
  9431. (setq new (read-char-exclusive)))
  9432. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9433. (= (upcase org-lowest-priority) org-lowest-priority))
  9434. (setq new (upcase new)))
  9435. (cond ((equal new ?\ ) (setq remove t))
  9436. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9437. (error "Priority must be between `%c' and `%c'"
  9438. org-highest-priority org-lowest-priority))))
  9439. ((eq action 'up)
  9440. (if (and (not have) (eq last-command this-command))
  9441. (setq new org-lowest-priority)
  9442. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9443. org-default-priority (1- current)))))
  9444. ((eq action 'down)
  9445. (if (and (not have) (eq last-command this-command))
  9446. (setq new org-highest-priority)
  9447. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9448. org-default-priority (1+ current)))))
  9449. (t (error "Invalid action")))
  9450. (if (or (< (upcase new) org-highest-priority)
  9451. (> (upcase new) org-lowest-priority))
  9452. (setq remove t))
  9453. (setq news (format "%c" new))
  9454. (if have
  9455. (if remove
  9456. (replace-match "" t t nil 1)
  9457. (replace-match news t t nil 2))
  9458. (if remove
  9459. (error "No priority cookie found in line")
  9460. (let ((case-fold-search nil))
  9461. (looking-at org-todo-line-regexp))
  9462. (if (match-end 2)
  9463. (progn
  9464. (goto-char (match-end 2))
  9465. (insert " [#" news "]"))
  9466. (goto-char (match-beginning 3))
  9467. (insert "[#" news "] "))))
  9468. (org-preserve-lc (org-set-tags nil 'align)))
  9469. (if remove
  9470. (message "Priority removed")
  9471. (message "Priority of current item set to %s" news))))
  9472. (defun org-get-priority (s)
  9473. "Find priority cookie and return priority."
  9474. (save-match-data
  9475. (if (not (string-match org-priority-regexp s))
  9476. (* 1000 (- org-lowest-priority org-default-priority))
  9477. (* 1000 (- org-lowest-priority
  9478. (string-to-char (match-string 2 s)))))))
  9479. ;;;; Tags
  9480. (defvar org-agenda-archives-mode)
  9481. (defvar org-map-continue-from nil
  9482. "Position from where mapping should continue.
  9483. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9484. (defvar org-scanner-tags nil
  9485. "The current tag list while the tags scanner is running.")
  9486. (defvar org-trust-scanner-tags nil
  9487. "Should `org-get-tags-at' use the tags fro the scanner.
  9488. This is for internal dynamical scoping only.
  9489. When this is non-nil, the function `org-get-tags-at' will return the value
  9490. of `org-scanner-tags' instead of building the list by itself. This
  9491. can lead to large speed-ups when the tags scanner is used in a file with
  9492. many entries, and when the list of tags is retrieved, for example to
  9493. obtain a list of properties. Building the tags list for each entry in such
  9494. a file becomes an N^2 operation - but with this variable set, it scales
  9495. as N.")
  9496. (defun org-scan-tags (action matcher &optional todo-only)
  9497. "Scan headline tags with inheritance and produce output ACTION.
  9498. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9499. or `agenda' to produce an entry list for an agenda view. It can also be
  9500. a Lisp form or a function that should be called at each matched headline, in
  9501. this case the return value is a list of all return values from these calls.
  9502. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9503. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9504. only lines with a TODO keyword are included in the output."
  9505. (require 'org-agenda)
  9506. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9507. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9508. (org-re
  9509. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9510. (props (list 'face 'default
  9511. 'done-face 'org-agenda-done
  9512. 'undone-face 'default
  9513. 'mouse-face 'highlight
  9514. 'org-not-done-regexp org-not-done-regexp
  9515. 'org-todo-regexp org-todo-regexp
  9516. 'keymap org-agenda-keymap
  9517. 'help-echo
  9518. (format "mouse-2 or RET jump to org file %s"
  9519. (abbreviate-file-name
  9520. (or (buffer-file-name (buffer-base-buffer))
  9521. (buffer-name (buffer-base-buffer)))))))
  9522. (case-fold-search nil)
  9523. (org-map-continue-from nil)
  9524. lspos tags tags-list
  9525. (tags-alist (list (cons 0 org-file-tags)))
  9526. (llast 0) rtn rtn1 level category i txt
  9527. todo marker entry priority)
  9528. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9529. (setq action (list 'lambda nil action)))
  9530. (save-excursion
  9531. (goto-char (point-min))
  9532. (when (eq action 'sparse-tree)
  9533. (org-overview)
  9534. (org-remove-occur-highlights))
  9535. (while (re-search-forward re nil t)
  9536. (catch :skip
  9537. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9538. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9539. (goto-char (setq lspos (match-beginning 0)))
  9540. (setq level (org-reduced-level (funcall outline-level))
  9541. category (org-get-category))
  9542. (setq i llast llast level)
  9543. ;; remove tag lists from same and sublevels
  9544. (while (>= i level)
  9545. (when (setq entry (assoc i tags-alist))
  9546. (setq tags-alist (delete entry tags-alist)))
  9547. (setq i (1- i)))
  9548. ;; add the next tags
  9549. (when tags
  9550. (setq tags (org-split-string tags ":")
  9551. tags-alist
  9552. (cons (cons level tags) tags-alist)))
  9553. ;; compile tags for current headline
  9554. (setq tags-list
  9555. (if org-use-tag-inheritance
  9556. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9557. tags)
  9558. org-scanner-tags tags-list)
  9559. (when org-use-tag-inheritance
  9560. (setcdr (car tags-alist)
  9561. (mapcar (lambda (x)
  9562. (setq x (copy-sequence x))
  9563. (org-add-prop-inherited x))
  9564. (cdar tags-alist))))
  9565. (when (and tags org-use-tag-inheritance
  9566. (or (not (eq t org-use-tag-inheritance))
  9567. org-tags-exclude-from-inheritance))
  9568. ;; selective inheritance, remove uninherited ones
  9569. (setcdr (car tags-alist)
  9570. (org-remove-uniherited-tags (cdar tags-alist))))
  9571. (when (and (or (not todo-only)
  9572. (and (member todo org-not-done-keywords)
  9573. (or (not org-agenda-tags-todo-honor-ignore-options)
  9574. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9575. (let ((case-fold-search t)) (eval matcher))
  9576. (or
  9577. (not (member org-archive-tag tags-list))
  9578. ;; we have an archive tag, should we use this anyway?
  9579. (or (not org-agenda-skip-archived-trees)
  9580. (and (eq action 'agenda) org-agenda-archives-mode))))
  9581. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9582. ;; select this headline
  9583. (cond
  9584. ((eq action 'sparse-tree)
  9585. (and org-highlight-sparse-tree-matches
  9586. (org-get-heading) (match-end 0)
  9587. (org-highlight-new-match
  9588. (match-beginning 0) (match-beginning 1)))
  9589. (org-show-context 'tags-tree))
  9590. ((eq action 'agenda)
  9591. (setq txt (org-format-agenda-item
  9592. ""
  9593. (concat
  9594. (if (eq org-tags-match-list-sublevels 'indented)
  9595. (make-string (1- level) ?.) "")
  9596. (org-get-heading))
  9597. category
  9598. tags-list
  9599. )
  9600. priority (org-get-priority txt))
  9601. (goto-char lspos)
  9602. (setq marker (org-agenda-new-marker))
  9603. (org-add-props txt props
  9604. 'org-marker marker 'org-hd-marker marker 'org-category category
  9605. 'todo-state todo
  9606. 'priority priority 'type "tagsmatch")
  9607. (push txt rtn))
  9608. ((functionp action)
  9609. (setq org-map-continue-from nil)
  9610. (save-excursion
  9611. (setq rtn1 (funcall action))
  9612. (push rtn1 rtn)))
  9613. (t (error "Invalid action")))
  9614. ;; if we are to skip sublevels, jump to end of subtree
  9615. (unless org-tags-match-list-sublevels
  9616. (org-end-of-subtree t)
  9617. (backward-char 1))))
  9618. ;; Get the correct position from where to continue
  9619. (if org-map-continue-from
  9620. (goto-char org-map-continue-from)
  9621. (and (= (point) lspos) (end-of-line 1)))))
  9622. (when (and (eq action 'sparse-tree)
  9623. (not org-sparse-tree-open-archived-trees))
  9624. (org-hide-archived-subtrees (point-min) (point-max)))
  9625. (nreverse rtn)))
  9626. (defun org-remove-uniherited-tags (tags)
  9627. "Remove all tags that are not inherited from the list TAGS."
  9628. (cond
  9629. ((eq org-use-tag-inheritance t)
  9630. (if org-tags-exclude-from-inheritance
  9631. (org-delete-all org-tags-exclude-from-inheritance tags)
  9632. tags))
  9633. ((not org-use-tag-inheritance) nil)
  9634. ((stringp org-use-tag-inheritance)
  9635. (delq nil (mapcar
  9636. (lambda (x)
  9637. (if (and (string-match org-use-tag-inheritance x)
  9638. (not (member x org-tags-exclude-from-inheritance)))
  9639. x nil))
  9640. tags)))
  9641. ((listp org-use-tag-inheritance)
  9642. (delq nil (mapcar
  9643. (lambda (x)
  9644. (if (member x org-use-tag-inheritance) x nil))
  9645. tags)))))
  9646. (defvar todo-only) ;; dynamically scoped
  9647. (defun org-match-sparse-tree (&optional todo-only match)
  9648. "Create a sparse tree according to tags string MATCH.
  9649. MATCH can contain positive and negative selection of tags, like
  9650. \"+WORK+URGENT-WITHBOSS\".
  9651. If optional argument TODO-ONLY is non-nil, only select lines that are
  9652. also TODO lines."
  9653. (interactive "P")
  9654. (org-prepare-agenda-buffers (list (current-buffer)))
  9655. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9656. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9657. (defvar org-cached-props nil)
  9658. (defun org-cached-entry-get (pom property)
  9659. (if (or (eq t org-use-property-inheritance)
  9660. (and (stringp org-use-property-inheritance)
  9661. (string-match org-use-property-inheritance property))
  9662. (and (listp org-use-property-inheritance)
  9663. (member property org-use-property-inheritance)))
  9664. ;; Caching is not possible, check it directly
  9665. (org-entry-get pom property 'inherit)
  9666. ;; Get all properties, so that we can do complicated checks easily
  9667. (cdr (assoc property (or org-cached-props
  9668. (setq org-cached-props
  9669. (org-entry-properties pom)))))))
  9670. (defun org-global-tags-completion-table (&optional files)
  9671. "Return the list of all tags in all agenda buffer/files."
  9672. (save-excursion
  9673. (org-uniquify
  9674. (delq nil
  9675. (apply 'append
  9676. (mapcar
  9677. (lambda (file)
  9678. (set-buffer (find-file-noselect file))
  9679. (append (org-get-buffer-tags)
  9680. (mapcar (lambda (x) (if (stringp (car-safe x))
  9681. (list (car-safe x)) nil))
  9682. org-tag-alist)))
  9683. (if (and files (car files))
  9684. files
  9685. (org-agenda-files))))))))
  9686. (defun org-make-tags-matcher (match)
  9687. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9688. ;; todo-only is scoped dynamically into this function, and the function
  9689. ;; may change it if the matcher asks for it.
  9690. (unless match
  9691. ;; Get a new match request, with completion
  9692. (let ((org-last-tags-completion-table
  9693. (org-global-tags-completion-table)))
  9694. (setq match (org-completing-read-no-ido
  9695. "Match: " 'org-tags-completion-function nil nil nil
  9696. 'org-tags-history))))
  9697. ;; Parse the string and create a lisp form
  9698. (let ((match0 match)
  9699. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9700. minus tag mm
  9701. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9702. orterms term orlist re-p str-p level-p level-op time-p
  9703. prop-p pn pv po cat-p gv rest)
  9704. (if (string-match "/+" match)
  9705. ;; match contains also a todo-matching request
  9706. (progn
  9707. (setq tagsmatch (substring match 0 (match-beginning 0))
  9708. todomatch (substring match (match-end 0)))
  9709. (if (string-match "^!" todomatch)
  9710. (setq todo-only t todomatch (substring todomatch 1)))
  9711. (if (string-match "^\\s-*$" todomatch)
  9712. (setq todomatch nil)))
  9713. ;; only matching tags
  9714. (setq tagsmatch match todomatch nil))
  9715. ;; Make the tags matcher
  9716. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9717. (setq tagsmatcher t)
  9718. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9719. (while (setq term (pop orterms))
  9720. (while (and (equal (substring term -1) "\\") orterms)
  9721. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9722. (while (string-match re term)
  9723. (setq rest (substring term (match-end 0))
  9724. minus (and (match-end 1)
  9725. (equal (match-string 1 term) "-"))
  9726. tag (match-string 2 term)
  9727. re-p (equal (string-to-char tag) ?{)
  9728. level-p (match-end 4)
  9729. prop-p (match-end 5)
  9730. mm (cond
  9731. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9732. (level-p
  9733. (setq level-op (org-op-to-function (match-string 3 term)))
  9734. `(,level-op level ,(string-to-number
  9735. (match-string 4 term))))
  9736. (prop-p
  9737. (setq pn (match-string 5 term)
  9738. po (match-string 6 term)
  9739. pv (match-string 7 term)
  9740. cat-p (equal pn "CATEGORY")
  9741. re-p (equal (string-to-char pv) ?{)
  9742. str-p (equal (string-to-char pv) ?\")
  9743. time-p (save-match-data
  9744. (string-match "^\"[[<].*[]>]\"$" pv))
  9745. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9746. (if time-p (setq pv (org-matcher-time pv)))
  9747. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9748. (cond
  9749. ((equal pn "CATEGORY")
  9750. (setq gv '(get-text-property (point) 'org-category)))
  9751. ((equal pn "TODO")
  9752. (setq gv 'todo))
  9753. (t
  9754. (setq gv `(org-cached-entry-get nil ,pn))))
  9755. (if re-p
  9756. (if (eq po 'org<>)
  9757. `(not (string-match ,pv (or ,gv "")))
  9758. `(string-match ,pv (or ,gv "")))
  9759. (if str-p
  9760. `(,po (or ,gv "") ,pv)
  9761. `(,po (string-to-number (or ,gv ""))
  9762. ,(string-to-number pv) ))))
  9763. (t `(member ,tag tags-list)))
  9764. mm (if minus (list 'not mm) mm)
  9765. term rest)
  9766. (push mm tagsmatcher))
  9767. (push (if (> (length tagsmatcher) 1)
  9768. (cons 'and tagsmatcher)
  9769. (car tagsmatcher))
  9770. orlist)
  9771. (setq tagsmatcher nil))
  9772. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9773. (setq tagsmatcher
  9774. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9775. ;; Make the todo matcher
  9776. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9777. (setq todomatcher t)
  9778. (setq orterms (org-split-string todomatch "|") orlist nil)
  9779. (while (setq term (pop orterms))
  9780. (while (string-match re term)
  9781. (setq minus (and (match-end 1)
  9782. (equal (match-string 1 term) "-"))
  9783. kwd (match-string 2 term)
  9784. re-p (equal (string-to-char kwd) ?{)
  9785. term (substring term (match-end 0))
  9786. mm (if re-p
  9787. `(string-match ,(substring kwd 1 -1) todo)
  9788. (list 'equal 'todo kwd))
  9789. mm (if minus (list 'not mm) mm))
  9790. (push mm todomatcher))
  9791. (push (if (> (length todomatcher) 1)
  9792. (cons 'and todomatcher)
  9793. (car todomatcher))
  9794. orlist)
  9795. (setq todomatcher nil))
  9796. (setq todomatcher (if (> (length orlist) 1)
  9797. (cons 'or orlist) (car orlist))))
  9798. ;; Return the string and lisp forms of the matcher
  9799. (setq matcher (if todomatcher
  9800. (list 'and tagsmatcher todomatcher)
  9801. tagsmatcher))
  9802. (cons match0 matcher)))
  9803. (defun org-op-to-function (op &optional stringp)
  9804. "Turn an operator into the appropriate function."
  9805. (setq op
  9806. (cond
  9807. ((equal op "<" ) '(< string< org-time<))
  9808. ((equal op ">" ) '(> org-string> org-time>))
  9809. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9810. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9811. ((member op '("=" "==")) '(= string= org-time=))
  9812. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9813. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9814. (defun org<> (a b) (not (= a b)))
  9815. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9816. (defun org-string>= (a b) (not (string< a b)))
  9817. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9818. (defun org-string<> (a b) (not (string= a b)))
  9819. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9820. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9821. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9822. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9823. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9824. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9825. (defun org-2ft (s)
  9826. "Convert S to a floating point time.
  9827. If S is already a number, just return it. If it is a string, parse
  9828. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9829. (cond
  9830. ((numberp s) s)
  9831. ((stringp s)
  9832. (condition-case nil
  9833. (float-time (apply 'encode-time (org-parse-time-string s)))
  9834. (error 0.)))
  9835. (t 0.)))
  9836. (defun org-time-today ()
  9837. "Time in seconds today at 0:00.
  9838. Returns the float number of seconds since the beginning of the
  9839. epoch to the beginning of today (00:00)."
  9840. (float-time (apply 'encode-time
  9841. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9842. (defun org-matcher-time (s)
  9843. "Interpret a time comparison value."
  9844. (save-match-data
  9845. (cond
  9846. ((string= s "<now>") (float-time))
  9847. ((string= s "<today>") (org-time-today))
  9848. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9849. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9850. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9851. (+ (org-time-today)
  9852. (* (string-to-number (match-string 1 s))
  9853. (cdr (assoc (match-string 2 s)
  9854. '(("d" . 86400.0) ("w" . 604800.0)
  9855. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9856. (t (org-2ft s)))))
  9857. (defun org-match-any-p (re list)
  9858. "Does re match any element of list?"
  9859. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9860. (delq nil list))
  9861. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9862. (defvar org-tags-overlay (org-make-overlay 1 1))
  9863. (org-detach-overlay org-tags-overlay)
  9864. (defun org-get-local-tags-at (&optional pos)
  9865. "Get a list of tags defined in the current headline."
  9866. (org-get-tags-at pos 'local))
  9867. (defun org-get-local-tags ()
  9868. "Get a list of tags defined in the current headline."
  9869. (org-get-tags-at nil 'local))
  9870. (defun org-get-tags-at (&optional pos local)
  9871. "Get a list of all headline tags applicable at POS.
  9872. POS defaults to point. If tags are inherited, the list contains
  9873. the targets in the same sequence as the headlines appear, i.e.
  9874. the tags of the current headline come last.
  9875. When LOCAL is non-nil, only return tags from the current headline,
  9876. ignore inherited ones."
  9877. (interactive)
  9878. (if (and org-trust-scanner-tags
  9879. (or (not pos) (equal pos (point)))
  9880. (not local))
  9881. org-scanner-tags
  9882. (let (tags ltags lastpos parent)
  9883. (save-excursion
  9884. (save-restriction
  9885. (widen)
  9886. (goto-char (or pos (point)))
  9887. (save-match-data
  9888. (catch 'done
  9889. (condition-case nil
  9890. (progn
  9891. (org-back-to-heading t)
  9892. (while (not (equal lastpos (point)))
  9893. (setq lastpos (point))
  9894. (when (looking-at
  9895. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9896. (setq ltags (org-split-string
  9897. (org-match-string-no-properties 1) ":"))
  9898. (when parent
  9899. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9900. (setq tags (append
  9901. (if parent
  9902. (org-remove-uniherited-tags ltags)
  9903. ltags)
  9904. tags)))
  9905. (or org-use-tag-inheritance (throw 'done t))
  9906. (if local (throw 'done t))
  9907. (or (org-up-heading-safe) (error nil))
  9908. (setq parent t)))
  9909. (error nil)))))
  9910. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9911. (defun org-add-prop-inherited (s)
  9912. (add-text-properties 0 (length s) '(inherited t) s)
  9913. s)
  9914. (defun org-toggle-tag (tag &optional onoff)
  9915. "Toggle the tag TAG for the current line.
  9916. If ONOFF is `on' or `off', don't toggle but set to this state."
  9917. (let (res current)
  9918. (save-excursion
  9919. (org-back-to-heading t)
  9920. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9921. (point-at-eol) t)
  9922. (progn
  9923. (setq current (match-string 1))
  9924. (replace-match ""))
  9925. (setq current ""))
  9926. (setq current (nreverse (org-split-string current ":")))
  9927. (cond
  9928. ((eq onoff 'on)
  9929. (setq res t)
  9930. (or (member tag current) (push tag current)))
  9931. ((eq onoff 'off)
  9932. (or (not (member tag current)) (setq current (delete tag current))))
  9933. (t (if (member tag current)
  9934. (setq current (delete tag current))
  9935. (setq res t)
  9936. (push tag current))))
  9937. (end-of-line 1)
  9938. (if current
  9939. (progn
  9940. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9941. (org-set-tags nil t))
  9942. (delete-horizontal-space))
  9943. (run-hooks 'org-after-tags-change-hook))
  9944. res))
  9945. (defun org-align-tags-here (to-col)
  9946. ;; Assumes that this is a headline
  9947. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9948. (beginning-of-line 1)
  9949. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9950. (< pos (match-beginning 2)))
  9951. (progn
  9952. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9953. (goto-char (match-beginning 1))
  9954. (insert " ")
  9955. (delete-region (point) (1+ (match-beginning 2)))
  9956. (setq ncol (max (1+ (current-column))
  9957. (1+ col)
  9958. (if (> to-col 0)
  9959. to-col
  9960. (- (abs to-col) tags-l))))
  9961. (setq p (point))
  9962. (insert (make-string (- ncol (current-column)) ?\ ))
  9963. (setq ncol (current-column))
  9964. (when indent-tabs-mode (tabify p (point-at-eol)))
  9965. (org-move-to-column (min ncol col) t))
  9966. (goto-char pos))))
  9967. (defun org-set-tags-command (&optional arg just-align)
  9968. "Call the set-tags command for the current entry."
  9969. (interactive "P")
  9970. (if (org-on-heading-p)
  9971. (org-set-tags arg just-align)
  9972. (save-excursion
  9973. (org-back-to-heading t)
  9974. (org-set-tags arg just-align))))
  9975. (defun org-set-tags (&optional arg just-align)
  9976. "Set the tags for the current headline.
  9977. With prefix ARG, realign all tags in headings in the current buffer."
  9978. (interactive "P")
  9979. (let* ((re (concat "^" outline-regexp))
  9980. (current (org-get-tags-string))
  9981. (col (current-column))
  9982. (org-setting-tags t)
  9983. table current-tags inherited-tags ; computed below when needed
  9984. tags p0 c0 c1 rpl)
  9985. (if arg
  9986. (save-excursion
  9987. (goto-char (point-min))
  9988. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9989. (while (re-search-forward re nil t)
  9990. (org-set-tags nil t)
  9991. (end-of-line 1)))
  9992. (message "All tags realigned to column %d" org-tags-column))
  9993. (if just-align
  9994. (setq tags current)
  9995. ;; Get a new set of tags from the user
  9996. (save-excursion
  9997. (setq table (append org-tag-persistent-alist
  9998. (or org-tag-alist (org-get-buffer-tags)))
  9999. org-last-tags-completion-table table
  10000. current-tags (org-split-string current ":")
  10001. inherited-tags (nreverse
  10002. (nthcdr (length current-tags)
  10003. (nreverse (org-get-tags-at))))
  10004. tags
  10005. (if (or (eq t org-use-fast-tag-selection)
  10006. (and org-use-fast-tag-selection
  10007. (delq nil (mapcar 'cdr table))))
  10008. (org-fast-tag-selection
  10009. current-tags inherited-tags table
  10010. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10011. (let ((org-add-colon-after-tag-completion t))
  10012. (org-trim
  10013. (org-without-partial-completion
  10014. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10015. nil nil current 'org-tags-history)))))))
  10016. (while (string-match "[-+&]+" tags)
  10017. ;; No boolean logic, just a list
  10018. (setq tags (replace-match ":" t t tags))))
  10019. (if org-tags-sort-function
  10020. (setq tags (mapconcat 'identity
  10021. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10022. org-tags-sort-function) ":")))
  10023. (if (string-match "\\`[\t ]*\\'" tags)
  10024. (setq tags "")
  10025. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10026. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10027. ;; Insert new tags at the correct column
  10028. (beginning-of-line 1)
  10029. (cond
  10030. ((and (equal current "") (equal tags "")))
  10031. ((re-search-forward
  10032. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10033. (point-at-eol) t)
  10034. (if (equal tags "")
  10035. (setq rpl "")
  10036. (goto-char (match-beginning 0))
  10037. (setq c0 (current-column) p0 (point)
  10038. c1 (max (1+ c0) (if (> org-tags-column 0)
  10039. org-tags-column
  10040. (- (- org-tags-column) (length tags))))
  10041. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10042. (replace-match rpl t t)
  10043. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10044. tags)
  10045. (t (error "Tags alignment failed")))
  10046. (org-move-to-column col)
  10047. (unless just-align
  10048. (run-hooks 'org-after-tags-change-hook)))))
  10049. (defun org-change-tag-in-region (beg end tag off)
  10050. "Add or remove TAG for each entry in the region.
  10051. This works in the agenda, and also in an org-mode buffer."
  10052. (interactive
  10053. (list (region-beginning) (region-end)
  10054. (let ((org-last-tags-completion-table
  10055. (if (org-mode-p)
  10056. (org-get-buffer-tags)
  10057. (org-global-tags-completion-table))))
  10058. (org-ido-completing-read
  10059. "Tag: " 'org-tags-completion-function nil nil nil
  10060. 'org-tags-history))
  10061. (progn
  10062. (message "[s]et or [r]emove? ")
  10063. (equal (read-char-exclusive) ?r))))
  10064. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10065. (let ((agendap (equal major-mode 'org-agenda-mode))
  10066. l1 l2 m buf pos newhead (cnt 0))
  10067. (goto-char end)
  10068. (setq l2 (1- (org-current-line)))
  10069. (goto-char beg)
  10070. (setq l1 (org-current-line))
  10071. (loop for l from l1 to l2 do
  10072. (goto-line l)
  10073. (setq m (get-text-property (point) 'org-hd-marker))
  10074. (when (or (and (org-mode-p) (org-on-heading-p))
  10075. (and agendap m))
  10076. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10077. pos (if agendap m (point)))
  10078. (with-current-buffer buf
  10079. (save-excursion
  10080. (save-restriction
  10081. (goto-char pos)
  10082. (setq cnt (1+ cnt))
  10083. (org-toggle-tag tag (if off 'off 'on))
  10084. (setq newhead (org-get-heading)))))
  10085. (and agendap (org-agenda-change-all-lines newhead m))))
  10086. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10087. (defun org-tags-completion-function (string predicate &optional flag)
  10088. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10089. (confirm (lambda (x) (stringp (car x)))))
  10090. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10091. (setq s1 (match-string 1 string)
  10092. s2 (match-string 2 string))
  10093. (setq s1 "" s2 string))
  10094. (cond
  10095. ((eq flag nil)
  10096. ;; try completion
  10097. (setq rtn (try-completion s2 ctable confirm))
  10098. (if (stringp rtn)
  10099. (setq rtn
  10100. (concat s1 s2 (substring rtn (length s2))
  10101. (if (and org-add-colon-after-tag-completion
  10102. (assoc rtn ctable))
  10103. ":" ""))))
  10104. rtn)
  10105. ((eq flag t)
  10106. ;; all-completions
  10107. (all-completions s2 ctable confirm)
  10108. )
  10109. ((eq flag 'lambda)
  10110. ;; exact match?
  10111. (assoc s2 ctable)))
  10112. ))
  10113. (defun org-fast-tag-insert (kwd tags face &optional end)
  10114. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10115. (insert (format "%-12s" (concat kwd ":"))
  10116. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10117. (or end "")))
  10118. (defun org-fast-tag-show-exit (flag)
  10119. (save-excursion
  10120. (goto-line 3)
  10121. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10122. (replace-match ""))
  10123. (when flag
  10124. (end-of-line 1)
  10125. (org-move-to-column (- (window-width) 19) t)
  10126. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10127. (defun org-set-current-tags-overlay (current prefix)
  10128. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10129. (if (featurep 'xemacs)
  10130. (org-overlay-display org-tags-overlay (concat prefix s)
  10131. 'secondary-selection)
  10132. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10133. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10134. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10135. "Fast tag selection with single keys.
  10136. CURRENT is the current list of tags in the headline, INHERITED is the
  10137. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10138. possibly with grouping information. TODO-TABLE is a similar table with
  10139. TODO keywords, should these have keys assigned to them.
  10140. If the keys are nil, a-z are automatically assigned.
  10141. Returns the new tags string, or nil to not change the current settings."
  10142. (let* ((fulltable (append table todo-table))
  10143. (maxlen (apply 'max (mapcar
  10144. (lambda (x)
  10145. (if (stringp (car x)) (string-width (car x)) 0))
  10146. fulltable)))
  10147. (buf (current-buffer))
  10148. (expert (eq org-fast-tag-selection-single-key 'expert))
  10149. (buffer-tags nil)
  10150. (fwidth (+ maxlen 3 1 3))
  10151. (ncol (/ (- (window-width) 4) fwidth))
  10152. (i-face 'org-done)
  10153. (c-face 'org-todo)
  10154. tg cnt e c char c1 c2 ntable tbl rtn
  10155. ov-start ov-end ov-prefix
  10156. (exit-after-next org-fast-tag-selection-single-key)
  10157. (done-keywords org-done-keywords)
  10158. groups ingroup)
  10159. (save-excursion
  10160. (beginning-of-line 1)
  10161. (if (looking-at
  10162. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10163. (setq ov-start (match-beginning 1)
  10164. ov-end (match-end 1)
  10165. ov-prefix "")
  10166. (setq ov-start (1- (point-at-eol))
  10167. ov-end (1+ ov-start))
  10168. (skip-chars-forward "^\n\r")
  10169. (setq ov-prefix
  10170. (concat
  10171. (buffer-substring (1- (point)) (point))
  10172. (if (> (current-column) org-tags-column)
  10173. " "
  10174. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10175. (org-move-overlay org-tags-overlay ov-start ov-end)
  10176. (save-window-excursion
  10177. (if expert
  10178. (set-buffer (get-buffer-create " *Org tags*"))
  10179. (delete-other-windows)
  10180. (split-window-vertically)
  10181. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10182. (erase-buffer)
  10183. (org-set-local 'org-done-keywords done-keywords)
  10184. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10185. (org-fast-tag-insert "Current" current c-face "\n\n")
  10186. (org-fast-tag-show-exit exit-after-next)
  10187. (org-set-current-tags-overlay current ov-prefix)
  10188. (setq tbl fulltable char ?a cnt 0)
  10189. (while (setq e (pop tbl))
  10190. (cond
  10191. ((equal e '(:startgroup))
  10192. (push '() groups) (setq ingroup t)
  10193. (when (not (= cnt 0))
  10194. (setq cnt 0)
  10195. (insert "\n"))
  10196. (insert "{ "))
  10197. ((equal e '(:endgroup))
  10198. (setq ingroup nil cnt 0)
  10199. (insert "}\n"))
  10200. ((equal e '(:newline))
  10201. (when (not (= cnt 0))
  10202. (setq cnt 0)
  10203. (insert "\n")
  10204. (setq e (car tbl))
  10205. (while (equal (car tbl) '(:newline))
  10206. (insert "\n")
  10207. (setq tbl (cdr tbl)))))
  10208. (t
  10209. (setq tg (car e) c2 nil)
  10210. (if (cdr e)
  10211. (setq c (cdr e))
  10212. ;; automatically assign a character.
  10213. (setq c1 (string-to-char
  10214. (downcase (substring
  10215. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10216. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10217. (while (or (rassoc char ntable) (rassoc char table))
  10218. (setq char (1+ char)))
  10219. (setq c2 c1))
  10220. (setq c (or c2 char)))
  10221. (if ingroup (push tg (car groups)))
  10222. (setq tg (org-add-props tg nil 'face
  10223. (cond
  10224. ((not (assoc tg table))
  10225. (org-get-todo-face tg))
  10226. ((member tg current) c-face)
  10227. ((member tg inherited) i-face)
  10228. (t nil))))
  10229. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10230. (insert "[" c "] " tg (make-string
  10231. (- fwidth 4 (length tg)) ?\ ))
  10232. (push (cons tg c) ntable)
  10233. (when (= (setq cnt (1+ cnt)) ncol)
  10234. (insert "\n")
  10235. (if ingroup (insert " "))
  10236. (setq cnt 0)))))
  10237. (setq ntable (nreverse ntable))
  10238. (insert "\n")
  10239. (goto-char (point-min))
  10240. (if (not expert) (org-fit-window-to-buffer))
  10241. (setq rtn
  10242. (catch 'exit
  10243. (while t
  10244. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10245. (if groups " [!] no groups" " [!]groups")
  10246. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10247. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10248. (cond
  10249. ((= c ?\r) (throw 'exit t))
  10250. ((= c ?!)
  10251. (setq groups (not groups))
  10252. (goto-char (point-min))
  10253. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10254. ((= c ?\C-c)
  10255. (if (not expert)
  10256. (org-fast-tag-show-exit
  10257. (setq exit-after-next (not exit-after-next)))
  10258. (setq expert nil)
  10259. (delete-other-windows)
  10260. (split-window-vertically)
  10261. (org-switch-to-buffer-other-window " *Org tags*")
  10262. (org-fit-window-to-buffer)))
  10263. ((or (= c ?\C-g)
  10264. (and (= c ?q) (not (rassoc c ntable))))
  10265. (org-detach-overlay org-tags-overlay)
  10266. (setq quit-flag t))
  10267. ((= c ?\ )
  10268. (setq current nil)
  10269. (if exit-after-next (setq exit-after-next 'now)))
  10270. ((= c ?\t)
  10271. (condition-case nil
  10272. (setq tg (org-ido-completing-read
  10273. "Tag: "
  10274. (or buffer-tags
  10275. (with-current-buffer buf
  10276. (org-get-buffer-tags)))))
  10277. (quit (setq tg "")))
  10278. (when (string-match "\\S-" tg)
  10279. (add-to-list 'buffer-tags (list tg))
  10280. (if (member tg current)
  10281. (setq current (delete tg current))
  10282. (push tg current)))
  10283. (if exit-after-next (setq exit-after-next 'now)))
  10284. ((setq e (rassoc c todo-table) tg (car e))
  10285. (with-current-buffer buf
  10286. (save-excursion (org-todo tg)))
  10287. (if exit-after-next (setq exit-after-next 'now)))
  10288. ((setq e (rassoc c ntable) tg (car e))
  10289. (if (member tg current)
  10290. (setq current (delete tg current))
  10291. (loop for g in groups do
  10292. (if (member tg g)
  10293. (mapc (lambda (x)
  10294. (setq current (delete x current)))
  10295. g)))
  10296. (push tg current))
  10297. (if exit-after-next (setq exit-after-next 'now))))
  10298. ;; Create a sorted list
  10299. (setq current
  10300. (sort current
  10301. (lambda (a b)
  10302. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10303. (if (eq exit-after-next 'now) (throw 'exit t))
  10304. (goto-char (point-min))
  10305. (beginning-of-line 2)
  10306. (delete-region (point) (point-at-eol))
  10307. (org-fast-tag-insert "Current" current c-face)
  10308. (org-set-current-tags-overlay current ov-prefix)
  10309. (while (re-search-forward
  10310. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10311. (setq tg (match-string 1))
  10312. (add-text-properties
  10313. (match-beginning 1) (match-end 1)
  10314. (list 'face
  10315. (cond
  10316. ((member tg current) c-face)
  10317. ((member tg inherited) i-face)
  10318. (t (get-text-property (match-beginning 1) 'face))))))
  10319. (goto-char (point-min)))))
  10320. (org-detach-overlay org-tags-overlay)
  10321. (if rtn
  10322. (mapconcat 'identity current ":")
  10323. nil))))
  10324. (defun org-get-tags-string ()
  10325. "Get the TAGS string in the current headline."
  10326. (unless (org-on-heading-p t)
  10327. (error "Not on a heading"))
  10328. (save-excursion
  10329. (beginning-of-line 1)
  10330. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10331. (org-match-string-no-properties 1)
  10332. "")))
  10333. (defun org-get-tags ()
  10334. "Get the list of tags specified in the current headline."
  10335. (org-split-string (org-get-tags-string) ":"))
  10336. (defun org-get-buffer-tags ()
  10337. "Get a table of all tags used in the buffer, for completion."
  10338. (let (tags)
  10339. (save-excursion
  10340. (goto-char (point-min))
  10341. (while (re-search-forward
  10342. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10343. (when (equal (char-after (point-at-bol 0)) ?*)
  10344. (mapc (lambda (x) (add-to-list 'tags x))
  10345. (org-split-string (org-match-string-no-properties 1) ":")))))
  10346. (mapcar 'list tags)))
  10347. ;;;; The mapping API
  10348. ;;;###autoload
  10349. (defun org-map-entries (func &optional match scope &rest skip)
  10350. "Call FUNC at each headline selected by MATCH in SCOPE.
  10351. FUNC is a function or a lisp form. The function will be called without
  10352. arguments, with the cursor positioned at the beginning of the headline.
  10353. The return values of all calls to the function will be collected and
  10354. returned as a list.
  10355. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10356. does not need to preserve point. After evaluation, the cursor will be
  10357. moved to the end of the line (presumably of the headline of the
  10358. processed entry) and search continues from there. Under some
  10359. circumstances, this may not produce the wanted results. For example,
  10360. if you have removed (e.g. archived) the current (sub)tree it could
  10361. mean that the next entry will be skipped entirely. In such cases, you
  10362. can specify the position from where search should continue by making
  10363. FUNC set the variable `org-map-continue-from' to the desired buffer
  10364. position.
  10365. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10366. Only headlines that are matched by this query will be considered during
  10367. the iteration. When MATCH is nil or t, all headlines will be
  10368. visited by the iteration.
  10369. SCOPE determines the scope of this command. It can be any of:
  10370. nil The current buffer, respecting the restriction if any
  10371. tree The subtree started with the entry at point
  10372. file The current buffer, without restriction
  10373. file-with-archives
  10374. The current buffer, and any archives associated with it
  10375. agenda All agenda files
  10376. agenda-with-archives
  10377. All agenda files with any archive files associated with them
  10378. \(file1 file2 ...)
  10379. If this is a list, all files in the list will be scanned
  10380. The remaining args are treated as settings for the skipping facilities of
  10381. the scanner. The following items can be given here:
  10382. archive skip trees with the archive tag.
  10383. comment skip trees with the COMMENT keyword
  10384. function or Emacs Lisp form:
  10385. will be used as value for `org-agenda-skip-function', so whenever
  10386. the the function returns t, FUNC will not be called for that
  10387. entry and search will continue from the point where the
  10388. function leaves it.
  10389. If your function needs to retrieve the tags including inherited tags
  10390. at the *current* entry, you can use the value of the variable
  10391. `org-scanner-tags' which will be much faster than getting the value
  10392. with `org-get-tags-at'. If your function gets properties with
  10393. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10394. to t around the call to `org-entry-properties' to get the same speedup.
  10395. Note that if your function moves around to retrieve tags and properties at
  10396. a *different* entry, you cannot use these techniques."
  10397. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10398. (org-agenda-skip-archived-trees (memq 'archive skip))
  10399. (org-agenda-skip-comment-trees (memq 'comment skip))
  10400. (org-agenda-skip-function
  10401. (car (org-delete-all '(comment archive) skip)))
  10402. (org-tags-match-list-sublevels t)
  10403. matcher file res
  10404. org-todo-keywords-for-agenda
  10405. org-done-keywords-for-agenda
  10406. org-todo-keyword-alist-for-agenda
  10407. org-tag-alist-for-agenda)
  10408. (cond
  10409. ((eq match t) (setq matcher t))
  10410. ((eq match nil) (setq matcher t))
  10411. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10412. (save-excursion
  10413. (save-restriction
  10414. (when (eq scope 'tree)
  10415. (org-back-to-heading t)
  10416. (org-narrow-to-subtree)
  10417. (setq scope nil))
  10418. (if (not scope)
  10419. (progn
  10420. (org-prepare-agenda-buffers
  10421. (list (buffer-file-name (current-buffer))))
  10422. (setq res (org-scan-tags func matcher)))
  10423. ;; Get the right scope
  10424. (cond
  10425. ((and scope (listp scope) (symbolp (car scope)))
  10426. (setq scope (eval scope)))
  10427. ((eq scope 'agenda)
  10428. (setq scope (org-agenda-files t)))
  10429. ((eq scope 'agenda-with-archives)
  10430. (setq scope (org-agenda-files t))
  10431. (setq scope (org-add-archive-files scope)))
  10432. ((eq scope 'file)
  10433. (setq scope (list (buffer-file-name))))
  10434. ((eq scope 'file-with-archives)
  10435. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10436. (org-prepare-agenda-buffers scope)
  10437. (while (setq file (pop scope))
  10438. (with-current-buffer (org-find-base-buffer-visiting file)
  10439. (save-excursion
  10440. (save-restriction
  10441. (widen)
  10442. (goto-char (point-min))
  10443. (setq res (append res (org-scan-tags func matcher))))))))))
  10444. res))
  10445. ;;;; Properties
  10446. ;;; Setting and retrieving properties
  10447. (defconst org-special-properties
  10448. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10449. "TIMESTAMP" "TIMESTAMP_IA")
  10450. "The special properties valid in Org-mode.
  10451. These are properties that are not defined in the property drawer,
  10452. but in some other way.")
  10453. (defconst org-default-properties
  10454. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10455. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10456. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10457. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10458. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10459. "CLOCK_MODELINE_TOTAL")
  10460. "Some properties that are used by Org-mode for various purposes.
  10461. Being in this list makes sure that they are offered for completion.")
  10462. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10463. "Regular expression matching the first line of a property drawer.")
  10464. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10465. "Regular expression matching the first line of a property drawer.")
  10466. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10467. "Regular expression matching the first line of a property drawer.")
  10468. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10469. "Regular expression matching the first line of a property drawer.")
  10470. (defconst org-property-drawer-re
  10471. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10472. org-property-end-re "\\)\n?")
  10473. "Matches an entire property drawer.")
  10474. (defconst org-clock-drawer-re
  10475. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10476. org-property-end-re "\\)\n?")
  10477. "Matches an entire clock drawer.")
  10478. (defun org-property-action ()
  10479. "Do an action on properties."
  10480. (interactive)
  10481. (let (c)
  10482. (org-at-property-p)
  10483. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10484. (setq c (read-char-exclusive))
  10485. (cond
  10486. ((equal c ?s)
  10487. (call-interactively 'org-set-property))
  10488. ((equal c ?d)
  10489. (call-interactively 'org-delete-property))
  10490. ((equal c ?D)
  10491. (call-interactively 'org-delete-property-globally))
  10492. ((equal c ?c)
  10493. (call-interactively 'org-compute-property-at-point))
  10494. (t (error "No such property action %c" c)))))
  10495. (defun org-at-property-p ()
  10496. "Is the cursor in a property line?"
  10497. ;; FIXME: Does not check if we are actually in the drawer.
  10498. ;; FIXME: also returns true on any drawers.....
  10499. ;; This is used by C-c C-c for property action.
  10500. (save-excursion
  10501. (beginning-of-line 1)
  10502. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10503. (defun org-get-property-block (&optional beg end force)
  10504. "Return the (beg . end) range of the body of the property drawer.
  10505. BEG and END can be beginning and end of subtree, if not given
  10506. they will be found.
  10507. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10508. (catch 'exit
  10509. (save-excursion
  10510. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10511. (end (or end (progn (outline-next-heading) (point)))))
  10512. (goto-char beg)
  10513. (if (re-search-forward org-property-start-re end t)
  10514. (setq beg (1+ (match-end 0)))
  10515. (if force
  10516. (save-excursion
  10517. (org-insert-property-drawer)
  10518. (setq end (progn (outline-next-heading) (point))))
  10519. (throw 'exit nil))
  10520. (goto-char beg)
  10521. (if (re-search-forward org-property-start-re end t)
  10522. (setq beg (1+ (match-end 0)))))
  10523. (if (re-search-forward org-property-end-re end t)
  10524. (setq end (match-beginning 0))
  10525. (or force (throw 'exit nil))
  10526. (goto-char beg)
  10527. (setq end beg)
  10528. (org-indent-line-function)
  10529. (insert ":END:\n"))
  10530. (cons beg end)))))
  10531. (defun org-entry-properties (&optional pom which)
  10532. "Get all properties of the entry at point-or-marker POM.
  10533. This includes the TODO keyword, the tags, time strings for deadline,
  10534. scheduled, and clocking, and any additional properties defined in the
  10535. entry. The return value is an alist, keys may occur multiple times
  10536. if the property key was used several times.
  10537. POM may also be nil, in which case the current entry is used.
  10538. If WHICH is nil or `all', get all properties. If WHICH is
  10539. `special' or `standard', only get that subclass."
  10540. (setq which (or which 'all))
  10541. (org-with-point-at pom
  10542. (let ((clockstr (substring org-clock-string 0 -1))
  10543. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10544. beg end range props sum-props key value string clocksum)
  10545. (save-excursion
  10546. (when (condition-case nil
  10547. (and (org-mode-p) (org-back-to-heading t))
  10548. (error nil))
  10549. (setq beg (point))
  10550. (setq sum-props (get-text-property (point) 'org-summaries))
  10551. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10552. (outline-next-heading)
  10553. (setq end (point))
  10554. (when (memq which '(all special))
  10555. ;; Get the special properties, like TODO and tags
  10556. (goto-char beg)
  10557. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10558. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10559. (when (looking-at org-priority-regexp)
  10560. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10561. (when (and (setq value (org-get-tags-string))
  10562. (string-match "\\S-" value))
  10563. (push (cons "TAGS" value) props))
  10564. (when (setq value (org-get-tags-at))
  10565. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10566. props))
  10567. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10568. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10569. string (if (equal key clockstr)
  10570. (org-no-properties
  10571. (org-trim
  10572. (buffer-substring
  10573. (match-beginning 3) (goto-char (point-at-eol)))))
  10574. (substring (org-match-string-no-properties 3) 1 -1)))
  10575. (unless key
  10576. (if (= (char-after (match-beginning 3)) ?\[)
  10577. (setq key "TIMESTAMP_IA")
  10578. (setq key "TIMESTAMP")))
  10579. (when (or (equal key clockstr) (not (assoc key props)))
  10580. (push (cons key string) props)))
  10581. )
  10582. (when (memq which '(all standard))
  10583. ;; Get the standard properties, like :PROP: ...
  10584. (setq range (org-get-property-block beg end))
  10585. (when range
  10586. (goto-char (car range))
  10587. (while (re-search-forward
  10588. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10589. (cdr range) t)
  10590. (setq key (org-match-string-no-properties 1)
  10591. value (org-trim (or (org-match-string-no-properties 2) "")))
  10592. (unless (member key excluded)
  10593. (push (cons key (or value "")) props)))))
  10594. (if clocksum
  10595. (push (cons "CLOCKSUM"
  10596. (org-columns-number-to-string (/ (float clocksum) 60.)
  10597. 'add_times))
  10598. props))
  10599. (unless (assoc "CATEGORY" props)
  10600. (setq value (or (org-get-category)
  10601. (progn (org-refresh-category-properties)
  10602. (org-get-category))))
  10603. (push (cons "CATEGORY" value) props))
  10604. (append sum-props (nreverse props)))))))
  10605. (defun org-entry-get (pom property &optional inherit)
  10606. "Get value of PROPERTY for entry at point-or-marker POM.
  10607. If INHERIT is non-nil and the entry does not have the property,
  10608. then also check higher levels of the hierarchy.
  10609. If INHERIT is the symbol `selective', use inheritance only if the setting
  10610. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10611. If the property is present but empty, the return value is the empty string.
  10612. If the property is not present at all, nil is returned."
  10613. (org-with-point-at pom
  10614. (if (and inherit (if (eq inherit 'selective)
  10615. (org-property-inherit-p property)
  10616. t))
  10617. (org-entry-get-with-inheritance property)
  10618. (if (member property org-special-properties)
  10619. ;; We need a special property. Use brute force, get all properties.
  10620. (cdr (assoc property (org-entry-properties nil 'special)))
  10621. (let ((range (org-get-property-block)))
  10622. (if (and range
  10623. (goto-char (car range))
  10624. (re-search-forward
  10625. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10626. (cdr range) t))
  10627. ;; Found the property, return it.
  10628. (if (match-end 1)
  10629. (org-match-string-no-properties 1)
  10630. "")))))))
  10631. (defun org-property-or-variable-value (var &optional inherit)
  10632. "Check if there is a property fixing the value of VAR.
  10633. If yes, return this value. If not, return the current value of the variable."
  10634. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10635. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10636. (read prop)
  10637. (symbol-value var))))
  10638. (defun org-entry-delete (pom property)
  10639. "Delete the property PROPERTY from entry at point-or-marker POM."
  10640. (org-with-point-at pom
  10641. (if (member property org-special-properties)
  10642. nil ; cannot delete these properties.
  10643. (let ((range (org-get-property-block)))
  10644. (if (and range
  10645. (goto-char (car range))
  10646. (re-search-forward
  10647. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10648. (cdr range) t))
  10649. (progn
  10650. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10651. t)
  10652. nil)))))
  10653. ;; Multi-values properties are properties that contain multiple values
  10654. ;; These values are assumed to be single words, separated by whitespace.
  10655. (defun org-entry-add-to-multivalued-property (pom property value)
  10656. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10657. (let* ((old (org-entry-get pom property))
  10658. (values (and old (org-split-string old "[ \t]"))))
  10659. (setq value (org-entry-protect-space value))
  10660. (unless (member value values)
  10661. (setq values (cons value values))
  10662. (org-entry-put pom property
  10663. (mapconcat 'identity values " ")))))
  10664. (defun org-entry-remove-from-multivalued-property (pom property value)
  10665. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10666. (let* ((old (org-entry-get pom property))
  10667. (values (and old (org-split-string old "[ \t]"))))
  10668. (setq value (org-entry-protect-space value))
  10669. (when (member value values)
  10670. (setq values (delete value values))
  10671. (org-entry-put pom property
  10672. (mapconcat 'identity values " ")))))
  10673. (defun org-entry-member-in-multivalued-property (pom property value)
  10674. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10675. (let* ((old (org-entry-get pom property))
  10676. (values (and old (org-split-string old "[ \t]"))))
  10677. (setq value (org-entry-protect-space value))
  10678. (member value values)))
  10679. (defun org-entry-get-multivalued-property (pom property)
  10680. "Return a list of values in a multivalued property."
  10681. (let* ((value (org-entry-get pom property))
  10682. (values (and value (org-split-string value "[ \t]"))))
  10683. (mapcar 'org-entry-restore-space values)))
  10684. (defun org-entry-put-multivalued-property (pom property &rest values)
  10685. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10686. VALUES should be a list of strings. Spaces will be protected."
  10687. (org-entry-put pom property
  10688. (mapconcat 'org-entry-protect-space values " "))
  10689. (let* ((value (org-entry-get pom property))
  10690. (values (and value (org-split-string value "[ \t]"))))
  10691. (mapcar 'org-entry-restore-space values)))
  10692. (defun org-entry-protect-space (s)
  10693. "Protect spaces and newline in string S."
  10694. (while (string-match " " s)
  10695. (setq s (replace-match "%20" t t s)))
  10696. (while (string-match "\n" s)
  10697. (setq s (replace-match "%0A" t t s)))
  10698. s)
  10699. (defun org-entry-restore-space (s)
  10700. "Restore spaces and newline in string S."
  10701. (while (string-match "%20" s)
  10702. (setq s (replace-match " " t t s)))
  10703. (while (string-match "%0A" s)
  10704. (setq s (replace-match "\n" t t s)))
  10705. s)
  10706. (defvar org-entry-property-inherited-from (make-marker)
  10707. "Marker pointing to the entry from where a property was inherited.
  10708. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10709. location of the entry where the inheritance search matched. If there was
  10710. no match, the marker will point nowhere.
  10711. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10712. is set.")
  10713. (defun org-entry-get-with-inheritance (property)
  10714. "Get entry property, and search higher levels if not present."
  10715. (move-marker org-entry-property-inherited-from nil)
  10716. (let (tmp)
  10717. (save-excursion
  10718. (save-restriction
  10719. (widen)
  10720. (catch 'ex
  10721. (while t
  10722. (when (setq tmp (org-entry-get nil property))
  10723. (org-back-to-heading t)
  10724. (move-marker org-entry-property-inherited-from (point))
  10725. (throw 'ex tmp))
  10726. (or (org-up-heading-safe) (throw 'ex nil)))))
  10727. (or tmp
  10728. (cdr (assoc property org-file-properties))
  10729. (cdr (assoc property org-global-properties))
  10730. (cdr (assoc property org-global-properties-fixed))))))
  10731. (defun org-entry-put (pom property value)
  10732. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10733. (org-with-point-at pom
  10734. (org-back-to-heading t)
  10735. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10736. range)
  10737. (cond
  10738. ((equal property "TODO")
  10739. (when (and (stringp value) (string-match "\\S-" value)
  10740. (not (member value org-todo-keywords-1)))
  10741. (error "\"%s\" is not a valid TODO state" value))
  10742. (if (or (not value)
  10743. (not (string-match "\\S-" value)))
  10744. (setq value 'none))
  10745. (org-todo value)
  10746. (org-set-tags nil 'align))
  10747. ((equal property "PRIORITY")
  10748. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10749. (string-to-char value) ?\ ))
  10750. (org-set-tags nil 'align))
  10751. ((equal property "SCHEDULED")
  10752. (if (re-search-forward org-scheduled-time-regexp end t)
  10753. (cond
  10754. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10755. ((eq value 'later) (org-timestamp-change 1 'day))
  10756. (t (call-interactively 'org-schedule)))
  10757. (call-interactively 'org-schedule)))
  10758. ((equal property "DEADLINE")
  10759. (if (re-search-forward org-deadline-time-regexp end t)
  10760. (cond
  10761. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10762. ((eq value 'later) (org-timestamp-change 1 'day))
  10763. (t (call-interactively 'org-deadline)))
  10764. (call-interactively 'org-deadline)))
  10765. ((member property org-special-properties)
  10766. (error "The %s property can not yet be set with `org-entry-put'"
  10767. property))
  10768. (t ; a non-special property
  10769. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10770. (setq range (org-get-property-block beg end 'force))
  10771. (goto-char (car range))
  10772. (if (re-search-forward
  10773. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10774. (progn
  10775. (delete-region (match-beginning 1) (match-end 1))
  10776. (goto-char (match-beginning 1)))
  10777. (goto-char (cdr range))
  10778. (insert "\n")
  10779. (backward-char 1)
  10780. (org-indent-line-function)
  10781. (insert ":" property ":"))
  10782. (and value (insert " " value))
  10783. (org-indent-line-function)))))))
  10784. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10785. "Get all property keys in the current buffer.
  10786. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10787. like tags and TODO state.
  10788. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10789. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10790. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10791. formats in the current buffer."
  10792. (let (rtn range cfmt s p)
  10793. (save-excursion
  10794. (save-restriction
  10795. (widen)
  10796. (goto-char (point-min))
  10797. (while (re-search-forward org-property-start-re nil t)
  10798. (setq range (org-get-property-block))
  10799. (goto-char (car range))
  10800. (while (re-search-forward
  10801. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10802. (cdr range) t)
  10803. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10804. (outline-next-heading))))
  10805. (when include-specials
  10806. (setq rtn (append org-special-properties rtn)))
  10807. (when include-defaults
  10808. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10809. (add-to-list 'rtn org-effort-property))
  10810. (when include-columns
  10811. (save-excursion
  10812. (save-restriction
  10813. (widen)
  10814. (goto-char (point-min))
  10815. (while (re-search-forward
  10816. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10817. nil t)
  10818. (setq cfmt (match-string 2) s 0)
  10819. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10820. cfmt s)
  10821. (setq s (match-end 0)
  10822. p (match-string 1 cfmt))
  10823. (unless (or (equal p "ITEM")
  10824. (member p org-special-properties))
  10825. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10826. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10827. (defun org-property-values (key)
  10828. "Return a list of all values of property KEY."
  10829. (save-excursion
  10830. (save-restriction
  10831. (widen)
  10832. (goto-char (point-min))
  10833. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10834. values)
  10835. (while (re-search-forward re nil t)
  10836. (add-to-list 'values (org-trim (match-string 1))))
  10837. (delete "" values)))))
  10838. (defun org-insert-property-drawer ()
  10839. "Insert a property drawer into the current entry."
  10840. (interactive)
  10841. (org-back-to-heading t)
  10842. (looking-at outline-regexp)
  10843. (let ((indent (if org-adapt-indentation
  10844. (- (match-end 0)(match-beginning 0))
  10845. 0))
  10846. (beg (point))
  10847. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10848. end hiddenp)
  10849. (outline-next-heading)
  10850. (setq end (point))
  10851. (goto-char beg)
  10852. (while (re-search-forward re end t))
  10853. (setq hiddenp (org-invisible-p))
  10854. (end-of-line 1)
  10855. (and (equal (char-after) ?\n) (forward-char 1))
  10856. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10857. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10858. ;; just skip this line
  10859. (beginning-of-line 2)
  10860. ;; Drawer start, find the end
  10861. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10862. (beginning-of-line 1)))
  10863. (org-skip-over-state-notes)
  10864. (skip-chars-backward " \t\n\r")
  10865. (if (eq (char-before) ?*) (forward-char 1))
  10866. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10867. (beginning-of-line 0)
  10868. (org-indent-to-column indent)
  10869. (beginning-of-line 2)
  10870. (org-indent-to-column indent)
  10871. (beginning-of-line 0)
  10872. (if hiddenp
  10873. (save-excursion
  10874. (org-back-to-heading t)
  10875. (hide-entry))
  10876. (org-flag-drawer t))))
  10877. (defun org-set-property (property value)
  10878. "In the current entry, set PROPERTY to VALUE.
  10879. When called interactively, this will prompt for a property name, offering
  10880. completion on existing and default properties. And then it will prompt
  10881. for a value, offering completion either on allowed values (via an inherited
  10882. xxx_ALL property) or on existing values in other instances of this property
  10883. in the current file."
  10884. (interactive
  10885. (let* ((completion-ignore-case t)
  10886. (keys (org-buffer-property-keys nil t t))
  10887. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10888. (prop (if (member prop0 keys)
  10889. prop0
  10890. (or (cdr (assoc (downcase prop0)
  10891. (mapcar (lambda (x) (cons (downcase x) x))
  10892. keys)))
  10893. prop0)))
  10894. (cur (org-entry-get nil prop))
  10895. (allowed (org-property-get-allowed-values nil prop 'table))
  10896. (existing (mapcar 'list (org-property-values prop)))
  10897. (val (if allowed
  10898. (org-completing-read "Value: " allowed nil 'req-match)
  10899. (let (org-completion-use-ido)
  10900. (org-completing-read
  10901. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10902. (concat "[" cur "]") "")
  10903. ": ")
  10904. existing nil nil "" nil cur)))))
  10905. (list prop (if (equal val "") cur val))))
  10906. (unless (equal (org-entry-get nil property) value)
  10907. (org-entry-put nil property value)))
  10908. (defun org-delete-property (property)
  10909. "In the current entry, delete PROPERTY."
  10910. (interactive
  10911. (let* ((completion-ignore-case t)
  10912. (prop (org-ido-completing-read
  10913. "Property: " (org-entry-properties nil 'standard))))
  10914. (list prop)))
  10915. (message "Property %s %s" property
  10916. (if (org-entry-delete nil property)
  10917. "deleted"
  10918. "was not present in the entry")))
  10919. (defun org-delete-property-globally (property)
  10920. "Remove PROPERTY globally, from all entries."
  10921. (interactive
  10922. (let* ((completion-ignore-case t)
  10923. (prop (org-ido-completing-read
  10924. "Globally remove property: "
  10925. (mapcar 'list (org-buffer-property-keys)))))
  10926. (list prop)))
  10927. (save-excursion
  10928. (save-restriction
  10929. (widen)
  10930. (goto-char (point-min))
  10931. (let ((cnt 0))
  10932. (while (re-search-forward
  10933. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10934. nil t)
  10935. (setq cnt (1+ cnt))
  10936. (replace-match ""))
  10937. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10938. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10939. (defun org-compute-property-at-point ()
  10940. "Compute the property at point.
  10941. This looks for an enclosing column format, extracts the operator and
  10942. then applies it to the property in the column format's scope."
  10943. (interactive)
  10944. (unless (org-at-property-p)
  10945. (error "Not at a property"))
  10946. (let ((prop (org-match-string-no-properties 2)))
  10947. (org-columns-get-format-and-top-level)
  10948. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10949. (error "No operator defined for property %s" prop))
  10950. (org-columns-compute prop)))
  10951. (defun org-property-get-allowed-values (pom property &optional table)
  10952. "Get allowed values for the property PROPERTY.
  10953. When TABLE is non-nil, return an alist that can directly be used for
  10954. completion."
  10955. (let (vals)
  10956. (cond
  10957. ((equal property "TODO")
  10958. (setq vals (org-with-point-at pom
  10959. (append org-todo-keywords-1 '("")))))
  10960. ((equal property "PRIORITY")
  10961. (let ((n org-lowest-priority))
  10962. (while (>= n org-highest-priority)
  10963. (push (char-to-string n) vals)
  10964. (setq n (1- n)))))
  10965. ((member property org-special-properties))
  10966. (t
  10967. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10968. (when (and vals (string-match "\\S-" vals))
  10969. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10970. (setq vals (mapcar (lambda (x)
  10971. (cond ((stringp x) x)
  10972. ((numberp x) (number-to-string x))
  10973. ((symbolp x) (symbol-name x))
  10974. (t "???")))
  10975. vals)))))
  10976. (if table (mapcar 'list vals) vals)))
  10977. (defun org-property-previous-allowed-value (&optional previous)
  10978. "Switch to the next allowed value for this property."
  10979. (interactive)
  10980. (org-property-next-allowed-value t))
  10981. (defun org-property-next-allowed-value (&optional previous)
  10982. "Switch to the next allowed value for this property."
  10983. (interactive)
  10984. (unless (org-at-property-p)
  10985. (error "Not at a property"))
  10986. (let* ((key (match-string 2))
  10987. (value (match-string 3))
  10988. (allowed (or (org-property-get-allowed-values (point) key)
  10989. (and (member value '("[ ]" "[-]" "[X]"))
  10990. '("[ ]" "[X]"))))
  10991. nval)
  10992. (unless allowed
  10993. (error "Allowed values for this property have not been defined"))
  10994. (if previous (setq allowed (reverse allowed)))
  10995. (if (member value allowed)
  10996. (setq nval (car (cdr (member value allowed)))))
  10997. (setq nval (or nval (car allowed)))
  10998. (if (equal nval value)
  10999. (error "Only one allowed value for this property"))
  11000. (org-at-property-p)
  11001. (replace-match (concat " :" key ": " nval) t t)
  11002. (org-indent-line-function)
  11003. (beginning-of-line 1)
  11004. (skip-chars-forward " \t")))
  11005. (defun org-find-entry-with-id (ident)
  11006. "Locate the entry that contains the ID property with exact value IDENT.
  11007. IDENT can be a string, a symbol or a number, this function will search for
  11008. the string representation of it.
  11009. Return the position where this entry starts, or nil if there is no such entry."
  11010. (interactive "sID: ")
  11011. (let ((id (cond
  11012. ((stringp ident) ident)
  11013. ((symbol-name ident) (symbol-name ident))
  11014. ((numberp ident) (number-to-string ident))
  11015. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11016. (case-fold-search nil))
  11017. (save-excursion
  11018. (save-restriction
  11019. (widen)
  11020. (goto-char (point-min))
  11021. (when (re-search-forward
  11022. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11023. nil t)
  11024. (org-back-to-heading t)
  11025. (point))))))
  11026. ;;;; Timestamps
  11027. (defvar org-last-changed-timestamp nil)
  11028. (defvar org-last-inserted-timestamp nil
  11029. "The last time stamp inserted with `org-insert-time-stamp'.")
  11030. (defvar org-time-was-given) ; dynamically scoped parameter
  11031. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11032. (defvar org-ts-what) ; dynamically scoped parameter
  11033. (defun org-time-stamp (arg &optional inactive)
  11034. "Prompt for a date/time and insert a time stamp.
  11035. If the user specifies a time like HH:MM, or if this command is called
  11036. with a prefix argument, the time stamp will contain date and time.
  11037. Otherwise, only the date will be included. All parts of a date not
  11038. specified by the user will be filled in from the current date/time.
  11039. So if you press just return without typing anything, the time stamp
  11040. will represent the current date/time. If there is already a timestamp
  11041. at the cursor, it will be modified."
  11042. (interactive "P")
  11043. (let* ((ts nil)
  11044. (default-time
  11045. ;; Default time is either today, or, when entering a range,
  11046. ;; the range start.
  11047. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11048. (save-excursion
  11049. (re-search-backward
  11050. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11051. (- (point) 20) t)))
  11052. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11053. (current-time)))
  11054. (default-input (and ts (org-get-compact-tod ts)))
  11055. org-time-was-given org-end-time-was-given time)
  11056. (cond
  11057. ((and (org-at-timestamp-p t)
  11058. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11059. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11060. (insert "--")
  11061. (setq time (let ((this-command this-command))
  11062. (org-read-date arg 'totime nil nil
  11063. default-time default-input)))
  11064. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11065. ((org-at-timestamp-p t)
  11066. (setq time (let ((this-command this-command))
  11067. (org-read-date arg 'totime nil nil default-time default-input)))
  11068. (when (org-at-timestamp-p t) ; just to get the match data
  11069. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11070. (replace-match "")
  11071. (setq org-last-changed-timestamp
  11072. (org-insert-time-stamp
  11073. time (or org-time-was-given arg)
  11074. inactive nil nil (list org-end-time-was-given))))
  11075. (message "Timestamp updated"))
  11076. (t
  11077. (setq time (let ((this-command this-command))
  11078. (org-read-date arg 'totime nil nil default-time default-input)))
  11079. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11080. nil nil (list org-end-time-was-given))))))
  11081. ;; FIXME: can we use this for something else, like computing time differences?
  11082. (defun org-get-compact-tod (s)
  11083. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11084. (let* ((t1 (match-string 1 s))
  11085. (h1 (string-to-number (match-string 2 s)))
  11086. (m1 (string-to-number (match-string 3 s)))
  11087. (t2 (and (match-end 4) (match-string 5 s)))
  11088. (h2 (and t2 (string-to-number (match-string 6 s))))
  11089. (m2 (and t2 (string-to-number (match-string 7 s))))
  11090. dh dm)
  11091. (if (not t2)
  11092. t1
  11093. (setq dh (- h2 h1) dm (- m2 m1))
  11094. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11095. (concat t1 "+" (number-to-string dh)
  11096. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11097. (defun org-time-stamp-inactive (&optional arg)
  11098. "Insert an inactive time stamp.
  11099. An inactive time stamp is enclosed in square brackets instead of angle
  11100. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11101. does not link to the calendar and cannot be changed with the S-cursor keys.
  11102. So these are more for recording a certain time/date."
  11103. (interactive "P")
  11104. (org-time-stamp arg 'inactive))
  11105. (defvar org-date-ovl (org-make-overlay 1 1))
  11106. (org-overlay-put org-date-ovl 'face 'org-warning)
  11107. (org-detach-overlay org-date-ovl)
  11108. (defvar org-ans1) ; dynamically scoped parameter
  11109. (defvar org-ans2) ; dynamically scoped parameter
  11110. (defvar org-plain-time-of-day-regexp) ; defined below
  11111. (defvar org-overriding-default-time nil) ; dynamically scoped
  11112. (defvar org-read-date-overlay nil)
  11113. (defvar org-dcst nil) ; dynamically scoped
  11114. (defvar org-read-date-history nil)
  11115. (defvar org-read-date-final-answer nil)
  11116. (defun org-read-date (&optional with-time to-time from-string prompt
  11117. default-time default-input)
  11118. "Read a date, possibly a time, and make things smooth for the user.
  11119. The prompt will suggest to enter an ISO date, but you can also enter anything
  11120. which will at least partially be understood by `parse-time-string'.
  11121. Unrecognized parts of the date will default to the current day, month, year,
  11122. hour and minute. If this command is called to replace a timestamp at point,
  11123. of to enter the second timestamp of a range, the default time is taken from the
  11124. existing stamp. For example,
  11125. 3-2-5 --> 2003-02-05
  11126. feb 15 --> currentyear-02-15
  11127. sep 12 9 --> 2009-09-12
  11128. 12:45 --> today 12:45
  11129. 22 sept 0:34 --> currentyear-09-22 0:34
  11130. 12 --> currentyear-currentmonth-12
  11131. Fri --> nearest Friday (today or later)
  11132. etc.
  11133. Furthermore you can specify a relative date by giving, as the *first* thing
  11134. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11135. change in days weeks, months, years.
  11136. With a single plus or minus, the date is relative to today. With a double
  11137. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11138. +4d --> four days from today
  11139. +4 --> same as above
  11140. +2w --> two weeks from today
  11141. ++5 --> five days from default date
  11142. The function understands only English month and weekday abbreviations,
  11143. but this can be configured with the variables `parse-time-months' and
  11144. `parse-time-weekdays'.
  11145. While prompting, a calendar is popped up - you can also select the
  11146. date with the mouse (button 1). The calendar shows a period of three
  11147. months. To scroll it to other months, use the keys `>' and `<'.
  11148. If you don't like the calendar, turn it off with
  11149. \(setq org-read-date-popup-calendar nil)
  11150. With optional argument TO-TIME, the date will immediately be converted
  11151. to an internal time.
  11152. With an optional argument WITH-TIME, the prompt will suggest to also
  11153. insert a time. Note that when WITH-TIME is not set, you can still
  11154. enter a time, and this function will inform the calling routine about
  11155. this change. The calling routine may then choose to change the format
  11156. used to insert the time stamp into the buffer to include the time.
  11157. With optional argument FROM-STRING, read from this string instead from
  11158. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11159. the time/date that is used for everything that is not specified by the
  11160. user."
  11161. (require 'parse-time)
  11162. (let* ((org-time-stamp-rounding-minutes
  11163. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11164. (org-dcst org-display-custom-times)
  11165. (ct (org-current-time))
  11166. (def (or org-overriding-default-time default-time ct))
  11167. (defdecode (decode-time def))
  11168. (dummy (progn
  11169. (when (< (nth 2 defdecode) org-extend-today-until)
  11170. (setcar (nthcdr 2 defdecode) -1)
  11171. (setcar (nthcdr 1 defdecode) 59)
  11172. (setq def (apply 'encode-time defdecode)
  11173. defdecode (decode-time def)))))
  11174. (calendar-frame-setup nil)
  11175. (calendar-move-hook nil)
  11176. (calendar-view-diary-initially-flag nil)
  11177. (view-diary-entries-initially nil)
  11178. (calendar-view-holidays-initially-flag nil)
  11179. (view-calendar-holidays-initially nil)
  11180. (timestr (format-time-string
  11181. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11182. (prompt (concat (if prompt (concat prompt " ") "")
  11183. (format "Date+time [%s]: " timestr)))
  11184. ans (org-ans0 "") org-ans1 org-ans2 final)
  11185. (cond
  11186. (from-string (setq ans from-string))
  11187. (org-read-date-popup-calendar
  11188. (save-excursion
  11189. (save-window-excursion
  11190. (calendar)
  11191. (calendar-forward-day (- (time-to-days def)
  11192. (calendar-absolute-from-gregorian
  11193. (calendar-current-date))))
  11194. (org-eval-in-calendar nil t)
  11195. (let* ((old-map (current-local-map))
  11196. (map (copy-keymap calendar-mode-map))
  11197. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11198. (org-defkey map (kbd "RET") 'org-calendar-select)
  11199. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11200. 'org-calendar-select-mouse)
  11201. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11202. 'org-calendar-select-mouse)
  11203. (org-defkey minibuffer-local-map [(meta shift left)]
  11204. (lambda () (interactive)
  11205. (org-eval-in-calendar '(calendar-backward-month 1))))
  11206. (org-defkey minibuffer-local-map [(meta shift right)]
  11207. (lambda () (interactive)
  11208. (org-eval-in-calendar '(calendar-forward-month 1))))
  11209. (org-defkey minibuffer-local-map [(meta shift up)]
  11210. (lambda () (interactive)
  11211. (org-eval-in-calendar '(calendar-backward-year 1))))
  11212. (org-defkey minibuffer-local-map [(meta shift down)]
  11213. (lambda () (interactive)
  11214. (org-eval-in-calendar '(calendar-forward-year 1))))
  11215. (org-defkey minibuffer-local-map [?\e (shift left)]
  11216. (lambda () (interactive)
  11217. (org-eval-in-calendar '(calendar-backward-month 1))))
  11218. (org-defkey minibuffer-local-map [?\e (shift right)]
  11219. (lambda () (interactive)
  11220. (org-eval-in-calendar '(calendar-forward-month 1))))
  11221. (org-defkey minibuffer-local-map [?\e (shift up)]
  11222. (lambda () (interactive)
  11223. (org-eval-in-calendar '(calendar-backward-year 1))))
  11224. (org-defkey minibuffer-local-map [?\e (shift down)]
  11225. (lambda () (interactive)
  11226. (org-eval-in-calendar '(calendar-forward-year 1))))
  11227. (org-defkey minibuffer-local-map [(shift up)]
  11228. (lambda () (interactive)
  11229. (org-eval-in-calendar '(calendar-backward-week 1))))
  11230. (org-defkey minibuffer-local-map [(shift down)]
  11231. (lambda () (interactive)
  11232. (org-eval-in-calendar '(calendar-forward-week 1))))
  11233. (org-defkey minibuffer-local-map [(shift left)]
  11234. (lambda () (interactive)
  11235. (org-eval-in-calendar '(calendar-backward-day 1))))
  11236. (org-defkey minibuffer-local-map [(shift right)]
  11237. (lambda () (interactive)
  11238. (org-eval-in-calendar '(calendar-forward-day 1))))
  11239. (org-defkey minibuffer-local-map ">"
  11240. (lambda () (interactive)
  11241. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11242. (org-defkey minibuffer-local-map "<"
  11243. (lambda () (interactive)
  11244. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11245. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11246. (unwind-protect
  11247. (progn
  11248. (use-local-map map)
  11249. (add-hook 'post-command-hook 'org-read-date-display)
  11250. (setq org-ans0 (read-string prompt default-input
  11251. 'org-read-date-history nil))
  11252. ;; org-ans0: from prompt
  11253. ;; org-ans1: from mouse click
  11254. ;; org-ans2: from calendar motion
  11255. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11256. (remove-hook 'post-command-hook 'org-read-date-display)
  11257. (use-local-map old-map)
  11258. (when org-read-date-overlay
  11259. (org-delete-overlay org-read-date-overlay)
  11260. (setq org-read-date-overlay nil)))))))
  11261. (t ; Naked prompt only
  11262. (unwind-protect
  11263. (setq ans (read-string prompt default-input
  11264. 'org-read-date-history timestr))
  11265. (when org-read-date-overlay
  11266. (org-delete-overlay org-read-date-overlay)
  11267. (setq org-read-date-overlay nil)))))
  11268. (setq final (org-read-date-analyze ans def defdecode))
  11269. (setq org-read-date-final-answer ans)
  11270. (if to-time
  11271. (apply 'encode-time final)
  11272. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11273. (format "%04d-%02d-%02d %02d:%02d"
  11274. (nth 5 final) (nth 4 final) (nth 3 final)
  11275. (nth 2 final) (nth 1 final))
  11276. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11277. (defvar def)
  11278. (defvar defdecode)
  11279. (defvar with-time)
  11280. (defun org-read-date-display ()
  11281. "Display the current date prompt interpretation in the minibuffer."
  11282. (when org-read-date-display-live
  11283. (when org-read-date-overlay
  11284. (org-delete-overlay org-read-date-overlay))
  11285. (let ((p (point)))
  11286. (end-of-line 1)
  11287. (while (not (equal (buffer-substring
  11288. (max (point-min) (- (point) 4)) (point))
  11289. " "))
  11290. (insert " "))
  11291. (goto-char p))
  11292. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11293. " " (or org-ans1 org-ans2)))
  11294. (org-end-time-was-given nil)
  11295. (f (org-read-date-analyze ans def defdecode))
  11296. (fmts (if org-dcst
  11297. org-time-stamp-custom-formats
  11298. org-time-stamp-formats))
  11299. (fmt (if (or with-time
  11300. (and (boundp 'org-time-was-given) org-time-was-given))
  11301. (cdr fmts)
  11302. (car fmts)))
  11303. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11304. (when (and org-end-time-was-given
  11305. (string-match org-plain-time-of-day-regexp txt))
  11306. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11307. org-end-time-was-given
  11308. (substring txt (match-end 0)))))
  11309. (setq org-read-date-overlay
  11310. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11311. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11312. (defun org-read-date-analyze (ans def defdecode)
  11313. "Analyse the combined answer of the date prompt."
  11314. ;; FIXME: cleanup and comment
  11315. (let (delta deltan deltaw deltadef year month day
  11316. hour minute second wday pm h2 m2 tl wday1
  11317. iso-year iso-weekday iso-week iso-year iso-date)
  11318. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11319. (setq ans "+0"))
  11320. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11321. (setq ans (replace-match "" t t ans)
  11322. deltan (car delta)
  11323. deltaw (nth 1 delta)
  11324. deltadef (nth 2 delta)))
  11325. ;; Check if there is an iso week date in there
  11326. ;; If yes, sore the info and postpone interpreting it until the rest
  11327. ;; of the parsing is done
  11328. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11329. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11330. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11331. iso-week (string-to-number (match-string 2 ans)))
  11332. (setq ans (replace-match "" t t ans)))
  11333. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11334. (when (string-match
  11335. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11336. (setq year (if (match-end 2)
  11337. (string-to-number (match-string 2 ans))
  11338. (string-to-number (format-time-string "%Y")))
  11339. month (string-to-number (match-string 3 ans))
  11340. day (string-to-number (match-string 4 ans)))
  11341. (if (< year 100) (setq year (+ 2000 year)))
  11342. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11343. t nil ans)))
  11344. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11345. ;; If there is a time with am/pm, and *no* time without it, we convert
  11346. ;; so that matching will be successful.
  11347. (loop for i from 1 to 2 do ; twice, for end time as well
  11348. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11349. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11350. (setq hour (string-to-number (match-string 1 ans))
  11351. minute (if (match-end 3)
  11352. (string-to-number (match-string 3 ans))
  11353. 0)
  11354. pm (equal ?p
  11355. (string-to-char (downcase (match-string 4 ans)))))
  11356. (if (and (= hour 12) (not pm))
  11357. (setq hour 0)
  11358. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11359. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11360. t t ans))))
  11361. ;; Check if a time range is given as a duration
  11362. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11363. (setq hour (string-to-number (match-string 1 ans))
  11364. h2 (+ hour (string-to-number (match-string 3 ans)))
  11365. minute (string-to-number (match-string 2 ans))
  11366. m2 (+ minute (if (match-end 5) (string-to-number
  11367. (match-string 5 ans))0)))
  11368. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11369. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11370. t t ans)))
  11371. ;; Check if there is a time range
  11372. (when (boundp 'org-end-time-was-given)
  11373. (setq org-time-was-given nil)
  11374. (when (and (string-match org-plain-time-of-day-regexp ans)
  11375. (match-end 8))
  11376. (setq org-end-time-was-given (match-string 8 ans))
  11377. (setq ans (concat (substring ans 0 (match-beginning 7))
  11378. (substring ans (match-end 7))))))
  11379. (setq tl (parse-time-string ans)
  11380. day (or (nth 3 tl) (nth 3 defdecode))
  11381. month (or (nth 4 tl)
  11382. (if (and org-read-date-prefer-future
  11383. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11384. (1+ (nth 4 defdecode))
  11385. (nth 4 defdecode)))
  11386. year (or (nth 5 tl)
  11387. (if (and org-read-date-prefer-future
  11388. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11389. (1+ (nth 5 defdecode))
  11390. (nth 5 defdecode)))
  11391. hour (or (nth 2 tl) (nth 2 defdecode))
  11392. minute (or (nth 1 tl) (nth 1 defdecode))
  11393. second (or (nth 0 tl) 0)
  11394. wday (nth 6 tl))
  11395. ;; Special date definitions below
  11396. (cond
  11397. (iso-week
  11398. ;; There was an iso week
  11399. (setq year (or iso-year year)
  11400. day (or iso-weekday wday 1)
  11401. wday nil ; to make sure that the trigger below does not match
  11402. iso-date (calendar-gregorian-from-absolute
  11403. (calendar-absolute-from-iso
  11404. (list iso-week day year))))
  11405. ; FIXME: Should we also push ISO weeks into the future?
  11406. ; (when (and org-read-date-prefer-future
  11407. ; (not iso-year)
  11408. ; (< (calendar-absolute-from-gregorian iso-date)
  11409. ; (time-to-days (current-time))))
  11410. ; (setq year (1+ year)
  11411. ; iso-date (calendar-gregorian-from-absolute
  11412. ; (calendar-absolute-from-iso
  11413. ; (list iso-week day year)))))
  11414. (setq month (car iso-date)
  11415. year (nth 2 iso-date)
  11416. day (nth 1 iso-date)))
  11417. (deltan
  11418. (unless deltadef
  11419. (let ((now (decode-time (current-time))))
  11420. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11421. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11422. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11423. ((equal deltaw "m") (setq month (+ month deltan)))
  11424. ((equal deltaw "y") (setq year (+ year deltan)))))
  11425. ((and wday (not (nth 3 tl)))
  11426. ;; Weekday was given, but no day, so pick that day in the week
  11427. ;; on or after the derived date.
  11428. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11429. (unless (equal wday wday1)
  11430. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11431. (if (and (boundp 'org-time-was-given)
  11432. (nth 2 tl))
  11433. (setq org-time-was-given t))
  11434. (if (< year 100) (setq year (+ 2000 year)))
  11435. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11436. (list second minute hour day month year)))
  11437. (defvar parse-time-weekdays)
  11438. (defun org-read-date-get-relative (s today default)
  11439. "Check string S for special relative date string.
  11440. TODAY and DEFAULT are internal times, for today and for a default.
  11441. Return shift list (N what def-flag)
  11442. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11443. N is the number of WHATs to shift.
  11444. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11445. the DEFAULT date rather than TODAY."
  11446. (when (and
  11447. (string-match
  11448. (concat
  11449. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11450. "\\([0-9]+\\)?"
  11451. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11452. "\\([ \t]\\|$\\)") s)
  11453. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11454. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11455. (string-to-char (substring (match-string 1 s) -1))
  11456. ?+))
  11457. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11458. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11459. (what (if (match-end 3) (match-string 3 s) "d"))
  11460. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11461. (date (if rel default today))
  11462. (wday (nth 6 (decode-time date)))
  11463. delta)
  11464. (if wday1
  11465. (progn
  11466. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11467. (if (= dir ?-) (setq delta (- delta 7)))
  11468. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11469. (list delta "d" rel))
  11470. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11471. (defun org-eval-in-calendar (form &optional keepdate)
  11472. "Eval FORM in the calendar window and return to current window.
  11473. Also, store the cursor date in variable org-ans2."
  11474. (let ((sf (selected-frame))
  11475. (sw (selected-window)))
  11476. (select-window (get-buffer-window "*Calendar*" t))
  11477. (eval form)
  11478. (when (and (not keepdate) (calendar-cursor-to-date))
  11479. (let* ((date (calendar-cursor-to-date))
  11480. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11481. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11482. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11483. (select-window sw)
  11484. (select-frame-set-input-focus sf)))
  11485. (defun org-calendar-select ()
  11486. "Return to `org-read-date' with the date currently selected.
  11487. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11488. (interactive)
  11489. (when (calendar-cursor-to-date)
  11490. (let* ((date (calendar-cursor-to-date))
  11491. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11492. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11493. (if (active-minibuffer-window) (exit-minibuffer))))
  11494. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11495. "Insert a date stamp for the date given by the internal TIME.
  11496. WITH-HM means, use the stamp format that includes the time of the day.
  11497. INACTIVE means use square brackets instead of angular ones, so that the
  11498. stamp will not contribute to the agenda.
  11499. PRE and POST are optional strings to be inserted before and after the
  11500. stamp.
  11501. The command returns the inserted time stamp."
  11502. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11503. stamp)
  11504. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11505. (insert-before-markers (or pre ""))
  11506. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11507. (when (listp extra)
  11508. (setq extra (car extra))
  11509. (if (and (stringp extra)
  11510. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11511. (setq extra (format "-%02d:%02d"
  11512. (string-to-number (match-string 1 extra))
  11513. (string-to-number (match-string 2 extra))))
  11514. (setq extra nil)))
  11515. (when extra
  11516. (backward-char 1)
  11517. (insert-before-markers extra)
  11518. (forward-char 1))
  11519. (insert-before-markers (or post ""))
  11520. (setq org-last-inserted-timestamp stamp)))
  11521. (defun org-toggle-time-stamp-overlays ()
  11522. "Toggle the use of custom time stamp formats."
  11523. (interactive)
  11524. (setq org-display-custom-times (not org-display-custom-times))
  11525. (unless org-display-custom-times
  11526. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11527. (while (setq p (next-single-property-change p 'display))
  11528. (if (and (get-text-property p 'display)
  11529. (eq (get-text-property p 'face) 'org-date))
  11530. (remove-text-properties
  11531. p (setq p (next-single-property-change p 'display))
  11532. '(display t))))
  11533. (set-buffer-modified-p bmp)))
  11534. (if (featurep 'xemacs)
  11535. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11536. (org-restart-font-lock)
  11537. (setq org-table-may-need-update t)
  11538. (if org-display-custom-times
  11539. (message "Time stamps are overlayed with custom format")
  11540. (message "Time stamp overlays removed")))
  11541. (defun org-display-custom-time (beg end)
  11542. "Overlay modified time stamp format over timestamp between BEG and END."
  11543. (let* ((ts (buffer-substring beg end))
  11544. t1 w1 with-hm tf time str w2 (off 0))
  11545. (save-match-data
  11546. (setq t1 (org-parse-time-string ts t))
  11547. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11548. (setq off (- (match-end 0) (match-beginning 0)))))
  11549. (setq end (- end off))
  11550. (setq w1 (- end beg)
  11551. with-hm (and (nth 1 t1) (nth 2 t1))
  11552. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11553. time (org-fix-decoded-time t1)
  11554. str (org-add-props
  11555. (format-time-string
  11556. (substring tf 1 -1) (apply 'encode-time time))
  11557. nil 'mouse-face 'highlight)
  11558. w2 (length str))
  11559. (if (not (= w2 w1))
  11560. (add-text-properties (1+ beg) (+ 2 beg)
  11561. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11562. (if (featurep 'xemacs)
  11563. (progn
  11564. (put-text-property beg end 'invisible t)
  11565. (put-text-property beg end 'end-glyph (make-glyph str)))
  11566. (put-text-property beg end 'display str))))
  11567. (defun org-translate-time (string)
  11568. "Translate all timestamps in STRING to custom format.
  11569. But do this only if the variable `org-display-custom-times' is set."
  11570. (when org-display-custom-times
  11571. (save-match-data
  11572. (let* ((start 0)
  11573. (re org-ts-regexp-both)
  11574. t1 with-hm inactive tf time str beg end)
  11575. (while (setq start (string-match re string start))
  11576. (setq beg (match-beginning 0)
  11577. end (match-end 0)
  11578. t1 (save-match-data
  11579. (org-parse-time-string (substring string beg end) t))
  11580. with-hm (and (nth 1 t1) (nth 2 t1))
  11581. inactive (equal (substring string beg (1+ beg)) "[")
  11582. tf (funcall (if with-hm 'cdr 'car)
  11583. org-time-stamp-custom-formats)
  11584. time (org-fix-decoded-time t1)
  11585. str (format-time-string
  11586. (concat
  11587. (if inactive "[" "<") (substring tf 1 -1)
  11588. (if inactive "]" ">"))
  11589. (apply 'encode-time time))
  11590. string (replace-match str t t string)
  11591. start (+ start (length str)))))))
  11592. string)
  11593. (defun org-fix-decoded-time (time)
  11594. "Set 0 instead of nil for the first 6 elements of time.
  11595. Don't touch the rest."
  11596. (let ((n 0))
  11597. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11598. (defun org-days-to-time (timestamp-string)
  11599. "Difference between TIMESTAMP-STRING and now in days."
  11600. (- (time-to-days (org-time-string-to-time timestamp-string))
  11601. (time-to-days (current-time))))
  11602. (defun org-deadline-close (timestamp-string &optional ndays)
  11603. "Is the time in TIMESTAMP-STRING close to the current date?"
  11604. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11605. (and (< (org-days-to-time timestamp-string) ndays)
  11606. (not (org-entry-is-done-p))))
  11607. (defun org-get-wdays (ts)
  11608. "Get the deadline lead time appropriate for timestring TS."
  11609. (cond
  11610. ((<= org-deadline-warning-days 0)
  11611. ;; 0 or negative, enforce this value no matter what
  11612. (- org-deadline-warning-days))
  11613. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11614. ;; lead time is specified.
  11615. (floor (* (string-to-number (match-string 1 ts))
  11616. (cdr (assoc (match-string 2 ts)
  11617. '(("d" . 1) ("w" . 7)
  11618. ("m" . 30.4) ("y" . 365.25)))))))
  11619. ;; go for the default.
  11620. (t org-deadline-warning-days)))
  11621. (defun org-calendar-select-mouse (ev)
  11622. "Return to `org-read-date' with the date currently selected.
  11623. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11624. (interactive "e")
  11625. (mouse-set-point ev)
  11626. (when (calendar-cursor-to-date)
  11627. (let* ((date (calendar-cursor-to-date))
  11628. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11629. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11630. (if (active-minibuffer-window) (exit-minibuffer))))
  11631. (defun org-check-deadlines (ndays)
  11632. "Check if there are any deadlines due or past due.
  11633. A deadline is considered due if it happens within `org-deadline-warning-days'
  11634. days from today's date. If the deadline appears in an entry marked DONE,
  11635. it is not shown. The prefix arg NDAYS can be used to test that many
  11636. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11637. (interactive "P")
  11638. (let* ((org-warn-days
  11639. (cond
  11640. ((equal ndays '(4)) 100000)
  11641. (ndays (prefix-numeric-value ndays))
  11642. (t (abs org-deadline-warning-days))))
  11643. (case-fold-search nil)
  11644. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11645. (callback
  11646. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11647. (message "%d deadlines past-due or due within %d days"
  11648. (org-occur regexp nil callback)
  11649. org-warn-days)))
  11650. (defun org-check-before-date (date)
  11651. "Check if there are deadlines or scheduled entries before DATE."
  11652. (interactive (list (org-read-date)))
  11653. (let ((case-fold-search nil)
  11654. (regexp (concat "\\<\\(" org-deadline-string
  11655. "\\|" org-scheduled-string
  11656. "\\) *<\\([^>]+\\)>"))
  11657. (callback
  11658. (lambda () (time-less-p
  11659. (org-time-string-to-time (match-string 2))
  11660. (org-time-string-to-time date)))))
  11661. (message "%d entries before %s"
  11662. (org-occur regexp nil callback) date)))
  11663. (defun org-check-after-date (date)
  11664. "Check if there are deadlines or scheduled entries after DATE."
  11665. (interactive (list (org-read-date)))
  11666. (let ((case-fold-search nil)
  11667. (regexp (concat "\\<\\(" org-deadline-string
  11668. "\\|" org-scheduled-string
  11669. "\\) *<\\([^>]+\\)>"))
  11670. (callback
  11671. (lambda () (not
  11672. (time-less-p
  11673. (org-time-string-to-time (match-string 2))
  11674. (org-time-string-to-time date))))))
  11675. (message "%d entries after %s"
  11676. (org-occur regexp nil callback) date)))
  11677. (defun org-evaluate-time-range (&optional to-buffer)
  11678. "Evaluate a time range by computing the difference between start and end.
  11679. Normally the result is just printed in the echo area, but with prefix arg
  11680. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11681. If the time range is actually in a table, the result is inserted into the
  11682. next column.
  11683. For time difference computation, a year is assumed to be exactly 365
  11684. days in order to avoid rounding problems."
  11685. (interactive "P")
  11686. (or
  11687. (org-clock-update-time-maybe)
  11688. (save-excursion
  11689. (unless (org-at-date-range-p t)
  11690. (goto-char (point-at-bol))
  11691. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11692. (if (not (org-at-date-range-p t))
  11693. (error "Not at a time-stamp range, and none found in current line")))
  11694. (let* ((ts1 (match-string 1))
  11695. (ts2 (match-string 2))
  11696. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11697. (match-end (match-end 0))
  11698. (time1 (org-time-string-to-time ts1))
  11699. (time2 (org-time-string-to-time ts2))
  11700. (t1 (time-to-seconds time1))
  11701. (t2 (time-to-seconds time2))
  11702. (diff (abs (- t2 t1)))
  11703. (negative (< (- t2 t1) 0))
  11704. ;; (ys (floor (* 365 24 60 60)))
  11705. (ds (* 24 60 60))
  11706. (hs (* 60 60))
  11707. (fy "%dy %dd %02d:%02d")
  11708. (fy1 "%dy %dd")
  11709. (fd "%dd %02d:%02d")
  11710. (fd1 "%dd")
  11711. (fh "%02d:%02d")
  11712. y d h m align)
  11713. (if havetime
  11714. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11715. y 0
  11716. d (floor (/ diff ds)) diff (mod diff ds)
  11717. h (floor (/ diff hs)) diff (mod diff hs)
  11718. m (floor (/ diff 60)))
  11719. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11720. y 0
  11721. d (floor (+ (/ diff ds) 0.5))
  11722. h 0 m 0))
  11723. (if (not to-buffer)
  11724. (message "%s" (org-make-tdiff-string y d h m))
  11725. (if (org-at-table-p)
  11726. (progn
  11727. (goto-char match-end)
  11728. (setq align t)
  11729. (and (looking-at " *|") (goto-char (match-end 0))))
  11730. (goto-char match-end))
  11731. (if (looking-at
  11732. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11733. (replace-match ""))
  11734. (if negative (insert " -"))
  11735. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11736. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11737. (insert " " (format fh h m))))
  11738. (if align (org-table-align))
  11739. (message "Time difference inserted")))))
  11740. (defun org-make-tdiff-string (y d h m)
  11741. (let ((fmt "")
  11742. (l nil))
  11743. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11744. l (push y l)))
  11745. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11746. l (push d l)))
  11747. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11748. l (push h l)))
  11749. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11750. l (push m l)))
  11751. (apply 'format fmt (nreverse l))))
  11752. (defun org-time-string-to-time (s)
  11753. (apply 'encode-time (org-parse-time-string s)))
  11754. (defun org-time-string-to-seconds (s)
  11755. (time-to-seconds (org-time-string-to-time s)))
  11756. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11757. "Convert a time stamp to an absolute day number.
  11758. If there is a specifyer for a cyclic time stamp, get the closest date to
  11759. DAYNR.
  11760. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11761. the variable date is bound by the calendar when this is called."
  11762. (cond
  11763. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11764. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11765. daynr
  11766. (+ daynr 1000)))
  11767. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11768. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11769. (time-to-days (current-time))) (match-string 0 s)
  11770. prefer show-all))
  11771. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11772. (defun org-days-to-iso-week (days)
  11773. "Return the iso week number."
  11774. (require 'cal-iso)
  11775. (car (calendar-iso-from-absolute days)))
  11776. (defun org-small-year-to-year (year)
  11777. "Convert 2-digit years into 4-digit years.
  11778. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11779. The year 2000 cannot be abbreviated. Any year larger than 99
  11780. is returned unchanged."
  11781. (if (< year 38)
  11782. (setq year (+ 2000 year))
  11783. (if (< year 100)
  11784. (setq year (+ 1900 year))))
  11785. year)
  11786. (defun org-time-from-absolute (d)
  11787. "Return the time corresponding to date D.
  11788. D may be an absolute day number, or a calendar-type list (month day year)."
  11789. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11790. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11791. (defun org-calendar-holiday ()
  11792. "List of holidays, for Diary display in Org-mode."
  11793. (require 'holidays)
  11794. (let ((hl (funcall
  11795. (if (fboundp 'calendar-check-holidays)
  11796. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11797. (if hl (mapconcat 'identity hl "; "))))
  11798. (defun org-diary-sexp-entry (sexp entry date)
  11799. "Process a SEXP diary ENTRY for DATE."
  11800. (require 'diary-lib)
  11801. (let ((result (if calendar-debug-sexp
  11802. (let ((stack-trace-on-error t))
  11803. (eval (car (read-from-string sexp))))
  11804. (condition-case nil
  11805. (eval (car (read-from-string sexp)))
  11806. (error
  11807. (beep)
  11808. (message "Bad sexp at line %d in %s: %s"
  11809. (org-current-line)
  11810. (buffer-file-name) sexp)
  11811. (sleep-for 2))))))
  11812. (cond ((stringp result) result)
  11813. ((and (consp result)
  11814. (stringp (cdr result))) (cdr result))
  11815. (result entry)
  11816. (t nil))))
  11817. (defun org-diary-to-ical-string (frombuf)
  11818. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11819. This uses the icalendar.el library."
  11820. (let* ((tmpdir (if (featurep 'xemacs)
  11821. (temp-directory)
  11822. temporary-file-directory))
  11823. (tmpfile (make-temp-name
  11824. (expand-file-name "orgics" tmpdir)))
  11825. buf rtn b e)
  11826. (save-excursion
  11827. (set-buffer frombuf)
  11828. (icalendar-export-region (point-min) (point-max) tmpfile)
  11829. (setq buf (find-buffer-visiting tmpfile))
  11830. (set-buffer buf)
  11831. (goto-char (point-min))
  11832. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11833. (setq b (match-beginning 0)))
  11834. (goto-char (point-max))
  11835. (if (re-search-backward "^END:VEVENT" nil t)
  11836. (setq e (match-end 0)))
  11837. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11838. (kill-buffer buf)
  11839. (delete-file tmpfile)
  11840. rtn))
  11841. (defun org-closest-date (start current change prefer show-all)
  11842. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11843. When PREFER is `past' return a date that is either CURRENT or past.
  11844. When PREFER is `future', return a date that is either CURRENT or future.
  11845. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11846. ;; Make the proper lists from the dates
  11847. (catch 'exit
  11848. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11849. dn dw sday cday n1 n2 n0
  11850. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11851. (setq start (org-date-to-gregorian start)
  11852. current (org-date-to-gregorian
  11853. (if show-all
  11854. current
  11855. (time-to-days (current-time))))
  11856. sday (calendar-absolute-from-gregorian start)
  11857. cday (calendar-absolute-from-gregorian current))
  11858. (if (<= cday sday) (throw 'exit sday))
  11859. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11860. (setq dn (string-to-number (match-string 1 change))
  11861. dw (cdr (assoc (match-string 2 change) a1)))
  11862. (error "Invalid change specifyer: %s" change))
  11863. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11864. (cond
  11865. ((eq dw 'day)
  11866. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11867. n2 (+ n1 dn)))
  11868. ((eq dw 'year)
  11869. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11870. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11871. (setq date1 (list m d y1)
  11872. n1 (calendar-absolute-from-gregorian date1)
  11873. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11874. n2 (calendar-absolute-from-gregorian date2)))
  11875. ((eq dw 'month)
  11876. ;; approx number of month between the two dates
  11877. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11878. ;; How often does dn fit in there?
  11879. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11880. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11881. m (+ m nm)
  11882. ny (floor (/ m 12))
  11883. y (+ y ny)
  11884. m (- m (* ny 12)))
  11885. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11886. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11887. (setq m2 (+ m dn) y2 y)
  11888. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11889. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11890. (while (<= n2 cday)
  11891. (setq n1 n2 m m2 y y2)
  11892. (setq m2 (+ m dn) y2 y)
  11893. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11894. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11895. ;; Make sure n1 is the earlier date
  11896. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11897. (if show-all
  11898. (cond
  11899. ((eq prefer 'past) n1)
  11900. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11901. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11902. (cond
  11903. ((eq prefer 'past) n1)
  11904. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11905. (t (if (= cday n1) n1 n2)))))))
  11906. (defun org-date-to-gregorian (date)
  11907. "Turn any specification of DATE into a gregorian date for the calendar."
  11908. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11909. ((and (listp date) (= (length date) 3)) date)
  11910. ((stringp date)
  11911. (setq date (org-parse-time-string date))
  11912. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11913. ((listp date)
  11914. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11915. (defun org-parse-time-string (s &optional nodefault)
  11916. "Parse the standard Org-mode time string.
  11917. This should be a lot faster than the normal `parse-time-string'.
  11918. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11919. hour and minute fields will be nil if not given."
  11920. (if (string-match org-ts-regexp0 s)
  11921. (list 0
  11922. (if (or (match-beginning 8) (not nodefault))
  11923. (string-to-number (or (match-string 8 s) "0")))
  11924. (if (or (match-beginning 7) (not nodefault))
  11925. (string-to-number (or (match-string 7 s) "0")))
  11926. (string-to-number (match-string 4 s))
  11927. (string-to-number (match-string 3 s))
  11928. (string-to-number (match-string 2 s))
  11929. nil nil nil)
  11930. (make-list 9 0)))
  11931. (defun org-timestamp-up (&optional arg)
  11932. "Increase the date item at the cursor by one.
  11933. If the cursor is on the year, change the year. If it is on the month or
  11934. the day, change that.
  11935. With prefix ARG, change by that many units."
  11936. (interactive "p")
  11937. (org-timestamp-change (prefix-numeric-value arg)))
  11938. (defun org-timestamp-down (&optional arg)
  11939. "Decrease the date item at the cursor by one.
  11940. If the cursor is on the year, change the year. If it is on the month or
  11941. the day, change that.
  11942. With prefix ARG, change by that many units."
  11943. (interactive "p")
  11944. (org-timestamp-change (- (prefix-numeric-value arg))))
  11945. (defun org-timestamp-up-day (&optional arg)
  11946. "Increase the date in the time stamp by one day.
  11947. With prefix ARG, change that many days."
  11948. (interactive "p")
  11949. (if (and (not (org-at-timestamp-p t))
  11950. (org-on-heading-p))
  11951. (org-todo 'up)
  11952. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11953. (defun org-timestamp-down-day (&optional arg)
  11954. "Decrease the date in the time stamp by one day.
  11955. With prefix ARG, change that many days."
  11956. (interactive "p")
  11957. (if (and (not (org-at-timestamp-p t))
  11958. (org-on-heading-p))
  11959. (org-todo 'down)
  11960. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11961. (defun org-at-timestamp-p (&optional inactive-ok)
  11962. "Determine if the cursor is in or at a timestamp."
  11963. (interactive)
  11964. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11965. (pos (point))
  11966. (ans (or (looking-at tsr)
  11967. (save-excursion
  11968. (skip-chars-backward "^[<\n\r\t")
  11969. (if (> (point) (point-min)) (backward-char 1))
  11970. (and (looking-at tsr)
  11971. (> (- (match-end 0) pos) -1))))))
  11972. (and ans
  11973. (boundp 'org-ts-what)
  11974. (setq org-ts-what
  11975. (cond
  11976. ((= pos (match-beginning 0)) 'bracket)
  11977. ((= pos (1- (match-end 0))) 'bracket)
  11978. ((org-pos-in-match-range pos 2) 'year)
  11979. ((org-pos-in-match-range pos 3) 'month)
  11980. ((org-pos-in-match-range pos 7) 'hour)
  11981. ((org-pos-in-match-range pos 8) 'minute)
  11982. ((or (org-pos-in-match-range pos 4)
  11983. (org-pos-in-match-range pos 5)) 'day)
  11984. ((and (> pos (or (match-end 8) (match-end 5)))
  11985. (< pos (match-end 0)))
  11986. (- pos (or (match-end 8) (match-end 5))))
  11987. (t 'day))))
  11988. ans))
  11989. (defun org-toggle-timestamp-type ()
  11990. "Toggle the type (<active> or [inactive]) of a time stamp."
  11991. (interactive)
  11992. (when (org-at-timestamp-p t)
  11993. (let ((beg (match-beginning 0)) (end (match-end 0))
  11994. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11995. (save-excursion
  11996. (goto-char beg)
  11997. (while (re-search-forward "[][<>]" end t)
  11998. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11999. t t)))
  12000. (message "Timestamp is now %sactive"
  12001. (if (equal (char-after beg) ?<) "" "in")))))
  12002. (defun org-timestamp-change (n &optional what)
  12003. "Change the date in the time stamp at point.
  12004. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12005. `year', `minute', `second'. If WHAT is not given, the cursor position
  12006. in the timestamp determines what will be changed."
  12007. (let ((pos (point))
  12008. with-hm inactive
  12009. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12010. org-ts-what
  12011. extra rem
  12012. ts time time0)
  12013. (if (not (org-at-timestamp-p t))
  12014. (error "Not at a timestamp"))
  12015. (if (and (not what) (eq org-ts-what 'bracket))
  12016. (org-toggle-timestamp-type)
  12017. (if (and (not what) (not (eq org-ts-what 'day))
  12018. org-display-custom-times
  12019. (get-text-property (point) 'display)
  12020. (not (get-text-property (1- (point)) 'display)))
  12021. (setq org-ts-what 'day))
  12022. (setq org-ts-what (or what org-ts-what)
  12023. inactive (= (char-after (match-beginning 0)) ?\[)
  12024. ts (match-string 0))
  12025. (replace-match "")
  12026. (if (string-match
  12027. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12028. ts)
  12029. (setq extra (match-string 1 ts)))
  12030. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12031. (setq with-hm t))
  12032. (setq time0 (org-parse-time-string ts))
  12033. (when (and (eq org-ts-what 'minute)
  12034. (eq current-prefix-arg nil))
  12035. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12036. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12037. (setcar (cdr time0) (+ (nth 1 time0)
  12038. (if (> n 0) (- rem) (- dm rem))))))
  12039. (setq time
  12040. (encode-time (or (car time0) 0)
  12041. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12042. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12043. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12044. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12045. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12046. (nthcdr 6 time0)))
  12047. (when (and (member org-ts-what '(hour minute))
  12048. extra
  12049. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12050. (setq extra (org-modify-ts-extra
  12051. extra
  12052. (if (eq org-ts-what 'hour) 2 5)
  12053. n dm)))
  12054. (when (integerp org-ts-what)
  12055. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12056. (if (eq what 'calendar)
  12057. (let ((cal-date (org-get-date-from-calendar)))
  12058. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12059. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12060. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12061. (setcar time0 (or (car time0) 0))
  12062. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12063. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12064. (setq time (apply 'encode-time time0))))
  12065. (setq org-last-changed-timestamp
  12066. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12067. (org-clock-update-time-maybe)
  12068. (goto-char pos)
  12069. ;; Try to recenter the calendar window, if any
  12070. (if (and org-calendar-follow-timestamp-change
  12071. (get-buffer-window "*Calendar*" t)
  12072. (memq org-ts-what '(day month year)))
  12073. (org-recenter-calendar (time-to-days time))))))
  12074. (defun org-modify-ts-extra (s pos n dm)
  12075. "Change the different parts of the lead-time and repeat fields in timestamp."
  12076. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12077. ng h m new rem)
  12078. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12079. (cond
  12080. ((or (org-pos-in-match-range pos 2)
  12081. (org-pos-in-match-range pos 3))
  12082. (setq m (string-to-number (match-string 3 s))
  12083. h (string-to-number (match-string 2 s)))
  12084. (if (org-pos-in-match-range pos 2)
  12085. (setq h (+ h n))
  12086. (setq n (* dm (org-no-warnings (signum n))))
  12087. (when (not (= 0 (setq rem (% m dm))))
  12088. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12089. (setq m (+ m n)))
  12090. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12091. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12092. (setq h (min 24 (max 0 h)))
  12093. (setq ng 1 new (format "-%02d:%02d" h m)))
  12094. ((org-pos-in-match-range pos 6)
  12095. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12096. ((org-pos-in-match-range pos 5)
  12097. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12098. ((org-pos-in-match-range pos 9)
  12099. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12100. ((org-pos-in-match-range pos 8)
  12101. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12102. (when ng
  12103. (setq s (concat
  12104. (substring s 0 (match-beginning ng))
  12105. new
  12106. (substring s (match-end ng))))))
  12107. s))
  12108. (defun org-recenter-calendar (date)
  12109. "If the calendar is visible, recenter it to DATE."
  12110. (let* ((win (selected-window))
  12111. (cwin (get-buffer-window "*Calendar*" t))
  12112. (calendar-move-hook nil))
  12113. (when cwin
  12114. (select-window cwin)
  12115. (calendar-goto-date (if (listp date) date
  12116. (calendar-gregorian-from-absolute date)))
  12117. (select-window win))))
  12118. (defun org-goto-calendar (&optional arg)
  12119. "Go to the Emacs calendar at the current date.
  12120. If there is a time stamp in the current line, go to that date.
  12121. A prefix ARG can be used to force the current date."
  12122. (interactive "P")
  12123. (let ((tsr org-ts-regexp) diff
  12124. (calendar-move-hook nil)
  12125. (calendar-view-holidays-initially-flag nil)
  12126. (view-calendar-holidays-initially nil)
  12127. (calendar-view-diary-initially-flag nil)
  12128. (view-diary-entries-initially nil))
  12129. (if (or (org-at-timestamp-p)
  12130. (save-excursion
  12131. (beginning-of-line 1)
  12132. (looking-at (concat ".*" tsr))))
  12133. (let ((d1 (time-to-days (current-time)))
  12134. (d2 (time-to-days
  12135. (org-time-string-to-time (match-string 1)))))
  12136. (setq diff (- d2 d1))))
  12137. (calendar)
  12138. (calendar-goto-today)
  12139. (if (and diff (not arg)) (calendar-forward-day diff))))
  12140. (defun org-get-date-from-calendar ()
  12141. "Return a list (month day year) of date at point in calendar."
  12142. (with-current-buffer "*Calendar*"
  12143. (save-match-data
  12144. (calendar-cursor-to-date))))
  12145. (defun org-date-from-calendar ()
  12146. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12147. If there is already a time stamp at the cursor position, update it."
  12148. (interactive)
  12149. (if (org-at-timestamp-p t)
  12150. (org-timestamp-change 0 'calendar)
  12151. (let ((cal-date (org-get-date-from-calendar)))
  12152. (org-insert-time-stamp
  12153. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12154. (defun org-minutes-to-hh:mm-string (m)
  12155. "Compute H:MM from a number of minutes."
  12156. (let ((h (/ m 60)))
  12157. (setq m (- m (* 60 h)))
  12158. (format org-time-clocksum-format h m)))
  12159. (defun org-hh:mm-string-to-minutes (s)
  12160. "Convert a string H:MM to a number of minutes.
  12161. If the string is just a number, interprete it as minutes.
  12162. In fact, the first hh:mm or number in the string will be taken,
  12163. there can be extra stuff in the string.
  12164. If no number is found, the return value is 0."
  12165. (cond
  12166. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12167. (+ (* (string-to-number (match-string 1 s)) 60)
  12168. (string-to-number (match-string 2 s))))
  12169. ((string-match "\\([0-9]+\\)" s)
  12170. (string-to-number (match-string 1 s)))
  12171. (t 0)))
  12172. ;;;; Files
  12173. (defun org-save-all-org-buffers ()
  12174. "Save all Org-mode buffers without user confirmation."
  12175. (interactive)
  12176. (message "Saving all Org-mode buffers...")
  12177. (save-some-buffers t 'org-mode-p)
  12178. (when (featurep 'org-id) (org-id-locations-save))
  12179. (message "Saving all Org-mode buffers... done"))
  12180. (defun org-revert-all-org-buffers ()
  12181. "Revert all Org-mode buffers.
  12182. Prompt for confirmation when there are unsaved changes.
  12183. Be sure you know what you are doing before letting this function
  12184. overwrite your changes.
  12185. This function is useful in a setup where one tracks org files
  12186. with a version control system, to revert on one machine after pulling
  12187. changes from another. I believe the procedure must be like this:
  12188. 1. M-x org-save-all-org-buffers
  12189. 2. Pull changes from the other machine, resolve conflicts
  12190. 3. M-x org-revert-all-org-buffers"
  12191. (interactive)
  12192. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12193. (error "Abort"))
  12194. (save-excursion
  12195. (save-window-excursion
  12196. (mapc
  12197. (lambda (b)
  12198. (when (and (with-current-buffer b (org-mode-p))
  12199. (with-current-buffer b buffer-file-name))
  12200. (switch-to-buffer b)
  12201. (revert-buffer t 'no-confirm)))
  12202. (buffer-list))
  12203. (when (and (featurep 'org-id) org-id-track-globally)
  12204. (org-id-locations-load)))))
  12205. ;;;; Agenda files
  12206. ;;;###autoload
  12207. (defun org-iswitchb (&optional arg)
  12208. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12209. With a prefix argument, restrict available to files.
  12210. With two prefix arguments, restrict available buffers to agenda files.
  12211. Due to some yet unresolved reason, the global function
  12212. `iswitchb-mode' needs to be active for this function to work."
  12213. (interactive "P")
  12214. (require 'iswitchb)
  12215. (let ((enabled iswitchb-mode) blist)
  12216. (or enabled (iswitchb-mode 1))
  12217. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12218. ((equal arg '(16)) (org-buffer-list 'agenda))
  12219. (t (org-buffer-list))))
  12220. (unwind-protect
  12221. (let ((iswitchb-make-buflist-hook
  12222. (lambda ()
  12223. (setq iswitchb-temp-buflist
  12224. (mapcar 'buffer-name blist)))))
  12225. (switch-to-buffer
  12226. (iswitchb-read-buffer
  12227. "Switch-to: " nil t))
  12228. (or enabled (iswitchb-mode -1))))))
  12229. ;;;###autoload
  12230. (defun org-ido-switchb (&optional arg)
  12231. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12232. With a prefix argument, restrict available to files.
  12233. With two prefix arguments, restrict available buffers to agenda files."
  12234. (interactive "P")
  12235. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12236. ((equal arg '(16)) (org-buffer-list 'agenda))
  12237. (t (org-buffer-list)))))
  12238. (switch-to-buffer
  12239. (org-ido-completing-read "Org buffer: "
  12240. (mapcar 'list (mapcar 'buffer-name blist))
  12241. nil t))))
  12242. (defun org-buffer-list (&optional predicate exclude-tmp)
  12243. "Return a list of Org buffers.
  12244. PREDICATE can be `export', `files' or `agenda'.
  12245. export restrict the list to Export buffers.
  12246. files restrict the list to buffers visiting Org files.
  12247. agenda restrict the list to buffers visiting agenda files.
  12248. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12249. (let* ((bfn nil)
  12250. (agenda-files (and (eq predicate 'agenda)
  12251. (mapcar 'file-truename (org-agenda-files t))))
  12252. (filter
  12253. (cond
  12254. ((eq predicate 'files)
  12255. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12256. ((eq predicate 'export)
  12257. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12258. ((eq predicate 'agenda)
  12259. (lambda (b)
  12260. (with-current-buffer b
  12261. (and (eq major-mode 'org-mode)
  12262. (setq bfn (buffer-file-name b))
  12263. (member (file-truename bfn) agenda-files)))))
  12264. (t (lambda (b) (with-current-buffer b
  12265. (or (eq major-mode 'org-mode)
  12266. (string-match "\*Org .*Export"
  12267. (buffer-name b)))))))))
  12268. (delq nil
  12269. (mapcar
  12270. (lambda(b)
  12271. (if (and (funcall filter b)
  12272. (or (not exclude-tmp)
  12273. (not (string-match "tmp" (buffer-name b)))))
  12274. b
  12275. nil))
  12276. (buffer-list)))))
  12277. (defun org-agenda-files (&optional unrestricted archives)
  12278. "Get the list of agenda files.
  12279. Optional UNRESTRICTED means return the full list even if a restriction
  12280. is currently in place.
  12281. When ARCHIVES is t, include all archive files hat are really being
  12282. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12283. `org-agenda-archives-mode' is t."
  12284. (let ((files
  12285. (cond
  12286. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12287. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12288. ((listp org-agenda-files) org-agenda-files)
  12289. (t (error "Invalid value of `org-agenda-files'")))))
  12290. (setq files (apply 'append
  12291. (mapcar (lambda (f)
  12292. (if (file-directory-p f)
  12293. (directory-files
  12294. f t org-agenda-file-regexp)
  12295. (list f)))
  12296. files)))
  12297. (when org-agenda-skip-unavailable-files
  12298. (setq files (delq nil
  12299. (mapcar (function
  12300. (lambda (file)
  12301. (and (file-readable-p file) file)))
  12302. files))))
  12303. (when (or (eq archives t)
  12304. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12305. (setq files (org-add-archive-files files)))
  12306. files))
  12307. (defun org-edit-agenda-file-list ()
  12308. "Edit the list of agenda files.
  12309. Depending on setup, this either uses customize to edit the variable
  12310. `org-agenda-files', or it visits the file that is holding the list. In the
  12311. latter case, the buffer is set up in a way that saving it automatically kills
  12312. the buffer and restores the previous window configuration."
  12313. (interactive)
  12314. (if (stringp org-agenda-files)
  12315. (let ((cw (current-window-configuration)))
  12316. (find-file org-agenda-files)
  12317. (org-set-local 'org-window-configuration cw)
  12318. (org-add-hook 'after-save-hook
  12319. (lambda ()
  12320. (set-window-configuration
  12321. (prog1 org-window-configuration
  12322. (kill-buffer (current-buffer))))
  12323. (org-install-agenda-files-menu)
  12324. (message "New agenda file list installed"))
  12325. nil 'local)
  12326. (message "%s" (substitute-command-keys
  12327. "Edit list and finish with \\[save-buffer]")))
  12328. (customize-variable 'org-agenda-files)))
  12329. (defun org-store-new-agenda-file-list (list)
  12330. "Set new value for the agenda file list and save it correctly."
  12331. (if (stringp org-agenda-files)
  12332. (let ((f org-agenda-files) b)
  12333. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12334. (with-temp-file f
  12335. (insert (mapconcat 'identity list "\n") "\n")))
  12336. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12337. (setq org-agenda-files list)
  12338. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12339. (defun org-read-agenda-file-list ()
  12340. "Read the list of agenda files from a file."
  12341. (when (file-directory-p org-agenda-files)
  12342. (error "`org-agenda-files' cannot be a single directory"))
  12343. (when (stringp org-agenda-files)
  12344. (with-temp-buffer
  12345. (insert-file-contents org-agenda-files)
  12346. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12347. ;;;###autoload
  12348. (defun org-cycle-agenda-files ()
  12349. "Cycle through the files in `org-agenda-files'.
  12350. If the current buffer visits an agenda file, find the next one in the list.
  12351. If the current buffer does not, find the first agenda file."
  12352. (interactive)
  12353. (let* ((fs (org-agenda-files t))
  12354. (files (append fs (list (car fs))))
  12355. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12356. file)
  12357. (unless files (error "No agenda files"))
  12358. (catch 'exit
  12359. (while (setq file (pop files))
  12360. (if (equal (file-truename file) tcf)
  12361. (when (car files)
  12362. (find-file (car files))
  12363. (throw 'exit t))))
  12364. (find-file (car fs)))
  12365. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12366. (defun org-agenda-file-to-front (&optional to-end)
  12367. "Move/add the current file to the top of the agenda file list.
  12368. If the file is not present in the list, it is added to the front. If it is
  12369. present, it is moved there. With optional argument TO-END, add/move to the
  12370. end of the list."
  12371. (interactive "P")
  12372. (let ((org-agenda-skip-unavailable-files nil)
  12373. (file-alist (mapcar (lambda (x)
  12374. (cons (file-truename x) x))
  12375. (org-agenda-files t)))
  12376. (ctf (file-truename buffer-file-name))
  12377. x had)
  12378. (setq x (assoc ctf file-alist) had x)
  12379. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12380. (if to-end
  12381. (setq file-alist (append (delq x file-alist) (list x)))
  12382. (setq file-alist (cons x (delq x file-alist))))
  12383. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12384. (org-install-agenda-files-menu)
  12385. (message "File %s to %s of agenda file list"
  12386. (if had "moved" "added") (if to-end "end" "front"))))
  12387. (defun org-remove-file (&optional file)
  12388. "Remove current file from the list of files in variable `org-agenda-files'.
  12389. These are the files which are being checked for agenda entries.
  12390. Optional argument FILE means, use this file instead of the current."
  12391. (interactive)
  12392. (let* ((org-agenda-skip-unavailable-files nil)
  12393. (file (or file buffer-file-name))
  12394. (true-file (file-truename file))
  12395. (afile (abbreviate-file-name file))
  12396. (files (delq nil (mapcar
  12397. (lambda (x)
  12398. (if (equal true-file
  12399. (file-truename x))
  12400. nil x))
  12401. (org-agenda-files t)))))
  12402. (if (not (= (length files) (length (org-agenda-files t))))
  12403. (progn
  12404. (org-store-new-agenda-file-list files)
  12405. (org-install-agenda-files-menu)
  12406. (message "Removed file: %s" afile))
  12407. (message "File was not in list: %s (not removed)" afile))))
  12408. (defun org-file-menu-entry (file)
  12409. (vector file (list 'find-file file) t))
  12410. (defun org-check-agenda-file (file)
  12411. "Make sure FILE exists. If not, ask user what to do."
  12412. (when (not (file-exists-p file))
  12413. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12414. (abbreviate-file-name file))
  12415. (let ((r (downcase (read-char-exclusive))))
  12416. (cond
  12417. ((equal r ?r)
  12418. (org-remove-file file)
  12419. (throw 'nextfile t))
  12420. (t (error "Abort"))))))
  12421. (defun org-get-agenda-file-buffer (file)
  12422. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12423. it to the list of buffers which might be released later."
  12424. (let ((buf (org-find-base-buffer-visiting file)))
  12425. (if buf
  12426. buf ; just return it
  12427. ;; Make a new buffer and remember it
  12428. (setq buf (find-file-noselect file))
  12429. (if buf (push buf org-agenda-new-buffers))
  12430. buf)))
  12431. (defun org-release-buffers (blist)
  12432. "Release all buffers in list, asking the user for confirmation when needed.
  12433. When a buffer is unmodified, it is just killed. When modified, it is saved
  12434. \(if the user agrees) and then killed."
  12435. (let (buf file)
  12436. (while (setq buf (pop blist))
  12437. (setq file (buffer-file-name buf))
  12438. (when (and (buffer-modified-p buf)
  12439. file
  12440. (y-or-n-p (format "Save file %s? " file)))
  12441. (with-current-buffer buf (save-buffer)))
  12442. (kill-buffer buf))))
  12443. (defun org-prepare-agenda-buffers (files)
  12444. "Create buffers for all agenda files, protect archived trees and comments."
  12445. (interactive)
  12446. (let ((pa '(:org-archived t))
  12447. (pc '(:org-comment t))
  12448. (pall '(:org-archived t :org-comment t))
  12449. (inhibit-read-only t)
  12450. (rea (concat ":" org-archive-tag ":"))
  12451. bmp file re)
  12452. (save-excursion
  12453. (save-restriction
  12454. (while (setq file (pop files))
  12455. (catch 'nextfile
  12456. (if (bufferp file)
  12457. (set-buffer file)
  12458. (org-check-agenda-file file)
  12459. (set-buffer (org-get-agenda-file-buffer file)))
  12460. (widen)
  12461. (setq bmp (buffer-modified-p))
  12462. (org-refresh-category-properties)
  12463. (setq org-todo-keywords-for-agenda
  12464. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12465. (setq org-done-keywords-for-agenda
  12466. (append org-done-keywords-for-agenda org-done-keywords))
  12467. (setq org-todo-keyword-alist-for-agenda
  12468. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12469. (setq org-tag-alist-for-agenda
  12470. (append org-tag-alist-for-agenda org-tag-alist))
  12471. (save-excursion
  12472. (remove-text-properties (point-min) (point-max) pall)
  12473. (when org-agenda-skip-archived-trees
  12474. (goto-char (point-min))
  12475. (while (re-search-forward rea nil t)
  12476. (if (org-on-heading-p t)
  12477. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12478. (goto-char (point-min))
  12479. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12480. (while (re-search-forward re nil t)
  12481. (add-text-properties
  12482. (match-beginning 0) (org-end-of-subtree t) pc)))
  12483. (set-buffer-modified-p bmp)))))
  12484. (setq org-todo-keyword-alist-for-agenda
  12485. (org-uniquify org-todo-keyword-alist-for-agenda)
  12486. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12487. ;;;; Embedded LaTeX
  12488. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12489. "Keymap for the minor `org-cdlatex-mode'.")
  12490. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12491. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12492. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12493. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12494. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12495. (defvar org-cdlatex-texmathp-advice-is-done nil
  12496. "Flag remembering if we have applied the advice to texmathp already.")
  12497. (define-minor-mode org-cdlatex-mode
  12498. "Toggle the minor `org-cdlatex-mode'.
  12499. This mode supports entering LaTeX environment and math in LaTeX fragments
  12500. in Org-mode.
  12501. \\{org-cdlatex-mode-map}"
  12502. nil " OCDL" nil
  12503. (when org-cdlatex-mode (require 'cdlatex))
  12504. (unless org-cdlatex-texmathp-advice-is-done
  12505. (setq org-cdlatex-texmathp-advice-is-done t)
  12506. (defadvice texmathp (around org-math-always-on activate)
  12507. "Always return t in org-mode buffers.
  12508. This is because we want to insert math symbols without dollars even outside
  12509. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12510. an embedded LaTeX fragment, let texmathp do its job.
  12511. \\[org-cdlatex-mode-map]"
  12512. (interactive)
  12513. (let (p)
  12514. (cond
  12515. ((not (org-mode-p)) ad-do-it)
  12516. ((eq this-command 'cdlatex-math-symbol)
  12517. (setq ad-return-value t
  12518. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12519. (t
  12520. (let ((p (org-inside-LaTeX-fragment-p)))
  12521. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12522. (setq ad-return-value t
  12523. texmathp-why '("Org-mode embedded math" . 0))
  12524. (if p ad-do-it)))))))))
  12525. (defun turn-on-org-cdlatex ()
  12526. "Unconditionally turn on `org-cdlatex-mode'."
  12527. (org-cdlatex-mode 1))
  12528. (defun org-inside-LaTeX-fragment-p ()
  12529. "Test if point is inside a LaTeX fragment.
  12530. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12531. sequence appearing also before point.
  12532. Even though the matchers for math are configurable, this function assumes
  12533. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12534. delimiters are skipped when they have been removed by customization.
  12535. The return value is nil, or a cons cell with the delimiter and
  12536. and the position of this delimiter.
  12537. This function does a reasonably good job, but can locally be fooled by
  12538. for example currency specifications. For example it will assume being in
  12539. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12540. fragments that are properly closed, but during editing, we have to live
  12541. with the uncertainty caused by missing closing delimiters. This function
  12542. looks only before point, not after."
  12543. (catch 'exit
  12544. (let ((pos (point))
  12545. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12546. (lim (progn
  12547. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12548. (point)))
  12549. dd-on str (start 0) m re)
  12550. (goto-char pos)
  12551. (when dodollar
  12552. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12553. re (nth 1 (assoc "$" org-latex-regexps)))
  12554. (while (string-match re str start)
  12555. (cond
  12556. ((= (match-end 0) (length str))
  12557. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12558. ((= (match-end 0) (- (length str) 5))
  12559. (throw 'exit nil))
  12560. (t (setq start (match-end 0))))))
  12561. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12562. (goto-char pos)
  12563. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12564. (and (match-beginning 2) (throw 'exit nil))
  12565. ;; count $$
  12566. (while (re-search-backward "\\$\\$" lim t)
  12567. (setq dd-on (not dd-on)))
  12568. (goto-char pos)
  12569. (if dd-on (cons "$$" m))))))
  12570. (defun org-try-cdlatex-tab ()
  12571. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12572. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12573. - inside a LaTeX fragment, or
  12574. - after the first word in a line, where an abbreviation expansion could
  12575. insert a LaTeX environment."
  12576. (when org-cdlatex-mode
  12577. (cond
  12578. ((save-excursion
  12579. (skip-chars-backward "a-zA-Z0-9*")
  12580. (skip-chars-backward " \t")
  12581. (bolp))
  12582. (cdlatex-tab) t)
  12583. ((org-inside-LaTeX-fragment-p)
  12584. (cdlatex-tab) t)
  12585. (t nil))))
  12586. (defun org-cdlatex-underscore-caret (&optional arg)
  12587. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12588. Revert to the normal definition outside of these fragments."
  12589. (interactive "P")
  12590. (if (org-inside-LaTeX-fragment-p)
  12591. (call-interactively 'cdlatex-sub-superscript)
  12592. (let (org-cdlatex-mode)
  12593. (call-interactively (key-binding (vector last-input-event))))))
  12594. (defun org-cdlatex-math-modify (&optional arg)
  12595. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12596. Revert to the normal definition outside of these fragments."
  12597. (interactive "P")
  12598. (if (org-inside-LaTeX-fragment-p)
  12599. (call-interactively 'cdlatex-math-modify)
  12600. (let (org-cdlatex-mode)
  12601. (call-interactively (key-binding (vector last-input-event))))))
  12602. (defvar org-latex-fragment-image-overlays nil
  12603. "List of overlays carrying the images of latex fragments.")
  12604. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12605. (defun org-remove-latex-fragment-image-overlays ()
  12606. "Remove all overlays with LaTeX fragment images in current buffer."
  12607. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12608. (setq org-latex-fragment-image-overlays nil))
  12609. (defun org-preview-latex-fragment (&optional subtree)
  12610. "Preview the LaTeX fragment at point, or all locally or globally.
  12611. If the cursor is in a LaTeX fragment, create the image and overlay
  12612. it over the source code. If there is no fragment at point, display
  12613. all fragments in the current text, from one headline to the next. With
  12614. prefix SUBTREE, display all fragments in the current subtree. With a
  12615. double prefix `C-u C-u', or when the cursor is before the first headline,
  12616. display all fragments in the buffer.
  12617. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12618. (interactive "P")
  12619. (org-remove-latex-fragment-image-overlays)
  12620. (save-excursion
  12621. (save-restriction
  12622. (let (beg end at msg)
  12623. (cond
  12624. ((or (equal subtree '(16))
  12625. (not (save-excursion
  12626. (re-search-backward (concat "^" outline-regexp) nil t))))
  12627. (setq beg (point-min) end (point-max)
  12628. msg "Creating images for buffer...%s"))
  12629. ((equal subtree '(4))
  12630. (org-back-to-heading)
  12631. (setq beg (point) end (org-end-of-subtree t)
  12632. msg "Creating images for subtree...%s"))
  12633. (t
  12634. (if (setq at (org-inside-LaTeX-fragment-p))
  12635. (goto-char (max (point-min) (- (cdr at) 2)))
  12636. (org-back-to-heading))
  12637. (setq beg (point) end (progn (outline-next-heading) (point))
  12638. msg (if at "Creating image...%s"
  12639. "Creating images for entry...%s"))))
  12640. (message msg "")
  12641. (narrow-to-region beg end)
  12642. (goto-char beg)
  12643. (org-format-latex
  12644. (concat "ltxpng/" (file-name-sans-extension
  12645. (file-name-nondirectory
  12646. buffer-file-name)))
  12647. default-directory 'overlays msg at 'forbuffer)
  12648. (message msg "done. Use `C-c C-c' to remove images.")))))
  12649. (defvar org-latex-regexps
  12650. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12651. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12652. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12653. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12654. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12655. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12656. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12657. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12658. "Regular expressions for matching embedded LaTeX.")
  12659. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12660. "Replace LaTeX fragments with links to an image, and produce images."
  12661. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12662. (let* ((prefixnodir (file-name-nondirectory prefix))
  12663. (absprefix (expand-file-name prefix dir))
  12664. (todir (file-name-directory absprefix))
  12665. (opt org-format-latex-options)
  12666. (matchers (plist-get opt :matchers))
  12667. (re-list org-latex-regexps)
  12668. (cnt 0) txt link beg end re e checkdir
  12669. executables-checked
  12670. m n block linkfile movefile ov)
  12671. ;; Check if there are old images files with this prefix, and remove them
  12672. (when (file-directory-p todir)
  12673. (mapc 'delete-file
  12674. (directory-files
  12675. todir 'full
  12676. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12677. ;; Check the different regular expressions
  12678. (while (setq e (pop re-list))
  12679. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12680. block (if (nth 3 e) "\n\n" ""))
  12681. (when (member m matchers)
  12682. (goto-char (point-min))
  12683. (while (re-search-forward re nil t)
  12684. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12685. (not (get-text-property (match-beginning n)
  12686. 'org-protected)))
  12687. (setq txt (match-string n)
  12688. beg (match-beginning n) end (match-end n)
  12689. cnt (1+ cnt)
  12690. linkfile (format "%s_%04d.png" prefix cnt)
  12691. movefile (format "%s_%04d.png" absprefix cnt)
  12692. link (concat block "[[file:" linkfile "]]" block))
  12693. (if msg (message msg cnt))
  12694. (goto-char beg)
  12695. (unless checkdir ; make sure the directory exists
  12696. (setq checkdir t)
  12697. (or (file-directory-p todir) (make-directory todir)))
  12698. (unless executables-checked
  12699. (org-check-external-command
  12700. "latex" "needed to convert LaTeX fragments to images")
  12701. (org-check-external-command
  12702. "dvipng" "needed to convert LaTeX fragments to images")
  12703. (setq executables-checked t))
  12704. (org-create-formula-image
  12705. txt movefile opt forbuffer)
  12706. (if overlays
  12707. (progn
  12708. (setq ov (org-make-overlay beg end))
  12709. (if (featurep 'xemacs)
  12710. (progn
  12711. (org-overlay-put ov 'invisible t)
  12712. (org-overlay-put
  12713. ov 'end-glyph
  12714. (make-glyph (vector 'png :file movefile))))
  12715. (org-overlay-put
  12716. ov 'display
  12717. (list 'image :type 'png :file movefile :ascent 'center)))
  12718. (push ov org-latex-fragment-image-overlays)
  12719. (goto-char end))
  12720. (delete-region beg end)
  12721. (insert link))))))))
  12722. ;; This function borrows from Ganesh Swami's latex2png.el
  12723. (defun org-create-formula-image (string tofile options buffer)
  12724. (let* ((tmpdir (if (featurep 'xemacs)
  12725. (temp-directory)
  12726. temporary-file-directory))
  12727. (texfilebase (make-temp-name
  12728. (expand-file-name "orgtex" tmpdir)))
  12729. (texfile (concat texfilebase ".tex"))
  12730. (dvifile (concat texfilebase ".dvi"))
  12731. (pngfile (concat texfilebase ".png"))
  12732. (fnh (if (featurep 'xemacs)
  12733. (font-height (get-face-font 'default))
  12734. (face-attribute 'default :height nil)))
  12735. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12736. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12737. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12738. "Black"))
  12739. (bg (or (plist-get options (if buffer :background :html-background))
  12740. "Transparent")))
  12741. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12742. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12743. (with-temp-file texfile
  12744. (insert org-format-latex-header
  12745. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12746. (let ((dir default-directory))
  12747. (condition-case nil
  12748. (progn
  12749. (cd tmpdir)
  12750. (call-process "latex" nil nil nil texfile))
  12751. (error nil))
  12752. (cd dir))
  12753. (if (not (file-exists-p dvifile))
  12754. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12755. (condition-case nil
  12756. (call-process "dvipng" nil nil nil
  12757. "-E" "-fg" fg "-bg" bg
  12758. "-D" dpi
  12759. ;;"-x" scale "-y" scale
  12760. "-T" "tight"
  12761. "-o" pngfile
  12762. dvifile)
  12763. (error nil))
  12764. (if (not (file-exists-p pngfile))
  12765. (progn (message "Failed to create png file from %s" texfile) nil)
  12766. ;; Use the requested file name and clean up
  12767. (copy-file pngfile tofile 'replace)
  12768. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12769. (delete-file (concat texfilebase e)))
  12770. pngfile))))
  12771. (defun org-dvipng-color (attr)
  12772. "Return an rgb color specification for dvipng."
  12773. (apply 'format "rgb %s %s %s"
  12774. (mapcar 'org-normalize-color
  12775. (color-values (face-attribute 'default attr nil)))))
  12776. (defun org-normalize-color (value)
  12777. "Return string to be used as color value for an RGB component."
  12778. (format "%g" (/ value 65535.0)))
  12779. ;;;; Key bindings
  12780. ;; Make `C-c C-x' a prefix key
  12781. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12782. ;; TAB key with modifiers
  12783. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12784. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12785. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12786. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12787. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12788. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12789. ;; The following line is necessary under Suse GNU/Linux
  12790. (unless (featurep 'xemacs)
  12791. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12792. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12793. (define-key org-mode-map [backtab] 'org-shifttab)
  12794. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12795. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12796. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12797. ;; Cursor keys with modifiers
  12798. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12799. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12800. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12801. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12802. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12803. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12804. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12805. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12806. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12807. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12808. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12809. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12810. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12811. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12812. ;;; Extra keys for tty access.
  12813. ;; We only set them when really needed because otherwise the
  12814. ;; menus don't show the simple keys
  12815. (when (or org-use-extra-keys
  12816. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12817. (not window-system))
  12818. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12819. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12820. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12821. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12822. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12823. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12824. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12825. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12826. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12827. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12828. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12829. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12830. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12831. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12832. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12833. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12834. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12835. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12836. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12837. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12838. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12839. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12840. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12841. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12842. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12843. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12844. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12845. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12846. ;; All the other keys
  12847. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12848. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12849. (if (boundp 'narrow-map)
  12850. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12851. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12852. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12853. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12854. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12855. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12856. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12857. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12858. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12859. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12860. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12861. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12862. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12863. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12864. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12865. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12866. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12867. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12868. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12869. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12870. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12871. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12872. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12873. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12874. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12875. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12876. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12877. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12878. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12879. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12880. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12881. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12882. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12883. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12884. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12885. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12886. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12887. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12888. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12889. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12890. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12891. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12892. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12893. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12894. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12895. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12896. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12897. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12898. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12899. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12900. (org-defkey org-mode-map "\C-m" 'org-return)
  12901. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12902. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12903. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12904. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12905. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12906. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12907. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12908. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12909. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12910. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12911. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12912. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12913. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12914. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12915. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12916. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12917. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12918. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  12919. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  12920. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12921. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12922. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12923. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12924. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12925. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12926. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12927. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12928. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12929. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12930. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12931. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12932. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12933. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12934. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12935. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12936. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12937. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  12938. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12939. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12940. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12941. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12942. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12943. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12944. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12945. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12946. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12947. (when (featurep 'xemacs)
  12948. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12949. (defvar org-self-insert-command-undo-counter 0)
  12950. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12951. (defun org-self-insert-command (N)
  12952. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12953. If the cursor is in a table looking at whitespace, the whitespace is
  12954. overwritten, and the table is not marked as requiring realignment."
  12955. (interactive "p")
  12956. (if (and
  12957. (org-table-p)
  12958. (progn
  12959. ;; check if we blank the field, and if that triggers align
  12960. (and (featurep 'org-table) org-table-auto-blank-field
  12961. (member last-command
  12962. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  12963. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12964. ;; got extra space, this field does not determine column width
  12965. (let (org-table-may-need-update) (org-table-blank-field))
  12966. ;; no extra space, this field may determine column width
  12967. (org-table-blank-field)))
  12968. t)
  12969. (eq N 1)
  12970. (looking-at "[^|\n]* |"))
  12971. (let (org-table-may-need-update)
  12972. (goto-char (1- (match-end 0)))
  12973. (delete-backward-char 1)
  12974. (goto-char (match-beginning 0))
  12975. (self-insert-command N))
  12976. (setq org-table-may-need-update t)
  12977. (self-insert-command N)
  12978. (org-fix-tags-on-the-fly)
  12979. (if org-self-insert-cluster-for-undo
  12980. (if (not (eq last-command 'org-self-insert-command))
  12981. (setq org-self-insert-command-undo-counter 1)
  12982. (if (>= org-self-insert-command-undo-counter 20)
  12983. (setq org-self-insert-command-undo-counter 1)
  12984. (and (> org-self-insert-command-undo-counter 0)
  12985. buffer-undo-list
  12986. (not (cadr buffer-undo-list)) ; remove nil entry
  12987. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12988. (setq org-self-insert-command-undo-counter
  12989. (1+ org-self-insert-command-undo-counter)))))))
  12990. (defun org-fix-tags-on-the-fly ()
  12991. (when (and (equal (char-after (point-at-bol)) ?*)
  12992. (org-on-heading-p))
  12993. (org-align-tags-here org-tags-column)))
  12994. (defun org-delete-backward-char (N)
  12995. "Like `delete-backward-char', insert whitespace at field end in tables.
  12996. When deleting backwards, in tables this function will insert whitespace in
  12997. front of the next \"|\" separator, to keep the table aligned. The table will
  12998. still be marked for re-alignment if the field did fill the entire column,
  12999. because, in this case the deletion might narrow the column."
  13000. (interactive "p")
  13001. (if (and (org-table-p)
  13002. (eq N 1)
  13003. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13004. (looking-at ".*?|"))
  13005. (let ((pos (point))
  13006. (noalign (looking-at "[^|\n\r]* |"))
  13007. (c org-table-may-need-update))
  13008. (backward-delete-char N)
  13009. (skip-chars-forward "^|")
  13010. (insert " ")
  13011. (goto-char (1- pos))
  13012. ;; noalign: if there were two spaces at the end, this field
  13013. ;; does not determine the width of the column.
  13014. (if noalign (setq org-table-may-need-update c)))
  13015. (backward-delete-char N)
  13016. (org-fix-tags-on-the-fly)))
  13017. (defun org-delete-char (N)
  13018. "Like `delete-char', but insert whitespace at field end in tables.
  13019. When deleting characters, in tables this function will insert whitespace in
  13020. front of the next \"|\" separator, to keep the table aligned. The table will
  13021. still be marked for re-alignment if the field did fill the entire column,
  13022. because, in this case the deletion might narrow the column."
  13023. (interactive "p")
  13024. (if (and (org-table-p)
  13025. (not (bolp))
  13026. (not (= (char-after) ?|))
  13027. (eq N 1))
  13028. (if (looking-at ".*?|")
  13029. (let ((pos (point))
  13030. (noalign (looking-at "[^|\n\r]* |"))
  13031. (c org-table-may-need-update))
  13032. (replace-match (concat
  13033. (substring (match-string 0) 1 -1)
  13034. " |"))
  13035. (goto-char pos)
  13036. ;; noalign: if there were two spaces at the end, this field
  13037. ;; does not determine the width of the column.
  13038. (if noalign (setq org-table-may-need-update c)))
  13039. (delete-char N))
  13040. (delete-char N)
  13041. (org-fix-tags-on-the-fly)))
  13042. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13043. (put 'org-self-insert-command 'delete-selection t)
  13044. (put 'orgtbl-self-insert-command 'delete-selection t)
  13045. (put 'org-delete-char 'delete-selection 'supersede)
  13046. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13047. (put 'org-yank 'delete-selection 'yank)
  13048. ;; Make `flyspell-mode' delay after some commands
  13049. (put 'org-self-insert-command 'flyspell-delayed t)
  13050. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13051. (put 'org-delete-char 'flyspell-delayed t)
  13052. (put 'org-delete-backward-char 'flyspell-delayed t)
  13053. ;; Make pabbrev-mode expand after org-mode commands
  13054. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13055. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13056. ;; How to do this: Measure non-white length of current string
  13057. ;; If equal to column width, we should realign.
  13058. (defun org-remap (map &rest commands)
  13059. "In MAP, remap the functions given in COMMANDS.
  13060. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13061. (let (new old)
  13062. (while commands
  13063. (setq old (pop commands) new (pop commands))
  13064. (if (fboundp 'command-remapping)
  13065. (org-defkey map (vector 'remap old) new)
  13066. (substitute-key-definition old new map global-map)))))
  13067. (when (eq org-enable-table-editor 'optimized)
  13068. ;; If the user wants maximum table support, we need to hijack
  13069. ;; some standard editing functions
  13070. (org-remap org-mode-map
  13071. 'self-insert-command 'org-self-insert-command
  13072. 'delete-char 'org-delete-char
  13073. 'delete-backward-char 'org-delete-backward-char)
  13074. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13075. (defvar org-ctrl-c-ctrl-c-hook nil
  13076. "Hook for functions attaching themselves to `C-c C-c'.
  13077. This can be used to add additional functionality to the C-c C-c key which
  13078. executes context-dependent commands.
  13079. Each function will be called with no arguments. The function must check
  13080. if the context is appropriate for it to act. If yes, it should do its
  13081. thing and then return a non-nil value. If the context is wrong,
  13082. just do nothing and return nil.")
  13083. (defvar org-tab-first-hook nil
  13084. "Hook for functions to attach themselves to TAB.
  13085. See `org-ctrl-c-ctrl-c-hook' for more information.
  13086. This hook runs as the first action when TAB is pressed, even before
  13087. `org-cycle' messes around with the `outline-regexp' to cater for
  13088. inline tasks and plain list item folding.
  13089. If any function in this hook returns t, not other actions like table
  13090. field motion visibility cycling will be done.")
  13091. (defvar org-tab-after-check-for-table-hook nil
  13092. "Hook for functions to attach themselves to TAB.
  13093. See `org-ctrl-c-ctrl-c-hook' for more information.
  13094. This hook runs after it has been established that the cursor is not in a
  13095. table, but before checking if the cursor is in a headline or if global cycling
  13096. should be done.
  13097. If any function in this hook returns t, not other actions like visibility
  13098. cycling will be done.")
  13099. (defvar org-tab-after-check-for-cycling-hook nil
  13100. "Hook for functions to attach themselves to TAB.
  13101. See `org-ctrl-c-ctrl-c-hook' for more information.
  13102. This hook runs after it has been established that not table field motion and
  13103. not visibility should be done because of current context. This is probably
  13104. the place where a package like yasnippets can hook in.")
  13105. (defvar org-metaleft-hook nil
  13106. "Hook for functions attaching themselves to `M-left'.
  13107. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13108. (defvar org-metaright-hook nil
  13109. "Hook for functions attaching themselves to `M-right'.
  13110. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13111. (defvar org-metaup-hook nil
  13112. "Hook for functions attaching themselves to `M-up'.
  13113. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13114. (defvar org-metadown-hook nil
  13115. "Hook for functions attaching themselves to `M-down'.
  13116. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13117. (defvar org-shiftmetaleft-hook nil
  13118. "Hook for functions attaching themselves to `M-S-left'.
  13119. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13120. (defvar org-shiftmetaright-hook nil
  13121. "Hook for functions attaching themselves to `M-S-right'.
  13122. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13123. (defvar org-shiftmetaup-hook nil
  13124. "Hook for functions attaching themselves to `M-S-up'.
  13125. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13126. (defvar org-shiftmetadown-hook nil
  13127. "Hook for functions attaching themselves to `M-S-down'.
  13128. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13129. (defvar org-metareturn-hook nil
  13130. "Hook for functions attaching themselves to `M-RET'.
  13131. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13132. (defun org-modifier-cursor-error ()
  13133. "Throw an error, a modified cursor command was applied in wrong context."
  13134. (error "This command is active in special context like tables, headlines or items"))
  13135. (defun org-shiftselect-error ()
  13136. "Throw an error because Shift-Cursor command was applied in wrong context."
  13137. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13138. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13139. (error "This command works only in special context like headlines or timestamps.")))
  13140. (defun org-call-for-shift-select (cmd)
  13141. (let ((this-command-keys-shift-translated t))
  13142. (call-interactively cmd)))
  13143. (defun org-shifttab (&optional arg)
  13144. "Global visibility cycling or move to previous table field.
  13145. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13146. on context.
  13147. See the individual commands for more information."
  13148. (interactive "P")
  13149. (cond
  13150. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13151. ((integerp arg)
  13152. (message "Content view to level: %d" arg)
  13153. (org-content (prefix-numeric-value arg))
  13154. (setq org-cycle-global-status 'overview))
  13155. (t (call-interactively 'org-global-cycle))))
  13156. (defun org-shiftmetaleft ()
  13157. "Promote subtree or delete table column.
  13158. Calls `org-promote-subtree', `org-outdent-item',
  13159. or `org-table-delete-column', depending on context.
  13160. See the individual commands for more information."
  13161. (interactive)
  13162. (cond
  13163. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13164. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13165. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13166. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13167. (t (org-modifier-cursor-error))))
  13168. (defun org-shiftmetaright ()
  13169. "Demote subtree or insert table column.
  13170. Calls `org-demote-subtree', `org-indent-item',
  13171. or `org-table-insert-column', depending on context.
  13172. See the individual commands for more information."
  13173. (interactive)
  13174. (cond
  13175. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13176. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13177. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13178. ((org-at-item-p) (call-interactively 'org-indent-item))
  13179. (t (org-modifier-cursor-error))))
  13180. (defun org-shiftmetaup (&optional arg)
  13181. "Move subtree up or kill table row.
  13182. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13183. `org-move-item-up' depending on context. See the individual commands
  13184. for more information."
  13185. (interactive "P")
  13186. (cond
  13187. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13188. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13189. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13190. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13191. (t (org-modifier-cursor-error))))
  13192. (defun org-shiftmetadown (&optional arg)
  13193. "Move subtree down or insert table row.
  13194. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13195. `org-move-item-down', depending on context. See the individual
  13196. commands for more information."
  13197. (interactive "P")
  13198. (cond
  13199. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13200. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13201. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13202. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13203. (t (org-modifier-cursor-error))))
  13204. (defun org-metaleft (&optional arg)
  13205. "Promote heading or move table column to left.
  13206. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13207. With no specific context, calls the Emacs default `backward-word'.
  13208. See the individual commands for more information."
  13209. (interactive "P")
  13210. (cond
  13211. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13212. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13213. ((or (org-on-heading-p)
  13214. (and (org-region-active-p)
  13215. (save-excursion
  13216. (goto-char (region-beginning))
  13217. (org-on-heading-p))))
  13218. (call-interactively 'org-do-promote))
  13219. ((or (org-at-item-p)
  13220. (and (org-region-active-p)
  13221. (save-excursion
  13222. (goto-char (region-beginning))
  13223. (org-at-item-p))))
  13224. (call-interactively 'org-outdent-item))
  13225. (t (call-interactively 'backward-word))))
  13226. (defun org-metaright (&optional arg)
  13227. "Demote subtree or move table column to right.
  13228. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13229. With no specific context, calls the Emacs default `forward-word'.
  13230. See the individual commands for more information."
  13231. (interactive "P")
  13232. (cond
  13233. ((run-hook-with-args-until-success 'org-metaright-hook))
  13234. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13235. ((or (org-on-heading-p)
  13236. (and (org-region-active-p)
  13237. (save-excursion
  13238. (goto-char (region-beginning))
  13239. (org-on-heading-p))))
  13240. (call-interactively 'org-do-demote))
  13241. ((or (org-at-item-p)
  13242. (and (org-region-active-p)
  13243. (save-excursion
  13244. (goto-char (region-beginning))
  13245. (org-at-item-p))))
  13246. (call-interactively 'org-indent-item))
  13247. (t (call-interactively 'forward-word))))
  13248. (defun org-metaup (&optional arg)
  13249. "Move subtree up or move table row up.
  13250. Calls `org-move-subtree-up' or `org-table-move-row' or
  13251. `org-move-item-up', depending on context. See the individual commands
  13252. for more information."
  13253. (interactive "P")
  13254. (cond
  13255. ((run-hook-with-args-until-success 'org-metaup-hook))
  13256. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13257. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13258. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13259. (t (transpose-lines 1) (beginning-of-line -1))))
  13260. (defun org-metadown (&optional arg)
  13261. "Move subtree down or move table row down.
  13262. Calls `org-move-subtree-down' or `org-table-move-row' or
  13263. `org-move-item-down', depending on context. See the individual
  13264. commands for more information."
  13265. (interactive "P")
  13266. (cond
  13267. ((run-hook-with-args-until-success 'org-metadown-hook))
  13268. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13269. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13270. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13271. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13272. (defun org-shiftup (&optional arg)
  13273. "Increase item in timestamp or increase priority of current headline.
  13274. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13275. depending on context. See the individual commands for more information."
  13276. (interactive "P")
  13277. (cond
  13278. ((and org-support-shift-select (org-region-active-p))
  13279. (org-call-for-shift-select 'previous-line))
  13280. ((org-at-timestamp-p t)
  13281. (call-interactively (if org-edit-timestamp-down-means-later
  13282. 'org-timestamp-down 'org-timestamp-up)))
  13283. ((and (not (eq org-support-shift-select 'always))
  13284. org-enable-priority-commands
  13285. (org-on-heading-p))
  13286. (call-interactively 'org-priority-up))
  13287. ((and (not org-support-shift-select) (org-at-item-p))
  13288. (call-interactively 'org-previous-item))
  13289. ((org-clocktable-try-shift 'up arg))
  13290. (org-support-shift-select
  13291. (org-call-for-shift-select 'previous-line))
  13292. (t (org-shiftselect-error))))
  13293. (defun org-shiftdown (&optional arg)
  13294. "Decrease item in timestamp or decrease priority of current headline.
  13295. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13296. depending on context. See the individual commands for more information."
  13297. (interactive "P")
  13298. (cond
  13299. ((and org-support-shift-select (org-region-active-p))
  13300. (org-call-for-shift-select 'next-line))
  13301. ((org-at-timestamp-p t)
  13302. (call-interactively (if org-edit-timestamp-down-means-later
  13303. 'org-timestamp-up 'org-timestamp-down)))
  13304. ((and (not (eq org-support-shift-select 'always))
  13305. org-enable-priority-commands
  13306. (org-on-heading-p))
  13307. (call-interactively 'org-priority-down))
  13308. ((and (not org-support-shift-select) (org-at-item-p))
  13309. (call-interactively 'org-next-item))
  13310. ((org-clocktable-try-shift 'down arg))
  13311. (org-support-shift-select
  13312. (org-call-for-shift-select 'next-line))
  13313. (t (org-shiftselect-error))))
  13314. (defun org-shiftright (&optional arg)
  13315. "Cycle the thing at point or in the current line, depending on context.
  13316. Depending on context, this does one of the following:
  13317. - switch a timestamp at point one day into the future
  13318. - on a headline, switch to the next TODO keyword.
  13319. - on an item, switch entire list to the next bullet type
  13320. - on a property line, switch to the next allowed value
  13321. - on a clocktable definition line, move time block into the future"
  13322. (interactive "P")
  13323. (cond
  13324. ((and org-support-shift-select (org-region-active-p))
  13325. (org-call-for-shift-select 'forward-char))
  13326. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13327. ((and (not (eq org-support-shift-select 'always))
  13328. (org-on-heading-p))
  13329. (let ((org-inhibit-logging
  13330. (not org-treat-S-cursor-todo-selection-as-state-change))
  13331. (org-inhibit-blocking
  13332. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13333. (org-call-with-arg 'org-todo 'right)))
  13334. ((or (and org-support-shift-select
  13335. (not (eq org-support-shift-select 'always))
  13336. (org-at-item-bullet-p))
  13337. (and (not org-support-shift-select) (org-at-item-p)))
  13338. (org-call-with-arg 'org-cycle-list-bullet nil))
  13339. ((and (not (eq org-support-shift-select 'always))
  13340. (org-at-property-p))
  13341. (call-interactively 'org-property-next-allowed-value))
  13342. ((org-clocktable-try-shift 'right arg))
  13343. (org-support-shift-select
  13344. (org-call-for-shift-select 'forward-char))
  13345. (t (org-shiftselect-error))))
  13346. (defun org-shiftleft (&optional arg)
  13347. "Cycle the thing at point or in the current line, depending on context.
  13348. Depending on context, this does one of the following:
  13349. - switch a timestamp at point one day into the past
  13350. - on a headline, switch to the previous TODO keyword.
  13351. - on an item, switch entire list to the previous bullet type
  13352. - on a property line, switch to the previous allowed value
  13353. - on a clocktable definition line, move time block into the past"
  13354. (interactive "P")
  13355. (cond
  13356. ((and org-support-shift-select (org-region-active-p))
  13357. (org-call-for-shift-select 'backward-char))
  13358. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13359. ((and (not (eq org-support-shift-select 'always))
  13360. (org-on-heading-p))
  13361. (let ((org-inhibit-logging
  13362. (not org-treat-S-cursor-todo-selection-as-state-change))
  13363. (org-inhibit-blocking
  13364. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13365. (org-call-with-arg 'org-todo 'left)))
  13366. ((or (and org-support-shift-select
  13367. (not (eq org-support-shift-select 'always))
  13368. (org-at-item-bullet-p))
  13369. (and (not org-support-shift-select) (org-at-item-p)))
  13370. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13371. ((and (not (eq org-support-shift-select 'always))
  13372. (org-at-property-p))
  13373. (call-interactively 'org-property-previous-allowed-value))
  13374. ((org-clocktable-try-shift 'left arg))
  13375. (org-support-shift-select
  13376. (org-call-for-shift-select 'backward-char))
  13377. (t (org-shiftselect-error))))
  13378. (defun org-shiftcontrolright ()
  13379. "Switch to next TODO set."
  13380. (interactive)
  13381. (cond
  13382. ((and org-support-shift-select (org-region-active-p))
  13383. (org-call-for-shift-select 'forward-word))
  13384. ((and (not (eq org-support-shift-select 'always))
  13385. (org-on-heading-p))
  13386. (org-call-with-arg 'org-todo 'nextset))
  13387. (org-support-shift-select
  13388. (org-call-for-shift-select 'forward-word))
  13389. (t (org-shiftselect-error))))
  13390. (defun org-shiftcontrolleft ()
  13391. "Switch to previous TODO set."
  13392. (interactive)
  13393. (cond
  13394. ((and org-support-shift-select (org-region-active-p))
  13395. (org-call-for-shift-select 'backward-word))
  13396. ((and (not (eq org-support-shift-select 'always))
  13397. (org-on-heading-p))
  13398. (org-call-with-arg 'org-todo 'previousset))
  13399. (org-support-shift-select
  13400. (org-call-for-shift-select 'backward-word))
  13401. (t (org-shiftselect-error))))
  13402. (defun org-ctrl-c-ret ()
  13403. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13404. (interactive)
  13405. (cond
  13406. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13407. (t (call-interactively 'org-insert-heading))))
  13408. (defun org-copy-special ()
  13409. "Copy region in table or copy current subtree.
  13410. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13411. See the individual commands for more information."
  13412. (interactive)
  13413. (call-interactively
  13414. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13415. (defun org-cut-special ()
  13416. "Cut region in table or cut current subtree.
  13417. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13418. See the individual commands for more information."
  13419. (interactive)
  13420. (call-interactively
  13421. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13422. (defun org-paste-special (arg)
  13423. "Paste rectangular region into table, or past subtree relative to level.
  13424. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13425. See the individual commands for more information."
  13426. (interactive "P")
  13427. (if (org-at-table-p)
  13428. (org-table-paste-rectangle)
  13429. (org-paste-subtree arg)))
  13430. (defun org-edit-special ()
  13431. "Call a special editor for the stuff at point.
  13432. When at a table, call the formula editor with `org-table-edit-formulas'.
  13433. When at the first line of an src example, call `org-edit-src-code'.
  13434. When in an #+include line, visit the include file. Otherwise call
  13435. `ffap' to visit the file at point."
  13436. (interactive)
  13437. (cond
  13438. ((org-at-table-p)
  13439. (call-interactively 'org-table-edit-formulas))
  13440. ((save-excursion
  13441. (beginning-of-line 1)
  13442. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13443. (find-file (org-trim (match-string 1))))
  13444. ((org-edit-src-code))
  13445. ((org-edit-fixed-width-region))
  13446. (t (call-interactively 'ffap))))
  13447. (defun org-ctrl-c-ctrl-c (&optional arg)
  13448. "Set tags in headline, or update according to changed information at point.
  13449. This command does many different things, depending on context:
  13450. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13451. this is what we do.
  13452. - If the cursor is in a headline, prompt for tags and insert them
  13453. into the current line, aligned to `org-tags-column'. When called
  13454. with prefix arg, realign all tags in the current buffer.
  13455. - If the cursor is in one of the special #+KEYWORD lines, this
  13456. triggers scanning the buffer for these lines and updating the
  13457. information.
  13458. - If the cursor is inside a table, realign the table. This command
  13459. works even if the automatic table editor has been turned off.
  13460. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13461. the entire table.
  13462. - If the cursor is at a footnote reference or definition, jump to
  13463. the corresponding definition or references, respectively.
  13464. - If the cursor is a the beginning of a dynamic block, update it.
  13465. - If the cursor is inside a table created by the table.el package,
  13466. activate that table.
  13467. - If the current buffer is a remember buffer, close note and file
  13468. it. A prefix argument of 1 files to the default location
  13469. without further interaction. A prefix argument of 2 files to
  13470. the currently clocking task.
  13471. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13472. links in this buffer.
  13473. - If the cursor is on a numbered item in a plain list, renumber the
  13474. ordered list.
  13475. - If the cursor is on a checkbox, toggle it."
  13476. (interactive "P")
  13477. (let ((org-enable-table-editor t))
  13478. (cond
  13479. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13480. org-occur-highlights
  13481. org-latex-fragment-image-overlays)
  13482. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13483. (org-remove-occur-highlights)
  13484. (org-remove-latex-fragment-image-overlays)
  13485. (message "Temporary highlights/overlays removed from current buffer"))
  13486. ((and (local-variable-p 'org-finish-function (current-buffer))
  13487. (fboundp org-finish-function))
  13488. (funcall org-finish-function))
  13489. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13490. ((org-at-property-p)
  13491. (call-interactively 'org-property-action))
  13492. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13493. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13494. ((org-at-table.el-p)
  13495. (require 'table)
  13496. (beginning-of-line 1)
  13497. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13498. (call-interactively 'table-recognize-table))
  13499. ((org-at-table-p)
  13500. (org-table-maybe-eval-formula)
  13501. (if arg
  13502. (call-interactively 'org-table-recalculate)
  13503. (org-table-maybe-recalculate-line))
  13504. (call-interactively 'org-table-align))
  13505. ((or (org-footnote-at-reference-p)
  13506. (org-footnote-at-definition-p))
  13507. (call-interactively 'org-footnote-action))
  13508. ((org-at-item-checkbox-p)
  13509. (call-interactively 'org-toggle-checkbox))
  13510. ((org-at-item-p)
  13511. (if arg
  13512. (call-interactively 'org-toggle-checkbox)
  13513. (call-interactively 'org-maybe-renumber-ordered-list)))
  13514. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13515. ;; Dynamic block
  13516. (beginning-of-line 1)
  13517. (save-excursion (org-update-dblock)))
  13518. ((save-excursion
  13519. (beginning-of-line 1)
  13520. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13521. (cond
  13522. ((equal (match-string 1) "TBLFM")
  13523. ;; Recalculate the table before this line
  13524. (save-excursion
  13525. (beginning-of-line 1)
  13526. (skip-chars-backward " \r\n\t")
  13527. (if (org-at-table-p)
  13528. (org-call-with-arg 'org-table-recalculate t))))
  13529. (t
  13530. ; (org-set-regexps-and-options)
  13531. ; (org-restart-font-lock)
  13532. (let ((org-inhibit-startup t)) (org-mode-restart))
  13533. (message "Local setup has been refreshed"))))
  13534. ((org-clock-update-time-maybe))
  13535. (t (error "C-c C-c can do nothing useful at this location.")))))
  13536. (defun org-mode-restart ()
  13537. "Restart Org-mode, to scan again for special lines.
  13538. Also updates the keyword regular expressions."
  13539. (interactive)
  13540. (org-mode)
  13541. (message "Org-mode restarted"))
  13542. (defun org-kill-note-or-show-branches ()
  13543. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13544. (interactive)
  13545. (if (not org-finish-function)
  13546. (call-interactively 'show-branches)
  13547. (let ((org-note-abort t))
  13548. (funcall org-finish-function))))
  13549. (defun org-return (&optional indent)
  13550. "Goto next table row or insert a newline.
  13551. Calls `org-table-next-row' or `newline', depending on context.
  13552. See the individual commands for more information."
  13553. (interactive)
  13554. (cond
  13555. ((bobp) (if indent (newline-and-indent) (newline)))
  13556. ((org-at-table-p)
  13557. (org-table-justify-field-maybe)
  13558. (call-interactively 'org-table-next-row))
  13559. ((and org-return-follows-link
  13560. (eq (get-text-property (point) 'face) 'org-link))
  13561. (call-interactively 'org-open-at-point))
  13562. ((and (org-at-heading-p)
  13563. (looking-at
  13564. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13565. (org-show-entry)
  13566. (end-of-line 1)
  13567. (newline))
  13568. (t (if indent (newline-and-indent) (newline)))))
  13569. (defun org-return-indent ()
  13570. "Goto next table row or insert a newline and indent.
  13571. Calls `org-table-next-row' or `newline-and-indent', depending on
  13572. context. See the individual commands for more information."
  13573. (interactive)
  13574. (org-return t))
  13575. (defun org-ctrl-c-star ()
  13576. "Compute table, or change heading status of lines.
  13577. Calls `org-table-recalculate' or `org-toggle-heading',
  13578. depending on context."
  13579. (interactive)
  13580. (cond
  13581. ((org-at-table-p)
  13582. (call-interactively 'org-table-recalculate))
  13583. (t
  13584. ;; Convert all lines in region to list items
  13585. (call-interactively 'org-toggle-heading))))
  13586. (defun org-ctrl-c-minus ()
  13587. "Insert separator line in table or modify bullet status of line.
  13588. Also turns a plain line or a region of lines into list items.
  13589. Calls `org-table-insert-hline', `org-toggle-item', or
  13590. `org-cycle-list-bullet', depending on context."
  13591. (interactive)
  13592. (cond
  13593. ((org-at-table-p)
  13594. (call-interactively 'org-table-insert-hline))
  13595. ((org-region-active-p)
  13596. (call-interactively 'org-toggle-item))
  13597. ((org-in-item-p)
  13598. (call-interactively 'org-cycle-list-bullet))
  13599. (t
  13600. (call-interactively 'org-toggle-item))))
  13601. (defun org-toggle-item ()
  13602. "Convert headings or normal lines to items, items to normal lines.
  13603. If there is no active region, only the current line is considered.
  13604. If the first line in the region is a headline, convert all headlines to items.
  13605. If the first line in the region is an item, convert all items to normal lines.
  13606. If the first line is normal text, add an item bullet to each line."
  13607. (interactive)
  13608. (let (l2 l beg end)
  13609. (if (org-region-active-p)
  13610. (setq beg (region-beginning) end (region-end))
  13611. (setq beg (point-at-bol)
  13612. end (min (1+ (point-at-eol)) (point-max))))
  13613. (save-excursion
  13614. (goto-char end)
  13615. (setq l2 (org-current-line))
  13616. (goto-char beg)
  13617. (beginning-of-line 1)
  13618. (setq l (1- (org-current-line)))
  13619. (if (org-at-item-p)
  13620. ;; We already have items, de-itemize
  13621. (while (< (setq l (1+ l)) l2)
  13622. (when (org-at-item-p)
  13623. (goto-char (match-beginning 2))
  13624. (delete-region (match-beginning 2) (match-end 2))
  13625. (and (looking-at "[ \t]+") (replace-match "")))
  13626. (beginning-of-line 2))
  13627. (if (org-on-heading-p)
  13628. ;; Headings, convert to items
  13629. (while (< (setq l (1+ l)) l2)
  13630. (if (looking-at org-outline-regexp)
  13631. (replace-match "- " t t))
  13632. (beginning-of-line 2))
  13633. ;; normal lines, turn them into items
  13634. (while (< (setq l (1+ l)) l2)
  13635. (unless (org-at-item-p)
  13636. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13637. (replace-match "\\1- \\2")))
  13638. (beginning-of-line 2)))))))
  13639. (defun org-toggle-heading (&optional nstars)
  13640. "Convert headings to normal text, or items or text to headings.
  13641. If there is no active region, only the current line is considered.
  13642. If the first line is a heading, remove the stars from all headlines
  13643. in the region.
  13644. If the first line is a plain list item, turn all plain list items
  13645. into headings.
  13646. If the first line is a normal line, turn each and every line in the
  13647. region into a heading.
  13648. When converting a line into a heading, the number of stars is chosen
  13649. such that the lines become children of the current entry. However,
  13650. when a prefix argument is given, its value determines the number of
  13651. stars to add."
  13652. (interactive "P")
  13653. (let (l2 l itemp beg end)
  13654. (if (org-region-active-p)
  13655. (setq beg (region-beginning) end (region-end))
  13656. (setq beg (point-at-bol)
  13657. end (min (1+ (point-at-eol)) (point-max))))
  13658. (save-excursion
  13659. (goto-char end)
  13660. (setq l2 (org-current-line))
  13661. (goto-char beg)
  13662. (beginning-of-line 1)
  13663. (setq l (1- (org-current-line)))
  13664. (if (org-on-heading-p)
  13665. ;; We already have headlines, de-star them
  13666. (while (< (setq l (1+ l)) l2)
  13667. (when (org-on-heading-p t)
  13668. (and (looking-at outline-regexp) (replace-match "")))
  13669. (beginning-of-line 2))
  13670. (setq itemp (org-at-item-p))
  13671. (let* ((stars
  13672. (if nstars
  13673. (make-string (prefix-numeric-value current-prefix-arg)
  13674. ?*)
  13675. (save-excursion
  13676. (if (re-search-backward org-complex-heading-regexp nil t)
  13677. (match-string 1) ""))))
  13678. (add-stars (cond (nstars "")
  13679. ((equal stars "") "*")
  13680. (org-odd-levels-only "**")
  13681. (t "*")))
  13682. (rpl (concat stars add-stars " ")))
  13683. (while (< (setq l (1+ l)) l2)
  13684. (if itemp
  13685. (and (org-at-item-p) (replace-match rpl t t))
  13686. (unless (org-on-heading-p)
  13687. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13688. (replace-match (concat rpl (match-string 2))))))
  13689. (beginning-of-line 2)))))))
  13690. (defun org-meta-return (&optional arg)
  13691. "Insert a new heading or wrap a region in a table.
  13692. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13693. See the individual commands for more information."
  13694. (interactive "P")
  13695. (cond
  13696. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13697. ((org-at-table-p)
  13698. (call-interactively 'org-table-wrap-region))
  13699. (t (call-interactively 'org-insert-heading))))
  13700. ;;; Menu entries
  13701. ;; Define the Org-mode menus
  13702. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13703. '("Tbl"
  13704. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13705. ["Next Field" org-cycle (org-at-table-p)]
  13706. ["Previous Field" org-shifttab (org-at-table-p)]
  13707. ["Next Row" org-return (org-at-table-p)]
  13708. "--"
  13709. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13710. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13711. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13712. "--"
  13713. ("Column"
  13714. ["Move Column Left" org-metaleft (org-at-table-p)]
  13715. ["Move Column Right" org-metaright (org-at-table-p)]
  13716. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13717. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13718. ("Row"
  13719. ["Move Row Up" org-metaup (org-at-table-p)]
  13720. ["Move Row Down" org-metadown (org-at-table-p)]
  13721. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13722. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13723. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13724. "--"
  13725. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13726. ("Rectangle"
  13727. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13728. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13729. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13730. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13731. "--"
  13732. ("Calculate"
  13733. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13734. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13735. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13736. "--"
  13737. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13738. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13739. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13740. "--"
  13741. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13742. "--"
  13743. ["Sum Column/Rectangle" org-table-sum
  13744. (or (org-at-table-p) (org-region-active-p))]
  13745. ["Which Column?" org-table-current-column (org-at-table-p)])
  13746. ["Debug Formulas"
  13747. org-table-toggle-formula-debugger
  13748. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13749. ["Show Col/Row Numbers"
  13750. org-table-toggle-coordinate-overlays
  13751. :style toggle
  13752. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13753. "--"
  13754. ["Create" org-table-create (and (not (org-at-table-p))
  13755. org-enable-table-editor)]
  13756. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13757. ["Import from File" org-table-import (not (org-at-table-p))]
  13758. ["Export to File" org-table-export (org-at-table-p)]
  13759. "--"
  13760. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13761. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13762. '("Org"
  13763. ("Show/Hide"
  13764. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13765. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13766. ["Sparse Tree..." org-sparse-tree t]
  13767. ["Reveal Context" org-reveal t]
  13768. ["Show All" show-all t]
  13769. "--"
  13770. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13771. "--"
  13772. ["New Heading" org-insert-heading t]
  13773. ("Navigate Headings"
  13774. ["Up" outline-up-heading t]
  13775. ["Next" outline-next-visible-heading t]
  13776. ["Previous" outline-previous-visible-heading t]
  13777. ["Next Same Level" outline-forward-same-level t]
  13778. ["Previous Same Level" outline-backward-same-level t]
  13779. "--"
  13780. ["Jump" org-goto t])
  13781. ("Edit Structure"
  13782. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13783. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13784. "--"
  13785. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13786. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13787. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13788. "--"
  13789. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13790. "--"
  13791. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13792. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13793. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13794. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13795. "--"
  13796. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13797. "--"
  13798. ["Convert to odd levels" org-convert-to-odd-levels t]
  13799. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13800. ("Editing"
  13801. ["Emphasis..." org-emphasize t]
  13802. ["Edit Source Example" org-edit-special t]
  13803. "--"
  13804. ["Footnote new/jump" org-footnote-action t]
  13805. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13806. ("Archive"
  13807. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13808. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13809. ; :active t :keys "C-u C-c C-x C-a"]
  13810. ["Sparse trees open ARCHIVE trees"
  13811. (setq org-sparse-tree-open-archived-trees
  13812. (not org-sparse-tree-open-archived-trees))
  13813. :style toggle :selected org-sparse-tree-open-archived-trees]
  13814. ["Cycling opens ARCHIVE trees"
  13815. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13816. :style toggle :selected org-cycle-open-archived-trees]
  13817. "--"
  13818. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13819. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13820. ; ["Check and Move Children" (org-archive-subtree '(4))
  13821. ; :active t :keys "C-u C-c C-x C-s"]
  13822. )
  13823. "--"
  13824. ("Hyperlinks"
  13825. ["Store Link (Global)" org-store-link t]
  13826. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13827. ["Insert Link" org-insert-link t]
  13828. ["Follow Link" org-open-at-point t]
  13829. "--"
  13830. ["Next link" org-next-link t]
  13831. ["Previous link" org-previous-link t]
  13832. "--"
  13833. ["Descriptive Links"
  13834. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13835. :style radio
  13836. :selected (member '(org-link) buffer-invisibility-spec)]
  13837. ["Literal Links"
  13838. (progn
  13839. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13840. :style radio
  13841. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13842. "--"
  13843. ("TODO Lists"
  13844. ["TODO/DONE/-" org-todo t]
  13845. ("Select keyword"
  13846. ["Next keyword" org-shiftright (org-on-heading-p)]
  13847. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13848. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13849. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13850. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13851. ["Show TODO Tree" org-show-todo-tree t]
  13852. ["Global TODO list" org-todo-list t]
  13853. "--"
  13854. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13855. :selected org-enforce-todo-dependencies :style toggle :active t]
  13856. "Settings for tree at point"
  13857. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13858. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13859. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13860. ["Do Children parallel" org-toggle-ordered-property :style radio
  13861. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13862. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13863. "--"
  13864. ["Set Priority" org-priority t]
  13865. ["Priority Up" org-shiftup t]
  13866. ["Priority Down" org-shiftdown t]
  13867. "--"
  13868. ["Get news from all feeds" org-feed-update-all t]
  13869. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13870. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13871. ("TAGS and Properties"
  13872. ["Set Tags" org-set-tags-command t]
  13873. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13874. "--"
  13875. ["Set property" org-set-property t]
  13876. ["Column view of properties" org-columns t]
  13877. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13878. ("Dates and Scheduling"
  13879. ["Timestamp" org-time-stamp t]
  13880. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13881. ("Change Date"
  13882. ["1 Day Later" org-shiftright t]
  13883. ["1 Day Earlier" org-shiftleft t]
  13884. ["1 ... Later" org-shiftup t]
  13885. ["1 ... Earlier" org-shiftdown t])
  13886. ["Compute Time Range" org-evaluate-time-range t]
  13887. ["Schedule Item" org-schedule t]
  13888. ["Deadline" org-deadline t]
  13889. "--"
  13890. ["Custom time format" org-toggle-time-stamp-overlays
  13891. :style radio :selected org-display-custom-times]
  13892. "--"
  13893. ["Goto Calendar" org-goto-calendar t]
  13894. ["Date from Calendar" org-date-from-calendar t]
  13895. "--"
  13896. ["Start/Restart Timer" org-timer-start t]
  13897. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13898. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13899. ["Insert Timer String" org-timer t]
  13900. ["Insert Timer Item" org-timer-item t])
  13901. ("Logging work"
  13902. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  13903. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  13904. ["Clock out" org-clock-out t]
  13905. ["Clock cancel" org-clock-cancel t]
  13906. "--"
  13907. ["Mark as default task" org-clock-mark-default-task t]
  13908. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  13909. ["Goto running clock" org-clock-goto t]
  13910. "--"
  13911. ["Display times" org-clock-display t]
  13912. ["Create clock table" org-clock-report t]
  13913. "--"
  13914. ["Record DONE time"
  13915. (progn (setq org-log-done (not org-log-done))
  13916. (message "Switching to %s will %s record a timestamp"
  13917. (car org-done-keywords)
  13918. (if org-log-done "automatically" "not")))
  13919. :style toggle :selected org-log-done])
  13920. "--"
  13921. ["Agenda Command..." org-agenda t]
  13922. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13923. ("File List for Agenda")
  13924. ("Special views current file"
  13925. ["TODO Tree" org-show-todo-tree t]
  13926. ["Check Deadlines" org-check-deadlines t]
  13927. ["Timeline" org-timeline t]
  13928. ["Tags/Property tree" org-match-sparse-tree t])
  13929. "--"
  13930. ["Export/Publish..." org-export t]
  13931. ("LaTeX"
  13932. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13933. :selected org-cdlatex-mode]
  13934. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13935. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13936. ["Modify math symbol" org-cdlatex-math-modify
  13937. (org-inside-LaTeX-fragment-p)]
  13938. ["Insert citation" org-reftex-citation t]
  13939. "--"
  13940. ["Export LaTeX fragments as images"
  13941. (if (featurep 'org-exp)
  13942. (setq org-export-with-LaTeX-fragments
  13943. (not org-export-with-LaTeX-fragments))
  13944. (require 'org-exp))
  13945. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13946. org-export-with-LaTeX-fragments)])
  13947. "--"
  13948. ("Documentation"
  13949. ["Show Version" org-version t]
  13950. ["Info Documentation" org-info t])
  13951. ("Customize"
  13952. ["Browse Org Group" org-customize t]
  13953. "--"
  13954. ["Expand This Menu" org-create-customize-menu
  13955. (fboundp 'customize-menu-create)])
  13956. "--"
  13957. ("Refresh/Reload"
  13958. ["Refresh setup current buffer" org-mode-restart t]
  13959. ["Reload Org (after update)" org-reload t]
  13960. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13961. ))
  13962. (defun org-info (&optional node)
  13963. "Read documentation for Org-mode in the info system.
  13964. With optional NODE, go directly to that node."
  13965. (interactive)
  13966. (info (format "(org)%s" (or node ""))))
  13967. (defun org-install-agenda-files-menu ()
  13968. (let ((bl (buffer-list)))
  13969. (save-excursion
  13970. (while bl
  13971. (set-buffer (pop bl))
  13972. (if (org-mode-p) (setq bl nil)))
  13973. (when (org-mode-p)
  13974. (easy-menu-change
  13975. '("Org") "File List for Agenda"
  13976. (append
  13977. (list
  13978. ["Edit File List" (org-edit-agenda-file-list) t]
  13979. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13980. ["Remove Current File from List" org-remove-file t]
  13981. ["Cycle through agenda files" org-cycle-agenda-files t]
  13982. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13983. "--")
  13984. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13985. ;;;; Documentation
  13986. ;;;###autoload
  13987. (defun org-require-autoloaded-modules ()
  13988. (interactive)
  13989. (mapc 'require
  13990. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13991. org-docbook org-exp org-html org-icalendar
  13992. org-id org-latex
  13993. org-publish org-remember org-table
  13994. org-timer org-xoxo)))
  13995. ;;;###autoload
  13996. (defun org-reload (&optional uncompiled)
  13997. "Reload all org lisp files.
  13998. With prefix arg UNCOMPILED, load the uncompiled versions."
  13999. (interactive "P")
  14000. (require 'find-func)
  14001. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14002. (dir-org (file-name-directory (org-find-library-name "org")))
  14003. (dir-org-contrib (ignore-errors
  14004. (file-name-directory
  14005. (org-find-library-name "org-contribdir"))))
  14006. (files
  14007. (append (directory-files dir-org t file-re)
  14008. (and dir-org-contrib
  14009. (directory-files dir-org-contrib t file-re))))
  14010. (remove-re (concat (if (featurep 'xemacs)
  14011. "org-colview" "org-colview-xemacs")
  14012. "\\'")))
  14013. (setq files (mapcar 'file-name-sans-extension files))
  14014. (setq files (mapcar
  14015. (lambda (x) (if (string-match remove-re x) nil x))
  14016. files))
  14017. (setq files (delq nil files))
  14018. (mapc
  14019. (lambda (f)
  14020. (when (featurep (intern (file-name-nondirectory f)))
  14021. (if (and (not uncompiled)
  14022. (file-exists-p (concat f ".elc")))
  14023. (load (concat f ".elc") nil nil t)
  14024. (load (concat f ".el") nil nil t))))
  14025. files))
  14026. (org-version))
  14027. ;;;###autoload
  14028. (defun org-customize ()
  14029. "Call the customize function with org as argument."
  14030. (interactive)
  14031. (org-load-modules-maybe)
  14032. (org-require-autoloaded-modules)
  14033. (customize-browse 'org))
  14034. (defun org-create-customize-menu ()
  14035. "Create a full customization menu for Org-mode, insert it into the menu."
  14036. (interactive)
  14037. (org-load-modules-maybe)
  14038. (org-require-autoloaded-modules)
  14039. (if (fboundp 'customize-menu-create)
  14040. (progn
  14041. (easy-menu-change
  14042. '("Org") "Customize"
  14043. `(["Browse Org group" org-customize t]
  14044. "--"
  14045. ,(customize-menu-create 'org)
  14046. ["Set" Custom-set t]
  14047. ["Save" Custom-save t]
  14048. ["Reset to Current" Custom-reset-current t]
  14049. ["Reset to Saved" Custom-reset-saved t]
  14050. ["Reset to Standard Settings" Custom-reset-standard t]))
  14051. (message "\"Org\"-menu now contains full customization menu"))
  14052. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14053. ;;;; Miscellaneous stuff
  14054. ;;; Generally useful functions
  14055. (defun org-find-text-property-in-string (prop s)
  14056. "Return the first non-nil value of property PROP in string S."
  14057. (or (get-text-property 0 prop s)
  14058. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14059. prop s)))
  14060. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14061. "Display the given MESSAGE as a warning."
  14062. (if (fboundp 'display-warning)
  14063. (display-warning 'org message
  14064. (if (featurep 'xemacs)
  14065. 'warning
  14066. :warning))
  14067. (let ((buf (get-buffer-create "*Org warnings*")))
  14068. (with-current-buffer buf
  14069. (goto-char (point-max))
  14070. (insert "Warning (Org): " message)
  14071. (unless (bolp)
  14072. (newline)))
  14073. (display-buffer buf)
  14074. (sit-for 0))))
  14075. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14076. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14077. (if (and marker (marker-buffer marker)
  14078. (buffer-live-p (marker-buffer marker)))
  14079. (progn
  14080. (switch-to-buffer (marker-buffer marker))
  14081. (if (or (> marker (point-max)) (< marker (point-min)))
  14082. (widen))
  14083. (goto-char marker)
  14084. (org-show-context 'org-goto))
  14085. (if bookmark
  14086. (bookmark-jump bookmark)
  14087. (error "Cannot find location"))))
  14088. (defun org-quote-csv-field (s)
  14089. "Quote field for inclusion in CSV material."
  14090. (if (string-match "[\",]" s)
  14091. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14092. s))
  14093. (defun org-plist-delete (plist property)
  14094. "Delete PROPERTY from PLIST.
  14095. This is in contrast to merely setting it to 0."
  14096. (let (p)
  14097. (while plist
  14098. (if (not (eq property (car plist)))
  14099. (setq p (plist-put p (car plist) (nth 1 plist))))
  14100. (setq plist (cddr plist)))
  14101. p))
  14102. (defun org-force-self-insert (N)
  14103. "Needed to enforce self-insert under remapping."
  14104. (interactive "p")
  14105. (self-insert-command N))
  14106. (defun org-string-width (s)
  14107. "Compute width of string, ignoring invisible characters.
  14108. This ignores character with invisibility property `org-link', and also
  14109. characters with property `org-cwidth', because these will become invisible
  14110. upon the next fontification round."
  14111. (let (b l)
  14112. (when (or (eq t buffer-invisibility-spec)
  14113. (assq 'org-link buffer-invisibility-spec))
  14114. (while (setq b (text-property-any 0 (length s)
  14115. 'invisible 'org-link s))
  14116. (setq s (concat (substring s 0 b)
  14117. (substring s (or (next-single-property-change
  14118. b 'invisible s) (length s)))))))
  14119. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14120. (setq s (concat (substring s 0 b)
  14121. (substring s (or (next-single-property-change
  14122. b 'org-cwidth s) (length s))))))
  14123. (setq l (string-width s) b -1)
  14124. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14125. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14126. l))
  14127. (defun org-get-indentation (&optional line)
  14128. "Get the indentation of the current line, interpreting tabs.
  14129. When LINE is given, assume it represents a line and compute its indentation."
  14130. (if line
  14131. (if (string-match "^ *" (org-remove-tabs line))
  14132. (match-end 0))
  14133. (save-excursion
  14134. (beginning-of-line 1)
  14135. (skip-chars-forward " \t")
  14136. (current-column))))
  14137. (defun org-remove-tabs (s &optional width)
  14138. "Replace tabulators in S with spaces.
  14139. Assumes that s is a single line, starting in column 0."
  14140. (setq width (or width tab-width))
  14141. (while (string-match "\t" s)
  14142. (setq s (replace-match
  14143. (make-string
  14144. (- (* width (/ (+ (match-beginning 0) width) width))
  14145. (match-beginning 0)) ?\ )
  14146. t t s)))
  14147. s)
  14148. (defun org-fix-indentation (line ind)
  14149. "Fix indentation in LINE.
  14150. IND is a cons cell with target and minimum indentation.
  14151. If the current indentation in LINE is smaller than the minimum,
  14152. leave it alone. If it is larger than ind, set it to the target."
  14153. (let* ((l (org-remove-tabs line))
  14154. (i (org-get-indentation l))
  14155. (i1 (car ind)) (i2 (cdr ind)))
  14156. (if (>= i i2) (setq l (substring line i2)))
  14157. (if (> i1 0)
  14158. (concat (make-string i1 ?\ ) l)
  14159. l)))
  14160. (defun org-remove-indentation (code &optional n)
  14161. "Remove the maximum common indentation from the lines in CODE.
  14162. N may optionally be the number of spaces to remove."
  14163. (with-temp-buffer
  14164. (insert code)
  14165. (org-do-remove-indentation n)
  14166. (buffer-string)))
  14167. (defun org-do-remove-indentation (&optional n)
  14168. "Remove the maximum common indentation from the buffer."
  14169. (untabify (point-min) (point-max))
  14170. (let ((min 10000) re)
  14171. (if n
  14172. (setq min n)
  14173. (goto-char (point-min))
  14174. (while (re-search-forward "^ *[^ \n]" nil t)
  14175. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14176. (unless (or (= min 0) (= min 10000))
  14177. (setq re (format "^ \\{%d\\}" min))
  14178. (goto-char (point-min))
  14179. (while (re-search-forward re nil t)
  14180. (replace-match "")
  14181. (end-of-line 1))
  14182. min)))
  14183. (defun org-base-buffer (buffer)
  14184. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14185. (if (not buffer)
  14186. buffer
  14187. (or (buffer-base-buffer buffer)
  14188. buffer)))
  14189. (defun org-trim (s)
  14190. "Remove whitespace at beginning and end of string."
  14191. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14192. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14193. s)
  14194. (defun org-wrap (string &optional width lines)
  14195. "Wrap string to either a number of lines, or a width in characters.
  14196. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14197. that costs. If there is a word longer than WIDTH, the text is actually
  14198. wrapped to the length of that word.
  14199. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14200. many lines, whatever width that takes.
  14201. The return value is a list of lines, without newlines at the end."
  14202. (let* ((words (org-split-string string "[ \t\n]+"))
  14203. (maxword (apply 'max (mapcar 'org-string-width words)))
  14204. w ll)
  14205. (cond (width
  14206. (org-do-wrap words (max maxword width)))
  14207. (lines
  14208. (setq w maxword)
  14209. (setq ll (org-do-wrap words maxword))
  14210. (if (<= (length ll) lines)
  14211. ll
  14212. (setq ll words)
  14213. (while (> (length ll) lines)
  14214. (setq w (1+ w))
  14215. (setq ll (org-do-wrap words w)))
  14216. ll))
  14217. (t (error "Cannot wrap this")))))
  14218. (defun org-do-wrap (words width)
  14219. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14220. (let (lines line)
  14221. (while words
  14222. (setq line (pop words))
  14223. (while (and words (< (+ (length line) (length (car words))) width))
  14224. (setq line (concat line " " (pop words))))
  14225. (setq lines (push line lines)))
  14226. (nreverse lines)))
  14227. (defun org-split-string (string &optional separators)
  14228. "Splits STRING into substrings at SEPARATORS.
  14229. No empty strings are returned if there are matches at the beginning
  14230. and end of string."
  14231. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14232. (start 0)
  14233. notfirst
  14234. (list nil))
  14235. (while (and (string-match rexp string
  14236. (if (and notfirst
  14237. (= start (match-beginning 0))
  14238. (< start (length string)))
  14239. (1+ start) start))
  14240. (< (match-beginning 0) (length string)))
  14241. (setq notfirst t)
  14242. (or (eq (match-beginning 0) 0)
  14243. (and (eq (match-beginning 0) (match-end 0))
  14244. (eq (match-beginning 0) start))
  14245. (setq list
  14246. (cons (substring string start (match-beginning 0))
  14247. list)))
  14248. (setq start (match-end 0)))
  14249. (or (eq start (length string))
  14250. (setq list
  14251. (cons (substring string start)
  14252. list)))
  14253. (nreverse list)))
  14254. (defun org-quote-vert (s)
  14255. "Replace \"|\" with \"\\vert\"."
  14256. (while (string-match "|" s)
  14257. (setq s (replace-match "\\vert" t t s)))
  14258. s)
  14259. (defun org-uuidgen-p (s)
  14260. "Is S an ID created by UUIDGEN?"
  14261. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14262. (defun org-context ()
  14263. "Return a list of contexts of the current cursor position.
  14264. If several contexts apply, all are returned.
  14265. Each context entry is a list with a symbol naming the context, and
  14266. two positions indicating start and end of the context. Possible
  14267. contexts are:
  14268. :headline anywhere in a headline
  14269. :headline-stars on the leading stars in a headline
  14270. :todo-keyword on a TODO keyword (including DONE) in a headline
  14271. :tags on the TAGS in a headline
  14272. :priority on the priority cookie in a headline
  14273. :item on the first line of a plain list item
  14274. :item-bullet on the bullet/number of a plain list item
  14275. :checkbox on the checkbox in a plain list item
  14276. :table in an org-mode table
  14277. :table-special on a special filed in a table
  14278. :table-table in a table.el table
  14279. :link on a hyperlink
  14280. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14281. :target on a <<target>>
  14282. :radio-target on a <<<radio-target>>>
  14283. :latex-fragment on a LaTeX fragment
  14284. :latex-preview on a LaTeX fragment with overlayed preview image
  14285. This function expects the position to be visible because it uses font-lock
  14286. faces as a help to recognize the following contexts: :table-special, :link,
  14287. and :keyword."
  14288. (let* ((f (get-text-property (point) 'face))
  14289. (faces (if (listp f) f (list f)))
  14290. (p (point)) clist o)
  14291. ;; First the large context
  14292. (cond
  14293. ((org-on-heading-p t)
  14294. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14295. (when (progn
  14296. (beginning-of-line 1)
  14297. (looking-at org-todo-line-tags-regexp))
  14298. (push (org-point-in-group p 1 :headline-stars) clist)
  14299. (push (org-point-in-group p 2 :todo-keyword) clist)
  14300. (push (org-point-in-group p 4 :tags) clist))
  14301. (goto-char p)
  14302. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14303. (if (looking-at "\\[#[A-Z0-9]\\]")
  14304. (push (org-point-in-group p 0 :priority) clist)))
  14305. ((org-at-item-p)
  14306. (push (org-point-in-group p 2 :item-bullet) clist)
  14307. (push (list :item (point-at-bol)
  14308. (save-excursion (org-end-of-item) (point)))
  14309. clist)
  14310. (and (org-at-item-checkbox-p)
  14311. (push (org-point-in-group p 0 :checkbox) clist)))
  14312. ((org-at-table-p)
  14313. (push (list :table (org-table-begin) (org-table-end)) clist)
  14314. (if (memq 'org-formula faces)
  14315. (push (list :table-special
  14316. (previous-single-property-change p 'face)
  14317. (next-single-property-change p 'face)) clist)))
  14318. ((org-at-table-p 'any)
  14319. (push (list :table-table) clist)))
  14320. (goto-char p)
  14321. ;; Now the small context
  14322. (cond
  14323. ((org-at-timestamp-p)
  14324. (push (org-point-in-group p 0 :timestamp) clist))
  14325. ((memq 'org-link faces)
  14326. (push (list :link
  14327. (previous-single-property-change p 'face)
  14328. (next-single-property-change p 'face)) clist))
  14329. ((memq 'org-special-keyword faces)
  14330. (push (list :keyword
  14331. (previous-single-property-change p 'face)
  14332. (next-single-property-change p 'face)) clist))
  14333. ((org-on-target-p)
  14334. (push (org-point-in-group p 0 :target) clist)
  14335. (goto-char (1- (match-beginning 0)))
  14336. (if (looking-at org-radio-target-regexp)
  14337. (push (org-point-in-group p 0 :radio-target) clist))
  14338. (goto-char p))
  14339. ((setq o (car (delq nil
  14340. (mapcar
  14341. (lambda (x)
  14342. (if (memq x org-latex-fragment-image-overlays) x))
  14343. (org-overlays-at (point))))))
  14344. (push (list :latex-fragment
  14345. (org-overlay-start o) (org-overlay-end o)) clist)
  14346. (push (list :latex-preview
  14347. (org-overlay-start o) (org-overlay-end o)) clist))
  14348. ((org-inside-LaTeX-fragment-p)
  14349. ;; FIXME: positions wrong.
  14350. (push (list :latex-fragment (point) (point)) clist)))
  14351. (setq clist (nreverse (delq nil clist)))
  14352. clist))
  14353. ;; FIXME: Compare with at-regexp-p Do we need both?
  14354. (defun org-in-regexp (re &optional nlines visually)
  14355. "Check if point is inside a match of regexp.
  14356. Normally only the current line is checked, but you can include NLINES extra
  14357. lines both before and after point into the search.
  14358. If VISUALLY is set, require that the cursor is not after the match but
  14359. really on, so that the block visually is on the match."
  14360. (catch 'exit
  14361. (let ((pos (point))
  14362. (eol (point-at-eol (+ 1 (or nlines 0))))
  14363. (inc (if visually 1 0)))
  14364. (save-excursion
  14365. (beginning-of-line (- 1 (or nlines 0)))
  14366. (while (re-search-forward re eol t)
  14367. (if (and (<= (match-beginning 0) pos)
  14368. (>= (+ inc (match-end 0)) pos))
  14369. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14370. (defun org-at-regexp-p (regexp)
  14371. "Is point inside a match of REGEXP in the current line?"
  14372. (catch 'exit
  14373. (save-excursion
  14374. (let ((pos (point)) (end (point-at-eol)))
  14375. (beginning-of-line 1)
  14376. (while (re-search-forward regexp end t)
  14377. (if (and (<= (match-beginning 0) pos)
  14378. (>= (match-end 0) pos))
  14379. (throw 'exit t)))
  14380. nil))))
  14381. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14382. "Call `multi-occur' with buffers for all agenda files."
  14383. (interactive "sOrg-files matching: \np")
  14384. (let* ((files (org-agenda-files))
  14385. (tnames (mapcar 'file-truename files))
  14386. (extra org-agenda-text-search-extra-files)
  14387. f)
  14388. (when (eq (car extra) 'agenda-archives)
  14389. (setq extra (cdr extra))
  14390. (setq files (org-add-archive-files files)))
  14391. (while (setq f (pop extra))
  14392. (unless (member (file-truename f) tnames)
  14393. (add-to-list 'files f 'append)
  14394. (add-to-list 'tnames (file-truename f) 'append)))
  14395. (multi-occur
  14396. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14397. regexp)))
  14398. (if (boundp 'occur-mode-find-occurrence-hook)
  14399. ;; Emacs 23
  14400. (add-hook 'occur-mode-find-occurrence-hook
  14401. (lambda ()
  14402. (when (org-mode-p)
  14403. (org-reveal))))
  14404. ;; Emacs 22
  14405. (defadvice occur-mode-goto-occurrence
  14406. (after org-occur-reveal activate)
  14407. (and (org-mode-p) (org-reveal)))
  14408. (defadvice occur-mode-goto-occurrence-other-window
  14409. (after org-occur-reveal activate)
  14410. (and (org-mode-p) (org-reveal)))
  14411. (defadvice occur-mode-display-occurrence
  14412. (after org-occur-reveal activate)
  14413. (when (org-mode-p)
  14414. (let ((pos (occur-mode-find-occurrence)))
  14415. (with-current-buffer (marker-buffer pos)
  14416. (save-excursion
  14417. (goto-char pos)
  14418. (org-reveal)))))))
  14419. (defun org-occur-link-in-agenda-files ()
  14420. "Create a link and search for it in the agendas.
  14421. The link is not stored in `org-stored-links', it is just created
  14422. for the search purpose."
  14423. (interactive)
  14424. (let ((link (condition-case nil
  14425. (org-store-link nil)
  14426. (error "Unable to create a link to here"))))
  14427. (org-occur-in-agenda-files (regexp-quote link))))
  14428. (defun org-uniquify (list)
  14429. "Remove duplicate elements from LIST."
  14430. (let (res)
  14431. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14432. res))
  14433. (defun org-delete-all (elts list)
  14434. "Remove all elements in ELTS from LIST."
  14435. (while elts
  14436. (setq list (delete (pop elts) list)))
  14437. list)
  14438. (defun org-back-over-empty-lines ()
  14439. "Move backwards over whitespace, to the beginning of the first empty line.
  14440. Returns the number of empty lines passed."
  14441. (let ((pos (point)))
  14442. (skip-chars-backward " \t\n\r")
  14443. (beginning-of-line 2)
  14444. (goto-char (min (point) pos))
  14445. (count-lines (point) pos)))
  14446. (defun org-skip-whitespace ()
  14447. (skip-chars-forward " \t\n\r"))
  14448. (defun org-point-in-group (point group &optional context)
  14449. "Check if POINT is in match-group GROUP.
  14450. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14451. match. If the match group does ot exist or point is not inside it,
  14452. return nil."
  14453. (and (match-beginning group)
  14454. (>= point (match-beginning group))
  14455. (<= point (match-end group))
  14456. (if context
  14457. (list context (match-beginning group) (match-end group))
  14458. t)))
  14459. (defun org-switch-to-buffer-other-window (&rest args)
  14460. "Switch to buffer in a second window on the current frame.
  14461. In particular, do not allow pop-up frames."
  14462. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14463. special-display-function)
  14464. (apply 'switch-to-buffer-other-window args)))
  14465. (defun org-combine-plists (&rest plists)
  14466. "Create a single property list from all plists in PLISTS.
  14467. The process starts by copying the first list, and then setting properties
  14468. from the other lists. Settings in the last list are the most significant
  14469. ones and overrule settings in the other lists."
  14470. (let ((rtn (copy-sequence (pop plists)))
  14471. p v ls)
  14472. (while plists
  14473. (setq ls (pop plists))
  14474. (while ls
  14475. (setq p (pop ls) v (pop ls))
  14476. (setq rtn (plist-put rtn p v))))
  14477. rtn))
  14478. (defun org-move-line-down (arg)
  14479. "Move the current line down. With prefix argument, move it past ARG lines."
  14480. (interactive "p")
  14481. (let ((col (current-column))
  14482. beg end pos)
  14483. (beginning-of-line 1) (setq beg (point))
  14484. (beginning-of-line 2) (setq end (point))
  14485. (beginning-of-line (+ 1 arg))
  14486. (setq pos (move-marker (make-marker) (point)))
  14487. (insert (delete-and-extract-region beg end))
  14488. (goto-char pos)
  14489. (org-move-to-column col)))
  14490. (defun org-move-line-up (arg)
  14491. "Move the current line up. With prefix argument, move it past ARG lines."
  14492. (interactive "p")
  14493. (let ((col (current-column))
  14494. beg end pos)
  14495. (beginning-of-line 1) (setq beg (point))
  14496. (beginning-of-line 2) (setq end (point))
  14497. (beginning-of-line (- arg))
  14498. (setq pos (move-marker (make-marker) (point)))
  14499. (insert (delete-and-extract-region beg end))
  14500. (goto-char pos)
  14501. (org-move-to-column col)))
  14502. (defun org-replace-escapes (string table)
  14503. "Replace %-escapes in STRING with values in TABLE.
  14504. TABLE is an association list with keys like \"%a\" and string values.
  14505. The sequences in STRING may contain normal field width and padding information,
  14506. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14507. so values can contain further %-escapes if they are define later in TABLE."
  14508. (let ((case-fold-search nil)
  14509. e re rpl)
  14510. (while (setq e (pop table))
  14511. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14512. (while (string-match re string)
  14513. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14514. (cdr e)))
  14515. (setq string (replace-match rpl t t string))))
  14516. string))
  14517. (defun org-sublist (list start end)
  14518. "Return a section of LIST, from START to END.
  14519. Counting starts at 1."
  14520. (let (rtn (c start))
  14521. (setq list (nthcdr (1- start) list))
  14522. (while (and list (<= c end))
  14523. (push (pop list) rtn)
  14524. (setq c (1+ c)))
  14525. (nreverse rtn)))
  14526. (defun org-find-base-buffer-visiting (file)
  14527. "Like `find-buffer-visiting' but always return the base buffer and
  14528. not an indirect buffer."
  14529. (let ((buf (or (get-file-buffer file)
  14530. (find-buffer-visiting file))))
  14531. (if buf
  14532. (or (buffer-base-buffer buf) buf)
  14533. nil)))
  14534. (defun org-image-file-name-regexp (&optional extensions)
  14535. "Return regexp matching the file names of images.
  14536. If EXTENSIONS is given, only match these."
  14537. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14538. (image-file-name-regexp)
  14539. (let ((image-file-name-extensions
  14540. (or extensions
  14541. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14542. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14543. (concat "\\."
  14544. (regexp-opt (nconc (mapcar 'upcase
  14545. image-file-name-extensions)
  14546. image-file-name-extensions)
  14547. t)
  14548. "\\'"))))
  14549. (defun org-file-image-p (file &optional extensions)
  14550. "Return non-nil if FILE is an image."
  14551. (save-match-data
  14552. (string-match (org-image-file-name-regexp extensions) file)))
  14553. (defun org-get-cursor-date ()
  14554. "Return the date at cursor in as a time.
  14555. This works in the calendar and in the agenda, anywhere else it just
  14556. returns the current time."
  14557. (let (date day defd)
  14558. (cond
  14559. ((eq major-mode 'calendar-mode)
  14560. (setq date (calendar-cursor-to-date)
  14561. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14562. ((eq major-mode 'org-agenda-mode)
  14563. (setq day (get-text-property (point) 'day))
  14564. (if day
  14565. (setq date (calendar-gregorian-from-absolute day)
  14566. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14567. (nth 2 date))))))
  14568. (or defd (current-time))))
  14569. (defvar org-agenda-action-marker (make-marker)
  14570. "Marker pointing to the entry for the next agenda action.")
  14571. (defun org-mark-entry-for-agenda-action ()
  14572. "Mark the current entry as target of an agenda action.
  14573. Agenda actions are actions executed from the agenda with the key `k',
  14574. which make use of the date at the cursor."
  14575. (interactive)
  14576. (move-marker org-agenda-action-marker
  14577. (save-excursion (org-back-to-heading t) (point))
  14578. (current-buffer))
  14579. (message
  14580. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14581. ;;; Paragraph filling stuff.
  14582. ;; We want this to be just right, so use the full arsenal.
  14583. (defun org-indent-line-function ()
  14584. "Indent line like previous, but further if previous was headline or item."
  14585. (interactive)
  14586. (let* ((pos (point))
  14587. (itemp (org-at-item-p))
  14588. (case-fold-search t)
  14589. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14590. column bpos bcol tpos tcol bullet btype bullet-type)
  14591. ;; Find the previous relevant line
  14592. (beginning-of-line 1)
  14593. (cond
  14594. ((looking-at "#") (setq column 0))
  14595. ((looking-at "\\*+ ") (setq column 0))
  14596. ((and (looking-at "[ \t]*:END:")
  14597. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14598. (save-excursion
  14599. (goto-char (1- (match-beginning 1)))
  14600. (setq column (current-column))))
  14601. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14602. (save-excursion
  14603. (re-search-backward
  14604. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14605. (setq column (org-get-indentation (match-string 0))))
  14606. (t
  14607. (beginning-of-line 0)
  14608. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14609. (not (looking-at "[ \t]*:END:"))
  14610. (not (looking-at org-drawer-regexp)))
  14611. (beginning-of-line 0))
  14612. (cond
  14613. ((looking-at "\\*+[ \t]+")
  14614. (if (not org-adapt-indentation)
  14615. (setq column 0)
  14616. (goto-char (match-end 0))
  14617. (setq column (current-column))))
  14618. ((looking-at org-drawer-regexp)
  14619. (goto-char (1- (match-beginning 1)))
  14620. (setq column (current-column)))
  14621. ((looking-at "\\([ \t]*\\):END:")
  14622. (goto-char (match-end 1))
  14623. (setq column (current-column)))
  14624. ((org-in-item-p)
  14625. (org-beginning-of-item)
  14626. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14627. (setq bpos (match-beginning 1) tpos (match-end 0)
  14628. bcol (progn (goto-char bpos) (current-column))
  14629. tcol (progn (goto-char tpos) (current-column))
  14630. bullet (match-string 1)
  14631. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14632. (if (> tcol (+ bcol org-description-max-indent))
  14633. (setq tcol (+ bcol 5)))
  14634. (if (not itemp)
  14635. (setq column tcol)
  14636. (goto-char pos)
  14637. (beginning-of-line 1)
  14638. (if (looking-at "\\S-")
  14639. (progn
  14640. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14641. (setq bullet (match-string 1)
  14642. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14643. (setq column (if (equal btype bullet-type) bcol tcol)))
  14644. (setq column (org-get-indentation)))))
  14645. (t (setq column (org-get-indentation))))))
  14646. (goto-char pos)
  14647. (if (<= (current-column) (current-indentation))
  14648. (org-indent-line-to column)
  14649. (save-excursion (org-indent-line-to column)))
  14650. (setq column (current-column))
  14651. (beginning-of-line 1)
  14652. (if (looking-at
  14653. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14654. (replace-match (concat "\\1" (format org-property-format
  14655. (match-string 2) (match-string 3)))
  14656. t nil))
  14657. (org-move-to-column column)))
  14658. (defun org-set-autofill-regexps ()
  14659. (interactive)
  14660. ;; In the paragraph separator we include headlines, because filling
  14661. ;; text in a line directly attached to a headline would otherwise
  14662. ;; fill the headline as well.
  14663. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14664. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14665. ;; The paragraph starter includes hand-formatted lists.
  14666. (org-set-local
  14667. 'paragraph-start
  14668. (concat
  14669. "\f" "\\|"
  14670. "[ ]*$" "\\|"
  14671. "\\*+ " "\\|"
  14672. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14673. "[ \t]*[:|]" "\\|"
  14674. "\\$\\$" "\\|"
  14675. "\\\\\\(begin\\|end\\|[][]\\)"))
  14676. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14677. ;; But only if the user has not turned off tables or fixed-width regions
  14678. (org-set-local
  14679. 'auto-fill-inhibit-regexp
  14680. (concat "\\*+ \\|#\\+"
  14681. "\\|[ \t]*" org-keyword-time-regexp
  14682. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14683. (concat
  14684. "\\|[ \t]*["
  14685. (if org-enable-table-editor "|" "")
  14686. (if org-enable-fixed-width-editor ":" "")
  14687. "]"))))
  14688. ;; We use our own fill-paragraph function, to make sure that tables
  14689. ;; and fixed-width regions are not wrapped. That function will pass
  14690. ;; through to `fill-paragraph' when appropriate.
  14691. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14692. ; Adaptive filling: To get full control, first make sure that
  14693. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14694. (org-set-local 'adaptive-fill-regexp "\000")
  14695. (org-set-local 'adaptive-fill-function
  14696. 'org-adaptive-fill-function)
  14697. (org-set-local
  14698. 'align-mode-rules-list
  14699. '((org-in-buffer-settings
  14700. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14701. (modes . '(org-mode))))))
  14702. (defun org-fill-paragraph (&optional justify)
  14703. "Re-align a table, pass through to fill-paragraph if no table."
  14704. (let ((table-p (org-at-table-p))
  14705. (table.el-p (org-at-table.el-p)))
  14706. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14707. (save-excursion (goto-char (point-at-bol))
  14708. (looking-at outline-regexp)))
  14709. t) ; skip headlines
  14710. (table.el-p t) ; skip table.el tables
  14711. (table-p (org-table-align) t) ; align org-mode tables
  14712. (t nil)))) ; call paragraph-fill
  14713. ;; For reference, this is the default value of adaptive-fill-regexp
  14714. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14715. (defun org-adaptive-fill-function ()
  14716. "Return a fill prefix for org-mode files.
  14717. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14718. work correctly."
  14719. (cond ((looking-at "#[ \t]+")
  14720. (match-string 0))
  14721. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14722. (save-excursion
  14723. (if (> (match-end 1) (+ (match-beginning 1)
  14724. org-description-max-indent))
  14725. (goto-char (+ (match-beginning 1) 5))
  14726. (goto-char (match-end 0)))
  14727. (make-string (current-column) ?\ )))
  14728. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14729. (save-excursion
  14730. (goto-char (match-end 0))
  14731. (make-string (current-column) ?\ )))
  14732. (t nil)))
  14733. ;;; Other stuff.
  14734. (defun org-toggle-fixed-width-section (arg)
  14735. "Toggle the fixed-width export.
  14736. If there is no active region, the QUOTE keyword at the current headline is
  14737. inserted or removed. When present, it causes the text between this headline
  14738. and the next to be exported as fixed-width text, and unmodified.
  14739. If there is an active region, this command adds or removes a colon as the
  14740. first character of this line. If the first character of a line is a colon,
  14741. this line is also exported in fixed-width font."
  14742. (interactive "P")
  14743. (let* ((cc 0)
  14744. (regionp (org-region-active-p))
  14745. (beg (if regionp (region-beginning) (point)))
  14746. (end (if regionp (region-end)))
  14747. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14748. (case-fold-search nil)
  14749. (re "[ \t]*\\(: \\)")
  14750. off)
  14751. (if regionp
  14752. (save-excursion
  14753. (goto-char beg)
  14754. (setq cc (current-column))
  14755. (beginning-of-line 1)
  14756. (setq off (looking-at re))
  14757. (while (> nlines 0)
  14758. (setq nlines (1- nlines))
  14759. (beginning-of-line 1)
  14760. (cond
  14761. (arg
  14762. (org-move-to-column cc t)
  14763. (insert ": \n")
  14764. (forward-line -1))
  14765. ((and off (looking-at re))
  14766. (replace-match "" t t nil 1))
  14767. ((not off) (org-move-to-column cc t) (insert ": ")))
  14768. (forward-line 1)))
  14769. (save-excursion
  14770. (org-back-to-heading)
  14771. (if (looking-at (concat outline-regexp
  14772. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14773. (replace-match "" t t nil 1)
  14774. (if (looking-at outline-regexp)
  14775. (progn
  14776. (goto-char (match-end 0))
  14777. (insert org-quote-string " "))))))))
  14778. (defun org-reftex-citation ()
  14779. "Use reftex-citation to insert a citation into the buffer.
  14780. This looks for a line like
  14781. #+BIBLIOGRAPHY: foo plain option:-d
  14782. and derives from it that foo.bib is the bbliography file relevant
  14783. for this document. It then installs the necessary environment for RefTeX
  14784. to work in this buffer and calls `reftex-citation' to insert a citation
  14785. into the buffer.
  14786. Export of such citations to both LaTeX and HTML is handled by the contributed
  14787. package org-exp-bibtex by Taru Karttunen."
  14788. (interactive)
  14789. (let ((reftex-docstruct-symbol 'rds)
  14790. (reftex-cite-format "\\cite{%l}")
  14791. rds bib)
  14792. (save-excursion
  14793. (save-restriction
  14794. (widen)
  14795. (let ((case-fold-search t)
  14796. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14797. (if (not (save-excursion
  14798. (or (re-search-forward re nil t)
  14799. (re-search-backward re nil t))))
  14800. (error "No bibliography defined in file")
  14801. (setq bib (concat (match-string 1) ".bib")
  14802. rds (list (list 'bib bib)))))))
  14803. (call-interactively 'reftex-citation)))
  14804. ;;;; Functions extending outline functionality
  14805. (defun org-beginning-of-line (&optional arg)
  14806. "Go to the beginning of the current line. If that is invisible, continue
  14807. to a visible line beginning. This makes the function of C-a more intuitive.
  14808. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14809. first attempt, and only move to after the tags when the cursor is already
  14810. beyond the end of the headline."
  14811. (interactive "P")
  14812. (let ((pos (point))
  14813. (special (if (consp org-special-ctrl-a/e)
  14814. (car org-special-ctrl-a/e)
  14815. org-special-ctrl-a/e))
  14816. refpos)
  14817. (if (org-bound-and-true-p line-move-visual)
  14818. (beginning-of-visual-line 1)
  14819. (beginning-of-line 1))
  14820. (if (and arg (fboundp 'move-beginning-of-line))
  14821. (call-interactively 'move-beginning-of-line)
  14822. (if (bobp)
  14823. nil
  14824. (backward-char 1)
  14825. (if (org-invisible-p)
  14826. (while (and (not (bobp)) (org-invisible-p))
  14827. (backward-char 1)
  14828. (beginning-of-line 1))
  14829. (forward-char 1))))
  14830. (when special
  14831. (cond
  14832. ((and (looking-at org-complex-heading-regexp)
  14833. (= (char-after (match-end 1)) ?\ ))
  14834. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14835. (point-at-eol)))
  14836. (goto-char
  14837. (if (eq special t)
  14838. (cond ((> pos refpos) refpos)
  14839. ((= pos (point)) refpos)
  14840. (t (point)))
  14841. (cond ((> pos (point)) (point))
  14842. ((not (eq last-command this-command)) (point))
  14843. (t refpos)))))
  14844. ((org-at-item-p)
  14845. (goto-char
  14846. (if (eq special t)
  14847. (cond ((> pos (match-end 4)) (match-end 4))
  14848. ((= pos (point)) (match-end 4))
  14849. (t (point)))
  14850. (cond ((> pos (point)) (point))
  14851. ((not (eq last-command this-command)) (point))
  14852. (t (match-end 4))))))))
  14853. (org-no-warnings
  14854. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14855. (defun org-end-of-line (&optional arg)
  14856. "Go to the end of the line.
  14857. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14858. first attempt, and only move to after the tags when the cursor is already
  14859. beyond the end of the headline."
  14860. (interactive "P")
  14861. (let ((special (if (consp org-special-ctrl-a/e)
  14862. (cdr org-special-ctrl-a/e)
  14863. org-special-ctrl-a/e)))
  14864. (if (or (not special)
  14865. (not (org-on-heading-p))
  14866. arg)
  14867. (call-interactively
  14868. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  14869. ((fboundp 'move-end-of-line) 'move-end-of-line)
  14870. (t 'end-of-line)))
  14871. (let ((pos (point)))
  14872. (beginning-of-line 1)
  14873. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14874. (if (eq special t)
  14875. (if (or (< pos (match-beginning 1))
  14876. (= pos (match-end 0)))
  14877. (goto-char (match-beginning 1))
  14878. (goto-char (match-end 0)))
  14879. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14880. (goto-char (match-end 0))
  14881. (goto-char (match-beginning 1))))
  14882. (call-interactively (if (fboundp 'move-end-of-line)
  14883. 'move-end-of-line
  14884. 'end-of-line)))))
  14885. (org-no-warnings
  14886. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14887. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14888. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14889. (defun org-backward-sentence (&optional arg)
  14890. "Go to beginning of sentence, or beginning of table field.
  14891. This will call `backward-sentence' or `org-table-beginning-of-field',
  14892. depending on context."
  14893. (interactive "P")
  14894. (cond
  14895. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14896. (t (call-interactively 'backward-sentence))))
  14897. (defun org-forward-sentence (&optional arg)
  14898. "Go to end of sentence, or end of table field.
  14899. This will call `forward-sentence' or `org-table-end-of-field',
  14900. depending on context."
  14901. (interactive "P")
  14902. (cond
  14903. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14904. (t (call-interactively 'forward-sentence))))
  14905. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14906. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14907. (defun org-kill-line (&optional arg)
  14908. "Kill line, to tags or end of line."
  14909. (interactive "P")
  14910. (cond
  14911. ((or (not org-special-ctrl-k)
  14912. (bolp)
  14913. (not (org-on-heading-p)))
  14914. (call-interactively 'kill-line))
  14915. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14916. (kill-region (point) (match-beginning 1))
  14917. (org-set-tags nil t))
  14918. (t (kill-region (point) (point-at-eol)))))
  14919. (define-key org-mode-map "\C-k" 'org-kill-line)
  14920. (defun org-yank (&optional arg)
  14921. "Yank. If the kill is a subtree, treat it specially.
  14922. This command will look at the current kill and check if is a single
  14923. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14924. cursor is at the beginning of a line or after the stars of a currently
  14925. empty headline, then the yank is handled specially. How exactly depends
  14926. on the value of the following variables, both set by default.
  14927. org-yank-folded-subtrees
  14928. When set, the subtree(s) will be folded after insertion, but only
  14929. if doing so would now swallow text after the yanked text.
  14930. org-yank-adjusted-subtrees
  14931. When set, the subtree will be promoted or demoted in order to
  14932. fit into the local outline tree structure, which means that the level
  14933. will be adjusted so that it becomes the smaller one of the two
  14934. *visible* surrounding headings.
  14935. Any prefix to this command will cause `yank' to be called directly with
  14936. no special treatment. In particular, a simple `C-u' prefix will just
  14937. plainly yank the text as it is.
  14938. \[1] The test checks if the first non-white line is a heading
  14939. and if there are no other headings with fewer stars."
  14940. (interactive "P")
  14941. (org-yank-generic 'yank arg))
  14942. (defun org-yank-generic (command arg)
  14943. "Perform some yank-like command.
  14944. This function implements the behavior described in the `org-yank'
  14945. documentation. However, it has been generalized to work for any
  14946. interactive command with similar behavior."
  14947. ;; pretend to be command COMMAND
  14948. (setq this-command command)
  14949. (if arg
  14950. (call-interactively command)
  14951. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14952. (and (org-kill-is-subtree-p)
  14953. (or (bolp)
  14954. (and (looking-at "[ \t]*$")
  14955. (string-match
  14956. "\\`\\*+\\'"
  14957. (buffer-substring (point-at-bol) (point)))))))
  14958. swallowp)
  14959. (cond
  14960. ((and subtreep org-yank-folded-subtrees)
  14961. (let ((beg (point))
  14962. end)
  14963. (if (and subtreep org-yank-adjusted-subtrees)
  14964. (org-paste-subtree nil nil 'for-yank)
  14965. (call-interactively command))
  14966. (setq end (point))
  14967. (goto-char beg)
  14968. (when (and (bolp) subtreep
  14969. (not (setq swallowp
  14970. (org-yank-folding-would-swallow-text beg end))))
  14971. (or (looking-at outline-regexp)
  14972. (re-search-forward (concat "^" outline-regexp) end t))
  14973. (while (and (< (point) end) (looking-at outline-regexp))
  14974. (hide-subtree)
  14975. (org-cycle-show-empty-lines 'folded)
  14976. (condition-case nil
  14977. (outline-forward-same-level 1)
  14978. (error (goto-char end)))))
  14979. (when swallowp
  14980. (message
  14981. "Inserted text not folded because that would swallow text"))
  14982. (goto-char end)
  14983. (skip-chars-forward " \t\n\r")
  14984. (beginning-of-line 1)
  14985. (push-mark beg 'nomsg)))
  14986. ((and subtreep org-yank-adjusted-subtrees)
  14987. (let ((beg (point-at-bol)))
  14988. (org-paste-subtree nil nil 'for-yank)
  14989. (push-mark beg 'nomsg)))
  14990. (t
  14991. (call-interactively command))))))
  14992. (defun org-yank-folding-would-swallow-text (beg end)
  14993. "Would hide-subtree at BEG swallow any text after END?"
  14994. (let (level)
  14995. (save-excursion
  14996. (goto-char beg)
  14997. (when (or (looking-at outline-regexp)
  14998. (re-search-forward (concat "^" outline-regexp) end t))
  14999. (setq level (org-outline-level)))
  15000. (goto-char end)
  15001. (skip-chars-forward " \t\r\n\v\f")
  15002. (if (or (eobp)
  15003. (and (bolp) (looking-at org-outline-regexp)
  15004. (<= (org-outline-level) level)))
  15005. nil ; Nothing would be swallowed
  15006. t)))) ; something would swallow
  15007. (define-key org-mode-map "\C-y" 'org-yank)
  15008. (defun org-invisible-p ()
  15009. "Check if point is at a character currently not visible."
  15010. ;; Early versions of noutline don't have `outline-invisible-p'.
  15011. (if (fboundp 'outline-invisible-p)
  15012. (outline-invisible-p)
  15013. (get-char-property (point) 'invisible)))
  15014. (defun org-invisible-p2 ()
  15015. "Check if point is at a character currently not visible."
  15016. (save-excursion
  15017. (if (and (eolp) (not (bobp))) (backward-char 1))
  15018. ;; Early versions of noutline don't have `outline-invisible-p'.
  15019. (if (fboundp 'outline-invisible-p)
  15020. (outline-invisible-p)
  15021. (get-char-property (point) 'invisible))))
  15022. (defun org-back-to-heading (&optional invisible-ok)
  15023. "Call `outline-back-to-heading', but provide a better error message."
  15024. (condition-case nil
  15025. (outline-back-to-heading invisible-ok)
  15026. (error (error "Before first headline at position %d in buffer %s"
  15027. (point) (current-buffer)))))
  15028. (defun org-before-first-heading-p ()
  15029. "Before first heading?"
  15030. (save-excursion
  15031. (null (re-search-backward "^\\*+ " nil t))))
  15032. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15033. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15034. (defun org-at-heading-or-item-p ()
  15035. (or (org-on-heading-p) (org-at-item-p)))
  15036. (defun org-on-target-p ()
  15037. (or (org-in-regexp org-radio-target-regexp)
  15038. (org-in-regexp org-target-regexp)))
  15039. (defun org-up-heading-all (arg)
  15040. "Move to the heading line of which the present line is a subheading.
  15041. This function considers both visible and invisible heading lines.
  15042. With argument, move up ARG levels."
  15043. (if (fboundp 'outline-up-heading-all)
  15044. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15045. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15046. (defun org-up-heading-safe ()
  15047. "Move to the heading line of which the present line is a subheading.
  15048. This version will not throw an error. It will return the level of the
  15049. headline found, or nil if no higher level is found.
  15050. Also, this function will be a lot faster than `outline-up-heading',
  15051. because it relies on stars being the outline starters. This can really
  15052. make a significant difference in outlines with very many siblings."
  15053. (let (start-level re)
  15054. (org-back-to-heading t)
  15055. (setq start-level (funcall outline-level))
  15056. (if (equal start-level 1)
  15057. nil
  15058. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15059. (if (re-search-backward re nil t)
  15060. (funcall outline-level)))))
  15061. (defun org-first-sibling-p ()
  15062. "Is this heading the first child of its parents?"
  15063. (interactive)
  15064. (let ((re (concat "^" outline-regexp))
  15065. level l)
  15066. (unless (org-at-heading-p t)
  15067. (error "Not at a heading"))
  15068. (setq level (funcall outline-level))
  15069. (save-excursion
  15070. (if (not (re-search-backward re nil t))
  15071. t
  15072. (setq l (funcall outline-level))
  15073. (< l level)))))
  15074. (defun org-goto-sibling (&optional previous)
  15075. "Goto the next sibling, even if it is invisible.
  15076. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15077. when a sibling was found. When none is found, return nil and don't
  15078. move point."
  15079. (let ((fun (if previous 're-search-backward 're-search-forward))
  15080. (pos (point))
  15081. (re (concat "^" outline-regexp))
  15082. level l)
  15083. (when (condition-case nil (org-back-to-heading t) (error nil))
  15084. (setq level (funcall outline-level))
  15085. (catch 'exit
  15086. (or previous (forward-char 1))
  15087. (while (funcall fun re nil t)
  15088. (setq l (funcall outline-level))
  15089. (when (< l level) (goto-char pos) (throw 'exit nil))
  15090. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15091. (goto-char pos)
  15092. nil))))
  15093. (defun org-show-siblings ()
  15094. "Show all siblings of the current headline."
  15095. (save-excursion
  15096. (while (org-goto-sibling) (org-flag-heading nil)))
  15097. (save-excursion
  15098. (while (org-goto-sibling 'previous)
  15099. (org-flag-heading nil))))
  15100. (defun org-show-hidden-entry ()
  15101. "Show an entry where even the heading is hidden."
  15102. (save-excursion
  15103. (org-show-entry)))
  15104. (defun org-flag-heading (flag &optional entry)
  15105. "Flag the current heading. FLAG non-nil means make invisible.
  15106. When ENTRY is non-nil, show the entire entry."
  15107. (save-excursion
  15108. (org-back-to-heading t)
  15109. ;; Check if we should show the entire entry
  15110. (if entry
  15111. (progn
  15112. (org-show-entry)
  15113. (save-excursion
  15114. (and (outline-next-heading)
  15115. (org-flag-heading nil))))
  15116. (outline-flag-region (max (point-min) (1- (point)))
  15117. (save-excursion (outline-end-of-heading) (point))
  15118. flag))))
  15119. (defun org-get-next-sibling ()
  15120. "Move to next heading of the same level, and return point.
  15121. If there is no such heading, return nil.
  15122. This is like outline-next-sibling, but invisible headings are ok."
  15123. (let ((level (funcall outline-level)))
  15124. (outline-next-heading)
  15125. (while (and (not (eobp)) (> (funcall outline-level) level))
  15126. (outline-next-heading))
  15127. (if (or (eobp) (< (funcall outline-level) level))
  15128. nil
  15129. (point))))
  15130. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15131. ;; This contains an exact copy of the original function, but it uses
  15132. ;; `org-back-to-heading', to make it work also in invisible
  15133. ;; trees. And is uses an invisible-OK argument.
  15134. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15135. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15136. ;; with many children and grandchildren etc, this can be much faster
  15137. ;; than the outline version.
  15138. (org-back-to-heading invisible-OK)
  15139. (let ((first t)
  15140. (level (funcall outline-level)))
  15141. (if (and (org-mode-p) (< level 1000))
  15142. ;; A true heading (not a plain list item), in Org-mode
  15143. ;; This means we can easily find the end by looking
  15144. ;; only for the right number of stars. Using a regexp to do
  15145. ;; this is so much faster than using a Lisp loop.
  15146. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15147. (forward-char 1)
  15148. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15149. ;; something else, do it the slow way
  15150. (while (and (not (eobp))
  15151. (or first (> (funcall outline-level) level)))
  15152. (setq first nil)
  15153. (outline-next-heading)))
  15154. (unless to-heading
  15155. (if (memq (preceding-char) '(?\n ?\^M))
  15156. (progn
  15157. ;; Go to end of line before heading
  15158. (forward-char -1)
  15159. (if (memq (preceding-char) '(?\n ?\^M))
  15160. ;; leave blank line before heading
  15161. (forward-char -1))))))
  15162. (point))
  15163. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15164. "Use Org version in org-mode, for dramatic speed-up."
  15165. (if (eq major-mode 'org-mode)
  15166. (org-end-of-subtree)
  15167. ad-do-it))
  15168. (defun org-forward-same-level (arg &optional invisible-ok)
  15169. "Move forward to the arg'th subheading at same level as this one.
  15170. Stop at the first and last subheadings of a superior heading."
  15171. (interactive "p")
  15172. (org-back-to-heading)
  15173. (org-on-heading-p)
  15174. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15175. (re (format "^\\*\\{1,%d\\} " level))
  15176. l)
  15177. (forward-char 1)
  15178. (while (> arg 0)
  15179. (while (and (re-search-forward re nil 'move)
  15180. (setq l (- (match-end 0) (match-beginning 0) 1))
  15181. (= l level)
  15182. (not invisible-ok)
  15183. (org-invisible-p))
  15184. (if (< l level) (setq arg 1)))
  15185. (setq arg (1- arg)))
  15186. (beginning-of-line 1)))
  15187. (defun org-backward-same-level (arg &optional invisible-ok)
  15188. "Move backward to the arg'th subheading at same level as this one.
  15189. Stop at the first and last subheadings of a superior heading."
  15190. (interactive "p")
  15191. (org-back-to-heading)
  15192. (org-on-heading-p)
  15193. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15194. (re (format "^\\*\\{1,%d\\} " level))
  15195. l)
  15196. (while (> arg 0)
  15197. (while (and (re-search-backward re nil 'move)
  15198. (setq l (- (match-end 0) (match-beginning 0) 1))
  15199. (= l level)
  15200. (not invisible-ok)
  15201. (org-invisible-p))
  15202. (if (< l level) (setq arg 1)))
  15203. (setq arg (1- arg)))))
  15204. (defun org-show-subtree ()
  15205. "Show everything after this heading at deeper levels."
  15206. (outline-flag-region
  15207. (point)
  15208. (save-excursion
  15209. (outline-end-of-subtree) (outline-next-heading) (point))
  15210. nil))
  15211. (defun org-show-entry ()
  15212. "Show the body directly following this heading.
  15213. Show the heading too, if it is currently invisible."
  15214. (interactive)
  15215. (save-excursion
  15216. (condition-case nil
  15217. (progn
  15218. (org-back-to-heading t)
  15219. (outline-flag-region
  15220. (max (point-min) (1- (point)))
  15221. (save-excursion
  15222. (if (re-search-forward
  15223. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15224. (match-beginning 1)
  15225. (point-max)))
  15226. nil)
  15227. (org-cycle-hide-drawers 'children))
  15228. (error nil))))
  15229. (defun org-make-options-regexp (kwds &optional extra)
  15230. "Make a regular expression for keyword lines."
  15231. (concat
  15232. "^"
  15233. "#?[ \t]*\\+\\("
  15234. (mapconcat 'regexp-quote kwds "\\|")
  15235. (if extra (concat "\\|" extra))
  15236. "\\):[ \t]*"
  15237. "\\(.*\\)"))
  15238. ;; Make isearch reveal the necessary context
  15239. (defun org-isearch-end ()
  15240. "Reveal context after isearch exits."
  15241. (when isearch-success ; only if search was successful
  15242. (if (featurep 'xemacs)
  15243. ;; Under XEmacs, the hook is run in the correct place,
  15244. ;; we directly show the context.
  15245. (org-show-context 'isearch)
  15246. ;; In Emacs the hook runs *before* restoring the overlays.
  15247. ;; So we have to use a one-time post-command-hook to do this.
  15248. ;; (Emacs 22 has a special variable, see function `org-mode')
  15249. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15250. isearch-mode-end-hook-quit)
  15251. ;; Only when the isearch was not quitted.
  15252. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15253. 'append 'local)))))
  15254. (defun org-isearch-post-command ()
  15255. "Remove self from hook, and show context."
  15256. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15257. (org-show-context 'isearch))
  15258. ;;;; Integration with and fixes for other packages
  15259. ;;; Imenu support
  15260. (defvar org-imenu-markers nil
  15261. "All markers currently used by Imenu.")
  15262. (make-variable-buffer-local 'org-imenu-markers)
  15263. (defun org-imenu-new-marker (&optional pos)
  15264. "Return a new marker for use by Imenu, and remember the marker."
  15265. (let ((m (make-marker)))
  15266. (move-marker m (or pos (point)))
  15267. (push m org-imenu-markers)
  15268. m))
  15269. (defun org-imenu-get-tree ()
  15270. "Produce the index for Imenu."
  15271. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15272. (setq org-imenu-markers nil)
  15273. (let* ((n org-imenu-depth)
  15274. (re (concat "^" outline-regexp))
  15275. (subs (make-vector (1+ n) nil))
  15276. (last-level 0)
  15277. m level head)
  15278. (save-excursion
  15279. (save-restriction
  15280. (widen)
  15281. (goto-char (point-max))
  15282. (while (re-search-backward re nil t)
  15283. (setq level (org-reduced-level (funcall outline-level)))
  15284. (when (<= level n)
  15285. (looking-at org-complex-heading-regexp)
  15286. (setq head (org-link-display-format
  15287. (org-match-string-no-properties 4))
  15288. m (org-imenu-new-marker))
  15289. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15290. (if (>= level last-level)
  15291. (push (cons head m) (aref subs level))
  15292. (push (cons head (aref subs (1+ level))) (aref subs level))
  15293. (loop for i from (1+ level) to n do (aset subs i nil)))
  15294. (setq last-level level)))))
  15295. (aref subs 1)))
  15296. (eval-after-load "imenu"
  15297. '(progn
  15298. (add-hook 'imenu-after-jump-hook
  15299. (lambda ()
  15300. (if (eq major-mode 'org-mode)
  15301. (org-show-context 'org-goto))))))
  15302. (defun org-link-display-format (link)
  15303. "Replace a link with either the description, or the link target
  15304. if no description is present"
  15305. (save-match-data
  15306. (if (string-match org-bracket-link-analytic-regexp link)
  15307. (replace-match (or (match-string 5 link)
  15308. (concat (match-string 1 link)
  15309. (match-string 3 link)))
  15310. nil nil link)
  15311. link)))
  15312. ;; Speedbar support
  15313. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15314. "Overlay marking the agenda restriction line in speedbar.")
  15315. (org-overlay-put org-speedbar-restriction-lock-overlay
  15316. 'face 'org-agenda-restriction-lock)
  15317. (org-overlay-put org-speedbar-restriction-lock-overlay
  15318. 'help-echo "Agendas are currently limited to this item.")
  15319. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15320. (defun org-speedbar-set-agenda-restriction ()
  15321. "Restrict future agenda commands to the location at point in speedbar.
  15322. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15323. (interactive)
  15324. (require 'org-agenda)
  15325. (let (p m tp np dir txt)
  15326. (cond
  15327. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15328. 'org-imenu t))
  15329. (setq m (get-text-property p 'org-imenu-marker))
  15330. (save-excursion
  15331. (save-restriction
  15332. (set-buffer (marker-buffer m))
  15333. (goto-char m)
  15334. (org-agenda-set-restriction-lock 'subtree))))
  15335. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15336. 'speedbar-function 'speedbar-find-file))
  15337. (setq tp (previous-single-property-change
  15338. (1+ p) 'speedbar-function)
  15339. np (next-single-property-change
  15340. tp 'speedbar-function)
  15341. dir (speedbar-line-directory)
  15342. txt (buffer-substring-no-properties (or tp (point-min))
  15343. (or np (point-max))))
  15344. (save-excursion
  15345. (save-restriction
  15346. (set-buffer (find-file-noselect
  15347. (let ((default-directory dir))
  15348. (expand-file-name txt))))
  15349. (unless (org-mode-p)
  15350. (error "Cannot restrict to non-Org-mode file"))
  15351. (org-agenda-set-restriction-lock 'file))))
  15352. (t (error "Don't know how to restrict Org-mode's agenda")))
  15353. (org-move-overlay org-speedbar-restriction-lock-overlay
  15354. (point-at-bol) (point-at-eol))
  15355. (setq current-prefix-arg nil)
  15356. (org-agenda-maybe-redo)))
  15357. (eval-after-load "speedbar"
  15358. '(progn
  15359. (speedbar-add-supported-extension ".org")
  15360. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15361. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15362. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15363. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15364. (add-hook 'speedbar-visiting-tag-hook
  15365. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15366. ;;; Fixes and Hacks for problems with other packages
  15367. ;; Make flyspell not check words in links, to not mess up our keymap
  15368. (defun org-mode-flyspell-verify ()
  15369. "Don't let flyspell put overlays at active buttons."
  15370. (and (not (get-text-property (point) 'keymap))
  15371. (not (get-text-property (point) 'org-no-flyspell))))
  15372. (defun org-remove-flyspell-overlays-in (beg end)
  15373. "Remove flyspell overlays in region."
  15374. (and (org-bound-and-true-p flyspell-mode)
  15375. (fboundp 'flyspell-delete-region-overlays)
  15376. (flyspell-delete-region-overlays beg end))
  15377. (add-text-properties beg end '(org-no-flyspell t)))
  15378. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15379. (eval-after-load "bookmark"
  15380. '(if (boundp 'bookmark-after-jump-hook)
  15381. ;; We can use the hook
  15382. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15383. ;; Hook not available, use advice
  15384. (defadvice bookmark-jump (after org-make-visible activate)
  15385. "Make the position visible."
  15386. (org-bookmark-jump-unhide))))
  15387. ;; Make sure saveplace show the location if it was hidden
  15388. (eval-after-load "saveplace"
  15389. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15390. "Make the position visible."
  15391. (org-bookmark-jump-unhide)))
  15392. (defun org-bookmark-jump-unhide ()
  15393. "Unhide the current position, to show the bookmark location."
  15394. (and (org-mode-p)
  15395. (or (org-invisible-p)
  15396. (save-excursion (goto-char (max (point-min) (1- (point))))
  15397. (org-invisible-p)))
  15398. (org-show-context 'bookmark-jump)))
  15399. ;; Make session.el ignore our circular variable
  15400. (eval-after-load "session"
  15401. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15402. ;;;; Experimental code
  15403. (defun org-closed-in-range ()
  15404. "Sparse tree of items closed in a certain time range.
  15405. Still experimental, may disappear in the future."
  15406. (interactive)
  15407. ;; Get the time interval from the user.
  15408. (let* ((time1 (time-to-seconds
  15409. (org-read-date nil 'to-time nil "Starting date: ")))
  15410. (time2 (time-to-seconds
  15411. (org-read-date nil 'to-time nil "End date:")))
  15412. ;; callback function
  15413. (callback (lambda ()
  15414. (let ((time
  15415. (time-to-seconds
  15416. (apply 'encode-time
  15417. (org-parse-time-string
  15418. (match-string 1))))))
  15419. ;; check if time in interval
  15420. (and (>= time time1) (<= time time2))))))
  15421. ;; make tree, check each match with the callback
  15422. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15423. ;;;; Finish up
  15424. (provide 'org)
  15425. (run-hooks 'org-load-hook)
  15426. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15427. ;;; org.el ends here