org.el 574 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450
  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.22trans
  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.22trans"
  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 " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C registry: A registry for Org links" org-registry)
  175. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  176. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  177. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  178. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  179. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  180. (defcustom org-support-shift-select nil
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly regions started in this way.
  184. In Org-mode, in special contexts, these same keys are used for other
  185. purposes, important enough to compete with shift selection. Org tries
  186. to balance these needs by supporting `shift-select-mode' outside these
  187. special contexts, under control of this variable.
  188. The default of this variable is nil, to avoid confusing behavior. Shifted
  189. cursor keys will then execute Org commands in the following contexts:
  190. - on a headline, changing TODO state (left/right) and priority (up/down)
  191. - on a time stamp, changing the time
  192. - in a plain list item, changing the bullet type
  193. - in a property definition line, switching between allowed values
  194. - in the BEGIN line of a clock table (changing the time block).
  195. Outside these contexts, the commands will throw an error.
  196. When this variable is t and the cursor is not in a special context,
  197. Org-mode will support shift-selection for making and enlarging regions.
  198. To make this more effective, the bullet cycling will no longer happen
  199. anywhere in an item line, but only if the cursor is exactly on the bullet.
  200. If you set this variable to the symbol `always', then the keys
  201. will not be special in headlines, property lines, and item lines, to make
  202. shift selection work there as well. If this is what you want, you can
  203. use the following alternative commands: `C-c C-t' and `C-c ,' to
  204. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  205. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  206. edited by hand or in column view.
  207. However, when the cursor is on a timestamp, shift-cursor commands
  208. will still edit the time stamp - this is just too good to give up.
  209. XEmacs user should have this variable set to nil, because shift-select-mode
  210. is Emacs 23 only."
  211. :group 'org
  212. :type '(choice
  213. (const :tag "Never" nil)
  214. (const :tag "When outside special context" t)
  215. (const :tag "Everywhere except timestamps" always)))
  216. (defgroup org-startup nil
  217. "Options concerning startup of Org-mode."
  218. :tag "Org Startup"
  219. :group 'org)
  220. (defcustom org-startup-folded t
  221. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  222. This can also be configured on a per-file basis by adding one of
  223. the following lines anywhere in the buffer:
  224. #+STARTUP: fold
  225. #+STARTUP: nofold
  226. #+STARTUP: content"
  227. :group 'org-startup
  228. :type '(choice
  229. (const :tag "nofold: show all" nil)
  230. (const :tag "fold: overview" t)
  231. (const :tag "content: all headlines" content)))
  232. (defcustom org-startup-truncated t
  233. "Non-nil means, entering Org-mode will set `truncate-lines'.
  234. This is useful since some lines containing links can be very long and
  235. uninteresting. Also tables look terrible when wrapped."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (defcustom org-startup-align-all-tables nil
  239. "Non-nil means, align all tables when visiting a file.
  240. This is useful when the column width in tables is forced with <N> cookies
  241. in table fields. Such tables will look correct only after the first re-align.
  242. This can also be configured on a per-file basis by adding one of
  243. the following lines anywhere in the buffer:
  244. #+STARTUP: align
  245. #+STARTUP: noalign"
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-insert-mode-line-in-empty-file nil
  249. "Non-nil means insert the first line setting Org-mode in empty files.
  250. When the function `org-mode' is called interactively in an empty file, this
  251. normally means that the file name does not automatically trigger Org-mode.
  252. To ensure that the file will always be in Org-mode in the future, a
  253. line enforcing Org-mode will be inserted into the buffer, if this option
  254. has been set."
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-replace-disputed-keys nil
  258. "Non-nil means use alternative key bindings for some keys.
  259. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  260. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  261. If you want to use Org-mode together with one of these other modes,
  262. or more generally if you would like to move some Org-mode commands to
  263. other keys, set this variable and configure the keys with the variable
  264. `org-disputed-keys'.
  265. This option is only relevant at load-time of Org-mode, and must be set
  266. *before* org.el is loaded. Changing it requires a restart of Emacs to
  267. become effective."
  268. :group 'org-startup
  269. :type 'boolean)
  270. (defcustom org-use-extra-keys nil
  271. "Non-nil means use extra key sequence definitions for certain
  272. commands. This happens automatically if you run XEmacs or if
  273. window-system is nil. This variable lets you do the same
  274. manually. You must set it before loading org.
  275. Example: on Carbon Emacs 22 running graphically, with an external
  276. keyboard on a Powerbook, the default way of setting M-left might
  277. not work for either Alt or ESC. Setting this variable will make
  278. it work for ESC."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (if (fboundp 'defvaralias)
  282. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  283. (defcustom org-disputed-keys
  284. '(([(shift up)] . [(meta p)])
  285. ([(shift down)] . [(meta n)])
  286. ([(shift left)] . [(meta -)])
  287. ([(shift right)] . [(meta +)])
  288. ([(control shift right)] . [(meta shift +)])
  289. ([(control shift left)] . [(meta shift -)]))
  290. "Keys for which Org-mode and other modes compete.
  291. This is an alist, cars are the default keys, second element specifies
  292. the alternative to use when `org-replace-disputed-keys' is t.
  293. Keys can be specified in any syntax supported by `define-key'.
  294. The value of this option takes effect only at Org-mode's startup,
  295. therefore you'll have to restart Emacs to apply it after changing."
  296. :group 'org-startup
  297. :type 'alist)
  298. (defun org-key (key)
  299. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  300. Or return the original if not disputed."
  301. (if org-replace-disputed-keys
  302. (let* ((nkey (key-description key))
  303. (x (org-find-if (lambda (x)
  304. (equal (key-description (car x)) nkey))
  305. org-disputed-keys)))
  306. (if x (cdr x) key))
  307. key))
  308. (defun org-find-if (predicate seq)
  309. (catch 'exit
  310. (while seq
  311. (if (funcall predicate (car seq))
  312. (throw 'exit (car seq))
  313. (pop seq)))))
  314. (defun org-defkey (keymap key def)
  315. "Define a key, possibly translated, as returned by `org-key'."
  316. (define-key keymap (org-key key) def))
  317. (defcustom org-ellipsis nil
  318. "The ellipsis to use in the Org-mode outline.
  319. When nil, just use the standard three dots. When a string, use that instead,
  320. When a face, use the standard 3 dots, but with the specified face.
  321. The change affects only Org-mode (which will then use its own display table).
  322. Changing this requires executing `M-x org-mode' in a buffer to become
  323. effective."
  324. :group 'org-startup
  325. :type '(choice (const :tag "Default" nil)
  326. (face :tag "Face" :value org-warning)
  327. (string :tag "String" :value "...#")))
  328. (defvar org-display-table nil
  329. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  330. (defgroup org-keywords nil
  331. "Keywords in Org-mode."
  332. :tag "Org Keywords"
  333. :group 'org)
  334. (defcustom org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-scheduled-string "SCHEDULED:"
  343. "String to mark scheduled TODO entries.
  344. A schedule is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-schedule].
  347. Changes become only effective after restarting Emacs."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-clock-string "CLOCK:"
  355. "String used as prefix for timestamps clocking work hours on an item."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-comment-string "COMMENT"
  359. "Entries starting with this keyword will never be exported.
  360. An entry can be toggled between COMMENT and normal with
  361. \\[org-toggle-comment].
  362. Changes become only effective after restarting Emacs."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-quote-string "QUOTE"
  366. "Entries starting with this keyword will be exported in fixed-width font.
  367. Quoting applies only to the text in the entry following the headline, and does
  368. not extend beyond the next headline, even if that is lower level.
  369. An entry can be toggled between QUOTE and normal with
  370. \\[org-toggle-fixed-width-section]."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defconst org-repeat-re
  374. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  375. "Regular expression for specifying repeated events.
  376. After a match, group 1 contains the repeat expression.")
  377. (defgroup org-structure nil
  378. "Options concerning the general structure of Org-mode files."
  379. :tag "Org Structure"
  380. :group 'org)
  381. (defgroup org-reveal-location nil
  382. "Options about how to make context of a location visible."
  383. :tag "Org Reveal Location"
  384. :group 'org-structure)
  385. (defconst org-context-choice
  386. '(choice
  387. (const :tag "Always" t)
  388. (const :tag "Never" nil)
  389. (repeat :greedy t :tag "Individual contexts"
  390. (cons
  391. (choice :tag "Context"
  392. (const agenda)
  393. (const org-goto)
  394. (const occur-tree)
  395. (const tags-tree)
  396. (const link-search)
  397. (const mark-goto)
  398. (const bookmark-jump)
  399. (const isearch)
  400. (const default))
  401. (boolean))))
  402. "Contexts for the reveal options.")
  403. (defcustom org-show-hierarchy-above '((default . t))
  404. "Non-nil means, show full hierarchy when revealing a location.
  405. Org-mode often shows locations in an org-mode file which might have
  406. been invisible before. When this is set, the hierarchy of headings
  407. above the exposed location is shown.
  408. Turning this off for example for sparse trees makes them very compact.
  409. Instead of t, this can also be an alist specifying this option for different
  410. contexts. Valid contexts are
  411. agenda when exposing an entry from the agenda
  412. org-goto when using the command `org-goto' on key C-c C-j
  413. occur-tree when using the command `org-occur' on key C-c /
  414. tags-tree when constructing a sparse tree based on tags matches
  415. link-search when exposing search matches associated with a link
  416. mark-goto when exposing the jump goal of a mark
  417. bookmark-jump when exposing a bookmark location
  418. isearch when exiting from an incremental search
  419. default default for all contexts not set explicitly"
  420. :group 'org-reveal-location
  421. :type org-context-choice)
  422. (defcustom org-show-following-heading '((default . nil))
  423. "Non-nil means, show following heading when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the heading following the
  426. match is shown.
  427. Turning this off for example for sparse trees makes them very compact,
  428. but makes it harder to edit the location of the match. In such a case,
  429. use the command \\[org-reveal] to show more context.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-reveal-location
  433. :type org-context-choice)
  434. (defcustom org-show-siblings '((default . nil) (isearch t))
  435. "Non-nil means, show all sibling heading when revealing a location.
  436. Org-mode often shows locations in an org-mode file which might have
  437. been invisible before. When this is set, the sibling of the current entry
  438. heading are all made visible. If `org-show-hierarchy-above' is t,
  439. the same happens on each level of the hierarchy above the current entry.
  440. By default this is on for the isearch context, off for all other contexts.
  441. Turning this off for example for sparse trees makes them very compact,
  442. but makes it harder to edit the location of the match. In such a case,
  443. use the command \\[org-reveal] to show more context.
  444. Instead of t, this can also be an alist specifying this option for different
  445. contexts. See `org-show-hierarchy-above' for valid contexts."
  446. :group 'org-reveal-location
  447. :type org-context-choice)
  448. (defcustom org-show-entry-below '((default . nil))
  449. "Non-nil means, show the entry below a headline when revealing a location.
  450. Org-mode often shows locations in an org-mode file which might have
  451. been invisible before. When this is set, the text below the headline that is
  452. exposed is also shown.
  453. By default this is off for all contexts.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-indirect-buffer-display 'other-window
  459. "How should indirect tree buffers be displayed?
  460. This applies to indirect buffers created with the commands
  461. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  462. Valid values are:
  463. current-window Display in the current window
  464. other-window Just display in another window.
  465. dedicated-frame Create one new frame, and re-use it each time.
  466. new-frame Make a new frame each time. Note that in this case
  467. previously-made indirect buffers are kept, and you need to
  468. kill these buffers yourself."
  469. :group 'org-structure
  470. :group 'org-agenda-windows
  471. :type '(choice
  472. (const :tag "In current window" current-window)
  473. (const :tag "In current frame, other window" other-window)
  474. (const :tag "Each time a new frame" new-frame)
  475. (const :tag "One dedicated frame" dedicated-frame)))
  476. (defgroup org-cycle nil
  477. "Options concerning visibility cycling in Org-mode."
  478. :tag "Org Cycle"
  479. :group 'org-structure)
  480. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  481. "Names of drawers. Drawers are not opened by cycling on the headline above.
  482. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  483. this:
  484. :DRAWERNAME:
  485. .....
  486. :END:
  487. The drawer \"PROPERTIES\" is special for capturing properties through
  488. the property API.
  489. Drawers can be defined on the per-file basis with a line like:
  490. #+DRAWERS: HIDDEN STATE PROPERTIES"
  491. :group 'org-structure
  492. :type '(repeat (string :tag "Drawer Name")))
  493. (defcustom org-cycle-global-at-bob nil
  494. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  495. This makes it possible to do global cycling without having to use S-TAB or
  496. C-u TAB. For this special case to work, the first line of the buffer
  497. must not be a headline - it may be empty or some other text. When used in
  498. this way, `org-cycle-hook' is disables temporarily, to make sure the
  499. cursor stays at the beginning of the buffer.
  500. When this option is nil, don't do anything special at the beginning
  501. of the buffer."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-emulate-tab t
  505. "Where should `org-cycle' emulate TAB.
  506. nil Never
  507. white Only in completely white lines
  508. whitestart Only at the beginning of lines, before the first non-white char
  509. t Everywhere except in headlines
  510. exc-hl-bol Everywhere except at the start of a headline
  511. If TAB is used in a place where it does not emulate TAB, the current subtree
  512. visibility is cycled."
  513. :group 'org-cycle
  514. :type '(choice (const :tag "Never" nil)
  515. (const :tag "Only in completely white lines" white)
  516. (const :tag "Before first char in a line" whitestart)
  517. (const :tag "Everywhere except in headlines" t)
  518. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  519. ))
  520. (defcustom org-cycle-separator-lines 2
  521. "Number of empty lines needed to keep an empty line between collapsed trees.
  522. If you leave an empty line between the end of a subtree and the following
  523. headline, this empty line is hidden when the subtree is folded.
  524. Org-mode will leave (exactly) one empty line visible if the number of
  525. empty lines is equal or larger to the number given in this variable.
  526. So the default 2 means, at least 2 empty lines after the end of a subtree
  527. are needed to produce free space between a collapsed subtree and the
  528. following headline.
  529. Special case: when 0, never leave empty lines in collapsed view."
  530. :group 'org-cycle
  531. :type 'integer)
  532. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  533. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  534. org-cycle-hide-drawers
  535. org-cycle-show-empty-lines
  536. org-optimize-window-after-visibility-change)
  537. "Hook that is run after `org-cycle' has changed the buffer visibility.
  538. The function(s) in this hook must accept a single argument which indicates
  539. the new state that was set by the most recent `org-cycle' command. The
  540. argument is a symbol. After a global state change, it can have the values
  541. `overview', `content', or `all'. After a local state change, it can have
  542. the values `folded', `children', or `subtree'."
  543. :group 'org-cycle
  544. :type 'hook)
  545. (defgroup org-edit-structure nil
  546. "Options concerning structure editing in Org-mode."
  547. :tag "Org Edit Structure"
  548. :group 'org-structure)
  549. (defcustom org-odd-levels-only nil
  550. "Non-nil means, skip even levels and only use odd levels for the outline.
  551. This has the effect that two stars are being added/taken away in
  552. promotion/demotion commands. It also influences how levels are
  553. handled by the exporters.
  554. Changing it requires restart of `font-lock-mode' to become effective
  555. for fontification also in regions already fontified.
  556. You may also set this on a per-file basis by adding one of the following
  557. lines to the buffer:
  558. #+STARTUP: odd
  559. #+STARTUP: oddeven"
  560. :group 'org-edit-structure
  561. :group 'org-font-lock
  562. :type 'boolean)
  563. (defcustom org-adapt-indentation t
  564. "Non-nil means, adapt indentation when promoting and demoting.
  565. When this is set and the *entire* text in an entry is indented, the
  566. indentation is increased by one space in a demotion command, and
  567. decreased by one in a promotion command. If any line in the entry
  568. body starts at column 0, indentation is not changed at all."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-special-ctrl-a/e nil
  572. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  573. When t, `C-a' will bring back the cursor to the beginning of the
  574. headline text, i.e. after the stars and after a possible TODO keyword.
  575. In an item, this will be the position after the bullet.
  576. When the cursor is already at that position, another `C-a' will bring
  577. it to the beginning of the line.
  578. `C-e' will jump to the end of the headline, ignoring the presence of tags
  579. in the headline. A second `C-e' will then jump to the true end of the
  580. line, after any tags.
  581. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  582. and only a directly following, identical keypress will bring the cursor
  583. to the special positions."
  584. :group 'org-edit-structure
  585. :type '(choice
  586. (const :tag "off" nil)
  587. (const :tag "after bullet first" t)
  588. (const :tag "border first" reversed)))
  589. (if (fboundp 'defvaralias)
  590. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  591. (defcustom org-special-ctrl-k nil
  592. "Non-nil means `C-k' will behave specially in headlines.
  593. When nil, `C-k' will call the default `kill-line' command.
  594. When t, the following will happen while the cursor is in the headline:
  595. - When the cursor is at the beginning of a headline, kill the entire
  596. line and possible the folded subtree below the line.
  597. - When in the middle of the headline text, kill the headline up to the tags.
  598. - When after the headline text, kill the tags."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-folded-subtrees t
  602. "Non-nil means, when yanking subtrees, fold them.
  603. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  604. it starts with a heading and all other headings in it are either children
  605. or siblings, then fold all the subtrees. However, do this only if no
  606. text after the yank would be swallowed into a folded tree by this action."
  607. :group 'org-edit-structure
  608. :type 'boolean)
  609. (defcustom org-yank-adjusted-subtrees nil
  610. "Non-nil means, when yanking subtrees, adjust the level.
  611. With this setting, `org-paste-subtree' is used to insert the subtree, see
  612. this function for details."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-M-RET-may-split-line '((default . t))
  616. "Non-nil means, M-RET will split the line at the cursor position.
  617. When nil, it will go to the end of the line before making a
  618. new line.
  619. You may also set this option in a different way for different
  620. contexts. Valid contexts are:
  621. headline when creating a new headline
  622. item when creating a new item
  623. table in a table field
  624. default the value to be used for all contexts not explicitly
  625. customized"
  626. :group 'org-structure
  627. :group 'org-table
  628. :type '(choice
  629. (const :tag "Always" t)
  630. (const :tag "Never" nil)
  631. (repeat :greedy t :tag "Individual contexts"
  632. (cons
  633. (choice :tag "Context"
  634. (const headline)
  635. (const item)
  636. (const table)
  637. (const default))
  638. (boolean)))))
  639. (defcustom org-insert-heading-respect-content nil
  640. "Non-nil means, insert new headings after the current subtree.
  641. When nil, the new heading is created directly after the current line.
  642. The commands \\[org-insert-heading-respect-content] and
  643. \\[org-insert-todo-heading-respect-content] turn this variable on
  644. for the duration of the command."
  645. :group 'org-structure
  646. :type 'boolean)
  647. (defcustom org-blank-before-new-entry '((heading . auto)
  648. (plain-list-item . auto))
  649. "Should `org-insert-heading' leave a blank line before new heading/item?
  650. The value is an alist, with `heading' and `plain-list-item' as car,
  651. and a boolean flag as cdr."
  652. :group 'org-edit-structure
  653. :type '(list
  654. (cons (const heading)
  655. (choice (const :tag "Never" nil)
  656. (const :tag "Always" t)
  657. (const :tag "Auto" auto)))
  658. (cons (const plain-list-item)
  659. (choice (const :tag "Never" nil)
  660. (const :tag "Always" t)
  661. (const :tag "Auto" auto)))))
  662. (defcustom org-insert-heading-hook nil
  663. "Hook being run after inserting a new heading."
  664. :group 'org-edit-structure
  665. :type 'hook)
  666. (defcustom org-enable-fixed-width-editor t
  667. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  668. This currently only means, they are never auto-wrapped.
  669. When nil, such lines will be treated like ordinary lines.
  670. See also the QUOTE keyword."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-edit-src-region-extra nil
  674. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  675. For examples see the function `org-edit-src-find-region-and-lang'.
  676. The regular expression identifying the begin marker should end with a newline,
  677. and the regexp marking the end line should start with a newline, to make sure
  678. there are kept outside the narrowed region."
  679. :group 'org-edit-structure
  680. :type '(repeat
  681. (list
  682. (regexp :tag "begin regexp")
  683. (regexp :tag "end regexp")
  684. (choice :tag "language"
  685. (string :tag "specify")
  686. (integer :tag "from match group")
  687. (const :tag "from `lang' element")
  688. (const :tag "from `style' element")))))
  689. (defcustom org-coderef-label-format "(ref:%s)"
  690. "The default coderef format.
  691. This format string will be used to search for coderef labels in literal
  692. examples (EXAMPLE and SRC blocks). The format can be overwritten
  693. an individual literal example with the -f option, like
  694. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  695. ...
  696. #+END_SRC
  697. If you want to use this for HTML export, make sure that the format does
  698. not introduce special font-locking, and avoid the HTML special
  699. characters `<', `>', and `&'. The reason for this restriction is that
  700. the labels are searched for only after htmlize has done its job."
  701. :group 'org-edit-structure ; FIXME this is not in the right group
  702. :type 'string)
  703. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  704. "The mode that should be used to edit fixed-width regions.
  705. These are the regions where each line starts with a colon."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const artist-mode)
  709. (const picture-mode)
  710. (const fundamental-mode)
  711. (function :tag "Other (specify)")))
  712. (defcustom org-goto-auto-isearch t
  713. "Non-nil means, typing characters in org-goto starts incremental search."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defgroup org-sparse-trees nil
  717. "Options concerning sparse trees in Org-mode."
  718. :tag "Org Sparse Trees"
  719. :group 'org-structure)
  720. (defcustom org-highlight-sparse-tree-matches t
  721. "Non-nil means, highlight all matches that define a sparse tree.
  722. The highlights will automatically disappear the next time the buffer is
  723. changed by an edit command."
  724. :group 'org-sparse-trees
  725. :type 'boolean)
  726. (defcustom org-remove-highlights-with-change t
  727. "Non-nil means, any change to the buffer will remove temporary highlights.
  728. Such highlights are created by `org-occur' and `org-clock-display'.
  729. When nil, `C-c C-c needs to be used to get rid of the highlights.
  730. The highlights created by `org-preview-latex-fragment' always need
  731. `C-c C-c' to be removed."
  732. :group 'org-sparse-trees
  733. :group 'org-time
  734. :type 'boolean)
  735. (defcustom org-occur-hook '(org-first-headline-recenter)
  736. "Hook that is run after `org-occur' has constructed a sparse tree.
  737. This can be used to recenter the window to show as much of the structure
  738. as possible."
  739. :group 'org-sparse-trees
  740. :type 'hook)
  741. (defgroup org-imenu-and-speedbar nil
  742. "Options concerning imenu and speedbar in Org-mode."
  743. :tag "Org Imenu and Speedbar"
  744. :group 'org-structure)
  745. (defcustom org-imenu-depth 2
  746. "The maximum level for Imenu access to Org-mode headlines.
  747. This also applied for speedbar access."
  748. :group 'org-imenu-and-speedbar
  749. :type 'number)
  750. (defgroup org-table nil
  751. "Options concerning tables in Org-mode."
  752. :tag "Org Table"
  753. :group 'org)
  754. (defcustom org-enable-table-editor 'optimized
  755. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  756. When nil, such lines will be treated like ordinary lines.
  757. When equal to the symbol `optimized', the table editor will be optimized to
  758. do the following:
  759. - Automatic overwrite mode in front of whitespace in table fields.
  760. This makes the structure of the table stay in tact as long as the edited
  761. field does not exceed the column width.
  762. - Minimize the number of realigns. Normally, the table is aligned each time
  763. TAB or RET are pressed to move to another field. With optimization this
  764. happens only if changes to a field might have changed the column width.
  765. Optimization requires replacing the functions `self-insert-command',
  766. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  767. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  768. very good at guessing when a re-align will be necessary, but you can always
  769. force one with \\[org-ctrl-c-ctrl-c].
  770. If you would like to use the optimized version in Org-mode, but the
  771. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  772. This variable can be used to turn on and off the table editor during a session,
  773. but in order to toggle optimization, a restart is required.
  774. See also the variable `org-table-auto-blank-field'."
  775. :group 'org-table
  776. :type '(choice
  777. (const :tag "off" nil)
  778. (const :tag "on" t)
  779. (const :tag "on, optimized" optimized)))
  780. (defcustom org-table-tab-recognizes-table.el t
  781. "Non-nil means, TAB will automatically notice a table.el table.
  782. When it sees such a table, it moves point into it and - if necessary -
  783. calls `table-recognize-table'."
  784. :group 'org-table-editing
  785. :type 'boolean)
  786. (defgroup org-link nil
  787. "Options concerning links in Org-mode."
  788. :tag "Org Link"
  789. :group 'org)
  790. (defvar org-link-abbrev-alist-local nil
  791. "Buffer-local version of `org-link-abbrev-alist', which see.
  792. The value of this is taken from the #+LINK lines.")
  793. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  794. (defcustom org-link-abbrev-alist nil
  795. "Alist of link abbreviations.
  796. The car of each element is a string, to be replaced at the start of a link.
  797. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  798. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  799. [[linkkey:tag][description]]
  800. If REPLACE is a string, the tag will simply be appended to create the link.
  801. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  802. the placeholder \"%h\" will cause a url-encoded version of the tag to
  803. be inserted at that point (see the function `url-hexify-string').
  804. REPLACE may also be a function that will be called with the tag as the
  805. only argument to create the link, which should be returned as a string.
  806. See the manual for examples."
  807. :group 'org-link
  808. :type '(repeat
  809. (cons
  810. (string :tag "Protocol")
  811. (choice
  812. (string :tag "Format")
  813. (function)))))
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][description]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative Relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute Absolute path, if possible with ~ for home directory.
  827. noabbrev Absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackets that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. footnote Footnote labels.
  849. Changing this variable requires a restart of Emacs to become effective."
  850. :group 'org-link
  851. :type '(set :greedy t
  852. (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "Plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "Footnotes" footnote)))
  859. (defcustom org-make-link-description-function nil
  860. "Function to use to generate link descriptions from links. If
  861. nil the link location will be used. This function must take two
  862. parameters; the first is the link and the second the description
  863. org-insert-link has generated, and should return the description
  864. to use."
  865. :group 'org-link
  866. :type 'function)
  867. (defgroup org-link-store nil
  868. "Options concerning storing links in Org-mode."
  869. :tag "Org Store Link"
  870. :group 'org-link)
  871. (defcustom org-email-link-description-format "Email %c: %.30s"
  872. "Format of the description part of a link to an email or usenet message.
  873. The following %-escapes will be replaced by corresponding information:
  874. %F full \"From\" field
  875. %f name, taken from \"From\" field, address if no name
  876. %T full \"To\" field
  877. %t first name in \"To\" field, address if no name
  878. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  879. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  880. %s subject
  881. %m message-id.
  882. You may use normal field width specification between the % and the letter.
  883. This is for example useful to limit the length of the subject.
  884. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  885. :group 'org-link-store
  886. :type 'string)
  887. (defcustom org-from-is-user-regexp
  888. (let (r1 r2)
  889. (when (and user-mail-address (not (string= user-mail-address "")))
  890. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  891. (when (and user-full-name (not (string= user-full-name "")))
  892. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  893. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  894. "Regexp matched against the \"From:\" header of an email or usenet message.
  895. It should match if the message is from the user him/herself."
  896. :group 'org-link-store
  897. :type 'regexp)
  898. (defcustom org-link-to-org-use-id 'create-if-interactive
  899. "Non-nil means, storing a link to an Org file will use entry IDs.
  900. Note that before this variable is even considered, org-id must be loaded,
  901. to please customize `org-modules' and turn it on.
  902. The variable can have the following values:
  903. t Create an ID if needed to make a link to the current entry.
  904. create-if-interactive
  905. If `org-store-link' is called directly (interactively, as a user
  906. command), do create an ID to support the link. But when doing the
  907. job for remember, only use the ID if it already exists. The
  908. purpose of this setting is to avoid proliferation of unwanted
  909. IDs, just because you happen to be in an Org file when you
  910. call `org-remember' that automatically and preemptively
  911. creates a link. If you do want to get an ID link in a remember
  912. template to an entry not having an ID, create it first by
  913. explicitly creating a link to it, using `C-c C-l' first.
  914. use-existing
  915. Use existing ID, do not create one.
  916. nil Never use an ID to make a link, instead link using a text search for
  917. the headline text."
  918. :group 'org-link-store
  919. :type '(choice
  920. (const :tag "Create ID to make link" t)
  921. (const :tag "Create if string link interactively"
  922. 'create-if-interactive)
  923. (const :tag "Only use existing" 'use-existing)
  924. (const :tag "Do not use ID to create link" nil)))
  925. (defcustom org-context-in-file-links t
  926. "Non-nil means, file links from `org-store-link' contain context.
  927. A search string will be added to the file name with :: as separator and
  928. used to find the context when the link is activated by the command
  929. `org-open-at-point'.
  930. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  931. negates this setting for the duration of the command."
  932. :group 'org-link-store
  933. :type 'boolean)
  934. (defcustom org-keep-stored-link-after-insertion nil
  935. "Non-nil means, keep link in list for entire session.
  936. The command `org-store-link' adds a link pointing to the current
  937. location to an internal list. These links accumulate during a session.
  938. The command `org-insert-link' can be used to insert links into any
  939. Org-mode file (offering completion for all stored links). When this
  940. option is nil, every link which has been inserted once using \\[org-insert-link]
  941. will be removed from the list, to make completing the unused links
  942. more efficient."
  943. :group 'org-link-store
  944. :type 'boolean)
  945. (defgroup org-link-follow nil
  946. "Options concerning following links in Org-mode."
  947. :tag "Org Follow Link"
  948. :group 'org-link)
  949. (defcustom org-link-translation-function nil
  950. "Function to translate links with different syntax to Org syntax.
  951. This can be used to translate links created for example by the Planner
  952. or emacs-wiki packages to Org syntax.
  953. The function must accept two parameters, a TYPE containing the link
  954. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  955. which is everything after the link protocol. It should return a cons
  956. with possibly modified values of type and path.
  957. Org contains a function for this, so if you set this variable to
  958. `org-translate-link-from-planner', you should be able follow many
  959. links created by planner."
  960. :group 'org-link-follow
  961. :type 'function)
  962. (defcustom org-follow-link-hook nil
  963. "Hook that is run after a link has been followed."
  964. :group 'org-link-follow
  965. :type 'hook)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded.
  969. This really should not be used, it does not make sense, and the
  970. implementation is bad."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-return-follows-link nil
  974. "Non-nil means, on links RET will follow the link.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mouse-1-follows-link
  979. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  980. "Non-nil means, mouse-1 on a link will follow the link.
  981. A longer mouse click will still set point. Does not work on XEmacs.
  982. Needs to be set before org.el is loaded."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-mark-ring-length 4
  986. "Number of different positions to be recorded in the ring
  987. Changing this requires a restart of Emacs to work correctly."
  988. :group 'org-link-follow
  989. :type 'integer)
  990. (defcustom org-link-frame-setup
  991. '((vm . vm-visit-folder-other-frame)
  992. (gnus . gnus-other-frame)
  993. (file . find-file-other-window))
  994. "Setup the frame configuration for following links.
  995. When following a link with Emacs, it may often be useful to display
  996. this link in another window or frame. This variable can be used to
  997. set this up for the different types of links.
  998. For VM, use any of
  999. `vm-visit-folder'
  1000. `vm-visit-folder-other-frame'
  1001. For Gnus, use any of
  1002. `gnus'
  1003. `gnus-other-frame'
  1004. `org-gnus-no-new-news'
  1005. For FILE, use any of
  1006. `find-file'
  1007. `find-file-other-window'
  1008. `find-file-other-frame'
  1009. For the calendar, use the variable `calendar-setup'.
  1010. For BBDB, it is currently only possible to display the matches in
  1011. another window."
  1012. :group 'org-link-follow
  1013. :type '(list
  1014. (cons (const vm)
  1015. (choice
  1016. (const vm-visit-folder)
  1017. (const vm-visit-folder-other-window)
  1018. (const vm-visit-folder-other-frame)))
  1019. (cons (const gnus)
  1020. (choice
  1021. (const gnus)
  1022. (const gnus-other-frame)
  1023. (const org-gnus-no-new-news)))
  1024. (cons (const file)
  1025. (choice
  1026. (const find-file)
  1027. (const find-file-other-window)
  1028. (const find-file-other-frame)))))
  1029. (defcustom org-display-internal-link-with-indirect-buffer nil
  1030. "Non-nil means, use indirect buffer to display infile links.
  1031. Activating internal links (from one location in a file to another location
  1032. in the same file) normally just jumps to the location. When the link is
  1033. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1034. another window. When this option is set, the other window actually displays
  1035. an indirect buffer clone of the current buffer, to avoid any visibility
  1036. changes to the current buffer."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-open-non-existing-files nil
  1040. "Non-nil means, `org-open-file' will open non-existing files.
  1041. When nil, an error will be generated."
  1042. :group 'org-link-follow
  1043. :type 'boolean)
  1044. (defcustom org-open-directory-means-index-dot-org nil
  1045. "Non-nil means, a link to a directory really means to index.org.
  1046. When nil, following a directory link will run dired or open a finder/explorer
  1047. window on that directory."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1051. "Function and arguments to call for following mailto links.
  1052. This is a list with the first element being a lisp function, and the
  1053. remaining elements being arguments to the function. In string arguments,
  1054. %a will be replaced by the address, and %s will be replaced by the subject
  1055. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1056. :group 'org-link-follow
  1057. :type '(choice
  1058. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1059. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1060. (const :tag "message-mail" (message-mail "%a" "%s"))
  1061. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1062. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1063. "Non-nil means, ask for confirmation before executing shell links.
  1064. Shell links can be dangerous: just think about a link
  1065. [[shell:rm -rf ~/*][Google Search]]
  1066. This link would show up in your Org-mode document as \"Google Search\",
  1067. but really it would remove your entire home directory.
  1068. Therefore we advise against setting this variable to nil.
  1069. Just change it to `y-or-n-p' of you want to confirm with a
  1070. single keystroke rather than having to type \"yes\"."
  1071. :group 'org-link-follow
  1072. :type '(choice
  1073. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1074. (const :tag "with y-or-n (faster)" y-or-n-p)
  1075. (const :tag "no confirmation (dangerous)" nil)))
  1076. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1078. Elisp links can be dangerous: just think about a link
  1079. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defconst org-file-apps-defaults-gnu
  1091. '((remote . emacs)
  1092. (system . mailcap)
  1093. (t . mailcap))
  1094. "Default file applications on a UNIX or GNU/Linux system.
  1095. See `org-file-apps'.")
  1096. (defconst org-file-apps-defaults-macosx
  1097. '((remote . emacs)
  1098. (t . "open %s")
  1099. (system . "open %s")
  1100. ("ps.gz" . "gv %s")
  1101. ("eps.gz" . "gv %s")
  1102. ("dvi" . "xdvi %s")
  1103. ("fig" . "xfig %s"))
  1104. "Default file applications on a MacOS X system.
  1105. The system \"open\" is known as a default, but we use X11 applications
  1106. for some files for which the OS does not have a good default.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-windowsnt
  1109. (list
  1110. '(remote . emacs)
  1111. (cons t
  1112. (list (if (featurep 'xemacs)
  1113. 'mswindows-shell-execute
  1114. 'w32-shell-execute)
  1115. "open" 'file))
  1116. (cons 'system
  1117. (list (if (featurep 'xemacs)
  1118. 'mswindows-shell-execute
  1119. 'w32-shell-execute)
  1120. "open" 'file)))
  1121. "Default file applications on a Windows NT system.
  1122. The system \"open\" is used for most files.
  1123. See `org-file-apps'.")
  1124. (defcustom org-file-apps
  1125. '(
  1126. (auto-mode . emacs)
  1127. ("\\.x?html?\\'" . default)
  1128. ("\\.pdf\\'" . default)
  1129. )
  1130. "External applications for opening `file:path' items in a document.
  1131. Org-mode uses system defaults for different file types, but
  1132. you can use this variable to set the application for a given file
  1133. extension. The entries in this list are cons cells where the car identifies
  1134. files and the cdr the corresponding command. Possible values for the
  1135. file identifier are
  1136. \"regex\" Regular expression matched against the file name. For backward
  1137. compatibility, this can also be a string with only alphanumeric
  1138. characters, which is then interpreted as an extension.
  1139. `directory' Matches a directory
  1140. `remote' Matches a remote file, accessible through tramp or efs.
  1141. Remote files most likely should be visited through Emacs
  1142. because external applications cannot handle such paths.
  1143. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1144. so all files Emacs knows how to handle. Using this with
  1145. command `emacs' will open most files in Emacs. Beware that this
  1146. will also open html files inside Emacs, unless you add
  1147. (\"html\" . default) to the list as well.
  1148. t Default for files not matched by any of the other options.
  1149. `system' The system command to open files, like `open' on Windows
  1150. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1151. that will be selected if you call `C-c C-o' with a double
  1152. `C-u C-u' prefix.
  1153. Possible values for the command are:
  1154. `emacs' The file will be visited by the current Emacs process.
  1155. `default' Use the default application for this file type, which is the
  1156. association for t in the list, most likely in the system-specific
  1157. part.
  1158. This can be used to overrule an unwanted setting in the
  1159. system-specific variable.
  1160. `system' Use the system command for opening files, like \"open\".
  1161. This command is specified by the entry whose car is `system'.
  1162. Most likely, the system-specific version of this variable
  1163. does define this command, but you can overrule/replace it
  1164. here.
  1165. string A command to be executed by a shell; %s will be replaced
  1166. by the path to the file.
  1167. sexp A Lisp form which will be evaluated. The file path will
  1168. be available in the Lisp variable `file'.
  1169. For more examples, see the system specific constants
  1170. `org-file-apps-defaults-macosx'
  1171. `org-file-apps-defaults-windowsnt'
  1172. `org-file-apps-defaults-gnu'."
  1173. :group 'org-link-follow
  1174. :type '(repeat
  1175. (cons (choice :value ""
  1176. (string :tag "Extension")
  1177. (const :tag "System command to open files" system)
  1178. (const :tag "Default for unrecognized files" t)
  1179. (const :tag "Remote file" remote)
  1180. (const :tag "Links to a directory" directory)
  1181. (const :tag "Any files that have Emacs modes"
  1182. auto-mode))
  1183. (choice :value ""
  1184. (const :tag "Visit with Emacs" emacs)
  1185. (const :tag "Use default" default)
  1186. (const :tag "Use the system command" system)
  1187. (string :tag "Command")
  1188. (sexp :tag "Lisp form")))))
  1189. (defgroup org-refile nil
  1190. "Options concerning refiling entries in Org-mode."
  1191. :tag "Org Refile"
  1192. :group 'org)
  1193. (defcustom org-directory "~/org"
  1194. "Directory with org files.
  1195. This is just a default location to look for Org files. There is no need
  1196. at all to put your files into this directory. It is only used in the
  1197. following situations:
  1198. 1. When a remember template specifies a target file that is not an
  1199. absolute path. The path will then be interpreted relative to
  1200. `org-directory'
  1201. 2. When a remember note is filed away in an interactive way (when exiting the
  1202. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1203. with `org-directory' as the default path."
  1204. :group 'org-refile
  1205. :group 'org-remember
  1206. :type 'directory)
  1207. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1208. "Default target for storing notes.
  1209. Used by the hooks for remember.el. This can be a string, or nil to mean
  1210. the value of `remember-data-file'.
  1211. You can set this on a per-template basis with the variable
  1212. `org-remember-templates'."
  1213. :group 'org-refile
  1214. :group 'org-remember
  1215. :type '(choice
  1216. (const :tag "Default from remember-data-file" nil)
  1217. file))
  1218. (defcustom org-goto-interface 'outline
  1219. "The default interface to be used for `org-goto'.
  1220. Allowed values are:
  1221. outline The interface shows an outline of the relevant file
  1222. and the correct heading is found by moving through
  1223. the outline or by searching with incremental search.
  1224. outline-path-completion Headlines in the current buffer are offered via
  1225. completion. This is the interface also used by
  1226. the refile command."
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Outline" outline)
  1230. (const :tag "Outline-path-completion" outline-path-completion)))
  1231. (defcustom org-goto-max-level 5
  1232. "Maximum level to be considered when running org-goto with refile interface."
  1233. :group 'org-refile
  1234. :type 'number)
  1235. (defcustom org-reverse-note-order nil
  1236. "Non-nil means, store new notes at the beginning of a file or entry.
  1237. When nil, new notes will be filed to the end of a file or entry.
  1238. This can also be a list with cons cells of regular expressions that
  1239. are matched against file names, and values."
  1240. :group 'org-remember
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Reverse always" t)
  1244. (const :tag "Reverse never" nil)
  1245. (repeat :tag "By file name regexp"
  1246. (cons regexp boolean))))
  1247. (defcustom org-refile-targets nil
  1248. "Targets for refiling entries with \\[org-refile].
  1249. This is list of cons cells. Each cell contains:
  1250. - a specification of the files to be considered, either a list of files,
  1251. or a symbol whose function or variable value will be used to retrieve
  1252. a file name or a list of file names. If you use `org-agenda-files' for
  1253. that, all agenda files will be scanned for targets. Nil means, consider
  1254. headings in the current buffer.
  1255. - A specification of how to find candidate refile targets. This may be
  1256. any of:
  1257. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1258. This tag has to be present in all target headlines, inheritance will
  1259. not be considered.
  1260. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1261. todo keyword.
  1262. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1263. headlines that are refiling targets.
  1264. - a cons cell (:level . N). Any headline of level N is considered a target.
  1265. Note that, when `org-odd-levels-only' is set, level corresponds to
  1266. order in hierarchy, not to the number of stars.
  1267. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1268. Note that, when `org-odd-levels-only' is set, level corresponds to
  1269. order in hierarchy, not to the number of stars.
  1270. When this variable is nil, all top-level headlines in the current buffer
  1271. are used, equivalent to the value `((nil . (:level . 1))'."
  1272. :group 'org-refile
  1273. :type '(repeat
  1274. (cons
  1275. (choice :value org-agenda-files
  1276. (const :tag "All agenda files" org-agenda-files)
  1277. (const :tag "Current buffer" nil)
  1278. (function) (variable) (file))
  1279. (choice :tag "Identify target headline by"
  1280. (cons :tag "Specific tag" (const :value :tag) (string))
  1281. (cons :tag "TODO keyword" (const :value :todo) (string))
  1282. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1283. (cons :tag "Level number" (const :value :level) (integer))
  1284. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1285. (defcustom org-refile-use-outline-path nil
  1286. "Non-nil means, provide refile targets as paths.
  1287. So a level 3 headline will be available as level1/level2/level3.
  1288. When the value is `file', also include the file name (without directory)
  1289. into the path. When `full-file-path', include the full file path."
  1290. :group 'org-refile
  1291. :type '(choice
  1292. (const :tag "Not" nil)
  1293. (const :tag "Yes" t)
  1294. (const :tag "Start with file name" file)
  1295. (const :tag "Start with full file path" full-file-path)))
  1296. (defcustom org-outline-path-complete-in-steps t
  1297. "Non-nil means, complete the outline path in hierarchical steps.
  1298. When Org-mode uses the refile interface to select an outline path
  1299. \(see variable `org-refile-use-outline-path'), the completion of
  1300. the path can be done is a single go, or if can be done in steps down
  1301. the headline hierarchy. Going in steps is probably the best if you
  1302. do not use a special completion package like `ido' or `icicles'.
  1303. However, when using these packages, going in one step can be very
  1304. fast, while still showing the whole path to the entry."
  1305. :group 'org-refile
  1306. :type 'boolean)
  1307. (defgroup org-todo nil
  1308. "Options concerning TODO items in Org-mode."
  1309. :tag "Org TODO"
  1310. :group 'org)
  1311. (defgroup org-progress nil
  1312. "Options concerning Progress logging in Org-mode."
  1313. :tag "Org Progress"
  1314. :group 'org-time)
  1315. (defvar org-todo-interpretation-widgets
  1316. '(
  1317. (:tag "Sequence (cycling hits every state)" sequence)
  1318. (:tag "Type (cycling directly to DONE)" type))
  1319. "The available interpretation symbols for customizing
  1320. `org-todo-keywords'.
  1321. Interested libraries should add to this list.")
  1322. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1323. "List of TODO entry keyword sequences and their interpretation.
  1324. \\<org-mode-map>This is a list of sequences.
  1325. Each sequence starts with a symbol, either `sequence' or `type',
  1326. indicating if the keywords should be interpreted as a sequence of
  1327. action steps, or as different types of TODO items. The first
  1328. keywords are states requiring action - these states will select a headline
  1329. for inclusion into the global TODO list Org-mode produces. If one of
  1330. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1331. signify that no further action is necessary. If \"|\" is not found,
  1332. the last keyword is treated as the only DONE state of the sequence.
  1333. The command \\[org-todo] cycles an entry through these states, and one
  1334. additional state where no keyword is present. For details about this
  1335. cycling, see the manual.
  1336. TODO keywords and interpretation can also be set on a per-file basis with
  1337. the special #+SEQ_TODO and #+TYP_TODO lines.
  1338. Each keyword can optionally specify a character for fast state selection
  1339. \(in combination with the variable `org-use-fast-todo-selection')
  1340. and specifiers for state change logging, using the same syntax
  1341. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1342. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1343. indicates to record a time stamp each time this state is selected.
  1344. Each keyword may also specify if a timestamp or a note should be
  1345. recorded when entering or leaving the state, by adding additional
  1346. characters in the parenthesis after the keyword. This looks like this:
  1347. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1348. record only the time of the state change. With X and Y being either
  1349. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1350. Y when leaving the state if and only if the *target* state does not
  1351. define X. You may omit any of the fast-selection key or X or /Y,
  1352. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1353. For backward compatibility, this variable may also be just a list
  1354. of keywords - in this case the interpretation (sequence or type) will be
  1355. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1356. :group 'org-todo
  1357. :group 'org-keywords
  1358. :type '(choice
  1359. (repeat :tag "Old syntax, just keywords"
  1360. (string :tag "Keyword"))
  1361. (repeat :tag "New syntax"
  1362. (cons
  1363. (choice
  1364. :tag "Interpretation"
  1365. ;;Quick and dirty way to see
  1366. ;;`org-todo-interpretations'. This takes the
  1367. ;;place of item arguments
  1368. :convert-widget
  1369. (lambda (widget)
  1370. (widget-put widget
  1371. :args (mapcar
  1372. #'(lambda (x)
  1373. (widget-convert
  1374. (cons 'const x)))
  1375. org-todo-interpretation-widgets))
  1376. widget))
  1377. (repeat
  1378. (string :tag "Keyword"))))))
  1379. (defvar org-todo-keywords-1 nil
  1380. "All TODO and DONE keywords active in a buffer.")
  1381. (make-variable-buffer-local 'org-todo-keywords-1)
  1382. (defvar org-todo-keywords-for-agenda nil)
  1383. (defvar org-done-keywords-for-agenda nil)
  1384. (defvar org-todo-keyword-alist-for-agenda nil)
  1385. (defvar org-tag-alist-for-agenda nil)
  1386. (defvar org-agenda-contributing-files nil)
  1387. (defvar org-not-done-keywords nil)
  1388. (make-variable-buffer-local 'org-not-done-keywords)
  1389. (defvar org-done-keywords nil)
  1390. (make-variable-buffer-local 'org-done-keywords)
  1391. (defvar org-todo-heads nil)
  1392. (make-variable-buffer-local 'org-todo-heads)
  1393. (defvar org-todo-sets nil)
  1394. (make-variable-buffer-local 'org-todo-sets)
  1395. (defvar org-todo-log-states nil)
  1396. (make-variable-buffer-local 'org-todo-log-states)
  1397. (defvar org-todo-kwd-alist nil)
  1398. (make-variable-buffer-local 'org-todo-kwd-alist)
  1399. (defvar org-todo-key-alist nil)
  1400. (make-variable-buffer-local 'org-todo-key-alist)
  1401. (defvar org-todo-key-trigger nil)
  1402. (make-variable-buffer-local 'org-todo-key-trigger)
  1403. (defcustom org-todo-interpretation 'sequence
  1404. "Controls how TODO keywords are interpreted.
  1405. This variable is in principle obsolete and is only used for
  1406. backward compatibility, if the interpretation of todo keywords is
  1407. not given already in `org-todo-keywords'. See that variable for
  1408. more information."
  1409. :group 'org-todo
  1410. :group 'org-keywords
  1411. :type '(choice (const sequence)
  1412. (const type)))
  1413. (defcustom org-use-fast-todo-selection t
  1414. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1415. This variable describes if and under what circumstances the cycling
  1416. mechanism for TODO keywords will be replaced by a single-key, direct
  1417. selection scheme.
  1418. When nil, fast selection is never used.
  1419. When the symbol `prefix', it will be used when `org-todo' is called with
  1420. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1421. in an agenda buffer.
  1422. When t, fast selection is used by default. In this case, the prefix
  1423. argument forces cycling instead.
  1424. In all cases, the special interface is only used if access keys have actually
  1425. been assigned by the user, i.e. if keywords in the configuration are followed
  1426. by a letter in parenthesis, like TODO(t)."
  1427. :group 'org-todo
  1428. :type '(choice
  1429. (const :tag "Never" nil)
  1430. (const :tag "By default" t)
  1431. (const :tag "Only with C-u C-c C-t" prefix)))
  1432. (defcustom org-provide-todo-statistics t
  1433. "Non-nil means, update todo statistics after insert and toggle.
  1434. When this is set, todo statistics is updated in the parent of the current
  1435. entry each time a todo state is changed."
  1436. :group 'org-todo
  1437. :type 'boolean)
  1438. (defcustom org-after-todo-state-change-hook nil
  1439. "Hook which is run after the state of a TODO item was changed.
  1440. The new state (a string with a TODO keyword, or nil) is available in the
  1441. Lisp variable `state'."
  1442. :group 'org-todo
  1443. :type 'hook)
  1444. (defvar org-blocker-hook nil
  1445. "Hook for functions that are allowed to block a state change.
  1446. Each function gets as its single argument a property list, see
  1447. `org-trigger-hook' for more information about this list.
  1448. If any of the functions in this hook returns nil, the state change
  1449. is blocked.")
  1450. (defvar org-trigger-hook nil
  1451. "Hook for functions that are triggered by a state change.
  1452. Each function gets as its single argument a property list with at least
  1453. the following elements:
  1454. (:type type-of-change :position pos-at-entry-start
  1455. :from old-state :to new-state)
  1456. Depending on the type, more properties may be present.
  1457. This mechanism is currently implemented for:
  1458. TODO state changes
  1459. ------------------
  1460. :type todo-state-change
  1461. :from previous state (keyword as a string), or nil, or a symbol
  1462. 'todo' or 'done', to indicate the general type of state.
  1463. :to new state, like in :from")
  1464. (defcustom org-enforce-todo-dependencies nil
  1465. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1466. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1467. be blocked if any prior sibling is not yet done.
  1468. This variable needs to be set before org.el is loaded, and you need to
  1469. restart Emacs after a change to make the change effective. The only way
  1470. to change is while Emacs is running is through the customize interface."
  1471. :set (lambda (var val)
  1472. (set var val)
  1473. (if val
  1474. (add-hook 'org-blocker-hook
  1475. 'org-block-todo-from-children-or-siblings)
  1476. (remove-hook 'org-blocker-hook
  1477. 'org-block-todo-from-children-or-siblings)))
  1478. :group 'org-todo
  1479. :type 'boolean)
  1480. (defcustom org-enforce-todo-checkbox-dependencies nil
  1481. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1482. When this is nil, checkboxes have no influence on switching TODO states.
  1483. When non-nil, you first need to check off all check boxes before the TODO
  1484. entry can be switched to DONE.
  1485. This variable needs to be set before org.el is loaded, and you need to
  1486. restart Emacs after a change to make the change effective. The only way
  1487. to change is while Emacs is running is through the customize interface."
  1488. :set (lambda (var val)
  1489. (set var val)
  1490. (if val
  1491. (add-hook 'org-blocker-hook
  1492. 'org-block-todo-from-checkboxes)
  1493. (remove-hook 'org-blocker-hook
  1494. 'org-block-todo-from-checkboxes)))
  1495. :group 'org-todo
  1496. :type 'boolean)
  1497. (defcustom org-todo-state-tags-triggers nil
  1498. "Tag changes that should be triggered by TODO state changes.
  1499. This is a list. Each entry is
  1500. (state-change (tag . flag) .......)
  1501. State-change can be a string with a state, and empty string to indicate the
  1502. state that has no TODO keyword, or it can be one of the symbols `todo'
  1503. or `done', meaning any not-done or done state, respectively."
  1504. :group 'org-todo
  1505. :group 'org-tags
  1506. :type '(repeat
  1507. (cons (choice :tag "When changing to"
  1508. (const :tag "Not-done state" todo)
  1509. (const :tag "Done state" done)
  1510. (string :tag "State"))
  1511. (repeat
  1512. (cons :tag "Tag action"
  1513. (string :tag "Tag")
  1514. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1515. (defcustom org-log-done nil
  1516. "Information to record when a task moves to the DONE state.
  1517. Possible values are:
  1518. nil Don't add anything, just change the keyword
  1519. time Add a time stamp to the task
  1520. note Prompt a closing note and add it with template `org-log-note-headings'
  1521. This option can also be set with on a per-file-basis with
  1522. #+STARTUP: nologdone
  1523. #+STARTUP: logdone
  1524. #+STARTUP: lognotedone
  1525. You can have local logging settings for a subtree by setting the LOGGING
  1526. property to one or more of these keywords."
  1527. :group 'org-todo
  1528. :group 'org-progress
  1529. :type '(choice
  1530. (const :tag "No logging" nil)
  1531. (const :tag "Record CLOSED timestamp" time)
  1532. (const :tag "Record CLOSED timestamp with closing note." note)))
  1533. ;; Normalize old uses of org-log-done.
  1534. (cond
  1535. ((eq org-log-done t) (setq org-log-done 'time))
  1536. ((and (listp org-log-done) (memq 'done org-log-done))
  1537. (setq org-log-done 'note)))
  1538. (defcustom org-log-note-clock-out nil
  1539. "Non-nil means, record a note when clocking out of an item.
  1540. This can also be configured on a per-file basis by adding one of
  1541. the following lines anywhere in the buffer:
  1542. #+STARTUP: lognoteclock-out
  1543. #+STARTUP: nolognoteclock-out"
  1544. :group 'org-todo
  1545. :group 'org-progress
  1546. :type 'boolean)
  1547. (defcustom org-log-done-with-time t
  1548. "Non-nil means, the CLOSED time stamp will contain date and time.
  1549. When nil, only the date will be recorded."
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-note-headings
  1553. '((done . "CLOSING NOTE %t")
  1554. (state . "State %-12s from %-12S %t")
  1555. (note . "Note taken on %t")
  1556. (clock-out . ""))
  1557. "Headings for notes added to entries.
  1558. The value is an alist, with the car being a symbol indicating the note
  1559. context, and the cdr is the heading to be used. The heading may also be the
  1560. empty string.
  1561. %t in the heading will be replaced by a time stamp.
  1562. %s will be replaced by the new TODO state, in double quotes.
  1563. %S will be replaced by the old TODO state, in double quotes.
  1564. %u will be replaced by the user name.
  1565. %U will be replaced by the full user name."
  1566. :group 'org-todo
  1567. :group 'org-progress
  1568. :type '(list :greedy t
  1569. (cons (const :tag "Heading when closing an item" done) string)
  1570. (cons (const :tag
  1571. "Heading when changing todo state (todo sequence only)"
  1572. state) string)
  1573. (cons (const :tag "Heading when just taking a note" note) string)
  1574. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1575. (unless (assq 'note org-log-note-headings)
  1576. (push '(note . "%t") org-log-note-headings))
  1577. (defcustom org-log-state-notes-into-drawer nil
  1578. "Non-nil means, insert state change notes and time stamps into a drawer.
  1579. When nil, state changes notes will be inserted after the headline and
  1580. any scheduling and clock lines, but not inside a drawer. When t,
  1581. notes
  1582. The value of this variable should be the name of the drawer to use.
  1583. LOGBOOK is proposed at the default drawer for this purpose, you can
  1584. also set this to a string to define the drawer of your choice.
  1585. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1586. will be ognored."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(choice
  1590. (const :tag "Not into a drawer" nil)
  1591. (const :tag "LOGBOOK" "LOGBOOK")
  1592. (string :tag "Other")))
  1593. (defcustom org-log-state-notes-insert-after-drawers nil
  1594. "Non-nil means, insert state change notes after any drawers in entry.
  1595. Only the drawers that *immediately* follow the headline and the
  1596. deadline/scheduled line are skipped.
  1597. When nil, insert notes right after the heading and perhaps the line
  1598. with deadline/scheduling if present.
  1599. This variable will have no effect if `org-log-state-notes-into-drawer' is
  1600. set."
  1601. :group 'org-todo
  1602. :group 'org-progress
  1603. :type 'boolean)
  1604. (defcustom org-log-states-order-reversed t
  1605. "Non-nil means, the latest state change note will be directly after heading.
  1606. When nil, the notes will be orderer according to time."
  1607. :group 'org-todo
  1608. :group 'org-progress
  1609. :type 'boolean)
  1610. (defcustom org-log-repeat 'time
  1611. "Non-nil means, record moving through the DONE state when triggering repeat.
  1612. An auto-repeating tasks is immediately switched back to TODO when marked
  1613. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1614. the TODO keyword definition, or recording a closing note by setting
  1615. `org-log-done', there will be no record of the task moving through DONE.
  1616. This variable forces taking a note anyway. Possible values are:
  1617. nil Don't force a record
  1618. time Record a time stamp
  1619. note Record a note
  1620. This option can also be set with on a per-file-basis with
  1621. #+STARTUP: logrepeat
  1622. #+STARTUP: lognoterepeat
  1623. #+STARTUP: nologrepeat
  1624. You can have local logging settings for a subtree by setting the LOGGING
  1625. property to one or more of these keywords."
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type '(choice
  1629. (const :tag "Don't force a record" nil)
  1630. (const :tag "Force recording the DONE state" time)
  1631. (const :tag "Force recording a note with the DONE state" note)))
  1632. (defgroup org-priorities nil
  1633. "Priorities in Org-mode."
  1634. :tag "Org Priorities"
  1635. :group 'org-todo)
  1636. (defcustom org-highest-priority ?A
  1637. "The highest priority of TODO items. A character like ?A, ?B etc.
  1638. Must have a smaller ASCII number than `org-lowest-priority'."
  1639. :group 'org-priorities
  1640. :type 'character)
  1641. (defcustom org-lowest-priority ?C
  1642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1643. Must have a larger ASCII number than `org-highest-priority'."
  1644. :group 'org-priorities
  1645. :type 'character)
  1646. (defcustom org-default-priority ?B
  1647. "The default priority of TODO items.
  1648. This is the priority an item get if no explicit priority is given."
  1649. :group 'org-priorities
  1650. :type 'character)
  1651. (defcustom org-priority-start-cycle-with-default t
  1652. "Non-nil means, start with default priority when starting to cycle.
  1653. When this is nil, the first step in the cycle will be (depending on the
  1654. command used) one higher or lower that the default priority."
  1655. :group 'org-priorities
  1656. :type 'boolean)
  1657. (defgroup org-time nil
  1658. "Options concerning time stamps and deadlines in Org-mode."
  1659. :tag "Org Time"
  1660. :group 'org)
  1661. (defcustom org-insert-labeled-timestamps-at-point nil
  1662. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1663. When nil, these labeled time stamps are forces into the second line of an
  1664. entry, just after the headline. When scheduling from the global TODO list,
  1665. the time stamp will always be forced into the second line."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1669. "Formats for `format-time-string' which are used for time stamps.
  1670. It is not recommended to change this constant.")
  1671. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1672. "Number of minutes to round time stamps to.
  1673. These are two values, the first applies when first creating a time stamp.
  1674. The second applies when changing it with the commands `S-up' and `S-down'.
  1675. When changing the time stamp, this means that it will change in steps
  1676. of N minutes, as given by the second value.
  1677. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1678. numbers should be factors of 60, so for example 5, 10, 15.
  1679. When this is larger than 1, you can still force an exact time-stamp by using
  1680. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1681. and by using a prefix arg to `S-up/down' to specify the exact number
  1682. of minutes to shift."
  1683. :group 'org-time
  1684. :get '(lambda (var) ; Make sure all entries have 5 elements
  1685. (if (integerp (default-value var))
  1686. (list (default-value var) 5)
  1687. (default-value var)))
  1688. :type '(list
  1689. (integer :tag "when inserting times")
  1690. (integer :tag "when modifying times")))
  1691. ;; Normalize old customizations of this variable.
  1692. (when (integerp org-time-stamp-rounding-minutes)
  1693. (setq org-time-stamp-rounding-minutes
  1694. (list org-time-stamp-rounding-minutes
  1695. org-time-stamp-rounding-minutes)))
  1696. (defcustom org-display-custom-times nil
  1697. "Non-nil means, overlay custom formats over all time stamps.
  1698. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1699. To turn this on on a per-file basis, insert anywhere in the file:
  1700. #+STARTUP: customtime"
  1701. :group 'org-time
  1702. :set 'set-default
  1703. :type 'sexp)
  1704. (make-variable-buffer-local 'org-display-custom-times)
  1705. (defcustom org-time-stamp-custom-formats
  1706. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1707. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1708. These are overlayed over the default ISO format if the variable
  1709. `org-display-custom-times' is set. Time like %H:%M should be at the
  1710. end of the second format."
  1711. :group 'org-time
  1712. :type 'sexp)
  1713. (defun org-time-stamp-format (&optional long inactive)
  1714. "Get the right format for a time string."
  1715. (let ((f (if long (cdr org-time-stamp-formats)
  1716. (car org-time-stamp-formats))))
  1717. (if inactive
  1718. (concat "[" (substring f 1 -1) "]")
  1719. f)))
  1720. (defcustom org-time-clocksum-format "%d:%02d"
  1721. "The format string used when creating CLOCKSUM lines, or when
  1722. org-mode generates a time duration."
  1723. :group 'org-time
  1724. :type 'string)
  1725. (defcustom org-deadline-warning-days 14
  1726. "No. of days before expiration during which a deadline becomes active.
  1727. This variable governs the display in sparse trees and in the agenda.
  1728. When 0 or negative, it means use this number (the absolute value of it)
  1729. even if a deadline has a different individual lead time specified."
  1730. :group 'org-time
  1731. :group 'org-agenda-daily/weekly
  1732. :type 'number)
  1733. (defcustom org-read-date-prefer-future t
  1734. "Non-nil means, assume future for incomplete date input from user.
  1735. This affects the following situations:
  1736. 1. The user gives a day, but no month.
  1737. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1738. read this as the third of *next* month. However, if you enter \"17\",
  1739. it will be considered as *this* month.
  1740. 2. The user gives a month but not a year.
  1741. For example, if it is april and you enter \"feb 2\", this will be read
  1742. as feb 2, *next* year. \"May 5\", however, will be this year.
  1743. Currently this does not work for ISO week specifications.
  1744. When this option is nil, the current month and year will always be used
  1745. as defaults."
  1746. :group 'org-time
  1747. :type 'boolean)
  1748. (defcustom org-read-date-display-live t
  1749. "Non-nil means, display current interpretation of date prompt live.
  1750. This display will be in an overlay, in the minibuffer."
  1751. :group 'org-time
  1752. :type 'boolean)
  1753. (defcustom org-read-date-popup-calendar t
  1754. "Non-nil means, pop up a calendar when prompting for a date.
  1755. In the calendar, the date can be selected with mouse-1. However, the
  1756. minibuffer will also be active, and you can simply enter the date as well.
  1757. When nil, only the minibuffer will be available."
  1758. :group 'org-time
  1759. :type 'boolean)
  1760. (if (fboundp 'defvaralias)
  1761. (defvaralias 'org-popup-calendar-for-date-prompt
  1762. 'org-read-date-popup-calendar))
  1763. (defcustom org-extend-today-until 0
  1764. "The hour when your day really ends. Must be an integer.
  1765. This has influence for the following applications:
  1766. - When switching the agenda to \"today\". It it is still earlier than
  1767. the time given here, the day recognized as TODAY is actually yesterday.
  1768. - When a date is read from the user and it is still before the time given
  1769. here, the current date and time will be assumed to be yesterday, 23:59.
  1770. Also, timestamps inserted in remember templates follow this rule.
  1771. IMPORTANT: This is a feature whose implementation is and likely will
  1772. remain incomplete. Really, it is only here because past midnight seems to
  1773. be the favorite working time of John Wiegley :-)"
  1774. :group 'org-time
  1775. :type 'number)
  1776. (defcustom org-edit-timestamp-down-means-later nil
  1777. "Non-nil means, S-down will increase the time in a time stamp.
  1778. When nil, S-up will increase."
  1779. :group 'org-time
  1780. :type 'boolean)
  1781. (defcustom org-calendar-follow-timestamp-change t
  1782. "Non-nil means, make the calendar window follow timestamp changes.
  1783. When a timestamp is modified and the calendar window is visible, it will be
  1784. moved to the new date."
  1785. :group 'org-time
  1786. :type 'boolean)
  1787. (defgroup org-tags nil
  1788. "Options concerning tags in Org-mode."
  1789. :tag "Org Tags"
  1790. :group 'org)
  1791. (defcustom org-tag-alist nil
  1792. "List of tags allowed in Org-mode files.
  1793. When this list is nil, Org-mode will base TAG input on what is already in the
  1794. buffer.
  1795. The value of this variable is an alist, the car of each entry must be a
  1796. keyword as a string, the cdr may be a character that is used to select
  1797. that tag through the fast-tag-selection interface.
  1798. See the manual for details."
  1799. :group 'org-tags
  1800. :type '(repeat
  1801. (choice
  1802. (cons (string :tag "Tag name")
  1803. (character :tag "Access char"))
  1804. (const :tag "Start radio group" (:startgroup))
  1805. (const :tag "End radio group" (:endgroup)))))
  1806. (defvar org-file-tags nil
  1807. "List of tags that can be inherited by all entries in the file.
  1808. The tags will be inherited if the variable `org-use-tag-inheritance'
  1809. says they should be.
  1810. This variable is populated from #+TAG lines.")
  1811. (defcustom org-use-fast-tag-selection 'auto
  1812. "Non-nil means, use fast tag selection scheme.
  1813. This is a special interface to select and deselect tags with single keys.
  1814. When nil, fast selection is never used.
  1815. When the symbol `auto', fast selection is used if and only if selection
  1816. characters for tags have been configured, either through the variable
  1817. `org-tag-alist' or through a #+TAGS line in the buffer.
  1818. When t, fast selection is always used and selection keys are assigned
  1819. automatically if necessary."
  1820. :group 'org-tags
  1821. :type '(choice
  1822. (const :tag "Always" t)
  1823. (const :tag "Never" nil)
  1824. (const :tag "When selection characters are configured" 'auto)))
  1825. (defcustom org-fast-tag-selection-single-key nil
  1826. "Non-nil means, fast tag selection exits after first change.
  1827. When nil, you have to press RET to exit it.
  1828. During fast tag selection, you can toggle this flag with `C-c'.
  1829. This variable can also have the value `expert'. In this case, the window
  1830. displaying the tags menu is not even shown, until you press C-c again."
  1831. :group 'org-tags
  1832. :type '(choice
  1833. (const :tag "No" nil)
  1834. (const :tag "Yes" t)
  1835. (const :tag "Expert" expert)))
  1836. (defvar org-fast-tag-selection-include-todo nil
  1837. "Non-nil means, fast tags selection interface will also offer TODO states.
  1838. This is an undocumented feature, you should not rely on it.")
  1839. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1840. "The column to which tags should be indented in a headline.
  1841. If this number is positive, it specifies the column. If it is negative,
  1842. it means that the tags should be flushright to that column. For example,
  1843. -80 works well for a normal 80 character screen."
  1844. :group 'org-tags
  1845. :type 'integer)
  1846. (defcustom org-auto-align-tags t
  1847. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1848. These operations change the length of a headline and therefore shift
  1849. the tags around. With this options turned on, after each such operation
  1850. the tags are again aligned to `org-tags-column'."
  1851. :group 'org-tags
  1852. :type 'boolean)
  1853. (defcustom org-use-tag-inheritance t
  1854. "Non-nil means, tags in levels apply also for sublevels.
  1855. When nil, only the tags directly given in a specific line apply there.
  1856. This may also be a list of tags that should be inherited, or a regexp that
  1857. matches tags that should be inherited. Additional control is possible
  1858. with the variable `org-tags-exclude-from-inheritance' which gives an
  1859. explicit list of tags to be excluded from inheritance., even if the value of
  1860. `org-use-tag-inheritance' would select it for inheritance.
  1861. If this option is t, a match early-on in a tree can lead to a large
  1862. number of matches in the subtree when constructing the agenda or creating
  1863. a sparse tree. If you only want to see the first match in a tree during
  1864. a search, check out the variable `org-tags-match-list-sublevels'."
  1865. :group 'org-tags
  1866. :type '(choice
  1867. (const :tag "Not" nil)
  1868. (const :tag "Always" t)
  1869. (repeat :tag "Specific tags" (string :tag "Tag"))
  1870. (regexp :tag "Tags matched by regexp")))
  1871. (defcustom org-tags-exclude-from-inheritance nil
  1872. "List of tags that should never be inherited.
  1873. This is a way to exclude a few tags from inheritance. For way to do
  1874. the opposite, to actively allow inheritance for selected tags,
  1875. see the variable `org-use-tag-inheritance'."
  1876. :group 'org-tags
  1877. :type '(repeat (string :tag "Tag")))
  1878. (defun org-tag-inherit-p (tag)
  1879. "Check if TAG is one that should be inherited."
  1880. (cond
  1881. ((member tag org-tags-exclude-from-inheritance) nil)
  1882. ((eq org-use-tag-inheritance t) t)
  1883. ((not org-use-tag-inheritance) nil)
  1884. ((stringp org-use-tag-inheritance)
  1885. (string-match org-use-tag-inheritance tag))
  1886. ((listp org-use-tag-inheritance)
  1887. (member tag org-use-tag-inheritance))
  1888. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1889. (defcustom org-tags-match-list-sublevels t
  1890. "Non-nil means list also sublevels of headlines matching tag search.
  1891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1892. the sublevels of a headline matching a tag search often also match
  1893. the same search. Listing all of them can create very long lists.
  1894. Setting this variable to nil causes subtrees of a match to be skipped.
  1895. This option is off by default, because inheritance in on. If you turn
  1896. inheritance off, you very likely want to turn this option on.
  1897. As a special case, if the tag search is restricted to TODO items, the
  1898. value of this variable is ignored and sublevels are always checked, to
  1899. make sure all corresponding TODO items find their way into the list.
  1900. This variable is semi-obsolete and probably should always be true. It
  1901. is better to limit inheritance to certain tags using the variables
  1902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1903. :group 'org-tags
  1904. :type 'boolean)
  1905. (defvar org-tags-history nil
  1906. "History of minibuffer reads for tags.")
  1907. (defvar org-last-tags-completion-table nil
  1908. "The last used completion table for tags.")
  1909. (defvar org-after-tags-change-hook nil
  1910. "Hook that is run after the tags in a line have changed.")
  1911. (defgroup org-properties nil
  1912. "Options concerning properties in Org-mode."
  1913. :tag "Org Properties"
  1914. :group 'org)
  1915. (defcustom org-property-format "%-10s %s"
  1916. "How property key/value pairs should be formatted by `indent-line'.
  1917. When `indent-line' hits a property definition, it will format the line
  1918. according to this format, mainly to make sure that the values are
  1919. lined-up with respect to each other."
  1920. :group 'org-properties
  1921. :type 'string)
  1922. (defcustom org-use-property-inheritance nil
  1923. "Non-nil means, properties apply also for sublevels.
  1924. This setting is chiefly used during property searches. Turning it on can
  1925. cause significant overhead when doing a search, which is why it is not
  1926. on by default.
  1927. When nil, only the properties directly given in the current entry count.
  1928. When t, every property is inherited. The value may also be a list of
  1929. properties that should have inheritance, or a regular expression matching
  1930. properties that should be inherited.
  1931. However, note that some special properties use inheritance under special
  1932. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1933. and the properties ending in \"_ALL\" when they are used as descriptor
  1934. for valid values of a property.
  1935. Note for programmers:
  1936. When querying an entry with `org-entry-get', you can control if inheritance
  1937. should be used. By default, `org-entry-get' looks only at the local
  1938. properties. You can request inheritance by setting the inherit argument
  1939. to t (to force inheritance) or to `selective' (to respect the setting
  1940. in this variable)."
  1941. :group 'org-properties
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Always" t)
  1945. (repeat :tag "Specific properties" (string :tag "Property"))
  1946. (regexp :tag "Properties matched by regexp")))
  1947. (defun org-property-inherit-p (property)
  1948. "Check if PROPERTY is one that should be inherited."
  1949. (cond
  1950. ((eq org-use-property-inheritance t) t)
  1951. ((not org-use-property-inheritance) nil)
  1952. ((stringp org-use-property-inheritance)
  1953. (string-match org-use-property-inheritance property))
  1954. ((listp org-use-property-inheritance)
  1955. (member property org-use-property-inheritance))
  1956. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1957. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1958. "The default column format, if no other format has been defined.
  1959. This variable can be set on the per-file basis by inserting a line
  1960. #+COLUMNS: %25ITEM ....."
  1961. :group 'org-properties
  1962. :type 'string)
  1963. (defcustom org-columns-ellipses ".."
  1964. "The ellipses to be used when a field in column view is truncated.
  1965. When this is the empty string, as many characters as possible are shown,
  1966. but then there will be no visual indication that the field has been truncated.
  1967. When this is a string of length N, the last N characters of a truncated
  1968. field are replaced by this string. If the column is narrower than the
  1969. ellipses string, only part of the ellipses string will be shown."
  1970. :group 'org-properties
  1971. :type 'string)
  1972. (defcustom org-columns-modify-value-for-display-function nil
  1973. "Function that modifies values for display in column view.
  1974. For example, it can be used to cut out a certain part from a time stamp.
  1975. The function must take 2 arguments:
  1976. column-title The title of the column (*not* the property name)
  1977. value The value that should be modified.
  1978. The function should return the value that should be displayed,
  1979. or nil if the normal value should be used."
  1980. :group 'org-properties
  1981. :type 'function)
  1982. (defcustom org-effort-property "Effort"
  1983. "The property that is being used to keep track of effort estimates.
  1984. Effort estimates given in this property need to have the format H:MM."
  1985. :group 'org-properties
  1986. :group 'org-progress
  1987. :type '(string :tag "Property"))
  1988. (defconst org-global-properties-fixed
  1989. '(("VISIBILITY_ALL" . "folded children content all"))
  1990. "List of property/value pairs that can be inherited by any entry.
  1991. These are fixed values, for the preset properties.")
  1992. (defcustom org-global-properties nil
  1993. "List of property/value pairs that can be inherited by any entry.
  1994. You can set buffer-local values for the same purpose in the variable
  1995. `org-file-properties' this by adding lines like
  1996. #+PROPERTY: NAME VALUE"
  1997. :group 'org-properties
  1998. :type '(repeat
  1999. (cons (string :tag "Property")
  2000. (string :tag "Value"))))
  2001. (defvar org-file-properties nil
  2002. "List of property/value pairs that can be inherited by any entry.
  2003. Valid for the current buffer.
  2004. This variable is populated from #+PROPERTY lines.")
  2005. (make-variable-buffer-local 'org-file-properties)
  2006. (defgroup org-agenda nil
  2007. "Options concerning agenda views in Org-mode."
  2008. :tag "Org Agenda"
  2009. :group 'org)
  2010. (defvar org-category nil
  2011. "Variable used by org files to set a category for agenda display.
  2012. Such files should use a file variable to set it, for example
  2013. # -*- mode: org; org-category: \"ELisp\"
  2014. or contain a special line
  2015. #+CATEGORY: ELisp
  2016. If the file does not specify a category, then file's base name
  2017. is used instead.")
  2018. (make-variable-buffer-local 'org-category)
  2019. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2020. (defcustom org-agenda-files nil
  2021. "The files to be used for agenda display.
  2022. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2023. \\[org-remove-file]. You can also use customize to edit the list.
  2024. If an entry is a directory, all files in that directory that are matched by
  2025. `org-agenda-file-regexp' will be part of the file list.
  2026. If the value of the variable is not a list but a single file name, then
  2027. the list of agenda files is actually stored and maintained in that file, one
  2028. agenda file per line."
  2029. :group 'org-agenda
  2030. :type '(choice
  2031. (repeat :tag "List of files and directories" file)
  2032. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2033. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2034. "Regular expression to match files for `org-agenda-files'.
  2035. If any element in the list in that variable contains a directory instead
  2036. of a normal file, all files in that directory that are matched by this
  2037. regular expression will be included."
  2038. :group 'org-agenda
  2039. :type 'regexp)
  2040. (defcustom org-agenda-text-search-extra-files nil
  2041. "List of extra files to be searched by text search commands.
  2042. These files will be search in addition to the agenda files by the
  2043. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2044. Note that these files will only be searched for text search commands,
  2045. not for the other agenda views like todo lists, tag searches or the weekly
  2046. agenda. This variable is intended to list notes and possibly archive files
  2047. that should also be searched by these two commands.
  2048. In fact, if the first element in the list is the symbol `agenda-archives',
  2049. than all archive files of all agenda files will be added to the search
  2050. scope."
  2051. :group 'org-agenda
  2052. :type '(set :greedy t
  2053. (const :tag "Agenda Archives" agenda-archives)
  2054. (repeat :inline t (file))))
  2055. (if (fboundp 'defvaralias)
  2056. (defvaralias 'org-agenda-multi-occur-extra-files
  2057. 'org-agenda-text-search-extra-files))
  2058. (defcustom org-agenda-skip-unavailable-files nil
  2059. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2060. A nil value means to remove them, after a query, from the list."
  2061. :group 'org-agenda
  2062. :type 'boolean)
  2063. (defcustom org-calendar-to-agenda-key [?c]
  2064. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2065. The command `org-calendar-goto-agenda' will be bound to this key. The
  2066. default is the character `c' because then `c' can be used to switch back and
  2067. forth between agenda and calendar."
  2068. :group 'org-agenda
  2069. :type 'sexp)
  2070. (defcustom org-calendar-agenda-action-key [?k]
  2071. "The key to be installed in `calendar-mode-map' for agenda-action.
  2072. The command `org-agenda-action' will be bound to this key. The
  2073. default is the character `k' because we use the same key in the agenda."
  2074. :group 'org-agenda
  2075. :type 'sexp)
  2076. (eval-after-load "calendar"
  2077. '(progn
  2078. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2079. 'org-calendar-goto-agenda)
  2080. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2081. 'org-agenda-action)))
  2082. (defgroup org-latex nil
  2083. "Options for embedding LaTeX code into Org-mode."
  2084. :tag "Org LaTeX"
  2085. :group 'org)
  2086. (defcustom org-format-latex-options
  2087. '(:foreground default :background default :scale 1.0
  2088. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2089. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2090. "Options for creating images from LaTeX fragments.
  2091. This is a property list with the following properties:
  2092. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2093. `default' means use the foreground of the default face.
  2094. :background the background color, or \"Transparent\".
  2095. `default' means use the background of the default face.
  2096. :scale a scaling factor for the size of the images.
  2097. :html-foreground, :html-background, :html-scale
  2098. the same numbers for HTML export.
  2099. :matchers a list indicating which matchers should be used to
  2100. find LaTeX fragments. Valid members of this list are:
  2101. \"begin\" find environments
  2102. \"$1\" find single characters surrounded by $.$
  2103. \"$\" find math expressions surrounded by $...$
  2104. \"$$\" find math expressions surrounded by $$....$$
  2105. \"\\(\" find math expressions surrounded by \\(...\\)
  2106. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2107. :group 'org-latex
  2108. :type 'plist)
  2109. (defcustom org-format-latex-header "\\documentclass{article}
  2110. \\usepackage{fullpage} % do not remove
  2111. \\usepackage{amssymb}
  2112. \\usepackage[usenames]{color}
  2113. \\usepackage{amsmath}
  2114. \\usepackage{latexsym}
  2115. \\usepackage[mathscr]{eucal}
  2116. \\pagestyle{empty} % do not remove"
  2117. "The document header used for processing LaTeX fragments."
  2118. :group 'org-latex
  2119. :type 'string)
  2120. (defgroup org-font-lock nil
  2121. "Font-lock settings for highlighting in Org-mode."
  2122. :tag "Org Font Lock"
  2123. :group 'org)
  2124. (defcustom org-level-color-stars-only nil
  2125. "Non-nil means fontify only the stars in each headline.
  2126. When nil, the entire headline is fontified.
  2127. Changing it requires restart of `font-lock-mode' to become effective
  2128. also in regions already fontified."
  2129. :group 'org-font-lock
  2130. :type 'boolean)
  2131. (defcustom org-hide-leading-stars nil
  2132. "Non-nil means, hide the first N-1 stars in a headline.
  2133. This works by using the face `org-hide' for these stars. This
  2134. face is white for a light background, and black for a dark
  2135. background. You may have to customize the face `org-hide' to
  2136. make this work.
  2137. Changing it requires restart of `font-lock-mode' to become effective
  2138. also in regions already fontified.
  2139. You may also set this on a per-file basis by adding one of the following
  2140. lines to the buffer:
  2141. #+STARTUP: hidestars
  2142. #+STARTUP: showstars"
  2143. :group 'org-font-lock
  2144. :type 'boolean)
  2145. (defcustom org-fontify-done-headline nil
  2146. "Non-nil means, change the face of a headline if it is marked DONE.
  2147. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2148. When this is non-nil, the headline after the keyword is set to the
  2149. `org-headline-done' as an additional indication."
  2150. :group 'org-font-lock
  2151. :type 'boolean)
  2152. (defcustom org-fontify-emphasized-text t
  2153. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2154. Changing this variable requires a restart of Emacs to take effect."
  2155. :group 'org-font-lock
  2156. :type 'boolean)
  2157. (defcustom org-highlight-latex-fragments-and-specials nil
  2158. "Non-nil means, fontify what is treated specially by the exporters."
  2159. :group 'org-font-lock
  2160. :type 'boolean)
  2161. (defcustom org-hide-emphasis-markers nil
  2162. "Non-nil mean font-lock should hide the emphasis marker characters."
  2163. :group 'org-font-lock
  2164. :type 'boolean)
  2165. (defvar org-emph-re nil
  2166. "Regular expression for matching emphasis.")
  2167. (defvar org-verbatim-re nil
  2168. "Regular expression for matching verbatim text.")
  2169. (defvar org-emphasis-regexp-components) ; defined just below
  2170. (defvar org-emphasis-alist) ; defined just below
  2171. (defun org-set-emph-re (var val)
  2172. "Set variable and compute the emphasis regular expression."
  2173. (set var val)
  2174. (when (and (boundp 'org-emphasis-alist)
  2175. (boundp 'org-emphasis-regexp-components)
  2176. org-emphasis-alist org-emphasis-regexp-components)
  2177. (let* ((e org-emphasis-regexp-components)
  2178. (pre (car e))
  2179. (post (nth 1 e))
  2180. (border (nth 2 e))
  2181. (body (nth 3 e))
  2182. (nl (nth 4 e))
  2183. (body1 (concat body "*?"))
  2184. (markers (mapconcat 'car org-emphasis-alist ""))
  2185. (vmarkers (mapconcat
  2186. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2187. org-emphasis-alist "")))
  2188. ;; make sure special characters appear at the right position in the class
  2189. (if (string-match "\\^" markers)
  2190. (setq markers (concat (replace-match "" t t markers) "^")))
  2191. (if (string-match "-" markers)
  2192. (setq markers (concat (replace-match "" t t markers) "-")))
  2193. (if (string-match "\\^" vmarkers)
  2194. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2195. (if (string-match "-" vmarkers)
  2196. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2197. (if (> nl 0)
  2198. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2199. (int-to-string nl) "\\}")))
  2200. ;; Make the regexp
  2201. (setq org-emph-re
  2202. (concat "\\([" pre "]\\|^\\)"
  2203. "\\("
  2204. "\\([" markers "]\\)"
  2205. "\\("
  2206. "[^" border "]\\|"
  2207. "[^" border "]"
  2208. body1
  2209. "[^" border "]"
  2210. "\\)"
  2211. "\\3\\)"
  2212. "\\([" post "]\\|$\\)"))
  2213. (setq org-verbatim-re
  2214. (concat "\\([" pre "]\\|^\\)"
  2215. "\\("
  2216. "\\([" vmarkers "]\\)"
  2217. "\\("
  2218. "[^" border "]\\|"
  2219. "[^" border "]"
  2220. body1
  2221. "[^" border "]"
  2222. "\\)"
  2223. "\\3\\)"
  2224. "\\([" post "]\\|$\\)")))))
  2225. (defcustom org-emphasis-regexp-components
  2226. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2227. "Components used to build the regular expression for emphasis.
  2228. This is a list with 6 entries. Terminology: In an emphasis string
  2229. like \" *strong word* \", we call the initial space PREMATCH, the final
  2230. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2231. and \"trong wor\" is the body. The different components in this variable
  2232. specify what is allowed/forbidden in each part:
  2233. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2234. post Chars allowed as postmatch. End of line will be allowed too.
  2235. border The chars *forbidden* as border characters.
  2236. body-regexp A regexp like \".\" to match a body character. Don't use
  2237. non-shy groups here, and don't allow newline here.
  2238. newline The maximum number of newlines allowed in an emphasis exp.
  2239. Use customize to modify this, or restart Emacs after changing it."
  2240. :group 'org-font-lock
  2241. :set 'org-set-emph-re
  2242. :type '(list
  2243. (sexp :tag "Allowed chars in pre ")
  2244. (sexp :tag "Allowed chars in post ")
  2245. (sexp :tag "Forbidden chars in border ")
  2246. (sexp :tag "Regexp for body ")
  2247. (integer :tag "number of newlines allowed")
  2248. (option (boolean :tag "Please ignore this button"))))
  2249. (defcustom org-emphasis-alist
  2250. `(("*" bold "<b>" "</b>")
  2251. ("/" italic "<i>" "</i>")
  2252. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2253. ("=" org-code "<code>" "</code>" verbatim)
  2254. ("~" org-verbatim "<code>" "</code>" verbatim)
  2255. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2256. "<del>" "</del>")
  2257. )
  2258. "Special syntax for emphasized text.
  2259. Text starting and ending with a special character will be emphasized, for
  2260. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2261. characters, the face to be used by font-lock for highlighting in Org-mode
  2262. Emacs buffers, and the HTML tags to be used for this.
  2263. Use customize to modify this, or restart Emacs after changing it."
  2264. :group 'org-font-lock
  2265. :set 'org-set-emph-re
  2266. :type '(repeat
  2267. (list
  2268. (string :tag "Marker character")
  2269. (choice
  2270. (face :tag "Font-lock-face")
  2271. (plist :tag "Face property list"))
  2272. (string :tag "HTML start tag")
  2273. (string :tag "HTML end tag")
  2274. (option (const verbatim)))))
  2275. ;;; Miscellaneous options
  2276. (defgroup org-completion nil
  2277. "Completion in Org-mode."
  2278. :tag "Org Completion"
  2279. :group 'org)
  2280. (defcustom org-completion-use-ido nil
  2281. "Non-nil means, use ido completion wherever possible.
  2282. Note that `ido-mode' must be active for this variable to be relevant.
  2283. If you decide to turn this variable on, you might well want to turn off
  2284. `org-outline-path-complete-in-steps'."
  2285. :group 'org-completion
  2286. :type 'boolean)
  2287. (defcustom org-completion-fallback-command 'hippie-expand
  2288. "The expansion command called by \\[org-complete] in normal context.
  2289. Normal means, no org-mode-specific context."
  2290. :group 'org-completion
  2291. :type 'function)
  2292. ;;; Functions and variables from ther packages
  2293. ;; Declared here to avoid compiler warnings
  2294. ;; XEmacs only
  2295. (defvar outline-mode-menu-heading)
  2296. (defvar outline-mode-menu-show)
  2297. (defvar outline-mode-menu-hide)
  2298. (defvar zmacs-regions) ; XEmacs regions
  2299. ;; Emacs only
  2300. (defvar mark-active)
  2301. ;; Various packages
  2302. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2303. (declare-function calendar-forward-day "cal-move" (arg))
  2304. (declare-function calendar-goto-date "cal-move" (date))
  2305. (declare-function calendar-goto-today "cal-move" ())
  2306. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2307. (defvar calc-embedded-close-formula)
  2308. (defvar calc-embedded-open-formula)
  2309. (declare-function cdlatex-tab "ext:cdlatex" ())
  2310. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2311. (defvar font-lock-unfontify-region-function)
  2312. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2313. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2314. (defvar iswitchb-temp-buflist)
  2315. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2316. (defvar org-agenda-tags-todo-honor-ignore-options)
  2317. (declare-function org-agenda-skip "org-agenda" ())
  2318. (declare-function org-format-agenda-item "org-agenda"
  2319. (extra txt &optional category tags dotime noprefix remove-re))
  2320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2321. (declare-function org-agenda-change-all-lines "org-agenda"
  2322. (newhead hdmarker &optional fixface just-this))
  2323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2326. (beg end))
  2327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2329. "org-agenda" (&optional end))
  2330. (declare-function parse-time-string "parse-time" (string))
  2331. (declare-function remember "remember" (&optional initial))
  2332. (declare-function remember-buffer-desc "remember" ())
  2333. (declare-function remember-finalize "remember" ())
  2334. (defvar remember-save-after-remembering)
  2335. (defvar remember-data-file)
  2336. (defvar remember-register)
  2337. (defvar remember-buffer)
  2338. (defvar remember-handler-functions)
  2339. (defvar remember-annotation-functions)
  2340. (defvar texmathp-why)
  2341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2343. (defvar w3m-current-url)
  2344. (defvar w3m-current-title)
  2345. (defvar org-latex-regexps)
  2346. ;;; Autoload and prepare some org modules
  2347. ;; Some table stuff that needs to be defined here, because it is used
  2348. ;; by the functions setting up org-mode or checking for table context.
  2349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2350. "Detects an org-type or table-type table.")
  2351. (defconst org-table-line-regexp "^[ \t]*|"
  2352. "Detects an org-type table line.")
  2353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2354. "Detects an org-type table line.")
  2355. (defconst org-table-hline-regexp "^[ \t]*|-"
  2356. "Detects an org-type table hline.")
  2357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2358. "Detects a table-type table hline.")
  2359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2360. "Searching from within a table (any type) this finds the first line
  2361. outside the table.")
  2362. ;; Autoload the functions in org-table.el that are needed by functions here.
  2363. (eval-and-compile
  2364. (org-autoload "org-table"
  2365. '(org-table-align org-table-begin org-table-blank-field
  2366. org-table-convert org-table-convert-region org-table-copy-down
  2367. org-table-copy-region org-table-create
  2368. org-table-create-or-convert-from-region
  2369. org-table-create-with-table.el org-table-current-dline
  2370. org-table-cut-region org-table-delete-column org-table-edit-field
  2371. org-table-edit-formulas org-table-end org-table-eval-formula
  2372. org-table-export org-table-field-info
  2373. org-table-get-stored-formulas org-table-goto-column
  2374. org-table-hline-and-move org-table-import org-table-insert-column
  2375. org-table-insert-hline org-table-insert-row org-table-iterate
  2376. org-table-justify-field-maybe org-table-kill-row
  2377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2378. org-table-move-column org-table-move-column-left
  2379. org-table-move-column-right org-table-move-row
  2380. org-table-move-row-down org-table-move-row-up
  2381. org-table-next-field org-table-next-row org-table-paste-rectangle
  2382. org-table-previous-field org-table-recalculate
  2383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2384. org-table-toggle-coordinate-overlays
  2385. org-table-toggle-formula-debugger org-table-wrap-region
  2386. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2387. (defun org-at-table-p (&optional table-type)
  2388. "Return t if the cursor is inside an org-type table.
  2389. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2390. (if org-enable-table-editor
  2391. (save-excursion
  2392. (beginning-of-line 1)
  2393. (looking-at (if table-type org-table-any-line-regexp
  2394. org-table-line-regexp)))
  2395. nil))
  2396. (defsubst org-table-p () (org-at-table-p))
  2397. (defun org-at-table.el-p ()
  2398. "Return t if and only if we are at a table.el table."
  2399. (and (org-at-table-p 'any)
  2400. (save-excursion
  2401. (goto-char (org-table-begin 'any))
  2402. (looking-at org-table1-hline-regexp))))
  2403. (defun org-table-recognize-table.el ()
  2404. "If there is a table.el table nearby, recognize it and move into it."
  2405. (if org-table-tab-recognizes-table.el
  2406. (if (org-at-table.el-p)
  2407. (progn
  2408. (beginning-of-line 1)
  2409. (if (looking-at org-table-dataline-regexp)
  2410. nil
  2411. (if (looking-at org-table1-hline-regexp)
  2412. (progn
  2413. (beginning-of-line 2)
  2414. (if (looking-at org-table-any-border-regexp)
  2415. (beginning-of-line -1)))))
  2416. (if (re-search-forward "|" (org-table-end t) t)
  2417. (progn
  2418. (require 'table)
  2419. (if (table--at-cell-p (point))
  2420. t
  2421. (message "recognizing table.el table...")
  2422. (table-recognize-table)
  2423. (message "recognizing table.el table...done")))
  2424. (error "This should not happen..."))
  2425. t)
  2426. nil)
  2427. nil))
  2428. (defun org-at-table-hline-p ()
  2429. "Return t if the cursor is inside a hline in a table."
  2430. (if org-enable-table-editor
  2431. (save-excursion
  2432. (beginning-of-line 1)
  2433. (looking-at org-table-hline-regexp))
  2434. nil))
  2435. (defvar org-table-clean-did-remove-column nil)
  2436. (defun org-table-map-tables (function)
  2437. "Apply FUNCTION to the start of all tables in the buffer."
  2438. (save-excursion
  2439. (save-restriction
  2440. (widen)
  2441. (goto-char (point-min))
  2442. (while (re-search-forward org-table-any-line-regexp nil t)
  2443. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2444. (beginning-of-line 1)
  2445. (if (looking-at org-table-line-regexp)
  2446. (save-excursion (funcall function)))
  2447. (re-search-forward org-table-any-border-regexp nil 1))))
  2448. (message "Mapping tables: done"))
  2449. ;; Declare and autoload functions from org-exp.el
  2450. (declare-function org-default-export-plist "org-exp")
  2451. (declare-function org-infile-export-plist "org-exp")
  2452. (declare-function org-get-current-options "org-exp")
  2453. (eval-and-compile
  2454. (org-autoload "org-exp"
  2455. '(org-export org-export-as-ascii org-export-visible
  2456. org-insert-export-options-template org-export-as-html-and-open
  2457. org-export-as-html-batch org-export-as-html-to-buffer
  2458. org-replace-region-by-html org-export-region-as-html
  2459. org-export-as-html org-export-icalendar-this-file
  2460. org-export-icalendar-all-agenda-files
  2461. org-table-clean-before-export
  2462. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2463. ;; Declare and autoload functions from org-agenda.el
  2464. (eval-and-compile
  2465. (org-autoload "org-agenda"
  2466. '(org-agenda org-agenda-list org-search-view
  2467. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2468. org-diary org-agenda-to-appt
  2469. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2470. ;; Autoload org-remember
  2471. (eval-and-compile
  2472. (org-autoload "org-remember"
  2473. '(org-remember-insinuate org-remember-annotation
  2474. org-remember-apply-template org-remember org-remember-handler)))
  2475. ;; Autoload org-clock.el
  2476. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2477. (beg end))
  2478. (declare-function org-clock-update-mode-line "org-clock" ())
  2479. (defvar org-clock-start-time)
  2480. (defvar org-clock-marker (make-marker)
  2481. "Marker recording the last clock-in.")
  2482. (eval-and-compile
  2483. (org-autoload
  2484. "org-clock"
  2485. '(org-clock-in org-clock-out org-clock-cancel
  2486. org-clock-goto org-clock-sum org-clock-display
  2487. org-clock-remove-overlays org-clock-report
  2488. org-clocktable-shift org-dblock-write:clocktable
  2489. org-get-clocktable)))
  2490. (defun org-clock-update-time-maybe ()
  2491. "If this is a CLOCK line, update it and return t.
  2492. Otherwise, return nil."
  2493. (interactive)
  2494. (save-excursion
  2495. (beginning-of-line 1)
  2496. (skip-chars-forward " \t")
  2497. (when (looking-at org-clock-string)
  2498. (let ((re (concat "[ \t]*" org-clock-string
  2499. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2500. "\\([ \t]*=>.*\\)?\\)?"))
  2501. ts te h m s neg)
  2502. (cond
  2503. ((not (looking-at re))
  2504. nil)
  2505. ((not (match-end 2))
  2506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2507. (> org-clock-marker (point))
  2508. (<= org-clock-marker (point-at-eol)))
  2509. ;; The clock is running here
  2510. (setq org-clock-start-time
  2511. (apply 'encode-time
  2512. (org-parse-time-string (match-string 1))))
  2513. (org-clock-update-mode-line)))
  2514. (t
  2515. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2516. (end-of-line 1)
  2517. (setq ts (match-string 1)
  2518. te (match-string 3))
  2519. (setq s (- (time-to-seconds
  2520. (apply 'encode-time (org-parse-time-string te)))
  2521. (time-to-seconds
  2522. (apply 'encode-time (org-parse-time-string ts))))
  2523. neg (< s 0)
  2524. s (abs s)
  2525. h (floor (/ s 3600))
  2526. s (- s (* 3600 h))
  2527. m (floor (/ s 60))
  2528. s (- s (* 60 s)))
  2529. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2530. t))))))
  2531. (defun org-check-running-clock ()
  2532. "Check if the current buffer contains the running clock.
  2533. If yes, offer to stop it and to save the buffer with the changes."
  2534. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2535. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2536. (buffer-name))))
  2537. (org-clock-out)
  2538. (when (y-or-n-p "Save changed buffer?")
  2539. (save-buffer))))
  2540. (defun org-clocktable-try-shift (dir n)
  2541. "Check if this line starts a clock table, if yes, shift the time block."
  2542. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2543. (org-clocktable-shift dir n)))
  2544. ;; Autoload org-timer.el
  2545. ;(declare-function org-timer "org-timer")
  2546. (eval-and-compile
  2547. (org-autoload
  2548. "org-timer"
  2549. '(org-timer-start org-timer org-timer-item
  2550. org-timer-change-times-in-region)))
  2551. ;; Autoload archiving code
  2552. ;; The stuff that is needed for cycling and tags has to be defined here.
  2553. (defgroup org-archive nil
  2554. "Options concerning archiving in Org-mode."
  2555. :tag "Org Archive"
  2556. :group 'org-structure)
  2557. (defcustom org-archive-location "%s_archive::"
  2558. "The location where subtrees should be archived.
  2559. The value of this variable is a string, consisting of two parts,
  2560. separated by a double-colon. The first part is a filename and
  2561. the second part is a headline.
  2562. When the filename is omitted, archiving happens in the same file.
  2563. %s in the filename will be replaced by the current file
  2564. name (without the directory part). Archiving to a different file
  2565. is useful to keep archived entries from contributing to the
  2566. Org-mode Agenda.
  2567. The archived entries will be filed as subtrees of the specified
  2568. headline. When the headline is omitted, the subtrees are simply
  2569. filed away at the end of the file, as top-level entries. Also in
  2570. the heading you can use %s to represent the file name, this can be
  2571. useful when using the same archive for a number of different files.
  2572. Here are a few examples:
  2573. \"%s_archive::\"
  2574. If the current file is Projects.org, archive in file
  2575. Projects.org_archive, as top-level trees. This is the default.
  2576. \"::* Archived Tasks\"
  2577. Archive in the current file, under the top-level headline
  2578. \"* Archived Tasks\".
  2579. \"~/org/archive.org::\"
  2580. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2581. \"~/org/archive.org::From %s\"
  2582. Archive in file ~/org/archive.org (absolute path), und headlines
  2583. \"From FILENAME\" where file name is the current file name.
  2584. \"basement::** Finished Tasks\"
  2585. Archive in file ./basement (relative path), as level 3 trees
  2586. below the level 2 heading \"** Finished Tasks\".
  2587. You may set this option on a per-file basis by adding to the buffer a
  2588. line like
  2589. #+ARCHIVE: basement::** Finished Tasks
  2590. You may also define it locally for a subtree by setting an ARCHIVE property
  2591. in the entry. If such a property is found in an entry, or anywhere up
  2592. the hierarchy, it will be used."
  2593. :group 'org-archive
  2594. :type 'string)
  2595. (defcustom org-archive-tag "ARCHIVE"
  2596. "The tag that marks a subtree as archived.
  2597. An archived subtree does not open during visibility cycling, and does
  2598. not contribute to the agenda listings.
  2599. After changing this, font-lock must be restarted in the relevant buffers to
  2600. get the proper fontification."
  2601. :group 'org-archive
  2602. :group 'org-keywords
  2603. :type 'string)
  2604. (defcustom org-agenda-skip-archived-trees t
  2605. "Non-nil means, the agenda will skip any items located in archived trees.
  2606. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2607. variable is no longer recommended, you should leave it at the value t.
  2608. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2609. :group 'org-archive
  2610. :group 'org-agenda-skip
  2611. :type 'boolean)
  2612. (defcustom org-cycle-open-archived-trees nil
  2613. "Non-nil means, `org-cycle' will open archived trees.
  2614. An archived tree is a tree marked with the tag ARCHIVE.
  2615. When nil, archived trees will stay folded. You can still open them with
  2616. normal outline commands like `show-all', but not with the cycling commands."
  2617. :group 'org-archive
  2618. :group 'org-cycle
  2619. :type 'boolean)
  2620. (defcustom org-sparse-tree-open-archived-trees nil
  2621. "Non-nil means sparse tree construction shows matches in archived trees.
  2622. When nil, matches in these trees are highlighted, but the trees are kept in
  2623. collapsed state."
  2624. :group 'org-archive
  2625. :group 'org-sparse-trees
  2626. :type 'boolean)
  2627. (defun org-cycle-hide-archived-subtrees (state)
  2628. "Re-hide all archived subtrees after a visibility state change."
  2629. (when (and (not org-cycle-open-archived-trees)
  2630. (not (memq state '(overview folded))))
  2631. (save-excursion
  2632. (let* ((globalp (memq state '(contents all)))
  2633. (beg (if globalp (point-min) (point)))
  2634. (end (if globalp (point-max) (org-end-of-subtree t))))
  2635. (org-hide-archived-subtrees beg end)
  2636. (goto-char beg)
  2637. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2638. (message "%s" (substitute-command-keys
  2639. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2640. (defun org-force-cycle-archived ()
  2641. "Cycle subtree even if it is archived."
  2642. (interactive)
  2643. (setq this-command 'org-cycle)
  2644. (let ((org-cycle-open-archived-trees t))
  2645. (call-interactively 'org-cycle)))
  2646. (defun org-hide-archived-subtrees (beg end)
  2647. "Re-hide all archived subtrees after a visibility state change."
  2648. (save-excursion
  2649. (let* ((re (concat ":" org-archive-tag ":")))
  2650. (goto-char beg)
  2651. (while (re-search-forward re end t)
  2652. (and (org-on-heading-p) (hide-subtree))
  2653. (org-end-of-subtree t)))))
  2654. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2655. (eval-and-compile
  2656. (org-autoload "org-archive"
  2657. '(org-add-archive-files org-archive-subtree
  2658. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2659. ;; Autoload Column View Code
  2660. (declare-function org-columns-number-to-string "org-colview")
  2661. (declare-function org-columns-get-format-and-top-level "org-colview")
  2662. (declare-function org-columns-compute "org-colview")
  2663. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2664. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2665. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2666. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2667. ;; Autoload ID code
  2668. (declare-function org-id-store-link "org-id")
  2669. (org-autoload "org-id"
  2670. '(org-id-get-create org-id-new org-id-copy org-id-get
  2671. org-id-get-with-outline-path-completion
  2672. org-id-get-with-outline-drilling
  2673. org-id-goto org-id-find org-id-store-link))
  2674. ;;; Variables for pre-computed regular expressions, all buffer local
  2675. (defvar org-drawer-regexp nil
  2676. "Matches first line of a hidden block.")
  2677. (make-variable-buffer-local 'org-drawer-regexp)
  2678. (defvar org-todo-regexp nil
  2679. "Matches any of the TODO state keywords.")
  2680. (make-variable-buffer-local 'org-todo-regexp)
  2681. (defvar org-not-done-regexp nil
  2682. "Matches any of the TODO state keywords except the last one.")
  2683. (make-variable-buffer-local 'org-not-done-regexp)
  2684. (defvar org-todo-line-regexp nil
  2685. "Matches a headline and puts TODO state into group 2 if present.")
  2686. (make-variable-buffer-local 'org-todo-line-regexp)
  2687. (defvar org-complex-heading-regexp nil
  2688. "Matches a headline and puts everything into groups:
  2689. group 1: the stars
  2690. group 2: The todo keyword, maybe
  2691. group 3: Priority cookie
  2692. group 4: True headline
  2693. group 5: Tags")
  2694. (make-variable-buffer-local 'org-complex-heading-regexp)
  2695. (defvar org-todo-line-tags-regexp nil
  2696. "Matches a headline and puts TODO state into group 2 if present.
  2697. Also put tags into group 4 if tags are present.")
  2698. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2699. (defvar org-nl-done-regexp nil
  2700. "Matches newline followed by a headline with the DONE keyword.")
  2701. (make-variable-buffer-local 'org-nl-done-regexp)
  2702. (defvar org-looking-at-done-regexp nil
  2703. "Matches the DONE keyword a point.")
  2704. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2705. (defvar org-ds-keyword-length 12
  2706. "Maximum length of the Deadline and SCHEDULED keywords.")
  2707. (make-variable-buffer-local 'org-ds-keyword-length)
  2708. (defvar org-deadline-regexp nil
  2709. "Matches the DEADLINE keyword.")
  2710. (make-variable-buffer-local 'org-deadline-regexp)
  2711. (defvar org-deadline-time-regexp nil
  2712. "Matches the DEADLINE keyword together with a time stamp.")
  2713. (make-variable-buffer-local 'org-deadline-time-regexp)
  2714. (defvar org-deadline-line-regexp nil
  2715. "Matches the DEADLINE keyword and the rest of the line.")
  2716. (make-variable-buffer-local 'org-deadline-line-regexp)
  2717. (defvar org-scheduled-regexp nil
  2718. "Matches the SCHEDULED keyword.")
  2719. (make-variable-buffer-local 'org-scheduled-regexp)
  2720. (defvar org-scheduled-time-regexp nil
  2721. "Matches the SCHEDULED keyword together with a time stamp.")
  2722. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2723. (defvar org-closed-time-regexp nil
  2724. "Matches the CLOSED keyword together with a time stamp.")
  2725. (make-variable-buffer-local 'org-closed-time-regexp)
  2726. (defvar org-keyword-time-regexp nil
  2727. "Matches any of the 4 keywords, together with the time stamp.")
  2728. (make-variable-buffer-local 'org-keyword-time-regexp)
  2729. (defvar org-keyword-time-not-clock-regexp nil
  2730. "Matches any of the 3 keywords, together with the time stamp.")
  2731. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2732. (defvar org-maybe-keyword-time-regexp nil
  2733. "Matches a timestamp, possibly preceeded by a keyword.")
  2734. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2735. (defvar org-planning-or-clock-line-re nil
  2736. "Matches a line with planning or clock info.")
  2737. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2738. (defconst org-plain-time-of-day-regexp
  2739. (concat
  2740. "\\(\\<[012]?[0-9]"
  2741. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2742. "\\(--?"
  2743. "\\(\\<[012]?[0-9]"
  2744. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2745. "\\)?")
  2746. "Regular expression to match a plain time or time range.
  2747. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2748. groups carry important information:
  2749. 0 the full match
  2750. 1 the first time, range or not
  2751. 8 the second time, if it is a range.")
  2752. (defconst org-plain-time-extension-regexp
  2753. (concat
  2754. "\\(\\<[012]?[0-9]"
  2755. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2756. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2757. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2758. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2759. groups carry important information:
  2760. 0 the full match
  2761. 7 hours of duration
  2762. 9 minutes of duration")
  2763. (defconst org-stamp-time-of-day-regexp
  2764. (concat
  2765. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2766. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2767. "\\(--?"
  2768. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2769. "Regular expression to match a timestamp time or time range.
  2770. After a match, the following groups carry important information:
  2771. 0 the full match
  2772. 1 date plus weekday, for backreferencing to make sure both times on same day
  2773. 2 the first time, range or not
  2774. 4 the second time, if it is a range.")
  2775. (defconst org-startup-options
  2776. '(("fold" org-startup-folded t)
  2777. ("overview" org-startup-folded t)
  2778. ("nofold" org-startup-folded nil)
  2779. ("showall" org-startup-folded nil)
  2780. ("content" org-startup-folded content)
  2781. ("hidestars" org-hide-leading-stars t)
  2782. ("showstars" org-hide-leading-stars nil)
  2783. ("odd" org-odd-levels-only t)
  2784. ("oddeven" org-odd-levels-only nil)
  2785. ("align" org-startup-align-all-tables t)
  2786. ("noalign" org-startup-align-all-tables nil)
  2787. ("customtime" org-display-custom-times t)
  2788. ("logdone" org-log-done time)
  2789. ("lognotedone" org-log-done note)
  2790. ("nologdone" org-log-done nil)
  2791. ("lognoteclock-out" org-log-note-clock-out t)
  2792. ("nolognoteclock-out" org-log-note-clock-out nil)
  2793. ("logrepeat" org-log-repeat state)
  2794. ("lognoterepeat" org-log-repeat note)
  2795. ("nologrepeat" org-log-repeat nil)
  2796. ("fninline" org-footnote-define-inline t)
  2797. ("nofninline" org-footnote-define-inline nil)
  2798. ("fnlocal" org-footnote-section nil)
  2799. ("fnauto" org-footnote-auto-label t)
  2800. ("fnprompt" org-footnote-auto-label nil)
  2801. ("fnconfirm" org-footnote-auto-label confirm)
  2802. ("fnplain" org-footnote-auto-label plain)
  2803. ("constcgs" constants-unit-system cgs)
  2804. ("constSI" constants-unit-system SI))
  2805. "Variable associated with STARTUP options for org-mode.
  2806. Each element is a list of three items: The startup options as written
  2807. in the #+STARTUP line, the corresponding variable, and the value to
  2808. set this variable to if the option is found. An optional forth element PUSH
  2809. means to push this value onto the list in the variable.")
  2810. (defun org-set-regexps-and-options ()
  2811. "Precompute regular expressions for current buffer."
  2812. (when (org-mode-p)
  2813. (org-set-local 'org-todo-kwd-alist nil)
  2814. (org-set-local 'org-todo-key-alist nil)
  2815. (org-set-local 'org-todo-key-trigger nil)
  2816. (org-set-local 'org-todo-keywords-1 nil)
  2817. (org-set-local 'org-done-keywords nil)
  2818. (org-set-local 'org-todo-heads nil)
  2819. (org-set-local 'org-todo-sets nil)
  2820. (org-set-local 'org-todo-log-states nil)
  2821. (org-set-local 'org-file-properties nil)
  2822. (org-set-local 'org-file-tags nil)
  2823. (let ((re (org-make-options-regexp
  2824. '("CATEGORY" "TODO" "COLUMNS"
  2825. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2826. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2827. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2828. (splitre "[ \t]+")
  2829. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2830. tail sep kws1 prio props ftags drawers
  2831. ext-setup-or-nil setup-contents (start 0))
  2832. (save-excursion
  2833. (save-restriction
  2834. (widen)
  2835. (goto-char (point-min))
  2836. (while (or (and ext-setup-or-nil
  2837. (string-match re ext-setup-or-nil start)
  2838. (setq start (match-end 0)))
  2839. (and (setq ext-setup-or-nil nil start 0)
  2840. (re-search-forward re nil t)))
  2841. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2842. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2843. (cond
  2844. ((equal key "CATEGORY")
  2845. (if (string-match "[ \t]+$" value)
  2846. (setq value (replace-match "" t t value)))
  2847. (setq cat value))
  2848. ((member key '("SEQ_TODO" "TODO"))
  2849. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2850. ((equal key "TYP_TODO")
  2851. (push (cons 'type (org-split-string value splitre)) kwds))
  2852. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2853. ;; general TODO-like setup
  2854. (push (cons (intern (downcase (match-string 1 key)))
  2855. (org-split-string value splitre)) kwds))
  2856. ((equal key "TAGS")
  2857. (setq tags (append tags (org-split-string value splitre))))
  2858. ((equal key "COLUMNS")
  2859. (org-set-local 'org-columns-default-format value))
  2860. ((equal key "LINK")
  2861. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2862. (push (cons (match-string 1 value)
  2863. (org-trim (match-string 2 value)))
  2864. links)))
  2865. ((equal key "PRIORITIES")
  2866. (setq prio (org-split-string value " +")))
  2867. ((equal key "PROPERTY")
  2868. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2869. (push (cons (match-string 1 value) (match-string 2 value))
  2870. props)))
  2871. ((equal key "FILETAGS")
  2872. (when (string-match "\\S-" value)
  2873. (setq ftags
  2874. (append
  2875. ftags
  2876. (apply 'append
  2877. (mapcar (lambda (x) (org-split-string x ":"))
  2878. (org-split-string value)))))))
  2879. ((equal key "DRAWERS")
  2880. (setq drawers (org-split-string value splitre)))
  2881. ((equal key "CONSTANTS")
  2882. (setq const (append const (org-split-string value splitre))))
  2883. ((equal key "STARTUP")
  2884. (let ((opts (org-split-string value splitre))
  2885. l var val)
  2886. (while (setq l (pop opts))
  2887. (when (setq l (assoc l org-startup-options))
  2888. (setq var (nth 1 l) val (nth 2 l))
  2889. (if (not (nth 3 l))
  2890. (set (make-local-variable var) val)
  2891. (if (not (listp (symbol-value var)))
  2892. (set (make-local-variable var) nil))
  2893. (set (make-local-variable var) (symbol-value var))
  2894. (add-to-list var val))))))
  2895. ((equal key "ARCHIVE")
  2896. (string-match " *$" value)
  2897. (setq arch (replace-match "" t t value))
  2898. (remove-text-properties 0 (length arch)
  2899. '(face t fontified t) arch))
  2900. ((equal key "SETUPFILE")
  2901. (setq setup-contents (org-file-contents
  2902. (expand-file-name
  2903. (org-remove-double-quotes value))
  2904. 'noerror))
  2905. (if (not ext-setup-or-nil)
  2906. (setq ext-setup-or-nil setup-contents start 0)
  2907. (setq ext-setup-or-nil
  2908. (concat (substring ext-setup-or-nil 0 start)
  2909. "\n" setup-contents "\n"
  2910. (substring ext-setup-or-nil start)))))
  2911. ))))
  2912. (when cat
  2913. (org-set-local 'org-category (intern cat))
  2914. (push (cons "CATEGORY" cat) props))
  2915. (when prio
  2916. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2917. (setq prio (mapcar 'string-to-char prio))
  2918. (org-set-local 'org-highest-priority (nth 0 prio))
  2919. (org-set-local 'org-lowest-priority (nth 1 prio))
  2920. (org-set-local 'org-default-priority (nth 2 prio)))
  2921. (and props (org-set-local 'org-file-properties (nreverse props)))
  2922. (and ftags (org-set-local 'org-file-tags ftags))
  2923. (and drawers (org-set-local 'org-drawers drawers))
  2924. (and arch (org-set-local 'org-archive-location arch))
  2925. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2926. ;; Process the TODO keywords
  2927. (unless kwds
  2928. ;; Use the global values as if they had been given locally.
  2929. (setq kwds (default-value 'org-todo-keywords))
  2930. (if (stringp (car kwds))
  2931. (setq kwds (list (cons org-todo-interpretation
  2932. (default-value 'org-todo-keywords)))))
  2933. (setq kwds (reverse kwds)))
  2934. (setq kwds (nreverse kwds))
  2935. (let (inter kws kw)
  2936. (while (setq kws (pop kwds))
  2937. (let ((kws (or
  2938. (run-hook-with-args-until-success
  2939. 'org-todo-setup-filter-hook kws)
  2940. kws)))
  2941. (setq inter (pop kws) sep (member "|" kws)
  2942. kws0 (delete "|" (copy-sequence kws))
  2943. kwsa nil
  2944. kws1 (mapcar
  2945. (lambda (x)
  2946. ;; 1 2
  2947. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2948. (progn
  2949. (setq kw (match-string 1 x)
  2950. key (and (match-end 2) (match-string 2 x))
  2951. log (org-extract-log-state-settings x))
  2952. (push (cons kw (and key (string-to-char key))) kwsa)
  2953. (and log (push log org-todo-log-states))
  2954. kw)
  2955. (error "Invalid TODO keyword %s" x)))
  2956. kws0)
  2957. kwsa (if kwsa (append '((:startgroup))
  2958. (nreverse kwsa)
  2959. '((:endgroup))))
  2960. hw (car kws1)
  2961. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2962. tail (list inter hw (car dws) (org-last dws))))
  2963. (add-to-list 'org-todo-heads hw 'append)
  2964. (push kws1 org-todo-sets)
  2965. (setq org-done-keywords (append org-done-keywords dws nil))
  2966. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2967. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2968. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2969. (setq org-todo-sets (nreverse org-todo-sets)
  2970. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2971. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2972. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2973. ;; Process the constants
  2974. (when const
  2975. (let (e cst)
  2976. (while (setq e (pop const))
  2977. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2978. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2979. (setq org-table-formula-constants-local cst)))
  2980. ;; Process the tags.
  2981. (when tags
  2982. (let (e tgs)
  2983. (while (setq e (pop tags))
  2984. (cond
  2985. ((equal e "{") (push '(:startgroup) tgs))
  2986. ((equal e "}") (push '(:endgroup) tgs))
  2987. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2988. (push (cons (match-string 1 e)
  2989. (string-to-char (match-string 2 e)))
  2990. tgs))
  2991. (t (push (list e) tgs))))
  2992. (org-set-local 'org-tag-alist nil)
  2993. (while (setq e (pop tgs))
  2994. (or (and (stringp (car e))
  2995. (assoc (car e) org-tag-alist))
  2996. (push e org-tag-alist)))))
  2997. ;; Compute the regular expressions and other local variables
  2998. (if (not org-done-keywords)
  2999. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3000. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3001. (length org-scheduled-string)
  3002. (length org-clock-string)
  3003. (length org-closed-string)))
  3004. org-drawer-regexp
  3005. (concat "^[ \t]*:\\("
  3006. (mapconcat 'regexp-quote org-drawers "\\|")
  3007. "\\):[ \t]*$")
  3008. org-not-done-keywords
  3009. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3010. org-todo-regexp
  3011. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3012. "\\|") "\\)\\>")
  3013. org-not-done-regexp
  3014. (concat "\\<\\("
  3015. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3016. "\\)\\>")
  3017. org-todo-line-regexp
  3018. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3019. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3020. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3021. org-complex-heading-regexp
  3022. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3023. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3024. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3025. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3026. org-nl-done-regexp
  3027. (concat "\n\\*+[ \t]+"
  3028. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3029. "\\)" "\\>")
  3030. org-todo-line-tags-regexp
  3031. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3032. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3033. (org-re
  3034. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3035. org-looking-at-done-regexp
  3036. (concat "^" "\\(?:"
  3037. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3038. "\\>")
  3039. org-deadline-regexp (concat "\\<" org-deadline-string)
  3040. org-deadline-time-regexp
  3041. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3042. org-deadline-line-regexp
  3043. (concat "\\<\\(" org-deadline-string "\\).*")
  3044. org-scheduled-regexp
  3045. (concat "\\<" org-scheduled-string)
  3046. org-scheduled-time-regexp
  3047. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3048. org-closed-time-regexp
  3049. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3050. org-keyword-time-regexp
  3051. (concat "\\<\\(" org-scheduled-string
  3052. "\\|" org-deadline-string
  3053. "\\|" org-closed-string
  3054. "\\|" org-clock-string "\\)"
  3055. " *[[<]\\([^]>]+\\)[]>]")
  3056. org-keyword-time-not-clock-regexp
  3057. (concat "\\<\\(" org-scheduled-string
  3058. "\\|" org-deadline-string
  3059. "\\|" org-closed-string
  3060. "\\)"
  3061. " *[[<]\\([^]>]+\\)[]>]")
  3062. org-maybe-keyword-time-regexp
  3063. (concat "\\(\\<\\(" org-scheduled-string
  3064. "\\|" org-deadline-string
  3065. "\\|" org-closed-string
  3066. "\\|" org-clock-string "\\)\\)?"
  3067. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3068. org-planning-or-clock-line-re
  3069. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3070. "\\|" org-deadline-string
  3071. "\\|" org-closed-string "\\|" org-clock-string
  3072. "\\)\\>\\)")
  3073. )
  3074. (org-compute-latex-and-specials-regexp)
  3075. (org-set-font-lock-defaults))))
  3076. (defun org-file-contents (file &optional noerror)
  3077. "Return the contents of FILE, as a string."
  3078. (if (or (not file)
  3079. (not (file-readable-p file)))
  3080. (if noerror
  3081. (progn
  3082. (message "Cannot read file %s" file)
  3083. (ding) (sit-for 2)
  3084. "")
  3085. (error "Cannot read file %s" file))
  3086. (with-temp-buffer
  3087. (insert-file-contents file)
  3088. (buffer-string))))
  3089. (defun org-extract-log-state-settings (x)
  3090. "Extract the log state setting from a TODO keyword string.
  3091. This will extract info from a string like \"WAIT(w@/!)\"."
  3092. (let (kw key log1 log2)
  3093. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3094. (setq kw (match-string 1 x)
  3095. key (and (match-end 2) (match-string 2 x))
  3096. log1 (and (match-end 3) (match-string 3 x))
  3097. log2 (and (match-end 4) (match-string 4 x)))
  3098. (and (or log1 log2)
  3099. (list kw
  3100. (and log1 (if (equal log1 "!") 'time 'note))
  3101. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3102. (defun org-remove-keyword-keys (list)
  3103. "Remove a pair of parenthesis at the end of each string in LIST."
  3104. (mapcar (lambda (x)
  3105. (if (string-match "(.*)$" x)
  3106. (substring x 0 (match-beginning 0))
  3107. x))
  3108. list))
  3109. ;; FIXME: this could be done much better, using second characters etc.
  3110. (defun org-assign-fast-keys (alist)
  3111. "Assign fast keys to a keyword-key alist.
  3112. Respect keys that are already there."
  3113. (let (new e k c c1 c2 (char ?a))
  3114. (while (setq e (pop alist))
  3115. (cond
  3116. ((equal e '(:startgroup)) (push e new))
  3117. ((equal e '(:endgroup)) (push e new))
  3118. (t
  3119. (setq k (car e) c2 nil)
  3120. (if (cdr e)
  3121. (setq c (cdr e))
  3122. ;; automatically assign a character.
  3123. (setq c1 (string-to-char
  3124. (downcase (substring
  3125. k (if (= (string-to-char k) ?@) 1 0)))))
  3126. (if (or (rassoc c1 new) (rassoc c1 alist))
  3127. (while (or (rassoc char new) (rassoc char alist))
  3128. (setq char (1+ char)))
  3129. (setq c2 c1))
  3130. (setq c (or c2 char)))
  3131. (push (cons k c) new))))
  3132. (nreverse new)))
  3133. ;;; Some variables used in various places
  3134. (defvar org-window-configuration nil
  3135. "Used in various places to store a window configuration.")
  3136. (defvar org-finish-function nil
  3137. "Function to be called when `C-c C-c' is used.
  3138. This is for getting out of special buffers like remember.")
  3139. ;; FIXME: Occasionally check by commenting these, to make sure
  3140. ;; no other functions uses these, forgetting to let-bind them.
  3141. (defvar entry)
  3142. (defvar state)
  3143. (defvar last-state)
  3144. (defvar date)
  3145. (defvar description)
  3146. ;; Defined somewhere in this file, but used before definition.
  3147. (defvar org-html-entities)
  3148. (defvar org-struct-menu)
  3149. (defvar org-org-menu)
  3150. (defvar org-tbl-menu)
  3151. (defvar org-agenda-keymap)
  3152. ;;;; Define the Org-mode
  3153. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3154. (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."))
  3155. ;; We use a before-change function to check if a table might need
  3156. ;; an update.
  3157. (defvar org-table-may-need-update t
  3158. "Indicates that a table might need an update.
  3159. This variable is set by `org-before-change-function'.
  3160. `org-table-align' sets it back to nil.")
  3161. (defun org-before-change-function (beg end)
  3162. "Every change indicates that a table might need an update."
  3163. (setq org-table-may-need-update t))
  3164. (defvar org-mode-map)
  3165. (defvar org-mode-hook nil
  3166. "Mode hook for Org-mode, run after the mode was turned on.")
  3167. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3168. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3169. (defvar org-table-buffer-is-an nil)
  3170. (defconst org-outline-regexp "\\*+ ")
  3171. ;;;###autoload
  3172. (define-derived-mode org-mode outline-mode "Org"
  3173. "Outline-based notes management and organizer, alias
  3174. \"Carsten's outline-mode for keeping track of everything.\"
  3175. Org-mode develops organizational tasks around a NOTES file which
  3176. contains information about projects as plain text. Org-mode is
  3177. implemented on top of outline-mode, which is ideal to keep the content
  3178. of large files well structured. It supports ToDo items, deadlines and
  3179. time stamps, which magically appear in the diary listing of the Emacs
  3180. calendar. Tables are easily created with a built-in table editor.
  3181. Plain text URL-like links connect to websites, emails (VM), Usenet
  3182. messages (Gnus), BBDB entries, and any files related to the project.
  3183. For printing and sharing of notes, an Org-mode file (or a part of it)
  3184. can be exported as a structured ASCII or HTML file.
  3185. The following commands are available:
  3186. \\{org-mode-map}"
  3187. ;; Get rid of Outline menus, they are not needed
  3188. ;; Need to do this here because define-derived-mode sets up
  3189. ;; the keymap so late. Still, it is a waste to call this each time
  3190. ;; we switch another buffer into org-mode.
  3191. (if (featurep 'xemacs)
  3192. (when (boundp 'outline-mode-menu-heading)
  3193. ;; Assume this is Greg's port, it used easymenu
  3194. (easy-menu-remove outline-mode-menu-heading)
  3195. (easy-menu-remove outline-mode-menu-show)
  3196. (easy-menu-remove outline-mode-menu-hide))
  3197. (define-key org-mode-map [menu-bar headings] 'undefined)
  3198. (define-key org-mode-map [menu-bar hide] 'undefined)
  3199. (define-key org-mode-map [menu-bar show] 'undefined))
  3200. (org-load-modules-maybe)
  3201. (easy-menu-add org-org-menu)
  3202. (easy-menu-add org-tbl-menu)
  3203. (org-install-agenda-files-menu)
  3204. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3205. (org-add-to-invisibility-spec '(org-cwidth))
  3206. (when (featurep 'xemacs)
  3207. (org-set-local 'line-move-ignore-invisible t))
  3208. (org-set-local 'outline-regexp org-outline-regexp)
  3209. (org-set-local 'outline-level 'org-outline-level)
  3210. (when (and org-ellipsis
  3211. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3212. (fboundp 'make-glyph-code))
  3213. (unless org-display-table
  3214. (setq org-display-table (make-display-table)))
  3215. (set-display-table-slot
  3216. org-display-table 4
  3217. (vconcat (mapcar
  3218. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3219. org-ellipsis)))
  3220. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3221. (setq buffer-display-table org-display-table))
  3222. (org-set-regexps-and-options)
  3223. (when (and org-tag-faces (not org-tags-special-faces-re))
  3224. ;; tag faces set outside customize.... force initialization.
  3225. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3226. ;; Calc embedded
  3227. (org-set-local 'calc-embedded-open-mode "# ")
  3228. (modify-syntax-entry ?# "<")
  3229. (modify-syntax-entry ?@ "w")
  3230. (if org-startup-truncated (setq truncate-lines t))
  3231. (org-set-local 'font-lock-unfontify-region-function
  3232. 'org-unfontify-region)
  3233. ;; Activate before-change-function
  3234. (org-set-local 'org-table-may-need-update t)
  3235. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3236. 'local)
  3237. ;; Check for running clock before killing a buffer
  3238. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3239. ;; Paragraphs and auto-filling
  3240. (org-set-autofill-regexps)
  3241. (setq indent-line-function 'org-indent-line-function)
  3242. (org-update-radio-target-regexp)
  3243. ;; Make sure dependence stuff works reliably, even for users who set it
  3244. ;; too late :-(
  3245. (if org-enforce-todo-dependencies
  3246. (add-hook 'org-blocker-hook
  3247. 'org-block-todo-from-children-or-siblings)
  3248. (remove-hook 'org-blocker-hook
  3249. 'org-block-todo-from-children-or-siblings))
  3250. (if org-enforce-todo-checkbox-dependencies
  3251. (add-hook 'org-blocker-hook
  3252. 'org-block-todo-from-checkboxes)
  3253. (remove-hook 'org-blocker-hook
  3254. 'org-block-todo-from-checkboxes))
  3255. ;; Comment characters
  3256. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3257. (org-set-local 'comment-padding " ")
  3258. ;; Align options lines
  3259. (org-set-local
  3260. 'align-mode-rules-list
  3261. '((org-in-buffer-settings
  3262. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3263. (modes . '(org-mode)))))
  3264. ;; Imenu
  3265. (org-set-local 'imenu-create-index-function
  3266. 'org-imenu-get-tree)
  3267. ;; Make isearch reveal context
  3268. (if (or (featurep 'xemacs)
  3269. (not (boundp 'outline-isearch-open-invisible-function)))
  3270. ;; Emacs 21 and XEmacs make use of the hook
  3271. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3272. ;; Emacs 22 deals with this through a special variable
  3273. (org-set-local 'outline-isearch-open-invisible-function
  3274. (lambda (&rest ignore) (org-show-context 'isearch))))
  3275. ;; If empty file that did not turn on org-mode automatically, make it to.
  3276. (if (and org-insert-mode-line-in-empty-file
  3277. (interactive-p)
  3278. (= (point-min) (point-max)))
  3279. (insert "# -*- mode: org -*-\n\n"))
  3280. (unless org-inhibit-startup
  3281. (when org-startup-align-all-tables
  3282. (let ((bmp (buffer-modified-p)))
  3283. (org-table-map-tables 'org-table-align)
  3284. (set-buffer-modified-p bmp)))
  3285. (org-set-startup-visibility)))
  3286. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3287. (defun org-current-time ()
  3288. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3289. (if (> (car org-time-stamp-rounding-minutes) 1)
  3290. (let ((r (car org-time-stamp-rounding-minutes))
  3291. (time (decode-time)))
  3292. (apply 'encode-time
  3293. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3294. (nthcdr 2 time))))
  3295. (current-time)))
  3296. ;;;; Font-Lock stuff, including the activators
  3297. (defvar org-mouse-map (make-sparse-keymap))
  3298. (org-defkey org-mouse-map
  3299. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3300. (org-defkey org-mouse-map
  3301. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3302. (when org-mouse-1-follows-link
  3303. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3304. (when org-tab-follows-link
  3305. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3306. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3307. (require 'font-lock)
  3308. (defconst org-non-link-chars "]\t\n\r<>")
  3309. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3310. "shell" "elisp"))
  3311. (defvar org-link-types-re nil
  3312. "Matches a link that has a url-like prefix like \"http:\"")
  3313. (defvar org-link-re-with-space nil
  3314. "Matches a link with spaces, optional angular brackets around it.")
  3315. (defvar org-link-re-with-space2 nil
  3316. "Matches a link with spaces, optional angular brackets around it.")
  3317. (defvar org-link-re-with-space3 nil
  3318. "Matches a link with spaces, only for internal part in bracket links.")
  3319. (defvar org-angle-link-re nil
  3320. "Matches link with angular brackets, spaces are allowed.")
  3321. (defvar org-plain-link-re nil
  3322. "Matches plain link, without spaces.")
  3323. (defvar org-bracket-link-regexp nil
  3324. "Matches a link in double brackets.")
  3325. (defvar org-bracket-link-analytic-regexp nil
  3326. "Regular expression used to analyze links.
  3327. Here is what the match groups contain after a match:
  3328. 1: http:
  3329. 2: http
  3330. 3: path
  3331. 4: [desc]
  3332. 5: desc")
  3333. (defvar org-bracket-link-analytic-regexp++ nil
  3334. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3335. (defvar org-any-link-re nil
  3336. "Regular expression matching any link.")
  3337. (defun org-make-link-regexps ()
  3338. "Update the link regular expressions.
  3339. This should be called after the variable `org-link-types' has changed."
  3340. (setq org-link-types-re
  3341. (concat
  3342. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3343. org-link-re-with-space
  3344. (concat
  3345. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3346. "\\([^" org-non-link-chars " ]"
  3347. "[^" org-non-link-chars "]*"
  3348. "[^" org-non-link-chars " ]\\)>?")
  3349. org-link-re-with-space2
  3350. (concat
  3351. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3352. "\\([^" org-non-link-chars " ]"
  3353. "[^\t\n\r]*"
  3354. "[^" org-non-link-chars " ]\\)>?")
  3355. org-link-re-with-space3
  3356. (concat
  3357. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3358. "\\([^" org-non-link-chars " ]"
  3359. "[^\t\n\r]*\\)")
  3360. org-angle-link-re
  3361. (concat
  3362. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3363. "\\([^" org-non-link-chars " ]"
  3364. "[^" org-non-link-chars "]*"
  3365. "\\)>")
  3366. org-plain-link-re
  3367. (concat
  3368. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3369. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3370. org-bracket-link-regexp
  3371. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3372. org-bracket-link-analytic-regexp
  3373. (concat
  3374. "\\[\\["
  3375. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3376. "\\([^]]+\\)"
  3377. "\\]"
  3378. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3379. "\\]")
  3380. org-bracket-link-analytic-regexp++
  3381. (concat
  3382. "\\[\\["
  3383. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3384. "\\([^]]+\\)"
  3385. "\\]"
  3386. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3387. "\\]")
  3388. org-any-link-re
  3389. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3390. org-angle-link-re "\\)\\|\\("
  3391. org-plain-link-re "\\)")))
  3392. (org-make-link-regexps)
  3393. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3394. "Regular expression for fast time stamp matching.")
  3395. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3396. "Regular expression for fast time stamp matching.")
  3397. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3398. "Regular expression matching time strings for analysis.
  3399. This one does not require the space after the date, so it can be used
  3400. on a string that terminates immediately after the date.")
  3401. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3402. "Regular expression matching time strings for analysis.")
  3403. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3404. "Regular expression matching time stamps, with groups.")
  3405. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3406. "Regular expression matching time stamps (also [..]), with groups.")
  3407. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3408. "Regular expression matching a time stamp range.")
  3409. (defconst org-tr-regexp-both
  3410. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3411. "Regular expression matching a time stamp range.")
  3412. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3413. org-ts-regexp "\\)?")
  3414. "Regular expression matching a time stamp or time stamp range.")
  3415. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3416. org-ts-regexp-both "\\)?")
  3417. "Regular expression matching a time stamp or time stamp range.
  3418. The time stamps may be either active or inactive.")
  3419. (defvar org-emph-face nil)
  3420. (defun org-do-emphasis-faces (limit)
  3421. "Run through the buffer and add overlays to links."
  3422. (let (rtn)
  3423. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3424. (if (not (= (char-after (match-beginning 3))
  3425. (char-after (match-beginning 4))))
  3426. (progn
  3427. (setq rtn t)
  3428. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3429. 'face
  3430. (nth 1 (assoc (match-string 3)
  3431. org-emphasis-alist)))
  3432. (add-text-properties (match-beginning 2) (match-end 2)
  3433. '(font-lock-multiline t))
  3434. (when org-hide-emphasis-markers
  3435. (add-text-properties (match-end 4) (match-beginning 5)
  3436. '(invisible org-link))
  3437. (add-text-properties (match-beginning 3) (match-end 3)
  3438. '(invisible org-link)))))
  3439. (backward-char 1))
  3440. rtn))
  3441. (defun org-emphasize (&optional char)
  3442. "Insert or change an emphasis, i.e. a font like bold or italic.
  3443. If there is an active region, change that region to a new emphasis.
  3444. If there is no region, just insert the marker characters and position
  3445. the cursor between them.
  3446. CHAR should be either the marker character, or the first character of the
  3447. HTML tag associated with that emphasis. If CHAR is a space, the means
  3448. to remove the emphasis of the selected region.
  3449. If char is not given (for example in an interactive call) it
  3450. will be prompted for."
  3451. (interactive)
  3452. (let ((eal org-emphasis-alist) e det
  3453. (erc org-emphasis-regexp-components)
  3454. (prompt "")
  3455. (string "") beg end move tag c s)
  3456. (if (org-region-active-p)
  3457. (setq beg (region-beginning) end (region-end)
  3458. string (buffer-substring beg end))
  3459. (setq move t))
  3460. (while (setq e (pop eal))
  3461. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3462. c (aref tag 0))
  3463. (push (cons c (string-to-char (car e))) det)
  3464. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3465. (substring tag 1)))))
  3466. (setq det (nreverse det))
  3467. (unless char
  3468. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3469. (setq char (read-char-exclusive)))
  3470. (setq char (or (cdr (assoc char det)) char))
  3471. (if (equal char ?\ )
  3472. (setq s "" move nil)
  3473. (unless (assoc (char-to-string char) org-emphasis-alist)
  3474. (error "No such emphasis marker: \"%c\"" char))
  3475. (setq s (char-to-string char)))
  3476. (while (and (> (length string) 1)
  3477. (equal (substring string 0 1) (substring string -1))
  3478. (assoc (substring string 0 1) org-emphasis-alist))
  3479. (setq string (substring string 1 -1)))
  3480. (setq string (concat s string s))
  3481. (if beg (delete-region beg end))
  3482. (unless (or (bolp)
  3483. (string-match (concat "[" (nth 0 erc) "\n]")
  3484. (char-to-string (char-before (point)))))
  3485. (insert " "))
  3486. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3487. (char-to-string (char-after (point))))
  3488. (insert " ") (backward-char 1))
  3489. (insert string)
  3490. (and move (backward-char 1))))
  3491. (defconst org-nonsticky-props
  3492. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3493. (defun org-activate-plain-links (limit)
  3494. "Run through the buffer and add overlays to links."
  3495. (catch 'exit
  3496. (let (f)
  3497. (while (re-search-forward org-plain-link-re limit t)
  3498. (setq f (get-text-property (match-beginning 0) 'face))
  3499. (if (or (eq f 'org-tag)
  3500. (and (listp f) (memq 'org-tag f)))
  3501. nil
  3502. (add-text-properties (match-beginning 0) (match-end 0)
  3503. (list 'mouse-face 'highlight
  3504. 'rear-nonsticky org-nonsticky-props
  3505. 'keymap org-mouse-map
  3506. ))
  3507. (throw 'exit t))))))
  3508. (defun org-activate-code (limit)
  3509. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3510. (progn
  3511. (remove-text-properties (match-beginning 0) (match-end 0)
  3512. '(display t invisible t intangible t))
  3513. t)))
  3514. (defun org-activate-angle-links (limit)
  3515. "Run through the buffer and add overlays to links."
  3516. (if (re-search-forward org-angle-link-re limit t)
  3517. (progn
  3518. (add-text-properties (match-beginning 0) (match-end 0)
  3519. (list 'mouse-face 'highlight
  3520. 'rear-nonsticky org-nonsticky-props
  3521. 'keymap org-mouse-map
  3522. ))
  3523. t)))
  3524. (defun org-activate-footnote-links (limit)
  3525. "Run through the buffer and add overlays to links."
  3526. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3527. limit t)
  3528. (progn
  3529. (add-text-properties (match-beginning 2) (match-end 2)
  3530. (list 'mouse-face 'highlight
  3531. 'rear-nonsticky org-nonsticky-props
  3532. 'keymap org-mouse-map
  3533. 'help-echo
  3534. (if (= (point-at-bol) (match-beginning 2))
  3535. "Footnote definition"
  3536. "Footnote reference")
  3537. ))
  3538. t)))
  3539. (defun org-activate-bracket-links (limit)
  3540. "Run through the buffer and add overlays to bracketed links."
  3541. (if (re-search-forward org-bracket-link-regexp limit t)
  3542. (let* ((help (concat "LINK: "
  3543. (org-match-string-no-properties 1)))
  3544. ;; FIXME: above we should remove the escapes.
  3545. ;; but that requires another match, protecting match data,
  3546. ;; a lot of overhead for font-lock.
  3547. (ip (org-maybe-intangible
  3548. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3549. 'keymap org-mouse-map 'mouse-face 'highlight
  3550. 'font-lock-multiline t 'help-echo help)))
  3551. (vp (list 'rear-nonsticky org-nonsticky-props
  3552. 'keymap org-mouse-map 'mouse-face 'highlight
  3553. ' font-lock-multiline t 'help-echo help)))
  3554. ;; We need to remove the invisible property here. Table narrowing
  3555. ;; may have made some of this invisible.
  3556. (remove-text-properties (match-beginning 0) (match-end 0)
  3557. '(invisible nil))
  3558. (if (match-end 3)
  3559. (progn
  3560. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3561. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3562. (add-text-properties (match-end 3) (match-end 0) ip))
  3563. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3564. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3565. (add-text-properties (match-end 1) (match-end 0) ip))
  3566. t)))
  3567. (defun org-activate-dates (limit)
  3568. "Run through the buffer and add overlays to dates."
  3569. (if (re-search-forward org-tsr-regexp-both limit t)
  3570. (progn
  3571. (add-text-properties (match-beginning 0) (match-end 0)
  3572. (list 'mouse-face 'highlight
  3573. 'rear-nonsticky org-nonsticky-props
  3574. 'keymap org-mouse-map))
  3575. (when org-display-custom-times
  3576. (if (match-end 3)
  3577. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3578. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3579. t)))
  3580. (defvar org-target-link-regexp nil
  3581. "Regular expression matching radio targets in plain text.")
  3582. (make-variable-buffer-local 'org-target-link-regexp)
  3583. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3584. "Regular expression matching a link target.")
  3585. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3586. "Regular expression matching a radio target.")
  3587. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3588. "Regular expression matching any target.")
  3589. (defun org-activate-target-links (limit)
  3590. "Run through the buffer and add overlays to target matches."
  3591. (when org-target-link-regexp
  3592. (let ((case-fold-search t))
  3593. (if (re-search-forward org-target-link-regexp limit t)
  3594. (progn
  3595. (add-text-properties (match-beginning 0) (match-end 0)
  3596. (list 'mouse-face 'highlight
  3597. 'rear-nonsticky org-nonsticky-props
  3598. 'keymap org-mouse-map
  3599. 'help-echo "Radio target link"
  3600. 'org-linked-text t))
  3601. t)))))
  3602. (defun org-update-radio-target-regexp ()
  3603. "Find all radio targets in this file and update the regular expression."
  3604. (interactive)
  3605. (when (memq 'radio org-activate-links)
  3606. (setq org-target-link-regexp
  3607. (org-make-target-link-regexp (org-all-targets 'radio)))
  3608. (org-restart-font-lock)))
  3609. (defun org-hide-wide-columns (limit)
  3610. (let (s e)
  3611. (setq s (text-property-any (point) (or limit (point-max))
  3612. 'org-cwidth t))
  3613. (when s
  3614. (setq e (next-single-property-change s 'org-cwidth))
  3615. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3616. (goto-char e)
  3617. t)))
  3618. (defvar org-latex-and-specials-regexp nil
  3619. "Regular expression for highlighting export special stuff.")
  3620. (defvar org-match-substring-regexp)
  3621. (defvar org-match-substring-with-braces-regexp)
  3622. (defvar org-export-html-special-string-regexps)
  3623. (defun org-compute-latex-and-specials-regexp ()
  3624. "Compute regular expression for stuff treated specially by exporters."
  3625. (if (not org-highlight-latex-fragments-and-specials)
  3626. (org-set-local 'org-latex-and-specials-regexp nil)
  3627. (require 'org-exp)
  3628. (let*
  3629. ((matchers (plist-get org-format-latex-options :matchers))
  3630. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3631. org-latex-regexps)))
  3632. (options (org-combine-plists (org-default-export-plist)
  3633. (org-infile-export-plist)))
  3634. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3635. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3636. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3637. (org-export-html-expand (plist-get options :expand-quoted-html))
  3638. (org-export-with-special-strings (plist-get options :special-strings))
  3639. (re-sub
  3640. (cond
  3641. ((equal org-export-with-sub-superscripts '{})
  3642. (list org-match-substring-with-braces-regexp))
  3643. (org-export-with-sub-superscripts
  3644. (list org-match-substring-regexp))
  3645. (t nil)))
  3646. (re-latex
  3647. (if org-export-with-LaTeX-fragments
  3648. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3649. (re-macros
  3650. (if org-export-with-TeX-macros
  3651. (list (concat "\\\\"
  3652. (regexp-opt
  3653. (append (mapcar 'car org-html-entities)
  3654. (if (boundp 'org-latex-entities)
  3655. org-latex-entities nil))
  3656. 'words))) ; FIXME
  3657. ))
  3658. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3659. (re-special (if org-export-with-special-strings
  3660. (mapcar (lambda (x) (car x))
  3661. org-export-html-special-string-regexps)))
  3662. (re-rest
  3663. (delq nil
  3664. (list
  3665. (if org-export-html-expand "@<[^>\n]+>")
  3666. ))))
  3667. (org-set-local
  3668. 'org-latex-and-specials-regexp
  3669. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3670. re-rest) "\\|")))))
  3671. (defun org-do-latex-and-special-faces (limit)
  3672. "Run through the buffer and add overlays to links."
  3673. (when org-latex-and-specials-regexp
  3674. (let (rtn d)
  3675. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3676. limit t))
  3677. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3678. 'face))
  3679. '(org-code org-verbatim underline)))
  3680. (progn
  3681. (setq rtn t
  3682. d (cond ((member (char-after (1+ (match-beginning 0)))
  3683. '(?_ ?^)) 1)
  3684. (t 0)))
  3685. (font-lock-prepend-text-property
  3686. (+ d (match-beginning 0)) (match-end 0)
  3687. 'face 'org-latex-and-export-specials)
  3688. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3689. '(font-lock-multiline t)))))
  3690. rtn)))
  3691. (defun org-restart-font-lock ()
  3692. "Restart font-lock-mode, to force refontification."
  3693. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3694. (font-lock-mode -1)
  3695. (font-lock-mode 1)))
  3696. (defun org-all-targets (&optional radio)
  3697. "Return a list of all targets in this file.
  3698. With optional argument RADIO, only find radio targets."
  3699. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3700. rtn)
  3701. (save-excursion
  3702. (goto-char (point-min))
  3703. (while (re-search-forward re nil t)
  3704. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3705. rtn)))
  3706. (defun org-make-target-link-regexp (targets)
  3707. "Make regular expression matching all strings in TARGETS.
  3708. The regular expression finds the targets also if there is a line break
  3709. between words."
  3710. (and targets
  3711. (concat
  3712. "\\<\\("
  3713. (mapconcat
  3714. (lambda (x)
  3715. (while (string-match " +" x)
  3716. (setq x (replace-match "\\s-+" t t x)))
  3717. x)
  3718. targets
  3719. "\\|")
  3720. "\\)\\>")))
  3721. (defun org-activate-tags (limit)
  3722. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3723. (progn
  3724. (add-text-properties (match-beginning 1) (match-end 1)
  3725. (list 'mouse-face 'highlight
  3726. 'rear-nonsticky org-nonsticky-props
  3727. 'keymap org-mouse-map))
  3728. t)))
  3729. (defun org-outline-level ()
  3730. (save-excursion
  3731. (looking-at outline-regexp)
  3732. (if (match-beginning 1)
  3733. (+ (org-get-string-indentation (match-string 1)) 1000)
  3734. (1- (- (match-end 0) (match-beginning 0))))))
  3735. (defvar org-font-lock-keywords nil)
  3736. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3737. "Regular expression matching a property line.")
  3738. (defvar org-font-lock-hook nil
  3739. "Functions to be called for special font lock stuff.")
  3740. (defun org-font-lock-hook (limit)
  3741. (run-hook-with-args 'org-font-lock-hook limit))
  3742. (defun org-set-font-lock-defaults ()
  3743. (let* ((em org-fontify-emphasized-text)
  3744. (lk org-activate-links)
  3745. (org-font-lock-extra-keywords
  3746. (list
  3747. ;; Call the hook
  3748. '(org-font-lock-hook)
  3749. ;; Headlines
  3750. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3751. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3752. ;; Table lines
  3753. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3754. (1 'org-table t))
  3755. ;; Table internals
  3756. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3757. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3758. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3759. ;; Drawers
  3760. (list org-drawer-regexp '(0 'org-special-keyword t))
  3761. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3762. ;; Properties
  3763. (list org-property-re
  3764. '(1 'org-special-keyword t)
  3765. '(3 'org-property-value t))
  3766. (if org-format-transports-properties-p
  3767. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3768. ;; Links
  3769. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3770. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3771. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3772. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3773. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3774. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3775. (if (memq 'footnote lk) '(org-activate-footnote-links
  3776. (2 'org-footnote t)))
  3777. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3778. '(org-hide-wide-columns (0 nil append))
  3779. ;; TODO lines
  3780. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3781. '(1 (org-get-todo-face 1) t))
  3782. ;; DONE
  3783. (if org-fontify-done-headline
  3784. (list (concat "^[*]+ +\\<\\("
  3785. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3786. "\\)\\(.*\\)")
  3787. '(2 'org-headline-done t))
  3788. nil)
  3789. ;; Priorities
  3790. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3791. ;; Tags
  3792. '(org-font-lock-add-tag-faces)
  3793. ;; Special keywords
  3794. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3795. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3796. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3797. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3798. ;; Emphasis
  3799. (if em
  3800. (if (featurep 'xemacs)
  3801. '(org-do-emphasis-faces (0 nil append))
  3802. '(org-do-emphasis-faces)))
  3803. ;; Checkboxes
  3804. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3805. 2 'bold prepend)
  3806. (if org-provide-checkbox-statistics
  3807. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3808. (0 (org-get-checkbox-statistics-face) t)))
  3809. ;; Description list items
  3810. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3811. 2 'bold prepend)
  3812. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3813. '(1 'org-archived prepend))
  3814. ;; Specials
  3815. '(org-do-latex-and-special-faces)
  3816. ;; Code
  3817. '(org-activate-code (1 'org-code t))
  3818. ;; COMMENT
  3819. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3820. "\\|" org-quote-string "\\)\\>")
  3821. '(1 'org-special-keyword t))
  3822. '("^#.*" (0 'font-lock-comment-face t))
  3823. )))
  3824. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3825. ;; Now set the full font-lock-keywords
  3826. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3827. (org-set-local 'font-lock-defaults
  3828. '(org-font-lock-keywords t nil nil backward-paragraph))
  3829. (kill-local-variable 'font-lock-keywords) nil))
  3830. (defvar org-m nil)
  3831. (defvar org-l nil)
  3832. (defvar org-f nil)
  3833. (defun org-get-level-face (n)
  3834. "Get the right face for match N in font-lock matching of headlines."
  3835. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3836. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3837. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3838. (cond
  3839. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3840. ((eq n 2) org-f)
  3841. (t (if org-level-color-stars-only nil org-f))))
  3842. (defun org-get-todo-face (kwd)
  3843. "Get the right face for a TODO keyword KWD.
  3844. If KWD is a number, get the corresponding match group."
  3845. (if (numberp kwd) (setq kwd (match-string kwd)))
  3846. (or (cdr (assoc kwd org-todo-keyword-faces))
  3847. (and (member kwd org-done-keywords) 'org-done)
  3848. 'org-todo))
  3849. (defun org-font-lock-add-tag-faces (limit)
  3850. "Add the special tag faces."
  3851. (when (and org-tag-faces org-tags-special-faces-re)
  3852. (while (re-search-forward org-tags-special-faces-re limit t)
  3853. (add-text-properties (match-beginning 1) (match-end 1)
  3854. (list 'face (org-get-tag-face 1)
  3855. 'font-lock-fontified t))
  3856. (backward-char 1))))
  3857. (defun org-get-tag-face (kwd)
  3858. "Get the right face for a TODO keyword KWD.
  3859. If KWD is a number, get the corresponding match group."
  3860. (if (numberp kwd) (setq kwd (match-string kwd)))
  3861. (or (cdr (assoc kwd org-tag-faces))
  3862. 'org-tag))
  3863. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3864. "Remove fontification and activation overlays from links."
  3865. (font-lock-default-unfontify-region beg end)
  3866. (let* ((buffer-undo-list t)
  3867. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3868. (inhibit-modification-hooks t)
  3869. deactivate-mark buffer-file-name buffer-file-truename)
  3870. (remove-text-properties beg end
  3871. '(mouse-face t keymap t org-linked-text t
  3872. invisible t intangible t))))
  3873. ;;;; Visibility cycling, including org-goto and indirect buffer
  3874. ;;; Cycling
  3875. (defvar org-cycle-global-status nil)
  3876. (make-variable-buffer-local 'org-cycle-global-status)
  3877. (defvar org-cycle-subtree-status nil)
  3878. (make-variable-buffer-local 'org-cycle-subtree-status)
  3879. ;;;###autoload
  3880. (defun org-cycle (&optional arg)
  3881. "Visibility cycling for Org-mode.
  3882. - When this function is called with a prefix argument, rotate the entire
  3883. buffer through 3 states (global cycling)
  3884. 1. OVERVIEW: Show only top-level headlines.
  3885. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3886. 3. SHOW ALL: Show everything.
  3887. When called with two C-u C-u prefixes, switch to the startup visibility,
  3888. determined by the variable `org-startup-folded', and by any VISIBILITY
  3889. properties in the buffer.
  3890. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3891. including drawers.
  3892. - When point is at the beginning of a headline, rotate the subtree started
  3893. by this line through 3 different states (local cycling)
  3894. 1. FOLDED: Only the main headline is shown.
  3895. 2. CHILDREN: The main headline and the direct children are shown.
  3896. From this state, you can move to one of the children
  3897. and zoom in further.
  3898. 3. SUBTREE: Show the entire subtree, including body text.
  3899. - When there is a numeric prefix, go up to a heading with level ARG, do
  3900. a `show-subtree' and return to the previous cursor position. If ARG
  3901. is negative, go up that many levels.
  3902. - When point is not at the beginning of a headline, execute the global
  3903. binding for TAB, which is re-indenting the line. See the option
  3904. `org-cycle-emulate-tab' for details.
  3905. - Special case: if point is at the beginning of the buffer and there is
  3906. no headline in line 1, this function will act as if called with prefix arg.
  3907. But only if also the variable `org-cycle-global-at-bob' is t."
  3908. (interactive "P")
  3909. (org-load-modules-maybe)
  3910. (let* ((outline-regexp
  3911. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3912. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3913. outline-regexp))
  3914. (bob-special (and org-cycle-global-at-bob (bobp)
  3915. (not (looking-at outline-regexp))))
  3916. (org-cycle-hook
  3917. (if bob-special
  3918. (delq 'org-optimize-window-after-visibility-change
  3919. (copy-sequence org-cycle-hook))
  3920. org-cycle-hook))
  3921. (pos (point)))
  3922. (if (or bob-special (equal arg '(4)))
  3923. ;; special case: use global cycling
  3924. (setq arg t))
  3925. (cond
  3926. ((equal arg '(16))
  3927. (org-set-startup-visibility)
  3928. (message "Startup visibility, plus VISIBILITY properties"))
  3929. ((equal arg '(64))
  3930. (show-all)
  3931. (message "Entire buffer visible, including drawers"))
  3932. ((org-at-table-p 'any)
  3933. ;; Enter the table or move to the next field in the table
  3934. (or (org-table-recognize-table.el)
  3935. (progn
  3936. (if arg (org-table-edit-field t)
  3937. (org-table-justify-field-maybe)
  3938. (call-interactively 'org-table-next-field)))))
  3939. ((eq arg t) ;; Global cycling
  3940. (cond
  3941. ((and (eq last-command this-command)
  3942. (eq org-cycle-global-status 'overview))
  3943. ;; We just created the overview - now do table of contents
  3944. ;; This can be slow in very large buffers, so indicate action
  3945. (message "CONTENTS...")
  3946. (org-content)
  3947. (message "CONTENTS...done")
  3948. (setq org-cycle-global-status 'contents)
  3949. (run-hook-with-args 'org-cycle-hook 'contents))
  3950. ((and (eq last-command this-command)
  3951. (eq org-cycle-global-status 'contents))
  3952. ;; We just showed the table of contents - now show everything
  3953. (show-all)
  3954. (message "SHOW ALL")
  3955. (setq org-cycle-global-status 'all)
  3956. (run-hook-with-args 'org-cycle-hook 'all))
  3957. (t
  3958. ;; Default action: go to overview
  3959. (org-overview)
  3960. (message "OVERVIEW")
  3961. (setq org-cycle-global-status 'overview)
  3962. (run-hook-with-args 'org-cycle-hook 'overview))))
  3963. ((and org-drawers org-drawer-regexp
  3964. (save-excursion
  3965. (beginning-of-line 1)
  3966. (looking-at org-drawer-regexp)))
  3967. ;; Toggle block visibility
  3968. (org-flag-drawer
  3969. (not (get-char-property (match-end 0) 'invisible))))
  3970. ((integerp arg)
  3971. ;; Show-subtree, ARG levels up from here.
  3972. (save-excursion
  3973. (org-back-to-heading)
  3974. (outline-up-heading (if (< arg 0) (- arg)
  3975. (- (funcall outline-level) arg)))
  3976. (org-show-subtree)))
  3977. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3978. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3979. ;; At a heading: rotate between three different views
  3980. (org-back-to-heading)
  3981. (let ((goal-column 0) eoh eol eos)
  3982. ;; First, some boundaries
  3983. (save-excursion
  3984. (org-back-to-heading)
  3985. (save-excursion
  3986. (beginning-of-line 2)
  3987. (while (and (not (eobp)) ;; this is like `next-line'
  3988. (get-char-property (1- (point)) 'invisible))
  3989. (beginning-of-line 2)) (setq eol (point)))
  3990. (outline-end-of-heading) (setq eoh (point))
  3991. (org-end-of-subtree t)
  3992. (unless (eobp)
  3993. (skip-chars-forward " \t\n")
  3994. (beginning-of-line 1) ; in case this is an item
  3995. )
  3996. (setq eos (1- (point))))
  3997. ;; Find out what to do next and set `this-command'
  3998. (cond
  3999. ((= eos eoh)
  4000. ;; Nothing is hidden behind this heading
  4001. (message "EMPTY ENTRY")
  4002. (setq org-cycle-subtree-status nil)
  4003. (save-excursion
  4004. (goto-char eos)
  4005. (outline-next-heading)
  4006. (if (org-invisible-p) (org-flag-heading nil))))
  4007. ((or (>= eol eos)
  4008. (not (string-match "\\S-" (buffer-substring eol eos))))
  4009. ;; Entire subtree is hidden in one line: open it
  4010. (org-show-entry)
  4011. (show-children)
  4012. (message "CHILDREN")
  4013. (save-excursion
  4014. (goto-char eos)
  4015. (outline-next-heading)
  4016. (if (org-invisible-p) (org-flag-heading nil)))
  4017. (setq org-cycle-subtree-status 'children)
  4018. (run-hook-with-args 'org-cycle-hook 'children))
  4019. ((and (eq last-command this-command)
  4020. (eq org-cycle-subtree-status 'children))
  4021. ;; We just showed the children, now show everything.
  4022. (org-show-subtree)
  4023. (message "SUBTREE")
  4024. (setq org-cycle-subtree-status 'subtree)
  4025. (run-hook-with-args 'org-cycle-hook 'subtree))
  4026. (t
  4027. ;; Default action: hide the subtree.
  4028. (hide-subtree)
  4029. (message "FOLDED")
  4030. (setq org-cycle-subtree-status 'folded)
  4031. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4032. ;; TAB emulation and template completion
  4033. (buffer-read-only (org-back-to-heading))
  4034. ((org-try-structure-completion))
  4035. ((org-try-cdlatex-tab))
  4036. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4037. (or (not (bolp))
  4038. (not (looking-at outline-regexp))))
  4039. (call-interactively (global-key-binding "\t")))
  4040. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4041. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4042. (or (and (eq org-cycle-emulate-tab 'white)
  4043. (= (match-end 0) (point-at-eol)))
  4044. (and (eq org-cycle-emulate-tab 'whitestart)
  4045. (>= (match-end 0) pos))))
  4046. t
  4047. (eq org-cycle-emulate-tab t))
  4048. (call-interactively (global-key-binding "\t")))
  4049. (t (save-excursion
  4050. (org-back-to-heading)
  4051. (org-cycle))))))
  4052. ;;;###autoload
  4053. (defun org-global-cycle (&optional arg)
  4054. "Cycle the global visibility. For details see `org-cycle'.
  4055. With C-u prefix arg, switch to startup visibility.
  4056. With a numeric prefix, show all headlines up to that level."
  4057. (interactive "P")
  4058. (let ((org-cycle-include-plain-lists
  4059. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4060. (cond
  4061. ((integerp arg)
  4062. (show-all)
  4063. (hide-sublevels arg)
  4064. (setq org-cycle-global-status 'contents))
  4065. ((equal arg '(4))
  4066. (org-set-startup-visibility)
  4067. (message "Startup visibility, plus VISIBILITY properties."))
  4068. (t
  4069. (org-cycle '(4))))))
  4070. (defun org-set-startup-visibility ()
  4071. "Set the visibility required by startup options and properties."
  4072. (cond
  4073. ((eq org-startup-folded t)
  4074. (org-cycle '(4)))
  4075. ((eq org-startup-folded 'content)
  4076. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4077. (org-cycle '(4)) (org-cycle '(4)))))
  4078. (org-set-visibility-according-to-property 'no-cleanup)
  4079. (org-cycle-hide-archived-subtrees 'all)
  4080. (org-cycle-hide-drawers 'all)
  4081. (org-cycle-show-empty-lines 'all))
  4082. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4083. "Switch subtree visibilities according to :VISIBILITY: property."
  4084. (interactive)
  4085. (let (org-show-entry-below state)
  4086. (save-excursion
  4087. (goto-char (point-min))
  4088. (while (re-search-forward
  4089. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4090. nil t)
  4091. (setq state (match-string 1))
  4092. (save-excursion
  4093. (org-back-to-heading t)
  4094. (hide-subtree)
  4095. (org-reveal)
  4096. (cond
  4097. ((equal state '("fold" "folded"))
  4098. (hide-subtree))
  4099. ((equal state "children")
  4100. (org-show-hidden-entry)
  4101. (show-children))
  4102. ((equal state "content")
  4103. (save-excursion
  4104. (save-restriction
  4105. (org-narrow-to-subtree)
  4106. (org-content))))
  4107. ((member state '("all" "showall"))
  4108. (show-subtree)))))
  4109. (unless no-cleanup
  4110. (org-cycle-hide-archived-subtrees 'all)
  4111. (org-cycle-hide-drawers 'all)
  4112. (org-cycle-show-empty-lines 'all)))))
  4113. (defun org-overview ()
  4114. "Switch to overview mode, showing only top-level headlines.
  4115. Really, this shows all headlines with level equal or greater than the level
  4116. of the first headline in the buffer. This is important, because if the
  4117. first headline is not level one, then (hide-sublevels 1) gives confusing
  4118. results."
  4119. (interactive)
  4120. (let ((level (save-excursion
  4121. (goto-char (point-min))
  4122. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4123. (progn
  4124. (goto-char (match-beginning 0))
  4125. (funcall outline-level))))))
  4126. (and level (hide-sublevels level))))
  4127. (defun org-content (&optional arg)
  4128. "Show all headlines in the buffer, like a table of contents.
  4129. With numerical argument N, show content up to level N."
  4130. (interactive "P")
  4131. (save-excursion
  4132. ;; Visit all headings and show their offspring
  4133. (and (integerp arg) (org-overview))
  4134. (goto-char (point-max))
  4135. (catch 'exit
  4136. (while (and (progn (condition-case nil
  4137. (outline-previous-visible-heading 1)
  4138. (error (goto-char (point-min))))
  4139. t)
  4140. (looking-at outline-regexp))
  4141. (if (integerp arg)
  4142. (show-children (1- arg))
  4143. (show-branches))
  4144. (if (bobp) (throw 'exit nil))))))
  4145. (defun org-optimize-window-after-visibility-change (state)
  4146. "Adjust the window after a change in outline visibility.
  4147. This function is the default value of the hook `org-cycle-hook'."
  4148. (when (get-buffer-window (current-buffer))
  4149. (cond
  4150. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4151. ; ((eq state 'overview) (org-beginning-of-line))
  4152. ((eq state 'content) nil)
  4153. ((eq state 'all) nil)
  4154. ((eq state 'folded) nil)
  4155. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4156. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4157. (defun org-compact-display-after-subtree-move ()
  4158. "Show a compacter version of the tree of the entry's parent."
  4159. (save-excursion
  4160. (if (org-up-heading-safe)
  4161. (progn
  4162. (hide-subtree)
  4163. (show-entry)
  4164. (show-children)
  4165. (org-cycle-show-empty-lines 'children)
  4166. (org-cycle-hide-drawers 'children))
  4167. (org-overview))))
  4168. (defun org-cycle-show-empty-lines (state)
  4169. "Show empty lines above all visible headlines.
  4170. The region to be covered depends on STATE when called through
  4171. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4172. entire buffer covered. Note that an empty line is only shown if there
  4173. are at least `org-cycle-separator-lines' empty lines before the headline."
  4174. (when (> org-cycle-separator-lines 0)
  4175. (save-excursion
  4176. (let* ((n org-cycle-separator-lines)
  4177. (re (cond
  4178. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4179. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4180. (t (let ((ns (number-to-string (- n 2))))
  4181. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4182. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4183. beg end)
  4184. (cond
  4185. ((memq state '(overview contents t))
  4186. (setq beg (point-min) end (point-max)))
  4187. ((memq state '(children folded))
  4188. (setq beg (point) end (progn (org-end-of-subtree t t)
  4189. (beginning-of-line 2)
  4190. (point)))))
  4191. (when beg
  4192. (goto-char beg)
  4193. (while (re-search-forward re end t)
  4194. (if (not (get-char-property (match-end 1) 'invisible))
  4195. (outline-flag-region
  4196. (match-beginning 1) (match-end 1) nil)))))))
  4197. ;; Never hide empty lines at the end of the file.
  4198. (save-excursion
  4199. (goto-char (point-max))
  4200. (outline-previous-heading)
  4201. (outline-end-of-heading)
  4202. (if (and (looking-at "[ \t\n]+")
  4203. (= (match-end 0) (point-max)))
  4204. (outline-flag-region (point) (match-end 0) nil))))
  4205. (defun org-show-empty-lines-in-parent ()
  4206. "Move to the parent and re-show empty lines before visible headlines."
  4207. (save-excursion
  4208. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4209. (org-cycle-show-empty-lines context))))
  4210. (defun org-cycle-hide-drawers (state)
  4211. "Re-hide all drawers after a visibility state change."
  4212. (when (and (org-mode-p)
  4213. (not (memq state '(overview folded))))
  4214. (save-excursion
  4215. (let* ((globalp (memq state '(contents all)))
  4216. (beg (if globalp (point-min) (point)))
  4217. (end (if globalp (point-max) (org-end-of-subtree t))))
  4218. (goto-char beg)
  4219. (while (re-search-forward org-drawer-regexp end t)
  4220. (org-flag-drawer t))))))
  4221. (defun org-flag-drawer (flag)
  4222. (save-excursion
  4223. (beginning-of-line 1)
  4224. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4225. (let ((b (match-end 0))
  4226. (outline-regexp org-outline-regexp))
  4227. (if (re-search-forward
  4228. "^[ \t]*:END:"
  4229. (save-excursion (outline-next-heading) (point)) t)
  4230. (outline-flag-region b (point-at-eol) flag)
  4231. (error ":END: line missing"))))))
  4232. (defun org-subtree-end-visible-p ()
  4233. "Is the end of the current subtree visible?"
  4234. (pos-visible-in-window-p
  4235. (save-excursion (org-end-of-subtree t) (point))))
  4236. (defun org-first-headline-recenter (&optional N)
  4237. "Move cursor to the first headline and recenter the headline.
  4238. Optional argument N means, put the headline into the Nth line of the window."
  4239. (goto-char (point-min))
  4240. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4241. (beginning-of-line)
  4242. (recenter (prefix-numeric-value N))))
  4243. ;;; Org-goto
  4244. (defvar org-goto-window-configuration nil)
  4245. (defvar org-goto-marker nil)
  4246. (defvar org-goto-map
  4247. (let ((map (make-sparse-keymap)))
  4248. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4249. (while (setq cmd (pop cmds))
  4250. (substitute-key-definition cmd cmd map global-map)))
  4251. (suppress-keymap map)
  4252. (org-defkey map "\C-m" 'org-goto-ret)
  4253. (org-defkey map [(return)] 'org-goto-ret)
  4254. (org-defkey map [(left)] 'org-goto-left)
  4255. (org-defkey map [(right)] 'org-goto-right)
  4256. (org-defkey map [(control ?g)] 'org-goto-quit)
  4257. (org-defkey map "\C-i" 'org-cycle)
  4258. (org-defkey map [(tab)] 'org-cycle)
  4259. (org-defkey map [(down)] 'outline-next-visible-heading)
  4260. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4261. (if org-goto-auto-isearch
  4262. (if (fboundp 'define-key-after)
  4263. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4264. nil)
  4265. (org-defkey map "q" 'org-goto-quit)
  4266. (org-defkey map "n" 'outline-next-visible-heading)
  4267. (org-defkey map "p" 'outline-previous-visible-heading)
  4268. (org-defkey map "f" 'outline-forward-same-level)
  4269. (org-defkey map "b" 'outline-backward-same-level)
  4270. (org-defkey map "u" 'outline-up-heading))
  4271. (org-defkey map "/" 'org-occur)
  4272. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4273. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4274. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4275. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4276. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4277. map))
  4278. (defconst org-goto-help
  4279. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4280. RET=jump to location [Q]uit and return to previous location
  4281. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4282. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4283. ;; FIXME: Docstring doe not mention both interfaces
  4284. (defun org-goto (&optional alternative-interface)
  4285. "Look up a different location in the current file, keeping current visibility.
  4286. When you want look-up or go to a different location in a document, the
  4287. fastest way is often to fold the entire buffer and then dive into the tree.
  4288. This method has the disadvantage, that the previous location will be folded,
  4289. which may not be what you want.
  4290. This command works around this by showing a copy of the current buffer
  4291. in an indirect buffer, in overview mode. You can dive into the tree in
  4292. that copy, use org-occur and incremental search to find a location.
  4293. When pressing RET or `Q', the command returns to the original buffer in
  4294. which the visibility is still unchanged. After RET is will also jump to
  4295. the location selected in the indirect buffer and expose the
  4296. the headline hierarchy above."
  4297. (interactive "P")
  4298. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4299. (org-refile-use-outline-path t)
  4300. (interface
  4301. (if (not alternative-interface)
  4302. org-goto-interface
  4303. (if (eq org-goto-interface 'outline)
  4304. 'outline-path-completion
  4305. 'outline)))
  4306. (org-goto-start-pos (point))
  4307. (selected-point
  4308. (if (eq interface 'outline)
  4309. (car (org-get-location (current-buffer) org-goto-help))
  4310. (nth 3 (org-refile-get-location "Goto: ")))))
  4311. (if selected-point
  4312. (progn
  4313. (org-mark-ring-push org-goto-start-pos)
  4314. (goto-char selected-point)
  4315. (if (or (org-invisible-p) (org-invisible-p2))
  4316. (org-show-context 'org-goto)))
  4317. (message "Quit"))))
  4318. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4319. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4320. (defvar org-goto-local-auto-isearch-map) ; defined below
  4321. (defun org-get-location (buf help)
  4322. "Let the user select a location in the Org-mode buffer BUF.
  4323. This function uses a recursive edit. It returns the selected position
  4324. or nil."
  4325. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4326. (isearch-hide-immediately nil)
  4327. (isearch-search-fun-function
  4328. (lambda () 'org-goto-local-search-headings))
  4329. (org-goto-selected-point org-goto-exit-command))
  4330. (save-excursion
  4331. (save-window-excursion
  4332. (delete-other-windows)
  4333. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4334. (switch-to-buffer
  4335. (condition-case nil
  4336. (make-indirect-buffer (current-buffer) "*org-goto*")
  4337. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4338. (with-output-to-temp-buffer "*Help*"
  4339. (princ help))
  4340. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4341. (setq buffer-read-only nil)
  4342. (let ((org-startup-truncated t)
  4343. (org-startup-folded nil)
  4344. (org-startup-align-all-tables nil))
  4345. (org-mode)
  4346. (org-overview))
  4347. (setq buffer-read-only t)
  4348. (if (and (boundp 'org-goto-start-pos)
  4349. (integer-or-marker-p org-goto-start-pos))
  4350. (let ((org-show-hierarchy-above t)
  4351. (org-show-siblings t)
  4352. (org-show-following-heading t))
  4353. (goto-char org-goto-start-pos)
  4354. (and (org-invisible-p) (org-show-context)))
  4355. (goto-char (point-min)))
  4356. (org-beginning-of-line)
  4357. (message "Select location and press RET")
  4358. (use-local-map org-goto-map)
  4359. (recursive-edit)
  4360. ))
  4361. (kill-buffer "*org-goto*")
  4362. (cons org-goto-selected-point org-goto-exit-command)))
  4363. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4364. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4365. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4366. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4367. (defun org-goto-local-search-headings (string bound noerror)
  4368. "Search and make sure that any matches are in headlines."
  4369. (catch 'return
  4370. (while (if isearch-forward
  4371. (search-forward string bound noerror)
  4372. (search-backward string bound noerror))
  4373. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4374. (and (member :headline context)
  4375. (not (member :tags context))))
  4376. (throw 'return (point))))))
  4377. (defun org-goto-local-auto-isearch ()
  4378. "Start isearch."
  4379. (interactive)
  4380. (goto-char (point-min))
  4381. (let ((keys (this-command-keys)))
  4382. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4383. (isearch-mode t)
  4384. (isearch-process-search-char (string-to-char keys)))))
  4385. (defun org-goto-ret (&optional arg)
  4386. "Finish `org-goto' by going to the new location."
  4387. (interactive "P")
  4388. (setq org-goto-selected-point (point)
  4389. org-goto-exit-command 'return)
  4390. (throw 'exit nil))
  4391. (defun org-goto-left ()
  4392. "Finish `org-goto' by going to the new location."
  4393. (interactive)
  4394. (if (org-on-heading-p)
  4395. (progn
  4396. (beginning-of-line 1)
  4397. (setq org-goto-selected-point (point)
  4398. org-goto-exit-command 'left)
  4399. (throw 'exit nil))
  4400. (error "Not on a heading")))
  4401. (defun org-goto-right ()
  4402. "Finish `org-goto' by going to the new location."
  4403. (interactive)
  4404. (if (org-on-heading-p)
  4405. (progn
  4406. (setq org-goto-selected-point (point)
  4407. org-goto-exit-command 'right)
  4408. (throw 'exit nil))
  4409. (error "Not on a heading")))
  4410. (defun org-goto-quit ()
  4411. "Finish `org-goto' without cursor motion."
  4412. (interactive)
  4413. (setq org-goto-selected-point nil)
  4414. (setq org-goto-exit-command 'quit)
  4415. (throw 'exit nil))
  4416. ;;; Indirect buffer display of subtrees
  4417. (defvar org-indirect-dedicated-frame nil
  4418. "This is the frame being used for indirect tree display.")
  4419. (defvar org-last-indirect-buffer nil)
  4420. (defun org-tree-to-indirect-buffer (&optional arg)
  4421. "Create indirect buffer and narrow it to current subtree.
  4422. With numerical prefix ARG, go up to this level and then take that tree.
  4423. If ARG is negative, go up that many levels.
  4424. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4425. indirect buffer previously made with this command, to avoid proliferation of
  4426. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4427. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4428. is kept so that you can work with several indirect buffers at the same time.
  4429. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4430. requests that a new frame be made for the new buffer, so that the dedicated
  4431. frame is not changed."
  4432. (interactive "P")
  4433. (let ((cbuf (current-buffer))
  4434. (cwin (selected-window))
  4435. (pos (point))
  4436. beg end level heading ibuf)
  4437. (save-excursion
  4438. (org-back-to-heading t)
  4439. (when (numberp arg)
  4440. (setq level (org-outline-level))
  4441. (if (< arg 0) (setq arg (+ level arg)))
  4442. (while (> (setq level (org-outline-level)) arg)
  4443. (outline-up-heading 1 t)))
  4444. (setq beg (point)
  4445. heading (org-get-heading))
  4446. (org-end-of-subtree t) (setq end (point)))
  4447. (if (and (buffer-live-p org-last-indirect-buffer)
  4448. (not (eq org-indirect-buffer-display 'new-frame))
  4449. (not arg))
  4450. (kill-buffer org-last-indirect-buffer))
  4451. (setq ibuf (org-get-indirect-buffer cbuf)
  4452. org-last-indirect-buffer ibuf)
  4453. (cond
  4454. ((or (eq org-indirect-buffer-display 'new-frame)
  4455. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4456. (select-frame (make-frame))
  4457. (delete-other-windows)
  4458. (switch-to-buffer ibuf)
  4459. (org-set-frame-title heading))
  4460. ((eq org-indirect-buffer-display 'dedicated-frame)
  4461. (raise-frame
  4462. (select-frame (or (and org-indirect-dedicated-frame
  4463. (frame-live-p org-indirect-dedicated-frame)
  4464. org-indirect-dedicated-frame)
  4465. (setq org-indirect-dedicated-frame (make-frame)))))
  4466. (delete-other-windows)
  4467. (switch-to-buffer ibuf)
  4468. (org-set-frame-title (concat "Indirect: " heading)))
  4469. ((eq org-indirect-buffer-display 'current-window)
  4470. (switch-to-buffer ibuf))
  4471. ((eq org-indirect-buffer-display 'other-window)
  4472. (pop-to-buffer ibuf))
  4473. (t (error "Invalid value.")))
  4474. (if (featurep 'xemacs)
  4475. (save-excursion (org-mode) (turn-on-font-lock)))
  4476. (narrow-to-region beg end)
  4477. (show-all)
  4478. (goto-char pos)
  4479. (and (window-live-p cwin) (select-window cwin))))
  4480. (defun org-get-indirect-buffer (&optional buffer)
  4481. (setq buffer (or buffer (current-buffer)))
  4482. (let ((n 1) (base (buffer-name buffer)) bname)
  4483. (while (buffer-live-p
  4484. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4485. (setq n (1+ n)))
  4486. (condition-case nil
  4487. (make-indirect-buffer buffer bname 'clone)
  4488. (error (make-indirect-buffer buffer bname)))))
  4489. (defun org-set-frame-title (title)
  4490. "Set the title of the current frame to the string TITLE."
  4491. ;; FIXME: how to name a single frame in XEmacs???
  4492. (unless (featurep 'xemacs)
  4493. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4494. ;;;; Structure editing
  4495. ;;; Inserting headlines
  4496. (defun org-previous-line-empty-p ()
  4497. (save-excursion
  4498. (and (not (bobp))
  4499. (or (beginning-of-line 0) t)
  4500. (save-match-data
  4501. (looking-at "[ \t]*$")))))
  4502. (defun org-insert-heading (&optional force-heading)
  4503. "Insert a new heading or item with same depth at point.
  4504. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4505. If point is at the beginning of a headline, insert a sibling before the
  4506. current headline. If point is not at the beginning, do not split the line,
  4507. but create the new headline after the current line."
  4508. (interactive "P")
  4509. (if (= (buffer-size) 0)
  4510. (insert "\n* ")
  4511. (when (or force-heading (not (org-insert-item)))
  4512. (let* ((empty-line-p nil)
  4513. (head (save-excursion
  4514. (condition-case nil
  4515. (progn
  4516. (org-back-to-heading)
  4517. (setq empty-line-p (org-previous-line-empty-p))
  4518. (match-string 0))
  4519. (error "*"))))
  4520. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4521. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4522. pos hide-previous previous-pos)
  4523. (cond
  4524. ((and (org-on-heading-p) (bolp)
  4525. (or (bobp)
  4526. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4527. ;; insert before the current line
  4528. (open-line (if blank 2 1)))
  4529. ((and (bolp)
  4530. (or (bobp)
  4531. (save-excursion
  4532. (backward-char 1) (not (org-invisible-p)))))
  4533. ;; insert right here
  4534. nil)
  4535. (t
  4536. ;; somewhere in the line
  4537. (save-excursion
  4538. (setq previous-pos (point-at-bol))
  4539. (end-of-line)
  4540. (setq hide-previous (org-invisible-p)))
  4541. (and org-insert-heading-respect-content (org-show-subtree))
  4542. (let ((split
  4543. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4544. (save-excursion
  4545. (let ((p (point)))
  4546. (goto-char (point-at-bol))
  4547. (and (looking-at org-complex-heading-regexp)
  4548. (> p (match-beginning 4)))))))
  4549. tags pos)
  4550. (cond
  4551. (org-insert-heading-respect-content
  4552. (org-end-of-subtree nil t)
  4553. (or (bolp) (newline))
  4554. (or (org-previous-line-empty-p)
  4555. (and blank (newline)))
  4556. (open-line 1))
  4557. ((org-on-heading-p)
  4558. (when hide-previous
  4559. (show-children)
  4560. (org-show-entry))
  4561. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4562. (setq tags (and (match-end 2) (match-string 2)))
  4563. (and (match-end 1)
  4564. (delete-region (match-beginning 1) (match-end 1)))
  4565. (setq pos (point-at-bol))
  4566. (or split (end-of-line 1))
  4567. (delete-horizontal-space)
  4568. (newline (if blank 2 1))
  4569. (when tags
  4570. (save-excursion
  4571. (goto-char pos)
  4572. (end-of-line 1)
  4573. (insert " " tags)
  4574. (org-set-tags nil 'align))))
  4575. (t
  4576. (or split (end-of-line 1))
  4577. (newline (if blank 2 1)))))))
  4578. (insert head) (just-one-space)
  4579. (setq pos (point))
  4580. (end-of-line 1)
  4581. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4582. (when (and org-insert-heading-respect-content hide-previous)
  4583. (save-excursion
  4584. (goto-char previous-pos)
  4585. (hide-subtree)))
  4586. (run-hooks 'org-insert-heading-hook)))))
  4587. (defun org-get-heading (&optional no-tags)
  4588. "Return the heading of the current entry, without the stars."
  4589. (save-excursion
  4590. (org-back-to-heading t)
  4591. (if (looking-at
  4592. (if no-tags
  4593. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4594. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4595. (match-string 1) "")))
  4596. (defun org-heading-components ()
  4597. "Return the components of the current heading.
  4598. This is a list with the following elements:
  4599. - the level as an integer
  4600. - the reduced level, different if `org-odd-levels-only' is set.
  4601. - the TODO keyword, or nil
  4602. - the priority character, like ?A, or nil if no priority is given
  4603. - the headline text itself, or the tags string if no headline text
  4604. - the tags string, or nil."
  4605. (save-excursion
  4606. (org-back-to-heading t)
  4607. (if (looking-at org-complex-heading-regexp)
  4608. (list (length (match-string 1))
  4609. (org-reduced-level (length (match-string 1)))
  4610. (org-match-string-no-properties 2)
  4611. (and (match-end 3) (aref (match-string 3) 2))
  4612. (org-match-string-no-properties 4)
  4613. (org-match-string-no-properties 5)))))
  4614. (defun org-insert-heading-after-current ()
  4615. "Insert a new heading with same level as current, after current subtree."
  4616. (interactive)
  4617. (org-back-to-heading)
  4618. (org-insert-heading)
  4619. (org-move-subtree-down)
  4620. (end-of-line 1))
  4621. (defun org-insert-heading-respect-content ()
  4622. (interactive)
  4623. (let ((org-insert-heading-respect-content t))
  4624. (org-insert-heading t)))
  4625. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4626. (interactive "P")
  4627. (let ((org-insert-heading-respect-content t))
  4628. (org-insert-todo-heading force-state t)))
  4629. (defun org-insert-todo-heading (arg &optional force-heading)
  4630. "Insert a new heading with the same level and TODO state as current heading.
  4631. If the heading has no TODO state, or if the state is DONE, use the first
  4632. state (TODO by default). Also with prefix arg, force first state."
  4633. (interactive "P")
  4634. (when (or force-heading (not (org-insert-item 'checkbox)))
  4635. (org-insert-heading force-heading)
  4636. (save-excursion
  4637. (org-back-to-heading)
  4638. (outline-previous-heading)
  4639. (looking-at org-todo-line-regexp))
  4640. (let*
  4641. ((new-mark-x
  4642. (if (or arg
  4643. (not (match-beginning 2))
  4644. (member (match-string 2) org-done-keywords))
  4645. (car org-todo-keywords-1)
  4646. (match-string 2)))
  4647. (new-mark
  4648. (or
  4649. (run-hook-with-args-until-success
  4650. 'org-todo-get-default-hook new-mark-x nil)
  4651. new-mark-x)))
  4652. (insert new-mark " "))
  4653. (when org-provide-todo-statistics
  4654. (org-update-parent-todo-statistics))))
  4655. (defun org-insert-subheading (arg)
  4656. "Insert a new subheading and demote it.
  4657. Works for outline headings and for plain lists alike."
  4658. (interactive "P")
  4659. (org-insert-heading arg)
  4660. (cond
  4661. ((org-on-heading-p) (org-do-demote))
  4662. ((org-at-item-p) (org-indent-item 1))))
  4663. (defun org-insert-todo-subheading (arg)
  4664. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4665. Works for outline headings and for plain lists alike."
  4666. (interactive "P")
  4667. (org-insert-todo-heading arg)
  4668. (cond
  4669. ((org-on-heading-p) (org-do-demote))
  4670. ((org-at-item-p) (org-indent-item 1))))
  4671. ;;; Promotion and Demotion
  4672. (defun org-promote-subtree ()
  4673. "Promote the entire subtree.
  4674. See also `org-promote'."
  4675. (interactive)
  4676. (save-excursion
  4677. (org-map-tree 'org-promote))
  4678. (org-fix-position-after-promote))
  4679. (defun org-demote-subtree ()
  4680. "Demote the entire subtree. See `org-demote'.
  4681. See also `org-promote'."
  4682. (interactive)
  4683. (save-excursion
  4684. (org-map-tree 'org-demote))
  4685. (org-fix-position-after-promote))
  4686. (defun org-do-promote ()
  4687. "Promote the current heading higher up the tree.
  4688. If the region is active in `transient-mark-mode', promote all headings
  4689. in the region."
  4690. (interactive)
  4691. (save-excursion
  4692. (if (org-region-active-p)
  4693. (org-map-region 'org-promote (region-beginning) (region-end))
  4694. (org-promote)))
  4695. (org-fix-position-after-promote))
  4696. (defun org-do-demote ()
  4697. "Demote the current heading lower down the tree.
  4698. If the region is active in `transient-mark-mode', demote all headings
  4699. in the region."
  4700. (interactive)
  4701. (save-excursion
  4702. (if (org-region-active-p)
  4703. (org-map-region 'org-demote (region-beginning) (region-end))
  4704. (org-demote)))
  4705. (org-fix-position-after-promote))
  4706. (defun org-fix-position-after-promote ()
  4707. "Make sure that after pro/demotion cursor position is right."
  4708. (let ((pos (point)))
  4709. (when (save-excursion
  4710. (beginning-of-line 1)
  4711. (looking-at org-todo-line-regexp)
  4712. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4713. (cond ((eobp) (insert " "))
  4714. ((eolp) (insert " "))
  4715. ((equal (char-after) ?\ ) (forward-char 1))))))
  4716. (defun org-reduced-level (l)
  4717. "Compute the effective level of a heading.
  4718. This takes into account the setting of `org-odd-levels-only'."
  4719. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4720. (defun org-get-valid-level (level &optional change)
  4721. "Rectify a level change under the influence of `org-odd-levels-only'
  4722. LEVEL is a current level, CHANGE is by how much the level should be
  4723. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4724. even level numbers will become the next higher odd number."
  4725. (if org-odd-levels-only
  4726. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4727. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4728. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4729. (max 1 (+ level change))))
  4730. (if (boundp 'define-obsolete-function-alias)
  4731. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4732. (define-obsolete-function-alias 'org-get-legal-level
  4733. 'org-get-valid-level)
  4734. (define-obsolete-function-alias 'org-get-legal-level
  4735. 'org-get-valid-level "23.1")))
  4736. (defun org-promote ()
  4737. "Promote the current heading higher up the tree.
  4738. If the region is active in `transient-mark-mode', promote all headings
  4739. in the region."
  4740. (org-back-to-heading t)
  4741. (let* ((level (save-match-data (funcall outline-level)))
  4742. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4743. (diff (abs (- level (length up-head) -1))))
  4744. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4745. (replace-match up-head nil t)
  4746. ;; Fixup tag positioning
  4747. (and org-auto-align-tags (org-set-tags nil t))
  4748. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4749. (defun org-demote ()
  4750. "Demote the current heading lower down the tree.
  4751. If the region is active in `transient-mark-mode', demote all headings
  4752. in the region."
  4753. (org-back-to-heading t)
  4754. (let* ((level (save-match-data (funcall outline-level)))
  4755. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4756. (diff (abs (- level (length down-head) -1))))
  4757. (replace-match down-head nil t)
  4758. ;; Fixup tag positioning
  4759. (and org-auto-align-tags (org-set-tags nil t))
  4760. (if org-adapt-indentation (org-fixup-indentation diff))))
  4761. (defun org-map-tree (fun)
  4762. "Call FUN for every heading underneath the current one."
  4763. (org-back-to-heading)
  4764. (let ((level (funcall outline-level)))
  4765. (save-excursion
  4766. (funcall fun)
  4767. (while (and (progn
  4768. (outline-next-heading)
  4769. (> (funcall outline-level) level))
  4770. (not (eobp)))
  4771. (funcall fun)))))
  4772. (defun org-map-region (fun beg end)
  4773. "Call FUN for every heading between BEG and END."
  4774. (let ((org-ignore-region t))
  4775. (save-excursion
  4776. (setq end (copy-marker end))
  4777. (goto-char beg)
  4778. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4779. (< (point) end))
  4780. (funcall fun))
  4781. (while (and (progn
  4782. (outline-next-heading)
  4783. (< (point) end))
  4784. (not (eobp)))
  4785. (funcall fun)))))
  4786. (defun org-fixup-indentation (diff)
  4787. "Change the indentation in the current entry by DIFF
  4788. However, if any line in the current entry has no indentation, or if it
  4789. would end up with no indentation after the change, nothing at all is done."
  4790. (save-excursion
  4791. (let ((end (save-excursion (outline-next-heading)
  4792. (point-marker)))
  4793. (prohibit (if (> diff 0)
  4794. "^\\S-"
  4795. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4796. col)
  4797. (unless (save-excursion (end-of-line 1)
  4798. (re-search-forward prohibit end t))
  4799. (while (and (< (point) end)
  4800. (re-search-forward "^[ \t]+" end t))
  4801. (goto-char (match-end 0))
  4802. (setq col (current-column))
  4803. (if (< diff 0) (replace-match ""))
  4804. (org-indent-to-column (+ diff col))))
  4805. (move-marker end nil))))
  4806. (defun org-convert-to-odd-levels ()
  4807. "Convert an org-mode file with all levels allowed to one with odd levels.
  4808. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4809. level 5 etc."
  4810. (interactive)
  4811. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4812. (let ((org-odd-levels-only nil) n)
  4813. (save-excursion
  4814. (goto-char (point-min))
  4815. (while (re-search-forward "^\\*\\*+ " nil t)
  4816. (setq n (- (length (match-string 0)) 2))
  4817. (while (>= (setq n (1- n)) 0)
  4818. (org-demote))
  4819. (end-of-line 1))))))
  4820. (defun org-convert-to-oddeven-levels ()
  4821. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4822. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4823. section with an even level, conversion would destroy the structure of the file. An error
  4824. is signaled in this case."
  4825. (interactive)
  4826. (goto-char (point-min))
  4827. ;; First check if there are no even levels
  4828. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4829. (org-show-context t)
  4830. (error "Not all levels are odd in this file. Conversion not possible."))
  4831. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4832. (let ((org-odd-levels-only nil) n)
  4833. (save-excursion
  4834. (goto-char (point-min))
  4835. (while (re-search-forward "^\\*\\*+ " nil t)
  4836. (setq n (/ (1- (length (match-string 0))) 2))
  4837. (while (>= (setq n (1- n)) 0)
  4838. (org-promote))
  4839. (end-of-line 1))))))
  4840. (defun org-tr-level (n)
  4841. "Make N odd if required."
  4842. (if org-odd-levels-only (1+ (/ n 2)) n))
  4843. ;;; Vertical tree motion, cutting and pasting of subtrees
  4844. (defun org-move-subtree-up (&optional arg)
  4845. "Move the current subtree up past ARG headlines of the same level."
  4846. (interactive "p")
  4847. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4848. (defun org-move-subtree-down (&optional arg)
  4849. "Move the current subtree down past ARG headlines of the same level."
  4850. (interactive "p")
  4851. (setq arg (prefix-numeric-value arg))
  4852. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4853. 'outline-get-last-sibling))
  4854. (ins-point (make-marker))
  4855. (cnt (abs arg))
  4856. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4857. ;; Select the tree
  4858. (org-back-to-heading)
  4859. (setq beg0 (point))
  4860. (save-excursion
  4861. (setq ne-beg (org-back-over-empty-lines))
  4862. (setq beg (point)))
  4863. (save-match-data
  4864. (save-excursion (outline-end-of-heading)
  4865. (setq folded (org-invisible-p)))
  4866. (outline-end-of-subtree))
  4867. (outline-next-heading)
  4868. (setq ne-end (org-back-over-empty-lines))
  4869. (setq end (point))
  4870. (goto-char beg0)
  4871. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4872. ;; include less whitespace
  4873. (save-excursion
  4874. (goto-char beg)
  4875. (forward-line (- ne-beg ne-end))
  4876. (setq beg (point))))
  4877. ;; Find insertion point, with error handling
  4878. (while (> cnt 0)
  4879. (or (and (funcall movfunc) (looking-at outline-regexp))
  4880. (progn (goto-char beg0)
  4881. (error "Cannot move past superior level or buffer limit")))
  4882. (setq cnt (1- cnt)))
  4883. (if (> arg 0)
  4884. ;; Moving forward - still need to move over subtree
  4885. (progn (org-end-of-subtree t t)
  4886. (save-excursion
  4887. (org-back-over-empty-lines)
  4888. (or (bolp) (newline)))))
  4889. (setq ne-ins (org-back-over-empty-lines))
  4890. (move-marker ins-point (point))
  4891. (setq txt (buffer-substring beg end))
  4892. (org-save-markers-in-region beg end)
  4893. (delete-region beg end)
  4894. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4895. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4896. (let ((bbb (point)))
  4897. (insert-before-markers txt)
  4898. (org-reinstall-markers-in-region bbb)
  4899. (move-marker ins-point bbb))
  4900. (or (bolp) (insert "\n"))
  4901. (setq ins-end (point))
  4902. (goto-char ins-point)
  4903. (org-skip-whitespace)
  4904. (when (and (< arg 0)
  4905. (org-first-sibling-p)
  4906. (> ne-ins ne-beg))
  4907. ;; Move whitespace back to beginning
  4908. (save-excursion
  4909. (goto-char ins-end)
  4910. (let ((kill-whole-line t))
  4911. (kill-line (- ne-ins ne-beg)) (point)))
  4912. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4913. (move-marker ins-point nil)
  4914. (org-compact-display-after-subtree-move)
  4915. (org-show-empty-lines-in-parent)
  4916. (unless folded
  4917. (org-show-entry)
  4918. (show-children)
  4919. (org-cycle-hide-drawers 'children))))
  4920. (defvar org-subtree-clip ""
  4921. "Clipboard for cut and paste of subtrees.
  4922. This is actually only a copy of the kill, because we use the normal kill
  4923. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4924. (defvar org-subtree-clip-folded nil
  4925. "Was the last copied subtree folded?
  4926. This is used to fold the tree back after pasting.")
  4927. (defun org-cut-subtree (&optional n)
  4928. "Cut the current subtree into the clipboard.
  4929. With prefix arg N, cut this many sequential subtrees.
  4930. This is a short-hand for marking the subtree and then cutting it."
  4931. (interactive "p")
  4932. (org-copy-subtree n 'cut))
  4933. (defun org-copy-subtree (&optional n cut force-store-markers)
  4934. "Cut the current subtree into the clipboard.
  4935. With prefix arg N, cut this many sequential subtrees.
  4936. This is a short-hand for marking the subtree and then copying it.
  4937. If CUT is non-nil, actually cut the subtree.
  4938. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4939. of some markers in the region, even if CUT is non-nil. This is
  4940. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4941. (interactive "p")
  4942. (let (beg end folded (beg0 (point)))
  4943. (if (interactive-p)
  4944. (org-back-to-heading nil) ; take what looks like a subtree
  4945. (org-back-to-heading t)) ; take what is really there
  4946. (org-back-over-empty-lines)
  4947. (setq beg (point))
  4948. (skip-chars-forward " \t\r\n")
  4949. (save-match-data
  4950. (save-excursion (outline-end-of-heading)
  4951. (setq folded (org-invisible-p)))
  4952. (condition-case nil
  4953. (outline-forward-same-level (1- n))
  4954. (error nil))
  4955. (org-end-of-subtree t t))
  4956. (org-back-over-empty-lines)
  4957. (setq end (point))
  4958. (goto-char beg0)
  4959. (when (> end beg)
  4960. (setq org-subtree-clip-folded folded)
  4961. (when (or cut force-store-markers)
  4962. (org-save-markers-in-region beg end))
  4963. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4964. (setq org-subtree-clip (current-kill 0))
  4965. (message "%s: Subtree(s) with %d characters"
  4966. (if cut "Cut" "Copied")
  4967. (length org-subtree-clip)))))
  4968. (defun org-paste-subtree (&optional level tree for-yank)
  4969. "Paste the clipboard as a subtree, with modification of headline level.
  4970. The entire subtree is promoted or demoted in order to match a new headline
  4971. level.
  4972. If the cursor is at the beginning of a headline, the same level as
  4973. that headline is used to paste the tree
  4974. If not, the new level is derived from the *visible* headings
  4975. before and after the insertion point, and taken to be the inferior headline
  4976. level of the two. So if the previous visible heading is level 3 and the
  4977. next is level 4 (or vice versa), level 4 will be used for insertion.
  4978. This makes sure that the subtree remains an independent subtree and does
  4979. not swallow low level entries.
  4980. You can also force a different level, either by using a numeric prefix
  4981. argument, or by inserting the heading marker by hand. For example, if the
  4982. cursor is after \"*****\", then the tree will be shifted to level 5.
  4983. If optional TREE is given, use this text instead of the kill ring.
  4984. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4985. move back over whitespace before inserting, and move point to the end of
  4986. the inserted text when done."
  4987. (interactive "P")
  4988. (unless (org-kill-is-subtree-p tree)
  4989. (error "%s"
  4990. (substitute-command-keys
  4991. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4992. (let* ((visp (not (org-invisible-p)))
  4993. (txt (or tree (and kill-ring (current-kill 0))))
  4994. (^re (concat "^\\(" outline-regexp "\\)"))
  4995. (re (concat "\\(" outline-regexp "\\)"))
  4996. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4997. (old-level (if (string-match ^re txt)
  4998. (- (match-end 0) (match-beginning 0) 1)
  4999. -1))
  5000. (force-level (cond (level (prefix-numeric-value level))
  5001. ((and (looking-at "[ \t]*$")
  5002. (string-match
  5003. ^re_ (buffer-substring
  5004. (point-at-bol) (point))))
  5005. (- (match-end 1) (match-beginning 1)))
  5006. ((and (bolp)
  5007. (looking-at org-outline-regexp))
  5008. (- (match-end 0) (point) 1))
  5009. (t nil)))
  5010. (previous-level (save-excursion
  5011. (condition-case nil
  5012. (progn
  5013. (outline-previous-visible-heading 1)
  5014. (if (looking-at re)
  5015. (- (match-end 0) (match-beginning 0) 1)
  5016. 1))
  5017. (error 1))))
  5018. (next-level (save-excursion
  5019. (condition-case nil
  5020. (progn
  5021. (or (looking-at outline-regexp)
  5022. (outline-next-visible-heading 1))
  5023. (if (looking-at re)
  5024. (- (match-end 0) (match-beginning 0) 1)
  5025. 1))
  5026. (error 1))))
  5027. (new-level (or force-level (max previous-level next-level)))
  5028. (shift (if (or (= old-level -1)
  5029. (= new-level -1)
  5030. (= old-level new-level))
  5031. 0
  5032. (- new-level old-level)))
  5033. (delta (if (> shift 0) -1 1))
  5034. (func (if (> shift 0) 'org-demote 'org-promote))
  5035. (org-odd-levels-only nil)
  5036. beg end newend)
  5037. ;; Remove the forced level indicator
  5038. (if force-level
  5039. (delete-region (point-at-bol) (point)))
  5040. ;; Paste
  5041. (beginning-of-line 1)
  5042. (unless for-yank (org-back-over-empty-lines))
  5043. (setq beg (point))
  5044. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5045. (insert-before-markers txt)
  5046. (unless (string-match "\n\\'" txt) (insert "\n"))
  5047. (setq newend (point))
  5048. (org-reinstall-markers-in-region beg)
  5049. (setq end (point))
  5050. (goto-char beg)
  5051. (skip-chars-forward " \t\n\r")
  5052. (setq beg (point))
  5053. (if (and (org-invisible-p) visp)
  5054. (save-excursion (outline-show-heading)))
  5055. ;; Shift if necessary
  5056. (unless (= shift 0)
  5057. (save-restriction
  5058. (narrow-to-region beg end)
  5059. (while (not (= shift 0))
  5060. (org-map-region func (point-min) (point-max))
  5061. (setq shift (+ delta shift)))
  5062. (goto-char (point-min))
  5063. (setq newend (point-max))))
  5064. (when (or (interactive-p) for-yank)
  5065. (message "Clipboard pasted as level %d subtree" new-level))
  5066. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5067. kill-ring
  5068. (eq org-subtree-clip (current-kill 0))
  5069. org-subtree-clip-folded)
  5070. ;; The tree was folded before it was killed/copied
  5071. (hide-subtree))
  5072. (and for-yank (goto-char newend))))
  5073. (defun org-kill-is-subtree-p (&optional txt)
  5074. "Check if the current kill is an outline subtree, or a set of trees.
  5075. Returns nil if kill does not start with a headline, or if the first
  5076. headline level is not the largest headline level in the tree.
  5077. So this will actually accept several entries of equal levels as well,
  5078. which is OK for `org-paste-subtree'.
  5079. If optional TXT is given, check this string instead of the current kill."
  5080. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5081. (start-level (and kill
  5082. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5083. org-outline-regexp "\\)")
  5084. kill)
  5085. (- (match-end 2) (match-beginning 2) 1)))
  5086. (re (concat "^" org-outline-regexp))
  5087. (start (1+ (or (match-beginning 2) -1))))
  5088. (if (not start-level)
  5089. (progn
  5090. nil) ;; does not even start with a heading
  5091. (catch 'exit
  5092. (while (setq start (string-match re kill (1+ start)))
  5093. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5094. (throw 'exit nil)))
  5095. t))))
  5096. (defvar org-markers-to-move nil
  5097. "Markers that should be moved with a cut-and-paste operation.
  5098. Those markers are stored together with their positions relative to
  5099. the start of the region.")
  5100. (defun org-save-markers-in-region (beg end)
  5101. "Check markers in region.
  5102. If these markers are between BEG and END, record their position relative
  5103. to BEG, so that after moving the block of text, we can put the markers back
  5104. into place.
  5105. This function gets called just before an entry or tree gets cut from the
  5106. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5107. called immediately, to move the markers with the entries."
  5108. (setq org-markers-to-move nil)
  5109. (when (featurep 'org-clock)
  5110. (org-clock-save-markers-for-cut-and-paste beg end))
  5111. (when (featurep 'org-agenda)
  5112. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5113. (defun org-check-and-save-marker (marker beg end)
  5114. "Check if MARKER is between BEG and END.
  5115. If yes, remember the marker and the distance to BEG."
  5116. (when (and (marker-buffer marker)
  5117. (equal (marker-buffer marker) (current-buffer)))
  5118. (if (and (>= marker beg) (< marker end))
  5119. (push (cons marker (- marker beg)) org-markers-to-move))))
  5120. (defun org-reinstall-markers-in-region (beg)
  5121. "Move all remembered markers to their position relative to BEG."
  5122. (mapc (lambda (x)
  5123. (move-marker (car x) (+ beg (cdr x))))
  5124. org-markers-to-move)
  5125. (setq org-markers-to-move nil))
  5126. (defun org-narrow-to-subtree ()
  5127. "Narrow buffer to the current subtree."
  5128. (interactive)
  5129. (save-excursion
  5130. (save-match-data
  5131. (narrow-to-region
  5132. (progn (org-back-to-heading) (point))
  5133. (progn (org-end-of-subtree t) (point))))))
  5134. ;;; Outline Sorting
  5135. (defun org-sort (with-case)
  5136. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5137. Optional argument WITH-CASE means sort case-sensitively.
  5138. With a double prefix argument, also remove duplicate entries."
  5139. (interactive "P")
  5140. (if (org-at-table-p)
  5141. (org-call-with-arg 'org-table-sort-lines with-case)
  5142. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5143. (defun org-sort-remove-invisible (s)
  5144. (remove-text-properties 0 (length s) org-rm-props s)
  5145. (while (string-match org-bracket-link-regexp s)
  5146. (setq s (replace-match (if (match-end 2)
  5147. (match-string 3 s)
  5148. (match-string 1 s)) t t s)))
  5149. s)
  5150. (defvar org-priority-regexp) ; defined later in the file
  5151. (defun org-sort-entries-or-items
  5152. (&optional with-case sorting-type getkey-func compare-func property)
  5153. "Sort entries on a certain level of an outline tree.
  5154. If there is an active region, the entries in the region are sorted.
  5155. Else, if the cursor is before the first entry, sort the top-level items.
  5156. Else, the children of the entry at point are sorted.
  5157. Sorting can be alphabetically, numerically, and by date/time as given by
  5158. the first time stamp in the entry. The command prompts for the sorting
  5159. type unless it has been given to the function through the SORTING-TYPE
  5160. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5161. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5162. called with point at the beginning of the record. It must return either
  5163. a string or a number that should serve as the sorting key for that record.
  5164. Comparing entries ignores case by default. However, with an optional argument
  5165. WITH-CASE, the sorting considers case as well."
  5166. (interactive "P")
  5167. (let ((case-func (if with-case 'identity 'downcase))
  5168. start beg end stars re re2
  5169. txt what tmp plain-list-p)
  5170. ;; Find beginning and end of region to sort
  5171. (cond
  5172. ((org-region-active-p)
  5173. ;; we will sort the region
  5174. (setq end (region-end)
  5175. what "region")
  5176. (goto-char (region-beginning))
  5177. (if (not (org-on-heading-p)) (outline-next-heading))
  5178. (setq start (point)))
  5179. ((org-at-item-p)
  5180. ;; we will sort this plain list
  5181. (org-beginning-of-item-list) (setq start (point))
  5182. (org-end-of-item-list) (setq end (point))
  5183. (goto-char start)
  5184. (setq plain-list-p t
  5185. what "plain list"))
  5186. ((or (org-on-heading-p)
  5187. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5188. ;; we will sort the children of the current headline
  5189. (org-back-to-heading)
  5190. (setq start (point)
  5191. end (progn (org-end-of-subtree t t)
  5192. (org-back-over-empty-lines)
  5193. (point))
  5194. what "children")
  5195. (goto-char start)
  5196. (show-subtree)
  5197. (outline-next-heading))
  5198. (t
  5199. ;; we will sort the top-level entries in this file
  5200. (goto-char (point-min))
  5201. (or (org-on-heading-p) (outline-next-heading))
  5202. (setq start (point) end (point-max) what "top-level")
  5203. (goto-char start)
  5204. (show-all)))
  5205. (setq beg (point))
  5206. (if (>= beg end) (error "Nothing to sort"))
  5207. (unless plain-list-p
  5208. (looking-at "\\(\\*+\\)")
  5209. (setq stars (match-string 1)
  5210. re (concat "^" (regexp-quote stars) " +")
  5211. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5212. txt (buffer-substring beg end))
  5213. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5214. (if (and (not (equal stars "*")) (string-match re2 txt))
  5215. (error "Region to sort contains a level above the first entry")))
  5216. (unless sorting-type
  5217. (message
  5218. (if plain-list-p
  5219. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5220. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5221. what)
  5222. (setq sorting-type (read-char-exclusive))
  5223. (and (= (downcase sorting-type) ?f)
  5224. (setq getkey-func
  5225. (org-ido-completing-read "Sort using function: "
  5226. obarray 'fboundp t nil nil))
  5227. (setq getkey-func (intern getkey-func)))
  5228. (and (= (downcase sorting-type) ?r)
  5229. (setq property
  5230. (org-ido-completing-read "Property: "
  5231. (mapcar 'list (org-buffer-property-keys t))
  5232. nil t))))
  5233. (message "Sorting entries...")
  5234. (save-restriction
  5235. (narrow-to-region start end)
  5236. (let ((dcst (downcase sorting-type))
  5237. (now (current-time)))
  5238. (sort-subr
  5239. (/= dcst sorting-type)
  5240. ;; This function moves to the beginning character of the "record" to
  5241. ;; be sorted.
  5242. (if plain-list-p
  5243. (lambda nil
  5244. (if (org-at-item-p) t (goto-char (point-max))))
  5245. (lambda nil
  5246. (if (re-search-forward re nil t)
  5247. (goto-char (match-beginning 0))
  5248. (goto-char (point-max)))))
  5249. ;; This function moves to the last character of the "record" being
  5250. ;; sorted.
  5251. (if plain-list-p
  5252. 'org-end-of-item
  5253. (lambda nil
  5254. (save-match-data
  5255. (condition-case nil
  5256. (outline-forward-same-level 1)
  5257. (error
  5258. (goto-char (point-max)))))))
  5259. ;; This function returns the value that gets sorted against.
  5260. (if plain-list-p
  5261. (lambda nil
  5262. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5263. (cond
  5264. ((= dcst ?n)
  5265. (string-to-number (buffer-substring (match-end 0)
  5266. (point-at-eol))))
  5267. ((= dcst ?a)
  5268. (buffer-substring (match-end 0) (point-at-eol)))
  5269. ((= dcst ?t)
  5270. (if (re-search-forward org-ts-regexp
  5271. (point-at-eol) t)
  5272. (org-time-string-to-time (match-string 0))
  5273. now))
  5274. ((= dcst ?f)
  5275. (if getkey-func
  5276. (progn
  5277. (setq tmp (funcall getkey-func))
  5278. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5279. tmp)
  5280. (error "Invalid key function `%s'" getkey-func)))
  5281. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5282. (lambda nil
  5283. (cond
  5284. ((= dcst ?n)
  5285. (if (looking-at org-complex-heading-regexp)
  5286. (string-to-number (match-string 4))
  5287. nil))
  5288. ((= dcst ?a)
  5289. (if (looking-at org-complex-heading-regexp)
  5290. (funcall case-func (match-string 4))
  5291. nil))
  5292. ((= dcst ?t)
  5293. (if (re-search-forward org-ts-regexp
  5294. (save-excursion
  5295. (forward-line 2)
  5296. (point)) t)
  5297. (org-time-string-to-time (match-string 0))
  5298. now))
  5299. ((= dcst ?p)
  5300. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5301. (string-to-char (match-string 2))
  5302. org-default-priority))
  5303. ((= dcst ?r)
  5304. (or (org-entry-get nil property) ""))
  5305. ((= dcst ?o)
  5306. (if (looking-at org-complex-heading-regexp)
  5307. (- 9999 (length (member (match-string 2)
  5308. org-todo-keywords-1)))))
  5309. ((= dcst ?f)
  5310. (if getkey-func
  5311. (progn
  5312. (setq tmp (funcall getkey-func))
  5313. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5314. tmp)
  5315. (error "Invalid key function `%s'" getkey-func)))
  5316. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5317. nil
  5318. (cond
  5319. ((= dcst ?a) 'string<)
  5320. ((= dcst ?t) 'time-less-p)
  5321. ((= dcst ?f) compare-func)
  5322. (t nil)))))
  5323. (message "Sorting entries...done")))
  5324. (defun org-do-sort (table what &optional with-case sorting-type)
  5325. "Sort TABLE of WHAT according to SORTING-TYPE.
  5326. The user will be prompted for the SORTING-TYPE if the call to this
  5327. function does not specify it. WHAT is only for the prompt, to indicate
  5328. what is being sorted. The sorting key will be extracted from
  5329. the car of the elements of the table.
  5330. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5331. (unless sorting-type
  5332. (message
  5333. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5334. what)
  5335. (setq sorting-type (read-char-exclusive)))
  5336. (let ((dcst (downcase sorting-type))
  5337. extractfun comparefun)
  5338. ;; Define the appropriate functions
  5339. (cond
  5340. ((= dcst ?n)
  5341. (setq extractfun 'string-to-number
  5342. comparefun (if (= dcst sorting-type) '< '>)))
  5343. ((= dcst ?a)
  5344. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5345. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5346. comparefun (if (= dcst sorting-type)
  5347. 'string<
  5348. (lambda (a b) (and (not (string< a b))
  5349. (not (string= a b)))))))
  5350. ((= dcst ?t)
  5351. (setq extractfun
  5352. (lambda (x)
  5353. (if (or (string-match org-ts-regexp x)
  5354. (string-match org-ts-regexp-both x))
  5355. (time-to-seconds
  5356. (org-time-string-to-time (match-string 0 x)))
  5357. 0))
  5358. comparefun (if (= dcst sorting-type) '< '>)))
  5359. (t (error "Invalid sorting type `%c'" sorting-type)))
  5360. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5361. table)
  5362. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5363. ;;; Editing source examples
  5364. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5365. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5366. (defvar org-edit-src-force-single-line nil)
  5367. (defvar org-edit-src-from-org-mode nil)
  5368. (defvar org-edit-src-picture nil)
  5369. (define-minor-mode org-exit-edit-mode
  5370. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5371. (defun org-edit-src-code ()
  5372. "Edit the source code example at point.
  5373. An indirect buffer is created, and that buffer is then narrowed to the
  5374. example at point and switched to the correct language mode. When done,
  5375. exit by killing the buffer with \\[org-edit-src-exit]."
  5376. (interactive)
  5377. (let ((line (org-current-line))
  5378. (case-fold-search t)
  5379. (msg (substitute-command-keys
  5380. "Edit, then exit with C-c ' (C-c and single quote)"))
  5381. (info (org-edit-src-find-region-and-lang))
  5382. (org-mode-p (eq major-mode 'org-mode))
  5383. beg end lang lang-f single lfmt)
  5384. (if (not info)
  5385. nil
  5386. (setq beg (nth 0 info)
  5387. end (nth 1 info)
  5388. lang (nth 2 info)
  5389. single (nth 3 info)
  5390. lfmt (nth 4 info)
  5391. lang-f (intern (concat lang "-mode")))
  5392. (unless (functionp lang-f)
  5393. (error "No such language mode: %s" lang-f))
  5394. (goto-line line)
  5395. (if (get-buffer "*Org Edit Src Example*")
  5396. (kill-buffer "*Org Edit Src Example*"))
  5397. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5398. "*Org Edit Src Example*"))
  5399. (narrow-to-region beg end)
  5400. (remove-text-properties beg end '(display nil invisible nil
  5401. intangible nil))
  5402. (let ((org-inhibit-startup t))
  5403. (funcall lang-f))
  5404. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5405. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5406. (when lfmt
  5407. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5408. (when org-mode-p
  5409. (goto-char (point-min))
  5410. (while (re-search-forward "^," nil t)
  5411. (replace-match "")))
  5412. (goto-line line)
  5413. (org-exit-edit-mode)
  5414. (org-set-local 'header-line-format msg)
  5415. (message "%s" msg)
  5416. t)))
  5417. (defun org-edit-fixed-width-region ()
  5418. "Edit the fixed-width ascii drawing at point.
  5419. This must be a region where each line starts with a colon followed by
  5420. a space character.
  5421. An indirect buffer is created, and that buffer is then narrowed to the
  5422. example at point and switched to artist-mode. When done,
  5423. exit by killing the buffer with \\[org-edit-src-exit]."
  5424. (interactive)
  5425. (let ((line (org-current-line))
  5426. (case-fold-search t)
  5427. (msg (substitute-command-keys
  5428. "Edit, then exit with C-c ' (C-c and single quote)"))
  5429. (org-mode-p (eq major-mode 'org-mode))
  5430. beg end)
  5431. (beginning-of-line 1)
  5432. (if (looking-at "[ \t]*[^:\n \t]")
  5433. nil
  5434. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5435. (setq beg (point) end beg)
  5436. (save-excursion
  5437. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5438. (setq beg (point-at-bol 2))
  5439. (setq beg (point))))
  5440. (save-excursion
  5441. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5442. (setq end (1- (match-beginning 0)))
  5443. (setq end (point))))
  5444. (goto-line line))
  5445. (if (get-buffer "*Org Edit Picture*")
  5446. (kill-buffer "*Org Edit Picture*"))
  5447. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5448. "*Org Edit Picture*"))
  5449. (narrow-to-region beg end)
  5450. (remove-text-properties beg end '(display nil invisible nil
  5451. intangible nil))
  5452. (when (fboundp 'font-lock-unfontify-region)
  5453. (font-lock-unfontify-region (point-min) (point-max)))
  5454. (cond
  5455. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5456. (fundamental-mode)
  5457. (artist-mode 1))
  5458. (t (funcall org-edit-fixed-width-region-mode)))
  5459. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5460. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5461. (set (make-local-variable 'org-edit-src-picture) t)
  5462. (goto-char (point-min))
  5463. (while (re-search-forward "^[ \t]*: ?" nil t)
  5464. (replace-match ""))
  5465. (goto-line line)
  5466. (org-exit-edit-mode)
  5467. (org-set-local 'header-line-format msg)
  5468. (message "%s" msg)
  5469. t)))
  5470. (defun org-edit-src-find-region-and-lang ()
  5471. "Find the region and language for a local edit.
  5472. Return a list with beginning and end of the region, a string representing
  5473. the language, a switch telling of the content should be in a single line."
  5474. (let ((re-list
  5475. (append
  5476. org-edit-src-region-extra
  5477. '(
  5478. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5479. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5480. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5481. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5482. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5483. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5484. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5485. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5486. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5487. ("^#\\+html:" "\n" "html" single-line)
  5488. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5489. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5490. ("^#\\+latex:" "\n" "latex" single-line)
  5491. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5492. ("^#\\+ascii:" "\n" "ascii" single-line)
  5493. )))
  5494. (pos (point))
  5495. re1 re2 single beg end lang lfmt match-re1)
  5496. (catch 'exit
  5497. (while (setq entry (pop re-list))
  5498. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5499. single (nth 3 entry))
  5500. (save-excursion
  5501. (if (or (looking-at re1)
  5502. (re-search-backward re1 nil t))
  5503. (progn
  5504. (setq match-re1 (match-string 0))
  5505. (setq beg (match-end 0)
  5506. lang (org-edit-src-get-lang lang)
  5507. lfmt (org-edit-src-get-label-format match-re1))
  5508. (if (and (re-search-forward re2 nil t)
  5509. (>= (match-end 0) pos))
  5510. (throw 'exit (list beg (match-beginning 0)
  5511. lang single lfmt))))
  5512. (if (or (looking-at re2)
  5513. (re-search-forward re2 nil t))
  5514. (progn
  5515. (setq end (match-beginning 0))
  5516. (if (and (re-search-backward re1 nil t)
  5517. (<= (match-beginning 0) pos))
  5518. (progn
  5519. (setq lfmt (org-edit-src-get-label-format
  5520. (match-string 0)))
  5521. (throw 'exit
  5522. (list (match-end 0) end
  5523. (org-edit-src-get-lang lang)
  5524. single lfmt))))))))))))
  5525. (defun org-edit-src-get-lang (lang)
  5526. "Extract the src language."
  5527. (let ((m (match-string 0)))
  5528. (cond
  5529. ((stringp lang) lang)
  5530. ((integerp lang) (match-string lang))
  5531. ((and (eq lang 'lang)
  5532. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5533. (match-string 1 m))
  5534. ((and (eq lang 'style)
  5535. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5536. (match-string 1 m))
  5537. (t "fundamental"))))
  5538. (defun org-edit-src-get-label-format (s)
  5539. "Extract the label format."
  5540. (save-match-data
  5541. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5542. (match-string 1 s))))
  5543. (defun org-edit-src-exit ()
  5544. "Exit special edit and protect problematic lines."
  5545. (interactive)
  5546. (unless (buffer-base-buffer (current-buffer))
  5547. (error "This is not an indirect buffer, something is wrong..."))
  5548. (unless (> (point-min) 1)
  5549. (error "This buffer is not narrowed, something is wrong..."))
  5550. (goto-char (point-min))
  5551. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5552. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5553. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5554. (goto-char (point-min))
  5555. (while (re-search-forward "\n" nil t)
  5556. (replace-match " "))
  5557. (goto-char (point-min))
  5558. (if (looking-at "\\s-*") (replace-match " "))
  5559. (if (re-search-forward "\\s-+\\'" nil t)
  5560. (replace-match "")))
  5561. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5562. (goto-char (point-min))
  5563. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5564. (replace-match ",\\1"))
  5565. (when font-lock-mode
  5566. (font-lock-unfontify-region (point-min) (point-max)))
  5567. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5568. (when (org-bound-and-true-p org-edit-src-picture)
  5569. (untabify (point-min) (point-max))
  5570. (goto-char (point-min))
  5571. (while (re-search-forward "^" nil t)
  5572. (replace-match ": "))
  5573. (when font-lock-mode
  5574. (font-lock-unfontify-region (point-min) (point-max)))
  5575. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5576. (kill-buffer (current-buffer))
  5577. (and (org-mode-p) (org-restart-font-lock)))
  5578. ;;; The orgstruct minor mode
  5579. ;; Define a minor mode which can be used in other modes in order to
  5580. ;; integrate the org-mode structure editing commands.
  5581. ;; This is really a hack, because the org-mode structure commands use
  5582. ;; keys which normally belong to the major mode. Here is how it
  5583. ;; works: The minor mode defines all the keys necessary to operate the
  5584. ;; structure commands, but wraps the commands into a function which
  5585. ;; tests if the cursor is currently at a headline or a plain list
  5586. ;; item. If that is the case, the structure command is used,
  5587. ;; temporarily setting many Org-mode variables like regular
  5588. ;; expressions for filling etc. However, when any of those keys is
  5589. ;; used at a different location, function uses `key-binding' to look
  5590. ;; up if the key has an associated command in another currently active
  5591. ;; keymap (minor modes, major mode, global), and executes that
  5592. ;; command. There might be problems if any of the keys is otherwise
  5593. ;; used as a prefix key.
  5594. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5595. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5596. ;; addresses this by checking explicitly for both bindings.
  5597. (defvar orgstruct-mode-map (make-sparse-keymap)
  5598. "Keymap for the minor `orgstruct-mode'.")
  5599. (defvar org-local-vars nil
  5600. "List of local variables, for use by `orgstruct-mode'")
  5601. ;;;###autoload
  5602. (define-minor-mode orgstruct-mode
  5603. "Toggle the minor more `orgstruct-mode'.
  5604. This mode is for using Org-mode structure commands in other modes.
  5605. The following key behave as if Org-mode was active, if the cursor
  5606. is on a headline, or on a plain list item (both in the definition
  5607. of Org-mode).
  5608. M-up Move entry/item up
  5609. M-down Move entry/item down
  5610. M-left Promote
  5611. M-right Demote
  5612. M-S-up Move entry/item up
  5613. M-S-down Move entry/item down
  5614. M-S-left Promote subtree
  5615. M-S-right Demote subtree
  5616. M-q Fill paragraph and items like in Org-mode
  5617. C-c ^ Sort entries
  5618. C-c - Cycle list bullet
  5619. TAB Cycle item visibility
  5620. M-RET Insert new heading/item
  5621. S-M-RET Insert new TODO heading / Checkbox item
  5622. C-c C-c Set tags / toggle checkbox"
  5623. nil " OrgStruct" nil
  5624. (org-load-modules-maybe)
  5625. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5626. ;;;###autoload
  5627. (defun turn-on-orgstruct ()
  5628. "Unconditionally turn on `orgstruct-mode'."
  5629. (orgstruct-mode 1))
  5630. ;;;###autoload
  5631. (defun turn-on-orgstruct++ ()
  5632. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5633. In addition to setting orgstruct-mode, this also exports all indentation and
  5634. autofilling variables from org-mode into the buffer. Note that turning
  5635. off orgstruct-mode will *not* remove these additional settings."
  5636. (orgstruct-mode 1)
  5637. (let (var val)
  5638. (mapc
  5639. (lambda (x)
  5640. (when (string-match
  5641. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5642. (symbol-name (car x)))
  5643. (setq var (car x) val (nth 1 x))
  5644. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5645. org-local-vars)))
  5646. (defun orgstruct-error ()
  5647. "Error when there is no default binding for a structure key."
  5648. (interactive)
  5649. (error "This key has no function outside structure elements"))
  5650. (defun orgstruct-setup ()
  5651. "Setup orgstruct keymaps."
  5652. (let ((nfunc 0)
  5653. (bindings
  5654. (list
  5655. '([(meta up)] org-metaup)
  5656. '([(meta down)] org-metadown)
  5657. '([(meta left)] org-metaleft)
  5658. '([(meta right)] org-metaright)
  5659. '([(meta shift up)] org-shiftmetaup)
  5660. '([(meta shift down)] org-shiftmetadown)
  5661. '([(meta shift left)] org-shiftmetaleft)
  5662. '([(meta shift right)] org-shiftmetaright)
  5663. '([(shift up)] org-shiftup)
  5664. '([(shift down)] org-shiftdown)
  5665. '([(shift left)] org-shiftleft)
  5666. '([(shift right)] org-shiftright)
  5667. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5668. '("\M-q" fill-paragraph)
  5669. '("\C-c^" org-sort)
  5670. '("\C-c-" org-cycle-list-bullet)))
  5671. elt key fun cmd)
  5672. (while (setq elt (pop bindings))
  5673. (setq nfunc (1+ nfunc))
  5674. (setq key (org-key (car elt))
  5675. fun (nth 1 elt)
  5676. cmd (orgstruct-make-binding fun nfunc key))
  5677. (org-defkey orgstruct-mode-map key cmd))
  5678. ;; Special treatment needed for TAB and RET
  5679. (org-defkey orgstruct-mode-map [(tab)]
  5680. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5681. (org-defkey orgstruct-mode-map "\C-i"
  5682. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5683. (org-defkey orgstruct-mode-map "\M-\C-m"
  5684. (orgstruct-make-binding 'org-insert-heading 105
  5685. "\M-\C-m" [(meta return)]))
  5686. (org-defkey orgstruct-mode-map [(meta return)]
  5687. (orgstruct-make-binding 'org-insert-heading 106
  5688. [(meta return)] "\M-\C-m"))
  5689. (org-defkey orgstruct-mode-map [(shift meta return)]
  5690. (orgstruct-make-binding 'org-insert-todo-heading 107
  5691. [(meta return)] "\M-\C-m"))
  5692. (unless org-local-vars
  5693. (setq org-local-vars (org-get-local-variables)))
  5694. t))
  5695. (defun orgstruct-make-binding (fun n &rest keys)
  5696. "Create a function for binding in the structure minor mode.
  5697. FUN is the command to call inside a table. N is used to create a unique
  5698. command name. KEYS are keys that should be checked in for a command
  5699. to execute outside of tables."
  5700. (eval
  5701. (list 'defun
  5702. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5703. '(arg)
  5704. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5705. "Outside of structure, run the binding of `"
  5706. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5707. "'.")
  5708. '(interactive "p")
  5709. (list 'if
  5710. '(org-context-p 'headline 'item)
  5711. (list 'org-run-like-in-org-mode (list 'quote fun))
  5712. (list 'let '(orgstruct-mode)
  5713. (list 'call-interactively
  5714. (append '(or)
  5715. (mapcar (lambda (k)
  5716. (list 'key-binding k))
  5717. keys)
  5718. '('orgstruct-error))))))))
  5719. (defun org-context-p (&rest contexts)
  5720. "Check if local context is any of CONTEXTS.
  5721. Possible values in the list of contexts are `table', `headline', and `item'."
  5722. (let ((pos (point)))
  5723. (goto-char (point-at-bol))
  5724. (prog1 (or (and (memq 'table contexts)
  5725. (looking-at "[ \t]*|"))
  5726. (and (memq 'headline contexts)
  5727. ;;????????? (looking-at "\\*+"))
  5728. (looking-at outline-regexp))
  5729. (and (memq 'item contexts)
  5730. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5731. (goto-char pos))))
  5732. (defun org-get-local-variables ()
  5733. "Return a list of all local variables in an org-mode buffer."
  5734. (let (varlist)
  5735. (with-current-buffer (get-buffer-create "*Org tmp*")
  5736. (erase-buffer)
  5737. (org-mode)
  5738. (setq varlist (buffer-local-variables)))
  5739. (kill-buffer "*Org tmp*")
  5740. (delq nil
  5741. (mapcar
  5742. (lambda (x)
  5743. (setq x
  5744. (if (symbolp x)
  5745. (list x)
  5746. (list (car x) (list 'quote (cdr x)))))
  5747. (if (string-match
  5748. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5749. (symbol-name (car x)))
  5750. x nil))
  5751. varlist))))
  5752. ;;;###autoload
  5753. (defun org-run-like-in-org-mode (cmd)
  5754. (org-load-modules-maybe)
  5755. (unless org-local-vars
  5756. (setq org-local-vars (org-get-local-variables)))
  5757. (eval (list 'let org-local-vars
  5758. (list 'call-interactively (list 'quote cmd)))))
  5759. ;;;; Archiving
  5760. (defun org-get-category (&optional pos)
  5761. "Get the category applying to position POS."
  5762. (get-text-property (or pos (point)) 'org-category))
  5763. (defun org-refresh-category-properties ()
  5764. "Refresh category text properties in the buffer."
  5765. (let ((def-cat (cond
  5766. ((null org-category)
  5767. (if buffer-file-name
  5768. (file-name-sans-extension
  5769. (file-name-nondirectory buffer-file-name))
  5770. "???"))
  5771. ((symbolp org-category) (symbol-name org-category))
  5772. (t org-category)))
  5773. beg end cat pos optionp)
  5774. (org-unmodified
  5775. (save-excursion
  5776. (save-restriction
  5777. (widen)
  5778. (goto-char (point-min))
  5779. (put-text-property (point) (point-max) 'org-category def-cat)
  5780. (while (re-search-forward
  5781. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5782. (setq pos (match-end 0)
  5783. optionp (equal (char-after (match-beginning 0)) ?#)
  5784. cat (org-trim (match-string 2)))
  5785. (if optionp
  5786. (setq beg (point-at-bol) end (point-max))
  5787. (org-back-to-heading t)
  5788. (setq beg (point) end (org-end-of-subtree t t)))
  5789. (put-text-property beg end 'org-category cat)
  5790. (goto-char pos)))))))
  5791. ;;;; Link Stuff
  5792. ;;; Link abbreviations
  5793. (defun org-link-expand-abbrev (link)
  5794. "Apply replacements as defined in `org-link-abbrev-alist."
  5795. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5796. (let* ((key (match-string 1 link))
  5797. (as (or (assoc key org-link-abbrev-alist-local)
  5798. (assoc key org-link-abbrev-alist)))
  5799. (tag (and (match-end 2) (match-string 3 link)))
  5800. rpl)
  5801. (if (not as)
  5802. link
  5803. (setq rpl (cdr as))
  5804. (cond
  5805. ((symbolp rpl) (funcall rpl tag))
  5806. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5807. ((string-match "%h" rpl)
  5808. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5809. (t (concat rpl tag)))))
  5810. link))
  5811. ;;; Storing and inserting links
  5812. (defvar org-insert-link-history nil
  5813. "Minibuffer history for links inserted with `org-insert-link'.")
  5814. (defvar org-stored-links nil
  5815. "Contains the links stored with `org-store-link'.")
  5816. (defvar org-store-link-plist nil
  5817. "Plist with info about the most recently link created with `org-store-link'.")
  5818. (defvar org-link-protocols nil
  5819. "Link protocols added to Org-mode using `org-add-link-type'.")
  5820. (defvar org-store-link-functions nil
  5821. "List of functions that are called to create and store a link.
  5822. Each function will be called in turn until one returns a non-nil
  5823. value. Each function should check if it is responsible for creating
  5824. this link (for example by looking at the major mode).
  5825. If not, it must exit and return nil.
  5826. If yes, it should return a non-nil value after a calling
  5827. `org-store-link-props' with a list of properties and values.
  5828. Special properties are:
  5829. :type The link prefix. like \"http\". This must be given.
  5830. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5831. This is obligatory as well.
  5832. :description Optional default description for the second pair
  5833. of brackets in an Org-mode link. The user can still change
  5834. this when inserting this link into an Org-mode buffer.
  5835. In addition to these, any additional properties can be specified
  5836. and then used in remember templates.")
  5837. (defun org-add-link-type (type &optional follow export)
  5838. "Add TYPE to the list of `org-link-types'.
  5839. Re-compute all regular expressions depending on `org-link-types'
  5840. FOLLOW and EXPORT are two functions.
  5841. FOLLOW should take the link path as the single argument and do whatever
  5842. is necessary to follow the link, for example find a file or display
  5843. a mail message.
  5844. EXPORT should format the link path for export to one of the export formats.
  5845. It should be a function accepting three arguments:
  5846. path the path of the link, the text after the prefix (like \"http:\")
  5847. desc the description of the link, if any, nil if there was no description
  5848. format the export format, a symbol like `html' or `latex'.
  5849. The function may use the FORMAT information to return different values
  5850. depending on the format. The return value will be put literally into
  5851. the exported file.
  5852. Org-mode has a built-in default for exporting links. If you are happy with
  5853. this default, there is no need to define an export function for the link
  5854. type. For a simple example of an export function, see `org-bbdb.el'."
  5855. (add-to-list 'org-link-types type t)
  5856. (org-make-link-regexps)
  5857. (if (assoc type org-link-protocols)
  5858. (setcdr (assoc type org-link-protocols) (list follow export))
  5859. (push (list type follow export) org-link-protocols)))
  5860. ;;;###autoload
  5861. (defun org-store-link (arg)
  5862. "\\<org-mode-map>Store an org-link to the current location.
  5863. This link is added to `org-stored-links' and can later be inserted
  5864. into an org-buffer with \\[org-insert-link].
  5865. For some link types, a prefix arg is interpreted:
  5866. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5867. For file links, arg negates `org-context-in-file-links'."
  5868. (interactive "P")
  5869. (org-load-modules-maybe)
  5870. (setq org-store-link-plist nil) ; reset
  5871. (let (link cpltxt desc description search txt)
  5872. (cond
  5873. ((run-hook-with-args-until-success 'org-store-link-functions)
  5874. (setq link (plist-get org-store-link-plist :link)
  5875. desc (or (plist-get org-store-link-plist :description) link)))
  5876. ((equal (buffer-name) "*Org Edit Src Example*")
  5877. (let (label gc)
  5878. (while (or (not label)
  5879. (save-excursion
  5880. (save-restriction
  5881. (widen)
  5882. (goto-char (point-min))
  5883. (re-search-forward
  5884. (regexp-quote (format org-coderef-label-format label))
  5885. nil t))))
  5886. (when label (message "Label exists already") (sit-for 2))
  5887. (setq label (read-string "Code line label: " label)))
  5888. (end-of-line 1)
  5889. (setq link (format org-coderef-label-format label))
  5890. (setq gc (- 79 (length link)))
  5891. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5892. (insert link)
  5893. (setq link (concat "(" label ")") desc nil)))
  5894. ((eq major-mode 'calendar-mode)
  5895. (let ((cd (calendar-cursor-to-date)))
  5896. (setq link
  5897. (format-time-string
  5898. (car org-time-stamp-formats)
  5899. (apply 'encode-time
  5900. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5901. nil nil nil))))
  5902. (org-store-link-props :type "calendar" :date cd)))
  5903. ((eq major-mode 'w3-mode)
  5904. (setq cpltxt (url-view-url t)
  5905. link (org-make-link cpltxt))
  5906. (org-store-link-props :type "w3" :url (url-view-url t)))
  5907. ((eq major-mode 'w3m-mode)
  5908. (setq cpltxt (or w3m-current-title w3m-current-url)
  5909. link (org-make-link w3m-current-url))
  5910. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5911. ((setq search (run-hook-with-args-until-success
  5912. 'org-create-file-search-functions))
  5913. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5914. "::" search))
  5915. (setq cpltxt (or description link)))
  5916. ((eq major-mode 'image-mode)
  5917. (setq cpltxt (concat "file:"
  5918. (abbreviate-file-name buffer-file-name))
  5919. link (org-make-link cpltxt))
  5920. (org-store-link-props :type "image" :file buffer-file-name))
  5921. ((eq major-mode 'dired-mode)
  5922. ;; link to the file in the current line
  5923. (setq cpltxt (concat "file:"
  5924. (abbreviate-file-name
  5925. (expand-file-name
  5926. (dired-get-filename nil t))))
  5927. link (org-make-link cpltxt)))
  5928. ((and buffer-file-name (org-mode-p))
  5929. (cond
  5930. ((org-in-regexp "<<\\(.*?\\)>>")
  5931. (setq cpltxt
  5932. (concat "file:"
  5933. (abbreviate-file-name buffer-file-name)
  5934. "::" (match-string 1))
  5935. link (org-make-link cpltxt)))
  5936. ((and (featurep 'org-id)
  5937. (or (eq org-link-to-org-use-id t)
  5938. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5939. (interactive-p))
  5940. (and org-link-to-org-use-id
  5941. (condition-case nil
  5942. (org-entry-get nil "ID")
  5943. (error nil)))))
  5944. ;; We can make a link using the ID.
  5945. (setq link (condition-case nil
  5946. (prog1 (org-id-store-link)
  5947. (setq desc (plist-get org-store-link-plist
  5948. :description)))
  5949. (error
  5950. ;; probably before first headline, link to file only
  5951. (concat "file:"
  5952. (abbreviate-file-name buffer-file-name))))))
  5953. (t
  5954. ;; Just link to current headline
  5955. (setq cpltxt (concat "file:"
  5956. (abbreviate-file-name buffer-file-name)))
  5957. ;; Add a context search string
  5958. (when (org-xor org-context-in-file-links arg)
  5959. (setq txt (cond
  5960. ((org-on-heading-p) nil)
  5961. ((org-region-active-p)
  5962. (buffer-substring (region-beginning) (region-end)))
  5963. (t nil)))
  5964. (when (or (null txt) (string-match "\\S-" txt))
  5965. (setq cpltxt
  5966. (concat cpltxt "::"
  5967. (condition-case nil
  5968. (org-make-org-heading-search-string txt)
  5969. (error "")))
  5970. desc "NONE")))
  5971. (if (string-match "::\\'" cpltxt)
  5972. (setq cpltxt (substring cpltxt 0 -2)))
  5973. (setq link (org-make-link cpltxt)))))
  5974. ((buffer-file-name (buffer-base-buffer))
  5975. ;; Just link to this file here.
  5976. (setq cpltxt (concat "file:"
  5977. (abbreviate-file-name
  5978. (buffer-file-name (buffer-base-buffer)))))
  5979. ;; Add a context string
  5980. (when (org-xor org-context-in-file-links arg)
  5981. (setq txt (if (org-region-active-p)
  5982. (buffer-substring (region-beginning) (region-end))
  5983. (buffer-substring (point-at-bol) (point-at-eol))))
  5984. ;; Only use search option if there is some text.
  5985. (when (string-match "\\S-" txt)
  5986. (setq cpltxt
  5987. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5988. desc "NONE")))
  5989. (setq link (org-make-link cpltxt)))
  5990. ((interactive-p)
  5991. (error "Cannot link to a buffer which is not visiting a file"))
  5992. (t (setq link nil)))
  5993. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5994. (setq link (or link cpltxt)
  5995. desc (or desc cpltxt))
  5996. (if (equal desc "NONE") (setq desc nil))
  5997. (if (and (interactive-p) link)
  5998. (progn
  5999. (setq org-stored-links
  6000. (cons (list link desc) org-stored-links))
  6001. (message "Stored: %s" (or desc link)))
  6002. (and link (org-make-link-string link desc)))))
  6003. (defun org-store-link-props (&rest plist)
  6004. "Store link properties, extract names and addresses."
  6005. (let (x adr)
  6006. (when (setq x (plist-get plist :from))
  6007. (setq adr (mail-extract-address-components x))
  6008. (setq plist (plist-put plist :fromname (car adr)))
  6009. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6010. (when (setq x (plist-get plist :to))
  6011. (setq adr (mail-extract-address-components x))
  6012. (setq plist (plist-put plist :toname (car adr)))
  6013. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6014. (let ((from (plist-get plist :from))
  6015. (to (plist-get plist :to)))
  6016. (when (and from to org-from-is-user-regexp)
  6017. (setq plist
  6018. (plist-put plist :fromto
  6019. (if (string-match org-from-is-user-regexp from)
  6020. (concat "to %t")
  6021. (concat "from %f"))))))
  6022. (setq org-store-link-plist plist))
  6023. (defun org-add-link-props (&rest plist)
  6024. "Add these properties to the link property list."
  6025. (let (key value)
  6026. (while plist
  6027. (setq key (pop plist) value (pop plist))
  6028. (setq org-store-link-plist
  6029. (plist-put org-store-link-plist key value)))))
  6030. (defun org-email-link-description (&optional fmt)
  6031. "Return the description part of an email link.
  6032. This takes information from `org-store-link-plist' and formats it
  6033. according to FMT (default from `org-email-link-description-format')."
  6034. (setq fmt (or fmt org-email-link-description-format))
  6035. (let* ((p org-store-link-plist)
  6036. (to (plist-get p :toaddress))
  6037. (from (plist-get p :fromaddress))
  6038. (table
  6039. (list
  6040. (cons "%c" (plist-get p :fromto))
  6041. (cons "%F" (plist-get p :from))
  6042. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6043. (cons "%T" (plist-get p :to))
  6044. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6045. (cons "%s" (plist-get p :subject))
  6046. (cons "%m" (plist-get p :message-id)))))
  6047. (when (string-match "%c" fmt)
  6048. ;; Check if the user wrote this message
  6049. (if (and org-from-is-user-regexp from to
  6050. (save-match-data (string-match org-from-is-user-regexp from)))
  6051. (setq fmt (replace-match "to %t" t t fmt))
  6052. (setq fmt (replace-match "from %f" t t fmt))))
  6053. (org-replace-escapes fmt table)))
  6054. (defun org-make-org-heading-search-string (&optional string heading)
  6055. "Make search string for STRING or current headline."
  6056. (interactive)
  6057. (let ((s (or string (org-get-heading))))
  6058. (unless (and string (not heading))
  6059. ;; We are using a headline, clean up garbage in there.
  6060. (if (string-match org-todo-regexp s)
  6061. (setq s (replace-match "" t t s)))
  6062. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6063. (setq s (replace-match "" t t s)))
  6064. (setq s (org-trim s))
  6065. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6066. org-comment-string "\\)") s)
  6067. (setq s (replace-match "" t t s)))
  6068. (while (string-match org-ts-regexp s)
  6069. (setq s (replace-match "" t t s))))
  6070. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6071. (setq s (replace-match " " t t s)))
  6072. (or string (setq s (concat "*" s))) ; Add * for headlines
  6073. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6074. (defun org-make-link (&rest strings)
  6075. "Concatenate STRINGS."
  6076. (apply 'concat strings))
  6077. (defun org-make-link-string (link &optional description)
  6078. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6079. (unless (string-match "\\S-" link)
  6080. (error "Empty link"))
  6081. (when (stringp description)
  6082. ;; Remove brackets from the description, they are fatal.
  6083. (while (string-match "\\[" description)
  6084. (setq description (replace-match "{" t t description)))
  6085. (while (string-match "\\]" description)
  6086. (setq description (replace-match "}" t t description))))
  6087. (when (equal (org-link-escape link) description)
  6088. ;; No description needed, it is identical
  6089. (setq description nil))
  6090. (when (and (not description)
  6091. (not (equal link (org-link-escape link))))
  6092. (setq description (org-extract-attributes link)))
  6093. (concat "[[" (org-link-escape link) "]"
  6094. (if description (concat "[" description "]") "")
  6095. "]"))
  6096. (defconst org-link-escape-chars
  6097. '((?\ . "%20")
  6098. (?\[ . "%5B")
  6099. (?\] . "%5D")
  6100. (?\340 . "%E0") ; `a
  6101. (?\342 . "%E2") ; ^a
  6102. (?\347 . "%E7") ; ,c
  6103. (?\350 . "%E8") ; `e
  6104. (?\351 . "%E9") ; 'e
  6105. (?\352 . "%EA") ; ^e
  6106. (?\356 . "%EE") ; ^i
  6107. (?\364 . "%F4") ; ^o
  6108. (?\371 . "%F9") ; `u
  6109. (?\373 . "%FB") ; ^u
  6110. (?\; . "%3B")
  6111. (?? . "%3F")
  6112. (?= . "%3D")
  6113. (?+ . "%2B")
  6114. )
  6115. "Association list of escapes for some characters problematic in links.
  6116. This is the list that is used for internal purposes.")
  6117. (defconst org-link-escape-chars-browser
  6118. '((?\ . "%20")) ; 32 for the SPC char
  6119. "Association list of escapes for some characters problematic in links.
  6120. This is the list that is used before handing over to the browser.")
  6121. (defun org-link-escape (text &optional table)
  6122. "Escape characters in TEXT that are problematic for links."
  6123. (setq table (or table org-link-escape-chars))
  6124. (when text
  6125. (let ((re (mapconcat (lambda (x) (regexp-quote
  6126. (char-to-string (car x))))
  6127. table "\\|")))
  6128. (while (string-match re text)
  6129. (setq text
  6130. (replace-match
  6131. (cdr (assoc (string-to-char (match-string 0 text))
  6132. table))
  6133. t t text)))
  6134. text)))
  6135. (defun org-link-unescape (text &optional table)
  6136. "Reverse the action of `org-link-escape'."
  6137. (setq table (or table org-link-escape-chars))
  6138. (when text
  6139. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6140. table "\\|")))
  6141. (while (string-match re text)
  6142. (setq text
  6143. (replace-match
  6144. (char-to-string (car (rassoc (match-string 0 text) table)))
  6145. t t text)))
  6146. text)))
  6147. (defun org-xor (a b)
  6148. "Exclusive or."
  6149. (if a (not b) b))
  6150. (defun org-fixup-message-id-for-http (s)
  6151. "Replace special characters in a message id, so it can be used in an http query."
  6152. (while (string-match "<" s)
  6153. (setq s (replace-match "%3C" t t s)))
  6154. (while (string-match ">" s)
  6155. (setq s (replace-match "%3E" t t s)))
  6156. (while (string-match "@" s)
  6157. (setq s (replace-match "%40" t t s)))
  6158. s)
  6159. ;;;###autoload
  6160. (defun org-insert-link-global ()
  6161. "Insert a link like Org-mode does.
  6162. This command can be called in any mode to insert a link in Org-mode syntax."
  6163. (interactive)
  6164. (org-load-modules-maybe)
  6165. (org-run-like-in-org-mode 'org-insert-link))
  6166. (defun org-insert-link (&optional complete-file link-location)
  6167. "Insert a link. At the prompt, enter the link.
  6168. Completion can be used to insert any of the link protocol prefixes like
  6169. http or ftp in use.
  6170. The history can be used to select a link previously stored with
  6171. `org-store-link'. When the empty string is entered (i.e. if you just
  6172. press RET at the prompt), the link defaults to the most recently
  6173. stored link. As SPC triggers completion in the minibuffer, you need to
  6174. use M-SPC or C-q SPC to force the insertion of a space character.
  6175. You will also be prompted for a description, and if one is given, it will
  6176. be displayed in the buffer instead of the link.
  6177. If there is already a link at point, this command will allow you to edit link
  6178. and description parts.
  6179. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6180. be selected using completion. The path to the file will be relative to the
  6181. current directory if the file is in the current directory or a subdirectory.
  6182. Otherwise, the link will be the absolute path as completed in the minibuffer
  6183. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6184. option `org-link-file-path-type'.
  6185. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6186. the current directory or below.
  6187. With three \\[universal-argument] prefixes, negate the meaning of
  6188. `org-keep-stored-link-after-insertion'.
  6189. If `org-make-link-description-function' is non-nil, this function will be
  6190. called with the link target, and the result will be the default
  6191. link description.
  6192. If the LINK-LOCATION parameter is non-nil, this value will be
  6193. used as the link location instead of reading one interactively."
  6194. (interactive "P")
  6195. (let* ((wcf (current-window-configuration))
  6196. (region (if (org-region-active-p)
  6197. (buffer-substring (region-beginning) (region-end))))
  6198. (remove (and region (list (region-beginning) (region-end))))
  6199. (desc region)
  6200. tmphist ; byte-compile incorrectly complains about this
  6201. (link link-location)
  6202. entry file)
  6203. (cond
  6204. (link-location) ; specified by arg, just use it.
  6205. ((org-in-regexp org-bracket-link-regexp 1)
  6206. ;; We do have a link at point, and we are going to edit it.
  6207. (setq remove (list (match-beginning 0) (match-end 0)))
  6208. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6209. (setq link (read-string "Link: "
  6210. (org-link-unescape
  6211. (org-match-string-no-properties 1)))))
  6212. ((or (org-in-regexp org-angle-link-re)
  6213. (org-in-regexp org-plain-link-re))
  6214. ;; Convert to bracket link
  6215. (setq remove (list (match-beginning 0) (match-end 0))
  6216. link (read-string "Link: "
  6217. (org-remove-angle-brackets (match-string 0)))))
  6218. ((member complete-file '((4) (16)))
  6219. ;; Completing read for file names.
  6220. (setq file (read-file-name "File: "))
  6221. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6222. (pwd1 (file-name-as-directory (abbreviate-file-name
  6223. (expand-file-name ".")))))
  6224. (cond
  6225. ((equal complete-file '(16))
  6226. (setq link (org-make-link
  6227. "file:"
  6228. (abbreviate-file-name (expand-file-name file)))))
  6229. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6230. (setq link (org-make-link "file:" (match-string 1 file))))
  6231. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6232. (expand-file-name file))
  6233. (setq link (org-make-link
  6234. "file:" (match-string 1 (expand-file-name file)))))
  6235. (t (setq link (org-make-link "file:" file))))))
  6236. (t
  6237. ;; Read link, with completion for stored links.
  6238. (with-output-to-temp-buffer "*Org Links*"
  6239. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6240. (when org-stored-links
  6241. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6242. (princ (mapconcat
  6243. (lambda (x)
  6244. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6245. (reverse org-stored-links) "\n"))))
  6246. (let ((cw (selected-window)))
  6247. (select-window (get-buffer-window "*Org Links*"))
  6248. (setq truncate-lines t)
  6249. (org-fit-window-to-buffer)
  6250. (select-window cw))
  6251. ;; Fake a link history, containing the stored links.
  6252. (setq tmphist (append (mapcar 'car org-stored-links)
  6253. org-insert-link-history))
  6254. (unwind-protect
  6255. (setq link
  6256. (let ((org-completion-use-ido nil))
  6257. (org-completing-read
  6258. "Link: "
  6259. (append
  6260. (mapcar (lambda (x) (list (concat (car x) ":")))
  6261. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6262. (mapcar (lambda (x) (list (concat x ":")))
  6263. org-link-types))
  6264. nil nil nil
  6265. 'tmphist
  6266. (or (car (car org-stored-links))))))
  6267. (set-window-configuration wcf)
  6268. (kill-buffer "*Org Links*"))
  6269. (setq entry (assoc link org-stored-links))
  6270. (or entry (push link org-insert-link-history))
  6271. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6272. (not org-keep-stored-link-after-insertion))
  6273. (setq org-stored-links (delq (assoc link org-stored-links)
  6274. org-stored-links)))
  6275. (setq desc (or desc (nth 1 entry)))))
  6276. (if (string-match org-plain-link-re link)
  6277. ;; URL-like link, normalize the use of angular brackets.
  6278. (setq link (org-make-link (org-remove-angle-brackets link))))
  6279. ;; Check if we are linking to the current file with a search option
  6280. ;; If yes, simplify the link by using only the search option.
  6281. (when (and buffer-file-name
  6282. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6283. (let* ((path (match-string 1 link))
  6284. (case-fold-search nil)
  6285. (search (match-string 2 link)))
  6286. (save-match-data
  6287. (if (equal (file-truename buffer-file-name) (file-truename path))
  6288. ;; We are linking to this same file, with a search option
  6289. (setq link search)))))
  6290. ;; Check if we can/should use a relative path. If yes, simplify the link
  6291. (when (string-match "^file:\\(.*\\)" link)
  6292. (let* ((path (match-string 1 link))
  6293. (origpath path)
  6294. (case-fold-search nil))
  6295. (cond
  6296. ((or (eq org-link-file-path-type 'absolute)
  6297. (equal complete-file '(16)))
  6298. (setq path (abbreviate-file-name (expand-file-name path))))
  6299. ((eq org-link-file-path-type 'noabbrev)
  6300. (setq path (expand-file-name path)))
  6301. ((eq org-link-file-path-type 'relative)
  6302. (setq path (file-relative-name path)))
  6303. (t
  6304. (save-match-data
  6305. (if (string-match (concat "^" (regexp-quote
  6306. (file-name-as-directory
  6307. (expand-file-name "."))))
  6308. (expand-file-name path))
  6309. ;; We are linking a file with relative path name.
  6310. (setq path (substring (expand-file-name path)
  6311. (match-end 0)))
  6312. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6313. (setq link (concat "file:" path))
  6314. (if (equal desc origpath)
  6315. (setq desc path))))
  6316. (if org-make-link-description-function
  6317. (setq desc (funcall org-make-link-description-function link desc)))
  6318. (setq desc (read-string "Description: " desc))
  6319. (unless (string-match "\\S-" desc) (setq desc nil))
  6320. (if remove (apply 'delete-region remove))
  6321. (insert (org-make-link-string link desc))))
  6322. (defun org-completing-read (&rest args)
  6323. "Completing-read with SPACE being a normal character."
  6324. (let ((minibuffer-local-completion-map
  6325. (copy-keymap minibuffer-local-completion-map)))
  6326. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6327. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6328. (apply 'org-ido-completing-read args)))
  6329. (defun org-ido-completing-read (&rest args)
  6330. "Completing-read using `ido-mode' speedups if available"
  6331. (if (and org-completion-use-ido
  6332. (fboundp 'ido-completing-read)
  6333. (boundp 'ido-mode) ido-mode
  6334. (listp (second args)))
  6335. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6336. (apply 'completing-read args)))
  6337. (defun org-extract-attributes (s)
  6338. "Extract the attributes cookie from a string and set as text property."
  6339. (let (a attr (start 0) key value)
  6340. (save-match-data
  6341. (when (string-match "{{\\([^}]+\\)}}$" s)
  6342. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6343. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6344. (setq key (match-string 1 a) value (match-string 2 a)
  6345. start (match-end 0)
  6346. attr (plist-put attr (intern key) value))))
  6347. (org-add-props s nil 'org-attr attr))
  6348. s))
  6349. (defun org-attributes-to-string (plist)
  6350. "Format a property list into an HTML attribute list."
  6351. (let ((s "") key value)
  6352. (while plist
  6353. (setq key (pop plist) value (pop plist))
  6354. (and value
  6355. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6356. s))
  6357. ;;; Opening/following a link
  6358. (defvar org-link-search-failed nil)
  6359. (defun org-next-link ()
  6360. "Move forward to the next link.
  6361. If the link is in hidden text, expose it."
  6362. (interactive)
  6363. (when (and org-link-search-failed (eq this-command last-command))
  6364. (goto-char (point-min))
  6365. (message "Link search wrapped back to beginning of buffer"))
  6366. (setq org-link-search-failed nil)
  6367. (let* ((pos (point))
  6368. (ct (org-context))
  6369. (a (assoc :link ct)))
  6370. (if a (goto-char (nth 2 a)))
  6371. (if (re-search-forward org-any-link-re nil t)
  6372. (progn
  6373. (goto-char (match-beginning 0))
  6374. (if (org-invisible-p) (org-show-context)))
  6375. (goto-char pos)
  6376. (setq org-link-search-failed t)
  6377. (error "No further link found"))))
  6378. (defun org-previous-link ()
  6379. "Move backward to the previous link.
  6380. If the link is in hidden text, expose it."
  6381. (interactive)
  6382. (when (and org-link-search-failed (eq this-command last-command))
  6383. (goto-char (point-max))
  6384. (message "Link search wrapped back to end of buffer"))
  6385. (setq org-link-search-failed nil)
  6386. (let* ((pos (point))
  6387. (ct (org-context))
  6388. (a (assoc :link ct)))
  6389. (if a (goto-char (nth 1 a)))
  6390. (if (re-search-backward org-any-link-re nil t)
  6391. (progn
  6392. (goto-char (match-beginning 0))
  6393. (if (org-invisible-p) (org-show-context)))
  6394. (goto-char pos)
  6395. (setq org-link-search-failed t)
  6396. (error "No further link found"))))
  6397. (defun org-translate-link (s)
  6398. "Translate a link string if a translation function has been defined."
  6399. (if (and org-link-translation-function
  6400. (fboundp org-link-translation-function)
  6401. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6402. (progn
  6403. (setq s (funcall org-link-translation-function
  6404. (match-string 1) (match-string 2)))
  6405. (concat (car s) ":" (cdr s)))
  6406. s))
  6407. (defun org-translate-link-from-planner (type path)
  6408. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6409. This is still an experimental function, your mileage may vary."
  6410. (cond
  6411. ((member type '("http" "https" "news" "ftp"))
  6412. ;; standard Internet links are the same.
  6413. nil)
  6414. ((and (equal type "irc") (string-match "^//" path))
  6415. ;; Planner has two / at the beginning of an irc link, we have 1.
  6416. ;; We should have zero, actually....
  6417. (setq path (substring path 1)))
  6418. ((and (equal type "lisp") (string-match "^/" path))
  6419. ;; Planner has a slash, we do not.
  6420. (setq type "elisp" path (substring path 1)))
  6421. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6422. ;; A typical message link. Planner has the id after the fina slash,
  6423. ;; we separate it with a hash mark
  6424. (setq path (concat (match-string 1 path) "#"
  6425. (org-remove-angle-brackets (match-string 2 path)))))
  6426. )
  6427. (cons type path))
  6428. (defun org-find-file-at-mouse (ev)
  6429. "Open file link or URL at mouse."
  6430. (interactive "e")
  6431. (mouse-set-point ev)
  6432. (org-open-at-point 'in-emacs))
  6433. (defun org-open-at-mouse (ev)
  6434. "Open file link or URL at mouse."
  6435. (interactive "e")
  6436. (mouse-set-point ev)
  6437. (if (eq major-mode 'org-agenda-mode)
  6438. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6439. (org-open-at-point))
  6440. (defvar org-window-config-before-follow-link nil
  6441. "The window configuration before following a link.
  6442. This is saved in case the need arises to restore it.")
  6443. (defvar org-open-link-marker (make-marker)
  6444. "Marker pointing to the location where `org-open-at-point; was called.")
  6445. ;;;###autoload
  6446. (defun org-open-at-point-global ()
  6447. "Follow a link like Org-mode does.
  6448. This command can be called in any mode to follow a link that has
  6449. Org-mode syntax."
  6450. (interactive)
  6451. (org-run-like-in-org-mode 'org-open-at-point))
  6452. ;;;###autoload
  6453. (defun org-open-link-from-string (s &optional arg)
  6454. "Open a link in the string S, as if it was in Org-mode."
  6455. (interactive "sLink: \nP")
  6456. (with-temp-buffer
  6457. (let ((org-inhibit-startup t))
  6458. (org-mode)
  6459. (insert s)
  6460. (goto-char (point-min))
  6461. (org-open-at-point arg))))
  6462. (defun org-open-at-point (&optional in-emacs)
  6463. "Open link at or after point.
  6464. If there is no link at point, this function will search forward up to
  6465. the end of the current subtree.
  6466. Normally, files will be opened by an appropriate application. If the
  6467. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6468. With a double prefix argument, try to open outside of Emacs, in the
  6469. application the system uses for this file type."
  6470. (interactive "P")
  6471. (org-load-modules-maybe)
  6472. (move-marker org-open-link-marker (point))
  6473. (setq org-window-config-before-follow-link (current-window-configuration))
  6474. (org-remove-occur-highlights nil nil t)
  6475. (cond
  6476. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6477. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6478. (org-footnote-action))
  6479. (t
  6480. (let (type path link line search (pos (point)))
  6481. (catch 'match
  6482. (save-excursion
  6483. (skip-chars-forward "^]\n\r")
  6484. (when (org-in-regexp org-bracket-link-regexp)
  6485. (setq link (org-extract-attributes
  6486. (org-link-unescape (org-match-string-no-properties 1))))
  6487. (while (string-match " *\n *" link)
  6488. (setq link (replace-match " " t t link)))
  6489. (setq link (org-link-expand-abbrev link))
  6490. (cond
  6491. ((or (file-name-absolute-p link)
  6492. (string-match "^\\.\\.?/" link))
  6493. (setq type "file" path link))
  6494. ((string-match org-link-re-with-space3 link)
  6495. (setq type (match-string 1 link) path (match-string 2 link)))
  6496. (t (setq type "thisfile" path link)))
  6497. (throw 'match t)))
  6498. (when (get-text-property (point) 'org-linked-text)
  6499. (setq type "thisfile"
  6500. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6501. (1+ (point)) (point))
  6502. path (buffer-substring
  6503. (previous-single-property-change pos 'org-linked-text)
  6504. (next-single-property-change pos 'org-linked-text)))
  6505. (throw 'match t))
  6506. (save-excursion
  6507. (when (or (org-in-regexp org-angle-link-re)
  6508. (org-in-regexp org-plain-link-re))
  6509. (setq type (match-string 1) path (match-string 2))
  6510. (throw 'match t)))
  6511. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6512. (setq type "tree-match"
  6513. path (match-string 1))
  6514. (throw 'match t))
  6515. (save-excursion
  6516. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6517. (setq type "tags"
  6518. path (match-string 1))
  6519. (while (string-match ":" path)
  6520. (setq path (replace-match "+" t t path)))
  6521. (throw 'match t))))
  6522. (unless path
  6523. (error "No link found"))
  6524. ;; Remove any trailing spaces in path
  6525. (if (string-match " +\\'" path)
  6526. (setq path (replace-match "" t t path)))
  6527. (if (and org-link-translation-function
  6528. (fboundp org-link-translation-function))
  6529. ;; Check if we need to translate the link
  6530. (let ((tmp (funcall org-link-translation-function type path)))
  6531. (setq type (car tmp) path (cdr tmp))))
  6532. (cond
  6533. ((assoc type org-link-protocols)
  6534. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6535. ((equal type "mailto")
  6536. (let ((cmd (car org-link-mailto-program))
  6537. (args (cdr org-link-mailto-program)) args1
  6538. (address path) (subject "") a)
  6539. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6540. (setq address (match-string 1 path)
  6541. subject (org-link-escape (match-string 2 path))))
  6542. (while args
  6543. (cond
  6544. ((not (stringp (car args))) (push (pop args) args1))
  6545. (t (setq a (pop args))
  6546. (if (string-match "%a" a)
  6547. (setq a (replace-match address t t a)))
  6548. (if (string-match "%s" a)
  6549. (setq a (replace-match subject t t a)))
  6550. (push a args1))))
  6551. (apply cmd (nreverse args1))))
  6552. ((member type '("http" "https" "ftp" "news"))
  6553. (browse-url (concat type ":" (org-link-escape
  6554. path org-link-escape-chars-browser))))
  6555. ((member type '("message"))
  6556. (browse-url (concat type ":" path)))
  6557. ((string= type "tags")
  6558. (org-tags-view in-emacs path))
  6559. ((string= type "thisfile")
  6560. (if in-emacs
  6561. (switch-to-buffer-other-window
  6562. (org-get-buffer-for-internal-link (current-buffer)))
  6563. (org-mark-ring-push))
  6564. (let ((cmd `(org-link-search
  6565. ,path
  6566. ,(cond ((equal in-emacs '(4)) 'occur)
  6567. ((equal in-emacs '(16)) 'org-occur)
  6568. (t nil))
  6569. ,pos)))
  6570. (condition-case nil (eval cmd)
  6571. (error (progn (widen) (eval cmd))))))
  6572. ((string= type "tree-match")
  6573. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6574. ((string= type "file")
  6575. (if (string-match "::\\([0-9]+\\)\\'" path)
  6576. (setq line (string-to-number (match-string 1 path))
  6577. path (substring path 0 (match-beginning 0)))
  6578. (if (string-match "::\\(.+\\)\\'" path)
  6579. (setq search (match-string 1 path)
  6580. path (substring path 0 (match-beginning 0)))))
  6581. (if (string-match "[*?{]" (file-name-nondirectory path))
  6582. (dired path)
  6583. (org-open-file path in-emacs line search)))
  6584. ((string= type "news")
  6585. (require 'org-gnus)
  6586. (org-gnus-follow-link path))
  6587. ((string= type "shell")
  6588. (let ((cmd path))
  6589. (if (or (not org-confirm-shell-link-function)
  6590. (funcall org-confirm-shell-link-function
  6591. (format "Execute \"%s\" in shell? "
  6592. (org-add-props cmd nil
  6593. 'face 'org-warning))))
  6594. (progn
  6595. (message "Executing %s" cmd)
  6596. (shell-command cmd))
  6597. (error "Abort"))))
  6598. ((string= type "elisp")
  6599. (let ((cmd path))
  6600. (if (or (not org-confirm-elisp-link-function)
  6601. (funcall org-confirm-elisp-link-function
  6602. (format "Execute \"%s\" as elisp? "
  6603. (org-add-props cmd nil
  6604. 'face 'org-warning))))
  6605. (message "%s => %s" cmd
  6606. (if (equal (string-to-char cmd) ?\()
  6607. (eval (read cmd))
  6608. (call-interactively (read cmd))))
  6609. (error "Abort"))))
  6610. (t
  6611. (browse-url-at-point))))))
  6612. (move-marker org-open-link-marker nil)
  6613. (run-hook-with-args 'org-follow-link-hook))
  6614. ;;;; Time estimates
  6615. (defun org-get-effort (&optional pom)
  6616. "Get the effort estimate for the current entry."
  6617. (org-entry-get pom org-effort-property))
  6618. ;;; File search
  6619. (defvar org-create-file-search-functions nil
  6620. "List of functions to construct the right search string for a file link.
  6621. These functions are called in turn with point at the location to
  6622. which the link should point.
  6623. A function in the hook should first test if it would like to
  6624. handle this file type, for example by checking the major-mode or
  6625. the file extension. If it decides not to handle this file, it
  6626. should just return nil to give other functions a chance. If it
  6627. does handle the file, it must return the search string to be used
  6628. when following the link. The search string will be part of the
  6629. file link, given after a double colon, and `org-open-at-point'
  6630. will automatically search for it. If special measures must be
  6631. taken to make the search successful, another function should be
  6632. added to the companion hook `org-execute-file-search-functions',
  6633. which see.
  6634. A function in this hook may also use `setq' to set the variable
  6635. `description' to provide a suggestion for the descriptive text to
  6636. be used for this link when it gets inserted into an Org-mode
  6637. buffer with \\[org-insert-link].")
  6638. (defvar org-execute-file-search-functions nil
  6639. "List of functions to execute a file search triggered by a link.
  6640. Functions added to this hook must accept a single argument, the
  6641. search string that was part of the file link, the part after the
  6642. double colon. The function must first check if it would like to
  6643. handle this search, for example by checking the major-mode or the
  6644. file extension. If it decides not to handle this search, it
  6645. should just return nil to give other functions a chance. If it
  6646. does handle the search, it must return a non-nil value to keep
  6647. other functions from trying.
  6648. Each function can access the current prefix argument through the
  6649. variable `current-prefix-argument'. Note that a single prefix is
  6650. used to force opening a link in Emacs, so it may be good to only
  6651. use a numeric or double prefix to guide the search function.
  6652. In case this is needed, a function in this hook can also restore
  6653. the window configuration before `org-open-at-point' was called using:
  6654. (set-window-configuration org-window-config-before-follow-link)")
  6655. (defun org-link-search (s &optional type avoid-pos)
  6656. "Search for a link search option.
  6657. If S is surrounded by forward slashes, it is interpreted as a
  6658. regular expression. In org-mode files, this will create an `org-occur'
  6659. sparse tree. In ordinary files, `occur' will be used to list matches.
  6660. If the current buffer is in `dired-mode', grep will be used to search
  6661. in all files. If AVOID-POS is given, ignore matches near that position."
  6662. (let ((case-fold-search t)
  6663. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6664. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6665. (append '(("") (" ") ("\t") ("\n"))
  6666. org-emphasis-alist)
  6667. "\\|") "\\)"))
  6668. (pos (point))
  6669. (pre nil) (post nil)
  6670. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6671. (cond
  6672. ;; First check if there are any special
  6673. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6674. ;; Now try the builtin stuff
  6675. ((save-excursion
  6676. (goto-char (point-min))
  6677. (and
  6678. (re-search-forward
  6679. (concat "<<" (regexp-quote s0) ">>") nil t)
  6680. (setq type 'dedicated
  6681. pos (match-beginning 0))))
  6682. ;; There is an exact target for this
  6683. (goto-char pos))
  6684. ((and (string-match "^(\\(.*\\))$" s0)
  6685. (save-excursion
  6686. (goto-char (point-min))
  6687. (and
  6688. (re-search-forward
  6689. (concat "[^[]" (regexp-quote
  6690. (format org-coderef-label-format
  6691. (match-string 1 s0))))
  6692. nil t)
  6693. (setq type 'dedicated
  6694. pos (1+ (match-beginning 0))))))
  6695. ;; There is a coderef target for this
  6696. (goto-char pos))
  6697. ((string-match "^/\\(.*\\)/$" s)
  6698. ;; A regular expression
  6699. (cond
  6700. ((org-mode-p)
  6701. (org-occur (match-string 1 s)))
  6702. ;;((eq major-mode 'dired-mode)
  6703. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6704. (t (org-do-occur (match-string 1 s)))))
  6705. (t
  6706. ;; A normal search strings
  6707. (when (equal (string-to-char s) ?*)
  6708. ;; Anchor on headlines, post may include tags.
  6709. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6710. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6711. s (substring s 1)))
  6712. (remove-text-properties
  6713. 0 (length s)
  6714. '(face nil mouse-face nil keymap nil fontified nil) s)
  6715. ;; Make a series of regular expressions to find a match
  6716. (setq words (org-split-string s "[ \n\r\t]+")
  6717. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6718. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6719. "\\)" markers)
  6720. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6721. re2a (concat "[ \t\r\n]" re2a_)
  6722. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6723. re4 (concat "[^a-zA-Z_]" re4_)
  6724. re1 (concat pre re2 post)
  6725. re3 (concat pre (if pre re4_ re4) post)
  6726. re5 (concat pre ".*" re4)
  6727. re2 (concat pre re2)
  6728. re2a (concat pre (if pre re2a_ re2a))
  6729. re4 (concat pre (if pre re4_ re4))
  6730. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6731. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6732. re5 "\\)"
  6733. ))
  6734. (cond
  6735. ((eq type 'org-occur) (org-occur reall))
  6736. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6737. (t (goto-char (point-min))
  6738. (setq type 'fuzzy)
  6739. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6740. (org-search-not-self 1 re1 nil t)
  6741. (org-search-not-self 1 re2 nil t)
  6742. (org-search-not-self 1 re2a nil t)
  6743. (org-search-not-self 1 re3 nil t)
  6744. (org-search-not-self 1 re4 nil t)
  6745. (org-search-not-self 1 re5 nil t)
  6746. )
  6747. (goto-char (match-beginning 1))
  6748. (goto-char pos)
  6749. (error "No match")))))
  6750. (t
  6751. ;; Normal string-search
  6752. (goto-char (point-min))
  6753. (if (search-forward s nil t)
  6754. (goto-char (match-beginning 0))
  6755. (error "No match"))))
  6756. (and (org-mode-p) (org-show-context 'link-search))
  6757. type))
  6758. (defun org-search-not-self (group &rest args)
  6759. "Execute `re-search-forward', but only accept matches that do not
  6760. enclose the position of `org-open-link-marker'."
  6761. (let ((m org-open-link-marker))
  6762. (catch 'exit
  6763. (while (apply 're-search-forward args)
  6764. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6765. (goto-char (match-end group))
  6766. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6767. (> (match-beginning 0) (marker-position m))
  6768. (< (match-end 0) (marker-position m)))
  6769. (save-match-data
  6770. (or (not (org-in-regexp
  6771. org-bracket-link-analytic-regexp 1))
  6772. (not (match-end 4)) ; no description
  6773. (and (<= (match-beginning 4) (point))
  6774. (>= (match-end 4) (point))))))
  6775. (throw 'exit (point))))))))
  6776. (defun org-get-buffer-for-internal-link (buffer)
  6777. "Return a buffer to be used for displaying the link target of internal links."
  6778. (cond
  6779. ((not org-display-internal-link-with-indirect-buffer)
  6780. buffer)
  6781. ((string-match "(Clone)$" (buffer-name buffer))
  6782. (message "Buffer is already a clone, not making another one")
  6783. ;; we also do not modify visibility in this case
  6784. buffer)
  6785. (t ; make a new indirect buffer for displaying the link
  6786. (let* ((bn (buffer-name buffer))
  6787. (ibn (concat bn "(Clone)"))
  6788. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6789. (with-current-buffer ib (org-overview))
  6790. ib))))
  6791. (defun org-do-occur (regexp &optional cleanup)
  6792. "Call the Emacs command `occur'.
  6793. If CLEANUP is non-nil, remove the printout of the regular expression
  6794. in the *Occur* buffer. This is useful if the regex is long and not useful
  6795. to read."
  6796. (occur regexp)
  6797. (when cleanup
  6798. (let ((cwin (selected-window)) win beg end)
  6799. (when (setq win (get-buffer-window "*Occur*"))
  6800. (select-window win))
  6801. (goto-char (point-min))
  6802. (when (re-search-forward "match[a-z]+" nil t)
  6803. (setq beg (match-end 0))
  6804. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6805. (setq end (1- (match-beginning 0)))))
  6806. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6807. (goto-char (point-min))
  6808. (select-window cwin))))
  6809. ;;; The mark ring for links jumps
  6810. (defvar org-mark-ring nil
  6811. "Mark ring for positions before jumps in Org-mode.")
  6812. (defvar org-mark-ring-last-goto nil
  6813. "Last position in the mark ring used to go back.")
  6814. ;; Fill and close the ring
  6815. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6816. (loop for i from 1 to org-mark-ring-length do
  6817. (push (make-marker) org-mark-ring))
  6818. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6819. org-mark-ring)
  6820. (defun org-mark-ring-push (&optional pos buffer)
  6821. "Put the current position or POS into the mark ring and rotate it."
  6822. (interactive)
  6823. (setq pos (or pos (point)))
  6824. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6825. (move-marker (car org-mark-ring)
  6826. (or pos (point))
  6827. (or buffer (current-buffer)))
  6828. (message "%s"
  6829. (substitute-command-keys
  6830. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6831. (defun org-mark-ring-goto (&optional n)
  6832. "Jump to the previous position in the mark ring.
  6833. With prefix arg N, jump back that many stored positions. When
  6834. called several times in succession, walk through the entire ring.
  6835. Org-mode commands jumping to a different position in the current file,
  6836. or to another Org-mode file, automatically push the old position
  6837. onto the ring."
  6838. (interactive "p")
  6839. (let (p m)
  6840. (if (eq last-command this-command)
  6841. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6842. (setq p org-mark-ring))
  6843. (setq org-mark-ring-last-goto p)
  6844. (setq m (car p))
  6845. (switch-to-buffer (marker-buffer m))
  6846. (goto-char m)
  6847. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6848. (defun org-remove-angle-brackets (s)
  6849. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6850. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6851. s)
  6852. (defun org-add-angle-brackets (s)
  6853. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6854. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6855. s)
  6856. (defun org-remove-double-quotes (s)
  6857. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6858. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6859. s)
  6860. ;;; Following specific links
  6861. (defun org-follow-timestamp-link ()
  6862. (cond
  6863. ((org-at-date-range-p t)
  6864. (let ((org-agenda-start-on-weekday)
  6865. (t1 (match-string 1))
  6866. (t2 (match-string 2)))
  6867. (setq t1 (time-to-days (org-time-string-to-time t1))
  6868. t2 (time-to-days (org-time-string-to-time t2)))
  6869. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6870. ((org-at-timestamp-p t)
  6871. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6872. (substring (match-string 1) 0 10)))
  6873. 1))
  6874. (t (error "This should not happen"))))
  6875. ;;; Following file links
  6876. (defvar org-wait nil)
  6877. (defun org-open-file (path &optional in-emacs line search)
  6878. "Open the file at PATH.
  6879. First, this expands any special file name abbreviations. Then the
  6880. configuration variable `org-file-apps' is checked if it contains an
  6881. entry for this file type, and if yes, the corresponding command is launched.
  6882. If no application is found, Emacs simply visits the file.
  6883. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6884. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6885. and o use an external application to visit the file.
  6886. Optional LINE specifies a line to go to, optional SEARCH a string to
  6887. search for. If LINE or SEARCH is given, the file will always be
  6888. opened in Emacs.
  6889. If the file does not exist, an error is thrown."
  6890. (setq in-emacs (or in-emacs line search))
  6891. (let* ((file (if (equal path "")
  6892. buffer-file-name
  6893. (substitute-in-file-name (expand-file-name path))))
  6894. (apps (append org-file-apps (org-default-apps)))
  6895. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6896. (dirp (if remp nil (file-directory-p file)))
  6897. (file (if (and dirp org-open-directory-means-index-dot-org)
  6898. (concat (file-name-as-directory file) "index.org")
  6899. file))
  6900. (a-m-a-p (assq 'auto-mode apps))
  6901. (dfile (downcase file))
  6902. (old-buffer (current-buffer))
  6903. (old-pos (point))
  6904. (old-mode major-mode)
  6905. ext cmd)
  6906. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6907. (setq ext (match-string 1 dfile))
  6908. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6909. (setq ext (match-string 1 dfile))))
  6910. (cond
  6911. ((equal in-emacs '(16))
  6912. (setq cmd (cdr (assoc 'system apps))))
  6913. (in-emacs (setq cmd 'emacs))
  6914. (t
  6915. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6916. (and dirp (cdr (assoc 'directory apps)))
  6917. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6918. 'string-match)
  6919. (cdr (assoc ext apps))
  6920. (cdr (assoc t apps))))))
  6921. (when (eq cmd 'system)
  6922. (setq cmd (cdr (assoc 'system apps))))
  6923. (when (eq cmd 'default)
  6924. (setq cmd (cdr (assoc t apps))))
  6925. (when (eq cmd 'mailcap)
  6926. (require 'mailcap)
  6927. (mailcap-parse-mailcaps)
  6928. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6929. (command (mailcap-mime-info mime-type)))
  6930. (if (stringp command)
  6931. (setq cmd command)
  6932. (setq cmd 'emacs))))
  6933. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6934. (not (file-exists-p file))
  6935. (not org-open-non-existing-files))
  6936. (error "No such file: %s" file))
  6937. (cond
  6938. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6939. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6940. (while (string-match "['\"]%s['\"]" cmd)
  6941. (setq cmd (replace-match "%s" t t cmd)))
  6942. (while (string-match "%s" cmd)
  6943. (setq cmd (replace-match
  6944. (save-match-data
  6945. (shell-quote-argument
  6946. (convert-standard-filename file)))
  6947. t t cmd)))
  6948. (save-window-excursion
  6949. (start-process-shell-command cmd nil cmd)
  6950. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6951. ))
  6952. ((or (stringp cmd)
  6953. (eq cmd 'emacs))
  6954. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6955. (widen)
  6956. (if line (goto-line line)
  6957. (if search (org-link-search search))))
  6958. ((consp cmd)
  6959. (let ((file (convert-standard-filename file)))
  6960. (eval cmd)))
  6961. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6962. (and (org-mode-p) (eq old-mode 'org-mode)
  6963. (or (not (equal old-buffer (current-buffer)))
  6964. (not (equal old-pos (point))))
  6965. (org-mark-ring-push old-pos old-buffer))))
  6966. (defun org-default-apps ()
  6967. "Return the default applications for this operating system."
  6968. (cond
  6969. ((eq system-type 'darwin)
  6970. org-file-apps-defaults-macosx)
  6971. ((eq system-type 'windows-nt)
  6972. org-file-apps-defaults-windowsnt)
  6973. (t org-file-apps-defaults-gnu)))
  6974. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6975. "Convert extensions to regular expressions in the cars of LIST.
  6976. Also, weed out any non-string entries, because the return value is used
  6977. only for regexp matching.
  6978. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6979. point to the symbol `emacs', indicating that the file should
  6980. be opened in Emacs."
  6981. (append
  6982. (delq nil
  6983. (mapcar (lambda (x)
  6984. (if (not (stringp (car x)))
  6985. nil
  6986. (if (string-match "\\W" (car x))
  6987. x
  6988. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6989. list))
  6990. (if add-auto-mode
  6991. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6992. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6993. (defun org-file-remote-p (file)
  6994. "Test whether FILE specifies a location on a remote system.
  6995. Return non-nil if the location is indeed remote.
  6996. For example, the filename \"/user@host:/foo\" specifies a location
  6997. on the system \"/user@host:\"."
  6998. (cond ((fboundp 'file-remote-p)
  6999. (file-remote-p file))
  7000. ((fboundp 'tramp-handle-file-remote-p)
  7001. (tramp-handle-file-remote-p file))
  7002. ((and (boundp 'ange-ftp-name-format)
  7003. (string-match (car ange-ftp-name-format) file))
  7004. t)
  7005. (t nil)))
  7006. ;;;; Refiling
  7007. (defun org-get-org-file ()
  7008. "Read a filename, with default directory `org-directory'."
  7009. (let ((default (or org-default-notes-file remember-data-file)))
  7010. (read-file-name (format "File name [%s]: " default)
  7011. (file-name-as-directory org-directory)
  7012. default)))
  7013. (defun org-notes-order-reversed-p ()
  7014. "Check if the current file should receive notes in reversed order."
  7015. (cond
  7016. ((not org-reverse-note-order) nil)
  7017. ((eq t org-reverse-note-order) t)
  7018. ((not (listp org-reverse-note-order)) nil)
  7019. (t (catch 'exit
  7020. (let ((all org-reverse-note-order)
  7021. entry)
  7022. (while (setq entry (pop all))
  7023. (if (string-match (car entry) buffer-file-name)
  7024. (throw 'exit (cdr entry))))
  7025. nil)))))
  7026. (defvar org-refile-target-table nil
  7027. "The list of refile targets, created by `org-refile'.")
  7028. (defvar org-agenda-new-buffers nil
  7029. "Buffers created to visit agenda files.")
  7030. (defun org-get-refile-targets (&optional default-buffer)
  7031. "Produce a table with refile targets."
  7032. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7033. targets txt re files f desc descre fast-path-p level)
  7034. (message "Getting targets...")
  7035. (with-current-buffer (or default-buffer (current-buffer))
  7036. (while (setq entry (pop entries))
  7037. (setq files (car entry) desc (cdr entry))
  7038. (setq fast-path-p nil)
  7039. (cond
  7040. ((null files) (setq files (list (current-buffer))))
  7041. ((eq files 'org-agenda-files)
  7042. (setq files (org-agenda-files 'unrestricted)))
  7043. ((and (symbolp files) (fboundp files))
  7044. (setq files (funcall files)))
  7045. ((and (symbolp files) (boundp files))
  7046. (setq files (symbol-value files))))
  7047. (if (stringp files) (setq files (list files)))
  7048. (cond
  7049. ((eq (car desc) :tag)
  7050. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7051. ((eq (car desc) :todo)
  7052. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7053. ((eq (car desc) :regexp)
  7054. (setq descre (cdr desc)))
  7055. ((eq (car desc) :level)
  7056. (setq descre (concat "^\\*\\{" (number-to-string
  7057. (if org-odd-levels-only
  7058. (1- (* 2 (cdr desc)))
  7059. (cdr desc)))
  7060. "\\}[ \t]")))
  7061. ((eq (car desc) :maxlevel)
  7062. (setq fast-path-p t)
  7063. (setq descre (concat "^\\*\\{1," (number-to-string
  7064. (if org-odd-levels-only
  7065. (1- (* 2 (cdr desc)))
  7066. (cdr desc)))
  7067. "\\}[ \t]")))
  7068. (t (error "Bad refiling target description %s" desc)))
  7069. (while (setq f (pop files))
  7070. (save-excursion
  7071. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7072. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7073. (setq f (expand-file-name f))
  7074. (save-excursion
  7075. (save-restriction
  7076. (widen)
  7077. (goto-char (point-min))
  7078. (while (re-search-forward descre nil t)
  7079. (goto-char (point-at-bol))
  7080. (when (looking-at org-complex-heading-regexp)
  7081. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7082. txt (org-link-display-format (match-string 4))
  7083. re (concat "^" (regexp-quote
  7084. (buffer-substring (match-beginning 1)
  7085. (match-end 4)))))
  7086. (if (match-end 5) (setq re (concat re "[ \t]+"
  7087. (regexp-quote
  7088. (match-string 5)))))
  7089. (setq re (concat re "[ \t]*$"))
  7090. (when org-refile-use-outline-path
  7091. (setq txt (mapconcat 'org-protect-slash
  7092. (append
  7093. (if (eq org-refile-use-outline-path 'file)
  7094. (list (file-name-nondirectory
  7095. (buffer-file-name (buffer-base-buffer))))
  7096. (if (eq org-refile-use-outline-path 'full-file-path)
  7097. (list (buffer-file-name (buffer-base-buffer)))))
  7098. (org-get-outline-path fast-path-p level txt)
  7099. (list txt))
  7100. "/")))
  7101. (push (list txt f re (point)) targets))
  7102. (goto-char (point-at-eol))))))))
  7103. (message "Getting targets...done")
  7104. (nreverse targets))))
  7105. (defun org-protect-slash (s)
  7106. (while (string-match "/" s)
  7107. (setq s (replace-match "\\" t t s)))
  7108. s)
  7109. (defvar org-olpa (make-vector 20 nil))
  7110. (defun org-get-outline-path (&optional fastp level heading)
  7111. "Return the outline path to the current entry, as a list."
  7112. (if fastp
  7113. (progn
  7114. (if (> level 19)
  7115. (error "Outline path failure, more than 19 levels."))
  7116. (loop for i from level upto 19 do
  7117. (aset org-olpa i nil))
  7118. (prog1
  7119. (delq nil (append org-olpa nil))
  7120. (aset org-olpa level heading)))
  7121. (let (rtn)
  7122. (save-excursion
  7123. (while (org-up-heading-safe)
  7124. (when (looking-at org-complex-heading-regexp)
  7125. (push (org-match-string-no-properties 4) rtn)))
  7126. rtn))))
  7127. (defvar org-refile-history nil
  7128. "History for refiling operations.")
  7129. (defun org-refile (&optional goto default-buffer)
  7130. "Move the entry at point to another heading.
  7131. The list of target headings is compiled using the information in
  7132. `org-refile-targets', which see. This list is created before each use
  7133. and will therefore always be up-to-date.
  7134. At the target location, the entry is filed as a subitem of the target heading.
  7135. Depending on `org-reverse-note-order', the new subitem will either be the
  7136. first or the last subitem.
  7137. If there is an active region, all entries in that region will be moved.
  7138. However, the region must fulfil the requirement that the first heading
  7139. is the first one sets the top-level of the moved text - at most siblings
  7140. below it are allowed.
  7141. With prefix arg GOTO, the command will only visit the target location,
  7142. not actually move anything.
  7143. With a double prefix `C-u C-u', go to the location where the last refiling
  7144. operation has put the subtree."
  7145. (interactive "P")
  7146. (let* ((cbuf (current-buffer))
  7147. (regionp (org-region-active-p))
  7148. (region-start (and regionp (region-beginning)))
  7149. (region-end (and regionp (region-end)))
  7150. (region-length (and regionp (- region-end region-start)))
  7151. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7152. pos it nbuf file re level reversed)
  7153. (when regionp (goto-char region-start)
  7154. (unless (org-kill-is-subtree-p
  7155. (buffer-substring region-start region-end))
  7156. (error "The region is not a (sequence of) subtree(s)")))
  7157. (if (equal goto '(16))
  7158. (org-refile-goto-last-stored)
  7159. (when (setq it (org-refile-get-location
  7160. (if goto "Goto: " "Refile to: ") default-buffer))
  7161. (setq file (nth 1 it)
  7162. re (nth 2 it)
  7163. pos (nth 3 it))
  7164. (if (and (equal (buffer-file-name) file)
  7165. (if regionp
  7166. (and (>= pos region-start)
  7167. (<= pos region-end))
  7168. (and (>= pos (point))
  7169. (< pos (save-excursion
  7170. (org-end-of-subtree t t))))))
  7171. (error "Cannot refile to position inside the tree or region"))
  7172. (setq nbuf (or (find-buffer-visiting file)
  7173. (find-file-noselect file)))
  7174. (if goto
  7175. (progn
  7176. (switch-to-buffer nbuf)
  7177. (goto-char pos)
  7178. (org-show-context 'org-goto))
  7179. (if regionp
  7180. (progn
  7181. (kill-new (buffer-substring region-start region-end))
  7182. (org-save-markers-in-region region-start region-end))
  7183. (org-copy-subtree 1 nil t))
  7184. (save-excursion
  7185. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7186. (find-file-noselect file))))
  7187. (setq reversed (org-notes-order-reversed-p))
  7188. (save-excursion
  7189. (save-restriction
  7190. (widen)
  7191. (goto-char pos)
  7192. (looking-at outline-regexp)
  7193. (setq level (org-get-valid-level (funcall outline-level) 1))
  7194. (goto-char
  7195. (if reversed
  7196. (or (outline-next-heading) (point-max))
  7197. (or (save-excursion (outline-get-next-sibling))
  7198. (org-end-of-subtree t t)
  7199. (point-max))))
  7200. (if (not (bolp)) (newline))
  7201. (bookmark-set "org-refile-last-stored")
  7202. (org-paste-subtree level))))
  7203. (if regionp
  7204. (delete-region (point) (+ (point) region-length))
  7205. (org-cut-subtree))
  7206. (setq org-markers-to-move nil)
  7207. (message "Refiled to \"%s\"" (car it)))))))
  7208. (defun org-refile-goto-last-stored ()
  7209. "Go to the location where the last refile was stored."
  7210. (interactive)
  7211. (bookmark-jump "org-refile-last-stored")
  7212. (message "This is the location of the last refile"))
  7213. (defun org-refile-get-location (&optional prompt default-buffer)
  7214. "Prompt the user for a refile location, using PROMPT."
  7215. (let ((org-refile-targets org-refile-targets)
  7216. (org-refile-use-outline-path org-refile-use-outline-path))
  7217. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7218. (unless org-refile-target-table
  7219. (error "No refile targets"))
  7220. (let* ((cbuf (current-buffer))
  7221. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7222. (cfunc (if (and org-refile-use-outline-path
  7223. org-outline-path-complete-in-steps)
  7224. 'org-olpath-completing-read
  7225. 'org-ido-completing-read))
  7226. (extra (if org-refile-use-outline-path "/" ""))
  7227. (filename (and cfn (expand-file-name cfn)))
  7228. (tbl (mapcar
  7229. (lambda (x)
  7230. (if (not (equal filename (nth 1 x)))
  7231. (cons (concat (car x) extra " ("
  7232. (file-name-nondirectory (nth 1 x)) ")")
  7233. (cdr x))
  7234. (cons (concat (car x) extra) (cdr x))))
  7235. org-refile-target-table))
  7236. (completion-ignore-case t))
  7237. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7238. tbl)))
  7239. (defun org-olpath-completing-read (prompt collection &rest args)
  7240. "Read an outline path like a file name."
  7241. (let ((thetable collection))
  7242. (apply
  7243. 'org-ido-completing-read prompt
  7244. (lambda (string predicate &optional flag)
  7245. (let (rtn r f (l (length string)))
  7246. (cond
  7247. ((eq flag nil)
  7248. ;; try completion
  7249. (try-completion string thetable))
  7250. ((eq flag t)
  7251. ;; all-completions
  7252. (setq rtn (all-completions string thetable predicate))
  7253. (mapcar
  7254. (lambda (x)
  7255. (setq r (substring x l))
  7256. (if (string-match " ([^)]*)$" x)
  7257. (setq f (match-string 0 x))
  7258. (setq f ""))
  7259. (if (string-match "/" r)
  7260. (concat string (substring r 0 (match-end 0)) f)
  7261. x))
  7262. rtn))
  7263. ((eq flag 'lambda)
  7264. ;; exact match?
  7265. (assoc string thetable)))
  7266. ))
  7267. args)))
  7268. ;;;; Dynamic blocks
  7269. (defun org-find-dblock (name)
  7270. "Find the first dynamic block with name NAME in the buffer.
  7271. If not found, stay at current position and return nil."
  7272. (let (pos)
  7273. (save-excursion
  7274. (goto-char (point-min))
  7275. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7276. nil t)
  7277. (match-beginning 0))))
  7278. (if pos (goto-char pos))
  7279. pos))
  7280. (defconst org-dblock-start-re
  7281. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7282. "Matches the startline of a dynamic block, with parameters.")
  7283. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7284. "Matches the end of a dynamic block.")
  7285. (defun org-create-dblock (plist)
  7286. "Create a dynamic block section, with parameters taken from PLIST.
  7287. PLIST must contain a :name entry which is used as name of the block."
  7288. (unless (bolp) (newline))
  7289. (let ((name (plist-get plist :name)))
  7290. (insert "#+BEGIN: " name)
  7291. (while plist
  7292. (if (eq (car plist) :name)
  7293. (setq plist (cddr plist))
  7294. (insert " " (prin1-to-string (pop plist)))))
  7295. (insert "\n\n#+END:\n")
  7296. (beginning-of-line -2)))
  7297. (defun org-prepare-dblock ()
  7298. "Prepare dynamic block for refresh.
  7299. This empties the block, puts the cursor at the insert position and returns
  7300. the property list including an extra property :name with the block name."
  7301. (unless (looking-at org-dblock-start-re)
  7302. (error "Not at a dynamic block"))
  7303. (let* ((begdel (1+ (match-end 0)))
  7304. (name (org-no-properties (match-string 1)))
  7305. (params (append (list :name name)
  7306. (read (concat "(" (match-string 3) ")")))))
  7307. (unless (re-search-forward org-dblock-end-re nil t)
  7308. (error "Dynamic block not terminated"))
  7309. (setq params
  7310. (append params
  7311. (list :content (buffer-substring
  7312. begdel (match-beginning 0)))))
  7313. (delete-region begdel (match-beginning 0))
  7314. (goto-char begdel)
  7315. (open-line 1)
  7316. params))
  7317. (defun org-map-dblocks (&optional command)
  7318. "Apply COMMAND to all dynamic blocks in the current buffer.
  7319. If COMMAND is not given, use `org-update-dblock'."
  7320. (let ((cmd (or command 'org-update-dblock))
  7321. pos)
  7322. (save-excursion
  7323. (goto-char (point-min))
  7324. (while (re-search-forward org-dblock-start-re nil t)
  7325. (goto-char (setq pos (match-beginning 0)))
  7326. (condition-case nil
  7327. (funcall cmd)
  7328. (error (message "Error during update of dynamic block")))
  7329. (goto-char pos)
  7330. (unless (re-search-forward org-dblock-end-re nil t)
  7331. (error "Dynamic block not terminated"))))))
  7332. (defun org-dblock-update (&optional arg)
  7333. "User command for updating dynamic blocks.
  7334. Update the dynamic block at point. With prefix ARG, update all dynamic
  7335. blocks in the buffer."
  7336. (interactive "P")
  7337. (if arg
  7338. (org-update-all-dblocks)
  7339. (or (looking-at org-dblock-start-re)
  7340. (org-beginning-of-dblock))
  7341. (org-update-dblock)))
  7342. (defun org-update-dblock ()
  7343. "Update the dynamic block at point
  7344. This means to empty the block, parse for parameters and then call
  7345. the correct writing function."
  7346. (save-window-excursion
  7347. (let* ((pos (point))
  7348. (line (org-current-line))
  7349. (params (org-prepare-dblock))
  7350. (name (plist-get params :name))
  7351. (cmd (intern (concat "org-dblock-write:" name))))
  7352. (message "Updating dynamic block `%s' at line %d..." name line)
  7353. (funcall cmd params)
  7354. (message "Updating dynamic block `%s' at line %d...done" name line)
  7355. (goto-char pos))))
  7356. (defun org-beginning-of-dblock ()
  7357. "Find the beginning of the dynamic block at point.
  7358. Error if there is no such block at point."
  7359. (let ((pos (point))
  7360. beg)
  7361. (end-of-line 1)
  7362. (if (and (re-search-backward org-dblock-start-re nil t)
  7363. (setq beg (match-beginning 0))
  7364. (re-search-forward org-dblock-end-re nil t)
  7365. (> (match-end 0) pos))
  7366. (goto-char beg)
  7367. (goto-char pos)
  7368. (error "Not in a dynamic block"))))
  7369. (defun org-update-all-dblocks ()
  7370. "Update all dynamic blocks in the buffer.
  7371. This function can be used in a hook."
  7372. (when (org-mode-p)
  7373. (org-map-dblocks 'org-update-dblock)))
  7374. ;;;; Completion
  7375. (defconst org-additional-option-like-keywords
  7376. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7377. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7378. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7379. "BEGIN_QUOTE" "END_QUOTE"
  7380. "BEGIN_VERSE" "END_VERSE"
  7381. "BEGIN_SRC" "END_SRC"
  7382. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7383. (defcustom org-structure-template-alist
  7384. '(
  7385. ("s" "#+begin_src ?\n\n#+end_src"
  7386. "<src lang=\"?\">\n\n</src>")
  7387. ("e" "#+begin_example\n?\n#+end_example"
  7388. "<example>\n?\n</example>")
  7389. ("q" "#+begin_quote\n?\n#+end_quote"
  7390. "<quote>\n?\n</quote>")
  7391. ("v" "#+begin_verse\n?\n#+end_verse"
  7392. "<verse>\n?\n/verse>")
  7393. ("l" "#+begin_latex\n?\n#+end_latex"
  7394. "<literal style=\"latex\">\n?\n</literal>")
  7395. ("L" "#+latex: "
  7396. "<literal style=\"latex\">?</literal>")
  7397. ("h" "#+begin_html\n?\n#+end_html"
  7398. "<literal style=\"html\">\n?\n</literal>")
  7399. ("H" "#+html: "
  7400. "<literal style=\"html\">?</literal>")
  7401. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7402. ("A" "#+ascii: ")
  7403. ("i" "#+include %file ?"
  7404. "<include file=%file markup=\"?\">")
  7405. )
  7406. "Structure completion elements.
  7407. This is a list of abbreviation keys and values. The value gets inserted
  7408. it you type @samp{.} followed by the key and then the completion key,
  7409. usually `M-TAB'. %file will be replaced by a file name after prompting
  7410. for the file using completion.
  7411. There are two templates for each key, the first uses the original Org syntax,
  7412. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7413. the default when the /org-mtags.el/ module has been loaded. See also the
  7414. variable `org-mtags-prefer-muse-templates'.
  7415. This is an experimental feature, it is undecided if it is going to stay in."
  7416. :group 'org-completion
  7417. :type '(repeat
  7418. (string :tag "Key")
  7419. (string :tag "Template")
  7420. (string :tag "Muse Template")))
  7421. (defun org-try-structure-completion ()
  7422. "Try to complete a structure template before point.
  7423. This looks for strings like \"<e\" on an otherwise empty line and
  7424. expands them."
  7425. (let ((l (buffer-substring (point-at-bol) (point)))
  7426. a)
  7427. (when (and (looking-at "[ \t]*$")
  7428. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7429. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7430. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7431. (match-beginning 1)) a)
  7432. t)))
  7433. (defun org-complete-expand-structure-template (start cell)
  7434. "Expand a structure template."
  7435. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7436. (rpl (nth (if musep 2 1) cell)))
  7437. (delete-region start (point))
  7438. (when (string-match "\\`#\\+" rpl)
  7439. (cond
  7440. ((bolp))
  7441. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7442. (delete-region (point-at-bol) (point)))
  7443. (t (newline))))
  7444. (setq start (point))
  7445. (if (string-match "%file" rpl)
  7446. (setq rpl (replace-match
  7447. (concat
  7448. "\""
  7449. (save-match-data
  7450. (abbreviate-file-name (read-file-name "Include file: ")))
  7451. "\"")
  7452. t t rpl)))
  7453. (insert rpl)
  7454. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7455. (defun org-complete (&optional arg)
  7456. "Perform completion on word at point.
  7457. At the beginning of a headline, this completes TODO keywords as given in
  7458. `org-todo-keywords'.
  7459. If the current word is preceded by a backslash, completes the TeX symbols
  7460. that are supported for HTML support.
  7461. If the current word is preceded by \"#+\", completes special words for
  7462. setting file options.
  7463. In the line after \"#+STARTUP:, complete valid keywords.\"
  7464. At all other locations, this simply calls the value of
  7465. `org-completion-fallback-command'."
  7466. (interactive "P")
  7467. (org-without-partial-completion
  7468. (catch 'exit
  7469. (let* ((a nil)
  7470. (end (point))
  7471. (beg1 (save-excursion
  7472. (skip-chars-backward (org-re "[:alnum:]_@"))
  7473. (point)))
  7474. (beg (save-excursion
  7475. (skip-chars-backward "a-zA-Z0-9_:$")
  7476. (point)))
  7477. (confirm (lambda (x) (stringp (car x))))
  7478. (searchhead (equal (char-before beg) ?*))
  7479. (struct
  7480. (when (and (member (char-before beg1) '(?. ?<))
  7481. (setq a (assoc (buffer-substring beg1 (point))
  7482. org-structure-template-alist)))
  7483. (org-complete-expand-structure-template (1- beg1) a)
  7484. (throw 'exit t)))
  7485. (tag (and (equal (char-before beg1) ?:)
  7486. (equal (char-after (point-at-bol)) ?*)))
  7487. (prop (and (equal (char-before beg1) ?:)
  7488. (not (equal (char-after (point-at-bol)) ?*))))
  7489. (texp (equal (char-before beg) ?\\))
  7490. (link (equal (char-before beg) ?\[))
  7491. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7492. beg)
  7493. "#+"))
  7494. (startup (string-match "^#\\+STARTUP:.*"
  7495. (buffer-substring (point-at-bol) (point))))
  7496. (completion-ignore-case opt)
  7497. (type nil)
  7498. (tbl nil)
  7499. (table (cond
  7500. (opt
  7501. (setq type :opt)
  7502. (require 'org-exp)
  7503. (append
  7504. (mapcar
  7505. (lambda (x)
  7506. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7507. (cons (match-string 2 x) (match-string 1 x)))
  7508. (org-split-string (org-get-current-options) "\n"))
  7509. (mapcar 'list org-additional-option-like-keywords)))
  7510. (startup
  7511. (setq type :startup)
  7512. org-startup-options)
  7513. (link (append org-link-abbrev-alist-local
  7514. org-link-abbrev-alist))
  7515. (texp
  7516. (setq type :tex)
  7517. org-html-entities)
  7518. ((string-match "\\`\\*+[ \t]+\\'"
  7519. (buffer-substring (point-at-bol) beg))
  7520. (setq type :todo)
  7521. (mapcar 'list org-todo-keywords-1))
  7522. (searchhead
  7523. (setq type :searchhead)
  7524. (save-excursion
  7525. (goto-char (point-min))
  7526. (while (re-search-forward org-todo-line-regexp nil t)
  7527. (push (list
  7528. (org-make-org-heading-search-string
  7529. (match-string 3) t))
  7530. tbl)))
  7531. tbl)
  7532. (tag (setq type :tag beg beg1)
  7533. (or org-tag-alist (org-get-buffer-tags)))
  7534. (prop (setq type :prop beg beg1)
  7535. (mapcar 'list (org-buffer-property-keys nil t t)))
  7536. (t (progn
  7537. (call-interactively org-completion-fallback-command)
  7538. (throw 'exit nil)))))
  7539. (pattern (buffer-substring-no-properties beg end))
  7540. (completion (try-completion pattern table confirm)))
  7541. (cond ((eq completion t)
  7542. (if (not (assoc (upcase pattern) table))
  7543. (message "Already complete")
  7544. (if (and (equal type :opt)
  7545. (not (member (car (assoc (upcase pattern) table))
  7546. org-additional-option-like-keywords)))
  7547. (insert (substring (cdr (assoc (upcase pattern) table))
  7548. (length pattern)))
  7549. (if (memq type '(:tag :prop)) (insert ":")))))
  7550. ((null completion)
  7551. (message "Can't find completion for \"%s\"" pattern)
  7552. (ding))
  7553. ((not (string= pattern completion))
  7554. (delete-region beg end)
  7555. (if (string-match " +$" completion)
  7556. (setq completion (replace-match "" t t completion)))
  7557. (insert completion)
  7558. (if (get-buffer-window "*Completions*")
  7559. (delete-window (get-buffer-window "*Completions*")))
  7560. (if (assoc completion table)
  7561. (if (eq type :todo) (insert " ")
  7562. (if (memq type '(:tag :prop)) (insert ":"))))
  7563. (if (and (equal type :opt) (assoc completion table))
  7564. (message "%s" (substitute-command-keys
  7565. "Press \\[org-complete] again to insert example settings"))))
  7566. (t
  7567. (message "Making completion list...")
  7568. (let ((list (sort (all-completions pattern table confirm)
  7569. 'string<)))
  7570. (with-output-to-temp-buffer "*Completions*"
  7571. (condition-case nil
  7572. ;; Protection needed for XEmacs and emacs 21
  7573. (display-completion-list list pattern)
  7574. (error (display-completion-list list)))))
  7575. (message "Making completion list...%s" "done")))))))
  7576. ;;;; TODO, DEADLINE, Comments
  7577. (defun org-toggle-comment ()
  7578. "Change the COMMENT state of an entry."
  7579. (interactive)
  7580. (save-excursion
  7581. (org-back-to-heading)
  7582. (let (case-fold-search)
  7583. (if (looking-at (concat outline-regexp
  7584. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7585. (replace-match "" t t nil 1)
  7586. (if (looking-at outline-regexp)
  7587. (progn
  7588. (goto-char (match-end 0))
  7589. (insert org-comment-string " ")))))))
  7590. (defvar org-last-todo-state-is-todo nil
  7591. "This is non-nil when the last TODO state change led to a TODO state.
  7592. If the last change removed the TODO tag or switched to DONE, then
  7593. this is nil.")
  7594. (defvar org-setting-tags nil) ; dynamically skipped
  7595. (defun org-parse-local-options (string var)
  7596. "Parse STRING for startup setting relevant for variable VAR."
  7597. (let ((rtn (symbol-value var))
  7598. e opts)
  7599. (save-match-data
  7600. (if (or (not string) (not (string-match "\\S-" string)))
  7601. rtn
  7602. (setq opts (delq nil (mapcar (lambda (x)
  7603. (setq e (assoc x org-startup-options))
  7604. (if (eq (nth 1 e) var) e nil))
  7605. (org-split-string string "[ \t]+"))))
  7606. (if (not opts)
  7607. rtn
  7608. (setq rtn nil)
  7609. (while (setq e (pop opts))
  7610. (if (not (nth 3 e))
  7611. (setq rtn (nth 2 e))
  7612. (if (not (listp rtn)) (setq rtn nil))
  7613. (push (nth 2 e) rtn)))
  7614. rtn)))))
  7615. (defvar org-todo-setup-filter-hook nil
  7616. "Hook for functions that pre-filter todo specs.
  7617. Each function takes a todo spec and returns either `nil' or the spec
  7618. transformed into canonical form." )
  7619. (defvar org-todo-get-default-hook nil
  7620. "Hook for functions that get a default item for todo.
  7621. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7622. `nil' or a string to be used for the todo mark." )
  7623. (defvar org-agenda-headline-snapshot-before-repeat)
  7624. (defun org-todo (&optional arg)
  7625. "Change the TODO state of an item.
  7626. The state of an item is given by a keyword at the start of the heading,
  7627. like
  7628. *** TODO Write paper
  7629. *** DONE Call mom
  7630. The different keywords are specified in the variable `org-todo-keywords'.
  7631. By default the available states are \"TODO\" and \"DONE\".
  7632. So for this example: when the item starts with TODO, it is changed to DONE.
  7633. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7634. DONE are present, add TODO at the beginning of the heading.
  7635. With C-u prefix arg, use completion to determine the new state.
  7636. With numeric prefix arg, switch to that state.
  7637. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7638. With a tripple C-u prefix, circumvent any state blocking.
  7639. For calling through lisp, arg is also interpreted in the following way:
  7640. 'none -> empty state
  7641. \"\"(empty string) -> switch to empty state
  7642. 'done -> switch to DONE
  7643. 'nextset -> switch to the next set of keywords
  7644. 'previousset -> switch to the previous set of keywords
  7645. \"WAITING\" -> switch to the specified keyword, but only if it
  7646. really is a member of `org-todo-keywords'."
  7647. (interactive "P")
  7648. (if (equal arg '(16)) (setq arg 'nextset))
  7649. (let ((org-blocker-hook org-blocker-hook))
  7650. (when (equal arg '(64))
  7651. (setq arg nil org-blocker-hook nil))
  7652. (save-excursion
  7653. (catch 'exit
  7654. (org-back-to-heading)
  7655. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7656. (or (looking-at (concat " +" org-todo-regexp " *"))
  7657. (looking-at " *"))
  7658. (let* ((match-data (match-data))
  7659. (startpos (point-at-bol))
  7660. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7661. (org-log-done org-log-done)
  7662. (org-log-repeat org-log-repeat)
  7663. (org-todo-log-states org-todo-log-states)
  7664. (this (match-string 1))
  7665. (hl-pos (match-beginning 0))
  7666. (head (org-get-todo-sequence-head this))
  7667. (ass (assoc head org-todo-kwd-alist))
  7668. (interpret (nth 1 ass))
  7669. (done-word (nth 3 ass))
  7670. (final-done-word (nth 4 ass))
  7671. (last-state (or this ""))
  7672. (completion-ignore-case t)
  7673. (member (member this org-todo-keywords-1))
  7674. (tail (cdr member))
  7675. (state (cond
  7676. ((and org-todo-key-trigger
  7677. (or (and (equal arg '(4))
  7678. (eq org-use-fast-todo-selection 'prefix))
  7679. (and (not arg) org-use-fast-todo-selection
  7680. (not (eq org-use-fast-todo-selection
  7681. 'prefix)))))
  7682. ;; Use fast selection
  7683. (org-fast-todo-selection))
  7684. ((and (equal arg '(4))
  7685. (or (not org-use-fast-todo-selection)
  7686. (not org-todo-key-trigger)))
  7687. ;; Read a state with completion
  7688. (org-ido-completing-read
  7689. "State: " (mapcar (lambda(x) (list x))
  7690. org-todo-keywords-1)
  7691. nil t))
  7692. ((eq arg 'right)
  7693. (if this
  7694. (if tail (car tail) nil)
  7695. (car org-todo-keywords-1)))
  7696. ((eq arg 'left)
  7697. (if (equal member org-todo-keywords-1)
  7698. nil
  7699. (if this
  7700. (nth (- (length org-todo-keywords-1)
  7701. (length tail) 2)
  7702. org-todo-keywords-1)
  7703. (org-last org-todo-keywords-1))))
  7704. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7705. (setq arg nil))) ; hack to fall back to cycling
  7706. (arg
  7707. ;; user or caller requests a specific state
  7708. (cond
  7709. ((equal arg "") nil)
  7710. ((eq arg 'none) nil)
  7711. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7712. ((eq arg 'nextset)
  7713. (or (car (cdr (member head org-todo-heads)))
  7714. (car org-todo-heads)))
  7715. ((eq arg 'previousset)
  7716. (let ((org-todo-heads (reverse org-todo-heads)))
  7717. (or (car (cdr (member head org-todo-heads)))
  7718. (car org-todo-heads))))
  7719. ((car (member arg org-todo-keywords-1)))
  7720. ((nth (1- (prefix-numeric-value arg))
  7721. org-todo-keywords-1))))
  7722. ((null member) (or head (car org-todo-keywords-1)))
  7723. ((equal this final-done-word) nil) ;; -> make empty
  7724. ((null tail) nil) ;; -> first entry
  7725. ((memq interpret '(type priority))
  7726. (if (eq this-command last-command)
  7727. (car tail)
  7728. (if (> (length tail) 0)
  7729. (or done-word (car org-done-keywords))
  7730. nil)))
  7731. (t
  7732. (car tail))))
  7733. (state (or
  7734. (run-hook-with-args-until-success
  7735. 'org-todo-get-default-hook state last-state)
  7736. state))
  7737. (next (if state (concat " " state " ") " "))
  7738. (change-plist (list :type 'todo-state-change :from this :to state
  7739. :position startpos))
  7740. dolog now-done-p)
  7741. (when org-blocker-hook
  7742. (setq org-last-todo-state-is-todo
  7743. (not (member this org-done-keywords)))
  7744. (unless (save-excursion
  7745. (save-match-data
  7746. (run-hook-with-args-until-failure
  7747. 'org-blocker-hook change-plist)))
  7748. (if (interactive-p)
  7749. (error "TODO state change from %s to %s blocked" this state)
  7750. ;; fail silently
  7751. (message "TODO state change from %s to %s blocked" this state)
  7752. (throw 'exit nil))))
  7753. (store-match-data match-data)
  7754. (replace-match next t t)
  7755. (unless (pos-visible-in-window-p hl-pos)
  7756. (message "TODO state changed to %s" (org-trim next)))
  7757. (unless head
  7758. (setq head (org-get-todo-sequence-head state)
  7759. ass (assoc head org-todo-kwd-alist)
  7760. interpret (nth 1 ass)
  7761. done-word (nth 3 ass)
  7762. final-done-word (nth 4 ass)))
  7763. (when (memq arg '(nextset previousset))
  7764. (message "Keyword-Set %d/%d: %s"
  7765. (- (length org-todo-sets) -1
  7766. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7767. (length org-todo-sets)
  7768. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7769. (setq org-last-todo-state-is-todo
  7770. (not (member state org-done-keywords)))
  7771. (setq now-done-p (and (member state org-done-keywords)
  7772. (not (member this org-done-keywords))))
  7773. (and logging (org-local-logging logging))
  7774. (when (and (or org-todo-log-states org-log-done)
  7775. (not (memq arg '(nextset previousset))))
  7776. ;; we need to look at recording a time and note
  7777. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7778. (nth 2 (assoc this org-todo-log-states))))
  7779. (when (and state
  7780. (member state org-not-done-keywords)
  7781. (not (member this org-not-done-keywords)))
  7782. ;; This is now a todo state and was not one before
  7783. ;; If there was a CLOSED time stamp, get rid of it.
  7784. (org-add-planning-info nil nil 'closed))
  7785. (when (and now-done-p org-log-done)
  7786. ;; It is now done, and it was not done before
  7787. (org-add-planning-info 'closed (org-current-time))
  7788. (if (and (not dolog) (eq 'note org-log-done))
  7789. (org-add-log-setup 'done state this 'findpos 'note)))
  7790. (when (and state dolog)
  7791. ;; This is a non-nil state, and we need to log it
  7792. (org-add-log-setup 'state state this 'findpos dolog)))
  7793. ;; Fixup tag positioning
  7794. (org-todo-trigger-tag-changes state)
  7795. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7796. (when org-provide-todo-statistics
  7797. (org-update-parent-todo-statistics))
  7798. (run-hooks 'org-after-todo-state-change-hook)
  7799. (if (and arg (not (member state org-done-keywords)))
  7800. (setq head (org-get-todo-sequence-head state)))
  7801. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7802. ;; Do we need to trigger a repeat?
  7803. (when now-done-p
  7804. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7805. ;; This is for the agenda, take a snapshot of the headline.
  7806. (save-match-data
  7807. (setq org-agenda-headline-snapshot-before-repeat
  7808. (org-get-heading))))
  7809. (org-auto-repeat-maybe state))
  7810. ;; Fixup cursor location if close to the keyword
  7811. (if (and (outline-on-heading-p)
  7812. (not (bolp))
  7813. (save-excursion (beginning-of-line 1)
  7814. (looking-at org-todo-line-regexp))
  7815. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7816. (progn
  7817. (goto-char (or (match-end 2) (match-end 1)))
  7818. (just-one-space)))
  7819. (when org-trigger-hook
  7820. (save-excursion
  7821. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7822. (defun org-block-todo-from-children-or-siblings (change-plist)
  7823. "Block turning an entry into a TODO, using the hierarchy.
  7824. This checks whether the current task should be blocked from state
  7825. changes. Such blocking occurs when:
  7826. 1. The task has children which are not all in a completed state.
  7827. 2. A task has a parent with the property :ORDERED:, and there
  7828. are siblings prior to the current task with incomplete
  7829. status."
  7830. (catch 'dont-block
  7831. ;; If this is not a todo state change, or if this entry is already DONE,
  7832. ;; do not block
  7833. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7834. (member (plist-get change-plist :from)
  7835. (cons 'done org-done-keywords))
  7836. (member (plist-get change-plist :to)
  7837. (cons 'todo org-not-done-keywords)))
  7838. (throw 'dont-block t))
  7839. ;; If this task has children, and any are undone, it's blocked
  7840. (save-excursion
  7841. (org-back-to-heading t)
  7842. (let ((this-level (funcall outline-level)))
  7843. (outline-next-heading)
  7844. (let ((child-level (funcall outline-level)))
  7845. (while (and (not (eobp))
  7846. (> child-level this-level))
  7847. ;; this todo has children, check whether they are all
  7848. ;; completed
  7849. (if (and (not (org-entry-is-done-p))
  7850. (org-entry-is-todo-p))
  7851. (throw 'dont-block nil))
  7852. (outline-next-heading)
  7853. (setq child-level (funcall outline-level))))))
  7854. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7855. ;; any previous siblings are undone, it's blocked
  7856. (save-excursion
  7857. (org-back-to-heading t)
  7858. (when (save-excursion
  7859. (ignore-errors
  7860. (outline-up-heading 1)
  7861. (org-entry-get (point) "ORDERED")))
  7862. (let* ((this-level (funcall outline-level))
  7863. (current-level this-level))
  7864. (while (and (not (bobp))
  7865. (= current-level this-level))
  7866. (outline-previous-heading)
  7867. (setq current-level (funcall outline-level))
  7868. (if (= current-level this-level)
  7869. ;; this todo has children, check whether they are all
  7870. ;; completed
  7871. (if (and (not (org-entry-is-done-p))
  7872. (org-entry-is-todo-p))
  7873. (throw 'dont-block nil)))))))
  7874. t)) ; don't block
  7875. (defun org-toggle-ordered-property ()
  7876. "Toggle the ORDERED property of the current entry."
  7877. (interactive)
  7878. (save-excursion
  7879. (org-back-to-heading)
  7880. (if (org-entry-get nil "ORDERED")
  7881. (progn
  7882. (org-delete-property "ORDERED")
  7883. (message "Subtasks can be completed in arbitrary order or parallel"))
  7884. (org-entry-put nil "ORDERED" "t")
  7885. (message "Subtasks must be completed in sequence"))))
  7886. (defun org-block-todo-from-checkboxes (change-plist)
  7887. "Block turning an entry into a TODO, using checkboxes.
  7888. This checks whether the current task should be blocked from state
  7889. changes because there are uncheckd boxes in this entry."
  7890. (catch 'dont-block
  7891. ;; If this is not a todo state change, or if this entry is already DONE,
  7892. ;; do not block
  7893. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7894. (member (plist-get change-plist :from)
  7895. (cons 'done org-done-keywords))
  7896. (member (plist-get change-plist :to)
  7897. (cons 'todo org-not-done-keywords)))
  7898. (throw 'dont-block t))
  7899. ;; If this task has checkboxes that are not checked, it's blocked
  7900. (save-excursion
  7901. (org-back-to-heading t)
  7902. (let ((beg (point)) end)
  7903. (outline-next-heading)
  7904. (setq end (point))
  7905. (goto-char beg)
  7906. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7907. end t)
  7908. (throw 'dont-block nil))))
  7909. t)) ; do not block
  7910. (defun org-update-parent-todo-statistics ()
  7911. "Update any statistics cookie in the parent of the current headline."
  7912. (interactive)
  7913. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7914. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7915. (catch 'exit
  7916. (save-excursion
  7917. (setq level (org-up-heading-safe))
  7918. (unless level
  7919. (throw 'exit nil))
  7920. (while (re-search-forward box-re (point-at-eol) t)
  7921. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7922. (setq is-percent (match-end 2))
  7923. (save-match-data
  7924. (unless (outline-next-heading) (throw 'exit nil))
  7925. (while (looking-at org-todo-line-regexp)
  7926. (setq kwd (match-string 2))
  7927. (and kwd (setq cnt-all (1+ cnt-all)))
  7928. (and (member kwd org-done-keywords)
  7929. (setq cnt-done (1+ cnt-done)))
  7930. (condition-case nil
  7931. (org-forward-same-level 1)
  7932. (error (end-of-line 1)))))
  7933. (replace-match
  7934. (if is-percent
  7935. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7936. (format "[%d/%d]" cnt-done cnt-all))))
  7937. (when cookie-present
  7938. (run-hook-with-args 'org-after-todo-statistics-hook
  7939. cnt-done (- cnt-all cnt-done)))))))
  7940. (defvar org-after-todo-statistics-hook nil
  7941. "Hook that is called after a TODO statistics cookie has been updated.
  7942. Each function is called with two arguments: the number of not-done entries
  7943. and the number of done entries.
  7944. For example, the following function, when added to this hook, will switch
  7945. an entry to DONE when all children are done, and back to TODO when new
  7946. entries are set to a TODO status. Note that this hook is only called
  7947. when there is a statistics cookie in the headline!
  7948. (defun org-summary-todo (n-done n-not-done)
  7949. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7950. (let (org-log-done org-log-states) ; turn off logging
  7951. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7952. ")
  7953. (defun org-todo-trigger-tag-changes (state)
  7954. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7955. (let ((l org-todo-state-tags-triggers)
  7956. changes)
  7957. (when (or (not state) (equal state ""))
  7958. (setq changes (append changes (cdr (assoc "" l)))))
  7959. (when (and (stringp state) (> (length state) 0))
  7960. (setq changes (append changes (cdr (assoc state l)))))
  7961. (when (member state org-not-done-keywords)
  7962. (setq changes (append changes (cdr (assoc 'todo l)))))
  7963. (when (member state org-done-keywords)
  7964. (setq changes (append changes (cdr (assoc 'done l)))))
  7965. (dolist (c changes)
  7966. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7967. (defun org-local-logging (value)
  7968. "Get logging settings from a property VALUE."
  7969. (let* (words w a)
  7970. ;; directly set the variables, they are already local.
  7971. (setq org-log-done nil
  7972. org-log-repeat nil
  7973. org-todo-log-states nil)
  7974. (setq words (org-split-string value))
  7975. (while (setq w (pop words))
  7976. (cond
  7977. ((setq a (assoc w org-startup-options))
  7978. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7979. (set (nth 1 a) (nth 2 a))))
  7980. ((setq a (org-extract-log-state-settings w))
  7981. (and (member (car a) org-todo-keywords-1)
  7982. (push a org-todo-log-states)))))))
  7983. (defun org-get-todo-sequence-head (kwd)
  7984. "Return the head of the TODO sequence to which KWD belongs.
  7985. If KWD is not set, check if there is a text property remembering the
  7986. right sequence."
  7987. (let (p)
  7988. (cond
  7989. ((not kwd)
  7990. (or (get-text-property (point-at-bol) 'org-todo-head)
  7991. (progn
  7992. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7993. nil (point-at-eol)))
  7994. (get-text-property p 'org-todo-head))))
  7995. ((not (member kwd org-todo-keywords-1))
  7996. (car org-todo-keywords-1))
  7997. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7998. (defun org-fast-todo-selection ()
  7999. "Fast TODO keyword selection with single keys.
  8000. Returns the new TODO keyword, or nil if no state change should occur."
  8001. (let* ((fulltable org-todo-key-alist)
  8002. (done-keywords org-done-keywords) ;; needed for the faces.
  8003. (maxlen (apply 'max (mapcar
  8004. (lambda (x)
  8005. (if (stringp (car x)) (string-width (car x)) 0))
  8006. fulltable)))
  8007. (expert nil)
  8008. (fwidth (+ maxlen 3 1 3))
  8009. (ncol (/ (- (window-width) 4) fwidth))
  8010. tg cnt e c tbl
  8011. groups ingroup)
  8012. (save-excursion
  8013. (save-window-excursion
  8014. (if expert
  8015. (set-buffer (get-buffer-create " *Org todo*"))
  8016. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8017. (erase-buffer)
  8018. (org-set-local 'org-done-keywords done-keywords)
  8019. (setq tbl fulltable cnt 0)
  8020. (while (setq e (pop tbl))
  8021. (cond
  8022. ((equal e '(:startgroup))
  8023. (push '() groups) (setq ingroup t)
  8024. (when (not (= cnt 0))
  8025. (setq cnt 0)
  8026. (insert "\n"))
  8027. (insert "{ "))
  8028. ((equal e '(:endgroup))
  8029. (setq ingroup nil cnt 0)
  8030. (insert "}\n"))
  8031. (t
  8032. (setq tg (car e) c (cdr e))
  8033. (if ingroup (push tg (car groups)))
  8034. (setq tg (org-add-props tg nil 'face
  8035. (org-get-todo-face tg)))
  8036. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8037. (insert "[" c "] " tg (make-string
  8038. (- fwidth 4 (length tg)) ?\ ))
  8039. (when (= (setq cnt (1+ cnt)) ncol)
  8040. (insert "\n")
  8041. (if ingroup (insert " "))
  8042. (setq cnt 0)))))
  8043. (insert "\n")
  8044. (goto-char (point-min))
  8045. (if (not expert) (org-fit-window-to-buffer))
  8046. (message "[a-z..]:Set [SPC]:clear")
  8047. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8048. (cond
  8049. ((or (= c ?\C-g)
  8050. (and (= c ?q) (not (rassoc c fulltable))))
  8051. (setq quit-flag t))
  8052. ((= c ?\ ) nil)
  8053. ((setq e (rassoc c fulltable) tg (car e))
  8054. tg)
  8055. (t (setq quit-flag t)))))))
  8056. (defun org-entry-is-todo-p ()
  8057. (member (org-get-todo-state) org-not-done-keywords))
  8058. (defun org-entry-is-done-p ()
  8059. (member (org-get-todo-state) org-done-keywords))
  8060. (defun org-get-todo-state ()
  8061. (save-excursion
  8062. (org-back-to-heading t)
  8063. (and (looking-at org-todo-line-regexp)
  8064. (match-end 2)
  8065. (match-string 2))))
  8066. (defun org-at-date-range-p (&optional inactive-ok)
  8067. "Is the cursor inside a date range?"
  8068. (interactive)
  8069. (save-excursion
  8070. (catch 'exit
  8071. (let ((pos (point)))
  8072. (skip-chars-backward "^[<\r\n")
  8073. (skip-chars-backward "<[")
  8074. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8075. (>= (match-end 0) pos)
  8076. (throw 'exit t))
  8077. (skip-chars-backward "^<[\r\n")
  8078. (skip-chars-backward "<[")
  8079. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8080. (>= (match-end 0) pos)
  8081. (throw 'exit t)))
  8082. nil)))
  8083. (defun org-get-repeat ()
  8084. "Check if there is a deadline/schedule with repeater in this entry."
  8085. (save-match-data
  8086. (save-excursion
  8087. (org-back-to-heading t)
  8088. (if (re-search-forward
  8089. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8090. (match-string 1)))))
  8091. (defvar org-last-changed-timestamp)
  8092. (defvar org-last-inserted-timestamp)
  8093. (defvar org-log-post-message)
  8094. (defvar org-log-note-purpose)
  8095. (defvar org-log-note-how)
  8096. (defvar org-log-note-extra)
  8097. (defun org-auto-repeat-maybe (done-word)
  8098. "Check if the current headline contains a repeated deadline/schedule.
  8099. If yes, set TODO state back to what it was and change the base date
  8100. of repeating deadline/scheduled time stamps to new date.
  8101. This function is run automatically after each state change to a DONE state."
  8102. ;; last-state is dynamically scoped into this function
  8103. (let* ((repeat (org-get-repeat))
  8104. (aa (assoc last-state org-todo-kwd-alist))
  8105. (interpret (nth 1 aa))
  8106. (head (nth 2 aa))
  8107. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8108. (msg "Entry repeats: ")
  8109. (org-log-done nil)
  8110. (org-todo-log-states nil)
  8111. (nshiftmax 10) (nshift 0)
  8112. re type n what ts time)
  8113. (when repeat
  8114. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8115. (org-todo (if (eq interpret 'type) last-state head))
  8116. (when org-log-repeat
  8117. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8118. (memq 'org-add-log-note post-command-hook))
  8119. ;; OK, we are already setup for some record
  8120. (if (eq org-log-repeat 'note)
  8121. ;; make sure we take a note, not only a time stamp
  8122. (setq org-log-note-how 'note))
  8123. ;; Set up for taking a record
  8124. (org-add-log-setup 'state (or done-word (car org-done-keywords)) this
  8125. 'findpos org-log-repeat)))
  8126. (org-back-to-heading t)
  8127. (org-add-planning-info nil nil 'closed)
  8128. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8129. org-deadline-time-regexp "\\)\\|\\("
  8130. org-ts-regexp "\\)"))
  8131. (while (re-search-forward
  8132. re (save-excursion (outline-next-heading) (point)) t)
  8133. (setq type (if (match-end 1) org-scheduled-string
  8134. (if (match-end 3) org-deadline-string "Plain:"))
  8135. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8136. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8137. (setq n (string-to-number (match-string 2 ts))
  8138. what (match-string 3 ts))
  8139. (if (equal what "w") (setq n (* n 7) what "d"))
  8140. ;; Preparation, see if we need to modify the start date for the change
  8141. (when (match-end 1)
  8142. (setq time (save-match-data (org-time-string-to-time ts)))
  8143. (cond
  8144. ((equal (match-string 1 ts) ".")
  8145. ;; Shift starting date to today
  8146. (org-timestamp-change
  8147. (- (time-to-days (current-time)) (time-to-days time))
  8148. 'day))
  8149. ((equal (match-string 1 ts) "+")
  8150. (while (or (= nshift 0)
  8151. (<= (time-to-days time) (time-to-days (current-time))))
  8152. (when (= (incf nshift) nshiftmax)
  8153. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8154. (error "Abort")))
  8155. (org-timestamp-change n (cdr (assoc what whata)))
  8156. (org-at-timestamp-p t)
  8157. (setq ts (match-string 1))
  8158. (setq time (save-match-data (org-time-string-to-time ts))))
  8159. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8160. ;; rematch, so that we have everything in place for the real shift
  8161. (org-at-timestamp-p t)
  8162. (setq ts (match-string 1))
  8163. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8164. (org-timestamp-change n (cdr (assoc what whata)))
  8165. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8166. (setq org-log-post-message msg)
  8167. (message "%s" msg))))
  8168. (defun org-show-todo-tree (arg)
  8169. "Make a compact tree which shows all headlines marked with TODO.
  8170. The tree will show the lines where the regexp matches, and all higher
  8171. headlines above the match.
  8172. With a \\[universal-argument] prefix, also show the DONE entries.
  8173. With a numeric prefix N, construct a sparse tree for the Nth element
  8174. of `org-todo-keywords-1'."
  8175. (interactive "P")
  8176. (let ((case-fold-search nil)
  8177. (kwd-re
  8178. (cond ((null arg) org-not-done-regexp)
  8179. ((equal arg '(4))
  8180. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8181. (mapcar 'list org-todo-keywords-1))))
  8182. (concat "\\("
  8183. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8184. "\\)\\>")))
  8185. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8186. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8187. org-todo-keywords-1)))
  8188. (t (error "Invalid prefix argument: %s" arg)))))
  8189. (message "%d TODO entries found"
  8190. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8191. (defun org-deadline (&optional remove time)
  8192. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8193. With argument REMOVE, remove any deadline from the item.
  8194. When TIME is set, it should be an internal time specification, and the
  8195. scheduling will use the corresponding date."
  8196. (interactive "P")
  8197. (if remove
  8198. (progn
  8199. (org-remove-timestamp-with-keyword org-deadline-string)
  8200. (message "Item no longer has a deadline."))
  8201. (if (org-get-repeat)
  8202. (error "Cannot change deadline on task with repeater, please do that by hand")
  8203. (org-add-planning-info 'deadline time 'closed)
  8204. (message "Deadline on %s" org-last-inserted-timestamp))))
  8205. (defun org-schedule (&optional remove time)
  8206. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8207. With argument REMOVE, remove any scheduling date from the item.
  8208. When TIME is set, it should be an internal time specification, and the
  8209. scheduling will use the corresponding date."
  8210. (interactive "P")
  8211. (if remove
  8212. (progn
  8213. (org-remove-timestamp-with-keyword org-scheduled-string)
  8214. (message "Item is no longer scheduled."))
  8215. (if (org-get-repeat)
  8216. (error "Cannot reschedule task with repeater, please do that by hand")
  8217. (org-add-planning-info 'scheduled time 'closed)
  8218. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8219. (defun org-get-scheduled-time (pom &optional inherit)
  8220. "Get the scheduled time as a time tuple, of a format suitable
  8221. for calling org-schedule with, or if there is no scheduling,
  8222. returns nil."
  8223. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8224. (when time
  8225. (apply 'encode-time (org-parse-time-string time)))))
  8226. (defun org-get-deadline-time (pom &optional inherit)
  8227. "Get the deadine as a time tuple, of a format suitable for
  8228. calling org-deadlin with, or if there is no scheduling, returns
  8229. nil."
  8230. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8231. (when time
  8232. (apply 'encode-time (org-parse-time-string time)))))
  8233. (defun org-remove-timestamp-with-keyword (keyword)
  8234. "Remove all time stamps with KEYWORD in the current entry."
  8235. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8236. beg)
  8237. (save-excursion
  8238. (org-back-to-heading t)
  8239. (setq beg (point))
  8240. (org-end-of-subtree t t)
  8241. (while (re-search-backward re beg t)
  8242. (replace-match "")
  8243. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8244. (equal (char-before) ?\ ))
  8245. (backward-delete-char 1)
  8246. (if (string-match "^[ \t]*$" (buffer-substring
  8247. (point-at-bol) (point-at-eol)))
  8248. (delete-region (point-at-bol)
  8249. (min (point-max) (1+ (point-at-eol))))))))))
  8250. (defun org-add-planning-info (what &optional time &rest remove)
  8251. "Insert new timestamp with keyword in the line directly after the headline.
  8252. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8253. If non is given, the user is prompted for a date.
  8254. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8255. be removed."
  8256. (interactive)
  8257. (let (org-time-was-given org-end-time-was-given ts
  8258. end default-time default-input)
  8259. (when (and (not time) (memq what '(scheduled deadline)))
  8260. ;; Try to get a default date/time from existing timestamp
  8261. (save-excursion
  8262. (org-back-to-heading t)
  8263. (setq end (save-excursion (outline-next-heading) (point)))
  8264. (when (re-search-forward (if (eq what 'scheduled)
  8265. org-scheduled-time-regexp
  8266. org-deadline-time-regexp)
  8267. end t)
  8268. (setq ts (match-string 1)
  8269. default-time
  8270. (apply 'encode-time (org-parse-time-string ts))
  8271. default-input (and ts (org-get-compact-tod ts))))))
  8272. (when what
  8273. ;; If necessary, get the time from the user
  8274. (setq time (or time (org-read-date nil 'to-time nil nil
  8275. default-time default-input))))
  8276. (when (and org-insert-labeled-timestamps-at-point
  8277. (member what '(scheduled deadline)))
  8278. (insert
  8279. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8280. (org-insert-time-stamp time org-time-was-given
  8281. nil nil nil (list org-end-time-was-given))
  8282. (setq what nil))
  8283. (save-excursion
  8284. (save-restriction
  8285. (let (col list elt ts buffer-invisibility-spec)
  8286. (org-back-to-heading t)
  8287. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8288. (goto-char (match-end 1))
  8289. (setq col (current-column))
  8290. (goto-char (match-end 0))
  8291. (if (eobp) (insert "\n") (forward-char 1))
  8292. (if (and (not (looking-at outline-regexp))
  8293. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8294. "[^\r\n]*"))
  8295. (not (equal (match-string 1) org-clock-string)))
  8296. (narrow-to-region (match-beginning 0) (match-end 0))
  8297. (insert-before-markers "\n")
  8298. (backward-char 1)
  8299. (narrow-to-region (point) (point))
  8300. (and org-adapt-indentation (org-indent-to-column col)))
  8301. ;; Check if we have to remove something.
  8302. (setq list (cons what remove))
  8303. (while list
  8304. (setq elt (pop list))
  8305. (goto-char (point-min))
  8306. (when (or (and (eq elt 'scheduled)
  8307. (re-search-forward org-scheduled-time-regexp nil t))
  8308. (and (eq elt 'deadline)
  8309. (re-search-forward org-deadline-time-regexp nil t))
  8310. (and (eq elt 'closed)
  8311. (re-search-forward org-closed-time-regexp nil t)))
  8312. (replace-match "")
  8313. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8314. (if (looking-at " +") (replace-match ""))))
  8315. (goto-char (point-max))
  8316. (when what
  8317. (insert
  8318. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8319. (cond ((eq what 'scheduled) org-scheduled-string)
  8320. ((eq what 'deadline) org-deadline-string)
  8321. ((eq what 'closed) org-closed-string))
  8322. " ")
  8323. (setq ts (org-insert-time-stamp
  8324. time
  8325. (or org-time-was-given
  8326. (and (eq what 'closed) org-log-done-with-time))
  8327. (eq what 'closed)
  8328. nil nil (list org-end-time-was-given)))
  8329. (end-of-line 1))
  8330. (goto-char (point-min))
  8331. (widen)
  8332. (if (and (looking-at "[ \t]+\n")
  8333. (equal (char-before) ?\n))
  8334. (delete-region (1- (point)) (point-at-eol)))
  8335. ts)))))
  8336. (defvar org-log-note-marker (make-marker))
  8337. (defvar org-log-note-purpose nil)
  8338. (defvar org-log-note-state nil)
  8339. (defvar org-log-note-previous-state nil)
  8340. (defvar org-log-note-how nil)
  8341. (defvar org-log-note-extra nil)
  8342. (defvar org-log-note-window-configuration nil)
  8343. (defvar org-log-note-return-to (make-marker))
  8344. (defvar org-log-post-message nil
  8345. "Message to be displayed after a log note has been stored.
  8346. The auto-repeater uses this.")
  8347. (defun org-add-note ()
  8348. "Add a note to the current entry.
  8349. This is done in the same way as adding a state change note."
  8350. (interactive)
  8351. (org-add-log-setup 'note nil nil 'findpos nil))
  8352. (defvar org-property-end-re)
  8353. (defun org-add-log-setup (&optional purpose state prev-state
  8354. findpos how &optional extra)
  8355. "Set up the post command hook to take a note.
  8356. If this is about to TODO state change, the new state is expected in STATE.
  8357. When FINDPOS is non-nil, find the correct position for the note in
  8358. the current entry. If not, assume that it can be inserted at point.
  8359. HOW is an indicator what kind of note should be created.
  8360. EXTRA is additional text that will be inserted into the notes buffer."
  8361. (let ((drawer (cond ((stringp org-log-state-notes-into-drawer)
  8362. org-log-state-notes-into-drawer)
  8363. (org-log-state-notes-into-drawer "LOGBOOK")
  8364. (t nil))))
  8365. (save-restriction
  8366. (save-excursion
  8367. (when findpos
  8368. (org-back-to-heading t)
  8369. (narrow-to-region (point) (save-excursion
  8370. (outline-next-heading) (point)))
  8371. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8372. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8373. "[^\r\n]*\\)?"))
  8374. (goto-char (match-end 0))
  8375. (cond
  8376. (drawer
  8377. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8378. nil t)
  8379. (goto-char (match-end 0))
  8380. (insert "\n:" drawer ":\n:END:")
  8381. (beginning-of-line 0)
  8382. (org-indent-line-function)
  8383. (beginning-of-line 2)
  8384. (org-indent-line-function)
  8385. (end-of-line 0)))
  8386. ((and org-log-state-notes-insert-after-drawers
  8387. (save-excursion
  8388. (forward-line) (looking-at org-drawer-regexp)))
  8389. (forward-line)
  8390. (while (looking-at org-drawer-regexp)
  8391. (goto-char (match-end 0))
  8392. (re-search-forward org-property-end-re (point-max) t)
  8393. (forward-line))
  8394. (forward-line -1)))
  8395. (unless org-log-states-order-reversed
  8396. (and (= (char-after) ?\n) (forward-char 1))
  8397. (org-skip-over-state-notes)
  8398. (skip-chars-backward " \t\n\r")))
  8399. (move-marker org-log-note-marker (point))
  8400. (setq org-log-note-purpose purpose
  8401. org-log-note-state state
  8402. org-log-note-previous-state prev-state
  8403. org-log-note-how how
  8404. org-log-note-extra extra)
  8405. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8406. (defun org-skip-over-state-notes ()
  8407. "Skip past the list of State notes in an entry."
  8408. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8409. (while (looking-at "[ \t]*- State")
  8410. (condition-case nil
  8411. (org-next-item)
  8412. (error (org-end-of-item)))))
  8413. (defun org-add-log-note (&optional purpose)
  8414. "Pop up a window for taking a note, and add this note later at point."
  8415. (remove-hook 'post-command-hook 'org-add-log-note)
  8416. (setq org-log-note-window-configuration (current-window-configuration))
  8417. (delete-other-windows)
  8418. (move-marker org-log-note-return-to (point))
  8419. (switch-to-buffer (marker-buffer org-log-note-marker))
  8420. (goto-char org-log-note-marker)
  8421. (org-switch-to-buffer-other-window "*Org Note*")
  8422. (erase-buffer)
  8423. (if (memq org-log-note-how '(time state))
  8424. (let (current-prefix-arg) (org-store-log-note))
  8425. (let ((org-inhibit-startup t)) (org-mode))
  8426. (insert (format "# Insert note for %s.
  8427. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8428. (cond
  8429. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8430. ((eq org-log-note-purpose 'done) "closed todo item")
  8431. ((eq org-log-note-purpose 'state)
  8432. (format "state change from \"%s\" to \"%s\""
  8433. (or org-log-note-previous-state "")
  8434. (or org-log-note-state "")))
  8435. ((eq org-log-note-purpose 'note)
  8436. "this entry")
  8437. (t (error "This should not happen")))))
  8438. (if org-log-note-extra (insert org-log-note-extra))
  8439. (org-set-local 'org-finish-function 'org-store-log-note)))
  8440. (defvar org-note-abort nil) ; dynamically scoped
  8441. (defun org-store-log-note ()
  8442. "Finish taking a log note, and insert it to where it belongs."
  8443. (let ((txt (buffer-string))
  8444. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8445. lines ind)
  8446. (kill-buffer (current-buffer))
  8447. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8448. (setq txt (replace-match "" t t txt)))
  8449. (if (string-match "\\s-+\\'" txt)
  8450. (setq txt (replace-match "" t t txt)))
  8451. (setq lines (org-split-string txt "\n"))
  8452. (when (and note (string-match "\\S-" note))
  8453. (setq note
  8454. (org-replace-escapes
  8455. note
  8456. (list (cons "%u" (user-login-name))
  8457. (cons "%U" user-full-name)
  8458. (cons "%t" (format-time-string
  8459. (org-time-stamp-format 'long 'inactive)
  8460. (current-time)))
  8461. (cons "%s" (if org-log-note-state
  8462. (concat "\"" org-log-note-state "\"")
  8463. ""))
  8464. (cons "%S" (if org-log-note-previous-state
  8465. (concat "\"" org-log-note-previous-state "\"")
  8466. "\"\"")))))
  8467. (if lines (setq note (concat note " \\\\")))
  8468. (push note lines))
  8469. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8470. (when lines
  8471. (save-excursion
  8472. (set-buffer (marker-buffer org-log-note-marker))
  8473. (save-excursion
  8474. (goto-char org-log-note-marker)
  8475. (move-marker org-log-note-marker nil)
  8476. (end-of-line 1)
  8477. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8478. (insert "- " (pop lines))
  8479. (org-indent-line-function)
  8480. (beginning-of-line 1)
  8481. (looking-at "[ \t]*")
  8482. (setq ind (concat (match-string 0) " "))
  8483. (end-of-line 1)
  8484. (while lines (insert "\n" ind (pop lines)))
  8485. (message "Note stored")
  8486. (org-back-to-heading t)
  8487. (org-cycle-hide-drawers 'children)))))
  8488. (set-window-configuration org-log-note-window-configuration)
  8489. (with-current-buffer (marker-buffer org-log-note-return-to)
  8490. (goto-char org-log-note-return-to))
  8491. (move-marker org-log-note-return-to nil)
  8492. (and org-log-post-message (message "%s" org-log-post-message)))
  8493. (defun org-sparse-tree (&optional arg)
  8494. "Create a sparse tree, prompt for the details.
  8495. This command can create sparse trees. You first need to select the type
  8496. of match used to create the tree:
  8497. t Show entries with a specific TODO keyword.
  8498. T Show entries selected by a tags match.
  8499. p Enter a property name and its value (both with completion on existing
  8500. names/values) and show entries with that property.
  8501. r Show entries matching a regular expression
  8502. d Show deadlines due within `org-deadline-warning-days'."
  8503. (interactive "P")
  8504. (let (ans kwd value)
  8505. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8506. (setq ans (read-char-exclusive))
  8507. (cond
  8508. ((equal ans ?d)
  8509. (call-interactively 'org-check-deadlines))
  8510. ((equal ans ?b)
  8511. (call-interactively 'org-check-before-date))
  8512. ((equal ans ?t)
  8513. (org-show-todo-tree '(4)))
  8514. ((equal ans ?T)
  8515. (call-interactively 'org-tags-sparse-tree))
  8516. ((member ans '(?p ?P))
  8517. (setq kwd (org-ido-completing-read "Property: "
  8518. (mapcar 'list (org-buffer-property-keys))))
  8519. (setq value (org-ido-completing-read "Value: "
  8520. (mapcar 'list (org-property-values kwd))))
  8521. (unless (string-match "\\`{.*}\\'" value)
  8522. (setq value (concat "\"" value "\"")))
  8523. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8524. ((member ans '(?r ?R ?/))
  8525. (call-interactively 'org-occur))
  8526. (t (error "No such sparse tree command \"%c\"" ans)))))
  8527. (defvar org-occur-highlights nil
  8528. "List of overlays used for occur matches.")
  8529. (make-variable-buffer-local 'org-occur-highlights)
  8530. (defvar org-occur-parameters nil
  8531. "Parameters of the active org-occur calls.
  8532. This is a list, each call to org-occur pushes as cons cell,
  8533. containing the regular expression and the callback, onto the list.
  8534. The list can contain several entries if `org-occur' has been called
  8535. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8536. will only contain one set of parameters. When the highlights are
  8537. removed (for example with `C-c C-c', or with the next edit (depending
  8538. on `org-remove-highlights-with-change'), this variable is emptied
  8539. as well.")
  8540. (make-variable-buffer-local 'org-occur-parameters)
  8541. (defun org-occur (regexp &optional keep-previous callback)
  8542. "Make a compact tree which shows all matches of REGEXP.
  8543. The tree will show the lines where the regexp matches, and all higher
  8544. headlines above the match. It will also show the heading after the match,
  8545. to make sure editing the matching entry is easy.
  8546. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8547. call to `org-occur' will be kept, to allow stacking of calls to this
  8548. command.
  8549. If CALLBACK is non-nil, it is a function which is called to confirm
  8550. that the match should indeed be shown."
  8551. (interactive "sRegexp: \nP")
  8552. (unless keep-previous
  8553. (org-remove-occur-highlights nil nil t))
  8554. (push (cons regexp callback) org-occur-parameters)
  8555. (let ((cnt 0))
  8556. (save-excursion
  8557. (goto-char (point-min))
  8558. (if (or (not keep-previous) ; do not want to keep
  8559. (not org-occur-highlights)) ; no previous matches
  8560. ;; hide everything
  8561. (org-overview))
  8562. (while (re-search-forward regexp nil t)
  8563. (when (or (not callback)
  8564. (save-match-data (funcall callback)))
  8565. (setq cnt (1+ cnt))
  8566. (when org-highlight-sparse-tree-matches
  8567. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8568. (org-show-context 'occur-tree))))
  8569. (when org-remove-highlights-with-change
  8570. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8571. nil 'local))
  8572. (unless org-sparse-tree-open-archived-trees
  8573. (org-hide-archived-subtrees (point-min) (point-max)))
  8574. (run-hooks 'org-occur-hook)
  8575. (if (interactive-p)
  8576. (message "%d match(es) for regexp %s" cnt regexp))
  8577. cnt))
  8578. (defun org-show-context (&optional key)
  8579. "Make sure point and context and visible.
  8580. How much context is shown depends upon the variables
  8581. `org-show-hierarchy-above', `org-show-following-heading'. and
  8582. `org-show-siblings'."
  8583. (let ((heading-p (org-on-heading-p t))
  8584. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8585. (following-p (org-get-alist-option org-show-following-heading key))
  8586. (entry-p (org-get-alist-option org-show-entry-below key))
  8587. (siblings-p (org-get-alist-option org-show-siblings key)))
  8588. (catch 'exit
  8589. ;; Show heading or entry text
  8590. (if (and heading-p (not entry-p))
  8591. (org-flag-heading nil) ; only show the heading
  8592. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8593. (org-show-hidden-entry))) ; show entire entry
  8594. (when following-p
  8595. ;; Show next sibling, or heading below text
  8596. (save-excursion
  8597. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8598. (org-flag-heading nil))))
  8599. (when siblings-p (org-show-siblings))
  8600. (when hierarchy-p
  8601. ;; show all higher headings, possibly with siblings
  8602. (save-excursion
  8603. (while (and (condition-case nil
  8604. (progn (org-up-heading-all 1) t)
  8605. (error nil))
  8606. (not (bobp)))
  8607. (org-flag-heading nil)
  8608. (when siblings-p (org-show-siblings))))))))
  8609. (defun org-reveal (&optional siblings)
  8610. "Show current entry, hierarchy above it, and the following headline.
  8611. This can be used to show a consistent set of context around locations
  8612. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8613. not t for the search context.
  8614. With optional argument SIBLINGS, on each level of the hierarchy all
  8615. siblings are shown. This repairs the tree structure to what it would
  8616. look like when opened with hierarchical calls to `org-cycle'."
  8617. (interactive "P")
  8618. (let ((org-show-hierarchy-above t)
  8619. (org-show-following-heading t)
  8620. (org-show-siblings (if siblings t org-show-siblings)))
  8621. (org-show-context nil)))
  8622. (defun org-highlight-new-match (beg end)
  8623. "Highlight from BEG to END and mark the highlight is an occur headline."
  8624. (let ((ov (org-make-overlay beg end)))
  8625. (org-overlay-put ov 'face 'secondary-selection)
  8626. (push ov org-occur-highlights)))
  8627. (defun org-remove-occur-highlights (&optional beg end noremove)
  8628. "Remove the occur highlights from the buffer.
  8629. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8630. from the `before-change-functions' in the current buffer."
  8631. (interactive)
  8632. (unless org-inhibit-highlight-removal
  8633. (mapc 'org-delete-overlay org-occur-highlights)
  8634. (setq org-occur-highlights nil)
  8635. (setq org-occur-parameters nil)
  8636. (unless noremove
  8637. (remove-hook 'before-change-functions
  8638. 'org-remove-occur-highlights 'local))))
  8639. ;;;; Priorities
  8640. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8641. "Regular expression matching the priority indicator.")
  8642. (defvar org-remove-priority-next-time nil)
  8643. (defun org-priority-up ()
  8644. "Increase the priority of the current item."
  8645. (interactive)
  8646. (org-priority 'up))
  8647. (defun org-priority-down ()
  8648. "Decrease the priority of the current item."
  8649. (interactive)
  8650. (org-priority 'down))
  8651. (defun org-priority (&optional action)
  8652. "Change the priority of an item by ARG.
  8653. ACTION can be `set', `up', `down', or a character."
  8654. (interactive)
  8655. (setq action (or action 'set))
  8656. (let (current new news have remove)
  8657. (save-excursion
  8658. (org-back-to-heading)
  8659. (if (looking-at org-priority-regexp)
  8660. (setq current (string-to-char (match-string 2))
  8661. have t)
  8662. (setq current org-default-priority))
  8663. (cond
  8664. ((or (eq action 'set)
  8665. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8666. (if (not (eq action 'set))
  8667. (setq new action)
  8668. (message "Priority %c-%c, SPC to remove: "
  8669. org-highest-priority org-lowest-priority)
  8670. (setq new (read-char-exclusive)))
  8671. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8672. (= (upcase org-lowest-priority) org-lowest-priority))
  8673. (setq new (upcase new)))
  8674. (cond ((equal new ?\ ) (setq remove t))
  8675. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8676. (error "Priority must be between `%c' and `%c'"
  8677. org-highest-priority org-lowest-priority))))
  8678. ((eq action 'up)
  8679. (if (and (not have) (eq last-command this-command))
  8680. (setq new org-lowest-priority)
  8681. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8682. org-default-priority (1- current)))))
  8683. ((eq action 'down)
  8684. (if (and (not have) (eq last-command this-command))
  8685. (setq new org-highest-priority)
  8686. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8687. org-default-priority (1+ current)))))
  8688. (t (error "Invalid action")))
  8689. (if (or (< (upcase new) org-highest-priority)
  8690. (> (upcase new) org-lowest-priority))
  8691. (setq remove t))
  8692. (setq news (format "%c" new))
  8693. (if have
  8694. (if remove
  8695. (replace-match "" t t nil 1)
  8696. (replace-match news t t nil 2))
  8697. (if remove
  8698. (error "No priority cookie found in line")
  8699. (looking-at org-todo-line-regexp)
  8700. (if (match-end 2)
  8701. (progn
  8702. (goto-char (match-end 2))
  8703. (insert " [#" news "]"))
  8704. (goto-char (match-beginning 3))
  8705. (insert "[#" news "] ")))))
  8706. (org-preserve-lc (org-set-tags nil 'align))
  8707. (if remove
  8708. (message "Priority removed")
  8709. (message "Priority of current item set to %s" news))))
  8710. (defun org-get-priority (s)
  8711. "Find priority cookie and return priority."
  8712. (save-match-data
  8713. (if (not (string-match org-priority-regexp s))
  8714. (* 1000 (- org-lowest-priority org-default-priority))
  8715. (* 1000 (- org-lowest-priority
  8716. (string-to-char (match-string 2 s)))))))
  8717. ;;;; Tags
  8718. (defvar org-agenda-archives-mode)
  8719. (defun org-scan-tags (action matcher &optional todo-only)
  8720. "Scan headline tags with inheritance and produce output ACTION.
  8721. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8722. or `agenda' to produce an entry list for an agenda view. It can also be
  8723. a Lisp form or a function that should be called at each matched headline, in
  8724. this case the return value is a list of all return values from these calls.
  8725. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8726. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8727. only lines with a TODO keyword are included in the output."
  8728. (require 'org-agenda)
  8729. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8730. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8731. (org-re
  8732. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8733. (props (list 'face 'default
  8734. 'done-face 'org-done
  8735. 'undone-face 'default
  8736. 'mouse-face 'highlight
  8737. 'org-not-done-regexp org-not-done-regexp
  8738. 'org-todo-regexp org-todo-regexp
  8739. 'keymap org-agenda-keymap
  8740. 'help-echo
  8741. (format "mouse-2 or RET jump to org file %s"
  8742. (abbreviate-file-name
  8743. (or (buffer-file-name (buffer-base-buffer))
  8744. (buffer-name (buffer-base-buffer)))))))
  8745. (case-fold-search nil)
  8746. lspos tags tags-list
  8747. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8748. (llast 0) rtn rtn1 level category i txt
  8749. todo marker entry priority)
  8750. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8751. (setq action (list 'lambda nil action)))
  8752. (save-excursion
  8753. (goto-char (point-min))
  8754. (when (eq action 'sparse-tree)
  8755. (org-overview)
  8756. (org-remove-occur-highlights))
  8757. (while (re-search-forward re nil t)
  8758. (catch :skip
  8759. (setq todo (if (match-end 1) (match-string 2))
  8760. tags (if (match-end 4) (match-string 4)))
  8761. (goto-char (setq lspos (1+ (match-beginning 0))))
  8762. (setq level (org-reduced-level (funcall outline-level))
  8763. category (org-get-category))
  8764. (setq i llast llast level)
  8765. ;; remove tag lists from same and sublevels
  8766. (while (>= i level)
  8767. (when (setq entry (assoc i tags-alist))
  8768. (setq tags-alist (delete entry tags-alist)))
  8769. (setq i (1- i)))
  8770. ;; add the next tags
  8771. (when tags
  8772. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8773. tags-alist
  8774. (cons (cons level tags) tags-alist)))
  8775. ;; compile tags for current headline
  8776. (setq tags-list
  8777. (if org-use-tag-inheritance
  8778. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8779. tags))
  8780. (when org-use-tag-inheritance
  8781. (setcdr (car tags-alist)
  8782. (mapcar (lambda (x)
  8783. (setq x (copy-sequence x))
  8784. (org-add-prop-inherited x))
  8785. (cdar tags-alist))))
  8786. (when (and tags org-use-tag-inheritance
  8787. (not (eq t org-use-tag-inheritance)))
  8788. ;; selective inheritance, remove uninherited ones
  8789. (setcdr (car tags-alist)
  8790. (org-remove-uniherited-tags (cdar tags-alist))))
  8791. (when (and (or (not todo-only)
  8792. (and (member todo org-not-done-keywords)
  8793. (or (not org-agenda-tags-todo-honor-ignore-options)
  8794. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8795. (let ((case-fold-search t)) (eval matcher))
  8796. (or
  8797. (not (member org-archive-tag tags-list))
  8798. ;; we have an archive tag, should we use this anyway?
  8799. (or (not org-agenda-skip-archived-trees)
  8800. (and (eq action 'agenda) org-agenda-archives-mode))))
  8801. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8802. ;; select this headline
  8803. (cond
  8804. ((eq action 'sparse-tree)
  8805. (and org-highlight-sparse-tree-matches
  8806. (org-get-heading) (match-end 0)
  8807. (org-highlight-new-match
  8808. (match-beginning 0) (match-beginning 1)))
  8809. (org-show-context 'tags-tree))
  8810. ((eq action 'agenda)
  8811. (setq txt (org-format-agenda-item
  8812. ""
  8813. (concat
  8814. (if org-tags-match-list-sublevels
  8815. (make-string (1- level) ?.) "")
  8816. (org-get-heading))
  8817. category (org-get-tags-at))
  8818. priority (org-get-priority txt))
  8819. (goto-char lspos)
  8820. (setq marker (org-agenda-new-marker))
  8821. (org-add-props txt props
  8822. 'org-marker marker 'org-hd-marker marker 'org-category category
  8823. 'priority priority 'type "tagsmatch")
  8824. (push txt rtn))
  8825. ((functionp action)
  8826. (save-excursion
  8827. (setq rtn1 (funcall action))
  8828. (push rtn1 rtn))
  8829. (goto-char (point-at-eol)))
  8830. (t (error "Invalid action")))
  8831. ;; if we are to skip sublevels, jump to end of subtree
  8832. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8833. (when (and (eq action 'sparse-tree)
  8834. (not org-sparse-tree-open-archived-trees))
  8835. (org-hide-archived-subtrees (point-min) (point-max)))
  8836. (nreverse rtn)))
  8837. (defun org-remove-uniherited-tags (tags)
  8838. "Remove all tags that are not inherited from the list TAGS."
  8839. (cond
  8840. ((eq org-use-tag-inheritance t)
  8841. (if org-tags-exclude-from-inheritance
  8842. (org-delete-all org-tags-exclude-from-inheritance tags)
  8843. tags))
  8844. ((not org-use-tag-inheritance) nil)
  8845. ((stringp org-use-tag-inheritance)
  8846. (delq nil (mapcar
  8847. (lambda (x)
  8848. (if (and (string-match org-use-tag-inheritance x)
  8849. (not (member x org-tags-exclude-from-inheritance)))
  8850. x nil))
  8851. tags)))
  8852. ((listp org-use-tag-inheritance)
  8853. (delq nil (mapcar
  8854. (lambda (x)
  8855. (if (member x org-use-tag-inheritance) x nil))
  8856. tags)))))
  8857. (defvar todo-only) ;; dynamically scoped
  8858. (defun org-tags-sparse-tree (&optional todo-only match)
  8859. "Create a sparse tree according to tags string MATCH.
  8860. MATCH can contain positive and negative selection of tags, like
  8861. \"+WORK+URGENT-WITHBOSS\".
  8862. If optional argument TODO-ONLY is non-nil, only select lines that are
  8863. also TODO lines."
  8864. (interactive "P")
  8865. (org-prepare-agenda-buffers (list (current-buffer)))
  8866. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8867. (defvar org-cached-props nil)
  8868. (defun org-cached-entry-get (pom property)
  8869. (if (or (eq t org-use-property-inheritance)
  8870. (and (stringp org-use-property-inheritance)
  8871. (string-match org-use-property-inheritance property))
  8872. (and (listp org-use-property-inheritance)
  8873. (member property org-use-property-inheritance)))
  8874. ;; Caching is not possible, check it directly
  8875. (org-entry-get pom property 'inherit)
  8876. ;; Get all properties, so that we can do complicated checks easily
  8877. (cdr (assoc property (or org-cached-props
  8878. (setq org-cached-props
  8879. (org-entry-properties pom)))))))
  8880. (defun org-global-tags-completion-table (&optional files)
  8881. "Return the list of all tags in all agenda buffer/files."
  8882. (save-excursion
  8883. (org-uniquify
  8884. (delq nil
  8885. (apply 'append
  8886. (mapcar
  8887. (lambda (file)
  8888. (set-buffer (find-file-noselect file))
  8889. (append (org-get-buffer-tags)
  8890. (mapcar (lambda (x) (if (stringp (car-safe x))
  8891. (list (car-safe x)) nil))
  8892. org-tag-alist)))
  8893. (if (and files (car files))
  8894. files
  8895. (org-agenda-files))))))))
  8896. (defun org-make-tags-matcher (match)
  8897. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8898. ;; todo-only is scoped dynamically into this function, and the function
  8899. ;; may change it if the matcher asks for it.
  8900. (unless match
  8901. ;; Get a new match request, with completion
  8902. (let ((org-last-tags-completion-table
  8903. (org-global-tags-completion-table)))
  8904. (setq match (org-completing-read
  8905. "Match: " 'org-tags-completion-function nil nil nil
  8906. 'org-tags-history))))
  8907. ;; Parse the string and create a lisp form
  8908. (let ((match0 match)
  8909. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8910. minus tag mm
  8911. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8912. orterms term orlist re-p str-p level-p level-op time-p
  8913. prop-p pn pv po cat-p gv rest)
  8914. (if (string-match "/+" match)
  8915. ;; match contains also a todo-matching request
  8916. (progn
  8917. (setq tagsmatch (substring match 0 (match-beginning 0))
  8918. todomatch (substring match (match-end 0)))
  8919. (if (string-match "^!" todomatch)
  8920. (setq todo-only t todomatch (substring todomatch 1)))
  8921. (if (string-match "^\\s-*$" todomatch)
  8922. (setq todomatch nil)))
  8923. ;; only matching tags
  8924. (setq tagsmatch match todomatch nil))
  8925. ;; Make the tags matcher
  8926. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8927. (setq tagsmatcher t)
  8928. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8929. (while (setq term (pop orterms))
  8930. (while (and (equal (substring term -1) "\\") orterms)
  8931. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8932. (while (string-match re term)
  8933. (setq rest (substring term (match-end 0))
  8934. minus (and (match-end 1)
  8935. (equal (match-string 1 term) "-"))
  8936. tag (match-string 2 term)
  8937. re-p (equal (string-to-char tag) ?{)
  8938. level-p (match-end 4)
  8939. prop-p (match-end 5)
  8940. mm (cond
  8941. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8942. (level-p
  8943. (setq level-op (org-op-to-function (match-string 3 term)))
  8944. `(,level-op level ,(string-to-number
  8945. (match-string 4 term))))
  8946. (prop-p
  8947. (setq pn (match-string 5 term)
  8948. po (match-string 6 term)
  8949. pv (match-string 7 term)
  8950. cat-p (equal pn "CATEGORY")
  8951. re-p (equal (string-to-char pv) ?{)
  8952. str-p (equal (string-to-char pv) ?\")
  8953. time-p (save-match-data
  8954. (string-match "^\"[[<].*[]>]\"$" pv))
  8955. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8956. (if time-p (setq pv (org-matcher-time pv)))
  8957. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8958. (cond
  8959. ((equal pn "CATEGORY")
  8960. (setq gv '(get-text-property (point) 'org-category)))
  8961. ((equal pn "TODO")
  8962. (setq gv 'todo))
  8963. (t
  8964. (setq gv `(org-cached-entry-get nil ,pn))))
  8965. (if re-p
  8966. (if (eq po 'org<>)
  8967. `(not (string-match ,pv (or ,gv "")))
  8968. `(string-match ,pv (or ,gv "")))
  8969. (if str-p
  8970. `(,po (or ,gv "") ,pv)
  8971. `(,po (string-to-number (or ,gv ""))
  8972. ,(string-to-number pv) ))))
  8973. (t `(member ,(downcase tag) tags-list)))
  8974. mm (if minus (list 'not mm) mm)
  8975. term rest)
  8976. (push mm tagsmatcher))
  8977. (push (if (> (length tagsmatcher) 1)
  8978. (cons 'and tagsmatcher)
  8979. (car tagsmatcher))
  8980. orlist)
  8981. (setq tagsmatcher nil))
  8982. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8983. (setq tagsmatcher
  8984. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8985. ;; Make the todo matcher
  8986. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8987. (setq todomatcher t)
  8988. (setq orterms (org-split-string todomatch "|") orlist nil)
  8989. (while (setq term (pop orterms))
  8990. (while (string-match re term)
  8991. (setq minus (and (match-end 1)
  8992. (equal (match-string 1 term) "-"))
  8993. kwd (match-string 2 term)
  8994. re-p (equal (string-to-char kwd) ?{)
  8995. term (substring term (match-end 0))
  8996. mm (if re-p
  8997. `(string-match ,(substring kwd 1 -1) todo)
  8998. (list 'equal 'todo kwd))
  8999. mm (if minus (list 'not mm) mm))
  9000. (push mm todomatcher))
  9001. (push (if (> (length todomatcher) 1)
  9002. (cons 'and todomatcher)
  9003. (car todomatcher))
  9004. orlist)
  9005. (setq todomatcher nil))
  9006. (setq todomatcher (if (> (length orlist) 1)
  9007. (cons 'or orlist) (car orlist))))
  9008. ;; Return the string and lisp forms of the matcher
  9009. (setq matcher (if todomatcher
  9010. (list 'and tagsmatcher todomatcher)
  9011. tagsmatcher))
  9012. (cons match0 matcher)))
  9013. (defun org-op-to-function (op &optional stringp)
  9014. "Turn an operator into the appropriate function."
  9015. (setq op
  9016. (cond
  9017. ((equal op "<" ) '(< string< org-time<))
  9018. ((equal op ">" ) '(> org-string> org-time>))
  9019. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9020. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9021. ((member op '("=" "==")) '(= string= org-time=))
  9022. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9023. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9024. (defun org<> (a b) (not (= a b)))
  9025. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9026. (defun org-string>= (a b) (not (string< a b)))
  9027. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9028. (defun org-string<> (a b) (not (string= a b)))
  9029. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9030. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9031. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9032. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9033. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9034. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9035. (defun org-2ft (s)
  9036. "Convert S to a floating point time.
  9037. If S is already a number, just return it. If it is a string, parse
  9038. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9039. (cond
  9040. ((numberp s) s)
  9041. ((stringp s)
  9042. (condition-case nil
  9043. (float-time (apply 'encode-time (org-parse-time-string s)))
  9044. (error 0.)))
  9045. (t 0.)))
  9046. (defun org-time-today ()
  9047. "Time in seconds today at 0:00.
  9048. Returns the float number of seconds since the beginning of the
  9049. epoch to the beginning of today (00:00)."
  9050. (float-time (apply 'encode-time
  9051. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9052. (defun org-matcher-time (s)
  9053. "Interpret a time comparison value."
  9054. (save-match-data
  9055. (cond
  9056. ((string= s "<now>") (float-time))
  9057. ((string= s "<today>") (org-time-today))
  9058. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9059. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9060. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9061. (+ (org-time-today)
  9062. (* (string-to-number (match-string 1 s))
  9063. (cdr (assoc (match-string 2 s)
  9064. '(("d" . 86400.0) ("w" . 604800.0)
  9065. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9066. (t (org-2ft s)))))
  9067. (defun org-match-any-p (re list)
  9068. "Does re match any element of list?"
  9069. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9070. (delq nil list))
  9071. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9072. (defvar org-tags-overlay (org-make-overlay 1 1))
  9073. (org-detach-overlay org-tags-overlay)
  9074. (defun org-get-local-tags-at (&optional pos)
  9075. "Get a list of tags defined in the current headline."
  9076. (org-get-tags-at pos 'local))
  9077. (defun org-get-local-tags ()
  9078. "Get a list of tags defined in the current headline."
  9079. (org-get-tags-at nil 'local))
  9080. (defun org-get-tags-at (&optional pos local)
  9081. "Get a list of all headline tags applicable at POS.
  9082. POS defaults to point. If tags are inherited, the list contains
  9083. the targets in the same sequence as the headlines appear, i.e.
  9084. the tags of the current headline come last.
  9085. When LOCAL is non-nil, only return tags from the current headline,
  9086. ignore inherited ones."
  9087. (interactive)
  9088. (let (tags ltags lastpos parent)
  9089. (save-excursion
  9090. (save-restriction
  9091. (widen)
  9092. (goto-char (or pos (point)))
  9093. (save-match-data
  9094. (catch 'done
  9095. (condition-case nil
  9096. (progn
  9097. (org-back-to-heading t)
  9098. (while (not (equal lastpos (point)))
  9099. (setq lastpos (point))
  9100. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9101. (setq ltags (org-split-string
  9102. (org-match-string-no-properties 1) ":"))
  9103. (when parent
  9104. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9105. (setq tags (append
  9106. (if parent
  9107. (org-remove-uniherited-tags ltags)
  9108. ltags)
  9109. tags)))
  9110. (or org-use-tag-inheritance (throw 'done t))
  9111. (if local (throw 'done t))
  9112. (org-up-heading-all 1)
  9113. (setq parent t)))
  9114. (error nil)))))
  9115. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9116. (defun org-add-prop-inherited (s)
  9117. (add-text-properties 0 (length s) '(inherited t) s)
  9118. s)
  9119. (defun org-toggle-tag (tag &optional onoff)
  9120. "Toggle the tag TAG for the current line.
  9121. If ONOFF is `on' or `off', don't toggle but set to this state."
  9122. (let (res current)
  9123. (save-excursion
  9124. (org-back-to-heading t)
  9125. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9126. (point-at-eol) t)
  9127. (progn
  9128. (setq current (match-string 1))
  9129. (replace-match ""))
  9130. (setq current ""))
  9131. (setq current (nreverse (org-split-string current ":")))
  9132. (cond
  9133. ((eq onoff 'on)
  9134. (setq res t)
  9135. (or (member tag current) (push tag current)))
  9136. ((eq onoff 'off)
  9137. (or (not (member tag current)) (setq current (delete tag current))))
  9138. (t (if (member tag current)
  9139. (setq current (delete tag current))
  9140. (setq res t)
  9141. (push tag current))))
  9142. (end-of-line 1)
  9143. (if current
  9144. (progn
  9145. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9146. (org-set-tags nil t))
  9147. (delete-horizontal-space))
  9148. (run-hooks 'org-after-tags-change-hook))
  9149. res))
  9150. (defun org-align-tags-here (to-col)
  9151. ;; Assumes that this is a headline
  9152. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9153. (beginning-of-line 1)
  9154. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9155. (< pos (match-beginning 2)))
  9156. (progn
  9157. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9158. (goto-char (match-beginning 1))
  9159. (insert " ")
  9160. (delete-region (point) (1+ (match-beginning 2)))
  9161. (setq ncol (max (1+ (current-column))
  9162. (1+ col)
  9163. (if (> to-col 0)
  9164. to-col
  9165. (- (abs to-col) tags-l))))
  9166. (setq p (point))
  9167. (insert (make-string (- ncol (current-column)) ?\ ))
  9168. (setq ncol (current-column))
  9169. (when indent-tabs-mode (tabify p (point-at-eol)))
  9170. (org-move-to-column (min ncol col) t))
  9171. (goto-char pos))))
  9172. (defun org-set-tags-command (&optional arg just-align)
  9173. "Call the set-tags command for the current entry."
  9174. (interactive "P")
  9175. (if (org-on-heading-p)
  9176. (org-set-tags arg just-align)
  9177. (save-excursion
  9178. (org-back-to-heading t)
  9179. (org-set-tags arg just-align))))
  9180. (defun org-set-tags (&optional arg just-align)
  9181. "Set the tags for the current headline.
  9182. With prefix ARG, realign all tags in headings in the current buffer."
  9183. (interactive "P")
  9184. (let* ((re (concat "^" outline-regexp))
  9185. (current (org-get-tags-string))
  9186. (col (current-column))
  9187. (org-setting-tags t)
  9188. table current-tags inherited-tags ; computed below when needed
  9189. tags p0 c0 c1 rpl)
  9190. (if arg
  9191. (save-excursion
  9192. (goto-char (point-min))
  9193. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9194. (while (re-search-forward re nil t)
  9195. (org-set-tags nil t)
  9196. (end-of-line 1)))
  9197. (message "All tags realigned to column %d" org-tags-column))
  9198. (if just-align
  9199. (setq tags current)
  9200. ;; Get a new set of tags from the user
  9201. (save-excursion
  9202. (setq table (or org-tag-alist (org-get-buffer-tags))
  9203. org-last-tags-completion-table table
  9204. current-tags (org-split-string current ":")
  9205. inherited-tags (nreverse
  9206. (nthcdr (length current-tags)
  9207. (nreverse (org-get-tags-at))))
  9208. tags
  9209. (if (or (eq t org-use-fast-tag-selection)
  9210. (and org-use-fast-tag-selection
  9211. (delq nil (mapcar 'cdr table))))
  9212. (org-fast-tag-selection
  9213. current-tags inherited-tags table
  9214. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9215. (let ((org-add-colon-after-tag-completion t))
  9216. (org-trim
  9217. (org-without-partial-completion
  9218. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9219. nil nil current 'org-tags-history)))))))
  9220. (while (string-match "[-+&]+" tags)
  9221. ;; No boolean logic, just a list
  9222. (setq tags (replace-match ":" t t tags))))
  9223. (if (string-match "\\`[\t ]*\\'" tags)
  9224. (setq tags "")
  9225. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9226. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9227. ;; Insert new tags at the correct column
  9228. (beginning-of-line 1)
  9229. (cond
  9230. ((and (equal current "") (equal tags "")))
  9231. ((re-search-forward
  9232. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9233. (point-at-eol) t)
  9234. (if (equal tags "")
  9235. (setq rpl "")
  9236. (goto-char (match-beginning 0))
  9237. (setq c0 (current-column) p0 (point)
  9238. c1 (max (1+ c0) (if (> org-tags-column 0)
  9239. org-tags-column
  9240. (- (- org-tags-column) (length tags))))
  9241. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9242. (replace-match rpl t t)
  9243. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9244. tags)
  9245. (t (error "Tags alignment failed")))
  9246. (org-move-to-column col)
  9247. (unless just-align
  9248. (run-hooks 'org-after-tags-change-hook)))))
  9249. (defun org-change-tag-in-region (beg end tag off)
  9250. "Add or remove TAG for each entry in the region.
  9251. This works in the agenda, and also in an org-mode buffer."
  9252. (interactive
  9253. (list (region-beginning) (region-end)
  9254. (let ((org-last-tags-completion-table
  9255. (if (org-mode-p)
  9256. (org-get-buffer-tags)
  9257. (org-global-tags-completion-table))))
  9258. (org-ido-completing-read
  9259. "Tag: " 'org-tags-completion-function nil nil nil
  9260. 'org-tags-history))
  9261. (progn
  9262. (message "[s]et or [r]emove? ")
  9263. (equal (read-char-exclusive) ?r))))
  9264. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9265. (let ((agendap (equal major-mode 'org-agenda-mode))
  9266. l1 l2 m buf pos newhead (cnt 0))
  9267. (goto-char end)
  9268. (setq l2 (1- (org-current-line)))
  9269. (goto-char beg)
  9270. (setq l1 (org-current-line))
  9271. (loop for l from l1 to l2 do
  9272. (goto-line l)
  9273. (setq m (get-text-property (point) 'org-hd-marker))
  9274. (when (or (and (org-mode-p) (org-on-heading-p))
  9275. (and agendap m))
  9276. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9277. pos (if agendap m (point)))
  9278. (with-current-buffer buf
  9279. (save-excursion
  9280. (save-restriction
  9281. (goto-char pos)
  9282. (setq cnt (1+ cnt))
  9283. (org-toggle-tag tag (if off 'off 'on))
  9284. (setq newhead (org-get-heading)))))
  9285. (and agendap (org-agenda-change-all-lines newhead m))))
  9286. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9287. (defun org-tags-completion-function (string predicate &optional flag)
  9288. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9289. (confirm (lambda (x) (stringp (car x)))))
  9290. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9291. (setq s1 (match-string 1 string)
  9292. s2 (match-string 2 string))
  9293. (setq s1 "" s2 string))
  9294. (cond
  9295. ((eq flag nil)
  9296. ;; try completion
  9297. (setq rtn (try-completion s2 ctable confirm))
  9298. (if (stringp rtn)
  9299. (setq rtn
  9300. (concat s1 s2 (substring rtn (length s2))
  9301. (if (and org-add-colon-after-tag-completion
  9302. (assoc rtn ctable))
  9303. ":" ""))))
  9304. rtn)
  9305. ((eq flag t)
  9306. ;; all-completions
  9307. (all-completions s2 ctable confirm)
  9308. )
  9309. ((eq flag 'lambda)
  9310. ;; exact match?
  9311. (assoc s2 ctable)))
  9312. ))
  9313. (defun org-fast-tag-insert (kwd tags face &optional end)
  9314. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9315. (insert (format "%-12s" (concat kwd ":"))
  9316. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9317. (or end "")))
  9318. (defun org-fast-tag-show-exit (flag)
  9319. (save-excursion
  9320. (goto-line 3)
  9321. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9322. (replace-match ""))
  9323. (when flag
  9324. (end-of-line 1)
  9325. (org-move-to-column (- (window-width) 19) t)
  9326. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9327. (defun org-set-current-tags-overlay (current prefix)
  9328. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9329. (if (featurep 'xemacs)
  9330. (org-overlay-display org-tags-overlay (concat prefix s)
  9331. 'secondary-selection)
  9332. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9333. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9334. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9335. "Fast tag selection with single keys.
  9336. CURRENT is the current list of tags in the headline, INHERITED is the
  9337. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9338. possibly with grouping information. TODO-TABLE is a similar table with
  9339. TODO keywords, should these have keys assigned to them.
  9340. If the keys are nil, a-z are automatically assigned.
  9341. Returns the new tags string, or nil to not change the current settings."
  9342. (let* ((fulltable (append table todo-table))
  9343. (maxlen (apply 'max (mapcar
  9344. (lambda (x)
  9345. (if (stringp (car x)) (string-width (car x)) 0))
  9346. fulltable)))
  9347. (buf (current-buffer))
  9348. (expert (eq org-fast-tag-selection-single-key 'expert))
  9349. (buffer-tags nil)
  9350. (fwidth (+ maxlen 3 1 3))
  9351. (ncol (/ (- (window-width) 4) fwidth))
  9352. (i-face 'org-done)
  9353. (c-face 'org-todo)
  9354. tg cnt e c char c1 c2 ntable tbl rtn
  9355. ov-start ov-end ov-prefix
  9356. (exit-after-next org-fast-tag-selection-single-key)
  9357. (done-keywords org-done-keywords)
  9358. groups ingroup)
  9359. (save-excursion
  9360. (beginning-of-line 1)
  9361. (if (looking-at
  9362. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9363. (setq ov-start (match-beginning 1)
  9364. ov-end (match-end 1)
  9365. ov-prefix "")
  9366. (setq ov-start (1- (point-at-eol))
  9367. ov-end (1+ ov-start))
  9368. (skip-chars-forward "^\n\r")
  9369. (setq ov-prefix
  9370. (concat
  9371. (buffer-substring (1- (point)) (point))
  9372. (if (> (current-column) org-tags-column)
  9373. " "
  9374. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9375. (org-move-overlay org-tags-overlay ov-start ov-end)
  9376. (save-window-excursion
  9377. (if expert
  9378. (set-buffer (get-buffer-create " *Org tags*"))
  9379. (delete-other-windows)
  9380. (split-window-vertically)
  9381. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9382. (erase-buffer)
  9383. (org-set-local 'org-done-keywords done-keywords)
  9384. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9385. (org-fast-tag-insert "Current" current c-face "\n\n")
  9386. (org-fast-tag-show-exit exit-after-next)
  9387. (org-set-current-tags-overlay current ov-prefix)
  9388. (setq tbl fulltable char ?a cnt 0)
  9389. (while (setq e (pop tbl))
  9390. (cond
  9391. ((equal e '(:startgroup))
  9392. (push '() groups) (setq ingroup t)
  9393. (when (not (= cnt 0))
  9394. (setq cnt 0)
  9395. (insert "\n"))
  9396. (insert "{ "))
  9397. ((equal e '(:endgroup))
  9398. (setq ingroup nil cnt 0)
  9399. (insert "}\n"))
  9400. (t
  9401. (setq tg (car e) c2 nil)
  9402. (if (cdr e)
  9403. (setq c (cdr e))
  9404. ;; automatically assign a character.
  9405. (setq c1 (string-to-char
  9406. (downcase (substring
  9407. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9408. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9409. (while (or (rassoc char ntable) (rassoc char table))
  9410. (setq char (1+ char)))
  9411. (setq c2 c1))
  9412. (setq c (or c2 char)))
  9413. (if ingroup (push tg (car groups)))
  9414. (setq tg (org-add-props tg nil 'face
  9415. (cond
  9416. ((not (assoc tg table))
  9417. (org-get-todo-face tg))
  9418. ((member tg current) c-face)
  9419. ((member tg inherited) i-face)
  9420. (t nil))))
  9421. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9422. (insert "[" c "] " tg (make-string
  9423. (- fwidth 4 (length tg)) ?\ ))
  9424. (push (cons tg c) ntable)
  9425. (when (= (setq cnt (1+ cnt)) ncol)
  9426. (insert "\n")
  9427. (if ingroup (insert " "))
  9428. (setq cnt 0)))))
  9429. (setq ntable (nreverse ntable))
  9430. (insert "\n")
  9431. (goto-char (point-min))
  9432. (if (not expert) (org-fit-window-to-buffer))
  9433. (setq rtn
  9434. (catch 'exit
  9435. (while t
  9436. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9437. (if groups " [!] no groups" " [!]groups")
  9438. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9439. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9440. (cond
  9441. ((= c ?\r) (throw 'exit t))
  9442. ((= c ?!)
  9443. (setq groups (not groups))
  9444. (goto-char (point-min))
  9445. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9446. ((= c ?\C-c)
  9447. (if (not expert)
  9448. (org-fast-tag-show-exit
  9449. (setq exit-after-next (not exit-after-next)))
  9450. (setq expert nil)
  9451. (delete-other-windows)
  9452. (split-window-vertically)
  9453. (org-switch-to-buffer-other-window " *Org tags*")
  9454. (org-fit-window-to-buffer)))
  9455. ((or (= c ?\C-g)
  9456. (and (= c ?q) (not (rassoc c ntable))))
  9457. (org-detach-overlay org-tags-overlay)
  9458. (setq quit-flag t))
  9459. ((= c ?\ )
  9460. (setq current nil)
  9461. (if exit-after-next (setq exit-after-next 'now)))
  9462. ((= c ?\t)
  9463. (condition-case nil
  9464. (setq tg (org-ido-completing-read
  9465. "Tag: "
  9466. (or buffer-tags
  9467. (with-current-buffer buf
  9468. (org-get-buffer-tags)))))
  9469. (quit (setq tg "")))
  9470. (when (string-match "\\S-" tg)
  9471. (add-to-list 'buffer-tags (list tg))
  9472. (if (member tg current)
  9473. (setq current (delete tg current))
  9474. (push tg current)))
  9475. (if exit-after-next (setq exit-after-next 'now)))
  9476. ((setq e (rassoc c todo-table) tg (car e))
  9477. (with-current-buffer buf
  9478. (save-excursion (org-todo tg)))
  9479. (if exit-after-next (setq exit-after-next 'now)))
  9480. ((setq e (rassoc c ntable) tg (car e))
  9481. (if (member tg current)
  9482. (setq current (delete tg current))
  9483. (loop for g in groups do
  9484. (if (member tg g)
  9485. (mapc (lambda (x)
  9486. (setq current (delete x current)))
  9487. g)))
  9488. (push tg current))
  9489. (if exit-after-next (setq exit-after-next 'now))))
  9490. ;; Create a sorted list
  9491. (setq current
  9492. (sort current
  9493. (lambda (a b)
  9494. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9495. (if (eq exit-after-next 'now) (throw 'exit t))
  9496. (goto-char (point-min))
  9497. (beginning-of-line 2)
  9498. (delete-region (point) (point-at-eol))
  9499. (org-fast-tag-insert "Current" current c-face)
  9500. (org-set-current-tags-overlay current ov-prefix)
  9501. (while (re-search-forward
  9502. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9503. (setq tg (match-string 1))
  9504. (add-text-properties
  9505. (match-beginning 1) (match-end 1)
  9506. (list 'face
  9507. (cond
  9508. ((member tg current) c-face)
  9509. ((member tg inherited) i-face)
  9510. (t (get-text-property (match-beginning 1) 'face))))))
  9511. (goto-char (point-min)))))
  9512. (org-detach-overlay org-tags-overlay)
  9513. (if rtn
  9514. (mapconcat 'identity current ":")
  9515. nil))))
  9516. (defun org-get-tags-string ()
  9517. "Get the TAGS string in the current headline."
  9518. (unless (org-on-heading-p t)
  9519. (error "Not on a heading"))
  9520. (save-excursion
  9521. (beginning-of-line 1)
  9522. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9523. (org-match-string-no-properties 1)
  9524. "")))
  9525. (defun org-get-tags ()
  9526. "Get the list of tags specified in the current headline."
  9527. (org-split-string (org-get-tags-string) ":"))
  9528. (defun org-get-buffer-tags ()
  9529. "Get a table of all tags used in the buffer, for completion."
  9530. (let (tags)
  9531. (save-excursion
  9532. (goto-char (point-min))
  9533. (while (re-search-forward
  9534. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9535. (when (equal (char-after (point-at-bol 0)) ?*)
  9536. (mapc (lambda (x) (add-to-list 'tags x))
  9537. (org-split-string (org-match-string-no-properties 1) ":")))))
  9538. (mapcar 'list tags)))
  9539. ;;;; The mapping API
  9540. ;;;###autoload
  9541. (defun org-map-entries (func &optional match scope &rest skip)
  9542. "Call FUNC at each headline selected by MATCH in SCOPE.
  9543. FUNC is a function or a lisp form. The function will be called without
  9544. arguments, with the cursor positioned at the beginning of the headline.
  9545. The return values of all calls to the function will be collected and
  9546. returned as a list.
  9547. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9548. Only headlines that are matched by this query will be considered during
  9549. the iteration. When MATCH is nil or t, all headlines will be
  9550. visited by the iteration.
  9551. SCOPE determines the scope of this command. It can be any of:
  9552. nil The current buffer, respecting the restriction if any
  9553. tree The subtree started with the entry at point
  9554. file The current buffer, without restriction
  9555. file-with-archives
  9556. The current buffer, and any archives associated with it
  9557. agenda All agenda files
  9558. agenda-with-archives
  9559. All agenda files with any archive files associated with them
  9560. \(file1 file2 ...)
  9561. If this is a list, all files in the list will be scanned
  9562. The remaining args are treated as settings for the skipping facilities of
  9563. the scanner. The following items can be given here:
  9564. archive skip trees with the archive tag.
  9565. comment skip trees with the COMMENT keyword
  9566. function or Emacs Lisp form:
  9567. will be used as value for `org-agenda-skip-function', so whenever
  9568. the the function returns t, FUNC will not be called for that
  9569. entry and search will continue from the point where the
  9570. function leaves it."
  9571. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9572. (org-agenda-skip-archived-trees (memq 'archive skip))
  9573. (org-agenda-skip-comment-trees (memq 'comment skip))
  9574. (org-agenda-skip-function
  9575. (car (org-delete-all '(comment archive) skip)))
  9576. (org-tags-match-list-sublevels t)
  9577. matcher file res
  9578. org-todo-keywords-for-agenda
  9579. org-done-keywords-for-agenda
  9580. org-todo-keyword-alist-for-agenda
  9581. org-tag-alist-for-agenda)
  9582. (cond
  9583. ((eq match t) (setq matcher t))
  9584. ((eq match nil) (setq matcher t))
  9585. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9586. (save-excursion
  9587. (save-restriction
  9588. (when (eq scope 'tree)
  9589. (org-back-to-heading t)
  9590. (org-narrow-to-subtree)
  9591. (setq scope nil))
  9592. (if (not scope)
  9593. (progn
  9594. (org-prepare-agenda-buffers
  9595. (list (buffer-file-name (current-buffer))))
  9596. (setq res (org-scan-tags func matcher)))
  9597. ;; Get the right scope
  9598. (cond
  9599. ((and scope (listp scope) (symbolp (car scope)))
  9600. (setq scope (eval scope)))
  9601. ((eq scope 'agenda)
  9602. (setq scope (org-agenda-files t)))
  9603. ((eq scope 'agenda-with-archives)
  9604. (setq scope (org-agenda-files t))
  9605. (setq scope (org-add-archive-files scope)))
  9606. ((eq scope 'file)
  9607. (setq scope (list (buffer-file-name))))
  9608. ((eq scope 'file-with-archives)
  9609. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9610. (org-prepare-agenda-buffers scope)
  9611. (while (setq file (pop scope))
  9612. (with-current-buffer (org-find-base-buffer-visiting file)
  9613. (save-excursion
  9614. (save-restriction
  9615. (widen)
  9616. (goto-char (point-min))
  9617. (setq res (append res (org-scan-tags func matcher))))))))))
  9618. res))
  9619. ;;;; Properties
  9620. ;;; Setting and retrieving properties
  9621. (defconst org-special-properties
  9622. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9623. "TIMESTAMP" "TIMESTAMP_IA")
  9624. "The special properties valid in Org-mode.
  9625. These are properties that are not defined in the property drawer,
  9626. but in some other way.")
  9627. (defconst org-default-properties
  9628. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9629. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9630. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9631. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9632. "Some properties that are used by Org-mode for various purposes.
  9633. Being in this list makes sure that they are offered for completion.")
  9634. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9635. "Regular expression matching the first line of a property drawer.")
  9636. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9637. "Regular expression matching the first line of a property drawer.")
  9638. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9639. "Regular expression matching the first line of a property drawer.")
  9640. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9641. "Regular expression matching the first line of a property drawer.")
  9642. (defconst org-property-drawer-re
  9643. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9644. org-property-end-re "\\)\n?")
  9645. "Matches an entire property drawer.")
  9646. (defconst org-clock-drawer-re
  9647. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9648. org-property-end-re "\\)\n?")
  9649. "Matches an entire clock drawer.")
  9650. (defun org-property-action ()
  9651. "Do an action on properties."
  9652. (interactive)
  9653. (let (c)
  9654. (org-at-property-p)
  9655. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9656. (setq c (read-char-exclusive))
  9657. (cond
  9658. ((equal c ?s)
  9659. (call-interactively 'org-set-property))
  9660. ((equal c ?d)
  9661. (call-interactively 'org-delete-property))
  9662. ((equal c ?D)
  9663. (call-interactively 'org-delete-property-globally))
  9664. ((equal c ?c)
  9665. (call-interactively 'org-compute-property-at-point))
  9666. (t (error "No such property action %c" c)))))
  9667. (defun org-at-property-p ()
  9668. "Is the cursor in a property line?"
  9669. ;; FIXME: Does not check if we are actually in the drawer.
  9670. ;; FIXME: also returns true on any drawers.....
  9671. ;; This is used by C-c C-c for property action.
  9672. (save-excursion
  9673. (beginning-of-line 1)
  9674. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9675. (defun org-get-property-block (&optional beg end force)
  9676. "Return the (beg . end) range of the body of the property drawer.
  9677. BEG and END can be beginning and end of subtree, if not given
  9678. they will be found.
  9679. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9680. (catch 'exit
  9681. (save-excursion
  9682. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9683. (end (or end (progn (outline-next-heading) (point)))))
  9684. (goto-char beg)
  9685. (if (re-search-forward org-property-start-re end t)
  9686. (setq beg (1+ (match-end 0)))
  9687. (if force
  9688. (save-excursion
  9689. (org-insert-property-drawer)
  9690. (setq end (progn (outline-next-heading) (point))))
  9691. (throw 'exit nil))
  9692. (goto-char beg)
  9693. (if (re-search-forward org-property-start-re end t)
  9694. (setq beg (1+ (match-end 0)))))
  9695. (if (re-search-forward org-property-end-re end t)
  9696. (setq end (match-beginning 0))
  9697. (or force (throw 'exit nil))
  9698. (goto-char beg)
  9699. (setq end beg)
  9700. (org-indent-line-function)
  9701. (insert ":END:\n"))
  9702. (cons beg end)))))
  9703. (defun org-entry-properties (&optional pom which)
  9704. "Get all properties of the entry at point-or-marker POM.
  9705. This includes the TODO keyword, the tags, time strings for deadline,
  9706. scheduled, and clocking, and any additional properties defined in the
  9707. entry. The return value is an alist, keys may occur multiple times
  9708. if the property key was used several times.
  9709. POM may also be nil, in which case the current entry is used.
  9710. If WHICH is nil or `all', get all properties. If WHICH is
  9711. `special' or `standard', only get that subclass."
  9712. (setq which (or which 'all))
  9713. (org-with-point-at pom
  9714. (let ((clockstr (substring org-clock-string 0 -1))
  9715. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9716. beg end range props sum-props key value string clocksum)
  9717. (save-excursion
  9718. (when (condition-case nil
  9719. (and (org-mode-p) (org-back-to-heading t))
  9720. (error nil))
  9721. (setq beg (point))
  9722. (setq sum-props (get-text-property (point) 'org-summaries))
  9723. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9724. (outline-next-heading)
  9725. (setq end (point))
  9726. (when (memq which '(all special))
  9727. ;; Get the special properties, like TODO and tags
  9728. (goto-char beg)
  9729. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9730. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9731. (when (looking-at org-priority-regexp)
  9732. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9733. (when (and (setq value (org-get-tags-string))
  9734. (string-match "\\S-" value))
  9735. (push (cons "TAGS" value) props))
  9736. (when (setq value (org-get-tags-at))
  9737. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9738. props))
  9739. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9740. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9741. string (if (equal key clockstr)
  9742. (org-no-properties
  9743. (org-trim
  9744. (buffer-substring
  9745. (match-beginning 3) (goto-char (point-at-eol)))))
  9746. (substring (org-match-string-no-properties 3) 1 -1)))
  9747. (unless key
  9748. (if (= (char-after (match-beginning 3)) ?\[)
  9749. (setq key "TIMESTAMP_IA")
  9750. (setq key "TIMESTAMP")))
  9751. (when (or (equal key clockstr) (not (assoc key props)))
  9752. (push (cons key string) props)))
  9753. )
  9754. (when (memq which '(all standard))
  9755. ;; Get the standard properties, like :PORP: ...
  9756. (setq range (org-get-property-block beg end))
  9757. (when range
  9758. (goto-char (car range))
  9759. (while (re-search-forward
  9760. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9761. (cdr range) t)
  9762. (setq key (org-match-string-no-properties 1)
  9763. value (org-trim (or (org-match-string-no-properties 2) "")))
  9764. (unless (member key excluded)
  9765. (push (cons key (or value "")) props)))))
  9766. (if clocksum
  9767. (push (cons "CLOCKSUM"
  9768. (org-columns-number-to-string (/ (float clocksum) 60.)
  9769. 'add_times))
  9770. props))
  9771. (unless (assoc "CATEGORY" props)
  9772. (setq value (or (org-get-category)
  9773. (progn (org-refresh-category-properties)
  9774. (org-get-category))))
  9775. (push (cons "CATEGORY" value) props))
  9776. (append sum-props (nreverse props)))))))
  9777. (defun org-entry-get (pom property &optional inherit)
  9778. "Get value of PROPERTY for entry at point-or-marker POM.
  9779. If INHERIT is non-nil and the entry does not have the property,
  9780. then also check higher levels of the hierarchy.
  9781. If INHERIT is the symbol `selective', use inheritance only if the setting
  9782. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9783. If the property is present but empty, the return value is the empty string.
  9784. If the property is not present at all, nil is returned."
  9785. (org-with-point-at pom
  9786. (if (and inherit (if (eq inherit 'selective)
  9787. (org-property-inherit-p property)
  9788. t))
  9789. (org-entry-get-with-inheritance property)
  9790. (if (member property org-special-properties)
  9791. ;; We need a special property. Use brute force, get all properties.
  9792. (cdr (assoc property (org-entry-properties nil 'special)))
  9793. (let ((range (org-get-property-block)))
  9794. (if (and range
  9795. (goto-char (car range))
  9796. (re-search-forward
  9797. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9798. (cdr range) t))
  9799. ;; Found the property, return it.
  9800. (if (match-end 1)
  9801. (org-match-string-no-properties 1)
  9802. "")))))))
  9803. (defun org-property-or-variable-value (var &optional inherit)
  9804. "Check if there is a property fixing the value of VAR.
  9805. If yes, return this value. If not, return the current value of the variable."
  9806. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9807. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9808. (read prop)
  9809. (symbol-value var))))
  9810. (defun org-entry-delete (pom property)
  9811. "Delete the property PROPERTY from entry at point-or-marker POM."
  9812. (org-with-point-at pom
  9813. (if (member property org-special-properties)
  9814. nil ; cannot delete these properties.
  9815. (let ((range (org-get-property-block)))
  9816. (if (and range
  9817. (goto-char (car range))
  9818. (re-search-forward
  9819. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9820. (cdr range) t))
  9821. (progn
  9822. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9823. t)
  9824. nil)))))
  9825. ;; Multi-values properties are properties that contain multiple values
  9826. ;; These values are assumed to be single words, separated by whitespace.
  9827. (defun org-entry-add-to-multivalued-property (pom property value)
  9828. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9829. (let* ((old (org-entry-get pom property))
  9830. (values (and old (org-split-string old "[ \t]"))))
  9831. (setq value (org-entry-protect-space value))
  9832. (unless (member value values)
  9833. (setq values (cons value values))
  9834. (org-entry-put pom property
  9835. (mapconcat 'identity values " ")))))
  9836. (defun org-entry-remove-from-multivalued-property (pom property value)
  9837. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9838. (let* ((old (org-entry-get pom property))
  9839. (values (and old (org-split-string old "[ \t]"))))
  9840. (setq value (org-entry-protect-space value))
  9841. (when (member value values)
  9842. (setq values (delete value values))
  9843. (org-entry-put pom property
  9844. (mapconcat 'identity values " ")))))
  9845. (defun org-entry-member-in-multivalued-property (pom property value)
  9846. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9847. (let* ((old (org-entry-get pom property))
  9848. (values (and old (org-split-string old "[ \t]"))))
  9849. (setq value (org-entry-protect-space value))
  9850. (member value values)))
  9851. (defun org-entry-get-multivalued-property (pom property)
  9852. "Return a list of values in a multivalued property."
  9853. (let* ((value (org-entry-get pom property))
  9854. (values (and value (org-split-string value "[ \t]"))))
  9855. (mapcar 'org-entry-restore-space values)))
  9856. (defun org-entry-put-multivalued-property (pom property &rest values)
  9857. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9858. VALUES should be a list of strings. Spaces will be protected."
  9859. (org-entry-put pom property
  9860. (mapconcat 'org-entry-protect-space values " "))
  9861. (let* ((value (org-entry-get pom property))
  9862. (values (and value (org-split-string value "[ \t]"))))
  9863. (mapcar 'org-entry-restore-space values)))
  9864. (defun org-entry-protect-space (s)
  9865. "Protect spaces and newline in string S."
  9866. (while (string-match " " s)
  9867. (setq s (replace-match "%20" t t s)))
  9868. (while (string-match "\n" s)
  9869. (setq s (replace-match "%0A" t t s)))
  9870. s)
  9871. (defun org-entry-restore-space (s)
  9872. "Restore spaces and newline in string S."
  9873. (while (string-match "%20" s)
  9874. (setq s (replace-match " " t t s)))
  9875. (while (string-match "%0A" s)
  9876. (setq s (replace-match "\n" t t s)))
  9877. s)
  9878. (defvar org-entry-property-inherited-from (make-marker)
  9879. "Marker pointing to the entry from where a property was inherited.
  9880. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9881. location of the entry where the inheritance search matched. If there was
  9882. no match, the marker will point nowhere.
  9883. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9884. is set.")
  9885. (defun org-entry-get-with-inheritance (property)
  9886. "Get entry property, and search higher levels if not present."
  9887. (move-marker org-entry-property-inherited-from nil)
  9888. (let (tmp)
  9889. (save-excursion
  9890. (save-restriction
  9891. (widen)
  9892. (catch 'ex
  9893. (while t
  9894. (when (setq tmp (org-entry-get nil property))
  9895. (org-back-to-heading t)
  9896. (move-marker org-entry-property-inherited-from (point))
  9897. (throw 'ex tmp))
  9898. (or (org-up-heading-safe) (throw 'ex nil)))))
  9899. (or tmp
  9900. (cdr (assoc property org-file-properties))
  9901. (cdr (assoc property org-global-properties))
  9902. (cdr (assoc property org-global-properties-fixed))))))
  9903. (defun org-entry-put (pom property value)
  9904. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9905. (org-with-point-at pom
  9906. (org-back-to-heading t)
  9907. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9908. range)
  9909. (cond
  9910. ((equal property "TODO")
  9911. (when (and (stringp value) (string-match "\\S-" value)
  9912. (not (member value org-todo-keywords-1)))
  9913. (error "\"%s\" is not a valid TODO state" value))
  9914. (if (or (not value)
  9915. (not (string-match "\\S-" value)))
  9916. (setq value 'none))
  9917. (org-todo value)
  9918. (org-set-tags nil 'align))
  9919. ((equal property "PRIORITY")
  9920. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9921. (string-to-char value) ?\ ))
  9922. (org-set-tags nil 'align))
  9923. ((equal property "SCHEDULED")
  9924. (if (re-search-forward org-scheduled-time-regexp end t)
  9925. (cond
  9926. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9927. ((eq value 'later) (org-timestamp-change 1 'day))
  9928. (t (call-interactively 'org-schedule)))
  9929. (call-interactively 'org-schedule)))
  9930. ((equal property "DEADLINE")
  9931. (if (re-search-forward org-deadline-time-regexp end t)
  9932. (cond
  9933. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9934. ((eq value 'later) (org-timestamp-change 1 'day))
  9935. (t (call-interactively 'org-deadline)))
  9936. (call-interactively 'org-deadline)))
  9937. ((member property org-special-properties)
  9938. (error "The %s property can not yet be set with `org-entry-put'"
  9939. property))
  9940. (t ; a non-special property
  9941. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9942. (setq range (org-get-property-block beg end 'force))
  9943. (goto-char (car range))
  9944. (if (re-search-forward
  9945. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9946. (progn
  9947. (delete-region (match-beginning 1) (match-end 1))
  9948. (goto-char (match-beginning 1)))
  9949. (goto-char (cdr range))
  9950. (insert "\n")
  9951. (backward-char 1)
  9952. (org-indent-line-function)
  9953. (insert ":" property ":"))
  9954. (and value (insert " " value))
  9955. (org-indent-line-function)))))))
  9956. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9957. "Get all property keys in the current buffer.
  9958. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9959. like tags and TODO state.
  9960. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9961. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9962. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9963. formats in the current buffer."
  9964. (let (rtn range cfmt s p)
  9965. (save-excursion
  9966. (save-restriction
  9967. (widen)
  9968. (goto-char (point-min))
  9969. (while (re-search-forward org-property-start-re nil t)
  9970. (setq range (org-get-property-block))
  9971. (goto-char (car range))
  9972. (while (re-search-forward
  9973. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9974. (cdr range) t)
  9975. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9976. (outline-next-heading))))
  9977. (when include-specials
  9978. (setq rtn (append org-special-properties rtn)))
  9979. (when include-defaults
  9980. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9981. (when include-columns
  9982. (save-excursion
  9983. (save-restriction
  9984. (widen)
  9985. (goto-char (point-min))
  9986. (while (re-search-forward
  9987. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9988. nil t)
  9989. (setq cfmt (match-string 2) s 0)
  9990. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9991. cfmt s)
  9992. (setq s (match-end 0)
  9993. p (match-string 1 cfmt))
  9994. (unless (or (equal p "ITEM")
  9995. (member p org-special-properties))
  9996. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9997. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9998. (defun org-property-values (key)
  9999. "Return a list of all values of property KEY."
  10000. (save-excursion
  10001. (save-restriction
  10002. (widen)
  10003. (goto-char (point-min))
  10004. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10005. values)
  10006. (while (re-search-forward re nil t)
  10007. (add-to-list 'values (org-trim (match-string 1))))
  10008. (delete "" values)))))
  10009. (defun org-insert-property-drawer ()
  10010. "Insert a property drawer into the current entry."
  10011. (interactive)
  10012. (org-back-to-heading t)
  10013. (looking-at outline-regexp)
  10014. (let ((indent (- (match-end 0)(match-beginning 0)))
  10015. (beg (point))
  10016. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10017. end hiddenp)
  10018. (outline-next-heading)
  10019. (setq end (point))
  10020. (goto-char beg)
  10021. (while (re-search-forward re end t))
  10022. (setq hiddenp (org-invisible-p))
  10023. (end-of-line 1)
  10024. (and (equal (char-after) ?\n) (forward-char 1))
  10025. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  10026. (beginning-of-line 2))
  10027. (org-skip-over-state-notes)
  10028. (skip-chars-backward " \t\n\r")
  10029. (if (eq (char-before) ?*) (forward-char 1))
  10030. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10031. (beginning-of-line 0)
  10032. (org-indent-to-column indent)
  10033. (beginning-of-line 2)
  10034. (org-indent-to-column indent)
  10035. (beginning-of-line 0)
  10036. (if hiddenp
  10037. (save-excursion
  10038. (org-back-to-heading t)
  10039. (hide-entry))
  10040. (org-flag-drawer t))))
  10041. (defun org-set-property (property value)
  10042. "In the current entry, set PROPERTY to VALUE.
  10043. When called interactively, this will prompt for a property name, offering
  10044. completion on existing and default properties. And then it will prompt
  10045. for a value, offering completion either on allowed values (via an inherited
  10046. xxx_ALL property) or on existing values in other instances of this property
  10047. in the current file."
  10048. (interactive
  10049. (let* ((completion-ignore-case t)
  10050. (keys (org-buffer-property-keys nil t t))
  10051. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10052. (prop (if (member prop0 keys)
  10053. prop0
  10054. (or (cdr (assoc (downcase prop0)
  10055. (mapcar (lambda (x) (cons (downcase x) x))
  10056. keys)))
  10057. prop0)))
  10058. (cur (org-entry-get nil prop))
  10059. (allowed (org-property-get-allowed-values nil prop 'table))
  10060. (existing (mapcar 'list (org-property-values prop)))
  10061. (val (if allowed
  10062. (org-completing-read "Value: " allowed nil 'req-match)
  10063. (org-completing-read
  10064. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10065. (concat "[" cur "]") "")
  10066. ": ")
  10067. existing nil nil "" nil cur))))
  10068. (list prop (if (equal val "") cur val))))
  10069. (unless (equal (org-entry-get nil property) value)
  10070. (org-entry-put nil property value)))
  10071. (defun org-delete-property (property)
  10072. "In the current entry, delete PROPERTY."
  10073. (interactive
  10074. (let* ((completion-ignore-case t)
  10075. (prop (org-ido-completing-read
  10076. "Property: " (org-entry-properties nil 'standard))))
  10077. (list prop)))
  10078. (message "Property %s %s" property
  10079. (if (org-entry-delete nil property)
  10080. "deleted"
  10081. "was not present in the entry")))
  10082. (defun org-delete-property-globally (property)
  10083. "Remove PROPERTY globally, from all entries."
  10084. (interactive
  10085. (let* ((completion-ignore-case t)
  10086. (prop (org-ido-completing-read
  10087. "Globally remove property: "
  10088. (mapcar 'list (org-buffer-property-keys)))))
  10089. (list prop)))
  10090. (save-excursion
  10091. (save-restriction
  10092. (widen)
  10093. (goto-char (point-min))
  10094. (let ((cnt 0))
  10095. (while (re-search-forward
  10096. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10097. nil t)
  10098. (setq cnt (1+ cnt))
  10099. (replace-match ""))
  10100. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10101. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10102. (defun org-compute-property-at-point ()
  10103. "Compute the property at point.
  10104. This looks for an enclosing column format, extracts the operator and
  10105. then applies it to the property in the column format's scope."
  10106. (interactive)
  10107. (unless (org-at-property-p)
  10108. (error "Not at a property"))
  10109. (let ((prop (org-match-string-no-properties 2)))
  10110. (org-columns-get-format-and-top-level)
  10111. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10112. (error "No operator defined for property %s" prop))
  10113. (org-columns-compute prop)))
  10114. (defun org-property-get-allowed-values (pom property &optional table)
  10115. "Get allowed values for the property PROPERTY.
  10116. When TABLE is non-nil, return an alist that can directly be used for
  10117. completion."
  10118. (let (vals)
  10119. (cond
  10120. ((equal property "TODO")
  10121. (setq vals (org-with-point-at pom
  10122. (append org-todo-keywords-1 '("")))))
  10123. ((equal property "PRIORITY")
  10124. (let ((n org-lowest-priority))
  10125. (while (>= n org-highest-priority)
  10126. (push (char-to-string n) vals)
  10127. (setq n (1- n)))))
  10128. ((member property org-special-properties))
  10129. (t
  10130. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10131. (when (and vals (string-match "\\S-" vals))
  10132. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10133. (setq vals (mapcar (lambda (x)
  10134. (cond ((stringp x) x)
  10135. ((numberp x) (number-to-string x))
  10136. ((symbolp x) (symbol-name x))
  10137. (t "???")))
  10138. vals)))))
  10139. (if table (mapcar 'list vals) vals)))
  10140. (defun org-property-previous-allowed-value (&optional previous)
  10141. "Switch to the next allowed value for this property."
  10142. (interactive)
  10143. (org-property-next-allowed-value t))
  10144. (defun org-property-next-allowed-value (&optional previous)
  10145. "Switch to the next allowed value for this property."
  10146. (interactive)
  10147. (unless (org-at-property-p)
  10148. (error "Not at a property"))
  10149. (let* ((key (match-string 2))
  10150. (value (match-string 3))
  10151. (allowed (or (org-property-get-allowed-values (point) key)
  10152. (and (member value '("[ ]" "[-]" "[X]"))
  10153. '("[ ]" "[X]"))))
  10154. nval)
  10155. (unless allowed
  10156. (error "Allowed values for this property have not been defined"))
  10157. (if previous (setq allowed (reverse allowed)))
  10158. (if (member value allowed)
  10159. (setq nval (car (cdr (member value allowed)))))
  10160. (setq nval (or nval (car allowed)))
  10161. (if (equal nval value)
  10162. (error "Only one allowed value for this property"))
  10163. (org-at-property-p)
  10164. (replace-match (concat " :" key ": " nval) t t)
  10165. (org-indent-line-function)
  10166. (beginning-of-line 1)
  10167. (skip-chars-forward " \t")))
  10168. (defun org-find-entry-with-id (ident)
  10169. "Locate the entry that contains the ID property with exact value IDENT.
  10170. IDENT can be a string, a symbol or a number, this function will search for
  10171. the string representation of it.
  10172. Return the position where this entry starts, or nil if there is no such entry."
  10173. (interactive "sID: ")
  10174. (let ((id (cond
  10175. ((stringp ident) ident)
  10176. ((symbol-name ident) (symbol-name ident))
  10177. ((numberp ident) (number-to-string ident))
  10178. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10179. (case-fold-search nil))
  10180. (save-excursion
  10181. (save-restriction
  10182. (widen)
  10183. (goto-char (point-min))
  10184. (when (re-search-forward
  10185. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10186. nil t)
  10187. (org-back-to-heading)
  10188. (point))))))
  10189. ;;;; Timestamps
  10190. (defvar org-last-changed-timestamp nil)
  10191. (defvar org-last-inserted-timestamp nil
  10192. "The last time stamp inserted with `org-insert-time-stamp'.")
  10193. (defvar org-time-was-given) ; dynamically scoped parameter
  10194. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10195. (defvar org-ts-what) ; dynamically scoped parameter
  10196. (defun org-time-stamp (arg &optional inactive)
  10197. "Prompt for a date/time and insert a time stamp.
  10198. If the user specifies a time like HH:MM, or if this command is called
  10199. with a prefix argument, the time stamp will contain date and time.
  10200. Otherwise, only the date will be included. All parts of a date not
  10201. specified by the user will be filled in from the current date/time.
  10202. So if you press just return without typing anything, the time stamp
  10203. will represent the current date/time. If there is already a timestamp
  10204. at the cursor, it will be modified."
  10205. (interactive "P")
  10206. (let* ((ts nil)
  10207. (default-time
  10208. ;; Default time is either today, or, when entering a range,
  10209. ;; the range start.
  10210. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10211. (save-excursion
  10212. (re-search-backward
  10213. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10214. (- (point) 20) t)))
  10215. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10216. (current-time)))
  10217. (default-input (and ts (org-get-compact-tod ts)))
  10218. org-time-was-given org-end-time-was-given time)
  10219. (cond
  10220. ((and (org-at-timestamp-p t)
  10221. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10222. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10223. (insert "--")
  10224. (setq time (let ((this-command this-command))
  10225. (org-read-date arg 'totime nil nil
  10226. default-time default-input)))
  10227. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10228. ((org-at-timestamp-p t)
  10229. (setq time (let ((this-command this-command))
  10230. (org-read-date arg 'totime nil nil default-time default-input)))
  10231. (when (org-at-timestamp-p t) ; just to get the match data
  10232. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10233. (replace-match "")
  10234. (setq org-last-changed-timestamp
  10235. (org-insert-time-stamp
  10236. time (or org-time-was-given arg)
  10237. inactive nil nil (list org-end-time-was-given))))
  10238. (message "Timestamp updated"))
  10239. (t
  10240. (setq time (let ((this-command this-command))
  10241. (org-read-date arg 'totime nil nil default-time default-input)))
  10242. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10243. nil nil (list org-end-time-was-given))))))
  10244. ;; FIXME: can we use this for something else, like computing time differences?
  10245. (defun org-get-compact-tod (s)
  10246. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10247. (let* ((t1 (match-string 1 s))
  10248. (h1 (string-to-number (match-string 2 s)))
  10249. (m1 (string-to-number (match-string 3 s)))
  10250. (t2 (and (match-end 4) (match-string 5 s)))
  10251. (h2 (and t2 (string-to-number (match-string 6 s))))
  10252. (m2 (and t2 (string-to-number (match-string 7 s))))
  10253. dh dm)
  10254. (if (not t2)
  10255. t1
  10256. (setq dh (- h2 h1) dm (- m2 m1))
  10257. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10258. (concat t1 "+" (number-to-string dh)
  10259. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10260. (defun org-time-stamp-inactive (&optional arg)
  10261. "Insert an inactive time stamp.
  10262. An inactive time stamp is enclosed in square brackets instead of angle
  10263. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10264. does not link to the calendar and cannot be changed with the S-cursor keys.
  10265. So these are more for recording a certain time/date."
  10266. (interactive "P")
  10267. (org-time-stamp arg 'inactive))
  10268. (defvar org-date-ovl (org-make-overlay 1 1))
  10269. (org-overlay-put org-date-ovl 'face 'org-warning)
  10270. (org-detach-overlay org-date-ovl)
  10271. (defvar org-ans1) ; dynamically scoped parameter
  10272. (defvar org-ans2) ; dynamically scoped parameter
  10273. (defvar org-plain-time-of-day-regexp) ; defined below
  10274. (defvar org-overriding-default-time nil) ; dynamically scoped
  10275. (defvar org-read-date-overlay nil)
  10276. (defvar org-dcst nil) ; dynamically scoped
  10277. (defun org-read-date (&optional with-time to-time from-string prompt
  10278. default-time default-input)
  10279. "Read a date, possibly a time, and make things smooth for the user.
  10280. The prompt will suggest to enter an ISO date, but you can also enter anything
  10281. which will at least partially be understood by `parse-time-string'.
  10282. Unrecognized parts of the date will default to the current day, month, year,
  10283. hour and minute. If this command is called to replace a timestamp at point,
  10284. of to enter the second timestamp of a range, the default time is taken from the
  10285. existing stamp. For example,
  10286. 3-2-5 --> 2003-02-05
  10287. feb 15 --> currentyear-02-15
  10288. sep 12 9 --> 2009-09-12
  10289. 12:45 --> today 12:45
  10290. 22 sept 0:34 --> currentyear-09-22 0:34
  10291. 12 --> currentyear-currentmonth-12
  10292. Fri --> nearest Friday (today or later)
  10293. etc.
  10294. Furthermore you can specify a relative date by giving, as the *first* thing
  10295. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10296. change in days weeks, months, years.
  10297. With a single plus or minus, the date is relative to today. With a double
  10298. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10299. +4d --> four days from today
  10300. +4 --> same as above
  10301. +2w --> two weeks from today
  10302. ++5 --> five days from default date
  10303. The function understands only English month and weekday abbreviations,
  10304. but this can be configured with the variables `parse-time-months' and
  10305. `parse-time-weekdays'.
  10306. While prompting, a calendar is popped up - you can also select the
  10307. date with the mouse (button 1). The calendar shows a period of three
  10308. months. To scroll it to other months, use the keys `>' and `<'.
  10309. If you don't like the calendar, turn it off with
  10310. \(setq org-read-date-popup-calendar nil)
  10311. With optional argument TO-TIME, the date will immediately be converted
  10312. to an internal time.
  10313. With an optional argument WITH-TIME, the prompt will suggest to also
  10314. insert a time. Note that when WITH-TIME is not set, you can still
  10315. enter a time, and this function will inform the calling routine about
  10316. this change. The calling routine may then choose to change the format
  10317. used to insert the time stamp into the buffer to include the time.
  10318. With optional argument FROM-STRING, read from this string instead from
  10319. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10320. the time/date that is used for everything that is not specified by the
  10321. user."
  10322. (require 'parse-time)
  10323. (let* ((org-time-stamp-rounding-minutes
  10324. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10325. (org-dcst org-display-custom-times)
  10326. (ct (org-current-time))
  10327. (def (or org-overriding-default-time default-time ct))
  10328. (defdecode (decode-time def))
  10329. (dummy (progn
  10330. (when (< (nth 2 defdecode) org-extend-today-until)
  10331. (setcar (nthcdr 2 defdecode) -1)
  10332. (setcar (nthcdr 1 defdecode) 59)
  10333. (setq def (apply 'encode-time defdecode)
  10334. defdecode (decode-time def)))))
  10335. (calendar-move-hook nil)
  10336. (calendar-view-diary-initially-flag nil)
  10337. (view-diary-entries-initially nil)
  10338. (calendar-view-holidays-initially-flag nil)
  10339. (view-calendar-holidays-initially nil)
  10340. (timestr (format-time-string
  10341. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10342. (prompt (concat (if prompt (concat prompt " ") "")
  10343. (format "Date+time [%s]: " timestr)))
  10344. ans (org-ans0 "") org-ans1 org-ans2 final)
  10345. (cond
  10346. (from-string (setq ans from-string))
  10347. (org-read-date-popup-calendar
  10348. (save-excursion
  10349. (save-window-excursion
  10350. (calendar)
  10351. (calendar-forward-day (- (time-to-days def)
  10352. (calendar-absolute-from-gregorian
  10353. (calendar-current-date))))
  10354. (org-eval-in-calendar nil t)
  10355. (let* ((old-map (current-local-map))
  10356. (map (copy-keymap calendar-mode-map))
  10357. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10358. (org-defkey map (kbd "RET") 'org-calendar-select)
  10359. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10360. 'org-calendar-select-mouse)
  10361. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10362. 'org-calendar-select-mouse)
  10363. (org-defkey minibuffer-local-map [(meta shift left)]
  10364. (lambda () (interactive)
  10365. (org-eval-in-calendar '(calendar-backward-month 1))))
  10366. (org-defkey minibuffer-local-map [(meta shift right)]
  10367. (lambda () (interactive)
  10368. (org-eval-in-calendar '(calendar-forward-month 1))))
  10369. (org-defkey minibuffer-local-map [(meta shift up)]
  10370. (lambda () (interactive)
  10371. (org-eval-in-calendar '(calendar-backward-year 1))))
  10372. (org-defkey minibuffer-local-map [(meta shift down)]
  10373. (lambda () (interactive)
  10374. (org-eval-in-calendar '(calendar-forward-year 1))))
  10375. (org-defkey minibuffer-local-map [(shift up)]
  10376. (lambda () (interactive)
  10377. (org-eval-in-calendar '(calendar-backward-week 1))))
  10378. (org-defkey minibuffer-local-map [(shift down)]
  10379. (lambda () (interactive)
  10380. (org-eval-in-calendar '(calendar-forward-week 1))))
  10381. (org-defkey minibuffer-local-map [(shift left)]
  10382. (lambda () (interactive)
  10383. (org-eval-in-calendar '(calendar-backward-day 1))))
  10384. (org-defkey minibuffer-local-map [(shift right)]
  10385. (lambda () (interactive)
  10386. (org-eval-in-calendar '(calendar-forward-day 1))))
  10387. (org-defkey minibuffer-local-map ">"
  10388. (lambda () (interactive)
  10389. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10390. (org-defkey minibuffer-local-map "<"
  10391. (lambda () (interactive)
  10392. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10393. (unwind-protect
  10394. (progn
  10395. (use-local-map map)
  10396. (add-hook 'post-command-hook 'org-read-date-display)
  10397. (setq org-ans0 (read-string prompt default-input nil nil))
  10398. ;; org-ans0: from prompt
  10399. ;; org-ans1: from mouse click
  10400. ;; org-ans2: from calendar motion
  10401. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10402. (remove-hook 'post-command-hook 'org-read-date-display)
  10403. (use-local-map old-map)
  10404. (when org-read-date-overlay
  10405. (org-delete-overlay org-read-date-overlay)
  10406. (setq org-read-date-overlay nil)))))))
  10407. (t ; Naked prompt only
  10408. (unwind-protect
  10409. (setq ans (read-string prompt default-input nil timestr))
  10410. (when org-read-date-overlay
  10411. (org-delete-overlay org-read-date-overlay)
  10412. (setq org-read-date-overlay nil)))))
  10413. (setq final (org-read-date-analyze ans def defdecode))
  10414. (if to-time
  10415. (apply 'encode-time final)
  10416. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10417. (format "%04d-%02d-%02d %02d:%02d"
  10418. (nth 5 final) (nth 4 final) (nth 3 final)
  10419. (nth 2 final) (nth 1 final))
  10420. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10421. (defvar def)
  10422. (defvar defdecode)
  10423. (defvar with-time)
  10424. (defun org-read-date-display ()
  10425. "Display the current date prompt interpretation in the minibuffer."
  10426. (when org-read-date-display-live
  10427. (when org-read-date-overlay
  10428. (org-delete-overlay org-read-date-overlay))
  10429. (let ((p (point)))
  10430. (end-of-line 1)
  10431. (while (not (equal (buffer-substring
  10432. (max (point-min) (- (point) 4)) (point))
  10433. " "))
  10434. (insert " "))
  10435. (goto-char p))
  10436. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10437. " " (or org-ans1 org-ans2)))
  10438. (org-end-time-was-given nil)
  10439. (f (org-read-date-analyze ans def defdecode))
  10440. (fmts (if org-dcst
  10441. org-time-stamp-custom-formats
  10442. org-time-stamp-formats))
  10443. (fmt (if (or with-time
  10444. (and (boundp 'org-time-was-given) org-time-was-given))
  10445. (cdr fmts)
  10446. (car fmts)))
  10447. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10448. (when (and org-end-time-was-given
  10449. (string-match org-plain-time-of-day-regexp txt))
  10450. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10451. org-end-time-was-given
  10452. (substring txt (match-end 0)))))
  10453. (setq org-read-date-overlay
  10454. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10455. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10456. (defun org-read-date-analyze (ans def defdecode)
  10457. "Analyse the combined answer of the date prompt."
  10458. ;; FIXME: cleanup and comment
  10459. (let (delta deltan deltaw deltadef year month day
  10460. hour minute second wday pm h2 m2 tl wday1
  10461. iso-year iso-weekday iso-week iso-year iso-date)
  10462. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10463. (setq ans "+0"))
  10464. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10465. (setq ans (replace-match "" t t ans)
  10466. deltan (car delta)
  10467. deltaw (nth 1 delta)
  10468. deltadef (nth 2 delta)))
  10469. ;; Check if there is an iso week date in there
  10470. ;; If yes, sore the info and postpone interpreting it until the rest
  10471. ;; of the parsing is done
  10472. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10473. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10474. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10475. iso-week (string-to-number (match-string 2 ans)))
  10476. (setq ans (replace-match "" t t ans)))
  10477. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10478. (when (string-match
  10479. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10480. (setq year (if (match-end 2)
  10481. (string-to-number (match-string 2 ans))
  10482. (string-to-number (format-time-string "%Y")))
  10483. month (string-to-number (match-string 3 ans))
  10484. day (string-to-number (match-string 4 ans)))
  10485. (if (< year 100) (setq year (+ 2000 year)))
  10486. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10487. t nil ans)))
  10488. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10489. ;; If there is a time with am/pm, and *no* time without it, we convert
  10490. ;; so that matching will be successful.
  10491. (loop for i from 1 to 2 do ; twice, for end time as well
  10492. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10493. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10494. (setq hour (string-to-number (match-string 1 ans))
  10495. minute (if (match-end 3)
  10496. (string-to-number (match-string 3 ans))
  10497. 0)
  10498. pm (equal ?p
  10499. (string-to-char (downcase (match-string 4 ans)))))
  10500. (if (and (= hour 12) (not pm))
  10501. (setq hour 0)
  10502. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10503. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10504. t t ans))))
  10505. ;; Check if a time range is given as a duration
  10506. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10507. (setq hour (string-to-number (match-string 1 ans))
  10508. h2 (+ hour (string-to-number (match-string 3 ans)))
  10509. minute (string-to-number (match-string 2 ans))
  10510. m2 (+ minute (if (match-end 5) (string-to-number
  10511. (match-string 5 ans))0)))
  10512. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10513. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10514. t t ans)))
  10515. ;; Check if there is a time range
  10516. (when (boundp 'org-end-time-was-given)
  10517. (setq org-time-was-given nil)
  10518. (when (and (string-match org-plain-time-of-day-regexp ans)
  10519. (match-end 8))
  10520. (setq org-end-time-was-given (match-string 8 ans))
  10521. (setq ans (concat (substring ans 0 (match-beginning 7))
  10522. (substring ans (match-end 7))))))
  10523. (setq tl (parse-time-string ans)
  10524. day (or (nth 3 tl) (nth 3 defdecode))
  10525. month (or (nth 4 tl)
  10526. (if (and org-read-date-prefer-future
  10527. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10528. (1+ (nth 4 defdecode))
  10529. (nth 4 defdecode)))
  10530. year (or (nth 5 tl)
  10531. (if (and org-read-date-prefer-future
  10532. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10533. (1+ (nth 5 defdecode))
  10534. (nth 5 defdecode)))
  10535. hour (or (nth 2 tl) (nth 2 defdecode))
  10536. minute (or (nth 1 tl) (nth 1 defdecode))
  10537. second (or (nth 0 tl) 0)
  10538. wday (nth 6 tl))
  10539. ;; Special date definitions below
  10540. (cond
  10541. (iso-week
  10542. ;; There was an iso week
  10543. (setq year (or iso-year year)
  10544. day (or iso-weekday wday 1)
  10545. wday nil ; to make sure that the trigger below does not match
  10546. iso-date (calendar-gregorian-from-absolute
  10547. (calendar-absolute-from-iso
  10548. (list iso-week day year))))
  10549. ; FIXME: Should we also push ISO weeks into the future?
  10550. ; (when (and org-read-date-prefer-future
  10551. ; (not iso-year)
  10552. ; (< (calendar-absolute-from-gregorian iso-date)
  10553. ; (time-to-days (current-time))))
  10554. ; (setq year (1+ year)
  10555. ; iso-date (calendar-gregorian-from-absolute
  10556. ; (calendar-absolute-from-iso
  10557. ; (list iso-week day year)))))
  10558. (setq month (car iso-date)
  10559. year (nth 2 iso-date)
  10560. day (nth 1 iso-date)))
  10561. (deltan
  10562. (unless deltadef
  10563. (let ((now (decode-time (current-time))))
  10564. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10565. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10566. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10567. ((equal deltaw "m") (setq month (+ month deltan)))
  10568. ((equal deltaw "y") (setq year (+ year deltan)))))
  10569. ((and wday (not (nth 3 tl)))
  10570. ;; Weekday was given, but no day, so pick that day in the week
  10571. ;; on or after the derived date.
  10572. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10573. (unless (equal wday wday1)
  10574. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10575. (if (and (boundp 'org-time-was-given)
  10576. (nth 2 tl))
  10577. (setq org-time-was-given t))
  10578. (if (< year 100) (setq year (+ 2000 year)))
  10579. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10580. (list second minute hour day month year)))
  10581. (defvar parse-time-weekdays)
  10582. (defun org-read-date-get-relative (s today default)
  10583. "Check string S for special relative date string.
  10584. TODAY and DEFAULT are internal times, for today and for a default.
  10585. Return shift list (N what def-flag)
  10586. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10587. N is the number of WHATs to shift.
  10588. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10589. the DEFAULT date rather than TODAY."
  10590. (when (and
  10591. (string-match
  10592. (concat
  10593. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10594. "\\([0-9]+\\)?"
  10595. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10596. "\\([ \t]\\|$\\)") s)
  10597. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10598. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10599. (string-to-char (substring (match-string 1 s) -1))
  10600. ?+))
  10601. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10602. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10603. (what (if (match-end 3) (match-string 3 s) "d"))
  10604. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10605. (date (if rel default today))
  10606. (wday (nth 6 (decode-time date)))
  10607. delta)
  10608. (if wday1
  10609. (progn
  10610. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10611. (if (= dir ?-) (setq delta (- delta 7)))
  10612. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10613. (list delta "d" rel))
  10614. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10615. (defun org-eval-in-calendar (form &optional keepdate)
  10616. "Eval FORM in the calendar window and return to current window.
  10617. Also, store the cursor date in variable org-ans2."
  10618. (let ((sw (selected-window)))
  10619. (select-window (get-buffer-window "*Calendar*"))
  10620. (eval form)
  10621. (when (and (not keepdate) (calendar-cursor-to-date))
  10622. (let* ((date (calendar-cursor-to-date))
  10623. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10624. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10625. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10626. (select-window sw)))
  10627. (defun org-calendar-select ()
  10628. "Return to `org-read-date' with the date currently selected.
  10629. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10630. (interactive)
  10631. (when (calendar-cursor-to-date)
  10632. (let* ((date (calendar-cursor-to-date))
  10633. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10634. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10635. (if (active-minibuffer-window) (exit-minibuffer))))
  10636. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10637. "Insert a date stamp for the date given by the internal TIME.
  10638. WITH-HM means, use the stamp format that includes the time of the day.
  10639. INACTIVE means use square brackets instead of angular ones, so that the
  10640. stamp will not contribute to the agenda.
  10641. PRE and POST are optional strings to be inserted before and after the
  10642. stamp.
  10643. The command returns the inserted time stamp."
  10644. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10645. stamp)
  10646. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10647. (insert-before-markers (or pre ""))
  10648. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10649. (when (listp extra)
  10650. (setq extra (car extra))
  10651. (if (and (stringp extra)
  10652. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10653. (setq extra (format "-%02d:%02d"
  10654. (string-to-number (match-string 1 extra))
  10655. (string-to-number (match-string 2 extra))))
  10656. (setq extra nil)))
  10657. (when extra
  10658. (backward-char 1)
  10659. (insert-before-markers extra)
  10660. (forward-char 1))
  10661. (insert-before-markers (or post ""))
  10662. (setq org-last-inserted-timestamp stamp)))
  10663. (defun org-toggle-time-stamp-overlays ()
  10664. "Toggle the use of custom time stamp formats."
  10665. (interactive)
  10666. (setq org-display-custom-times (not org-display-custom-times))
  10667. (unless org-display-custom-times
  10668. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10669. (while (setq p (next-single-property-change p 'display))
  10670. (if (and (get-text-property p 'display)
  10671. (eq (get-text-property p 'face) 'org-date))
  10672. (remove-text-properties
  10673. p (setq p (next-single-property-change p 'display))
  10674. '(display t))))
  10675. (set-buffer-modified-p bmp)))
  10676. (if (featurep 'xemacs)
  10677. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10678. (org-restart-font-lock)
  10679. (setq org-table-may-need-update t)
  10680. (if org-display-custom-times
  10681. (message "Time stamps are overlayed with custom format")
  10682. (message "Time stamp overlays removed")))
  10683. (defun org-display-custom-time (beg end)
  10684. "Overlay modified time stamp format over timestamp between BEG and END."
  10685. (let* ((ts (buffer-substring beg end))
  10686. t1 w1 with-hm tf time str w2 (off 0))
  10687. (save-match-data
  10688. (setq t1 (org-parse-time-string ts t))
  10689. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10690. (setq off (- (match-end 0) (match-beginning 0)))))
  10691. (setq end (- end off))
  10692. (setq w1 (- end beg)
  10693. with-hm (and (nth 1 t1) (nth 2 t1))
  10694. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10695. time (org-fix-decoded-time t1)
  10696. str (org-add-props
  10697. (format-time-string
  10698. (substring tf 1 -1) (apply 'encode-time time))
  10699. nil 'mouse-face 'highlight)
  10700. w2 (length str))
  10701. (if (not (= w2 w1))
  10702. (add-text-properties (1+ beg) (+ 2 beg)
  10703. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10704. (if (featurep 'xemacs)
  10705. (progn
  10706. (put-text-property beg end 'invisible t)
  10707. (put-text-property beg end 'end-glyph (make-glyph str)))
  10708. (put-text-property beg end 'display str))))
  10709. (defun org-translate-time (string)
  10710. "Translate all timestamps in STRING to custom format.
  10711. But do this only if the variable `org-display-custom-times' is set."
  10712. (when org-display-custom-times
  10713. (save-match-data
  10714. (let* ((start 0)
  10715. (re org-ts-regexp-both)
  10716. t1 with-hm inactive tf time str beg end)
  10717. (while (setq start (string-match re string start))
  10718. (setq beg (match-beginning 0)
  10719. end (match-end 0)
  10720. t1 (save-match-data
  10721. (org-parse-time-string (substring string beg end) t))
  10722. with-hm (and (nth 1 t1) (nth 2 t1))
  10723. inactive (equal (substring string beg (1+ beg)) "[")
  10724. tf (funcall (if with-hm 'cdr 'car)
  10725. org-time-stamp-custom-formats)
  10726. time (org-fix-decoded-time t1)
  10727. str (format-time-string
  10728. (concat
  10729. (if inactive "[" "<") (substring tf 1 -1)
  10730. (if inactive "]" ">"))
  10731. (apply 'encode-time time))
  10732. string (replace-match str t t string)
  10733. start (+ start (length str)))))))
  10734. string)
  10735. (defun org-fix-decoded-time (time)
  10736. "Set 0 instead of nil for the first 6 elements of time.
  10737. Don't touch the rest."
  10738. (let ((n 0))
  10739. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10740. (defun org-days-to-time (timestamp-string)
  10741. "Difference between TIMESTAMP-STRING and now in days."
  10742. (- (time-to-days (org-time-string-to-time timestamp-string))
  10743. (time-to-days (current-time))))
  10744. (defun org-deadline-close (timestamp-string &optional ndays)
  10745. "Is the time in TIMESTAMP-STRING close to the current date?"
  10746. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10747. (and (< (org-days-to-time timestamp-string) ndays)
  10748. (not (org-entry-is-done-p))))
  10749. (defun org-get-wdays (ts)
  10750. "Get the deadline lead time appropriate for timestring TS."
  10751. (cond
  10752. ((<= org-deadline-warning-days 0)
  10753. ;; 0 or negative, enforce this value no matter what
  10754. (- org-deadline-warning-days))
  10755. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10756. ;; lead time is specified.
  10757. (floor (* (string-to-number (match-string 1 ts))
  10758. (cdr (assoc (match-string 2 ts)
  10759. '(("d" . 1) ("w" . 7)
  10760. ("m" . 30.4) ("y" . 365.25)))))))
  10761. ;; go for the default.
  10762. (t org-deadline-warning-days)))
  10763. (defun org-calendar-select-mouse (ev)
  10764. "Return to `org-read-date' with the date currently selected.
  10765. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10766. (interactive "e")
  10767. (mouse-set-point ev)
  10768. (when (calendar-cursor-to-date)
  10769. (let* ((date (calendar-cursor-to-date))
  10770. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10771. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10772. (if (active-minibuffer-window) (exit-minibuffer))))
  10773. (defun org-check-deadlines (ndays)
  10774. "Check if there are any deadlines due or past due.
  10775. A deadline is considered due if it happens within `org-deadline-warning-days'
  10776. days from today's date. If the deadline appears in an entry marked DONE,
  10777. it is not shown. The prefix arg NDAYS can be used to test that many
  10778. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10779. (interactive "P")
  10780. (let* ((org-warn-days
  10781. (cond
  10782. ((equal ndays '(4)) 100000)
  10783. (ndays (prefix-numeric-value ndays))
  10784. (t (abs org-deadline-warning-days))))
  10785. (case-fold-search nil)
  10786. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10787. (callback
  10788. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10789. (message "%d deadlines past-due or due within %d days"
  10790. (org-occur regexp nil callback)
  10791. org-warn-days)))
  10792. (defun org-check-before-date (date)
  10793. "Check if there are deadlines or scheduled entries before DATE."
  10794. (interactive (list (org-read-date)))
  10795. (let ((case-fold-search nil)
  10796. (regexp (concat "\\<\\(" org-deadline-string
  10797. "\\|" org-scheduled-string
  10798. "\\) *<\\([^>]+\\)>"))
  10799. (callback
  10800. (lambda () (time-less-p
  10801. (org-time-string-to-time (match-string 2))
  10802. (org-time-string-to-time date)))))
  10803. (message "%d entries before %s"
  10804. (org-occur regexp nil callback) date)))
  10805. (defun org-evaluate-time-range (&optional to-buffer)
  10806. "Evaluate a time range by computing the difference between start and end.
  10807. Normally the result is just printed in the echo area, but with prefix arg
  10808. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10809. If the time range is actually in a table, the result is inserted into the
  10810. next column.
  10811. For time difference computation, a year is assumed to be exactly 365
  10812. days in order to avoid rounding problems."
  10813. (interactive "P")
  10814. (or
  10815. (org-clock-update-time-maybe)
  10816. (save-excursion
  10817. (unless (org-at-date-range-p t)
  10818. (goto-char (point-at-bol))
  10819. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10820. (if (not (org-at-date-range-p t))
  10821. (error "Not at a time-stamp range, and none found in current line")))
  10822. (let* ((ts1 (match-string 1))
  10823. (ts2 (match-string 2))
  10824. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10825. (match-end (match-end 0))
  10826. (time1 (org-time-string-to-time ts1))
  10827. (time2 (org-time-string-to-time ts2))
  10828. (t1 (time-to-seconds time1))
  10829. (t2 (time-to-seconds time2))
  10830. (diff (abs (- t2 t1)))
  10831. (negative (< (- t2 t1) 0))
  10832. ;; (ys (floor (* 365 24 60 60)))
  10833. (ds (* 24 60 60))
  10834. (hs (* 60 60))
  10835. (fy "%dy %dd %02d:%02d")
  10836. (fy1 "%dy %dd")
  10837. (fd "%dd %02d:%02d")
  10838. (fd1 "%dd")
  10839. (fh "%02d:%02d")
  10840. y d h m align)
  10841. (if havetime
  10842. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10843. y 0
  10844. d (floor (/ diff ds)) diff (mod diff ds)
  10845. h (floor (/ diff hs)) diff (mod diff hs)
  10846. m (floor (/ diff 60)))
  10847. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10848. y 0
  10849. d (floor (+ (/ diff ds) 0.5))
  10850. h 0 m 0))
  10851. (if (not to-buffer)
  10852. (message "%s" (org-make-tdiff-string y d h m))
  10853. (if (org-at-table-p)
  10854. (progn
  10855. (goto-char match-end)
  10856. (setq align t)
  10857. (and (looking-at " *|") (goto-char (match-end 0))))
  10858. (goto-char match-end))
  10859. (if (looking-at
  10860. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10861. (replace-match ""))
  10862. (if negative (insert " -"))
  10863. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10864. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10865. (insert " " (format fh h m))))
  10866. (if align (org-table-align))
  10867. (message "Time difference inserted")))))
  10868. (defun org-make-tdiff-string (y d h m)
  10869. (let ((fmt "")
  10870. (l nil))
  10871. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10872. l (push y l)))
  10873. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10874. l (push d l)))
  10875. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10876. l (push h l)))
  10877. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10878. l (push m l)))
  10879. (apply 'format fmt (nreverse l))))
  10880. (defun org-time-string-to-time (s)
  10881. (apply 'encode-time (org-parse-time-string s)))
  10882. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10883. "Convert a time stamp to an absolute day number.
  10884. If there is a specifyer for a cyclic time stamp, get the closest date to
  10885. DAYNR.
  10886. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10887. (cond
  10888. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10889. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10890. daynr
  10891. (+ daynr 1000)))
  10892. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10893. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10894. (time-to-days (current-time))) (match-string 0 s)
  10895. prefer show-all))
  10896. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10897. (defun org-days-to-iso-week (days)
  10898. "Return the iso week number."
  10899. (require 'cal-iso)
  10900. (car (calendar-iso-from-absolute days)))
  10901. (defun org-small-year-to-year (year)
  10902. "Convert 2-digit years into 4-digit years.
  10903. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10904. The year 2000 cannot be abbreviated. Any year larger than 99
  10905. is returned unchanged."
  10906. (if (< year 38)
  10907. (setq year (+ 2000 year))
  10908. (if (< year 100)
  10909. (setq year (+ 1900 year))))
  10910. year)
  10911. (defun org-time-from-absolute (d)
  10912. "Return the time corresponding to date D.
  10913. D may be an absolute day number, or a calendar-type list (month day year)."
  10914. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10915. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10916. (defun org-calendar-holiday ()
  10917. "List of holidays, for Diary display in Org-mode."
  10918. (require 'holidays)
  10919. (let ((hl (funcall
  10920. (if (fboundp 'calendar-check-holidays)
  10921. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10922. (if hl (mapconcat 'identity hl "; "))))
  10923. (defun org-diary-sexp-entry (sexp entry date)
  10924. "Process a SEXP diary ENTRY for DATE."
  10925. (require 'diary-lib)
  10926. (let ((result (if calendar-debug-sexp
  10927. (let ((stack-trace-on-error t))
  10928. (eval (car (read-from-string sexp))))
  10929. (condition-case nil
  10930. (eval (car (read-from-string sexp)))
  10931. (error
  10932. (beep)
  10933. (message "Bad sexp at line %d in %s: %s"
  10934. (org-current-line)
  10935. (buffer-file-name) sexp)
  10936. (sleep-for 2))))))
  10937. (cond ((stringp result) result)
  10938. ((and (consp result)
  10939. (stringp (cdr result))) (cdr result))
  10940. (result entry)
  10941. (t nil))))
  10942. (defun org-diary-to-ical-string (frombuf)
  10943. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10944. This uses the icalendar.el library."
  10945. (let* ((tmpdir (if (featurep 'xemacs)
  10946. (temp-directory)
  10947. temporary-file-directory))
  10948. (tmpfile (make-temp-name
  10949. (expand-file-name "orgics" tmpdir)))
  10950. buf rtn b e)
  10951. (save-excursion
  10952. (set-buffer frombuf)
  10953. (icalendar-export-region (point-min) (point-max) tmpfile)
  10954. (setq buf (find-buffer-visiting tmpfile))
  10955. (set-buffer buf)
  10956. (goto-char (point-min))
  10957. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10958. (setq b (match-beginning 0)))
  10959. (goto-char (point-max))
  10960. (if (re-search-backward "^END:VEVENT" nil t)
  10961. (setq e (match-end 0)))
  10962. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10963. (kill-buffer buf)
  10964. (delete-file tmpfile)
  10965. rtn))
  10966. (defun org-closest-date (start current change prefer show-all)
  10967. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10968. When PREFER is `past' return a date that is either CURRENT or past.
  10969. When PREFER is `future', return a date that is either CURRENT or future.
  10970. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10971. ;; Make the proper lists from the dates
  10972. (catch 'exit
  10973. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10974. dn dw sday cday n1 n2 n0
  10975. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10976. (setq start (org-date-to-gregorian start)
  10977. current (org-date-to-gregorian
  10978. (if show-all
  10979. current
  10980. (time-to-days (current-time))))
  10981. sday (calendar-absolute-from-gregorian start)
  10982. cday (calendar-absolute-from-gregorian current))
  10983. (if (<= cday sday) (throw 'exit sday))
  10984. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10985. (setq dn (string-to-number (match-string 1 change))
  10986. dw (cdr (assoc (match-string 2 change) a1)))
  10987. (error "Invalid change specifyer: %s" change))
  10988. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10989. (cond
  10990. ((eq dw 'day)
  10991. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10992. n2 (+ n1 dn)))
  10993. ((eq dw 'year)
  10994. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10995. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10996. (setq date1 (list m d y1)
  10997. n1 (calendar-absolute-from-gregorian date1)
  10998. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10999. n2 (calendar-absolute-from-gregorian date2)))
  11000. ((eq dw 'month)
  11001. ;; approx number of month between the two dates
  11002. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11003. ;; How often does dn fit in there?
  11004. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11005. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11006. m (+ m nm)
  11007. ny (floor (/ m 12))
  11008. y (+ y ny)
  11009. m (- m (* ny 12)))
  11010. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11011. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11012. (setq m2 (+ m dn) y2 y)
  11013. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11014. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11015. (while (<= n2 cday)
  11016. (setq n1 n2 m m2 y y2)
  11017. (setq m2 (+ m dn) y2 y)
  11018. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11019. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11020. ;; Make sure n1 is the earlier date
  11021. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11022. (if show-all
  11023. (cond
  11024. ((eq prefer 'past) n1)
  11025. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11026. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11027. (cond
  11028. ((eq prefer 'past) n1)
  11029. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11030. (t (if (= cday n1) n1 n2)))))))
  11031. (defun org-date-to-gregorian (date)
  11032. "Turn any specification of DATE into a gregorian date for the calendar."
  11033. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11034. ((and (listp date) (= (length date) 3)) date)
  11035. ((stringp date)
  11036. (setq date (org-parse-time-string date))
  11037. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11038. ((listp date)
  11039. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11040. (defun org-parse-time-string (s &optional nodefault)
  11041. "Parse the standard Org-mode time string.
  11042. This should be a lot faster than the normal `parse-time-string'.
  11043. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11044. hour and minute fields will be nil if not given."
  11045. (if (string-match org-ts-regexp0 s)
  11046. (list 0
  11047. (if (or (match-beginning 8) (not nodefault))
  11048. (string-to-number (or (match-string 8 s) "0")))
  11049. (if (or (match-beginning 7) (not nodefault))
  11050. (string-to-number (or (match-string 7 s) "0")))
  11051. (string-to-number (match-string 4 s))
  11052. (string-to-number (match-string 3 s))
  11053. (string-to-number (match-string 2 s))
  11054. nil nil nil)
  11055. (make-list 9 0)))
  11056. (defun org-timestamp-up (&optional arg)
  11057. "Increase the date item at the cursor by one.
  11058. If the cursor is on the year, change the year. If it is on the month or
  11059. the day, change that.
  11060. With prefix ARG, change by that many units."
  11061. (interactive "p")
  11062. (org-timestamp-change (prefix-numeric-value arg)))
  11063. (defun org-timestamp-down (&optional arg)
  11064. "Decrease the date item at the cursor by one.
  11065. If the cursor is on the year, change the year. If it is on the month or
  11066. the day, change that.
  11067. With prefix ARG, change by that many units."
  11068. (interactive "p")
  11069. (org-timestamp-change (- (prefix-numeric-value arg))))
  11070. (defun org-timestamp-up-day (&optional arg)
  11071. "Increase the date in the time stamp by one day.
  11072. With prefix ARG, change that many days."
  11073. (interactive "p")
  11074. (if (and (not (org-at-timestamp-p t))
  11075. (org-on-heading-p))
  11076. (org-todo 'up)
  11077. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11078. (defun org-timestamp-down-day (&optional arg)
  11079. "Decrease the date in the time stamp by one day.
  11080. With prefix ARG, change that many days."
  11081. (interactive "p")
  11082. (if (and (not (org-at-timestamp-p t))
  11083. (org-on-heading-p))
  11084. (org-todo 'down)
  11085. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11086. (defun org-at-timestamp-p (&optional inactive-ok)
  11087. "Determine if the cursor is in or at a timestamp."
  11088. (interactive)
  11089. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11090. (pos (point))
  11091. (ans (or (looking-at tsr)
  11092. (save-excursion
  11093. (skip-chars-backward "^[<\n\r\t")
  11094. (if (> (point) (point-min)) (backward-char 1))
  11095. (and (looking-at tsr)
  11096. (> (- (match-end 0) pos) -1))))))
  11097. (and ans
  11098. (boundp 'org-ts-what)
  11099. (setq org-ts-what
  11100. (cond
  11101. ((= pos (match-beginning 0)) 'bracket)
  11102. ((= pos (1- (match-end 0))) 'bracket)
  11103. ((org-pos-in-match-range pos 2) 'year)
  11104. ((org-pos-in-match-range pos 3) 'month)
  11105. ((org-pos-in-match-range pos 7) 'hour)
  11106. ((org-pos-in-match-range pos 8) 'minute)
  11107. ((or (org-pos-in-match-range pos 4)
  11108. (org-pos-in-match-range pos 5)) 'day)
  11109. ((and (> pos (or (match-end 8) (match-end 5)))
  11110. (< pos (match-end 0)))
  11111. (- pos (or (match-end 8) (match-end 5))))
  11112. (t 'day))))
  11113. ans))
  11114. (defun org-toggle-timestamp-type ()
  11115. "Toggle the type (<active> or [inactive]) of a time stamp."
  11116. (interactive)
  11117. (when (org-at-timestamp-p t)
  11118. (let ((beg (match-beginning 0)) (end (match-end 0))
  11119. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11120. (save-excursion
  11121. (goto-char beg)
  11122. (while (re-search-forward "[][<>]" end t)
  11123. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11124. t t)))
  11125. (message "Timestamp is now %sactive"
  11126. (if (equal (char-after beg) ?<) "" "in")))))
  11127. (defun org-timestamp-change (n &optional what)
  11128. "Change the date in the time stamp at point.
  11129. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11130. `year', `minute', `second'. If WHAT is not given, the cursor position
  11131. in the timestamp determines what will be changed."
  11132. (let ((pos (point))
  11133. with-hm inactive
  11134. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11135. org-ts-what
  11136. extra rem
  11137. ts time time0)
  11138. (if (not (org-at-timestamp-p t))
  11139. (error "Not at a timestamp"))
  11140. (if (and (not what) (eq org-ts-what 'bracket))
  11141. (org-toggle-timestamp-type)
  11142. (if (and (not what) (not (eq org-ts-what 'day))
  11143. org-display-custom-times
  11144. (get-text-property (point) 'display)
  11145. (not (get-text-property (1- (point)) 'display)))
  11146. (setq org-ts-what 'day))
  11147. (setq org-ts-what (or what org-ts-what)
  11148. inactive (= (char-after (match-beginning 0)) ?\[)
  11149. ts (match-string 0))
  11150. (replace-match "")
  11151. (if (string-match
  11152. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11153. ts)
  11154. (setq extra (match-string 1 ts)))
  11155. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11156. (setq with-hm t))
  11157. (setq time0 (org-parse-time-string ts))
  11158. (when (and (eq org-ts-what 'minute)
  11159. (eq current-prefix-arg nil))
  11160. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11161. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11162. (setcar (cdr time0) (+ (nth 1 time0)
  11163. (if (> n 0) (- rem) (- dm rem))))))
  11164. (setq time
  11165. (encode-time (or (car time0) 0)
  11166. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11167. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11168. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11169. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11170. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11171. (nthcdr 6 time0)))
  11172. (when (integerp org-ts-what)
  11173. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11174. (if (eq what 'calendar)
  11175. (let ((cal-date (org-get-date-from-calendar)))
  11176. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11177. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11178. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11179. (setcar time0 (or (car time0) 0))
  11180. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11181. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11182. (setq time (apply 'encode-time time0))))
  11183. (setq org-last-changed-timestamp
  11184. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11185. (org-clock-update-time-maybe)
  11186. (goto-char pos)
  11187. ;; Try to recenter the calendar window, if any
  11188. (if (and org-calendar-follow-timestamp-change
  11189. (get-buffer-window "*Calendar*" t)
  11190. (memq org-ts-what '(day month year)))
  11191. (org-recenter-calendar (time-to-days time))))))
  11192. (defun org-modify-ts-extra (s pos n dm)
  11193. "Change the different parts of the lead-time and repeat fields in timestamp."
  11194. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11195. ng h m new rem)
  11196. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11197. (cond
  11198. ((or (org-pos-in-match-range pos 2)
  11199. (org-pos-in-match-range pos 3))
  11200. (setq m (string-to-number (match-string 3 s))
  11201. h (string-to-number (match-string 2 s)))
  11202. (if (org-pos-in-match-range pos 2)
  11203. (setq h (+ h n))
  11204. (setq n (* dm (org-no-warnings (signum n))))
  11205. (when (not (= 0 (setq rem (% m dm))))
  11206. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11207. (setq m (+ m n)))
  11208. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11209. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11210. (setq h (min 24 (max 0 h)))
  11211. (setq ng 1 new (format "-%02d:%02d" h m)))
  11212. ((org-pos-in-match-range pos 6)
  11213. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11214. ((org-pos-in-match-range pos 5)
  11215. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11216. ((org-pos-in-match-range pos 9)
  11217. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11218. ((org-pos-in-match-range pos 8)
  11219. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11220. (when ng
  11221. (setq s (concat
  11222. (substring s 0 (match-beginning ng))
  11223. new
  11224. (substring s (match-end ng))))))
  11225. s))
  11226. (defun org-recenter-calendar (date)
  11227. "If the calendar is visible, recenter it to DATE."
  11228. (let* ((win (selected-window))
  11229. (cwin (get-buffer-window "*Calendar*" t))
  11230. (calendar-move-hook nil))
  11231. (when cwin
  11232. (select-window cwin)
  11233. (calendar-goto-date (if (listp date) date
  11234. (calendar-gregorian-from-absolute date)))
  11235. (select-window win))))
  11236. (defun org-goto-calendar (&optional arg)
  11237. "Go to the Emacs calendar at the current date.
  11238. If there is a time stamp in the current line, go to that date.
  11239. A prefix ARG can be used to force the current date."
  11240. (interactive "P")
  11241. (let ((tsr org-ts-regexp) diff
  11242. (calendar-move-hook nil)
  11243. (calendar-view-holidays-initially-flag nil)
  11244. (view-calendar-holidays-initially nil)
  11245. (calendar-view-diary-initially-flag nil)
  11246. (view-diary-entries-initially nil))
  11247. (if (or (org-at-timestamp-p)
  11248. (save-excursion
  11249. (beginning-of-line 1)
  11250. (looking-at (concat ".*" tsr))))
  11251. (let ((d1 (time-to-days (current-time)))
  11252. (d2 (time-to-days
  11253. (org-time-string-to-time (match-string 1)))))
  11254. (setq diff (- d2 d1))))
  11255. (calendar)
  11256. (calendar-goto-today)
  11257. (if (and diff (not arg)) (calendar-forward-day diff))))
  11258. (defun org-get-date-from-calendar ()
  11259. "Return a list (month day year) of date at point in calendar."
  11260. (with-current-buffer "*Calendar*"
  11261. (save-match-data
  11262. (calendar-cursor-to-date))))
  11263. (defun org-date-from-calendar ()
  11264. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11265. If there is already a time stamp at the cursor position, update it."
  11266. (interactive)
  11267. (if (org-at-timestamp-p t)
  11268. (org-timestamp-change 0 'calendar)
  11269. (let ((cal-date (org-get-date-from-calendar)))
  11270. (org-insert-time-stamp
  11271. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11272. (defun org-minutes-to-hh:mm-string (m)
  11273. "Compute H:MM from a number of minutes."
  11274. (let ((h (/ m 60)))
  11275. (setq m (- m (* 60 h)))
  11276. (format org-time-clocksum-format h m)))
  11277. (defun org-hh:mm-string-to-minutes (s)
  11278. "Convert a string H:MM to a number of minutes."
  11279. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11280. (+ (* (string-to-number (match-string 1 s)) 60)
  11281. (string-to-number (match-string 2 s)))
  11282. 0))
  11283. ;;;; Files
  11284. (defun org-save-all-org-buffers ()
  11285. "Save all Org-mode buffers without user confirmation."
  11286. (interactive)
  11287. (message "Saving all Org-mode buffers...")
  11288. (save-some-buffers t 'org-mode-p)
  11289. (when (featurep 'org-id) (org-id-locations-save))
  11290. (message "Saving all Org-mode buffers... done"))
  11291. (defun org-revert-all-org-buffers ()
  11292. "Revert all Org-mode buffers.
  11293. Prompt for confirmation when there are unsaved changes.
  11294. Be sure you know what you are doing before letting this function
  11295. overwrite your changes.
  11296. This function is useful in a setup where one tracks org files
  11297. with a version control system, to revert on one machine after pulling
  11298. changes from another. I believe the procedure must be like this:
  11299. 1. M-x org-save-all-org-buffers
  11300. 2. Pull changes from the other machine, resolve conflicts
  11301. 3. M-x org-revert-all-org-buffers"
  11302. (interactive)
  11303. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11304. (error "Abort"))
  11305. (save-excursion
  11306. (save-window-excursion
  11307. (mapc
  11308. (lambda (b)
  11309. (when (and (with-current-buffer b (org-mode-p))
  11310. (with-current-buffer b buffer-file-name))
  11311. (switch-to-buffer b)
  11312. (revert-buffer t 'no-confirm)))
  11313. (buffer-list))
  11314. (when (and (featurep 'org-id) org-id-track-globally)
  11315. (org-id-locations-load)))))
  11316. ;;;; Agenda files
  11317. ;;;###autoload
  11318. (defun org-iswitchb (&optional arg)
  11319. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11320. With a prefix argument, restrict available to files.
  11321. With two prefix arguments, restrict available buffers to agenda files.
  11322. Due to some yet unresolved reason, the global function
  11323. `iswitchb-mode' needs to be active for this function to work."
  11324. (interactive "P")
  11325. (require 'iswitchb)
  11326. (let ((enabled iswitchb-mode) blist)
  11327. (or enabled (iswitchb-mode 1))
  11328. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11329. ((equal arg '(16)) (org-buffer-list 'agenda))
  11330. (t (org-buffer-list))))
  11331. (unwind-protect
  11332. (let ((iswitchb-make-buflist-hook
  11333. (lambda ()
  11334. (setq iswitchb-temp-buflist
  11335. (mapcar 'buffer-name blist)))))
  11336. (switch-to-buffer
  11337. (iswitchb-read-buffer
  11338. "Switch-to: " nil t))
  11339. (or enabled (iswitchb-mode -1))))))
  11340. ;;;###autoload
  11341. (defun org-ido-switchb (&optional arg)
  11342. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11343. With a prefix argument, restrict available to files.
  11344. With two prefix arguments, restrict available buffers to agenda files."
  11345. (interactive "P")
  11346. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11347. ((equal arg '(16)) (org-buffer-list 'agenda))
  11348. (t (org-buffer-list)))))
  11349. (switch-to-buffer
  11350. (org-ido-completing-read "Org buffer: "
  11351. (mapcar 'buffer-name blist)
  11352. nil t))))
  11353. (defun org-buffer-list (&optional predicate exclude-tmp)
  11354. "Return a list of Org buffers.
  11355. PREDICATE can be `export', `files' or `agenda'.
  11356. export restrict the list to Export buffers.
  11357. files restrict the list to buffers visiting Org files.
  11358. agenda restrict the list to buffers visiting agenda files.
  11359. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11360. (let* ((bfn nil)
  11361. (agenda-files (and (eq predicate 'agenda)
  11362. (mapcar 'file-truename (org-agenda-files t))))
  11363. (filter
  11364. (cond
  11365. ((eq predicate 'files)
  11366. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11367. ((eq predicate 'export)
  11368. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11369. ((eq predicate 'agenda)
  11370. (lambda (b)
  11371. (with-current-buffer b
  11372. (and (eq major-mode 'org-mode)
  11373. (setq bfn (buffer-file-name b))
  11374. (member (file-truename bfn) agenda-files)))))
  11375. (t (lambda (b) (with-current-buffer b
  11376. (or (eq major-mode 'org-mode)
  11377. (string-match "\*Org .*Export"
  11378. (buffer-name b)))))))))
  11379. (delq nil
  11380. (mapcar
  11381. (lambda(b)
  11382. (if (and (funcall filter b)
  11383. (or (not exclude-tmp)
  11384. (not (string-match "tmp" (buffer-name b)))))
  11385. b
  11386. nil))
  11387. (buffer-list)))))
  11388. (defun org-agenda-files (&optional unrestricted archives)
  11389. "Get the list of agenda files.
  11390. Optional UNRESTRICTED means return the full list even if a restriction
  11391. is currently in place.
  11392. When ARCHIVES is t, include all archive files hat are really being
  11393. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11394. `org-agenda-archives-mode' is t."
  11395. (let ((files
  11396. (cond
  11397. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11398. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11399. ((listp org-agenda-files) org-agenda-files)
  11400. (t (error "Invalid value of `org-agenda-files'")))))
  11401. (setq files (apply 'append
  11402. (mapcar (lambda (f)
  11403. (if (file-directory-p f)
  11404. (directory-files
  11405. f t org-agenda-file-regexp)
  11406. (list f)))
  11407. files)))
  11408. (when org-agenda-skip-unavailable-files
  11409. (setq files (delq nil
  11410. (mapcar (function
  11411. (lambda (file)
  11412. (and (file-readable-p file) file)))
  11413. files))))
  11414. (when (or (eq archives t)
  11415. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11416. (setq files (org-add-archive-files files)))
  11417. files))
  11418. (defun org-edit-agenda-file-list ()
  11419. "Edit the list of agenda files.
  11420. Depending on setup, this either uses customize to edit the variable
  11421. `org-agenda-files', or it visits the file that is holding the list. In the
  11422. latter case, the buffer is set up in a way that saving it automatically kills
  11423. the buffer and restores the previous window configuration."
  11424. (interactive)
  11425. (if (stringp org-agenda-files)
  11426. (let ((cw (current-window-configuration)))
  11427. (find-file org-agenda-files)
  11428. (org-set-local 'org-window-configuration cw)
  11429. (org-add-hook 'after-save-hook
  11430. (lambda ()
  11431. (set-window-configuration
  11432. (prog1 org-window-configuration
  11433. (kill-buffer (current-buffer))))
  11434. (org-install-agenda-files-menu)
  11435. (message "New agenda file list installed"))
  11436. nil 'local)
  11437. (message "%s" (substitute-command-keys
  11438. "Edit list and finish with \\[save-buffer]")))
  11439. (customize-variable 'org-agenda-files)))
  11440. (defun org-store-new-agenda-file-list (list)
  11441. "Set new value for the agenda file list and save it correctly."
  11442. (if (stringp org-agenda-files)
  11443. (let ((f org-agenda-files) b)
  11444. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11445. (with-temp-file f
  11446. (insert (mapconcat 'identity list "\n") "\n")))
  11447. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11448. (setq org-agenda-files list)
  11449. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11450. (defun org-read-agenda-file-list ()
  11451. "Read the list of agenda files from a file."
  11452. (when (file-directory-p org-agenda-files)
  11453. (error "`org-agenda-files' cannot be a single directory"))
  11454. (when (stringp org-agenda-files)
  11455. (with-temp-buffer
  11456. (insert-file-contents org-agenda-files)
  11457. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11458. ;;;###autoload
  11459. (defun org-cycle-agenda-files ()
  11460. "Cycle through the files in `org-agenda-files'.
  11461. If the current buffer visits an agenda file, find the next one in the list.
  11462. If the current buffer does not, find the first agenda file."
  11463. (interactive)
  11464. (let* ((fs (org-agenda-files t))
  11465. (files (append fs (list (car fs))))
  11466. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11467. file)
  11468. (unless files (error "No agenda files"))
  11469. (catch 'exit
  11470. (while (setq file (pop files))
  11471. (if (equal (file-truename file) tcf)
  11472. (when (car files)
  11473. (find-file (car files))
  11474. (throw 'exit t))))
  11475. (find-file (car fs)))
  11476. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11477. (defun org-agenda-file-to-front (&optional to-end)
  11478. "Move/add the current file to the top of the agenda file list.
  11479. If the file is not present in the list, it is added to the front. If it is
  11480. present, it is moved there. With optional argument TO-END, add/move to the
  11481. end of the list."
  11482. (interactive "P")
  11483. (let ((org-agenda-skip-unavailable-files nil)
  11484. (file-alist (mapcar (lambda (x)
  11485. (cons (file-truename x) x))
  11486. (org-agenda-files t)))
  11487. (ctf (file-truename buffer-file-name))
  11488. x had)
  11489. (setq x (assoc ctf file-alist) had x)
  11490. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11491. (if to-end
  11492. (setq file-alist (append (delq x file-alist) (list x)))
  11493. (setq file-alist (cons x (delq x file-alist))))
  11494. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11495. (org-install-agenda-files-menu)
  11496. (message "File %s to %s of agenda file list"
  11497. (if had "moved" "added") (if to-end "end" "front"))))
  11498. (defun org-remove-file (&optional file)
  11499. "Remove current file from the list of files in variable `org-agenda-files'.
  11500. These are the files which are being checked for agenda entries.
  11501. Optional argument FILE means, use this file instead of the current."
  11502. (interactive)
  11503. (let* ((org-agenda-skip-unavailable-files nil)
  11504. (file (or file buffer-file-name))
  11505. (true-file (file-truename file))
  11506. (afile (abbreviate-file-name file))
  11507. (files (delq nil (mapcar
  11508. (lambda (x)
  11509. (if (equal true-file
  11510. (file-truename x))
  11511. nil x))
  11512. (org-agenda-files t)))))
  11513. (if (not (= (length files) (length (org-agenda-files t))))
  11514. (progn
  11515. (org-store-new-agenda-file-list files)
  11516. (org-install-agenda-files-menu)
  11517. (message "Removed file: %s" afile))
  11518. (message "File was not in list: %s (not removed)" afile))))
  11519. (defun org-file-menu-entry (file)
  11520. (vector file (list 'find-file file) t))
  11521. (defun org-check-agenda-file (file)
  11522. "Make sure FILE exists. If not, ask user what to do."
  11523. (when (not (file-exists-p file))
  11524. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11525. (abbreviate-file-name file))
  11526. (let ((r (downcase (read-char-exclusive))))
  11527. (cond
  11528. ((equal r ?r)
  11529. (org-remove-file file)
  11530. (throw 'nextfile t))
  11531. (t (error "Abort"))))))
  11532. (defun org-get-agenda-file-buffer (file)
  11533. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11534. it to the list of buffers which might be released later."
  11535. (let ((buf (org-find-base-buffer-visiting file)))
  11536. (if buf
  11537. buf ; just return it
  11538. ;; Make a new buffer and remember it
  11539. (setq buf (find-file-noselect file))
  11540. (if buf (push buf org-agenda-new-buffers))
  11541. buf)))
  11542. (defun org-release-buffers (blist)
  11543. "Release all buffers in list, asking the user for confirmation when needed.
  11544. When a buffer is unmodified, it is just killed. When modified, it is saved
  11545. \(if the user agrees) and then killed."
  11546. (let (buf file)
  11547. (while (setq buf (pop blist))
  11548. (setq file (buffer-file-name buf))
  11549. (when (and (buffer-modified-p buf)
  11550. file
  11551. (y-or-n-p (format "Save file %s? " file)))
  11552. (with-current-buffer buf (save-buffer)))
  11553. (kill-buffer buf))))
  11554. (defun org-prepare-agenda-buffers (files)
  11555. "Create buffers for all agenda files, protect archived trees and comments."
  11556. (interactive)
  11557. (let ((pa '(:org-archived t))
  11558. (pc '(:org-comment t))
  11559. (pall '(:org-archived t :org-comment t))
  11560. (inhibit-read-only t)
  11561. (rea (concat ":" org-archive-tag ":"))
  11562. bmp file re)
  11563. (save-excursion
  11564. (save-restriction
  11565. (while (setq file (pop files))
  11566. (if (bufferp file)
  11567. (set-buffer file)
  11568. (org-check-agenda-file file)
  11569. (set-buffer (org-get-agenda-file-buffer file)))
  11570. (widen)
  11571. (setq bmp (buffer-modified-p))
  11572. (org-refresh-category-properties)
  11573. (setq org-todo-keywords-for-agenda
  11574. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11575. (setq org-done-keywords-for-agenda
  11576. (append org-done-keywords-for-agenda org-done-keywords))
  11577. (setq org-todo-keyword-alist-for-agenda
  11578. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11579. (setq org-tag-alist-for-agenda
  11580. (append org-tag-alist-for-agenda org-tag-alist))
  11581. (save-excursion
  11582. (remove-text-properties (point-min) (point-max) pall)
  11583. (when org-agenda-skip-archived-trees
  11584. (goto-char (point-min))
  11585. (while (re-search-forward rea nil t)
  11586. (if (org-on-heading-p t)
  11587. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11588. (goto-char (point-min))
  11589. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11590. (while (re-search-forward re nil t)
  11591. (add-text-properties
  11592. (match-beginning 0) (org-end-of-subtree t) pc)))
  11593. (set-buffer-modified-p bmp))))
  11594. (setq org-todo-keyword-alist-for-agenda
  11595. (org-uniquify org-todo-keyword-alist-for-agenda)
  11596. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11597. ;;;; Embedded LaTeX
  11598. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11599. "Keymap for the minor `org-cdlatex-mode'.")
  11600. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11601. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11602. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11603. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11604. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11605. (defvar org-cdlatex-texmathp-advice-is-done nil
  11606. "Flag remembering if we have applied the advice to texmathp already.")
  11607. (define-minor-mode org-cdlatex-mode
  11608. "Toggle the minor `org-cdlatex-mode'.
  11609. This mode supports entering LaTeX environment and math in LaTeX fragments
  11610. in Org-mode.
  11611. \\{org-cdlatex-mode-map}"
  11612. nil " OCDL" nil
  11613. (when org-cdlatex-mode (require 'cdlatex))
  11614. (unless org-cdlatex-texmathp-advice-is-done
  11615. (setq org-cdlatex-texmathp-advice-is-done t)
  11616. (defadvice texmathp (around org-math-always-on activate)
  11617. "Always return t in org-mode buffers.
  11618. This is because we want to insert math symbols without dollars even outside
  11619. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11620. an embedded LaTeX fragment, let texmathp do its job.
  11621. \\[org-cdlatex-mode-map]"
  11622. (interactive)
  11623. (let (p)
  11624. (cond
  11625. ((not (org-mode-p)) ad-do-it)
  11626. ((eq this-command 'cdlatex-math-symbol)
  11627. (setq ad-return-value t
  11628. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11629. (t
  11630. (let ((p (org-inside-LaTeX-fragment-p)))
  11631. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11632. (setq ad-return-value t
  11633. texmathp-why '("Org-mode embedded math" . 0))
  11634. (if p ad-do-it)))))))))
  11635. (defun turn-on-org-cdlatex ()
  11636. "Unconditionally turn on `org-cdlatex-mode'."
  11637. (org-cdlatex-mode 1))
  11638. (defun org-inside-LaTeX-fragment-p ()
  11639. "Test if point is inside a LaTeX fragment.
  11640. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11641. sequence appearing also before point.
  11642. Even though the matchers for math are configurable, this function assumes
  11643. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11644. delimiters are skipped when they have been removed by customization.
  11645. The return value is nil, or a cons cell with the delimiter and
  11646. and the position of this delimiter.
  11647. This function does a reasonably good job, but can locally be fooled by
  11648. for example currency specifications. For example it will assume being in
  11649. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11650. fragments that are properly closed, but during editing, we have to live
  11651. with the uncertainty caused by missing closing delimiters. This function
  11652. looks only before point, not after."
  11653. (catch 'exit
  11654. (let ((pos (point))
  11655. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11656. (lim (progn
  11657. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11658. (point)))
  11659. dd-on str (start 0) m re)
  11660. (goto-char pos)
  11661. (when dodollar
  11662. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11663. re (nth 1 (assoc "$" org-latex-regexps)))
  11664. (while (string-match re str start)
  11665. (cond
  11666. ((= (match-end 0) (length str))
  11667. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11668. ((= (match-end 0) (- (length str) 5))
  11669. (throw 'exit nil))
  11670. (t (setq start (match-end 0))))))
  11671. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11672. (goto-char pos)
  11673. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11674. (and (match-beginning 2) (throw 'exit nil))
  11675. ;; count $$
  11676. (while (re-search-backward "\\$\\$" lim t)
  11677. (setq dd-on (not dd-on)))
  11678. (goto-char pos)
  11679. (if dd-on (cons "$$" m))))))
  11680. (defun org-try-cdlatex-tab ()
  11681. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11682. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11683. - inside a LaTeX fragment, or
  11684. - after the first word in a line, where an abbreviation expansion could
  11685. insert a LaTeX environment."
  11686. (when org-cdlatex-mode
  11687. (cond
  11688. ((save-excursion
  11689. (skip-chars-backward "a-zA-Z0-9*")
  11690. (skip-chars-backward " \t")
  11691. (bolp))
  11692. (cdlatex-tab) t)
  11693. ((org-inside-LaTeX-fragment-p)
  11694. (cdlatex-tab) t)
  11695. (t nil))))
  11696. (defun org-cdlatex-underscore-caret (&optional arg)
  11697. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11698. Revert to the normal definition outside of these fragments."
  11699. (interactive "P")
  11700. (if (org-inside-LaTeX-fragment-p)
  11701. (call-interactively 'cdlatex-sub-superscript)
  11702. (let (org-cdlatex-mode)
  11703. (call-interactively (key-binding (vector last-input-event))))))
  11704. (defun org-cdlatex-math-modify (&optional arg)
  11705. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11706. Revert to the normal definition outside of these fragments."
  11707. (interactive "P")
  11708. (if (org-inside-LaTeX-fragment-p)
  11709. (call-interactively 'cdlatex-math-modify)
  11710. (let (org-cdlatex-mode)
  11711. (call-interactively (key-binding (vector last-input-event))))))
  11712. (defvar org-latex-fragment-image-overlays nil
  11713. "List of overlays carrying the images of latex fragments.")
  11714. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11715. (defun org-remove-latex-fragment-image-overlays ()
  11716. "Remove all overlays with LaTeX fragment images in current buffer."
  11717. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11718. (setq org-latex-fragment-image-overlays nil))
  11719. (defun org-preview-latex-fragment (&optional subtree)
  11720. "Preview the LaTeX fragment at point, or all locally or globally.
  11721. If the cursor is in a LaTeX fragment, create the image and overlay
  11722. it over the source code. If there is no fragment at point, display
  11723. all fragments in the current text, from one headline to the next. With
  11724. prefix SUBTREE, display all fragments in the current subtree. With a
  11725. double prefix `C-u C-u', or when the cursor is before the first headline,
  11726. display all fragments in the buffer.
  11727. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11728. (interactive "P")
  11729. (org-remove-latex-fragment-image-overlays)
  11730. (save-excursion
  11731. (save-restriction
  11732. (let (beg end at msg)
  11733. (cond
  11734. ((or (equal subtree '(16))
  11735. (not (save-excursion
  11736. (re-search-backward (concat "^" outline-regexp) nil t))))
  11737. (setq beg (point-min) end (point-max)
  11738. msg "Creating images for buffer...%s"))
  11739. ((equal subtree '(4))
  11740. (org-back-to-heading)
  11741. (setq beg (point) end (org-end-of-subtree t)
  11742. msg "Creating images for subtree...%s"))
  11743. (t
  11744. (if (setq at (org-inside-LaTeX-fragment-p))
  11745. (goto-char (max (point-min) (- (cdr at) 2)))
  11746. (org-back-to-heading))
  11747. (setq beg (point) end (progn (outline-next-heading) (point))
  11748. msg (if at "Creating image...%s"
  11749. "Creating images for entry...%s"))))
  11750. (message msg "")
  11751. (narrow-to-region beg end)
  11752. (goto-char beg)
  11753. (org-format-latex
  11754. (concat "ltxpng/" (file-name-sans-extension
  11755. (file-name-nondirectory
  11756. buffer-file-name)))
  11757. default-directory 'overlays msg at 'forbuffer)
  11758. (message msg "done. Use `C-c C-c' to remove images.")))))
  11759. (defvar org-latex-regexps
  11760. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11761. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11762. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11763. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11764. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11765. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11766. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11767. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11768. "Regular expressions for matching embedded LaTeX.")
  11769. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11770. "Replace LaTeX fragments with links to an image, and produce images."
  11771. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11772. (let* ((prefixnodir (file-name-nondirectory prefix))
  11773. (absprefix (expand-file-name prefix dir))
  11774. (todir (file-name-directory absprefix))
  11775. (opt org-format-latex-options)
  11776. (matchers (plist-get opt :matchers))
  11777. (re-list org-latex-regexps)
  11778. (cnt 0) txt link beg end re e checkdir
  11779. m n block linkfile movefile ov)
  11780. ;; Check if there are old images files with this prefix, and remove them
  11781. (when (file-directory-p todir)
  11782. (mapc 'delete-file
  11783. (directory-files
  11784. todir 'full
  11785. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11786. ;; Check the different regular expressions
  11787. (while (setq e (pop re-list))
  11788. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11789. block (if (nth 3 e) "\n\n" ""))
  11790. (when (member m matchers)
  11791. (goto-char (point-min))
  11792. (while (re-search-forward re nil t)
  11793. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11794. (not (get-text-property (match-beginning n)
  11795. 'org-protected)))
  11796. (setq txt (match-string n)
  11797. beg (match-beginning n) end (match-end n)
  11798. cnt (1+ cnt)
  11799. linkfile (format "%s_%04d.png" prefix cnt)
  11800. movefile (format "%s_%04d.png" absprefix cnt)
  11801. link (concat block "[[file:" linkfile "]]" block))
  11802. (if msg (message msg cnt))
  11803. (goto-char beg)
  11804. (unless checkdir ; make sure the directory exists
  11805. (setq checkdir t)
  11806. (or (file-directory-p todir) (make-directory todir)))
  11807. (org-create-formula-image
  11808. txt movefile opt forbuffer)
  11809. (if overlays
  11810. (progn
  11811. (setq ov (org-make-overlay beg end))
  11812. (if (featurep 'xemacs)
  11813. (progn
  11814. (org-overlay-put ov 'invisible t)
  11815. (org-overlay-put
  11816. ov 'end-glyph
  11817. (make-glyph (vector 'png :file movefile))))
  11818. (org-overlay-put
  11819. ov 'display
  11820. (list 'image :type 'png :file movefile :ascent 'center)))
  11821. (push ov org-latex-fragment-image-overlays)
  11822. (goto-char end))
  11823. (delete-region beg end)
  11824. (insert link))))))))
  11825. ;; This function borrows from Ganesh Swami's latex2png.el
  11826. (defun org-create-formula-image (string tofile options buffer)
  11827. (let* ((tmpdir (if (featurep 'xemacs)
  11828. (temp-directory)
  11829. temporary-file-directory))
  11830. (texfilebase (make-temp-name
  11831. (expand-file-name "orgtex" tmpdir)))
  11832. (texfile (concat texfilebase ".tex"))
  11833. (dvifile (concat texfilebase ".dvi"))
  11834. (pngfile (concat texfilebase ".png"))
  11835. (fnh (if (featurep 'xemacs)
  11836. (font-height (get-face-font 'default))
  11837. (face-attribute 'default :height nil)))
  11838. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11839. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11840. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11841. "Black"))
  11842. (bg (or (plist-get options (if buffer :background :html-background))
  11843. "Transparent")))
  11844. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11845. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11846. (with-temp-file texfile
  11847. (insert org-format-latex-header
  11848. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11849. (let ((dir default-directory))
  11850. (condition-case nil
  11851. (progn
  11852. (cd tmpdir)
  11853. (call-process "latex" nil nil nil texfile))
  11854. (error nil))
  11855. (cd dir))
  11856. (if (not (file-exists-p dvifile))
  11857. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11858. (condition-case nil
  11859. (call-process "dvipng" nil nil nil
  11860. "-E" "-fg" fg "-bg" bg
  11861. "-D" dpi
  11862. ;;"-x" scale "-y" scale
  11863. "-T" "tight"
  11864. "-o" pngfile
  11865. dvifile)
  11866. (error nil))
  11867. (if (not (file-exists-p pngfile))
  11868. (progn (message "Failed to create png file from %s" texfile) nil)
  11869. ;; Use the requested file name and clean up
  11870. (copy-file pngfile tofile 'replace)
  11871. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11872. (delete-file (concat texfilebase e)))
  11873. pngfile))))
  11874. (defun org-dvipng-color (attr)
  11875. "Return an rgb color specification for dvipng."
  11876. (apply 'format "rgb %s %s %s"
  11877. (mapcar 'org-normalize-color
  11878. (color-values (face-attribute 'default attr nil)))))
  11879. (defun org-normalize-color (value)
  11880. "Return string to be used as color value for an RGB component."
  11881. (format "%g" (/ value 65535.0)))
  11882. ;;;; Key bindings
  11883. ;; Make `C-c C-x' a prefix key
  11884. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11885. ;; TAB key with modifiers
  11886. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11887. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11888. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11889. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11890. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11891. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11892. ;; The following line is necessary under Suse GNU/Linux
  11893. (unless (featurep 'xemacs)
  11894. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11895. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11896. (define-key org-mode-map [backtab] 'org-shifttab)
  11897. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11898. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11899. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11900. ;; Cursor keys with modifiers
  11901. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11902. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11903. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11904. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11905. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11906. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11907. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11908. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11909. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11910. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11911. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11912. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11913. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11914. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11915. ;;; Extra keys for tty access.
  11916. ;; We only set them when really needed because otherwise the
  11917. ;; menus don't show the simple keys
  11918. (when (or org-use-extra-keys
  11919. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11920. (not window-system))
  11921. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11922. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11923. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11924. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11925. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11926. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11927. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11928. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11929. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11930. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11931. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11932. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11933. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11934. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11935. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11936. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11937. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11938. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11939. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11940. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11941. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11942. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11943. ;; All the other keys
  11944. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11945. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11946. (if (boundp 'narrow-map)
  11947. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11948. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11949. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11950. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11951. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11952. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11953. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11954. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11955. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11956. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11957. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11958. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11959. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11960. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11961. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11962. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11963. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11964. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11965. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11966. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11967. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11968. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11969. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11970. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11971. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11972. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11973. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11974. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11975. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11976. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11977. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11978. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11979. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11980. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11981. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11982. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11983. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11984. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11985. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11986. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11987. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11988. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11989. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11990. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11991. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11992. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11993. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11994. (org-defkey org-mode-map "\C-m" 'org-return)
  11995. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11996. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11997. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11998. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11999. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12000. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12001. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12002. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12003. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12004. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12005. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12006. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12007. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12008. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12009. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12010. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12011. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12012. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12013. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12014. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12015. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12016. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12017. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12018. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12019. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12020. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12021. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12022. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12023. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12024. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12025. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12026. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12027. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12028. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12029. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12030. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12031. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12032. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12033. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12034. (when (featurep 'xemacs)
  12035. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12036. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12037. (defun org-self-insert-command (N)
  12038. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12039. If the cursor is in a table looking at whitespace, the whitespace is
  12040. overwritten, and the table is not marked as requiring realignment."
  12041. (interactive "p")
  12042. (if (and (org-table-p)
  12043. (progn
  12044. ;; check if we blank the field, and if that triggers align
  12045. (and (featurep 'org-table) org-table-auto-blank-field
  12046. (member last-command
  12047. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12048. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12049. ;; got extra space, this field does not determine column width
  12050. (let (org-table-may-need-update) (org-table-blank-field))
  12051. ;; no extra space, this field may determine column width
  12052. (org-table-blank-field)))
  12053. t)
  12054. (eq N 1)
  12055. (looking-at "[^|\n]* |"))
  12056. (let (org-table-may-need-update)
  12057. (goto-char (1- (match-end 0)))
  12058. (delete-backward-char 1)
  12059. (goto-char (match-beginning 0))
  12060. (self-insert-command N))
  12061. (setq org-table-may-need-update t)
  12062. (self-insert-command N)
  12063. (org-fix-tags-on-the-fly)))
  12064. (defun org-fix-tags-on-the-fly ()
  12065. (when (and (equal (char-after (point-at-bol)) ?*)
  12066. (org-on-heading-p))
  12067. (org-align-tags-here org-tags-column)))
  12068. (defun org-delete-backward-char (N)
  12069. "Like `delete-backward-char', insert whitespace at field end in tables.
  12070. When deleting backwards, in tables this function will insert whitespace in
  12071. front of the next \"|\" separator, to keep the table aligned. The table will
  12072. still be marked for re-alignment if the field did fill the entire column,
  12073. because, in this case the deletion might narrow the column."
  12074. (interactive "p")
  12075. (if (and (org-table-p)
  12076. (eq N 1)
  12077. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12078. (looking-at ".*?|"))
  12079. (let ((pos (point))
  12080. (noalign (looking-at "[^|\n\r]* |"))
  12081. (c org-table-may-need-update))
  12082. (backward-delete-char N)
  12083. (skip-chars-forward "^|")
  12084. (insert " ")
  12085. (goto-char (1- pos))
  12086. ;; noalign: if there were two spaces at the end, this field
  12087. ;; does not determine the width of the column.
  12088. (if noalign (setq org-table-may-need-update c)))
  12089. (backward-delete-char N)
  12090. (org-fix-tags-on-the-fly)))
  12091. (defun org-delete-char (N)
  12092. "Like `delete-char', but insert whitespace at field end in tables.
  12093. When deleting characters, in tables this function will insert whitespace in
  12094. front of the next \"|\" separator, to keep the table aligned. The table will
  12095. still be marked for re-alignment if the field did fill the entire column,
  12096. because, in this case the deletion might narrow the column."
  12097. (interactive "p")
  12098. (if (and (org-table-p)
  12099. (not (bolp))
  12100. (not (= (char-after) ?|))
  12101. (eq N 1))
  12102. (if (looking-at ".*?|")
  12103. (let ((pos (point))
  12104. (noalign (looking-at "[^|\n\r]* |"))
  12105. (c org-table-may-need-update))
  12106. (replace-match (concat
  12107. (substring (match-string 0) 1 -1)
  12108. " |"))
  12109. (goto-char pos)
  12110. ;; noalign: if there were two spaces at the end, this field
  12111. ;; does not determine the width of the column.
  12112. (if noalign (setq org-table-may-need-update c)))
  12113. (delete-char N))
  12114. (delete-char N)
  12115. (org-fix-tags-on-the-fly)))
  12116. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12117. (put 'org-self-insert-command 'delete-selection t)
  12118. (put 'orgtbl-self-insert-command 'delete-selection t)
  12119. (put 'org-delete-char 'delete-selection 'supersede)
  12120. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12121. (put 'org-yank 'delete-selection 'yank)
  12122. ;; Make `flyspell-mode' delay after some commands
  12123. (put 'org-self-insert-command 'flyspell-delayed t)
  12124. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12125. (put 'org-delete-char 'flyspell-delayed t)
  12126. (put 'org-delete-backward-char 'flyspell-delayed t)
  12127. ;; Make pabbrev-mode expand after org-mode commands
  12128. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12129. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12130. ;; How to do this: Measure non-white length of current string
  12131. ;; If equal to column width, we should realign.
  12132. (defun org-remap (map &rest commands)
  12133. "In MAP, remap the functions given in COMMANDS.
  12134. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12135. (let (new old)
  12136. (while commands
  12137. (setq old (pop commands) new (pop commands))
  12138. (if (fboundp 'command-remapping)
  12139. (org-defkey map (vector 'remap old) new)
  12140. (substitute-key-definition old new map global-map)))))
  12141. (when (eq org-enable-table-editor 'optimized)
  12142. ;; If the user wants maximum table support, we need to hijack
  12143. ;; some standard editing functions
  12144. (org-remap org-mode-map
  12145. 'self-insert-command 'org-self-insert-command
  12146. 'delete-char 'org-delete-char
  12147. 'delete-backward-char 'org-delete-backward-char)
  12148. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12149. (defun org-modifier-cursor-error ()
  12150. "Throw an error, a modified cursor command was applied in wrong context."
  12151. (error "This command is active in special context like tables, headlines or items"))
  12152. (defun org-shiftselect-error ()
  12153. "Throw an error because Shift-Cursor command was applied in wrong context."
  12154. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12155. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12156. (error "This command works only in special context like headlines or timestamps.")))
  12157. (defun org-call-for-shift-select (cmd)
  12158. (let ((this-command-keys-shift-translated t))
  12159. (call-interactively cmd)))
  12160. (defun org-shifttab (&optional arg)
  12161. "Global visibility cycling or move to previous table field.
  12162. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12163. on context.
  12164. See the individual commands for more information."
  12165. (interactive "P")
  12166. (cond
  12167. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12168. ((integerp arg)
  12169. (message "Content view to level: %d" arg)
  12170. (org-content (prefix-numeric-value arg))
  12171. (setq org-cycle-global-status 'overview))
  12172. (t (call-interactively 'org-global-cycle))))
  12173. (defun org-shiftmetaleft ()
  12174. "Promote subtree or delete table column.
  12175. Calls `org-promote-subtree', `org-outdent-item',
  12176. or `org-table-delete-column', depending on context.
  12177. See the individual commands for more information."
  12178. (interactive)
  12179. (cond
  12180. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12181. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12182. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12183. (t (org-modifier-cursor-error))))
  12184. (defun org-shiftmetaright ()
  12185. "Demote subtree or insert table column.
  12186. Calls `org-demote-subtree', `org-indent-item',
  12187. or `org-table-insert-column', depending on context.
  12188. See the individual commands for more information."
  12189. (interactive)
  12190. (cond
  12191. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12192. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12193. ((org-at-item-p) (call-interactively 'org-indent-item))
  12194. (t (org-modifier-cursor-error))))
  12195. (defun org-shiftmetaup (&optional arg)
  12196. "Move subtree up or kill table row.
  12197. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12198. `org-move-item-up' depending on context. See the individual commands
  12199. for more information."
  12200. (interactive "P")
  12201. (cond
  12202. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12203. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12204. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12205. (t (org-modifier-cursor-error))))
  12206. (defun org-shiftmetadown (&optional arg)
  12207. "Move subtree down or insert table row.
  12208. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12209. `org-move-item-down', depending on context. See the individual
  12210. commands for more information."
  12211. (interactive "P")
  12212. (cond
  12213. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12214. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12215. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12216. (t (org-modifier-cursor-error))))
  12217. (defun org-metaleft (&optional arg)
  12218. "Promote heading or move table column to left.
  12219. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12220. With no specific context, calls the Emacs default `backward-word'.
  12221. See the individual commands for more information."
  12222. (interactive "P")
  12223. (cond
  12224. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12225. ((or (org-on-heading-p) (org-region-active-p))
  12226. (call-interactively 'org-do-promote))
  12227. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12228. (t (call-interactively 'backward-word))))
  12229. (defun org-metaright (&optional arg)
  12230. "Demote subtree or move table column to right.
  12231. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12232. With no specific context, calls the Emacs default `forward-word'.
  12233. See the individual commands for more information."
  12234. (interactive "P")
  12235. (cond
  12236. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12237. ((or (org-on-heading-p) (org-region-active-p))
  12238. (call-interactively 'org-do-demote))
  12239. ((org-at-item-p) (call-interactively 'org-indent-item))
  12240. (t (call-interactively 'forward-word))))
  12241. (defun org-metaup (&optional arg)
  12242. "Move subtree up or move table row up.
  12243. Calls `org-move-subtree-up' or `org-table-move-row' or
  12244. `org-move-item-up', depending on context. See the individual commands
  12245. for more information."
  12246. (interactive "P")
  12247. (cond
  12248. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12249. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12250. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12251. (t (transpose-lines 1) (beginning-of-line -1))))
  12252. (defun org-metadown (&optional arg)
  12253. "Move subtree down or move table row down.
  12254. Calls `org-move-subtree-down' or `org-table-move-row' or
  12255. `org-move-item-down', depending on context. See the individual
  12256. commands for more information."
  12257. (interactive "P")
  12258. (cond
  12259. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12260. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12261. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12262. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12263. (defun org-shiftup (&optional arg)
  12264. "Increase item in timestamp or increase priority of current headline.
  12265. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12266. depending on context. See the individual commands for more information."
  12267. (interactive "P")
  12268. (cond
  12269. ((and org-support-shift-select (org-region-active-p))
  12270. (org-call-for-shift-select 'previous-line))
  12271. ((org-at-timestamp-p t)
  12272. (call-interactively (if org-edit-timestamp-down-means-later
  12273. 'org-timestamp-down 'org-timestamp-up)))
  12274. ((and (not (eq org-support-shift-select 'always))
  12275. (org-on-heading-p))
  12276. (call-interactively 'org-priority-up))
  12277. ((and (not org-support-shift-select) (org-at-item-p))
  12278. (call-interactively 'org-previous-item))
  12279. ((org-clocktable-try-shift 'up arg))
  12280. (org-support-shift-select
  12281. (org-call-for-shift-select 'previous-line))
  12282. (t (org-shiftselect-error))))
  12283. (defun org-shiftdown (&optional arg)
  12284. "Decrease item in timestamp or decrease priority of current headline.
  12285. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12286. depending on context. See the individual commands for more information."
  12287. (interactive "P")
  12288. (cond
  12289. ((and org-support-shift-select (org-region-active-p))
  12290. (org-call-for-shift-select 'next-line))
  12291. ((org-at-timestamp-p t)
  12292. (call-interactively (if org-edit-timestamp-down-means-later
  12293. 'org-timestamp-up 'org-timestamp-down)))
  12294. ((and (not (eq org-support-shift-select 'always))
  12295. (org-on-heading-p))
  12296. (call-interactively 'org-priority-down))
  12297. ((and (not org-support-shift-select) (org-at-item-p))
  12298. (call-interactively 'org-next-item))
  12299. ((org-clocktable-try-shift 'down arg))
  12300. (org-support-shift-select
  12301. (org-call-for-shift-select 'next-line))
  12302. (t (org-shiftselect-error))))
  12303. (defun org-shiftright (&optional arg)
  12304. "Cycle the thing at point or in the current line, depending on context.
  12305. Depending on context, this does one of the following:
  12306. - switch a timestamp at point one day into the future
  12307. - on a headline, switch to the next TODO keyword.
  12308. - on an item, switch entire list to the next bullet type
  12309. - on a property line, switch to the next allowed value
  12310. - on a clocktable definition line, move time block into the future"
  12311. (interactive "P")
  12312. (cond
  12313. ((and org-support-shift-select (org-region-active-p))
  12314. (org-call-for-shift-select 'forward-char))
  12315. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12316. ((and (not (eq org-support-shift-select 'always))
  12317. (org-on-heading-p))
  12318. (org-call-with-arg 'org-todo 'right))
  12319. ((or (and org-support-shift-select
  12320. (not (eq org-support-shift-select 'always))
  12321. (org-at-item-bullet-p))
  12322. (and (not org-support-shift-select) (org-at-item-p)))
  12323. (org-call-with-arg 'org-cycle-list-bullet nil))
  12324. ((and (not (eq org-support-shift-select 'always))
  12325. (org-at-property-p))
  12326. (call-interactively 'org-property-next-allowed-value))
  12327. ((org-clocktable-try-shift 'right arg))
  12328. (org-support-shift-select
  12329. (org-call-for-shift-select 'forward-char))
  12330. (t (org-shiftselect-error))))
  12331. (defun org-shiftleft (&optional arg)
  12332. "Cycle the thing at point or in the current line, depending on context.
  12333. Depending on context, this does one of the following:
  12334. - switch a timestamp at point one day into the past
  12335. - on a headline, switch to the previous TODO keyword.
  12336. - on an item, switch entire list to the previous bullet type
  12337. - on a property line, switch to the previous allowed value
  12338. - on a clocktable definition line, move time block into the past"
  12339. (interactive "P")
  12340. (cond
  12341. ((and org-support-shift-select (org-region-active-p))
  12342. (org-call-for-shift-select 'backward-char))
  12343. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12344. ((and (not (eq org-support-shift-select 'always))
  12345. (org-on-heading-p))
  12346. (org-call-with-arg 'org-todo 'left))
  12347. ((or (and org-support-shift-select
  12348. (not (eq org-support-shift-select 'always))
  12349. (org-at-item-bullet-p))
  12350. (and (not org-support-shift-select) (org-at-item-p)))
  12351. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12352. ((and (not (eq org-support-shift-select 'always))
  12353. (org-at-property-p))
  12354. (call-interactively 'org-property-previous-allowed-value))
  12355. ((org-clocktable-try-shift 'left arg))
  12356. (org-support-shift-select
  12357. (org-call-for-shift-select 'backward-char))
  12358. (t (org-shiftselect-error))))
  12359. (defun org-shiftcontrolright ()
  12360. "Switch to next TODO set."
  12361. (interactive)
  12362. (cond
  12363. ((and org-support-shift-select (org-region-active-p))
  12364. (org-call-for-shift-select 'forward-word))
  12365. ((and (not (eq org-support-shift-select 'always))
  12366. (org-on-heading-p))
  12367. (org-call-with-arg 'org-todo 'nextset))
  12368. (org-support-shift-select
  12369. (org-call-for-shift-select 'forward-word))
  12370. (t (org-shiftselect-error))))
  12371. (defun org-shiftcontrolleft ()
  12372. "Switch to previous TODO set."
  12373. (interactive)
  12374. (cond
  12375. ((and org-support-shift-select (org-region-active-p))
  12376. (org-call-for-shift-select 'backward-word))
  12377. ((and (not (eq org-support-shift-select 'always))
  12378. (org-on-heading-p))
  12379. (org-call-with-arg 'org-todo 'previousset))
  12380. (org-support-shift-select
  12381. (org-call-for-shift-select 'backward-word))
  12382. (t (org-shiftselect-error))))
  12383. (defun org-ctrl-c-ret ()
  12384. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12385. (interactive)
  12386. (cond
  12387. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12388. (t (call-interactively 'org-insert-heading))))
  12389. (defun org-copy-special ()
  12390. "Copy region in table or copy current subtree.
  12391. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12392. See the individual commands for more information."
  12393. (interactive)
  12394. (call-interactively
  12395. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12396. (defun org-cut-special ()
  12397. "Cut region in table or cut current subtree.
  12398. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12399. See the individual commands for more information."
  12400. (interactive)
  12401. (call-interactively
  12402. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12403. (defun org-paste-special (arg)
  12404. "Paste rectangular region into table, or past subtree relative to level.
  12405. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12406. See the individual commands for more information."
  12407. (interactive "P")
  12408. (if (org-at-table-p)
  12409. (org-table-paste-rectangle)
  12410. (org-paste-subtree arg)))
  12411. (defun org-edit-special ()
  12412. "Call a special editor for the stuff at point.
  12413. When at a table, call the formula editor with `org-table-edit-formulas'.
  12414. When at the first line of an src example, call `org-edit-src-code'.
  12415. When in an #+include line, visit the include file. Otherwise call
  12416. `ffap' to visit the file at point."
  12417. (interactive)
  12418. (cond
  12419. ((org-at-table-p)
  12420. (call-interactively 'org-table-edit-formulas))
  12421. ((save-excursion
  12422. (beginning-of-line 1)
  12423. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12424. (find-file (org-trim (match-string 1))))
  12425. ((org-edit-src-code))
  12426. ((org-edit-fixed-width-region))
  12427. (t (call-interactively 'ffap))))
  12428. (defun org-ctrl-c-ctrl-c (&optional arg)
  12429. "Set tags in headline, or update according to changed information at point.
  12430. This command does many different things, depending on context:
  12431. - If the cursor is in a headline, prompt for tags and insert them
  12432. into the current line, aligned to `org-tags-column'. When called
  12433. with prefix arg, realign all tags in the current buffer.
  12434. - If the cursor is in one of the special #+KEYWORD lines, this
  12435. triggers scanning the buffer for these lines and updating the
  12436. information.
  12437. - If the cursor is inside a table, realign the table. This command
  12438. works even if the automatic table editor has been turned off.
  12439. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12440. the entire table.
  12441. - If the cursor is at a footnote reference or definition, jump to
  12442. the corresponding definition or references, respectively.
  12443. - If the cursor is a the beginning of a dynamic block, update it.
  12444. - If the cursor is inside a table created by the table.el package,
  12445. activate that table.
  12446. - If the current buffer is a remember buffer, close note and file
  12447. it. A prefix argument of 1 files to the default location
  12448. without further interaction. A prefix argument of 2 files to
  12449. the currently clocking task.
  12450. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12451. links in this buffer.
  12452. - If the cursor is on a numbered item in a plain list, renumber the
  12453. ordered list.
  12454. - If the cursor is on a checkbox, toggle it."
  12455. (interactive "P")
  12456. (let ((org-enable-table-editor t))
  12457. (cond
  12458. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12459. org-occur-highlights
  12460. org-latex-fragment-image-overlays)
  12461. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12462. (org-remove-occur-highlights)
  12463. (org-remove-latex-fragment-image-overlays)
  12464. (message "Temporary highlights/overlays removed from current buffer"))
  12465. ((and (local-variable-p 'org-finish-function (current-buffer))
  12466. (fboundp org-finish-function))
  12467. (funcall org-finish-function))
  12468. ((org-at-property-p)
  12469. (call-interactively 'org-property-action))
  12470. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12471. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12472. ((org-at-table.el-p)
  12473. (require 'table)
  12474. (beginning-of-line 1)
  12475. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12476. (call-interactively 'table-recognize-table))
  12477. ((org-at-table-p)
  12478. (org-table-maybe-eval-formula)
  12479. (if arg
  12480. (call-interactively 'org-table-recalculate)
  12481. (org-table-maybe-recalculate-line))
  12482. (call-interactively 'org-table-align))
  12483. ((or (org-footnote-at-reference-p)
  12484. (org-footnote-at-definition-p))
  12485. (call-interactively 'org-footnote-action))
  12486. ((org-at-item-checkbox-p)
  12487. (call-interactively 'org-toggle-checkbox))
  12488. ((org-at-item-p)
  12489. (call-interactively 'org-maybe-renumber-ordered-list))
  12490. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12491. ;; Dynamic block
  12492. (beginning-of-line 1)
  12493. (save-excursion (org-update-dblock)))
  12494. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12495. (cond
  12496. ((equal (match-string 1) "TBLFM")
  12497. ;; Recalculate the table before this line
  12498. (save-excursion
  12499. (beginning-of-line 1)
  12500. (skip-chars-backward " \r\n\t")
  12501. (if (org-at-table-p)
  12502. (org-call-with-arg 'org-table-recalculate t))))
  12503. (t
  12504. ; (org-set-regexps-and-options)
  12505. ; (org-restart-font-lock)
  12506. (let ((org-inhibit-startup t)) (org-mode-restart))
  12507. (message "Local setup has been refreshed"))))
  12508. (t (error "C-c C-c can do nothing useful at this location.")))))
  12509. (defun org-mode-restart ()
  12510. "Restart Org-mode, to scan again for special lines.
  12511. Also updates the keyword regular expressions."
  12512. (interactive)
  12513. (org-mode)
  12514. (message "Org-mode restarted"))
  12515. (defun org-kill-note-or-show-branches ()
  12516. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12517. (interactive)
  12518. (if (not org-finish-function)
  12519. (call-interactively 'show-branches)
  12520. (let ((org-note-abort t))
  12521. (funcall org-finish-function))))
  12522. (defun org-return (&optional indent)
  12523. "Goto next table row or insert a newline.
  12524. Calls `org-table-next-row' or `newline', depending on context.
  12525. See the individual commands for more information."
  12526. (interactive)
  12527. (cond
  12528. ((bobp) (if indent (newline-and-indent) (newline)))
  12529. ((org-at-table-p)
  12530. (org-table-justify-field-maybe)
  12531. (call-interactively 'org-table-next-row))
  12532. ((and org-return-follows-link
  12533. (eq (get-text-property (point) 'face) 'org-link))
  12534. (call-interactively 'org-open-at-point))
  12535. ((and (org-at-heading-p)
  12536. (looking-at
  12537. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12538. (org-show-entry)
  12539. (end-of-line 1)
  12540. (newline))
  12541. (t (if indent (newline-and-indent) (newline)))))
  12542. (defun org-return-indent ()
  12543. "Goto next table row or insert a newline and indent.
  12544. Calls `org-table-next-row' or `newline-and-indent', depending on
  12545. context. See the individual commands for more information."
  12546. (interactive)
  12547. (org-return t))
  12548. (defun org-ctrl-c-star ()
  12549. "Compute table, or change heading status of lines.
  12550. Calls `org-table-recalculate' or `org-toggle-heading',
  12551. depending on context."
  12552. (interactive)
  12553. (cond
  12554. ((org-at-table-p)
  12555. (call-interactively 'org-table-recalculate))
  12556. (t
  12557. ;; Convert all lines in region to list items
  12558. (call-interactively 'org-toggle-heading))))
  12559. (defun org-ctrl-c-minus ()
  12560. "Insert separator line in table or modify bullet status of line.
  12561. Also turns a plain line or a region of lines into list items.
  12562. Calls `org-table-insert-hline', `org-toggle-item', or
  12563. `org-cycle-list-bullet', depending on context."
  12564. (interactive)
  12565. (cond
  12566. ((org-at-table-p)
  12567. (call-interactively 'org-table-insert-hline))
  12568. ((org-region-active-p)
  12569. (call-interactively 'org-toggle-item))
  12570. ((org-in-item-p)
  12571. (call-interactively 'org-cycle-list-bullet))
  12572. (t
  12573. (call-interactively 'org-toggle-item))))
  12574. (defun org-toggle-item ()
  12575. "Convert headings or normal lines to items, items to normal lines.
  12576. If there is no active region, only the current line is considered.
  12577. If the first line in the region is a headline, convert all headlines to items.
  12578. If the first line in the region is an item, convert all items to normal lines.
  12579. If the first line is normal text, add an item bullet to each line."
  12580. (interactive)
  12581. (let (l2 l beg end)
  12582. (if (org-region-active-p)
  12583. (setq beg (region-beginning) end (region-end))
  12584. (setq beg (point-at-bol)
  12585. end (min (1+ (point-at-eol)) (point-max))))
  12586. (save-excursion
  12587. (goto-char end)
  12588. (setq l2 (org-current-line))
  12589. (goto-char beg)
  12590. (beginning-of-line 1)
  12591. (setq l (1- (org-current-line)))
  12592. (if (org-at-item-p)
  12593. ;; We already have items, de-itemize
  12594. (while (< (setq l (1+ l)) l2)
  12595. (when (org-at-item-p)
  12596. (goto-char (match-beginning 2))
  12597. (delete-region (match-beginning 2) (match-end 2))
  12598. (and (looking-at "[ \t]+") (replace-match "")))
  12599. (beginning-of-line 2))
  12600. (if (org-on-heading-p)
  12601. ;; Headings, convert to items
  12602. (while (< (setq l (1+ l)) l2)
  12603. (if (looking-at org-outline-regexp)
  12604. (replace-match "- " t t))
  12605. (beginning-of-line 2))
  12606. ;; normal lines, turn them into items
  12607. (while (< (setq l (1+ l)) l2)
  12608. (unless (org-at-item-p)
  12609. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12610. (replace-match "\\1- \\2")))
  12611. (beginning-of-line 2)))))))
  12612. (defun org-toggle-heading (&optional nstars)
  12613. "Convert headings to normal text, or items or text to headings.
  12614. If there is no active region, only the current line is considered.
  12615. If the first line is a heading, remove the stars from all headlines
  12616. in the region.
  12617. If the first line is a plain list item, turn all plain list items into
  12618. headings.
  12619. If the first line is a normal line, turn each and every line in the region
  12620. into a heading.
  12621. When converting a line into a heading, the number of stars is chosen
  12622. such that the lines become children of the current entry. However, when
  12623. a prefix argument is given, its value determines the number of stars to add."
  12624. (interactive "P")
  12625. (let (l2 l itemp beg end)
  12626. (if (org-region-active-p)
  12627. (setq beg (region-beginning) end (region-end))
  12628. (setq beg (point-at-bol)
  12629. end (min (1+ (point-at-eol)) (point-max))))
  12630. (save-excursion
  12631. (goto-char end)
  12632. (setq l2 (org-current-line))
  12633. (goto-char beg)
  12634. (beginning-of-line 1)
  12635. (setq l (1- (org-current-line)))
  12636. (if (org-on-heading-p)
  12637. ;; We already have headlines, de-star them
  12638. (while (< (setq l (1+ l)) l2)
  12639. (when (org-on-heading-p t)
  12640. (and (looking-at outline-regexp) (replace-match "")))
  12641. (beginning-of-line 2))
  12642. (setq itemp (org-at-item-p))
  12643. (let* ((stars
  12644. (if nstars
  12645. (make-string (prefix-numeric-value current-prefix-arg)
  12646. ?*)
  12647. (save-excursion
  12648. (re-search-backward org-complex-heading-regexp nil t)
  12649. (or (match-string 1) "*"))))
  12650. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12651. (rpl (concat stars add-stars " ")))
  12652. (while (< (setq l (1+ l)) l2)
  12653. (if itemp
  12654. (and (org-at-item-p) (replace-match rpl t t))
  12655. (unless (org-on-heading-p)
  12656. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12657. (replace-match (concat rpl (match-string 2))))))
  12658. (beginning-of-line 2)))))))
  12659. (defun org-meta-return (&optional arg)
  12660. "Insert a new heading or wrap a region in a table.
  12661. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12662. See the individual commands for more information."
  12663. (interactive "P")
  12664. (cond
  12665. ((org-at-table-p)
  12666. (call-interactively 'org-table-wrap-region))
  12667. (t (call-interactively 'org-insert-heading))))
  12668. ;;; Menu entries
  12669. ;; Define the Org-mode menus
  12670. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12671. '("Tbl"
  12672. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12673. ["Next Field" org-cycle (org-at-table-p)]
  12674. ["Previous Field" org-shifttab (org-at-table-p)]
  12675. ["Next Row" org-return (org-at-table-p)]
  12676. "--"
  12677. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12678. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12679. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12680. "--"
  12681. ("Column"
  12682. ["Move Column Left" org-metaleft (org-at-table-p)]
  12683. ["Move Column Right" org-metaright (org-at-table-p)]
  12684. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12685. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12686. ("Row"
  12687. ["Move Row Up" org-metaup (org-at-table-p)]
  12688. ["Move Row Down" org-metadown (org-at-table-p)]
  12689. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12690. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12691. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12692. "--"
  12693. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12694. ("Rectangle"
  12695. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12696. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12697. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12698. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12699. "--"
  12700. ("Calculate"
  12701. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12702. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12703. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12704. "--"
  12705. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12706. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12707. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12708. "--"
  12709. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12710. "--"
  12711. ["Sum Column/Rectangle" org-table-sum
  12712. (or (org-at-table-p) (org-region-active-p))]
  12713. ["Which Column?" org-table-current-column (org-at-table-p)])
  12714. ["Debug Formulas"
  12715. org-table-toggle-formula-debugger
  12716. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12717. ["Show Col/Row Numbers"
  12718. org-table-toggle-coordinate-overlays
  12719. :style toggle
  12720. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12721. "--"
  12722. ["Create" org-table-create (and (not (org-at-table-p))
  12723. org-enable-table-editor)]
  12724. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12725. ["Import from File" org-table-import (not (org-at-table-p))]
  12726. ["Export to File" org-table-export (org-at-table-p)]
  12727. "--"
  12728. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12729. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12730. '("Org"
  12731. ("Show/Hide"
  12732. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12733. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12734. ["Sparse Tree..." org-sparse-tree t]
  12735. ["Reveal Context" org-reveal t]
  12736. ["Show All" show-all t]
  12737. "--"
  12738. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12739. "--"
  12740. ["New Heading" org-insert-heading t]
  12741. ("Navigate Headings"
  12742. ["Up" outline-up-heading t]
  12743. ["Next" outline-next-visible-heading t]
  12744. ["Previous" outline-previous-visible-heading t]
  12745. ["Next Same Level" outline-forward-same-level t]
  12746. ["Previous Same Level" outline-backward-same-level t]
  12747. "--"
  12748. ["Jump" org-goto t])
  12749. ("Edit Structure"
  12750. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12751. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12752. "--"
  12753. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12754. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12755. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12756. "--"
  12757. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12758. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12759. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12760. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12761. "--"
  12762. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12763. "--"
  12764. ["Convert to odd levels" org-convert-to-odd-levels t]
  12765. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12766. ("Editing"
  12767. ["Emphasis..." org-emphasize t]
  12768. ["Edit Source Example" org-edit-special t]
  12769. "--"
  12770. ["Footnote new/jump" org-footnote-action t]
  12771. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12772. ("Archive"
  12773. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12774. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12775. ; :active t :keys "C-u C-c C-x C-a"]
  12776. ["Sparse trees open ARCHIVE trees"
  12777. (setq org-sparse-tree-open-archived-trees
  12778. (not org-sparse-tree-open-archived-trees))
  12779. :style toggle :selected org-sparse-tree-open-archived-trees]
  12780. ["Cycling opens ARCHIVE trees"
  12781. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12782. :style toggle :selected org-cycle-open-archived-trees]
  12783. "--"
  12784. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12785. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12786. ; ["Check and Move Children" (org-archive-subtree '(4))
  12787. ; :active t :keys "C-u C-c C-x C-s"]
  12788. )
  12789. "--"
  12790. ("TODO Lists"
  12791. ["TODO/DONE/-" org-todo t]
  12792. ("Select keyword"
  12793. ["Next keyword" org-shiftright (org-on-heading-p)]
  12794. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12795. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12796. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12797. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12798. ["Show TODO Tree" org-show-todo-tree t]
  12799. ["Global TODO list" org-todo-list t]
  12800. "--"
  12801. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12802. :selected org-enforce-todo-dependencies :style toggle :active t]
  12803. "Settings for tree at point"
  12804. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12805. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12806. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12807. ["Do Children parallel" org-toggle-ordered-property :style radio
  12808. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12809. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12810. "--"
  12811. ["Set Priority" org-priority t]
  12812. ["Priority Up" org-shiftup t]
  12813. ["Priority Down" org-shiftdown t])
  12814. ("TAGS and Properties"
  12815. ["Set Tags" org-set-tags-command t]
  12816. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12817. "--"
  12818. ["Set property" org-set-property t]
  12819. ["Column view of properties" org-columns t]
  12820. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12821. ("Dates and Scheduling"
  12822. ["Timestamp" org-time-stamp t]
  12823. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12824. ("Change Date"
  12825. ["1 Day Later" org-shiftright t]
  12826. ["1 Day Earlier" org-shiftleft t]
  12827. ["1 ... Later" org-shiftup t]
  12828. ["1 ... Earlier" org-shiftdown t])
  12829. ["Compute Time Range" org-evaluate-time-range t]
  12830. ["Schedule Item" org-schedule t]
  12831. ["Deadline" org-deadline t]
  12832. "--"
  12833. ["Custom time format" org-toggle-time-stamp-overlays
  12834. :style radio :selected org-display-custom-times]
  12835. "--"
  12836. ["Goto Calendar" org-goto-calendar t]
  12837. ["Date from Calendar" org-date-from-calendar t]
  12838. "--"
  12839. ["Start/Restart Timer" org-timer-start t]
  12840. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12841. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12842. ["Insert Timer String" org-timer t]
  12843. ["Insert Timer Item" org-timer-item t])
  12844. ("Logging work"
  12845. ["Clock in" org-clock-in t]
  12846. ["Clock out" org-clock-out t]
  12847. ["Clock cancel" org-clock-cancel t]
  12848. ["Goto running clock" org-clock-goto t]
  12849. ["Display times" org-clock-display t]
  12850. ["Create clock table" org-clock-report t]
  12851. "--"
  12852. ["Record DONE time"
  12853. (progn (setq org-log-done (not org-log-done))
  12854. (message "Switching to %s will %s record a timestamp"
  12855. (car org-done-keywords)
  12856. (if org-log-done "automatically" "not")))
  12857. :style toggle :selected org-log-done])
  12858. "--"
  12859. ["Agenda Command..." org-agenda t]
  12860. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12861. ("File List for Agenda")
  12862. ("Special views current file"
  12863. ["TODO Tree" org-show-todo-tree t]
  12864. ["Check Deadlines" org-check-deadlines t]
  12865. ["Timeline" org-timeline t]
  12866. ["Tags Tree" org-tags-sparse-tree t])
  12867. "--"
  12868. ("Hyperlinks"
  12869. ["Store Link (Global)" org-store-link t]
  12870. ["Insert Link" org-insert-link t]
  12871. ["Follow Link" org-open-at-point t]
  12872. "--"
  12873. ["Next link" org-next-link t]
  12874. ["Previous link" org-previous-link t]
  12875. "--"
  12876. ["Descriptive Links"
  12877. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12878. :style radio
  12879. :selected (member '(org-link) buffer-invisibility-spec)]
  12880. ["Literal Links"
  12881. (progn
  12882. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12883. :style radio
  12884. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12885. "--"
  12886. ["Export/Publish..." org-export t]
  12887. ("LaTeX"
  12888. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12889. :selected org-cdlatex-mode]
  12890. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12891. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12892. ["Modify math symbol" org-cdlatex-math-modify
  12893. (org-inside-LaTeX-fragment-p)]
  12894. ["Export LaTeX fragments as images"
  12895. (if (featurep 'org-exp)
  12896. (setq org-export-with-LaTeX-fragments
  12897. (not org-export-with-LaTeX-fragments))
  12898. (require 'org-exp))
  12899. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12900. org-export-with-LaTeX-fragments)])
  12901. "--"
  12902. ("Documentation"
  12903. ["Show Version" org-version t]
  12904. ["Info Documentation" org-info t])
  12905. ("Customize"
  12906. ["Browse Org Group" org-customize t]
  12907. "--"
  12908. ["Expand This Menu" org-create-customize-menu
  12909. (fboundp 'customize-menu-create)])
  12910. "--"
  12911. ["Refresh setup" org-mode-restart t]
  12912. ))
  12913. (defun org-info (&optional node)
  12914. "Read documentation for Org-mode in the info system.
  12915. With optional NODE, go directly to that node."
  12916. (interactive)
  12917. (info (format "(org)%s" (or node ""))))
  12918. (defun org-install-agenda-files-menu ()
  12919. (let ((bl (buffer-list)))
  12920. (save-excursion
  12921. (while bl
  12922. (set-buffer (pop bl))
  12923. (if (org-mode-p) (setq bl nil)))
  12924. (when (org-mode-p)
  12925. (easy-menu-change
  12926. '("Org") "File List for Agenda"
  12927. (append
  12928. (list
  12929. ["Edit File List" (org-edit-agenda-file-list) t]
  12930. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12931. ["Remove Current File from List" org-remove-file t]
  12932. ["Cycle through agenda files" org-cycle-agenda-files t]
  12933. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12934. "--")
  12935. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12936. ;;;; Documentation
  12937. ;;;###autoload
  12938. (defun org-require-autoloaded-modules ()
  12939. (interactive)
  12940. (mapc 'require
  12941. '(org-agenda org-archive org-attach org-clock org-colview
  12942. org-exp org-id org-export-latex org-publish
  12943. org-remember org-table org-timer)))
  12944. ;;;###autoload
  12945. (defun org-customize ()
  12946. "Call the customize function with org as argument."
  12947. (interactive)
  12948. (org-load-modules-maybe)
  12949. (org-require-autoloaded-modules)
  12950. (customize-browse 'org))
  12951. (defun org-create-customize-menu ()
  12952. "Create a full customization menu for Org-mode, insert it into the menu."
  12953. (interactive)
  12954. (org-load-modules-maybe)
  12955. (org-require-autoloaded-modules)
  12956. (if (fboundp 'customize-menu-create)
  12957. (progn
  12958. (easy-menu-change
  12959. '("Org") "Customize"
  12960. `(["Browse Org group" org-customize t]
  12961. "--"
  12962. ,(customize-menu-create 'org)
  12963. ["Set" Custom-set t]
  12964. ["Save" Custom-save t]
  12965. ["Reset to Current" Custom-reset-current t]
  12966. ["Reset to Saved" Custom-reset-saved t]
  12967. ["Reset to Standard Settings" Custom-reset-standard t]))
  12968. (message "\"Org\"-menu now contains full customization menu"))
  12969. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12970. ;;;; Miscellaneous stuff
  12971. ;;; Generally useful functions
  12972. (defun org-find-text-property-in-string (prop s)
  12973. "Return the first non-nil value of property PROP in string S."
  12974. (or (get-text-property 0 prop s)
  12975. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12976. prop s)))
  12977. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12978. "Display the given MESSAGE as a warning."
  12979. (if (fboundp 'display-warning)
  12980. (display-warning 'org message
  12981. (if (featurep 'xemacs)
  12982. 'warning
  12983. :warning))
  12984. (let ((buf (get-buffer-create "*Org warnings*")))
  12985. (with-current-buffer buf
  12986. (goto-char (point-max))
  12987. (insert "Warning (Org): " message)
  12988. (unless (bolp)
  12989. (newline)))
  12990. (display-buffer buf)
  12991. (sit-for 0))))
  12992. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12993. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12994. (if (and marker (marker-buffer marker)
  12995. (buffer-live-p (marker-buffer marker)))
  12996. (progn
  12997. (switch-to-buffer (marker-buffer marker))
  12998. (if (or (> marker (point-max)) (< marker (point-min)))
  12999. (widen))
  13000. (goto-char marker)
  13001. (org-show-context 'org-goto))
  13002. (if bookmark
  13003. (bookmark-jump bookmark)
  13004. (error "Cannot find location"))))
  13005. (defun org-quote-csv-field (s)
  13006. "Quote field for inclusion in CSV material."
  13007. (if (string-match "[\",]" s)
  13008. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13009. s))
  13010. (defun org-plist-delete (plist property)
  13011. "Delete PROPERTY from PLIST.
  13012. This is in contrast to merely setting it to 0."
  13013. (let (p)
  13014. (while plist
  13015. (if (not (eq property (car plist)))
  13016. (setq p (plist-put p (car plist) (nth 1 plist))))
  13017. (setq plist (cddr plist)))
  13018. p))
  13019. (defun org-force-self-insert (N)
  13020. "Needed to enforce self-insert under remapping."
  13021. (interactive "p")
  13022. (self-insert-command N))
  13023. (defun org-string-width (s)
  13024. "Compute width of string, ignoring invisible characters.
  13025. This ignores character with invisibility property `org-link', and also
  13026. characters with property `org-cwidth', because these will become invisible
  13027. upon the next fontification round."
  13028. (let (b l)
  13029. (when (or (eq t buffer-invisibility-spec)
  13030. (assq 'org-link buffer-invisibility-spec))
  13031. (while (setq b (text-property-any 0 (length s)
  13032. 'invisible 'org-link s))
  13033. (setq s (concat (substring s 0 b)
  13034. (substring s (or (next-single-property-change
  13035. b 'invisible s) (length s)))))))
  13036. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13037. (setq s (concat (substring s 0 b)
  13038. (substring s (or (next-single-property-change
  13039. b 'org-cwidth s) (length s))))))
  13040. (setq l (string-width s) b -1)
  13041. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13042. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13043. l))
  13044. (defun org-get-indentation (&optional line)
  13045. "Get the indentation of the current line, interpreting tabs.
  13046. When LINE is given, assume it represents a line and compute its indentation."
  13047. (if line
  13048. (if (string-match "^ *" (org-remove-tabs line))
  13049. (match-end 0))
  13050. (save-excursion
  13051. (beginning-of-line 1)
  13052. (skip-chars-forward " \t")
  13053. (current-column))))
  13054. (defun org-remove-tabs (s &optional width)
  13055. "Replace tabulators in S with spaces.
  13056. Assumes that s is a single line, starting in column 0."
  13057. (setq width (or width tab-width))
  13058. (while (string-match "\t" s)
  13059. (setq s (replace-match
  13060. (make-string
  13061. (- (* width (/ (+ (match-beginning 0) width) width))
  13062. (match-beginning 0)) ?\ )
  13063. t t s)))
  13064. s)
  13065. (defun org-fix-indentation (line ind)
  13066. "Fix indentation in LINE.
  13067. IND is a cons cell with target and minimum indentation.
  13068. If the current indentation in LINE is smaller than the minimum,
  13069. leave it alone. If it is larger than ind, set it to the target."
  13070. (let* ((l (org-remove-tabs line))
  13071. (i (org-get-indentation l))
  13072. (i1 (car ind)) (i2 (cdr ind)))
  13073. (if (>= i i2) (setq l (substring line i2)))
  13074. (if (> i1 0)
  13075. (concat (make-string i1 ?\ ) l)
  13076. l)))
  13077. (defun org-base-buffer (buffer)
  13078. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13079. (if (not buffer)
  13080. buffer
  13081. (or (buffer-base-buffer buffer)
  13082. buffer)))
  13083. (defun org-trim (s)
  13084. "Remove whitespace at beginning and end of string."
  13085. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13086. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13087. s)
  13088. (defun org-wrap (string &optional width lines)
  13089. "Wrap string to either a number of lines, or a width in characters.
  13090. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13091. that costs. If there is a word longer than WIDTH, the text is actually
  13092. wrapped to the length of that word.
  13093. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13094. many lines, whatever width that takes.
  13095. The return value is a list of lines, without newlines at the end."
  13096. (let* ((words (org-split-string string "[ \t\n]+"))
  13097. (maxword (apply 'max (mapcar 'org-string-width words)))
  13098. w ll)
  13099. (cond (width
  13100. (org-do-wrap words (max maxword width)))
  13101. (lines
  13102. (setq w maxword)
  13103. (setq ll (org-do-wrap words maxword))
  13104. (if (<= (length ll) lines)
  13105. ll
  13106. (setq ll words)
  13107. (while (> (length ll) lines)
  13108. (setq w (1+ w))
  13109. (setq ll (org-do-wrap words w)))
  13110. ll))
  13111. (t (error "Cannot wrap this")))))
  13112. (defun org-do-wrap (words width)
  13113. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13114. (let (lines line)
  13115. (while words
  13116. (setq line (pop words))
  13117. (while (and words (< (+ (length line) (length (car words))) width))
  13118. (setq line (concat line " " (pop words))))
  13119. (setq lines (push line lines)))
  13120. (nreverse lines)))
  13121. (defun org-split-string (string &optional separators)
  13122. "Splits STRING into substrings at SEPARATORS.
  13123. No empty strings are returned if there are matches at the beginning
  13124. and end of string."
  13125. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13126. (start 0)
  13127. notfirst
  13128. (list nil))
  13129. (while (and (string-match rexp string
  13130. (if (and notfirst
  13131. (= start (match-beginning 0))
  13132. (< start (length string)))
  13133. (1+ start) start))
  13134. (< (match-beginning 0) (length string)))
  13135. (setq notfirst t)
  13136. (or (eq (match-beginning 0) 0)
  13137. (and (eq (match-beginning 0) (match-end 0))
  13138. (eq (match-beginning 0) start))
  13139. (setq list
  13140. (cons (substring string start (match-beginning 0))
  13141. list)))
  13142. (setq start (match-end 0)))
  13143. (or (eq start (length string))
  13144. (setq list
  13145. (cons (substring string start)
  13146. list)))
  13147. (nreverse list)))
  13148. (defun org-context ()
  13149. "Return a list of contexts of the current cursor position.
  13150. If several contexts apply, all are returned.
  13151. Each context entry is a list with a symbol naming the context, and
  13152. two positions indicating start and end of the context. Possible
  13153. contexts are:
  13154. :headline anywhere in a headline
  13155. :headline-stars on the leading stars in a headline
  13156. :todo-keyword on a TODO keyword (including DONE) in a headline
  13157. :tags on the TAGS in a headline
  13158. :priority on the priority cookie in a headline
  13159. :item on the first line of a plain list item
  13160. :item-bullet on the bullet/number of a plain list item
  13161. :checkbox on the checkbox in a plain list item
  13162. :table in an org-mode table
  13163. :table-special on a special filed in a table
  13164. :table-table in a table.el table
  13165. :link on a hyperlink
  13166. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13167. :target on a <<target>>
  13168. :radio-target on a <<<radio-target>>>
  13169. :latex-fragment on a LaTeX fragment
  13170. :latex-preview on a LaTeX fragment with overlayed preview image
  13171. This function expects the position to be visible because it uses font-lock
  13172. faces as a help to recognize the following contexts: :table-special, :link,
  13173. and :keyword."
  13174. (let* ((f (get-text-property (point) 'face))
  13175. (faces (if (listp f) f (list f)))
  13176. (p (point)) clist o)
  13177. ;; First the large context
  13178. (cond
  13179. ((org-on-heading-p t)
  13180. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13181. (when (progn
  13182. (beginning-of-line 1)
  13183. (looking-at org-todo-line-tags-regexp))
  13184. (push (org-point-in-group p 1 :headline-stars) clist)
  13185. (push (org-point-in-group p 2 :todo-keyword) clist)
  13186. (push (org-point-in-group p 4 :tags) clist))
  13187. (goto-char p)
  13188. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13189. (if (looking-at "\\[#[A-Z0-9]\\]")
  13190. (push (org-point-in-group p 0 :priority) clist)))
  13191. ((org-at-item-p)
  13192. (push (org-point-in-group p 2 :item-bullet) clist)
  13193. (push (list :item (point-at-bol)
  13194. (save-excursion (org-end-of-item) (point)))
  13195. clist)
  13196. (and (org-at-item-checkbox-p)
  13197. (push (org-point-in-group p 0 :checkbox) clist)))
  13198. ((org-at-table-p)
  13199. (push (list :table (org-table-begin) (org-table-end)) clist)
  13200. (if (memq 'org-formula faces)
  13201. (push (list :table-special
  13202. (previous-single-property-change p 'face)
  13203. (next-single-property-change p 'face)) clist)))
  13204. ((org-at-table-p 'any)
  13205. (push (list :table-table) clist)))
  13206. (goto-char p)
  13207. ;; Now the small context
  13208. (cond
  13209. ((org-at-timestamp-p)
  13210. (push (org-point-in-group p 0 :timestamp) clist))
  13211. ((memq 'org-link faces)
  13212. (push (list :link
  13213. (previous-single-property-change p 'face)
  13214. (next-single-property-change p 'face)) clist))
  13215. ((memq 'org-special-keyword faces)
  13216. (push (list :keyword
  13217. (previous-single-property-change p 'face)
  13218. (next-single-property-change p 'face)) clist))
  13219. ((org-on-target-p)
  13220. (push (org-point-in-group p 0 :target) clist)
  13221. (goto-char (1- (match-beginning 0)))
  13222. (if (looking-at org-radio-target-regexp)
  13223. (push (org-point-in-group p 0 :radio-target) clist))
  13224. (goto-char p))
  13225. ((setq o (car (delq nil
  13226. (mapcar
  13227. (lambda (x)
  13228. (if (memq x org-latex-fragment-image-overlays) x))
  13229. (org-overlays-at (point))))))
  13230. (push (list :latex-fragment
  13231. (org-overlay-start o) (org-overlay-end o)) clist)
  13232. (push (list :latex-preview
  13233. (org-overlay-start o) (org-overlay-end o)) clist))
  13234. ((org-inside-LaTeX-fragment-p)
  13235. ;; FIXME: positions wrong.
  13236. (push (list :latex-fragment (point) (point)) clist)))
  13237. (setq clist (nreverse (delq nil clist)))
  13238. clist))
  13239. ;; FIXME: Compare with at-regexp-p Do we need both?
  13240. (defun org-in-regexp (re &optional nlines visually)
  13241. "Check if point is inside a match of regexp.
  13242. Normally only the current line is checked, but you can include NLINES extra
  13243. lines both before and after point into the search.
  13244. If VISUALLY is set, require that the cursor is not after the match but
  13245. really on, so that the block visually is on the match."
  13246. (catch 'exit
  13247. (let ((pos (point))
  13248. (eol (point-at-eol (+ 1 (or nlines 0))))
  13249. (inc (if visually 1 0)))
  13250. (save-excursion
  13251. (beginning-of-line (- 1 (or nlines 0)))
  13252. (while (re-search-forward re eol t)
  13253. (if (and (<= (match-beginning 0) pos)
  13254. (>= (+ inc (match-end 0)) pos))
  13255. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13256. (defun org-at-regexp-p (regexp)
  13257. "Is point inside a match of REGEXP in the current line?"
  13258. (catch 'exit
  13259. (save-excursion
  13260. (let ((pos (point)) (end (point-at-eol)))
  13261. (beginning-of-line 1)
  13262. (while (re-search-forward regexp end t)
  13263. (if (and (<= (match-beginning 0) pos)
  13264. (>= (match-end 0) pos))
  13265. (throw 'exit t)))
  13266. nil))))
  13267. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13268. "Call `multi-occur' with buffers for all agenda files."
  13269. (interactive "sOrg-files matching: \np")
  13270. (let* ((files (org-agenda-files))
  13271. (tnames (mapcar 'file-truename files))
  13272. (extra org-agenda-text-search-extra-files)
  13273. f)
  13274. (when (eq (car extra) 'agenda-archives)
  13275. (setq extra (cdr extra))
  13276. (setq files (org-add-archive-files files)))
  13277. (while (setq f (pop extra))
  13278. (unless (member (file-truename f) tnames)
  13279. (add-to-list 'files f 'append)
  13280. (add-to-list 'tnames (file-truename f) 'append)))
  13281. (multi-occur
  13282. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13283. regexp)))
  13284. (if (boundp 'occur-mode-find-occurrence-hook)
  13285. ;; Emacs 23
  13286. (add-hook 'occur-mode-find-occurrence-hook
  13287. (lambda ()
  13288. (when (org-mode-p)
  13289. (org-reveal))))
  13290. ;; Emacs 22
  13291. (defadvice occur-mode-goto-occurrence
  13292. (after org-occur-reveal activate)
  13293. (and (org-mode-p) (org-reveal)))
  13294. (defadvice occur-mode-goto-occurrence-other-window
  13295. (after org-occur-reveal activate)
  13296. (and (org-mode-p) (org-reveal)))
  13297. (defadvice occur-mode-display-occurrence
  13298. (after org-occur-reveal activate)
  13299. (when (org-mode-p)
  13300. (let ((pos (occur-mode-find-occurrence)))
  13301. (with-current-buffer (marker-buffer pos)
  13302. (save-excursion
  13303. (goto-char pos)
  13304. (org-reveal)))))))
  13305. (defun org-uniquify (list)
  13306. "Remove duplicate elements from LIST."
  13307. (let (res)
  13308. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13309. res))
  13310. (defun org-delete-all (elts list)
  13311. "Remove all elements in ELTS from LIST."
  13312. (while elts
  13313. (setq list (delete (pop elts) list)))
  13314. list)
  13315. (defun org-back-over-empty-lines ()
  13316. "Move backwards over whitespace, to the beginning of the first empty line.
  13317. Returns the number of empty lines passed."
  13318. (let ((pos (point)))
  13319. (skip-chars-backward " \t\n\r")
  13320. (beginning-of-line 2)
  13321. (goto-char (min (point) pos))
  13322. (count-lines (point) pos)))
  13323. (defun org-skip-whitespace ()
  13324. (skip-chars-forward " \t\n\r"))
  13325. (defun org-point-in-group (point group &optional context)
  13326. "Check if POINT is in match-group GROUP.
  13327. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13328. match. If the match group does ot exist or point is not inside it,
  13329. return nil."
  13330. (and (match-beginning group)
  13331. (>= point (match-beginning group))
  13332. (<= point (match-end group))
  13333. (if context
  13334. (list context (match-beginning group) (match-end group))
  13335. t)))
  13336. (defun org-switch-to-buffer-other-window (&rest args)
  13337. "Switch to buffer in a second window on the current frame.
  13338. In particular, do not allow pop-up frames."
  13339. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13340. special-display-function)
  13341. (apply 'switch-to-buffer-other-window args)))
  13342. (defun org-combine-plists (&rest plists)
  13343. "Create a single property list from all plists in PLISTS.
  13344. The process starts by copying the first list, and then setting properties
  13345. from the other lists. Settings in the last list are the most significant
  13346. ones and overrule settings in the other lists."
  13347. (let ((rtn (copy-sequence (pop plists)))
  13348. p v ls)
  13349. (while plists
  13350. (setq ls (pop plists))
  13351. (while ls
  13352. (setq p (pop ls) v (pop ls))
  13353. (setq rtn (plist-put rtn p v))))
  13354. rtn))
  13355. (defun org-move-line-down (arg)
  13356. "Move the current line down. With prefix argument, move it past ARG lines."
  13357. (interactive "p")
  13358. (let ((col (current-column))
  13359. beg end pos)
  13360. (beginning-of-line 1) (setq beg (point))
  13361. (beginning-of-line 2) (setq end (point))
  13362. (beginning-of-line (+ 1 arg))
  13363. (setq pos (move-marker (make-marker) (point)))
  13364. (insert (delete-and-extract-region beg end))
  13365. (goto-char pos)
  13366. (org-move-to-column col)))
  13367. (defun org-move-line-up (arg)
  13368. "Move the current line up. With prefix argument, move it past ARG lines."
  13369. (interactive "p")
  13370. (let ((col (current-column))
  13371. beg end pos)
  13372. (beginning-of-line 1) (setq beg (point))
  13373. (beginning-of-line 2) (setq end (point))
  13374. (beginning-of-line (- arg))
  13375. (setq pos (move-marker (make-marker) (point)))
  13376. (insert (delete-and-extract-region beg end))
  13377. (goto-char pos)
  13378. (org-move-to-column col)))
  13379. (defun org-replace-escapes (string table)
  13380. "Replace %-escapes in STRING with values in TABLE.
  13381. TABLE is an association list with keys like \"%a\" and string values.
  13382. The sequences in STRING may contain normal field width and padding information,
  13383. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13384. so values can contain further %-escapes if they are define later in TABLE."
  13385. (let ((case-fold-search nil)
  13386. e re rpl)
  13387. (while (setq e (pop table))
  13388. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13389. (while (string-match re string)
  13390. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13391. (cdr e)))
  13392. (setq string (replace-match rpl t t string))))
  13393. string))
  13394. (defun org-sublist (list start end)
  13395. "Return a section of LIST, from START to END.
  13396. Counting starts at 1."
  13397. (let (rtn (c start))
  13398. (setq list (nthcdr (1- start) list))
  13399. (while (and list (<= c end))
  13400. (push (pop list) rtn)
  13401. (setq c (1+ c)))
  13402. (nreverse rtn)))
  13403. (defun org-find-base-buffer-visiting (file)
  13404. "Like `find-buffer-visiting' but alway return the base buffer and
  13405. not an indirect buffer."
  13406. (let ((buf (find-buffer-visiting file)))
  13407. (if buf
  13408. (or (buffer-base-buffer buf) buf)
  13409. nil)))
  13410. (defun org-image-file-name-regexp (&optional extensions)
  13411. "Return regexp matching the file names of images.
  13412. If EXTENSIONS is given, only match these."
  13413. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13414. (image-file-name-regexp)
  13415. (let ((image-file-name-extensions
  13416. (or extensions
  13417. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13418. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13419. (concat "\\."
  13420. (regexp-opt (nconc (mapcar 'upcase
  13421. image-file-name-extensions)
  13422. image-file-name-extensions)
  13423. t)
  13424. "\\'"))))
  13425. (defun org-file-image-p (file &optional extensions)
  13426. "Return non-nil if FILE is an image."
  13427. (save-match-data
  13428. (string-match (org-image-file-name-regexp extensions) file)))
  13429. (defun org-get-cursor-date ()
  13430. "Return the date at cursor in as a time.
  13431. This works in the calendar and in the agenda, anywhere else it just
  13432. returns the current time."
  13433. (let (date day defd)
  13434. (cond
  13435. ((eq major-mode 'calendar-mode)
  13436. (setq date (calendar-cursor-to-date)
  13437. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13438. ((eq major-mode 'org-agenda-mode)
  13439. (setq day (get-text-property (point) 'day))
  13440. (if day
  13441. (setq date (calendar-gregorian-from-absolute day)
  13442. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13443. (nth 2 date))))))
  13444. (or defd (current-time))))
  13445. (defvar org-agenda-action-marker (make-marker)
  13446. "Marker pointing to the entry for the next agenda action.")
  13447. (defun org-mark-entry-for-agenda-action ()
  13448. "Mark the current entry as target of an agenda action.
  13449. Agenda actions are actions executed from the agenda with the key `k',
  13450. which make use of the date at the cursor."
  13451. (interactive)
  13452. (move-marker org-agenda-action-marker
  13453. (save-excursion (org-back-to-heading t) (point))
  13454. (current-buffer))
  13455. (message
  13456. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13457. ;;; Paragraph filling stuff.
  13458. ;; We want this to be just right, so use the full arsenal.
  13459. (defun org-indent-line-function ()
  13460. "Indent line like previous, but further if previous was headline or item."
  13461. (interactive)
  13462. (let* ((pos (point))
  13463. (itemp (org-at-item-p))
  13464. column bpos bcol tpos tcol bullet btype bullet-type)
  13465. ;; Find the previous relevant line
  13466. (beginning-of-line 1)
  13467. (cond
  13468. ((looking-at "#") (setq column 0))
  13469. ((looking-at "\\*+ ") (setq column 0))
  13470. (t
  13471. (beginning-of-line 0)
  13472. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13473. (beginning-of-line 0))
  13474. (cond
  13475. ((looking-at "\\*+[ \t]+")
  13476. (if (not org-adapt-indentation)
  13477. (setq column 0)
  13478. (goto-char (match-end 0))
  13479. (setq column (current-column))))
  13480. ((org-in-item-p)
  13481. (org-beginning-of-item)
  13482. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13483. (setq bpos (match-beginning 1) tpos (match-end 0)
  13484. bcol (progn (goto-char bpos) (current-column))
  13485. tcol (progn (goto-char tpos) (current-column))
  13486. bullet (match-string 1)
  13487. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13488. (if (> tcol (+ bcol org-description-max-indent))
  13489. (setq tcol (+ bcol 5)))
  13490. (if (not itemp)
  13491. (setq column tcol)
  13492. (goto-char pos)
  13493. (beginning-of-line 1)
  13494. (if (looking-at "\\S-")
  13495. (progn
  13496. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13497. (setq bullet (match-string 1)
  13498. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13499. (setq column (if (equal btype bullet-type) bcol tcol)))
  13500. (setq column (org-get-indentation)))))
  13501. (t (setq column (org-get-indentation))))))
  13502. (goto-char pos)
  13503. (if (<= (current-column) (current-indentation))
  13504. (org-indent-line-to column)
  13505. (save-excursion (org-indent-line-to column)))
  13506. (setq column (current-column))
  13507. (beginning-of-line 1)
  13508. (if (looking-at
  13509. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13510. (replace-match (concat "\\1" (format org-property-format
  13511. (match-string 2) (match-string 3)))
  13512. t nil))
  13513. (org-move-to-column column)))
  13514. (defun org-set-autofill-regexps ()
  13515. (interactive)
  13516. ;; In the paragraph separator we include headlines, because filling
  13517. ;; text in a line directly attached to a headline would otherwise
  13518. ;; fill the headline as well.
  13519. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13520. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13521. ;; The paragraph starter includes hand-formatted lists.
  13522. (org-set-local 'paragraph-start
  13523. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13524. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13525. ;; But only if the user has not turned off tables or fixed-width regions
  13526. (org-set-local
  13527. 'auto-fill-inhibit-regexp
  13528. (concat "\\*+ \\|#\\+"
  13529. "\\|[ \t]*" org-keyword-time-regexp
  13530. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13531. (concat
  13532. "\\|[ \t]*["
  13533. (if org-enable-table-editor "|" "")
  13534. (if org-enable-fixed-width-editor ":" "")
  13535. "]"))))
  13536. ;; We use our own fill-paragraph function, to make sure that tables
  13537. ;; and fixed-width regions are not wrapped. That function will pass
  13538. ;; through to `fill-paragraph' when appropriate.
  13539. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13540. ; Adaptive filling: To get full control, first make sure that
  13541. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13542. (org-set-local 'adaptive-fill-regexp "\000")
  13543. (org-set-local 'adaptive-fill-function
  13544. 'org-adaptive-fill-function)
  13545. (org-set-local
  13546. 'align-mode-rules-list
  13547. '((org-in-buffer-settings
  13548. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13549. (modes . '(org-mode))))))
  13550. (defun org-fill-paragraph (&optional justify)
  13551. "Re-align a table, pass through to fill-paragraph if no table."
  13552. (let ((table-p (org-at-table-p))
  13553. (table.el-p (org-at-table.el-p)))
  13554. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13555. (save-excursion (goto-char (point-at-bol))
  13556. (looking-at outline-regexp)))
  13557. t) ; skip headlines
  13558. (table.el-p t) ; skip table.el tables
  13559. (table-p (org-table-align) t) ; align org-mode tables
  13560. (t nil)))) ; call paragraph-fill
  13561. ;; For reference, this is the default value of adaptive-fill-regexp
  13562. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13563. (defun org-adaptive-fill-function ()
  13564. "Return a fill prefix for org-mode files.
  13565. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13566. work correctly."
  13567. (cond ((looking-at "#[ \t]+")
  13568. (match-string 0))
  13569. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13570. (save-excursion
  13571. (if (> (match-end 1) (+ (match-beginning 1)
  13572. org-description-max-indent))
  13573. (goto-char (+ (match-beginning 1) 5))
  13574. (goto-char (match-end 0)))
  13575. (make-string (current-column) ?\ )))
  13576. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13577. (save-excursion
  13578. (goto-char (match-end 0))
  13579. (make-string (current-column) ?\ )))
  13580. (t nil)))
  13581. ;;; Other stuff.
  13582. (defun org-toggle-fixed-width-section (arg)
  13583. "Toggle the fixed-width export.
  13584. If there is no active region, the QUOTE keyword at the current headline is
  13585. inserted or removed. When present, it causes the text between this headline
  13586. and the next to be exported as fixed-width text, and unmodified.
  13587. If there is an active region, this command adds or removes a colon as the
  13588. first character of this line. If the first character of a line is a colon,
  13589. this line is also exported in fixed-width font."
  13590. (interactive "P")
  13591. (let* ((cc 0)
  13592. (regionp (org-region-active-p))
  13593. (beg (if regionp (region-beginning) (point)))
  13594. (end (if regionp (region-end)))
  13595. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13596. (case-fold-search nil)
  13597. (re "[ \t]*\\(:\\)")
  13598. off)
  13599. (if regionp
  13600. (save-excursion
  13601. (goto-char beg)
  13602. (setq cc (current-column))
  13603. (beginning-of-line 1)
  13604. (setq off (looking-at re))
  13605. (while (> nlines 0)
  13606. (setq nlines (1- nlines))
  13607. (beginning-of-line 1)
  13608. (cond
  13609. (arg
  13610. (org-move-to-column cc t)
  13611. (insert ":\n")
  13612. (forward-line -1))
  13613. ((and off (looking-at re))
  13614. (replace-match "" t t nil 1))
  13615. ((not off) (org-move-to-column cc t) (insert ":")))
  13616. (forward-line 1)))
  13617. (save-excursion
  13618. (org-back-to-heading)
  13619. (if (looking-at (concat outline-regexp
  13620. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13621. (replace-match "" t t nil 1)
  13622. (if (looking-at outline-regexp)
  13623. (progn
  13624. (goto-char (match-end 0))
  13625. (insert org-quote-string " "))))))))
  13626. ;;;; Functions extending outline functionality
  13627. (defun org-beginning-of-line (&optional arg)
  13628. "Go to the beginning of the current line. If that is invisible, continue
  13629. to a visible line beginning. This makes the function of C-a more intuitive.
  13630. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13631. first attempt, and only move to after the tags when the cursor is already
  13632. beyond the end of the headline."
  13633. (interactive "P")
  13634. (let ((pos (point)) refpos)
  13635. (beginning-of-line 1)
  13636. (if (bobp)
  13637. nil
  13638. (backward-char 1)
  13639. (if (org-invisible-p)
  13640. (while (and (not (bobp)) (org-invisible-p))
  13641. (backward-char 1)
  13642. (beginning-of-line 1))
  13643. (forward-char 1)))
  13644. (when org-special-ctrl-a/e
  13645. (cond
  13646. ((and (looking-at org-complex-heading-regexp)
  13647. (= (char-after (match-end 1)) ?\ ))
  13648. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13649. (point-at-eol)))
  13650. (goto-char
  13651. (if (eq org-special-ctrl-a/e t)
  13652. (cond ((> pos refpos) refpos)
  13653. ((= pos (point)) refpos)
  13654. (t (point)))
  13655. (cond ((> pos (point)) (point))
  13656. ((not (eq last-command this-command)) (point))
  13657. (t refpos)))))
  13658. ((org-at-item-p)
  13659. (goto-char
  13660. (if (eq org-special-ctrl-a/e t)
  13661. (cond ((> pos (match-end 4)) (match-end 4))
  13662. ((= pos (point)) (match-end 4))
  13663. (t (point)))
  13664. (cond ((> pos (point)) (point))
  13665. ((not (eq last-command this-command)) (point))
  13666. (t (match-end 4))))))))
  13667. (org-no-warnings
  13668. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13669. (defun org-end-of-line (&optional arg)
  13670. "Go to the end of the line.
  13671. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13672. first attempt, and only move to after the tags when the cursor is already
  13673. beyond the end of the headline."
  13674. (interactive "P")
  13675. (if (or (not org-special-ctrl-a/e)
  13676. (not (org-on-heading-p)))
  13677. (end-of-line arg)
  13678. (let ((pos (point)))
  13679. (beginning-of-line 1)
  13680. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13681. (if (eq org-special-ctrl-a/e t)
  13682. (if (or (< pos (match-beginning 1))
  13683. (= pos (match-end 0)))
  13684. (goto-char (match-beginning 1))
  13685. (goto-char (match-end 0)))
  13686. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13687. (goto-char (match-end 0))
  13688. (goto-char (match-beginning 1))))
  13689. (end-of-line arg))))
  13690. (org-no-warnings
  13691. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13692. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13693. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13694. (defun org-kill-line (&optional arg)
  13695. "Kill line, to tags or end of line."
  13696. (interactive "P")
  13697. (cond
  13698. ((or (not org-special-ctrl-k)
  13699. (bolp)
  13700. (not (org-on-heading-p)))
  13701. (call-interactively 'kill-line))
  13702. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13703. (kill-region (point) (match-beginning 1))
  13704. (org-set-tags nil t))
  13705. (t (kill-region (point) (point-at-eol)))))
  13706. (define-key org-mode-map "\C-k" 'org-kill-line)
  13707. (defun org-yank (&optional arg)
  13708. "Yank. If the kill is a subtree, treat it specially.
  13709. This command will look at the current kill and check if is a single
  13710. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13711. cursor is at the beginning of a line or after the stars of a currently
  13712. empty headline, then the yank is handled specially. How exactly depends
  13713. on the value of the following variables, both set by default.
  13714. org-yank-folded-subtrees
  13715. When set, the subtree(s) will be folded after insertion, but only
  13716. if doing so would now swallow text after the yanked text.
  13717. org-yank-adjusted-subtrees
  13718. When set, the subtree will be promoted or demoted in order to
  13719. fit into the local outline tree structure, which means that the level
  13720. will be adjusted so that it becomes the smaller one of the two
  13721. *visible* surrounding headings.
  13722. Any prefix to this command will cause `yank' to be called directly with
  13723. no special treatment. In particular, a simple `C-u' prefix will just
  13724. plainly yank the text as it is.
  13725. \[1] Basically, the test checks if the first non-white line is a heading
  13726. and if there are no other headings with fewer stars."
  13727. (interactive "P")
  13728. (setq this-command 'yank)
  13729. (if arg
  13730. (call-interactively 'yank)
  13731. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13732. (and (org-kill-is-subtree-p)
  13733. (or (bolp)
  13734. (and (looking-at "[ \t]*$")
  13735. (string-match
  13736. "\\`\\*+\\'"
  13737. (buffer-substring (point-at-bol) (point)))))))
  13738. swallowp)
  13739. (cond
  13740. ((and subtreep org-yank-folded-subtrees)
  13741. (let ((beg (point))
  13742. end)
  13743. (if (and subtreep org-yank-adjusted-subtrees)
  13744. (org-paste-subtree nil nil 'for-yank)
  13745. (call-interactively 'yank))
  13746. (setq end (point))
  13747. (goto-char beg)
  13748. (when (and (bolp) subtreep
  13749. (not (setq swallowp
  13750. (org-yank-folding-would-swallow-text beg end))))
  13751. (or (looking-at outline-regexp)
  13752. (re-search-forward (concat "^" outline-regexp) end t))
  13753. (while (and (< (point) end) (looking-at outline-regexp))
  13754. (hide-subtree)
  13755. (org-cycle-show-empty-lines 'folded)
  13756. (condition-case nil
  13757. (outline-forward-same-level 1)
  13758. (error (goto-char end)))))
  13759. (when swallowp
  13760. (message
  13761. "Yanked text not folded because that would swallow text"))
  13762. (goto-char end)
  13763. (skip-chars-forward " \t\n\r")
  13764. (beginning-of-line 1)
  13765. (push-mark beg 'nomsg)))
  13766. ((and subtreep org-yank-adjusted-subtrees)
  13767. (let ((beg (point-at-bol)))
  13768. (org-paste-subtree nil nil 'for-yank)
  13769. (push-mark beg 'nomsg)))
  13770. (t
  13771. (call-interactively 'yank))))))
  13772. (defun org-yank-folding-would-swallow-text (beg end)
  13773. "Would hide-subtree at BEG swallow any text after END?"
  13774. (let (level)
  13775. (save-excursion
  13776. (goto-char beg)
  13777. (when (or (looking-at outline-regexp)
  13778. (re-search-forward (concat "^" outline-regexp) end t))
  13779. (setq level (org-outline-level)))
  13780. (goto-char end)
  13781. (skip-chars-forward " \t\r\n\v\f")
  13782. (if (or (eobp)
  13783. (and (bolp) (looking-at org-outline-regexp)
  13784. (<= (org-outline-level) level)))
  13785. nil ; Nothing would be swallowed
  13786. t)))) ; something would swallow
  13787. (define-key org-mode-map "\C-y" 'org-yank)
  13788. (defun org-invisible-p ()
  13789. "Check if point is at a character currently not visible."
  13790. ;; Early versions of noutline don't have `outline-invisible-p'.
  13791. (if (fboundp 'outline-invisible-p)
  13792. (outline-invisible-p)
  13793. (get-char-property (point) 'invisible)))
  13794. (defun org-invisible-p2 ()
  13795. "Check if point is at a character currently not visible."
  13796. (save-excursion
  13797. (if (and (eolp) (not (bobp))) (backward-char 1))
  13798. ;; Early versions of noutline don't have `outline-invisible-p'.
  13799. (if (fboundp 'outline-invisible-p)
  13800. (outline-invisible-p)
  13801. (get-char-property (point) 'invisible))))
  13802. (defun org-back-to-heading (&optional invisible-ok)
  13803. "Call `outline-back-to-heading', but provide a better error message."
  13804. (condition-case nil
  13805. (outline-back-to-heading invisible-ok)
  13806. (error (error "Before first headline at position %d in buffer %s"
  13807. (point) (current-buffer)))))
  13808. (defun org-before-first-heading-p ()
  13809. "Before first heading?"
  13810. (save-excursion
  13811. (null (re-search-backward "^\\*+ " nil t))))
  13812. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13813. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13814. (defun org-at-heading-or-item-p ()
  13815. (or (org-on-heading-p) (org-at-item-p)))
  13816. (defun org-on-target-p ()
  13817. (or (org-in-regexp org-radio-target-regexp)
  13818. (org-in-regexp org-target-regexp)))
  13819. (defun org-up-heading-all (arg)
  13820. "Move to the heading line of which the present line is a subheading.
  13821. This function considers both visible and invisible heading lines.
  13822. With argument, move up ARG levels."
  13823. (if (fboundp 'outline-up-heading-all)
  13824. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13825. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13826. (defun org-up-heading-safe ()
  13827. "Move to the heading line of which the present line is a subheading.
  13828. This version will not throw an error. It will return the level of the
  13829. headline found, or nil if no higher level is found."
  13830. (let (start-level re)
  13831. (org-back-to-heading t)
  13832. (setq start-level (funcall outline-level))
  13833. (if (equal start-level 1)
  13834. nil
  13835. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13836. (if (re-search-backward re nil t)
  13837. (funcall outline-level)))))
  13838. (defun org-first-sibling-p ()
  13839. "Is this heading the first child of its parents?"
  13840. (interactive)
  13841. (let ((re (concat "^" outline-regexp))
  13842. level l)
  13843. (unless (org-at-heading-p t)
  13844. (error "Not at a heading"))
  13845. (setq level (funcall outline-level))
  13846. (save-excursion
  13847. (if (not (re-search-backward re nil t))
  13848. t
  13849. (setq l (funcall outline-level))
  13850. (< l level)))))
  13851. (defun org-goto-sibling (&optional previous)
  13852. "Goto the next sibling, even if it is invisible.
  13853. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13854. when a sibling was found. When none is found, return nil and don't
  13855. move point."
  13856. (let ((fun (if previous 're-search-backward 're-search-forward))
  13857. (pos (point))
  13858. (re (concat "^" outline-regexp))
  13859. level l)
  13860. (when (condition-case nil (org-back-to-heading t) (error nil))
  13861. (setq level (funcall outline-level))
  13862. (catch 'exit
  13863. (or previous (forward-char 1))
  13864. (while (funcall fun re nil t)
  13865. (setq l (funcall outline-level))
  13866. (when (< l level) (goto-char pos) (throw 'exit nil))
  13867. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13868. (goto-char pos)
  13869. nil))))
  13870. (defun org-show-siblings ()
  13871. "Show all siblings of the current headline."
  13872. (save-excursion
  13873. (while (org-goto-sibling) (org-flag-heading nil)))
  13874. (save-excursion
  13875. (while (org-goto-sibling 'previous)
  13876. (org-flag-heading nil))))
  13877. (defun org-show-hidden-entry ()
  13878. "Show an entry where even the heading is hidden."
  13879. (save-excursion
  13880. (org-show-entry)))
  13881. (defun org-flag-heading (flag &optional entry)
  13882. "Flag the current heading. FLAG non-nil means make invisible.
  13883. When ENTRY is non-nil, show the entire entry."
  13884. (save-excursion
  13885. (org-back-to-heading t)
  13886. ;; Check if we should show the entire entry
  13887. (if entry
  13888. (progn
  13889. (org-show-entry)
  13890. (save-excursion
  13891. (and (outline-next-heading)
  13892. (org-flag-heading nil))))
  13893. (outline-flag-region (max (point-min) (1- (point)))
  13894. (save-excursion (outline-end-of-heading) (point))
  13895. flag))))
  13896. (defun org-forward-same-level (arg)
  13897. "Move forward to the ARG'th subheading at same level as this one.
  13898. Stop at the first and last subheadings of a superior heading.
  13899. This is like outline-forward-same-level, but invisible headings are ok."
  13900. (interactive "p")
  13901. (org-back-to-heading t)
  13902. (while (> arg 0)
  13903. (let ((point-to-move-to (save-excursion
  13904. (org-get-next-sibling))))
  13905. (if point-to-move-to
  13906. (progn
  13907. (goto-char point-to-move-to)
  13908. (setq arg (1- arg)))
  13909. (progn
  13910. (setq arg 0)
  13911. (error "No following same-level heading"))))))
  13912. (defun org-get-next-sibling ()
  13913. "Move to next heading of the same level, and return point.
  13914. If there is no such heading, return nil.
  13915. This is like outline-next-sibling, but invisible headings are ok."
  13916. (let ((level (funcall outline-level)))
  13917. (outline-next-heading)
  13918. (while (and (not (eobp)) (> (funcall outline-level) level))
  13919. (outline-next-heading))
  13920. (if (or (eobp) (< (funcall outline-level) level))
  13921. nil
  13922. (point))))
  13923. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13924. ;; This is an exact copy of the original function, but it uses
  13925. ;; `org-back-to-heading', to make it work also in invisible
  13926. ;; trees. And is uses an invisible-OK argument.
  13927. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13928. (org-back-to-heading invisible-OK)
  13929. (let ((first t)
  13930. (level (funcall outline-level)))
  13931. (while (and (not (eobp))
  13932. (or first (> (funcall outline-level) level)))
  13933. (setq first nil)
  13934. (outline-next-heading))
  13935. (unless to-heading
  13936. (if (memq (preceding-char) '(?\n ?\^M))
  13937. (progn
  13938. ;; Go to end of line before heading
  13939. (forward-char -1)
  13940. (if (memq (preceding-char) '(?\n ?\^M))
  13941. ;; leave blank line before heading
  13942. (forward-char -1))))))
  13943. (point))
  13944. (defun org-show-subtree ()
  13945. "Show everything after this heading at deeper levels."
  13946. (outline-flag-region
  13947. (point)
  13948. (save-excursion
  13949. (outline-end-of-subtree) (outline-next-heading) (point))
  13950. nil))
  13951. (defun org-show-entry ()
  13952. "Show the body directly following this heading.
  13953. Show the heading too, if it is currently invisible."
  13954. (interactive)
  13955. (save-excursion
  13956. (condition-case nil
  13957. (progn
  13958. (org-back-to-heading t)
  13959. (outline-flag-region
  13960. (max (point-min) (1- (point)))
  13961. (save-excursion
  13962. (re-search-forward
  13963. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13964. (or (match-beginning 1) (point-max)))
  13965. nil))
  13966. (error nil))))
  13967. (defun org-make-options-regexp (kwds &optional extra)
  13968. "Make a regular expression for keyword lines."
  13969. (concat
  13970. "^"
  13971. "#?[ \t]*\\+\\("
  13972. (mapconcat 'regexp-quote kwds "\\|")
  13973. (if extra (concat "\\|" extra))
  13974. "\\):[ \t]*"
  13975. "\\(.+\\)"))
  13976. ;; Make isearch reveal the necessary context
  13977. (defun org-isearch-end ()
  13978. "Reveal context after isearch exits."
  13979. (when isearch-success ; only if search was successful
  13980. (if (featurep 'xemacs)
  13981. ;; Under XEmacs, the hook is run in the correct place,
  13982. ;; we directly show the context.
  13983. (org-show-context 'isearch)
  13984. ;; In Emacs the hook runs *before* restoring the overlays.
  13985. ;; So we have to use a one-time post-command-hook to do this.
  13986. ;; (Emacs 22 has a special variable, see function `org-mode')
  13987. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13988. isearch-mode-end-hook-quit)
  13989. ;; Only when the isearch was not quitted.
  13990. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13991. 'append 'local)))))
  13992. (defun org-isearch-post-command ()
  13993. "Remove self from hook, and show context."
  13994. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13995. (org-show-context 'isearch))
  13996. ;;;; Integration with and fixes for other packages
  13997. ;;; Imenu support
  13998. (defvar org-imenu-markers nil
  13999. "All markers currently used by Imenu.")
  14000. (make-variable-buffer-local 'org-imenu-markers)
  14001. (defun org-imenu-new-marker (&optional pos)
  14002. "Return a new marker for use by Imenu, and remember the marker."
  14003. (let ((m (make-marker)))
  14004. (move-marker m (or pos (point)))
  14005. (push m org-imenu-markers)
  14006. m))
  14007. (defun org-imenu-get-tree ()
  14008. "Produce the index for Imenu."
  14009. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14010. (setq org-imenu-markers nil)
  14011. (let* ((n org-imenu-depth)
  14012. (re (concat "^" outline-regexp))
  14013. (subs (make-vector (1+ n) nil))
  14014. (last-level 0)
  14015. m level head)
  14016. (save-excursion
  14017. (save-restriction
  14018. (widen)
  14019. (goto-char (point-max))
  14020. (while (re-search-backward re nil t)
  14021. (setq level (org-reduced-level (funcall outline-level)))
  14022. (when (<= level n)
  14023. (looking-at org-complex-heading-regexp)
  14024. (setq head (org-link-display-format
  14025. (org-match-string-no-properties 4))
  14026. m (org-imenu-new-marker))
  14027. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14028. (if (>= level last-level)
  14029. (push (cons head m) (aref subs level))
  14030. (push (cons head (aref subs (1+ level))) (aref subs level))
  14031. (loop for i from (1+ level) to n do (aset subs i nil)))
  14032. (setq last-level level)))))
  14033. (aref subs 1)))
  14034. (eval-after-load "imenu"
  14035. '(progn
  14036. (add-hook 'imenu-after-jump-hook
  14037. (lambda ()
  14038. (if (eq major-mode 'org-mode)
  14039. (org-show-context 'org-goto))))))
  14040. (defun org-link-display-format (link)
  14041. "Replace a link with either the description, or the link target
  14042. if no description is present"
  14043. (save-match-data
  14044. (if (string-match org-bracket-link-analytic-regexp link)
  14045. (replace-match (or (match-string 5 link)
  14046. (concat (match-string 1 link)
  14047. (match-string 3 link)))
  14048. nil nil link)
  14049. link)))
  14050. ;; Speedbar support
  14051. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14052. "Overlay marking the agenda restriction line in speedbar.")
  14053. (org-overlay-put org-speedbar-restriction-lock-overlay
  14054. 'face 'org-agenda-restriction-lock)
  14055. (org-overlay-put org-speedbar-restriction-lock-overlay
  14056. 'help-echo "Agendas are currently limited to this item.")
  14057. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14058. (defun org-speedbar-set-agenda-restriction ()
  14059. "Restrict future agenda commands to the location at point in speedbar.
  14060. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14061. (interactive)
  14062. (require 'org-agenda)
  14063. (let (p m tp np dir txt)
  14064. (cond
  14065. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14066. 'org-imenu t))
  14067. (setq m (get-text-property p 'org-imenu-marker))
  14068. (save-excursion
  14069. (save-restriction
  14070. (set-buffer (marker-buffer m))
  14071. (goto-char m)
  14072. (org-agenda-set-restriction-lock 'subtree))))
  14073. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14074. 'speedbar-function 'speedbar-find-file))
  14075. (setq tp (previous-single-property-change
  14076. (1+ p) 'speedbar-function)
  14077. np (next-single-property-change
  14078. tp 'speedbar-function)
  14079. dir (speedbar-line-directory)
  14080. txt (buffer-substring-no-properties (or tp (point-min))
  14081. (or np (point-max))))
  14082. (save-excursion
  14083. (save-restriction
  14084. (set-buffer (find-file-noselect
  14085. (let ((default-directory dir))
  14086. (expand-file-name txt))))
  14087. (unless (org-mode-p)
  14088. (error "Cannot restrict to non-Org-mode file"))
  14089. (org-agenda-set-restriction-lock 'file))))
  14090. (t (error "Don't know how to restrict Org-mode's agenda")))
  14091. (org-move-overlay org-speedbar-restriction-lock-overlay
  14092. (point-at-bol) (point-at-eol))
  14093. (setq current-prefix-arg nil)
  14094. (org-agenda-maybe-redo)))
  14095. (eval-after-load "speedbar"
  14096. '(progn
  14097. (speedbar-add-supported-extension ".org")
  14098. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14099. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14100. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14101. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14102. (add-hook 'speedbar-visiting-tag-hook
  14103. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14104. ;;; Fixes and Hacks for problems with other packages
  14105. ;; Make flyspell not check words in links, to not mess up our keymap
  14106. (defun org-mode-flyspell-verify ()
  14107. "Don't let flyspell put overlays at active buttons."
  14108. (not (get-text-property (point) 'keymap)))
  14109. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14110. (eval-after-load "bookmark"
  14111. '(if (boundp 'bookmark-after-jump-hook)
  14112. ;; We can use the hook
  14113. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14114. ;; Hook not available, use advice
  14115. (defadvice bookmark-jump (after org-make-visible activate)
  14116. "Make the position visible."
  14117. (org-bookmark-jump-unhide))))
  14118. ;; Make sure saveplace show the location if it was hidden
  14119. (eval-after-load "saveplace"
  14120. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14121. "Make the position visible."
  14122. (org-bookmark-jump-unhide)))
  14123. (defun org-bookmark-jump-unhide ()
  14124. "Unhide the current position, to show the bookmark location."
  14125. (and (org-mode-p)
  14126. (or (org-invisible-p)
  14127. (save-excursion (goto-char (max (point-min) (1- (point))))
  14128. (org-invisible-p)))
  14129. (org-show-context 'bookmark-jump)))
  14130. ;; Make session.el ignore our circular variable
  14131. (eval-after-load "session"
  14132. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14133. ;;;; Experimental code
  14134. (defun org-closed-in-range ()
  14135. "Sparse tree of items closed in a certain time range.
  14136. Still experimental, may disappear in the future."
  14137. (interactive)
  14138. ;; Get the time interval from the user.
  14139. (let* ((time1 (time-to-seconds
  14140. (org-read-date nil 'to-time nil "Starting date: ")))
  14141. (time2 (time-to-seconds
  14142. (org-read-date nil 'to-time nil "End date:")))
  14143. ;; callback function
  14144. (callback (lambda ()
  14145. (let ((time
  14146. (time-to-seconds
  14147. (apply 'encode-time
  14148. (org-parse-time-string
  14149. (match-string 1))))))
  14150. ;; check if time in interval
  14151. (and (>= time time1) (<= time time2))))))
  14152. ;; make tree, check each match with the callback
  14153. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14154. ;;;; Finish up
  14155. (provide 'org)
  14156. (run-hooks 'org-load-hook)
  14157. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14158. ;;; org.el ends here