org.el 631 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.27trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.27trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  185. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  186. (defcustom org-support-shift-select nil
  187. "Non-nil means, make shift-cursor commands select text when possible.
  188. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  189. selecting a region, or enlarge thusly regions started in this way.
  190. In Org-mode, in special contexts, these same keys are used for other
  191. purposes, important enough to compete with shift selection. Org tries
  192. to balance these needs by supporting `shift-select-mode' outside these
  193. special contexts, under control of this variable.
  194. The default of this variable is nil, to avoid confusing behavior. Shifted
  195. cursor keys will then execute Org commands in the following contexts:
  196. - on a headline, changing TODO state (left/right) and priority (up/down)
  197. - on a time stamp, changing the time
  198. - in a plain list item, changing the bullet type
  199. - in a property definition line, switching between allowed values
  200. - in the BEGIN line of a clock table (changing the time block).
  201. Outside these contexts, the commands will throw an error.
  202. When this variable is t and the cursor is not in a special context,
  203. Org-mode will support shift-selection for making and enlarging regions.
  204. To make this more effective, the bullet cycling will no longer happen
  205. anywhere in an item line, but only if the cursor is exactly on the bullet.
  206. If you set this variable to the symbol `always', then the keys
  207. will not be special in headlines, property lines, and item lines, to make
  208. shift selection work there as well. If this is what you want, you can
  209. use the following alternative commands: `C-c C-t' and `C-c ,' to
  210. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  211. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  212. edited by hand or in column view.
  213. However, when the cursor is on a timestamp, shift-cursor commands
  214. will still edit the time stamp - this is just too good to give up.
  215. XEmacs user should have this variable set to nil, because shift-select-mode
  216. is Emacs 23 only."
  217. :group 'org
  218. :type '(choice
  219. (const :tag "Never" nil)
  220. (const :tag "When outside special context" t)
  221. (const :tag "Everywhere except timestamps" always)))
  222. (defgroup org-startup nil
  223. "Options concerning startup of Org-mode."
  224. :tag "Org Startup"
  225. :group 'org)
  226. (defcustom org-startup-folded t
  227. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  228. This can also be configured on a per-file basis by adding one of
  229. the following lines anywhere in the buffer:
  230. #+STARTUP: fold
  231. #+STARTUP: nofold
  232. #+STARTUP: content"
  233. :group 'org-startup
  234. :type '(choice
  235. (const :tag "nofold: show all" nil)
  236. (const :tag "fold: overview" t)
  237. (const :tag "content: all headlines" content)))
  238. (defcustom org-startup-truncated t
  239. "Non-nil means, entering Org-mode will set `truncate-lines'.
  240. This is useful since some lines containing links can be very long and
  241. uninteresting. Also tables look terrible when wrapped."
  242. :group 'org-startup
  243. :type 'boolean)
  244. (defcustom org-startup-align-all-tables nil
  245. "Non-nil means, align all tables when visiting a file.
  246. This is useful when the column width in tables is forced with <N> cookies
  247. in table fields. Such tables will look correct only after the first re-align.
  248. This can also be configured on a per-file basis by adding one of
  249. the following lines anywhere in the buffer:
  250. #+STARTUP: align
  251. #+STARTUP: noalign"
  252. :group 'org-startup
  253. :type 'boolean)
  254. (defcustom org-insert-mode-line-in-empty-file nil
  255. "Non-nil means insert the first line setting Org-mode in empty files.
  256. When the function `org-mode' is called interactively in an empty file, this
  257. normally means that the file name does not automatically trigger Org-mode.
  258. To ensure that the file will always be in Org-mode in the future, a
  259. line enforcing Org-mode will be inserted into the buffer, if this option
  260. has been set."
  261. :group 'org-startup
  262. :type 'boolean)
  263. (defcustom org-replace-disputed-keys nil
  264. "Non-nil means use alternative key bindings for some keys.
  265. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  266. These keys are also used by other packages like shift-selection-mode'
  267. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  268. If you want to use Org-mode together with one of these other modes,
  269. or more generally if you would like to move some Org-mode commands to
  270. other keys, set this variable and configure the keys with the variable
  271. `org-disputed-keys'.
  272. This option is only relevant at load-time of Org-mode, and must be set
  273. *before* org.el is loaded. Changing it requires a restart of Emacs to
  274. become effective."
  275. :group 'org-startup
  276. :type 'boolean)
  277. (defcustom org-use-extra-keys nil
  278. "Non-nil means use extra key sequence definitions for certain
  279. commands. This happens automatically if you run XEmacs or if
  280. window-system is nil. This variable lets you do the same
  281. manually. You must set it before loading org.
  282. Example: on Carbon Emacs 22 running graphically, with an external
  283. keyboard on a Powerbook, the default way of setting M-left might
  284. not work for either Alt or ESC. Setting this variable will make
  285. it work for ESC."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (if (fboundp 'defvaralias)
  289. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  290. (defcustom org-disputed-keys
  291. '(([(shift up)] . [(meta p)])
  292. ([(shift down)] . [(meta n)])
  293. ([(shift left)] . [(meta -)])
  294. ([(shift right)] . [(meta +)])
  295. ([(control shift right)] . [(meta shift +)])
  296. ([(control shift left)] . [(meta shift -)]))
  297. "Keys for which Org-mode and other modes compete.
  298. This is an alist, cars are the default keys, second element specifies
  299. the alternative to use when `org-replace-disputed-keys' is t.
  300. Keys can be specified in any syntax supported by `define-key'.
  301. The value of this option takes effect only at Org-mode's startup,
  302. therefore you'll have to restart Emacs to apply it after changing."
  303. :group 'org-startup
  304. :type 'alist)
  305. (defun org-key (key)
  306. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  307. Or return the original if not disputed."
  308. (if org-replace-disputed-keys
  309. (let* ((nkey (key-description key))
  310. (x (org-find-if (lambda (x)
  311. (equal (key-description (car x)) nkey))
  312. org-disputed-keys)))
  313. (if x (cdr x) key))
  314. key))
  315. (defun org-find-if (predicate seq)
  316. (catch 'exit
  317. (while seq
  318. (if (funcall predicate (car seq))
  319. (throw 'exit (car seq))
  320. (pop seq)))))
  321. (defun org-defkey (keymap key def)
  322. "Define a key, possibly translated, as returned by `org-key'."
  323. (define-key keymap (org-key key) def))
  324. (defcustom org-ellipsis nil
  325. "The ellipsis to use in the Org-mode outline.
  326. When nil, just use the standard three dots. When a string, use that instead,
  327. When a face, use the standard 3 dots, but with the specified face.
  328. The change affects only Org-mode (which will then use its own display table).
  329. Changing this requires executing `M-x org-mode' in a buffer to become
  330. effective."
  331. :group 'org-startup
  332. :type '(choice (const :tag "Default" nil)
  333. (face :tag "Face" :value org-warning)
  334. (string :tag "String" :value "...#")))
  335. (defvar org-display-table nil
  336. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  337. (defgroup org-keywords nil
  338. "Keywords in Org-mode."
  339. :tag "Org Keywords"
  340. :group 'org)
  341. (defcustom org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].
  346. Changes become only effective after restarting Emacs."
  347. :group 'org-keywords
  348. :type 'string)
  349. (defcustom org-scheduled-string "SCHEDULED:"
  350. "String to mark scheduled TODO entries.
  351. A schedule is this string, followed by a time stamp. Should be a word,
  352. terminated by a colon. You can insert a schedule keyword and
  353. a timestamp with \\[org-schedule].
  354. Changes become only effective after restarting Emacs."
  355. :group 'org-keywords
  356. :type 'string)
  357. (defcustom org-closed-string "CLOSED:"
  358. "String used as the prefix for timestamps logging closing a TODO entry."
  359. :group 'org-keywords
  360. :type 'string)
  361. (defcustom org-clock-string "CLOCK:"
  362. "String used as prefix for timestamps clocking work hours on an item."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-comment-string "COMMENT"
  366. "Entries starting with this keyword will never be exported.
  367. An entry can be toggled between COMMENT and normal with
  368. \\[org-toggle-comment].
  369. Changes become only effective after restarting Emacs."
  370. :group 'org-keywords
  371. :type 'string)
  372. (defcustom org-quote-string "QUOTE"
  373. "Entries starting with this keyword will be exported in fixed-width font.
  374. Quoting applies only to the text in the entry following the headline, and does
  375. not extend beyond the next headline, even if that is lower level.
  376. An entry can be toggled between QUOTE and normal with
  377. \\[org-toggle-fixed-width-section]."
  378. :group 'org-keywords
  379. :type 'string)
  380. (defconst org-repeat-re
  381. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  382. "Regular expression for specifying repeated events.
  383. After a match, group 1 contains the repeat expression.")
  384. (defgroup org-structure nil
  385. "Options concerning the general structure of Org-mode files."
  386. :tag "Org Structure"
  387. :group 'org)
  388. (defgroup org-reveal-location nil
  389. "Options about how to make context of a location visible."
  390. :tag "Org Reveal Location"
  391. :group 'org-structure)
  392. (defconst org-context-choice
  393. '(choice
  394. (const :tag "Always" t)
  395. (const :tag "Never" nil)
  396. (repeat :greedy t :tag "Individual contexts"
  397. (cons
  398. (choice :tag "Context"
  399. (const agenda)
  400. (const org-goto)
  401. (const occur-tree)
  402. (const tags-tree)
  403. (const link-search)
  404. (const mark-goto)
  405. (const bookmark-jump)
  406. (const isearch)
  407. (const default))
  408. (boolean))))
  409. "Contexts for the reveal options.")
  410. (defcustom org-show-hierarchy-above '((default . t))
  411. "Non-nil means, show full hierarchy when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the hierarchy of headings
  414. above the exposed location is shown.
  415. Turning this off for example for sparse trees makes them very compact.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. Valid contexts are
  418. agenda when exposing an entry from the agenda
  419. org-goto when using the command `org-goto' on key C-c C-j
  420. occur-tree when using the command `org-occur' on key C-c /
  421. tags-tree when constructing a sparse tree based on tags matches
  422. link-search when exposing search matches associated with a link
  423. mark-goto when exposing the jump goal of a mark
  424. bookmark-jump when exposing a bookmark location
  425. isearch when exiting from an incremental search
  426. default default for all contexts not set explicitly"
  427. :group 'org-reveal-location
  428. :type org-context-choice)
  429. (defcustom org-show-following-heading '((default . nil))
  430. "Non-nil means, show following heading when revealing a location.
  431. Org-mode often shows locations in an org-mode file which might have
  432. been invisible before. When this is set, the heading following the
  433. match is shown.
  434. Turning this off for example for sparse trees makes them very compact,
  435. but makes it harder to edit the location of the match. In such a case,
  436. use the command \\[org-reveal] to show more context.
  437. Instead of t, this can also be an alist specifying this option for different
  438. contexts. See `org-show-hierarchy-above' for valid contexts."
  439. :group 'org-reveal-location
  440. :type org-context-choice)
  441. (defcustom org-show-siblings '((default . nil) (isearch t))
  442. "Non-nil means, show all sibling heading when revealing a location.
  443. Org-mode often shows locations in an org-mode file which might have
  444. been invisible before. When this is set, the sibling of the current entry
  445. heading are all made visible. If `org-show-hierarchy-above' is t,
  446. the same happens on each level of the hierarchy above the current entry.
  447. By default this is on for the isearch context, off for all other contexts.
  448. Turning this off for example for sparse trees makes them very compact,
  449. but makes it harder to edit the location of the match. In such a case,
  450. use the command \\[org-reveal] to show more context.
  451. Instead of t, this can also be an alist specifying this option for different
  452. contexts. See `org-show-hierarchy-above' for valid contexts."
  453. :group 'org-reveal-location
  454. :type org-context-choice)
  455. (defcustom org-show-entry-below '((default . nil))
  456. "Non-nil means, show the entry below a headline when revealing a location.
  457. Org-mode often shows locations in an org-mode file which might have
  458. been invisible before. When this is set, the text below the headline that is
  459. exposed is also shown.
  460. By default this is off for all contexts.
  461. Instead of t, this can also be an alist specifying this option for different
  462. contexts. See `org-show-hierarchy-above' for valid contexts."
  463. :group 'org-reveal-location
  464. :type org-context-choice)
  465. (defcustom org-indirect-buffer-display 'other-window
  466. "How should indirect tree buffers be displayed?
  467. This applies to indirect buffers created with the commands
  468. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  469. Valid values are:
  470. current-window Display in the current window
  471. other-window Just display in another window.
  472. dedicated-frame Create one new frame, and re-use it each time.
  473. new-frame Make a new frame each time. Note that in this case
  474. previously-made indirect buffers are kept, and you need to
  475. kill these buffers yourself."
  476. :group 'org-structure
  477. :group 'org-agenda-windows
  478. :type '(choice
  479. (const :tag "In current window" current-window)
  480. (const :tag "In current frame, other window" other-window)
  481. (const :tag "Each time a new frame" new-frame)
  482. (const :tag "One dedicated frame" dedicated-frame)))
  483. (defgroup org-cycle nil
  484. "Options concerning visibility cycling in Org-mode."
  485. :tag "Org Cycle"
  486. :group 'org-structure)
  487. (defcustom org-cycle-max-level nil
  488. "Maximum level which should still be subject to visibility cycling.
  489. Levels higher than this will, for cycling, be treated as text, not a headline.
  490. When `org-odd-levels-only' is set, a value of N in this variable actually
  491. means 2N-1 stars as the limiting headline.
  492. When nil, cycle all levels.
  493. Note that the limiting level of cycling is also influenced by
  494. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  495. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  496. than its value."
  497. :group 'org-cycle
  498. :type '(choice
  499. (const :tag "No limit" nil)
  500. (integer :tag "Maximum level")))
  501. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  502. "Names of drawers. Drawers are not opened by cycling on the headline above.
  503. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  504. this:
  505. :DRAWERNAME:
  506. .....
  507. :END:
  508. The drawer \"PROPERTIES\" is special for capturing properties through
  509. the property API.
  510. Drawers can be defined on the per-file basis with a line like:
  511. #+DRAWERS: HIDDEN STATE PROPERTIES"
  512. :group 'org-structure
  513. :group 'org-cycle
  514. :type '(repeat (string :tag "Drawer Name")))
  515. (defcustom org-hide-block-startup nil
  516. "Non-nil means, , entering Org-mode will fold all blocks.
  517. This can also be set in on a per-file basis with
  518. #+STARTUP: hideblocks
  519. #+STARTUP: showblocks"
  520. :group 'org-startup
  521. :group 'org-cycle
  522. :type 'boolean)
  523. (defcustom org-cycle-global-at-bob nil
  524. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  525. This makes it possible to do global cycling without having to use S-TAB or
  526. C-u TAB. For this special case to work, the first line of the buffer
  527. must not be a headline - it may be empty or some other text. When used in
  528. this way, `org-cycle-hook' is disables temporarily, to make sure the
  529. cursor stays at the beginning of the buffer.
  530. When this option is nil, don't do anything special at the beginning
  531. of the buffer."
  532. :group 'org-cycle
  533. :type 'boolean)
  534. (defcustom org-cycle-emulate-tab t
  535. "Where should `org-cycle' emulate TAB.
  536. nil Never
  537. white Only in completely white lines
  538. whitestart Only at the beginning of lines, before the first non-white char
  539. t Everywhere except in headlines
  540. exc-hl-bol Everywhere except at the start of a headline
  541. If TAB is used in a place where it does not emulate TAB, the current subtree
  542. visibility is cycled."
  543. :group 'org-cycle
  544. :type '(choice (const :tag "Never" nil)
  545. (const :tag "Only in completely white lines" white)
  546. (const :tag "Before first char in a line" whitestart)
  547. (const :tag "Everywhere except in headlines" t)
  548. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  549. ))
  550. (defcustom org-cycle-separator-lines 2
  551. "Number of empty lines needed to keep an empty line between collapsed trees.
  552. If you leave an empty line between the end of a subtree and the following
  553. headline, this empty line is hidden when the subtree is folded.
  554. Org-mode will leave (exactly) one empty line visible if the number of
  555. empty lines is equal or larger to the number given in this variable.
  556. So the default 2 means, at least 2 empty lines after the end of a subtree
  557. are needed to produce free space between a collapsed subtree and the
  558. following headline.
  559. Special case: when 0, never leave empty lines in collapsed view."
  560. :group 'org-cycle
  561. :type 'integer)
  562. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  563. (defcustom org-pre-cycle-hook nil
  564. "Hook that is run before visibility cycling is happening.
  565. The function(s) in this hook must accept a single argument which indicates
  566. the new state that will be set right after running this hook. The
  567. argument is a symbol. Before a global state change, it can have the values
  568. `overview', `content', or `all'. Before a local state change, it can have
  569. the values `folded', `children', or `subtree'."
  570. :group 'org-cycle
  571. :type 'hook)
  572. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  573. org-cycle-hide-drawers
  574. org-cycle-show-empty-lines
  575. org-optimize-window-after-visibility-change)
  576. "Hook that is run after `org-cycle' has changed the buffer visibility.
  577. The function(s) in this hook must accept a single argument which indicates
  578. the new state that was set by the most recent `org-cycle' command. The
  579. argument is a symbol. After a global state change, it can have the values
  580. `overview', `content', or `all'. After a local state change, it can have
  581. the values `folded', `children', or `subtree'."
  582. :group 'org-cycle
  583. :type 'hook)
  584. (defgroup org-edit-structure nil
  585. "Options concerning structure editing in Org-mode."
  586. :tag "Org Edit Structure"
  587. :group 'org-structure)
  588. (defcustom org-odd-levels-only nil
  589. "Non-nil means, skip even levels and only use odd levels for the outline.
  590. This has the effect that two stars are being added/taken away in
  591. promotion/demotion commands. It also influences how levels are
  592. handled by the exporters.
  593. Changing it requires restart of `font-lock-mode' to become effective
  594. for fontification also in regions already fontified.
  595. You may also set this on a per-file basis by adding one of the following
  596. lines to the buffer:
  597. #+STARTUP: odd
  598. #+STARTUP: oddeven"
  599. :group 'org-edit-structure
  600. :group 'org-font-lock
  601. :type 'boolean)
  602. (defcustom org-adapt-indentation t
  603. "Non-nil means, adapt indentation when promoting and demoting.
  604. When this is set and the *entire* text in an entry is indented, the
  605. indentation is increased by one space in a demotion command, and
  606. decreased by one in a promotion command. If any line in the entry
  607. body starts at column 0, indentation is not changed at all.
  608. This variable also influences how property drawers and planning
  609. information is inserted. When t, these lines drawers will be inserted
  610. indented. When nil, they will not be indented."
  611. :group 'org-edit-structure
  612. :type 'boolean)
  613. (defcustom org-special-ctrl-a/e nil
  614. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  615. When t, `C-a' will bring back the cursor to the beginning of the
  616. headline text, i.e. after the stars and after a possible TODO keyword.
  617. In an item, this will be the position after the bullet.
  618. When the cursor is already at that position, another `C-a' will bring
  619. it to the beginning of the line.
  620. `C-e' will jump to the end of the headline, ignoring the presence of tags
  621. in the headline. A second `C-e' will then jump to the true end of the
  622. line, after any tags.
  623. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  624. and only a directly following, identical keypress will bring the cursor
  625. to the special positions.
  626. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  627. set separately."
  628. :group 'org-edit-structure
  629. :type '(choice
  630. (const :tag "off" nil)
  631. (const :tag "after stars/bullet and before tags first" t)
  632. (const :tag "true line boundary first" reversed)
  633. (cons :tag "Set C-a and C-e separately"
  634. (choice :tag "Special C-a"
  635. (const :tag "off" nil)
  636. (const :tag "after stars/bullet first" t)
  637. (const :tag "before stars/bullet first" reversed))
  638. (choice :tag "Special C-e"
  639. (const :tag "off" nil)
  640. (const :tag "before tags first" t)
  641. (const :tag "after tags first" reversed)))))
  642. (if (fboundp 'defvaralias)
  643. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  644. (defcustom org-special-ctrl-k nil
  645. "Non-nil means `C-k' will behave specially in headlines.
  646. When nil, `C-k' will call the default `kill-line' command.
  647. When t, the following will happen while the cursor is in the headline:
  648. - When the cursor is at the beginning of a headline, kill the entire
  649. line and possible the folded subtree below the line.
  650. - When in the middle of the headline text, kill the headline up to the tags.
  651. - When after the headline text, kill the tags."
  652. :group 'org-edit-structure
  653. :type 'boolean)
  654. (defcustom org-yank-folded-subtrees t
  655. "Non-nil means, when yanking subtrees, fold them.
  656. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  657. it starts with a heading and all other headings in it are either children
  658. or siblings, then fold all the subtrees. However, do this only if no
  659. text after the yank would be swallowed into a folded tree by this action."
  660. :group 'org-edit-structure
  661. :type 'boolean)
  662. (defcustom org-yank-adjusted-subtrees nil
  663. "Non-nil means, when yanking subtrees, adjust the level.
  664. With this setting, `org-paste-subtree' is used to insert the subtree, see
  665. this function for details."
  666. :group 'org-edit-structure
  667. :type 'boolean)
  668. (defcustom org-M-RET-may-split-line '((default . t))
  669. "Non-nil means, M-RET will split the line at the cursor position.
  670. When nil, it will go to the end of the line before making a
  671. new line.
  672. You may also set this option in a different way for different
  673. contexts. Valid contexts are:
  674. headline when creating a new headline
  675. item when creating a new item
  676. table in a table field
  677. default the value to be used for all contexts not explicitly
  678. customized"
  679. :group 'org-structure
  680. :group 'org-table
  681. :type '(choice
  682. (const :tag "Always" t)
  683. (const :tag "Never" nil)
  684. (repeat :greedy t :tag "Individual contexts"
  685. (cons
  686. (choice :tag "Context"
  687. (const headline)
  688. (const item)
  689. (const table)
  690. (const default))
  691. (boolean)))))
  692. (defcustom org-insert-heading-respect-content nil
  693. "Non-nil means, insert new headings after the current subtree.
  694. When nil, the new heading is created directly after the current line.
  695. The commands \\[org-insert-heading-respect-content] and
  696. \\[org-insert-todo-heading-respect-content] turn this variable on
  697. for the duration of the command."
  698. :group 'org-structure
  699. :type 'boolean)
  700. (defcustom org-blank-before-new-entry '((heading . auto)
  701. (plain-list-item . auto))
  702. "Should `org-insert-heading' leave a blank line before new heading/item?
  703. The value is an alist, with `heading' and `plain-list-item' as car,
  704. and a boolean flag as cdr. For plain lists, if the variable
  705. `org-empty-line-terminates-plain-lists' is set, the setting here
  706. is ignored and no empty line is inserted, to keep the list in tact."
  707. :group 'org-edit-structure
  708. :type '(list
  709. (cons (const heading)
  710. (choice (const :tag "Never" nil)
  711. (const :tag "Always" t)
  712. (const :tag "Auto" auto)))
  713. (cons (const plain-list-item)
  714. (choice (const :tag "Never" nil)
  715. (const :tag "Always" t)
  716. (const :tag "Auto" auto)))))
  717. (defcustom org-insert-heading-hook nil
  718. "Hook being run after inserting a new heading."
  719. :group 'org-edit-structure
  720. :type 'hook)
  721. (defcustom org-enable-fixed-width-editor t
  722. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  723. This currently only means, they are never auto-wrapped.
  724. When nil, such lines will be treated like ordinary lines.
  725. See also the QUOTE keyword."
  726. :group 'org-edit-structure
  727. :type 'boolean)
  728. (defcustom org-edit-src-region-extra nil
  729. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  730. For examples see the function `org-edit-src-find-region-and-lang'.
  731. The regular expression identifying the begin marker should end with a newline,
  732. and the regexp marking the end line should start with a newline, to make sure
  733. there are kept outside the narrowed region."
  734. :group 'org-edit-structure
  735. :type '(repeat
  736. (list
  737. (regexp :tag "begin regexp")
  738. (regexp :tag "end regexp")
  739. (choice :tag "language"
  740. (string :tag "specify")
  741. (integer :tag "from match group")
  742. (const :tag "from `lang' element")
  743. (const :tag "from `style' element")))))
  744. (defcustom org-coderef-label-format "(ref:%s)"
  745. "The default coderef format.
  746. This format string will be used to search for coderef labels in literal
  747. examples (EXAMPLE and SRC blocks). The format can be overwritten
  748. an individual literal example with the -f option, like
  749. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  750. ...
  751. #+END_SRC
  752. If you want to use this for HTML export, make sure that the format does
  753. not introduce special font-locking, and avoid the HTML special
  754. characters `<', `>', and `&'. The reason for this restriction is that
  755. the labels are searched for only after htmlize has done its job."
  756. :group 'org-edit-structure ; FIXME this is not in the right group
  757. :type 'string)
  758. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  759. "The mode that should be used to edit fixed-width regions.
  760. These are the regions where each line starts with a colon."
  761. :group 'org-edit-structure
  762. :type '(choice
  763. (const artist-mode)
  764. (const picture-mode)
  765. (const fundamental-mode)
  766. (function :tag "Other (specify)")))
  767. (defcustom org-edit-src-persistent-message t
  768. "Non-nil means show persistent exit help message while editing src examples.
  769. The message is shown in the header-line, which will be created in the
  770. first line of the window showing the editing buffer.
  771. When nil, the message will only be shown intermittently in the echo area."
  772. :group 'org-edit-structure
  773. :type 'boolean)
  774. (defcustom org-goto-auto-isearch t
  775. "Non-nil means, typing characters in org-goto starts incremental search."
  776. :group 'org-edit-structure
  777. :type 'boolean)
  778. (defgroup org-sparse-trees nil
  779. "Options concerning sparse trees in Org-mode."
  780. :tag "Org Sparse Trees"
  781. :group 'org-structure)
  782. (defcustom org-highlight-sparse-tree-matches t
  783. "Non-nil means, highlight all matches that define a sparse tree.
  784. The highlights will automatically disappear the next time the buffer is
  785. changed by an edit command."
  786. :group 'org-sparse-trees
  787. :type 'boolean)
  788. (defcustom org-remove-highlights-with-change t
  789. "Non-nil means, any change to the buffer will remove temporary highlights.
  790. Such highlights are created by `org-occur' and `org-clock-display'.
  791. When nil, `C-c C-c needs to be used to get rid of the highlights.
  792. The highlights created by `org-preview-latex-fragment' always need
  793. `C-c C-c' to be removed."
  794. :group 'org-sparse-trees
  795. :group 'org-time
  796. :type 'boolean)
  797. (defcustom org-occur-hook '(org-first-headline-recenter)
  798. "Hook that is run after `org-occur' has constructed a sparse tree.
  799. This can be used to recenter the window to show as much of the structure
  800. as possible."
  801. :group 'org-sparse-trees
  802. :type 'hook)
  803. (defgroup org-imenu-and-speedbar nil
  804. "Options concerning imenu and speedbar in Org-mode."
  805. :tag "Org Imenu and Speedbar"
  806. :group 'org-structure)
  807. (defcustom org-imenu-depth 2
  808. "The maximum level for Imenu access to Org-mode headlines.
  809. This also applied for speedbar access."
  810. :group 'org-imenu-and-speedbar
  811. :type 'integer)
  812. (defgroup org-table nil
  813. "Options concerning tables in Org-mode."
  814. :tag "Org Table"
  815. :group 'org)
  816. (defcustom org-enable-table-editor 'optimized
  817. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  818. When nil, such lines will be treated like ordinary lines.
  819. When equal to the symbol `optimized', the table editor will be optimized to
  820. do the following:
  821. - Automatic overwrite mode in front of whitespace in table fields.
  822. This makes the structure of the table stay in tact as long as the edited
  823. field does not exceed the column width.
  824. - Minimize the number of realigns. Normally, the table is aligned each time
  825. TAB or RET are pressed to move to another field. With optimization this
  826. happens only if changes to a field might have changed the column width.
  827. Optimization requires replacing the functions `self-insert-command',
  828. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  829. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  830. very good at guessing when a re-align will be necessary, but you can always
  831. force one with \\[org-ctrl-c-ctrl-c].
  832. If you would like to use the optimized version in Org-mode, but the
  833. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  834. This variable can be used to turn on and off the table editor during a session,
  835. but in order to toggle optimization, a restart is required.
  836. See also the variable `org-table-auto-blank-field'."
  837. :group 'org-table
  838. :type '(choice
  839. (const :tag "off" nil)
  840. (const :tag "on" t)
  841. (const :tag "on, optimized" optimized)))
  842. (defcustom org-self-insert-cluster-for-undo t
  843. "Non-nil means cluster self-insert commands for undo when possible.
  844. If this is set, then, like in the Emacs command loop, 20 consequtive
  845. characters will be undone together.
  846. This is configurable, because there is some impact on typing performance."
  847. :group 'org-table
  848. :type 'boolean)
  849. (defcustom org-table-tab-recognizes-table.el t
  850. "Non-nil means, TAB will automatically notice a table.el table.
  851. When it sees such a table, it moves point into it and - if necessary -
  852. calls `table-recognize-table'."
  853. :group 'org-table-editing
  854. :type 'boolean)
  855. (defgroup org-link nil
  856. "Options concerning links in Org-mode."
  857. :tag "Org Link"
  858. :group 'org)
  859. (defvar org-link-abbrev-alist-local nil
  860. "Buffer-local version of `org-link-abbrev-alist', which see.
  861. The value of this is taken from the #+LINK lines.")
  862. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  863. (defcustom org-link-abbrev-alist nil
  864. "Alist of link abbreviations.
  865. The car of each element is a string, to be replaced at the start of a link.
  866. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  867. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  868. [[linkkey:tag][description]]
  869. The 'linkkey' must be a word word, starting with a letter, followed
  870. by letters, numbers, '-' or '_'.
  871. If REPLACE is a string, the tag will simply be appended to create the link.
  872. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  873. the placeholder \"%h\" will cause a url-encoded version of the tag to
  874. be inserted at that point (see the function `url-hexify-string').
  875. REPLACE may also be a function that will be called with the tag as the
  876. only argument to create the link, which should be returned as a string.
  877. See the manual for examples."
  878. :group 'org-link
  879. :type '(repeat
  880. (cons
  881. (string :tag "Protocol")
  882. (choice
  883. (string :tag "Format")
  884. (function)))))
  885. (defcustom org-descriptive-links t
  886. "Non-nil means, hide link part and only show description of bracket links.
  887. Bracket links are like [[link][description]]. This variable sets the initial
  888. state in new org-mode buffers. The setting can then be toggled on a
  889. per-buffer basis from the Org->Hyperlinks menu."
  890. :group 'org-link
  891. :type 'boolean)
  892. (defcustom org-link-file-path-type 'adaptive
  893. "How the path name in file links should be stored.
  894. Valid values are:
  895. relative Relative to the current directory, i.e. the directory of the file
  896. into which the link is being inserted.
  897. absolute Absolute path, if possible with ~ for home directory.
  898. noabbrev Absolute path, no abbreviation of home directory.
  899. adaptive Use relative path for files in the current directory and sub-
  900. directories of it. For other files, use an absolute path."
  901. :group 'org-link
  902. :type '(choice
  903. (const relative)
  904. (const absolute)
  905. (const noabbrev)
  906. (const adaptive)))
  907. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  908. "Types of links that should be activated in Org-mode files.
  909. This is a list of symbols, each leading to the activation of a certain link
  910. type. In principle, it does not hurt to turn on most link types - there may
  911. be a small gain when turning off unused link types. The types are:
  912. bracket The recommended [[link][description]] or [[link]] links with hiding.
  913. angular Links in angular brackets that may contain whitespace like
  914. <bbdb:Carsten Dominik>.
  915. plain Plain links in normal text, no whitespace, like http://google.com.
  916. radio Text that is matched by a radio target, see manual for details.
  917. tag Tag settings in a headline (link to tag search).
  918. date Time stamps (link to calendar).
  919. footnote Footnote labels.
  920. Changing this variable requires a restart of Emacs to become effective."
  921. :group 'org-link
  922. :type '(set :greedy t
  923. (const :tag "Double bracket links (new style)" bracket)
  924. (const :tag "Angular bracket links (old style)" angular)
  925. (const :tag "Plain text links" plain)
  926. (const :tag "Radio target matches" radio)
  927. (const :tag "Tags" tag)
  928. (const :tag "Timestamps" date)
  929. (const :tag "Footnotes" footnote)))
  930. (defcustom org-make-link-description-function nil
  931. "Function to use to generate link descriptions from links. If
  932. nil the link location will be used. This function must take two
  933. parameters; the first is the link and the second the description
  934. org-insert-link has generated, and should return the description
  935. to use."
  936. :group 'org-link
  937. :type 'function)
  938. (defgroup org-link-store nil
  939. "Options concerning storing links in Org-mode."
  940. :tag "Org Store Link"
  941. :group 'org-link)
  942. (defcustom org-email-link-description-format "Email %c: %.30s"
  943. "Format of the description part of a link to an email or usenet message.
  944. The following %-escapes will be replaced by corresponding information:
  945. %F full \"From\" field
  946. %f name, taken from \"From\" field, address if no name
  947. %T full \"To\" field
  948. %t first name in \"To\" field, address if no name
  949. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  950. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  951. %s subject
  952. %m message-id.
  953. You may use normal field width specification between the % and the letter.
  954. This is for example useful to limit the length of the subject.
  955. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  956. :group 'org-link-store
  957. :type 'string)
  958. (defcustom org-from-is-user-regexp
  959. (let (r1 r2)
  960. (when (and user-mail-address (not (string= user-mail-address "")))
  961. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  962. (when (and user-full-name (not (string= user-full-name "")))
  963. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  964. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  965. "Regexp matched against the \"From:\" header of an email or usenet message.
  966. It should match if the message is from the user him/herself."
  967. :group 'org-link-store
  968. :type 'regexp)
  969. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  970. "Non-nil means, storing a link to an Org file will use entry IDs.
  971. Note that before this variable is even considered, org-id must be loaded,
  972. to please customize `org-modules' and turn it on.
  973. The variable can have the following values:
  974. t Create an ID if needed to make a link to the current entry.
  975. create-if-interactive
  976. If `org-store-link' is called directly (interactively, as a user
  977. command), do create an ID to support the link. But when doing the
  978. job for remember, only use the ID if it already exists. The
  979. purpose of this setting is to avoid proliferation of unwanted
  980. IDs, just because you happen to be in an Org file when you
  981. call `org-remember' that automatically and preemptively
  982. creates a link. If you do want to get an ID link in a remember
  983. template to an entry not having an ID, create it first by
  984. explicitly creating a link to it, using `C-c C-l' first.
  985. use-existing
  986. Use existing ID, do not create one.
  987. nil Never use an ID to make a link, instead link using a text search for
  988. the headline text."
  989. :group 'org-link-store
  990. :type '(choice
  991. (const :tag "Create ID to make link" t)
  992. (const :tag "Create if storing link interactively"
  993. create-if-interactive)
  994. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  995. create-if-interactive-and-no-custom-id)
  996. (const :tag "Only use existing" use-existing)
  997. (const :tag "Do not use ID to create link" nil)))
  998. (defcustom org-context-in-file-links t
  999. "Non-nil means, file links from `org-store-link' contain context.
  1000. A search string will be added to the file name with :: as separator and
  1001. used to find the context when the link is activated by the command
  1002. `org-open-at-point'.
  1003. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1004. negates this setting for the duration of the command."
  1005. :group 'org-link-store
  1006. :type 'boolean)
  1007. (defcustom org-keep-stored-link-after-insertion nil
  1008. "Non-nil means, keep link in list for entire session.
  1009. The command `org-store-link' adds a link pointing to the current
  1010. location to an internal list. These links accumulate during a session.
  1011. The command `org-insert-link' can be used to insert links into any
  1012. Org-mode file (offering completion for all stored links). When this
  1013. option is nil, every link which has been inserted once using \\[org-insert-link]
  1014. will be removed from the list, to make completing the unused links
  1015. more efficient."
  1016. :group 'org-link-store
  1017. :type 'boolean)
  1018. (defgroup org-link-follow nil
  1019. "Options concerning following links in Org-mode."
  1020. :tag "Org Follow Link"
  1021. :group 'org-link)
  1022. (defcustom org-link-translation-function nil
  1023. "Function to translate links with different syntax to Org syntax.
  1024. This can be used to translate links created for example by the Planner
  1025. or emacs-wiki packages to Org syntax.
  1026. The function must accept two parameters, a TYPE containing the link
  1027. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1028. which is everything after the link protocol. It should return a cons
  1029. with possibly modified values of type and path.
  1030. Org contains a function for this, so if you set this variable to
  1031. `org-translate-link-from-planner', you should be able follow many
  1032. links created by planner."
  1033. :group 'org-link-follow
  1034. :type 'function)
  1035. (defcustom org-follow-link-hook nil
  1036. "Hook that is run after a link has been followed."
  1037. :group 'org-link-follow
  1038. :type 'hook)
  1039. (defcustom org-tab-follows-link nil
  1040. "Non-nil means, on links TAB will follow the link.
  1041. Needs to be set before org.el is loaded.
  1042. This really should not be used, it does not make sense, and the
  1043. implementation is bad."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-return-follows-link nil
  1047. "Non-nil means, on links RET will follow the link.
  1048. Needs to be set before org.el is loaded."
  1049. :group 'org-link-follow
  1050. :type 'boolean)
  1051. (defcustom org-mouse-1-follows-link
  1052. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1053. "Non-nil means, mouse-1 on a link will follow the link.
  1054. A longer mouse click will still set point. Does not work on XEmacs.
  1055. Needs to be set before org.el is loaded."
  1056. :group 'org-link-follow
  1057. :type 'boolean)
  1058. (defcustom org-mark-ring-length 4
  1059. "Number of different positions to be recorded in the ring
  1060. Changing this requires a restart of Emacs to work correctly."
  1061. :group 'org-link-follow
  1062. :type 'integer)
  1063. (defcustom org-link-frame-setup
  1064. '((vm . vm-visit-folder-other-frame)
  1065. (gnus . gnus-other-frame)
  1066. (file . find-file-other-window))
  1067. "Setup the frame configuration for following links.
  1068. When following a link with Emacs, it may often be useful to display
  1069. this link in another window or frame. This variable can be used to
  1070. set this up for the different types of links.
  1071. For VM, use any of
  1072. `vm-visit-folder'
  1073. `vm-visit-folder-other-frame'
  1074. For Gnus, use any of
  1075. `gnus'
  1076. `gnus-other-frame'
  1077. `org-gnus-no-new-news'
  1078. For FILE, use any of
  1079. `find-file'
  1080. `find-file-other-window'
  1081. `find-file-other-frame'
  1082. For the calendar, use the variable `calendar-setup'.
  1083. For BBDB, it is currently only possible to display the matches in
  1084. another window."
  1085. :group 'org-link-follow
  1086. :type '(list
  1087. (cons (const vm)
  1088. (choice
  1089. (const vm-visit-folder)
  1090. (const vm-visit-folder-other-window)
  1091. (const vm-visit-folder-other-frame)))
  1092. (cons (const gnus)
  1093. (choice
  1094. (const gnus)
  1095. (const gnus-other-frame)
  1096. (const org-gnus-no-new-news)))
  1097. (cons (const file)
  1098. (choice
  1099. (const find-file)
  1100. (const find-file-other-window)
  1101. (const find-file-other-frame)))))
  1102. (defcustom org-display-internal-link-with-indirect-buffer nil
  1103. "Non-nil means, use indirect buffer to display infile links.
  1104. Activating internal links (from one location in a file to another location
  1105. in the same file) normally just jumps to the location. When the link is
  1106. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1107. another window. When this option is set, the other window actually displays
  1108. an indirect buffer clone of the current buffer, to avoid any visibility
  1109. changes to the current buffer."
  1110. :group 'org-link-follow
  1111. :type 'boolean)
  1112. (defcustom org-open-non-existing-files nil
  1113. "Non-nil means, `org-open-file' will open non-existing files.
  1114. When nil, an error will be generated."
  1115. :group 'org-link-follow
  1116. :type 'boolean)
  1117. (defcustom org-open-directory-means-index-dot-org nil
  1118. "Non-nil means, a link to a directory really means to index.org.
  1119. When nil, following a directory link will run dired or open a finder/explorer
  1120. window on that directory."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1124. "Function and arguments to call for following mailto links.
  1125. This is a list with the first element being a lisp function, and the
  1126. remaining elements being arguments to the function. In string arguments,
  1127. %a will be replaced by the address, and %s will be replaced by the subject
  1128. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1129. :group 'org-link-follow
  1130. :type '(choice
  1131. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1132. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1133. (const :tag "message-mail" (message-mail "%a" "%s"))
  1134. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1135. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing shell links.
  1137. Shell links can be dangerous: just think about a link
  1138. [[shell:rm -rf ~/*][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' if you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1150. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1151. Elisp links can be dangerous: just think about a link
  1152. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1153. This link would show up in your Org-mode document as \"Google Search\",
  1154. but really it would remove your entire home directory.
  1155. Therefore we advise against setting this variable to nil.
  1156. Just change it to `y-or-n-p' if you want to confirm with a
  1157. single keystroke rather than having to type \"yes\"."
  1158. :group 'org-link-follow
  1159. :type '(choice
  1160. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1161. (const :tag "with y-or-n (faster)" y-or-n-p)
  1162. (const :tag "no confirmation (dangerous)" nil)))
  1163. (defconst org-file-apps-defaults-gnu
  1164. '((remote . emacs)
  1165. (system . mailcap)
  1166. (t . mailcap))
  1167. "Default file applications on a UNIX or GNU/Linux system.
  1168. See `org-file-apps'.")
  1169. (defconst org-file-apps-defaults-macosx
  1170. '((remote . emacs)
  1171. (t . "open %s")
  1172. (system . "open %s")
  1173. ("ps.gz" . "gv %s")
  1174. ("eps.gz" . "gv %s")
  1175. ("dvi" . "xdvi %s")
  1176. ("fig" . "xfig %s"))
  1177. "Default file applications on a MacOS X system.
  1178. The system \"open\" is known as a default, but we use X11 applications
  1179. for some files for which the OS does not have a good default.
  1180. See `org-file-apps'.")
  1181. (defconst org-file-apps-defaults-windowsnt
  1182. (list
  1183. '(remote . emacs)
  1184. (cons t
  1185. (list (if (featurep 'xemacs)
  1186. 'mswindows-shell-execute
  1187. 'w32-shell-execute)
  1188. "open" 'file))
  1189. (cons 'system
  1190. (list (if (featurep 'xemacs)
  1191. 'mswindows-shell-execute
  1192. 'w32-shell-execute)
  1193. "open" 'file)))
  1194. "Default file applications on a Windows NT system.
  1195. The system \"open\" is used for most files.
  1196. See `org-file-apps'.")
  1197. (defcustom org-file-apps
  1198. '(
  1199. (auto-mode . emacs)
  1200. ("\\.x?html?\\'" . default)
  1201. ("\\.pdf\\'" . default)
  1202. )
  1203. "External applications for opening `file:path' items in a document.
  1204. Org-mode uses system defaults for different file types, but
  1205. you can use this variable to set the application for a given file
  1206. extension. The entries in this list are cons cells where the car identifies
  1207. files and the cdr the corresponding command. Possible values for the
  1208. file identifier are
  1209. \"regex\" Regular expression matched against the file name. For backward
  1210. compatibility, this can also be a string with only alphanumeric
  1211. characters, which is then interpreted as an extension.
  1212. `directory' Matches a directory
  1213. `remote' Matches a remote file, accessible through tramp or efs.
  1214. Remote files most likely should be visited through Emacs
  1215. because external applications cannot handle such paths.
  1216. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1217. so all files Emacs knows how to handle. Using this with
  1218. command `emacs' will open most files in Emacs. Beware that this
  1219. will also open html files inside Emacs, unless you add
  1220. (\"html\" . default) to the list as well.
  1221. t Default for files not matched by any of the other options.
  1222. `system' The system command to open files, like `open' on Windows
  1223. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1224. that will be selected if you call `C-c C-o' with a double
  1225. `C-u C-u' prefix.
  1226. Possible values for the command are:
  1227. `emacs' The file will be visited by the current Emacs process.
  1228. `default' Use the default application for this file type, which is the
  1229. association for t in the list, most likely in the system-specific
  1230. part.
  1231. This can be used to overrule an unwanted setting in the
  1232. system-specific variable.
  1233. `system' Use the system command for opening files, like \"open\".
  1234. This command is specified by the entry whose car is `system'.
  1235. Most likely, the system-specific version of this variable
  1236. does define this command, but you can overrule/replace it
  1237. here.
  1238. string A command to be executed by a shell; %s will be replaced
  1239. by the path to the file.
  1240. sexp A Lisp form which will be evaluated. The file path will
  1241. be available in the Lisp variable `file'.
  1242. For more examples, see the system specific constants
  1243. `org-file-apps-defaults-macosx'
  1244. `org-file-apps-defaults-windowsnt'
  1245. `org-file-apps-defaults-gnu'."
  1246. :group 'org-link-follow
  1247. :type '(repeat
  1248. (cons (choice :value ""
  1249. (string :tag "Extension")
  1250. (const :tag "System command to open files" system)
  1251. (const :tag "Default for unrecognized files" t)
  1252. (const :tag "Remote file" remote)
  1253. (const :tag "Links to a directory" directory)
  1254. (const :tag "Any files that have Emacs modes"
  1255. auto-mode))
  1256. (choice :value ""
  1257. (const :tag "Visit with Emacs" emacs)
  1258. (const :tag "Use default" default)
  1259. (const :tag "Use the system command" system)
  1260. (string :tag "Command")
  1261. (sexp :tag "Lisp form")))))
  1262. (defgroup org-refile nil
  1263. "Options concerning refiling entries in Org-mode."
  1264. :tag "Org Refile"
  1265. :group 'org)
  1266. (defcustom org-directory "~/org"
  1267. "Directory with org files.
  1268. This is just a default location to look for Org files. There is no need
  1269. at all to put your files into this directory. It is only used in the
  1270. following situations:
  1271. 1. When a remember template specifies a target file that is not an
  1272. absolute path. The path will then be interpreted relative to
  1273. `org-directory'
  1274. 2. When a remember note is filed away in an interactive way (when exiting the
  1275. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1276. with `org-directory' as the default path."
  1277. :group 'org-refile
  1278. :group 'org-remember
  1279. :type 'directory)
  1280. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1281. "Default target for storing notes.
  1282. Used by the hooks for remember.el. This can be a string, or nil to mean
  1283. the value of `remember-data-file'.
  1284. You can set this on a per-template basis with the variable
  1285. `org-remember-templates'."
  1286. :group 'org-refile
  1287. :group 'org-remember
  1288. :type '(choice
  1289. (const :tag "Default from remember-data-file" nil)
  1290. file))
  1291. (defcustom org-goto-interface 'outline
  1292. "The default interface to be used for `org-goto'.
  1293. Allowed values are:
  1294. outline The interface shows an outline of the relevant file
  1295. and the correct heading is found by moving through
  1296. the outline or by searching with incremental search.
  1297. outline-path-completion Headlines in the current buffer are offered via
  1298. completion. This is the interface also used by
  1299. the refile command."
  1300. :group 'org-refile
  1301. :type '(choice
  1302. (const :tag "Outline" outline)
  1303. (const :tag "Outline-path-completion" outline-path-completion)))
  1304. (defcustom org-goto-max-level 5
  1305. "Maximum level to be considered when running org-goto with refile interface."
  1306. :group 'org-refile
  1307. :type 'integer)
  1308. (defcustom org-reverse-note-order nil
  1309. "Non-nil means, store new notes at the beginning of a file or entry.
  1310. When nil, new notes will be filed to the end of a file or entry.
  1311. This can also be a list with cons cells of regular expressions that
  1312. are matched against file names, and values."
  1313. :group 'org-remember
  1314. :group 'org-refile
  1315. :type '(choice
  1316. (const :tag "Reverse always" t)
  1317. (const :tag "Reverse never" nil)
  1318. (repeat :tag "By file name regexp"
  1319. (cons regexp boolean))))
  1320. (defcustom org-refile-targets nil
  1321. "Targets for refiling entries with \\[org-refile].
  1322. This is list of cons cells. Each cell contains:
  1323. - a specification of the files to be considered, either a list of files,
  1324. or a symbol whose function or variable value will be used to retrieve
  1325. a file name or a list of file names. If you use `org-agenda-files' for
  1326. that, all agenda files will be scanned for targets. Nil means, consider
  1327. headings in the current buffer.
  1328. - A specification of how to find candidate refile targets. This may be
  1329. any of:
  1330. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1331. This tag has to be present in all target headlines, inheritance will
  1332. not be considered.
  1333. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1334. todo keyword.
  1335. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1336. headlines that are refiling targets.
  1337. - a cons cell (:level . N). Any headline of level N is considered a target.
  1338. Note that, when `org-odd-levels-only' is set, level corresponds to
  1339. order in hierarchy, not to the number of stars.
  1340. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1341. Note that, when `org-odd-levels-only' is set, level corresponds to
  1342. order in hierarchy, not to the number of stars.
  1343. You can set the variable `org-refile-target-verify-function' to a function
  1344. to verify each headline found by the simple critery above.
  1345. When this variable is nil, all top-level headlines in the current buffer
  1346. are used, equivalent to the value `((nil . (:level . 1))'."
  1347. :group 'org-refile
  1348. :type '(repeat
  1349. (cons
  1350. (choice :value org-agenda-files
  1351. (const :tag "All agenda files" org-agenda-files)
  1352. (const :tag "Current buffer" nil)
  1353. (function) (variable) (file))
  1354. (choice :tag "Identify target headline by"
  1355. (cons :tag "Specific tag" (const :value :tag) (string))
  1356. (cons :tag "TODO keyword" (const :value :todo) (string))
  1357. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1358. (cons :tag "Level number" (const :value :level) (integer))
  1359. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1360. (defcustom org-refile-target-verify-function nil
  1361. "Function to verify if the headline at point should be a refile target.
  1362. The function will be called without arguments, with point at the
  1363. beginning of the headline. It should return t and leave point
  1364. where it is if the headline is a valid target for refiling.
  1365. If the target should not be selected, the function must return nil.
  1366. In addition to this, it may move point to a place from where the search
  1367. should be continued. For example, the function may decide that the entire
  1368. subtree of the current entry should be excluded and move point to the end
  1369. of the subtree."
  1370. :group 'org-refile
  1371. :type 'function)
  1372. (defcustom org-refile-use-outline-path nil
  1373. "Non-nil means, provide refile targets as paths.
  1374. So a level 3 headline will be available as level1/level2/level3.
  1375. When the value is `file', also include the file name (without directory)
  1376. into the path. In this case, you can also stop the completion after
  1377. the file name, to get entries inserted as top level in the file.
  1378. When `full-file-path', include the full file path."
  1379. :group 'org-refile
  1380. :type '(choice
  1381. (const :tag "Not" nil)
  1382. (const :tag "Yes" t)
  1383. (const :tag "Start with file name" file)
  1384. (const :tag "Start with full file path" full-file-path)))
  1385. (defcustom org-outline-path-complete-in-steps t
  1386. "Non-nil means, complete the outline path in hierarchical steps.
  1387. When Org-mode uses the refile interface to select an outline path
  1388. \(see variable `org-refile-use-outline-path'), the completion of
  1389. the path can be done is a single go, or if can be done in steps down
  1390. the headline hierarchy. Going in steps is probably the best if you
  1391. do not use a special completion package like `ido' or `icicles'.
  1392. However, when using these packages, going in one step can be very
  1393. fast, while still showing the whole path to the entry."
  1394. :group 'org-refile
  1395. :type 'boolean)
  1396. (defcustom org-refile-allow-creating-parent-nodes nil
  1397. "Non-nil means, allow to create new nodes as refile targets.
  1398. New nodes are then created by adding \"/new node name\" to the completion
  1399. of an existing node. When the value of this variable is `confirm',
  1400. new node creation must be confirmed by the user (recommended)
  1401. When nil, the completion must match an existing entry.
  1402. Note that, if the new heading is not seen by the criteria
  1403. listed in `org-refile-targets', multiple instances of the same
  1404. heading would be created by trying again to file under the new
  1405. heading."
  1406. :group 'org-refile
  1407. :type '(choice
  1408. (const :tag "Never" nil)
  1409. (const :tag "Always" t)
  1410. (const :tag "Prompt for confirmation" confirm)))
  1411. (defgroup org-todo nil
  1412. "Options concerning TODO items in Org-mode."
  1413. :tag "Org TODO"
  1414. :group 'org)
  1415. (defgroup org-progress nil
  1416. "Options concerning Progress logging in Org-mode."
  1417. :tag "Org Progress"
  1418. :group 'org-time)
  1419. (defvar org-todo-interpretation-widgets
  1420. '(
  1421. (:tag "Sequence (cycling hits every state)" sequence)
  1422. (:tag "Type (cycling directly to DONE)" type))
  1423. "The available interpretation symbols for customizing
  1424. `org-todo-keywords'.
  1425. Interested libraries should add to this list.")
  1426. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1427. "List of TODO entry keyword sequences and their interpretation.
  1428. \\<org-mode-map>This is a list of sequences.
  1429. Each sequence starts with a symbol, either `sequence' or `type',
  1430. indicating if the keywords should be interpreted as a sequence of
  1431. action steps, or as different types of TODO items. The first
  1432. keywords are states requiring action - these states will select a headline
  1433. for inclusion into the global TODO list Org-mode produces. If one of
  1434. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1435. signify that no further action is necessary. If \"|\" is not found,
  1436. the last keyword is treated as the only DONE state of the sequence.
  1437. The command \\[org-todo] cycles an entry through these states, and one
  1438. additional state where no keyword is present. For details about this
  1439. cycling, see the manual.
  1440. TODO keywords and interpretation can also be set on a per-file basis with
  1441. the special #+SEQ_TODO and #+TYP_TODO lines.
  1442. Each keyword can optionally specify a character for fast state selection
  1443. \(in combination with the variable `org-use-fast-todo-selection')
  1444. and specifiers for state change logging, using the same syntax
  1445. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1446. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1447. indicates to record a time stamp each time this state is selected.
  1448. Each keyword may also specify if a timestamp or a note should be
  1449. recorded when entering or leaving the state, by adding additional
  1450. characters in the parenthesis after the keyword. This looks like this:
  1451. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1452. record only the time of the state change. With X and Y being either
  1453. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1454. Y when leaving the state if and only if the *target* state does not
  1455. define X. You may omit any of the fast-selection key or X or /Y,
  1456. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1457. For backward compatibility, this variable may also be just a list
  1458. of keywords - in this case the interpretation (sequence or type) will be
  1459. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1460. :group 'org-todo
  1461. :group 'org-keywords
  1462. :type '(choice
  1463. (repeat :tag "Old syntax, just keywords"
  1464. (string :tag "Keyword"))
  1465. (repeat :tag "New syntax"
  1466. (cons
  1467. (choice
  1468. :tag "Interpretation"
  1469. ;;Quick and dirty way to see
  1470. ;;`org-todo-interpretations'. This takes the
  1471. ;;place of item arguments
  1472. :convert-widget
  1473. (lambda (widget)
  1474. (widget-put widget
  1475. :args (mapcar
  1476. #'(lambda (x)
  1477. (widget-convert
  1478. (cons 'const x)))
  1479. org-todo-interpretation-widgets))
  1480. widget))
  1481. (repeat
  1482. (string :tag "Keyword"))))))
  1483. (defvar org-todo-keywords-1 nil
  1484. "All TODO and DONE keywords active in a buffer.")
  1485. (make-variable-buffer-local 'org-todo-keywords-1)
  1486. (defvar org-todo-keywords-for-agenda nil)
  1487. (defvar org-done-keywords-for-agenda nil)
  1488. (defvar org-todo-keyword-alist-for-agenda nil)
  1489. (defvar org-tag-alist-for-agenda nil)
  1490. (defvar org-agenda-contributing-files nil)
  1491. (defvar org-not-done-keywords nil)
  1492. (make-variable-buffer-local 'org-not-done-keywords)
  1493. (defvar org-done-keywords nil)
  1494. (make-variable-buffer-local 'org-done-keywords)
  1495. (defvar org-todo-heads nil)
  1496. (make-variable-buffer-local 'org-todo-heads)
  1497. (defvar org-todo-sets nil)
  1498. (make-variable-buffer-local 'org-todo-sets)
  1499. (defvar org-todo-log-states nil)
  1500. (make-variable-buffer-local 'org-todo-log-states)
  1501. (defvar org-todo-kwd-alist nil)
  1502. (make-variable-buffer-local 'org-todo-kwd-alist)
  1503. (defvar org-todo-key-alist nil)
  1504. (make-variable-buffer-local 'org-todo-key-alist)
  1505. (defvar org-todo-key-trigger nil)
  1506. (make-variable-buffer-local 'org-todo-key-trigger)
  1507. (defcustom org-todo-interpretation 'sequence
  1508. "Controls how TODO keywords are interpreted.
  1509. This variable is in principle obsolete and is only used for
  1510. backward compatibility, if the interpretation of todo keywords is
  1511. not given already in `org-todo-keywords'. See that variable for
  1512. more information."
  1513. :group 'org-todo
  1514. :group 'org-keywords
  1515. :type '(choice (const sequence)
  1516. (const type)))
  1517. (defcustom org-use-fast-todo-selection t
  1518. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1519. This variable describes if and under what circumstances the cycling
  1520. mechanism for TODO keywords will be replaced by a single-key, direct
  1521. selection scheme.
  1522. When nil, fast selection is never used.
  1523. When the symbol `prefix', it will be used when `org-todo' is called with
  1524. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1525. in an agenda buffer.
  1526. When t, fast selection is used by default. In this case, the prefix
  1527. argument forces cycling instead.
  1528. In all cases, the special interface is only used if access keys have actually
  1529. been assigned by the user, i.e. if keywords in the configuration are followed
  1530. by a letter in parenthesis, like TODO(t)."
  1531. :group 'org-todo
  1532. :type '(choice
  1533. (const :tag "Never" nil)
  1534. (const :tag "By default" t)
  1535. (const :tag "Only with C-u C-c C-t" prefix)))
  1536. (defcustom org-provide-todo-statistics t
  1537. "Non-nil means, update todo statistics after insert and toggle.
  1538. When this is set, todo statistics is updated in the parent of the current
  1539. entry each time a todo state is changed."
  1540. :group 'org-todo
  1541. :type 'boolean)
  1542. (defcustom org-hierarchical-todo-statistics t
  1543. "Non-nil means, TODO statistics covers just direct children.
  1544. When nil, all entries in the subtree are considered.
  1545. This has only an effect if `org-provide-todo-statistics' is set."
  1546. :group 'org-todo
  1547. :type 'boolean)
  1548. (defcustom org-after-todo-state-change-hook nil
  1549. "Hook which is run after the state of a TODO item was changed.
  1550. The new state (a string with a TODO keyword, or nil) is available in the
  1551. Lisp variable `state'."
  1552. :group 'org-todo
  1553. :type 'hook)
  1554. (defvar org-blocker-hook nil
  1555. "Hook for functions that are allowed to block a state change.
  1556. Each function gets as its single argument a property list, see
  1557. `org-trigger-hook' for more information about this list.
  1558. If any of the functions in this hook returns nil, the state change
  1559. is blocked.")
  1560. (defvar org-trigger-hook nil
  1561. "Hook for functions that are triggered by a state change.
  1562. Each function gets as its single argument a property list with at least
  1563. the following elements:
  1564. (:type type-of-change :position pos-at-entry-start
  1565. :from old-state :to new-state)
  1566. Depending on the type, more properties may be present.
  1567. This mechanism is currently implemented for:
  1568. TODO state changes
  1569. ------------------
  1570. :type todo-state-change
  1571. :from previous state (keyword as a string), or nil, or a symbol
  1572. 'todo' or 'done', to indicate the general type of state.
  1573. :to new state, like in :from")
  1574. (defcustom org-enforce-todo-dependencies nil
  1575. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1576. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1577. be blocked if any prior sibling is not yet done.
  1578. Finally, if the parent is blocked because of ordered siblings of its own,
  1579. the child will also be blocked.
  1580. This variable needs to be set before org.el is loaded, and you need to
  1581. restart Emacs after a change to make the change effective. The only way
  1582. to change is while Emacs is running is through the customize interface."
  1583. :set (lambda (var val)
  1584. (set var val)
  1585. (if val
  1586. (add-hook 'org-blocker-hook
  1587. 'org-block-todo-from-children-or-siblings-or-parent)
  1588. (remove-hook 'org-blocker-hook
  1589. 'org-block-todo-from-children-or-siblings-or-parent)))
  1590. :group 'org-todo
  1591. :type 'boolean)
  1592. (defcustom org-enforce-todo-checkbox-dependencies nil
  1593. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1594. When this is nil, checkboxes have no influence on switching TODO states.
  1595. When non-nil, you first need to check off all check boxes before the TODO
  1596. entry can be switched to DONE.
  1597. This variable needs to be set before org.el is loaded, and you need to
  1598. restart Emacs after a change to make the change effective. The only way
  1599. to change is while Emacs is running is through the customize interface."
  1600. :set (lambda (var val)
  1601. (set var val)
  1602. (if val
  1603. (add-hook 'org-blocker-hook
  1604. 'org-block-todo-from-checkboxes)
  1605. (remove-hook 'org-blocker-hook
  1606. 'org-block-todo-from-checkboxes)))
  1607. :group 'org-todo
  1608. :type 'boolean)
  1609. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1610. "Non-nil means, inserting a TODO heading is treated as state change.
  1611. So when the command \\[org-insert-todo-heading] is used, state change
  1612. logging will apply if appropriate. When nil, the new TODO item will
  1613. be inserted directly, and no logging will take place."
  1614. :group 'org-todo
  1615. :type 'boolean)
  1616. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1617. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1618. This is the default behavior. However, setting this to nil allows a
  1619. convenient way to select a TODO state and bypass any logging associated
  1620. with that."
  1621. :group 'org-todo
  1622. :type 'boolean)
  1623. (defcustom org-todo-state-tags-triggers nil
  1624. "Tag changes that should be triggered by TODO state changes.
  1625. This is a list. Each entry is
  1626. (state-change (tag . flag) .......)
  1627. State-change can be a string with a state, and empty string to indicate the
  1628. state that has no TODO keyword, or it can be one of the symbols `todo'
  1629. or `done', meaning any not-done or done state, respectively."
  1630. :group 'org-todo
  1631. :group 'org-tags
  1632. :type '(repeat
  1633. (cons (choice :tag "When changing to"
  1634. (const :tag "Not-done state" todo)
  1635. (const :tag "Done state" done)
  1636. (string :tag "State"))
  1637. (repeat
  1638. (cons :tag "Tag action"
  1639. (string :tag "Tag")
  1640. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1641. (defcustom org-log-done nil
  1642. "Information to record when a task moves to the DONE state.
  1643. Possible values are:
  1644. nil Don't add anything, just change the keyword
  1645. time Add a time stamp to the task
  1646. note Prompt a closing note and add it with template `org-log-note-headings'
  1647. This option can also be set with on a per-file-basis with
  1648. #+STARTUP: nologdone
  1649. #+STARTUP: logdone
  1650. #+STARTUP: lognotedone
  1651. You can have local logging settings for a subtree by setting the LOGGING
  1652. property to one or more of these keywords."
  1653. :group 'org-todo
  1654. :group 'org-progress
  1655. :type '(choice
  1656. (const :tag "No logging" nil)
  1657. (const :tag "Record CLOSED timestamp" time)
  1658. (const :tag "Record CLOSED timestamp with closing note." note)))
  1659. ;; Normalize old uses of org-log-done.
  1660. (cond
  1661. ((eq org-log-done t) (setq org-log-done 'time))
  1662. ((and (listp org-log-done) (memq 'done org-log-done))
  1663. (setq org-log-done 'note)))
  1664. (defcustom org-log-note-clock-out nil
  1665. "Non-nil means, record a note when clocking out of an item.
  1666. This can also be configured on a per-file basis by adding one of
  1667. the following lines anywhere in the buffer:
  1668. #+STARTUP: lognoteclock-out
  1669. #+STARTUP: nolognoteclock-out"
  1670. :group 'org-todo
  1671. :group 'org-progress
  1672. :type 'boolean)
  1673. (defcustom org-log-done-with-time t
  1674. "Non-nil means, the CLOSED time stamp will contain date and time.
  1675. When nil, only the date will be recorded."
  1676. :group 'org-progress
  1677. :type 'boolean)
  1678. (defcustom org-log-note-headings
  1679. '((done . "CLOSING NOTE %t")
  1680. (state . "State %-12s from %-12S %t")
  1681. (note . "Note taken on %t")
  1682. (clock-out . ""))
  1683. "Headings for notes added to entries.
  1684. The value is an alist, with the car being a symbol indicating the note
  1685. context, and the cdr is the heading to be used. The heading may also be the
  1686. empty string.
  1687. %t in the heading will be replaced by a time stamp.
  1688. %s will be replaced by the new TODO state, in double quotes.
  1689. %S will be replaced by the old TODO state, in double quotes.
  1690. %u will be replaced by the user name.
  1691. %U will be replaced by the full user name."
  1692. :group 'org-todo
  1693. :group 'org-progress
  1694. :type '(list :greedy t
  1695. (cons (const :tag "Heading when closing an item" done) string)
  1696. (cons (const :tag
  1697. "Heading when changing todo state (todo sequence only)"
  1698. state) string)
  1699. (cons (const :tag "Heading when just taking a note" note) string)
  1700. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1701. (unless (assq 'note org-log-note-headings)
  1702. (push '(note . "%t") org-log-note-headings))
  1703. (defcustom org-log-into-drawer nil
  1704. "Non-nil means, insert state change notes and time stamps into a drawer.
  1705. When nil, state changes notes will be inserted after the headline and
  1706. any scheduling and clock lines, but not inside a drawer.
  1707. The value of this variable should be the name of the drawer to use.
  1708. LOGBOOK is proposed at the default drawer for this purpose, you can
  1709. also set this to a string to define the drawer of your choice.
  1710. A value of t is also allowed, representing \"LOGBOOK\".
  1711. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1712. will be ignored.
  1713. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1714. a subtree."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type '(choice
  1718. (const :tag "Not into a drawer" nil)
  1719. (const :tag "LOGBOOK" t)
  1720. (string :tag "Other")))
  1721. (if (fboundp 'defvaralias)
  1722. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1723. (defun org-log-into-drawer ()
  1724. "Return the value of `org-log-into-drawer', but let properties overrule.
  1725. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1726. used instead of the default value."
  1727. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1728. (cond
  1729. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1730. ((equal p "t") "LOGBOOK")
  1731. (t p))))
  1732. (defcustom org-log-state-notes-insert-after-drawers nil
  1733. "Non-nil means, insert state change notes after any drawers in entry.
  1734. Only the drawers that *immediately* follow the headline and the
  1735. deadline/scheduled line are skipped.
  1736. When nil, insert notes right after the heading and perhaps the line
  1737. with deadline/scheduling if present.
  1738. This variable will have no effect if `org-log-into-drawer' is
  1739. set."
  1740. :group 'org-todo
  1741. :group 'org-progress
  1742. :type 'boolean)
  1743. (defcustom org-log-states-order-reversed t
  1744. "Non-nil means, the latest state change note will be directly after heading.
  1745. When nil, the notes will be orderer according to time."
  1746. :group 'org-todo
  1747. :group 'org-progress
  1748. :type 'boolean)
  1749. (defcustom org-log-repeat 'time
  1750. "Non-nil means, record moving through the DONE state when triggering repeat.
  1751. An auto-repeating tasks is immediately switched back to TODO when marked
  1752. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1753. the TODO keyword definition, or recording a closing note by setting
  1754. `org-log-done', there will be no record of the task moving through DONE.
  1755. This variable forces taking a note anyway. Possible values are:
  1756. nil Don't force a record
  1757. time Record a time stamp
  1758. note Record a note
  1759. This option can also be set with on a per-file-basis with
  1760. #+STARTUP: logrepeat
  1761. #+STARTUP: lognoterepeat
  1762. #+STARTUP: nologrepeat
  1763. You can have local logging settings for a subtree by setting the LOGGING
  1764. property to one or more of these keywords."
  1765. :group 'org-todo
  1766. :group 'org-progress
  1767. :type '(choice
  1768. (const :tag "Don't force a record" nil)
  1769. (const :tag "Force recording the DONE state" time)
  1770. (const :tag "Force recording a note with the DONE state" note)))
  1771. (defgroup org-priorities nil
  1772. "Priorities in Org-mode."
  1773. :tag "Org Priorities"
  1774. :group 'org-todo)
  1775. (defcustom org-enable-priority-commands t
  1776. "Non-nil means, priority commands are active.
  1777. When nil, these commands will be disabled, so that you never accidentally
  1778. set a priority."
  1779. :group 'org-priorities
  1780. :type 'boolean)
  1781. (defcustom org-highest-priority ?A
  1782. "The highest priority of TODO items. A character like ?A, ?B etc.
  1783. Must have a smaller ASCII number than `org-lowest-priority'."
  1784. :group 'org-priorities
  1785. :type 'character)
  1786. (defcustom org-lowest-priority ?C
  1787. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1788. Must have a larger ASCII number than `org-highest-priority'."
  1789. :group 'org-priorities
  1790. :type 'character)
  1791. (defcustom org-default-priority ?B
  1792. "The default priority of TODO items.
  1793. This is the priority an item get if no explicit priority is given."
  1794. :group 'org-priorities
  1795. :type 'character)
  1796. (defcustom org-priority-start-cycle-with-default t
  1797. "Non-nil means, start with default priority when starting to cycle.
  1798. When this is nil, the first step in the cycle will be (depending on the
  1799. command used) one higher or lower that the default priority."
  1800. :group 'org-priorities
  1801. :type 'boolean)
  1802. (defgroup org-time nil
  1803. "Options concerning time stamps and deadlines in Org-mode."
  1804. :tag "Org Time"
  1805. :group 'org)
  1806. (defcustom org-insert-labeled-timestamps-at-point nil
  1807. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1808. When nil, these labeled time stamps are forces into the second line of an
  1809. entry, just after the headline. When scheduling from the global TODO list,
  1810. the time stamp will always be forced into the second line."
  1811. :group 'org-time
  1812. :type 'boolean)
  1813. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1814. "Formats for `format-time-string' which are used for time stamps.
  1815. It is not recommended to change this constant.")
  1816. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1817. "Number of minutes to round time stamps to.
  1818. These are two values, the first applies when first creating a time stamp.
  1819. The second applies when changing it with the commands `S-up' and `S-down'.
  1820. When changing the time stamp, this means that it will change in steps
  1821. of N minutes, as given by the second value.
  1822. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1823. numbers should be factors of 60, so for example 5, 10, 15.
  1824. When this is larger than 1, you can still force an exact time-stamp by using
  1825. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1826. and by using a prefix arg to `S-up/down' to specify the exact number
  1827. of minutes to shift."
  1828. :group 'org-time
  1829. :get '(lambda (var) ; Make sure all entries have 5 elements
  1830. (if (integerp (default-value var))
  1831. (list (default-value var) 5)
  1832. (default-value var)))
  1833. :type '(list
  1834. (integer :tag "when inserting times")
  1835. (integer :tag "when modifying times")))
  1836. ;; Normalize old customizations of this variable.
  1837. (when (integerp org-time-stamp-rounding-minutes)
  1838. (setq org-time-stamp-rounding-minutes
  1839. (list org-time-stamp-rounding-minutes
  1840. org-time-stamp-rounding-minutes)))
  1841. (defcustom org-display-custom-times nil
  1842. "Non-nil means, overlay custom formats over all time stamps.
  1843. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1844. To turn this on on a per-file basis, insert anywhere in the file:
  1845. #+STARTUP: customtime"
  1846. :group 'org-time
  1847. :set 'set-default
  1848. :type 'sexp)
  1849. (make-variable-buffer-local 'org-display-custom-times)
  1850. (defcustom org-time-stamp-custom-formats
  1851. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1852. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1853. These are overlayed over the default ISO format if the variable
  1854. `org-display-custom-times' is set. Time like %H:%M should be at the
  1855. end of the second format. The custom formats are also honored by export
  1856. commands, if custom time display is turned on at the time of export."
  1857. :group 'org-time
  1858. :type 'sexp)
  1859. (defun org-time-stamp-format (&optional long inactive)
  1860. "Get the right format for a time string."
  1861. (let ((f (if long (cdr org-time-stamp-formats)
  1862. (car org-time-stamp-formats))))
  1863. (if inactive
  1864. (concat "[" (substring f 1 -1) "]")
  1865. f)))
  1866. (defcustom org-time-clocksum-format "%d:%02d"
  1867. "The format string used when creating CLOCKSUM lines, or when
  1868. org-mode generates a time duration."
  1869. :group 'org-time
  1870. :type 'string)
  1871. (defcustom org-deadline-warning-days 14
  1872. "No. of days before expiration during which a deadline becomes active.
  1873. This variable governs the display in sparse trees and in the agenda.
  1874. When 0 or negative, it means use this number (the absolute value of it)
  1875. even if a deadline has a different individual lead time specified.
  1876. Custom commands can set this variable in the options section."
  1877. :group 'org-time
  1878. :group 'org-agenda-daily/weekly
  1879. :type 'integer)
  1880. (defcustom org-read-date-prefer-future t
  1881. "Non-nil means, assume future for incomplete date input from user.
  1882. This affects the following situations:
  1883. 1. The user gives a day, but no month.
  1884. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1885. read this as the third of *next* month. However, if you enter \"17\",
  1886. it will be considered as *this* month.
  1887. 2. The user gives a month but not a year.
  1888. For example, if it is april and you enter \"feb 2\", this will be read
  1889. as feb 2, *next* year. \"May 5\", however, will be this year.
  1890. Currently this does not work for ISO week specifications.
  1891. When this option is nil, the current month and year will always be used
  1892. as defaults."
  1893. :group 'org-time
  1894. :type 'boolean)
  1895. (defcustom org-read-date-display-live t
  1896. "Non-nil means, display current interpretation of date prompt live.
  1897. This display will be in an overlay, in the minibuffer."
  1898. :group 'org-time
  1899. :type 'boolean)
  1900. (defcustom org-read-date-popup-calendar t
  1901. "Non-nil means, pop up a calendar when prompting for a date.
  1902. In the calendar, the date can be selected with mouse-1. However, the
  1903. minibuffer will also be active, and you can simply enter the date as well.
  1904. When nil, only the minibuffer will be available."
  1905. :group 'org-time
  1906. :type 'boolean)
  1907. (if (fboundp 'defvaralias)
  1908. (defvaralias 'org-popup-calendar-for-date-prompt
  1909. 'org-read-date-popup-calendar))
  1910. (defcustom org-read-date-minibuffer-setup-hook nil
  1911. "Hook to be used to set up keys for the date/time interface.
  1912. Add key definitions to `minibuffer-local-map', which will be a temporary
  1913. copy."
  1914. :group 'org-time
  1915. :type 'hook)
  1916. (defcustom org-extend-today-until 0
  1917. "The hour when your day really ends. Must be an integer.
  1918. This has influence for the following applications:
  1919. - When switching the agenda to \"today\". It it is still earlier than
  1920. the time given here, the day recognized as TODAY is actually yesterday.
  1921. - When a date is read from the user and it is still before the time given
  1922. here, the current date and time will be assumed to be yesterday, 23:59.
  1923. Also, timestamps inserted in remember templates follow this rule.
  1924. IMPORTANT: This is a feature whose implementation is and likely will
  1925. remain incomplete. Really, it is only here because past midnight seems to
  1926. be the favorite working time of John Wiegley :-)"
  1927. :group 'org-time
  1928. :type 'integer)
  1929. (defcustom org-edit-timestamp-down-means-later nil
  1930. "Non-nil means, S-down will increase the time in a time stamp.
  1931. When nil, S-up will increase."
  1932. :group 'org-time
  1933. :type 'boolean)
  1934. (defcustom org-calendar-follow-timestamp-change t
  1935. "Non-nil means, make the calendar window follow timestamp changes.
  1936. When a timestamp is modified and the calendar window is visible, it will be
  1937. moved to the new date."
  1938. :group 'org-time
  1939. :type 'boolean)
  1940. (defgroup org-tags nil
  1941. "Options concerning tags in Org-mode."
  1942. :tag "Org Tags"
  1943. :group 'org)
  1944. (defcustom org-tag-alist nil
  1945. "List of tags allowed in Org-mode files.
  1946. When this list is nil, Org-mode will base TAG input on what is already in the
  1947. buffer.
  1948. The value of this variable is an alist, the car of each entry must be a
  1949. keyword as a string, the cdr may be a character that is used to select
  1950. that tag through the fast-tag-selection interface.
  1951. See the manual for details."
  1952. :group 'org-tags
  1953. :type '(repeat
  1954. (choice
  1955. (cons (string :tag "Tag name")
  1956. (character :tag "Access char"))
  1957. (const :tag "Start radio group" (:startgroup))
  1958. (const :tag "End radio group" (:endgroup))
  1959. (const :tag "New line" (:newline)))))
  1960. (defcustom org-tag-persistent-alist nil
  1961. "List of tags that will always appear in all Org-mode files.
  1962. This is in addition to any in buffer settings or customizations
  1963. of `org-tag-alist'.
  1964. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1965. The value of this variable is an alist, the car of each entry must be a
  1966. keyword as a string, the cdr may be a character that is used to select
  1967. that tag through the fast-tag-selection interface.
  1968. See the manual for details.
  1969. To disable these tags on a per-file basis, insert anywhere in the file:
  1970. #+STARTUP: noptag"
  1971. :group 'org-tags
  1972. :type '(repeat
  1973. (choice
  1974. (cons (string :tag "Tag name")
  1975. (character :tag "Access char"))
  1976. (const :tag "Start radio group" (:startgroup))
  1977. (const :tag "End radio group" (:endgroup))
  1978. (const :tag "New line" (:newline)))))
  1979. (defvar org-file-tags nil
  1980. "List of tags that can be inherited by all entries in the file.
  1981. The tags will be inherited if the variable `org-use-tag-inheritance'
  1982. says they should be.
  1983. This variable is populated from #+TAG lines.")
  1984. (defcustom org-use-fast-tag-selection 'auto
  1985. "Non-nil means, use fast tag selection scheme.
  1986. This is a special interface to select and deselect tags with single keys.
  1987. When nil, fast selection is never used.
  1988. When the symbol `auto', fast selection is used if and only if selection
  1989. characters for tags have been configured, either through the variable
  1990. `org-tag-alist' or through a #+TAGS line in the buffer.
  1991. When t, fast selection is always used and selection keys are assigned
  1992. automatically if necessary."
  1993. :group 'org-tags
  1994. :type '(choice
  1995. (const :tag "Always" t)
  1996. (const :tag "Never" nil)
  1997. (const :tag "When selection characters are configured" 'auto)))
  1998. (defcustom org-fast-tag-selection-single-key nil
  1999. "Non-nil means, fast tag selection exits after first change.
  2000. When nil, you have to press RET to exit it.
  2001. During fast tag selection, you can toggle this flag with `C-c'.
  2002. This variable can also have the value `expert'. In this case, the window
  2003. displaying the tags menu is not even shown, until you press C-c again."
  2004. :group 'org-tags
  2005. :type '(choice
  2006. (const :tag "No" nil)
  2007. (const :tag "Yes" t)
  2008. (const :tag "Expert" expert)))
  2009. (defvar org-fast-tag-selection-include-todo nil
  2010. "Non-nil means, fast tags selection interface will also offer TODO states.
  2011. This is an undocumented feature, you should not rely on it.")
  2012. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2013. "The column to which tags should be indented in a headline.
  2014. If this number is positive, it specifies the column. If it is negative,
  2015. it means that the tags should be flushright to that column. For example,
  2016. -80 works well for a normal 80 character screen."
  2017. :group 'org-tags
  2018. :type 'integer)
  2019. (defcustom org-auto-align-tags t
  2020. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2021. These operations change the length of a headline and therefore shift
  2022. the tags around. With this options turned on, after each such operation
  2023. the tags are again aligned to `org-tags-column'."
  2024. :group 'org-tags
  2025. :type 'boolean)
  2026. (defcustom org-use-tag-inheritance t
  2027. "Non-nil means, tags in levels apply also for sublevels.
  2028. When nil, only the tags directly given in a specific line apply there.
  2029. This may also be a list of tags that should be inherited, or a regexp that
  2030. matches tags that should be inherited. Additional control is possible
  2031. with the variable `org-tags-exclude-from-inheritance' which gives an
  2032. explicit list of tags to be excluded from inheritance., even if the value of
  2033. `org-use-tag-inheritance' would select it for inheritance.
  2034. If this option is t, a match early-on in a tree can lead to a large
  2035. number of matches in the subtree when constructing the agenda or creating
  2036. a sparse tree. If you only want to see the first match in a tree during
  2037. a search, check out the variable `org-tags-match-list-sublevels'."
  2038. :group 'org-tags
  2039. :type '(choice
  2040. (const :tag "Not" nil)
  2041. (const :tag "Always" t)
  2042. (repeat :tag "Specific tags" (string :tag "Tag"))
  2043. (regexp :tag "Tags matched by regexp")))
  2044. (defcustom org-tags-exclude-from-inheritance nil
  2045. "List of tags that should never be inherited.
  2046. This is a way to exclude a few tags from inheritance. For way to do
  2047. the opposite, to actively allow inheritance for selected tags,
  2048. see the variable `org-use-tag-inheritance'."
  2049. :group 'org-tags
  2050. :type '(repeat (string :tag "Tag")))
  2051. (defun org-tag-inherit-p (tag)
  2052. "Check if TAG is one that should be inherited."
  2053. (cond
  2054. ((member tag org-tags-exclude-from-inheritance) nil)
  2055. ((eq org-use-tag-inheritance t) t)
  2056. ((not org-use-tag-inheritance) nil)
  2057. ((stringp org-use-tag-inheritance)
  2058. (string-match org-use-tag-inheritance tag))
  2059. ((listp org-use-tag-inheritance)
  2060. (member tag org-use-tag-inheritance))
  2061. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2062. (defcustom org-tags-match-list-sublevels t
  2063. "Non-nil means list also sublevels of headlines matching a search.
  2064. This variable applies to tags/property searches, and also to stuck
  2065. projects because this search is based on a tags match as well.
  2066. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2067. the sublevels of a headline matching a tag search often also match
  2068. the same search. Listing all of them can create very long lists.
  2069. Setting this variable to nil causes subtrees of a match to be skipped.
  2070. As a special case, if the tag search is restricted to TODO items, the
  2071. value of this variable is ignored and sublevels are always checked, to
  2072. make sure all corresponding TODO items find their way into the list.
  2073. This variable is semi-obsolete and probably should always be true. It
  2074. is better to limit inheritance to certain tags using the variables
  2075. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2076. :group 'org-tags
  2077. :type '(choice
  2078. (const :tag "No, don't list them" nil)
  2079. (const :tag "Yes, do list them" t)
  2080. (const :tag "List them, indented with leading dots" indented)))
  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-highlight-latex-fragments-and-specials nil
  2343. "Non-nil means, fontify what is treated specially by the exporters."
  2344. :group 'org-font-lock
  2345. :type 'boolean)
  2346. (defcustom org-hide-emphasis-markers nil
  2347. "Non-nil mean font-lock should hide the emphasis marker characters."
  2348. :group 'org-font-lock
  2349. :type 'boolean)
  2350. (defvar org-emph-re nil
  2351. "Regular expression for matching emphasis.")
  2352. (defvar org-verbatim-re nil
  2353. "Regular expression for matching verbatim text.")
  2354. (defvar org-emphasis-regexp-components) ; defined just below
  2355. (defvar org-emphasis-alist) ; defined just below
  2356. (defun org-set-emph-re (var val)
  2357. "Set variable and compute the emphasis regular expression."
  2358. (set var val)
  2359. (when (and (boundp 'org-emphasis-alist)
  2360. (boundp 'org-emphasis-regexp-components)
  2361. org-emphasis-alist org-emphasis-regexp-components)
  2362. (let* ((e org-emphasis-regexp-components)
  2363. (pre (car e))
  2364. (post (nth 1 e))
  2365. (border (nth 2 e))
  2366. (body (nth 3 e))
  2367. (nl (nth 4 e))
  2368. (body1 (concat body "*?"))
  2369. (markers (mapconcat 'car org-emphasis-alist ""))
  2370. (vmarkers (mapconcat
  2371. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2372. org-emphasis-alist "")))
  2373. ;; make sure special characters appear at the right position in the class
  2374. (if (string-match "\\^" markers)
  2375. (setq markers (concat (replace-match "" t t markers) "^")))
  2376. (if (string-match "-" markers)
  2377. (setq markers (concat (replace-match "" t t markers) "-")))
  2378. (if (string-match "\\^" vmarkers)
  2379. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2380. (if (string-match "-" vmarkers)
  2381. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2382. (if (> nl 0)
  2383. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2384. (int-to-string nl) "\\}")))
  2385. ;; Make the regexp
  2386. (setq org-emph-re
  2387. (concat "\\([" pre "]\\|^\\)"
  2388. "\\("
  2389. "\\([" markers "]\\)"
  2390. "\\("
  2391. "[^" border "]\\|"
  2392. "[^" border "]"
  2393. body1
  2394. "[^" border "]"
  2395. "\\)"
  2396. "\\3\\)"
  2397. "\\([" post "]\\|$\\)"))
  2398. (setq org-verbatim-re
  2399. (concat "\\([" pre "]\\|^\\)"
  2400. "\\("
  2401. "\\([" vmarkers "]\\)"
  2402. "\\("
  2403. "[^" border "]\\|"
  2404. "[^" border "]"
  2405. body1
  2406. "[^" border "]"
  2407. "\\)"
  2408. "\\3\\)"
  2409. "\\([" post "]\\|$\\)")))))
  2410. (defcustom org-emphasis-regexp-components
  2411. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2412. "Components used to build the regular expression for emphasis.
  2413. This is a list with 6 entries. Terminology: In an emphasis string
  2414. like \" *strong word* \", we call the initial space PREMATCH, the final
  2415. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2416. and \"trong wor\" is the body. The different components in this variable
  2417. specify what is allowed/forbidden in each part:
  2418. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2419. post Chars allowed as postmatch. End of line will be allowed too.
  2420. border The chars *forbidden* as border characters.
  2421. body-regexp A regexp like \".\" to match a body character. Don't use
  2422. non-shy groups here, and don't allow newline here.
  2423. newline The maximum number of newlines allowed in an emphasis exp.
  2424. Use customize to modify this, or restart Emacs after changing it."
  2425. :group 'org-font-lock
  2426. :set 'org-set-emph-re
  2427. :type '(list
  2428. (sexp :tag "Allowed chars in pre ")
  2429. (sexp :tag "Allowed chars in post ")
  2430. (sexp :tag "Forbidden chars in border ")
  2431. (sexp :tag "Regexp for body ")
  2432. (integer :tag "number of newlines allowed")
  2433. (option (boolean :tag "Please ignore this button"))))
  2434. (defcustom org-emphasis-alist
  2435. `(("*" bold "<b>" "</b>")
  2436. ("/" italic "<i>" "</i>")
  2437. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2438. ("=" org-code "<code>" "</code>" verbatim)
  2439. ("~" org-verbatim "<code>" "</code>" verbatim)
  2440. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2441. "<del>" "</del>")
  2442. )
  2443. "Special syntax for emphasized text.
  2444. Text starting and ending with a special character will be emphasized, for
  2445. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2446. characters, the face to be used by font-lock for highlighting in Org-mode
  2447. Emacs buffers, and the HTML tags to be used for this.
  2448. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2449. Use customize to modify this, or restart Emacs after changing it."
  2450. :group 'org-font-lock
  2451. :set 'org-set-emph-re
  2452. :type '(repeat
  2453. (list
  2454. (string :tag "Marker character")
  2455. (choice
  2456. (face :tag "Font-lock-face")
  2457. (plist :tag "Face property list"))
  2458. (string :tag "HTML start tag")
  2459. (string :tag "HTML end tag")
  2460. (option (const verbatim)))))
  2461. ;;; Miscellaneous options
  2462. (defgroup org-completion nil
  2463. "Completion in Org-mode."
  2464. :tag "Org Completion"
  2465. :group 'org)
  2466. (defcustom org-completion-use-ido nil
  2467. "Non-nil means, use ido completion wherever possible.
  2468. Note that `ido-mode' must be active for this variable to be relevant.
  2469. If you decide to turn this variable on, you might well want to turn off
  2470. `org-outline-path-complete-in-steps'."
  2471. :group 'org-completion
  2472. :type 'boolean)
  2473. (defcustom org-completion-fallback-command 'hippie-expand
  2474. "The expansion command called by \\[org-complete] in normal context.
  2475. Normal means, no org-mode-specific context."
  2476. :group 'org-completion
  2477. :type 'function)
  2478. ;;; Functions and variables from ther packages
  2479. ;; Declared here to avoid compiler warnings
  2480. ;; XEmacs only
  2481. (defvar outline-mode-menu-heading)
  2482. (defvar outline-mode-menu-show)
  2483. (defvar outline-mode-menu-hide)
  2484. (defvar zmacs-regions) ; XEmacs regions
  2485. ;; Emacs only
  2486. (defvar mark-active)
  2487. ;; Various packages
  2488. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2489. (declare-function calendar-forward-day "cal-move" (arg))
  2490. (declare-function calendar-goto-date "cal-move" (date))
  2491. (declare-function calendar-goto-today "cal-move" ())
  2492. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2493. (defvar calc-embedded-close-formula)
  2494. (defvar calc-embedded-open-formula)
  2495. (declare-function cdlatex-tab "ext:cdlatex" ())
  2496. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2497. (defvar font-lock-unfontify-region-function)
  2498. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2499. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2500. (defvar iswitchb-temp-buflist)
  2501. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2502. (defvar org-agenda-tags-todo-honor-ignore-options)
  2503. (declare-function org-agenda-skip "org-agenda" ())
  2504. (declare-function org-format-agenda-item "org-agenda"
  2505. (extra txt &optional category tags dotime noprefix remove-re))
  2506. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2507. (declare-function org-agenda-change-all-lines "org-agenda"
  2508. (newhead hdmarker &optional fixface just-this))
  2509. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2510. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2511. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2512. (beg end))
  2513. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2514. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2515. "org-agenda" (&optional end))
  2516. (declare-function parse-time-string "parse-time" (string))
  2517. (declare-function remember "remember" (&optional initial))
  2518. (declare-function remember-buffer-desc "remember" ())
  2519. (declare-function remember-finalize "remember" ())
  2520. (defvar remember-save-after-remembering)
  2521. (defvar remember-data-file)
  2522. (defvar remember-register)
  2523. (defvar remember-buffer)
  2524. (defvar remember-handler-functions)
  2525. (defvar remember-annotation-functions)
  2526. (defvar texmathp-why)
  2527. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2528. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2529. (defvar w3m-current-url)
  2530. (defvar w3m-current-title)
  2531. (defvar org-latex-regexps)
  2532. ;;; Autoload and prepare some org modules
  2533. ;; Some table stuff that needs to be defined here, because it is used
  2534. ;; by the functions setting up org-mode or checking for table context.
  2535. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2536. "Detects an org-type or table-type table.")
  2537. (defconst org-table-line-regexp "^[ \t]*|"
  2538. "Detects an org-type table line.")
  2539. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2540. "Detects an org-type table line.")
  2541. (defconst org-table-hline-regexp "^[ \t]*|-"
  2542. "Detects an org-type table hline.")
  2543. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2544. "Detects a table-type table hline.")
  2545. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2546. "Searching from within a table (any type) this finds the first line
  2547. outside the table.")
  2548. ;; Autoload the functions in org-table.el that are needed by functions here.
  2549. (eval-and-compile
  2550. (org-autoload "org-table"
  2551. '(org-table-align org-table-begin org-table-blank-field
  2552. org-table-convert org-table-convert-region org-table-copy-down
  2553. org-table-copy-region org-table-create
  2554. org-table-create-or-convert-from-region
  2555. org-table-create-with-table.el org-table-current-dline
  2556. org-table-cut-region org-table-delete-column org-table-edit-field
  2557. org-table-edit-formulas org-table-end org-table-eval-formula
  2558. org-table-export org-table-field-info
  2559. org-table-get-stored-formulas org-table-goto-column
  2560. org-table-hline-and-move org-table-import org-table-insert-column
  2561. org-table-insert-hline org-table-insert-row org-table-iterate
  2562. org-table-justify-field-maybe org-table-kill-row
  2563. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2564. org-table-move-column org-table-move-column-left
  2565. org-table-move-column-right org-table-move-row
  2566. org-table-move-row-down org-table-move-row-up
  2567. org-table-next-field org-table-next-row org-table-paste-rectangle
  2568. org-table-previous-field org-table-recalculate
  2569. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2570. org-table-toggle-coordinate-overlays
  2571. org-table-toggle-formula-debugger org-table-wrap-region
  2572. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2573. (defun org-at-table-p (&optional table-type)
  2574. "Return t if the cursor is inside an org-type table.
  2575. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2576. (if org-enable-table-editor
  2577. (save-excursion
  2578. (beginning-of-line 1)
  2579. (looking-at (if table-type org-table-any-line-regexp
  2580. org-table-line-regexp)))
  2581. nil))
  2582. (defsubst org-table-p () (org-at-table-p))
  2583. (defun org-at-table.el-p ()
  2584. "Return t if and only if we are at a table.el table."
  2585. (and (org-at-table-p 'any)
  2586. (save-excursion
  2587. (goto-char (org-table-begin 'any))
  2588. (looking-at org-table1-hline-regexp))))
  2589. (defun org-table-recognize-table.el ()
  2590. "If there is a table.el table nearby, recognize it and move into it."
  2591. (if org-table-tab-recognizes-table.el
  2592. (if (org-at-table.el-p)
  2593. (progn
  2594. (beginning-of-line 1)
  2595. (if (looking-at org-table-dataline-regexp)
  2596. nil
  2597. (if (looking-at org-table1-hline-regexp)
  2598. (progn
  2599. (beginning-of-line 2)
  2600. (if (looking-at org-table-any-border-regexp)
  2601. (beginning-of-line -1)))))
  2602. (if (re-search-forward "|" (org-table-end t) t)
  2603. (progn
  2604. (require 'table)
  2605. (if (table--at-cell-p (point))
  2606. t
  2607. (message "recognizing table.el table...")
  2608. (table-recognize-table)
  2609. (message "recognizing table.el table...done")))
  2610. (error "This should not happen..."))
  2611. t)
  2612. nil)
  2613. nil))
  2614. (defun org-at-table-hline-p ()
  2615. "Return t if the cursor is inside a hline in a table."
  2616. (if org-enable-table-editor
  2617. (save-excursion
  2618. (beginning-of-line 1)
  2619. (looking-at org-table-hline-regexp))
  2620. nil))
  2621. (defvar org-table-clean-did-remove-column nil)
  2622. (defun org-table-map-tables (function)
  2623. "Apply FUNCTION to the start of all tables in the buffer."
  2624. (save-excursion
  2625. (save-restriction
  2626. (widen)
  2627. (goto-char (point-min))
  2628. (while (re-search-forward org-table-any-line-regexp nil t)
  2629. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2630. (beginning-of-line 1)
  2631. (if (looking-at org-table-line-regexp)
  2632. (save-excursion (funcall function)))
  2633. (re-search-forward org-table-any-border-regexp nil 1))))
  2634. (message "Mapping tables: done"))
  2635. ;; Declare and autoload functions from org-exp.el & Co
  2636. (declare-function org-default-export-plist "org-exp")
  2637. (declare-function org-infile-export-plist "org-exp")
  2638. (declare-function org-get-current-options "org-exp")
  2639. (eval-and-compile
  2640. (org-autoload "org-exp"
  2641. '(org-export org-export-visible
  2642. org-insert-export-options-template
  2643. org-table-clean-before-export))
  2644. (org-autoload "org-ascii"
  2645. '(org-export-as-ascii org-export-ascii-preprocess
  2646. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2647. org-export-region-as-ascii))
  2648. (org-autoload "org-html"
  2649. '(org-export-as-html-and-open
  2650. org-export-as-html-batch org-export-as-html-to-buffer
  2651. org-replace-region-by-html org-export-region-as-html
  2652. org-export-as-html))
  2653. (org-autoload "org-icalendar"
  2654. '(org-export-icalendar-this-file
  2655. org-export-icalendar-all-agenda-files
  2656. org-export-icalendar-combine-agenda-files))
  2657. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2658. ;; Declare and autoload functions from org-agenda.el
  2659. (eval-and-compile
  2660. (org-autoload "org-agenda"
  2661. '(org-agenda org-agenda-list org-search-view
  2662. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2663. org-diary org-agenda-to-appt
  2664. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2665. ;; Autoload org-remember
  2666. (eval-and-compile
  2667. (org-autoload "org-remember"
  2668. '(org-remember-insinuate org-remember-annotation
  2669. org-remember-apply-template org-remember org-remember-handler)))
  2670. ;; Autoload org-clock.el
  2671. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2672. (beg end))
  2673. (declare-function org-clock-update-mode-line "org-clock" ())
  2674. (defvar org-clock-start-time)
  2675. (defvar org-clock-marker (make-marker)
  2676. "Marker recording the last clock-in.")
  2677. (eval-and-compile
  2678. (org-autoload
  2679. "org-clock"
  2680. '(org-clock-in org-clock-out org-clock-cancel
  2681. org-clock-goto org-clock-sum org-clock-display
  2682. org-clock-remove-overlays org-clock-report
  2683. org-clocktable-shift org-dblock-write:clocktable
  2684. org-get-clocktable)))
  2685. (defun org-clock-update-time-maybe ()
  2686. "If this is a CLOCK line, update it and return t.
  2687. Otherwise, return nil."
  2688. (interactive)
  2689. (save-excursion
  2690. (beginning-of-line 1)
  2691. (skip-chars-forward " \t")
  2692. (when (looking-at org-clock-string)
  2693. (let ((re (concat "[ \t]*" org-clock-string
  2694. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2695. "\\([ \t]*=>.*\\)?\\)?"))
  2696. ts te h m s neg)
  2697. (cond
  2698. ((not (looking-at re))
  2699. nil)
  2700. ((not (match-end 2))
  2701. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2702. (> org-clock-marker (point))
  2703. (<= org-clock-marker (point-at-eol)))
  2704. ;; The clock is running here
  2705. (setq org-clock-start-time
  2706. (apply 'encode-time
  2707. (org-parse-time-string (match-string 1))))
  2708. (org-clock-update-mode-line)))
  2709. (t
  2710. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2711. (end-of-line 1)
  2712. (setq ts (match-string 1)
  2713. te (match-string 3))
  2714. (setq s (- (time-to-seconds
  2715. (apply 'encode-time (org-parse-time-string te)))
  2716. (time-to-seconds
  2717. (apply 'encode-time (org-parse-time-string ts))))
  2718. neg (< s 0)
  2719. s (abs s)
  2720. h (floor (/ s 3600))
  2721. s (- s (* 3600 h))
  2722. m (floor (/ s 60))
  2723. s (- s (* 60 s)))
  2724. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2725. t))))))
  2726. (defun org-check-running-clock ()
  2727. "Check if the current buffer contains the running clock.
  2728. If yes, offer to stop it and to save the buffer with the changes."
  2729. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2730. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2731. (buffer-name))))
  2732. (org-clock-out)
  2733. (when (y-or-n-p "Save changed buffer?")
  2734. (save-buffer))))
  2735. (defun org-clocktable-try-shift (dir n)
  2736. "Check if this line starts a clock table, if yes, shift the time block."
  2737. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2738. (org-clocktable-shift dir n)))
  2739. ;; Autoload org-timer.el
  2740. (eval-and-compile
  2741. (org-autoload
  2742. "org-timer"
  2743. '(org-timer-start org-timer org-timer-item
  2744. org-timer-change-times-in-region)))
  2745. ;; Autoload org-feed.el
  2746. (eval-and-compile
  2747. (org-autoload
  2748. "org-feed"
  2749. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2750. ;; Autoload archiving code
  2751. ;; The stuff that is needed for cycling and tags has to be defined here.
  2752. (defgroup org-archive nil
  2753. "Options concerning archiving in Org-mode."
  2754. :tag "Org Archive"
  2755. :group 'org-structure)
  2756. (defcustom org-archive-location "%s_archive::"
  2757. "The location where subtrees should be archived.
  2758. The value of this variable is a string, consisting of two parts,
  2759. separated by a double-colon. The first part is a filename and
  2760. the second part is a headline.
  2761. When the filename is omitted, archiving happens in the same file.
  2762. %s in the filename will be replaced by the current file
  2763. name (without the directory part). Archiving to a different file
  2764. is useful to keep archived entries from contributing to the
  2765. Org-mode Agenda.
  2766. The archived entries will be filed as subtrees of the specified
  2767. headline. When the headline is omitted, the subtrees are simply
  2768. filed away at the end of the file, as top-level entries. Also in
  2769. the heading you can use %s to represent the file name, this can be
  2770. useful when using the same archive for a number of different files.
  2771. Here are a few examples:
  2772. \"%s_archive::\"
  2773. If the current file is Projects.org, archive in file
  2774. Projects.org_archive, as top-level trees. This is the default.
  2775. \"::* Archived Tasks\"
  2776. Archive in the current file, under the top-level headline
  2777. \"* Archived Tasks\".
  2778. \"~/org/archive.org::\"
  2779. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2780. \"~/org/archive.org::From %s\"
  2781. Archive in file ~/org/archive.org (absolute path), und headlines
  2782. \"From FILENAME\" where file name is the current file name.
  2783. \"basement::** Finished Tasks\"
  2784. Archive in file ./basement (relative path), as level 3 trees
  2785. below the level 2 heading \"** Finished Tasks\".
  2786. You may set this option on a per-file basis by adding to the buffer a
  2787. line like
  2788. #+ARCHIVE: basement::** Finished Tasks
  2789. You may also define it locally for a subtree by setting an ARCHIVE property
  2790. in the entry. If such a property is found in an entry, or anywhere up
  2791. the hierarchy, it will be used."
  2792. :group 'org-archive
  2793. :type 'string)
  2794. (defcustom org-archive-tag "ARCHIVE"
  2795. "The tag that marks a subtree as archived.
  2796. An archived subtree does not open during visibility cycling, and does
  2797. not contribute to the agenda listings.
  2798. After changing this, font-lock must be restarted in the relevant buffers to
  2799. get the proper fontification."
  2800. :group 'org-archive
  2801. :group 'org-keywords
  2802. :type 'string)
  2803. (defcustom org-agenda-skip-archived-trees t
  2804. "Non-nil means, the agenda will skip any items located in archived trees.
  2805. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2806. variable is no longer recommended, you should leave it at the value t.
  2807. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2808. :group 'org-archive
  2809. :group 'org-agenda-skip
  2810. :type 'boolean)
  2811. (defcustom org-columns-skip-arrchived-trees t
  2812. "Non-nil means, irgnore archived trees when creating column view."
  2813. :group 'org-archive
  2814. :group 'org-properties
  2815. :type 'boolean)
  2816. (defcustom org-cycle-open-archived-trees nil
  2817. "Non-nil means, `org-cycle' will open archived trees.
  2818. An archived tree is a tree marked with the tag ARCHIVE.
  2819. When nil, archived trees will stay folded. You can still open them with
  2820. normal outline commands like `show-all', but not with the cycling commands."
  2821. :group 'org-archive
  2822. :group 'org-cycle
  2823. :type 'boolean)
  2824. (defcustom org-sparse-tree-open-archived-trees nil
  2825. "Non-nil means sparse tree construction shows matches in archived trees.
  2826. When nil, matches in these trees are highlighted, but the trees are kept in
  2827. collapsed state."
  2828. :group 'org-archive
  2829. :group 'org-sparse-trees
  2830. :type 'boolean)
  2831. (defun org-cycle-hide-archived-subtrees (state)
  2832. "Re-hide all archived subtrees after a visibility state change."
  2833. (when (and (not org-cycle-open-archived-trees)
  2834. (not (memq state '(overview folded))))
  2835. (save-excursion
  2836. (let* ((globalp (memq state '(contents all)))
  2837. (beg (if globalp (point-min) (point)))
  2838. (end (if globalp (point-max) (org-end-of-subtree t))))
  2839. (org-hide-archived-subtrees beg end)
  2840. (goto-char beg)
  2841. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2842. (message "%s" (substitute-command-keys
  2843. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2844. (defun org-force-cycle-archived ()
  2845. "Cycle subtree even if it is archived."
  2846. (interactive)
  2847. (setq this-command 'org-cycle)
  2848. (let ((org-cycle-open-archived-trees t))
  2849. (call-interactively 'org-cycle)))
  2850. (defun org-hide-archived-subtrees (beg end)
  2851. "Re-hide all archived subtrees after a visibility state change."
  2852. (save-excursion
  2853. (let* ((re (concat ":" org-archive-tag ":")))
  2854. (goto-char beg)
  2855. (while (re-search-forward re end t)
  2856. (and (org-on-heading-p) (hide-subtree))
  2857. (org-end-of-subtree t)))))
  2858. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2859. (eval-and-compile
  2860. (org-autoload "org-archive"
  2861. '(org-add-archive-files org-archive-subtree
  2862. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2863. ;; Autoload Column View Code
  2864. (declare-function org-columns-number-to-string "org-colview")
  2865. (declare-function org-columns-get-format-and-top-level "org-colview")
  2866. (declare-function org-columns-compute "org-colview")
  2867. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2868. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2869. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2870. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2871. ;; Autoload ID code
  2872. (declare-function org-id-store-link "org-id")
  2873. (declare-function org-id-locations-load "org-id")
  2874. (declare-function org-id-locations-save "org-id")
  2875. (defvar org-id-track-globally)
  2876. (org-autoload "org-id"
  2877. '(org-id-get-create org-id-new org-id-copy org-id-get
  2878. org-id-get-with-outline-path-completion
  2879. org-id-get-with-outline-drilling
  2880. org-id-goto org-id-find org-id-store-link))
  2881. ;; Autoload Plotting Code
  2882. (org-autoload "org-plot"
  2883. '(org-plot/gnuplot))
  2884. ;;; Variables for pre-computed regular expressions, all buffer local
  2885. (defvar org-drawer-regexp nil
  2886. "Matches first line of a hidden block.")
  2887. (make-variable-buffer-local 'org-drawer-regexp)
  2888. (defvar org-todo-regexp nil
  2889. "Matches any of the TODO state keywords.")
  2890. (make-variable-buffer-local 'org-todo-regexp)
  2891. (defvar org-not-done-regexp nil
  2892. "Matches any of the TODO state keywords except the last one.")
  2893. (make-variable-buffer-local 'org-not-done-regexp)
  2894. (defvar org-not-done-heading-regexp nil
  2895. "Matches a TODO headline that is not done.")
  2896. (make-variable-buffer-local 'org-not-done-regexp)
  2897. (defvar org-todo-line-regexp nil
  2898. "Matches a headline and puts TODO state into group 2 if present.")
  2899. (make-variable-buffer-local 'org-todo-line-regexp)
  2900. (defvar org-complex-heading-regexp nil
  2901. "Matches a headline and puts everything into groups:
  2902. group 1: the stars
  2903. group 2: The todo keyword, maybe
  2904. group 3: Priority cookie
  2905. group 4: True headline
  2906. group 5: Tags")
  2907. (make-variable-buffer-local 'org-complex-heading-regexp)
  2908. (defvar org-todo-line-tags-regexp nil
  2909. "Matches a headline and puts TODO state into group 2 if present.
  2910. Also put tags into group 4 if tags are present.")
  2911. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2912. (defvar org-nl-done-regexp nil
  2913. "Matches newline followed by a headline with the DONE keyword.")
  2914. (make-variable-buffer-local 'org-nl-done-regexp)
  2915. (defvar org-looking-at-done-regexp nil
  2916. "Matches the DONE keyword a point.")
  2917. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2918. (defvar org-ds-keyword-length 12
  2919. "Maximum length of the Deadline and SCHEDULED keywords.")
  2920. (make-variable-buffer-local 'org-ds-keyword-length)
  2921. (defvar org-deadline-regexp nil
  2922. "Matches the DEADLINE keyword.")
  2923. (make-variable-buffer-local 'org-deadline-regexp)
  2924. (defvar org-deadline-time-regexp nil
  2925. "Matches the DEADLINE keyword together with a time stamp.")
  2926. (make-variable-buffer-local 'org-deadline-time-regexp)
  2927. (defvar org-deadline-line-regexp nil
  2928. "Matches the DEADLINE keyword and the rest of the line.")
  2929. (make-variable-buffer-local 'org-deadline-line-regexp)
  2930. (defvar org-scheduled-regexp nil
  2931. "Matches the SCHEDULED keyword.")
  2932. (make-variable-buffer-local 'org-scheduled-regexp)
  2933. (defvar org-scheduled-time-regexp nil
  2934. "Matches the SCHEDULED keyword together with a time stamp.")
  2935. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2936. (defvar org-closed-time-regexp nil
  2937. "Matches the CLOSED keyword together with a time stamp.")
  2938. (make-variable-buffer-local 'org-closed-time-regexp)
  2939. (defvar org-keyword-time-regexp nil
  2940. "Matches any of the 4 keywords, together with the time stamp.")
  2941. (make-variable-buffer-local 'org-keyword-time-regexp)
  2942. (defvar org-keyword-time-not-clock-regexp nil
  2943. "Matches any of the 3 keywords, together with the time stamp.")
  2944. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2945. (defvar org-maybe-keyword-time-regexp nil
  2946. "Matches a timestamp, possibly preceeded by a keyword.")
  2947. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2948. (defvar org-planning-or-clock-line-re nil
  2949. "Matches a line with planning or clock info.")
  2950. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2951. (defconst org-plain-time-of-day-regexp
  2952. (concat
  2953. "\\(\\<[012]?[0-9]"
  2954. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2955. "\\(--?"
  2956. "\\(\\<[012]?[0-9]"
  2957. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2958. "\\)?")
  2959. "Regular expression to match a plain time or time range.
  2960. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2961. groups carry important information:
  2962. 0 the full match
  2963. 1 the first time, range or not
  2964. 8 the second time, if it is a range.")
  2965. (defconst org-plain-time-extension-regexp
  2966. (concat
  2967. "\\(\\<[012]?[0-9]"
  2968. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2969. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2970. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2971. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2972. groups carry important information:
  2973. 0 the full match
  2974. 7 hours of duration
  2975. 9 minutes of duration")
  2976. (defconst org-stamp-time-of-day-regexp
  2977. (concat
  2978. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2979. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2980. "\\(--?"
  2981. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2982. "Regular expression to match a timestamp time or time range.
  2983. After a match, the following groups carry important information:
  2984. 0 the full match
  2985. 1 date plus weekday, for backreferencing to make sure both times on same day
  2986. 2 the first time, range or not
  2987. 4 the second time, if it is a range.")
  2988. (defconst org-startup-options
  2989. '(("fold" org-startup-folded t)
  2990. ("overview" org-startup-folded t)
  2991. ("nofold" org-startup-folded nil)
  2992. ("showall" org-startup-folded nil)
  2993. ("content" org-startup-folded content)
  2994. ("hidestars" org-hide-leading-stars t)
  2995. ("showstars" org-hide-leading-stars nil)
  2996. ("odd" org-odd-levels-only t)
  2997. ("oddeven" org-odd-levels-only nil)
  2998. ("align" org-startup-align-all-tables t)
  2999. ("noalign" org-startup-align-all-tables nil)
  3000. ("customtime" org-display-custom-times t)
  3001. ("logdone" org-log-done time)
  3002. ("lognotedone" org-log-done note)
  3003. ("nologdone" org-log-done nil)
  3004. ("lognoteclock-out" org-log-note-clock-out t)
  3005. ("nolognoteclock-out" org-log-note-clock-out nil)
  3006. ("logrepeat" org-log-repeat state)
  3007. ("lognoterepeat" org-log-repeat note)
  3008. ("nologrepeat" org-log-repeat nil)
  3009. ("fninline" org-footnote-define-inline t)
  3010. ("nofninline" org-footnote-define-inline nil)
  3011. ("fnlocal" org-footnote-section nil)
  3012. ("fnauto" org-footnote-auto-label t)
  3013. ("fnprompt" org-footnote-auto-label nil)
  3014. ("fnconfirm" org-footnote-auto-label confirm)
  3015. ("fnplain" org-footnote-auto-label plain)
  3016. ("constcgs" constants-unit-system cgs)
  3017. ("constSI" constants-unit-system SI)
  3018. ("noptag" org-tag-persistent-alist nil)
  3019. ("hideblocks" org-hide-block-startup t)
  3020. ("nohideblocks" org-hide-block-startup nil))
  3021. "Variable associated with STARTUP options for org-mode.
  3022. Each element is a list of three items: The startup options as written
  3023. in the #+STARTUP line, the corresponding variable, and the value to
  3024. set this variable to if the option is found. An optional forth element PUSH
  3025. means to push this value onto the list in the variable.")
  3026. (defun org-set-regexps-and-options ()
  3027. "Precompute regular expressions for current buffer."
  3028. (when (org-mode-p)
  3029. (org-set-local 'org-todo-kwd-alist nil)
  3030. (org-set-local 'org-todo-key-alist nil)
  3031. (org-set-local 'org-todo-key-trigger nil)
  3032. (org-set-local 'org-todo-keywords-1 nil)
  3033. (org-set-local 'org-done-keywords nil)
  3034. (org-set-local 'org-todo-heads nil)
  3035. (org-set-local 'org-todo-sets nil)
  3036. (org-set-local 'org-todo-log-states nil)
  3037. (org-set-local 'org-file-properties nil)
  3038. (org-set-local 'org-file-tags nil)
  3039. (let ((re (org-make-options-regexp
  3040. '("CATEGORY" "TODO" "COLUMNS"
  3041. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3042. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3043. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3044. (splitre "[ \t]+")
  3045. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3046. tail sep kws1 prio props ftags drawers
  3047. ext-setup-or-nil setup-contents (start 0))
  3048. (save-excursion
  3049. (save-restriction
  3050. (widen)
  3051. (goto-char (point-min))
  3052. (while (or (and ext-setup-or-nil
  3053. (string-match re ext-setup-or-nil start)
  3054. (setq start (match-end 0)))
  3055. (and (setq ext-setup-or-nil nil start 0)
  3056. (re-search-forward re nil t)))
  3057. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3058. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3059. (cond
  3060. ((equal key "CATEGORY")
  3061. (if (string-match "[ \t]+$" value)
  3062. (setq value (replace-match "" t t value)))
  3063. (setq cat value))
  3064. ((member key '("SEQ_TODO" "TODO"))
  3065. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3066. ((equal key "TYP_TODO")
  3067. (push (cons 'type (org-split-string value splitre)) kwds))
  3068. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3069. ;; general TODO-like setup
  3070. (push (cons (intern (downcase (match-string 1 key)))
  3071. (org-split-string value splitre)) kwds))
  3072. ((equal key "TAGS")
  3073. (setq tags (append tags (if tags '("\\n") nil)
  3074. (org-split-string value splitre))))
  3075. ((equal key "COLUMNS")
  3076. (org-set-local 'org-columns-default-format value))
  3077. ((equal key "LINK")
  3078. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3079. (push (cons (match-string 1 value)
  3080. (org-trim (match-string 2 value)))
  3081. links)))
  3082. ((equal key "PRIORITIES")
  3083. (setq prio (org-split-string value " +")))
  3084. ((equal key "PROPERTY")
  3085. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3086. (push (cons (match-string 1 value) (match-string 2 value))
  3087. props)))
  3088. ((equal key "FILETAGS")
  3089. (when (string-match "\\S-" value)
  3090. (setq ftags
  3091. (append
  3092. ftags
  3093. (apply 'append
  3094. (mapcar (lambda (x) (org-split-string x ":"))
  3095. (org-split-string value)))))))
  3096. ((equal key "DRAWERS")
  3097. (setq drawers (org-split-string value splitre)))
  3098. ((equal key "CONSTANTS")
  3099. (setq const (append const (org-split-string value splitre))))
  3100. ((equal key "STARTUP")
  3101. (let ((opts (org-split-string value splitre))
  3102. l var val)
  3103. (while (setq l (pop opts))
  3104. (when (setq l (assoc l org-startup-options))
  3105. (setq var (nth 1 l) val (nth 2 l))
  3106. (if (not (nth 3 l))
  3107. (set (make-local-variable var) val)
  3108. (if (not (listp (symbol-value var)))
  3109. (set (make-local-variable var) nil))
  3110. (set (make-local-variable var) (symbol-value var))
  3111. (add-to-list var val))))))
  3112. ((equal key "ARCHIVE")
  3113. (string-match " *$" value)
  3114. (setq arch (replace-match "" t t value))
  3115. (remove-text-properties 0 (length arch)
  3116. '(face t fontified t) arch))
  3117. ((equal key "SETUPFILE")
  3118. (setq setup-contents (org-file-contents
  3119. (expand-file-name
  3120. (org-remove-double-quotes value))
  3121. 'noerror))
  3122. (if (not ext-setup-or-nil)
  3123. (setq ext-setup-or-nil setup-contents start 0)
  3124. (setq ext-setup-or-nil
  3125. (concat (substring ext-setup-or-nil 0 start)
  3126. "\n" setup-contents "\n"
  3127. (substring ext-setup-or-nil start)))))
  3128. ))))
  3129. (when cat
  3130. (org-set-local 'org-category (intern cat))
  3131. (push (cons "CATEGORY" cat) props))
  3132. (when prio
  3133. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3134. (setq prio (mapcar 'string-to-char prio))
  3135. (org-set-local 'org-highest-priority (nth 0 prio))
  3136. (org-set-local 'org-lowest-priority (nth 1 prio))
  3137. (org-set-local 'org-default-priority (nth 2 prio)))
  3138. (and props (org-set-local 'org-file-properties (nreverse props)))
  3139. (and ftags (org-set-local 'org-file-tags
  3140. (mapcar 'org-add-prop-inherited ftags)))
  3141. (and drawers (org-set-local 'org-drawers drawers))
  3142. (and arch (org-set-local 'org-archive-location arch))
  3143. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3144. ;; Process the TODO keywords
  3145. (unless kwds
  3146. ;; Use the global values as if they had been given locally.
  3147. (setq kwds (default-value 'org-todo-keywords))
  3148. (if (stringp (car kwds))
  3149. (setq kwds (list (cons org-todo-interpretation
  3150. (default-value 'org-todo-keywords)))))
  3151. (setq kwds (reverse kwds)))
  3152. (setq kwds (nreverse kwds))
  3153. (let (inter kws kw)
  3154. (while (setq kws (pop kwds))
  3155. (let ((kws (or
  3156. (run-hook-with-args-until-success
  3157. 'org-todo-setup-filter-hook kws)
  3158. kws)))
  3159. (setq inter (pop kws) sep (member "|" kws)
  3160. kws0 (delete "|" (copy-sequence kws))
  3161. kwsa nil
  3162. kws1 (mapcar
  3163. (lambda (x)
  3164. ;; 1 2
  3165. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3166. (progn
  3167. (setq kw (match-string 1 x)
  3168. key (and (match-end 2) (match-string 2 x))
  3169. log (org-extract-log-state-settings x))
  3170. (push (cons kw (and key (string-to-char key))) kwsa)
  3171. (and log (push log org-todo-log-states))
  3172. kw)
  3173. (error "Invalid TODO keyword %s" x)))
  3174. kws0)
  3175. kwsa (if kwsa (append '((:startgroup))
  3176. (nreverse kwsa)
  3177. '((:endgroup))))
  3178. hw (car kws1)
  3179. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3180. tail (list inter hw (car dws) (org-last dws))))
  3181. (add-to-list 'org-todo-heads hw 'append)
  3182. (push kws1 org-todo-sets)
  3183. (setq org-done-keywords (append org-done-keywords dws nil))
  3184. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3185. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3186. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3187. (setq org-todo-sets (nreverse org-todo-sets)
  3188. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3189. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3190. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3191. ;; Process the constants
  3192. (when const
  3193. (let (e cst)
  3194. (while (setq e (pop const))
  3195. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3196. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3197. (setq org-table-formula-constants-local cst)))
  3198. ;; Process the tags.
  3199. (when tags
  3200. (let (e tgs)
  3201. (while (setq e (pop tags))
  3202. (cond
  3203. ((equal e "{") (push '(:startgroup) tgs))
  3204. ((equal e "}") (push '(:endgroup) tgs))
  3205. ((equal e "\\n") (push '(:newline) tgs))
  3206. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3207. (push (cons (match-string 1 e)
  3208. (string-to-char (match-string 2 e)))
  3209. tgs))
  3210. (t (push (list e) tgs))))
  3211. (org-set-local 'org-tag-alist nil)
  3212. (while (setq e (pop tgs))
  3213. (or (and (stringp (car e))
  3214. (assoc (car e) org-tag-alist))
  3215. (push e org-tag-alist)))))
  3216. ;; Compute the regular expressions and other local variables
  3217. (if (not org-done-keywords)
  3218. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3219. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3220. (length org-scheduled-string)
  3221. (length org-clock-string)
  3222. (length org-closed-string)))
  3223. org-drawer-regexp
  3224. (concat "^[ \t]*:\\("
  3225. (mapconcat 'regexp-quote org-drawers "\\|")
  3226. "\\):[ \t]*$")
  3227. org-not-done-keywords
  3228. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3229. org-todo-regexp
  3230. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3231. "\\|") "\\)\\>")
  3232. org-not-done-regexp
  3233. (concat "\\<\\("
  3234. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3235. "\\)\\>")
  3236. org-not-done-heading-regexp
  3237. (concat "^\\(\\*+\\)[ \t]+\\("
  3238. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3239. "\\)\\>")
  3240. org-todo-line-regexp
  3241. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3242. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3243. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3244. org-complex-heading-regexp
  3245. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3246. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3247. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3248. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3249. org-nl-done-regexp
  3250. (concat "\n\\*+[ \t]+"
  3251. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3252. "\\)" "\\>")
  3253. org-todo-line-tags-regexp
  3254. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3255. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3256. (org-re
  3257. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3258. org-looking-at-done-regexp
  3259. (concat "^" "\\(?:"
  3260. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3261. "\\>")
  3262. org-deadline-regexp (concat "\\<" org-deadline-string)
  3263. org-deadline-time-regexp
  3264. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3265. org-deadline-line-regexp
  3266. (concat "\\<\\(" org-deadline-string "\\).*")
  3267. org-scheduled-regexp
  3268. (concat "\\<" org-scheduled-string)
  3269. org-scheduled-time-regexp
  3270. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3271. org-closed-time-regexp
  3272. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3273. org-keyword-time-regexp
  3274. (concat "\\<\\(" org-scheduled-string
  3275. "\\|" org-deadline-string
  3276. "\\|" org-closed-string
  3277. "\\|" org-clock-string "\\)"
  3278. " *[[<]\\([^]>]+\\)[]>]")
  3279. org-keyword-time-not-clock-regexp
  3280. (concat "\\<\\(" org-scheduled-string
  3281. "\\|" org-deadline-string
  3282. "\\|" org-closed-string
  3283. "\\)"
  3284. " *[[<]\\([^]>]+\\)[]>]")
  3285. org-maybe-keyword-time-regexp
  3286. (concat "\\(\\<\\(" org-scheduled-string
  3287. "\\|" org-deadline-string
  3288. "\\|" org-closed-string
  3289. "\\|" org-clock-string "\\)\\)?"
  3290. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3291. org-planning-or-clock-line-re
  3292. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3293. "\\|" org-deadline-string
  3294. "\\|" org-closed-string "\\|" org-clock-string
  3295. "\\)\\>\\)")
  3296. )
  3297. (org-compute-latex-and-specials-regexp)
  3298. (org-set-font-lock-defaults))))
  3299. (defun org-file-contents (file &optional noerror)
  3300. "Return the contents of FILE, as a string."
  3301. (if (or (not file)
  3302. (not (file-readable-p file)))
  3303. (if noerror
  3304. (progn
  3305. (message "Cannot read file %s" file)
  3306. (ding) (sit-for 2)
  3307. "")
  3308. (error "Cannot read file %s" file))
  3309. (with-temp-buffer
  3310. (insert-file-contents file)
  3311. (buffer-string))))
  3312. (defun org-extract-log-state-settings (x)
  3313. "Extract the log state setting from a TODO keyword string.
  3314. This will extract info from a string like \"WAIT(w@/!)\"."
  3315. (let (kw key log1 log2)
  3316. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3317. (setq kw (match-string 1 x)
  3318. key (and (match-end 2) (match-string 2 x))
  3319. log1 (and (match-end 3) (match-string 3 x))
  3320. log2 (and (match-end 4) (match-string 4 x)))
  3321. (and (or log1 log2)
  3322. (list kw
  3323. (and log1 (if (equal log1 "!") 'time 'note))
  3324. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3325. (defun org-remove-keyword-keys (list)
  3326. "Remove a pair of parenthesis at the end of each string in LIST."
  3327. (mapcar (lambda (x)
  3328. (if (string-match "(.*)$" x)
  3329. (substring x 0 (match-beginning 0))
  3330. x))
  3331. list))
  3332. ;; FIXME: this could be done much better, using second characters etc.
  3333. (defun org-assign-fast-keys (alist)
  3334. "Assign fast keys to a keyword-key alist.
  3335. Respect keys that are already there."
  3336. (let (new e k c c1 c2 (char ?a))
  3337. (while (setq e (pop alist))
  3338. (cond
  3339. ((equal e '(:startgroup)) (push e new))
  3340. ((equal e '(:endgroup)) (push e new))
  3341. ((equal e '(:newline)) (push e new))
  3342. (t
  3343. (setq k (car e) c2 nil)
  3344. (if (cdr e)
  3345. (setq c (cdr e))
  3346. ;; automatically assign a character.
  3347. (setq c1 (string-to-char
  3348. (downcase (substring
  3349. k (if (= (string-to-char k) ?@) 1 0)))))
  3350. (if (or (rassoc c1 new) (rassoc c1 alist))
  3351. (while (or (rassoc char new) (rassoc char alist))
  3352. (setq char (1+ char)))
  3353. (setq c2 c1))
  3354. (setq c (or c2 char)))
  3355. (push (cons k c) new))))
  3356. (nreverse new)))
  3357. ;;; Some variables used in various places
  3358. (defvar org-window-configuration nil
  3359. "Used in various places to store a window configuration.")
  3360. (defvar org-finish-function nil
  3361. "Function to be called when `C-c C-c' is used.
  3362. This is for getting out of special buffers like remember.")
  3363. ;; FIXME: Occasionally check by commenting these, to make sure
  3364. ;; no other functions uses these, forgetting to let-bind them.
  3365. (defvar entry)
  3366. (defvar state)
  3367. (defvar last-state)
  3368. (defvar date)
  3369. (defvar description)
  3370. ;; Defined somewhere in this file, but used before definition.
  3371. (defvar org-html-entities)
  3372. (defvar org-struct-menu)
  3373. (defvar org-org-menu)
  3374. (defvar org-tbl-menu)
  3375. (defvar org-agenda-keymap)
  3376. ;;;; Define the Org-mode
  3377. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3378. (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."))
  3379. ;; We use a before-change function to check if a table might need
  3380. ;; an update.
  3381. (defvar org-table-may-need-update t
  3382. "Indicates that a table might need an update.
  3383. This variable is set by `org-before-change-function'.
  3384. `org-table-align' sets it back to nil.")
  3385. (defun org-before-change-function (beg end)
  3386. "Every change indicates that a table might need an update."
  3387. (setq org-table-may-need-update t))
  3388. (defvar org-mode-map)
  3389. (defvar org-mode-hook nil
  3390. "Mode hook for Org-mode, run after the mode was turned on.")
  3391. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3392. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3393. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3394. (defvar org-table-buffer-is-an nil)
  3395. (defconst org-outline-regexp "\\*+ ")
  3396. ;;;###autoload
  3397. (define-derived-mode org-mode outline-mode "Org"
  3398. "Outline-based notes management and organizer, alias
  3399. \"Carsten's outline-mode for keeping track of everything.\"
  3400. Org-mode develops organizational tasks around a NOTES file which
  3401. contains information about projects as plain text. Org-mode is
  3402. implemented on top of outline-mode, which is ideal to keep the content
  3403. of large files well structured. It supports ToDo items, deadlines and
  3404. time stamps, which magically appear in the diary listing of the Emacs
  3405. calendar. Tables are easily created with a built-in table editor.
  3406. Plain text URL-like links connect to websites, emails (VM), Usenet
  3407. messages (Gnus), BBDB entries, and any files related to the project.
  3408. For printing and sharing of notes, an Org-mode file (or a part of it)
  3409. can be exported as a structured ASCII or HTML file.
  3410. The following commands are available:
  3411. \\{org-mode-map}"
  3412. ;; Get rid of Outline menus, they are not needed
  3413. ;; Need to do this here because define-derived-mode sets up
  3414. ;; the keymap so late. Still, it is a waste to call this each time
  3415. ;; we switch another buffer into org-mode.
  3416. (if (featurep 'xemacs)
  3417. (when (boundp 'outline-mode-menu-heading)
  3418. ;; Assume this is Greg's port, it used easymenu
  3419. (easy-menu-remove outline-mode-menu-heading)
  3420. (easy-menu-remove outline-mode-menu-show)
  3421. (easy-menu-remove outline-mode-menu-hide))
  3422. (define-key org-mode-map [menu-bar headings] 'undefined)
  3423. (define-key org-mode-map [menu-bar hide] 'undefined)
  3424. (define-key org-mode-map [menu-bar show] 'undefined))
  3425. (org-load-modules-maybe)
  3426. (easy-menu-add org-org-menu)
  3427. (easy-menu-add org-tbl-menu)
  3428. (org-install-agenda-files-menu)
  3429. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3430. (org-add-to-invisibility-spec '(org-cwidth))
  3431. (org-add-to-invisibility-spec '(org-hide-block . t))
  3432. (when (featurep 'xemacs)
  3433. (org-set-local 'line-move-ignore-invisible t))
  3434. (org-set-local 'outline-regexp org-outline-regexp)
  3435. (org-set-local 'outline-level 'org-outline-level)
  3436. (when (and org-ellipsis
  3437. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3438. (fboundp 'make-glyph-code))
  3439. (unless org-display-table
  3440. (setq org-display-table (make-display-table)))
  3441. (set-display-table-slot
  3442. org-display-table 4
  3443. (vconcat (mapcar
  3444. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3445. org-ellipsis)))
  3446. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3447. (setq buffer-display-table org-display-table))
  3448. (org-set-regexps-and-options)
  3449. (when (and org-tag-faces (not org-tags-special-faces-re))
  3450. ;; tag faces set outside customize.... force initialization.
  3451. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3452. ;; Calc embedded
  3453. (org-set-local 'calc-embedded-open-mode "# ")
  3454. (modify-syntax-entry ?# "<")
  3455. (modify-syntax-entry ?@ "w")
  3456. (if org-startup-truncated (setq truncate-lines t))
  3457. (org-set-local 'font-lock-unfontify-region-function
  3458. 'org-unfontify-region)
  3459. ;; Activate before-change-function
  3460. (org-set-local 'org-table-may-need-update t)
  3461. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3462. 'local)
  3463. ;; Check for running clock before killing a buffer
  3464. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3465. ;; Paragraphs and auto-filling
  3466. (org-set-autofill-regexps)
  3467. (setq indent-line-function 'org-indent-line-function)
  3468. (org-update-radio-target-regexp)
  3469. ;; Make sure dependence stuff works reliably, even for users who set it
  3470. ;; too late :-(
  3471. (if org-enforce-todo-dependencies
  3472. (add-hook 'org-blocker-hook
  3473. 'org-block-todo-from-children-or-siblings-or-parent)
  3474. (remove-hook 'org-blocker-hook
  3475. 'org-block-todo-from-children-or-siblings-or-parent))
  3476. (if org-enforce-todo-checkbox-dependencies
  3477. (add-hook 'org-blocker-hook
  3478. 'org-block-todo-from-checkboxes)
  3479. (remove-hook 'org-blocker-hook
  3480. 'org-block-todo-from-checkboxes))
  3481. ;; Comment characters
  3482. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3483. (org-set-local 'comment-padding " ")
  3484. ;; Align options lines
  3485. (org-set-local
  3486. 'align-mode-rules-list
  3487. '((org-in-buffer-settings
  3488. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3489. (modes . '(org-mode)))))
  3490. ;; Imenu
  3491. (org-set-local 'imenu-create-index-function
  3492. 'org-imenu-get-tree)
  3493. ;; Make isearch reveal context
  3494. (if (or (featurep 'xemacs)
  3495. (not (boundp 'outline-isearch-open-invisible-function)))
  3496. ;; Emacs 21 and XEmacs make use of the hook
  3497. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3498. ;; Emacs 22 deals with this through a special variable
  3499. (org-set-local 'outline-isearch-open-invisible-function
  3500. (lambda (&rest ignore) (org-show-context 'isearch))))
  3501. ;; If empty file that did not turn on org-mode automatically, make it to.
  3502. (if (and org-insert-mode-line-in-empty-file
  3503. (interactive-p)
  3504. (= (point-min) (point-max)))
  3505. (insert "# -*- mode: org -*-\n\n"))
  3506. (unless org-inhibit-startup
  3507. (when org-startup-align-all-tables
  3508. (let ((bmp (buffer-modified-p)))
  3509. (org-table-map-tables 'org-table-align)
  3510. (set-buffer-modified-p bmp)))
  3511. (org-set-startup-visibility)))
  3512. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3513. (defun org-current-time ()
  3514. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3515. (if (> (car org-time-stamp-rounding-minutes) 1)
  3516. (let ((r (car org-time-stamp-rounding-minutes))
  3517. (time (decode-time)))
  3518. (apply 'encode-time
  3519. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3520. (nthcdr 2 time))))
  3521. (current-time)))
  3522. ;;;; Font-Lock stuff, including the activators
  3523. (defvar org-mouse-map (make-sparse-keymap))
  3524. (org-defkey org-mouse-map
  3525. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3526. (org-defkey org-mouse-map
  3527. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3528. (when org-mouse-1-follows-link
  3529. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3530. (when org-tab-follows-link
  3531. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3532. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3533. (require 'font-lock)
  3534. (defconst org-non-link-chars "]\t\n\r<>")
  3535. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3536. "shell" "elisp"))
  3537. (defvar org-link-types-re nil
  3538. "Matches a link that has a url-like prefix like \"http:\"")
  3539. (defvar org-link-re-with-space nil
  3540. "Matches a link with spaces, optional angular brackets around it.")
  3541. (defvar org-link-re-with-space2 nil
  3542. "Matches a link with spaces, optional angular brackets around it.")
  3543. (defvar org-link-re-with-space3 nil
  3544. "Matches a link with spaces, only for internal part in bracket links.")
  3545. (defvar org-angle-link-re nil
  3546. "Matches link with angular brackets, spaces are allowed.")
  3547. (defvar org-plain-link-re nil
  3548. "Matches plain link, without spaces.")
  3549. (defvar org-bracket-link-regexp nil
  3550. "Matches a link in double brackets.")
  3551. (defvar org-bracket-link-analytic-regexp nil
  3552. "Regular expression used to analyze links.
  3553. Here is what the match groups contain after a match:
  3554. 1: http:
  3555. 2: http
  3556. 3: path
  3557. 4: [desc]
  3558. 5: desc")
  3559. (defvar org-bracket-link-analytic-regexp++ nil
  3560. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3561. (defvar org-any-link-re nil
  3562. "Regular expression matching any link.")
  3563. (defun org-make-link-regexps ()
  3564. "Update the link regular expressions.
  3565. This should be called after the variable `org-link-types' has changed."
  3566. (setq org-link-types-re
  3567. (concat
  3568. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3569. org-link-re-with-space
  3570. (concat
  3571. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3572. "\\([^" org-non-link-chars " ]"
  3573. "[^" org-non-link-chars "]*"
  3574. "[^" org-non-link-chars " ]\\)>?")
  3575. org-link-re-with-space2
  3576. (concat
  3577. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3578. "\\([^" org-non-link-chars " ]"
  3579. "[^\t\n\r]*"
  3580. "[^" org-non-link-chars " ]\\)>?")
  3581. org-link-re-with-space3
  3582. (concat
  3583. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3584. "\\([^" org-non-link-chars " ]"
  3585. "[^\t\n\r]*\\)")
  3586. org-angle-link-re
  3587. (concat
  3588. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3589. "\\([^" org-non-link-chars " ]"
  3590. "[^" org-non-link-chars "]*"
  3591. "\\)>")
  3592. org-plain-link-re
  3593. (concat
  3594. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3595. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3596. org-bracket-link-regexp
  3597. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3598. org-bracket-link-analytic-regexp
  3599. (concat
  3600. "\\[\\["
  3601. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3602. "\\([^]]+\\)"
  3603. "\\]"
  3604. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3605. "\\]")
  3606. org-bracket-link-analytic-regexp++
  3607. (concat
  3608. "\\[\\["
  3609. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3610. "\\([^]]+\\)"
  3611. "\\]"
  3612. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3613. "\\]")
  3614. org-any-link-re
  3615. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3616. org-angle-link-re "\\)\\|\\("
  3617. org-plain-link-re "\\)")))
  3618. (org-make-link-regexps)
  3619. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3620. "Regular expression for fast time stamp matching.")
  3621. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3622. "Regular expression for fast time stamp matching.")
  3623. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3624. "Regular expression matching time strings for analysis.
  3625. This one does not require the space after the date, so it can be used
  3626. on a string that terminates immediately after the date.")
  3627. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3628. "Regular expression matching time strings for analysis.")
  3629. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3630. "Regular expression matching time stamps, with groups.")
  3631. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3632. "Regular expression matching time stamps (also [..]), with groups.")
  3633. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3634. "Regular expression matching a time stamp range.")
  3635. (defconst org-tr-regexp-both
  3636. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3637. "Regular expression matching a time stamp range.")
  3638. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3639. org-ts-regexp "\\)?")
  3640. "Regular expression matching a time stamp or time stamp range.")
  3641. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3642. org-ts-regexp-both "\\)?")
  3643. "Regular expression matching a time stamp or time stamp range.
  3644. The time stamps may be either active or inactive.")
  3645. (defvar org-emph-face nil)
  3646. (defun org-do-emphasis-faces (limit)
  3647. "Run through the buffer and add overlays to links."
  3648. (let (rtn)
  3649. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3650. (if (not (= (char-after (match-beginning 3))
  3651. (char-after (match-beginning 4))))
  3652. (progn
  3653. (setq rtn t)
  3654. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3655. 'face
  3656. (nth 1 (assoc (match-string 3)
  3657. org-emphasis-alist)))
  3658. (add-text-properties (match-beginning 2) (match-end 2)
  3659. '(font-lock-multiline t))
  3660. (when org-hide-emphasis-markers
  3661. (add-text-properties (match-end 4) (match-beginning 5)
  3662. '(invisible org-link))
  3663. (add-text-properties (match-beginning 3) (match-end 3)
  3664. '(invisible org-link)))))
  3665. (backward-char 1))
  3666. rtn))
  3667. (defun org-emphasize (&optional char)
  3668. "Insert or change an emphasis, i.e. a font like bold or italic.
  3669. If there is an active region, change that region to a new emphasis.
  3670. If there is no region, just insert the marker characters and position
  3671. the cursor between them.
  3672. CHAR should be either the marker character, or the first character of the
  3673. HTML tag associated with that emphasis. If CHAR is a space, the means
  3674. to remove the emphasis of the selected region.
  3675. If char is not given (for example in an interactive call) it
  3676. will be prompted for."
  3677. (interactive)
  3678. (let ((eal org-emphasis-alist) e det
  3679. (erc org-emphasis-regexp-components)
  3680. (prompt "")
  3681. (string "") beg end move tag c s)
  3682. (if (org-region-active-p)
  3683. (setq beg (region-beginning) end (region-end)
  3684. string (buffer-substring beg end))
  3685. (setq move t))
  3686. (while (setq e (pop eal))
  3687. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3688. c (aref tag 0))
  3689. (push (cons c (string-to-char (car e))) det)
  3690. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3691. (substring tag 1)))))
  3692. (setq det (nreverse det))
  3693. (unless char
  3694. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3695. (setq char (read-char-exclusive)))
  3696. (setq char (or (cdr (assoc char det)) char))
  3697. (if (equal char ?\ )
  3698. (setq s "" move nil)
  3699. (unless (assoc (char-to-string char) org-emphasis-alist)
  3700. (error "No such emphasis marker: \"%c\"" char))
  3701. (setq s (char-to-string char)))
  3702. (while (and (> (length string) 1)
  3703. (equal (substring string 0 1) (substring string -1))
  3704. (assoc (substring string 0 1) org-emphasis-alist))
  3705. (setq string (substring string 1 -1)))
  3706. (setq string (concat s string s))
  3707. (if beg (delete-region beg end))
  3708. (unless (or (bolp)
  3709. (string-match (concat "[" (nth 0 erc) "\n]")
  3710. (char-to-string (char-before (point)))))
  3711. (insert " "))
  3712. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3713. (char-to-string (char-after (point))))
  3714. (insert " ") (backward-char 1))
  3715. (insert string)
  3716. (and move (backward-char 1))))
  3717. (defconst org-nonsticky-props
  3718. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3719. (defsubst org-rear-nonsticky-at (pos)
  3720. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3721. (defun org-activate-plain-links (limit)
  3722. "Run through the buffer and add overlays to links."
  3723. (catch 'exit
  3724. (let (f)
  3725. (while (re-search-forward org-plain-link-re limit t)
  3726. (setq f (get-text-property (match-beginning 0) 'face))
  3727. (if (or (eq f 'org-tag)
  3728. (and (listp f) (memq 'org-tag f)))
  3729. nil
  3730. (add-text-properties (match-beginning 0) (match-end 0)
  3731. (list 'mouse-face 'highlight
  3732. 'keymap org-mouse-map))
  3733. (org-rear-nonsticky-at (match-end 0))
  3734. (throw 'exit t))))))
  3735. (defun org-activate-code (limit)
  3736. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3737. (progn
  3738. (remove-text-properties (match-beginning 0) (match-end 0)
  3739. '(display t invisible t intangible t))
  3740. t)))
  3741. (defvar org-protecting-blocks
  3742. '("src" "example" "latex" "ascii" "html" "docbook")
  3743. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3744. This is needed for font-lock setup.")
  3745. (defun org-fontify-meta-lines-and-blocks (limit)
  3746. "Fontify #+ lines and blocks, in the correct ways."
  3747. (let ((case-fold-search t))
  3748. (if (re-search-forward
  3749. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3750. limit t)
  3751. (let ((beg (match-beginning 0))
  3752. (beg1 (line-beginning-position 2))
  3753. (dc1 (downcase (match-string 2)))
  3754. (dc3 (downcase (match-string 3)))
  3755. end end1 quoting)
  3756. (cond
  3757. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3758. ;; a single line of backend-specific content
  3759. (remove-text-properties (match-beginning 0) (match-end 0)
  3760. '(display t invisible t intangible t))
  3761. (add-text-properties (match-beginning 1) (match-end 3)
  3762. '(font-lock-fontified t face org-meta-line))
  3763. (add-text-properties (match-beginning 6) (match-end 6)
  3764. '(font-lock-fontified t face org-block))
  3765. t)
  3766. ((and (match-end 4) (equal dc3 "begin"))
  3767. ;; Truely a block
  3768. (setq quoting (member (downcase (match-string 5))
  3769. org-protecting-blocks))
  3770. (when (re-search-forward
  3771. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3772. nil t) ;; on purpose, we look further than LIMIT
  3773. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3774. (when quoting
  3775. (remove-text-properties beg end
  3776. '(display t invisible t intangible t)))
  3777. (add-text-properties
  3778. beg end
  3779. '(font-lock-fontified t font-lock-multiline t))
  3780. (add-text-properties beg beg1 '(face org-meta-line))
  3781. (add-text-properties end1 end '(face org-meta-line))
  3782. (when quoting
  3783. (add-text-properties beg1 end1 '(face org-block)))
  3784. t))
  3785. ((not (member (char-after beg) '(?\ ?\t)))
  3786. ;; just any other in-buffer setting, but not indented
  3787. (add-text-properties
  3788. beg (match-end 0)
  3789. '(font-lock-fontified t face org-meta-line))
  3790. t)
  3791. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:"))
  3792. (and (match-end 4) (equal dc3 "attr")))
  3793. (add-text-properties
  3794. beg (match-end 0)
  3795. '(font-lock-fontified t face org-meta-line))
  3796. t)
  3797. (t nil))))))
  3798. (defun org-activate-angle-links (limit)
  3799. "Run through the buffer and add overlays to links."
  3800. (if (re-search-forward org-angle-link-re limit t)
  3801. (progn
  3802. (add-text-properties (match-beginning 0) (match-end 0)
  3803. (list 'mouse-face 'highlight
  3804. 'keymap org-mouse-map))
  3805. (org-rear-nonsticky-at (match-end 0))
  3806. t)))
  3807. (defun org-activate-footnote-links (limit)
  3808. "Run through the buffer and add overlays to links."
  3809. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3810. limit t)
  3811. (progn
  3812. (add-text-properties (match-beginning 2) (match-end 2)
  3813. (list 'mouse-face 'highlight
  3814. 'keymap org-mouse-map
  3815. 'help-echo
  3816. (if (= (point-at-bol) (match-beginning 2))
  3817. "Footnote definition"
  3818. "Footnote reference")
  3819. ))
  3820. (org-rear-nonsticky-at (match-end 2))
  3821. t)))
  3822. (defun org-activate-bracket-links (limit)
  3823. "Run through the buffer and add overlays to bracketed links."
  3824. (if (re-search-forward org-bracket-link-regexp limit t)
  3825. (let* ((help (concat "LINK: "
  3826. (org-match-string-no-properties 1)))
  3827. ;; FIXME: above we should remove the escapes.
  3828. ;; but that requires another match, protecting match data,
  3829. ;; a lot of overhead for font-lock.
  3830. (ip (org-maybe-intangible
  3831. (list 'invisible 'org-link
  3832. 'keymap org-mouse-map 'mouse-face 'highlight
  3833. 'font-lock-multiline t 'help-echo help)))
  3834. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3835. 'font-lock-multiline t 'help-echo help)))
  3836. ;; We need to remove the invisible property here. Table narrowing
  3837. ;; may have made some of this invisible.
  3838. (remove-text-properties (match-beginning 0) (match-end 0)
  3839. '(invisible nil))
  3840. (if (match-end 3)
  3841. (progn
  3842. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3843. (org-rear-nonsticky-at (match-beginning 3))
  3844. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3845. (org-rear-nonsticky-at (match-end 3))
  3846. (add-text-properties (match-end 3) (match-end 0) ip)
  3847. (org-rear-nonsticky-at (match-end 0)))
  3848. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3849. (org-rear-nonsticky-at (match-beginning 1))
  3850. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3851. (org-rear-nonsticky-at (match-end 1))
  3852. (add-text-properties (match-end 1) (match-end 0) ip)
  3853. (org-rear-nonsticky-at (match-end 0)))
  3854. t)))
  3855. (defun org-activate-dates (limit)
  3856. "Run through the buffer and add overlays to dates."
  3857. (if (re-search-forward org-tsr-regexp-both limit t)
  3858. (progn
  3859. (add-text-properties (match-beginning 0) (match-end 0)
  3860. (list 'mouse-face 'highlight
  3861. 'keymap org-mouse-map))
  3862. (org-rear-nonsticky-at (match-end 0))
  3863. (when org-display-custom-times
  3864. (if (match-end 3)
  3865. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3866. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3867. t)))
  3868. (defvar org-target-link-regexp nil
  3869. "Regular expression matching radio targets in plain text.")
  3870. (make-variable-buffer-local 'org-target-link-regexp)
  3871. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3872. "Regular expression matching a link target.")
  3873. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3874. "Regular expression matching a radio target.")
  3875. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3876. "Regular expression matching any target.")
  3877. (defun org-activate-target-links (limit)
  3878. "Run through the buffer and add overlays to target matches."
  3879. (when org-target-link-regexp
  3880. (let ((case-fold-search t))
  3881. (if (re-search-forward org-target-link-regexp limit t)
  3882. (progn
  3883. (add-text-properties (match-beginning 0) (match-end 0)
  3884. (list 'mouse-face 'highlight
  3885. 'keymap org-mouse-map
  3886. 'help-echo "Radio target link"
  3887. 'org-linked-text t))
  3888. (org-rear-nonsticky-at (match-end 0))
  3889. t)))))
  3890. (defun org-update-radio-target-regexp ()
  3891. "Find all radio targets in this file and update the regular expression."
  3892. (interactive)
  3893. (when (memq 'radio org-activate-links)
  3894. (setq org-target-link-regexp
  3895. (org-make-target-link-regexp (org-all-targets 'radio)))
  3896. (org-restart-font-lock)))
  3897. (defun org-hide-wide-columns (limit)
  3898. (let (s e)
  3899. (setq s (text-property-any (point) (or limit (point-max))
  3900. 'org-cwidth t))
  3901. (when s
  3902. (setq e (next-single-property-change s 'org-cwidth))
  3903. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3904. (goto-char e)
  3905. t)))
  3906. (defvar org-latex-and-specials-regexp nil
  3907. "Regular expression for highlighting export special stuff.")
  3908. (defvar org-match-substring-regexp)
  3909. (defvar org-match-substring-with-braces-regexp)
  3910. (defvar org-export-html-special-string-regexps)
  3911. (defun org-compute-latex-and-specials-regexp ()
  3912. "Compute regular expression for stuff treated specially by exporters."
  3913. (if (not org-highlight-latex-fragments-and-specials)
  3914. (org-set-local 'org-latex-and-specials-regexp nil)
  3915. (require 'org-exp)
  3916. (let*
  3917. ((matchers (plist-get org-format-latex-options :matchers))
  3918. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3919. org-latex-regexps)))
  3920. (options (org-combine-plists (org-default-export-plist)
  3921. (org-infile-export-plist)))
  3922. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3923. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3924. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3925. (org-export-html-expand (plist-get options :expand-quoted-html))
  3926. (org-export-with-special-strings (plist-get options :special-strings))
  3927. (re-sub
  3928. (cond
  3929. ((equal org-export-with-sub-superscripts '{})
  3930. (list org-match-substring-with-braces-regexp))
  3931. (org-export-with-sub-superscripts
  3932. (list org-match-substring-regexp))
  3933. (t nil)))
  3934. (re-latex
  3935. (if org-export-with-LaTeX-fragments
  3936. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3937. (re-macros
  3938. (if org-export-with-TeX-macros
  3939. (list (concat "\\\\"
  3940. (regexp-opt
  3941. (append (mapcar 'car org-html-entities)
  3942. (if (boundp 'org-latex-entities)
  3943. (mapcar (lambda (x)
  3944. (or (car-safe x) x))
  3945. org-latex-entities)
  3946. nil))
  3947. 'words))) ; FIXME
  3948. ))
  3949. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3950. (re-special (if org-export-with-special-strings
  3951. (mapcar (lambda (x) (car x))
  3952. org-export-html-special-string-regexps)))
  3953. (re-rest
  3954. (delq nil
  3955. (list
  3956. (if org-export-html-expand "@<[^>\n]+>")
  3957. ))))
  3958. (org-set-local
  3959. 'org-latex-and-specials-regexp
  3960. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3961. re-rest) "\\|")))))
  3962. (defun org-do-latex-and-special-faces (limit)
  3963. "Run through the buffer and add overlays to links."
  3964. (when org-latex-and-specials-regexp
  3965. (let (rtn d)
  3966. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3967. limit t))
  3968. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3969. 'face))
  3970. '(org-code org-verbatim underline)))
  3971. (progn
  3972. (setq rtn t
  3973. d (cond ((member (char-after (1+ (match-beginning 0)))
  3974. '(?_ ?^)) 1)
  3975. (t 0)))
  3976. (font-lock-prepend-text-property
  3977. (+ d (match-beginning 0)) (match-end 0)
  3978. 'face 'org-latex-and-export-specials)
  3979. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3980. '(font-lock-multiline t)))))
  3981. rtn)))
  3982. (defun org-restart-font-lock ()
  3983. "Restart font-lock-mode, to force refontification."
  3984. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3985. (font-lock-mode -1)
  3986. (font-lock-mode 1)))
  3987. (defun org-all-targets (&optional radio)
  3988. "Return a list of all targets in this file.
  3989. With optional argument RADIO, only find radio targets."
  3990. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3991. rtn)
  3992. (save-excursion
  3993. (goto-char (point-min))
  3994. (while (re-search-forward re nil t)
  3995. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3996. rtn)))
  3997. (defun org-make-target-link-regexp (targets)
  3998. "Make regular expression matching all strings in TARGETS.
  3999. The regular expression finds the targets also if there is a line break
  4000. between words."
  4001. (and targets
  4002. (concat
  4003. "\\<\\("
  4004. (mapconcat
  4005. (lambda (x)
  4006. (while (string-match " +" x)
  4007. (setq x (replace-match "\\s-+" t t x)))
  4008. x)
  4009. targets
  4010. "\\|")
  4011. "\\)\\>")))
  4012. (defun org-activate-tags (limit)
  4013. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4014. (progn
  4015. (add-text-properties (match-beginning 1) (match-end 1)
  4016. (list 'mouse-face 'highlight
  4017. 'keymap org-mouse-map))
  4018. (org-rear-nonsticky-at (match-end 1))
  4019. t)))
  4020. (defun org-outline-level ()
  4021. (save-excursion
  4022. (looking-at outline-regexp)
  4023. (if (match-beginning 1)
  4024. (+ (org-get-string-indentation (match-string 1)) 1000)
  4025. (1- (- (match-end 0) (match-beginning 0))))))
  4026. (defvar org-font-lock-keywords nil)
  4027. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4028. "Regular expression matching a property line.")
  4029. (defvar org-font-lock-hook nil
  4030. "Functions to be called for special font lock stuff.")
  4031. (defun org-font-lock-hook (limit)
  4032. (run-hook-with-args 'org-font-lock-hook limit))
  4033. (defun org-set-font-lock-defaults ()
  4034. (let* ((em org-fontify-emphasized-text)
  4035. (lk org-activate-links)
  4036. (org-font-lock-extra-keywords
  4037. (list
  4038. ;; Call the hook
  4039. '(org-font-lock-hook)
  4040. ;; Headlines
  4041. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4042. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4043. ;; Table lines
  4044. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4045. (1 'org-table t))
  4046. ;; Table internals
  4047. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4048. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4049. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4050. ;; Drawers
  4051. (list org-drawer-regexp '(0 'org-special-keyword t))
  4052. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4053. ;; Properties
  4054. (list org-property-re
  4055. '(1 'org-special-keyword t)
  4056. '(3 'org-property-value t))
  4057. (if org-format-transports-properties-p
  4058. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4059. ;; Links
  4060. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4061. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4062. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4063. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4064. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4065. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4066. (if (memq 'footnote lk) '(org-activate-footnote-links
  4067. (2 'org-footnote t)))
  4068. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4069. '(org-hide-wide-columns (0 nil append))
  4070. ;; TODO lines
  4071. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4072. '(1 (org-get-todo-face 1) t))
  4073. ;; DONE
  4074. (if org-fontify-done-headline
  4075. (list (concat "^[*]+ +\\<\\("
  4076. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4077. "\\)\\(.*\\)")
  4078. '(2 'org-headline-done t))
  4079. nil)
  4080. ;; Priorities
  4081. '(org-font-lock-add-priority-faces)
  4082. ;; Tags
  4083. '(org-font-lock-add-tag-faces)
  4084. ;; Special keywords
  4085. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4086. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4087. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4088. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4089. ;; Emphasis
  4090. (if em
  4091. (if (featurep 'xemacs)
  4092. '(org-do-emphasis-faces (0 nil append))
  4093. '(org-do-emphasis-faces)))
  4094. ;; Checkboxes
  4095. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4096. 2 'org-checkbox prepend)
  4097. (if org-provide-checkbox-statistics
  4098. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4099. (0 (org-get-checkbox-statistics-face) t)))
  4100. ;; Description list items
  4101. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4102. 2 'bold prepend)
  4103. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4104. '(1 'org-archived prepend))
  4105. ;; Specials
  4106. '(org-do-latex-and-special-faces)
  4107. ;; Code
  4108. '(org-activate-code (1 'org-code t))
  4109. ;; COMMENT
  4110. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4111. "\\|" org-quote-string "\\)\\>")
  4112. '(1 'org-special-keyword t))
  4113. '("^#.*" (0 'font-lock-comment-face t))
  4114. ;; Blocks and meta lines
  4115. '(org-fontify-meta-lines-and-blocks)
  4116. )))
  4117. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4118. ;; Now set the full font-lock-keywords
  4119. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4120. (org-set-local 'font-lock-defaults
  4121. '(org-font-lock-keywords t nil nil backward-paragraph))
  4122. (kill-local-variable 'font-lock-keywords) nil))
  4123. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4124. "Fontify string S like in Org-mode"
  4125. (with-temp-buffer
  4126. (insert s)
  4127. (let ((org-odd-levels-only odd-levels))
  4128. (org-mode)
  4129. (font-lock-fontify-buffer)
  4130. (buffer-string))))
  4131. (defvar org-m nil)
  4132. (defvar org-l nil)
  4133. (defvar org-f nil)
  4134. (defun org-get-level-face (n)
  4135. "Get the right face for match N in font-lock matching of headlines."
  4136. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4137. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4138. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4139. (cond
  4140. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4141. ((eq n 2) org-f)
  4142. (t (if org-level-color-stars-only nil org-f))))
  4143. (defun org-get-todo-face (kwd)
  4144. "Get the right face for a TODO keyword KWD.
  4145. If KWD is a number, get the corresponding match group."
  4146. (if (numberp kwd) (setq kwd (match-string kwd)))
  4147. (or (cdr (assoc kwd org-todo-keyword-faces))
  4148. (and (member kwd org-done-keywords) 'org-done)
  4149. 'org-todo))
  4150. (defun org-font-lock-add-tag-faces (limit)
  4151. "Add the special tag faces."
  4152. (when (and org-tag-faces org-tags-special-faces-re)
  4153. (while (re-search-forward org-tags-special-faces-re limit t)
  4154. (add-text-properties (match-beginning 1) (match-end 1)
  4155. (list 'face (org-get-tag-face 1)
  4156. 'font-lock-fontified t))
  4157. (backward-char 1))))
  4158. (defun org-font-lock-add-priority-faces (limit)
  4159. "Add the special priority faces."
  4160. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4161. (add-text-properties
  4162. (match-beginning 0) (match-end 0)
  4163. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4164. org-priority-faces))
  4165. 'org-special-keyword)
  4166. 'font-lock-fontified t))))
  4167. (defun org-get-tag-face (kwd)
  4168. "Get the right face for a TODO keyword KWD.
  4169. If KWD is a number, get the corresponding match group."
  4170. (if (numberp kwd) (setq kwd (match-string kwd)))
  4171. (or (cdr (assoc kwd org-tag-faces))
  4172. 'org-tag))
  4173. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4174. "Remove fontification and activation overlays from links."
  4175. (font-lock-default-unfontify-region beg end)
  4176. (let* ((buffer-undo-list t)
  4177. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4178. (inhibit-modification-hooks t)
  4179. deactivate-mark buffer-file-name buffer-file-truename)
  4180. (remove-text-properties beg end
  4181. '(mouse-face t keymap t org-linked-text t
  4182. invisible t intangible t))))
  4183. ;;;; Visibility cycling, including org-goto and indirect buffer
  4184. ;;; Cycling
  4185. (defvar org-cycle-global-status nil)
  4186. (make-variable-buffer-local 'org-cycle-global-status)
  4187. (defvar org-cycle-subtree-status nil)
  4188. (make-variable-buffer-local 'org-cycle-subtree-status)
  4189. ;;;###autoload
  4190. (defvar org-inlinetask-min-level)
  4191. (defun org-cycle (&optional arg)
  4192. "TAB-action and visibility cycling for Org-mode.
  4193. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4194. is outine visibility cycling, but it also invokes other actions
  4195. in special contexts.
  4196. - When this function is called with a prefix argument, rotate the entire
  4197. buffer through 3 states (global cycling)
  4198. 1. OVERVIEW: Show only top-level headlines.
  4199. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4200. 3. SHOW ALL: Show everything.
  4201. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4202. determined by the variable `org-startup-folded', and by any VISIBILITY
  4203. properties in the buffer.
  4204. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4205. including any drawers.
  4206. - When inside a table, re-align the table and move to the next field.
  4207. - When point is at the beginning of a headline, rotate the subtree started
  4208. by this line through 3 different states (local cycling)
  4209. 1. FOLDED: Only the main headline is shown.
  4210. 2. CHILDREN: The main headline and the direct children are shown.
  4211. From this state, you can move to one of the children
  4212. and zoom in further.
  4213. 3. SUBTREE: Show the entire subtree, including body text.
  4214. - When there is a numeric prefix, go up to a heading with level ARG, do
  4215. a `show-subtree' and return to the previous cursor position. If ARG
  4216. is negative, go up that many levels.
  4217. - When point is not at the beginning of a headline, execute the global
  4218. binding for TAB, which is re-indenting the line. See the option
  4219. `org-cycle-emulate-tab' for details.
  4220. - Special case: if point is at the beginning of the buffer and there is
  4221. no headline in line 1, this function will act as if called with prefix arg.
  4222. But only if also the variable `org-cycle-global-at-bob' is t."
  4223. (interactive "P")
  4224. (org-load-modules-maybe)
  4225. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4226. (let* ((limit-level
  4227. (or org-cycle-max-level
  4228. (and (boundp 'org-inlinetask-min-level)
  4229. org-inlinetask-min-level
  4230. (1- org-inlinetask-min-level))))
  4231. (nstars (and limit-level
  4232. (if org-odd-levels-only
  4233. (and limit-level (1- (* limit-level 2)))
  4234. limit-level)))
  4235. (outline-regexp
  4236. (cond
  4237. ((not (org-mode-p)) outline-regexp)
  4238. (org-cycle-include-plain-lists
  4239. (concat "\\(?:\\*"
  4240. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4241. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4242. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4243. (bob-special (and org-cycle-global-at-bob (bobp)
  4244. (not (looking-at outline-regexp))))
  4245. (org-cycle-hook
  4246. (if bob-special
  4247. (delq 'org-optimize-window-after-visibility-change
  4248. (copy-sequence org-cycle-hook))
  4249. org-cycle-hook))
  4250. (pos (point)))
  4251. (if (or bob-special (equal arg '(4)))
  4252. ;; special case: use global cycling
  4253. (setq arg t))
  4254. (cond
  4255. ((equal arg '(16))
  4256. (org-set-startup-visibility)
  4257. (message "Startup visibility, plus VISIBILITY properties"))
  4258. ((equal arg '(64))
  4259. (show-all)
  4260. (message "Entire buffer visible, including drawers"))
  4261. ((org-at-table-p 'any)
  4262. ;; Enter the table or move to the next field in the table
  4263. (or (org-table-recognize-table.el)
  4264. (progn
  4265. (if arg (org-table-edit-field t)
  4266. (org-table-justify-field-maybe)
  4267. (call-interactively 'org-table-next-field)))))
  4268. ((run-hook-with-args-until-success
  4269. 'org-tab-after-check-for-table-hook))
  4270. ((eq arg t) ;; Global cycling
  4271. (org-cycle-internal-global))
  4272. ((and org-drawers org-drawer-regexp
  4273. (save-excursion
  4274. (beginning-of-line 1)
  4275. (looking-at org-drawer-regexp)))
  4276. ;; Toggle block visibility
  4277. (org-flag-drawer
  4278. (not (get-char-property (match-end 0) 'invisible))))
  4279. ((integerp arg)
  4280. ;; Show-subtree, ARG levels up from here.
  4281. (save-excursion
  4282. (org-back-to-heading)
  4283. (outline-up-heading (if (< arg 0) (- arg)
  4284. (- (funcall outline-level) arg)))
  4285. (org-show-subtree)))
  4286. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4287. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4288. (org-cycle-internal-local))
  4289. ;; TAB emulation and template completion
  4290. (buffer-read-only (org-back-to-heading))
  4291. ((run-hook-with-args-until-success
  4292. 'org-tab-after-check-for-cycling-hook))
  4293. ((org-try-structure-completion))
  4294. ((org-try-cdlatex-tab))
  4295. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4296. (or (not (bolp))
  4297. (not (looking-at outline-regexp))))
  4298. (call-interactively (global-key-binding "\t")))
  4299. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4300. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4301. (or (and (eq org-cycle-emulate-tab 'white)
  4302. (= (match-end 0) (point-at-eol)))
  4303. (and (eq org-cycle-emulate-tab 'whitestart)
  4304. (>= (match-end 0) pos))))
  4305. t
  4306. (eq org-cycle-emulate-tab t))
  4307. (call-interactively (global-key-binding "\t")))
  4308. (t (save-excursion
  4309. (org-back-to-heading)
  4310. (org-cycle)))))))
  4311. (defun org-cycle-internal-global ()
  4312. "Do the global cycling action."
  4313. (cond
  4314. ((and (eq last-command this-command)
  4315. (eq org-cycle-global-status 'overview))
  4316. ;; We just created the overview - now do table of contents
  4317. ;; This can be slow in very large buffers, so indicate action
  4318. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4319. (message "CONTENTS...")
  4320. (org-content)
  4321. (message "CONTENTS...done")
  4322. (setq org-cycle-global-status 'contents)
  4323. (run-hook-with-args 'org-cycle-hook 'contents))
  4324. ((and (eq last-command this-command)
  4325. (eq org-cycle-global-status 'contents))
  4326. ;; We just showed the table of contents - now show everything
  4327. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4328. (show-all)
  4329. (message "SHOW ALL")
  4330. (setq org-cycle-global-status 'all)
  4331. (run-hook-with-args 'org-cycle-hook 'all))
  4332. (t
  4333. ;; Default action: go to overview
  4334. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4335. (org-overview)
  4336. (message "OVERVIEW")
  4337. (setq org-cycle-global-status 'overview)
  4338. (run-hook-with-args 'org-cycle-hook 'overview))))
  4339. (defun org-cycle-internal-local ()
  4340. "Do the local cycling action."
  4341. (org-back-to-heading)
  4342. (let ((goal-column 0) eoh eol eos)
  4343. ;; First, some boundaries
  4344. (save-excursion
  4345. (org-back-to-heading)
  4346. (save-excursion
  4347. (beginning-of-line 2)
  4348. (while (and (not (eobp)) ;; this is like `next-line'
  4349. (get-char-property (1- (point)) 'invisible))
  4350. (beginning-of-line 2)) (setq eol (point)))
  4351. (outline-end-of-heading) (setq eoh (point))
  4352. (org-end-of-subtree t)
  4353. (unless (eobp)
  4354. (skip-chars-forward " \t\n")
  4355. (beginning-of-line 1) ; in case this is an item
  4356. )
  4357. (setq eos (1- (point))))
  4358. ;; Find out what to do next and set `this-command'
  4359. (cond
  4360. ((= eos eoh)
  4361. ;; Nothing is hidden behind this heading
  4362. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4363. (message "EMPTY ENTRY")
  4364. (setq org-cycle-subtree-status nil)
  4365. (save-excursion
  4366. (goto-char eos)
  4367. (outline-next-heading)
  4368. (if (org-invisible-p) (org-flag-heading nil))))
  4369. ((or (>= eol eos)
  4370. (not (string-match "\\S-" (buffer-substring eol eos))))
  4371. ;; Entire subtree is hidden in one line: open it
  4372. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4373. (org-show-entry)
  4374. (show-children)
  4375. (message "CHILDREN")
  4376. (save-excursion
  4377. (goto-char eos)
  4378. (outline-next-heading)
  4379. (if (org-invisible-p) (org-flag-heading nil)))
  4380. (setq org-cycle-subtree-status 'children)
  4381. (run-hook-with-args 'org-cycle-hook 'children))
  4382. ((and (eq last-command this-command)
  4383. (eq org-cycle-subtree-status 'children))
  4384. ;; We just showed the children, now show everything.
  4385. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4386. (org-show-subtree)
  4387. (message "SUBTREE")
  4388. (setq org-cycle-subtree-status 'subtree)
  4389. (run-hook-with-args 'org-cycle-hook 'subtree))
  4390. (t
  4391. ;; Default action: hide the subtree.
  4392. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4393. (hide-subtree)
  4394. (message "FOLDED")
  4395. (setq org-cycle-subtree-status 'folded)
  4396. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4397. ;;;###autoload
  4398. (defun org-global-cycle (&optional arg)
  4399. "Cycle the global visibility. For details see `org-cycle'.
  4400. With C-u prefix arg, switch to startup visibility.
  4401. With a numeric prefix, show all headlines up to that level."
  4402. (interactive "P")
  4403. (let ((org-cycle-include-plain-lists
  4404. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4405. (cond
  4406. ((integerp arg)
  4407. (show-all)
  4408. (hide-sublevels arg)
  4409. (setq org-cycle-global-status 'contents))
  4410. ((equal arg '(4))
  4411. (org-set-startup-visibility)
  4412. (message "Startup visibility, plus VISIBILITY properties."))
  4413. (t
  4414. (org-cycle '(4))))))
  4415. (defun org-set-startup-visibility ()
  4416. "Set the visibility required by startup options and properties."
  4417. (cond
  4418. ((eq org-startup-folded t)
  4419. (org-cycle '(4)))
  4420. ((eq org-startup-folded 'content)
  4421. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4422. (org-cycle '(4)) (org-cycle '(4)))))
  4423. (if org-hide-block-startup (org-hide-block-all))
  4424. (org-set-visibility-according-to-property 'no-cleanup)
  4425. (org-cycle-hide-archived-subtrees 'all)
  4426. (org-cycle-hide-drawers 'all)
  4427. (org-cycle-show-empty-lines 'all))
  4428. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4429. "Switch subtree visibilities according to :VISIBILITY: property."
  4430. (interactive)
  4431. (let (org-show-entry-below state)
  4432. (save-excursion
  4433. (goto-char (point-min))
  4434. (while (re-search-forward
  4435. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4436. nil t)
  4437. (setq state (match-string 1))
  4438. (save-excursion
  4439. (org-back-to-heading t)
  4440. (hide-subtree)
  4441. (org-reveal)
  4442. (cond
  4443. ((equal state '("fold" "folded"))
  4444. (hide-subtree))
  4445. ((equal state "children")
  4446. (org-show-hidden-entry)
  4447. (show-children))
  4448. ((equal state "content")
  4449. (save-excursion
  4450. (save-restriction
  4451. (org-narrow-to-subtree)
  4452. (org-content))))
  4453. ((member state '("all" "showall"))
  4454. (show-subtree)))))
  4455. (unless no-cleanup
  4456. (org-cycle-hide-archived-subtrees 'all)
  4457. (org-cycle-hide-drawers 'all)
  4458. (org-cycle-show-empty-lines 'all)))))
  4459. (defun org-overview ()
  4460. "Switch to overview mode, showing only top-level headlines.
  4461. Really, this shows all headlines with level equal or greater than the level
  4462. of the first headline in the buffer. This is important, because if the
  4463. first headline is not level one, then (hide-sublevels 1) gives confusing
  4464. results."
  4465. (interactive)
  4466. (let ((level (save-excursion
  4467. (goto-char (point-min))
  4468. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4469. (progn
  4470. (goto-char (match-beginning 0))
  4471. (funcall outline-level))))))
  4472. (and level (hide-sublevels level))))
  4473. (defun org-content (&optional arg)
  4474. "Show all headlines in the buffer, like a table of contents.
  4475. With numerical argument N, show content up to level N."
  4476. (interactive "P")
  4477. (save-excursion
  4478. ;; Visit all headings and show their offspring
  4479. (and (integerp arg) (org-overview))
  4480. (goto-char (point-max))
  4481. (catch 'exit
  4482. (while (and (progn (condition-case nil
  4483. (outline-previous-visible-heading 1)
  4484. (error (goto-char (point-min))))
  4485. t)
  4486. (looking-at outline-regexp))
  4487. (if (integerp arg)
  4488. (show-children (1- arg))
  4489. (show-branches))
  4490. (if (bobp) (throw 'exit nil))))))
  4491. (defun org-optimize-window-after-visibility-change (state)
  4492. "Adjust the window after a change in outline visibility.
  4493. This function is the default value of the hook `org-cycle-hook'."
  4494. (when (get-buffer-window (current-buffer))
  4495. (cond
  4496. ((eq state 'content) nil)
  4497. ((eq state 'all) nil)
  4498. ((eq state 'folded) nil)
  4499. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4500. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4501. (defun org-compact-display-after-subtree-move ()
  4502. "Show a compacter version of the tree of the entry's parent."
  4503. (save-excursion
  4504. (if (org-up-heading-safe)
  4505. (progn
  4506. (hide-subtree)
  4507. (show-entry)
  4508. (show-children)
  4509. (org-cycle-show-empty-lines 'children)
  4510. (org-cycle-hide-drawers 'children))
  4511. (org-overview))))
  4512. (defun org-cycle-show-empty-lines (state)
  4513. "Show empty lines above all visible headlines.
  4514. The region to be covered depends on STATE when called through
  4515. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4516. entire buffer covered. Note that an empty line is only shown if there
  4517. are at least `org-cycle-separator-lines' empty lines before the headline."
  4518. (when (> org-cycle-separator-lines 0)
  4519. (save-excursion
  4520. (let* ((n org-cycle-separator-lines)
  4521. (re (cond
  4522. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4523. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4524. (t (let ((ns (number-to-string (- n 2))))
  4525. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4526. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4527. beg end)
  4528. (cond
  4529. ((memq state '(overview contents t))
  4530. (setq beg (point-min) end (point-max)))
  4531. ((memq state '(children folded))
  4532. (setq beg (point) end (progn (org-end-of-subtree t t)
  4533. (beginning-of-line 2)
  4534. (point)))))
  4535. (when beg
  4536. (goto-char beg)
  4537. (while (re-search-forward re end t)
  4538. (if (not (get-char-property (match-end 1) 'invisible))
  4539. (outline-flag-region
  4540. (match-beginning 1) (match-end 1) nil)))))))
  4541. ;; Never hide empty lines at the end of the file.
  4542. (save-excursion
  4543. (goto-char (point-max))
  4544. (outline-previous-heading)
  4545. (outline-end-of-heading)
  4546. (if (and (looking-at "[ \t\n]+")
  4547. (= (match-end 0) (point-max)))
  4548. (outline-flag-region (point) (match-end 0) nil))))
  4549. (defun org-show-empty-lines-in-parent ()
  4550. "Move to the parent and re-show empty lines before visible headlines."
  4551. (save-excursion
  4552. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4553. (org-cycle-show-empty-lines context))))
  4554. (defun org-cycle-hide-drawers (state)
  4555. "Re-hide all drawers after a visibility state change."
  4556. (when (and (org-mode-p)
  4557. (not (memq state '(overview folded))))
  4558. (save-excursion
  4559. (let* ((globalp (memq state '(contents all)))
  4560. (beg (if globalp (point-min) (point)))
  4561. (end (if globalp (point-max) (org-end-of-subtree t))))
  4562. (goto-char beg)
  4563. (while (re-search-forward org-drawer-regexp end t)
  4564. (org-flag-drawer t))))))
  4565. (defun org-flag-drawer (flag)
  4566. (save-excursion
  4567. (beginning-of-line 1)
  4568. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4569. (let ((b (match-end 0))
  4570. (outline-regexp org-outline-regexp))
  4571. (if (re-search-forward
  4572. "^[ \t]*:END:"
  4573. (save-excursion (outline-next-heading) (point)) t)
  4574. (outline-flag-region b (point-at-eol) flag)
  4575. (error ":END: line missing"))))))
  4576. (defun org-subtree-end-visible-p ()
  4577. "Is the end of the current subtree visible?"
  4578. (pos-visible-in-window-p
  4579. (save-excursion (org-end-of-subtree t) (point))))
  4580. (defun org-first-headline-recenter (&optional N)
  4581. "Move cursor to the first headline and recenter the headline.
  4582. Optional argument N means, put the headline into the Nth line of the window."
  4583. (goto-char (point-min))
  4584. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4585. (beginning-of-line)
  4586. (recenter (prefix-numeric-value N))))
  4587. ;;; Folding of blocks
  4588. (defconst org-block-regexp
  4589. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4590. "Regular expression for hiding blocks.")
  4591. (defvar org-hide-block-overlays nil
  4592. "Overays hiding blocks.")
  4593. (make-variable-buffer-local 'org-hide-block-overlays)
  4594. (defun org-block-map (function &optional start end)
  4595. "Call func at the head of all source blocks in the current
  4596. buffer. Optional arguments START and END can be used to limit
  4597. the range."
  4598. (let ((start (or start (point-min)))
  4599. (end (or end (point-max))))
  4600. (save-excursion
  4601. (goto-char start)
  4602. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4603. (save-excursion
  4604. (save-match-data
  4605. (goto-char (match-beginning 0))
  4606. (funcall function)))))))
  4607. (defun org-hide-block-toggle-all ()
  4608. "Toggle the visibility of all blocks in the current buffer."
  4609. (org-block-map #'org-hide-block-toggle))
  4610. (defun org-hide-block-all ()
  4611. "Fold all blocks in the current buffer."
  4612. (interactive)
  4613. (org-show-block-all)
  4614. (org-block-map #'org-hide-block-toggle-maybe))
  4615. (defun org-show-block-all ()
  4616. "Unfold all blocks in the current buffer."
  4617. (mapc 'org-delete-overlay org-hide-block-overlays)
  4618. (setq org-hide-block-overlays nil))
  4619. (defun org-hide-block-toggle-maybe ()
  4620. "Toggle visibility of block at point."
  4621. (interactive)
  4622. (let ((case-fold-search t))
  4623. (if (save-excursion
  4624. (beginning-of-line 1)
  4625. (looking-at org-block-regexp))
  4626. (progn (org-hide-block-toggle)
  4627. t) ;; to signal that we took action
  4628. nil))) ;; to signal that we did not
  4629. (defun org-hide-block-toggle (&optional force)
  4630. "Toggle the visibility of the current block."
  4631. (interactive)
  4632. (save-excursion
  4633. (beginning-of-line)
  4634. (if (re-search-forward org-block-regexp nil t)
  4635. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4636. (end (match-end 0))
  4637. ov) ;; end of entire body
  4638. (if (memq t (mapcar (lambda (overlay)
  4639. (eq (org-overlay-get overlay 'invisible)
  4640. 'org-hide-block))
  4641. (org-overlays-at start)))
  4642. (if (or (not force) (eq force 'off))
  4643. (mapc (lambda (ov)
  4644. (when (member ov org-hide-block-overlays)
  4645. (setq org-hide-block-overlays
  4646. (delq ov org-hide-block-overlays)))
  4647. (when (eq (org-overlay-get ov 'invisible)
  4648. 'org-hide-block)
  4649. (org-delete-overlay ov)))
  4650. (org-overlays-at start)))
  4651. (setq ov (make-overlay start end))
  4652. (org-overlay-put ov 'invisible 'org-hide-block)
  4653. (push ov org-hide-block-overlays)))
  4654. (error "Not looking at a source block"))))
  4655. ;; org-tab-after-check-for-cycling-hook
  4656. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4657. ;; Remove overlays when changing major mode
  4658. (add-hook 'org-mode-hook
  4659. (lambda () (org-add-hook 'change-major-mode-hook
  4660. 'org-show-block-all 'append 'local)))
  4661. ;;; Org-goto
  4662. (defvar org-goto-window-configuration nil)
  4663. (defvar org-goto-marker nil)
  4664. (defvar org-goto-map
  4665. (let ((map (make-sparse-keymap)))
  4666. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4667. (while (setq cmd (pop cmds))
  4668. (substitute-key-definition cmd cmd map global-map)))
  4669. (suppress-keymap map)
  4670. (org-defkey map "\C-m" 'org-goto-ret)
  4671. (org-defkey map [(return)] 'org-goto-ret)
  4672. (org-defkey map [(left)] 'org-goto-left)
  4673. (org-defkey map [(right)] 'org-goto-right)
  4674. (org-defkey map [(control ?g)] 'org-goto-quit)
  4675. (org-defkey map "\C-i" 'org-cycle)
  4676. (org-defkey map [(tab)] 'org-cycle)
  4677. (org-defkey map [(down)] 'outline-next-visible-heading)
  4678. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4679. (if org-goto-auto-isearch
  4680. (if (fboundp 'define-key-after)
  4681. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4682. nil)
  4683. (org-defkey map "q" 'org-goto-quit)
  4684. (org-defkey map "n" 'outline-next-visible-heading)
  4685. (org-defkey map "p" 'outline-previous-visible-heading)
  4686. (org-defkey map "f" 'outline-forward-same-level)
  4687. (org-defkey map "b" 'outline-backward-same-level)
  4688. (org-defkey map "u" 'outline-up-heading))
  4689. (org-defkey map "/" 'org-occur)
  4690. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4691. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4692. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4693. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4694. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4695. map))
  4696. (defconst org-goto-help
  4697. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4698. RET=jump to location [Q]uit and return to previous location
  4699. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4700. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4701. ;; FIXME: Docstring doe not mention both interfaces
  4702. (defun org-goto (&optional alternative-interface)
  4703. "Look up a different location in the current file, keeping current visibility.
  4704. When you want look-up or go to a different location in a document, the
  4705. fastest way is often to fold the entire buffer and then dive into the tree.
  4706. This method has the disadvantage, that the previous location will be folded,
  4707. which may not be what you want.
  4708. This command works around this by showing a copy of the current buffer
  4709. in an indirect buffer, in overview mode. You can dive into the tree in
  4710. that copy, use org-occur and incremental search to find a location.
  4711. When pressing RET or `Q', the command returns to the original buffer in
  4712. which the visibility is still unchanged. After RET is will also jump to
  4713. the location selected in the indirect buffer and expose the
  4714. the headline hierarchy above."
  4715. (interactive "P")
  4716. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4717. (org-refile-use-outline-path t)
  4718. (org-refile-target-verify-function nil)
  4719. (interface
  4720. (if (not alternative-interface)
  4721. org-goto-interface
  4722. (if (eq org-goto-interface 'outline)
  4723. 'outline-path-completion
  4724. 'outline)))
  4725. (org-goto-start-pos (point))
  4726. (selected-point
  4727. (if (eq interface 'outline)
  4728. (car (org-get-location (current-buffer) org-goto-help))
  4729. (nth 3 (org-refile-get-location "Goto: ")))))
  4730. (if selected-point
  4731. (progn
  4732. (org-mark-ring-push org-goto-start-pos)
  4733. (goto-char selected-point)
  4734. (if (or (org-invisible-p) (org-invisible-p2))
  4735. (org-show-context 'org-goto)))
  4736. (message "Quit"))))
  4737. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4738. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4739. (defvar org-goto-local-auto-isearch-map) ; defined below
  4740. (defun org-get-location (buf help)
  4741. "Let the user select a location in the Org-mode buffer BUF.
  4742. This function uses a recursive edit. It returns the selected position
  4743. or nil."
  4744. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4745. (isearch-hide-immediately nil)
  4746. (isearch-search-fun-function
  4747. (lambda () 'org-goto-local-search-headings))
  4748. (org-goto-selected-point org-goto-exit-command))
  4749. (save-excursion
  4750. (save-window-excursion
  4751. (delete-other-windows)
  4752. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4753. (switch-to-buffer
  4754. (condition-case nil
  4755. (make-indirect-buffer (current-buffer) "*org-goto*")
  4756. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4757. (with-output-to-temp-buffer "*Help*"
  4758. (princ help))
  4759. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4760. (setq buffer-read-only nil)
  4761. (let ((org-startup-truncated t)
  4762. (org-startup-folded nil)
  4763. (org-startup-align-all-tables nil))
  4764. (org-mode)
  4765. (org-overview))
  4766. (setq buffer-read-only t)
  4767. (if (and (boundp 'org-goto-start-pos)
  4768. (integer-or-marker-p org-goto-start-pos))
  4769. (let ((org-show-hierarchy-above t)
  4770. (org-show-siblings t)
  4771. (org-show-following-heading t))
  4772. (goto-char org-goto-start-pos)
  4773. (and (org-invisible-p) (org-show-context)))
  4774. (goto-char (point-min)))
  4775. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4776. (message "Select location and press RET")
  4777. (use-local-map org-goto-map)
  4778. (recursive-edit)
  4779. ))
  4780. (kill-buffer "*org-goto*")
  4781. (cons org-goto-selected-point org-goto-exit-command)))
  4782. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4783. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4784. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4785. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4786. (defun org-goto-local-search-headings (string bound noerror)
  4787. "Search and make sure that any matches are in headlines."
  4788. (catch 'return
  4789. (while (if isearch-forward
  4790. (search-forward string bound noerror)
  4791. (search-backward string bound noerror))
  4792. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4793. (and (member :headline context)
  4794. (not (member :tags context))))
  4795. (throw 'return (point))))))
  4796. (defun org-goto-local-auto-isearch ()
  4797. "Start isearch."
  4798. (interactive)
  4799. (goto-char (point-min))
  4800. (let ((keys (this-command-keys)))
  4801. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4802. (isearch-mode t)
  4803. (isearch-process-search-char (string-to-char keys)))))
  4804. (defun org-goto-ret (&optional arg)
  4805. "Finish `org-goto' by going to the new location."
  4806. (interactive "P")
  4807. (setq org-goto-selected-point (point)
  4808. org-goto-exit-command 'return)
  4809. (throw 'exit nil))
  4810. (defun org-goto-left ()
  4811. "Finish `org-goto' by going to the new location."
  4812. (interactive)
  4813. (if (org-on-heading-p)
  4814. (progn
  4815. (beginning-of-line 1)
  4816. (setq org-goto-selected-point (point)
  4817. org-goto-exit-command 'left)
  4818. (throw 'exit nil))
  4819. (error "Not on a heading")))
  4820. (defun org-goto-right ()
  4821. "Finish `org-goto' by going to the new location."
  4822. (interactive)
  4823. (if (org-on-heading-p)
  4824. (progn
  4825. (setq org-goto-selected-point (point)
  4826. org-goto-exit-command 'right)
  4827. (throw 'exit nil))
  4828. (error "Not on a heading")))
  4829. (defun org-goto-quit ()
  4830. "Finish `org-goto' without cursor motion."
  4831. (interactive)
  4832. (setq org-goto-selected-point nil)
  4833. (setq org-goto-exit-command 'quit)
  4834. (throw 'exit nil))
  4835. ;;; Indirect buffer display of subtrees
  4836. (defvar org-indirect-dedicated-frame nil
  4837. "This is the frame being used for indirect tree display.")
  4838. (defvar org-last-indirect-buffer nil)
  4839. (defun org-tree-to-indirect-buffer (&optional arg)
  4840. "Create indirect buffer and narrow it to current subtree.
  4841. With numerical prefix ARG, go up to this level and then take that tree.
  4842. If ARG is negative, go up that many levels.
  4843. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4844. indirect buffer previously made with this command, to avoid proliferation of
  4845. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4846. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4847. is kept so that you can work with several indirect buffers at the same time.
  4848. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4849. requests that a new frame be made for the new buffer, so that the dedicated
  4850. frame is not changed."
  4851. (interactive "P")
  4852. (let ((cbuf (current-buffer))
  4853. (cwin (selected-window))
  4854. (pos (point))
  4855. beg end level heading ibuf)
  4856. (save-excursion
  4857. (org-back-to-heading t)
  4858. (when (numberp arg)
  4859. (setq level (org-outline-level))
  4860. (if (< arg 0) (setq arg (+ level arg)))
  4861. (while (> (setq level (org-outline-level)) arg)
  4862. (outline-up-heading 1 t)))
  4863. (setq beg (point)
  4864. heading (org-get-heading))
  4865. (org-end-of-subtree t) (setq end (point)))
  4866. (if (and (buffer-live-p org-last-indirect-buffer)
  4867. (not (eq org-indirect-buffer-display 'new-frame))
  4868. (not arg))
  4869. (kill-buffer org-last-indirect-buffer))
  4870. (setq ibuf (org-get-indirect-buffer cbuf)
  4871. org-last-indirect-buffer ibuf)
  4872. (cond
  4873. ((or (eq org-indirect-buffer-display 'new-frame)
  4874. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4875. (select-frame (make-frame))
  4876. (delete-other-windows)
  4877. (switch-to-buffer ibuf)
  4878. (org-set-frame-title heading))
  4879. ((eq org-indirect-buffer-display 'dedicated-frame)
  4880. (raise-frame
  4881. (select-frame (or (and org-indirect-dedicated-frame
  4882. (frame-live-p org-indirect-dedicated-frame)
  4883. org-indirect-dedicated-frame)
  4884. (setq org-indirect-dedicated-frame (make-frame)))))
  4885. (delete-other-windows)
  4886. (switch-to-buffer ibuf)
  4887. (org-set-frame-title (concat "Indirect: " heading)))
  4888. ((eq org-indirect-buffer-display 'current-window)
  4889. (switch-to-buffer ibuf))
  4890. ((eq org-indirect-buffer-display 'other-window)
  4891. (pop-to-buffer ibuf))
  4892. (t (error "Invalid value.")))
  4893. (if (featurep 'xemacs)
  4894. (save-excursion (org-mode) (turn-on-font-lock)))
  4895. (narrow-to-region beg end)
  4896. (show-all)
  4897. (goto-char pos)
  4898. (and (window-live-p cwin) (select-window cwin))))
  4899. (defun org-get-indirect-buffer (&optional buffer)
  4900. (setq buffer (or buffer (current-buffer)))
  4901. (let ((n 1) (base (buffer-name buffer)) bname)
  4902. (while (buffer-live-p
  4903. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4904. (setq n (1+ n)))
  4905. (condition-case nil
  4906. (make-indirect-buffer buffer bname 'clone)
  4907. (error (make-indirect-buffer buffer bname)))))
  4908. (defun org-set-frame-title (title)
  4909. "Set the title of the current frame to the string TITLE."
  4910. ;; FIXME: how to name a single frame in XEmacs???
  4911. (unless (featurep 'xemacs)
  4912. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4913. ;;;; Structure editing
  4914. ;;; Inserting headlines
  4915. (defun org-previous-line-empty-p ()
  4916. (save-excursion
  4917. (and (not (bobp))
  4918. (or (beginning-of-line 0) t)
  4919. (save-match-data
  4920. (looking-at "[ \t]*$")))))
  4921. (defun org-insert-heading (&optional force-heading)
  4922. "Insert a new heading or item with same depth at point.
  4923. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4924. If point is at the beginning of a headline, insert a sibling before the
  4925. current headline. If point is not at the beginning, do not split the line,
  4926. but create the new headline after the current line."
  4927. (interactive "P")
  4928. (if (= (buffer-size) 0)
  4929. (insert "\n* ")
  4930. (when (or force-heading (not (org-insert-item)))
  4931. (let* ((empty-line-p nil)
  4932. (head (save-excursion
  4933. (condition-case nil
  4934. (progn
  4935. (org-back-to-heading)
  4936. (setq empty-line-p (org-previous-line-empty-p))
  4937. (match-string 0))
  4938. (error "*"))))
  4939. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4940. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4941. pos hide-previous previous-pos)
  4942. (cond
  4943. ((and (org-on-heading-p) (bolp)
  4944. (or (bobp)
  4945. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4946. ;; insert before the current line
  4947. (open-line (if blank 2 1)))
  4948. ((and (bolp)
  4949. (or (bobp)
  4950. (save-excursion
  4951. (backward-char 1) (not (org-invisible-p)))))
  4952. ;; insert right here
  4953. nil)
  4954. (t
  4955. ;; somewhere in the line
  4956. (save-excursion
  4957. (setq previous-pos (point-at-bol))
  4958. (end-of-line)
  4959. (setq hide-previous (org-invisible-p)))
  4960. (and org-insert-heading-respect-content (org-show-subtree))
  4961. (let ((split
  4962. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4963. (save-excursion
  4964. (let ((p (point)))
  4965. (goto-char (point-at-bol))
  4966. (and (looking-at org-complex-heading-regexp)
  4967. (> p (match-beginning 4)))))))
  4968. tags pos)
  4969. (cond
  4970. (org-insert-heading-respect-content
  4971. (org-end-of-subtree nil t)
  4972. (or (bolp) (newline))
  4973. (or (org-previous-line-empty-p)
  4974. (and blank (newline)))
  4975. (open-line 1))
  4976. ((org-on-heading-p)
  4977. (when hide-previous
  4978. (show-children)
  4979. (org-show-entry))
  4980. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4981. (setq tags (and (match-end 2) (match-string 2)))
  4982. (and (match-end 1)
  4983. (delete-region (match-beginning 1) (match-end 1)))
  4984. (setq pos (point-at-bol))
  4985. (or split (end-of-line 1))
  4986. (delete-horizontal-space)
  4987. (newline (if blank 2 1))
  4988. (when tags
  4989. (save-excursion
  4990. (goto-char pos)
  4991. (end-of-line 1)
  4992. (insert " " tags)
  4993. (org-set-tags nil 'align))))
  4994. (t
  4995. (or split (end-of-line 1))
  4996. (newline (if blank 2 1)))))))
  4997. (insert head) (just-one-space)
  4998. (setq pos (point))
  4999. (end-of-line 1)
  5000. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5001. (when (and org-insert-heading-respect-content hide-previous)
  5002. (save-excursion
  5003. (goto-char previous-pos)
  5004. (hide-subtree)))
  5005. (run-hooks 'org-insert-heading-hook)))))
  5006. (defun org-get-heading (&optional no-tags)
  5007. "Return the heading of the current entry, without the stars."
  5008. (save-excursion
  5009. (org-back-to-heading t)
  5010. (if (looking-at
  5011. (if no-tags
  5012. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5013. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5014. (match-string 1) "")))
  5015. (defun org-heading-components ()
  5016. "Return the components of the current heading.
  5017. This is a list with the following elements:
  5018. - the level as an integer
  5019. - the reduced level, different if `org-odd-levels-only' is set.
  5020. - the TODO keyword, or nil
  5021. - the priority character, like ?A, or nil if no priority is given
  5022. - the headline text itself, or the tags string if no headline text
  5023. - the tags string, or nil."
  5024. (save-excursion
  5025. (org-back-to-heading t)
  5026. (if (looking-at org-complex-heading-regexp)
  5027. (list (length (match-string 1))
  5028. (org-reduced-level (length (match-string 1)))
  5029. (org-match-string-no-properties 2)
  5030. (and (match-end 3) (aref (match-string 3) 2))
  5031. (org-match-string-no-properties 4)
  5032. (org-match-string-no-properties 5)))))
  5033. (defun org-get-entry ()
  5034. "Get the entry text, after heading, entire subtree."
  5035. (save-excursion
  5036. (org-back-to-heading t)
  5037. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5038. (defun org-insert-heading-after-current ()
  5039. "Insert a new heading with same level as current, after current subtree."
  5040. (interactive)
  5041. (org-back-to-heading)
  5042. (org-insert-heading)
  5043. (org-move-subtree-down)
  5044. (end-of-line 1))
  5045. (defun org-insert-heading-respect-content ()
  5046. (interactive)
  5047. (let ((org-insert-heading-respect-content t))
  5048. (org-insert-heading t)))
  5049. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5050. (interactive "P")
  5051. (let ((org-insert-heading-respect-content t))
  5052. (org-insert-todo-heading force-state t)))
  5053. (defun org-insert-todo-heading (arg &optional force-heading)
  5054. "Insert a new heading with the same level and TODO state as current heading.
  5055. If the heading has no TODO state, or if the state is DONE, use the first
  5056. state (TODO by default). Also with prefix arg, force first state."
  5057. (interactive "P")
  5058. (when (or force-heading (not (org-insert-item 'checkbox)))
  5059. (org-insert-heading force-heading)
  5060. (save-excursion
  5061. (org-back-to-heading)
  5062. (outline-previous-heading)
  5063. (looking-at org-todo-line-regexp))
  5064. (let*
  5065. ((new-mark-x
  5066. (if (or arg
  5067. (not (match-beginning 2))
  5068. (member (match-string 2) org-done-keywords))
  5069. (car org-todo-keywords-1)
  5070. (match-string 2)))
  5071. (new-mark
  5072. (or
  5073. (run-hook-with-args-until-success
  5074. 'org-todo-get-default-hook new-mark-x nil)
  5075. new-mark-x)))
  5076. (beginning-of-line 1)
  5077. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5078. (if org-treat-insert-todo-heading-as-state-change
  5079. (org-todo new-mark)
  5080. (insert new-mark " "))))
  5081. (when org-provide-todo-statistics
  5082. (org-update-parent-todo-statistics))))
  5083. (defun org-insert-subheading (arg)
  5084. "Insert a new subheading and demote it.
  5085. Works for outline headings and for plain lists alike."
  5086. (interactive "P")
  5087. (org-insert-heading arg)
  5088. (cond
  5089. ((org-on-heading-p) (org-do-demote))
  5090. ((org-at-item-p) (org-indent-item 1))))
  5091. (defun org-insert-todo-subheading (arg)
  5092. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5093. Works for outline headings and for plain lists alike."
  5094. (interactive "P")
  5095. (org-insert-todo-heading arg)
  5096. (cond
  5097. ((org-on-heading-p) (org-do-demote))
  5098. ((org-at-item-p) (org-indent-item 1))))
  5099. ;;; Promotion and Demotion
  5100. (defun org-promote-subtree ()
  5101. "Promote the entire subtree.
  5102. See also `org-promote'."
  5103. (interactive)
  5104. (save-excursion
  5105. (org-map-tree 'org-promote))
  5106. (org-fix-position-after-promote))
  5107. (defun org-demote-subtree ()
  5108. "Demote the entire subtree. See `org-demote'.
  5109. See also `org-promote'."
  5110. (interactive)
  5111. (save-excursion
  5112. (org-map-tree 'org-demote))
  5113. (org-fix-position-after-promote))
  5114. (defun org-do-promote ()
  5115. "Promote the current heading higher up the tree.
  5116. If the region is active in `transient-mark-mode', promote all headings
  5117. in the region."
  5118. (interactive)
  5119. (save-excursion
  5120. (if (org-region-active-p)
  5121. (org-map-region 'org-promote (region-beginning) (region-end))
  5122. (org-promote)))
  5123. (org-fix-position-after-promote))
  5124. (defun org-do-demote ()
  5125. "Demote the current heading lower down the tree.
  5126. If the region is active in `transient-mark-mode', demote all headings
  5127. in the region."
  5128. (interactive)
  5129. (save-excursion
  5130. (if (org-region-active-p)
  5131. (org-map-region 'org-demote (region-beginning) (region-end))
  5132. (org-demote)))
  5133. (org-fix-position-after-promote))
  5134. (defun org-fix-position-after-promote ()
  5135. "Make sure that after pro/demotion cursor position is right."
  5136. (let ((pos (point)))
  5137. (when (save-excursion
  5138. (beginning-of-line 1)
  5139. (looking-at org-todo-line-regexp)
  5140. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5141. (cond ((eobp) (insert " "))
  5142. ((eolp) (insert " "))
  5143. ((equal (char-after) ?\ ) (forward-char 1))))))
  5144. (defun org-reduced-level (l)
  5145. "Compute the effective level of a heading.
  5146. This takes into account the setting of `org-odd-levels-only'."
  5147. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5148. (defun org-get-valid-level (level &optional change)
  5149. "Rectify a level change under the influence of `org-odd-levels-only'
  5150. LEVEL is a current level, CHANGE is by how much the level should be
  5151. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5152. even level numbers will become the next higher odd number."
  5153. (if org-odd-levels-only
  5154. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5155. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5156. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5157. (max 1 (+ level (or change 0)))))
  5158. (if (boundp 'define-obsolete-function-alias)
  5159. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5160. (define-obsolete-function-alias 'org-get-legal-level
  5161. 'org-get-valid-level)
  5162. (define-obsolete-function-alias 'org-get-legal-level
  5163. 'org-get-valid-level "23.1")))
  5164. (defun org-promote ()
  5165. "Promote the current heading higher up the tree.
  5166. If the region is active in `transient-mark-mode', promote all headings
  5167. in the region."
  5168. (org-back-to-heading t)
  5169. (let* ((level (save-match-data (funcall outline-level)))
  5170. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5171. (diff (abs (- level (length up-head) -1))))
  5172. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5173. (replace-match up-head nil t)
  5174. ;; Fixup tag positioning
  5175. (and org-auto-align-tags (org-set-tags nil t))
  5176. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5177. (defun org-demote ()
  5178. "Demote the current heading lower down the tree.
  5179. If the region is active in `transient-mark-mode', demote all headings
  5180. in the region."
  5181. (org-back-to-heading t)
  5182. (let* ((level (save-match-data (funcall outline-level)))
  5183. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5184. (diff (abs (- level (length down-head) -1))))
  5185. (replace-match down-head nil t)
  5186. ;; Fixup tag positioning
  5187. (and org-auto-align-tags (org-set-tags nil t))
  5188. (if org-adapt-indentation (org-fixup-indentation diff))))
  5189. (defun org-map-tree (fun)
  5190. "Call FUN for every heading underneath the current one."
  5191. (org-back-to-heading)
  5192. (let ((level (funcall outline-level)))
  5193. (save-excursion
  5194. (funcall fun)
  5195. (while (and (progn
  5196. (outline-next-heading)
  5197. (> (funcall outline-level) level))
  5198. (not (eobp)))
  5199. (funcall fun)))))
  5200. (defun org-map-region (fun beg end)
  5201. "Call FUN for every heading between BEG and END."
  5202. (let ((org-ignore-region t))
  5203. (save-excursion
  5204. (setq end (copy-marker end))
  5205. (goto-char beg)
  5206. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5207. (< (point) end))
  5208. (funcall fun))
  5209. (while (and (progn
  5210. (outline-next-heading)
  5211. (< (point) end))
  5212. (not (eobp)))
  5213. (funcall fun)))))
  5214. (defun org-fixup-indentation (diff)
  5215. "Change the indentation in the current entry by DIFF
  5216. However, if any line in the current entry has no indentation, or if it
  5217. would end up with no indentation after the change, nothing at all is done."
  5218. (save-excursion
  5219. (let ((end (save-excursion (outline-next-heading)
  5220. (point-marker)))
  5221. (prohibit (if (> diff 0)
  5222. "^\\S-"
  5223. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5224. col)
  5225. (unless (save-excursion (end-of-line 1)
  5226. (re-search-forward prohibit end t))
  5227. (while (and (< (point) end)
  5228. (re-search-forward "^[ \t]+" end t))
  5229. (goto-char (match-end 0))
  5230. (setq col (current-column))
  5231. (if (< diff 0) (replace-match ""))
  5232. (org-indent-to-column (+ diff col))))
  5233. (move-marker end nil))))
  5234. (defun org-convert-to-odd-levels ()
  5235. "Convert an org-mode file with all levels allowed to one with odd levels.
  5236. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5237. level 5 etc."
  5238. (interactive)
  5239. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5240. (let ((org-odd-levels-only nil) n)
  5241. (save-excursion
  5242. (goto-char (point-min))
  5243. (while (re-search-forward "^\\*\\*+ " nil t)
  5244. (setq n (- (length (match-string 0)) 2))
  5245. (while (>= (setq n (1- n)) 0)
  5246. (org-demote))
  5247. (end-of-line 1))))))
  5248. (defun org-convert-to-oddeven-levels ()
  5249. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5250. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5251. section with an even level, conversion would destroy the structure of the file. An error
  5252. is signaled in this case."
  5253. (interactive)
  5254. (goto-char (point-min))
  5255. ;; First check if there are no even levels
  5256. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5257. (org-show-context t)
  5258. (error "Not all levels are odd in this file. Conversion not possible."))
  5259. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5260. (let ((org-odd-levels-only nil) n)
  5261. (save-excursion
  5262. (goto-char (point-min))
  5263. (while (re-search-forward "^\\*\\*+ " nil t)
  5264. (setq n (/ (1- (length (match-string 0))) 2))
  5265. (while (>= (setq n (1- n)) 0)
  5266. (org-promote))
  5267. (end-of-line 1))))))
  5268. (defun org-tr-level (n)
  5269. "Make N odd if required."
  5270. (if org-odd-levels-only (1+ (/ n 2)) n))
  5271. ;;; Vertical tree motion, cutting and pasting of subtrees
  5272. (defun org-move-subtree-up (&optional arg)
  5273. "Move the current subtree up past ARG headlines of the same level."
  5274. (interactive "p")
  5275. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5276. (defun org-move-subtree-down (&optional arg)
  5277. "Move the current subtree down past ARG headlines of the same level."
  5278. (interactive "p")
  5279. (setq arg (prefix-numeric-value arg))
  5280. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5281. 'outline-get-last-sibling))
  5282. (ins-point (make-marker))
  5283. (cnt (abs arg))
  5284. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5285. ;; Select the tree
  5286. (org-back-to-heading)
  5287. (setq beg0 (point))
  5288. (save-excursion
  5289. (setq ne-beg (org-back-over-empty-lines))
  5290. (setq beg (point)))
  5291. (save-match-data
  5292. (save-excursion (outline-end-of-heading)
  5293. (setq folded (org-invisible-p)))
  5294. (outline-end-of-subtree))
  5295. (outline-next-heading)
  5296. (setq ne-end (org-back-over-empty-lines))
  5297. (setq end (point))
  5298. (goto-char beg0)
  5299. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5300. ;; include less whitespace
  5301. (save-excursion
  5302. (goto-char beg)
  5303. (forward-line (- ne-beg ne-end))
  5304. (setq beg (point))))
  5305. ;; Find insertion point, with error handling
  5306. (while (> cnt 0)
  5307. (or (and (funcall movfunc) (looking-at outline-regexp))
  5308. (progn (goto-char beg0)
  5309. (error "Cannot move past superior level or buffer limit")))
  5310. (setq cnt (1- cnt)))
  5311. (if (> arg 0)
  5312. ;; Moving forward - still need to move over subtree
  5313. (progn (org-end-of-subtree t t)
  5314. (save-excursion
  5315. (org-back-over-empty-lines)
  5316. (or (bolp) (newline)))))
  5317. (setq ne-ins (org-back-over-empty-lines))
  5318. (move-marker ins-point (point))
  5319. (setq txt (buffer-substring beg end))
  5320. (org-save-markers-in-region beg end)
  5321. (delete-region beg end)
  5322. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5323. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5324. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5325. (let ((bbb (point)))
  5326. (insert-before-markers txt)
  5327. (org-reinstall-markers-in-region bbb)
  5328. (move-marker ins-point bbb))
  5329. (or (bolp) (insert "\n"))
  5330. (setq ins-end (point))
  5331. (goto-char ins-point)
  5332. (org-skip-whitespace)
  5333. (when (and (< arg 0)
  5334. (org-first-sibling-p)
  5335. (> ne-ins ne-beg))
  5336. ;; Move whitespace back to beginning
  5337. (save-excursion
  5338. (goto-char ins-end)
  5339. (let ((kill-whole-line t))
  5340. (kill-line (- ne-ins ne-beg)) (point)))
  5341. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5342. (move-marker ins-point nil)
  5343. (org-compact-display-after-subtree-move)
  5344. (org-show-empty-lines-in-parent)
  5345. (unless folded
  5346. (org-show-entry)
  5347. (show-children)
  5348. (org-cycle-hide-drawers 'children))))
  5349. (defvar org-subtree-clip ""
  5350. "Clipboard for cut and paste of subtrees.
  5351. This is actually only a copy of the kill, because we use the normal kill
  5352. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5353. (defvar org-subtree-clip-folded nil
  5354. "Was the last copied subtree folded?
  5355. This is used to fold the tree back after pasting.")
  5356. (defun org-cut-subtree (&optional n)
  5357. "Cut the current subtree into the clipboard.
  5358. With prefix arg N, cut this many sequential subtrees.
  5359. This is a short-hand for marking the subtree and then cutting it."
  5360. (interactive "p")
  5361. (org-copy-subtree n 'cut))
  5362. (defun org-copy-subtree (&optional n cut force-store-markers)
  5363. "Cut the current subtree into the clipboard.
  5364. With prefix arg N, cut this many sequential subtrees.
  5365. This is a short-hand for marking the subtree and then copying it.
  5366. If CUT is non-nil, actually cut the subtree.
  5367. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5368. of some markers in the region, even if CUT is non-nil. This is
  5369. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5370. (interactive "p")
  5371. (let (beg end folded (beg0 (point)))
  5372. (if (interactive-p)
  5373. (org-back-to-heading nil) ; take what looks like a subtree
  5374. (org-back-to-heading t)) ; take what is really there
  5375. (org-back-over-empty-lines)
  5376. (setq beg (point))
  5377. (skip-chars-forward " \t\r\n")
  5378. (save-match-data
  5379. (save-excursion (outline-end-of-heading)
  5380. (setq folded (org-invisible-p)))
  5381. (condition-case nil
  5382. (outline-forward-same-level (1- n))
  5383. (error nil))
  5384. (org-end-of-subtree t t))
  5385. (org-back-over-empty-lines)
  5386. (setq end (point))
  5387. (goto-char beg0)
  5388. (when (> end beg)
  5389. (setq org-subtree-clip-folded folded)
  5390. (when (or cut force-store-markers)
  5391. (org-save-markers-in-region beg end))
  5392. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5393. (setq org-subtree-clip (current-kill 0))
  5394. (message "%s: Subtree(s) with %d characters"
  5395. (if cut "Cut" "Copied")
  5396. (length org-subtree-clip)))))
  5397. (defun org-paste-subtree (&optional level tree for-yank)
  5398. "Paste the clipboard as a subtree, with modification of headline level.
  5399. The entire subtree is promoted or demoted in order to match a new headline
  5400. level.
  5401. If the cursor is at the beginning of a headline, the same level as
  5402. that headline is used to paste the tree
  5403. If not, the new level is derived from the *visible* headings
  5404. before and after the insertion point, and taken to be the inferior headline
  5405. level of the two. So if the previous visible heading is level 3 and the
  5406. next is level 4 (or vice versa), level 4 will be used for insertion.
  5407. This makes sure that the subtree remains an independent subtree and does
  5408. not swallow low level entries.
  5409. You can also force a different level, either by using a numeric prefix
  5410. argument, or by inserting the heading marker by hand. For example, if the
  5411. cursor is after \"*****\", then the tree will be shifted to level 5.
  5412. If optional TREE is given, use this text instead of the kill ring.
  5413. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5414. move back over whitespace before inserting, and move point to the end of
  5415. the inserted text when done."
  5416. (interactive "P")
  5417. (unless (org-kill-is-subtree-p tree)
  5418. (error "%s"
  5419. (substitute-command-keys
  5420. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5421. (let* ((visp (not (org-invisible-p)))
  5422. (txt (or tree (and kill-ring (current-kill 0))))
  5423. (^re (concat "^\\(" outline-regexp "\\)"))
  5424. (re (concat "\\(" outline-regexp "\\)"))
  5425. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5426. (old-level (if (string-match ^re txt)
  5427. (- (match-end 0) (match-beginning 0) 1)
  5428. -1))
  5429. (force-level (cond (level (prefix-numeric-value level))
  5430. ((and (looking-at "[ \t]*$")
  5431. (string-match
  5432. ^re_ (buffer-substring
  5433. (point-at-bol) (point))))
  5434. (- (match-end 1) (match-beginning 1)))
  5435. ((and (bolp)
  5436. (looking-at org-outline-regexp))
  5437. (- (match-end 0) (point) 1))
  5438. (t nil)))
  5439. (previous-level (save-excursion
  5440. (condition-case nil
  5441. (progn
  5442. (outline-previous-visible-heading 1)
  5443. (if (looking-at re)
  5444. (- (match-end 0) (match-beginning 0) 1)
  5445. 1))
  5446. (error 1))))
  5447. (next-level (save-excursion
  5448. (condition-case nil
  5449. (progn
  5450. (or (looking-at outline-regexp)
  5451. (outline-next-visible-heading 1))
  5452. (if (looking-at re)
  5453. (- (match-end 0) (match-beginning 0) 1)
  5454. 1))
  5455. (error 1))))
  5456. (new-level (or force-level (max previous-level next-level)))
  5457. (shift (if (or (= old-level -1)
  5458. (= new-level -1)
  5459. (= old-level new-level))
  5460. 0
  5461. (- new-level old-level)))
  5462. (delta (if (> shift 0) -1 1))
  5463. (func (if (> shift 0) 'org-demote 'org-promote))
  5464. (org-odd-levels-only nil)
  5465. beg end newend)
  5466. ;; Remove the forced level indicator
  5467. (if force-level
  5468. (delete-region (point-at-bol) (point)))
  5469. ;; Paste
  5470. (beginning-of-line 1)
  5471. (unless for-yank (org-back-over-empty-lines))
  5472. (setq beg (point))
  5473. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5474. (insert-before-markers txt)
  5475. (unless (string-match "\n\\'" txt) (insert "\n"))
  5476. (setq newend (point))
  5477. (org-reinstall-markers-in-region beg)
  5478. (setq end (point))
  5479. (goto-char beg)
  5480. (skip-chars-forward " \t\n\r")
  5481. (setq beg (point))
  5482. (if (and (org-invisible-p) visp)
  5483. (save-excursion (outline-show-heading)))
  5484. ;; Shift if necessary
  5485. (unless (= shift 0)
  5486. (save-restriction
  5487. (narrow-to-region beg end)
  5488. (while (not (= shift 0))
  5489. (org-map-region func (point-min) (point-max))
  5490. (setq shift (+ delta shift)))
  5491. (goto-char (point-min))
  5492. (setq newend (point-max))))
  5493. (when (or (interactive-p) for-yank)
  5494. (message "Clipboard pasted as level %d subtree" new-level))
  5495. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5496. kill-ring
  5497. (eq org-subtree-clip (current-kill 0))
  5498. org-subtree-clip-folded)
  5499. ;; The tree was folded before it was killed/copied
  5500. (hide-subtree))
  5501. (and for-yank (goto-char newend))))
  5502. (defun org-kill-is-subtree-p (&optional txt)
  5503. "Check if the current kill is an outline subtree, or a set of trees.
  5504. Returns nil if kill does not start with a headline, or if the first
  5505. headline level is not the largest headline level in the tree.
  5506. So this will actually accept several entries of equal levels as well,
  5507. which is OK for `org-paste-subtree'.
  5508. If optional TXT is given, check this string instead of the current kill."
  5509. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5510. (start-level (and kill
  5511. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5512. org-outline-regexp "\\)")
  5513. kill)
  5514. (- (match-end 2) (match-beginning 2) 1)))
  5515. (re (concat "^" org-outline-regexp))
  5516. (start (1+ (or (match-beginning 2) -1))))
  5517. (if (not start-level)
  5518. (progn
  5519. nil) ;; does not even start with a heading
  5520. (catch 'exit
  5521. (while (setq start (string-match re kill (1+ start)))
  5522. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5523. (throw 'exit nil)))
  5524. t))))
  5525. (defvar org-markers-to-move nil
  5526. "Markers that should be moved with a cut-and-paste operation.
  5527. Those markers are stored together with their positions relative to
  5528. the start of the region.")
  5529. (defun org-save-markers-in-region (beg end)
  5530. "Check markers in region.
  5531. If these markers are between BEG and END, record their position relative
  5532. to BEG, so that after moving the block of text, we can put the markers back
  5533. into place.
  5534. This function gets called just before an entry or tree gets cut from the
  5535. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5536. called immediately, to move the markers with the entries."
  5537. (setq org-markers-to-move nil)
  5538. (when (featurep 'org-clock)
  5539. (org-clock-save-markers-for-cut-and-paste beg end))
  5540. (when (featurep 'org-agenda)
  5541. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5542. (defun org-check-and-save-marker (marker beg end)
  5543. "Check if MARKER is between BEG and END.
  5544. If yes, remember the marker and the distance to BEG."
  5545. (when (and (marker-buffer marker)
  5546. (equal (marker-buffer marker) (current-buffer)))
  5547. (if (and (>= marker beg) (< marker end))
  5548. (push (cons marker (- marker beg)) org-markers-to-move))))
  5549. (defun org-reinstall-markers-in-region (beg)
  5550. "Move all remembered markers to their position relative to BEG."
  5551. (mapc (lambda (x)
  5552. (move-marker (car x) (+ beg (cdr x))))
  5553. org-markers-to-move)
  5554. (setq org-markers-to-move nil))
  5555. (defun org-narrow-to-subtree ()
  5556. "Narrow buffer to the current subtree."
  5557. (interactive)
  5558. (save-excursion
  5559. (save-match-data
  5560. (narrow-to-region
  5561. (progn (org-back-to-heading) (point))
  5562. (progn (org-end-of-subtree t) (point))))))
  5563. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5564. "Clone the task (subtree) at point N times.
  5565. The clones will be inserted as siblings.
  5566. In interactive use, the user will be prompted for the number of clones
  5567. to be produced, and for a time SHIFT, which may be a repeater as used
  5568. in time stamps, for example `+3d'.
  5569. When a valid repeater is given and the entry contains any time stamps,
  5570. the clones will become a sequence in time, with time stamps in the
  5571. subtree shifted for each clone produced. If SHIFT is nil or the
  5572. empty string, time stamps will be left alone.
  5573. If the original subtree did contain time stamps with a repeater,
  5574. the following will happen:
  5575. - the repeater will be removed in each clone
  5576. - an additional clone will be produced, with the current, unshifted
  5577. date(s) in the entry.
  5578. - the original entry will be placed *after* all the clones, with
  5579. repeater intact.
  5580. - the start days in the repeater in the original entry will be shifted
  5581. to past the last clone.
  5582. I this way you can spell out a number of instances of a repeating task,
  5583. and still retain the repeater to cover future instances of the task."
  5584. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5585. (let (beg end template task
  5586. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5587. (if (not (and (integerp n) (> n 0)))
  5588. (error "Invalid number of replications %s" n))
  5589. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5590. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5591. shift)))
  5592. (error "Invalid shift specification %s" shift))
  5593. (when doshift
  5594. (setq shift-n (string-to-number (match-string 1 shift))
  5595. shift-what (cdr (assoc (match-string 2 shift)
  5596. '(("d" . day) ("w" . week)
  5597. ("m" . month) ("y" . year))))))
  5598. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5599. (setq nmin 1 nmax n)
  5600. (org-back-to-heading t)
  5601. (setq beg (point))
  5602. (org-end-of-subtree t t)
  5603. (setq end (point))
  5604. (setq template (buffer-substring beg end))
  5605. (when (and doshift
  5606. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5607. (delete-region beg end)
  5608. (setq end beg)
  5609. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5610. (goto-char end)
  5611. (loop for n from nmin to nmax do
  5612. (if (not doshift)
  5613. (setq task template)
  5614. (with-temp-buffer
  5615. (insert template)
  5616. (org-mode)
  5617. (goto-char (point-min))
  5618. (while (re-search-forward org-ts-regexp nil t)
  5619. (org-timestamp-change (* n shift-n) shift-what))
  5620. (unless (= n n-no-remove)
  5621. (goto-char (point-min))
  5622. (while (re-search-forward org-ts-regexp nil t)
  5623. (save-excursion
  5624. (goto-char (match-beginning 0))
  5625. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5626. (delete-region (match-beginning 1) (match-end 1))))))
  5627. (setq task (buffer-string))))
  5628. (insert task))
  5629. (goto-char beg)))
  5630. ;;; Outline Sorting
  5631. (defun org-sort (with-case)
  5632. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5633. Optional argument WITH-CASE means sort case-sensitively.
  5634. With a double prefix argument, also remove duplicate entries."
  5635. (interactive "P")
  5636. (if (org-at-table-p)
  5637. (org-call-with-arg 'org-table-sort-lines with-case)
  5638. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5639. (defun org-sort-remove-invisible (s)
  5640. (remove-text-properties 0 (length s) org-rm-props s)
  5641. (while (string-match org-bracket-link-regexp s)
  5642. (setq s (replace-match (if (match-end 2)
  5643. (match-string 3 s)
  5644. (match-string 1 s)) t t s)))
  5645. s)
  5646. (defvar org-priority-regexp) ; defined later in the file
  5647. (defvar org-after-sorting-entries-or-items-hook nil
  5648. "Hook that is run after a bunch of entries or items have been sorted.
  5649. When children are sorted, the cursor is in the parent line when this
  5650. hook gets called. When a region or a plain list is sorted, the cursor
  5651. will be in the first entry of the sorted region/list.")
  5652. (defun org-sort-entries-or-items
  5653. (&optional with-case sorting-type getkey-func compare-func property)
  5654. "Sort entries on a certain level of an outline tree, or plain list items.
  5655. If there is an active region, the entries in the region are sorted.
  5656. Else, if the cursor is before the first entry, sort the top-level items.
  5657. Else, the children of the entry at point are sorted.
  5658. If the cursor is at the first item in a plain list, the list items will be
  5659. sorted.
  5660. Sorting can be alphabetically, numerically, by date/time as given by
  5661. a time stamp, by a property or by priority.
  5662. The command prompts for the sorting type unless it has been given to the
  5663. function through the SORTING-TYPE argument, which needs to a character,
  5664. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5665. precise meaning of each character:
  5666. n Numerically, by converting the beginning of the entry/item to a number.
  5667. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5668. t By date/time, either the first active time stamp in the entry, or, if
  5669. none exist, by the first inactive one.
  5670. In items, only the first line will be chekced.
  5671. s By the scheduled date/time.
  5672. d By deadline date/time.
  5673. c By creation time, which is assumed to be the first inactive time stamp
  5674. at the beginning of a line.
  5675. p By priority according to the cookie.
  5676. r By the value of a property.
  5677. Capital letters will reverse the sort order.
  5678. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5679. called with point at the beginning of the record. It must return either
  5680. a string or a number that should serve as the sorting key for that record.
  5681. Comparing entries ignores case by default. However, with an optional argument
  5682. WITH-CASE, the sorting considers case as well."
  5683. (interactive "P")
  5684. (let ((case-func (if with-case 'identity 'downcase))
  5685. start beg end stars re re2
  5686. txt what tmp plain-list-p)
  5687. ;; Find beginning and end of region to sort
  5688. (cond
  5689. ((org-region-active-p)
  5690. ;; we will sort the region
  5691. (setq end (region-end)
  5692. what "region")
  5693. (goto-char (region-beginning))
  5694. (if (not (org-on-heading-p)) (outline-next-heading))
  5695. (setq start (point)))
  5696. ((org-at-item-p)
  5697. ;; we will sort this plain list
  5698. (org-beginning-of-item-list) (setq start (point))
  5699. (org-end-of-item-list) (setq end (point))
  5700. (goto-char start)
  5701. (setq plain-list-p t
  5702. what "plain list"))
  5703. ((or (org-on-heading-p)
  5704. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5705. ;; we will sort the children of the current headline
  5706. (org-back-to-heading)
  5707. (setq start (point)
  5708. end (progn (org-end-of-subtree t t)
  5709. (org-back-over-empty-lines)
  5710. (point))
  5711. what "children")
  5712. (goto-char start)
  5713. (show-subtree)
  5714. (outline-next-heading))
  5715. (t
  5716. ;; we will sort the top-level entries in this file
  5717. (goto-char (point-min))
  5718. (or (org-on-heading-p) (outline-next-heading))
  5719. (setq start (point) end (point-max) what "top-level")
  5720. (goto-char start)
  5721. (show-all)))
  5722. (setq beg (point))
  5723. (if (>= beg end) (error "Nothing to sort"))
  5724. (unless plain-list-p
  5725. (looking-at "\\(\\*+\\)")
  5726. (setq stars (match-string 1)
  5727. re (concat "^" (regexp-quote stars) " +")
  5728. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5729. txt (buffer-substring beg end))
  5730. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5731. (if (and (not (equal stars "*")) (string-match re2 txt))
  5732. (error "Region to sort contains a level above the first entry")))
  5733. (unless sorting-type
  5734. (message
  5735. (if plain-list-p
  5736. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5737. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5738. [t]ime [s]cheduled [d]eadline [c]reated
  5739. A/N/T/S/D/C/P/O/F means reversed:")
  5740. what)
  5741. (setq sorting-type (read-char-exclusive))
  5742. (and (= (downcase sorting-type) ?f)
  5743. (setq getkey-func
  5744. (org-ido-completing-read "Sort using function: "
  5745. obarray 'fboundp t nil nil))
  5746. (setq getkey-func (intern getkey-func)))
  5747. (and (= (downcase sorting-type) ?r)
  5748. (setq property
  5749. (org-ido-completing-read "Property: "
  5750. (mapcar 'list (org-buffer-property-keys t))
  5751. nil t))))
  5752. (message "Sorting entries...")
  5753. (save-restriction
  5754. (narrow-to-region start end)
  5755. (let ((dcst (downcase sorting-type))
  5756. (case-fold-search nil)
  5757. (now (current-time)))
  5758. (sort-subr
  5759. (/= dcst sorting-type)
  5760. ;; This function moves to the beginning character of the "record" to
  5761. ;; be sorted.
  5762. (if plain-list-p
  5763. (lambda nil
  5764. (if (org-at-item-p) t (goto-char (point-max))))
  5765. (lambda nil
  5766. (if (re-search-forward re nil t)
  5767. (goto-char (match-beginning 0))
  5768. (goto-char (point-max)))))
  5769. ;; This function moves to the last character of the "record" being
  5770. ;; sorted.
  5771. (if plain-list-p
  5772. 'org-end-of-item
  5773. (lambda nil
  5774. (save-match-data
  5775. (condition-case nil
  5776. (outline-forward-same-level 1)
  5777. (error
  5778. (goto-char (point-max)))))))
  5779. ;; This function returns the value that gets sorted against.
  5780. (if plain-list-p
  5781. (lambda nil
  5782. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5783. (cond
  5784. ((= dcst ?n)
  5785. (string-to-number (buffer-substring (match-end 0)
  5786. (point-at-eol))))
  5787. ((= dcst ?a)
  5788. (buffer-substring (match-end 0) (point-at-eol)))
  5789. ((= dcst ?t)
  5790. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5791. (re-search-forward org-ts-regexp-both
  5792. (point-at-eol) t))
  5793. (org-time-string-to-seconds (match-string 0))
  5794. (time-to-seconds now)))
  5795. ((= dcst ?f)
  5796. (if getkey-func
  5797. (progn
  5798. (setq tmp (funcall getkey-func))
  5799. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5800. tmp)
  5801. (error "Invalid key function `%s'" getkey-func)))
  5802. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5803. (lambda nil
  5804. (cond
  5805. ((= dcst ?n)
  5806. (if (looking-at org-complex-heading-regexp)
  5807. (string-to-number (match-string 4))
  5808. nil))
  5809. ((= dcst ?a)
  5810. (if (looking-at org-complex-heading-regexp)
  5811. (funcall case-func (match-string 4))
  5812. nil))
  5813. ((= dcst ?t)
  5814. (let ((end (save-excursion (outline-next-heading) (point))))
  5815. (if (or (re-search-forward org-ts-regexp end t)
  5816. (re-search-forward org-ts-regexp-both end t))
  5817. (org-time-string-to-seconds (match-string 0))
  5818. (time-to-seconds now))))
  5819. ((= dcst ?c)
  5820. (let ((end (save-excursion (outline-next-heading) (point))))
  5821. (if (re-search-forward
  5822. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5823. end t)
  5824. (org-time-string-to-seconds (match-string 0))
  5825. (time-to-seconds now))))
  5826. ((= dcst ?s)
  5827. (let ((end (save-excursion (outline-next-heading) (point))))
  5828. (if (re-search-forward org-scheduled-time-regexp end t)
  5829. (org-time-string-to-seconds (match-string 1))
  5830. (time-to-seconds now))))
  5831. ((= dcst ?d)
  5832. (let ((end (save-excursion (outline-next-heading) (point))))
  5833. (if (re-search-forward org-deadline-time-regexp end t)
  5834. (org-time-string-to-seconds (match-string 1))
  5835. (time-to-seconds now))))
  5836. ((= dcst ?p)
  5837. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5838. (string-to-char (match-string 2))
  5839. org-default-priority))
  5840. ((= dcst ?r)
  5841. (or (org-entry-get nil property) ""))
  5842. ((= dcst ?o)
  5843. (if (looking-at org-complex-heading-regexp)
  5844. (- 9999 (length (member (match-string 2)
  5845. org-todo-keywords-1)))))
  5846. ((= dcst ?f)
  5847. (if getkey-func
  5848. (progn
  5849. (setq tmp (funcall getkey-func))
  5850. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5851. tmp)
  5852. (error "Invalid key function `%s'" getkey-func)))
  5853. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5854. nil
  5855. (cond
  5856. ((= dcst ?a) 'string<)
  5857. ((= dcst ?t) 'time-less-p)
  5858. ((= dcst ?f) compare-func)
  5859. ((= dcst ?p) '<)
  5860. (t nil)))))
  5861. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5862. (message "Sorting entries...done")))
  5863. (defun org-do-sort (table what &optional with-case sorting-type)
  5864. "Sort TABLE of WHAT according to SORTING-TYPE.
  5865. The user will be prompted for the SORTING-TYPE if the call to this
  5866. function does not specify it. WHAT is only for the prompt, to indicate
  5867. what is being sorted. The sorting key will be extracted from
  5868. the car of the elements of the table.
  5869. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5870. (unless sorting-type
  5871. (message
  5872. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5873. what)
  5874. (setq sorting-type (read-char-exclusive)))
  5875. (let ((dcst (downcase sorting-type))
  5876. extractfun comparefun)
  5877. ;; Define the appropriate functions
  5878. (cond
  5879. ((= dcst ?n)
  5880. (setq extractfun 'string-to-number
  5881. comparefun (if (= dcst sorting-type) '< '>)))
  5882. ((= dcst ?a)
  5883. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5884. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5885. comparefun (if (= dcst sorting-type)
  5886. 'string<
  5887. (lambda (a b) (and (not (string< a b))
  5888. (not (string= a b)))))))
  5889. ((= dcst ?t)
  5890. (setq extractfun
  5891. (lambda (x)
  5892. (if (or (string-match org-ts-regexp x)
  5893. (string-match org-ts-regexp-both x))
  5894. (time-to-seconds
  5895. (org-time-string-to-time (match-string 0 x)))
  5896. 0))
  5897. comparefun (if (= dcst sorting-type) '< '>)))
  5898. (t (error "Invalid sorting type `%c'" sorting-type)))
  5899. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5900. table)
  5901. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5902. ;;; Editing source examples
  5903. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5904. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5905. (define-key org-exit-edit-mode-map "\C-x\C-s" 'org-edit-src-save)
  5906. (defvar org-edit-src-force-single-line nil)
  5907. (defvar org-edit-src-from-org-mode nil)
  5908. (defvar org-edit-src-picture nil)
  5909. (defvar org-edit-src-beg-marker nil)
  5910. (defvar org-edit-src-end-marker nil)
  5911. (defvar org-edit-src-overlay nil)
  5912. (defvar org-edit-src-nindent nil)
  5913. (define-minor-mode org-exit-edit-mode
  5914. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5915. (defun org-edit-src-code ()
  5916. "Edit the source code example at point.
  5917. The example is copied to a separate buffer, and that buffer is switched
  5918. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  5919. This will remove the original code in the Org buffer, and replace it with
  5920. the edited version."
  5921. (interactive)
  5922. (let ((line (org-current-line))
  5923. (case-fold-search t)
  5924. (msg (substitute-command-keys
  5925. "Edit, then exit with C-c ' (C-c and single quote)"))
  5926. (info (org-edit-src-find-region-and-lang))
  5927. (org-mode-p (eq major-mode 'org-mode))
  5928. (beg (make-marker))
  5929. (end (make-marker))
  5930. nindent ovl lang lang-f single lfmt code begline buffer)
  5931. (if (not info)
  5932. nil
  5933. (setq beg (move-marker beg (nth 0 info))
  5934. end (move-marker end (nth 1 info))
  5935. code (buffer-substring-no-properties beg end)
  5936. lang (nth 2 info)
  5937. single (nth 3 info)
  5938. lfmt (nth 4 info)
  5939. lang-f (intern (concat lang "-mode"))
  5940. begline (save-excursion (goto-char beg) (org-current-line)))
  5941. (unless (functionp lang-f)
  5942. (error "No such language mode: %s" lang-f))
  5943. (goto-line line)
  5944. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  5945. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  5946. (switch-to-buffer buffer)
  5947. (when buffer
  5948. (with-current-buffer buffer
  5949. (if (boundp 'org-edit-src-overlay)
  5950. (org-delete-overlay org-edit-src-overlay)))
  5951. (kill-buffer buffer))
  5952. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  5953. (setq ovl (org-make-overlay beg end))
  5954. (org-overlay-put ovl 'face 'secondary-selection)
  5955. (org-overlay-put ovl 'edit-buffer buffer)
  5956. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  5957. (org-overlay-put ovl 'face 'secondary-selection)
  5958. (org-overlay-put ovl
  5959. 'keymap
  5960. (let ((map (make-sparse-keymap)))
  5961. (define-key map [mouse-1] 'org-edit-src-continue)
  5962. map))
  5963. (org-overlay-put ovl :read-only "Leave me alone")
  5964. (switch-to-buffer buffer)
  5965. (insert code)
  5966. (remove-text-properties (point-min) (point-max)
  5967. '(display nil invisible nil intangible nil))
  5968. (setq nindent (org-do-remove-indentation))
  5969. (let ((org-inhibit-startup t))
  5970. (funcall lang-f))
  5971. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5972. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5973. (when lfmt
  5974. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5975. (when org-mode-p
  5976. (goto-char (point-min))
  5977. (while (re-search-forward "^," nil t)
  5978. (replace-match "")))
  5979. (goto-line (1+ (- line begline)))
  5980. (org-exit-edit-mode)
  5981. (org-set-local 'org-edit-src-beg-marker beg)
  5982. (org-set-local 'org-edit-src-end-marker end)
  5983. (org-set-local 'org-edit-src-overlay ovl)
  5984. (org-set-local 'org-edit-src-nindent nindent)
  5985. (and org-edit-src-persistent-message
  5986. (org-set-local 'header-line-format msg)))
  5987. (message "%s" msg)
  5988. t)))
  5989. (defun org-edit-src-continue (e)
  5990. (interactive "e")
  5991. (mouse-set-point e)
  5992. (let ((buf (get-char-property (point) 'edit-buffer)))
  5993. (if buf (switch-to-buffer buf)
  5994. (error "Something is wrong here"))))
  5995. (defun org-edit-src-find-buffer (beg end)
  5996. "Find a source editing buffer that is already editing the region BEG to END."
  5997. (catch 'exit
  5998. (mapc
  5999. (lambda (b)
  6000. (with-current-buffer b
  6001. (if (and (string-match "\\`*Org Edit " (buffer-name))
  6002. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  6003. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  6004. (equal beg org-edit-src-beg-marker)
  6005. (equal end org-edit-src-end-marker))
  6006. (throw 'exit (current-buffer)))))
  6007. (buffer-list))
  6008. nil))
  6009. (defun org-edit-fixed-width-region ()
  6010. "Edit the fixed-width ascii drawing at point.
  6011. This must be a region where each line starts with a colon followed by
  6012. a space character.
  6013. An new buffer is created and the fixed-width region is copied into it,
  6014. and the buffer is switched into `artist-mode' for editing. When done,
  6015. exit with \\[org-edit-src-exit]. The edited text will then replace
  6016. the fragment in the Org-mode buffer."
  6017. (interactive)
  6018. (let ((line (org-current-line))
  6019. (case-fold-search t)
  6020. (msg (substitute-command-keys
  6021. "Edit, then exit with C-c ' (C-c and single quote)"))
  6022. (org-mode-p (eq major-mode 'org-mode))
  6023. (beg (make-marker))
  6024. (end (make-marker))
  6025. nindent ovl beg1 end1 code begline buffer)
  6026. (beginning-of-line 1)
  6027. (if (looking-at "[ \t]*[^:\n \t]")
  6028. nil
  6029. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  6030. (setq beg1 (point) end1 beg1)
  6031. (save-excursion
  6032. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  6033. (setq beg1 (point-at-bol 2))
  6034. (setq beg1 (point))))
  6035. (save-excursion
  6036. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  6037. (setq end1 (1- (match-beginning 0)))
  6038. (setq end1 (point))))
  6039. (goto-line line))
  6040. (setq beg (move-marker beg beg1)
  6041. end (move-marker end end1)
  6042. code (buffer-substring-no-properties beg end)
  6043. begline (save-excursion (goto-char beg) (org-current-line)))
  6044. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  6045. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  6046. (switch-to-buffer buffer)
  6047. (when buffer
  6048. (with-current-buffer buffer
  6049. (if (boundp 'org-edit-src-overlay)
  6050. (org-delete-overlay org-edit-src-overlay)))
  6051. (kill-buffer buffer))
  6052. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  6053. (setq ovl (org-make-overlay beg end))
  6054. (org-overlay-put ovl 'face 'secondary-selection)
  6055. (org-overlay-put ovl 'edit-buffer buffer)
  6056. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  6057. (org-overlay-put ovl 'face 'secondary-selection)
  6058. (org-overlay-put ovl
  6059. 'keymap
  6060. (let ((map (make-sparse-keymap)))
  6061. (define-key map [mouse-1] 'org-edit-src-continue)
  6062. map))
  6063. (org-overlay-put ovl :read-only "Leave me alone")
  6064. (switch-to-buffer buffer)
  6065. (insert code)
  6066. (remove-text-properties (point-min) (point-max)
  6067. '(display nil invisible nil intangible nil))
  6068. (setq nindent (org-do-remove-indentation))
  6069. (cond
  6070. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  6071. (fundamental-mode)
  6072. (artist-mode 1))
  6073. (t (funcall org-edit-fixed-width-region-mode)))
  6074. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  6075. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  6076. (set (make-local-variable 'org-edit-src-picture) t)
  6077. (goto-char (point-min))
  6078. (while (re-search-forward "^[ \t]*: ?" nil t)
  6079. (replace-match ""))
  6080. (goto-line (1+ (- line begline)))
  6081. (org-exit-edit-mode)
  6082. (org-set-local 'org-edit-src-beg-marker beg)
  6083. (org-set-local 'org-edit-src-end-marker end)
  6084. (org-set-local 'org-edit-src-overlay ovl)
  6085. (org-set-local 'org-edit-src-nindent nindent)
  6086. (and org-edit-src-persistent-message
  6087. (org-set-local 'header-line-format msg)))
  6088. (message "%s" msg)
  6089. t)))
  6090. (defun org-edit-src-find-region-and-lang ()
  6091. "Find the region and language for a local edit.
  6092. Return a list with beginning and end of the region, a string representing
  6093. the language, a switch telling of the content should be in a single line."
  6094. (let ((re-list
  6095. (append
  6096. org-edit-src-region-extra
  6097. '(
  6098. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  6099. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  6100. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  6101. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  6102. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  6103. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  6104. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  6105. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  6106. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  6107. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  6108. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  6109. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  6110. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  6111. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  6112. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  6113. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  6114. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  6115. )))
  6116. (pos (point))
  6117. re1 re2 single beg end lang lfmt match-re1)
  6118. (catch 'exit
  6119. (while (setq entry (pop re-list))
  6120. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  6121. single (nth 3 entry))
  6122. (save-excursion
  6123. (if (or (looking-at re1)
  6124. (re-search-backward re1 nil t))
  6125. (progn
  6126. (setq match-re1 (match-string 0))
  6127. (setq beg (match-end 0)
  6128. lang (org-edit-src-get-lang lang)
  6129. lfmt (org-edit-src-get-label-format match-re1))
  6130. (if (and (re-search-forward re2 nil t)
  6131. (>= (match-end 0) pos))
  6132. (throw 'exit (list beg (match-beginning 0)
  6133. lang single lfmt))))
  6134. (if (or (looking-at re2)
  6135. (re-search-forward re2 nil t))
  6136. (progn
  6137. (setq end (match-beginning 0))
  6138. (if (and (re-search-backward re1 nil t)
  6139. (<= (match-beginning 0) pos))
  6140. (progn
  6141. (setq lfmt (org-edit-src-get-label-format
  6142. (match-string 0)))
  6143. (throw 'exit
  6144. (list (match-end 0) end
  6145. (org-edit-src-get-lang lang)
  6146. single lfmt))))))))))))
  6147. (defun org-edit-src-get-lang (lang)
  6148. "Extract the src language."
  6149. (let ((m (match-string 0)))
  6150. (cond
  6151. ((stringp lang) lang)
  6152. ((integerp lang) (match-string lang))
  6153. ((and (eq lang 'lang)
  6154. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  6155. (match-string 1 m))
  6156. ((and (eq lang 'style)
  6157. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  6158. (match-string 1 m))
  6159. (t "fundamental"))))
  6160. (defun org-edit-src-get-label-format (s)
  6161. "Extract the label format."
  6162. (save-match-data
  6163. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  6164. (match-string 1 s))))
  6165. (defun org-edit-src-exit ()
  6166. "Exit special edit and protect problematic lines."
  6167. (interactive)
  6168. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  6169. (error "This is not an sub-editing buffer, something is wrong..."))
  6170. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  6171. 1
  6172. (org-current-line)))
  6173. (beg org-edit-src-beg-marker)
  6174. (end org-edit-src-end-marker)
  6175. (ovl org-edit-src-overlay)
  6176. (buffer (current-buffer))
  6177. (nindent org-edit-src-nindent)
  6178. code)
  6179. (goto-char (point-min))
  6180. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  6181. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  6182. (when (org-bound-and-true-p org-edit-src-force-single-line)
  6183. (goto-char (point-min))
  6184. (while (re-search-forward "\n" nil t)
  6185. (replace-match " "))
  6186. (goto-char (point-min))
  6187. (if (looking-at "\\s-*") (replace-match " "))
  6188. (if (re-search-forward "\\s-+\\'" nil t)
  6189. (replace-match "")))
  6190. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  6191. (goto-char (point-min))
  6192. (while (re-search-forward
  6193. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  6194. (replace-match ",\\1")))
  6195. (when (org-bound-and-true-p org-edit-src-picture)
  6196. (untabify (point-min) (point-max))
  6197. (goto-char (point-min))
  6198. (while (re-search-forward "^" nil t)
  6199. (replace-match ": ")))
  6200. (when nindent
  6201. (setq nindent (make-string nindent ?\ ))
  6202. (goto-char (point-min))
  6203. (while (re-search-forward "^" nil t)
  6204. (replace-match nindent)))
  6205. (setq code (buffer-string))
  6206. (switch-to-buffer (marker-buffer beg))
  6207. (kill-buffer buffer)
  6208. (goto-char beg)
  6209. (org-delete-overlay ovl)
  6210. (delete-region beg end)
  6211. (insert code)
  6212. (goto-char beg)
  6213. (goto-line (1- (+ (org-current-line) line)))
  6214. (move-marker beg nil)
  6215. (move-marker end nil)))
  6216. (defun org-edit-src-save ()
  6217. "Save parent buffer with current state source-code buffer."
  6218. (interactive)
  6219. (let ((p (point)) (m (mark)) msg)
  6220. (org-edit-src-exit)
  6221. (save-buffer)
  6222. (setq msg (current-message))
  6223. (org-edit-src-code)
  6224. (push-mark m 'nomessage)
  6225. (goto-char (min p (point-max)))
  6226. (message (or msg ""))))
  6227. ;;; The orgstruct minor mode
  6228. ;; Define a minor mode which can be used in other modes in order to
  6229. ;; integrate the org-mode structure editing commands.
  6230. ;; This is really a hack, because the org-mode structure commands use
  6231. ;; keys which normally belong to the major mode. Here is how it
  6232. ;; works: The minor mode defines all the keys necessary to operate the
  6233. ;; structure commands, but wraps the commands into a function which
  6234. ;; tests if the cursor is currently at a headline or a plain list
  6235. ;; item. If that is the case, the structure command is used,
  6236. ;; temporarily setting many Org-mode variables like regular
  6237. ;; expressions for filling etc. However, when any of those keys is
  6238. ;; used at a different location, function uses `key-binding' to look
  6239. ;; up if the key has an associated command in another currently active
  6240. ;; keymap (minor modes, major mode, global), and executes that
  6241. ;; command. There might be problems if any of the keys is otherwise
  6242. ;; used as a prefix key.
  6243. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6244. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6245. ;; addresses this by checking explicitly for both bindings.
  6246. (defvar orgstruct-mode-map (make-sparse-keymap)
  6247. "Keymap for the minor `orgstruct-mode'.")
  6248. (defvar org-local-vars nil
  6249. "List of local variables, for use by `orgstruct-mode'")
  6250. ;;;###autoload
  6251. (define-minor-mode orgstruct-mode
  6252. "Toggle the minor more `orgstruct-mode'.
  6253. This mode is for using Org-mode structure commands in other modes.
  6254. The following key behave as if Org-mode was active, if the cursor
  6255. is on a headline, or on a plain list item (both in the definition
  6256. of Org-mode).
  6257. M-up Move entry/item up
  6258. M-down Move entry/item down
  6259. M-left Promote
  6260. M-right Demote
  6261. M-S-up Move entry/item up
  6262. M-S-down Move entry/item down
  6263. M-S-left Promote subtree
  6264. M-S-right Demote subtree
  6265. M-q Fill paragraph and items like in Org-mode
  6266. C-c ^ Sort entries
  6267. C-c - Cycle list bullet
  6268. TAB Cycle item visibility
  6269. M-RET Insert new heading/item
  6270. S-M-RET Insert new TODO heading / Checkbox item
  6271. C-c C-c Set tags / toggle checkbox"
  6272. nil " OrgStruct" nil
  6273. (org-load-modules-maybe)
  6274. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6275. ;;;###autoload
  6276. (defun turn-on-orgstruct ()
  6277. "Unconditionally turn on `orgstruct-mode'."
  6278. (orgstruct-mode 1))
  6279. (defun orgstruct++-mode (&optional arg)
  6280. "Toggle `orgstruct-mode', the enhanced version of it.
  6281. In addition to setting orgstruct-mode, this also exports all indentation and
  6282. autofilling variables from org-mode into the buffer. It will also
  6283. recognize item context in multiline items.
  6284. Note that turning off orgstruct-mode will *not* remove the
  6285. indentation/paragraph settings. This can only be done by refreshing the
  6286. major mode, for example with \[normal-mode]."
  6287. (interactive "P")
  6288. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6289. (if (< arg 1)
  6290. (orgstruct-mode -1)
  6291. (orgstruct-mode 1)
  6292. (let (var val)
  6293. (mapc
  6294. (lambda (x)
  6295. (when (string-match
  6296. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6297. (symbol-name (car x)))
  6298. (setq var (car x) val (nth 1 x))
  6299. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6300. org-local-vars)
  6301. (org-set-local 'orgstruct-is-++ t))))
  6302. (defvar orgstruct-is-++ nil
  6303. "Is orgstruct-mode in ++ version in the current-buffer?")
  6304. (make-variable-buffer-local 'orgstruct-is-++)
  6305. ;;;###autoload
  6306. (defun turn-on-orgstruct++ ()
  6307. "Unconditionally turn on `orgstruct++-mode'."
  6308. (orgstruct++-mode 1))
  6309. (defun orgstruct-error ()
  6310. "Error when there is no default binding for a structure key."
  6311. (interactive)
  6312. (error "This key has no function outside structure elements"))
  6313. (defun orgstruct-setup ()
  6314. "Setup orgstruct keymaps."
  6315. (let ((nfunc 0)
  6316. (bindings
  6317. (list
  6318. '([(meta up)] org-metaup)
  6319. '([(meta down)] org-metadown)
  6320. '([(meta left)] org-metaleft)
  6321. '([(meta right)] org-metaright)
  6322. '([(meta shift up)] org-shiftmetaup)
  6323. '([(meta shift down)] org-shiftmetadown)
  6324. '([(meta shift left)] org-shiftmetaleft)
  6325. '([(meta shift right)] org-shiftmetaright)
  6326. '([?\e (up)] org-metaup)
  6327. '([?\e (down)] org-metadown)
  6328. '([?\e (left)] org-metaleft)
  6329. '([?\e (right)] org-metaright)
  6330. '([?\e (shift up)] org-shiftmetaup)
  6331. '([?\e (shift down)] org-shiftmetadown)
  6332. '([?\e (shift left)] org-shiftmetaleft)
  6333. '([?\e (shift right)] org-shiftmetaright)
  6334. '([(shift up)] org-shiftup)
  6335. '([(shift down)] org-shiftdown)
  6336. '([(shift left)] org-shiftleft)
  6337. '([(shift right)] org-shiftright)
  6338. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6339. '("\M-q" fill-paragraph)
  6340. '("\C-c^" org-sort)
  6341. '("\C-c-" org-cycle-list-bullet)))
  6342. elt key fun cmd)
  6343. (while (setq elt (pop bindings))
  6344. (setq nfunc (1+ nfunc))
  6345. (setq key (org-key (car elt))
  6346. fun (nth 1 elt)
  6347. cmd (orgstruct-make-binding fun nfunc key))
  6348. (org-defkey orgstruct-mode-map key cmd))
  6349. ;; Special treatment needed for TAB and RET
  6350. (org-defkey orgstruct-mode-map [(tab)]
  6351. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6352. (org-defkey orgstruct-mode-map "\C-i"
  6353. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6354. (org-defkey orgstruct-mode-map "\M-\C-m"
  6355. (orgstruct-make-binding 'org-insert-heading 105
  6356. "\M-\C-m" [(meta return)]))
  6357. (org-defkey orgstruct-mode-map [(meta return)]
  6358. (orgstruct-make-binding 'org-insert-heading 106
  6359. [(meta return)] "\M-\C-m"))
  6360. (org-defkey orgstruct-mode-map [(shift meta return)]
  6361. (orgstruct-make-binding 'org-insert-todo-heading 107
  6362. [(meta return)] "\M-\C-m"))
  6363. (org-defkey orgstruct-mode-map "\e\C-m"
  6364. (orgstruct-make-binding 'org-insert-heading 108
  6365. "\e\C-m" [?\e (return)]))
  6366. (org-defkey orgstruct-mode-map [?\e (return)]
  6367. (orgstruct-make-binding 'org-insert-heading 109
  6368. [?\e (return)] "\e\C-m"))
  6369. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6370. (orgstruct-make-binding 'org-insert-todo-heading 110
  6371. [?\e (return)] "\e\C-m"))
  6372. (unless org-local-vars
  6373. (setq org-local-vars (org-get-local-variables)))
  6374. t))
  6375. (defun orgstruct-make-binding (fun n &rest keys)
  6376. "Create a function for binding in the structure minor mode.
  6377. FUN is the command to call inside a table. N is used to create a unique
  6378. command name. KEYS are keys that should be checked in for a command
  6379. to execute outside of tables."
  6380. (eval
  6381. (list 'defun
  6382. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6383. '(arg)
  6384. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6385. "Outside of structure, run the binding of `"
  6386. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6387. "'.")
  6388. '(interactive "p")
  6389. (list 'if
  6390. `(org-context-p 'headline 'item
  6391. (and orgstruct-is-++
  6392. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6393. 'item-body))
  6394. (list 'org-run-like-in-org-mode (list 'quote fun))
  6395. (list 'let '(orgstruct-mode)
  6396. (list 'call-interactively
  6397. (append '(or)
  6398. (mapcar (lambda (k)
  6399. (list 'key-binding k))
  6400. keys)
  6401. '('orgstruct-error))))))))
  6402. (defun org-context-p (&rest contexts)
  6403. "Check if local context is any of CONTEXTS.
  6404. Possible values in the list of contexts are `table', `headline', and `item'."
  6405. (let ((pos (point)))
  6406. (goto-char (point-at-bol))
  6407. (prog1 (or (and (memq 'table contexts)
  6408. (looking-at "[ \t]*|"))
  6409. (and (memq 'headline contexts)
  6410. ;;????????? (looking-at "\\*+"))
  6411. (looking-at outline-regexp))
  6412. (and (memq 'item contexts)
  6413. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6414. (and (memq 'item-body contexts)
  6415. (org-in-item-p)))
  6416. (goto-char pos))))
  6417. (defun org-get-local-variables ()
  6418. "Return a list of all local variables in an org-mode buffer."
  6419. (let (varlist)
  6420. (with-current-buffer (get-buffer-create "*Org tmp*")
  6421. (erase-buffer)
  6422. (org-mode)
  6423. (setq varlist (buffer-local-variables)))
  6424. (kill-buffer "*Org tmp*")
  6425. (delq nil
  6426. (mapcar
  6427. (lambda (x)
  6428. (setq x
  6429. (if (symbolp x)
  6430. (list x)
  6431. (list (car x) (list 'quote (cdr x)))))
  6432. (if (string-match
  6433. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6434. (symbol-name (car x)))
  6435. x nil))
  6436. varlist))))
  6437. ;;;###autoload
  6438. (defun org-run-like-in-org-mode (cmd)
  6439. "Run a command, pretending that the current buffer is in Org-mode.
  6440. This will temporarily bind local variables that are typically bound in
  6441. Org-mode to the values they have in Org-mode, and then interactively
  6442. call CMD."
  6443. (org-load-modules-maybe)
  6444. (unless org-local-vars
  6445. (setq org-local-vars (org-get-local-variables)))
  6446. (eval (list 'let org-local-vars
  6447. (list 'call-interactively (list 'quote cmd)))))
  6448. ;;;; Archiving
  6449. (defun org-get-category (&optional pos)
  6450. "Get the category applying to position POS."
  6451. (get-text-property (or pos (point)) 'org-category))
  6452. (defun org-refresh-category-properties ()
  6453. "Refresh category text properties in the buffer."
  6454. (let ((def-cat (cond
  6455. ((null org-category)
  6456. (if buffer-file-name
  6457. (file-name-sans-extension
  6458. (file-name-nondirectory buffer-file-name))
  6459. "???"))
  6460. ((symbolp org-category) (symbol-name org-category))
  6461. (t org-category)))
  6462. beg end cat pos optionp)
  6463. (org-unmodified
  6464. (save-excursion
  6465. (save-restriction
  6466. (widen)
  6467. (goto-char (point-min))
  6468. (put-text-property (point) (point-max) 'org-category def-cat)
  6469. (while (re-search-forward
  6470. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6471. (setq pos (match-end 0)
  6472. optionp (equal (char-after (match-beginning 0)) ?#)
  6473. cat (org-trim (match-string 2)))
  6474. (if optionp
  6475. (setq beg (point-at-bol) end (point-max))
  6476. (org-back-to-heading t)
  6477. (setq beg (point) end (org-end-of-subtree t t)))
  6478. (put-text-property beg end 'org-category cat)
  6479. (goto-char pos)))))))
  6480. ;;;; Link Stuff
  6481. ;;; Link abbreviations
  6482. (defun org-link-expand-abbrev (link)
  6483. "Apply replacements as defined in `org-link-abbrev-alist."
  6484. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6485. (let* ((key (match-string 1 link))
  6486. (as (or (assoc key org-link-abbrev-alist-local)
  6487. (assoc key org-link-abbrev-alist)))
  6488. (tag (and (match-end 2) (match-string 3 link)))
  6489. rpl)
  6490. (if (not as)
  6491. link
  6492. (setq rpl (cdr as))
  6493. (cond
  6494. ((symbolp rpl) (funcall rpl tag))
  6495. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6496. ((string-match "%h" rpl)
  6497. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6498. (t (concat rpl tag)))))
  6499. link))
  6500. ;;; Storing and inserting links
  6501. (defvar org-insert-link-history nil
  6502. "Minibuffer history for links inserted with `org-insert-link'.")
  6503. (defvar org-stored-links nil
  6504. "Contains the links stored with `org-store-link'.")
  6505. (defvar org-store-link-plist nil
  6506. "Plist with info about the most recently link created with `org-store-link'.")
  6507. (defvar org-link-protocols nil
  6508. "Link protocols added to Org-mode using `org-add-link-type'.")
  6509. (defvar org-store-link-functions nil
  6510. "List of functions that are called to create and store a link.
  6511. Each function will be called in turn until one returns a non-nil
  6512. value. Each function should check if it is responsible for creating
  6513. this link (for example by looking at the major mode).
  6514. If not, it must exit and return nil.
  6515. If yes, it should return a non-nil value after a calling
  6516. `org-store-link-props' with a list of properties and values.
  6517. Special properties are:
  6518. :type The link prefix. like \"http\". This must be given.
  6519. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6520. This is obligatory as well.
  6521. :description Optional default description for the second pair
  6522. of brackets in an Org-mode link. The user can still change
  6523. this when inserting this link into an Org-mode buffer.
  6524. In addition to these, any additional properties can be specified
  6525. and then used in remember templates.")
  6526. (defun org-add-link-type (type &optional follow export)
  6527. "Add TYPE to the list of `org-link-types'.
  6528. Re-compute all regular expressions depending on `org-link-types'
  6529. FOLLOW and EXPORT are two functions.
  6530. FOLLOW should take the link path as the single argument and do whatever
  6531. is necessary to follow the link, for example find a file or display
  6532. a mail message.
  6533. EXPORT should format the link path for export to one of the export formats.
  6534. It should be a function accepting three arguments:
  6535. path the path of the link, the text after the prefix (like \"http:\")
  6536. desc the description of the link, if any, nil if there was no description
  6537. format the export format, a symbol like `html' or `latex'.
  6538. The function may use the FORMAT information to return different values
  6539. depending on the format. The return value will be put literally into
  6540. the exported file.
  6541. Org-mode has a built-in default for exporting links. If you are happy with
  6542. this default, there is no need to define an export function for the link
  6543. type. For a simple example of an export function, see `org-bbdb.el'."
  6544. (add-to-list 'org-link-types type t)
  6545. (org-make-link-regexps)
  6546. (if (assoc type org-link-protocols)
  6547. (setcdr (assoc type org-link-protocols) (list follow export))
  6548. (push (list type follow export) org-link-protocols)))
  6549. ;;;###autoload
  6550. (defun org-store-link (arg)
  6551. "\\<org-mode-map>Store an org-link to the current location.
  6552. This link is added to `org-stored-links' and can later be inserted
  6553. into an org-buffer with \\[org-insert-link].
  6554. For some link types, a prefix arg is interpreted:
  6555. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6556. For file links, arg negates `org-context-in-file-links'."
  6557. (interactive "P")
  6558. (org-load-modules-maybe)
  6559. (setq org-store-link-plist nil) ; reset
  6560. (let (link cpltxt desc description search txt custom-id)
  6561. (cond
  6562. ((run-hook-with-args-until-success 'org-store-link-functions)
  6563. (setq link (plist-get org-store-link-plist :link)
  6564. desc (or (plist-get org-store-link-plist :description) link)))
  6565. ((equal (buffer-name) "*Org Edit Src Example*")
  6566. (let (label gc)
  6567. (while (or (not label)
  6568. (save-excursion
  6569. (save-restriction
  6570. (widen)
  6571. (goto-char (point-min))
  6572. (re-search-forward
  6573. (regexp-quote (format org-coderef-label-format label))
  6574. nil t))))
  6575. (when label (message "Label exists already") (sit-for 2))
  6576. (setq label (read-string "Code line label: " label)))
  6577. (end-of-line 1)
  6578. (setq link (format org-coderef-label-format label))
  6579. (setq gc (- 79 (length link)))
  6580. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6581. (insert link)
  6582. (setq link (concat "(" label ")") desc nil)))
  6583. ((eq major-mode 'calendar-mode)
  6584. (let ((cd (calendar-cursor-to-date)))
  6585. (setq link
  6586. (format-time-string
  6587. (car org-time-stamp-formats)
  6588. (apply 'encode-time
  6589. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6590. nil nil nil))))
  6591. (org-store-link-props :type "calendar" :date cd)))
  6592. ((eq major-mode 'w3-mode)
  6593. (setq cpltxt (if (and (buffer-name)
  6594. (not (string-match "Untitled" (buffer-name))))
  6595. (buffer-name)
  6596. (url-view-url t))
  6597. link (org-make-link (url-view-url t)))
  6598. (org-store-link-props :type "w3" :url (url-view-url t)))
  6599. ((eq major-mode 'w3m-mode)
  6600. (setq cpltxt (or w3m-current-title w3m-current-url)
  6601. link (org-make-link w3m-current-url))
  6602. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6603. ((setq search (run-hook-with-args-until-success
  6604. 'org-create-file-search-functions))
  6605. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6606. "::" search))
  6607. (setq cpltxt (or description link)))
  6608. ((eq major-mode 'image-mode)
  6609. (setq cpltxt (concat "file:"
  6610. (abbreviate-file-name buffer-file-name))
  6611. link (org-make-link cpltxt))
  6612. (org-store-link-props :type "image" :file buffer-file-name))
  6613. ((eq major-mode 'dired-mode)
  6614. ;; link to the file in the current line
  6615. (setq cpltxt (concat "file:"
  6616. (abbreviate-file-name
  6617. (expand-file-name
  6618. (dired-get-filename nil t))))
  6619. link (org-make-link cpltxt)))
  6620. ((and buffer-file-name (org-mode-p))
  6621. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6622. (cond
  6623. ((org-in-regexp "<<\\(.*?\\)>>")
  6624. (setq cpltxt
  6625. (concat "file:"
  6626. (abbreviate-file-name buffer-file-name)
  6627. "::" (match-string 1))
  6628. link (org-make-link cpltxt)))
  6629. ((and (featurep 'org-id)
  6630. (or (eq org-link-to-org-use-id t)
  6631. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6632. (interactive-p))
  6633. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6634. (interactive-p)
  6635. (not custom-id))
  6636. (and org-link-to-org-use-id
  6637. (condition-case nil
  6638. (org-entry-get nil "ID")
  6639. (error nil)))))
  6640. ;; We can make a link using the ID.
  6641. (setq link (condition-case nil
  6642. (prog1 (org-id-store-link)
  6643. (setq desc (plist-get org-store-link-plist
  6644. :description)))
  6645. (error
  6646. ;; probably before first headline, link to file only
  6647. (concat "file:"
  6648. (abbreviate-file-name buffer-file-name))))))
  6649. (t
  6650. ;; Just link to current headline
  6651. (setq cpltxt (concat "file:"
  6652. (abbreviate-file-name buffer-file-name)))
  6653. ;; Add a context search string
  6654. (when (org-xor org-context-in-file-links arg)
  6655. (setq txt (cond
  6656. ((org-on-heading-p) nil)
  6657. ((org-region-active-p)
  6658. (buffer-substring (region-beginning) (region-end)))
  6659. (t nil)))
  6660. (when (or (null txt) (string-match "\\S-" txt))
  6661. (setq cpltxt
  6662. (concat cpltxt "::"
  6663. (condition-case nil
  6664. (org-make-org-heading-search-string txt)
  6665. (error "")))
  6666. desc "NONE")))
  6667. (if (string-match "::\\'" cpltxt)
  6668. (setq cpltxt (substring cpltxt 0 -2)))
  6669. (setq link (org-make-link cpltxt)))))
  6670. ((buffer-file-name (buffer-base-buffer))
  6671. ;; Just link to this file here.
  6672. (setq cpltxt (concat "file:"
  6673. (abbreviate-file-name
  6674. (buffer-file-name (buffer-base-buffer)))))
  6675. ;; Add a context string
  6676. (when (org-xor org-context-in-file-links arg)
  6677. (setq txt (if (org-region-active-p)
  6678. (buffer-substring (region-beginning) (region-end))
  6679. (buffer-substring (point-at-bol) (point-at-eol))))
  6680. ;; Only use search option if there is some text.
  6681. (when (string-match "\\S-" txt)
  6682. (setq cpltxt
  6683. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6684. desc "NONE")))
  6685. (setq link (org-make-link cpltxt)))
  6686. ((interactive-p)
  6687. (error "Cannot link to a buffer which is not visiting a file"))
  6688. (t (setq link nil)))
  6689. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6690. (setq link (or link cpltxt)
  6691. desc (or desc cpltxt))
  6692. (if (equal desc "NONE") (setq desc nil))
  6693. (if (and (interactive-p) link)
  6694. (progn
  6695. (setq org-stored-links
  6696. (cons (list link desc) org-stored-links))
  6697. (message "Stored: %s" (or desc link))
  6698. (when custom-id
  6699. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6700. "::#" custom-id))
  6701. (setq org-stored-links
  6702. (cons (list link desc) org-stored-links))))
  6703. (and link (org-make-link-string link desc)))))
  6704. (defun org-store-link-props (&rest plist)
  6705. "Store link properties, extract names and addresses."
  6706. (let (x adr)
  6707. (when (setq x (plist-get plist :from))
  6708. (setq adr (mail-extract-address-components x))
  6709. (setq plist (plist-put plist :fromname (car adr)))
  6710. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6711. (when (setq x (plist-get plist :to))
  6712. (setq adr (mail-extract-address-components x))
  6713. (setq plist (plist-put plist :toname (car adr)))
  6714. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6715. (let ((from (plist-get plist :from))
  6716. (to (plist-get plist :to)))
  6717. (when (and from to org-from-is-user-regexp)
  6718. (setq plist
  6719. (plist-put plist :fromto
  6720. (if (string-match org-from-is-user-regexp from)
  6721. (concat "to %t")
  6722. (concat "from %f"))))))
  6723. (setq org-store-link-plist plist))
  6724. (defun org-add-link-props (&rest plist)
  6725. "Add these properties to the link property list."
  6726. (let (key value)
  6727. (while plist
  6728. (setq key (pop plist) value (pop plist))
  6729. (setq org-store-link-plist
  6730. (plist-put org-store-link-plist key value)))))
  6731. (defun org-email-link-description (&optional fmt)
  6732. "Return the description part of an email link.
  6733. This takes information from `org-store-link-plist' and formats it
  6734. according to FMT (default from `org-email-link-description-format')."
  6735. (setq fmt (or fmt org-email-link-description-format))
  6736. (let* ((p org-store-link-plist)
  6737. (to (plist-get p :toaddress))
  6738. (from (plist-get p :fromaddress))
  6739. (table
  6740. (list
  6741. (cons "%c" (plist-get p :fromto))
  6742. (cons "%F" (plist-get p :from))
  6743. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6744. (cons "%T" (plist-get p :to))
  6745. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6746. (cons "%s" (plist-get p :subject))
  6747. (cons "%m" (plist-get p :message-id)))))
  6748. (when (string-match "%c" fmt)
  6749. ;; Check if the user wrote this message
  6750. (if (and org-from-is-user-regexp from to
  6751. (save-match-data (string-match org-from-is-user-regexp from)))
  6752. (setq fmt (replace-match "to %t" t t fmt))
  6753. (setq fmt (replace-match "from %f" t t fmt))))
  6754. (org-replace-escapes fmt table)))
  6755. (defun org-make-org-heading-search-string (&optional string heading)
  6756. "Make search string for STRING or current headline."
  6757. (interactive)
  6758. (let ((s (or string (org-get-heading))))
  6759. (unless (and string (not heading))
  6760. ;; We are using a headline, clean up garbage in there.
  6761. (if (string-match org-todo-regexp s)
  6762. (setq s (replace-match "" t t s)))
  6763. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6764. (setq s (replace-match "" t t s)))
  6765. (setq s (org-trim s))
  6766. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6767. org-comment-string "\\)") s)
  6768. (setq s (replace-match "" t t s)))
  6769. (while (string-match org-ts-regexp s)
  6770. (setq s (replace-match "" t t s))))
  6771. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6772. (setq s (replace-match " " t t s)))
  6773. (or string (setq s (concat "*" s))) ; Add * for headlines
  6774. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6775. (defun org-make-link (&rest strings)
  6776. "Concatenate STRINGS."
  6777. (apply 'concat strings))
  6778. (defun org-make-link-string (link &optional description)
  6779. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6780. (unless (string-match "\\S-" link)
  6781. (error "Empty link"))
  6782. (when (stringp description)
  6783. ;; Remove brackets from the description, they are fatal.
  6784. (while (string-match "\\[" description)
  6785. (setq description (replace-match "{" t t description)))
  6786. (while (string-match "\\]" description)
  6787. (setq description (replace-match "}" t t description))))
  6788. (when (equal (org-link-escape link) description)
  6789. ;; No description needed, it is identical
  6790. (setq description nil))
  6791. (when (and (not description)
  6792. (not (equal link (org-link-escape link))))
  6793. (setq description (org-extract-attributes link)))
  6794. (concat "[[" (org-link-escape link) "]"
  6795. (if description (concat "[" description "]") "")
  6796. "]"))
  6797. (defconst org-link-escape-chars
  6798. '((?\ . "%20")
  6799. (?\[ . "%5B")
  6800. (?\] . "%5D")
  6801. (?\340 . "%E0") ; `a
  6802. (?\342 . "%E2") ; ^a
  6803. (?\347 . "%E7") ; ,c
  6804. (?\350 . "%E8") ; `e
  6805. (?\351 . "%E9") ; 'e
  6806. (?\352 . "%EA") ; ^e
  6807. (?\356 . "%EE") ; ^i
  6808. (?\364 . "%F4") ; ^o
  6809. (?\371 . "%F9") ; `u
  6810. (?\373 . "%FB") ; ^u
  6811. (?\; . "%3B")
  6812. (?? . "%3F")
  6813. (?= . "%3D")
  6814. (?+ . "%2B")
  6815. )
  6816. "Association list of escapes for some characters problematic in links.
  6817. This is the list that is used for internal purposes.")
  6818. (defvar org-url-encoding-use-url-hexify nil)
  6819. (defconst org-link-escape-chars-browser
  6820. '((?\ . "%20")) ; 32 for the SPC char
  6821. "Association list of escapes for some characters problematic in links.
  6822. This is the list that is used before handing over to the browser.")
  6823. (defun org-link-escape (text &optional table)
  6824. "Escape characters in TEXT that are problematic for links."
  6825. (if org-url-encoding-use-url-hexify
  6826. (url-hexify-string text)
  6827. (setq table (or table org-link-escape-chars))
  6828. (when text
  6829. (let ((re (mapconcat (lambda (x) (regexp-quote
  6830. (char-to-string (car x))))
  6831. table "\\|")))
  6832. (while (string-match re text)
  6833. (setq text
  6834. (replace-match
  6835. (cdr (assoc (string-to-char (match-string 0 text))
  6836. table))
  6837. t t text)))
  6838. text))))
  6839. (defun org-link-unescape (text &optional table)
  6840. "Reverse the action of `org-link-escape'."
  6841. (if org-url-encoding-use-url-hexify
  6842. (url-unhex-string text)
  6843. (setq table (or table org-link-escape-chars))
  6844. (when text
  6845. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6846. table "\\|")))
  6847. (while (string-match re text)
  6848. (setq text
  6849. (replace-match
  6850. (char-to-string (car (rassoc (match-string 0 text) table)))
  6851. t t text)))
  6852. text))))
  6853. (defun org-xor (a b)
  6854. "Exclusive or."
  6855. (if a (not b) b))
  6856. (defun org-fixup-message-id-for-http (s)
  6857. "Replace special characters in a message id, so it can be used in an http query."
  6858. (while (string-match "<" s)
  6859. (setq s (replace-match "%3C" t t s)))
  6860. (while (string-match ">" s)
  6861. (setq s (replace-match "%3E" t t s)))
  6862. (while (string-match "@" s)
  6863. (setq s (replace-match "%40" t t s)))
  6864. s)
  6865. ;;;###autoload
  6866. (defun org-insert-link-global ()
  6867. "Insert a link like Org-mode does.
  6868. This command can be called in any mode to insert a link in Org-mode syntax."
  6869. (interactive)
  6870. (org-load-modules-maybe)
  6871. (org-run-like-in-org-mode 'org-insert-link))
  6872. (defun org-insert-link (&optional complete-file link-location)
  6873. "Insert a link. At the prompt, enter the link.
  6874. Completion can be used to insert any of the link protocol prefixes like
  6875. http or ftp in use.
  6876. The history can be used to select a link previously stored with
  6877. `org-store-link'. When the empty string is entered (i.e. if you just
  6878. press RET at the prompt), the link defaults to the most recently
  6879. stored link. As SPC triggers completion in the minibuffer, you need to
  6880. use M-SPC or C-q SPC to force the insertion of a space character.
  6881. You will also be prompted for a description, and if one is given, it will
  6882. be displayed in the buffer instead of the link.
  6883. If there is already a link at point, this command will allow you to edit link
  6884. and description parts.
  6885. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6886. be selected using completion. The path to the file will be relative to the
  6887. current directory if the file is in the current directory or a subdirectory.
  6888. Otherwise, the link will be the absolute path as completed in the minibuffer
  6889. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6890. option `org-link-file-path-type'.
  6891. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6892. the current directory or below.
  6893. With three \\[universal-argument] prefixes, negate the meaning of
  6894. `org-keep-stored-link-after-insertion'.
  6895. If `org-make-link-description-function' is non-nil, this function will be
  6896. called with the link target, and the result will be the default
  6897. link description.
  6898. If the LINK-LOCATION parameter is non-nil, this value will be
  6899. used as the link location instead of reading one interactively."
  6900. (interactive "P")
  6901. (let* ((wcf (current-window-configuration))
  6902. (region (if (org-region-active-p)
  6903. (buffer-substring (region-beginning) (region-end))))
  6904. (remove (and region (list (region-beginning) (region-end))))
  6905. (desc region)
  6906. tmphist ; byte-compile incorrectly complains about this
  6907. (link link-location)
  6908. entry file all-prefixes)
  6909. (cond
  6910. (link-location) ; specified by arg, just use it.
  6911. ((org-in-regexp org-bracket-link-regexp 1)
  6912. ;; We do have a link at point, and we are going to edit it.
  6913. (setq remove (list (match-beginning 0) (match-end 0)))
  6914. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6915. (setq link (read-string "Link: "
  6916. (org-link-unescape
  6917. (org-match-string-no-properties 1)))))
  6918. ((or (org-in-regexp org-angle-link-re)
  6919. (org-in-regexp org-plain-link-re))
  6920. ;; Convert to bracket link
  6921. (setq remove (list (match-beginning 0) (match-end 0))
  6922. link (read-string "Link: "
  6923. (org-remove-angle-brackets (match-string 0)))))
  6924. ((member complete-file '((4) (16)))
  6925. ;; Completing read for file names.
  6926. (setq link (org-file-complete-link complete-file)))
  6927. (t
  6928. ;; Read link, with completion for stored links.
  6929. (with-output-to-temp-buffer "*Org Links*"
  6930. (princ "Insert a link.
  6931. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6932. (when org-stored-links
  6933. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6934. (princ (mapconcat
  6935. (lambda (x)
  6936. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6937. (reverse org-stored-links) "\n"))))
  6938. (let ((cw (selected-window)))
  6939. (select-window (get-buffer-window "*Org Links*"))
  6940. (setq truncate-lines t)
  6941. (unless (pos-visible-in-window-p (point-max))
  6942. (org-fit-window-to-buffer))
  6943. (and (window-live-p cw) (select-window cw)))
  6944. ;; Fake a link history, containing the stored links.
  6945. (setq tmphist (append (mapcar 'car org-stored-links)
  6946. org-insert-link-history))
  6947. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6948. (mapcar 'car org-link-abbrev-alist)
  6949. org-link-types))
  6950. (unwind-protect
  6951. (progn
  6952. (setq link
  6953. (let ((org-completion-use-ido nil))
  6954. (org-completing-read
  6955. "Link: "
  6956. (mapcar (lambda (x) (list (concat x ":")))
  6957. all-prefixes)
  6958. nil nil nil
  6959. 'tmphist
  6960. (or (car (car org-stored-links))))))
  6961. (if (or (member link all-prefixes)
  6962. (and (equal ":" (substring link -1))
  6963. (member (substring link 0 -1) all-prefixes)
  6964. (setq link (substring link 0 -1))))
  6965. (setq link (org-link-try-special-completion link))))
  6966. (set-window-configuration wcf)
  6967. (kill-buffer "*Org Links*"))
  6968. (setq entry (assoc link org-stored-links))
  6969. (or entry (push link org-insert-link-history))
  6970. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6971. (not org-keep-stored-link-after-insertion))
  6972. (setq org-stored-links (delq (assoc link org-stored-links)
  6973. org-stored-links)))
  6974. (setq desc (or desc (nth 1 entry)))))
  6975. (if (string-match org-plain-link-re link)
  6976. ;; URL-like link, normalize the use of angular brackets.
  6977. (setq link (org-make-link (org-remove-angle-brackets link))))
  6978. ;; Check if we are linking to the current file with a search option
  6979. ;; If yes, simplify the link by using only the search option.
  6980. (when (and buffer-file-name
  6981. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6982. (let* ((path (match-string 1 link))
  6983. (case-fold-search nil)
  6984. (search (match-string 2 link)))
  6985. (save-match-data
  6986. (if (equal (file-truename buffer-file-name) (file-truename path))
  6987. ;; We are linking to this same file, with a search option
  6988. (setq link search)))))
  6989. ;; Check if we can/should use a relative path. If yes, simplify the link
  6990. (when (string-match "^file:\\(.*\\)" link)
  6991. (let* ((path (match-string 1 link))
  6992. (origpath path)
  6993. (case-fold-search nil))
  6994. (cond
  6995. ((or (eq org-link-file-path-type 'absolute)
  6996. (equal complete-file '(16)))
  6997. (setq path (abbreviate-file-name (expand-file-name path))))
  6998. ((eq org-link-file-path-type 'noabbrev)
  6999. (setq path (expand-file-name path)))
  7000. ((eq org-link-file-path-type 'relative)
  7001. (setq path (file-relative-name path)))
  7002. (t
  7003. (save-match-data
  7004. (if (string-match (concat "^" (regexp-quote
  7005. (file-name-as-directory
  7006. (expand-file-name "."))))
  7007. (expand-file-name path))
  7008. ;; We are linking a file with relative path name.
  7009. (setq path (substring (expand-file-name path)
  7010. (match-end 0)))
  7011. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7012. (setq link (concat "file:" path))
  7013. (if (equal desc origpath)
  7014. (setq desc path))))
  7015. (if org-make-link-description-function
  7016. (setq desc (funcall org-make-link-description-function link desc)))
  7017. (setq desc (read-string "Description: " desc))
  7018. (unless (string-match "\\S-" desc) (setq desc nil))
  7019. (if remove (apply 'delete-region remove))
  7020. (insert (org-make-link-string link desc))))
  7021. (defun org-link-try-special-completion (type)
  7022. "If there is completion support for link type TAPE, offer it."
  7023. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7024. (if (functionp fun)
  7025. (funcall fun)
  7026. (read-string "Link (no completion support): " (concat type ":")))))
  7027. (defun org-file-complete-link (&optional arg)
  7028. "Create a file link using completion."
  7029. (let (file link)
  7030. (setq file (read-file-name "File: "))
  7031. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7032. (pwd1 (file-name-as-directory (abbreviate-file-name
  7033. (expand-file-name ".")))))
  7034. (cond
  7035. ((equal arg '(16))
  7036. (setq link (org-make-link
  7037. "file:"
  7038. (abbreviate-file-name (expand-file-name file)))))
  7039. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7040. (setq link (org-make-link "file:" (match-string 1 file))))
  7041. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7042. (expand-file-name file))
  7043. (setq link (org-make-link
  7044. "file:" (match-string 1 (expand-file-name file)))))
  7045. (t (setq link (org-make-link "file:" file)))))
  7046. link))
  7047. (defun org-completing-read (&rest args)
  7048. "Completing-read with SPACE being a normal character."
  7049. (let ((minibuffer-local-completion-map
  7050. (copy-keymap minibuffer-local-completion-map)))
  7051. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7052. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7053. (apply 'org-ido-completing-read args)))
  7054. (defun org-completing-read-no-ido (&rest args)
  7055. (let (org-completion-use-ido)
  7056. (apply 'org-completing-read args)))
  7057. (defun org-ido-completing-read (&rest args)
  7058. "Completing-read using `ido-mode' speedups if available"
  7059. (if (and org-completion-use-ido
  7060. (fboundp 'ido-completing-read)
  7061. (boundp 'ido-mode) ido-mode
  7062. (listp (second args)))
  7063. (let ((ido-enter-matching-directory nil))
  7064. (apply 'ido-completing-read (concat (car args))
  7065. (mapcar (lambda (x) (car x)) (nth 1 args))
  7066. (cddr args)))
  7067. (apply 'completing-read args)))
  7068. (defun org-extract-attributes (s)
  7069. "Extract the attributes cookie from a string and set as text property."
  7070. (let (a attr (start 0) key value)
  7071. (save-match-data
  7072. (when (string-match "{{\\([^}]+\\)}}$" s)
  7073. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7074. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7075. (setq key (match-string 1 a) value (match-string 2 a)
  7076. start (match-end 0)
  7077. attr (plist-put attr (intern key) value))))
  7078. (org-add-props s nil 'org-attr attr))
  7079. s))
  7080. (defun org-extract-attributes-from-string (tag)
  7081. (let (key value attr)
  7082. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7083. (setq key (match-string 1 tag) value (match-string 2 tag)
  7084. tag (replace-match "" t t tag)
  7085. attr (plist-put attr (intern key) value)))
  7086. (cons tag attr)))
  7087. (defun org-attributes-to-string (plist)
  7088. "Format a property list into an HTML attribute list."
  7089. (let ((s "") key value)
  7090. (while plist
  7091. (setq key (pop plist) value (pop plist))
  7092. (and value
  7093. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7094. s))
  7095. ;;; Opening/following a link
  7096. (defvar org-link-search-failed nil)
  7097. (defun org-next-link ()
  7098. "Move forward to the next link.
  7099. If the link is in hidden text, expose it."
  7100. (interactive)
  7101. (when (and org-link-search-failed (eq this-command last-command))
  7102. (goto-char (point-min))
  7103. (message "Link search wrapped back to beginning of buffer"))
  7104. (setq org-link-search-failed nil)
  7105. (let* ((pos (point))
  7106. (ct (org-context))
  7107. (a (assoc :link ct)))
  7108. (if a (goto-char (nth 2 a)))
  7109. (if (re-search-forward org-any-link-re nil t)
  7110. (progn
  7111. (goto-char (match-beginning 0))
  7112. (if (org-invisible-p) (org-show-context)))
  7113. (goto-char pos)
  7114. (setq org-link-search-failed t)
  7115. (error "No further link found"))))
  7116. (defun org-previous-link ()
  7117. "Move backward to the previous link.
  7118. If the link is in hidden text, expose it."
  7119. (interactive)
  7120. (when (and org-link-search-failed (eq this-command last-command))
  7121. (goto-char (point-max))
  7122. (message "Link search wrapped back to end of buffer"))
  7123. (setq org-link-search-failed nil)
  7124. (let* ((pos (point))
  7125. (ct (org-context))
  7126. (a (assoc :link ct)))
  7127. (if a (goto-char (nth 1 a)))
  7128. (if (re-search-backward org-any-link-re nil t)
  7129. (progn
  7130. (goto-char (match-beginning 0))
  7131. (if (org-invisible-p) (org-show-context)))
  7132. (goto-char pos)
  7133. (setq org-link-search-failed t)
  7134. (error "No further link found"))))
  7135. (defun org-translate-link (s)
  7136. "Translate a link string if a translation function has been defined."
  7137. (if (and org-link-translation-function
  7138. (fboundp org-link-translation-function)
  7139. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7140. (progn
  7141. (setq s (funcall org-link-translation-function
  7142. (match-string 1) (match-string 2)))
  7143. (concat (car s) ":" (cdr s)))
  7144. s))
  7145. (defun org-translate-link-from-planner (type path)
  7146. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7147. This is still an experimental function, your mileage may vary."
  7148. (cond
  7149. ((member type '("http" "https" "news" "ftp"))
  7150. ;; standard Internet links are the same.
  7151. nil)
  7152. ((and (equal type "irc") (string-match "^//" path))
  7153. ;; Planner has two / at the beginning of an irc link, we have 1.
  7154. ;; We should have zero, actually....
  7155. (setq path (substring path 1)))
  7156. ((and (equal type "lisp") (string-match "^/" path))
  7157. ;; Planner has a slash, we do not.
  7158. (setq type "elisp" path (substring path 1)))
  7159. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7160. ;; A typical message link. Planner has the id after the fina slash,
  7161. ;; we separate it with a hash mark
  7162. (setq path (concat (match-string 1 path) "#"
  7163. (org-remove-angle-brackets (match-string 2 path)))))
  7164. )
  7165. (cons type path))
  7166. (defun org-find-file-at-mouse (ev)
  7167. "Open file link or URL at mouse."
  7168. (interactive "e")
  7169. (mouse-set-point ev)
  7170. (org-open-at-point 'in-emacs))
  7171. (defun org-open-at-mouse (ev)
  7172. "Open file link or URL at mouse."
  7173. (interactive "e")
  7174. (mouse-set-point ev)
  7175. (if (eq major-mode 'org-agenda-mode)
  7176. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7177. (org-open-at-point))
  7178. (defvar org-window-config-before-follow-link nil
  7179. "The window configuration before following a link.
  7180. This is saved in case the need arises to restore it.")
  7181. (defvar org-open-link-marker (make-marker)
  7182. "Marker pointing to the location where `org-open-at-point; was called.")
  7183. ;;;###autoload
  7184. (defun org-open-at-point-global ()
  7185. "Follow a link like Org-mode does.
  7186. This command can be called in any mode to follow a link that has
  7187. Org-mode syntax."
  7188. (interactive)
  7189. (org-run-like-in-org-mode 'org-open-at-point))
  7190. ;;;###autoload
  7191. (defun org-open-link-from-string (s &optional arg)
  7192. "Open a link in the string S, as if it was in Org-mode."
  7193. (interactive "sLink: \nP")
  7194. (let ((reference-buffer (current-buffer)))
  7195. (with-temp-buffer
  7196. (let ((org-inhibit-startup t))
  7197. (org-mode)
  7198. (insert s)
  7199. (goto-char (point-min))
  7200. (org-open-at-point arg reference-buffer)))))
  7201. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7202. "Open link at or after point.
  7203. If there is no link at point, this function will search forward up to
  7204. the end of the current line.
  7205. Normally, files will be opened by an appropriate application. If the
  7206. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7207. With a double prefix argument, try to open outside of Emacs, in the
  7208. application the system uses for this file type."
  7209. (interactive "P")
  7210. (org-load-modules-maybe)
  7211. (move-marker org-open-link-marker (point))
  7212. (setq org-window-config-before-follow-link (current-window-configuration))
  7213. (org-remove-occur-highlights nil nil t)
  7214. (cond
  7215. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7216. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7217. (org-footnote-action))
  7218. (t
  7219. (let (type path link line search (pos (point)))
  7220. (catch 'match
  7221. (save-excursion
  7222. (skip-chars-forward "^]\n\r")
  7223. (when (org-in-regexp org-bracket-link-regexp)
  7224. (setq link (org-extract-attributes
  7225. (org-link-unescape (org-match-string-no-properties 1))))
  7226. (while (string-match " *\n *" link)
  7227. (setq link (replace-match " " t t link)))
  7228. (setq link (org-link-expand-abbrev link))
  7229. (cond
  7230. ((or (file-name-absolute-p link)
  7231. (string-match "^\\.\\.?/" link))
  7232. (setq type "file" path link))
  7233. ((string-match org-link-re-with-space3 link)
  7234. (setq type (match-string 1 link) path (match-string 2 link)))
  7235. (t (setq type "thisfile" path link)))
  7236. (throw 'match t)))
  7237. (when (get-text-property (point) 'org-linked-text)
  7238. (setq type "thisfile"
  7239. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7240. (1+ (point)) (point))
  7241. path (buffer-substring
  7242. (previous-single-property-change pos 'org-linked-text)
  7243. (next-single-property-change pos 'org-linked-text)))
  7244. (throw 'match t))
  7245. (save-excursion
  7246. (when (or (org-in-regexp org-angle-link-re)
  7247. (org-in-regexp org-plain-link-re))
  7248. (setq type (match-string 1) path (match-string 2))
  7249. (throw 'match t)))
  7250. (save-excursion
  7251. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7252. (setq type "tags"
  7253. path (match-string 1))
  7254. (while (string-match ":" path)
  7255. (setq path (replace-match "+" t t path)))
  7256. (throw 'match t)))
  7257. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7258. (setq type "tree-match"
  7259. path (match-string 1))
  7260. (throw 'match t)))
  7261. (unless path
  7262. (error "No link found"))
  7263. ;; switch back to reference buffer
  7264. ;; needed when if called in a temporary buffer through
  7265. ;; org-open-link-from-string
  7266. (and reference-buffer (switch-to-buffer reference-buffer))
  7267. ;; Remove any trailing spaces in path
  7268. (if (string-match " +\\'" path)
  7269. (setq path (replace-match "" t t path)))
  7270. (if (and org-link-translation-function
  7271. (fboundp org-link-translation-function))
  7272. ;; Check if we need to translate the link
  7273. (let ((tmp (funcall org-link-translation-function type path)))
  7274. (setq type (car tmp) path (cdr tmp))))
  7275. (cond
  7276. ((assoc type org-link-protocols)
  7277. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7278. ((equal type "mailto")
  7279. (let ((cmd (car org-link-mailto-program))
  7280. (args (cdr org-link-mailto-program)) args1
  7281. (address path) (subject "") a)
  7282. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7283. (setq address (match-string 1 path)
  7284. subject (org-link-escape (match-string 2 path))))
  7285. (while args
  7286. (cond
  7287. ((not (stringp (car args))) (push (pop args) args1))
  7288. (t (setq a (pop args))
  7289. (if (string-match "%a" a)
  7290. (setq a (replace-match address t t a)))
  7291. (if (string-match "%s" a)
  7292. (setq a (replace-match subject t t a)))
  7293. (push a args1))))
  7294. (apply cmd (nreverse args1))))
  7295. ((member type '("http" "https" "ftp" "news"))
  7296. (browse-url (concat type ":" (org-link-escape
  7297. path org-link-escape-chars-browser))))
  7298. ((member type '("message"))
  7299. (browse-url (concat type ":" path)))
  7300. ((string= type "tags")
  7301. (org-tags-view in-emacs path))
  7302. ((string= type "thisfile")
  7303. (if in-emacs
  7304. (switch-to-buffer-other-window
  7305. (org-get-buffer-for-internal-link (current-buffer)))
  7306. (org-mark-ring-push))
  7307. (let ((cmd `(org-link-search
  7308. ,path
  7309. ,(cond ((equal in-emacs '(4)) 'occur)
  7310. ((equal in-emacs '(16)) 'org-occur)
  7311. (t nil))
  7312. ,pos)))
  7313. (condition-case nil (eval cmd)
  7314. (error (progn (widen) (eval cmd))))))
  7315. ((string= type "tree-match")
  7316. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7317. ((string= type "file")
  7318. (if (string-match "::\\([0-9]+\\)\\'" path)
  7319. (setq line (string-to-number (match-string 1 path))
  7320. path (substring path 0 (match-beginning 0)))
  7321. (if (string-match "::\\(.+\\)\\'" path)
  7322. (setq search (match-string 1 path)
  7323. path (substring path 0 (match-beginning 0)))))
  7324. (if (string-match "[*?{]" (file-name-nondirectory path))
  7325. (dired path)
  7326. (org-open-file path in-emacs line search)))
  7327. ((string= type "news")
  7328. (require 'org-gnus)
  7329. (org-gnus-follow-link path))
  7330. ((string= type "shell")
  7331. (let ((cmd path))
  7332. (if (or (not org-confirm-shell-link-function)
  7333. (funcall org-confirm-shell-link-function
  7334. (format "Execute \"%s\" in shell? "
  7335. (org-add-props cmd nil
  7336. 'face 'org-warning))))
  7337. (progn
  7338. (message "Executing %s" cmd)
  7339. (shell-command cmd))
  7340. (error "Abort"))))
  7341. ((string= type "elisp")
  7342. (let ((cmd path))
  7343. (if (or (not org-confirm-elisp-link-function)
  7344. (funcall org-confirm-elisp-link-function
  7345. (format "Execute \"%s\" as elisp? "
  7346. (org-add-props cmd nil
  7347. 'face 'org-warning))))
  7348. (message "%s => %s" cmd
  7349. (if (equal (string-to-char cmd) ?\()
  7350. (eval (read cmd))
  7351. (call-interactively (read cmd))))
  7352. (error "Abort"))))
  7353. (t
  7354. (browse-url-at-point))))))
  7355. (move-marker org-open-link-marker nil)
  7356. (run-hook-with-args 'org-follow-link-hook))
  7357. ;;;; Time estimates
  7358. (defun org-get-effort (&optional pom)
  7359. "Get the effort estimate for the current entry."
  7360. (org-entry-get pom org-effort-property))
  7361. ;;; File search
  7362. (defvar org-create-file-search-functions nil
  7363. "List of functions to construct the right search string for a file link.
  7364. These functions are called in turn with point at the location to
  7365. which the link should point.
  7366. A function in the hook should first test if it would like to
  7367. handle this file type, for example by checking the major-mode or
  7368. the file extension. If it decides not to handle this file, it
  7369. should just return nil to give other functions a chance. If it
  7370. does handle the file, it must return the search string to be used
  7371. when following the link. The search string will be part of the
  7372. file link, given after a double colon, and `org-open-at-point'
  7373. will automatically search for it. If special measures must be
  7374. taken to make the search successful, another function should be
  7375. added to the companion hook `org-execute-file-search-functions',
  7376. which see.
  7377. A function in this hook may also use `setq' to set the variable
  7378. `description' to provide a suggestion for the descriptive text to
  7379. be used for this link when it gets inserted into an Org-mode
  7380. buffer with \\[org-insert-link].")
  7381. (defvar org-execute-file-search-functions nil
  7382. "List of functions to execute a file search triggered by a link.
  7383. Functions added to this hook must accept a single argument, the
  7384. search string that was part of the file link, the part after the
  7385. double colon. The function must first check if it would like to
  7386. handle this search, for example by checking the major-mode or the
  7387. file extension. If it decides not to handle this search, it
  7388. should just return nil to give other functions a chance. If it
  7389. does handle the search, it must return a non-nil value to keep
  7390. other functions from trying.
  7391. Each function can access the current prefix argument through the
  7392. variable `current-prefix-argument'. Note that a single prefix is
  7393. used to force opening a link in Emacs, so it may be good to only
  7394. use a numeric or double prefix to guide the search function.
  7395. In case this is needed, a function in this hook can also restore
  7396. the window configuration before `org-open-at-point' was called using:
  7397. (set-window-configuration org-window-config-before-follow-link)")
  7398. (defun org-link-search (s &optional type avoid-pos)
  7399. "Search for a link search option.
  7400. If S is surrounded by forward slashes, it is interpreted as a
  7401. regular expression. In org-mode files, this will create an `org-occur'
  7402. sparse tree. In ordinary files, `occur' will be used to list matches.
  7403. If the current buffer is in `dired-mode', grep will be used to search
  7404. in all files. If AVOID-POS is given, ignore matches near that position."
  7405. (let ((case-fold-search t)
  7406. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7407. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7408. (append '(("") (" ") ("\t") ("\n"))
  7409. org-emphasis-alist)
  7410. "\\|") "\\)"))
  7411. (pos (point))
  7412. (pre nil) (post nil)
  7413. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7414. (cond
  7415. ;; First check if there are any special
  7416. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7417. ;; Now try the builtin stuff
  7418. ((and (equal (string-to-char s0) ?#)
  7419. (> (length s0) 1)
  7420. (save-excursion
  7421. (goto-char (point-min))
  7422. (and
  7423. (re-search-forward
  7424. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7425. (setq type 'dedicated
  7426. pos (match-beginning 0))))
  7427. ;; There is an exact target for this
  7428. (goto-char pos)
  7429. (org-back-to-heading t)))
  7430. ((save-excursion
  7431. (goto-char (point-min))
  7432. (and
  7433. (re-search-forward
  7434. (concat "<<" (regexp-quote s0) ">>") nil t)
  7435. (setq type 'dedicated
  7436. pos (match-beginning 0))))
  7437. ;; There is an exact target for this
  7438. (goto-char pos))
  7439. ((and (string-match "^(\\(.*\\))$" s0)
  7440. (save-excursion
  7441. (goto-char (point-min))
  7442. (and
  7443. (re-search-forward
  7444. (concat "[^[]" (regexp-quote
  7445. (format org-coderef-label-format
  7446. (match-string 1 s0))))
  7447. nil t)
  7448. (setq type 'dedicated
  7449. pos (1+ (match-beginning 0))))))
  7450. ;; There is a coderef target for this
  7451. (goto-char pos))
  7452. ((string-match "^/\\(.*\\)/$" s)
  7453. ;; A regular expression
  7454. (cond
  7455. ((org-mode-p)
  7456. (org-occur (match-string 1 s)))
  7457. ;;((eq major-mode 'dired-mode)
  7458. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7459. (t (org-do-occur (match-string 1 s)))))
  7460. (t
  7461. ;; A normal search strings
  7462. (when (equal (string-to-char s) ?*)
  7463. ;; Anchor on headlines, post may include tags.
  7464. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7465. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7466. s (substring s 1)))
  7467. (remove-text-properties
  7468. 0 (length s)
  7469. '(face nil mouse-face nil keymap nil fontified nil) s)
  7470. ;; Make a series of regular expressions to find a match
  7471. (setq words (org-split-string s "[ \n\r\t]+")
  7472. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7473. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7474. "\\)" markers)
  7475. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7476. re2a (concat "[ \t\r\n]" re2a_)
  7477. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7478. re4 (concat "[^a-zA-Z_]" re4_)
  7479. re1 (concat pre re2 post)
  7480. re3 (concat pre (if pre re4_ re4) post)
  7481. re5 (concat pre ".*" re4)
  7482. re2 (concat pre re2)
  7483. re2a (concat pre (if pre re2a_ re2a))
  7484. re4 (concat pre (if pre re4_ re4))
  7485. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7486. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7487. re5 "\\)"
  7488. ))
  7489. (cond
  7490. ((eq type 'org-occur) (org-occur reall))
  7491. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7492. (t (goto-char (point-min))
  7493. (setq type 'fuzzy)
  7494. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7495. (org-search-not-self 1 re1 nil t)
  7496. (org-search-not-self 1 re2 nil t)
  7497. (org-search-not-self 1 re2a nil t)
  7498. (org-search-not-self 1 re3 nil t)
  7499. (org-search-not-self 1 re4 nil t)
  7500. (org-search-not-self 1 re5 nil t)
  7501. )
  7502. (goto-char (match-beginning 1))
  7503. (goto-char pos)
  7504. (error "No match")))))
  7505. (t
  7506. ;; Normal string-search
  7507. (goto-char (point-min))
  7508. (if (search-forward s nil t)
  7509. (goto-char (match-beginning 0))
  7510. (error "No match"))))
  7511. (and (org-mode-p) (org-show-context 'link-search))
  7512. type))
  7513. (defun org-search-not-self (group &rest args)
  7514. "Execute `re-search-forward', but only accept matches that do not
  7515. enclose the position of `org-open-link-marker'."
  7516. (let ((m org-open-link-marker))
  7517. (catch 'exit
  7518. (while (apply 're-search-forward args)
  7519. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7520. (goto-char (match-end group))
  7521. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7522. (> (match-beginning 0) (marker-position m))
  7523. (< (match-end 0) (marker-position m)))
  7524. (save-match-data
  7525. (or (not (org-in-regexp
  7526. org-bracket-link-analytic-regexp 1))
  7527. (not (match-end 4)) ; no description
  7528. (and (<= (match-beginning 4) (point))
  7529. (>= (match-end 4) (point))))))
  7530. (throw 'exit (point))))))))
  7531. (defun org-get-buffer-for-internal-link (buffer)
  7532. "Return a buffer to be used for displaying the link target of internal links."
  7533. (cond
  7534. ((not org-display-internal-link-with-indirect-buffer)
  7535. buffer)
  7536. ((string-match "(Clone)$" (buffer-name buffer))
  7537. (message "Buffer is already a clone, not making another one")
  7538. ;; we also do not modify visibility in this case
  7539. buffer)
  7540. (t ; make a new indirect buffer for displaying the link
  7541. (let* ((bn (buffer-name buffer))
  7542. (ibn (concat bn "(Clone)"))
  7543. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7544. (with-current-buffer ib (org-overview))
  7545. ib))))
  7546. (defun org-do-occur (regexp &optional cleanup)
  7547. "Call the Emacs command `occur'.
  7548. If CLEANUP is non-nil, remove the printout of the regular expression
  7549. in the *Occur* buffer. This is useful if the regex is long and not useful
  7550. to read."
  7551. (occur regexp)
  7552. (when cleanup
  7553. (let ((cwin (selected-window)) win beg end)
  7554. (when (setq win (get-buffer-window "*Occur*"))
  7555. (select-window win))
  7556. (goto-char (point-min))
  7557. (when (re-search-forward "match[a-z]+" nil t)
  7558. (setq beg (match-end 0))
  7559. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7560. (setq end (1- (match-beginning 0)))))
  7561. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7562. (goto-char (point-min))
  7563. (select-window cwin))))
  7564. ;;; The mark ring for links jumps
  7565. (defvar org-mark-ring nil
  7566. "Mark ring for positions before jumps in Org-mode.")
  7567. (defvar org-mark-ring-last-goto nil
  7568. "Last position in the mark ring used to go back.")
  7569. ;; Fill and close the ring
  7570. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7571. (loop for i from 1 to org-mark-ring-length do
  7572. (push (make-marker) org-mark-ring))
  7573. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7574. org-mark-ring)
  7575. (defun org-mark-ring-push (&optional pos buffer)
  7576. "Put the current position or POS into the mark ring and rotate it."
  7577. (interactive)
  7578. (setq pos (or pos (point)))
  7579. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7580. (move-marker (car org-mark-ring)
  7581. (or pos (point))
  7582. (or buffer (current-buffer)))
  7583. (message "%s"
  7584. (substitute-command-keys
  7585. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7586. (defun org-mark-ring-goto (&optional n)
  7587. "Jump to the previous position in the mark ring.
  7588. With prefix arg N, jump back that many stored positions. When
  7589. called several times in succession, walk through the entire ring.
  7590. Org-mode commands jumping to a different position in the current file,
  7591. or to another Org-mode file, automatically push the old position
  7592. onto the ring."
  7593. (interactive "p")
  7594. (let (p m)
  7595. (if (eq last-command this-command)
  7596. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7597. (setq p org-mark-ring))
  7598. (setq org-mark-ring-last-goto p)
  7599. (setq m (car p))
  7600. (switch-to-buffer (marker-buffer m))
  7601. (goto-char m)
  7602. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7603. (defun org-remove-angle-brackets (s)
  7604. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7605. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7606. s)
  7607. (defun org-add-angle-brackets (s)
  7608. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7609. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7610. s)
  7611. (defun org-remove-double-quotes (s)
  7612. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7613. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7614. s)
  7615. ;;; Following specific links
  7616. (defun org-follow-timestamp-link ()
  7617. (cond
  7618. ((org-at-date-range-p t)
  7619. (let ((org-agenda-start-on-weekday)
  7620. (t1 (match-string 1))
  7621. (t2 (match-string 2)))
  7622. (setq t1 (time-to-days (org-time-string-to-time t1))
  7623. t2 (time-to-days (org-time-string-to-time t2)))
  7624. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7625. ((org-at-timestamp-p t)
  7626. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7627. (substring (match-string 1) 0 10)))
  7628. 1))
  7629. (t (error "This should not happen"))))
  7630. ;;; Following file links
  7631. (defvar org-wait nil)
  7632. (defun org-open-file (path &optional in-emacs line search)
  7633. "Open the file at PATH.
  7634. First, this expands any special file name abbreviations. Then the
  7635. configuration variable `org-file-apps' is checked if it contains an
  7636. entry for this file type, and if yes, the corresponding command is launched.
  7637. If no application is found, Emacs simply visits the file.
  7638. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7639. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7640. and o use an external application to visit the file.
  7641. Optional LINE specifies a line to go to, optional SEARCH a string to
  7642. search for. If LINE or SEARCH is given, the file will always be
  7643. opened in Emacs.
  7644. If the file does not exist, an error is thrown."
  7645. (setq in-emacs (or in-emacs line search))
  7646. (let* ((file (if (equal path "")
  7647. buffer-file-name
  7648. (substitute-in-file-name (expand-file-name path))))
  7649. (apps (append org-file-apps (org-default-apps)))
  7650. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7651. (dirp (if remp nil (file-directory-p file)))
  7652. (file (if (and dirp org-open-directory-means-index-dot-org)
  7653. (concat (file-name-as-directory file) "index.org")
  7654. file))
  7655. (a-m-a-p (assq 'auto-mode apps))
  7656. (dfile (downcase file))
  7657. (old-buffer (current-buffer))
  7658. (old-pos (point))
  7659. (old-mode major-mode)
  7660. ext cmd)
  7661. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7662. (setq ext (match-string 1 dfile))
  7663. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7664. (setq ext (match-string 1 dfile))))
  7665. (cond
  7666. ((equal in-emacs '(16))
  7667. (setq cmd (cdr (assoc 'system apps))))
  7668. (in-emacs (setq cmd 'emacs))
  7669. (t
  7670. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7671. (and dirp (cdr (assoc 'directory apps)))
  7672. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7673. 'string-match)
  7674. (cdr (assoc ext apps))
  7675. (cdr (assoc t apps))))))
  7676. (when (eq cmd 'system)
  7677. (setq cmd (cdr (assoc 'system apps))))
  7678. (when (eq cmd 'default)
  7679. (setq cmd (cdr (assoc t apps))))
  7680. (when (eq cmd 'mailcap)
  7681. (require 'mailcap)
  7682. (mailcap-parse-mailcaps)
  7683. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7684. (command (mailcap-mime-info mime-type)))
  7685. (if (stringp command)
  7686. (setq cmd command)
  7687. (setq cmd 'emacs))))
  7688. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7689. (not (file-exists-p file))
  7690. (not org-open-non-existing-files))
  7691. (error "No such file: %s" file))
  7692. (cond
  7693. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7694. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7695. (while (string-match "['\"]%s['\"]" cmd)
  7696. (setq cmd (replace-match "%s" t t cmd)))
  7697. (while (string-match "%s" cmd)
  7698. (setq cmd (replace-match
  7699. (save-match-data
  7700. (shell-quote-argument
  7701. (convert-standard-filename file)))
  7702. t t cmd)))
  7703. (save-window-excursion
  7704. (start-process-shell-command cmd nil cmd)
  7705. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7706. ))
  7707. ((or (stringp cmd)
  7708. (eq cmd 'emacs))
  7709. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7710. (widen)
  7711. (if line (goto-line line)
  7712. (if search (org-link-search search))))
  7713. ((consp cmd)
  7714. (let ((file (convert-standard-filename file)))
  7715. (eval cmd)))
  7716. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7717. (and (org-mode-p) (eq old-mode 'org-mode)
  7718. (or (not (equal old-buffer (current-buffer)))
  7719. (not (equal old-pos (point))))
  7720. (org-mark-ring-push old-pos old-buffer))))
  7721. (defun org-default-apps ()
  7722. "Return the default applications for this operating system."
  7723. (cond
  7724. ((eq system-type 'darwin)
  7725. org-file-apps-defaults-macosx)
  7726. ((eq system-type 'windows-nt)
  7727. org-file-apps-defaults-windowsnt)
  7728. (t org-file-apps-defaults-gnu)))
  7729. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7730. "Convert extensions to regular expressions in the cars of LIST.
  7731. Also, weed out any non-string entries, because the return value is used
  7732. only for regexp matching.
  7733. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7734. point to the symbol `emacs', indicating that the file should
  7735. be opened in Emacs."
  7736. (append
  7737. (delq nil
  7738. (mapcar (lambda (x)
  7739. (if (not (stringp (car x)))
  7740. nil
  7741. (if (string-match "\\W" (car x))
  7742. x
  7743. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7744. list))
  7745. (if add-auto-mode
  7746. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7747. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7748. (defun org-file-remote-p (file)
  7749. "Test whether FILE specifies a location on a remote system.
  7750. Return non-nil if the location is indeed remote.
  7751. For example, the filename \"/user@host:/foo\" specifies a location
  7752. on the system \"/user@host:\"."
  7753. (cond ((fboundp 'file-remote-p)
  7754. (file-remote-p file))
  7755. ((fboundp 'tramp-handle-file-remote-p)
  7756. (tramp-handle-file-remote-p file))
  7757. ((and (boundp 'ange-ftp-name-format)
  7758. (string-match (car ange-ftp-name-format) file))
  7759. t)
  7760. (t nil)))
  7761. ;;;; Refiling
  7762. (defun org-get-org-file ()
  7763. "Read a filename, with default directory `org-directory'."
  7764. (let ((default (or org-default-notes-file remember-data-file)))
  7765. (read-file-name (format "File name [%s]: " default)
  7766. (file-name-as-directory org-directory)
  7767. default)))
  7768. (defun org-notes-order-reversed-p ()
  7769. "Check if the current file should receive notes in reversed order."
  7770. (cond
  7771. ((not org-reverse-note-order) nil)
  7772. ((eq t org-reverse-note-order) t)
  7773. ((not (listp org-reverse-note-order)) nil)
  7774. (t (catch 'exit
  7775. (let ((all org-reverse-note-order)
  7776. entry)
  7777. (while (setq entry (pop all))
  7778. (if (string-match (car entry) buffer-file-name)
  7779. (throw 'exit (cdr entry))))
  7780. nil)))))
  7781. (defvar org-refile-target-table nil
  7782. "The list of refile targets, created by `org-refile'.")
  7783. (defvar org-agenda-new-buffers nil
  7784. "Buffers created to visit agenda files.")
  7785. (defun org-get-refile-targets (&optional default-buffer)
  7786. "Produce a table with refile targets."
  7787. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7788. targets txt re files f desc descre fast-path-p level pos0)
  7789. (message "Getting targets...")
  7790. (with-current-buffer (or default-buffer (current-buffer))
  7791. (while (setq entry (pop entries))
  7792. (setq files (car entry) desc (cdr entry))
  7793. (setq fast-path-p nil)
  7794. (cond
  7795. ((null files) (setq files (list (current-buffer))))
  7796. ((eq files 'org-agenda-files)
  7797. (setq files (org-agenda-files 'unrestricted)))
  7798. ((and (symbolp files) (fboundp files))
  7799. (setq files (funcall files)))
  7800. ((and (symbolp files) (boundp files))
  7801. (setq files (symbol-value files))))
  7802. (if (stringp files) (setq files (list files)))
  7803. (cond
  7804. ((eq (car desc) :tag)
  7805. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7806. ((eq (car desc) :todo)
  7807. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7808. ((eq (car desc) :regexp)
  7809. (setq descre (cdr desc)))
  7810. ((eq (car desc) :level)
  7811. (setq descre (concat "^\\*\\{" (number-to-string
  7812. (if org-odd-levels-only
  7813. (1- (* 2 (cdr desc)))
  7814. (cdr desc)))
  7815. "\\}[ \t]")))
  7816. ((eq (car desc) :maxlevel)
  7817. (setq fast-path-p t)
  7818. (setq descre (concat "^\\*\\{1," (number-to-string
  7819. (if org-odd-levels-only
  7820. (1- (* 2 (cdr desc)))
  7821. (cdr desc)))
  7822. "\\}[ \t]")))
  7823. (t (error "Bad refiling target description %s" desc)))
  7824. (while (setq f (pop files))
  7825. (save-excursion
  7826. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7827. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7828. (setq f (expand-file-name f))
  7829. (if (eq org-refile-use-outline-path 'file)
  7830. (push (list (file-name-nondirectory f) f nil nil) targets))
  7831. (save-excursion
  7832. (save-restriction
  7833. (widen)
  7834. (goto-char (point-min))
  7835. (while (re-search-forward descre nil t)
  7836. (goto-char (setq pos0 (point-at-bol)))
  7837. (catch 'next
  7838. (when org-refile-target-verify-function
  7839. (save-match-data
  7840. (or (funcall org-refile-target-verify-function)
  7841. (throw 'next t))))
  7842. (when (looking-at org-complex-heading-regexp)
  7843. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7844. txt (org-link-display-format (match-string 4))
  7845. re (concat "^" (regexp-quote
  7846. (buffer-substring (match-beginning 1)
  7847. (match-end 4)))))
  7848. (if (match-end 5) (setq re (concat re "[ \t]+"
  7849. (regexp-quote
  7850. (match-string 5)))))
  7851. (setq re (concat re "[ \t]*$"))
  7852. (when org-refile-use-outline-path
  7853. (setq txt (mapconcat 'org-protect-slash
  7854. (append
  7855. (if (eq org-refile-use-outline-path 'file)
  7856. (list (file-name-nondirectory
  7857. (buffer-file-name (buffer-base-buffer))))
  7858. (if (eq org-refile-use-outline-path 'full-file-path)
  7859. (list (buffer-file-name (buffer-base-buffer)))))
  7860. (org-get-outline-path fast-path-p level txt)
  7861. (list txt))
  7862. "/")))
  7863. (push (list txt f re (point)) targets)))
  7864. (when (= (point) pos0)
  7865. ;; verification function has not moved point
  7866. (goto-char (point-at-eol))))))))))
  7867. (message "Getting targets...done")
  7868. (nreverse targets)))
  7869. (defun org-protect-slash (s)
  7870. (while (string-match "/" s)
  7871. (setq s (replace-match "\\" t t s)))
  7872. s)
  7873. (defvar org-olpa (make-vector 20 nil))
  7874. (defun org-get-outline-path (&optional fastp level heading)
  7875. "Return the outline path to the current entry, as a list."
  7876. (if fastp
  7877. (progn
  7878. (if (> level 19)
  7879. (error "Outline path failure, more than 19 levels."))
  7880. (loop for i from level upto 19 do
  7881. (aset org-olpa i nil))
  7882. (prog1
  7883. (delq nil (append org-olpa nil))
  7884. (aset org-olpa level heading)))
  7885. (let (rtn)
  7886. (save-excursion
  7887. (while (org-up-heading-safe)
  7888. (when (looking-at org-complex-heading-regexp)
  7889. (push (org-match-string-no-properties 4) rtn)))
  7890. rtn))))
  7891. (defvar org-refile-history nil
  7892. "History for refiling operations.")
  7893. (defvar org-after-refile-insert-hook nil
  7894. "Hook run after `org-refile' has inserted its stuff at the new location.
  7895. Note that this is still *before* the stuff will be removed from
  7896. the *old* location.")
  7897. (defun org-refile (&optional goto default-buffer)
  7898. "Move the entry at point to another heading.
  7899. The list of target headings is compiled using the information in
  7900. `org-refile-targets', which see. This list is created before each use
  7901. and will therefore always be up-to-date.
  7902. At the target location, the entry is filed as a subitem of the target heading.
  7903. Depending on `org-reverse-note-order', the new subitem will either be the
  7904. first or the last subitem.
  7905. If there is an active region, all entries in that region will be moved.
  7906. However, the region must fulfil the requirement that the first heading
  7907. is the first one sets the top-level of the moved text - at most siblings
  7908. below it are allowed.
  7909. With prefix arg GOTO, the command will only visit the target location,
  7910. not actually move anything.
  7911. With a double prefix `C-u C-u', go to the location where the last refiling
  7912. operation has put the subtree.
  7913. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7914. (interactive "P")
  7915. (let* ((cbuf (current-buffer))
  7916. (regionp (org-region-active-p))
  7917. (region-start (and regionp (region-beginning)))
  7918. (region-end (and regionp (region-end)))
  7919. (region-length (and regionp (- region-end region-start)))
  7920. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7921. pos it nbuf file re level reversed)
  7922. (when regionp (goto-char region-start)
  7923. (unless (org-kill-is-subtree-p
  7924. (buffer-substring region-start region-end))
  7925. (error "The region is not a (sequence of) subtree(s)")))
  7926. (if (equal goto '(16))
  7927. (org-refile-goto-last-stored)
  7928. (when (setq it (org-refile-get-location
  7929. (if goto "Goto: " "Refile to: ") default-buffer
  7930. org-refile-allow-creating-parent-nodes))
  7931. (setq file (nth 1 it)
  7932. re (nth 2 it)
  7933. pos (nth 3 it))
  7934. (if (and (not goto)
  7935. pos
  7936. (equal (buffer-file-name) file)
  7937. (if regionp
  7938. (and (>= pos region-start)
  7939. (<= pos region-end))
  7940. (and (>= pos (point))
  7941. (< pos (save-excursion
  7942. (org-end-of-subtree t t))))))
  7943. (error "Cannot refile to position inside the tree or region"))
  7944. (setq nbuf (or (find-buffer-visiting file)
  7945. (find-file-noselect file)))
  7946. (if goto
  7947. (progn
  7948. (switch-to-buffer nbuf)
  7949. (goto-char pos)
  7950. (org-show-context 'org-goto))
  7951. (if regionp
  7952. (progn
  7953. (kill-new (buffer-substring region-start region-end))
  7954. (org-save-markers-in-region region-start region-end))
  7955. (org-copy-subtree 1 nil t))
  7956. (save-excursion
  7957. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7958. (find-file-noselect file))))
  7959. (setq reversed (org-notes-order-reversed-p))
  7960. (save-excursion
  7961. (save-restriction
  7962. (widen)
  7963. (if pos
  7964. (progn
  7965. (goto-char pos)
  7966. (looking-at outline-regexp)
  7967. (setq level (org-get-valid-level (funcall outline-level) 1))
  7968. (goto-char
  7969. (if reversed
  7970. (or (outline-next-heading) (point-max))
  7971. (or (save-excursion (outline-get-next-sibling))
  7972. (org-end-of-subtree t t)
  7973. (point-max)))))
  7974. (setq level 1)
  7975. (if (not reversed)
  7976. (goto-char (point-max))
  7977. (goto-char (point-min))
  7978. (or (outline-next-heading) (goto-char (point-max)))))
  7979. (if (not (bolp)) (newline))
  7980. (bookmark-set "org-refile-last-stored")
  7981. (org-paste-subtree level)
  7982. (run-hooks 'org-after-refile-insert-hook))))
  7983. (if regionp
  7984. (delete-region (point) (+ (point) region-length))
  7985. (org-cut-subtree))
  7986. (setq org-markers-to-move nil)
  7987. (message "Refiled to \"%s\"" (car it))))))
  7988. (org-reveal))
  7989. (defun org-refile-goto-last-stored ()
  7990. "Go to the location where the last refile was stored."
  7991. (interactive)
  7992. (bookmark-jump "org-refile-last-stored")
  7993. (message "This is the location of the last refile"))
  7994. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7995. "Prompt the user for a refile location, using PROMPT."
  7996. (let ((org-refile-targets org-refile-targets)
  7997. (org-refile-use-outline-path org-refile-use-outline-path))
  7998. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7999. (unless org-refile-target-table
  8000. (error "No refile targets"))
  8001. (let* ((cbuf (current-buffer))
  8002. (partial-completion-mode nil)
  8003. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8004. (cfunc (if (and org-refile-use-outline-path
  8005. org-outline-path-complete-in-steps)
  8006. 'org-olpath-completing-read
  8007. 'org-ido-completing-read))
  8008. (extra (if org-refile-use-outline-path "/" ""))
  8009. (filename (and cfn (expand-file-name cfn)))
  8010. (tbl (mapcar
  8011. (lambda (x)
  8012. (if (and (not (member org-refile-use-outline-path
  8013. '(file full-file-path)))
  8014. (not (equal filename (nth 1 x))))
  8015. (cons (concat (car x) extra " ("
  8016. (file-name-nondirectory (nth 1 x)) ")")
  8017. (cdr x))
  8018. (cons (concat (car x) extra) (cdr x))))
  8019. org-refile-target-table))
  8020. (completion-ignore-case t)
  8021. pa answ parent-target child parent)
  8022. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8023. nil 'org-refile-history))
  8024. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8025. (if pa
  8026. pa
  8027. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8028. (setq parent (match-string 1 answ)
  8029. child (match-string 2 answ))
  8030. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8031. (when (and parent-target
  8032. (or (eq new-nodes t)
  8033. (and (eq new-nodes 'confirm)
  8034. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8035. (org-refile-new-child parent-target child))))))
  8036. (defun org-refile-new-child (parent-target child)
  8037. "Use refile target PARENT-TARGET to add new CHILD below it."
  8038. (unless parent-target
  8039. (error "Cannot find parent for new node"))
  8040. (let ((file (nth 1 parent-target))
  8041. (pos (nth 3 parent-target))
  8042. level)
  8043. (with-current-buffer (or (find-buffer-visiting file)
  8044. (find-file-noselect file))
  8045. (save-excursion
  8046. (save-restriction
  8047. (widen)
  8048. (if pos
  8049. (goto-char pos)
  8050. (goto-char (point-max))
  8051. (if (not (bolp)) (newline)))
  8052. (when (looking-at outline-regexp)
  8053. (setq level (funcall outline-level))
  8054. (org-end-of-subtree t t))
  8055. (org-back-over-empty-lines)
  8056. (insert "\n" (make-string
  8057. (if pos (org-get-valid-level level 1) 1) ?*)
  8058. " " child "\n")
  8059. (beginning-of-line 0)
  8060. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8061. (defun org-olpath-completing-read (prompt collection &rest args)
  8062. "Read an outline path like a file name."
  8063. (let ((thetable collection)
  8064. (org-completion-use-ido nil)) ; does not work with ido.
  8065. (apply
  8066. 'org-ido-completing-read prompt
  8067. (lambda (string predicate &optional flag)
  8068. (let (rtn r f (l (length string)))
  8069. (cond
  8070. ((eq flag nil)
  8071. ;; try completion
  8072. (try-completion string thetable))
  8073. ((eq flag t)
  8074. ;; all-completions
  8075. (setq rtn (all-completions string thetable predicate))
  8076. (mapcar
  8077. (lambda (x)
  8078. (setq r (substring x l))
  8079. (if (string-match " ([^)]*)$" x)
  8080. (setq f (match-string 0 x))
  8081. (setq f ""))
  8082. (if (string-match "/" r)
  8083. (concat string (substring r 0 (match-end 0)) f)
  8084. x))
  8085. rtn))
  8086. ((eq flag 'lambda)
  8087. ;; exact match?
  8088. (assoc string thetable)))
  8089. ))
  8090. args)))
  8091. ;;;; Dynamic blocks
  8092. (defun org-find-dblock (name)
  8093. "Find the first dynamic block with name NAME in the buffer.
  8094. If not found, stay at current position and return nil."
  8095. (let (pos)
  8096. (save-excursion
  8097. (goto-char (point-min))
  8098. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8099. nil t)
  8100. (match-beginning 0))))
  8101. (if pos (goto-char pos))
  8102. pos))
  8103. (defconst org-dblock-start-re
  8104. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8105. "Matches the startline of a dynamic block, with parameters.")
  8106. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8107. "Matches the end of a dynamic block.")
  8108. (defun org-create-dblock (plist)
  8109. "Create a dynamic block section, with parameters taken from PLIST.
  8110. PLIST must contain a :name entry which is used as name of the block."
  8111. (unless (bolp) (newline))
  8112. (let ((name (plist-get plist :name)))
  8113. (insert "#+BEGIN: " name)
  8114. (while plist
  8115. (if (eq (car plist) :name)
  8116. (setq plist (cddr plist))
  8117. (insert " " (prin1-to-string (pop plist)))))
  8118. (insert "\n\n#+END:\n")
  8119. (beginning-of-line -2)))
  8120. (defun org-prepare-dblock ()
  8121. "Prepare dynamic block for refresh.
  8122. This empties the block, puts the cursor at the insert position and returns
  8123. the property list including an extra property :name with the block name."
  8124. (unless (looking-at org-dblock-start-re)
  8125. (error "Not at a dynamic block"))
  8126. (let* ((begdel (1+ (match-end 0)))
  8127. (name (org-no-properties (match-string 1)))
  8128. (params (append (list :name name)
  8129. (read (concat "(" (match-string 3) ")")))))
  8130. (unless (re-search-forward org-dblock-end-re nil t)
  8131. (error "Dynamic block not terminated"))
  8132. (setq params
  8133. (append params
  8134. (list :content (buffer-substring
  8135. begdel (match-beginning 0)))))
  8136. (delete-region begdel (match-beginning 0))
  8137. (goto-char begdel)
  8138. (open-line 1)
  8139. params))
  8140. (defun org-map-dblocks (&optional command)
  8141. "Apply COMMAND to all dynamic blocks in the current buffer.
  8142. If COMMAND is not given, use `org-update-dblock'."
  8143. (let ((cmd (or command 'org-update-dblock))
  8144. pos)
  8145. (save-excursion
  8146. (goto-char (point-min))
  8147. (while (re-search-forward org-dblock-start-re nil t)
  8148. (goto-char (setq pos (match-beginning 0)))
  8149. (condition-case nil
  8150. (funcall cmd)
  8151. (error (message "Error during update of dynamic block")))
  8152. (goto-char pos)
  8153. (unless (re-search-forward org-dblock-end-re nil t)
  8154. (error "Dynamic block not terminated"))))))
  8155. (defun org-dblock-update (&optional arg)
  8156. "User command for updating dynamic blocks.
  8157. Update the dynamic block at point. With prefix ARG, update all dynamic
  8158. blocks in the buffer."
  8159. (interactive "P")
  8160. (if arg
  8161. (org-update-all-dblocks)
  8162. (or (looking-at org-dblock-start-re)
  8163. (org-beginning-of-dblock))
  8164. (org-update-dblock)))
  8165. (defun org-update-dblock ()
  8166. "Update the dynamic block at point
  8167. This means to empty the block, parse for parameters and then call
  8168. the correct writing function."
  8169. (save-window-excursion
  8170. (let* ((pos (point))
  8171. (line (org-current-line))
  8172. (params (org-prepare-dblock))
  8173. (name (plist-get params :name))
  8174. (cmd (intern (concat "org-dblock-write:" name))))
  8175. (message "Updating dynamic block `%s' at line %d..." name line)
  8176. (funcall cmd params)
  8177. (message "Updating dynamic block `%s' at line %d...done" name line)
  8178. (goto-char pos))))
  8179. (defun org-beginning-of-dblock ()
  8180. "Find the beginning of the dynamic block at point.
  8181. Error if there is no such block at point."
  8182. (let ((pos (point))
  8183. beg)
  8184. (end-of-line 1)
  8185. (if (and (re-search-backward org-dblock-start-re nil t)
  8186. (setq beg (match-beginning 0))
  8187. (re-search-forward org-dblock-end-re nil t)
  8188. (> (match-end 0) pos))
  8189. (goto-char beg)
  8190. (goto-char pos)
  8191. (error "Not in a dynamic block"))))
  8192. (defun org-update-all-dblocks ()
  8193. "Update all dynamic blocks in the buffer.
  8194. This function can be used in a hook."
  8195. (when (org-mode-p)
  8196. (org-map-dblocks 'org-update-dblock)))
  8197. ;;;; Completion
  8198. (defconst org-additional-option-like-keywords
  8199. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  8200. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  8201. "LATEX_HEADER:"
  8202. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8203. "BEGIN_QUOTE" "END_QUOTE"
  8204. "BEGIN_VERSE" "END_VERSE"
  8205. "BEGIN_CENTER" "END_CENTER"
  8206. "BEGIN_SRC" "END_SRC"
  8207. "CATEGORY" "COLUMNS"
  8208. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  8209. (defcustom org-structure-template-alist
  8210. '(
  8211. ("s" "#+begin_src ?\n\n#+end_src"
  8212. "<src lang=\"?\">\n\n</src>")
  8213. ("e" "#+begin_example\n?\n#+end_example"
  8214. "<example>\n?\n</example>")
  8215. ("q" "#+begin_quote\n?\n#+end_quote"
  8216. "<quote>\n?\n</quote>")
  8217. ("v" "#+begin_verse\n?\n#+end_verse"
  8218. "<verse>\n?\n/verse>")
  8219. ("c" "#+begin_center\n?\n#+end_center"
  8220. "<center>\n?\n/center>")
  8221. ("l" "#+begin_latex\n?\n#+end_latex"
  8222. "<literal style=\"latex\">\n?\n</literal>")
  8223. ("L" "#+latex: "
  8224. "<literal style=\"latex\">?</literal>")
  8225. ("h" "#+begin_html\n?\n#+end_html"
  8226. "<literal style=\"html\">\n?\n</literal>")
  8227. ("H" "#+html: "
  8228. "<literal style=\"html\">?</literal>")
  8229. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8230. ("A" "#+ascii: ")
  8231. ("i" "#+include %file ?"
  8232. "<include file=%file markup=\"?\">")
  8233. )
  8234. "Structure completion elements.
  8235. This is a list of abbreviation keys and values. The value gets inserted
  8236. it you type @samp{.} followed by the key and then the completion key,
  8237. usually `M-TAB'. %file will be replaced by a file name after prompting
  8238. for the file using completion.
  8239. There are two templates for each key, the first uses the original Org syntax,
  8240. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8241. the default when the /org-mtags.el/ module has been loaded. See also the
  8242. variable `org-mtags-prefer-muse-templates'.
  8243. This is an experimental feature, it is undecided if it is going to stay in."
  8244. :group 'org-completion
  8245. :type '(repeat
  8246. (string :tag "Key")
  8247. (string :tag "Template")
  8248. (string :tag "Muse Template")))
  8249. (defun org-try-structure-completion ()
  8250. "Try to complete a structure template before point.
  8251. This looks for strings like \"<e\" on an otherwise empty line and
  8252. expands them."
  8253. (let ((l (buffer-substring (point-at-bol) (point)))
  8254. a)
  8255. (when (and (looking-at "[ \t]*$")
  8256. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8257. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8258. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8259. (match-beginning 1)) a)
  8260. t)))
  8261. (defun org-complete-expand-structure-template (start cell)
  8262. "Expand a structure template."
  8263. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8264. (rpl (nth (if musep 2 1) cell))
  8265. (ind ""))
  8266. (delete-region start (point))
  8267. (when (string-match "\\`#\\+" rpl)
  8268. (cond
  8269. ((bolp))
  8270. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8271. (setq ind (buffer-substring (point-at-bol) (point))))
  8272. (t (newline))))
  8273. (setq start (point))
  8274. (if (string-match "%file" rpl)
  8275. (setq rpl (replace-match
  8276. (concat
  8277. "\""
  8278. (save-match-data
  8279. (abbreviate-file-name (read-file-name "Include file: ")))
  8280. "\"")
  8281. t t rpl)))
  8282. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8283. (concat "\n" ind)))
  8284. (insert rpl)
  8285. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8286. (defun org-complete (&optional arg)
  8287. "Perform completion on word at point.
  8288. At the beginning of a headline, this completes TODO keywords as given in
  8289. `org-todo-keywords'.
  8290. If the current word is preceded by a backslash, completes the TeX symbols
  8291. that are supported for HTML support.
  8292. If the current word is preceded by \"#+\", completes special words for
  8293. setting file options.
  8294. In the line after \"#+STARTUP:, complete valid keywords.\"
  8295. At all other locations, this simply calls the value of
  8296. `org-completion-fallback-command'."
  8297. (interactive "P")
  8298. (org-without-partial-completion
  8299. (catch 'exit
  8300. (let* ((a nil)
  8301. (end (point))
  8302. (beg1 (save-excursion
  8303. (skip-chars-backward (org-re "[:alnum:]_@"))
  8304. (point)))
  8305. (beg (save-excursion
  8306. (skip-chars-backward "a-zA-Z0-9_:$")
  8307. (point)))
  8308. (confirm (lambda (x) (stringp (car x))))
  8309. (searchhead (equal (char-before beg) ?*))
  8310. (struct
  8311. (when (and (member (char-before beg1) '(?. ?<))
  8312. (setq a (assoc (buffer-substring beg1 (point))
  8313. org-structure-template-alist)))
  8314. (org-complete-expand-structure-template (1- beg1) a)
  8315. (throw 'exit t)))
  8316. (tag (and (equal (char-before beg1) ?:)
  8317. (equal (char-after (point-at-bol)) ?*)))
  8318. (prop (and (equal (char-before beg1) ?:)
  8319. (not (equal (char-after (point-at-bol)) ?*))))
  8320. (texp (equal (char-before beg) ?\\))
  8321. (link (equal (char-before beg) ?\[))
  8322. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8323. beg)
  8324. "#+"))
  8325. (startup (string-match "^#\\+STARTUP:.*"
  8326. (buffer-substring (point-at-bol) (point))))
  8327. (completion-ignore-case opt)
  8328. (type nil)
  8329. (tbl nil)
  8330. (table (cond
  8331. (opt
  8332. (setq type :opt)
  8333. (require 'org-exp)
  8334. (append
  8335. (mapcar
  8336. (lambda (x)
  8337. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8338. (cons (match-string 2 x) (match-string 1 x)))
  8339. (org-split-string (org-get-current-options) "\n"))
  8340. (mapcar 'list org-additional-option-like-keywords)))
  8341. (startup
  8342. (setq type :startup)
  8343. org-startup-options)
  8344. (link (append org-link-abbrev-alist-local
  8345. org-link-abbrev-alist))
  8346. (texp
  8347. (setq type :tex)
  8348. org-html-entities)
  8349. ((string-match "\\`\\*+[ \t]+\\'"
  8350. (buffer-substring (point-at-bol) beg))
  8351. (setq type :todo)
  8352. (mapcar 'list org-todo-keywords-1))
  8353. (searchhead
  8354. (setq type :searchhead)
  8355. (save-excursion
  8356. (goto-char (point-min))
  8357. (while (re-search-forward org-todo-line-regexp nil t)
  8358. (push (list
  8359. (org-make-org-heading-search-string
  8360. (match-string 3) t))
  8361. tbl)))
  8362. tbl)
  8363. (tag (setq type :tag beg beg1)
  8364. (or org-tag-alist (org-get-buffer-tags)))
  8365. (prop (setq type :prop beg beg1)
  8366. (mapcar 'list (org-buffer-property-keys nil t t)))
  8367. (t (progn
  8368. (call-interactively org-completion-fallback-command)
  8369. (throw 'exit nil)))))
  8370. (pattern (buffer-substring-no-properties beg end))
  8371. (completion (try-completion pattern table confirm)))
  8372. (cond ((eq completion t)
  8373. (if (not (assoc (upcase pattern) table))
  8374. (message "Already complete")
  8375. (if (and (equal type :opt)
  8376. (not (member (car (assoc (upcase pattern) table))
  8377. org-additional-option-like-keywords)))
  8378. (insert (substring (cdr (assoc (upcase pattern) table))
  8379. (length pattern)))
  8380. (if (memq type '(:tag :prop)) (insert ":")))))
  8381. ((null completion)
  8382. (message "Can't find completion for \"%s\"" pattern)
  8383. (ding))
  8384. ((not (string= pattern completion))
  8385. (delete-region beg end)
  8386. (if (string-match " +$" completion)
  8387. (setq completion (replace-match "" t t completion)))
  8388. (insert completion)
  8389. (if (get-buffer-window "*Completions*")
  8390. (delete-window (get-buffer-window "*Completions*")))
  8391. (if (assoc completion table)
  8392. (if (eq type :todo) (insert " ")
  8393. (if (memq type '(:tag :prop)) (insert ":"))))
  8394. (if (and (equal type :opt) (assoc completion table))
  8395. (message "%s" (substitute-command-keys
  8396. "Press \\[org-complete] again to insert example settings"))))
  8397. (t
  8398. (message "Making completion list...")
  8399. (let ((list (sort (all-completions pattern table confirm)
  8400. 'string<)))
  8401. (with-output-to-temp-buffer "*Completions*"
  8402. (condition-case nil
  8403. ;; Protection needed for XEmacs and emacs 21
  8404. (display-completion-list list pattern)
  8405. (error (display-completion-list list)))))
  8406. (message "Making completion list...%s" "done")))))))
  8407. ;;;; TODO, DEADLINE, Comments
  8408. (defun org-toggle-comment ()
  8409. "Change the COMMENT state of an entry."
  8410. (interactive)
  8411. (save-excursion
  8412. (org-back-to-heading)
  8413. (let (case-fold-search)
  8414. (if (looking-at (concat outline-regexp
  8415. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8416. (replace-match "" t t nil 1)
  8417. (if (looking-at outline-regexp)
  8418. (progn
  8419. (goto-char (match-end 0))
  8420. (insert org-comment-string " ")))))))
  8421. (defvar org-last-todo-state-is-todo nil
  8422. "This is non-nil when the last TODO state change led to a TODO state.
  8423. If the last change removed the TODO tag or switched to DONE, then
  8424. this is nil.")
  8425. (defvar org-setting-tags nil) ; dynamically skipped
  8426. (defun org-parse-local-options (string var)
  8427. "Parse STRING for startup setting relevant for variable VAR."
  8428. (let ((rtn (symbol-value var))
  8429. e opts)
  8430. (save-match-data
  8431. (if (or (not string) (not (string-match "\\S-" string)))
  8432. rtn
  8433. (setq opts (delq nil (mapcar (lambda (x)
  8434. (setq e (assoc x org-startup-options))
  8435. (if (eq (nth 1 e) var) e nil))
  8436. (org-split-string string "[ \t]+"))))
  8437. (if (not opts)
  8438. rtn
  8439. (setq rtn nil)
  8440. (while (setq e (pop opts))
  8441. (if (not (nth 3 e))
  8442. (setq rtn (nth 2 e))
  8443. (if (not (listp rtn)) (setq rtn nil))
  8444. (push (nth 2 e) rtn)))
  8445. rtn)))))
  8446. (defvar org-todo-setup-filter-hook nil
  8447. "Hook for functions that pre-filter todo specs.
  8448. Each function takes a todo spec and returns either `nil' or the spec
  8449. transformed into canonical form." )
  8450. (defvar org-todo-get-default-hook nil
  8451. "Hook for functions that get a default item for todo.
  8452. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8453. `nil' or a string to be used for the todo mark." )
  8454. (defvar org-agenda-headline-snapshot-before-repeat)
  8455. (defun org-todo (&optional arg)
  8456. "Change the TODO state of an item.
  8457. The state of an item is given by a keyword at the start of the heading,
  8458. like
  8459. *** TODO Write paper
  8460. *** DONE Call mom
  8461. The different keywords are specified in the variable `org-todo-keywords'.
  8462. By default the available states are \"TODO\" and \"DONE\".
  8463. So for this example: when the item starts with TODO, it is changed to DONE.
  8464. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8465. DONE are present, add TODO at the beginning of the heading.
  8466. With C-u prefix arg, use completion to determine the new state.
  8467. With numeric prefix arg, switch to that state.
  8468. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8469. With a tripple C-u prefix, circumvent any state blocking.
  8470. For calling through lisp, arg is also interpreted in the following way:
  8471. 'none -> empty state
  8472. \"\"(empty string) -> switch to empty state
  8473. 'done -> switch to DONE
  8474. 'nextset -> switch to the next set of keywords
  8475. 'previousset -> switch to the previous set of keywords
  8476. \"WAITING\" -> switch to the specified keyword, but only if it
  8477. really is a member of `org-todo-keywords'."
  8478. (interactive "P")
  8479. (if (equal arg '(16)) (setq arg 'nextset))
  8480. (let ((org-blocker-hook org-blocker-hook)
  8481. (case-fold-search nil))
  8482. (when (equal arg '(64))
  8483. (setq arg nil org-blocker-hook nil))
  8484. (when (and org-blocker-hook
  8485. (org-entry-get nil "NOBLOCKING"))
  8486. (setq org-blocker-hook nil))
  8487. (save-excursion
  8488. (catch 'exit
  8489. (org-back-to-heading)
  8490. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8491. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8492. (looking-at " *"))
  8493. (let* ((match-data (match-data))
  8494. (startpos (point-at-bol))
  8495. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8496. (org-log-done org-log-done)
  8497. (org-log-repeat org-log-repeat)
  8498. (org-todo-log-states org-todo-log-states)
  8499. (this (match-string 1))
  8500. (hl-pos (match-beginning 0))
  8501. (head (org-get-todo-sequence-head this))
  8502. (ass (assoc head org-todo-kwd-alist))
  8503. (interpret (nth 1 ass))
  8504. (done-word (nth 3 ass))
  8505. (final-done-word (nth 4 ass))
  8506. (last-state (or this ""))
  8507. (completion-ignore-case t)
  8508. (member (member this org-todo-keywords-1))
  8509. (tail (cdr member))
  8510. (state (cond
  8511. ((and org-todo-key-trigger
  8512. (or (and (equal arg '(4))
  8513. (eq org-use-fast-todo-selection 'prefix))
  8514. (and (not arg) org-use-fast-todo-selection
  8515. (not (eq org-use-fast-todo-selection
  8516. 'prefix)))))
  8517. ;; Use fast selection
  8518. (org-fast-todo-selection))
  8519. ((and (equal arg '(4))
  8520. (or (not org-use-fast-todo-selection)
  8521. (not org-todo-key-trigger)))
  8522. ;; Read a state with completion
  8523. (org-ido-completing-read
  8524. "State: " (mapcar (lambda(x) (list x))
  8525. org-todo-keywords-1)
  8526. nil t))
  8527. ((eq arg 'right)
  8528. (if this
  8529. (if tail (car tail) nil)
  8530. (car org-todo-keywords-1)))
  8531. ((eq arg 'left)
  8532. (if (equal member org-todo-keywords-1)
  8533. nil
  8534. (if this
  8535. (nth (- (length org-todo-keywords-1)
  8536. (length tail) 2)
  8537. org-todo-keywords-1)
  8538. (org-last org-todo-keywords-1))))
  8539. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8540. (setq arg nil))) ; hack to fall back to cycling
  8541. (arg
  8542. ;; user or caller requests a specific state
  8543. (cond
  8544. ((equal arg "") nil)
  8545. ((eq arg 'none) nil)
  8546. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8547. ((eq arg 'nextset)
  8548. (or (car (cdr (member head org-todo-heads)))
  8549. (car org-todo-heads)))
  8550. ((eq arg 'previousset)
  8551. (let ((org-todo-heads (reverse org-todo-heads)))
  8552. (or (car (cdr (member head org-todo-heads)))
  8553. (car org-todo-heads))))
  8554. ((car (member arg org-todo-keywords-1)))
  8555. ((nth (1- (prefix-numeric-value arg))
  8556. org-todo-keywords-1))))
  8557. ((null member) (or head (car org-todo-keywords-1)))
  8558. ((equal this final-done-word) nil) ;; -> make empty
  8559. ((null tail) nil) ;; -> first entry
  8560. ((memq interpret '(type priority))
  8561. (if (eq this-command last-command)
  8562. (car tail)
  8563. (if (> (length tail) 0)
  8564. (or done-word (car org-done-keywords))
  8565. nil)))
  8566. (t
  8567. (car tail))))
  8568. (state (or
  8569. (run-hook-with-args-until-success
  8570. 'org-todo-get-default-hook state last-state)
  8571. state))
  8572. (next (if state (concat " " state " ") " "))
  8573. (change-plist (list :type 'todo-state-change :from this :to state
  8574. :position startpos))
  8575. dolog now-done-p)
  8576. (when org-blocker-hook
  8577. (setq org-last-todo-state-is-todo
  8578. (not (member this org-done-keywords)))
  8579. (unless (save-excursion
  8580. (save-match-data
  8581. (run-hook-with-args-until-failure
  8582. 'org-blocker-hook change-plist)))
  8583. (if (interactive-p)
  8584. (error "TODO state change from %s to %s blocked" this state)
  8585. ;; fail silently
  8586. (message "TODO state change from %s to %s blocked" this state)
  8587. (throw 'exit nil))))
  8588. (store-match-data match-data)
  8589. (replace-match next t t)
  8590. (unless (pos-visible-in-window-p hl-pos)
  8591. (message "TODO state changed to %s" (org-trim next)))
  8592. (unless head
  8593. (setq head (org-get-todo-sequence-head state)
  8594. ass (assoc head org-todo-kwd-alist)
  8595. interpret (nth 1 ass)
  8596. done-word (nth 3 ass)
  8597. final-done-word (nth 4 ass)))
  8598. (when (memq arg '(nextset previousset))
  8599. (message "Keyword-Set %d/%d: %s"
  8600. (- (length org-todo-sets) -1
  8601. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8602. (length org-todo-sets)
  8603. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8604. (setq org-last-todo-state-is-todo
  8605. (not (member state org-done-keywords)))
  8606. (setq now-done-p (and (member state org-done-keywords)
  8607. (not (member this org-done-keywords))))
  8608. (and logging (org-local-logging logging))
  8609. (when (and (or org-todo-log-states org-log-done)
  8610. (not org-inhibit-logging)
  8611. (not (memq arg '(nextset previousset))))
  8612. ;; we need to look at recording a time and note
  8613. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8614. (nth 2 (assoc this org-todo-log-states))))
  8615. (when (and state
  8616. (member state org-not-done-keywords)
  8617. (not (member this org-not-done-keywords)))
  8618. ;; This is now a todo state and was not one before
  8619. ;; If there was a CLOSED time stamp, get rid of it.
  8620. (org-add-planning-info nil nil 'closed))
  8621. (when (and now-done-p org-log-done)
  8622. ;; It is now done, and it was not done before
  8623. (org-add-planning-info 'closed (org-current-time))
  8624. (if (and (not dolog) (eq 'note org-log-done))
  8625. (org-add-log-setup 'done state this 'findpos 'note)))
  8626. (when (and state dolog)
  8627. ;; This is a non-nil state, and we need to log it
  8628. (org-add-log-setup 'state state this 'findpos dolog)))
  8629. ;; Fixup tag positioning
  8630. (org-todo-trigger-tag-changes state)
  8631. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8632. (when org-provide-todo-statistics
  8633. (org-update-parent-todo-statistics))
  8634. (run-hooks 'org-after-todo-state-change-hook)
  8635. (if (and arg (not (member state org-done-keywords)))
  8636. (setq head (org-get-todo-sequence-head state)))
  8637. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8638. ;; Do we need to trigger a repeat?
  8639. (when now-done-p
  8640. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8641. ;; This is for the agenda, take a snapshot of the headline.
  8642. (save-match-data
  8643. (setq org-agenda-headline-snapshot-before-repeat
  8644. (org-get-heading))))
  8645. (org-auto-repeat-maybe state))
  8646. ;; Fixup cursor location if close to the keyword
  8647. (if (and (outline-on-heading-p)
  8648. (not (bolp))
  8649. (save-excursion (beginning-of-line 1)
  8650. (looking-at org-todo-line-regexp))
  8651. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8652. (progn
  8653. (goto-char (or (match-end 2) (match-end 1)))
  8654. (and (looking-at " ") (just-one-space))))
  8655. (when org-trigger-hook
  8656. (save-excursion
  8657. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8658. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8659. "Block turning an entry into a TODO, using the hierarchy.
  8660. This checks whether the current task should be blocked from state
  8661. changes. Such blocking occurs when:
  8662. 1. The task has children which are not all in a completed state.
  8663. 2. A task has a parent with the property :ORDERED:, and there
  8664. are siblings prior to the current task with incomplete
  8665. status.
  8666. 3. The parent of the task is blocked because it has siblings that should
  8667. be done first, or is child of a block grandparent TODO entry."
  8668. (catch 'dont-block
  8669. ;; If this is not a todo state change, or if this entry is already DONE,
  8670. ;; do not block
  8671. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8672. (member (plist-get change-plist :from)
  8673. (cons 'done org-done-keywords))
  8674. (member (plist-get change-plist :to)
  8675. (cons 'todo org-not-done-keywords)))
  8676. (throw 'dont-block t))
  8677. ;; If this task has children, and any are undone, it's blocked
  8678. (save-excursion
  8679. (org-back-to-heading t)
  8680. (let ((this-level (funcall outline-level)))
  8681. (outline-next-heading)
  8682. (let ((child-level (funcall outline-level)))
  8683. (while (and (not (eobp))
  8684. (> child-level this-level))
  8685. ;; this todo has children, check whether they are all
  8686. ;; completed
  8687. (if (and (not (org-entry-is-done-p))
  8688. (org-entry-is-todo-p))
  8689. (throw 'dont-block nil))
  8690. (outline-next-heading)
  8691. (setq child-level (funcall outline-level))))))
  8692. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8693. ;; any previous siblings are undone, it's blocked
  8694. (save-excursion
  8695. (org-back-to-heading t)
  8696. (let* ((pos (point))
  8697. (parent-pos (and (org-up-heading-safe) (point))))
  8698. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8699. (when (and (org-entry-get (point) "ORDERED")
  8700. (forward-line 1)
  8701. (re-search-forward org-not-done-heading-regexp pos t))
  8702. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8703. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8704. (while t
  8705. (goto-char parent-pos)
  8706. (if (not (looking-at org-not-done-heading-regexp))
  8707. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8708. (setq pos (point))
  8709. (setq parent-pos (and (org-up-heading-safe) (point)))
  8710. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8711. (when (and (org-entry-get (point) "ORDERED")
  8712. (forward-line 1)
  8713. (re-search-forward org-not-done-heading-regexp pos t))
  8714. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8715. (defcustom org-track-ordered-property-with-tag nil
  8716. "Should the ORDERED property also be shown as a tag?
  8717. The ORDERED property decides if an entry should require subtasks to be
  8718. completed in sequence. Since a property is not very visible, setting
  8719. this option means that toggling the ORDERED property with the command
  8720. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8721. not relevant for the behavior, but it makes things more visible.
  8722. Note that toggling the tag with tags commands will not change the property
  8723. and therefore not influence behavior!
  8724. This can be t, meaning the tag ORDERED should be used, It can also be a
  8725. string to select a different tag for this task."
  8726. :group 'org-todo
  8727. :type '(choice
  8728. (const :tag "No tracking" nil)
  8729. (const :tag "Track with ORDERED tag" t)
  8730. (string :tag "Use other tag")))
  8731. (defun org-toggle-ordered-property ()
  8732. "Toggle the ORDERED property of the current entry.
  8733. For better visibility, you can track the value of this property with a tag.
  8734. See variable `org-track-ordered-property-with-tag'."
  8735. (interactive)
  8736. (let* ((t1 org-track-ordered-property-with-tag)
  8737. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8738. (save-excursion
  8739. (org-back-to-heading)
  8740. (if (org-entry-get nil "ORDERED")
  8741. (progn
  8742. (org-delete-property "ORDERED")
  8743. (and tag (org-toggle-tag tag 'off))
  8744. (message "Subtasks can be completed in arbitrary order"))
  8745. (org-entry-put nil "ORDERED" "t")
  8746. (and tag (org-toggle-tag tag 'on))
  8747. (message "Subtasks must be completed in sequence")))))
  8748. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8749. (defun org-block-todo-from-checkboxes (change-plist)
  8750. "Block turning an entry into a TODO, using checkboxes.
  8751. This checks whether the current task should be blocked from state
  8752. changes because there are uncheckd boxes in this entry."
  8753. (catch 'dont-block
  8754. ;; If this is not a todo state change, or if this entry is already DONE,
  8755. ;; do not block
  8756. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8757. (member (plist-get change-plist :from)
  8758. (cons 'done org-done-keywords))
  8759. (member (plist-get change-plist :to)
  8760. (cons 'todo org-not-done-keywords)))
  8761. (throw 'dont-block t))
  8762. ;; If this task has checkboxes that are not checked, it's blocked
  8763. (save-excursion
  8764. (org-back-to-heading t)
  8765. (let ((beg (point)) end)
  8766. (outline-next-heading)
  8767. (setq end (point))
  8768. (goto-char beg)
  8769. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8770. end t)
  8771. (progn
  8772. (if (boundp 'org-blocked-by-checkboxes)
  8773. (setq org-blocked-by-checkboxes t))
  8774. (throw 'dont-block nil)))))
  8775. t)) ; do not block
  8776. (defvar org-entry-property-inherited-from) ;; defined below
  8777. (defun org-update-parent-todo-statistics ()
  8778. "Update any statistics cookie in the parent of the current headline.
  8779. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8780. the entire subtree and this will travel up the hierarchy and update
  8781. statistics everywhere."
  8782. (interactive)
  8783. (let* ((lim 0) prop
  8784. (recursive (or (not org-hierarchical-todo-statistics)
  8785. (string-match
  8786. "\\<recursive\\>"
  8787. (or (setq prop (org-entry-get
  8788. nil "COOKIE_DATA" 'inherit)) ""))))
  8789. (lim (or (and prop (marker-position
  8790. org-entry-property-inherited-from))
  8791. lim))
  8792. (first t)
  8793. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8794. level ltoggle l1
  8795. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8796. (catch 'exit
  8797. (save-excursion
  8798. (beginning-of-line 1)
  8799. (if (org-at-heading-p)
  8800. (setq ltoggle (funcall outline-level))
  8801. (error "This should not happen"))
  8802. (while (and (setq level (org-up-heading-safe))
  8803. (or recursive first)
  8804. (>= (point) lim))
  8805. (setq first nil)
  8806. (unless (and level
  8807. (not (string-match
  8808. "\\<checkbox\\>"
  8809. (downcase
  8810. (or (org-entry-get
  8811. nil "COOKIE_DATA")
  8812. "")))))
  8813. (throw 'exit nil))
  8814. (while (re-search-forward box-re (point-at-eol) t)
  8815. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8816. (setq is-percent (match-end 2))
  8817. (save-match-data
  8818. (unless (outline-next-heading) (throw 'exit nil))
  8819. (while (and (looking-at org-complex-heading-regexp)
  8820. (> (setq l1 (length (match-string 1))) level))
  8821. (setq kwd (and (or recursive (= l1 ltoggle))
  8822. (match-string 2)))
  8823. (and kwd (setq cnt-all (1+ cnt-all)))
  8824. (and (member kwd org-done-keywords)
  8825. (setq cnt-done (1+ cnt-done)))
  8826. (outline-next-heading)))
  8827. (replace-match
  8828. (if is-percent
  8829. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8830. (format "[%d/%d]" cnt-done cnt-all)))))
  8831. (when cookie-present
  8832. (run-hook-with-args 'org-after-todo-statistics-hook
  8833. cnt-done (- cnt-all cnt-done)))))
  8834. (run-hooks 'org-todo-statistics-hook)))
  8835. (defvar org-after-todo-statistics-hook nil
  8836. "Hook that is called after a TODO statistics cookie has been updated.
  8837. Each function is called with two arguments: the number of not-done entries
  8838. and the number of done entries.
  8839. For example, the following function, when added to this hook, will switch
  8840. an entry to DONE when all children are done, and back to TODO when new
  8841. entries are set to a TODO status. Note that this hook is only called
  8842. when there is a statistics cookie in the headline!
  8843. (defun org-summary-todo (n-done n-not-done)
  8844. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8845. (let (org-log-done org-log-states) ; turn off logging
  8846. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8847. ")
  8848. (defvar org-todo-statistics-hook nil
  8849. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8850. This hook runs even if there is no statisics cookie present, in which case
  8851. `org-after-todo-statistics-hook' would not run.")
  8852. (defun org-todo-trigger-tag-changes (state)
  8853. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8854. (let ((l org-todo-state-tags-triggers)
  8855. changes)
  8856. (when (or (not state) (equal state ""))
  8857. (setq changes (append changes (cdr (assoc "" l)))))
  8858. (when (and (stringp state) (> (length state) 0))
  8859. (setq changes (append changes (cdr (assoc state l)))))
  8860. (when (member state org-not-done-keywords)
  8861. (setq changes (append changes (cdr (assoc 'todo l)))))
  8862. (when (member state org-done-keywords)
  8863. (setq changes (append changes (cdr (assoc 'done l)))))
  8864. (dolist (c changes)
  8865. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8866. (defun org-local-logging (value)
  8867. "Get logging settings from a property VALUE."
  8868. (let* (words w a)
  8869. ;; directly set the variables, they are already local.
  8870. (setq org-log-done nil
  8871. org-log-repeat nil
  8872. org-todo-log-states nil)
  8873. (setq words (org-split-string value))
  8874. (while (setq w (pop words))
  8875. (cond
  8876. ((setq a (assoc w org-startup-options))
  8877. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8878. (set (nth 1 a) (nth 2 a))))
  8879. ((setq a (org-extract-log-state-settings w))
  8880. (and (member (car a) org-todo-keywords-1)
  8881. (push a org-todo-log-states)))))))
  8882. (defun org-get-todo-sequence-head (kwd)
  8883. "Return the head of the TODO sequence to which KWD belongs.
  8884. If KWD is not set, check if there is a text property remembering the
  8885. right sequence."
  8886. (let (p)
  8887. (cond
  8888. ((not kwd)
  8889. (or (get-text-property (point-at-bol) 'org-todo-head)
  8890. (progn
  8891. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8892. nil (point-at-eol)))
  8893. (get-text-property p 'org-todo-head))))
  8894. ((not (member kwd org-todo-keywords-1))
  8895. (car org-todo-keywords-1))
  8896. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8897. (defun org-fast-todo-selection ()
  8898. "Fast TODO keyword selection with single keys.
  8899. Returns the new TODO keyword, or nil if no state change should occur."
  8900. (let* ((fulltable org-todo-key-alist)
  8901. (done-keywords org-done-keywords) ;; needed for the faces.
  8902. (maxlen (apply 'max (mapcar
  8903. (lambda (x)
  8904. (if (stringp (car x)) (string-width (car x)) 0))
  8905. fulltable)))
  8906. (expert nil)
  8907. (fwidth (+ maxlen 3 1 3))
  8908. (ncol (/ (- (window-width) 4) fwidth))
  8909. tg cnt e c tbl
  8910. groups ingroup)
  8911. (save-excursion
  8912. (save-window-excursion
  8913. (if expert
  8914. (set-buffer (get-buffer-create " *Org todo*"))
  8915. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8916. (erase-buffer)
  8917. (org-set-local 'org-done-keywords done-keywords)
  8918. (setq tbl fulltable cnt 0)
  8919. (while (setq e (pop tbl))
  8920. (cond
  8921. ((equal e '(:startgroup))
  8922. (push '() groups) (setq ingroup t)
  8923. (when (not (= cnt 0))
  8924. (setq cnt 0)
  8925. (insert "\n"))
  8926. (insert "{ "))
  8927. ((equal e '(:endgroup))
  8928. (setq ingroup nil cnt 0)
  8929. (insert "}\n"))
  8930. ((equal e '(:newline))
  8931. (when (not (= cnt 0))
  8932. (setq cnt 0)
  8933. (insert "\n")
  8934. (setq e (car tbl))
  8935. (while (equal (car tbl) '(:newline))
  8936. (insert "\n")
  8937. (setq tbl (cdr tbl)))))
  8938. (t
  8939. (setq tg (car e) c (cdr e))
  8940. (if ingroup (push tg (car groups)))
  8941. (setq tg (org-add-props tg nil 'face
  8942. (org-get-todo-face tg)))
  8943. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8944. (insert "[" c "] " tg (make-string
  8945. (- fwidth 4 (length tg)) ?\ ))
  8946. (when (= (setq cnt (1+ cnt)) ncol)
  8947. (insert "\n")
  8948. (if ingroup (insert " "))
  8949. (setq cnt 0)))))
  8950. (insert "\n")
  8951. (goto-char (point-min))
  8952. (if (not expert) (org-fit-window-to-buffer))
  8953. (message "[a-z..]:Set [SPC]:clear")
  8954. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8955. (cond
  8956. ((or (= c ?\C-g)
  8957. (and (= c ?q) (not (rassoc c fulltable))))
  8958. (setq quit-flag t))
  8959. ((= c ?\ ) nil)
  8960. ((setq e (rassoc c fulltable) tg (car e))
  8961. tg)
  8962. (t (setq quit-flag t)))))))
  8963. (defun org-entry-is-todo-p ()
  8964. (member (org-get-todo-state) org-not-done-keywords))
  8965. (defun org-entry-is-done-p ()
  8966. (member (org-get-todo-state) org-done-keywords))
  8967. (defun org-get-todo-state ()
  8968. (save-excursion
  8969. (org-back-to-heading t)
  8970. (and (looking-at org-todo-line-regexp)
  8971. (match-end 2)
  8972. (match-string 2))))
  8973. (defun org-at-date-range-p (&optional inactive-ok)
  8974. "Is the cursor inside a date range?"
  8975. (interactive)
  8976. (save-excursion
  8977. (catch 'exit
  8978. (let ((pos (point)))
  8979. (skip-chars-backward "^[<\r\n")
  8980. (skip-chars-backward "<[")
  8981. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8982. (>= (match-end 0) pos)
  8983. (throw 'exit t))
  8984. (skip-chars-backward "^<[\r\n")
  8985. (skip-chars-backward "<[")
  8986. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8987. (>= (match-end 0) pos)
  8988. (throw 'exit t)))
  8989. nil)))
  8990. (defun org-get-repeat ()
  8991. "Check if there is a deadline/schedule with repeater in this entry."
  8992. (save-match-data
  8993. (save-excursion
  8994. (org-back-to-heading t)
  8995. (if (re-search-forward
  8996. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8997. (match-string 1)))))
  8998. (defvar org-last-changed-timestamp)
  8999. (defvar org-last-inserted-timestamp)
  9000. (defvar org-log-post-message)
  9001. (defvar org-log-note-purpose)
  9002. (defvar org-log-note-how)
  9003. (defvar org-log-note-extra)
  9004. (defun org-auto-repeat-maybe (done-word)
  9005. "Check if the current headline contains a repeated deadline/schedule.
  9006. If yes, set TODO state back to what it was and change the base date
  9007. of repeating deadline/scheduled time stamps to new date.
  9008. This function is run automatically after each state change to a DONE state."
  9009. ;; last-state is dynamically scoped into this function
  9010. (let* ((repeat (org-get-repeat))
  9011. (aa (assoc last-state org-todo-kwd-alist))
  9012. (interpret (nth 1 aa))
  9013. (head (nth 2 aa))
  9014. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9015. (msg "Entry repeats: ")
  9016. (org-log-done nil)
  9017. (org-todo-log-states nil)
  9018. (nshiftmax 10) (nshift 0)
  9019. re type n what ts time)
  9020. (when repeat
  9021. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9022. (org-todo (if (eq interpret 'type) last-state head))
  9023. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9024. (org-time-stamp-format t t)))
  9025. (when org-log-repeat
  9026. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9027. (memq 'org-add-log-note post-command-hook))
  9028. ;; OK, we are already setup for some record
  9029. (if (eq org-log-repeat 'note)
  9030. ;; make sure we take a note, not only a time stamp
  9031. (setq org-log-note-how 'note))
  9032. ;; Set up for taking a record
  9033. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9034. last-state
  9035. 'findpos org-log-repeat)))
  9036. (org-back-to-heading t)
  9037. (org-add-planning-info nil nil 'closed)
  9038. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9039. org-deadline-time-regexp "\\)\\|\\("
  9040. org-ts-regexp "\\)"))
  9041. (while (re-search-forward
  9042. re (save-excursion (outline-next-heading) (point)) t)
  9043. (setq type (if (match-end 1) org-scheduled-string
  9044. (if (match-end 3) org-deadline-string "Plain:"))
  9045. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9046. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9047. (setq n (string-to-number (match-string 2 ts))
  9048. what (match-string 3 ts))
  9049. (if (equal what "w") (setq n (* n 7) what "d"))
  9050. ;; Preparation, see if we need to modify the start date for the change
  9051. (when (match-end 1)
  9052. (setq time (save-match-data (org-time-string-to-time ts)))
  9053. (cond
  9054. ((equal (match-string 1 ts) ".")
  9055. ;; Shift starting date to today
  9056. (org-timestamp-change
  9057. (- (time-to-days (current-time)) (time-to-days time))
  9058. 'day))
  9059. ((equal (match-string 1 ts) "+")
  9060. (while (or (= nshift 0)
  9061. (<= (time-to-days time) (time-to-days (current-time))))
  9062. (when (= (incf nshift) nshiftmax)
  9063. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9064. (error "Abort")))
  9065. (org-timestamp-change n (cdr (assoc what whata)))
  9066. (org-at-timestamp-p t)
  9067. (setq ts (match-string 1))
  9068. (setq time (save-match-data (org-time-string-to-time ts))))
  9069. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9070. ;; rematch, so that we have everything in place for the real shift
  9071. (org-at-timestamp-p t)
  9072. (setq ts (match-string 1))
  9073. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9074. (org-timestamp-change n (cdr (assoc what whata)))
  9075. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9076. (setq org-log-post-message msg)
  9077. (message "%s" msg))))
  9078. (defun org-show-todo-tree (arg)
  9079. "Make a compact tree which shows all headlines marked with TODO.
  9080. The tree will show the lines where the regexp matches, and all higher
  9081. headlines above the match.
  9082. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9083. With a numeric prefix N, construct a sparse tree for the Nth element
  9084. of `org-todo-keywords-1'."
  9085. (interactive "P")
  9086. (let ((case-fold-search nil)
  9087. (kwd-re
  9088. (cond ((null arg) org-not-done-regexp)
  9089. ((equal arg '(4))
  9090. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  9091. (mapcar 'list org-todo-keywords-1))))
  9092. (concat "\\("
  9093. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9094. "\\)\\>")))
  9095. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9096. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9097. org-todo-keywords-1)))
  9098. (t (error "Invalid prefix argument: %s" arg)))))
  9099. (message "%d TODO entries found"
  9100. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9101. (defun org-deadline (&optional remove time)
  9102. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9103. With argument REMOVE, remove any deadline from the item.
  9104. When TIME is set, it should be an internal time specification, and the
  9105. scheduling will use the corresponding date."
  9106. (interactive "P")
  9107. (if remove
  9108. (progn
  9109. (org-remove-timestamp-with-keyword org-deadline-string)
  9110. (message "Item no longer has a deadline."))
  9111. (if (org-get-repeat)
  9112. (error "Cannot change deadline on task with repeater, please do that by hand")
  9113. (org-add-planning-info 'deadline time 'closed)
  9114. (message "Deadline on %s" org-last-inserted-timestamp))))
  9115. (defun org-schedule (&optional remove time)
  9116. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9117. With argument REMOVE, remove any scheduling date from the item.
  9118. When TIME is set, it should be an internal time specification, and the
  9119. scheduling will use the corresponding date."
  9120. (interactive "P")
  9121. (if remove
  9122. (progn
  9123. (org-remove-timestamp-with-keyword org-scheduled-string)
  9124. (message "Item is no longer scheduled."))
  9125. (if (org-get-repeat)
  9126. (error "Cannot reschedule task with repeater, please do that by hand")
  9127. (org-add-planning-info 'scheduled time 'closed)
  9128. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9129. (defun org-get-scheduled-time (pom &optional inherit)
  9130. "Get the scheduled time as a time tuple, of a format suitable
  9131. for calling org-schedule with, or if there is no scheduling,
  9132. returns nil."
  9133. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9134. (when time
  9135. (apply 'encode-time (org-parse-time-string time)))))
  9136. (defun org-get-deadline-time (pom &optional inherit)
  9137. "Get the deadine as a time tuple, of a format suitable for
  9138. calling org-deadlin with, or if there is no scheduling, returns
  9139. nil."
  9140. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9141. (when time
  9142. (apply 'encode-time (org-parse-time-string time)))))
  9143. (defun org-remove-timestamp-with-keyword (keyword)
  9144. "Remove all time stamps with KEYWORD in the current entry."
  9145. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9146. beg)
  9147. (save-excursion
  9148. (org-back-to-heading t)
  9149. (setq beg (point))
  9150. (org-end-of-subtree t t)
  9151. (while (re-search-backward re beg t)
  9152. (replace-match "")
  9153. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9154. (equal (char-before) ?\ ))
  9155. (backward-delete-char 1)
  9156. (if (string-match "^[ \t]*$" (buffer-substring
  9157. (point-at-bol) (point-at-eol)))
  9158. (delete-region (point-at-bol)
  9159. (min (point-max) (1+ (point-at-eol))))))))))
  9160. (defun org-add-planning-info (what &optional time &rest remove)
  9161. "Insert new timestamp with keyword in the line directly after the headline.
  9162. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9163. If non is given, the user is prompted for a date.
  9164. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9165. be removed."
  9166. (interactive)
  9167. (let (org-time-was-given org-end-time-was-given ts
  9168. end default-time default-input)
  9169. (catch 'exit
  9170. (when (and (not time) (memq what '(scheduled deadline)))
  9171. ;; Try to get a default date/time from existing timestamp
  9172. (save-excursion
  9173. (org-back-to-heading t)
  9174. (setq end (save-excursion (outline-next-heading) (point)))
  9175. (when (re-search-forward (if (eq what 'scheduled)
  9176. org-scheduled-time-regexp
  9177. org-deadline-time-regexp)
  9178. end t)
  9179. (setq ts (match-string 1)
  9180. default-time
  9181. (apply 'encode-time (org-parse-time-string ts))
  9182. default-input (and ts (org-get-compact-tod ts))))))
  9183. (when what
  9184. ;; If necessary, get the time from the user
  9185. (setq time (or time (org-read-date nil 'to-time nil nil
  9186. default-time default-input))))
  9187. (when (and org-insert-labeled-timestamps-at-point
  9188. (member what '(scheduled deadline)))
  9189. (insert
  9190. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9191. (org-insert-time-stamp time org-time-was-given
  9192. nil nil nil (list org-end-time-was-given))
  9193. (setq what nil))
  9194. (save-excursion
  9195. (save-restriction
  9196. (let (col list elt ts buffer-invisibility-spec)
  9197. (org-back-to-heading t)
  9198. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9199. (goto-char (match-end 1))
  9200. (setq col (current-column))
  9201. (goto-char (match-end 0))
  9202. (if (eobp) (insert "\n") (forward-char 1))
  9203. (when (and (not what)
  9204. (not (looking-at
  9205. (concat "[ \t]*"
  9206. org-keyword-time-not-clock-regexp))))
  9207. ;; Nothing to add, nothing to remove...... :-)
  9208. (throw 'exit nil))
  9209. (if (and (not (looking-at outline-regexp))
  9210. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9211. "[^\r\n]*"))
  9212. (not (equal (match-string 1) org-clock-string)))
  9213. (narrow-to-region (match-beginning 0) (match-end 0))
  9214. (insert-before-markers "\n")
  9215. (backward-char 1)
  9216. (narrow-to-region (point) (point))
  9217. (and org-adapt-indentation (org-indent-to-column col)))
  9218. ;; Check if we have to remove something.
  9219. (setq list (cons what remove))
  9220. (while list
  9221. (setq elt (pop list))
  9222. (goto-char (point-min))
  9223. (when (or (and (eq elt 'scheduled)
  9224. (re-search-forward org-scheduled-time-regexp nil t))
  9225. (and (eq elt 'deadline)
  9226. (re-search-forward org-deadline-time-regexp nil t))
  9227. (and (eq elt 'closed)
  9228. (re-search-forward org-closed-time-regexp nil t)))
  9229. (replace-match "")
  9230. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9231. (if (looking-at " +") (replace-match ""))))
  9232. (goto-char (point-max))
  9233. (when what
  9234. (insert
  9235. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9236. (cond ((eq what 'scheduled) org-scheduled-string)
  9237. ((eq what 'deadline) org-deadline-string)
  9238. ((eq what 'closed) org-closed-string))
  9239. " ")
  9240. (setq ts (org-insert-time-stamp
  9241. time
  9242. (or org-time-was-given
  9243. (and (eq what 'closed) org-log-done-with-time))
  9244. (eq what 'closed)
  9245. nil nil (list org-end-time-was-given)))
  9246. (end-of-line 1))
  9247. (goto-char (point-min))
  9248. (widen)
  9249. (if (and (looking-at "[ \t]+\n")
  9250. (equal (char-before) ?\n))
  9251. (delete-region (1- (point)) (point-at-eol)))
  9252. ts))))))
  9253. (defvar org-log-note-marker (make-marker))
  9254. (defvar org-log-note-purpose nil)
  9255. (defvar org-log-note-state nil)
  9256. (defvar org-log-note-previous-state nil)
  9257. (defvar org-log-note-how nil)
  9258. (defvar org-log-note-extra nil)
  9259. (defvar org-log-note-window-configuration nil)
  9260. (defvar org-log-note-return-to (make-marker))
  9261. (defvar org-log-post-message nil
  9262. "Message to be displayed after a log note has been stored.
  9263. The auto-repeater uses this.")
  9264. (defun org-add-note ()
  9265. "Add a note to the current entry.
  9266. This is done in the same way as adding a state change note."
  9267. (interactive)
  9268. (org-add-log-setup 'note nil nil 'findpos nil))
  9269. (defvar org-property-end-re)
  9270. (defun org-add-log-setup (&optional purpose state prev-state
  9271. findpos how &optional extra)
  9272. "Set up the post command hook to take a note.
  9273. If this is about to TODO state change, the new state is expected in STATE.
  9274. When FINDPOS is non-nil, find the correct position for the note in
  9275. the current entry. If not, assume that it can be inserted at point.
  9276. HOW is an indicator what kind of note should be created.
  9277. EXTRA is additional text that will be inserted into the notes buffer."
  9278. (let* ((org-log-into-drawer (org-log-into-drawer))
  9279. (drawer (cond ((stringp org-log-into-drawer)
  9280. org-log-into-drawer)
  9281. (org-log-into-drawer "LOGBOOK")
  9282. (t nil))))
  9283. (save-restriction
  9284. (save-excursion
  9285. (when findpos
  9286. (org-back-to-heading t)
  9287. (narrow-to-region (point) (save-excursion
  9288. (outline-next-heading) (point)))
  9289. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9290. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9291. "[^\r\n]*\\)?"))
  9292. (goto-char (match-end 0))
  9293. (cond
  9294. (drawer
  9295. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9296. nil t)
  9297. (progn
  9298. (goto-char (match-end 0))
  9299. (or org-log-states-order-reversed
  9300. (and (re-search-forward org-property-end-re nil t)
  9301. (goto-char (1- (match-beginning 0))))))
  9302. (insert "\n:" drawer ":\n:END:")
  9303. (beginning-of-line 0)
  9304. (org-indent-line-function)
  9305. (beginning-of-line 2)
  9306. (org-indent-line-function)
  9307. (end-of-line 0)))
  9308. ((and org-log-state-notes-insert-after-drawers
  9309. (save-excursion
  9310. (forward-line) (looking-at org-drawer-regexp)))
  9311. (forward-line)
  9312. (while (looking-at org-drawer-regexp)
  9313. (goto-char (match-end 0))
  9314. (re-search-forward org-property-end-re (point-max) t)
  9315. (forward-line))
  9316. (forward-line -1)))
  9317. (unless org-log-states-order-reversed
  9318. (and (= (char-after) ?\n) (forward-char 1))
  9319. (org-skip-over-state-notes)
  9320. (skip-chars-backward " \t\n\r")))
  9321. (move-marker org-log-note-marker (point))
  9322. (setq org-log-note-purpose purpose
  9323. org-log-note-state state
  9324. org-log-note-previous-state prev-state
  9325. org-log-note-how how
  9326. org-log-note-extra extra)
  9327. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9328. (defun org-skip-over-state-notes ()
  9329. "Skip past the list of State notes in an entry."
  9330. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9331. (while (looking-at "[ \t]*- State")
  9332. (condition-case nil
  9333. (org-next-item)
  9334. (error (org-end-of-item)))))
  9335. (defun org-add-log-note (&optional purpose)
  9336. "Pop up a window for taking a note, and add this note later at point."
  9337. (remove-hook 'post-command-hook 'org-add-log-note)
  9338. (setq org-log-note-window-configuration (current-window-configuration))
  9339. (delete-other-windows)
  9340. (move-marker org-log-note-return-to (point))
  9341. (switch-to-buffer (marker-buffer org-log-note-marker))
  9342. (goto-char org-log-note-marker)
  9343. (org-switch-to-buffer-other-window "*Org Note*")
  9344. (erase-buffer)
  9345. (if (memq org-log-note-how '(time state))
  9346. (let (current-prefix-arg) (org-store-log-note))
  9347. (let ((org-inhibit-startup t)) (org-mode))
  9348. (insert (format "# Insert note for %s.
  9349. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9350. (cond
  9351. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9352. ((eq org-log-note-purpose 'done) "closed todo item")
  9353. ((eq org-log-note-purpose 'state)
  9354. (format "state change from \"%s\" to \"%s\""
  9355. (or org-log-note-previous-state "")
  9356. (or org-log-note-state "")))
  9357. ((eq org-log-note-purpose 'note)
  9358. "this entry")
  9359. (t (error "This should not happen")))))
  9360. (if org-log-note-extra (insert org-log-note-extra))
  9361. (org-set-local 'org-finish-function 'org-store-log-note)))
  9362. (defvar org-note-abort nil) ; dynamically scoped
  9363. (defun org-store-log-note ()
  9364. "Finish taking a log note, and insert it to where it belongs."
  9365. (let ((txt (buffer-string))
  9366. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9367. lines ind)
  9368. (kill-buffer (current-buffer))
  9369. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9370. (setq txt (replace-match "" t t txt)))
  9371. (if (string-match "\\s-+\\'" txt)
  9372. (setq txt (replace-match "" t t txt)))
  9373. (setq lines (org-split-string txt "\n"))
  9374. (when (and note (string-match "\\S-" note))
  9375. (setq note
  9376. (org-replace-escapes
  9377. note
  9378. (list (cons "%u" (user-login-name))
  9379. (cons "%U" user-full-name)
  9380. (cons "%t" (format-time-string
  9381. (org-time-stamp-format 'long 'inactive)
  9382. (current-time)))
  9383. (cons "%s" (if org-log-note-state
  9384. (concat "\"" org-log-note-state "\"")
  9385. ""))
  9386. (cons "%S" (if org-log-note-previous-state
  9387. (concat "\"" org-log-note-previous-state "\"")
  9388. "\"\"")))))
  9389. (if lines (setq note (concat note " \\\\")))
  9390. (push note lines))
  9391. (when (or current-prefix-arg org-note-abort)
  9392. (when org-log-into-drawer
  9393. (org-remove-empty-drawer-at
  9394. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9395. org-log-note-marker))
  9396. (setq lines nil))
  9397. (when lines
  9398. (save-excursion
  9399. (set-buffer (marker-buffer org-log-note-marker))
  9400. (save-excursion
  9401. (goto-char org-log-note-marker)
  9402. (move-marker org-log-note-marker nil)
  9403. (end-of-line 1)
  9404. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9405. (insert "- " (pop lines))
  9406. (org-indent-line-function)
  9407. (beginning-of-line 1)
  9408. (looking-at "[ \t]*")
  9409. (setq ind (concat (match-string 0) " "))
  9410. (end-of-line 1)
  9411. (while lines (insert "\n" ind (pop lines)))
  9412. (message "Note stored")
  9413. (org-back-to-heading t)
  9414. (org-cycle-hide-drawers 'children)))))
  9415. (set-window-configuration org-log-note-window-configuration)
  9416. (with-current-buffer (marker-buffer org-log-note-return-to)
  9417. (goto-char org-log-note-return-to))
  9418. (move-marker org-log-note-return-to nil)
  9419. (and org-log-post-message (message "%s" org-log-post-message)))
  9420. (defun org-remove-empty-drawer-at (drawer pos)
  9421. "Remove an emptyr DARWER drawer at position POS.
  9422. POS may also be a marker."
  9423. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9424. (save-excursion
  9425. (save-restriction
  9426. (widen)
  9427. (goto-char pos)
  9428. (if (org-in-regexp
  9429. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9430. (replace-match ""))))))
  9431. (defun org-sparse-tree (&optional arg)
  9432. "Create a sparse tree, prompt for the details.
  9433. This command can create sparse trees. You first need to select the type
  9434. of match used to create the tree:
  9435. t Show entries with a specific TODO keyword.
  9436. m Show entries selected by a tags/property match.
  9437. p Enter a property name and its value (both with completion on existing
  9438. names/values) and show entries with that property.
  9439. r Show entries matching a regular expression.
  9440. d Show deadlines due within `org-deadline-warning-days'.
  9441. b Show deadlines and scheduled items before a date.
  9442. a Show deadlines and scheduled items after a date."
  9443. (interactive "P")
  9444. (let (ans kwd value)
  9445. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9446. (setq ans (read-char-exclusive))
  9447. (cond
  9448. ((equal ans ?d)
  9449. (call-interactively 'org-check-deadlines))
  9450. ((equal ans ?b)
  9451. (call-interactively 'org-check-before-date))
  9452. ((equal ans ?a)
  9453. (call-interactively 'org-check-after-date))
  9454. ((equal ans ?t)
  9455. (org-show-todo-tree '(4)))
  9456. ((member ans '(?T ?m))
  9457. (call-interactively 'org-match-sparse-tree))
  9458. ((member ans '(?p ?P))
  9459. (setq kwd (org-ido-completing-read "Property: "
  9460. (mapcar 'list (org-buffer-property-keys))))
  9461. (setq value (org-ido-completing-read "Value: "
  9462. (mapcar 'list (org-property-values kwd))))
  9463. (unless (string-match "\\`{.*}\\'" value)
  9464. (setq value (concat "\"" value "\"")))
  9465. (org-match-sparse-tree arg (concat kwd "=" value)))
  9466. ((member ans '(?r ?R ?/))
  9467. (call-interactively 'org-occur))
  9468. (t (error "No such sparse tree command \"%c\"" ans)))))
  9469. (defvar org-occur-highlights nil
  9470. "List of overlays used for occur matches.")
  9471. (make-variable-buffer-local 'org-occur-highlights)
  9472. (defvar org-occur-parameters nil
  9473. "Parameters of the active org-occur calls.
  9474. This is a list, each call to org-occur pushes as cons cell,
  9475. containing the regular expression and the callback, onto the list.
  9476. The list can contain several entries if `org-occur' has been called
  9477. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9478. will only contain one set of parameters. When the highlights are
  9479. removed (for example with `C-c C-c', or with the next edit (depending
  9480. on `org-remove-highlights-with-change'), this variable is emptied
  9481. as well.")
  9482. (make-variable-buffer-local 'org-occur-parameters)
  9483. (defun org-occur (regexp &optional keep-previous callback)
  9484. "Make a compact tree which shows all matches of REGEXP.
  9485. The tree will show the lines where the regexp matches, and all higher
  9486. headlines above the match. It will also show the heading after the match,
  9487. to make sure editing the matching entry is easy.
  9488. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9489. call to `org-occur' will be kept, to allow stacking of calls to this
  9490. command.
  9491. If CALLBACK is non-nil, it is a function which is called to confirm
  9492. that the match should indeed be shown."
  9493. (interactive "sRegexp: \nP")
  9494. (unless keep-previous
  9495. (org-remove-occur-highlights nil nil t))
  9496. (push (cons regexp callback) org-occur-parameters)
  9497. (let ((cnt 0))
  9498. (save-excursion
  9499. (goto-char (point-min))
  9500. (if (or (not keep-previous) ; do not want to keep
  9501. (not org-occur-highlights)) ; no previous matches
  9502. ;; hide everything
  9503. (org-overview))
  9504. (while (re-search-forward regexp nil t)
  9505. (when (or (not callback)
  9506. (save-match-data (funcall callback)))
  9507. (setq cnt (1+ cnt))
  9508. (when org-highlight-sparse-tree-matches
  9509. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9510. (org-show-context 'occur-tree))))
  9511. (when org-remove-highlights-with-change
  9512. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9513. nil 'local))
  9514. (unless org-sparse-tree-open-archived-trees
  9515. (org-hide-archived-subtrees (point-min) (point-max)))
  9516. (run-hooks 'org-occur-hook)
  9517. (if (interactive-p)
  9518. (message "%d match(es) for regexp %s" cnt regexp))
  9519. cnt))
  9520. (defun org-show-context (&optional key)
  9521. "Make sure point and context and visible.
  9522. How much context is shown depends upon the variables
  9523. `org-show-hierarchy-above', `org-show-following-heading'. and
  9524. `org-show-siblings'."
  9525. (let ((heading-p (org-on-heading-p t))
  9526. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9527. (following-p (org-get-alist-option org-show-following-heading key))
  9528. (entry-p (org-get-alist-option org-show-entry-below key))
  9529. (siblings-p (org-get-alist-option org-show-siblings key)))
  9530. (catch 'exit
  9531. ;; Show heading or entry text
  9532. (if (and heading-p (not entry-p))
  9533. (org-flag-heading nil) ; only show the heading
  9534. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9535. (org-show-hidden-entry))) ; show entire entry
  9536. (when following-p
  9537. ;; Show next sibling, or heading below text
  9538. (save-excursion
  9539. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9540. (org-flag-heading nil))))
  9541. (when siblings-p (org-show-siblings))
  9542. (when hierarchy-p
  9543. ;; show all higher headings, possibly with siblings
  9544. (save-excursion
  9545. (while (and (condition-case nil
  9546. (progn (org-up-heading-all 1) t)
  9547. (error nil))
  9548. (not (bobp)))
  9549. (org-flag-heading nil)
  9550. (when siblings-p (org-show-siblings))))))))
  9551. (defun org-reveal (&optional siblings)
  9552. "Show current entry, hierarchy above it, and the following headline.
  9553. This can be used to show a consistent set of context around locations
  9554. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9555. not t for the search context.
  9556. With optional argument SIBLINGS, on each level of the hierarchy all
  9557. siblings are shown. This repairs the tree structure to what it would
  9558. look like when opened with hierarchical calls to `org-cycle'."
  9559. (interactive "P")
  9560. (let ((org-show-hierarchy-above t)
  9561. (org-show-following-heading t)
  9562. (org-show-siblings (if siblings t org-show-siblings)))
  9563. (org-show-context nil)))
  9564. (defun org-highlight-new-match (beg end)
  9565. "Highlight from BEG to END and mark the highlight is an occur headline."
  9566. (let ((ov (org-make-overlay beg end)))
  9567. (org-overlay-put ov 'face 'secondary-selection)
  9568. (push ov org-occur-highlights)))
  9569. (defun org-remove-occur-highlights (&optional beg end noremove)
  9570. "Remove the occur highlights from the buffer.
  9571. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9572. from the `before-change-functions' in the current buffer."
  9573. (interactive)
  9574. (unless org-inhibit-highlight-removal
  9575. (mapc 'org-delete-overlay org-occur-highlights)
  9576. (setq org-occur-highlights nil)
  9577. (setq org-occur-parameters nil)
  9578. (unless noremove
  9579. (remove-hook 'before-change-functions
  9580. 'org-remove-occur-highlights 'local))))
  9581. ;;;; Priorities
  9582. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9583. "Regular expression matching the priority indicator.")
  9584. (defvar org-remove-priority-next-time nil)
  9585. (defun org-priority-up ()
  9586. "Increase the priority of the current item."
  9587. (interactive)
  9588. (org-priority 'up))
  9589. (defun org-priority-down ()
  9590. "Decrease the priority of the current item."
  9591. (interactive)
  9592. (org-priority 'down))
  9593. (defun org-priority (&optional action)
  9594. "Change the priority of an item by ARG.
  9595. ACTION can be `set', `up', `down', or a character."
  9596. (interactive)
  9597. (unless org-enable-priority-commands
  9598. (error "Priority commands are disabled"))
  9599. (setq action (or action 'set))
  9600. (let (current new news have remove)
  9601. (save-excursion
  9602. (org-back-to-heading t)
  9603. (if (looking-at org-priority-regexp)
  9604. (setq current (string-to-char (match-string 2))
  9605. have t)
  9606. (setq current org-default-priority))
  9607. (cond
  9608. ((or (eq action 'set)
  9609. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9610. (if (not (eq action 'set))
  9611. (setq new action)
  9612. (message "Priority %c-%c, SPC to remove: "
  9613. org-highest-priority org-lowest-priority)
  9614. (setq new (read-char-exclusive)))
  9615. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9616. (= (upcase org-lowest-priority) org-lowest-priority))
  9617. (setq new (upcase new)))
  9618. (cond ((equal new ?\ ) (setq remove t))
  9619. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9620. (error "Priority must be between `%c' and `%c'"
  9621. org-highest-priority org-lowest-priority))))
  9622. ((eq action 'up)
  9623. (if (and (not have) (eq last-command this-command))
  9624. (setq new org-lowest-priority)
  9625. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9626. org-default-priority (1- current)))))
  9627. ((eq action 'down)
  9628. (if (and (not have) (eq last-command this-command))
  9629. (setq new org-highest-priority)
  9630. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9631. org-default-priority (1+ current)))))
  9632. (t (error "Invalid action")))
  9633. (if (or (< (upcase new) org-highest-priority)
  9634. (> (upcase new) org-lowest-priority))
  9635. (setq remove t))
  9636. (setq news (format "%c" new))
  9637. (if have
  9638. (if remove
  9639. (replace-match "" t t nil 1)
  9640. (replace-match news t t nil 2))
  9641. (if remove
  9642. (error "No priority cookie found in line")
  9643. (let ((case-fold-search nil))
  9644. (looking-at org-todo-line-regexp))
  9645. (if (match-end 2)
  9646. (progn
  9647. (goto-char (match-end 2))
  9648. (insert " [#" news "]"))
  9649. (goto-char (match-beginning 3))
  9650. (insert "[#" news "] ")))))
  9651. (org-preserve-lc (org-set-tags nil 'align))
  9652. (if remove
  9653. (message "Priority removed")
  9654. (message "Priority of current item set to %s" news))))
  9655. (defun org-get-priority (s)
  9656. "Find priority cookie and return priority."
  9657. (save-match-data
  9658. (if (not (string-match org-priority-regexp s))
  9659. (* 1000 (- org-lowest-priority org-default-priority))
  9660. (* 1000 (- org-lowest-priority
  9661. (string-to-char (match-string 2 s)))))))
  9662. ;;;; Tags
  9663. (defvar org-agenda-archives-mode)
  9664. (defvar org-map-continue-from nil
  9665. "Position from where mapping should continue.
  9666. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9667. (defvar org-scanner-tags nil
  9668. "The current tag list while the tags scanner is running.")
  9669. (defvar org-trust-scanner-tags nil
  9670. "Should `org-get-tags-at' use the tags fro the scanner.
  9671. This is for internal dynamical scoping only.
  9672. When this is non-nil, the function `org-get-tags-at' will return the value
  9673. of `org-scanner-tags' instead of building the list by itself. This
  9674. can lead to large speed-ups when the tags scanner is used in a file with
  9675. many entries, and when the list of tags is retrieved, for example to
  9676. obtain a list of properties. Building the tags list for each entry in such
  9677. a file becomes an N^2 operation - but with this variable set, it scales
  9678. as N.")
  9679. (defun org-scan-tags (action matcher &optional todo-only)
  9680. "Scan headline tags with inheritance and produce output ACTION.
  9681. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9682. or `agenda' to produce an entry list for an agenda view. It can also be
  9683. a Lisp form or a function that should be called at each matched headline, in
  9684. this case the return value is a list of all return values from these calls.
  9685. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9686. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9687. only lines with a TODO keyword are included in the output."
  9688. (require 'org-agenda)
  9689. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9690. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9691. (org-re
  9692. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9693. (props (list 'face 'default
  9694. 'done-face 'org-agenda-done
  9695. 'undone-face 'default
  9696. 'mouse-face 'highlight
  9697. 'org-not-done-regexp org-not-done-regexp
  9698. 'org-todo-regexp org-todo-regexp
  9699. 'keymap org-agenda-keymap
  9700. 'help-echo
  9701. (format "mouse-2 or RET jump to org file %s"
  9702. (abbreviate-file-name
  9703. (or (buffer-file-name (buffer-base-buffer))
  9704. (buffer-name (buffer-base-buffer)))))))
  9705. (case-fold-search nil)
  9706. (org-map-continue-from nil)
  9707. lspos tags tags-list
  9708. (tags-alist (list (cons 0 org-file-tags)))
  9709. (llast 0) rtn rtn1 level category i txt
  9710. todo marker entry priority)
  9711. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9712. (setq action (list 'lambda nil action)))
  9713. (save-excursion
  9714. (goto-char (point-min))
  9715. (when (eq action 'sparse-tree)
  9716. (org-overview)
  9717. (org-remove-occur-highlights))
  9718. (while (re-search-forward re nil t)
  9719. (catch :skip
  9720. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9721. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9722. (goto-char (setq lspos (match-beginning 0)))
  9723. (setq level (org-reduced-level (funcall outline-level))
  9724. category (org-get-category))
  9725. (setq i llast llast level)
  9726. ;; remove tag lists from same and sublevels
  9727. (while (>= i level)
  9728. (when (setq entry (assoc i tags-alist))
  9729. (setq tags-alist (delete entry tags-alist)))
  9730. (setq i (1- i)))
  9731. ;; add the next tags
  9732. (when tags
  9733. (setq tags (org-split-string tags ":")
  9734. tags-alist
  9735. (cons (cons level tags) tags-alist)))
  9736. ;; compile tags for current headline
  9737. (setq tags-list
  9738. (if org-use-tag-inheritance
  9739. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9740. tags)
  9741. org-scanner-tags tags-list)
  9742. (when org-use-tag-inheritance
  9743. (setcdr (car tags-alist)
  9744. (mapcar (lambda (x)
  9745. (setq x (copy-sequence x))
  9746. (org-add-prop-inherited x))
  9747. (cdar tags-alist))))
  9748. (when (and tags org-use-tag-inheritance
  9749. (or (not (eq t org-use-tag-inheritance))
  9750. org-tags-exclude-from-inheritance))
  9751. ;; selective inheritance, remove uninherited ones
  9752. (setcdr (car tags-alist)
  9753. (org-remove-uniherited-tags (cdar tags-alist))))
  9754. (when (and (or (not todo-only)
  9755. (and (member todo org-not-done-keywords)
  9756. (or (not org-agenda-tags-todo-honor-ignore-options)
  9757. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9758. (let ((case-fold-search t)) (eval matcher))
  9759. (or
  9760. (not (member org-archive-tag tags-list))
  9761. ;; we have an archive tag, should we use this anyway?
  9762. (or (not org-agenda-skip-archived-trees)
  9763. (and (eq action 'agenda) org-agenda-archives-mode))))
  9764. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9765. ;; select this headline
  9766. (cond
  9767. ((eq action 'sparse-tree)
  9768. (and org-highlight-sparse-tree-matches
  9769. (org-get-heading) (match-end 0)
  9770. (org-highlight-new-match
  9771. (match-beginning 0) (match-beginning 1)))
  9772. (org-show-context 'tags-tree))
  9773. ((eq action 'agenda)
  9774. (setq txt (org-format-agenda-item
  9775. ""
  9776. (concat
  9777. (if (eq org-tags-match-list-sublevels 'indented)
  9778. (make-string (1- level) ?.) "")
  9779. (org-get-heading))
  9780. category
  9781. tags-list
  9782. )
  9783. priority (org-get-priority txt))
  9784. (goto-char lspos)
  9785. (setq marker (org-agenda-new-marker))
  9786. (org-add-props txt props
  9787. 'org-marker marker 'org-hd-marker marker 'org-category category
  9788. 'todo-state todo
  9789. 'priority priority 'type "tagsmatch")
  9790. (push txt rtn))
  9791. ((functionp action)
  9792. (setq org-map-continue-from nil)
  9793. (save-excursion
  9794. (setq rtn1 (funcall action))
  9795. (push rtn1 rtn)))
  9796. (t (error "Invalid action")))
  9797. ;; if we are to skip sublevels, jump to end of subtree
  9798. (unless org-tags-match-list-sublevels
  9799. (org-end-of-subtree t)
  9800. (backward-char 1))))
  9801. ;; Get the correct position from where to continue
  9802. (if org-map-continue-from
  9803. (goto-char org-map-continue-from)
  9804. (and (= (point) lspos) (end-of-line 1)))))
  9805. (when (and (eq action 'sparse-tree)
  9806. (not org-sparse-tree-open-archived-trees))
  9807. (org-hide-archived-subtrees (point-min) (point-max)))
  9808. (nreverse rtn)))
  9809. (defun org-remove-uniherited-tags (tags)
  9810. "Remove all tags that are not inherited from the list TAGS."
  9811. (cond
  9812. ((eq org-use-tag-inheritance t)
  9813. (if org-tags-exclude-from-inheritance
  9814. (org-delete-all org-tags-exclude-from-inheritance tags)
  9815. tags))
  9816. ((not org-use-tag-inheritance) nil)
  9817. ((stringp org-use-tag-inheritance)
  9818. (delq nil (mapcar
  9819. (lambda (x)
  9820. (if (and (string-match org-use-tag-inheritance x)
  9821. (not (member x org-tags-exclude-from-inheritance)))
  9822. x nil))
  9823. tags)))
  9824. ((listp org-use-tag-inheritance)
  9825. (delq nil (mapcar
  9826. (lambda (x)
  9827. (if (member x org-use-tag-inheritance) x nil))
  9828. tags)))))
  9829. (defvar todo-only) ;; dynamically scoped
  9830. (defun org-match-sparse-tree (&optional todo-only match)
  9831. "Create a sparse tree according to tags string MATCH.
  9832. MATCH can contain positive and negative selection of tags, like
  9833. \"+WORK+URGENT-WITHBOSS\".
  9834. If optional argument TODO-ONLY is non-nil, only select lines that are
  9835. also TODO lines."
  9836. (interactive "P")
  9837. (org-prepare-agenda-buffers (list (current-buffer)))
  9838. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9839. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9840. (defvar org-cached-props nil)
  9841. (defun org-cached-entry-get (pom property)
  9842. (if (or (eq t org-use-property-inheritance)
  9843. (and (stringp org-use-property-inheritance)
  9844. (string-match org-use-property-inheritance property))
  9845. (and (listp org-use-property-inheritance)
  9846. (member property org-use-property-inheritance)))
  9847. ;; Caching is not possible, check it directly
  9848. (org-entry-get pom property 'inherit)
  9849. ;; Get all properties, so that we can do complicated checks easily
  9850. (cdr (assoc property (or org-cached-props
  9851. (setq org-cached-props
  9852. (org-entry-properties pom)))))))
  9853. (defun org-global-tags-completion-table (&optional files)
  9854. "Return the list of all tags in all agenda buffer/files."
  9855. (save-excursion
  9856. (org-uniquify
  9857. (delq nil
  9858. (apply 'append
  9859. (mapcar
  9860. (lambda (file)
  9861. (set-buffer (find-file-noselect file))
  9862. (append (org-get-buffer-tags)
  9863. (mapcar (lambda (x) (if (stringp (car-safe x))
  9864. (list (car-safe x)) nil))
  9865. org-tag-alist)))
  9866. (if (and files (car files))
  9867. files
  9868. (org-agenda-files))))))))
  9869. (defun org-make-tags-matcher (match)
  9870. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9871. ;; todo-only is scoped dynamically into this function, and the function
  9872. ;; may change it if the matcher asks for it.
  9873. (unless match
  9874. ;; Get a new match request, with completion
  9875. (let ((org-last-tags-completion-table
  9876. (org-global-tags-completion-table)))
  9877. (setq match (org-completing-read-no-ido
  9878. "Match: " 'org-tags-completion-function nil nil nil
  9879. 'org-tags-history))))
  9880. ;; Parse the string and create a lisp form
  9881. (let ((match0 match)
  9882. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9883. minus tag mm
  9884. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9885. orterms term orlist re-p str-p level-p level-op time-p
  9886. prop-p pn pv po cat-p gv rest)
  9887. (if (string-match "/+" match)
  9888. ;; match contains also a todo-matching request
  9889. (progn
  9890. (setq tagsmatch (substring match 0 (match-beginning 0))
  9891. todomatch (substring match (match-end 0)))
  9892. (if (string-match "^!" todomatch)
  9893. (setq todo-only t todomatch (substring todomatch 1)))
  9894. (if (string-match "^\\s-*$" todomatch)
  9895. (setq todomatch nil)))
  9896. ;; only matching tags
  9897. (setq tagsmatch match todomatch nil))
  9898. ;; Make the tags matcher
  9899. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9900. (setq tagsmatcher t)
  9901. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9902. (while (setq term (pop orterms))
  9903. (while (and (equal (substring term -1) "\\") orterms)
  9904. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9905. (while (string-match re term)
  9906. (setq rest (substring term (match-end 0))
  9907. minus (and (match-end 1)
  9908. (equal (match-string 1 term) "-"))
  9909. tag (match-string 2 term)
  9910. re-p (equal (string-to-char tag) ?{)
  9911. level-p (match-end 4)
  9912. prop-p (match-end 5)
  9913. mm (cond
  9914. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9915. (level-p
  9916. (setq level-op (org-op-to-function (match-string 3 term)))
  9917. `(,level-op level ,(string-to-number
  9918. (match-string 4 term))))
  9919. (prop-p
  9920. (setq pn (match-string 5 term)
  9921. po (match-string 6 term)
  9922. pv (match-string 7 term)
  9923. cat-p (equal pn "CATEGORY")
  9924. re-p (equal (string-to-char pv) ?{)
  9925. str-p (equal (string-to-char pv) ?\")
  9926. time-p (save-match-data
  9927. (string-match "^\"[[<].*[]>]\"$" pv))
  9928. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9929. (if time-p (setq pv (org-matcher-time pv)))
  9930. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9931. (cond
  9932. ((equal pn "CATEGORY")
  9933. (setq gv '(get-text-property (point) 'org-category)))
  9934. ((equal pn "TODO")
  9935. (setq gv 'todo))
  9936. (t
  9937. (setq gv `(org-cached-entry-get nil ,pn))))
  9938. (if re-p
  9939. (if (eq po 'org<>)
  9940. `(not (string-match ,pv (or ,gv "")))
  9941. `(string-match ,pv (or ,gv "")))
  9942. (if str-p
  9943. `(,po (or ,gv "") ,pv)
  9944. `(,po (string-to-number (or ,gv ""))
  9945. ,(string-to-number pv) ))))
  9946. (t `(member ,tag tags-list)))
  9947. mm (if minus (list 'not mm) mm)
  9948. term rest)
  9949. (push mm tagsmatcher))
  9950. (push (if (> (length tagsmatcher) 1)
  9951. (cons 'and tagsmatcher)
  9952. (car tagsmatcher))
  9953. orlist)
  9954. (setq tagsmatcher nil))
  9955. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9956. (setq tagsmatcher
  9957. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9958. ;; Make the todo matcher
  9959. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9960. (setq todomatcher t)
  9961. (setq orterms (org-split-string todomatch "|") orlist nil)
  9962. (while (setq term (pop orterms))
  9963. (while (string-match re term)
  9964. (setq minus (and (match-end 1)
  9965. (equal (match-string 1 term) "-"))
  9966. kwd (match-string 2 term)
  9967. re-p (equal (string-to-char kwd) ?{)
  9968. term (substring term (match-end 0))
  9969. mm (if re-p
  9970. `(string-match ,(substring kwd 1 -1) todo)
  9971. (list 'equal 'todo kwd))
  9972. mm (if minus (list 'not mm) mm))
  9973. (push mm todomatcher))
  9974. (push (if (> (length todomatcher) 1)
  9975. (cons 'and todomatcher)
  9976. (car todomatcher))
  9977. orlist)
  9978. (setq todomatcher nil))
  9979. (setq todomatcher (if (> (length orlist) 1)
  9980. (cons 'or orlist) (car orlist))))
  9981. ;; Return the string and lisp forms of the matcher
  9982. (setq matcher (if todomatcher
  9983. (list 'and tagsmatcher todomatcher)
  9984. tagsmatcher))
  9985. (cons match0 matcher)))
  9986. (defun org-op-to-function (op &optional stringp)
  9987. "Turn an operator into the appropriate function."
  9988. (setq op
  9989. (cond
  9990. ((equal op "<" ) '(< string< org-time<))
  9991. ((equal op ">" ) '(> org-string> org-time>))
  9992. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9993. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9994. ((member op '("=" "==")) '(= string= org-time=))
  9995. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9996. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9997. (defun org<> (a b) (not (= a b)))
  9998. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9999. (defun org-string>= (a b) (not (string< a b)))
  10000. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10001. (defun org-string<> (a b) (not (string= a b)))
  10002. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10003. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10004. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10005. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10006. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10007. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10008. (defun org-2ft (s)
  10009. "Convert S to a floating point time.
  10010. If S is already a number, just return it. If it is a string, parse
  10011. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10012. (cond
  10013. ((numberp s) s)
  10014. ((stringp s)
  10015. (condition-case nil
  10016. (float-time (apply 'encode-time (org-parse-time-string s)))
  10017. (error 0.)))
  10018. (t 0.)))
  10019. (defun org-time-today ()
  10020. "Time in seconds today at 0:00.
  10021. Returns the float number of seconds since the beginning of the
  10022. epoch to the beginning of today (00:00)."
  10023. (float-time (apply 'encode-time
  10024. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10025. (defun org-matcher-time (s)
  10026. "Interpret a time comparison value."
  10027. (save-match-data
  10028. (cond
  10029. ((string= s "<now>") (float-time))
  10030. ((string= s "<today>") (org-time-today))
  10031. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10032. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10033. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10034. (+ (org-time-today)
  10035. (* (string-to-number (match-string 1 s))
  10036. (cdr (assoc (match-string 2 s)
  10037. '(("d" . 86400.0) ("w" . 604800.0)
  10038. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10039. (t (org-2ft s)))))
  10040. (defun org-match-any-p (re list)
  10041. "Does re match any element of list?"
  10042. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10043. (delq nil list))
  10044. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10045. (defvar org-tags-overlay (org-make-overlay 1 1))
  10046. (org-detach-overlay org-tags-overlay)
  10047. (defun org-get-local-tags-at (&optional pos)
  10048. "Get a list of tags defined in the current headline."
  10049. (org-get-tags-at pos 'local))
  10050. (defun org-get-local-tags ()
  10051. "Get a list of tags defined in the current headline."
  10052. (org-get-tags-at nil 'local))
  10053. (defun org-get-tags-at (&optional pos local)
  10054. "Get a list of all headline tags applicable at POS.
  10055. POS defaults to point. If tags are inherited, the list contains
  10056. the targets in the same sequence as the headlines appear, i.e.
  10057. the tags of the current headline come last.
  10058. When LOCAL is non-nil, only return tags from the current headline,
  10059. ignore inherited ones."
  10060. (interactive)
  10061. (if (and org-trust-scanner-tags
  10062. (or (not pos) (equal pos (point)))
  10063. (not local))
  10064. org-scanner-tags
  10065. (let (tags ltags lastpos parent)
  10066. (save-excursion
  10067. (save-restriction
  10068. (widen)
  10069. (goto-char (or pos (point)))
  10070. (save-match-data
  10071. (catch 'done
  10072. (condition-case nil
  10073. (progn
  10074. (org-back-to-heading t)
  10075. (while (not (equal lastpos (point)))
  10076. (setq lastpos (point))
  10077. (when (looking-at
  10078. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10079. (setq ltags (org-split-string
  10080. (org-match-string-no-properties 1) ":"))
  10081. (when parent
  10082. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10083. (setq tags (append
  10084. (if parent
  10085. (org-remove-uniherited-tags ltags)
  10086. ltags)
  10087. tags)))
  10088. (or org-use-tag-inheritance (throw 'done t))
  10089. (if local (throw 'done t))
  10090. (or (org-up-heading-safe) (error nil))
  10091. (setq parent t)))
  10092. (error nil)))))
  10093. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10094. (defun org-add-prop-inherited (s)
  10095. (add-text-properties 0 (length s) '(inherited t) s)
  10096. s)
  10097. (defun org-toggle-tag (tag &optional onoff)
  10098. "Toggle the tag TAG for the current line.
  10099. If ONOFF is `on' or `off', don't toggle but set to this state."
  10100. (let (res current)
  10101. (save-excursion
  10102. (org-back-to-heading t)
  10103. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10104. (point-at-eol) t)
  10105. (progn
  10106. (setq current (match-string 1))
  10107. (replace-match ""))
  10108. (setq current ""))
  10109. (setq current (nreverse (org-split-string current ":")))
  10110. (cond
  10111. ((eq onoff 'on)
  10112. (setq res t)
  10113. (or (member tag current) (push tag current)))
  10114. ((eq onoff 'off)
  10115. (or (not (member tag current)) (setq current (delete tag current))))
  10116. (t (if (member tag current)
  10117. (setq current (delete tag current))
  10118. (setq res t)
  10119. (push tag current))))
  10120. (end-of-line 1)
  10121. (if current
  10122. (progn
  10123. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10124. (org-set-tags nil t))
  10125. (delete-horizontal-space))
  10126. (run-hooks 'org-after-tags-change-hook))
  10127. res))
  10128. (defun org-align-tags-here (to-col)
  10129. ;; Assumes that this is a headline
  10130. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10131. (beginning-of-line 1)
  10132. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10133. (< pos (match-beginning 2)))
  10134. (progn
  10135. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10136. (goto-char (match-beginning 1))
  10137. (insert " ")
  10138. (delete-region (point) (1+ (match-beginning 2)))
  10139. (setq ncol (max (1+ (current-column))
  10140. (1+ col)
  10141. (if (> to-col 0)
  10142. to-col
  10143. (- (abs to-col) tags-l))))
  10144. (setq p (point))
  10145. (insert (make-string (- ncol (current-column)) ?\ ))
  10146. (setq ncol (current-column))
  10147. (when indent-tabs-mode (tabify p (point-at-eol)))
  10148. (org-move-to-column (min ncol col) t))
  10149. (goto-char pos))))
  10150. (defun org-set-tags-command (&optional arg just-align)
  10151. "Call the set-tags command for the current entry."
  10152. (interactive "P")
  10153. (if (org-on-heading-p)
  10154. (org-set-tags arg just-align)
  10155. (save-excursion
  10156. (org-back-to-heading t)
  10157. (org-set-tags arg just-align))))
  10158. (defun org-set-tags (&optional arg just-align)
  10159. "Set the tags for the current headline.
  10160. With prefix ARG, realign all tags in headings in the current buffer."
  10161. (interactive "P")
  10162. (let* ((re (concat "^" outline-regexp))
  10163. (current (org-get-tags-string))
  10164. (col (current-column))
  10165. (org-setting-tags t)
  10166. table current-tags inherited-tags ; computed below when needed
  10167. tags p0 c0 c1 rpl)
  10168. (if arg
  10169. (save-excursion
  10170. (goto-char (point-min))
  10171. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10172. (while (re-search-forward re nil t)
  10173. (org-set-tags nil t)
  10174. (end-of-line 1)))
  10175. (message "All tags realigned to column %d" org-tags-column))
  10176. (if just-align
  10177. (setq tags current)
  10178. ;; Get a new set of tags from the user
  10179. (save-excursion
  10180. (setq table (append org-tag-persistent-alist
  10181. (or org-tag-alist (org-get-buffer-tags)))
  10182. org-last-tags-completion-table table
  10183. current-tags (org-split-string current ":")
  10184. inherited-tags (nreverse
  10185. (nthcdr (length current-tags)
  10186. (nreverse (org-get-tags-at))))
  10187. tags
  10188. (if (or (eq t org-use-fast-tag-selection)
  10189. (and org-use-fast-tag-selection
  10190. (delq nil (mapcar 'cdr table))))
  10191. (org-fast-tag-selection
  10192. current-tags inherited-tags table
  10193. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10194. (let ((org-add-colon-after-tag-completion t))
  10195. (org-trim
  10196. (org-without-partial-completion
  10197. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  10198. nil nil current 'org-tags-history)))))))
  10199. (while (string-match "[-+&]+" tags)
  10200. ;; No boolean logic, just a list
  10201. (setq tags (replace-match ":" t t tags))))
  10202. (if (string-match "\\`[\t ]*\\'" tags)
  10203. (setq tags "")
  10204. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10205. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10206. ;; Insert new tags at the correct column
  10207. (beginning-of-line 1)
  10208. (cond
  10209. ((and (equal current "") (equal tags "")))
  10210. ((re-search-forward
  10211. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10212. (point-at-eol) t)
  10213. (if (equal tags "")
  10214. (setq rpl "")
  10215. (goto-char (match-beginning 0))
  10216. (setq c0 (current-column) p0 (point)
  10217. c1 (max (1+ c0) (if (> org-tags-column 0)
  10218. org-tags-column
  10219. (- (- org-tags-column) (length tags))))
  10220. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10221. (replace-match rpl t t)
  10222. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10223. tags)
  10224. (t (error "Tags alignment failed")))
  10225. (org-move-to-column col)
  10226. (unless just-align
  10227. (run-hooks 'org-after-tags-change-hook)))))
  10228. (defun org-change-tag-in-region (beg end tag off)
  10229. "Add or remove TAG for each entry in the region.
  10230. This works in the agenda, and also in an org-mode buffer."
  10231. (interactive
  10232. (list (region-beginning) (region-end)
  10233. (let ((org-last-tags-completion-table
  10234. (if (org-mode-p)
  10235. (org-get-buffer-tags)
  10236. (org-global-tags-completion-table))))
  10237. (org-ido-completing-read
  10238. "Tag: " 'org-tags-completion-function nil nil nil
  10239. 'org-tags-history))
  10240. (progn
  10241. (message "[s]et or [r]emove? ")
  10242. (equal (read-char-exclusive) ?r))))
  10243. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10244. (let ((agendap (equal major-mode 'org-agenda-mode))
  10245. l1 l2 m buf pos newhead (cnt 0))
  10246. (goto-char end)
  10247. (setq l2 (1- (org-current-line)))
  10248. (goto-char beg)
  10249. (setq l1 (org-current-line))
  10250. (loop for l from l1 to l2 do
  10251. (goto-line l)
  10252. (setq m (get-text-property (point) 'org-hd-marker))
  10253. (when (or (and (org-mode-p) (org-on-heading-p))
  10254. (and agendap m))
  10255. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10256. pos (if agendap m (point)))
  10257. (with-current-buffer buf
  10258. (save-excursion
  10259. (save-restriction
  10260. (goto-char pos)
  10261. (setq cnt (1+ cnt))
  10262. (org-toggle-tag tag (if off 'off 'on))
  10263. (setq newhead (org-get-heading)))))
  10264. (and agendap (org-agenda-change-all-lines newhead m))))
  10265. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10266. (defun org-tags-completion-function (string predicate &optional flag)
  10267. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10268. (confirm (lambda (x) (stringp (car x)))))
  10269. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10270. (setq s1 (match-string 1 string)
  10271. s2 (match-string 2 string))
  10272. (setq s1 "" s2 string))
  10273. (cond
  10274. ((eq flag nil)
  10275. ;; try completion
  10276. (setq rtn (try-completion s2 ctable confirm))
  10277. (if (stringp rtn)
  10278. (setq rtn
  10279. (concat s1 s2 (substring rtn (length s2))
  10280. (if (and org-add-colon-after-tag-completion
  10281. (assoc rtn ctable))
  10282. ":" ""))))
  10283. rtn)
  10284. ((eq flag t)
  10285. ;; all-completions
  10286. (all-completions s2 ctable confirm)
  10287. )
  10288. ((eq flag 'lambda)
  10289. ;; exact match?
  10290. (assoc s2 ctable)))
  10291. ))
  10292. (defun org-fast-tag-insert (kwd tags face &optional end)
  10293. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10294. (insert (format "%-12s" (concat kwd ":"))
  10295. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10296. (or end "")))
  10297. (defun org-fast-tag-show-exit (flag)
  10298. (save-excursion
  10299. (goto-line 3)
  10300. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10301. (replace-match ""))
  10302. (when flag
  10303. (end-of-line 1)
  10304. (org-move-to-column (- (window-width) 19) t)
  10305. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10306. (defun org-set-current-tags-overlay (current prefix)
  10307. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10308. (if (featurep 'xemacs)
  10309. (org-overlay-display org-tags-overlay (concat prefix s)
  10310. 'secondary-selection)
  10311. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10312. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10313. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10314. "Fast tag selection with single keys.
  10315. CURRENT is the current list of tags in the headline, INHERITED is the
  10316. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10317. possibly with grouping information. TODO-TABLE is a similar table with
  10318. TODO keywords, should these have keys assigned to them.
  10319. If the keys are nil, a-z are automatically assigned.
  10320. Returns the new tags string, or nil to not change the current settings."
  10321. (let* ((fulltable (append table todo-table))
  10322. (maxlen (apply 'max (mapcar
  10323. (lambda (x)
  10324. (if (stringp (car x)) (string-width (car x)) 0))
  10325. fulltable)))
  10326. (buf (current-buffer))
  10327. (expert (eq org-fast-tag-selection-single-key 'expert))
  10328. (buffer-tags nil)
  10329. (fwidth (+ maxlen 3 1 3))
  10330. (ncol (/ (- (window-width) 4) fwidth))
  10331. (i-face 'org-done)
  10332. (c-face 'org-todo)
  10333. tg cnt e c char c1 c2 ntable tbl rtn
  10334. ov-start ov-end ov-prefix
  10335. (exit-after-next org-fast-tag-selection-single-key)
  10336. (done-keywords org-done-keywords)
  10337. groups ingroup)
  10338. (save-excursion
  10339. (beginning-of-line 1)
  10340. (if (looking-at
  10341. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10342. (setq ov-start (match-beginning 1)
  10343. ov-end (match-end 1)
  10344. ov-prefix "")
  10345. (setq ov-start (1- (point-at-eol))
  10346. ov-end (1+ ov-start))
  10347. (skip-chars-forward "^\n\r")
  10348. (setq ov-prefix
  10349. (concat
  10350. (buffer-substring (1- (point)) (point))
  10351. (if (> (current-column) org-tags-column)
  10352. " "
  10353. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10354. (org-move-overlay org-tags-overlay ov-start ov-end)
  10355. (save-window-excursion
  10356. (if expert
  10357. (set-buffer (get-buffer-create " *Org tags*"))
  10358. (delete-other-windows)
  10359. (split-window-vertically)
  10360. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10361. (erase-buffer)
  10362. (org-set-local 'org-done-keywords done-keywords)
  10363. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10364. (org-fast-tag-insert "Current" current c-face "\n\n")
  10365. (org-fast-tag-show-exit exit-after-next)
  10366. (org-set-current-tags-overlay current ov-prefix)
  10367. (setq tbl fulltable char ?a cnt 0)
  10368. (while (setq e (pop tbl))
  10369. (cond
  10370. ((equal e '(:startgroup))
  10371. (push '() groups) (setq ingroup t)
  10372. (when (not (= cnt 0))
  10373. (setq cnt 0)
  10374. (insert "\n"))
  10375. (insert "{ "))
  10376. ((equal e '(:endgroup))
  10377. (setq ingroup nil cnt 0)
  10378. (insert "}\n"))
  10379. ((equal e '(:newline))
  10380. (when (not (= cnt 0))
  10381. (setq cnt 0)
  10382. (insert "\n")
  10383. (setq e (car tbl))
  10384. (while (equal (car tbl) '(:newline))
  10385. (insert "\n")
  10386. (setq tbl (cdr tbl)))))
  10387. (t
  10388. (setq tg (car e) c2 nil)
  10389. (if (cdr e)
  10390. (setq c (cdr e))
  10391. ;; automatically assign a character.
  10392. (setq c1 (string-to-char
  10393. (downcase (substring
  10394. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10395. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10396. (while (or (rassoc char ntable) (rassoc char table))
  10397. (setq char (1+ char)))
  10398. (setq c2 c1))
  10399. (setq c (or c2 char)))
  10400. (if ingroup (push tg (car groups)))
  10401. (setq tg (org-add-props tg nil 'face
  10402. (cond
  10403. ((not (assoc tg table))
  10404. (org-get-todo-face tg))
  10405. ((member tg current) c-face)
  10406. ((member tg inherited) i-face)
  10407. (t nil))))
  10408. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10409. (insert "[" c "] " tg (make-string
  10410. (- fwidth 4 (length tg)) ?\ ))
  10411. (push (cons tg c) ntable)
  10412. (when (= (setq cnt (1+ cnt)) ncol)
  10413. (insert "\n")
  10414. (if ingroup (insert " "))
  10415. (setq cnt 0)))))
  10416. (setq ntable (nreverse ntable))
  10417. (insert "\n")
  10418. (goto-char (point-min))
  10419. (if (not expert) (org-fit-window-to-buffer))
  10420. (setq rtn
  10421. (catch 'exit
  10422. (while t
  10423. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10424. (if groups " [!] no groups" " [!]groups")
  10425. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10426. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10427. (cond
  10428. ((= c ?\r) (throw 'exit t))
  10429. ((= c ?!)
  10430. (setq groups (not groups))
  10431. (goto-char (point-min))
  10432. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10433. ((= c ?\C-c)
  10434. (if (not expert)
  10435. (org-fast-tag-show-exit
  10436. (setq exit-after-next (not exit-after-next)))
  10437. (setq expert nil)
  10438. (delete-other-windows)
  10439. (split-window-vertically)
  10440. (org-switch-to-buffer-other-window " *Org tags*")
  10441. (org-fit-window-to-buffer)))
  10442. ((or (= c ?\C-g)
  10443. (and (= c ?q) (not (rassoc c ntable))))
  10444. (org-detach-overlay org-tags-overlay)
  10445. (setq quit-flag t))
  10446. ((= c ?\ )
  10447. (setq current nil)
  10448. (if exit-after-next (setq exit-after-next 'now)))
  10449. ((= c ?\t)
  10450. (condition-case nil
  10451. (setq tg (org-ido-completing-read
  10452. "Tag: "
  10453. (or buffer-tags
  10454. (with-current-buffer buf
  10455. (org-get-buffer-tags)))))
  10456. (quit (setq tg "")))
  10457. (when (string-match "\\S-" tg)
  10458. (add-to-list 'buffer-tags (list tg))
  10459. (if (member tg current)
  10460. (setq current (delete tg current))
  10461. (push tg current)))
  10462. (if exit-after-next (setq exit-after-next 'now)))
  10463. ((setq e (rassoc c todo-table) tg (car e))
  10464. (with-current-buffer buf
  10465. (save-excursion (org-todo tg)))
  10466. (if exit-after-next (setq exit-after-next 'now)))
  10467. ((setq e (rassoc c ntable) tg (car e))
  10468. (if (member tg current)
  10469. (setq current (delete tg current))
  10470. (loop for g in groups do
  10471. (if (member tg g)
  10472. (mapc (lambda (x)
  10473. (setq current (delete x current)))
  10474. g)))
  10475. (push tg current))
  10476. (if exit-after-next (setq exit-after-next 'now))))
  10477. ;; Create a sorted list
  10478. (setq current
  10479. (sort current
  10480. (lambda (a b)
  10481. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10482. (if (eq exit-after-next 'now) (throw 'exit t))
  10483. (goto-char (point-min))
  10484. (beginning-of-line 2)
  10485. (delete-region (point) (point-at-eol))
  10486. (org-fast-tag-insert "Current" current c-face)
  10487. (org-set-current-tags-overlay current ov-prefix)
  10488. (while (re-search-forward
  10489. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10490. (setq tg (match-string 1))
  10491. (add-text-properties
  10492. (match-beginning 1) (match-end 1)
  10493. (list 'face
  10494. (cond
  10495. ((member tg current) c-face)
  10496. ((member tg inherited) i-face)
  10497. (t (get-text-property (match-beginning 1) 'face))))))
  10498. (goto-char (point-min)))))
  10499. (org-detach-overlay org-tags-overlay)
  10500. (if rtn
  10501. (mapconcat 'identity current ":")
  10502. nil))))
  10503. (defun org-get-tags-string ()
  10504. "Get the TAGS string in the current headline."
  10505. (unless (org-on-heading-p t)
  10506. (error "Not on a heading"))
  10507. (save-excursion
  10508. (beginning-of-line 1)
  10509. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10510. (org-match-string-no-properties 1)
  10511. "")))
  10512. (defun org-get-tags ()
  10513. "Get the list of tags specified in the current headline."
  10514. (org-split-string (org-get-tags-string) ":"))
  10515. (defun org-get-buffer-tags ()
  10516. "Get a table of all tags used in the buffer, for completion."
  10517. (let (tags)
  10518. (save-excursion
  10519. (goto-char (point-min))
  10520. (while (re-search-forward
  10521. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10522. (when (equal (char-after (point-at-bol 0)) ?*)
  10523. (mapc (lambda (x) (add-to-list 'tags x))
  10524. (org-split-string (org-match-string-no-properties 1) ":")))))
  10525. (mapcar 'list tags)))
  10526. ;;;; The mapping API
  10527. ;;;###autoload
  10528. (defun org-map-entries (func &optional match scope &rest skip)
  10529. "Call FUNC at each headline selected by MATCH in SCOPE.
  10530. FUNC is a function or a lisp form. The function will be called without
  10531. arguments, with the cursor positioned at the beginning of the headline.
  10532. The return values of all calls to the function will be collected and
  10533. returned as a list.
  10534. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10535. does not need to preserve point. After evaluaton, the cursor will be
  10536. moved to the end of the line (presumably of the headline of the
  10537. processed entry) and search continues from there. Under some
  10538. circumstances, this may not produce the wanted results. For example,
  10539. if you have removed (e.g. archived) the current (sub)tree it could
  10540. mean that the next entry will be skipped entirely. In such cases, you
  10541. can specify the position from where search should continue by making
  10542. FUNC set the variable `org-map-continue-from' to the desired buffer
  10543. position.
  10544. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10545. Only headlines that are matched by this query will be considered during
  10546. the iteration. When MATCH is nil or t, all headlines will be
  10547. visited by the iteration.
  10548. SCOPE determines the scope of this command. It can be any of:
  10549. nil The current buffer, respecting the restriction if any
  10550. tree The subtree started with the entry at point
  10551. file The current buffer, without restriction
  10552. file-with-archives
  10553. The current buffer, and any archives associated with it
  10554. agenda All agenda files
  10555. agenda-with-archives
  10556. All agenda files with any archive files associated with them
  10557. \(file1 file2 ...)
  10558. If this is a list, all files in the list will be scanned
  10559. The remaining args are treated as settings for the skipping facilities of
  10560. the scanner. The following items can be given here:
  10561. archive skip trees with the archive tag.
  10562. comment skip trees with the COMMENT keyword
  10563. function or Emacs Lisp form:
  10564. will be used as value for `org-agenda-skip-function', so whenever
  10565. the the function returns t, FUNC will not be called for that
  10566. entry and search will continue from the point where the
  10567. function leaves it.
  10568. If your function needs to retrieve the tags including inherited tags
  10569. at the *current* entry, you can use the value of the variable
  10570. `org-scanner-tags' which will be much faster than getting the value
  10571. with `org-get-tags-at'. If your function gets properties with
  10572. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10573. to t around the call to `org-entry-properties' to get the same speedup.
  10574. Note that if your function moves around to retrieve tags and properties at
  10575. a *different* entry, you cannot use these techniques."
  10576. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10577. (org-agenda-skip-archived-trees (memq 'archive skip))
  10578. (org-agenda-skip-comment-trees (memq 'comment skip))
  10579. (org-agenda-skip-function
  10580. (car (org-delete-all '(comment archive) skip)))
  10581. (org-tags-match-list-sublevels t)
  10582. matcher file res
  10583. org-todo-keywords-for-agenda
  10584. org-done-keywords-for-agenda
  10585. org-todo-keyword-alist-for-agenda
  10586. org-tag-alist-for-agenda)
  10587. (cond
  10588. ((eq match t) (setq matcher t))
  10589. ((eq match nil) (setq matcher t))
  10590. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10591. (save-excursion
  10592. (save-restriction
  10593. (when (eq scope 'tree)
  10594. (org-back-to-heading t)
  10595. (org-narrow-to-subtree)
  10596. (setq scope nil))
  10597. (if (not scope)
  10598. (progn
  10599. (org-prepare-agenda-buffers
  10600. (list (buffer-file-name (current-buffer))))
  10601. (setq res (org-scan-tags func matcher)))
  10602. ;; Get the right scope
  10603. (cond
  10604. ((and scope (listp scope) (symbolp (car scope)))
  10605. (setq scope (eval scope)))
  10606. ((eq scope 'agenda)
  10607. (setq scope (org-agenda-files t)))
  10608. ((eq scope 'agenda-with-archives)
  10609. (setq scope (org-agenda-files t))
  10610. (setq scope (org-add-archive-files scope)))
  10611. ((eq scope 'file)
  10612. (setq scope (list (buffer-file-name))))
  10613. ((eq scope 'file-with-archives)
  10614. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10615. (org-prepare-agenda-buffers scope)
  10616. (while (setq file (pop scope))
  10617. (with-current-buffer (org-find-base-buffer-visiting file)
  10618. (save-excursion
  10619. (save-restriction
  10620. (widen)
  10621. (goto-char (point-min))
  10622. (setq res (append res (org-scan-tags func matcher))))))))))
  10623. res))
  10624. ;;;; Properties
  10625. ;;; Setting and retrieving properties
  10626. (defconst org-special-properties
  10627. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10628. "TIMESTAMP" "TIMESTAMP_IA")
  10629. "The special properties valid in Org-mode.
  10630. These are properties that are not defined in the property drawer,
  10631. but in some other way.")
  10632. (defconst org-default-properties
  10633. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10634. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10635. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10636. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10637. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10638. "CLOCK_MODELINE_TOTAL")
  10639. "Some properties that are used by Org-mode for various purposes.
  10640. Being in this list makes sure that they are offered for completion.")
  10641. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10642. "Regular expression matching the first line of a property drawer.")
  10643. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10644. "Regular expression matching the first line of a property drawer.")
  10645. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10646. "Regular expression matching the first line of a property drawer.")
  10647. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10648. "Regular expression matching the first line of a property drawer.")
  10649. (defconst org-property-drawer-re
  10650. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10651. org-property-end-re "\\)\n?")
  10652. "Matches an entire property drawer.")
  10653. (defconst org-clock-drawer-re
  10654. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10655. org-property-end-re "\\)\n?")
  10656. "Matches an entire clock drawer.")
  10657. (defun org-property-action ()
  10658. "Do an action on properties."
  10659. (interactive)
  10660. (let (c)
  10661. (org-at-property-p)
  10662. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10663. (setq c (read-char-exclusive))
  10664. (cond
  10665. ((equal c ?s)
  10666. (call-interactively 'org-set-property))
  10667. ((equal c ?d)
  10668. (call-interactively 'org-delete-property))
  10669. ((equal c ?D)
  10670. (call-interactively 'org-delete-property-globally))
  10671. ((equal c ?c)
  10672. (call-interactively 'org-compute-property-at-point))
  10673. (t (error "No such property action %c" c)))))
  10674. (defun org-at-property-p ()
  10675. "Is the cursor in a property line?"
  10676. ;; FIXME: Does not check if we are actually in the drawer.
  10677. ;; FIXME: also returns true on any drawers.....
  10678. ;; This is used by C-c C-c for property action.
  10679. (save-excursion
  10680. (beginning-of-line 1)
  10681. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10682. (defun org-get-property-block (&optional beg end force)
  10683. "Return the (beg . end) range of the body of the property drawer.
  10684. BEG and END can be beginning and end of subtree, if not given
  10685. they will be found.
  10686. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10687. (catch 'exit
  10688. (save-excursion
  10689. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10690. (end (or end (progn (outline-next-heading) (point)))))
  10691. (goto-char beg)
  10692. (if (re-search-forward org-property-start-re end t)
  10693. (setq beg (1+ (match-end 0)))
  10694. (if force
  10695. (save-excursion
  10696. (org-insert-property-drawer)
  10697. (setq end (progn (outline-next-heading) (point))))
  10698. (throw 'exit nil))
  10699. (goto-char beg)
  10700. (if (re-search-forward org-property-start-re end t)
  10701. (setq beg (1+ (match-end 0)))))
  10702. (if (re-search-forward org-property-end-re end t)
  10703. (setq end (match-beginning 0))
  10704. (or force (throw 'exit nil))
  10705. (goto-char beg)
  10706. (setq end beg)
  10707. (org-indent-line-function)
  10708. (insert ":END:\n"))
  10709. (cons beg end)))))
  10710. (defun org-entry-properties (&optional pom which)
  10711. "Get all properties of the entry at point-or-marker POM.
  10712. This includes the TODO keyword, the tags, time strings for deadline,
  10713. scheduled, and clocking, and any additional properties defined in the
  10714. entry. The return value is an alist, keys may occur multiple times
  10715. if the property key was used several times.
  10716. POM may also be nil, in which case the current entry is used.
  10717. If WHICH is nil or `all', get all properties. If WHICH is
  10718. `special' or `standard', only get that subclass."
  10719. (setq which (or which 'all))
  10720. (org-with-point-at pom
  10721. (let ((clockstr (substring org-clock-string 0 -1))
  10722. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10723. beg end range props sum-props key value string clocksum)
  10724. (save-excursion
  10725. (when (condition-case nil
  10726. (and (org-mode-p) (org-back-to-heading t))
  10727. (error nil))
  10728. (setq beg (point))
  10729. (setq sum-props (get-text-property (point) 'org-summaries))
  10730. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10731. (outline-next-heading)
  10732. (setq end (point))
  10733. (when (memq which '(all special))
  10734. ;; Get the special properties, like TODO and tags
  10735. (goto-char beg)
  10736. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10737. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10738. (when (looking-at org-priority-regexp)
  10739. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10740. (when (and (setq value (org-get-tags-string))
  10741. (string-match "\\S-" value))
  10742. (push (cons "TAGS" value) props))
  10743. (when (setq value (org-get-tags-at))
  10744. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10745. props))
  10746. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10747. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10748. string (if (equal key clockstr)
  10749. (org-no-properties
  10750. (org-trim
  10751. (buffer-substring
  10752. (match-beginning 3) (goto-char (point-at-eol)))))
  10753. (substring (org-match-string-no-properties 3) 1 -1)))
  10754. (unless key
  10755. (if (= (char-after (match-beginning 3)) ?\[)
  10756. (setq key "TIMESTAMP_IA")
  10757. (setq key "TIMESTAMP")))
  10758. (when (or (equal key clockstr) (not (assoc key props)))
  10759. (push (cons key string) props)))
  10760. )
  10761. (when (memq which '(all standard))
  10762. ;; Get the standard properties, like :PROP: ...
  10763. (setq range (org-get-property-block beg end))
  10764. (when range
  10765. (goto-char (car range))
  10766. (while (re-search-forward
  10767. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10768. (cdr range) t)
  10769. (setq key (org-match-string-no-properties 1)
  10770. value (org-trim (or (org-match-string-no-properties 2) "")))
  10771. (unless (member key excluded)
  10772. (push (cons key (or value "")) props)))))
  10773. (if clocksum
  10774. (push (cons "CLOCKSUM"
  10775. (org-columns-number-to-string (/ (float clocksum) 60.)
  10776. 'add_times))
  10777. props))
  10778. (unless (assoc "CATEGORY" props)
  10779. (setq value (or (org-get-category)
  10780. (progn (org-refresh-category-properties)
  10781. (org-get-category))))
  10782. (push (cons "CATEGORY" value) props))
  10783. (append sum-props (nreverse props)))))))
  10784. (defun org-entry-get (pom property &optional inherit)
  10785. "Get value of PROPERTY for entry at point-or-marker POM.
  10786. If INHERIT is non-nil and the entry does not have the property,
  10787. then also check higher levels of the hierarchy.
  10788. If INHERIT is the symbol `selective', use inheritance only if the setting
  10789. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10790. If the property is present but empty, the return value is the empty string.
  10791. If the property is not present at all, nil is returned."
  10792. (org-with-point-at pom
  10793. (if (and inherit (if (eq inherit 'selective)
  10794. (org-property-inherit-p property)
  10795. t))
  10796. (org-entry-get-with-inheritance property)
  10797. (if (member property org-special-properties)
  10798. ;; We need a special property. Use brute force, get all properties.
  10799. (cdr (assoc property (org-entry-properties nil 'special)))
  10800. (let ((range (org-get-property-block)))
  10801. (if (and range
  10802. (goto-char (car range))
  10803. (re-search-forward
  10804. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10805. (cdr range) t))
  10806. ;; Found the property, return it.
  10807. (if (match-end 1)
  10808. (org-match-string-no-properties 1)
  10809. "")))))))
  10810. (defun org-property-or-variable-value (var &optional inherit)
  10811. "Check if there is a property fixing the value of VAR.
  10812. If yes, return this value. If not, return the current value of the variable."
  10813. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10814. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10815. (read prop)
  10816. (symbol-value var))))
  10817. (defun org-entry-delete (pom property)
  10818. "Delete the property PROPERTY from entry at point-or-marker POM."
  10819. (org-with-point-at pom
  10820. (if (member property org-special-properties)
  10821. nil ; cannot delete these properties.
  10822. (let ((range (org-get-property-block)))
  10823. (if (and range
  10824. (goto-char (car range))
  10825. (re-search-forward
  10826. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10827. (cdr range) t))
  10828. (progn
  10829. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10830. t)
  10831. nil)))))
  10832. ;; Multi-values properties are properties that contain multiple values
  10833. ;; These values are assumed to be single words, separated by whitespace.
  10834. (defun org-entry-add-to-multivalued-property (pom property value)
  10835. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10836. (let* ((old (org-entry-get pom property))
  10837. (values (and old (org-split-string old "[ \t]"))))
  10838. (setq value (org-entry-protect-space value))
  10839. (unless (member value values)
  10840. (setq values (cons value values))
  10841. (org-entry-put pom property
  10842. (mapconcat 'identity values " ")))))
  10843. (defun org-entry-remove-from-multivalued-property (pom property value)
  10844. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10845. (let* ((old (org-entry-get pom property))
  10846. (values (and old (org-split-string old "[ \t]"))))
  10847. (setq value (org-entry-protect-space value))
  10848. (when (member value values)
  10849. (setq values (delete value values))
  10850. (org-entry-put pom property
  10851. (mapconcat 'identity values " ")))))
  10852. (defun org-entry-member-in-multivalued-property (pom property value)
  10853. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10854. (let* ((old (org-entry-get pom property))
  10855. (values (and old (org-split-string old "[ \t]"))))
  10856. (setq value (org-entry-protect-space value))
  10857. (member value values)))
  10858. (defun org-entry-get-multivalued-property (pom property)
  10859. "Return a list of values in a multivalued property."
  10860. (let* ((value (org-entry-get pom property))
  10861. (values (and value (org-split-string value "[ \t]"))))
  10862. (mapcar 'org-entry-restore-space values)))
  10863. (defun org-entry-put-multivalued-property (pom property &rest values)
  10864. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10865. VALUES should be a list of strings. Spaces will be protected."
  10866. (org-entry-put pom property
  10867. (mapconcat 'org-entry-protect-space values " "))
  10868. (let* ((value (org-entry-get pom property))
  10869. (values (and value (org-split-string value "[ \t]"))))
  10870. (mapcar 'org-entry-restore-space values)))
  10871. (defun org-entry-protect-space (s)
  10872. "Protect spaces and newline in string S."
  10873. (while (string-match " " s)
  10874. (setq s (replace-match "%20" t t s)))
  10875. (while (string-match "\n" s)
  10876. (setq s (replace-match "%0A" t t s)))
  10877. s)
  10878. (defun org-entry-restore-space (s)
  10879. "Restore spaces and newline in string S."
  10880. (while (string-match "%20" s)
  10881. (setq s (replace-match " " t t s)))
  10882. (while (string-match "%0A" s)
  10883. (setq s (replace-match "\n" t t s)))
  10884. s)
  10885. (defvar org-entry-property-inherited-from (make-marker)
  10886. "Marker pointing to the entry from where a property was inherited.
  10887. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10888. location of the entry where the inheritance search matched. If there was
  10889. no match, the marker will point nowhere.
  10890. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10891. is set.")
  10892. (defun org-entry-get-with-inheritance (property)
  10893. "Get entry property, and search higher levels if not present."
  10894. (move-marker org-entry-property-inherited-from nil)
  10895. (let (tmp)
  10896. (save-excursion
  10897. (save-restriction
  10898. (widen)
  10899. (catch 'ex
  10900. (while t
  10901. (when (setq tmp (org-entry-get nil property))
  10902. (org-back-to-heading t)
  10903. (move-marker org-entry-property-inherited-from (point))
  10904. (throw 'ex tmp))
  10905. (or (org-up-heading-safe) (throw 'ex nil)))))
  10906. (or tmp
  10907. (cdr (assoc property org-file-properties))
  10908. (cdr (assoc property org-global-properties))
  10909. (cdr (assoc property org-global-properties-fixed))))))
  10910. (defun org-entry-put (pom property value)
  10911. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10912. (org-with-point-at pom
  10913. (org-back-to-heading t)
  10914. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10915. range)
  10916. (cond
  10917. ((equal property "TODO")
  10918. (when (and (stringp value) (string-match "\\S-" value)
  10919. (not (member value org-todo-keywords-1)))
  10920. (error "\"%s\" is not a valid TODO state" value))
  10921. (if (or (not value)
  10922. (not (string-match "\\S-" value)))
  10923. (setq value 'none))
  10924. (org-todo value)
  10925. (org-set-tags nil 'align))
  10926. ((equal property "PRIORITY")
  10927. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10928. (string-to-char value) ?\ ))
  10929. (org-set-tags nil 'align))
  10930. ((equal property "SCHEDULED")
  10931. (if (re-search-forward org-scheduled-time-regexp end t)
  10932. (cond
  10933. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10934. ((eq value 'later) (org-timestamp-change 1 'day))
  10935. (t (call-interactively 'org-schedule)))
  10936. (call-interactively 'org-schedule)))
  10937. ((equal property "DEADLINE")
  10938. (if (re-search-forward org-deadline-time-regexp end t)
  10939. (cond
  10940. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10941. ((eq value 'later) (org-timestamp-change 1 'day))
  10942. (t (call-interactively 'org-deadline)))
  10943. (call-interactively 'org-deadline)))
  10944. ((member property org-special-properties)
  10945. (error "The %s property can not yet be set with `org-entry-put'"
  10946. property))
  10947. (t ; a non-special property
  10948. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10949. (setq range (org-get-property-block beg end 'force))
  10950. (goto-char (car range))
  10951. (if (re-search-forward
  10952. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10953. (progn
  10954. (delete-region (match-beginning 1) (match-end 1))
  10955. (goto-char (match-beginning 1)))
  10956. (goto-char (cdr range))
  10957. (insert "\n")
  10958. (backward-char 1)
  10959. (org-indent-line-function)
  10960. (insert ":" property ":"))
  10961. (and value (insert " " value))
  10962. (org-indent-line-function)))))))
  10963. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10964. "Get all property keys in the current buffer.
  10965. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10966. like tags and TODO state.
  10967. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10968. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10969. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10970. formats in the current buffer."
  10971. (let (rtn range cfmt s p)
  10972. (save-excursion
  10973. (save-restriction
  10974. (widen)
  10975. (goto-char (point-min))
  10976. (while (re-search-forward org-property-start-re nil t)
  10977. (setq range (org-get-property-block))
  10978. (goto-char (car range))
  10979. (while (re-search-forward
  10980. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10981. (cdr range) t)
  10982. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10983. (outline-next-heading))))
  10984. (when include-specials
  10985. (setq rtn (append org-special-properties rtn)))
  10986. (when include-defaults
  10987. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10988. (add-to-list 'rtn org-effort-property))
  10989. (when include-columns
  10990. (save-excursion
  10991. (save-restriction
  10992. (widen)
  10993. (goto-char (point-min))
  10994. (while (re-search-forward
  10995. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10996. nil t)
  10997. (setq cfmt (match-string 2) s 0)
  10998. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10999. cfmt s)
  11000. (setq s (match-end 0)
  11001. p (match-string 1 cfmt))
  11002. (unless (or (equal p "ITEM")
  11003. (member p org-special-properties))
  11004. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11005. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11006. (defun org-property-values (key)
  11007. "Return a list of all values of property KEY."
  11008. (save-excursion
  11009. (save-restriction
  11010. (widen)
  11011. (goto-char (point-min))
  11012. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11013. values)
  11014. (while (re-search-forward re nil t)
  11015. (add-to-list 'values (org-trim (match-string 1))))
  11016. (delete "" values)))))
  11017. (defun org-insert-property-drawer ()
  11018. "Insert a property drawer into the current entry."
  11019. (interactive)
  11020. (org-back-to-heading t)
  11021. (looking-at outline-regexp)
  11022. (let ((indent (if org-adapt-indentation
  11023. (- (match-end 0)(match-beginning 0))
  11024. 0))
  11025. (beg (point))
  11026. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11027. end hiddenp)
  11028. (outline-next-heading)
  11029. (setq end (point))
  11030. (goto-char beg)
  11031. (while (re-search-forward re end t))
  11032. (setq hiddenp (org-invisible-p))
  11033. (end-of-line 1)
  11034. (and (equal (char-after) ?\n) (forward-char 1))
  11035. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11036. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11037. ;; just skip this line
  11038. (beginning-of-line 2)
  11039. ;; Drawer start, find the end
  11040. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11041. (beginning-of-line 1)))
  11042. (org-skip-over-state-notes)
  11043. (skip-chars-backward " \t\n\r")
  11044. (if (eq (char-before) ?*) (forward-char 1))
  11045. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11046. (beginning-of-line 0)
  11047. (org-indent-to-column indent)
  11048. (beginning-of-line 2)
  11049. (org-indent-to-column indent)
  11050. (beginning-of-line 0)
  11051. (if hiddenp
  11052. (save-excursion
  11053. (org-back-to-heading t)
  11054. (hide-entry))
  11055. (org-flag-drawer t))))
  11056. (defun org-set-property (property value)
  11057. "In the current entry, set PROPERTY to VALUE.
  11058. When called interactively, this will prompt for a property name, offering
  11059. completion on existing and default properties. And then it will prompt
  11060. for a value, offering completion either on allowed values (via an inherited
  11061. xxx_ALL property) or on existing values in other instances of this property
  11062. in the current file."
  11063. (interactive
  11064. (let* ((completion-ignore-case t)
  11065. (keys (org-buffer-property-keys nil t t))
  11066. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  11067. (prop (if (member prop0 keys)
  11068. prop0
  11069. (or (cdr (assoc (downcase prop0)
  11070. (mapcar (lambda (x) (cons (downcase x) x))
  11071. keys)))
  11072. prop0)))
  11073. (cur (org-entry-get nil prop))
  11074. (allowed (org-property-get-allowed-values nil prop 'table))
  11075. (existing (mapcar 'list (org-property-values prop)))
  11076. (val (if allowed
  11077. (org-completing-read "Value: " allowed nil 'req-match)
  11078. (let (org-completion-use-ido)
  11079. (org-completing-read
  11080. (concat "Value" (if (and cur (string-match "\\S-" cur))
  11081. (concat "[" cur "]") "")
  11082. ": ")
  11083. existing nil nil "" nil cur)))))
  11084. (list prop (if (equal val "") cur val))))
  11085. (unless (equal (org-entry-get nil property) value)
  11086. (org-entry-put nil property value)))
  11087. (defun org-delete-property (property)
  11088. "In the current entry, delete PROPERTY."
  11089. (interactive
  11090. (let* ((completion-ignore-case t)
  11091. (prop (org-ido-completing-read
  11092. "Property: " (org-entry-properties nil 'standard))))
  11093. (list prop)))
  11094. (message "Property %s %s" property
  11095. (if (org-entry-delete nil property)
  11096. "deleted"
  11097. "was not present in the entry")))
  11098. (defun org-delete-property-globally (property)
  11099. "Remove PROPERTY globally, from all entries."
  11100. (interactive
  11101. (let* ((completion-ignore-case t)
  11102. (prop (org-ido-completing-read
  11103. "Globally remove property: "
  11104. (mapcar 'list (org-buffer-property-keys)))))
  11105. (list prop)))
  11106. (save-excursion
  11107. (save-restriction
  11108. (widen)
  11109. (goto-char (point-min))
  11110. (let ((cnt 0))
  11111. (while (re-search-forward
  11112. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11113. nil t)
  11114. (setq cnt (1+ cnt))
  11115. (replace-match ""))
  11116. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11117. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11118. (defun org-compute-property-at-point ()
  11119. "Compute the property at point.
  11120. This looks for an enclosing column format, extracts the operator and
  11121. then applies it to the property in the column format's scope."
  11122. (interactive)
  11123. (unless (org-at-property-p)
  11124. (error "Not at a property"))
  11125. (let ((prop (org-match-string-no-properties 2)))
  11126. (org-columns-get-format-and-top-level)
  11127. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11128. (error "No operator defined for property %s" prop))
  11129. (org-columns-compute prop)))
  11130. (defun org-property-get-allowed-values (pom property &optional table)
  11131. "Get allowed values for the property PROPERTY.
  11132. When TABLE is non-nil, return an alist that can directly be used for
  11133. completion."
  11134. (let (vals)
  11135. (cond
  11136. ((equal property "TODO")
  11137. (setq vals (org-with-point-at pom
  11138. (append org-todo-keywords-1 '("")))))
  11139. ((equal property "PRIORITY")
  11140. (let ((n org-lowest-priority))
  11141. (while (>= n org-highest-priority)
  11142. (push (char-to-string n) vals)
  11143. (setq n (1- n)))))
  11144. ((member property org-special-properties))
  11145. (t
  11146. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11147. (when (and vals (string-match "\\S-" vals))
  11148. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11149. (setq vals (mapcar (lambda (x)
  11150. (cond ((stringp x) x)
  11151. ((numberp x) (number-to-string x))
  11152. ((symbolp x) (symbol-name x))
  11153. (t "???")))
  11154. vals)))))
  11155. (if table (mapcar 'list vals) vals)))
  11156. (defun org-property-previous-allowed-value (&optional previous)
  11157. "Switch to the next allowed value for this property."
  11158. (interactive)
  11159. (org-property-next-allowed-value t))
  11160. (defun org-property-next-allowed-value (&optional previous)
  11161. "Switch to the next allowed value for this property."
  11162. (interactive)
  11163. (unless (org-at-property-p)
  11164. (error "Not at a property"))
  11165. (let* ((key (match-string 2))
  11166. (value (match-string 3))
  11167. (allowed (or (org-property-get-allowed-values (point) key)
  11168. (and (member value '("[ ]" "[-]" "[X]"))
  11169. '("[ ]" "[X]"))))
  11170. nval)
  11171. (unless allowed
  11172. (error "Allowed values for this property have not been defined"))
  11173. (if previous (setq allowed (reverse allowed)))
  11174. (if (member value allowed)
  11175. (setq nval (car (cdr (member value allowed)))))
  11176. (setq nval (or nval (car allowed)))
  11177. (if (equal nval value)
  11178. (error "Only one allowed value for this property"))
  11179. (org-at-property-p)
  11180. (replace-match (concat " :" key ": " nval) t t)
  11181. (org-indent-line-function)
  11182. (beginning-of-line 1)
  11183. (skip-chars-forward " \t")))
  11184. (defun org-find-entry-with-id (ident)
  11185. "Locate the entry that contains the ID property with exact value IDENT.
  11186. IDENT can be a string, a symbol or a number, this function will search for
  11187. the string representation of it.
  11188. Return the position where this entry starts, or nil if there is no such entry."
  11189. (interactive "sID: ")
  11190. (let ((id (cond
  11191. ((stringp ident) ident)
  11192. ((symbol-name ident) (symbol-name ident))
  11193. ((numberp ident) (number-to-string ident))
  11194. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11195. (case-fold-search nil))
  11196. (save-excursion
  11197. (save-restriction
  11198. (widen)
  11199. (goto-char (point-min))
  11200. (when (re-search-forward
  11201. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11202. nil t)
  11203. (org-back-to-heading)
  11204. (point))))))
  11205. ;;;; Timestamps
  11206. (defvar org-last-changed-timestamp nil)
  11207. (defvar org-last-inserted-timestamp nil
  11208. "The last time stamp inserted with `org-insert-time-stamp'.")
  11209. (defvar org-time-was-given) ; dynamically scoped parameter
  11210. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11211. (defvar org-ts-what) ; dynamically scoped parameter
  11212. (defun org-time-stamp (arg &optional inactive)
  11213. "Prompt for a date/time and insert a time stamp.
  11214. If the user specifies a time like HH:MM, or if this command is called
  11215. with a prefix argument, the time stamp will contain date and time.
  11216. Otherwise, only the date will be included. All parts of a date not
  11217. specified by the user will be filled in from the current date/time.
  11218. So if you press just return without typing anything, the time stamp
  11219. will represent the current date/time. If there is already a timestamp
  11220. at the cursor, it will be modified."
  11221. (interactive "P")
  11222. (let* ((ts nil)
  11223. (default-time
  11224. ;; Default time is either today, or, when entering a range,
  11225. ;; the range start.
  11226. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11227. (save-excursion
  11228. (re-search-backward
  11229. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11230. (- (point) 20) t)))
  11231. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11232. (current-time)))
  11233. (default-input (and ts (org-get-compact-tod ts)))
  11234. org-time-was-given org-end-time-was-given time)
  11235. (cond
  11236. ((and (org-at-timestamp-p t)
  11237. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11238. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11239. (insert "--")
  11240. (setq time (let ((this-command this-command))
  11241. (org-read-date arg 'totime nil nil
  11242. default-time default-input)))
  11243. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11244. ((org-at-timestamp-p t)
  11245. (setq time (let ((this-command this-command))
  11246. (org-read-date arg 'totime nil nil default-time default-input)))
  11247. (when (org-at-timestamp-p t) ; just to get the match data
  11248. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11249. (replace-match "")
  11250. (setq org-last-changed-timestamp
  11251. (org-insert-time-stamp
  11252. time (or org-time-was-given arg)
  11253. inactive nil nil (list org-end-time-was-given))))
  11254. (message "Timestamp updated"))
  11255. (t
  11256. (setq time (let ((this-command this-command))
  11257. (org-read-date arg 'totime nil nil default-time default-input)))
  11258. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11259. nil nil (list org-end-time-was-given))))))
  11260. ;; FIXME: can we use this for something else, like computing time differences?
  11261. (defun org-get-compact-tod (s)
  11262. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11263. (let* ((t1 (match-string 1 s))
  11264. (h1 (string-to-number (match-string 2 s)))
  11265. (m1 (string-to-number (match-string 3 s)))
  11266. (t2 (and (match-end 4) (match-string 5 s)))
  11267. (h2 (and t2 (string-to-number (match-string 6 s))))
  11268. (m2 (and t2 (string-to-number (match-string 7 s))))
  11269. dh dm)
  11270. (if (not t2)
  11271. t1
  11272. (setq dh (- h2 h1) dm (- m2 m1))
  11273. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11274. (concat t1 "+" (number-to-string dh)
  11275. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11276. (defun org-time-stamp-inactive (&optional arg)
  11277. "Insert an inactive time stamp.
  11278. An inactive time stamp is enclosed in square brackets instead of angle
  11279. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11280. does not link to the calendar and cannot be changed with the S-cursor keys.
  11281. So these are more for recording a certain time/date."
  11282. (interactive "P")
  11283. (org-time-stamp arg 'inactive))
  11284. (defvar org-date-ovl (org-make-overlay 1 1))
  11285. (org-overlay-put org-date-ovl 'face 'org-warning)
  11286. (org-detach-overlay org-date-ovl)
  11287. (defvar org-ans1) ; dynamically scoped parameter
  11288. (defvar org-ans2) ; dynamically scoped parameter
  11289. (defvar org-plain-time-of-day-regexp) ; defined below
  11290. (defvar org-overriding-default-time nil) ; dynamically scoped
  11291. (defvar org-read-date-overlay nil)
  11292. (defvar org-dcst nil) ; dynamically scoped
  11293. (defvar org-read-date-history nil)
  11294. (defun org-read-date (&optional with-time to-time from-string prompt
  11295. default-time default-input)
  11296. "Read a date, possibly a time, and make things smooth for the user.
  11297. The prompt will suggest to enter an ISO date, but you can also enter anything
  11298. which will at least partially be understood by `parse-time-string'.
  11299. Unrecognized parts of the date will default to the current day, month, year,
  11300. hour and minute. If this command is called to replace a timestamp at point,
  11301. of to enter the second timestamp of a range, the default time is taken from the
  11302. existing stamp. For example,
  11303. 3-2-5 --> 2003-02-05
  11304. feb 15 --> currentyear-02-15
  11305. sep 12 9 --> 2009-09-12
  11306. 12:45 --> today 12:45
  11307. 22 sept 0:34 --> currentyear-09-22 0:34
  11308. 12 --> currentyear-currentmonth-12
  11309. Fri --> nearest Friday (today or later)
  11310. etc.
  11311. Furthermore you can specify a relative date by giving, as the *first* thing
  11312. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11313. change in days weeks, months, years.
  11314. With a single plus or minus, the date is relative to today. With a double
  11315. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11316. +4d --> four days from today
  11317. +4 --> same as above
  11318. +2w --> two weeks from today
  11319. ++5 --> five days from default date
  11320. The function understands only English month and weekday abbreviations,
  11321. but this can be configured with the variables `parse-time-months' and
  11322. `parse-time-weekdays'.
  11323. While prompting, a calendar is popped up - you can also select the
  11324. date with the mouse (button 1). The calendar shows a period of three
  11325. months. To scroll it to other months, use the keys `>' and `<'.
  11326. If you don't like the calendar, turn it off with
  11327. \(setq org-read-date-popup-calendar nil)
  11328. With optional argument TO-TIME, the date will immediately be converted
  11329. to an internal time.
  11330. With an optional argument WITH-TIME, the prompt will suggest to also
  11331. insert a time. Note that when WITH-TIME is not set, you can still
  11332. enter a time, and this function will inform the calling routine about
  11333. this change. The calling routine may then choose to change the format
  11334. used to insert the time stamp into the buffer to include the time.
  11335. With optional argument FROM-STRING, read from this string instead from
  11336. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11337. the time/date that is used for everything that is not specified by the
  11338. user."
  11339. (require 'parse-time)
  11340. (let* ((org-time-stamp-rounding-minutes
  11341. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11342. (org-dcst org-display-custom-times)
  11343. (ct (org-current-time))
  11344. (def (or org-overriding-default-time default-time ct))
  11345. (defdecode (decode-time def))
  11346. (dummy (progn
  11347. (when (< (nth 2 defdecode) org-extend-today-until)
  11348. (setcar (nthcdr 2 defdecode) -1)
  11349. (setcar (nthcdr 1 defdecode) 59)
  11350. (setq def (apply 'encode-time defdecode)
  11351. defdecode (decode-time def)))))
  11352. (calendar-move-hook nil)
  11353. (calendar-view-diary-initially-flag nil)
  11354. (view-diary-entries-initially nil)
  11355. (calendar-view-holidays-initially-flag nil)
  11356. (view-calendar-holidays-initially nil)
  11357. (timestr (format-time-string
  11358. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11359. (prompt (concat (if prompt (concat prompt " ") "")
  11360. (format "Date+time [%s]: " timestr)))
  11361. ans (org-ans0 "") org-ans1 org-ans2 final)
  11362. (cond
  11363. (from-string (setq ans from-string))
  11364. (org-read-date-popup-calendar
  11365. (save-excursion
  11366. (save-window-excursion
  11367. (calendar)
  11368. (calendar-forward-day (- (time-to-days def)
  11369. (calendar-absolute-from-gregorian
  11370. (calendar-current-date))))
  11371. (org-eval-in-calendar nil t)
  11372. (let* ((old-map (current-local-map))
  11373. (map (copy-keymap calendar-mode-map))
  11374. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11375. (org-defkey map (kbd "RET") 'org-calendar-select)
  11376. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11377. 'org-calendar-select-mouse)
  11378. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11379. 'org-calendar-select-mouse)
  11380. (org-defkey minibuffer-local-map [(meta shift left)]
  11381. (lambda () (interactive)
  11382. (org-eval-in-calendar '(calendar-backward-month 1))))
  11383. (org-defkey minibuffer-local-map [(meta shift right)]
  11384. (lambda () (interactive)
  11385. (org-eval-in-calendar '(calendar-forward-month 1))))
  11386. (org-defkey minibuffer-local-map [(meta shift up)]
  11387. (lambda () (interactive)
  11388. (org-eval-in-calendar '(calendar-backward-year 1))))
  11389. (org-defkey minibuffer-local-map [(meta shift down)]
  11390. (lambda () (interactive)
  11391. (org-eval-in-calendar '(calendar-forward-year 1))))
  11392. (org-defkey minibuffer-local-map [?\e (shift left)]
  11393. (lambda () (interactive)
  11394. (org-eval-in-calendar '(calendar-backward-month 1))))
  11395. (org-defkey minibuffer-local-map [?\e (shift right)]
  11396. (lambda () (interactive)
  11397. (org-eval-in-calendar '(calendar-forward-month 1))))
  11398. (org-defkey minibuffer-local-map [?\e (shift up)]
  11399. (lambda () (interactive)
  11400. (org-eval-in-calendar '(calendar-backward-year 1))))
  11401. (org-defkey minibuffer-local-map [?\e (shift down)]
  11402. (lambda () (interactive)
  11403. (org-eval-in-calendar '(calendar-forward-year 1))))
  11404. (org-defkey minibuffer-local-map [(shift up)]
  11405. (lambda () (interactive)
  11406. (org-eval-in-calendar '(calendar-backward-week 1))))
  11407. (org-defkey minibuffer-local-map [(shift down)]
  11408. (lambda () (interactive)
  11409. (org-eval-in-calendar '(calendar-forward-week 1))))
  11410. (org-defkey minibuffer-local-map [(shift left)]
  11411. (lambda () (interactive)
  11412. (org-eval-in-calendar '(calendar-backward-day 1))))
  11413. (org-defkey minibuffer-local-map [(shift right)]
  11414. (lambda () (interactive)
  11415. (org-eval-in-calendar '(calendar-forward-day 1))))
  11416. (org-defkey minibuffer-local-map ">"
  11417. (lambda () (interactive)
  11418. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11419. (org-defkey minibuffer-local-map "<"
  11420. (lambda () (interactive)
  11421. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11422. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11423. (unwind-protect
  11424. (progn
  11425. (use-local-map map)
  11426. (add-hook 'post-command-hook 'org-read-date-display)
  11427. (setq org-ans0 (read-string prompt default-input
  11428. 'org-read-date-history nil))
  11429. ;; org-ans0: from prompt
  11430. ;; org-ans1: from mouse click
  11431. ;; org-ans2: from calendar motion
  11432. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11433. (remove-hook 'post-command-hook 'org-read-date-display)
  11434. (use-local-map old-map)
  11435. (when org-read-date-overlay
  11436. (org-delete-overlay org-read-date-overlay)
  11437. (setq org-read-date-overlay nil)))))))
  11438. (t ; Naked prompt only
  11439. (unwind-protect
  11440. (setq ans (read-string prompt default-input
  11441. 'org-read-date-history timestr))
  11442. (when org-read-date-overlay
  11443. (org-delete-overlay org-read-date-overlay)
  11444. (setq org-read-date-overlay nil)))))
  11445. (setq final (org-read-date-analyze ans def defdecode))
  11446. (if to-time
  11447. (apply 'encode-time final)
  11448. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11449. (format "%04d-%02d-%02d %02d:%02d"
  11450. (nth 5 final) (nth 4 final) (nth 3 final)
  11451. (nth 2 final) (nth 1 final))
  11452. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11453. (defvar def)
  11454. (defvar defdecode)
  11455. (defvar with-time)
  11456. (defun org-read-date-display ()
  11457. "Display the current date prompt interpretation in the minibuffer."
  11458. (when org-read-date-display-live
  11459. (when org-read-date-overlay
  11460. (org-delete-overlay org-read-date-overlay))
  11461. (let ((p (point)))
  11462. (end-of-line 1)
  11463. (while (not (equal (buffer-substring
  11464. (max (point-min) (- (point) 4)) (point))
  11465. " "))
  11466. (insert " "))
  11467. (goto-char p))
  11468. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11469. " " (or org-ans1 org-ans2)))
  11470. (org-end-time-was-given nil)
  11471. (f (org-read-date-analyze ans def defdecode))
  11472. (fmts (if org-dcst
  11473. org-time-stamp-custom-formats
  11474. org-time-stamp-formats))
  11475. (fmt (if (or with-time
  11476. (and (boundp 'org-time-was-given) org-time-was-given))
  11477. (cdr fmts)
  11478. (car fmts)))
  11479. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11480. (when (and org-end-time-was-given
  11481. (string-match org-plain-time-of-day-regexp txt))
  11482. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11483. org-end-time-was-given
  11484. (substring txt (match-end 0)))))
  11485. (setq org-read-date-overlay
  11486. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11487. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11488. (defun org-read-date-analyze (ans def defdecode)
  11489. "Analyse the combined answer of the date prompt."
  11490. ;; FIXME: cleanup and comment
  11491. (let (delta deltan deltaw deltadef year month day
  11492. hour minute second wday pm h2 m2 tl wday1
  11493. iso-year iso-weekday iso-week iso-year iso-date)
  11494. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11495. (setq ans "+0"))
  11496. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11497. (setq ans (replace-match "" t t ans)
  11498. deltan (car delta)
  11499. deltaw (nth 1 delta)
  11500. deltadef (nth 2 delta)))
  11501. ;; Check if there is an iso week date in there
  11502. ;; If yes, sore the info and postpone interpreting it until the rest
  11503. ;; of the parsing is done
  11504. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11505. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11506. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11507. iso-week (string-to-number (match-string 2 ans)))
  11508. (setq ans (replace-match "" t t ans)))
  11509. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11510. (when (string-match
  11511. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11512. (setq year (if (match-end 2)
  11513. (string-to-number (match-string 2 ans))
  11514. (string-to-number (format-time-string "%Y")))
  11515. month (string-to-number (match-string 3 ans))
  11516. day (string-to-number (match-string 4 ans)))
  11517. (if (< year 100) (setq year (+ 2000 year)))
  11518. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11519. t nil ans)))
  11520. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11521. ;; If there is a time with am/pm, and *no* time without it, we convert
  11522. ;; so that matching will be successful.
  11523. (loop for i from 1 to 2 do ; twice, for end time as well
  11524. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11525. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11526. (setq hour (string-to-number (match-string 1 ans))
  11527. minute (if (match-end 3)
  11528. (string-to-number (match-string 3 ans))
  11529. 0)
  11530. pm (equal ?p
  11531. (string-to-char (downcase (match-string 4 ans)))))
  11532. (if (and (= hour 12) (not pm))
  11533. (setq hour 0)
  11534. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11535. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11536. t t ans))))
  11537. ;; Check if a time range is given as a duration
  11538. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11539. (setq hour (string-to-number (match-string 1 ans))
  11540. h2 (+ hour (string-to-number (match-string 3 ans)))
  11541. minute (string-to-number (match-string 2 ans))
  11542. m2 (+ minute (if (match-end 5) (string-to-number
  11543. (match-string 5 ans))0)))
  11544. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11545. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11546. t t ans)))
  11547. ;; Check if there is a time range
  11548. (when (boundp 'org-end-time-was-given)
  11549. (setq org-time-was-given nil)
  11550. (when (and (string-match org-plain-time-of-day-regexp ans)
  11551. (match-end 8))
  11552. (setq org-end-time-was-given (match-string 8 ans))
  11553. (setq ans (concat (substring ans 0 (match-beginning 7))
  11554. (substring ans (match-end 7))))))
  11555. (setq tl (parse-time-string ans)
  11556. day (or (nth 3 tl) (nth 3 defdecode))
  11557. month (or (nth 4 tl)
  11558. (if (and org-read-date-prefer-future
  11559. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11560. (1+ (nth 4 defdecode))
  11561. (nth 4 defdecode)))
  11562. year (or (nth 5 tl)
  11563. (if (and org-read-date-prefer-future
  11564. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11565. (1+ (nth 5 defdecode))
  11566. (nth 5 defdecode)))
  11567. hour (or (nth 2 tl) (nth 2 defdecode))
  11568. minute (or (nth 1 tl) (nth 1 defdecode))
  11569. second (or (nth 0 tl) 0)
  11570. wday (nth 6 tl))
  11571. ;; Special date definitions below
  11572. (cond
  11573. (iso-week
  11574. ;; There was an iso week
  11575. (setq year (or iso-year year)
  11576. day (or iso-weekday wday 1)
  11577. wday nil ; to make sure that the trigger below does not match
  11578. iso-date (calendar-gregorian-from-absolute
  11579. (calendar-absolute-from-iso
  11580. (list iso-week day year))))
  11581. ; FIXME: Should we also push ISO weeks into the future?
  11582. ; (when (and org-read-date-prefer-future
  11583. ; (not iso-year)
  11584. ; (< (calendar-absolute-from-gregorian iso-date)
  11585. ; (time-to-days (current-time))))
  11586. ; (setq year (1+ year)
  11587. ; iso-date (calendar-gregorian-from-absolute
  11588. ; (calendar-absolute-from-iso
  11589. ; (list iso-week day year)))))
  11590. (setq month (car iso-date)
  11591. year (nth 2 iso-date)
  11592. day (nth 1 iso-date)))
  11593. (deltan
  11594. (unless deltadef
  11595. (let ((now (decode-time (current-time))))
  11596. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11597. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11598. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11599. ((equal deltaw "m") (setq month (+ month deltan)))
  11600. ((equal deltaw "y") (setq year (+ year deltan)))))
  11601. ((and wday (not (nth 3 tl)))
  11602. ;; Weekday was given, but no day, so pick that day in the week
  11603. ;; on or after the derived date.
  11604. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11605. (unless (equal wday wday1)
  11606. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11607. (if (and (boundp 'org-time-was-given)
  11608. (nth 2 tl))
  11609. (setq org-time-was-given t))
  11610. (if (< year 100) (setq year (+ 2000 year)))
  11611. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11612. (list second minute hour day month year)))
  11613. (defvar parse-time-weekdays)
  11614. (defun org-read-date-get-relative (s today default)
  11615. "Check string S for special relative date string.
  11616. TODAY and DEFAULT are internal times, for today and for a default.
  11617. Return shift list (N what def-flag)
  11618. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11619. N is the number of WHATs to shift.
  11620. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11621. the DEFAULT date rather than TODAY."
  11622. (when (and
  11623. (string-match
  11624. (concat
  11625. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11626. "\\([0-9]+\\)?"
  11627. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11628. "\\([ \t]\\|$\\)") s)
  11629. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11630. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11631. (string-to-char (substring (match-string 1 s) -1))
  11632. ?+))
  11633. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11634. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11635. (what (if (match-end 3) (match-string 3 s) "d"))
  11636. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11637. (date (if rel default today))
  11638. (wday (nth 6 (decode-time date)))
  11639. delta)
  11640. (if wday1
  11641. (progn
  11642. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11643. (if (= dir ?-) (setq delta (- delta 7)))
  11644. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11645. (list delta "d" rel))
  11646. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11647. (defun org-eval-in-calendar (form &optional keepdate)
  11648. "Eval FORM in the calendar window and return to current window.
  11649. Also, store the cursor date in variable org-ans2."
  11650. (let ((sw (selected-window)))
  11651. (select-window (get-buffer-window "*Calendar*"))
  11652. (eval form)
  11653. (when (and (not keepdate) (calendar-cursor-to-date))
  11654. (let* ((date (calendar-cursor-to-date))
  11655. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11656. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11657. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11658. (select-window sw)))
  11659. (defun org-calendar-select ()
  11660. "Return to `org-read-date' with the date currently selected.
  11661. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11662. (interactive)
  11663. (when (calendar-cursor-to-date)
  11664. (let* ((date (calendar-cursor-to-date))
  11665. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11666. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11667. (if (active-minibuffer-window) (exit-minibuffer))))
  11668. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11669. "Insert a date stamp for the date given by the internal TIME.
  11670. WITH-HM means, use the stamp format that includes the time of the day.
  11671. INACTIVE means use square brackets instead of angular ones, so that the
  11672. stamp will not contribute to the agenda.
  11673. PRE and POST are optional strings to be inserted before and after the
  11674. stamp.
  11675. The command returns the inserted time stamp."
  11676. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11677. stamp)
  11678. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11679. (insert-before-markers (or pre ""))
  11680. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11681. (when (listp extra)
  11682. (setq extra (car extra))
  11683. (if (and (stringp extra)
  11684. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11685. (setq extra (format "-%02d:%02d"
  11686. (string-to-number (match-string 1 extra))
  11687. (string-to-number (match-string 2 extra))))
  11688. (setq extra nil)))
  11689. (when extra
  11690. (backward-char 1)
  11691. (insert-before-markers extra)
  11692. (forward-char 1))
  11693. (insert-before-markers (or post ""))
  11694. (setq org-last-inserted-timestamp stamp)))
  11695. (defun org-toggle-time-stamp-overlays ()
  11696. "Toggle the use of custom time stamp formats."
  11697. (interactive)
  11698. (setq org-display-custom-times (not org-display-custom-times))
  11699. (unless org-display-custom-times
  11700. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11701. (while (setq p (next-single-property-change p 'display))
  11702. (if (and (get-text-property p 'display)
  11703. (eq (get-text-property p 'face) 'org-date))
  11704. (remove-text-properties
  11705. p (setq p (next-single-property-change p 'display))
  11706. '(display t))))
  11707. (set-buffer-modified-p bmp)))
  11708. (if (featurep 'xemacs)
  11709. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11710. (org-restart-font-lock)
  11711. (setq org-table-may-need-update t)
  11712. (if org-display-custom-times
  11713. (message "Time stamps are overlayed with custom format")
  11714. (message "Time stamp overlays removed")))
  11715. (defun org-display-custom-time (beg end)
  11716. "Overlay modified time stamp format over timestamp between BEG and END."
  11717. (let* ((ts (buffer-substring beg end))
  11718. t1 w1 with-hm tf time str w2 (off 0))
  11719. (save-match-data
  11720. (setq t1 (org-parse-time-string ts t))
  11721. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11722. (setq off (- (match-end 0) (match-beginning 0)))))
  11723. (setq end (- end off))
  11724. (setq w1 (- end beg)
  11725. with-hm (and (nth 1 t1) (nth 2 t1))
  11726. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11727. time (org-fix-decoded-time t1)
  11728. str (org-add-props
  11729. (format-time-string
  11730. (substring tf 1 -1) (apply 'encode-time time))
  11731. nil 'mouse-face 'highlight)
  11732. w2 (length str))
  11733. (if (not (= w2 w1))
  11734. (add-text-properties (1+ beg) (+ 2 beg)
  11735. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11736. (if (featurep 'xemacs)
  11737. (progn
  11738. (put-text-property beg end 'invisible t)
  11739. (put-text-property beg end 'end-glyph (make-glyph str)))
  11740. (put-text-property beg end 'display str))))
  11741. (defun org-translate-time (string)
  11742. "Translate all timestamps in STRING to custom format.
  11743. But do this only if the variable `org-display-custom-times' is set."
  11744. (when org-display-custom-times
  11745. (save-match-data
  11746. (let* ((start 0)
  11747. (re org-ts-regexp-both)
  11748. t1 with-hm inactive tf time str beg end)
  11749. (while (setq start (string-match re string start))
  11750. (setq beg (match-beginning 0)
  11751. end (match-end 0)
  11752. t1 (save-match-data
  11753. (org-parse-time-string (substring string beg end) t))
  11754. with-hm (and (nth 1 t1) (nth 2 t1))
  11755. inactive (equal (substring string beg (1+ beg)) "[")
  11756. tf (funcall (if with-hm 'cdr 'car)
  11757. org-time-stamp-custom-formats)
  11758. time (org-fix-decoded-time t1)
  11759. str (format-time-string
  11760. (concat
  11761. (if inactive "[" "<") (substring tf 1 -1)
  11762. (if inactive "]" ">"))
  11763. (apply 'encode-time time))
  11764. string (replace-match str t t string)
  11765. start (+ start (length str)))))))
  11766. string)
  11767. (defun org-fix-decoded-time (time)
  11768. "Set 0 instead of nil for the first 6 elements of time.
  11769. Don't touch the rest."
  11770. (let ((n 0))
  11771. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11772. (defun org-days-to-time (timestamp-string)
  11773. "Difference between TIMESTAMP-STRING and now in days."
  11774. (- (time-to-days (org-time-string-to-time timestamp-string))
  11775. (time-to-days (current-time))))
  11776. (defun org-deadline-close (timestamp-string &optional ndays)
  11777. "Is the time in TIMESTAMP-STRING close to the current date?"
  11778. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11779. (and (< (org-days-to-time timestamp-string) ndays)
  11780. (not (org-entry-is-done-p))))
  11781. (defun org-get-wdays (ts)
  11782. "Get the deadline lead time appropriate for timestring TS."
  11783. (cond
  11784. ((<= org-deadline-warning-days 0)
  11785. ;; 0 or negative, enforce this value no matter what
  11786. (- org-deadline-warning-days))
  11787. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11788. ;; lead time is specified.
  11789. (floor (* (string-to-number (match-string 1 ts))
  11790. (cdr (assoc (match-string 2 ts)
  11791. '(("d" . 1) ("w" . 7)
  11792. ("m" . 30.4) ("y" . 365.25)))))))
  11793. ;; go for the default.
  11794. (t org-deadline-warning-days)))
  11795. (defun org-calendar-select-mouse (ev)
  11796. "Return to `org-read-date' with the date currently selected.
  11797. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11798. (interactive "e")
  11799. (mouse-set-point ev)
  11800. (when (calendar-cursor-to-date)
  11801. (let* ((date (calendar-cursor-to-date))
  11802. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11803. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11804. (if (active-minibuffer-window) (exit-minibuffer))))
  11805. (defun org-check-deadlines (ndays)
  11806. "Check if there are any deadlines due or past due.
  11807. A deadline is considered due if it happens within `org-deadline-warning-days'
  11808. days from today's date. If the deadline appears in an entry marked DONE,
  11809. it is not shown. The prefix arg NDAYS can be used to test that many
  11810. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11811. (interactive "P")
  11812. (let* ((org-warn-days
  11813. (cond
  11814. ((equal ndays '(4)) 100000)
  11815. (ndays (prefix-numeric-value ndays))
  11816. (t (abs org-deadline-warning-days))))
  11817. (case-fold-search nil)
  11818. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11819. (callback
  11820. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11821. (message "%d deadlines past-due or due within %d days"
  11822. (org-occur regexp nil callback)
  11823. org-warn-days)))
  11824. (defun org-check-before-date (date)
  11825. "Check if there are deadlines or scheduled entries before DATE."
  11826. (interactive (list (org-read-date)))
  11827. (let ((case-fold-search nil)
  11828. (regexp (concat "\\<\\(" org-deadline-string
  11829. "\\|" org-scheduled-string
  11830. "\\) *<\\([^>]+\\)>"))
  11831. (callback
  11832. (lambda () (time-less-p
  11833. (org-time-string-to-time (match-string 2))
  11834. (org-time-string-to-time date)))))
  11835. (message "%d entries before %s"
  11836. (org-occur regexp nil callback) date)))
  11837. (defun org-check-after-date (date)
  11838. "Check if there are deadlines or scheduled entries after DATE."
  11839. (interactive (list (org-read-date)))
  11840. (let ((case-fold-search nil)
  11841. (regexp (concat "\\<\\(" org-deadline-string
  11842. "\\|" org-scheduled-string
  11843. "\\) *<\\([^>]+\\)>"))
  11844. (callback
  11845. (lambda () (not
  11846. (time-less-p
  11847. (org-time-string-to-time (match-string 2))
  11848. (org-time-string-to-time date))))))
  11849. (message "%d entries after %s"
  11850. (org-occur regexp nil callback) date)))
  11851. (defun org-evaluate-time-range (&optional to-buffer)
  11852. "Evaluate a time range by computing the difference between start and end.
  11853. Normally the result is just printed in the echo area, but with prefix arg
  11854. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11855. If the time range is actually in a table, the result is inserted into the
  11856. next column.
  11857. For time difference computation, a year is assumed to be exactly 365
  11858. days in order to avoid rounding problems."
  11859. (interactive "P")
  11860. (or
  11861. (org-clock-update-time-maybe)
  11862. (save-excursion
  11863. (unless (org-at-date-range-p t)
  11864. (goto-char (point-at-bol))
  11865. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11866. (if (not (org-at-date-range-p t))
  11867. (error "Not at a time-stamp range, and none found in current line")))
  11868. (let* ((ts1 (match-string 1))
  11869. (ts2 (match-string 2))
  11870. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11871. (match-end (match-end 0))
  11872. (time1 (org-time-string-to-time ts1))
  11873. (time2 (org-time-string-to-time ts2))
  11874. (t1 (time-to-seconds time1))
  11875. (t2 (time-to-seconds time2))
  11876. (diff (abs (- t2 t1)))
  11877. (negative (< (- t2 t1) 0))
  11878. ;; (ys (floor (* 365 24 60 60)))
  11879. (ds (* 24 60 60))
  11880. (hs (* 60 60))
  11881. (fy "%dy %dd %02d:%02d")
  11882. (fy1 "%dy %dd")
  11883. (fd "%dd %02d:%02d")
  11884. (fd1 "%dd")
  11885. (fh "%02d:%02d")
  11886. y d h m align)
  11887. (if havetime
  11888. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11889. y 0
  11890. d (floor (/ diff ds)) diff (mod diff ds)
  11891. h (floor (/ diff hs)) diff (mod diff hs)
  11892. m (floor (/ diff 60)))
  11893. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11894. y 0
  11895. d (floor (+ (/ diff ds) 0.5))
  11896. h 0 m 0))
  11897. (if (not to-buffer)
  11898. (message "%s" (org-make-tdiff-string y d h m))
  11899. (if (org-at-table-p)
  11900. (progn
  11901. (goto-char match-end)
  11902. (setq align t)
  11903. (and (looking-at " *|") (goto-char (match-end 0))))
  11904. (goto-char match-end))
  11905. (if (looking-at
  11906. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11907. (replace-match ""))
  11908. (if negative (insert " -"))
  11909. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11910. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11911. (insert " " (format fh h m))))
  11912. (if align (org-table-align))
  11913. (message "Time difference inserted")))))
  11914. (defun org-make-tdiff-string (y d h m)
  11915. (let ((fmt "")
  11916. (l nil))
  11917. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11918. l (push y l)))
  11919. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11920. l (push d l)))
  11921. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11922. l (push h l)))
  11923. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11924. l (push m l)))
  11925. (apply 'format fmt (nreverse l))))
  11926. (defun org-time-string-to-time (s)
  11927. (apply 'encode-time (org-parse-time-string s)))
  11928. (defun org-time-string-to-seconds (s)
  11929. (time-to-seconds (org-time-string-to-time s)))
  11930. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11931. "Convert a time stamp to an absolute day number.
  11932. If there is a specifyer for a cyclic time stamp, get the closest date to
  11933. DAYNR.
  11934. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11935. (cond
  11936. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11937. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11938. daynr
  11939. (+ daynr 1000)))
  11940. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11941. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11942. (time-to-days (current-time))) (match-string 0 s)
  11943. prefer show-all))
  11944. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11945. (defun org-days-to-iso-week (days)
  11946. "Return the iso week number."
  11947. (require 'cal-iso)
  11948. (car (calendar-iso-from-absolute days)))
  11949. (defun org-small-year-to-year (year)
  11950. "Convert 2-digit years into 4-digit years.
  11951. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11952. The year 2000 cannot be abbreviated. Any year larger than 99
  11953. is returned unchanged."
  11954. (if (< year 38)
  11955. (setq year (+ 2000 year))
  11956. (if (< year 100)
  11957. (setq year (+ 1900 year))))
  11958. year)
  11959. (defun org-time-from-absolute (d)
  11960. "Return the time corresponding to date D.
  11961. D may be an absolute day number, or a calendar-type list (month day year)."
  11962. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11963. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11964. (defun org-calendar-holiday ()
  11965. "List of holidays, for Diary display in Org-mode."
  11966. (require 'holidays)
  11967. (let ((hl (funcall
  11968. (if (fboundp 'calendar-check-holidays)
  11969. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11970. (if hl (mapconcat 'identity hl "; "))))
  11971. (defun org-diary-sexp-entry (sexp entry date)
  11972. "Process a SEXP diary ENTRY for DATE."
  11973. (require 'diary-lib)
  11974. (let ((result (if calendar-debug-sexp
  11975. (let ((stack-trace-on-error t))
  11976. (eval (car (read-from-string sexp))))
  11977. (condition-case nil
  11978. (eval (car (read-from-string sexp)))
  11979. (error
  11980. (beep)
  11981. (message "Bad sexp at line %d in %s: %s"
  11982. (org-current-line)
  11983. (buffer-file-name) sexp)
  11984. (sleep-for 2))))))
  11985. (cond ((stringp result) result)
  11986. ((and (consp result)
  11987. (stringp (cdr result))) (cdr result))
  11988. (result entry)
  11989. (t nil))))
  11990. (defun org-diary-to-ical-string (frombuf)
  11991. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11992. This uses the icalendar.el library."
  11993. (let* ((tmpdir (if (featurep 'xemacs)
  11994. (temp-directory)
  11995. temporary-file-directory))
  11996. (tmpfile (make-temp-name
  11997. (expand-file-name "orgics" tmpdir)))
  11998. buf rtn b e)
  11999. (save-excursion
  12000. (set-buffer frombuf)
  12001. (icalendar-export-region (point-min) (point-max) tmpfile)
  12002. (setq buf (find-buffer-visiting tmpfile))
  12003. (set-buffer buf)
  12004. (goto-char (point-min))
  12005. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12006. (setq b (match-beginning 0)))
  12007. (goto-char (point-max))
  12008. (if (re-search-backward "^END:VEVENT" nil t)
  12009. (setq e (match-end 0)))
  12010. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12011. (kill-buffer buf)
  12012. (delete-file tmpfile)
  12013. rtn))
  12014. (defun org-closest-date (start current change prefer show-all)
  12015. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12016. When PREFER is `past' return a date that is either CURRENT or past.
  12017. When PREFER is `future', return a date that is either CURRENT or future.
  12018. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12019. ;; Make the proper lists from the dates
  12020. (catch 'exit
  12021. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12022. dn dw sday cday n1 n2 n0
  12023. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12024. (setq start (org-date-to-gregorian start)
  12025. current (org-date-to-gregorian
  12026. (if show-all
  12027. current
  12028. (time-to-days (current-time))))
  12029. sday (calendar-absolute-from-gregorian start)
  12030. cday (calendar-absolute-from-gregorian current))
  12031. (if (<= cday sday) (throw 'exit sday))
  12032. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12033. (setq dn (string-to-number (match-string 1 change))
  12034. dw (cdr (assoc (match-string 2 change) a1)))
  12035. (error "Invalid change specifyer: %s" change))
  12036. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12037. (cond
  12038. ((eq dw 'day)
  12039. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12040. n2 (+ n1 dn)))
  12041. ((eq dw 'year)
  12042. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12043. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12044. (setq date1 (list m d y1)
  12045. n1 (calendar-absolute-from-gregorian date1)
  12046. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12047. n2 (calendar-absolute-from-gregorian date2)))
  12048. ((eq dw 'month)
  12049. ;; approx number of month between the two dates
  12050. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12051. ;; How often does dn fit in there?
  12052. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12053. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12054. m (+ m nm)
  12055. ny (floor (/ m 12))
  12056. y (+ y ny)
  12057. m (- m (* ny 12)))
  12058. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12059. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12060. (setq m2 (+ m dn) y2 y)
  12061. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12062. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12063. (while (<= n2 cday)
  12064. (setq n1 n2 m m2 y y2)
  12065. (setq m2 (+ m dn) y2 y)
  12066. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12067. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12068. ;; Make sure n1 is the earlier date
  12069. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12070. (if show-all
  12071. (cond
  12072. ((eq prefer 'past) n1)
  12073. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12074. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12075. (cond
  12076. ((eq prefer 'past) n1)
  12077. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12078. (t (if (= cday n1) n1 n2)))))))
  12079. (defun org-date-to-gregorian (date)
  12080. "Turn any specification of DATE into a gregorian date for the calendar."
  12081. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12082. ((and (listp date) (= (length date) 3)) date)
  12083. ((stringp date)
  12084. (setq date (org-parse-time-string date))
  12085. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12086. ((listp date)
  12087. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12088. (defun org-parse-time-string (s &optional nodefault)
  12089. "Parse the standard Org-mode time string.
  12090. This should be a lot faster than the normal `parse-time-string'.
  12091. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12092. hour and minute fields will be nil if not given."
  12093. (if (string-match org-ts-regexp0 s)
  12094. (list 0
  12095. (if (or (match-beginning 8) (not nodefault))
  12096. (string-to-number (or (match-string 8 s) "0")))
  12097. (if (or (match-beginning 7) (not nodefault))
  12098. (string-to-number (or (match-string 7 s) "0")))
  12099. (string-to-number (match-string 4 s))
  12100. (string-to-number (match-string 3 s))
  12101. (string-to-number (match-string 2 s))
  12102. nil nil nil)
  12103. (make-list 9 0)))
  12104. (defun org-timestamp-up (&optional arg)
  12105. "Increase the date item at the cursor by one.
  12106. If the cursor is on the year, change the year. If it is on the month or
  12107. the day, change that.
  12108. With prefix ARG, change by that many units."
  12109. (interactive "p")
  12110. (org-timestamp-change (prefix-numeric-value arg)))
  12111. (defun org-timestamp-down (&optional arg)
  12112. "Decrease the date item at the cursor by one.
  12113. If the cursor is on the year, change the year. If it is on the month or
  12114. the day, change that.
  12115. With prefix ARG, change by that many units."
  12116. (interactive "p")
  12117. (org-timestamp-change (- (prefix-numeric-value arg))))
  12118. (defun org-timestamp-up-day (&optional arg)
  12119. "Increase the date in the time stamp by one day.
  12120. With prefix ARG, change that many days."
  12121. (interactive "p")
  12122. (if (and (not (org-at-timestamp-p t))
  12123. (org-on-heading-p))
  12124. (org-todo 'up)
  12125. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12126. (defun org-timestamp-down-day (&optional arg)
  12127. "Decrease the date in the time stamp by one day.
  12128. With prefix ARG, change that many days."
  12129. (interactive "p")
  12130. (if (and (not (org-at-timestamp-p t))
  12131. (org-on-heading-p))
  12132. (org-todo 'down)
  12133. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12134. (defun org-at-timestamp-p (&optional inactive-ok)
  12135. "Determine if the cursor is in or at a timestamp."
  12136. (interactive)
  12137. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12138. (pos (point))
  12139. (ans (or (looking-at tsr)
  12140. (save-excursion
  12141. (skip-chars-backward "^[<\n\r\t")
  12142. (if (> (point) (point-min)) (backward-char 1))
  12143. (and (looking-at tsr)
  12144. (> (- (match-end 0) pos) -1))))))
  12145. (and ans
  12146. (boundp 'org-ts-what)
  12147. (setq org-ts-what
  12148. (cond
  12149. ((= pos (match-beginning 0)) 'bracket)
  12150. ((= pos (1- (match-end 0))) 'bracket)
  12151. ((org-pos-in-match-range pos 2) 'year)
  12152. ((org-pos-in-match-range pos 3) 'month)
  12153. ((org-pos-in-match-range pos 7) 'hour)
  12154. ((org-pos-in-match-range pos 8) 'minute)
  12155. ((or (org-pos-in-match-range pos 4)
  12156. (org-pos-in-match-range pos 5)) 'day)
  12157. ((and (> pos (or (match-end 8) (match-end 5)))
  12158. (< pos (match-end 0)))
  12159. (- pos (or (match-end 8) (match-end 5))))
  12160. (t 'day))))
  12161. ans))
  12162. (defun org-toggle-timestamp-type ()
  12163. "Toggle the type (<active> or [inactive]) of a time stamp."
  12164. (interactive)
  12165. (when (org-at-timestamp-p t)
  12166. (let ((beg (match-beginning 0)) (end (match-end 0))
  12167. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12168. (save-excursion
  12169. (goto-char beg)
  12170. (while (re-search-forward "[][<>]" end t)
  12171. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12172. t t)))
  12173. (message "Timestamp is now %sactive"
  12174. (if (equal (char-after beg) ?<) "" "in")))))
  12175. (defun org-timestamp-change (n &optional what)
  12176. "Change the date in the time stamp at point.
  12177. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12178. `year', `minute', `second'. If WHAT is not given, the cursor position
  12179. in the timestamp determines what will be changed."
  12180. (let ((pos (point))
  12181. with-hm inactive
  12182. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12183. org-ts-what
  12184. extra rem
  12185. ts time time0)
  12186. (if (not (org-at-timestamp-p t))
  12187. (error "Not at a timestamp"))
  12188. (if (and (not what) (eq org-ts-what 'bracket))
  12189. (org-toggle-timestamp-type)
  12190. (if (and (not what) (not (eq org-ts-what 'day))
  12191. org-display-custom-times
  12192. (get-text-property (point) 'display)
  12193. (not (get-text-property (1- (point)) 'display)))
  12194. (setq org-ts-what 'day))
  12195. (setq org-ts-what (or what org-ts-what)
  12196. inactive (= (char-after (match-beginning 0)) ?\[)
  12197. ts (match-string 0))
  12198. (replace-match "")
  12199. (if (string-match
  12200. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12201. ts)
  12202. (setq extra (match-string 1 ts)))
  12203. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12204. (setq with-hm t))
  12205. (setq time0 (org-parse-time-string ts))
  12206. (when (and (eq org-ts-what 'minute)
  12207. (eq current-prefix-arg nil))
  12208. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12209. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12210. (setcar (cdr time0) (+ (nth 1 time0)
  12211. (if (> n 0) (- rem) (- dm rem))))))
  12212. (setq time
  12213. (encode-time (or (car time0) 0)
  12214. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12215. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12216. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12217. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12218. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12219. (nthcdr 6 time0)))
  12220. (when (and (member org-ts-what '(hour minute))
  12221. extra
  12222. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12223. (setq extra (org-modify-ts-extra
  12224. extra
  12225. (if (eq org-ts-what 'hour) 2 5)
  12226. n dm)))
  12227. (when (integerp org-ts-what)
  12228. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12229. (if (eq what 'calendar)
  12230. (let ((cal-date (org-get-date-from-calendar)))
  12231. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12232. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12233. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12234. (setcar time0 (or (car time0) 0))
  12235. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12236. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12237. (setq time (apply 'encode-time time0))))
  12238. (setq org-last-changed-timestamp
  12239. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12240. (org-clock-update-time-maybe)
  12241. (goto-char pos)
  12242. ;; Try to recenter the calendar window, if any
  12243. (if (and org-calendar-follow-timestamp-change
  12244. (get-buffer-window "*Calendar*" t)
  12245. (memq org-ts-what '(day month year)))
  12246. (org-recenter-calendar (time-to-days time))))))
  12247. (defun org-modify-ts-extra (s pos n dm)
  12248. "Change the different parts of the lead-time and repeat fields in timestamp."
  12249. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12250. ng h m new rem)
  12251. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12252. (cond
  12253. ((or (org-pos-in-match-range pos 2)
  12254. (org-pos-in-match-range pos 3))
  12255. (setq m (string-to-number (match-string 3 s))
  12256. h (string-to-number (match-string 2 s)))
  12257. (if (org-pos-in-match-range pos 2)
  12258. (setq h (+ h n))
  12259. (setq n (* dm (org-no-warnings (signum n))))
  12260. (when (not (= 0 (setq rem (% m dm))))
  12261. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12262. (setq m (+ m n)))
  12263. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12264. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12265. (setq h (min 24 (max 0 h)))
  12266. (setq ng 1 new (format "-%02d:%02d" h m)))
  12267. ((org-pos-in-match-range pos 6)
  12268. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12269. ((org-pos-in-match-range pos 5)
  12270. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12271. ((org-pos-in-match-range pos 9)
  12272. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12273. ((org-pos-in-match-range pos 8)
  12274. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12275. (when ng
  12276. (setq s (concat
  12277. (substring s 0 (match-beginning ng))
  12278. new
  12279. (substring s (match-end ng))))))
  12280. s))
  12281. (defun org-recenter-calendar (date)
  12282. "If the calendar is visible, recenter it to DATE."
  12283. (let* ((win (selected-window))
  12284. (cwin (get-buffer-window "*Calendar*" t))
  12285. (calendar-move-hook nil))
  12286. (when cwin
  12287. (select-window cwin)
  12288. (calendar-goto-date (if (listp date) date
  12289. (calendar-gregorian-from-absolute date)))
  12290. (select-window win))))
  12291. (defun org-goto-calendar (&optional arg)
  12292. "Go to the Emacs calendar at the current date.
  12293. If there is a time stamp in the current line, go to that date.
  12294. A prefix ARG can be used to force the current date."
  12295. (interactive "P")
  12296. (let ((tsr org-ts-regexp) diff
  12297. (calendar-move-hook nil)
  12298. (calendar-view-holidays-initially-flag nil)
  12299. (view-calendar-holidays-initially nil)
  12300. (calendar-view-diary-initially-flag nil)
  12301. (view-diary-entries-initially nil))
  12302. (if (or (org-at-timestamp-p)
  12303. (save-excursion
  12304. (beginning-of-line 1)
  12305. (looking-at (concat ".*" tsr))))
  12306. (let ((d1 (time-to-days (current-time)))
  12307. (d2 (time-to-days
  12308. (org-time-string-to-time (match-string 1)))))
  12309. (setq diff (- d2 d1))))
  12310. (calendar)
  12311. (calendar-goto-today)
  12312. (if (and diff (not arg)) (calendar-forward-day diff))))
  12313. (defun org-get-date-from-calendar ()
  12314. "Return a list (month day year) of date at point in calendar."
  12315. (with-current-buffer "*Calendar*"
  12316. (save-match-data
  12317. (calendar-cursor-to-date))))
  12318. (defun org-date-from-calendar ()
  12319. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12320. If there is already a time stamp at the cursor position, update it."
  12321. (interactive)
  12322. (if (org-at-timestamp-p t)
  12323. (org-timestamp-change 0 'calendar)
  12324. (let ((cal-date (org-get-date-from-calendar)))
  12325. (org-insert-time-stamp
  12326. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12327. (defun org-minutes-to-hh:mm-string (m)
  12328. "Compute H:MM from a number of minutes."
  12329. (let ((h (/ m 60)))
  12330. (setq m (- m (* 60 h)))
  12331. (format org-time-clocksum-format h m)))
  12332. (defun org-hh:mm-string-to-minutes (s)
  12333. "Convert a string H:MM to a number of minutes."
  12334. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12335. (+ (* (string-to-number (match-string 1 s)) 60)
  12336. (string-to-number (match-string 2 s)))
  12337. 0))
  12338. ;;;; Files
  12339. (defun org-save-all-org-buffers ()
  12340. "Save all Org-mode buffers without user confirmation."
  12341. (interactive)
  12342. (message "Saving all Org-mode buffers...")
  12343. (save-some-buffers t 'org-mode-p)
  12344. (when (featurep 'org-id) (org-id-locations-save))
  12345. (message "Saving all Org-mode buffers... done"))
  12346. (defun org-revert-all-org-buffers ()
  12347. "Revert all Org-mode buffers.
  12348. Prompt for confirmation when there are unsaved changes.
  12349. Be sure you know what you are doing before letting this function
  12350. overwrite your changes.
  12351. This function is useful in a setup where one tracks org files
  12352. with a version control system, to revert on one machine after pulling
  12353. changes from another. I believe the procedure must be like this:
  12354. 1. M-x org-save-all-org-buffers
  12355. 2. Pull changes from the other machine, resolve conflicts
  12356. 3. M-x org-revert-all-org-buffers"
  12357. (interactive)
  12358. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12359. (error "Abort"))
  12360. (save-excursion
  12361. (save-window-excursion
  12362. (mapc
  12363. (lambda (b)
  12364. (when (and (with-current-buffer b (org-mode-p))
  12365. (with-current-buffer b buffer-file-name))
  12366. (switch-to-buffer b)
  12367. (revert-buffer t 'no-confirm)))
  12368. (buffer-list))
  12369. (when (and (featurep 'org-id) org-id-track-globally)
  12370. (org-id-locations-load)))))
  12371. ;;;; Agenda files
  12372. ;;;###autoload
  12373. (defun org-iswitchb (&optional arg)
  12374. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12375. With a prefix argument, restrict available to files.
  12376. With two prefix arguments, restrict available buffers to agenda files.
  12377. Due to some yet unresolved reason, the global function
  12378. `iswitchb-mode' needs to be active for this function to work."
  12379. (interactive "P")
  12380. (require 'iswitchb)
  12381. (let ((enabled iswitchb-mode) blist)
  12382. (or enabled (iswitchb-mode 1))
  12383. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12384. ((equal arg '(16)) (org-buffer-list 'agenda))
  12385. (t (org-buffer-list))))
  12386. (unwind-protect
  12387. (let ((iswitchb-make-buflist-hook
  12388. (lambda ()
  12389. (setq iswitchb-temp-buflist
  12390. (mapcar 'buffer-name blist)))))
  12391. (switch-to-buffer
  12392. (iswitchb-read-buffer
  12393. "Switch-to: " nil t))
  12394. (or enabled (iswitchb-mode -1))))))
  12395. ;;;###autoload
  12396. (defun org-ido-switchb (&optional arg)
  12397. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12398. With a prefix argument, restrict available to files.
  12399. With two prefix arguments, restrict available buffers to agenda files."
  12400. (interactive "P")
  12401. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12402. ((equal arg '(16)) (org-buffer-list 'agenda))
  12403. (t (org-buffer-list)))))
  12404. (switch-to-buffer
  12405. (org-ido-completing-read "Org buffer: "
  12406. (mapcar 'list (mapcar 'buffer-name blist))
  12407. nil t))))
  12408. (defun org-buffer-list (&optional predicate exclude-tmp)
  12409. "Return a list of Org buffers.
  12410. PREDICATE can be `export', `files' or `agenda'.
  12411. export restrict the list to Export buffers.
  12412. files restrict the list to buffers visiting Org files.
  12413. agenda restrict the list to buffers visiting agenda files.
  12414. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12415. (let* ((bfn nil)
  12416. (agenda-files (and (eq predicate 'agenda)
  12417. (mapcar 'file-truename (org-agenda-files t))))
  12418. (filter
  12419. (cond
  12420. ((eq predicate 'files)
  12421. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12422. ((eq predicate 'export)
  12423. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12424. ((eq predicate 'agenda)
  12425. (lambda (b)
  12426. (with-current-buffer b
  12427. (and (eq major-mode 'org-mode)
  12428. (setq bfn (buffer-file-name b))
  12429. (member (file-truename bfn) agenda-files)))))
  12430. (t (lambda (b) (with-current-buffer b
  12431. (or (eq major-mode 'org-mode)
  12432. (string-match "\*Org .*Export"
  12433. (buffer-name b)))))))))
  12434. (delq nil
  12435. (mapcar
  12436. (lambda(b)
  12437. (if (and (funcall filter b)
  12438. (or (not exclude-tmp)
  12439. (not (string-match "tmp" (buffer-name b)))))
  12440. b
  12441. nil))
  12442. (buffer-list)))))
  12443. (defun org-agenda-files (&optional unrestricted archives)
  12444. "Get the list of agenda files.
  12445. Optional UNRESTRICTED means return the full list even if a restriction
  12446. is currently in place.
  12447. When ARCHIVES is t, include all archive files hat are really being
  12448. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12449. `org-agenda-archives-mode' is t."
  12450. (let ((files
  12451. (cond
  12452. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12453. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12454. ((listp org-agenda-files) org-agenda-files)
  12455. (t (error "Invalid value of `org-agenda-files'")))))
  12456. (setq files (apply 'append
  12457. (mapcar (lambda (f)
  12458. (if (file-directory-p f)
  12459. (directory-files
  12460. f t org-agenda-file-regexp)
  12461. (list f)))
  12462. files)))
  12463. (when org-agenda-skip-unavailable-files
  12464. (setq files (delq nil
  12465. (mapcar (function
  12466. (lambda (file)
  12467. (and (file-readable-p file) file)))
  12468. files))))
  12469. (when (or (eq archives t)
  12470. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12471. (setq files (org-add-archive-files files)))
  12472. files))
  12473. (defun org-edit-agenda-file-list ()
  12474. "Edit the list of agenda files.
  12475. Depending on setup, this either uses customize to edit the variable
  12476. `org-agenda-files', or it visits the file that is holding the list. In the
  12477. latter case, the buffer is set up in a way that saving it automatically kills
  12478. the buffer and restores the previous window configuration."
  12479. (interactive)
  12480. (if (stringp org-agenda-files)
  12481. (let ((cw (current-window-configuration)))
  12482. (find-file org-agenda-files)
  12483. (org-set-local 'org-window-configuration cw)
  12484. (org-add-hook 'after-save-hook
  12485. (lambda ()
  12486. (set-window-configuration
  12487. (prog1 org-window-configuration
  12488. (kill-buffer (current-buffer))))
  12489. (org-install-agenda-files-menu)
  12490. (message "New agenda file list installed"))
  12491. nil 'local)
  12492. (message "%s" (substitute-command-keys
  12493. "Edit list and finish with \\[save-buffer]")))
  12494. (customize-variable 'org-agenda-files)))
  12495. (defun org-store-new-agenda-file-list (list)
  12496. "Set new value for the agenda file list and save it correctly."
  12497. (if (stringp org-agenda-files)
  12498. (let ((f org-agenda-files) b)
  12499. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12500. (with-temp-file f
  12501. (insert (mapconcat 'identity list "\n") "\n")))
  12502. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12503. (setq org-agenda-files list)
  12504. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12505. (defun org-read-agenda-file-list ()
  12506. "Read the list of agenda files from a file."
  12507. (when (file-directory-p org-agenda-files)
  12508. (error "`org-agenda-files' cannot be a single directory"))
  12509. (when (stringp org-agenda-files)
  12510. (with-temp-buffer
  12511. (insert-file-contents org-agenda-files)
  12512. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12513. ;;;###autoload
  12514. (defun org-cycle-agenda-files ()
  12515. "Cycle through the files in `org-agenda-files'.
  12516. If the current buffer visits an agenda file, find the next one in the list.
  12517. If the current buffer does not, find the first agenda file."
  12518. (interactive)
  12519. (let* ((fs (org-agenda-files t))
  12520. (files (append fs (list (car fs))))
  12521. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12522. file)
  12523. (unless files (error "No agenda files"))
  12524. (catch 'exit
  12525. (while (setq file (pop files))
  12526. (if (equal (file-truename file) tcf)
  12527. (when (car files)
  12528. (find-file (car files))
  12529. (throw 'exit t))))
  12530. (find-file (car fs)))
  12531. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12532. (defun org-agenda-file-to-front (&optional to-end)
  12533. "Move/add the current file to the top of the agenda file list.
  12534. If the file is not present in the list, it is added to the front. If it is
  12535. present, it is moved there. With optional argument TO-END, add/move to the
  12536. end of the list."
  12537. (interactive "P")
  12538. (let ((org-agenda-skip-unavailable-files nil)
  12539. (file-alist (mapcar (lambda (x)
  12540. (cons (file-truename x) x))
  12541. (org-agenda-files t)))
  12542. (ctf (file-truename buffer-file-name))
  12543. x had)
  12544. (setq x (assoc ctf file-alist) had x)
  12545. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12546. (if to-end
  12547. (setq file-alist (append (delq x file-alist) (list x)))
  12548. (setq file-alist (cons x (delq x file-alist))))
  12549. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12550. (org-install-agenda-files-menu)
  12551. (message "File %s to %s of agenda file list"
  12552. (if had "moved" "added") (if to-end "end" "front"))))
  12553. (defun org-remove-file (&optional file)
  12554. "Remove current file from the list of files in variable `org-agenda-files'.
  12555. These are the files which are being checked for agenda entries.
  12556. Optional argument FILE means, use this file instead of the current."
  12557. (interactive)
  12558. (let* ((org-agenda-skip-unavailable-files nil)
  12559. (file (or file buffer-file-name))
  12560. (true-file (file-truename file))
  12561. (afile (abbreviate-file-name file))
  12562. (files (delq nil (mapcar
  12563. (lambda (x)
  12564. (if (equal true-file
  12565. (file-truename x))
  12566. nil x))
  12567. (org-agenda-files t)))))
  12568. (if (not (= (length files) (length (org-agenda-files t))))
  12569. (progn
  12570. (org-store-new-agenda-file-list files)
  12571. (org-install-agenda-files-menu)
  12572. (message "Removed file: %s" afile))
  12573. (message "File was not in list: %s (not removed)" afile))))
  12574. (defun org-file-menu-entry (file)
  12575. (vector file (list 'find-file file) t))
  12576. (defun org-check-agenda-file (file)
  12577. "Make sure FILE exists. If not, ask user what to do."
  12578. (when (not (file-exists-p file))
  12579. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12580. (abbreviate-file-name file))
  12581. (let ((r (downcase (read-char-exclusive))))
  12582. (cond
  12583. ((equal r ?r)
  12584. (org-remove-file file)
  12585. (throw 'nextfile t))
  12586. (t (error "Abort"))))))
  12587. (defun org-get-agenda-file-buffer (file)
  12588. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12589. it to the list of buffers which might be released later."
  12590. (let ((buf (org-find-base-buffer-visiting file)))
  12591. (if buf
  12592. buf ; just return it
  12593. ;; Make a new buffer and remember it
  12594. (setq buf (find-file-noselect file))
  12595. (if buf (push buf org-agenda-new-buffers))
  12596. buf)))
  12597. (defun org-release-buffers (blist)
  12598. "Release all buffers in list, asking the user for confirmation when needed.
  12599. When a buffer is unmodified, it is just killed. When modified, it is saved
  12600. \(if the user agrees) and then killed."
  12601. (let (buf file)
  12602. (while (setq buf (pop blist))
  12603. (setq file (buffer-file-name buf))
  12604. (when (and (buffer-modified-p buf)
  12605. file
  12606. (y-or-n-p (format "Save file %s? " file)))
  12607. (with-current-buffer buf (save-buffer)))
  12608. (kill-buffer buf))))
  12609. (defun org-prepare-agenda-buffers (files)
  12610. "Create buffers for all agenda files, protect archived trees and comments."
  12611. (interactive)
  12612. (let ((pa '(:org-archived t))
  12613. (pc '(:org-comment t))
  12614. (pall '(:org-archived t :org-comment t))
  12615. (inhibit-read-only t)
  12616. (rea (concat ":" org-archive-tag ":"))
  12617. bmp file re)
  12618. (save-excursion
  12619. (save-restriction
  12620. (while (setq file (pop files))
  12621. (catch 'nextfile
  12622. (if (bufferp file)
  12623. (set-buffer file)
  12624. (org-check-agenda-file file)
  12625. (set-buffer (org-get-agenda-file-buffer file)))
  12626. (widen)
  12627. (setq bmp (buffer-modified-p))
  12628. (org-refresh-category-properties)
  12629. (setq org-todo-keywords-for-agenda
  12630. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12631. (setq org-done-keywords-for-agenda
  12632. (append org-done-keywords-for-agenda org-done-keywords))
  12633. (setq org-todo-keyword-alist-for-agenda
  12634. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12635. (setq org-tag-alist-for-agenda
  12636. (append org-tag-alist-for-agenda org-tag-alist))
  12637. (save-excursion
  12638. (remove-text-properties (point-min) (point-max) pall)
  12639. (when org-agenda-skip-archived-trees
  12640. (goto-char (point-min))
  12641. (while (re-search-forward rea nil t)
  12642. (if (org-on-heading-p t)
  12643. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12644. (goto-char (point-min))
  12645. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12646. (while (re-search-forward re nil t)
  12647. (add-text-properties
  12648. (match-beginning 0) (org-end-of-subtree t) pc)))
  12649. (set-buffer-modified-p bmp)))))
  12650. (setq org-todo-keyword-alist-for-agenda
  12651. (org-uniquify org-todo-keyword-alist-for-agenda)
  12652. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12653. ;;;; Embedded LaTeX
  12654. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12655. "Keymap for the minor `org-cdlatex-mode'.")
  12656. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12657. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12658. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12659. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12660. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12661. (defvar org-cdlatex-texmathp-advice-is-done nil
  12662. "Flag remembering if we have applied the advice to texmathp already.")
  12663. (define-minor-mode org-cdlatex-mode
  12664. "Toggle the minor `org-cdlatex-mode'.
  12665. This mode supports entering LaTeX environment and math in LaTeX fragments
  12666. in Org-mode.
  12667. \\{org-cdlatex-mode-map}"
  12668. nil " OCDL" nil
  12669. (when org-cdlatex-mode (require 'cdlatex))
  12670. (unless org-cdlatex-texmathp-advice-is-done
  12671. (setq org-cdlatex-texmathp-advice-is-done t)
  12672. (defadvice texmathp (around org-math-always-on activate)
  12673. "Always return t in org-mode buffers.
  12674. This is because we want to insert math symbols without dollars even outside
  12675. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12676. an embedded LaTeX fragment, let texmathp do its job.
  12677. \\[org-cdlatex-mode-map]"
  12678. (interactive)
  12679. (let (p)
  12680. (cond
  12681. ((not (org-mode-p)) ad-do-it)
  12682. ((eq this-command 'cdlatex-math-symbol)
  12683. (setq ad-return-value t
  12684. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12685. (t
  12686. (let ((p (org-inside-LaTeX-fragment-p)))
  12687. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12688. (setq ad-return-value t
  12689. texmathp-why '("Org-mode embedded math" . 0))
  12690. (if p ad-do-it)))))))))
  12691. (defun turn-on-org-cdlatex ()
  12692. "Unconditionally turn on `org-cdlatex-mode'."
  12693. (org-cdlatex-mode 1))
  12694. (defun org-inside-LaTeX-fragment-p ()
  12695. "Test if point is inside a LaTeX fragment.
  12696. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12697. sequence appearing also before point.
  12698. Even though the matchers for math are configurable, this function assumes
  12699. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12700. delimiters are skipped when they have been removed by customization.
  12701. The return value is nil, or a cons cell with the delimiter and
  12702. and the position of this delimiter.
  12703. This function does a reasonably good job, but can locally be fooled by
  12704. for example currency specifications. For example it will assume being in
  12705. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12706. fragments that are properly closed, but during editing, we have to live
  12707. with the uncertainty caused by missing closing delimiters. This function
  12708. looks only before point, not after."
  12709. (catch 'exit
  12710. (let ((pos (point))
  12711. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12712. (lim (progn
  12713. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12714. (point)))
  12715. dd-on str (start 0) m re)
  12716. (goto-char pos)
  12717. (when dodollar
  12718. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12719. re (nth 1 (assoc "$" org-latex-regexps)))
  12720. (while (string-match re str start)
  12721. (cond
  12722. ((= (match-end 0) (length str))
  12723. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12724. ((= (match-end 0) (- (length str) 5))
  12725. (throw 'exit nil))
  12726. (t (setq start (match-end 0))))))
  12727. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12728. (goto-char pos)
  12729. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12730. (and (match-beginning 2) (throw 'exit nil))
  12731. ;; count $$
  12732. (while (re-search-backward "\\$\\$" lim t)
  12733. (setq dd-on (not dd-on)))
  12734. (goto-char pos)
  12735. (if dd-on (cons "$$" m))))))
  12736. (defun org-try-cdlatex-tab ()
  12737. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12738. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12739. - inside a LaTeX fragment, or
  12740. - after the first word in a line, where an abbreviation expansion could
  12741. insert a LaTeX environment."
  12742. (when org-cdlatex-mode
  12743. (cond
  12744. ((save-excursion
  12745. (skip-chars-backward "a-zA-Z0-9*")
  12746. (skip-chars-backward " \t")
  12747. (bolp))
  12748. (cdlatex-tab) t)
  12749. ((org-inside-LaTeX-fragment-p)
  12750. (cdlatex-tab) t)
  12751. (t nil))))
  12752. (defun org-cdlatex-underscore-caret (&optional arg)
  12753. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12754. Revert to the normal definition outside of these fragments."
  12755. (interactive "P")
  12756. (if (org-inside-LaTeX-fragment-p)
  12757. (call-interactively 'cdlatex-sub-superscript)
  12758. (let (org-cdlatex-mode)
  12759. (call-interactively (key-binding (vector last-input-event))))))
  12760. (defun org-cdlatex-math-modify (&optional arg)
  12761. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12762. Revert to the normal definition outside of these fragments."
  12763. (interactive "P")
  12764. (if (org-inside-LaTeX-fragment-p)
  12765. (call-interactively 'cdlatex-math-modify)
  12766. (let (org-cdlatex-mode)
  12767. (call-interactively (key-binding (vector last-input-event))))))
  12768. (defvar org-latex-fragment-image-overlays nil
  12769. "List of overlays carrying the images of latex fragments.")
  12770. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12771. (defun org-remove-latex-fragment-image-overlays ()
  12772. "Remove all overlays with LaTeX fragment images in current buffer."
  12773. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12774. (setq org-latex-fragment-image-overlays nil))
  12775. (defun org-preview-latex-fragment (&optional subtree)
  12776. "Preview the LaTeX fragment at point, or all locally or globally.
  12777. If the cursor is in a LaTeX fragment, create the image and overlay
  12778. it over the source code. If there is no fragment at point, display
  12779. all fragments in the current text, from one headline to the next. With
  12780. prefix SUBTREE, display all fragments in the current subtree. With a
  12781. double prefix `C-u C-u', or when the cursor is before the first headline,
  12782. display all fragments in the buffer.
  12783. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12784. (interactive "P")
  12785. (org-remove-latex-fragment-image-overlays)
  12786. (save-excursion
  12787. (save-restriction
  12788. (let (beg end at msg)
  12789. (cond
  12790. ((or (equal subtree '(16))
  12791. (not (save-excursion
  12792. (re-search-backward (concat "^" outline-regexp) nil t))))
  12793. (setq beg (point-min) end (point-max)
  12794. msg "Creating images for buffer...%s"))
  12795. ((equal subtree '(4))
  12796. (org-back-to-heading)
  12797. (setq beg (point) end (org-end-of-subtree t)
  12798. msg "Creating images for subtree...%s"))
  12799. (t
  12800. (if (setq at (org-inside-LaTeX-fragment-p))
  12801. (goto-char (max (point-min) (- (cdr at) 2)))
  12802. (org-back-to-heading))
  12803. (setq beg (point) end (progn (outline-next-heading) (point))
  12804. msg (if at "Creating image...%s"
  12805. "Creating images for entry...%s"))))
  12806. (message msg "")
  12807. (narrow-to-region beg end)
  12808. (goto-char beg)
  12809. (org-format-latex
  12810. (concat "ltxpng/" (file-name-sans-extension
  12811. (file-name-nondirectory
  12812. buffer-file-name)))
  12813. default-directory 'overlays msg at 'forbuffer)
  12814. (message msg "done. Use `C-c C-c' to remove images.")))))
  12815. (defvar org-latex-regexps
  12816. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12817. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12818. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12819. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12820. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12821. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12822. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12823. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12824. "Regular expressions for matching embedded LaTeX.")
  12825. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12826. "Replace LaTeX fragments with links to an image, and produce images."
  12827. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12828. (let* ((prefixnodir (file-name-nondirectory prefix))
  12829. (absprefix (expand-file-name prefix dir))
  12830. (todir (file-name-directory absprefix))
  12831. (opt org-format-latex-options)
  12832. (matchers (plist-get opt :matchers))
  12833. (re-list org-latex-regexps)
  12834. (cnt 0) txt link beg end re e checkdir
  12835. executables-checked
  12836. m n block linkfile movefile ov)
  12837. ;; Check if there are old images files with this prefix, and remove them
  12838. (when (file-directory-p todir)
  12839. (mapc 'delete-file
  12840. (directory-files
  12841. todir 'full
  12842. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12843. ;; Check the different regular expressions
  12844. (while (setq e (pop re-list))
  12845. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12846. block (if (nth 3 e) "\n\n" ""))
  12847. (when (member m matchers)
  12848. (goto-char (point-min))
  12849. (while (re-search-forward re nil t)
  12850. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12851. (not (get-text-property (match-beginning n)
  12852. 'org-protected)))
  12853. (setq txt (match-string n)
  12854. beg (match-beginning n) end (match-end n)
  12855. cnt (1+ cnt)
  12856. linkfile (format "%s_%04d.png" prefix cnt)
  12857. movefile (format "%s_%04d.png" absprefix cnt)
  12858. link (concat block "[[file:" linkfile "]]" block))
  12859. (if msg (message msg cnt))
  12860. (goto-char beg)
  12861. (unless checkdir ; make sure the directory exists
  12862. (setq checkdir t)
  12863. (or (file-directory-p todir) (make-directory todir)))
  12864. (unless executables-checked
  12865. (org-check-external-command
  12866. "latex" "needed to convert LaTeX fragments to images")
  12867. (org-check-external-command
  12868. "dvipng" "needed to convert LaTeX fragments to images")
  12869. (setq executables-checked t))
  12870. (org-create-formula-image
  12871. txt movefile opt forbuffer)
  12872. (if overlays
  12873. (progn
  12874. (setq ov (org-make-overlay beg end))
  12875. (if (featurep 'xemacs)
  12876. (progn
  12877. (org-overlay-put ov 'invisible t)
  12878. (org-overlay-put
  12879. ov 'end-glyph
  12880. (make-glyph (vector 'png :file movefile))))
  12881. (org-overlay-put
  12882. ov 'display
  12883. (list 'image :type 'png :file movefile :ascent 'center)))
  12884. (push ov org-latex-fragment-image-overlays)
  12885. (goto-char end))
  12886. (delete-region beg end)
  12887. (insert link))))))))
  12888. ;; This function borrows from Ganesh Swami's latex2png.el
  12889. (defun org-create-formula-image (string tofile options buffer)
  12890. (let* ((tmpdir (if (featurep 'xemacs)
  12891. (temp-directory)
  12892. temporary-file-directory))
  12893. (texfilebase (make-temp-name
  12894. (expand-file-name "orgtex" tmpdir)))
  12895. (texfile (concat texfilebase ".tex"))
  12896. (dvifile (concat texfilebase ".dvi"))
  12897. (pngfile (concat texfilebase ".png"))
  12898. (fnh (if (featurep 'xemacs)
  12899. (font-height (get-face-font 'default))
  12900. (face-attribute 'default :height nil)))
  12901. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12902. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12903. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12904. "Black"))
  12905. (bg (or (plist-get options (if buffer :background :html-background))
  12906. "Transparent")))
  12907. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12908. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12909. (with-temp-file texfile
  12910. (insert org-format-latex-header
  12911. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12912. (let ((dir default-directory))
  12913. (condition-case nil
  12914. (progn
  12915. (cd tmpdir)
  12916. (call-process "latex" nil nil nil texfile))
  12917. (error nil))
  12918. (cd dir))
  12919. (if (not (file-exists-p dvifile))
  12920. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12921. (condition-case nil
  12922. (call-process "dvipng" nil nil nil
  12923. "-E" "-fg" fg "-bg" bg
  12924. "-D" dpi
  12925. ;;"-x" scale "-y" scale
  12926. "-T" "tight"
  12927. "-o" pngfile
  12928. dvifile)
  12929. (error nil))
  12930. (if (not (file-exists-p pngfile))
  12931. (progn (message "Failed to create png file from %s" texfile) nil)
  12932. ;; Use the requested file name and clean up
  12933. (copy-file pngfile tofile 'replace)
  12934. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12935. (delete-file (concat texfilebase e)))
  12936. pngfile))))
  12937. (defun org-dvipng-color (attr)
  12938. "Return an rgb color specification for dvipng."
  12939. (apply 'format "rgb %s %s %s"
  12940. (mapcar 'org-normalize-color
  12941. (color-values (face-attribute 'default attr nil)))))
  12942. (defun org-normalize-color (value)
  12943. "Return string to be used as color value for an RGB component."
  12944. (format "%g" (/ value 65535.0)))
  12945. ;;;; Key bindings
  12946. ;; Make `C-c C-x' a prefix key
  12947. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12948. ;; TAB key with modifiers
  12949. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12950. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12951. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12952. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12953. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12954. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12955. ;; The following line is necessary under Suse GNU/Linux
  12956. (unless (featurep 'xemacs)
  12957. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12958. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12959. (define-key org-mode-map [backtab] 'org-shifttab)
  12960. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12961. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12962. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12963. ;; Cursor keys with modifiers
  12964. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12965. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12966. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12967. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12968. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12969. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12970. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12971. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12972. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12973. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12974. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12975. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12976. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12977. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12978. ;;; Extra keys for tty access.
  12979. ;; We only set them when really needed because otherwise the
  12980. ;; menus don't show the simple keys
  12981. (when (or org-use-extra-keys
  12982. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12983. (not window-system))
  12984. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12985. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12986. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12987. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12988. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12989. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12990. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12991. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12992. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12993. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12994. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12995. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12996. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12997. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12998. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12999. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13000. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13001. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13002. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13003. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13004. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13005. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13006. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13007. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13008. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13009. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13010. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13011. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13012. ;; All the other keys
  13013. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13014. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13015. (if (boundp 'narrow-map)
  13016. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13017. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13018. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13019. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13020. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13021. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13022. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13023. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13024. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13025. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13026. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13027. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13028. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13029. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13030. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13031. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13032. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13033. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13034. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13035. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13036. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13037. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13038. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13039. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13040. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13041. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13042. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13043. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13044. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13045. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13046. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13047. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13048. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13049. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13050. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13051. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13052. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13053. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13054. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13055. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13056. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13057. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13058. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13059. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13060. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13061. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13062. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13063. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13064. (org-defkey org-mode-map "\C-m" 'org-return)
  13065. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13066. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13067. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13068. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13069. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13070. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13071. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13072. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13073. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13074. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13075. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13076. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13077. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13078. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13079. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13080. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13081. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13082. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13083. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13084. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13085. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13086. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13087. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13088. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13089. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13090. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13091. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13092. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13093. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13094. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13095. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13096. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13097. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13098. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13099. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13100. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13101. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13102. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13103. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13104. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13105. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13106. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13107. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13108. (when (featurep 'xemacs)
  13109. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13110. (defvar org-self-insert-command-undo-counter 0)
  13111. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13112. (defun org-self-insert-command (N)
  13113. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13114. If the cursor is in a table looking at whitespace, the whitespace is
  13115. overwritten, and the table is not marked as requiring realignment."
  13116. (interactive "p")
  13117. (if (and
  13118. (org-table-p)
  13119. (progn
  13120. ;; check if we blank the field, and if that triggers align
  13121. (and (featurep 'org-table) org-table-auto-blank-field
  13122. (member last-command
  13123. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  13124. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13125. ;; got extra space, this field does not determine column width
  13126. (let (org-table-may-need-update) (org-table-blank-field))
  13127. ;; no extra space, this field may determine column width
  13128. (org-table-blank-field)))
  13129. t)
  13130. (eq N 1)
  13131. (looking-at "[^|\n]* |"))
  13132. (let (org-table-may-need-update)
  13133. (goto-char (1- (match-end 0)))
  13134. (delete-backward-char 1)
  13135. (goto-char (match-beginning 0))
  13136. (self-insert-command N))
  13137. (setq org-table-may-need-update t)
  13138. (self-insert-command N)
  13139. (org-fix-tags-on-the-fly)
  13140. (if org-self-insert-cluster-for-undo
  13141. (if (not (eq last-command 'org-self-insert-command))
  13142. (setq org-self-insert-command-undo-counter 1)
  13143. (if (>= org-self-insert-command-undo-counter 20)
  13144. (setq org-self-insert-command-undo-counter 1)
  13145. (and (> org-self-insert-command-undo-counter 0)
  13146. buffer-undo-list
  13147. (not (cadr buffer-undo-list)) ; remove nil entry
  13148. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13149. (setq org-self-insert-command-undo-counter
  13150. (1+ org-self-insert-command-undo-counter)))))))
  13151. (defun org-fix-tags-on-the-fly ()
  13152. (when (and (equal (char-after (point-at-bol)) ?*)
  13153. (org-on-heading-p))
  13154. (org-align-tags-here org-tags-column)))
  13155. (defun org-delete-backward-char (N)
  13156. "Like `delete-backward-char', insert whitespace at field end in tables.
  13157. When deleting backwards, in tables this function will insert whitespace in
  13158. front of the next \"|\" separator, to keep the table aligned. The table will
  13159. still be marked for re-alignment if the field did fill the entire column,
  13160. because, in this case the deletion might narrow the column."
  13161. (interactive "p")
  13162. (if (and (org-table-p)
  13163. (eq N 1)
  13164. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13165. (looking-at ".*?|"))
  13166. (let ((pos (point))
  13167. (noalign (looking-at "[^|\n\r]* |"))
  13168. (c org-table-may-need-update))
  13169. (backward-delete-char N)
  13170. (skip-chars-forward "^|")
  13171. (insert " ")
  13172. (goto-char (1- pos))
  13173. ;; noalign: if there were two spaces at the end, this field
  13174. ;; does not determine the width of the column.
  13175. (if noalign (setq org-table-may-need-update c)))
  13176. (backward-delete-char N)
  13177. (org-fix-tags-on-the-fly)))
  13178. (defun org-delete-char (N)
  13179. "Like `delete-char', but insert whitespace at field end in tables.
  13180. When deleting characters, in tables this function will insert whitespace in
  13181. front of the next \"|\" separator, to keep the table aligned. The table will
  13182. still be marked for re-alignment if the field did fill the entire column,
  13183. because, in this case the deletion might narrow the column."
  13184. (interactive "p")
  13185. (if (and (org-table-p)
  13186. (not (bolp))
  13187. (not (= (char-after) ?|))
  13188. (eq N 1))
  13189. (if (looking-at ".*?|")
  13190. (let ((pos (point))
  13191. (noalign (looking-at "[^|\n\r]* |"))
  13192. (c org-table-may-need-update))
  13193. (replace-match (concat
  13194. (substring (match-string 0) 1 -1)
  13195. " |"))
  13196. (goto-char pos)
  13197. ;; noalign: if there were two spaces at the end, this field
  13198. ;; does not determine the width of the column.
  13199. (if noalign (setq org-table-may-need-update c)))
  13200. (delete-char N))
  13201. (delete-char N)
  13202. (org-fix-tags-on-the-fly)))
  13203. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13204. (put 'org-self-insert-command 'delete-selection t)
  13205. (put 'orgtbl-self-insert-command 'delete-selection t)
  13206. (put 'org-delete-char 'delete-selection 'supersede)
  13207. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13208. (put 'org-yank 'delete-selection 'yank)
  13209. ;; Make `flyspell-mode' delay after some commands
  13210. (put 'org-self-insert-command 'flyspell-delayed t)
  13211. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13212. (put 'org-delete-char 'flyspell-delayed t)
  13213. (put 'org-delete-backward-char 'flyspell-delayed t)
  13214. ;; Make pabbrev-mode expand after org-mode commands
  13215. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13216. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13217. ;; How to do this: Measure non-white length of current string
  13218. ;; If equal to column width, we should realign.
  13219. (defun org-remap (map &rest commands)
  13220. "In MAP, remap the functions given in COMMANDS.
  13221. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13222. (let (new old)
  13223. (while commands
  13224. (setq old (pop commands) new (pop commands))
  13225. (if (fboundp 'command-remapping)
  13226. (org-defkey map (vector 'remap old) new)
  13227. (substitute-key-definition old new map global-map)))))
  13228. (when (eq org-enable-table-editor 'optimized)
  13229. ;; If the user wants maximum table support, we need to hijack
  13230. ;; some standard editing functions
  13231. (org-remap org-mode-map
  13232. 'self-insert-command 'org-self-insert-command
  13233. 'delete-char 'org-delete-char
  13234. 'delete-backward-char 'org-delete-backward-char)
  13235. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13236. (defvar org-ctrl-c-ctrl-c-hook nil
  13237. "Hook for functions attaching themselves to `C-c C-c'.
  13238. This can be used to add additional functionality to the C-c C-c key which
  13239. executes context-dependent commands.
  13240. Each function will be called with no arguments. The function must check
  13241. if the context is appropriate for it to act. If yes, it should do its
  13242. thing and then return a non-nil value. If the context is wrong,
  13243. just do nothing and return nil.")
  13244. (defvar org-tab-first-hook nil
  13245. "Hook for functions to attach themselves to TAB.
  13246. See `org-ctrl-c-ctrl-c-hook' for more information.
  13247. This hook runs as the first action when TAB is pressed, even before
  13248. `org-cycle' messes around with the `outline-regexp' to cater for
  13249. inline tasks and plain list item folding.
  13250. If any function in this hook returns t, not other actions like table
  13251. field motion visibility cycling will be done.")
  13252. (defvar org-tab-after-check-for-table-hook nil
  13253. "Hook for functions to attach themselves to TAB.
  13254. See `org-ctrl-c-ctrl-c-hook' for more information.
  13255. This hook runs after it has been established that the cursor is not in a
  13256. table, but before checking if the cursor is in a headline or if global cycling
  13257. should be done.
  13258. If any function in this hook returns t, not other actions like visibility
  13259. cycling will be done.")
  13260. (defvar org-tab-after-check-for-cycling-hook nil
  13261. "Hook for functions to attach themselves to TAB.
  13262. See `org-ctrl-c-ctrl-c-hook' for more information.
  13263. This hook runs after it has been established that not table field motion and
  13264. not visibility should be done because of current context. This is probably
  13265. the place where a package like yasnippets can hook in.")
  13266. (defvar org-metaleft-hook nil
  13267. "Hook for functions attaching themselves to `M-left'.
  13268. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13269. (defvar org-metaright-hook nil
  13270. "Hook for functions attaching themselves to `M-right'.
  13271. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13272. (defvar org-metaup-hook nil
  13273. "Hook for functions attaching themselves to `M-up'.
  13274. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13275. (defvar org-metadown-hook nil
  13276. "Hook for functions attaching themselves to `M-down'.
  13277. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13278. (defvar org-shiftmetaleft-hook nil
  13279. "Hook for functions attaching themselves to `M-S-left'.
  13280. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13281. (defvar org-shiftmetaright-hook nil
  13282. "Hook for functions attaching themselves to `M-S-right'.
  13283. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13284. (defvar org-shiftmetaup-hook nil
  13285. "Hook for functions attaching themselves to `M-S-up'.
  13286. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13287. (defvar org-shiftmetadown-hook nil
  13288. "Hook for functions attaching themselves to `M-S-down'.
  13289. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13290. (defvar org-metareturn-hook nil
  13291. "Hook for functions attaching themselves to `M-RET'.
  13292. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13293. (defun org-modifier-cursor-error ()
  13294. "Throw an error, a modified cursor command was applied in wrong context."
  13295. (error "This command is active in special context like tables, headlines or items"))
  13296. (defun org-shiftselect-error ()
  13297. "Throw an error because Shift-Cursor command was applied in wrong context."
  13298. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13299. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13300. (error "This command works only in special context like headlines or timestamps.")))
  13301. (defun org-call-for-shift-select (cmd)
  13302. (let ((this-command-keys-shift-translated t))
  13303. (call-interactively cmd)))
  13304. (defun org-shifttab (&optional arg)
  13305. "Global visibility cycling or move to previous table field.
  13306. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13307. on context.
  13308. See the individual commands for more information."
  13309. (interactive "P")
  13310. (cond
  13311. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13312. ((integerp arg)
  13313. (message "Content view to level: %d" arg)
  13314. (org-content (prefix-numeric-value arg))
  13315. (setq org-cycle-global-status 'overview))
  13316. (t (call-interactively 'org-global-cycle))))
  13317. (defun org-shiftmetaleft ()
  13318. "Promote subtree or delete table column.
  13319. Calls `org-promote-subtree', `org-outdent-item',
  13320. or `org-table-delete-column', depending on context.
  13321. See the individual commands for more information."
  13322. (interactive)
  13323. (cond
  13324. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13325. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13326. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13327. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13328. (t (org-modifier-cursor-error))))
  13329. (defun org-shiftmetaright ()
  13330. "Demote subtree or insert table column.
  13331. Calls `org-demote-subtree', `org-indent-item',
  13332. or `org-table-insert-column', depending on context.
  13333. See the individual commands for more information."
  13334. (interactive)
  13335. (cond
  13336. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13337. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13338. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13339. ((org-at-item-p) (call-interactively 'org-indent-item))
  13340. (t (org-modifier-cursor-error))))
  13341. (defun org-shiftmetaup (&optional arg)
  13342. "Move subtree up or kill table row.
  13343. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13344. `org-move-item-up' depending on context. See the individual commands
  13345. for more information."
  13346. (interactive "P")
  13347. (cond
  13348. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13349. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13350. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13351. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13352. (t (org-modifier-cursor-error))))
  13353. (defun org-shiftmetadown (&optional arg)
  13354. "Move subtree down or insert table row.
  13355. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13356. `org-move-item-down', depending on context. See the individual
  13357. commands for more information."
  13358. (interactive "P")
  13359. (cond
  13360. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13361. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13362. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13363. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13364. (t (org-modifier-cursor-error))))
  13365. (defun org-metaleft (&optional arg)
  13366. "Promote heading or move table column to left.
  13367. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13368. With no specific context, calls the Emacs default `backward-word'.
  13369. See the individual commands for more information."
  13370. (interactive "P")
  13371. (cond
  13372. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13373. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13374. ((or (org-on-heading-p)
  13375. (and (org-region-active-p)
  13376. (save-excursion
  13377. (goto-char (region-beginning))
  13378. (org-on-heading-p))))
  13379. (call-interactively 'org-do-promote))
  13380. ((or (org-at-item-p)
  13381. (and (org-region-active-p)
  13382. (save-excursion
  13383. (goto-char (region-beginning))
  13384. (org-at-item-p))))
  13385. (call-interactively 'org-outdent-item))
  13386. (t (call-interactively 'backward-word))))
  13387. (defun org-metaright (&optional arg)
  13388. "Demote subtree or move table column to right.
  13389. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13390. With no specific context, calls the Emacs default `forward-word'.
  13391. See the individual commands for more information."
  13392. (interactive "P")
  13393. (cond
  13394. ((run-hook-with-args-until-success 'org-metaright-hook))
  13395. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13396. ((or (org-on-heading-p)
  13397. (and (org-region-active-p)
  13398. (save-excursion
  13399. (goto-char (region-beginning))
  13400. (org-on-heading-p))))
  13401. (call-interactively 'org-do-demote))
  13402. ((or (org-at-item-p)
  13403. (and (org-region-active-p)
  13404. (save-excursion
  13405. (goto-char (region-beginning))
  13406. (org-at-item-p))))
  13407. (call-interactively 'org-indent-item))
  13408. (t (call-interactively 'forward-word))))
  13409. (defun org-metaup (&optional arg)
  13410. "Move subtree up or move table row up.
  13411. Calls `org-move-subtree-up' or `org-table-move-row' or
  13412. `org-move-item-up', depending on context. See the individual commands
  13413. for more information."
  13414. (interactive "P")
  13415. (cond
  13416. ((run-hook-with-args-until-success 'org-metaup-hook))
  13417. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13418. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13419. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13420. (t (transpose-lines 1) (beginning-of-line -1))))
  13421. (defun org-metadown (&optional arg)
  13422. "Move subtree down or move table row down.
  13423. Calls `org-move-subtree-down' or `org-table-move-row' or
  13424. `org-move-item-down', depending on context. See the individual
  13425. commands for more information."
  13426. (interactive "P")
  13427. (cond
  13428. ((run-hook-with-args-until-success 'org-metadown-hook))
  13429. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13430. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13431. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13432. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13433. (defun org-shiftup (&optional arg)
  13434. "Increase item in timestamp or increase priority of current headline.
  13435. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13436. depending on context. See the individual commands for more information."
  13437. (interactive "P")
  13438. (cond
  13439. ((and org-support-shift-select (org-region-active-p))
  13440. (org-call-for-shift-select 'previous-line))
  13441. ((org-at-timestamp-p t)
  13442. (call-interactively (if org-edit-timestamp-down-means-later
  13443. 'org-timestamp-down 'org-timestamp-up)))
  13444. ((and (not (eq org-support-shift-select 'always))
  13445. org-enable-priority-commands
  13446. (org-on-heading-p))
  13447. (call-interactively 'org-priority-up))
  13448. ((and (not org-support-shift-select) (org-at-item-p))
  13449. (call-interactively 'org-previous-item))
  13450. ((org-clocktable-try-shift 'up arg))
  13451. (org-support-shift-select
  13452. (org-call-for-shift-select 'previous-line))
  13453. (t (org-shiftselect-error))))
  13454. (defun org-shiftdown (&optional arg)
  13455. "Decrease item in timestamp or decrease priority of current headline.
  13456. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13457. depending on context. See the individual commands for more information."
  13458. (interactive "P")
  13459. (cond
  13460. ((and org-support-shift-select (org-region-active-p))
  13461. (org-call-for-shift-select 'next-line))
  13462. ((org-at-timestamp-p t)
  13463. (call-interactively (if org-edit-timestamp-down-means-later
  13464. 'org-timestamp-up 'org-timestamp-down)))
  13465. ((and (not (eq org-support-shift-select 'always))
  13466. org-enable-priority-commands
  13467. (org-on-heading-p))
  13468. (call-interactively 'org-priority-down))
  13469. ((and (not org-support-shift-select) (org-at-item-p))
  13470. (call-interactively 'org-next-item))
  13471. ((org-clocktable-try-shift 'down arg))
  13472. (org-support-shift-select
  13473. (org-call-for-shift-select 'next-line))
  13474. (t (org-shiftselect-error))))
  13475. (defun org-shiftright (&optional arg)
  13476. "Cycle the thing at point or in the current line, depending on context.
  13477. Depending on context, this does one of the following:
  13478. - switch a timestamp at point one day into the future
  13479. - on a headline, switch to the next TODO keyword.
  13480. - on an item, switch entire list to the next bullet type
  13481. - on a property line, switch to the next allowed value
  13482. - on a clocktable definition line, move time block into the future"
  13483. (interactive "P")
  13484. (cond
  13485. ((and org-support-shift-select (org-region-active-p))
  13486. (org-call-for-shift-select 'forward-char))
  13487. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13488. ((and (not (eq org-support-shift-select 'always))
  13489. (org-on-heading-p))
  13490. (let ((org-inhibit-logging
  13491. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13492. (org-call-with-arg 'org-todo 'right)))
  13493. ((or (and org-support-shift-select
  13494. (not (eq org-support-shift-select 'always))
  13495. (org-at-item-bullet-p))
  13496. (and (not org-support-shift-select) (org-at-item-p)))
  13497. (org-call-with-arg 'org-cycle-list-bullet nil))
  13498. ((and (not (eq org-support-shift-select 'always))
  13499. (org-at-property-p))
  13500. (call-interactively 'org-property-next-allowed-value))
  13501. ((org-clocktable-try-shift 'right arg))
  13502. (org-support-shift-select
  13503. (org-call-for-shift-select 'forward-char))
  13504. (t (org-shiftselect-error))))
  13505. (defun org-shiftleft (&optional arg)
  13506. "Cycle the thing at point or in the current line, depending on context.
  13507. Depending on context, this does one of the following:
  13508. - switch a timestamp at point one day into the past
  13509. - on a headline, switch to the previous TODO keyword.
  13510. - on an item, switch entire list to the previous bullet type
  13511. - on a property line, switch to the previous allowed value
  13512. - on a clocktable definition line, move time block into the past"
  13513. (interactive "P")
  13514. (cond
  13515. ((and org-support-shift-select (org-region-active-p))
  13516. (org-call-for-shift-select 'backward-char))
  13517. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13518. ((and (not (eq org-support-shift-select 'always))
  13519. (org-on-heading-p))
  13520. (let ((org-inhibit-logging
  13521. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13522. (org-call-with-arg 'org-todo 'left)))
  13523. ((or (and org-support-shift-select
  13524. (not (eq org-support-shift-select 'always))
  13525. (org-at-item-bullet-p))
  13526. (and (not org-support-shift-select) (org-at-item-p)))
  13527. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13528. ((and (not (eq org-support-shift-select 'always))
  13529. (org-at-property-p))
  13530. (call-interactively 'org-property-previous-allowed-value))
  13531. ((org-clocktable-try-shift 'left arg))
  13532. (org-support-shift-select
  13533. (org-call-for-shift-select 'backward-char))
  13534. (t (org-shiftselect-error))))
  13535. (defun org-shiftcontrolright ()
  13536. "Switch to next TODO set."
  13537. (interactive)
  13538. (cond
  13539. ((and org-support-shift-select (org-region-active-p))
  13540. (org-call-for-shift-select 'forward-word))
  13541. ((and (not (eq org-support-shift-select 'always))
  13542. (org-on-heading-p))
  13543. (org-call-with-arg 'org-todo 'nextset))
  13544. (org-support-shift-select
  13545. (org-call-for-shift-select 'forward-word))
  13546. (t (org-shiftselect-error))))
  13547. (defun org-shiftcontrolleft ()
  13548. "Switch to previous TODO set."
  13549. (interactive)
  13550. (cond
  13551. ((and org-support-shift-select (org-region-active-p))
  13552. (org-call-for-shift-select 'backward-word))
  13553. ((and (not (eq org-support-shift-select 'always))
  13554. (org-on-heading-p))
  13555. (org-call-with-arg 'org-todo 'previousset))
  13556. (org-support-shift-select
  13557. (org-call-for-shift-select 'backward-word))
  13558. (t (org-shiftselect-error))))
  13559. (defun org-ctrl-c-ret ()
  13560. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13561. (interactive)
  13562. (cond
  13563. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13564. (t (call-interactively 'org-insert-heading))))
  13565. (defun org-copy-special ()
  13566. "Copy region in table or copy current subtree.
  13567. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13568. See the individual commands for more information."
  13569. (interactive)
  13570. (call-interactively
  13571. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13572. (defun org-cut-special ()
  13573. "Cut region in table or cut current subtree.
  13574. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13575. See the individual commands for more information."
  13576. (interactive)
  13577. (call-interactively
  13578. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13579. (defun org-paste-special (arg)
  13580. "Paste rectangular region into table, or past subtree relative to level.
  13581. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13582. See the individual commands for more information."
  13583. (interactive "P")
  13584. (if (org-at-table-p)
  13585. (org-table-paste-rectangle)
  13586. (org-paste-subtree arg)))
  13587. (defun org-edit-special ()
  13588. "Call a special editor for the stuff at point.
  13589. When at a table, call the formula editor with `org-table-edit-formulas'.
  13590. When at the first line of an src example, call `org-edit-src-code'.
  13591. When in an #+include line, visit the include file. Otherwise call
  13592. `ffap' to visit the file at point."
  13593. (interactive)
  13594. (cond
  13595. ((org-at-table-p)
  13596. (call-interactively 'org-table-edit-formulas))
  13597. ((save-excursion
  13598. (beginning-of-line 1)
  13599. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13600. (find-file (org-trim (match-string 1))))
  13601. ((org-edit-src-code))
  13602. ((org-edit-fixed-width-region))
  13603. (t (call-interactively 'ffap))))
  13604. (defun org-ctrl-c-ctrl-c (&optional arg)
  13605. "Set tags in headline, or update according to changed information at point.
  13606. This command does many different things, depending on context:
  13607. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13608. this is what we do.
  13609. - If the cursor is in a headline, prompt for tags and insert them
  13610. into the current line, aligned to `org-tags-column'. When called
  13611. with prefix arg, realign all tags in the current buffer.
  13612. - If the cursor is in one of the special #+KEYWORD lines, this
  13613. triggers scanning the buffer for these lines and updating the
  13614. information.
  13615. - If the cursor is inside a table, realign the table. This command
  13616. works even if the automatic table editor has been turned off.
  13617. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13618. the entire table.
  13619. - If the cursor is at a footnote reference or definition, jump to
  13620. the corresponding definition or references, respectively.
  13621. - If the cursor is a the beginning of a dynamic block, update it.
  13622. - If the cursor is inside a table created by the table.el package,
  13623. activate that table.
  13624. - If the current buffer is a remember buffer, close note and file
  13625. it. A prefix argument of 1 files to the default location
  13626. without further interaction. A prefix argument of 2 files to
  13627. the currently clocking task.
  13628. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13629. links in this buffer.
  13630. - If the cursor is on a numbered item in a plain list, renumber the
  13631. ordered list.
  13632. - If the cursor is on a checkbox, toggle it."
  13633. (interactive "P")
  13634. (let ((org-enable-table-editor t))
  13635. (cond
  13636. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13637. org-occur-highlights
  13638. org-latex-fragment-image-overlays)
  13639. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13640. (org-remove-occur-highlights)
  13641. (org-remove-latex-fragment-image-overlays)
  13642. (message "Temporary highlights/overlays removed from current buffer"))
  13643. ((and (local-variable-p 'org-finish-function (current-buffer))
  13644. (fboundp org-finish-function))
  13645. (funcall org-finish-function))
  13646. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13647. ((org-at-property-p)
  13648. (call-interactively 'org-property-action))
  13649. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13650. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13651. ((org-at-table.el-p)
  13652. (require 'table)
  13653. (beginning-of-line 1)
  13654. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13655. (call-interactively 'table-recognize-table))
  13656. ((org-at-table-p)
  13657. (org-table-maybe-eval-formula)
  13658. (if arg
  13659. (call-interactively 'org-table-recalculate)
  13660. (org-table-maybe-recalculate-line))
  13661. (call-interactively 'org-table-align))
  13662. ((or (org-footnote-at-reference-p)
  13663. (org-footnote-at-definition-p))
  13664. (call-interactively 'org-footnote-action))
  13665. ((org-at-item-checkbox-p)
  13666. (call-interactively 'org-toggle-checkbox))
  13667. ((org-at-item-p)
  13668. (if arg
  13669. (call-interactively 'org-toggle-checkbox)
  13670. (call-interactively 'org-maybe-renumber-ordered-list)))
  13671. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13672. ;; Dynamic block
  13673. (beginning-of-line 1)
  13674. (save-excursion (org-update-dblock)))
  13675. ((save-excursion
  13676. (beginning-of-line 1)
  13677. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13678. (cond
  13679. ((equal (match-string 1) "TBLFM")
  13680. ;; Recalculate the table before this line
  13681. (save-excursion
  13682. (beginning-of-line 1)
  13683. (skip-chars-backward " \r\n\t")
  13684. (if (org-at-table-p)
  13685. (org-call-with-arg 'org-table-recalculate t))))
  13686. (t
  13687. ; (org-set-regexps-and-options)
  13688. ; (org-restart-font-lock)
  13689. (let ((org-inhibit-startup t)) (org-mode-restart))
  13690. (message "Local setup has been refreshed"))))
  13691. (t (error "C-c C-c can do nothing useful at this location.")))))
  13692. (defun org-mode-restart ()
  13693. "Restart Org-mode, to scan again for special lines.
  13694. Also updates the keyword regular expressions."
  13695. (interactive)
  13696. (org-mode)
  13697. (message "Org-mode restarted"))
  13698. (defun org-kill-note-or-show-branches ()
  13699. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13700. (interactive)
  13701. (if (not org-finish-function)
  13702. (call-interactively 'show-branches)
  13703. (let ((org-note-abort t))
  13704. (funcall org-finish-function))))
  13705. (defun org-return (&optional indent)
  13706. "Goto next table row or insert a newline.
  13707. Calls `org-table-next-row' or `newline', depending on context.
  13708. See the individual commands for more information."
  13709. (interactive)
  13710. (cond
  13711. ((bobp) (if indent (newline-and-indent) (newline)))
  13712. ((org-at-table-p)
  13713. (org-table-justify-field-maybe)
  13714. (call-interactively 'org-table-next-row))
  13715. ((and org-return-follows-link
  13716. (eq (get-text-property (point) 'face) 'org-link))
  13717. (call-interactively 'org-open-at-point))
  13718. ((and (org-at-heading-p)
  13719. (looking-at
  13720. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13721. (org-show-entry)
  13722. (end-of-line 1)
  13723. (newline))
  13724. (t (if indent (newline-and-indent) (newline)))))
  13725. (defun org-return-indent ()
  13726. "Goto next table row or insert a newline and indent.
  13727. Calls `org-table-next-row' or `newline-and-indent', depending on
  13728. context. See the individual commands for more information."
  13729. (interactive)
  13730. (org-return t))
  13731. (defun org-ctrl-c-star ()
  13732. "Compute table, or change heading status of lines.
  13733. Calls `org-table-recalculate' or `org-toggle-heading',
  13734. depending on context."
  13735. (interactive)
  13736. (cond
  13737. ((org-at-table-p)
  13738. (call-interactively 'org-table-recalculate))
  13739. (t
  13740. ;; Convert all lines in region to list items
  13741. (call-interactively 'org-toggle-heading))))
  13742. (defun org-ctrl-c-minus ()
  13743. "Insert separator line in table or modify bullet status of line.
  13744. Also turns a plain line or a region of lines into list items.
  13745. Calls `org-table-insert-hline', `org-toggle-item', or
  13746. `org-cycle-list-bullet', depending on context."
  13747. (interactive)
  13748. (cond
  13749. ((org-at-table-p)
  13750. (call-interactively 'org-table-insert-hline))
  13751. ((org-region-active-p)
  13752. (call-interactively 'org-toggle-item))
  13753. ((org-in-item-p)
  13754. (call-interactively 'org-cycle-list-bullet))
  13755. (t
  13756. (call-interactively 'org-toggle-item))))
  13757. (defun org-toggle-item ()
  13758. "Convert headings or normal lines to items, items to normal lines.
  13759. If there is no active region, only the current line is considered.
  13760. If the first line in the region is a headline, convert all headlines to items.
  13761. If the first line in the region is an item, convert all items to normal lines.
  13762. If the first line is normal text, add an item bullet to each line."
  13763. (interactive)
  13764. (let (l2 l beg end)
  13765. (if (org-region-active-p)
  13766. (setq beg (region-beginning) end (region-end))
  13767. (setq beg (point-at-bol)
  13768. end (min (1+ (point-at-eol)) (point-max))))
  13769. (save-excursion
  13770. (goto-char end)
  13771. (setq l2 (org-current-line))
  13772. (goto-char beg)
  13773. (beginning-of-line 1)
  13774. (setq l (1- (org-current-line)))
  13775. (if (org-at-item-p)
  13776. ;; We already have items, de-itemize
  13777. (while (< (setq l (1+ l)) l2)
  13778. (when (org-at-item-p)
  13779. (goto-char (match-beginning 2))
  13780. (delete-region (match-beginning 2) (match-end 2))
  13781. (and (looking-at "[ \t]+") (replace-match "")))
  13782. (beginning-of-line 2))
  13783. (if (org-on-heading-p)
  13784. ;; Headings, convert to items
  13785. (while (< (setq l (1+ l)) l2)
  13786. (if (looking-at org-outline-regexp)
  13787. (replace-match "- " t t))
  13788. (beginning-of-line 2))
  13789. ;; normal lines, turn them into items
  13790. (while (< (setq l (1+ l)) l2)
  13791. (unless (org-at-item-p)
  13792. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13793. (replace-match "\\1- \\2")))
  13794. (beginning-of-line 2)))))))
  13795. (defun org-toggle-heading (&optional nstars)
  13796. "Convert headings to normal text, or items or text to headings.
  13797. If there is no active region, only the current line is considered.
  13798. If the first line is a heading, remove the stars from all headlines
  13799. in the region.
  13800. If the first line is a plain list item, turn all plain list items into
  13801. headings.
  13802. If the first line is a normal line, turn each and every line in the region
  13803. into a heading.
  13804. When converting a line into a heading, the number of stars is chosen
  13805. such that the lines become children of the current entry. However, when
  13806. a prefix argument is given, its value determines the number of stars to add."
  13807. (interactive "P")
  13808. (let (l2 l itemp beg end)
  13809. (if (org-region-active-p)
  13810. (setq beg (region-beginning) end (region-end))
  13811. (setq beg (point-at-bol)
  13812. end (min (1+ (point-at-eol)) (point-max))))
  13813. (save-excursion
  13814. (goto-char end)
  13815. (setq l2 (org-current-line))
  13816. (goto-char beg)
  13817. (beginning-of-line 1)
  13818. (setq l (1- (org-current-line)))
  13819. (if (org-on-heading-p)
  13820. ;; We already have headlines, de-star them
  13821. (while (< (setq l (1+ l)) l2)
  13822. (when (org-on-heading-p t)
  13823. (and (looking-at outline-regexp) (replace-match "")))
  13824. (beginning-of-line 2))
  13825. (setq itemp (org-at-item-p))
  13826. (let* ((stars
  13827. (if nstars
  13828. (make-string (prefix-numeric-value current-prefix-arg)
  13829. ?*)
  13830. (save-excursion
  13831. (re-search-backward org-complex-heading-regexp nil t)
  13832. (or (match-string 1) ""))))
  13833. (add-stars (cond (nstars "")
  13834. ((equal stars "") "*")
  13835. (org-odd-levels-only "**")
  13836. (t "*")))
  13837. (rpl (concat stars add-stars " ")))
  13838. (while (< (setq l (1+ l)) l2)
  13839. (if itemp
  13840. (and (org-at-item-p) (replace-match rpl t t))
  13841. (unless (org-on-heading-p)
  13842. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13843. (replace-match (concat rpl (match-string 2))))))
  13844. (beginning-of-line 2)))))))
  13845. (defun org-meta-return (&optional arg)
  13846. "Insert a new heading or wrap a region in a table.
  13847. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13848. See the individual commands for more information."
  13849. (interactive "P")
  13850. (cond
  13851. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13852. ((org-at-table-p)
  13853. (call-interactively 'org-table-wrap-region))
  13854. (t (call-interactively 'org-insert-heading))))
  13855. ;;; Menu entries
  13856. ;; Define the Org-mode menus
  13857. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13858. '("Tbl"
  13859. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13860. ["Next Field" org-cycle (org-at-table-p)]
  13861. ["Previous Field" org-shifttab (org-at-table-p)]
  13862. ["Next Row" org-return (org-at-table-p)]
  13863. "--"
  13864. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13865. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13866. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13867. "--"
  13868. ("Column"
  13869. ["Move Column Left" org-metaleft (org-at-table-p)]
  13870. ["Move Column Right" org-metaright (org-at-table-p)]
  13871. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13872. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13873. ("Row"
  13874. ["Move Row Up" org-metaup (org-at-table-p)]
  13875. ["Move Row Down" org-metadown (org-at-table-p)]
  13876. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13877. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13878. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13879. "--"
  13880. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13881. ("Rectangle"
  13882. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13883. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13884. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13885. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13886. "--"
  13887. ("Calculate"
  13888. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13889. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13890. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13891. "--"
  13892. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13893. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13894. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13895. "--"
  13896. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13897. "--"
  13898. ["Sum Column/Rectangle" org-table-sum
  13899. (or (org-at-table-p) (org-region-active-p))]
  13900. ["Which Column?" org-table-current-column (org-at-table-p)])
  13901. ["Debug Formulas"
  13902. org-table-toggle-formula-debugger
  13903. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13904. ["Show Col/Row Numbers"
  13905. org-table-toggle-coordinate-overlays
  13906. :style toggle
  13907. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13908. "--"
  13909. ["Create" org-table-create (and (not (org-at-table-p))
  13910. org-enable-table-editor)]
  13911. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13912. ["Import from File" org-table-import (not (org-at-table-p))]
  13913. ["Export to File" org-table-export (org-at-table-p)]
  13914. "--"
  13915. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13916. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13917. '("Org"
  13918. ("Show/Hide"
  13919. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13920. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13921. ["Sparse Tree..." org-sparse-tree t]
  13922. ["Reveal Context" org-reveal t]
  13923. ["Show All" show-all t]
  13924. "--"
  13925. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13926. "--"
  13927. ["New Heading" org-insert-heading t]
  13928. ("Navigate Headings"
  13929. ["Up" outline-up-heading t]
  13930. ["Next" outline-next-visible-heading t]
  13931. ["Previous" outline-previous-visible-heading t]
  13932. ["Next Same Level" outline-forward-same-level t]
  13933. ["Previous Same Level" outline-backward-same-level t]
  13934. "--"
  13935. ["Jump" org-goto t])
  13936. ("Edit Structure"
  13937. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13938. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13939. "--"
  13940. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13941. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13942. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13943. "--"
  13944. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13945. "--"
  13946. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13947. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13948. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13949. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13950. "--"
  13951. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13952. "--"
  13953. ["Convert to odd levels" org-convert-to-odd-levels t]
  13954. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13955. ("Editing"
  13956. ["Emphasis..." org-emphasize t]
  13957. ["Edit Source Example" org-edit-special t]
  13958. "--"
  13959. ["Footnote new/jump" org-footnote-action t]
  13960. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13961. ("Archive"
  13962. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13963. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13964. ; :active t :keys "C-u C-c C-x C-a"]
  13965. ["Sparse trees open ARCHIVE trees"
  13966. (setq org-sparse-tree-open-archived-trees
  13967. (not org-sparse-tree-open-archived-trees))
  13968. :style toggle :selected org-sparse-tree-open-archived-trees]
  13969. ["Cycling opens ARCHIVE trees"
  13970. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13971. :style toggle :selected org-cycle-open-archived-trees]
  13972. "--"
  13973. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13974. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13975. ; ["Check and Move Children" (org-archive-subtree '(4))
  13976. ; :active t :keys "C-u C-c C-x C-s"]
  13977. )
  13978. "--"
  13979. ("Hyperlinks"
  13980. ["Store Link (Global)" org-store-link t]
  13981. ["Insert Link" org-insert-link t]
  13982. ["Follow Link" org-open-at-point t]
  13983. "--"
  13984. ["Next link" org-next-link t]
  13985. ["Previous link" org-previous-link t]
  13986. "--"
  13987. ["Descriptive Links"
  13988. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13989. :style radio
  13990. :selected (member '(org-link) buffer-invisibility-spec)]
  13991. ["Literal Links"
  13992. (progn
  13993. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13994. :style radio
  13995. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13996. "--"
  13997. ("TODO Lists"
  13998. ["TODO/DONE/-" org-todo t]
  13999. ("Select keyword"
  14000. ["Next keyword" org-shiftright (org-on-heading-p)]
  14001. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14002. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14003. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14004. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14005. ["Show TODO Tree" org-show-todo-tree t]
  14006. ["Global TODO list" org-todo-list t]
  14007. "--"
  14008. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14009. :selected org-enforce-todo-dependencies :style toggle :active t]
  14010. "Settings for tree at point"
  14011. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14012. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14013. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14014. ["Do Children parallel" org-toggle-ordered-property :style radio
  14015. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14016. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14017. "--"
  14018. ["Set Priority" org-priority t]
  14019. ["Priority Up" org-shiftup t]
  14020. ["Priority Down" org-shiftdown t]
  14021. "--"
  14022. ["Get news from all feeds" org-feed-update-all t]
  14023. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14024. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14025. ("TAGS and Properties"
  14026. ["Set Tags" org-set-tags-command t]
  14027. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14028. "--"
  14029. ["Set property" org-set-property t]
  14030. ["Column view of properties" org-columns t]
  14031. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14032. ("Dates and Scheduling"
  14033. ["Timestamp" org-time-stamp t]
  14034. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14035. ("Change Date"
  14036. ["1 Day Later" org-shiftright t]
  14037. ["1 Day Earlier" org-shiftleft t]
  14038. ["1 ... Later" org-shiftup t]
  14039. ["1 ... Earlier" org-shiftdown t])
  14040. ["Compute Time Range" org-evaluate-time-range t]
  14041. ["Schedule Item" org-schedule t]
  14042. ["Deadline" org-deadline t]
  14043. "--"
  14044. ["Custom time format" org-toggle-time-stamp-overlays
  14045. :style radio :selected org-display-custom-times]
  14046. "--"
  14047. ["Goto Calendar" org-goto-calendar t]
  14048. ["Date from Calendar" org-date-from-calendar t]
  14049. "--"
  14050. ["Start/Restart Timer" org-timer-start t]
  14051. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14052. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14053. ["Insert Timer String" org-timer t]
  14054. ["Insert Timer Item" org-timer-item t])
  14055. ("Logging work"
  14056. ["Clock in" org-clock-in t]
  14057. ["Clock out" org-clock-out t]
  14058. ["Clock cancel" org-clock-cancel t]
  14059. ["Goto running clock" org-clock-goto t]
  14060. ["Display times" org-clock-display t]
  14061. ["Create clock table" org-clock-report t]
  14062. "--"
  14063. ["Record DONE time"
  14064. (progn (setq org-log-done (not org-log-done))
  14065. (message "Switching to %s will %s record a timestamp"
  14066. (car org-done-keywords)
  14067. (if org-log-done "automatically" "not")))
  14068. :style toggle :selected org-log-done])
  14069. "--"
  14070. ["Agenda Command..." org-agenda t]
  14071. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14072. ("File List for Agenda")
  14073. ("Special views current file"
  14074. ["TODO Tree" org-show-todo-tree t]
  14075. ["Check Deadlines" org-check-deadlines t]
  14076. ["Timeline" org-timeline t]
  14077. ["Tags/Property tree" org-match-sparse-tree t])
  14078. "--"
  14079. ["Export/Publish..." org-export t]
  14080. ("LaTeX"
  14081. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14082. :selected org-cdlatex-mode]
  14083. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14084. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14085. ["Modify math symbol" org-cdlatex-math-modify
  14086. (org-inside-LaTeX-fragment-p)]
  14087. ["Insert citation" org-reftex-citation t]
  14088. "--"
  14089. ["Export LaTeX fragments as images"
  14090. (if (featurep 'org-exp)
  14091. (setq org-export-with-LaTeX-fragments
  14092. (not org-export-with-LaTeX-fragments))
  14093. (require 'org-exp))
  14094. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14095. org-export-with-LaTeX-fragments)])
  14096. "--"
  14097. ("Documentation"
  14098. ["Show Version" org-version t]
  14099. ["Info Documentation" org-info t])
  14100. ("Customize"
  14101. ["Browse Org Group" org-customize t]
  14102. "--"
  14103. ["Expand This Menu" org-create-customize-menu
  14104. (fboundp 'customize-menu-create)])
  14105. "--"
  14106. ("Refresh/Reload"
  14107. ["Refresh setup current buffer" org-mode-restart t]
  14108. ["Reload Org (after update)" org-reload t]
  14109. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14110. ))
  14111. (defun org-info (&optional node)
  14112. "Read documentation for Org-mode in the info system.
  14113. With optional NODE, go directly to that node."
  14114. (interactive)
  14115. (info (format "(org)%s" (or node ""))))
  14116. (defun org-install-agenda-files-menu ()
  14117. (let ((bl (buffer-list)))
  14118. (save-excursion
  14119. (while bl
  14120. (set-buffer (pop bl))
  14121. (if (org-mode-p) (setq bl nil)))
  14122. (when (org-mode-p)
  14123. (easy-menu-change
  14124. '("Org") "File List for Agenda"
  14125. (append
  14126. (list
  14127. ["Edit File List" (org-edit-agenda-file-list) t]
  14128. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14129. ["Remove Current File from List" org-remove-file t]
  14130. ["Cycle through agenda files" org-cycle-agenda-files t]
  14131. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14132. "--")
  14133. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14134. ;;;; Documentation
  14135. ;;;###autoload
  14136. (defun org-require-autoloaded-modules ()
  14137. (interactive)
  14138. (mapc 'require
  14139. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14140. org-docbook org-exp org-html org-icalendar
  14141. org-id org-latex
  14142. org-publish org-remember org-table
  14143. org-timer org-xoxo)))
  14144. ;;;###autoload
  14145. (defun org-reload (&optional uncompiled)
  14146. "Reload all org lisp files.
  14147. With prefix arg UNCOMPILED, load the uncompiled versions."
  14148. (interactive "P")
  14149. (require 'find-func)
  14150. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14151. (dir-org (file-name-directory (org-find-library-name "org")))
  14152. (dir-org-contrib (ignore-errors
  14153. (file-name-directory
  14154. (org-find-library-name "org-contribdir"))))
  14155. (files
  14156. (append (directory-files dir-org t file-re)
  14157. (and dir-org-contrib
  14158. (directory-files dir-org-contrib t file-re))))
  14159. (remove-re (concat (if (featurep 'xemacs)
  14160. "org-colview" "org-colview-xemacs")
  14161. "\\'")))
  14162. (setq files (mapcar 'file-name-sans-extension files))
  14163. (setq files (mapcar
  14164. (lambda (x) (if (string-match remove-re x) nil x))
  14165. files))
  14166. (setq files (delq nil files))
  14167. (mapc
  14168. (lambda (f)
  14169. (when (featurep (intern (file-name-nondirectory f)))
  14170. (if (and (not uncompiled)
  14171. (file-exists-p (concat f ".elc")))
  14172. (load (concat f ".elc") nil nil t)
  14173. (load (concat f ".el") nil nil t))))
  14174. files))
  14175. (org-version))
  14176. ;;;###autoload
  14177. (defun org-customize ()
  14178. "Call the customize function with org as argument."
  14179. (interactive)
  14180. (org-load-modules-maybe)
  14181. (org-require-autoloaded-modules)
  14182. (customize-browse 'org))
  14183. (defun org-create-customize-menu ()
  14184. "Create a full customization menu for Org-mode, insert it into the menu."
  14185. (interactive)
  14186. (org-load-modules-maybe)
  14187. (org-require-autoloaded-modules)
  14188. (if (fboundp 'customize-menu-create)
  14189. (progn
  14190. (easy-menu-change
  14191. '("Org") "Customize"
  14192. `(["Browse Org group" org-customize t]
  14193. "--"
  14194. ,(customize-menu-create 'org)
  14195. ["Set" Custom-set t]
  14196. ["Save" Custom-save t]
  14197. ["Reset to Current" Custom-reset-current t]
  14198. ["Reset to Saved" Custom-reset-saved t]
  14199. ["Reset to Standard Settings" Custom-reset-standard t]))
  14200. (message "\"Org\"-menu now contains full customization menu"))
  14201. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14202. ;;;; Miscellaneous stuff
  14203. ;;; Generally useful functions
  14204. (defun org-find-text-property-in-string (prop s)
  14205. "Return the first non-nil value of property PROP in string S."
  14206. (or (get-text-property 0 prop s)
  14207. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14208. prop s)))
  14209. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14210. "Display the given MESSAGE as a warning."
  14211. (if (fboundp 'display-warning)
  14212. (display-warning 'org message
  14213. (if (featurep 'xemacs)
  14214. 'warning
  14215. :warning))
  14216. (let ((buf (get-buffer-create "*Org warnings*")))
  14217. (with-current-buffer buf
  14218. (goto-char (point-max))
  14219. (insert "Warning (Org): " message)
  14220. (unless (bolp)
  14221. (newline)))
  14222. (display-buffer buf)
  14223. (sit-for 0))))
  14224. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14225. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14226. (if (and marker (marker-buffer marker)
  14227. (buffer-live-p (marker-buffer marker)))
  14228. (progn
  14229. (switch-to-buffer (marker-buffer marker))
  14230. (if (or (> marker (point-max)) (< marker (point-min)))
  14231. (widen))
  14232. (goto-char marker)
  14233. (org-show-context 'org-goto))
  14234. (if bookmark
  14235. (bookmark-jump bookmark)
  14236. (error "Cannot find location"))))
  14237. (defun org-quote-csv-field (s)
  14238. "Quote field for inclusion in CSV material."
  14239. (if (string-match "[\",]" s)
  14240. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14241. s))
  14242. (defun org-plist-delete (plist property)
  14243. "Delete PROPERTY from PLIST.
  14244. This is in contrast to merely setting it to 0."
  14245. (let (p)
  14246. (while plist
  14247. (if (not (eq property (car plist)))
  14248. (setq p (plist-put p (car plist) (nth 1 plist))))
  14249. (setq plist (cddr plist)))
  14250. p))
  14251. (defun org-force-self-insert (N)
  14252. "Needed to enforce self-insert under remapping."
  14253. (interactive "p")
  14254. (self-insert-command N))
  14255. (defun org-string-width (s)
  14256. "Compute width of string, ignoring invisible characters.
  14257. This ignores character with invisibility property `org-link', and also
  14258. characters with property `org-cwidth', because these will become invisible
  14259. upon the next fontification round."
  14260. (let (b l)
  14261. (when (or (eq t buffer-invisibility-spec)
  14262. (assq 'org-link buffer-invisibility-spec))
  14263. (while (setq b (text-property-any 0 (length s)
  14264. 'invisible 'org-link s))
  14265. (setq s (concat (substring s 0 b)
  14266. (substring s (or (next-single-property-change
  14267. b 'invisible s) (length s)))))))
  14268. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14269. (setq s (concat (substring s 0 b)
  14270. (substring s (or (next-single-property-change
  14271. b 'org-cwidth s) (length s))))))
  14272. (setq l (string-width s) b -1)
  14273. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14274. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14275. l))
  14276. (defun org-get-indentation (&optional line)
  14277. "Get the indentation of the current line, interpreting tabs.
  14278. When LINE is given, assume it represents a line and compute its indentation."
  14279. (if line
  14280. (if (string-match "^ *" (org-remove-tabs line))
  14281. (match-end 0))
  14282. (save-excursion
  14283. (beginning-of-line 1)
  14284. (skip-chars-forward " \t")
  14285. (current-column))))
  14286. (defun org-remove-tabs (s &optional width)
  14287. "Replace tabulators in S with spaces.
  14288. Assumes that s is a single line, starting in column 0."
  14289. (setq width (or width tab-width))
  14290. (while (string-match "\t" s)
  14291. (setq s (replace-match
  14292. (make-string
  14293. (- (* width (/ (+ (match-beginning 0) width) width))
  14294. (match-beginning 0)) ?\ )
  14295. t t s)))
  14296. s)
  14297. (defun org-fix-indentation (line ind)
  14298. "Fix indentation in LINE.
  14299. IND is a cons cell with target and minimum indentation.
  14300. If the current indentation in LINE is smaller than the minimum,
  14301. leave it alone. If it is larger than ind, set it to the target."
  14302. (let* ((l (org-remove-tabs line))
  14303. (i (org-get-indentation l))
  14304. (i1 (car ind)) (i2 (cdr ind)))
  14305. (if (>= i i2) (setq l (substring line i2)))
  14306. (if (> i1 0)
  14307. (concat (make-string i1 ?\ ) l)
  14308. l)))
  14309. (defun org-remove-indentation (code &optional n)
  14310. "Remove the maximum common indentation from the lines in CODE.
  14311. N may optionally be the number of spaces to remove."
  14312. (with-temp-buffer
  14313. (insert code)
  14314. (org-do-remove-indentation n)
  14315. (buffer-string)))
  14316. (defun org-do-remove-indentation (&optional n)
  14317. "Remove the maximum common indentation from the buffer."
  14318. (untabify (point-min) (point-max))
  14319. (let ((min 10000) re)
  14320. (if n
  14321. (setq min n)
  14322. (goto-char (point-min))
  14323. (while (re-search-forward "^ *[^ \n]" nil t)
  14324. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14325. (unless (or (= min 0) (= min 10000))
  14326. (setq re (format "^ \\{%d\\}" min))
  14327. (goto-char (point-min))
  14328. (while (re-search-forward re nil t)
  14329. (replace-match "")
  14330. (end-of-line 1))
  14331. min)))
  14332. (defun org-base-buffer (buffer)
  14333. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14334. (if (not buffer)
  14335. buffer
  14336. (or (buffer-base-buffer buffer)
  14337. buffer)))
  14338. (defun org-trim (s)
  14339. "Remove whitespace at beginning and end of string."
  14340. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14341. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14342. s)
  14343. (defun org-wrap (string &optional width lines)
  14344. "Wrap string to either a number of lines, or a width in characters.
  14345. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14346. that costs. If there is a word longer than WIDTH, the text is actually
  14347. wrapped to the length of that word.
  14348. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14349. many lines, whatever width that takes.
  14350. The return value is a list of lines, without newlines at the end."
  14351. (let* ((words (org-split-string string "[ \t\n]+"))
  14352. (maxword (apply 'max (mapcar 'org-string-width words)))
  14353. w ll)
  14354. (cond (width
  14355. (org-do-wrap words (max maxword width)))
  14356. (lines
  14357. (setq w maxword)
  14358. (setq ll (org-do-wrap words maxword))
  14359. (if (<= (length ll) lines)
  14360. ll
  14361. (setq ll words)
  14362. (while (> (length ll) lines)
  14363. (setq w (1+ w))
  14364. (setq ll (org-do-wrap words w)))
  14365. ll))
  14366. (t (error "Cannot wrap this")))))
  14367. (defun org-do-wrap (words width)
  14368. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14369. (let (lines line)
  14370. (while words
  14371. (setq line (pop words))
  14372. (while (and words (< (+ (length line) (length (car words))) width))
  14373. (setq line (concat line " " (pop words))))
  14374. (setq lines (push line lines)))
  14375. (nreverse lines)))
  14376. (defun org-split-string (string &optional separators)
  14377. "Splits STRING into substrings at SEPARATORS.
  14378. No empty strings are returned if there are matches at the beginning
  14379. and end of string."
  14380. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14381. (start 0)
  14382. notfirst
  14383. (list nil))
  14384. (while (and (string-match rexp string
  14385. (if (and notfirst
  14386. (= start (match-beginning 0))
  14387. (< start (length string)))
  14388. (1+ start) start))
  14389. (< (match-beginning 0) (length string)))
  14390. (setq notfirst t)
  14391. (or (eq (match-beginning 0) 0)
  14392. (and (eq (match-beginning 0) (match-end 0))
  14393. (eq (match-beginning 0) start))
  14394. (setq list
  14395. (cons (substring string start (match-beginning 0))
  14396. list)))
  14397. (setq start (match-end 0)))
  14398. (or (eq start (length string))
  14399. (setq list
  14400. (cons (substring string start)
  14401. list)))
  14402. (nreverse list)))
  14403. (defun org-uuidgen-p (s)
  14404. "Is S an ID created by UUIDGEN?"
  14405. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14406. (defun org-context ()
  14407. "Return a list of contexts of the current cursor position.
  14408. If several contexts apply, all are returned.
  14409. Each context entry is a list with a symbol naming the context, and
  14410. two positions indicating start and end of the context. Possible
  14411. contexts are:
  14412. :headline anywhere in a headline
  14413. :headline-stars on the leading stars in a headline
  14414. :todo-keyword on a TODO keyword (including DONE) in a headline
  14415. :tags on the TAGS in a headline
  14416. :priority on the priority cookie in a headline
  14417. :item on the first line of a plain list item
  14418. :item-bullet on the bullet/number of a plain list item
  14419. :checkbox on the checkbox in a plain list item
  14420. :table in an org-mode table
  14421. :table-special on a special filed in a table
  14422. :table-table in a table.el table
  14423. :link on a hyperlink
  14424. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14425. :target on a <<target>>
  14426. :radio-target on a <<<radio-target>>>
  14427. :latex-fragment on a LaTeX fragment
  14428. :latex-preview on a LaTeX fragment with overlayed preview image
  14429. This function expects the position to be visible because it uses font-lock
  14430. faces as a help to recognize the following contexts: :table-special, :link,
  14431. and :keyword."
  14432. (let* ((f (get-text-property (point) 'face))
  14433. (faces (if (listp f) f (list f)))
  14434. (p (point)) clist o)
  14435. ;; First the large context
  14436. (cond
  14437. ((org-on-heading-p t)
  14438. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14439. (when (progn
  14440. (beginning-of-line 1)
  14441. (looking-at org-todo-line-tags-regexp))
  14442. (push (org-point-in-group p 1 :headline-stars) clist)
  14443. (push (org-point-in-group p 2 :todo-keyword) clist)
  14444. (push (org-point-in-group p 4 :tags) clist))
  14445. (goto-char p)
  14446. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14447. (if (looking-at "\\[#[A-Z0-9]\\]")
  14448. (push (org-point-in-group p 0 :priority) clist)))
  14449. ((org-at-item-p)
  14450. (push (org-point-in-group p 2 :item-bullet) clist)
  14451. (push (list :item (point-at-bol)
  14452. (save-excursion (org-end-of-item) (point)))
  14453. clist)
  14454. (and (org-at-item-checkbox-p)
  14455. (push (org-point-in-group p 0 :checkbox) clist)))
  14456. ((org-at-table-p)
  14457. (push (list :table (org-table-begin) (org-table-end)) clist)
  14458. (if (memq 'org-formula faces)
  14459. (push (list :table-special
  14460. (previous-single-property-change p 'face)
  14461. (next-single-property-change p 'face)) clist)))
  14462. ((org-at-table-p 'any)
  14463. (push (list :table-table) clist)))
  14464. (goto-char p)
  14465. ;; Now the small context
  14466. (cond
  14467. ((org-at-timestamp-p)
  14468. (push (org-point-in-group p 0 :timestamp) clist))
  14469. ((memq 'org-link faces)
  14470. (push (list :link
  14471. (previous-single-property-change p 'face)
  14472. (next-single-property-change p 'face)) clist))
  14473. ((memq 'org-special-keyword faces)
  14474. (push (list :keyword
  14475. (previous-single-property-change p 'face)
  14476. (next-single-property-change p 'face)) clist))
  14477. ((org-on-target-p)
  14478. (push (org-point-in-group p 0 :target) clist)
  14479. (goto-char (1- (match-beginning 0)))
  14480. (if (looking-at org-radio-target-regexp)
  14481. (push (org-point-in-group p 0 :radio-target) clist))
  14482. (goto-char p))
  14483. ((setq o (car (delq nil
  14484. (mapcar
  14485. (lambda (x)
  14486. (if (memq x org-latex-fragment-image-overlays) x))
  14487. (org-overlays-at (point))))))
  14488. (push (list :latex-fragment
  14489. (org-overlay-start o) (org-overlay-end o)) clist)
  14490. (push (list :latex-preview
  14491. (org-overlay-start o) (org-overlay-end o)) clist))
  14492. ((org-inside-LaTeX-fragment-p)
  14493. ;; FIXME: positions wrong.
  14494. (push (list :latex-fragment (point) (point)) clist)))
  14495. (setq clist (nreverse (delq nil clist)))
  14496. clist))
  14497. ;; FIXME: Compare with at-regexp-p Do we need both?
  14498. (defun org-in-regexp (re &optional nlines visually)
  14499. "Check if point is inside a match of regexp.
  14500. Normally only the current line is checked, but you can include NLINES extra
  14501. lines both before and after point into the search.
  14502. If VISUALLY is set, require that the cursor is not after the match but
  14503. really on, so that the block visually is on the match."
  14504. (catch 'exit
  14505. (let ((pos (point))
  14506. (eol (point-at-eol (+ 1 (or nlines 0))))
  14507. (inc (if visually 1 0)))
  14508. (save-excursion
  14509. (beginning-of-line (- 1 (or nlines 0)))
  14510. (while (re-search-forward re eol t)
  14511. (if (and (<= (match-beginning 0) pos)
  14512. (>= (+ inc (match-end 0)) pos))
  14513. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14514. (defun org-at-regexp-p (regexp)
  14515. "Is point inside a match of REGEXP in the current line?"
  14516. (catch 'exit
  14517. (save-excursion
  14518. (let ((pos (point)) (end (point-at-eol)))
  14519. (beginning-of-line 1)
  14520. (while (re-search-forward regexp end t)
  14521. (if (and (<= (match-beginning 0) pos)
  14522. (>= (match-end 0) pos))
  14523. (throw 'exit t)))
  14524. nil))))
  14525. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14526. "Call `multi-occur' with buffers for all agenda files."
  14527. (interactive "sOrg-files matching: \np")
  14528. (let* ((files (org-agenda-files))
  14529. (tnames (mapcar 'file-truename files))
  14530. (extra org-agenda-text-search-extra-files)
  14531. f)
  14532. (when (eq (car extra) 'agenda-archives)
  14533. (setq extra (cdr extra))
  14534. (setq files (org-add-archive-files files)))
  14535. (while (setq f (pop extra))
  14536. (unless (member (file-truename f) tnames)
  14537. (add-to-list 'files f 'append)
  14538. (add-to-list 'tnames (file-truename f) 'append)))
  14539. (multi-occur
  14540. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14541. regexp)))
  14542. (if (boundp 'occur-mode-find-occurrence-hook)
  14543. ;; Emacs 23
  14544. (add-hook 'occur-mode-find-occurrence-hook
  14545. (lambda ()
  14546. (when (org-mode-p)
  14547. (org-reveal))))
  14548. ;; Emacs 22
  14549. (defadvice occur-mode-goto-occurrence
  14550. (after org-occur-reveal activate)
  14551. (and (org-mode-p) (org-reveal)))
  14552. (defadvice occur-mode-goto-occurrence-other-window
  14553. (after org-occur-reveal activate)
  14554. (and (org-mode-p) (org-reveal)))
  14555. (defadvice occur-mode-display-occurrence
  14556. (after org-occur-reveal activate)
  14557. (when (org-mode-p)
  14558. (let ((pos (occur-mode-find-occurrence)))
  14559. (with-current-buffer (marker-buffer pos)
  14560. (save-excursion
  14561. (goto-char pos)
  14562. (org-reveal)))))))
  14563. (defun org-uniquify (list)
  14564. "Remove duplicate elements from LIST."
  14565. (let (res)
  14566. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14567. res))
  14568. (defun org-delete-all (elts list)
  14569. "Remove all elements in ELTS from LIST."
  14570. (while elts
  14571. (setq list (delete (pop elts) list)))
  14572. list)
  14573. (defun org-back-over-empty-lines ()
  14574. "Move backwards over whitespace, to the beginning of the first empty line.
  14575. Returns the number of empty lines passed."
  14576. (let ((pos (point)))
  14577. (skip-chars-backward " \t\n\r")
  14578. (beginning-of-line 2)
  14579. (goto-char (min (point) pos))
  14580. (count-lines (point) pos)))
  14581. (defun org-skip-whitespace ()
  14582. (skip-chars-forward " \t\n\r"))
  14583. (defun org-point-in-group (point group &optional context)
  14584. "Check if POINT is in match-group GROUP.
  14585. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14586. match. If the match group does ot exist or point is not inside it,
  14587. return nil."
  14588. (and (match-beginning group)
  14589. (>= point (match-beginning group))
  14590. (<= point (match-end group))
  14591. (if context
  14592. (list context (match-beginning group) (match-end group))
  14593. t)))
  14594. (defun org-switch-to-buffer-other-window (&rest args)
  14595. "Switch to buffer in a second window on the current frame.
  14596. In particular, do not allow pop-up frames."
  14597. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14598. special-display-function)
  14599. (apply 'switch-to-buffer-other-window args)))
  14600. (defun org-combine-plists (&rest plists)
  14601. "Create a single property list from all plists in PLISTS.
  14602. The process starts by copying the first list, and then setting properties
  14603. from the other lists. Settings in the last list are the most significant
  14604. ones and overrule settings in the other lists."
  14605. (let ((rtn (copy-sequence (pop plists)))
  14606. p v ls)
  14607. (while plists
  14608. (setq ls (pop plists))
  14609. (while ls
  14610. (setq p (pop ls) v (pop ls))
  14611. (setq rtn (plist-put rtn p v))))
  14612. rtn))
  14613. (defun org-move-line-down (arg)
  14614. "Move the current line down. With prefix argument, move it past ARG lines."
  14615. (interactive "p")
  14616. (let ((col (current-column))
  14617. beg end pos)
  14618. (beginning-of-line 1) (setq beg (point))
  14619. (beginning-of-line 2) (setq end (point))
  14620. (beginning-of-line (+ 1 arg))
  14621. (setq pos (move-marker (make-marker) (point)))
  14622. (insert (delete-and-extract-region beg end))
  14623. (goto-char pos)
  14624. (org-move-to-column col)))
  14625. (defun org-move-line-up (arg)
  14626. "Move the current line up. With prefix argument, move it past ARG lines."
  14627. (interactive "p")
  14628. (let ((col (current-column))
  14629. beg end pos)
  14630. (beginning-of-line 1) (setq beg (point))
  14631. (beginning-of-line 2) (setq end (point))
  14632. (beginning-of-line (- arg))
  14633. (setq pos (move-marker (make-marker) (point)))
  14634. (insert (delete-and-extract-region beg end))
  14635. (goto-char pos)
  14636. (org-move-to-column col)))
  14637. (defun org-replace-escapes (string table)
  14638. "Replace %-escapes in STRING with values in TABLE.
  14639. TABLE is an association list with keys like \"%a\" and string values.
  14640. The sequences in STRING may contain normal field width and padding information,
  14641. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14642. so values can contain further %-escapes if they are define later in TABLE."
  14643. (let ((case-fold-search nil)
  14644. e re rpl)
  14645. (while (setq e (pop table))
  14646. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14647. (while (string-match re string)
  14648. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14649. (cdr e)))
  14650. (setq string (replace-match rpl t t string))))
  14651. string))
  14652. (defun org-sublist (list start end)
  14653. "Return a section of LIST, from START to END.
  14654. Counting starts at 1."
  14655. (let (rtn (c start))
  14656. (setq list (nthcdr (1- start) list))
  14657. (while (and list (<= c end))
  14658. (push (pop list) rtn)
  14659. (setq c (1+ c)))
  14660. (nreverse rtn)))
  14661. (defun org-find-base-buffer-visiting (file)
  14662. "Like `find-buffer-visiting' but always return the base buffer and
  14663. not an indirect buffer."
  14664. (let ((buf (or (get-file-buffer file)
  14665. (find-buffer-visiting file))))
  14666. (if buf
  14667. (or (buffer-base-buffer buf) buf)
  14668. nil)))
  14669. (defun org-image-file-name-regexp (&optional extensions)
  14670. "Return regexp matching the file names of images.
  14671. If EXTENSIONS is given, only match these."
  14672. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14673. (image-file-name-regexp)
  14674. (let ((image-file-name-extensions
  14675. (or extensions
  14676. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14677. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14678. (concat "\\."
  14679. (regexp-opt (nconc (mapcar 'upcase
  14680. image-file-name-extensions)
  14681. image-file-name-extensions)
  14682. t)
  14683. "\\'"))))
  14684. (defun org-file-image-p (file &optional extensions)
  14685. "Return non-nil if FILE is an image."
  14686. (save-match-data
  14687. (string-match (org-image-file-name-regexp extensions) file)))
  14688. (defun org-get-cursor-date ()
  14689. "Return the date at cursor in as a time.
  14690. This works in the calendar and in the agenda, anywhere else it just
  14691. returns the current time."
  14692. (let (date day defd)
  14693. (cond
  14694. ((eq major-mode 'calendar-mode)
  14695. (setq date (calendar-cursor-to-date)
  14696. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14697. ((eq major-mode 'org-agenda-mode)
  14698. (setq day (get-text-property (point) 'day))
  14699. (if day
  14700. (setq date (calendar-gregorian-from-absolute day)
  14701. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14702. (nth 2 date))))))
  14703. (or defd (current-time))))
  14704. (defvar org-agenda-action-marker (make-marker)
  14705. "Marker pointing to the entry for the next agenda action.")
  14706. (defun org-mark-entry-for-agenda-action ()
  14707. "Mark the current entry as target of an agenda action.
  14708. Agenda actions are actions executed from the agenda with the key `k',
  14709. which make use of the date at the cursor."
  14710. (interactive)
  14711. (move-marker org-agenda-action-marker
  14712. (save-excursion (org-back-to-heading t) (point))
  14713. (current-buffer))
  14714. (message
  14715. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14716. ;;; Paragraph filling stuff.
  14717. ;; We want this to be just right, so use the full arsenal.
  14718. (defun org-indent-line-function ()
  14719. "Indent line like previous, but further if previous was headline or item."
  14720. (interactive)
  14721. (let* ((pos (point))
  14722. (itemp (org-at-item-p))
  14723. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14724. column bpos bcol tpos tcol bullet btype bullet-type)
  14725. ;; Find the previous relevant line
  14726. (beginning-of-line 1)
  14727. (cond
  14728. ((looking-at "#") (setq column 0))
  14729. ((looking-at "\\*+ ") (setq column 0))
  14730. ((and (looking-at "[ \t]*:END:")
  14731. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14732. (save-excursion
  14733. (goto-char (1- (match-beginning 1)))
  14734. (setq column (current-column))))
  14735. (t
  14736. (beginning-of-line 0)
  14737. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14738. (not (looking-at "[ \t]*:END:"))
  14739. (not (looking-at org-drawer-regexp)))
  14740. (beginning-of-line 0))
  14741. (cond
  14742. ((looking-at "\\*+[ \t]+")
  14743. (if (not org-adapt-indentation)
  14744. (setq column 0)
  14745. (goto-char (match-end 0))
  14746. (setq column (current-column))))
  14747. ((looking-at org-drawer-regexp)
  14748. (goto-char (1- (match-beginning 1)))
  14749. (setq column (current-column)))
  14750. ((looking-at "\\([ \t]*\\):END:")
  14751. (goto-char (match-end 1))
  14752. (setq column (current-column)))
  14753. ((org-in-item-p)
  14754. (org-beginning-of-item)
  14755. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14756. (setq bpos (match-beginning 1) tpos (match-end 0)
  14757. bcol (progn (goto-char bpos) (current-column))
  14758. tcol (progn (goto-char tpos) (current-column))
  14759. bullet (match-string 1)
  14760. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14761. (if (> tcol (+ bcol org-description-max-indent))
  14762. (setq tcol (+ bcol 5)))
  14763. (if (not itemp)
  14764. (setq column tcol)
  14765. (goto-char pos)
  14766. (beginning-of-line 1)
  14767. (if (looking-at "\\S-")
  14768. (progn
  14769. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14770. (setq bullet (match-string 1)
  14771. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14772. (setq column (if (equal btype bullet-type) bcol tcol)))
  14773. (setq column (org-get-indentation)))))
  14774. (t (setq column (org-get-indentation))))))
  14775. (goto-char pos)
  14776. (if (<= (current-column) (current-indentation))
  14777. (org-indent-line-to column)
  14778. (save-excursion (org-indent-line-to column)))
  14779. (setq column (current-column))
  14780. (beginning-of-line 1)
  14781. (if (looking-at
  14782. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14783. (replace-match (concat "\\1" (format org-property-format
  14784. (match-string 2) (match-string 3)))
  14785. t nil))
  14786. (org-move-to-column column)))
  14787. (defun org-set-autofill-regexps ()
  14788. (interactive)
  14789. ;; In the paragraph separator we include headlines, because filling
  14790. ;; text in a line directly attached to a headline would otherwise
  14791. ;; fill the headline as well.
  14792. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14793. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14794. ;; The paragraph starter includes hand-formatted lists.
  14795. (org-set-local 'paragraph-start
  14796. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14797. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14798. ;; But only if the user has not turned off tables or fixed-width regions
  14799. (org-set-local
  14800. 'auto-fill-inhibit-regexp
  14801. (concat "\\*+ \\|#\\+"
  14802. "\\|[ \t]*" org-keyword-time-regexp
  14803. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14804. (concat
  14805. "\\|[ \t]*["
  14806. (if org-enable-table-editor "|" "")
  14807. (if org-enable-fixed-width-editor ":" "")
  14808. "]"))))
  14809. ;; We use our own fill-paragraph function, to make sure that tables
  14810. ;; and fixed-width regions are not wrapped. That function will pass
  14811. ;; through to `fill-paragraph' when appropriate.
  14812. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14813. ; Adaptive filling: To get full control, first make sure that
  14814. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14815. (org-set-local 'adaptive-fill-regexp "\000")
  14816. (org-set-local 'adaptive-fill-function
  14817. 'org-adaptive-fill-function)
  14818. (org-set-local
  14819. 'align-mode-rules-list
  14820. '((org-in-buffer-settings
  14821. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14822. (modes . '(org-mode))))))
  14823. (defun org-fill-paragraph (&optional justify)
  14824. "Re-align a table, pass through to fill-paragraph if no table."
  14825. (let ((table-p (org-at-table-p))
  14826. (table.el-p (org-at-table.el-p)))
  14827. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14828. (save-excursion (goto-char (point-at-bol))
  14829. (looking-at outline-regexp)))
  14830. t) ; skip headlines
  14831. (table.el-p t) ; skip table.el tables
  14832. (table-p (org-table-align) t) ; align org-mode tables
  14833. (t nil)))) ; call paragraph-fill
  14834. ;; For reference, this is the default value of adaptive-fill-regexp
  14835. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14836. (defun org-adaptive-fill-function ()
  14837. "Return a fill prefix for org-mode files.
  14838. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14839. work correctly."
  14840. (cond ((looking-at "#[ \t]+")
  14841. (match-string 0))
  14842. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14843. (save-excursion
  14844. (if (> (match-end 1) (+ (match-beginning 1)
  14845. org-description-max-indent))
  14846. (goto-char (+ (match-beginning 1) 5))
  14847. (goto-char (match-end 0)))
  14848. (make-string (current-column) ?\ )))
  14849. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14850. (save-excursion
  14851. (goto-char (match-end 0))
  14852. (make-string (current-column) ?\ )))
  14853. (t nil)))
  14854. ;;; Other stuff.
  14855. (defun org-toggle-fixed-width-section (arg)
  14856. "Toggle the fixed-width export.
  14857. If there is no active region, the QUOTE keyword at the current headline is
  14858. inserted or removed. When present, it causes the text between this headline
  14859. and the next to be exported as fixed-width text, and unmodified.
  14860. If there is an active region, this command adds or removes a colon as the
  14861. first character of this line. If the first character of a line is a colon,
  14862. this line is also exported in fixed-width font."
  14863. (interactive "P")
  14864. (let* ((cc 0)
  14865. (regionp (org-region-active-p))
  14866. (beg (if regionp (region-beginning) (point)))
  14867. (end (if regionp (region-end)))
  14868. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14869. (case-fold-search nil)
  14870. (re "[ \t]*\\(: \\)")
  14871. off)
  14872. (if regionp
  14873. (save-excursion
  14874. (goto-char beg)
  14875. (setq cc (current-column))
  14876. (beginning-of-line 1)
  14877. (setq off (looking-at re))
  14878. (while (> nlines 0)
  14879. (setq nlines (1- nlines))
  14880. (beginning-of-line 1)
  14881. (cond
  14882. (arg
  14883. (org-move-to-column cc t)
  14884. (insert ": \n")
  14885. (forward-line -1))
  14886. ((and off (looking-at re))
  14887. (replace-match "" t t nil 1))
  14888. ((not off) (org-move-to-column cc t) (insert ": ")))
  14889. (forward-line 1)))
  14890. (save-excursion
  14891. (org-back-to-heading)
  14892. (if (looking-at (concat outline-regexp
  14893. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14894. (replace-match "" t t nil 1)
  14895. (if (looking-at outline-regexp)
  14896. (progn
  14897. (goto-char (match-end 0))
  14898. (insert org-quote-string " "))))))))
  14899. (defun org-reftex-citation ()
  14900. "Use reftex-citation to insert a citation into the buffer.
  14901. This looks for a line like
  14902. #+BIBLIOGRAPHY: foo plain option:-d
  14903. and derives from it that foo.bib is the bbliography file relevant
  14904. for this document. It then installs the necessary environment for RefTeX
  14905. to work in this buffer and calls `reftex-citation' to insert a citation
  14906. into the buffer.
  14907. Export of such citations to both LaTeX and HTML is handled by the contributed
  14908. package org-exp-bibtex by Taru Karttunen."
  14909. (interactive)
  14910. (let ((reftex-docstruct-symbol 'rds)
  14911. (reftex-cite-format "\\cite{%l}")
  14912. rds bib)
  14913. (save-excursion
  14914. (save-restriction
  14915. (widen)
  14916. (let ((case-fold-search t)
  14917. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14918. (if (not (save-excursion
  14919. (or (re-search-forward re nil t)
  14920. (re-search-backward re nil t))))
  14921. (error "No bibliography defined in file")
  14922. (setq bib (concat (match-string 1) ".bib")
  14923. rds (list (list 'bib bib)))))))
  14924. (call-interactively 'reftex-citation)))
  14925. ;;;; Functions extending outline functionality
  14926. (defun org-beginning-of-line (&optional arg)
  14927. "Go to the beginning of the current line. If that is invisible, continue
  14928. to a visible line beginning. This makes the function of C-a more intuitive.
  14929. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14930. first attempt, and only move to after the tags when the cursor is already
  14931. beyond the end of the headline."
  14932. (interactive "P")
  14933. (let ((pos (point))
  14934. (special (if (consp org-special-ctrl-a/e)
  14935. (car org-special-ctrl-a/e)
  14936. org-special-ctrl-a/e))
  14937. refpos)
  14938. (beginning-of-line 1)
  14939. (if (and arg (fboundp 'move-beginning-of-line))
  14940. (call-interactively 'move-beginning-of-line)
  14941. (if (bobp)
  14942. nil
  14943. (backward-char 1)
  14944. (if (org-invisible-p)
  14945. (while (and (not (bobp)) (org-invisible-p))
  14946. (backward-char 1)
  14947. (beginning-of-line 1))
  14948. (forward-char 1))))
  14949. (when special
  14950. (cond
  14951. ((and (looking-at org-complex-heading-regexp)
  14952. (= (char-after (match-end 1)) ?\ ))
  14953. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14954. (point-at-eol)))
  14955. (goto-char
  14956. (if (eq special t)
  14957. (cond ((> pos refpos) refpos)
  14958. ((= pos (point)) refpos)
  14959. (t (point)))
  14960. (cond ((> pos (point)) (point))
  14961. ((not (eq last-command this-command)) (point))
  14962. (t refpos)))))
  14963. ((org-at-item-p)
  14964. (goto-char
  14965. (if (eq special t)
  14966. (cond ((> pos (match-end 4)) (match-end 4))
  14967. ((= pos (point)) (match-end 4))
  14968. (t (point)))
  14969. (cond ((> pos (point)) (point))
  14970. ((not (eq last-command this-command)) (point))
  14971. (t (match-end 4))))))))
  14972. (org-no-warnings
  14973. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14974. (defun org-end-of-line (&optional arg)
  14975. "Go to the end of the line.
  14976. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14977. first attempt, and only move to after the tags when the cursor is already
  14978. beyond the end of the headline."
  14979. (interactive "P")
  14980. (let ((special (if (consp org-special-ctrl-a/e)
  14981. (cdr org-special-ctrl-a/e)
  14982. org-special-ctrl-a/e)))
  14983. (if (or (not special)
  14984. (not (org-on-heading-p))
  14985. arg)
  14986. (call-interactively (if (fboundp 'move-end-of-line)
  14987. 'move-end-of-line
  14988. 'end-of-line))
  14989. (let ((pos (point)))
  14990. (beginning-of-line 1)
  14991. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14992. (if (eq special t)
  14993. (if (or (< pos (match-beginning 1))
  14994. (= pos (match-end 0)))
  14995. (goto-char (match-beginning 1))
  14996. (goto-char (match-end 0)))
  14997. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14998. (goto-char (match-end 0))
  14999. (goto-char (match-beginning 1))))
  15000. (call-interactively (if (fboundp 'move-end-of-line)
  15001. 'move-end-of-line
  15002. 'end-of-line)))))
  15003. (org-no-warnings
  15004. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15005. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15006. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15007. (defun org-backward-sentence (&optional arg)
  15008. "Go to beginning of sentence, or beginning of table field.
  15009. This will call `backward-sentence' or `org-table-beginning-of-field',
  15010. depending on context."
  15011. (interactive "P")
  15012. (cond
  15013. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15014. (t (call-interactively 'backward-sentence))))
  15015. (defun org-forward-sentence (&optional arg)
  15016. "Go to end of sentence, or end of table field.
  15017. This will call `forward-sentence' or `org-table-end-of-field',
  15018. depending on context."
  15019. (interactive "P")
  15020. (cond
  15021. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15022. (t (call-interactively 'forward-sentence))))
  15023. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15024. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15025. (defun org-kill-line (&optional arg)
  15026. "Kill line, to tags or end of line."
  15027. (interactive "P")
  15028. (cond
  15029. ((or (not org-special-ctrl-k)
  15030. (bolp)
  15031. (not (org-on-heading-p)))
  15032. (call-interactively 'kill-line))
  15033. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15034. (kill-region (point) (match-beginning 1))
  15035. (org-set-tags nil t))
  15036. (t (kill-region (point) (point-at-eol)))))
  15037. (define-key org-mode-map "\C-k" 'org-kill-line)
  15038. (defun org-yank (&optional arg)
  15039. "Yank. If the kill is a subtree, treat it specially.
  15040. This command will look at the current kill and check if is a single
  15041. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15042. cursor is at the beginning of a line or after the stars of a currently
  15043. empty headline, then the yank is handled specially. How exactly depends
  15044. on the value of the following variables, both set by default.
  15045. org-yank-folded-subtrees
  15046. When set, the subtree(s) will be folded after insertion, but only
  15047. if doing so would now swallow text after the yanked text.
  15048. org-yank-adjusted-subtrees
  15049. When set, the subtree will be promoted or demoted in order to
  15050. fit into the local outline tree structure, which means that the level
  15051. will be adjusted so that it becomes the smaller one of the two
  15052. *visible* surrounding headings.
  15053. Any prefix to this command will cause `yank' to be called directly with
  15054. no special treatment. In particular, a simple `C-u' prefix will just
  15055. plainly yank the text as it is.
  15056. \[1] The test checks if the first non-white line is a heading
  15057. and if there are no other headings with fewer stars."
  15058. (interactive "P")
  15059. (org-yank-generic 'yank arg))
  15060. (defun org-yank-generic (command arg)
  15061. "Perform some yank-like command.
  15062. This function implements the behavior described in the `org-yank'
  15063. documentation. However, it has been generalized to work for any
  15064. interactive command with similar behavior."
  15065. ;; pretend to be command COMMAND
  15066. (setq this-command command)
  15067. (if arg
  15068. (call-interactively command)
  15069. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15070. (and (org-kill-is-subtree-p)
  15071. (or (bolp)
  15072. (and (looking-at "[ \t]*$")
  15073. (string-match
  15074. "\\`\\*+\\'"
  15075. (buffer-substring (point-at-bol) (point)))))))
  15076. swallowp)
  15077. (cond
  15078. ((and subtreep org-yank-folded-subtrees)
  15079. (let ((beg (point))
  15080. end)
  15081. (if (and subtreep org-yank-adjusted-subtrees)
  15082. (org-paste-subtree nil nil 'for-yank)
  15083. (call-interactively command))
  15084. (setq end (point))
  15085. (goto-char beg)
  15086. (when (and (bolp) subtreep
  15087. (not (setq swallowp
  15088. (org-yank-folding-would-swallow-text beg end))))
  15089. (or (looking-at outline-regexp)
  15090. (re-search-forward (concat "^" outline-regexp) end t))
  15091. (while (and (< (point) end) (looking-at outline-regexp))
  15092. (hide-subtree)
  15093. (org-cycle-show-empty-lines 'folded)
  15094. (condition-case nil
  15095. (outline-forward-same-level 1)
  15096. (error (goto-char end)))))
  15097. (when swallowp
  15098. (message
  15099. "Inserted text not folded because that would swallow text"))
  15100. (goto-char end)
  15101. (skip-chars-forward " \t\n\r")
  15102. (beginning-of-line 1)
  15103. (push-mark beg 'nomsg)))
  15104. ((and subtreep org-yank-adjusted-subtrees)
  15105. (let ((beg (point-at-bol)))
  15106. (org-paste-subtree nil nil 'for-yank)
  15107. (push-mark beg 'nomsg)))
  15108. (t
  15109. (call-interactively command))))))
  15110. (defun org-yank-folding-would-swallow-text (beg end)
  15111. "Would hide-subtree at BEG swallow any text after END?"
  15112. (let (level)
  15113. (save-excursion
  15114. (goto-char beg)
  15115. (when (or (looking-at outline-regexp)
  15116. (re-search-forward (concat "^" outline-regexp) end t))
  15117. (setq level (org-outline-level)))
  15118. (goto-char end)
  15119. (skip-chars-forward " \t\r\n\v\f")
  15120. (if (or (eobp)
  15121. (and (bolp) (looking-at org-outline-regexp)
  15122. (<= (org-outline-level) level)))
  15123. nil ; Nothing would be swallowed
  15124. t)))) ; something would swallow
  15125. (define-key org-mode-map "\C-y" 'org-yank)
  15126. (defun org-invisible-p ()
  15127. "Check if point is at a character currently not visible."
  15128. ;; Early versions of noutline don't have `outline-invisible-p'.
  15129. (if (fboundp 'outline-invisible-p)
  15130. (outline-invisible-p)
  15131. (get-char-property (point) 'invisible)))
  15132. (defun org-invisible-p2 ()
  15133. "Check if point is at a character currently not visible."
  15134. (save-excursion
  15135. (if (and (eolp) (not (bobp))) (backward-char 1))
  15136. ;; Early versions of noutline don't have `outline-invisible-p'.
  15137. (if (fboundp 'outline-invisible-p)
  15138. (outline-invisible-p)
  15139. (get-char-property (point) 'invisible))))
  15140. (defun org-back-to-heading (&optional invisible-ok)
  15141. "Call `outline-back-to-heading', but provide a better error message."
  15142. (condition-case nil
  15143. (outline-back-to-heading invisible-ok)
  15144. (error (error "Before first headline at position %d in buffer %s"
  15145. (point) (current-buffer)))))
  15146. (defun org-before-first-heading-p ()
  15147. "Before first heading?"
  15148. (save-excursion
  15149. (null (re-search-backward "^\\*+ " nil t))))
  15150. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15151. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15152. (defun org-at-heading-or-item-p ()
  15153. (or (org-on-heading-p) (org-at-item-p)))
  15154. (defun org-on-target-p ()
  15155. (or (org-in-regexp org-radio-target-regexp)
  15156. (org-in-regexp org-target-regexp)))
  15157. (defun org-up-heading-all (arg)
  15158. "Move to the heading line of which the present line is a subheading.
  15159. This function considers both visible and invisible heading lines.
  15160. With argument, move up ARG levels."
  15161. (if (fboundp 'outline-up-heading-all)
  15162. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15163. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15164. (defun org-up-heading-safe ()
  15165. "Move to the heading line of which the present line is a subheading.
  15166. This version will not throw an error. It will return the level of the
  15167. headline found, or nil if no higher level is found.
  15168. Also, this function will be a lot faster than `outline-up-heading',
  15169. because it relies on stars being the outline starters. This can really
  15170. make a significant difference in outlines with very many siblings."
  15171. (let (start-level re)
  15172. (org-back-to-heading t)
  15173. (setq start-level (funcall outline-level))
  15174. (if (equal start-level 1)
  15175. nil
  15176. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15177. (if (re-search-backward re nil t)
  15178. (funcall outline-level)))))
  15179. (defun org-first-sibling-p ()
  15180. "Is this heading the first child of its parents?"
  15181. (interactive)
  15182. (let ((re (concat "^" outline-regexp))
  15183. level l)
  15184. (unless (org-at-heading-p t)
  15185. (error "Not at a heading"))
  15186. (setq level (funcall outline-level))
  15187. (save-excursion
  15188. (if (not (re-search-backward re nil t))
  15189. t
  15190. (setq l (funcall outline-level))
  15191. (< l level)))))
  15192. (defun org-goto-sibling (&optional previous)
  15193. "Goto the next sibling, even if it is invisible.
  15194. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15195. when a sibling was found. When none is found, return nil and don't
  15196. move point."
  15197. (let ((fun (if previous 're-search-backward 're-search-forward))
  15198. (pos (point))
  15199. (re (concat "^" outline-regexp))
  15200. level l)
  15201. (when (condition-case nil (org-back-to-heading t) (error nil))
  15202. (setq level (funcall outline-level))
  15203. (catch 'exit
  15204. (or previous (forward-char 1))
  15205. (while (funcall fun re nil t)
  15206. (setq l (funcall outline-level))
  15207. (when (< l level) (goto-char pos) (throw 'exit nil))
  15208. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15209. (goto-char pos)
  15210. nil))))
  15211. (defun org-show-siblings ()
  15212. "Show all siblings of the current headline."
  15213. (save-excursion
  15214. (while (org-goto-sibling) (org-flag-heading nil)))
  15215. (save-excursion
  15216. (while (org-goto-sibling 'previous)
  15217. (org-flag-heading nil))))
  15218. (defun org-show-hidden-entry ()
  15219. "Show an entry where even the heading is hidden."
  15220. (save-excursion
  15221. (org-show-entry)))
  15222. (defun org-flag-heading (flag &optional entry)
  15223. "Flag the current heading. FLAG non-nil means make invisible.
  15224. When ENTRY is non-nil, show the entire entry."
  15225. (save-excursion
  15226. (org-back-to-heading t)
  15227. ;; Check if we should show the entire entry
  15228. (if entry
  15229. (progn
  15230. (org-show-entry)
  15231. (save-excursion
  15232. (and (outline-next-heading)
  15233. (org-flag-heading nil))))
  15234. (outline-flag-region (max (point-min) (1- (point)))
  15235. (save-excursion (outline-end-of-heading) (point))
  15236. flag))))
  15237. (defun org-forward-same-level (arg)
  15238. "Move forward to the ARG'th subheading at same level as this one.
  15239. Stop at the first and last subheadings of a superior heading.
  15240. This is like outline-forward-same-level, but invisible headings are ok."
  15241. (interactive "p")
  15242. (org-back-to-heading t)
  15243. (while (> arg 0)
  15244. (let ((point-to-move-to (save-excursion
  15245. (org-get-next-sibling))))
  15246. (if point-to-move-to
  15247. (progn
  15248. (goto-char point-to-move-to)
  15249. (setq arg (1- arg)))
  15250. (progn
  15251. (setq arg 0)
  15252. (error "No following same-level heading"))))))
  15253. (defun org-get-next-sibling ()
  15254. "Move to next heading of the same level, and return point.
  15255. If there is no such heading, return nil.
  15256. This is like outline-next-sibling, but invisible headings are ok."
  15257. (let ((level (funcall outline-level)))
  15258. (outline-next-heading)
  15259. (while (and (not (eobp)) (> (funcall outline-level) level))
  15260. (outline-next-heading))
  15261. (if (or (eobp) (< (funcall outline-level) level))
  15262. nil
  15263. (point))))
  15264. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15265. ;; This contains an exact copy of the original function, but it uses
  15266. ;; `org-back-to-heading', to make it work also in invisible
  15267. ;; trees. And is uses an invisible-OK argument.
  15268. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15269. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15270. ;; with many children and grandchildren etc, this can be much faster
  15271. ;; than the outline version.
  15272. (org-back-to-heading invisible-OK)
  15273. (let ((first t)
  15274. (level (funcall outline-level)))
  15275. (if (and (org-mode-p) (< level 1000))
  15276. ;; A true heading (not a plain list item), in Org-mode
  15277. ;; This means we can easily find the end by looking
  15278. ;; only for the right number of stars. Using a regexp to do
  15279. ;; this is so much faster than using a Lisp loop.
  15280. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15281. (forward-char 1)
  15282. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15283. ;; something else, do it the slow way
  15284. (while (and (not (eobp))
  15285. (or first (> (funcall outline-level) level)))
  15286. (setq first nil)
  15287. (outline-next-heading)))
  15288. (unless to-heading
  15289. (if (memq (preceding-char) '(?\n ?\^M))
  15290. (progn
  15291. ;; Go to end of line before heading
  15292. (forward-char -1)
  15293. (if (memq (preceding-char) '(?\n ?\^M))
  15294. ;; leave blank line before heading
  15295. (forward-char -1))))))
  15296. (point))
  15297. (defun org-show-subtree ()
  15298. "Show everything after this heading at deeper levels."
  15299. (outline-flag-region
  15300. (point)
  15301. (save-excursion
  15302. (outline-end-of-subtree) (outline-next-heading) (point))
  15303. nil))
  15304. (defun org-show-entry ()
  15305. "Show the body directly following this heading.
  15306. Show the heading too, if it is currently invisible."
  15307. (interactive)
  15308. (save-excursion
  15309. (condition-case nil
  15310. (progn
  15311. (org-back-to-heading t)
  15312. (outline-flag-region
  15313. (max (point-min) (1- (point)))
  15314. (save-excursion
  15315. (if (re-search-forward
  15316. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15317. (match-beginning 1)
  15318. (point-max)))
  15319. nil))
  15320. (error nil))))
  15321. (defun org-make-options-regexp (kwds &optional extra)
  15322. "Make a regular expression for keyword lines."
  15323. (concat
  15324. "^"
  15325. "#?[ \t]*\\+\\("
  15326. (mapconcat 'regexp-quote kwds "\\|")
  15327. (if extra (concat "\\|" extra))
  15328. "\\):[ \t]*"
  15329. "\\(.+\\)"))
  15330. ;; Make isearch reveal the necessary context
  15331. (defun org-isearch-end ()
  15332. "Reveal context after isearch exits."
  15333. (when isearch-success ; only if search was successful
  15334. (if (featurep 'xemacs)
  15335. ;; Under XEmacs, the hook is run in the correct place,
  15336. ;; we directly show the context.
  15337. (org-show-context 'isearch)
  15338. ;; In Emacs the hook runs *before* restoring the overlays.
  15339. ;; So we have to use a one-time post-command-hook to do this.
  15340. ;; (Emacs 22 has a special variable, see function `org-mode')
  15341. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15342. isearch-mode-end-hook-quit)
  15343. ;; Only when the isearch was not quitted.
  15344. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15345. 'append 'local)))))
  15346. (defun org-isearch-post-command ()
  15347. "Remove self from hook, and show context."
  15348. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15349. (org-show-context 'isearch))
  15350. ;;;; Integration with and fixes for other packages
  15351. ;;; Imenu support
  15352. (defvar org-imenu-markers nil
  15353. "All markers currently used by Imenu.")
  15354. (make-variable-buffer-local 'org-imenu-markers)
  15355. (defun org-imenu-new-marker (&optional pos)
  15356. "Return a new marker for use by Imenu, and remember the marker."
  15357. (let ((m (make-marker)))
  15358. (move-marker m (or pos (point)))
  15359. (push m org-imenu-markers)
  15360. m))
  15361. (defun org-imenu-get-tree ()
  15362. "Produce the index for Imenu."
  15363. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15364. (setq org-imenu-markers nil)
  15365. (let* ((n org-imenu-depth)
  15366. (re (concat "^" outline-regexp))
  15367. (subs (make-vector (1+ n) nil))
  15368. (last-level 0)
  15369. m level head)
  15370. (save-excursion
  15371. (save-restriction
  15372. (widen)
  15373. (goto-char (point-max))
  15374. (while (re-search-backward re nil t)
  15375. (setq level (org-reduced-level (funcall outline-level)))
  15376. (when (<= level n)
  15377. (looking-at org-complex-heading-regexp)
  15378. (setq head (org-link-display-format
  15379. (org-match-string-no-properties 4))
  15380. m (org-imenu-new-marker))
  15381. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15382. (if (>= level last-level)
  15383. (push (cons head m) (aref subs level))
  15384. (push (cons head (aref subs (1+ level))) (aref subs level))
  15385. (loop for i from (1+ level) to n do (aset subs i nil)))
  15386. (setq last-level level)))))
  15387. (aref subs 1)))
  15388. (eval-after-load "imenu"
  15389. '(progn
  15390. (add-hook 'imenu-after-jump-hook
  15391. (lambda ()
  15392. (if (eq major-mode 'org-mode)
  15393. (org-show-context 'org-goto))))))
  15394. (defun org-link-display-format (link)
  15395. "Replace a link with either the description, or the link target
  15396. if no description is present"
  15397. (save-match-data
  15398. (if (string-match org-bracket-link-analytic-regexp link)
  15399. (replace-match (or (match-string 5 link)
  15400. (concat (match-string 1 link)
  15401. (match-string 3 link)))
  15402. nil nil link)
  15403. link)))
  15404. ;; Speedbar support
  15405. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15406. "Overlay marking the agenda restriction line in speedbar.")
  15407. (org-overlay-put org-speedbar-restriction-lock-overlay
  15408. 'face 'org-agenda-restriction-lock)
  15409. (org-overlay-put org-speedbar-restriction-lock-overlay
  15410. 'help-echo "Agendas are currently limited to this item.")
  15411. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15412. (defun org-speedbar-set-agenda-restriction ()
  15413. "Restrict future agenda commands to the location at point in speedbar.
  15414. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15415. (interactive)
  15416. (require 'org-agenda)
  15417. (let (p m tp np dir txt)
  15418. (cond
  15419. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15420. 'org-imenu t))
  15421. (setq m (get-text-property p 'org-imenu-marker))
  15422. (save-excursion
  15423. (save-restriction
  15424. (set-buffer (marker-buffer m))
  15425. (goto-char m)
  15426. (org-agenda-set-restriction-lock 'subtree))))
  15427. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15428. 'speedbar-function 'speedbar-find-file))
  15429. (setq tp (previous-single-property-change
  15430. (1+ p) 'speedbar-function)
  15431. np (next-single-property-change
  15432. tp 'speedbar-function)
  15433. dir (speedbar-line-directory)
  15434. txt (buffer-substring-no-properties (or tp (point-min))
  15435. (or np (point-max))))
  15436. (save-excursion
  15437. (save-restriction
  15438. (set-buffer (find-file-noselect
  15439. (let ((default-directory dir))
  15440. (expand-file-name txt))))
  15441. (unless (org-mode-p)
  15442. (error "Cannot restrict to non-Org-mode file"))
  15443. (org-agenda-set-restriction-lock 'file))))
  15444. (t (error "Don't know how to restrict Org-mode's agenda")))
  15445. (org-move-overlay org-speedbar-restriction-lock-overlay
  15446. (point-at-bol) (point-at-eol))
  15447. (setq current-prefix-arg nil)
  15448. (org-agenda-maybe-redo)))
  15449. (eval-after-load "speedbar"
  15450. '(progn
  15451. (speedbar-add-supported-extension ".org")
  15452. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15453. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15454. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15455. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15456. (add-hook 'speedbar-visiting-tag-hook
  15457. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15458. ;;; Fixes and Hacks for problems with other packages
  15459. ;; Make flyspell not check words in links, to not mess up our keymap
  15460. (defun org-mode-flyspell-verify ()
  15461. "Don't let flyspell put overlays at active buttons."
  15462. (not (get-text-property (point) 'keymap)))
  15463. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15464. (eval-after-load "bookmark"
  15465. '(if (boundp 'bookmark-after-jump-hook)
  15466. ;; We can use the hook
  15467. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15468. ;; Hook not available, use advice
  15469. (defadvice bookmark-jump (after org-make-visible activate)
  15470. "Make the position visible."
  15471. (org-bookmark-jump-unhide))))
  15472. ;; Make sure saveplace show the location if it was hidden
  15473. (eval-after-load "saveplace"
  15474. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15475. "Make the position visible."
  15476. (org-bookmark-jump-unhide)))
  15477. (defun org-bookmark-jump-unhide ()
  15478. "Unhide the current position, to show the bookmark location."
  15479. (and (org-mode-p)
  15480. (or (org-invisible-p)
  15481. (save-excursion (goto-char (max (point-min) (1- (point))))
  15482. (org-invisible-p)))
  15483. (org-show-context 'bookmark-jump)))
  15484. ;; Make session.el ignore our circular variable
  15485. (eval-after-load "session"
  15486. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15487. ;;;; Experimental code
  15488. (defun org-closed-in-range ()
  15489. "Sparse tree of items closed in a certain time range.
  15490. Still experimental, may disappear in the future."
  15491. (interactive)
  15492. ;; Get the time interval from the user.
  15493. (let* ((time1 (time-to-seconds
  15494. (org-read-date nil 'to-time nil "Starting date: ")))
  15495. (time2 (time-to-seconds
  15496. (org-read-date nil 'to-time nil "End date:")))
  15497. ;; callback function
  15498. (callback (lambda ()
  15499. (let ((time
  15500. (time-to-seconds
  15501. (apply 'encode-time
  15502. (org-parse-time-string
  15503. (match-string 1))))))
  15504. ;; check if time in interval
  15505. (and (>= time time1) (<= time time2))))))
  15506. ;; make tree, check each match with the callback
  15507. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15508. ;;;; Finish up
  15509. (provide 'org)
  15510. (run-hooks 'org-load-hook)
  15511. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15512. ;;; org.el ends here