org.el 609 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365
  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.26c
  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.26c"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-pre-cycle-hook nil
  555. "Hook that is run before visibility cycling is happening.
  556. The function(s) in this hook must accept a single argument which indicates
  557. the new state that will be set right after running this hook. The
  558. argument is a symbol. Before a global state change, it can have the values
  559. `overview', `content', or `all'. Before a local state change, it can have
  560. the values `folded', `children', or `subtree'."
  561. :group 'org-cycle
  562. :type 'hook)
  563. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  564. org-cycle-hide-drawers
  565. org-cycle-show-empty-lines
  566. org-optimize-window-after-visibility-change)
  567. "Hook that is run after `org-cycle' has changed the buffer visibility.
  568. The function(s) in this hook must accept a single argument which indicates
  569. the new state that was set by the most recent `org-cycle' command. The
  570. argument is a symbol. After a global state change, it can have the values
  571. `overview', `content', or `all'. After a local state change, it can have
  572. the values `folded', `children', or `subtree'."
  573. :group 'org-cycle
  574. :type 'hook)
  575. (defgroup org-edit-structure nil
  576. "Options concerning structure editing in Org-mode."
  577. :tag "Org Edit Structure"
  578. :group 'org-structure)
  579. (defcustom org-odd-levels-only nil
  580. "Non-nil means, skip even levels and only use odd levels for the outline.
  581. This has the effect that two stars are being added/taken away in
  582. promotion/demotion commands. It also influences how levels are
  583. handled by the exporters.
  584. Changing it requires restart of `font-lock-mode' to become effective
  585. for fontification also in regions already fontified.
  586. You may also set this on a per-file basis by adding one of the following
  587. lines to the buffer:
  588. #+STARTUP: odd
  589. #+STARTUP: oddeven"
  590. :group 'org-edit-structure
  591. :group 'org-font-lock
  592. :type 'boolean)
  593. (defcustom org-adapt-indentation t
  594. "Non-nil means, adapt indentation when promoting and demoting.
  595. When this is set and the *entire* text in an entry is indented, the
  596. indentation is increased by one space in a demotion command, and
  597. decreased by one in a promotion command. If any line in the entry
  598. body starts at column 0, indentation is not changed at all."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-special-ctrl-a/e nil
  602. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  603. When t, `C-a' will bring back the cursor to the beginning of the
  604. headline text, i.e. after the stars and after a possible TODO keyword.
  605. In an item, this will be the position after the bullet.
  606. When the cursor is already at that position, another `C-a' will bring
  607. it to the beginning of the line.
  608. `C-e' will jump to the end of the headline, ignoring the presence of tags
  609. in the headline. A second `C-e' will then jump to the true end of the
  610. line, after any tags.
  611. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  612. and only a directly following, identical keypress will bring the cursor
  613. to the special positions.
  614. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  615. set separately."
  616. :group 'org-edit-structure
  617. :type '(choice
  618. (const :tag "off" nil)
  619. (const :tag "after stars/bullet and before tags first" t)
  620. (const :tag "true line boundary first" reversed)
  621. (cons :tag "Set C-a and C-e separately"
  622. (choice :tag "Special C-a"
  623. (const :tag "off" nil)
  624. (const :tag "after stars/bullet first" t)
  625. (const :tag "before stars/bullet first" reversed))
  626. (choice :tag "Special C-e"
  627. (const :tag "off" nil)
  628. (const :tag "before tags first" t)
  629. (const :tag "after tags first" reversed)))))
  630. (if (fboundp 'defvaralias)
  631. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  632. (defcustom org-special-ctrl-k nil
  633. "Non-nil means `C-k' will behave specially in headlines.
  634. When nil, `C-k' will call the default `kill-line' command.
  635. When t, the following will happen while the cursor is in the headline:
  636. - When the cursor is at the beginning of a headline, kill the entire
  637. line and possible the folded subtree below the line.
  638. - When in the middle of the headline text, kill the headline up to the tags.
  639. - When after the headline text, kill the tags."
  640. :group 'org-edit-structure
  641. :type 'boolean)
  642. (defcustom org-yank-folded-subtrees t
  643. "Non-nil means, when yanking subtrees, fold them.
  644. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  645. it starts with a heading and all other headings in it are either children
  646. or siblings, then fold all the subtrees. However, do this only if no
  647. text after the yank would be swallowed into a folded tree by this action."
  648. :group 'org-edit-structure
  649. :type 'boolean)
  650. (defcustom org-yank-adjusted-subtrees nil
  651. "Non-nil means, when yanking subtrees, adjust the level.
  652. With this setting, `org-paste-subtree' is used to insert the subtree, see
  653. this function for details."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defcustom org-M-RET-may-split-line '((default . t))
  657. "Non-nil means, M-RET will split the line at the cursor position.
  658. When nil, it will go to the end of the line before making a
  659. new line.
  660. You may also set this option in a different way for different
  661. contexts. Valid contexts are:
  662. headline when creating a new headline
  663. item when creating a new item
  664. table in a table field
  665. default the value to be used for all contexts not explicitly
  666. customized"
  667. :group 'org-structure
  668. :group 'org-table
  669. :type '(choice
  670. (const :tag "Always" t)
  671. (const :tag "Never" nil)
  672. (repeat :greedy t :tag "Individual contexts"
  673. (cons
  674. (choice :tag "Context"
  675. (const headline)
  676. (const item)
  677. (const table)
  678. (const default))
  679. (boolean)))))
  680. (defcustom org-insert-heading-respect-content nil
  681. "Non-nil means, insert new headings after the current subtree.
  682. When nil, the new heading is created directly after the current line.
  683. The commands \\[org-insert-heading-respect-content] and
  684. \\[org-insert-todo-heading-respect-content] turn this variable on
  685. for the duration of the command."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-blank-before-new-entry '((heading . auto)
  689. (plain-list-item . auto))
  690. "Should `org-insert-heading' leave a blank line before new heading/item?
  691. The value is an alist, with `heading' and `plain-list-item' as car,
  692. and a boolean flag as cdr. For plain lists, if the variable
  693. `org-empty-line-terminates-plain-lists' is set, the setting here
  694. is ignored and no empty line is inserted, to keep the list in tact."
  695. :group 'org-edit-structure
  696. :type '(list
  697. (cons (const heading)
  698. (choice (const :tag "Never" nil)
  699. (const :tag "Always" t)
  700. (const :tag "Auto" auto)))
  701. (cons (const plain-list-item)
  702. (choice (const :tag "Never" nil)
  703. (const :tag "Always" t)
  704. (const :tag "Auto" auto)))))
  705. (defcustom org-insert-heading-hook nil
  706. "Hook being run after inserting a new heading."
  707. :group 'org-edit-structure
  708. :type 'hook)
  709. (defcustom org-enable-fixed-width-editor t
  710. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  711. This currently only means, they are never auto-wrapped.
  712. When nil, such lines will be treated like ordinary lines.
  713. See also the QUOTE keyword."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-edit-src-region-extra nil
  717. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  718. For examples see the function `org-edit-src-find-region-and-lang'.
  719. The regular expression identifying the begin marker should end with a newline,
  720. and the regexp marking the end line should start with a newline, to make sure
  721. there are kept outside the narrowed region."
  722. :group 'org-edit-structure
  723. :type '(repeat
  724. (list
  725. (regexp :tag "begin regexp")
  726. (regexp :tag "end regexp")
  727. (choice :tag "language"
  728. (string :tag "specify")
  729. (integer :tag "from match group")
  730. (const :tag "from `lang' element")
  731. (const :tag "from `style' element")))))
  732. (defcustom org-coderef-label-format "(ref:%s)"
  733. "The default coderef format.
  734. This format string will be used to search for coderef labels in literal
  735. examples (EXAMPLE and SRC blocks). The format can be overwritten
  736. an individual literal example with the -f option, like
  737. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  738. ...
  739. #+END_SRC
  740. If you want to use this for HTML export, make sure that the format does
  741. not introduce special font-locking, and avoid the HTML special
  742. characters `<', `>', and `&'. The reason for this restriction is that
  743. the labels are searched for only after htmlize has done its job."
  744. :group 'org-edit-structure ; FIXME this is not in the right group
  745. :type 'string)
  746. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  747. "The mode that should be used to edit fixed-width regions.
  748. These are the regions where each line starts with a colon."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const artist-mode)
  752. (const picture-mode)
  753. (const fundamental-mode)
  754. (function :tag "Other (specify)")))
  755. (defcustom org-goto-auto-isearch t
  756. "Non-nil means, typing characters in org-goto starts incremental search."
  757. :group 'org-edit-structure
  758. :type 'boolean)
  759. (defgroup org-sparse-trees nil
  760. "Options concerning sparse trees in Org-mode."
  761. :tag "Org Sparse Trees"
  762. :group 'org-structure)
  763. (defcustom org-highlight-sparse-tree-matches t
  764. "Non-nil means, highlight all matches that define a sparse tree.
  765. The highlights will automatically disappear the next time the buffer is
  766. changed by an edit command."
  767. :group 'org-sparse-trees
  768. :type 'boolean)
  769. (defcustom org-remove-highlights-with-change t
  770. "Non-nil means, any change to the buffer will remove temporary highlights.
  771. Such highlights are created by `org-occur' and `org-clock-display'.
  772. When nil, `C-c C-c needs to be used to get rid of the highlights.
  773. The highlights created by `org-preview-latex-fragment' always need
  774. `C-c C-c' to be removed."
  775. :group 'org-sparse-trees
  776. :group 'org-time
  777. :type 'boolean)
  778. (defcustom org-occur-hook '(org-first-headline-recenter)
  779. "Hook that is run after `org-occur' has constructed a sparse tree.
  780. This can be used to recenter the window to show as much of the structure
  781. as possible."
  782. :group 'org-sparse-trees
  783. :type 'hook)
  784. (defgroup org-imenu-and-speedbar nil
  785. "Options concerning imenu and speedbar in Org-mode."
  786. :tag "Org Imenu and Speedbar"
  787. :group 'org-structure)
  788. (defcustom org-imenu-depth 2
  789. "The maximum level for Imenu access to Org-mode headlines.
  790. This also applied for speedbar access."
  791. :group 'org-imenu-and-speedbar
  792. :type 'integer)
  793. (defgroup org-table nil
  794. "Options concerning tables in Org-mode."
  795. :tag "Org Table"
  796. :group 'org)
  797. (defcustom org-enable-table-editor 'optimized
  798. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  799. When nil, such lines will be treated like ordinary lines.
  800. When equal to the symbol `optimized', the table editor will be optimized to
  801. do the following:
  802. - Automatic overwrite mode in front of whitespace in table fields.
  803. This makes the structure of the table stay in tact as long as the edited
  804. field does not exceed the column width.
  805. - Minimize the number of realigns. Normally, the table is aligned each time
  806. TAB or RET are pressed to move to another field. With optimization this
  807. happens only if changes to a field might have changed the column width.
  808. Optimization requires replacing the functions `self-insert-command',
  809. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  810. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  811. very good at guessing when a re-align will be necessary, but you can always
  812. force one with \\[org-ctrl-c-ctrl-c].
  813. If you would like to use the optimized version in Org-mode, but the
  814. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  815. This variable can be used to turn on and off the table editor during a session,
  816. but in order to toggle optimization, a restart is required.
  817. See also the variable `org-table-auto-blank-field'."
  818. :group 'org-table
  819. :type '(choice
  820. (const :tag "off" nil)
  821. (const :tag "on" t)
  822. (const :tag "on, optimized" optimized)))
  823. (defcustom org-self-insert-cluster-for-undo t
  824. "Non-nil means cluster self-insert commands for undo when possible.
  825. If this is set, then, like in the Emacs command loop, 20 consequtive
  826. characters will be undone together.
  827. This is configurable, because there is some impact on typing performance."
  828. :group 'org-table
  829. :type 'boolean)
  830. (defcustom org-table-tab-recognizes-table.el t
  831. "Non-nil means, TAB will automatically notice a table.el table.
  832. When it sees such a table, it moves point into it and - if necessary -
  833. calls `table-recognize-table'."
  834. :group 'org-table-editing
  835. :type 'boolean)
  836. (defgroup org-link nil
  837. "Options concerning links in Org-mode."
  838. :tag "Org Link"
  839. :group 'org)
  840. (defvar org-link-abbrev-alist-local nil
  841. "Buffer-local version of `org-link-abbrev-alist', which see.
  842. The value of this is taken from the #+LINK lines.")
  843. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  844. (defcustom org-link-abbrev-alist nil
  845. "Alist of link abbreviations.
  846. The car of each element is a string, to be replaced at the start of a link.
  847. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  848. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  849. [[linkkey:tag][description]]
  850. The 'linkkey' must be a word word, starting with a letter, followed
  851. by letters, numbers, '-' or '_'.
  852. If REPLACE is a string, the tag will simply be appended to create the link.
  853. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  854. the placeholder \"%h\" will cause a url-encoded version of the tag to
  855. be inserted at that point (see the function `url-hexify-string').
  856. REPLACE may also be a function that will be called with the tag as the
  857. only argument to create the link, which should be returned as a string.
  858. See the manual for examples."
  859. :group 'org-link
  860. :type '(repeat
  861. (cons
  862. (string :tag "Protocol")
  863. (choice
  864. (string :tag "Format")
  865. (function)))))
  866. (defcustom org-descriptive-links t
  867. "Non-nil means, hide link part and only show description of bracket links.
  868. Bracket links are like [[link][description]]. This variable sets the initial
  869. state in new org-mode buffers. The setting can then be toggled on a
  870. per-buffer basis from the Org->Hyperlinks menu."
  871. :group 'org-link
  872. :type 'boolean)
  873. (defcustom org-link-file-path-type 'adaptive
  874. "How the path name in file links should be stored.
  875. Valid values are:
  876. relative Relative to the current directory, i.e. the directory of the file
  877. into which the link is being inserted.
  878. absolute Absolute path, if possible with ~ for home directory.
  879. noabbrev Absolute path, no abbreviation of home directory.
  880. adaptive Use relative path for files in the current directory and sub-
  881. directories of it. For other files, use an absolute path."
  882. :group 'org-link
  883. :type '(choice
  884. (const relative)
  885. (const absolute)
  886. (const noabbrev)
  887. (const adaptive)))
  888. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  889. "Types of links that should be activated in Org-mode files.
  890. This is a list of symbols, each leading to the activation of a certain link
  891. type. In principle, it does not hurt to turn on most link types - there may
  892. be a small gain when turning off unused link types. The types are:
  893. bracket The recommended [[link][description]] or [[link]] links with hiding.
  894. angular Links in angular brackets that may contain whitespace like
  895. <bbdb:Carsten Dominik>.
  896. plain Plain links in normal text, no whitespace, like http://google.com.
  897. radio Text that is matched by a radio target, see manual for details.
  898. tag Tag settings in a headline (link to tag search).
  899. date Time stamps (link to calendar).
  900. footnote Footnote labels.
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set :greedy t
  904. (const :tag "Double bracket links (new style)" bracket)
  905. (const :tag "Angular bracket links (old style)" angular)
  906. (const :tag "Plain text links" plain)
  907. (const :tag "Radio target matches" radio)
  908. (const :tag "Tags" tag)
  909. (const :tag "Timestamps" date)
  910. (const :tag "Footnotes" footnote)))
  911. (defcustom org-make-link-description-function nil
  912. "Function to use to generate link descriptions from links. If
  913. nil the link location will be used. This function must take two
  914. parameters; the first is the link and the second the description
  915. org-insert-link has generated, and should return the description
  916. to use."
  917. :group 'org-link
  918. :type 'function)
  919. (defgroup org-link-store nil
  920. "Options concerning storing links in Org-mode."
  921. :tag "Org Store Link"
  922. :group 'org-link)
  923. (defcustom org-email-link-description-format "Email %c: %.30s"
  924. "Format of the description part of a link to an email or usenet message.
  925. The following %-escapes will be replaced by corresponding information:
  926. %F full \"From\" field
  927. %f name, taken from \"From\" field, address if no name
  928. %T full \"To\" field
  929. %t first name in \"To\" field, address if no name
  930. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  931. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  932. %s subject
  933. %m message-id.
  934. You may use normal field width specification between the % and the letter.
  935. This is for example useful to limit the length of the subject.
  936. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  937. :group 'org-link-store
  938. :type 'string)
  939. (defcustom org-from-is-user-regexp
  940. (let (r1 r2)
  941. (when (and user-mail-address (not (string= user-mail-address "")))
  942. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  943. (when (and user-full-name (not (string= user-full-name "")))
  944. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  945. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  946. "Regexp matched against the \"From:\" header of an email or usenet message.
  947. It should match if the message is from the user him/herself."
  948. :group 'org-link-store
  949. :type 'regexp)
  950. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  951. "Non-nil means, storing a link to an Org file will use entry IDs.
  952. Note that before this variable is even considered, org-id must be loaded,
  953. to please customize `org-modules' and turn it on.
  954. The variable can have the following values:
  955. t Create an ID if needed to make a link to the current entry.
  956. create-if-interactive
  957. If `org-store-link' is called directly (interactively, as a user
  958. command), do create an ID to support the link. But when doing the
  959. job for remember, only use the ID if it already exists. The
  960. purpose of this setting is to avoid proliferation of unwanted
  961. IDs, just because you happen to be in an Org file when you
  962. call `org-remember' that automatically and preemptively
  963. creates a link. If you do want to get an ID link in a remember
  964. template to an entry not having an ID, create it first by
  965. explicitly creating a link to it, using `C-c C-l' first.
  966. use-existing
  967. Use existing ID, do not create one.
  968. nil Never use an ID to make a link, instead link using a text search for
  969. the headline text."
  970. :group 'org-link-store
  971. :type '(choice
  972. (const :tag "Create ID to make link" t)
  973. (const :tag "Create if storing link interactively"
  974. create-if-interactive)
  975. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  976. create-if-interactive-and-no-custom-id)
  977. (const :tag "Only use existing" use-existing)
  978. (const :tag "Do not use ID to create link" nil)))
  979. (defcustom org-context-in-file-links t
  980. "Non-nil means, file links from `org-store-link' contain context.
  981. A search string will be added to the file name with :: as separator and
  982. used to find the context when the link is activated by the command
  983. `org-open-at-point'.
  984. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  985. negates this setting for the duration of the command."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defcustom org-keep-stored-link-after-insertion nil
  989. "Non-nil means, keep link in list for entire session.
  990. The command `org-store-link' adds a link pointing to the current
  991. location to an internal list. These links accumulate during a session.
  992. The command `org-insert-link' can be used to insert links into any
  993. Org-mode file (offering completion for all stored links). When this
  994. option is nil, every link which has been inserted once using \\[org-insert-link]
  995. will be removed from the list, to make completing the unused links
  996. more efficient."
  997. :group 'org-link-store
  998. :type 'boolean)
  999. (defgroup org-link-follow nil
  1000. "Options concerning following links in Org-mode."
  1001. :tag "Org Follow Link"
  1002. :group 'org-link)
  1003. (defcustom org-link-translation-function nil
  1004. "Function to translate links with different syntax to Org syntax.
  1005. This can be used to translate links created for example by the Planner
  1006. or emacs-wiki packages to Org syntax.
  1007. The function must accept two parameters, a TYPE containing the link
  1008. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1009. which is everything after the link protocol. It should return a cons
  1010. with possibly modified values of type and path.
  1011. Org contains a function for this, so if you set this variable to
  1012. `org-translate-link-from-planner', you should be able follow many
  1013. links created by planner."
  1014. :group 'org-link-follow
  1015. :type 'function)
  1016. (defcustom org-follow-link-hook nil
  1017. "Hook that is run after a link has been followed."
  1018. :group 'org-link-follow
  1019. :type 'hook)
  1020. (defcustom org-tab-follows-link nil
  1021. "Non-nil means, on links TAB will follow the link.
  1022. Needs to be set before org.el is loaded.
  1023. This really should not be used, it does not make sense, and the
  1024. implementation is bad."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-return-follows-link nil
  1028. "Non-nil means, on links RET will follow the link.
  1029. Needs to be set before org.el is loaded."
  1030. :group 'org-link-follow
  1031. :type 'boolean)
  1032. (defcustom org-mouse-1-follows-link
  1033. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1034. "Non-nil means, mouse-1 on a link will follow the link.
  1035. A longer mouse click will still set point. Does not work on XEmacs.
  1036. Needs to be set before org.el is loaded."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-mark-ring-length 4
  1040. "Number of different positions to be recorded in the ring
  1041. Changing this requires a restart of Emacs to work correctly."
  1042. :group 'org-link-follow
  1043. :type 'integer)
  1044. (defcustom org-link-frame-setup
  1045. '((vm . vm-visit-folder-other-frame)
  1046. (gnus . gnus-other-frame)
  1047. (file . find-file-other-window))
  1048. "Setup the frame configuration for following links.
  1049. When following a link with Emacs, it may often be useful to display
  1050. this link in another window or frame. This variable can be used to
  1051. set this up for the different types of links.
  1052. For VM, use any of
  1053. `vm-visit-folder'
  1054. `vm-visit-folder-other-frame'
  1055. For Gnus, use any of
  1056. `gnus'
  1057. `gnus-other-frame'
  1058. `org-gnus-no-new-news'
  1059. For FILE, use any of
  1060. `find-file'
  1061. `find-file-other-window'
  1062. `find-file-other-frame'
  1063. For the calendar, use the variable `calendar-setup'.
  1064. For BBDB, it is currently only possible to display the matches in
  1065. another window."
  1066. :group 'org-link-follow
  1067. :type '(list
  1068. (cons (const vm)
  1069. (choice
  1070. (const vm-visit-folder)
  1071. (const vm-visit-folder-other-window)
  1072. (const vm-visit-folder-other-frame)))
  1073. (cons (const gnus)
  1074. (choice
  1075. (const gnus)
  1076. (const gnus-other-frame)
  1077. (const org-gnus-no-new-news)))
  1078. (cons (const file)
  1079. (choice
  1080. (const find-file)
  1081. (const find-file-other-window)
  1082. (const find-file-other-frame)))))
  1083. (defcustom org-display-internal-link-with-indirect-buffer nil
  1084. "Non-nil means, use indirect buffer to display infile links.
  1085. Activating internal links (from one location in a file to another location
  1086. in the same file) normally just jumps to the location. When the link is
  1087. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1088. another window. When this option is set, the other window actually displays
  1089. an indirect buffer clone of the current buffer, to avoid any visibility
  1090. changes to the current buffer."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-open-non-existing-files nil
  1094. "Non-nil means, `org-open-file' will open non-existing files.
  1095. When nil, an error will be generated."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-open-directory-means-index-dot-org nil
  1099. "Non-nil means, a link to a directory really means to index.org.
  1100. When nil, following a directory link will run dired or open a finder/explorer
  1101. window on that directory."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1105. "Function and arguments to call for following mailto links.
  1106. This is a list with the first element being a lisp function, and the
  1107. remaining elements being arguments to the function. In string arguments,
  1108. %a will be replaced by the address, and %s will be replaced by the subject
  1109. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1113. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1114. (const :tag "message-mail" (message-mail "%a" "%s"))
  1115. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1116. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1117. "Non-nil means, ask for confirmation before executing shell links.
  1118. Shell links can be dangerous: just think about a link
  1119. [[shell:rm -rf ~/*][Google Search]]
  1120. This link would show up in your Org-mode document as \"Google Search\",
  1121. but really it would remove your entire home directory.
  1122. Therefore we advise against setting this variable to nil.
  1123. Just change it to `y-or-n-p' if you want to confirm with a
  1124. single keystroke rather than having to type \"yes\"."
  1125. :group 'org-link-follow
  1126. :type '(choice
  1127. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1128. (const :tag "with y-or-n (faster)" y-or-n-p)
  1129. (const :tag "no confirmation (dangerous)" nil)))
  1130. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1131. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1132. Elisp links can be dangerous: just think about a link
  1133. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1134. This link would show up in your Org-mode document as \"Google Search\",
  1135. but really it would remove your entire home directory.
  1136. Therefore we advise against setting this variable to nil.
  1137. Just change it to `y-or-n-p' if you want to confirm with a
  1138. single keystroke rather than having to type \"yes\"."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1142. (const :tag "with y-or-n (faster)" y-or-n-p)
  1143. (const :tag "no confirmation (dangerous)" nil)))
  1144. (defconst org-file-apps-defaults-gnu
  1145. '((remote . emacs)
  1146. (system . mailcap)
  1147. (t . mailcap))
  1148. "Default file applications on a UNIX or GNU/Linux system.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-macosx
  1151. '((remote . emacs)
  1152. (t . "open %s")
  1153. (system . "open %s")
  1154. ("ps.gz" . "gv %s")
  1155. ("eps.gz" . "gv %s")
  1156. ("dvi" . "xdvi %s")
  1157. ("fig" . "xfig %s"))
  1158. "Default file applications on a MacOS X system.
  1159. The system \"open\" is known as a default, but we use X11 applications
  1160. for some files for which the OS does not have a good default.
  1161. See `org-file-apps'.")
  1162. (defconst org-file-apps-defaults-windowsnt
  1163. (list
  1164. '(remote . emacs)
  1165. (cons t
  1166. (list (if (featurep 'xemacs)
  1167. 'mswindows-shell-execute
  1168. 'w32-shell-execute)
  1169. "open" 'file))
  1170. (cons 'system
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. (auto-mode . emacs)
  1181. ("\\.x?html?\\'" . default)
  1182. ("\\.pdf\\'" . default)
  1183. )
  1184. "External applications for opening `file:path' items in a document.
  1185. Org-mode uses system defaults for different file types, but
  1186. you can use this variable to set the application for a given file
  1187. extension. The entries in this list are cons cells where the car identifies
  1188. files and the cdr the corresponding command. Possible values for the
  1189. file identifier are
  1190. \"regex\" Regular expression matched against the file name. For backward
  1191. compatibility, this can also be a string with only alphanumeric
  1192. characters, which is then interpreted as an extension.
  1193. `directory' Matches a directory
  1194. `remote' Matches a remote file, accessible through tramp or efs.
  1195. Remote files most likely should be visited through Emacs
  1196. because external applications cannot handle such paths.
  1197. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1198. so all files Emacs knows how to handle. Using this with
  1199. command `emacs' will open most files in Emacs. Beware that this
  1200. will also open html files inside Emacs, unless you add
  1201. (\"html\" . default) to the list as well.
  1202. t Default for files not matched by any of the other options.
  1203. `system' The system command to open files, like `open' on Windows
  1204. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1205. that will be selected if you call `C-c C-o' with a double
  1206. `C-u C-u' prefix.
  1207. Possible values for the command are:
  1208. `emacs' The file will be visited by the current Emacs process.
  1209. `default' Use the default application for this file type, which is the
  1210. association for t in the list, most likely in the system-specific
  1211. part.
  1212. This can be used to overrule an unwanted setting in the
  1213. system-specific variable.
  1214. `system' Use the system command for opening files, like \"open\".
  1215. This command is specified by the entry whose car is `system'.
  1216. Most likely, the system-specific version of this variable
  1217. does define this command, but you can overrule/replace it
  1218. here.
  1219. string A command to be executed by a shell; %s will be replaced
  1220. by the path to the file.
  1221. sexp A Lisp form which will be evaluated. The file path will
  1222. be available in the Lisp variable `file'.
  1223. For more examples, see the system specific constants
  1224. `org-file-apps-defaults-macosx'
  1225. `org-file-apps-defaults-windowsnt'
  1226. `org-file-apps-defaults-gnu'."
  1227. :group 'org-link-follow
  1228. :type '(repeat
  1229. (cons (choice :value ""
  1230. (string :tag "Extension")
  1231. (const :tag "System command to open files" system)
  1232. (const :tag "Default for unrecognized files" t)
  1233. (const :tag "Remote file" remote)
  1234. (const :tag "Links to a directory" directory)
  1235. (const :tag "Any files that have Emacs modes"
  1236. auto-mode))
  1237. (choice :value ""
  1238. (const :tag "Visit with Emacs" emacs)
  1239. (const :tag "Use default" default)
  1240. (const :tag "Use the system command" system)
  1241. (string :tag "Command")
  1242. (sexp :tag "Lisp form")))))
  1243. (defgroup org-refile nil
  1244. "Options concerning refiling entries in Org-mode."
  1245. :tag "Org Refile"
  1246. :group 'org)
  1247. (defcustom org-directory "~/org"
  1248. "Directory with org files.
  1249. This is just a default location to look for Org files. There is no need
  1250. at all to put your files into this directory. It is only used in the
  1251. following situations:
  1252. 1. When a remember template specifies a target file that is not an
  1253. absolute path. The path will then be interpreted relative to
  1254. `org-directory'
  1255. 2. When a remember note is filed away in an interactive way (when exiting the
  1256. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1257. with `org-directory' as the default path."
  1258. :group 'org-refile
  1259. :group 'org-remember
  1260. :type 'directory)
  1261. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1262. "Default target for storing notes.
  1263. Used by the hooks for remember.el. This can be a string, or nil to mean
  1264. the value of `remember-data-file'.
  1265. You can set this on a per-template basis with the variable
  1266. `org-remember-templates'."
  1267. :group 'org-refile
  1268. :group 'org-remember
  1269. :type '(choice
  1270. (const :tag "Default from remember-data-file" nil)
  1271. file))
  1272. (defcustom org-goto-interface 'outline
  1273. "The default interface to be used for `org-goto'.
  1274. Allowed values are:
  1275. outline The interface shows an outline of the relevant file
  1276. and the correct heading is found by moving through
  1277. the outline or by searching with incremental search.
  1278. outline-path-completion Headlines in the current buffer are offered via
  1279. completion. This is the interface also used by
  1280. the refile command."
  1281. :group 'org-refile
  1282. :type '(choice
  1283. (const :tag "Outline" outline)
  1284. (const :tag "Outline-path-completion" outline-path-completion)))
  1285. (defcustom org-goto-max-level 5
  1286. "Maximum level to be considered when running org-goto with refile interface."
  1287. :group 'org-refile
  1288. :type 'integer)
  1289. (defcustom org-reverse-note-order nil
  1290. "Non-nil means, store new notes at the beginning of a file or entry.
  1291. When nil, new notes will be filed to the end of a file or entry.
  1292. This can also be a list with cons cells of regular expressions that
  1293. are matched against file names, and values."
  1294. :group 'org-remember
  1295. :group 'org-refile
  1296. :type '(choice
  1297. (const :tag "Reverse always" t)
  1298. (const :tag "Reverse never" nil)
  1299. (repeat :tag "By file name regexp"
  1300. (cons regexp boolean))))
  1301. (defcustom org-refile-targets nil
  1302. "Targets for refiling entries with \\[org-refile].
  1303. This is list of cons cells. Each cell contains:
  1304. - a specification of the files to be considered, either a list of files,
  1305. or a symbol whose function or variable value will be used to retrieve
  1306. a file name or a list of file names. If you use `org-agenda-files' for
  1307. that, all agenda files will be scanned for targets. Nil means, consider
  1308. headings in the current buffer.
  1309. - A specification of how to find candidate refile targets. This may be
  1310. any of:
  1311. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1312. This tag has to be present in all target headlines, inheritance will
  1313. not be considered.
  1314. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1315. todo keyword.
  1316. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1317. headlines that are refiling targets.
  1318. - a cons cell (:level . N). Any headline of level N is considered a target.
  1319. Note that, when `org-odd-levels-only' is set, level corresponds to
  1320. order in hierarchy, not to the number of stars.
  1321. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1322. Note that, when `org-odd-levels-only' is set, level corresponds to
  1323. order in hierarchy, not to the number of stars.
  1324. You can set the variable `org-refile-target-verify-function' to a function
  1325. to verify each headline found by the simple critery above.
  1326. When this variable is nil, all top-level headlines in the current buffer
  1327. are used, equivalent to the value `((nil . (:level . 1))'."
  1328. :group 'org-refile
  1329. :type '(repeat
  1330. (cons
  1331. (choice :value org-agenda-files
  1332. (const :tag "All agenda files" org-agenda-files)
  1333. (const :tag "Current buffer" nil)
  1334. (function) (variable) (file))
  1335. (choice :tag "Identify target headline by"
  1336. (cons :tag "Specific tag" (const :value :tag) (string))
  1337. (cons :tag "TODO keyword" (const :value :todo) (string))
  1338. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1339. (cons :tag "Level number" (const :value :level) (integer))
  1340. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1341. (defcustom org-refile-target-verify-function nil
  1342. "Function to verify if the headline at point should be a refile target.
  1343. The function will be called without arguments, with point at the
  1344. beginning of the headline. It should return t and leave point
  1345. where it is if the headline is a valid target for refiling.
  1346. If the target should not be selected, the function must return nil.
  1347. In addition to this, it may move point to a place from where the search
  1348. should be continued. For example, the function may decide that the entire
  1349. subtree of the current entry should be excluded and move point to the end
  1350. of the subtree."
  1351. :group 'org-refile
  1352. :type 'function)
  1353. (defcustom org-refile-use-outline-path nil
  1354. "Non-nil means, provide refile targets as paths.
  1355. So a level 3 headline will be available as level1/level2/level3.
  1356. When the value is `file', also include the file name (without directory)
  1357. into the path. In this case, you can also stop the completion after
  1358. the file name, to get entries inserted as top level in the file.
  1359. When `full-file-path', include the full file path."
  1360. :group 'org-refile
  1361. :type '(choice
  1362. (const :tag "Not" nil)
  1363. (const :tag "Yes" t)
  1364. (const :tag "Start with file name" file)
  1365. (const :tag "Start with full file path" full-file-path)))
  1366. (defcustom org-outline-path-complete-in-steps t
  1367. "Non-nil means, complete the outline path in hierarchical steps.
  1368. When Org-mode uses the refile interface to select an outline path
  1369. \(see variable `org-refile-use-outline-path'), the completion of
  1370. the path can be done is a single go, or if can be done in steps down
  1371. the headline hierarchy. Going in steps is probably the best if you
  1372. do not use a special completion package like `ido' or `icicles'.
  1373. However, when using these packages, going in one step can be very
  1374. fast, while still showing the whole path to the entry."
  1375. :group 'org-refile
  1376. :type 'boolean)
  1377. (defcustom org-refile-allow-creating-parent-nodes nil
  1378. "Non-nil means, allow to create new nodes as refile targets.
  1379. New nodes are then created by adding \"/new node name\" to the completion
  1380. of an existing node. When the value of this variable is `confirm',
  1381. new node creation must be confirmed by the user (recommended)
  1382. When nil, the completion must match an existing entry.
  1383. Note that, if the new heading is not seen by the criteria
  1384. listed in `org-refile-targets', multiple instances of the same
  1385. heading would be created by trying again to file under the new
  1386. heading."
  1387. :group 'org-refile
  1388. :type '(choice
  1389. (const :tag "Never" nil)
  1390. (const :tag "Always" t)
  1391. (const :tag "Prompt for confirmation" confirm)))
  1392. (defgroup org-todo nil
  1393. "Options concerning TODO items in Org-mode."
  1394. :tag "Org TODO"
  1395. :group 'org)
  1396. (defgroup org-progress nil
  1397. "Options concerning Progress logging in Org-mode."
  1398. :tag "Org Progress"
  1399. :group 'org-time)
  1400. (defvar org-todo-interpretation-widgets
  1401. '(
  1402. (:tag "Sequence (cycling hits every state)" sequence)
  1403. (:tag "Type (cycling directly to DONE)" type))
  1404. "The available interpretation symbols for customizing
  1405. `org-todo-keywords'.
  1406. Interested libraries should add to this list.")
  1407. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1408. "List of TODO entry keyword sequences and their interpretation.
  1409. \\<org-mode-map>This is a list of sequences.
  1410. Each sequence starts with a symbol, either `sequence' or `type',
  1411. indicating if the keywords should be interpreted as a sequence of
  1412. action steps, or as different types of TODO items. The first
  1413. keywords are states requiring action - these states will select a headline
  1414. for inclusion into the global TODO list Org-mode produces. If one of
  1415. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1416. signify that no further action is necessary. If \"|\" is not found,
  1417. the last keyword is treated as the only DONE state of the sequence.
  1418. The command \\[org-todo] cycles an entry through these states, and one
  1419. additional state where no keyword is present. For details about this
  1420. cycling, see the manual.
  1421. TODO keywords and interpretation can also be set on a per-file basis with
  1422. the special #+SEQ_TODO and #+TYP_TODO lines.
  1423. Each keyword can optionally specify a character for fast state selection
  1424. \(in combination with the variable `org-use-fast-todo-selection')
  1425. and specifiers for state change logging, using the same syntax
  1426. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1427. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1428. indicates to record a time stamp each time this state is selected.
  1429. Each keyword may also specify if a timestamp or a note should be
  1430. recorded when entering or leaving the state, by adding additional
  1431. characters in the parenthesis after the keyword. This looks like this:
  1432. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1433. record only the time of the state change. With X and Y being either
  1434. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1435. Y when leaving the state if and only if the *target* state does not
  1436. define X. You may omit any of the fast-selection key or X or /Y,
  1437. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1438. For backward compatibility, this variable may also be just a list
  1439. of keywords - in this case the interpretation (sequence or type) will be
  1440. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1441. :group 'org-todo
  1442. :group 'org-keywords
  1443. :type '(choice
  1444. (repeat :tag "Old syntax, just keywords"
  1445. (string :tag "Keyword"))
  1446. (repeat :tag "New syntax"
  1447. (cons
  1448. (choice
  1449. :tag "Interpretation"
  1450. ;;Quick and dirty way to see
  1451. ;;`org-todo-interpretations'. This takes the
  1452. ;;place of item arguments
  1453. :convert-widget
  1454. (lambda (widget)
  1455. (widget-put widget
  1456. :args (mapcar
  1457. #'(lambda (x)
  1458. (widget-convert
  1459. (cons 'const x)))
  1460. org-todo-interpretation-widgets))
  1461. widget))
  1462. (repeat
  1463. (string :tag "Keyword"))))))
  1464. (defvar org-todo-keywords-1 nil
  1465. "All TODO and DONE keywords active in a buffer.")
  1466. (make-variable-buffer-local 'org-todo-keywords-1)
  1467. (defvar org-todo-keywords-for-agenda nil)
  1468. (defvar org-done-keywords-for-agenda nil)
  1469. (defvar org-todo-keyword-alist-for-agenda nil)
  1470. (defvar org-tag-alist-for-agenda nil)
  1471. (defvar org-agenda-contributing-files nil)
  1472. (defvar org-not-done-keywords nil)
  1473. (make-variable-buffer-local 'org-not-done-keywords)
  1474. (defvar org-done-keywords nil)
  1475. (make-variable-buffer-local 'org-done-keywords)
  1476. (defvar org-todo-heads nil)
  1477. (make-variable-buffer-local 'org-todo-heads)
  1478. (defvar org-todo-sets nil)
  1479. (make-variable-buffer-local 'org-todo-sets)
  1480. (defvar org-todo-log-states nil)
  1481. (make-variable-buffer-local 'org-todo-log-states)
  1482. (defvar org-todo-kwd-alist nil)
  1483. (make-variable-buffer-local 'org-todo-kwd-alist)
  1484. (defvar org-todo-key-alist nil)
  1485. (make-variable-buffer-local 'org-todo-key-alist)
  1486. (defvar org-todo-key-trigger nil)
  1487. (make-variable-buffer-local 'org-todo-key-trigger)
  1488. (defcustom org-todo-interpretation 'sequence
  1489. "Controls how TODO keywords are interpreted.
  1490. This variable is in principle obsolete and is only used for
  1491. backward compatibility, if the interpretation of todo keywords is
  1492. not given already in `org-todo-keywords'. See that variable for
  1493. more information."
  1494. :group 'org-todo
  1495. :group 'org-keywords
  1496. :type '(choice (const sequence)
  1497. (const type)))
  1498. (defcustom org-use-fast-todo-selection t
  1499. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1500. This variable describes if and under what circumstances the cycling
  1501. mechanism for TODO keywords will be replaced by a single-key, direct
  1502. selection scheme.
  1503. When nil, fast selection is never used.
  1504. When the symbol `prefix', it will be used when `org-todo' is called with
  1505. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1506. in an agenda buffer.
  1507. When t, fast selection is used by default. In this case, the prefix
  1508. argument forces cycling instead.
  1509. In all cases, the special interface is only used if access keys have actually
  1510. been assigned by the user, i.e. if keywords in the configuration are followed
  1511. by a letter in parenthesis, like TODO(t)."
  1512. :group 'org-todo
  1513. :type '(choice
  1514. (const :tag "Never" nil)
  1515. (const :tag "By default" t)
  1516. (const :tag "Only with C-u C-c C-t" prefix)))
  1517. (defcustom org-provide-todo-statistics t
  1518. "Non-nil means, update todo statistics after insert and toggle.
  1519. When this is set, todo statistics is updated in the parent of the current
  1520. entry each time a todo state is changed."
  1521. :group 'org-todo
  1522. :type 'boolean)
  1523. (defcustom org-after-todo-state-change-hook nil
  1524. "Hook which is run after the state of a TODO item was changed.
  1525. The new state (a string with a TODO keyword, or nil) is available in the
  1526. Lisp variable `state'."
  1527. :group 'org-todo
  1528. :type 'hook)
  1529. (defvar org-blocker-hook nil
  1530. "Hook for functions that are allowed to block a state change.
  1531. Each function gets as its single argument a property list, see
  1532. `org-trigger-hook' for more information about this list.
  1533. If any of the functions in this hook returns nil, the state change
  1534. is blocked.")
  1535. (defvar org-trigger-hook nil
  1536. "Hook for functions that are triggered by a state change.
  1537. Each function gets as its single argument a property list with at least
  1538. the following elements:
  1539. (:type type-of-change :position pos-at-entry-start
  1540. :from old-state :to new-state)
  1541. Depending on the type, more properties may be present.
  1542. This mechanism is currently implemented for:
  1543. TODO state changes
  1544. ------------------
  1545. :type todo-state-change
  1546. :from previous state (keyword as a string), or nil, or a symbol
  1547. 'todo' or 'done', to indicate the general type of state.
  1548. :to new state, like in :from")
  1549. (defcustom org-enforce-todo-dependencies nil
  1550. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1551. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1552. be blocked if any prior sibling is not yet done.
  1553. Finally, if the parent is blocked because of ordered siblings of its own,
  1554. the child will also be blocked.
  1555. This variable needs to be set before org.el is loaded, and you need to
  1556. restart Emacs after a change to make the change effective. The only way
  1557. to change is while Emacs is running is through the customize interface."
  1558. :set (lambda (var val)
  1559. (set var val)
  1560. (if val
  1561. (add-hook 'org-blocker-hook
  1562. 'org-block-todo-from-children-or-siblings-or-parent)
  1563. (remove-hook 'org-blocker-hook
  1564. 'org-block-todo-from-children-or-siblings-or-parent)))
  1565. :group 'org-todo
  1566. :type 'boolean)
  1567. (defcustom org-enforce-todo-checkbox-dependencies nil
  1568. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1569. When this is nil, checkboxes have no influence on switching TODO states.
  1570. When non-nil, you first need to check off all check boxes before the TODO
  1571. entry can be switched to DONE.
  1572. This variable needs to be set before org.el is loaded, and you need to
  1573. restart Emacs after a change to make the change effective. The only way
  1574. to change is while Emacs is running is through the customize interface."
  1575. :set (lambda (var val)
  1576. (set var val)
  1577. (if val
  1578. (add-hook 'org-blocker-hook
  1579. 'org-block-todo-from-checkboxes)
  1580. (remove-hook 'org-blocker-hook
  1581. 'org-block-todo-from-checkboxes)))
  1582. :group 'org-todo
  1583. :type 'boolean)
  1584. (defcustom org-todo-state-tags-triggers nil
  1585. "Tag changes that should be triggered by TODO state changes.
  1586. This is a list. Each entry is
  1587. (state-change (tag . flag) .......)
  1588. State-change can be a string with a state, and empty string to indicate the
  1589. state that has no TODO keyword, or it can be one of the symbols `todo'
  1590. or `done', meaning any not-done or done state, respectively."
  1591. :group 'org-todo
  1592. :group 'org-tags
  1593. :type '(repeat
  1594. (cons (choice :tag "When changing to"
  1595. (const :tag "Not-done state" todo)
  1596. (const :tag "Done state" done)
  1597. (string :tag "State"))
  1598. (repeat
  1599. (cons :tag "Tag action"
  1600. (string :tag "Tag")
  1601. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1602. (defcustom org-log-done nil
  1603. "Information to record when a task moves to the DONE state.
  1604. Possible values are:
  1605. nil Don't add anything, just change the keyword
  1606. time Add a time stamp to the task
  1607. note Prompt a closing note and add it with template `org-log-note-headings'
  1608. This option can also be set with on a per-file-basis with
  1609. #+STARTUP: nologdone
  1610. #+STARTUP: logdone
  1611. #+STARTUP: lognotedone
  1612. You can have local logging settings for a subtree by setting the LOGGING
  1613. property to one or more of these keywords."
  1614. :group 'org-todo
  1615. :group 'org-progress
  1616. :type '(choice
  1617. (const :tag "No logging" nil)
  1618. (const :tag "Record CLOSED timestamp" time)
  1619. (const :tag "Record CLOSED timestamp with closing note." note)))
  1620. ;; Normalize old uses of org-log-done.
  1621. (cond
  1622. ((eq org-log-done t) (setq org-log-done 'time))
  1623. ((and (listp org-log-done) (memq 'done org-log-done))
  1624. (setq org-log-done 'note)))
  1625. (defcustom org-log-note-clock-out nil
  1626. "Non-nil means, record a note when clocking out of an item.
  1627. This can also be configured on a per-file basis by adding one of
  1628. the following lines anywhere in the buffer:
  1629. #+STARTUP: lognoteclock-out
  1630. #+STARTUP: nolognoteclock-out"
  1631. :group 'org-todo
  1632. :group 'org-progress
  1633. :type 'boolean)
  1634. (defcustom org-log-done-with-time t
  1635. "Non-nil means, the CLOSED time stamp will contain date and time.
  1636. When nil, only the date will be recorded."
  1637. :group 'org-progress
  1638. :type 'boolean)
  1639. (defcustom org-log-note-headings
  1640. '((done . "CLOSING NOTE %t")
  1641. (state . "State %-12s from %-12S %t")
  1642. (note . "Note taken on %t")
  1643. (clock-out . ""))
  1644. "Headings for notes added to entries.
  1645. The value is an alist, with the car being a symbol indicating the note
  1646. context, and the cdr is the heading to be used. The heading may also be the
  1647. empty string.
  1648. %t in the heading will be replaced by a time stamp.
  1649. %s will be replaced by the new TODO state, in double quotes.
  1650. %S will be replaced by the old TODO state, in double quotes.
  1651. %u will be replaced by the user name.
  1652. %U will be replaced by the full user name."
  1653. :group 'org-todo
  1654. :group 'org-progress
  1655. :type '(list :greedy t
  1656. (cons (const :tag "Heading when closing an item" done) string)
  1657. (cons (const :tag
  1658. "Heading when changing todo state (todo sequence only)"
  1659. state) string)
  1660. (cons (const :tag "Heading when just taking a note" note) string)
  1661. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1662. (unless (assq 'note org-log-note-headings)
  1663. (push '(note . "%t") org-log-note-headings))
  1664. (defcustom org-log-into-drawer nil
  1665. "Non-nil means, insert state change notes and time stamps into a drawer.
  1666. When nil, state changes notes will be inserted after the headline and
  1667. any scheduling and clock lines, but not inside a drawer.
  1668. The value of this variable should be the name of the drawer to use.
  1669. LOGBOOK is proposed at the default drawer for this purpose, you can
  1670. also set this to a string to define the drawer of your choice.
  1671. A value of t is also allowed, representing \"LOGBOOK\".
  1672. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1673. will be ignored."
  1674. :group 'org-todo
  1675. :group 'org-progress
  1676. :type '(choice
  1677. (const :tag "Not into a drawer" nil)
  1678. (const :tag "LOGBOOK" t)
  1679. (string :tag "Other")))
  1680. (if (fboundp 'defvaralias)
  1681. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1682. (defcustom org-log-state-notes-insert-after-drawers nil
  1683. "Non-nil means, insert state change notes after any drawers in entry.
  1684. Only the drawers that *immediately* follow the headline and the
  1685. deadline/scheduled line are skipped.
  1686. When nil, insert notes right after the heading and perhaps the line
  1687. with deadline/scheduling if present.
  1688. This variable will have no effect if `org-log-into-drawer' is
  1689. set."
  1690. :group 'org-todo
  1691. :group 'org-progress
  1692. :type 'boolean)
  1693. (defcustom org-log-states-order-reversed t
  1694. "Non-nil means, the latest state change note will be directly after heading.
  1695. When nil, the notes will be orderer according to time."
  1696. :group 'org-todo
  1697. :group 'org-progress
  1698. :type 'boolean)
  1699. (defcustom org-log-repeat 'time
  1700. "Non-nil means, record moving through the DONE state when triggering repeat.
  1701. An auto-repeating tasks is immediately switched back to TODO when marked
  1702. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1703. the TODO keyword definition, or recording a closing note by setting
  1704. `org-log-done', there will be no record of the task moving through DONE.
  1705. This variable forces taking a note anyway. Possible values are:
  1706. nil Don't force a record
  1707. time Record a time stamp
  1708. note Record a note
  1709. This option can also be set with on a per-file-basis with
  1710. #+STARTUP: logrepeat
  1711. #+STARTUP: lognoterepeat
  1712. #+STARTUP: nologrepeat
  1713. You can have local logging settings for a subtree by setting the LOGGING
  1714. property to one or more of these keywords."
  1715. :group 'org-todo
  1716. :group 'org-progress
  1717. :type '(choice
  1718. (const :tag "Don't force a record" nil)
  1719. (const :tag "Force recording the DONE state" time)
  1720. (const :tag "Force recording a note with the DONE state" note)))
  1721. (defgroup org-priorities nil
  1722. "Priorities in Org-mode."
  1723. :tag "Org Priorities"
  1724. :group 'org-todo)
  1725. (defcustom org-highest-priority ?A
  1726. "The highest priority of TODO items. A character like ?A, ?B etc.
  1727. Must have a smaller ASCII number than `org-lowest-priority'."
  1728. :group 'org-priorities
  1729. :type 'character)
  1730. (defcustom org-lowest-priority ?C
  1731. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1732. Must have a larger ASCII number than `org-highest-priority'."
  1733. :group 'org-priorities
  1734. :type 'character)
  1735. (defcustom org-default-priority ?B
  1736. "The default priority of TODO items.
  1737. This is the priority an item get if no explicit priority is given."
  1738. :group 'org-priorities
  1739. :type 'character)
  1740. (defcustom org-priority-start-cycle-with-default t
  1741. "Non-nil means, start with default priority when starting to cycle.
  1742. When this is nil, the first step in the cycle will be (depending on the
  1743. command used) one higher or lower that the default priority."
  1744. :group 'org-priorities
  1745. :type 'boolean)
  1746. (defgroup org-time nil
  1747. "Options concerning time stamps and deadlines in Org-mode."
  1748. :tag "Org Time"
  1749. :group 'org)
  1750. (defcustom org-insert-labeled-timestamps-at-point nil
  1751. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1752. When nil, these labeled time stamps are forces into the second line of an
  1753. entry, just after the headline. When scheduling from the global TODO list,
  1754. the time stamp will always be forced into the second line."
  1755. :group 'org-time
  1756. :type 'boolean)
  1757. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1758. "Formats for `format-time-string' which are used for time stamps.
  1759. It is not recommended to change this constant.")
  1760. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1761. "Number of minutes to round time stamps to.
  1762. These are two values, the first applies when first creating a time stamp.
  1763. The second applies when changing it with the commands `S-up' and `S-down'.
  1764. When changing the time stamp, this means that it will change in steps
  1765. of N minutes, as given by the second value.
  1766. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1767. numbers should be factors of 60, so for example 5, 10, 15.
  1768. When this is larger than 1, you can still force an exact time-stamp by using
  1769. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1770. and by using a prefix arg to `S-up/down' to specify the exact number
  1771. of minutes to shift."
  1772. :group 'org-time
  1773. :get '(lambda (var) ; Make sure all entries have 5 elements
  1774. (if (integerp (default-value var))
  1775. (list (default-value var) 5)
  1776. (default-value var)))
  1777. :type '(list
  1778. (integer :tag "when inserting times")
  1779. (integer :tag "when modifying times")))
  1780. ;; Normalize old customizations of this variable.
  1781. (when (integerp org-time-stamp-rounding-minutes)
  1782. (setq org-time-stamp-rounding-minutes
  1783. (list org-time-stamp-rounding-minutes
  1784. org-time-stamp-rounding-minutes)))
  1785. (defcustom org-display-custom-times nil
  1786. "Non-nil means, overlay custom formats over all time stamps.
  1787. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1788. To turn this on on a per-file basis, insert anywhere in the file:
  1789. #+STARTUP: customtime"
  1790. :group 'org-time
  1791. :set 'set-default
  1792. :type 'sexp)
  1793. (make-variable-buffer-local 'org-display-custom-times)
  1794. (defcustom org-time-stamp-custom-formats
  1795. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1796. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1797. These are overlayed over the default ISO format if the variable
  1798. `org-display-custom-times' is set. Time like %H:%M should be at the
  1799. end of the second format. The custom formats are also honored by export
  1800. commands, if custom time display is turned on at the time of export."
  1801. :group 'org-time
  1802. :type 'sexp)
  1803. (defun org-time-stamp-format (&optional long inactive)
  1804. "Get the right format for a time string."
  1805. (let ((f (if long (cdr org-time-stamp-formats)
  1806. (car org-time-stamp-formats))))
  1807. (if inactive
  1808. (concat "[" (substring f 1 -1) "]")
  1809. f)))
  1810. (defcustom org-time-clocksum-format "%d:%02d"
  1811. "The format string used when creating CLOCKSUM lines, or when
  1812. org-mode generates a time duration."
  1813. :group 'org-time
  1814. :type 'string)
  1815. (defcustom org-deadline-warning-days 14
  1816. "No. of days before expiration during which a deadline becomes active.
  1817. This variable governs the display in sparse trees and in the agenda.
  1818. When 0 or negative, it means use this number (the absolute value of it)
  1819. even if a deadline has a different individual lead time specified.
  1820. Custom commands can set this variable in the options section."
  1821. :group 'org-time
  1822. :group 'org-agenda-daily/weekly
  1823. :type 'integer)
  1824. (defcustom org-read-date-prefer-future t
  1825. "Non-nil means, assume future for incomplete date input from user.
  1826. This affects the following situations:
  1827. 1. The user gives a day, but no month.
  1828. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1829. read this as the third of *next* month. However, if you enter \"17\",
  1830. it will be considered as *this* month.
  1831. 2. The user gives a month but not a year.
  1832. For example, if it is april and you enter \"feb 2\", this will be read
  1833. as feb 2, *next* year. \"May 5\", however, will be this year.
  1834. Currently this does not work for ISO week specifications.
  1835. When this option is nil, the current month and year will always be used
  1836. as defaults."
  1837. :group 'org-time
  1838. :type 'boolean)
  1839. (defcustom org-read-date-display-live t
  1840. "Non-nil means, display current interpretation of date prompt live.
  1841. This display will be in an overlay, in the minibuffer."
  1842. :group 'org-time
  1843. :type 'boolean)
  1844. (defcustom org-read-date-popup-calendar t
  1845. "Non-nil means, pop up a calendar when prompting for a date.
  1846. In the calendar, the date can be selected with mouse-1. However, the
  1847. minibuffer will also be active, and you can simply enter the date as well.
  1848. When nil, only the minibuffer will be available."
  1849. :group 'org-time
  1850. :type 'boolean)
  1851. (if (fboundp 'defvaralias)
  1852. (defvaralias 'org-popup-calendar-for-date-prompt
  1853. 'org-read-date-popup-calendar))
  1854. (defcustom org-read-date-minibuffer-setup-hook nil
  1855. "Hook to be used to set up keys for the date/time interface.
  1856. Add key definitions to `minibuffer-local-map', which will be a temporary
  1857. copy."
  1858. :group 'org-time
  1859. :type 'hook)
  1860. (defcustom org-extend-today-until 0
  1861. "The hour when your day really ends. Must be an integer.
  1862. This has influence for the following applications:
  1863. - When switching the agenda to \"today\". It it is still earlier than
  1864. the time given here, the day recognized as TODAY is actually yesterday.
  1865. - When a date is read from the user and it is still before the time given
  1866. here, the current date and time will be assumed to be yesterday, 23:59.
  1867. Also, timestamps inserted in remember templates follow this rule.
  1868. IMPORTANT: This is a feature whose implementation is and likely will
  1869. remain incomplete. Really, it is only here because past midnight seems to
  1870. be the favorite working time of John Wiegley :-)"
  1871. :group 'org-time
  1872. :type 'integer)
  1873. (defcustom org-edit-timestamp-down-means-later nil
  1874. "Non-nil means, S-down will increase the time in a time stamp.
  1875. When nil, S-up will increase."
  1876. :group 'org-time
  1877. :type 'boolean)
  1878. (defcustom org-calendar-follow-timestamp-change t
  1879. "Non-nil means, make the calendar window follow timestamp changes.
  1880. When a timestamp is modified and the calendar window is visible, it will be
  1881. moved to the new date."
  1882. :group 'org-time
  1883. :type 'boolean)
  1884. (defgroup org-tags nil
  1885. "Options concerning tags in Org-mode."
  1886. :tag "Org Tags"
  1887. :group 'org)
  1888. (defcustom org-tag-alist nil
  1889. "List of tags allowed in Org-mode files.
  1890. When this list is nil, Org-mode will base TAG input on what is already in the
  1891. buffer.
  1892. The value of this variable is an alist, the car of each entry must be a
  1893. keyword as a string, the cdr may be a character that is used to select
  1894. that tag through the fast-tag-selection interface.
  1895. See the manual for details."
  1896. :group 'org-tags
  1897. :type '(repeat
  1898. (choice
  1899. (cons (string :tag "Tag name")
  1900. (character :tag "Access char"))
  1901. (const :tag "Start radio group" (:startgroup))
  1902. (const :tag "End radio group" (:endgroup))
  1903. (const :tag "New line" (:newline)))))
  1904. (defcustom org-tag-persistent-alist nil
  1905. "List of tags that will always appear in all Org-mode files.
  1906. This is in addition to any in buffer settings or customizations
  1907. of `org-tag-alist'.
  1908. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1909. The value of this variable is an alist, the car of each entry must be a
  1910. keyword as a string, the cdr may be a character that is used to select
  1911. that tag through the fast-tag-selection interface.
  1912. See the manual for details.
  1913. To disable these tags on a per-file basis, insert anywhere in the file:
  1914. #+STARTUP: noptag"
  1915. :group 'org-tags
  1916. :type '(repeat
  1917. (choice
  1918. (cons (string :tag "Tag name")
  1919. (character :tag "Access char"))
  1920. (const :tag "Start radio group" (:startgroup))
  1921. (const :tag "End radio group" (:endgroup))
  1922. (const :tag "New line" (:newline)))))
  1923. (defvar org-file-tags nil
  1924. "List of tags that can be inherited by all entries in the file.
  1925. The tags will be inherited if the variable `org-use-tag-inheritance'
  1926. says they should be.
  1927. This variable is populated from #+TAG lines.")
  1928. (defcustom org-use-fast-tag-selection 'auto
  1929. "Non-nil means, use fast tag selection scheme.
  1930. This is a special interface to select and deselect tags with single keys.
  1931. When nil, fast selection is never used.
  1932. When the symbol `auto', fast selection is used if and only if selection
  1933. characters for tags have been configured, either through the variable
  1934. `org-tag-alist' or through a #+TAGS line in the buffer.
  1935. When t, fast selection is always used and selection keys are assigned
  1936. automatically if necessary."
  1937. :group 'org-tags
  1938. :type '(choice
  1939. (const :tag "Always" t)
  1940. (const :tag "Never" nil)
  1941. (const :tag "When selection characters are configured" 'auto)))
  1942. (defcustom org-fast-tag-selection-single-key nil
  1943. "Non-nil means, fast tag selection exits after first change.
  1944. When nil, you have to press RET to exit it.
  1945. During fast tag selection, you can toggle this flag with `C-c'.
  1946. This variable can also have the value `expert'. In this case, the window
  1947. displaying the tags menu is not even shown, until you press C-c again."
  1948. :group 'org-tags
  1949. :type '(choice
  1950. (const :tag "No" nil)
  1951. (const :tag "Yes" t)
  1952. (const :tag "Expert" expert)))
  1953. (defvar org-fast-tag-selection-include-todo nil
  1954. "Non-nil means, fast tags selection interface will also offer TODO states.
  1955. This is an undocumented feature, you should not rely on it.")
  1956. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1957. "The column to which tags should be indented in a headline.
  1958. If this number is positive, it specifies the column. If it is negative,
  1959. it means that the tags should be flushright to that column. For example,
  1960. -80 works well for a normal 80 character screen."
  1961. :group 'org-tags
  1962. :type 'integer)
  1963. (defcustom org-auto-align-tags t
  1964. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1965. These operations change the length of a headline and therefore shift
  1966. the tags around. With this options turned on, after each such operation
  1967. the tags are again aligned to `org-tags-column'."
  1968. :group 'org-tags
  1969. :type 'boolean)
  1970. (defcustom org-use-tag-inheritance t
  1971. "Non-nil means, tags in levels apply also for sublevels.
  1972. When nil, only the tags directly given in a specific line apply there.
  1973. This may also be a list of tags that should be inherited, or a regexp that
  1974. matches tags that should be inherited. Additional control is possible
  1975. with the variable `org-tags-exclude-from-inheritance' which gives an
  1976. explicit list of tags to be excluded from inheritance., even if the value of
  1977. `org-use-tag-inheritance' would select it for inheritance.
  1978. If this option is t, a match early-on in a tree can lead to a large
  1979. number of matches in the subtree when constructing the agenda or creating
  1980. a sparse tree. If you only want to see the first match in a tree during
  1981. a search, check out the variable `org-tags-match-list-sublevels'."
  1982. :group 'org-tags
  1983. :type '(choice
  1984. (const :tag "Not" nil)
  1985. (const :tag "Always" t)
  1986. (repeat :tag "Specific tags" (string :tag "Tag"))
  1987. (regexp :tag "Tags matched by regexp")))
  1988. (defcustom org-tags-exclude-from-inheritance nil
  1989. "List of tags that should never be inherited.
  1990. This is a way to exclude a few tags from inheritance. For way to do
  1991. the opposite, to actively allow inheritance for selected tags,
  1992. see the variable `org-use-tag-inheritance'."
  1993. :group 'org-tags
  1994. :type '(repeat (string :tag "Tag")))
  1995. (defun org-tag-inherit-p (tag)
  1996. "Check if TAG is one that should be inherited."
  1997. (cond
  1998. ((member tag org-tags-exclude-from-inheritance) nil)
  1999. ((eq org-use-tag-inheritance t) t)
  2000. ((not org-use-tag-inheritance) nil)
  2001. ((stringp org-use-tag-inheritance)
  2002. (string-match org-use-tag-inheritance tag))
  2003. ((listp org-use-tag-inheritance)
  2004. (member tag org-use-tag-inheritance))
  2005. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2006. (defcustom org-tags-match-list-sublevels t
  2007. "Non-nil means list also sublevels of headlines matching a search.
  2008. This variable applies to tags/property searches, and also to stuck
  2009. projects because this search is based on a tags match as well.
  2010. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2011. the sublevels of a headline matching a tag search often also match
  2012. the same search. Listing all of them can create very long lists.
  2013. Setting this variable to nil causes subtrees of a match to be skipped.
  2014. As a special case, if the tag search is restricted to TODO items, the
  2015. value of this variable is ignored and sublevels are always checked, to
  2016. make sure all corresponding TODO items find their way into the list.
  2017. This variable is semi-obsolete and probably should always be true. It
  2018. is better to limit inheritance to certain tags using the variables
  2019. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2020. :group 'org-tags
  2021. :type '(choice
  2022. (const :tag "No, don't list them" nil)
  2023. (const :tag "Yes, do list them" t)
  2024. (const :tag "List them, indented with leading dots" indented)))
  2025. (defvar org-tags-history nil
  2026. "History of minibuffer reads for tags.")
  2027. (defvar org-last-tags-completion-table nil
  2028. "The last used completion table for tags.")
  2029. (defvar org-after-tags-change-hook nil
  2030. "Hook that is run after the tags in a line have changed.")
  2031. (defgroup org-properties nil
  2032. "Options concerning properties in Org-mode."
  2033. :tag "Org Properties"
  2034. :group 'org)
  2035. (defcustom org-property-format "%-10s %s"
  2036. "How property key/value pairs should be formatted by `indent-line'.
  2037. When `indent-line' hits a property definition, it will format the line
  2038. according to this format, mainly to make sure that the values are
  2039. lined-up with respect to each other."
  2040. :group 'org-properties
  2041. :type 'string)
  2042. (defcustom org-use-property-inheritance nil
  2043. "Non-nil means, properties apply also for sublevels.
  2044. This setting is chiefly used during property searches. Turning it on can
  2045. cause significant overhead when doing a search, which is why it is not
  2046. on by default.
  2047. When nil, only the properties directly given in the current entry count.
  2048. When t, every property is inherited. The value may also be a list of
  2049. properties that should have inheritance, or a regular expression matching
  2050. properties that should be inherited.
  2051. However, note that some special properties use inheritance under special
  2052. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2053. and the properties ending in \"_ALL\" when they are used as descriptor
  2054. for valid values of a property.
  2055. Note for programmers:
  2056. When querying an entry with `org-entry-get', you can control if inheritance
  2057. should be used. By default, `org-entry-get' looks only at the local
  2058. properties. You can request inheritance by setting the inherit argument
  2059. to t (to force inheritance) or to `selective' (to respect the setting
  2060. in this variable)."
  2061. :group 'org-properties
  2062. :type '(choice
  2063. (const :tag "Not" nil)
  2064. (const :tag "Always" t)
  2065. (repeat :tag "Specific properties" (string :tag "Property"))
  2066. (regexp :tag "Properties matched by regexp")))
  2067. (defun org-property-inherit-p (property)
  2068. "Check if PROPERTY is one that should be inherited."
  2069. (cond
  2070. ((eq org-use-property-inheritance t) t)
  2071. ((not org-use-property-inheritance) nil)
  2072. ((stringp org-use-property-inheritance)
  2073. (string-match org-use-property-inheritance property))
  2074. ((listp org-use-property-inheritance)
  2075. (member property org-use-property-inheritance))
  2076. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2077. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2078. "The default column format, if no other format has been defined.
  2079. This variable can be set on the per-file basis by inserting a line
  2080. #+COLUMNS: %25ITEM ....."
  2081. :group 'org-properties
  2082. :type 'string)
  2083. (defcustom org-columns-ellipses ".."
  2084. "The ellipses to be used when a field in column view is truncated.
  2085. When this is the empty string, as many characters as possible are shown,
  2086. but then there will be no visual indication that the field has been truncated.
  2087. When this is a string of length N, the last N characters of a truncated
  2088. field are replaced by this string. If the column is narrower than the
  2089. ellipses string, only part of the ellipses string will be shown."
  2090. :group 'org-properties
  2091. :type 'string)
  2092. (defcustom org-columns-modify-value-for-display-function nil
  2093. "Function that modifies values for display in column view.
  2094. For example, it can be used to cut out a certain part from a time stamp.
  2095. The function must take 2 arguments:
  2096. column-title The title of the column (*not* the property name)
  2097. value The value that should be modified.
  2098. The function should return the value that should be displayed,
  2099. or nil if the normal value should be used."
  2100. :group 'org-properties
  2101. :type 'function)
  2102. (defcustom org-effort-property "Effort"
  2103. "The property that is being used to keep track of effort estimates.
  2104. Effort estimates given in this property need to have the format H:MM."
  2105. :group 'org-properties
  2106. :group 'org-progress
  2107. :type '(string :tag "Property"))
  2108. (defconst org-global-properties-fixed
  2109. '(("VISIBILITY_ALL" . "folded children content all"))
  2110. "List of property/value pairs that can be inherited by any entry.
  2111. These are fixed values, for the preset properties. The user variable
  2112. that can be used to add to this list is `org-global-properties'.
  2113. The entries in this list are cons cells where the car is a property
  2114. name and cdr is a string with the value. If the value represents
  2115. multiple items like an \"_ALL\" property, separate the items by
  2116. spaces.")
  2117. (defcustom org-global-properties nil
  2118. "List of property/value pairs that can be inherited by any entry.
  2119. This list will be combined with the constant `org-global-properties-fixed'.
  2120. The entries in this list are cons cells where the car is a property
  2121. name and cdr is a string with the value.
  2122. You can set buffer-local values for the same purpose in the variable
  2123. `org-file-properties' this by adding lines like
  2124. #+PROPERTY: NAME VALUE"
  2125. :group 'org-properties
  2126. :type '(repeat
  2127. (cons (string :tag "Property")
  2128. (string :tag "Value"))))
  2129. (defvar org-file-properties nil
  2130. "List of property/value pairs that can be inherited by any entry.
  2131. Valid for the current buffer.
  2132. This variable is populated from #+PROPERTY lines.")
  2133. (make-variable-buffer-local 'org-file-properties)
  2134. (defgroup org-agenda nil
  2135. "Options concerning agenda views in Org-mode."
  2136. :tag "Org Agenda"
  2137. :group 'org)
  2138. (defvar org-category nil
  2139. "Variable used by org files to set a category for agenda display.
  2140. Such files should use a file variable to set it, for example
  2141. # -*- mode: org; org-category: \"ELisp\"
  2142. or contain a special line
  2143. #+CATEGORY: ELisp
  2144. If the file does not specify a category, then file's base name
  2145. is used instead.")
  2146. (make-variable-buffer-local 'org-category)
  2147. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2148. (defcustom org-agenda-files nil
  2149. "The files to be used for agenda display.
  2150. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2151. \\[org-remove-file]. You can also use customize to edit the list.
  2152. If an entry is a directory, all files in that directory that are matched by
  2153. `org-agenda-file-regexp' will be part of the file list.
  2154. If the value of the variable is not a list but a single file name, then
  2155. the list of agenda files is actually stored and maintained in that file, one
  2156. agenda file per line."
  2157. :group 'org-agenda
  2158. :type '(choice
  2159. (repeat :tag "List of files and directories" file)
  2160. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2161. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2162. "Regular expression to match files for `org-agenda-files'.
  2163. If any element in the list in that variable contains a directory instead
  2164. of a normal file, all files in that directory that are matched by this
  2165. regular expression will be included."
  2166. :group 'org-agenda
  2167. :type 'regexp)
  2168. (defcustom org-agenda-text-search-extra-files nil
  2169. "List of extra files to be searched by text search commands.
  2170. These files will be search in addition to the agenda files by the
  2171. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2172. Note that these files will only be searched for text search commands,
  2173. not for the other agenda views like todo lists, tag searches or the weekly
  2174. agenda. This variable is intended to list notes and possibly archive files
  2175. that should also be searched by these two commands.
  2176. In fact, if the first element in the list is the symbol `agenda-archives',
  2177. than all archive files of all agenda files will be added to the search
  2178. scope."
  2179. :group 'org-agenda
  2180. :type '(set :greedy t
  2181. (const :tag "Agenda Archives" agenda-archives)
  2182. (repeat :inline t (file))))
  2183. (if (fboundp 'defvaralias)
  2184. (defvaralias 'org-agenda-multi-occur-extra-files
  2185. 'org-agenda-text-search-extra-files))
  2186. (defcustom org-agenda-skip-unavailable-files nil
  2187. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2188. A nil value means to remove them, after a query, from the list."
  2189. :group 'org-agenda
  2190. :type 'boolean)
  2191. (defcustom org-calendar-to-agenda-key [?c]
  2192. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2193. The command `org-calendar-goto-agenda' will be bound to this key. The
  2194. default is the character `c' because then `c' can be used to switch back and
  2195. forth between agenda and calendar."
  2196. :group 'org-agenda
  2197. :type 'sexp)
  2198. (defcustom org-calendar-agenda-action-key [?k]
  2199. "The key to be installed in `calendar-mode-map' for agenda-action.
  2200. The command `org-agenda-action' will be bound to this key. The
  2201. default is the character `k' because we use the same key in the agenda."
  2202. :group 'org-agenda
  2203. :type 'sexp)
  2204. (eval-after-load "calendar"
  2205. '(progn
  2206. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2207. 'org-calendar-goto-agenda)
  2208. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2209. 'org-agenda-action)))
  2210. (defgroup org-latex nil
  2211. "Options for embedding LaTeX code into Org-mode."
  2212. :tag "Org LaTeX"
  2213. :group 'org)
  2214. (defcustom org-format-latex-options
  2215. '(:foreground default :background default :scale 1.0
  2216. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2217. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2218. "Options for creating images from LaTeX fragments.
  2219. This is a property list with the following properties:
  2220. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2221. `default' means use the foreground of the default face.
  2222. :background the background color, or \"Transparent\".
  2223. `default' means use the background of the default face.
  2224. :scale a scaling factor for the size of the images.
  2225. :html-foreground, :html-background, :html-scale
  2226. the same numbers for HTML export.
  2227. :matchers a list indicating which matchers should be used to
  2228. find LaTeX fragments. Valid members of this list are:
  2229. \"begin\" find environments
  2230. \"$1\" find single characters surrounded by $.$
  2231. \"$\" find math expressions surrounded by $...$
  2232. \"$$\" find math expressions surrounded by $$....$$
  2233. \"\\(\" find math expressions surrounded by \\(...\\)
  2234. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2235. :group 'org-latex
  2236. :type 'plist)
  2237. (defcustom org-format-latex-header "\\documentclass{article}
  2238. \\usepackage{fullpage} % do not remove
  2239. \\usepackage{amssymb}
  2240. \\usepackage[usenames]{color}
  2241. \\usepackage{amsmath}
  2242. \\usepackage{latexsym}
  2243. \\usepackage[mathscr]{eucal}
  2244. \\pagestyle{empty} % do not remove"
  2245. "The document header used for processing LaTeX fragments."
  2246. :group 'org-latex
  2247. :type 'string)
  2248. (defgroup org-font-lock nil
  2249. "Font-lock settings for highlighting in Org-mode."
  2250. :tag "Org Font Lock"
  2251. :group 'org)
  2252. (defcustom org-level-color-stars-only nil
  2253. "Non-nil means fontify only the stars in each headline.
  2254. When nil, the entire headline is fontified.
  2255. Changing it requires restart of `font-lock-mode' to become effective
  2256. also in regions already fontified."
  2257. :group 'org-font-lock
  2258. :type 'boolean)
  2259. (defcustom org-hide-leading-stars nil
  2260. "Non-nil means, hide the first N-1 stars in a headline.
  2261. This works by using the face `org-hide' for these stars. This
  2262. face is white for a light background, and black for a dark
  2263. background. You may have to customize the face `org-hide' to
  2264. make this work.
  2265. Changing it requires restart of `font-lock-mode' to become effective
  2266. also in regions already fontified.
  2267. You may also set this on a per-file basis by adding one of the following
  2268. lines to the buffer:
  2269. #+STARTUP: hidestars
  2270. #+STARTUP: showstars"
  2271. :group 'org-font-lock
  2272. :type 'boolean)
  2273. (defcustom org-fontify-done-headline nil
  2274. "Non-nil means, change the face of a headline if it is marked DONE.
  2275. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2276. When this is non-nil, the headline after the keyword is set to the
  2277. `org-headline-done' as an additional indication."
  2278. :group 'org-font-lock
  2279. :type 'boolean)
  2280. (defcustom org-fontify-emphasized-text t
  2281. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2282. Changing this variable requires a restart of Emacs to take effect."
  2283. :group 'org-font-lock
  2284. :type 'boolean)
  2285. (defcustom org-highlight-latex-fragments-and-specials nil
  2286. "Non-nil means, fontify what is treated specially by the exporters."
  2287. :group 'org-font-lock
  2288. :type 'boolean)
  2289. (defcustom org-hide-emphasis-markers nil
  2290. "Non-nil mean font-lock should hide the emphasis marker characters."
  2291. :group 'org-font-lock
  2292. :type 'boolean)
  2293. (defvar org-emph-re nil
  2294. "Regular expression for matching emphasis.")
  2295. (defvar org-verbatim-re nil
  2296. "Regular expression for matching verbatim text.")
  2297. (defvar org-emphasis-regexp-components) ; defined just below
  2298. (defvar org-emphasis-alist) ; defined just below
  2299. (defun org-set-emph-re (var val)
  2300. "Set variable and compute the emphasis regular expression."
  2301. (set var val)
  2302. (when (and (boundp 'org-emphasis-alist)
  2303. (boundp 'org-emphasis-regexp-components)
  2304. org-emphasis-alist org-emphasis-regexp-components)
  2305. (let* ((e org-emphasis-regexp-components)
  2306. (pre (car e))
  2307. (post (nth 1 e))
  2308. (border (nth 2 e))
  2309. (body (nth 3 e))
  2310. (nl (nth 4 e))
  2311. (body1 (concat body "*?"))
  2312. (markers (mapconcat 'car org-emphasis-alist ""))
  2313. (vmarkers (mapconcat
  2314. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2315. org-emphasis-alist "")))
  2316. ;; make sure special characters appear at the right position in the class
  2317. (if (string-match "\\^" markers)
  2318. (setq markers (concat (replace-match "" t t markers) "^")))
  2319. (if (string-match "-" markers)
  2320. (setq markers (concat (replace-match "" t t markers) "-")))
  2321. (if (string-match "\\^" vmarkers)
  2322. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2323. (if (string-match "-" vmarkers)
  2324. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2325. (if (> nl 0)
  2326. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2327. (int-to-string nl) "\\}")))
  2328. ;; Make the regexp
  2329. (setq org-emph-re
  2330. (concat "\\([" pre "]\\|^\\)"
  2331. "\\("
  2332. "\\([" markers "]\\)"
  2333. "\\("
  2334. "[^" border "]\\|"
  2335. "[^" border "]"
  2336. body1
  2337. "[^" border "]"
  2338. "\\)"
  2339. "\\3\\)"
  2340. "\\([" post "]\\|$\\)"))
  2341. (setq org-verbatim-re
  2342. (concat "\\([" pre "]\\|^\\)"
  2343. "\\("
  2344. "\\([" vmarkers "]\\)"
  2345. "\\("
  2346. "[^" border "]\\|"
  2347. "[^" border "]"
  2348. body1
  2349. "[^" border "]"
  2350. "\\)"
  2351. "\\3\\)"
  2352. "\\([" post "]\\|$\\)")))))
  2353. (defcustom org-emphasis-regexp-components
  2354. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2355. "Components used to build the regular expression for emphasis.
  2356. This is a list with 6 entries. Terminology: In an emphasis string
  2357. like \" *strong word* \", we call the initial space PREMATCH, the final
  2358. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2359. and \"trong wor\" is the body. The different components in this variable
  2360. specify what is allowed/forbidden in each part:
  2361. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2362. post Chars allowed as postmatch. End of line will be allowed too.
  2363. border The chars *forbidden* as border characters.
  2364. body-regexp A regexp like \".\" to match a body character. Don't use
  2365. non-shy groups here, and don't allow newline here.
  2366. newline The maximum number of newlines allowed in an emphasis exp.
  2367. Use customize to modify this, or restart Emacs after changing it."
  2368. :group 'org-font-lock
  2369. :set 'org-set-emph-re
  2370. :type '(list
  2371. (sexp :tag "Allowed chars in pre ")
  2372. (sexp :tag "Allowed chars in post ")
  2373. (sexp :tag "Forbidden chars in border ")
  2374. (sexp :tag "Regexp for body ")
  2375. (integer :tag "number of newlines allowed")
  2376. (option (boolean :tag "Please ignore this button"))))
  2377. (defcustom org-emphasis-alist
  2378. `(("*" bold "<b>" "</b>")
  2379. ("/" italic "<i>" "</i>")
  2380. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2381. ("=" org-code "<code>" "</code>" verbatim)
  2382. ("~" org-verbatim "<code>" "</code>" verbatim)
  2383. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2384. "<del>" "</del>")
  2385. )
  2386. "Special syntax for emphasized text.
  2387. Text starting and ending with a special character will be emphasized, for
  2388. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2389. characters, the face to be used by font-lock for highlighting in Org-mode
  2390. Emacs buffers, and the HTML tags to be used for this.
  2391. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2392. Use customize to modify this, or restart Emacs after changing it."
  2393. :group 'org-font-lock
  2394. :set 'org-set-emph-re
  2395. :type '(repeat
  2396. (list
  2397. (string :tag "Marker character")
  2398. (choice
  2399. (face :tag "Font-lock-face")
  2400. (plist :tag "Face property list"))
  2401. (string :tag "HTML start tag")
  2402. (string :tag "HTML end tag")
  2403. (option (const verbatim)))))
  2404. ;;; Miscellaneous options
  2405. (defgroup org-completion nil
  2406. "Completion in Org-mode."
  2407. :tag "Org Completion"
  2408. :group 'org)
  2409. (defcustom org-completion-use-ido nil
  2410. "Non-nil means, use ido completion wherever possible.
  2411. Note that `ido-mode' must be active for this variable to be relevant.
  2412. If you decide to turn this variable on, you might well want to turn off
  2413. `org-outline-path-complete-in-steps'."
  2414. :group 'org-completion
  2415. :type 'boolean)
  2416. (defcustom org-completion-fallback-command 'hippie-expand
  2417. "The expansion command called by \\[org-complete] in normal context.
  2418. Normal means, no org-mode-specific context."
  2419. :group 'org-completion
  2420. :type 'function)
  2421. ;;; Functions and variables from ther packages
  2422. ;; Declared here to avoid compiler warnings
  2423. ;; XEmacs only
  2424. (defvar outline-mode-menu-heading)
  2425. (defvar outline-mode-menu-show)
  2426. (defvar outline-mode-menu-hide)
  2427. (defvar zmacs-regions) ; XEmacs regions
  2428. ;; Emacs only
  2429. (defvar mark-active)
  2430. ;; Various packages
  2431. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2432. (declare-function calendar-forward-day "cal-move" (arg))
  2433. (declare-function calendar-goto-date "cal-move" (date))
  2434. (declare-function calendar-goto-today "cal-move" ())
  2435. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2436. (defvar calc-embedded-close-formula)
  2437. (defvar calc-embedded-open-formula)
  2438. (declare-function cdlatex-tab "ext:cdlatex" ())
  2439. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2440. (defvar font-lock-unfontify-region-function)
  2441. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2442. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2443. (defvar iswitchb-temp-buflist)
  2444. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2445. (defvar org-agenda-tags-todo-honor-ignore-options)
  2446. (declare-function org-agenda-skip "org-agenda" ())
  2447. (declare-function org-format-agenda-item "org-agenda"
  2448. (extra txt &optional category tags dotime noprefix remove-re))
  2449. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2450. (declare-function org-agenda-change-all-lines "org-agenda"
  2451. (newhead hdmarker &optional fixface just-this))
  2452. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2453. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2454. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2455. (beg end))
  2456. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2457. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2458. "org-agenda" (&optional end))
  2459. (declare-function parse-time-string "parse-time" (string))
  2460. (declare-function remember "remember" (&optional initial))
  2461. (declare-function remember-buffer-desc "remember" ())
  2462. (declare-function remember-finalize "remember" ())
  2463. (defvar remember-save-after-remembering)
  2464. (defvar remember-data-file)
  2465. (defvar remember-register)
  2466. (defvar remember-buffer)
  2467. (defvar remember-handler-functions)
  2468. (defvar remember-annotation-functions)
  2469. (defvar texmathp-why)
  2470. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2471. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2472. (defvar w3m-current-url)
  2473. (defvar w3m-current-title)
  2474. (defvar org-latex-regexps)
  2475. ;;; Autoload and prepare some org modules
  2476. ;; Some table stuff that needs to be defined here, because it is used
  2477. ;; by the functions setting up org-mode or checking for table context.
  2478. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2479. "Detects an org-type or table-type table.")
  2480. (defconst org-table-line-regexp "^[ \t]*|"
  2481. "Detects an org-type table line.")
  2482. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2483. "Detects an org-type table line.")
  2484. (defconst org-table-hline-regexp "^[ \t]*|-"
  2485. "Detects an org-type table hline.")
  2486. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2487. "Detects a table-type table hline.")
  2488. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2489. "Searching from within a table (any type) this finds the first line
  2490. outside the table.")
  2491. ;; Autoload the functions in org-table.el that are needed by functions here.
  2492. (eval-and-compile
  2493. (org-autoload "org-table"
  2494. '(org-table-align org-table-begin org-table-blank-field
  2495. org-table-convert org-table-convert-region org-table-copy-down
  2496. org-table-copy-region org-table-create
  2497. org-table-create-or-convert-from-region
  2498. org-table-create-with-table.el org-table-current-dline
  2499. org-table-cut-region org-table-delete-column org-table-edit-field
  2500. org-table-edit-formulas org-table-end org-table-eval-formula
  2501. org-table-export org-table-field-info
  2502. org-table-get-stored-formulas org-table-goto-column
  2503. org-table-hline-and-move org-table-import org-table-insert-column
  2504. org-table-insert-hline org-table-insert-row org-table-iterate
  2505. org-table-justify-field-maybe org-table-kill-row
  2506. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2507. org-table-move-column org-table-move-column-left
  2508. org-table-move-column-right org-table-move-row
  2509. org-table-move-row-down org-table-move-row-up
  2510. org-table-next-field org-table-next-row org-table-paste-rectangle
  2511. org-table-previous-field org-table-recalculate
  2512. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2513. org-table-toggle-coordinate-overlays
  2514. org-table-toggle-formula-debugger org-table-wrap-region
  2515. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2516. (defun org-at-table-p (&optional table-type)
  2517. "Return t if the cursor is inside an org-type table.
  2518. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2519. (if org-enable-table-editor
  2520. (save-excursion
  2521. (beginning-of-line 1)
  2522. (looking-at (if table-type org-table-any-line-regexp
  2523. org-table-line-regexp)))
  2524. nil))
  2525. (defsubst org-table-p () (org-at-table-p))
  2526. (defun org-at-table.el-p ()
  2527. "Return t if and only if we are at a table.el table."
  2528. (and (org-at-table-p 'any)
  2529. (save-excursion
  2530. (goto-char (org-table-begin 'any))
  2531. (looking-at org-table1-hline-regexp))))
  2532. (defun org-table-recognize-table.el ()
  2533. "If there is a table.el table nearby, recognize it and move into it."
  2534. (if org-table-tab-recognizes-table.el
  2535. (if (org-at-table.el-p)
  2536. (progn
  2537. (beginning-of-line 1)
  2538. (if (looking-at org-table-dataline-regexp)
  2539. nil
  2540. (if (looking-at org-table1-hline-regexp)
  2541. (progn
  2542. (beginning-of-line 2)
  2543. (if (looking-at org-table-any-border-regexp)
  2544. (beginning-of-line -1)))))
  2545. (if (re-search-forward "|" (org-table-end t) t)
  2546. (progn
  2547. (require 'table)
  2548. (if (table--at-cell-p (point))
  2549. t
  2550. (message "recognizing table.el table...")
  2551. (table-recognize-table)
  2552. (message "recognizing table.el table...done")))
  2553. (error "This should not happen..."))
  2554. t)
  2555. nil)
  2556. nil))
  2557. (defun org-at-table-hline-p ()
  2558. "Return t if the cursor is inside a hline in a table."
  2559. (if org-enable-table-editor
  2560. (save-excursion
  2561. (beginning-of-line 1)
  2562. (looking-at org-table-hline-regexp))
  2563. nil))
  2564. (defvar org-table-clean-did-remove-column nil)
  2565. (defun org-table-map-tables (function)
  2566. "Apply FUNCTION to the start of all tables in the buffer."
  2567. (save-excursion
  2568. (save-restriction
  2569. (widen)
  2570. (goto-char (point-min))
  2571. (while (re-search-forward org-table-any-line-regexp nil t)
  2572. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2573. (beginning-of-line 1)
  2574. (if (looking-at org-table-line-regexp)
  2575. (save-excursion (funcall function)))
  2576. (re-search-forward org-table-any-border-regexp nil 1))))
  2577. (message "Mapping tables: done"))
  2578. ;; Declare and autoload functions from org-exp.el & Co
  2579. (declare-function org-default-export-plist "org-exp")
  2580. (declare-function org-infile-export-plist "org-exp")
  2581. (declare-function org-get-current-options "org-exp")
  2582. (eval-and-compile
  2583. (org-autoload "org-exp"
  2584. '(org-export org-export-visible
  2585. org-insert-export-options-template
  2586. org-table-clean-before-export))
  2587. (org-autoload "org-ascii"
  2588. '(org-export-as-ascii org-export-ascii-preprocess))
  2589. (org-autoload "org-html"
  2590. '(org-export-as-html-and-open
  2591. org-export-as-html-batch org-export-as-html-to-buffer
  2592. org-replace-region-by-html org-export-region-as-html
  2593. org-export-as-html))
  2594. (org-autoload "org-icalendar"
  2595. '(org-export-icalendar-this-file
  2596. org-export-icalendar-all-agenda-files
  2597. org-export-icalendar-combine-agenda-files))
  2598. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2599. ;; Declare and autoload functions from org-agenda.el
  2600. (eval-and-compile
  2601. (org-autoload "org-agenda"
  2602. '(org-agenda org-agenda-list org-search-view
  2603. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2604. org-diary org-agenda-to-appt
  2605. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2606. ;; Autoload org-remember
  2607. (eval-and-compile
  2608. (org-autoload "org-remember"
  2609. '(org-remember-insinuate org-remember-annotation
  2610. org-remember-apply-template org-remember org-remember-handler)))
  2611. ;; Autoload org-clock.el
  2612. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2613. (beg end))
  2614. (declare-function org-clock-update-mode-line "org-clock" ())
  2615. (defvar org-clock-start-time)
  2616. (defvar org-clock-marker (make-marker)
  2617. "Marker recording the last clock-in.")
  2618. (eval-and-compile
  2619. (org-autoload
  2620. "org-clock"
  2621. '(org-clock-in org-clock-out org-clock-cancel
  2622. org-clock-goto org-clock-sum org-clock-display
  2623. org-clock-remove-overlays org-clock-report
  2624. org-clocktable-shift org-dblock-write:clocktable
  2625. org-get-clocktable)))
  2626. (defun org-clock-update-time-maybe ()
  2627. "If this is a CLOCK line, update it and return t.
  2628. Otherwise, return nil."
  2629. (interactive)
  2630. (save-excursion
  2631. (beginning-of-line 1)
  2632. (skip-chars-forward " \t")
  2633. (when (looking-at org-clock-string)
  2634. (let ((re (concat "[ \t]*" org-clock-string
  2635. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2636. "\\([ \t]*=>.*\\)?\\)?"))
  2637. ts te h m s neg)
  2638. (cond
  2639. ((not (looking-at re))
  2640. nil)
  2641. ((not (match-end 2))
  2642. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2643. (> org-clock-marker (point))
  2644. (<= org-clock-marker (point-at-eol)))
  2645. ;; The clock is running here
  2646. (setq org-clock-start-time
  2647. (apply 'encode-time
  2648. (org-parse-time-string (match-string 1))))
  2649. (org-clock-update-mode-line)))
  2650. (t
  2651. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2652. (end-of-line 1)
  2653. (setq ts (match-string 1)
  2654. te (match-string 3))
  2655. (setq s (- (time-to-seconds
  2656. (apply 'encode-time (org-parse-time-string te)))
  2657. (time-to-seconds
  2658. (apply 'encode-time (org-parse-time-string ts))))
  2659. neg (< s 0)
  2660. s (abs s)
  2661. h (floor (/ s 3600))
  2662. s (- s (* 3600 h))
  2663. m (floor (/ s 60))
  2664. s (- s (* 60 s)))
  2665. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2666. t))))))
  2667. (defun org-check-running-clock ()
  2668. "Check if the current buffer contains the running clock.
  2669. If yes, offer to stop it and to save the buffer with the changes."
  2670. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2671. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2672. (buffer-name))))
  2673. (org-clock-out)
  2674. (when (y-or-n-p "Save changed buffer?")
  2675. (save-buffer))))
  2676. (defun org-clocktable-try-shift (dir n)
  2677. "Check if this line starts a clock table, if yes, shift the time block."
  2678. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2679. (org-clocktable-shift dir n)))
  2680. ;; Autoload org-timer.el
  2681. (eval-and-compile
  2682. (org-autoload
  2683. "org-timer"
  2684. '(org-timer-start org-timer org-timer-item
  2685. org-timer-change-times-in-region)))
  2686. ;; Autoload org-feed.el
  2687. (eval-and-compile
  2688. (org-autoload
  2689. "org-feed"
  2690. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2691. ;; Autoload archiving code
  2692. ;; The stuff that is needed for cycling and tags has to be defined here.
  2693. (defgroup org-archive nil
  2694. "Options concerning archiving in Org-mode."
  2695. :tag "Org Archive"
  2696. :group 'org-structure)
  2697. (defcustom org-archive-location "%s_archive::"
  2698. "The location where subtrees should be archived.
  2699. The value of this variable is a string, consisting of two parts,
  2700. separated by a double-colon. The first part is a filename and
  2701. the second part is a headline.
  2702. When the filename is omitted, archiving happens in the same file.
  2703. %s in the filename will be replaced by the current file
  2704. name (without the directory part). Archiving to a different file
  2705. is useful to keep archived entries from contributing to the
  2706. Org-mode Agenda.
  2707. The archived entries will be filed as subtrees of the specified
  2708. headline. When the headline is omitted, the subtrees are simply
  2709. filed away at the end of the file, as top-level entries. Also in
  2710. the heading you can use %s to represent the file name, this can be
  2711. useful when using the same archive for a number of different files.
  2712. Here are a few examples:
  2713. \"%s_archive::\"
  2714. If the current file is Projects.org, archive in file
  2715. Projects.org_archive, as top-level trees. This is the default.
  2716. \"::* Archived Tasks\"
  2717. Archive in the current file, under the top-level headline
  2718. \"* Archived Tasks\".
  2719. \"~/org/archive.org::\"
  2720. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2721. \"~/org/archive.org::From %s\"
  2722. Archive in file ~/org/archive.org (absolute path), und headlines
  2723. \"From FILENAME\" where file name is the current file name.
  2724. \"basement::** Finished Tasks\"
  2725. Archive in file ./basement (relative path), as level 3 trees
  2726. below the level 2 heading \"** Finished Tasks\".
  2727. You may set this option on a per-file basis by adding to the buffer a
  2728. line like
  2729. #+ARCHIVE: basement::** Finished Tasks
  2730. You may also define it locally for a subtree by setting an ARCHIVE property
  2731. in the entry. If such a property is found in an entry, or anywhere up
  2732. the hierarchy, it will be used."
  2733. :group 'org-archive
  2734. :type 'string)
  2735. (defcustom org-archive-tag "ARCHIVE"
  2736. "The tag that marks a subtree as archived.
  2737. An archived subtree does not open during visibility cycling, and does
  2738. not contribute to the agenda listings.
  2739. After changing this, font-lock must be restarted in the relevant buffers to
  2740. get the proper fontification."
  2741. :group 'org-archive
  2742. :group 'org-keywords
  2743. :type 'string)
  2744. (defcustom org-agenda-skip-archived-trees t
  2745. "Non-nil means, the agenda will skip any items located in archived trees.
  2746. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2747. variable is no longer recommended, you should leave it at the value t.
  2748. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2749. :group 'org-archive
  2750. :group 'org-agenda-skip
  2751. :type 'boolean)
  2752. (defcustom org-columns-skip-arrchived-trees t
  2753. "Non-nil means, irgnore archived trees when creating column view."
  2754. :group 'org-archive
  2755. :group 'org-properties
  2756. :type 'boolean)
  2757. (defcustom org-cycle-open-archived-trees nil
  2758. "Non-nil means, `org-cycle' will open archived trees.
  2759. An archived tree is a tree marked with the tag ARCHIVE.
  2760. When nil, archived trees will stay folded. You can still open them with
  2761. normal outline commands like `show-all', but not with the cycling commands."
  2762. :group 'org-archive
  2763. :group 'org-cycle
  2764. :type 'boolean)
  2765. (defcustom org-sparse-tree-open-archived-trees nil
  2766. "Non-nil means sparse tree construction shows matches in archived trees.
  2767. When nil, matches in these trees are highlighted, but the trees are kept in
  2768. collapsed state."
  2769. :group 'org-archive
  2770. :group 'org-sparse-trees
  2771. :type 'boolean)
  2772. (defun org-cycle-hide-archived-subtrees (state)
  2773. "Re-hide all archived subtrees after a visibility state change."
  2774. (when (and (not org-cycle-open-archived-trees)
  2775. (not (memq state '(overview folded))))
  2776. (save-excursion
  2777. (let* ((globalp (memq state '(contents all)))
  2778. (beg (if globalp (point-min) (point)))
  2779. (end (if globalp (point-max) (org-end-of-subtree t))))
  2780. (org-hide-archived-subtrees beg end)
  2781. (goto-char beg)
  2782. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2783. (message "%s" (substitute-command-keys
  2784. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2785. (defun org-force-cycle-archived ()
  2786. "Cycle subtree even if it is archived."
  2787. (interactive)
  2788. (setq this-command 'org-cycle)
  2789. (let ((org-cycle-open-archived-trees t))
  2790. (call-interactively 'org-cycle)))
  2791. (defun org-hide-archived-subtrees (beg end)
  2792. "Re-hide all archived subtrees after a visibility state change."
  2793. (save-excursion
  2794. (let* ((re (concat ":" org-archive-tag ":")))
  2795. (goto-char beg)
  2796. (while (re-search-forward re end t)
  2797. (and (org-on-heading-p) (hide-subtree))
  2798. (org-end-of-subtree t)))))
  2799. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2800. (eval-and-compile
  2801. (org-autoload "org-archive"
  2802. '(org-add-archive-files org-archive-subtree
  2803. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2804. ;; Autoload Column View Code
  2805. (declare-function org-columns-number-to-string "org-colview")
  2806. (declare-function org-columns-get-format-and-top-level "org-colview")
  2807. (declare-function org-columns-compute "org-colview")
  2808. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2809. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2810. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2811. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2812. ;; Autoload ID code
  2813. (declare-function org-id-store-link "org-id")
  2814. (declare-function org-id-locations-load "org-id")
  2815. (declare-function org-id-locations-save "org-id")
  2816. (defvar org-id-track-globally)
  2817. (org-autoload "org-id"
  2818. '(org-id-get-create org-id-new org-id-copy org-id-get
  2819. org-id-get-with-outline-path-completion
  2820. org-id-get-with-outline-drilling
  2821. org-id-goto org-id-find org-id-store-link))
  2822. ;; Autoload Plotting Code
  2823. (org-autoload "org-plot"
  2824. '(org-plot/gnuplot))
  2825. ;;; Variables for pre-computed regular expressions, all buffer local
  2826. (defvar org-drawer-regexp nil
  2827. "Matches first line of a hidden block.")
  2828. (make-variable-buffer-local 'org-drawer-regexp)
  2829. (defvar org-todo-regexp nil
  2830. "Matches any of the TODO state keywords.")
  2831. (make-variable-buffer-local 'org-todo-regexp)
  2832. (defvar org-not-done-regexp nil
  2833. "Matches any of the TODO state keywords except the last one.")
  2834. (make-variable-buffer-local 'org-not-done-regexp)
  2835. (defvar org-not-done-heading-regexp nil
  2836. "Matches a TODO headline that is not done.")
  2837. (make-variable-buffer-local 'org-not-done-regexp)
  2838. (defvar org-todo-line-regexp nil
  2839. "Matches a headline and puts TODO state into group 2 if present.")
  2840. (make-variable-buffer-local 'org-todo-line-regexp)
  2841. (defvar org-complex-heading-regexp nil
  2842. "Matches a headline and puts everything into groups:
  2843. group 1: the stars
  2844. group 2: The todo keyword, maybe
  2845. group 3: Priority cookie
  2846. group 4: True headline
  2847. group 5: Tags")
  2848. (make-variable-buffer-local 'org-complex-heading-regexp)
  2849. (defvar org-todo-line-tags-regexp nil
  2850. "Matches a headline and puts TODO state into group 2 if present.
  2851. Also put tags into group 4 if tags are present.")
  2852. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2853. (defvar org-nl-done-regexp nil
  2854. "Matches newline followed by a headline with the DONE keyword.")
  2855. (make-variable-buffer-local 'org-nl-done-regexp)
  2856. (defvar org-looking-at-done-regexp nil
  2857. "Matches the DONE keyword a point.")
  2858. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2859. (defvar org-ds-keyword-length 12
  2860. "Maximum length of the Deadline and SCHEDULED keywords.")
  2861. (make-variable-buffer-local 'org-ds-keyword-length)
  2862. (defvar org-deadline-regexp nil
  2863. "Matches the DEADLINE keyword.")
  2864. (make-variable-buffer-local 'org-deadline-regexp)
  2865. (defvar org-deadline-time-regexp nil
  2866. "Matches the DEADLINE keyword together with a time stamp.")
  2867. (make-variable-buffer-local 'org-deadline-time-regexp)
  2868. (defvar org-deadline-line-regexp nil
  2869. "Matches the DEADLINE keyword and the rest of the line.")
  2870. (make-variable-buffer-local 'org-deadline-line-regexp)
  2871. (defvar org-scheduled-regexp nil
  2872. "Matches the SCHEDULED keyword.")
  2873. (make-variable-buffer-local 'org-scheduled-regexp)
  2874. (defvar org-scheduled-time-regexp nil
  2875. "Matches the SCHEDULED keyword together with a time stamp.")
  2876. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2877. (defvar org-closed-time-regexp nil
  2878. "Matches the CLOSED keyword together with a time stamp.")
  2879. (make-variable-buffer-local 'org-closed-time-regexp)
  2880. (defvar org-keyword-time-regexp nil
  2881. "Matches any of the 4 keywords, together with the time stamp.")
  2882. (make-variable-buffer-local 'org-keyword-time-regexp)
  2883. (defvar org-keyword-time-not-clock-regexp nil
  2884. "Matches any of the 3 keywords, together with the time stamp.")
  2885. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2886. (defvar org-maybe-keyword-time-regexp nil
  2887. "Matches a timestamp, possibly preceeded by a keyword.")
  2888. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2889. (defvar org-planning-or-clock-line-re nil
  2890. "Matches a line with planning or clock info.")
  2891. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2892. (defconst org-plain-time-of-day-regexp
  2893. (concat
  2894. "\\(\\<[012]?[0-9]"
  2895. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2896. "\\(--?"
  2897. "\\(\\<[012]?[0-9]"
  2898. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2899. "\\)?")
  2900. "Regular expression to match a plain time or time range.
  2901. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2902. groups carry important information:
  2903. 0 the full match
  2904. 1 the first time, range or not
  2905. 8 the second time, if it is a range.")
  2906. (defconst org-plain-time-extension-regexp
  2907. (concat
  2908. "\\(\\<[012]?[0-9]"
  2909. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2910. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2911. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2912. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2913. groups carry important information:
  2914. 0 the full match
  2915. 7 hours of duration
  2916. 9 minutes of duration")
  2917. (defconst org-stamp-time-of-day-regexp
  2918. (concat
  2919. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2920. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2921. "\\(--?"
  2922. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2923. "Regular expression to match a timestamp time or time range.
  2924. After a match, the following groups carry important information:
  2925. 0 the full match
  2926. 1 date plus weekday, for backreferencing to make sure both times on same day
  2927. 2 the first time, range or not
  2928. 4 the second time, if it is a range.")
  2929. (defconst org-startup-options
  2930. '(("fold" org-startup-folded t)
  2931. ("overview" org-startup-folded t)
  2932. ("nofold" org-startup-folded nil)
  2933. ("showall" org-startup-folded nil)
  2934. ("content" org-startup-folded content)
  2935. ("hidestars" org-hide-leading-stars t)
  2936. ("showstars" org-hide-leading-stars nil)
  2937. ("odd" org-odd-levels-only t)
  2938. ("oddeven" org-odd-levels-only nil)
  2939. ("align" org-startup-align-all-tables t)
  2940. ("noalign" org-startup-align-all-tables nil)
  2941. ("customtime" org-display-custom-times t)
  2942. ("logdone" org-log-done time)
  2943. ("lognotedone" org-log-done note)
  2944. ("nologdone" org-log-done nil)
  2945. ("lognoteclock-out" org-log-note-clock-out t)
  2946. ("nolognoteclock-out" org-log-note-clock-out nil)
  2947. ("logrepeat" org-log-repeat state)
  2948. ("lognoterepeat" org-log-repeat note)
  2949. ("nologrepeat" org-log-repeat nil)
  2950. ("fninline" org-footnote-define-inline t)
  2951. ("nofninline" org-footnote-define-inline nil)
  2952. ("fnlocal" org-footnote-section nil)
  2953. ("fnauto" org-footnote-auto-label t)
  2954. ("fnprompt" org-footnote-auto-label nil)
  2955. ("fnconfirm" org-footnote-auto-label confirm)
  2956. ("fnplain" org-footnote-auto-label plain)
  2957. ("constcgs" constants-unit-system cgs)
  2958. ("constSI" constants-unit-system SI)
  2959. ("noptag" org-tag-persistent-alist nil))
  2960. "Variable associated with STARTUP options for org-mode.
  2961. Each element is a list of three items: The startup options as written
  2962. in the #+STARTUP line, the corresponding variable, and the value to
  2963. set this variable to if the option is found. An optional forth element PUSH
  2964. means to push this value onto the list in the variable.")
  2965. (defun org-set-regexps-and-options ()
  2966. "Precompute regular expressions for current buffer."
  2967. (when (org-mode-p)
  2968. (org-set-local 'org-todo-kwd-alist nil)
  2969. (org-set-local 'org-todo-key-alist nil)
  2970. (org-set-local 'org-todo-key-trigger nil)
  2971. (org-set-local 'org-todo-keywords-1 nil)
  2972. (org-set-local 'org-done-keywords nil)
  2973. (org-set-local 'org-todo-heads nil)
  2974. (org-set-local 'org-todo-sets nil)
  2975. (org-set-local 'org-todo-log-states nil)
  2976. (org-set-local 'org-file-properties nil)
  2977. (org-set-local 'org-file-tags nil)
  2978. (let ((re (org-make-options-regexp
  2979. '("CATEGORY" "TODO" "COLUMNS"
  2980. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2981. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2982. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2983. (splitre "[ \t]+")
  2984. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2985. tail sep kws1 prio props ftags drawers
  2986. ext-setup-or-nil setup-contents (start 0))
  2987. (save-excursion
  2988. (save-restriction
  2989. (widen)
  2990. (goto-char (point-min))
  2991. (while (or (and ext-setup-or-nil
  2992. (string-match re ext-setup-or-nil start)
  2993. (setq start (match-end 0)))
  2994. (and (setq ext-setup-or-nil nil start 0)
  2995. (re-search-forward re nil t)))
  2996. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2997. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2998. (cond
  2999. ((equal key "CATEGORY")
  3000. (if (string-match "[ \t]+$" value)
  3001. (setq value (replace-match "" t t value)))
  3002. (setq cat value))
  3003. ((member key '("SEQ_TODO" "TODO"))
  3004. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3005. ((equal key "TYP_TODO")
  3006. (push (cons 'type (org-split-string value splitre)) kwds))
  3007. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3008. ;; general TODO-like setup
  3009. (push (cons (intern (downcase (match-string 1 key)))
  3010. (org-split-string value splitre)) kwds))
  3011. ((equal key "TAGS")
  3012. (setq tags (append tags (if tags '("\\n") nil)
  3013. (org-split-string value splitre))))
  3014. ((equal key "COLUMNS")
  3015. (org-set-local 'org-columns-default-format value))
  3016. ((equal key "LINK")
  3017. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3018. (push (cons (match-string 1 value)
  3019. (org-trim (match-string 2 value)))
  3020. links)))
  3021. ((equal key "PRIORITIES")
  3022. (setq prio (org-split-string value " +")))
  3023. ((equal key "PROPERTY")
  3024. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3025. (push (cons (match-string 1 value) (match-string 2 value))
  3026. props)))
  3027. ((equal key "FILETAGS")
  3028. (when (string-match "\\S-" value)
  3029. (setq ftags
  3030. (append
  3031. ftags
  3032. (apply 'append
  3033. (mapcar (lambda (x) (org-split-string x ":"))
  3034. (org-split-string value)))))))
  3035. ((equal key "DRAWERS")
  3036. (setq drawers (org-split-string value splitre)))
  3037. ((equal key "CONSTANTS")
  3038. (setq const (append const (org-split-string value splitre))))
  3039. ((equal key "STARTUP")
  3040. (let ((opts (org-split-string value splitre))
  3041. l var val)
  3042. (while (setq l (pop opts))
  3043. (when (setq l (assoc l org-startup-options))
  3044. (setq var (nth 1 l) val (nth 2 l))
  3045. (if (not (nth 3 l))
  3046. (set (make-local-variable var) val)
  3047. (if (not (listp (symbol-value var)))
  3048. (set (make-local-variable var) nil))
  3049. (set (make-local-variable var) (symbol-value var))
  3050. (add-to-list var val))))))
  3051. ((equal key "ARCHIVE")
  3052. (string-match " *$" value)
  3053. (setq arch (replace-match "" t t value))
  3054. (remove-text-properties 0 (length arch)
  3055. '(face t fontified t) arch))
  3056. ((equal key "SETUPFILE")
  3057. (setq setup-contents (org-file-contents
  3058. (expand-file-name
  3059. (org-remove-double-quotes value))
  3060. 'noerror))
  3061. (if (not ext-setup-or-nil)
  3062. (setq ext-setup-or-nil setup-contents start 0)
  3063. (setq ext-setup-or-nil
  3064. (concat (substring ext-setup-or-nil 0 start)
  3065. "\n" setup-contents "\n"
  3066. (substring ext-setup-or-nil start)))))
  3067. ))))
  3068. (when cat
  3069. (org-set-local 'org-category (intern cat))
  3070. (push (cons "CATEGORY" cat) props))
  3071. (when prio
  3072. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3073. (setq prio (mapcar 'string-to-char prio))
  3074. (org-set-local 'org-highest-priority (nth 0 prio))
  3075. (org-set-local 'org-lowest-priority (nth 1 prio))
  3076. (org-set-local 'org-default-priority (nth 2 prio)))
  3077. (and props (org-set-local 'org-file-properties (nreverse props)))
  3078. (and ftags (org-set-local 'org-file-tags
  3079. (mapcar 'org-add-prop-inherited ftags)))
  3080. (and drawers (org-set-local 'org-drawers drawers))
  3081. (and arch (org-set-local 'org-archive-location arch))
  3082. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3083. ;; Process the TODO keywords
  3084. (unless kwds
  3085. ;; Use the global values as if they had been given locally.
  3086. (setq kwds (default-value 'org-todo-keywords))
  3087. (if (stringp (car kwds))
  3088. (setq kwds (list (cons org-todo-interpretation
  3089. (default-value 'org-todo-keywords)))))
  3090. (setq kwds (reverse kwds)))
  3091. (setq kwds (nreverse kwds))
  3092. (let (inter kws kw)
  3093. (while (setq kws (pop kwds))
  3094. (let ((kws (or
  3095. (run-hook-with-args-until-success
  3096. 'org-todo-setup-filter-hook kws)
  3097. kws)))
  3098. (setq inter (pop kws) sep (member "|" kws)
  3099. kws0 (delete "|" (copy-sequence kws))
  3100. kwsa nil
  3101. kws1 (mapcar
  3102. (lambda (x)
  3103. ;; 1 2
  3104. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3105. (progn
  3106. (setq kw (match-string 1 x)
  3107. key (and (match-end 2) (match-string 2 x))
  3108. log (org-extract-log-state-settings x))
  3109. (push (cons kw (and key (string-to-char key))) kwsa)
  3110. (and log (push log org-todo-log-states))
  3111. kw)
  3112. (error "Invalid TODO keyword %s" x)))
  3113. kws0)
  3114. kwsa (if kwsa (append '((:startgroup))
  3115. (nreverse kwsa)
  3116. '((:endgroup))))
  3117. hw (car kws1)
  3118. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3119. tail (list inter hw (car dws) (org-last dws))))
  3120. (add-to-list 'org-todo-heads hw 'append)
  3121. (push kws1 org-todo-sets)
  3122. (setq org-done-keywords (append org-done-keywords dws nil))
  3123. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3124. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3125. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3126. (setq org-todo-sets (nreverse org-todo-sets)
  3127. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3128. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3129. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3130. ;; Process the constants
  3131. (when const
  3132. (let (e cst)
  3133. (while (setq e (pop const))
  3134. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3135. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3136. (setq org-table-formula-constants-local cst)))
  3137. ;; Process the tags.
  3138. (when tags
  3139. (let (e tgs)
  3140. (while (setq e (pop tags))
  3141. (cond
  3142. ((equal e "{") (push '(:startgroup) tgs))
  3143. ((equal e "}") (push '(:endgroup) tgs))
  3144. ((equal e "\\n") (push '(:newline) tgs))
  3145. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3146. (push (cons (match-string 1 e)
  3147. (string-to-char (match-string 2 e)))
  3148. tgs))
  3149. (t (push (list e) tgs))))
  3150. (org-set-local 'org-tag-alist nil)
  3151. (while (setq e (pop tgs))
  3152. (or (and (stringp (car e))
  3153. (assoc (car e) org-tag-alist))
  3154. (push e org-tag-alist)))))
  3155. ;; Compute the regular expressions and other local variables
  3156. (if (not org-done-keywords)
  3157. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3158. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3159. (length org-scheduled-string)
  3160. (length org-clock-string)
  3161. (length org-closed-string)))
  3162. org-drawer-regexp
  3163. (concat "^[ \t]*:\\("
  3164. (mapconcat 'regexp-quote org-drawers "\\|")
  3165. "\\):[ \t]*$")
  3166. org-not-done-keywords
  3167. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3168. org-todo-regexp
  3169. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3170. "\\|") "\\)\\>")
  3171. org-not-done-regexp
  3172. (concat "\\<\\("
  3173. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3174. "\\)\\>")
  3175. org-not-done-heading-regexp
  3176. (concat "^\\(\\*+\\)[ \t]+\\("
  3177. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3178. "\\)\\>")
  3179. org-todo-line-regexp
  3180. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3181. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3182. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3183. org-complex-heading-regexp
  3184. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3185. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3186. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3187. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3188. org-nl-done-regexp
  3189. (concat "\n\\*+[ \t]+"
  3190. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3191. "\\)" "\\>")
  3192. org-todo-line-tags-regexp
  3193. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3194. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3195. (org-re
  3196. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3197. org-looking-at-done-regexp
  3198. (concat "^" "\\(?:"
  3199. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3200. "\\>")
  3201. org-deadline-regexp (concat "\\<" org-deadline-string)
  3202. org-deadline-time-regexp
  3203. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3204. org-deadline-line-regexp
  3205. (concat "\\<\\(" org-deadline-string "\\).*")
  3206. org-scheduled-regexp
  3207. (concat "\\<" org-scheduled-string)
  3208. org-scheduled-time-regexp
  3209. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3210. org-closed-time-regexp
  3211. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3212. org-keyword-time-regexp
  3213. (concat "\\<\\(" org-scheduled-string
  3214. "\\|" org-deadline-string
  3215. "\\|" org-closed-string
  3216. "\\|" org-clock-string "\\)"
  3217. " *[[<]\\([^]>]+\\)[]>]")
  3218. org-keyword-time-not-clock-regexp
  3219. (concat "\\<\\(" org-scheduled-string
  3220. "\\|" org-deadline-string
  3221. "\\|" org-closed-string
  3222. "\\)"
  3223. " *[[<]\\([^]>]+\\)[]>]")
  3224. org-maybe-keyword-time-regexp
  3225. (concat "\\(\\<\\(" org-scheduled-string
  3226. "\\|" org-deadline-string
  3227. "\\|" org-closed-string
  3228. "\\|" org-clock-string "\\)\\)?"
  3229. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3230. org-planning-or-clock-line-re
  3231. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3232. "\\|" org-deadline-string
  3233. "\\|" org-closed-string "\\|" org-clock-string
  3234. "\\)\\>\\)")
  3235. )
  3236. (org-compute-latex-and-specials-regexp)
  3237. (org-set-font-lock-defaults))))
  3238. (defun org-file-contents (file &optional noerror)
  3239. "Return the contents of FILE, as a string."
  3240. (if (or (not file)
  3241. (not (file-readable-p file)))
  3242. (if noerror
  3243. (progn
  3244. (message "Cannot read file %s" file)
  3245. (ding) (sit-for 2)
  3246. "")
  3247. (error "Cannot read file %s" file))
  3248. (with-temp-buffer
  3249. (insert-file-contents file)
  3250. (buffer-string))))
  3251. (defun org-extract-log-state-settings (x)
  3252. "Extract the log state setting from a TODO keyword string.
  3253. This will extract info from a string like \"WAIT(w@/!)\"."
  3254. (let (kw key log1 log2)
  3255. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3256. (setq kw (match-string 1 x)
  3257. key (and (match-end 2) (match-string 2 x))
  3258. log1 (and (match-end 3) (match-string 3 x))
  3259. log2 (and (match-end 4) (match-string 4 x)))
  3260. (and (or log1 log2)
  3261. (list kw
  3262. (and log1 (if (equal log1 "!") 'time 'note))
  3263. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3264. (defun org-remove-keyword-keys (list)
  3265. "Remove a pair of parenthesis at the end of each string in LIST."
  3266. (mapcar (lambda (x)
  3267. (if (string-match "(.*)$" x)
  3268. (substring x 0 (match-beginning 0))
  3269. x))
  3270. list))
  3271. ;; FIXME: this could be done much better, using second characters etc.
  3272. (defun org-assign-fast-keys (alist)
  3273. "Assign fast keys to a keyword-key alist.
  3274. Respect keys that are already there."
  3275. (let (new e k c c1 c2 (char ?a))
  3276. (while (setq e (pop alist))
  3277. (cond
  3278. ((equal e '(:startgroup)) (push e new))
  3279. ((equal e '(:endgroup)) (push e new))
  3280. ((equal e '(:newline)) (push e new))
  3281. (t
  3282. (setq k (car e) c2 nil)
  3283. (if (cdr e)
  3284. (setq c (cdr e))
  3285. ;; automatically assign a character.
  3286. (setq c1 (string-to-char
  3287. (downcase (substring
  3288. k (if (= (string-to-char k) ?@) 1 0)))))
  3289. (if (or (rassoc c1 new) (rassoc c1 alist))
  3290. (while (or (rassoc char new) (rassoc char alist))
  3291. (setq char (1+ char)))
  3292. (setq c2 c1))
  3293. (setq c (or c2 char)))
  3294. (push (cons k c) new))))
  3295. (nreverse new)))
  3296. ;;; Some variables used in various places
  3297. (defvar org-window-configuration nil
  3298. "Used in various places to store a window configuration.")
  3299. (defvar org-finish-function nil
  3300. "Function to be called when `C-c C-c' is used.
  3301. This is for getting out of special buffers like remember.")
  3302. ;; FIXME: Occasionally check by commenting these, to make sure
  3303. ;; no other functions uses these, forgetting to let-bind them.
  3304. (defvar entry)
  3305. (defvar state)
  3306. (defvar last-state)
  3307. (defvar date)
  3308. (defvar description)
  3309. ;; Defined somewhere in this file, but used before definition.
  3310. (defvar org-html-entities)
  3311. (defvar org-struct-menu)
  3312. (defvar org-org-menu)
  3313. (defvar org-tbl-menu)
  3314. (defvar org-agenda-keymap)
  3315. ;;;; Define the Org-mode
  3316. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3317. (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."))
  3318. ;; We use a before-change function to check if a table might need
  3319. ;; an update.
  3320. (defvar org-table-may-need-update t
  3321. "Indicates that a table might need an update.
  3322. This variable is set by `org-before-change-function'.
  3323. `org-table-align' sets it back to nil.")
  3324. (defun org-before-change-function (beg end)
  3325. "Every change indicates that a table might need an update."
  3326. (setq org-table-may-need-update t))
  3327. (defvar org-mode-map)
  3328. (defvar org-mode-hook nil
  3329. "Mode hook for Org-mode, run after the mode was turned on.")
  3330. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3331. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3332. (defvar org-table-buffer-is-an nil)
  3333. (defconst org-outline-regexp "\\*+ ")
  3334. ;;;###autoload
  3335. (define-derived-mode org-mode outline-mode "Org"
  3336. "Outline-based notes management and organizer, alias
  3337. \"Carsten's outline-mode for keeping track of everything.\"
  3338. Org-mode develops organizational tasks around a NOTES file which
  3339. contains information about projects as plain text. Org-mode is
  3340. implemented on top of outline-mode, which is ideal to keep the content
  3341. of large files well structured. It supports ToDo items, deadlines and
  3342. time stamps, which magically appear in the diary listing of the Emacs
  3343. calendar. Tables are easily created with a built-in table editor.
  3344. Plain text URL-like links connect to websites, emails (VM), Usenet
  3345. messages (Gnus), BBDB entries, and any files related to the project.
  3346. For printing and sharing of notes, an Org-mode file (or a part of it)
  3347. can be exported as a structured ASCII or HTML file.
  3348. The following commands are available:
  3349. \\{org-mode-map}"
  3350. ;; Get rid of Outline menus, they are not needed
  3351. ;; Need to do this here because define-derived-mode sets up
  3352. ;; the keymap so late. Still, it is a waste to call this each time
  3353. ;; we switch another buffer into org-mode.
  3354. (if (featurep 'xemacs)
  3355. (when (boundp 'outline-mode-menu-heading)
  3356. ;; Assume this is Greg's port, it used easymenu
  3357. (easy-menu-remove outline-mode-menu-heading)
  3358. (easy-menu-remove outline-mode-menu-show)
  3359. (easy-menu-remove outline-mode-menu-hide))
  3360. (define-key org-mode-map [menu-bar headings] 'undefined)
  3361. (define-key org-mode-map [menu-bar hide] 'undefined)
  3362. (define-key org-mode-map [menu-bar show] 'undefined))
  3363. (org-load-modules-maybe)
  3364. (easy-menu-add org-org-menu)
  3365. (easy-menu-add org-tbl-menu)
  3366. (org-install-agenda-files-menu)
  3367. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3368. (org-add-to-invisibility-spec '(org-cwidth))
  3369. (when (featurep 'xemacs)
  3370. (org-set-local 'line-move-ignore-invisible t))
  3371. (org-set-local 'outline-regexp org-outline-regexp)
  3372. (org-set-local 'outline-level 'org-outline-level)
  3373. (when (and org-ellipsis
  3374. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3375. (fboundp 'make-glyph-code))
  3376. (unless org-display-table
  3377. (setq org-display-table (make-display-table)))
  3378. (set-display-table-slot
  3379. org-display-table 4
  3380. (vconcat (mapcar
  3381. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3382. org-ellipsis)))
  3383. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3384. (setq buffer-display-table org-display-table))
  3385. (org-set-regexps-and-options)
  3386. (when (and org-tag-faces (not org-tags-special-faces-re))
  3387. ;; tag faces set outside customize.... force initialization.
  3388. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3389. ;; Calc embedded
  3390. (org-set-local 'calc-embedded-open-mode "# ")
  3391. (modify-syntax-entry ?# "<")
  3392. (modify-syntax-entry ?@ "w")
  3393. (if org-startup-truncated (setq truncate-lines t))
  3394. (org-set-local 'font-lock-unfontify-region-function
  3395. 'org-unfontify-region)
  3396. ;; Activate before-change-function
  3397. (org-set-local 'org-table-may-need-update t)
  3398. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3399. 'local)
  3400. ;; Check for running clock before killing a buffer
  3401. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3402. ;; Paragraphs and auto-filling
  3403. (org-set-autofill-regexps)
  3404. (setq indent-line-function 'org-indent-line-function)
  3405. (org-update-radio-target-regexp)
  3406. ;; Make sure dependence stuff works reliably, even for users who set it
  3407. ;; too late :-(
  3408. (if org-enforce-todo-dependencies
  3409. (add-hook 'org-blocker-hook
  3410. 'org-block-todo-from-children-or-siblings-or-parent)
  3411. (remove-hook 'org-blocker-hook
  3412. 'org-block-todo-from-children-or-siblings-or-parent))
  3413. (if org-enforce-todo-checkbox-dependencies
  3414. (add-hook 'org-blocker-hook
  3415. 'org-block-todo-from-checkboxes)
  3416. (remove-hook 'org-blocker-hook
  3417. 'org-block-todo-from-checkboxes))
  3418. ;; Comment characters
  3419. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3420. (org-set-local 'comment-padding " ")
  3421. ;; Align options lines
  3422. (org-set-local
  3423. 'align-mode-rules-list
  3424. '((org-in-buffer-settings
  3425. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3426. (modes . '(org-mode)))))
  3427. ;; Imenu
  3428. (org-set-local 'imenu-create-index-function
  3429. 'org-imenu-get-tree)
  3430. ;; Make isearch reveal context
  3431. (if (or (featurep 'xemacs)
  3432. (not (boundp 'outline-isearch-open-invisible-function)))
  3433. ;; Emacs 21 and XEmacs make use of the hook
  3434. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3435. ;; Emacs 22 deals with this through a special variable
  3436. (org-set-local 'outline-isearch-open-invisible-function
  3437. (lambda (&rest ignore) (org-show-context 'isearch))))
  3438. ;; If empty file that did not turn on org-mode automatically, make it to.
  3439. (if (and org-insert-mode-line-in-empty-file
  3440. (interactive-p)
  3441. (= (point-min) (point-max)))
  3442. (insert "# -*- mode: org -*-\n\n"))
  3443. (unless org-inhibit-startup
  3444. (when org-startup-align-all-tables
  3445. (let ((bmp (buffer-modified-p)))
  3446. (org-table-map-tables 'org-table-align)
  3447. (set-buffer-modified-p bmp)))
  3448. (org-set-startup-visibility)))
  3449. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3450. (defun org-current-time ()
  3451. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3452. (if (> (car org-time-stamp-rounding-minutes) 1)
  3453. (let ((r (car org-time-stamp-rounding-minutes))
  3454. (time (decode-time)))
  3455. (apply 'encode-time
  3456. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3457. (nthcdr 2 time))))
  3458. (current-time)))
  3459. ;;;; Font-Lock stuff, including the activators
  3460. (defvar org-mouse-map (make-sparse-keymap))
  3461. (org-defkey org-mouse-map
  3462. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3463. (org-defkey org-mouse-map
  3464. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3465. (when org-mouse-1-follows-link
  3466. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3467. (when org-tab-follows-link
  3468. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3469. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3470. (require 'font-lock)
  3471. (defconst org-non-link-chars "]\t\n\r<>")
  3472. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3473. "shell" "elisp"))
  3474. (defvar org-link-types-re nil
  3475. "Matches a link that has a url-like prefix like \"http:\"")
  3476. (defvar org-link-re-with-space nil
  3477. "Matches a link with spaces, optional angular brackets around it.")
  3478. (defvar org-link-re-with-space2 nil
  3479. "Matches a link with spaces, optional angular brackets around it.")
  3480. (defvar org-link-re-with-space3 nil
  3481. "Matches a link with spaces, only for internal part in bracket links.")
  3482. (defvar org-angle-link-re nil
  3483. "Matches link with angular brackets, spaces are allowed.")
  3484. (defvar org-plain-link-re nil
  3485. "Matches plain link, without spaces.")
  3486. (defvar org-bracket-link-regexp nil
  3487. "Matches a link in double brackets.")
  3488. (defvar org-bracket-link-analytic-regexp nil
  3489. "Regular expression used to analyze links.
  3490. Here is what the match groups contain after a match:
  3491. 1: http:
  3492. 2: http
  3493. 3: path
  3494. 4: [desc]
  3495. 5: desc")
  3496. (defvar org-bracket-link-analytic-regexp++ nil
  3497. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3498. (defvar org-any-link-re nil
  3499. "Regular expression matching any link.")
  3500. (defun org-make-link-regexps ()
  3501. "Update the link regular expressions.
  3502. This should be called after the variable `org-link-types' has changed."
  3503. (setq org-link-types-re
  3504. (concat
  3505. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3506. org-link-re-with-space
  3507. (concat
  3508. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3509. "\\([^" org-non-link-chars " ]"
  3510. "[^" org-non-link-chars "]*"
  3511. "[^" org-non-link-chars " ]\\)>?")
  3512. org-link-re-with-space2
  3513. (concat
  3514. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3515. "\\([^" org-non-link-chars " ]"
  3516. "[^\t\n\r]*"
  3517. "[^" org-non-link-chars " ]\\)>?")
  3518. org-link-re-with-space3
  3519. (concat
  3520. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3521. "\\([^" org-non-link-chars " ]"
  3522. "[^\t\n\r]*\\)")
  3523. org-angle-link-re
  3524. (concat
  3525. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3526. "\\([^" org-non-link-chars " ]"
  3527. "[^" org-non-link-chars "]*"
  3528. "\\)>")
  3529. org-plain-link-re
  3530. (concat
  3531. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3532. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3533. org-bracket-link-regexp
  3534. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3535. org-bracket-link-analytic-regexp
  3536. (concat
  3537. "\\[\\["
  3538. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3539. "\\([^]]+\\)"
  3540. "\\]"
  3541. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3542. "\\]")
  3543. org-bracket-link-analytic-regexp++
  3544. (concat
  3545. "\\[\\["
  3546. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3547. "\\([^]]+\\)"
  3548. "\\]"
  3549. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3550. "\\]")
  3551. org-any-link-re
  3552. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3553. org-angle-link-re "\\)\\|\\("
  3554. org-plain-link-re "\\)")))
  3555. (org-make-link-regexps)
  3556. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3557. "Regular expression for fast time stamp matching.")
  3558. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3559. "Regular expression for fast time stamp matching.")
  3560. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3561. "Regular expression matching time strings for analysis.
  3562. This one does not require the space after the date, so it can be used
  3563. on a string that terminates immediately after the date.")
  3564. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3565. "Regular expression matching time strings for analysis.")
  3566. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3567. "Regular expression matching time stamps, with groups.")
  3568. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3569. "Regular expression matching time stamps (also [..]), with groups.")
  3570. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3571. "Regular expression matching a time stamp range.")
  3572. (defconst org-tr-regexp-both
  3573. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3574. "Regular expression matching a time stamp range.")
  3575. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3576. org-ts-regexp "\\)?")
  3577. "Regular expression matching a time stamp or time stamp range.")
  3578. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3579. org-ts-regexp-both "\\)?")
  3580. "Regular expression matching a time stamp or time stamp range.
  3581. The time stamps may be either active or inactive.")
  3582. (defvar org-emph-face nil)
  3583. (defun org-do-emphasis-faces (limit)
  3584. "Run through the buffer and add overlays to links."
  3585. (let (rtn)
  3586. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3587. (if (not (= (char-after (match-beginning 3))
  3588. (char-after (match-beginning 4))))
  3589. (progn
  3590. (setq rtn t)
  3591. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3592. 'face
  3593. (nth 1 (assoc (match-string 3)
  3594. org-emphasis-alist)))
  3595. (add-text-properties (match-beginning 2) (match-end 2)
  3596. '(font-lock-multiline t))
  3597. (when org-hide-emphasis-markers
  3598. (add-text-properties (match-end 4) (match-beginning 5)
  3599. '(invisible org-link))
  3600. (add-text-properties (match-beginning 3) (match-end 3)
  3601. '(invisible org-link)))))
  3602. (backward-char 1))
  3603. rtn))
  3604. (defun org-emphasize (&optional char)
  3605. "Insert or change an emphasis, i.e. a font like bold or italic.
  3606. If there is an active region, change that region to a new emphasis.
  3607. If there is no region, just insert the marker characters and position
  3608. the cursor between them.
  3609. CHAR should be either the marker character, or the first character of the
  3610. HTML tag associated with that emphasis. If CHAR is a space, the means
  3611. to remove the emphasis of the selected region.
  3612. If char is not given (for example in an interactive call) it
  3613. will be prompted for."
  3614. (interactive)
  3615. (let ((eal org-emphasis-alist) e det
  3616. (erc org-emphasis-regexp-components)
  3617. (prompt "")
  3618. (string "") beg end move tag c s)
  3619. (if (org-region-active-p)
  3620. (setq beg (region-beginning) end (region-end)
  3621. string (buffer-substring beg end))
  3622. (setq move t))
  3623. (while (setq e (pop eal))
  3624. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3625. c (aref tag 0))
  3626. (push (cons c (string-to-char (car e))) det)
  3627. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3628. (substring tag 1)))))
  3629. (setq det (nreverse det))
  3630. (unless char
  3631. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3632. (setq char (read-char-exclusive)))
  3633. (setq char (or (cdr (assoc char det)) char))
  3634. (if (equal char ?\ )
  3635. (setq s "" move nil)
  3636. (unless (assoc (char-to-string char) org-emphasis-alist)
  3637. (error "No such emphasis marker: \"%c\"" char))
  3638. (setq s (char-to-string char)))
  3639. (while (and (> (length string) 1)
  3640. (equal (substring string 0 1) (substring string -1))
  3641. (assoc (substring string 0 1) org-emphasis-alist))
  3642. (setq string (substring string 1 -1)))
  3643. (setq string (concat s string s))
  3644. (if beg (delete-region beg end))
  3645. (unless (or (bolp)
  3646. (string-match (concat "[" (nth 0 erc) "\n]")
  3647. (char-to-string (char-before (point)))))
  3648. (insert " "))
  3649. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3650. (char-to-string (char-after (point))))
  3651. (insert " ") (backward-char 1))
  3652. (insert string)
  3653. (and move (backward-char 1))))
  3654. (defconst org-nonsticky-props
  3655. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3656. (defsubst org-rear-nonsticky-at (pos)
  3657. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3658. (defun org-activate-plain-links (limit)
  3659. "Run through the buffer and add overlays to links."
  3660. (catch 'exit
  3661. (let (f)
  3662. (while (re-search-forward org-plain-link-re limit t)
  3663. (setq f (get-text-property (match-beginning 0) 'face))
  3664. (if (or (eq f 'org-tag)
  3665. (and (listp f) (memq 'org-tag f)))
  3666. nil
  3667. (add-text-properties (match-beginning 0) (match-end 0)
  3668. (list 'mouse-face 'highlight
  3669. 'keymap org-mouse-map))
  3670. (org-rear-nonsticky-at (match-end 0))
  3671. (throw 'exit t))))))
  3672. (defun org-activate-code (limit)
  3673. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3674. (progn
  3675. (remove-text-properties (match-beginning 0) (match-end 0)
  3676. '(display t invisible t intangible t))
  3677. t)))
  3678. (defun org-activate-angle-links (limit)
  3679. "Run through the buffer and add overlays to links."
  3680. (if (re-search-forward org-angle-link-re limit t)
  3681. (progn
  3682. (add-text-properties (match-beginning 0) (match-end 0)
  3683. (list 'mouse-face 'highlight
  3684. 'keymap org-mouse-map))
  3685. (org-rear-nonsticky-at (match-end 0))
  3686. t)))
  3687. (defun org-activate-footnote-links (limit)
  3688. "Run through the buffer and add overlays to links."
  3689. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3690. limit t)
  3691. (progn
  3692. (add-text-properties (match-beginning 2) (match-end 2)
  3693. (list 'mouse-face 'highlight
  3694. 'keymap org-mouse-map
  3695. 'help-echo
  3696. (if (= (point-at-bol) (match-beginning 2))
  3697. "Footnote definition"
  3698. "Footnote reference")
  3699. ))
  3700. (org-rear-nonsticky-at (match-end 2))
  3701. t)))
  3702. (defun org-activate-bracket-links (limit)
  3703. "Run through the buffer and add overlays to bracketed links."
  3704. (if (re-search-forward org-bracket-link-regexp limit t)
  3705. (let* ((help (concat "LINK: "
  3706. (org-match-string-no-properties 1)))
  3707. ;; FIXME: above we should remove the escapes.
  3708. ;; but that requires another match, protecting match data,
  3709. ;; a lot of overhead for font-lock.
  3710. (ip (org-maybe-intangible
  3711. (list 'invisible 'org-link
  3712. 'keymap org-mouse-map 'mouse-face 'highlight
  3713. 'font-lock-multiline t 'help-echo help)))
  3714. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3715. 'font-lock-multiline t 'help-echo help)))
  3716. ;; We need to remove the invisible property here. Table narrowing
  3717. ;; may have made some of this invisible.
  3718. (remove-text-properties (match-beginning 0) (match-end 0)
  3719. '(invisible nil))
  3720. (if (match-end 3)
  3721. (progn
  3722. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3723. (org-rear-nonsticky-at (match-beginning 3))
  3724. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3725. (org-rear-nonsticky-at (match-end 3))
  3726. (add-text-properties (match-end 3) (match-end 0) ip)
  3727. (org-rear-nonsticky-at (match-end 0)))
  3728. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3729. (org-rear-nonsticky-at (match-beginning 1))
  3730. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3731. (org-rear-nonsticky-at (match-end 1))
  3732. (add-text-properties (match-end 1) (match-end 0) ip)
  3733. (org-rear-nonsticky-at (match-end 0)))
  3734. t)))
  3735. (defun org-activate-dates (limit)
  3736. "Run through the buffer and add overlays to dates."
  3737. (if (re-search-forward org-tsr-regexp-both limit t)
  3738. (progn
  3739. (add-text-properties (match-beginning 0) (match-end 0)
  3740. (list 'mouse-face 'highlight
  3741. 'keymap org-mouse-map))
  3742. (org-rear-nonsticky-at (match-end 0))
  3743. (when org-display-custom-times
  3744. (if (match-end 3)
  3745. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3746. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3747. t)))
  3748. (defvar org-target-link-regexp nil
  3749. "Regular expression matching radio targets in plain text.")
  3750. (make-variable-buffer-local 'org-target-link-regexp)
  3751. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3752. "Regular expression matching a link target.")
  3753. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3754. "Regular expression matching a radio target.")
  3755. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3756. "Regular expression matching any target.")
  3757. (defun org-activate-target-links (limit)
  3758. "Run through the buffer and add overlays to target matches."
  3759. (when org-target-link-regexp
  3760. (let ((case-fold-search t))
  3761. (if (re-search-forward org-target-link-regexp limit t)
  3762. (progn
  3763. (add-text-properties (match-beginning 0) (match-end 0)
  3764. (list 'mouse-face 'highlight
  3765. 'keymap org-mouse-map
  3766. 'help-echo "Radio target link"
  3767. 'org-linked-text t))
  3768. (org-rear-nonsticky-at (match-end 0))
  3769. t)))))
  3770. (defun org-update-radio-target-regexp ()
  3771. "Find all radio targets in this file and update the regular expression."
  3772. (interactive)
  3773. (when (memq 'radio org-activate-links)
  3774. (setq org-target-link-regexp
  3775. (org-make-target-link-regexp (org-all-targets 'radio)))
  3776. (org-restart-font-lock)))
  3777. (defun org-hide-wide-columns (limit)
  3778. (let (s e)
  3779. (setq s (text-property-any (point) (or limit (point-max))
  3780. 'org-cwidth t))
  3781. (when s
  3782. (setq e (next-single-property-change s 'org-cwidth))
  3783. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3784. (goto-char e)
  3785. t)))
  3786. (defvar org-latex-and-specials-regexp nil
  3787. "Regular expression for highlighting export special stuff.")
  3788. (defvar org-match-substring-regexp)
  3789. (defvar org-match-substring-with-braces-regexp)
  3790. (defvar org-export-html-special-string-regexps)
  3791. (defun org-compute-latex-and-specials-regexp ()
  3792. "Compute regular expression for stuff treated specially by exporters."
  3793. (if (not org-highlight-latex-fragments-and-specials)
  3794. (org-set-local 'org-latex-and-specials-regexp nil)
  3795. (require 'org-exp)
  3796. (let*
  3797. ((matchers (plist-get org-format-latex-options :matchers))
  3798. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3799. org-latex-regexps)))
  3800. (options (org-combine-plists (org-default-export-plist)
  3801. (org-infile-export-plist)))
  3802. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3803. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3804. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3805. (org-export-html-expand (plist-get options :expand-quoted-html))
  3806. (org-export-with-special-strings (plist-get options :special-strings))
  3807. (re-sub
  3808. (cond
  3809. ((equal org-export-with-sub-superscripts '{})
  3810. (list org-match-substring-with-braces-regexp))
  3811. (org-export-with-sub-superscripts
  3812. (list org-match-substring-regexp))
  3813. (t nil)))
  3814. (re-latex
  3815. (if org-export-with-LaTeX-fragments
  3816. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3817. (re-macros
  3818. (if org-export-with-TeX-macros
  3819. (list (concat "\\\\"
  3820. (regexp-opt
  3821. (append (mapcar 'car org-html-entities)
  3822. (if (boundp 'org-latex-entities)
  3823. org-latex-entities nil))
  3824. 'words))) ; FIXME
  3825. ))
  3826. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3827. (re-special (if org-export-with-special-strings
  3828. (mapcar (lambda (x) (car x))
  3829. org-export-html-special-string-regexps)))
  3830. (re-rest
  3831. (delq nil
  3832. (list
  3833. (if org-export-html-expand "@<[^>\n]+>")
  3834. ))))
  3835. (org-set-local
  3836. 'org-latex-and-specials-regexp
  3837. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3838. re-rest) "\\|")))))
  3839. (defun org-do-latex-and-special-faces (limit)
  3840. "Run through the buffer and add overlays to links."
  3841. (when org-latex-and-specials-regexp
  3842. (let (rtn d)
  3843. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3844. limit t))
  3845. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3846. 'face))
  3847. '(org-code org-verbatim underline)))
  3848. (progn
  3849. (setq rtn t
  3850. d (cond ((member (char-after (1+ (match-beginning 0)))
  3851. '(?_ ?^)) 1)
  3852. (t 0)))
  3853. (font-lock-prepend-text-property
  3854. (+ d (match-beginning 0)) (match-end 0)
  3855. 'face 'org-latex-and-export-specials)
  3856. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3857. '(font-lock-multiline t)))))
  3858. rtn)))
  3859. (defun org-restart-font-lock ()
  3860. "Restart font-lock-mode, to force refontification."
  3861. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3862. (font-lock-mode -1)
  3863. (font-lock-mode 1)))
  3864. (defun org-all-targets (&optional radio)
  3865. "Return a list of all targets in this file.
  3866. With optional argument RADIO, only find radio targets."
  3867. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3868. rtn)
  3869. (save-excursion
  3870. (goto-char (point-min))
  3871. (while (re-search-forward re nil t)
  3872. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3873. rtn)))
  3874. (defun org-make-target-link-regexp (targets)
  3875. "Make regular expression matching all strings in TARGETS.
  3876. The regular expression finds the targets also if there is a line break
  3877. between words."
  3878. (and targets
  3879. (concat
  3880. "\\<\\("
  3881. (mapconcat
  3882. (lambda (x)
  3883. (while (string-match " +" x)
  3884. (setq x (replace-match "\\s-+" t t x)))
  3885. x)
  3886. targets
  3887. "\\|")
  3888. "\\)\\>")))
  3889. (defun org-activate-tags (limit)
  3890. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3891. (progn
  3892. (add-text-properties (match-beginning 1) (match-end 1)
  3893. (list 'mouse-face 'highlight
  3894. 'keymap org-mouse-map))
  3895. (org-rear-nonsticky-at (match-end 1))
  3896. t)))
  3897. (defun org-outline-level ()
  3898. (save-excursion
  3899. (looking-at outline-regexp)
  3900. (if (match-beginning 1)
  3901. (+ (org-get-string-indentation (match-string 1)) 1000)
  3902. (1- (- (match-end 0) (match-beginning 0))))))
  3903. (defvar org-font-lock-keywords nil)
  3904. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3905. "Regular expression matching a property line.")
  3906. (defvar org-font-lock-hook nil
  3907. "Functions to be called for special font lock stuff.")
  3908. (defun org-font-lock-hook (limit)
  3909. (run-hook-with-args 'org-font-lock-hook limit))
  3910. (defun org-set-font-lock-defaults ()
  3911. (let* ((em org-fontify-emphasized-text)
  3912. (lk org-activate-links)
  3913. (org-font-lock-extra-keywords
  3914. (list
  3915. ;; Call the hook
  3916. '(org-font-lock-hook)
  3917. ;; Headlines
  3918. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3919. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3920. ;; Table lines
  3921. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3922. (1 'org-table t))
  3923. ;; Table internals
  3924. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3925. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3926. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3927. ;; Drawers
  3928. (list org-drawer-regexp '(0 'org-special-keyword t))
  3929. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3930. ;; Properties
  3931. (list org-property-re
  3932. '(1 'org-special-keyword t)
  3933. '(3 'org-property-value t))
  3934. (if org-format-transports-properties-p
  3935. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3936. ;; Links
  3937. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3938. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3939. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3940. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3941. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3942. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3943. (if (memq 'footnote lk) '(org-activate-footnote-links
  3944. (2 'org-footnote t)))
  3945. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3946. '(org-hide-wide-columns (0 nil append))
  3947. ;; TODO lines
  3948. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  3949. '(1 (org-get-todo-face 1) t))
  3950. ;; DONE
  3951. (if org-fontify-done-headline
  3952. (list (concat "^[*]+ +\\<\\("
  3953. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3954. "\\)\\(.*\\)")
  3955. '(2 'org-headline-done t))
  3956. nil)
  3957. ;; Priorities
  3958. '(org-font-lock-add-priority-faces)
  3959. ;; Tags
  3960. '(org-font-lock-add-tag-faces)
  3961. ;; Special keywords
  3962. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3963. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3964. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3965. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3966. ;; Emphasis
  3967. (if em
  3968. (if (featurep 'xemacs)
  3969. '(org-do-emphasis-faces (0 nil append))
  3970. '(org-do-emphasis-faces)))
  3971. ;; Checkboxes
  3972. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3973. 2 'org-checkbox prepend)
  3974. (if org-provide-checkbox-statistics
  3975. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3976. (0 (org-get-checkbox-statistics-face) t)))
  3977. ;; Description list items
  3978. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3979. 2 'bold prepend)
  3980. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3981. '(1 'org-archived prepend))
  3982. ;; Specials
  3983. '(org-do-latex-and-special-faces)
  3984. ;; Code
  3985. '(org-activate-code (1 'org-code t))
  3986. ;; COMMENT
  3987. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3988. "\\|" org-quote-string "\\)\\>")
  3989. '(1 'org-special-keyword t))
  3990. '("^#.*" (0 'font-lock-comment-face t))
  3991. )))
  3992. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3993. ;; Now set the full font-lock-keywords
  3994. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3995. (org-set-local 'font-lock-defaults
  3996. '(org-font-lock-keywords t nil nil backward-paragraph))
  3997. (kill-local-variable 'font-lock-keywords) nil))
  3998. (defvar org-m nil)
  3999. (defvar org-l nil)
  4000. (defvar org-f nil)
  4001. (defun org-get-level-face (n)
  4002. "Get the right face for match N in font-lock matching of headlines."
  4003. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4004. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4005. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4006. (cond
  4007. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4008. ((eq n 2) org-f)
  4009. (t (if org-level-color-stars-only nil org-f))))
  4010. (defun org-get-todo-face (kwd)
  4011. "Get the right face for a TODO keyword KWD.
  4012. If KWD is a number, get the corresponding match group."
  4013. (if (numberp kwd) (setq kwd (match-string kwd)))
  4014. (or (cdr (assoc kwd org-todo-keyword-faces))
  4015. (and (member kwd org-done-keywords) 'org-done)
  4016. 'org-todo))
  4017. (defun org-font-lock-add-tag-faces (limit)
  4018. "Add the special tag faces."
  4019. (when (and org-tag-faces org-tags-special-faces-re)
  4020. (while (re-search-forward org-tags-special-faces-re limit t)
  4021. (add-text-properties (match-beginning 1) (match-end 1)
  4022. (list 'face (org-get-tag-face 1)
  4023. 'font-lock-fontified t))
  4024. (backward-char 1))))
  4025. (defun org-font-lock-add-priority-faces (limit)
  4026. "Add the special priority faces."
  4027. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4028. (add-text-properties
  4029. (match-beginning 0) (match-end 0)
  4030. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4031. org-priority-faces))
  4032. 'org-special-keyword)
  4033. 'font-lock-fontified t))))
  4034. (defun org-get-tag-face (kwd)
  4035. "Get the right face for a TODO keyword KWD.
  4036. If KWD is a number, get the corresponding match group."
  4037. (if (numberp kwd) (setq kwd (match-string kwd)))
  4038. (or (cdr (assoc kwd org-tag-faces))
  4039. 'org-tag))
  4040. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4041. "Remove fontification and activation overlays from links."
  4042. (font-lock-default-unfontify-region beg end)
  4043. (let* ((buffer-undo-list t)
  4044. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4045. (inhibit-modification-hooks t)
  4046. deactivate-mark buffer-file-name buffer-file-truename)
  4047. (remove-text-properties beg end
  4048. '(mouse-face t keymap t org-linked-text t
  4049. invisible t intangible t))))
  4050. ;;;; Visibility cycling, including org-goto and indirect buffer
  4051. ;;; Cycling
  4052. (defvar org-cycle-global-status nil)
  4053. (make-variable-buffer-local 'org-cycle-global-status)
  4054. (defvar org-cycle-subtree-status nil)
  4055. (make-variable-buffer-local 'org-cycle-subtree-status)
  4056. ;;;###autoload
  4057. (defvar org-inlinetask-min-level)
  4058. (defun org-cycle (&optional arg)
  4059. "TAB-action and visibility cycling for Org-mode.
  4060. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4061. is outine visibility cycling, but it also invokes other actions
  4062. in special contexts.
  4063. - When this function is called with a prefix argument, rotate the entire
  4064. buffer through 3 states (global cycling)
  4065. 1. OVERVIEW: Show only top-level headlines.
  4066. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4067. 3. SHOW ALL: Show everything.
  4068. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4069. determined by the variable `org-startup-folded', and by any VISIBILITY
  4070. properties in the buffer.
  4071. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4072. including any drawers.
  4073. - When inside a table, re-align the table and move to the next field.
  4074. - When point is at the beginning of a headline, rotate the subtree started
  4075. by this line through 3 different states (local cycling)
  4076. 1. FOLDED: Only the main headline is shown.
  4077. 2. CHILDREN: The main headline and the direct children are shown.
  4078. From this state, you can move to one of the children
  4079. and zoom in further.
  4080. 3. SUBTREE: Show the entire subtree, including body text.
  4081. - When there is a numeric prefix, go up to a heading with level ARG, do
  4082. a `show-subtree' and return to the previous cursor position. If ARG
  4083. is negative, go up that many levels.
  4084. - When point is not at the beginning of a headline, execute the global
  4085. binding for TAB, which is re-indenting the line. See the option
  4086. `org-cycle-emulate-tab' for details.
  4087. - Special case: if point is at the beginning of the buffer and there is
  4088. no headline in line 1, this function will act as if called with prefix arg.
  4089. But only if also the variable `org-cycle-global-at-bob' is t."
  4090. (interactive "P")
  4091. (org-load-modules-maybe)
  4092. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4093. (let* ((limit-level
  4094. (or org-cycle-max-level
  4095. (and (boundp 'org-inlinetask-min-level)
  4096. org-inlinetask-min-level
  4097. (1- org-inlinetask-min-level))))
  4098. (nstars (and limit-level
  4099. (if org-odd-levels-only
  4100. (and limit-level (1- (* limit-level 2)))
  4101. limit-level)))
  4102. (outline-regexp
  4103. (cond
  4104. ((not (org-mode-p)) outline-regexp)
  4105. (org-cycle-include-plain-lists
  4106. (concat "\\(?:\\*"
  4107. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4108. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4109. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4110. (bob-special (and org-cycle-global-at-bob (bobp)
  4111. (not (looking-at outline-regexp))))
  4112. (org-cycle-hook
  4113. (if bob-special
  4114. (delq 'org-optimize-window-after-visibility-change
  4115. (copy-sequence org-cycle-hook))
  4116. org-cycle-hook))
  4117. (pos (point)))
  4118. (if (or bob-special (equal arg '(4)))
  4119. ;; special case: use global cycling
  4120. (setq arg t))
  4121. (cond
  4122. ((equal arg '(16))
  4123. (org-set-startup-visibility)
  4124. (message "Startup visibility, plus VISIBILITY properties"))
  4125. ((equal arg '(64))
  4126. (show-all)
  4127. (message "Entire buffer visible, including drawers"))
  4128. ((org-at-table-p 'any)
  4129. ;; Enter the table or move to the next field in the table
  4130. (or (org-table-recognize-table.el)
  4131. (progn
  4132. (if arg (org-table-edit-field t)
  4133. (org-table-justify-field-maybe)
  4134. (call-interactively 'org-table-next-field)))))
  4135. ((run-hook-with-args-until-success
  4136. 'org-tab-after-check-for-table-hook))
  4137. ((eq arg t) ;; Global cycling
  4138. (org-cycle-internal-global))
  4139. ((and org-drawers org-drawer-regexp
  4140. (save-excursion
  4141. (beginning-of-line 1)
  4142. (looking-at org-drawer-regexp)))
  4143. ;; Toggle block visibility
  4144. (org-flag-drawer
  4145. (not (get-char-property (match-end 0) 'invisible))))
  4146. ((integerp arg)
  4147. ;; Show-subtree, ARG levels up from here.
  4148. (save-excursion
  4149. (org-back-to-heading)
  4150. (outline-up-heading (if (< arg 0) (- arg)
  4151. (- (funcall outline-level) arg)))
  4152. (org-show-subtree)))
  4153. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4154. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4155. (org-cycle-internal-local))
  4156. ;; TAB emulation and template completion
  4157. (buffer-read-only (org-back-to-heading))
  4158. ((run-hook-with-args-until-success
  4159. 'org-tab-after-check-for-cycling-hook))
  4160. ((org-try-structure-completion))
  4161. ((org-try-cdlatex-tab))
  4162. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4163. (or (not (bolp))
  4164. (not (looking-at outline-regexp))))
  4165. (call-interactively (global-key-binding "\t")))
  4166. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4167. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4168. (or (and (eq org-cycle-emulate-tab 'white)
  4169. (= (match-end 0) (point-at-eol)))
  4170. (and (eq org-cycle-emulate-tab 'whitestart)
  4171. (>= (match-end 0) pos))))
  4172. t
  4173. (eq org-cycle-emulate-tab t))
  4174. (call-interactively (global-key-binding "\t")))
  4175. (t (save-excursion
  4176. (org-back-to-heading)
  4177. (org-cycle)))))))
  4178. (defun org-cycle-internal-global ()
  4179. "Do the global cycling action."
  4180. (cond
  4181. ((and (eq last-command this-command)
  4182. (eq org-cycle-global-status 'overview))
  4183. ;; We just created the overview - now do table of contents
  4184. ;; This can be slow in very large buffers, so indicate action
  4185. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4186. (message "CONTENTS...")
  4187. (org-content)
  4188. (message "CONTENTS...done")
  4189. (setq org-cycle-global-status 'contents)
  4190. (run-hook-with-args 'org-cycle-hook 'contents))
  4191. ((and (eq last-command this-command)
  4192. (eq org-cycle-global-status 'contents))
  4193. ;; We just showed the table of contents - now show everything
  4194. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4195. (show-all)
  4196. (message "SHOW ALL")
  4197. (setq org-cycle-global-status 'all)
  4198. (run-hook-with-args 'org-cycle-hook 'all))
  4199. (t
  4200. ;; Default action: go to overview
  4201. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4202. (org-overview)
  4203. (message "OVERVIEW")
  4204. (setq org-cycle-global-status 'overview)
  4205. (run-hook-with-args 'org-cycle-hook 'overview))))
  4206. (defun org-cycle-internal-local ()
  4207. "Do the local cycling action."
  4208. (org-back-to-heading)
  4209. (let ((goal-column 0) eoh eol eos)
  4210. ;; First, some boundaries
  4211. (save-excursion
  4212. (org-back-to-heading)
  4213. (save-excursion
  4214. (beginning-of-line 2)
  4215. (while (and (not (eobp)) ;; this is like `next-line'
  4216. (get-char-property (1- (point)) 'invisible))
  4217. (beginning-of-line 2)) (setq eol (point)))
  4218. (outline-end-of-heading) (setq eoh (point))
  4219. (org-end-of-subtree t)
  4220. (unless (eobp)
  4221. (skip-chars-forward " \t\n")
  4222. (beginning-of-line 1) ; in case this is an item
  4223. )
  4224. (setq eos (1- (point))))
  4225. ;; Find out what to do next and set `this-command'
  4226. (cond
  4227. ((= eos eoh)
  4228. ;; Nothing is hidden behind this heading
  4229. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4230. (message "EMPTY ENTRY")
  4231. (setq org-cycle-subtree-status nil)
  4232. (save-excursion
  4233. (goto-char eos)
  4234. (outline-next-heading)
  4235. (if (org-invisible-p) (org-flag-heading nil))))
  4236. ((or (>= eol eos)
  4237. (not (string-match "\\S-" (buffer-substring eol eos))))
  4238. ;; Entire subtree is hidden in one line: open it
  4239. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4240. (org-show-entry)
  4241. (show-children)
  4242. (message "CHILDREN")
  4243. (save-excursion
  4244. (goto-char eos)
  4245. (outline-next-heading)
  4246. (if (org-invisible-p) (org-flag-heading nil)))
  4247. (setq org-cycle-subtree-status 'children)
  4248. (run-hook-with-args 'org-cycle-hook 'children))
  4249. ((and (eq last-command this-command)
  4250. (eq org-cycle-subtree-status 'children))
  4251. ;; We just showed the children, now show everything.
  4252. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4253. (org-show-subtree)
  4254. (message "SUBTREE")
  4255. (setq org-cycle-subtree-status 'subtree)
  4256. (run-hook-with-args 'org-cycle-hook 'subtree))
  4257. (t
  4258. ;; Default action: hide the subtree.
  4259. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4260. (hide-subtree)
  4261. (message "FOLDED")
  4262. (setq org-cycle-subtree-status 'folded)
  4263. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4264. ;;;###autoload
  4265. (defun org-global-cycle (&optional arg)
  4266. "Cycle the global visibility. For details see `org-cycle'.
  4267. With C-u prefix arg, switch to startup visibility.
  4268. With a numeric prefix, show all headlines up to that level."
  4269. (interactive "P")
  4270. (let ((org-cycle-include-plain-lists
  4271. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4272. (cond
  4273. ((integerp arg)
  4274. (show-all)
  4275. (hide-sublevels arg)
  4276. (setq org-cycle-global-status 'contents))
  4277. ((equal arg '(4))
  4278. (org-set-startup-visibility)
  4279. (message "Startup visibility, plus VISIBILITY properties."))
  4280. (t
  4281. (org-cycle '(4))))))
  4282. (defun org-set-startup-visibility ()
  4283. "Set the visibility required by startup options and properties."
  4284. (cond
  4285. ((eq org-startup-folded t)
  4286. (org-cycle '(4)))
  4287. ((eq org-startup-folded 'content)
  4288. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4289. (org-cycle '(4)) (org-cycle '(4)))))
  4290. (org-set-visibility-according-to-property 'no-cleanup)
  4291. (org-cycle-hide-archived-subtrees 'all)
  4292. (org-cycle-hide-drawers 'all)
  4293. (org-cycle-show-empty-lines 'all))
  4294. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4295. "Switch subtree visibilities according to :VISIBILITY: property."
  4296. (interactive)
  4297. (let (org-show-entry-below state)
  4298. (save-excursion
  4299. (goto-char (point-min))
  4300. (while (re-search-forward
  4301. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4302. nil t)
  4303. (setq state (match-string 1))
  4304. (save-excursion
  4305. (org-back-to-heading t)
  4306. (hide-subtree)
  4307. (org-reveal)
  4308. (cond
  4309. ((equal state '("fold" "folded"))
  4310. (hide-subtree))
  4311. ((equal state "children")
  4312. (org-show-hidden-entry)
  4313. (show-children))
  4314. ((equal state "content")
  4315. (save-excursion
  4316. (save-restriction
  4317. (org-narrow-to-subtree)
  4318. (org-content))))
  4319. ((member state '("all" "showall"))
  4320. (show-subtree)))))
  4321. (unless no-cleanup
  4322. (org-cycle-hide-archived-subtrees 'all)
  4323. (org-cycle-hide-drawers 'all)
  4324. (org-cycle-show-empty-lines 'all)))))
  4325. (defun org-overview ()
  4326. "Switch to overview mode, showing only top-level headlines.
  4327. Really, this shows all headlines with level equal or greater than the level
  4328. of the first headline in the buffer. This is important, because if the
  4329. first headline is not level one, then (hide-sublevels 1) gives confusing
  4330. results."
  4331. (interactive)
  4332. (let ((level (save-excursion
  4333. (goto-char (point-min))
  4334. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4335. (progn
  4336. (goto-char (match-beginning 0))
  4337. (funcall outline-level))))))
  4338. (and level (hide-sublevels level))))
  4339. (defun org-content (&optional arg)
  4340. "Show all headlines in the buffer, like a table of contents.
  4341. With numerical argument N, show content up to level N."
  4342. (interactive "P")
  4343. (save-excursion
  4344. ;; Visit all headings and show their offspring
  4345. (and (integerp arg) (org-overview))
  4346. (goto-char (point-max))
  4347. (catch 'exit
  4348. (while (and (progn (condition-case nil
  4349. (outline-previous-visible-heading 1)
  4350. (error (goto-char (point-min))))
  4351. t)
  4352. (looking-at outline-regexp))
  4353. (if (integerp arg)
  4354. (show-children (1- arg))
  4355. (show-branches))
  4356. (if (bobp) (throw 'exit nil))))))
  4357. (defun org-optimize-window-after-visibility-change (state)
  4358. "Adjust the window after a change in outline visibility.
  4359. This function is the default value of the hook `org-cycle-hook'."
  4360. (when (get-buffer-window (current-buffer))
  4361. (cond
  4362. ((eq state 'content) nil)
  4363. ((eq state 'all) nil)
  4364. ((eq state 'folded) nil)
  4365. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4366. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4367. (defun org-compact-display-after-subtree-move ()
  4368. "Show a compacter version of the tree of the entry's parent."
  4369. (save-excursion
  4370. (if (org-up-heading-safe)
  4371. (progn
  4372. (hide-subtree)
  4373. (show-entry)
  4374. (show-children)
  4375. (org-cycle-show-empty-lines 'children)
  4376. (org-cycle-hide-drawers 'children))
  4377. (org-overview))))
  4378. (defun org-cycle-show-empty-lines (state)
  4379. "Show empty lines above all visible headlines.
  4380. The region to be covered depends on STATE when called through
  4381. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4382. entire buffer covered. Note that an empty line is only shown if there
  4383. are at least `org-cycle-separator-lines' empty lines before the headline."
  4384. (when (> org-cycle-separator-lines 0)
  4385. (save-excursion
  4386. (let* ((n org-cycle-separator-lines)
  4387. (re (cond
  4388. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4389. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4390. (t (let ((ns (number-to-string (- n 2))))
  4391. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4392. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4393. beg end)
  4394. (cond
  4395. ((memq state '(overview contents t))
  4396. (setq beg (point-min) end (point-max)))
  4397. ((memq state '(children folded))
  4398. (setq beg (point) end (progn (org-end-of-subtree t t)
  4399. (beginning-of-line 2)
  4400. (point)))))
  4401. (when beg
  4402. (goto-char beg)
  4403. (while (re-search-forward re end t)
  4404. (if (not (get-char-property (match-end 1) 'invisible))
  4405. (outline-flag-region
  4406. (match-beginning 1) (match-end 1) nil)))))))
  4407. ;; Never hide empty lines at the end of the file.
  4408. (save-excursion
  4409. (goto-char (point-max))
  4410. (outline-previous-heading)
  4411. (outline-end-of-heading)
  4412. (if (and (looking-at "[ \t\n]+")
  4413. (= (match-end 0) (point-max)))
  4414. (outline-flag-region (point) (match-end 0) nil))))
  4415. (defun org-show-empty-lines-in-parent ()
  4416. "Move to the parent and re-show empty lines before visible headlines."
  4417. (save-excursion
  4418. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4419. (org-cycle-show-empty-lines context))))
  4420. (defun org-cycle-hide-drawers (state)
  4421. "Re-hide all drawers after a visibility state change."
  4422. (when (and (org-mode-p)
  4423. (not (memq state '(overview folded))))
  4424. (save-excursion
  4425. (let* ((globalp (memq state '(contents all)))
  4426. (beg (if globalp (point-min) (point)))
  4427. (end (if globalp (point-max) (org-end-of-subtree t))))
  4428. (goto-char beg)
  4429. (while (re-search-forward org-drawer-regexp end t)
  4430. (org-flag-drawer t))))))
  4431. (defun org-flag-drawer (flag)
  4432. (save-excursion
  4433. (beginning-of-line 1)
  4434. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4435. (let ((b (match-end 0))
  4436. (outline-regexp org-outline-regexp))
  4437. (if (re-search-forward
  4438. "^[ \t]*:END:"
  4439. (save-excursion (outline-next-heading) (point)) t)
  4440. (outline-flag-region b (point-at-eol) flag)
  4441. (error ":END: line missing"))))))
  4442. (defun org-subtree-end-visible-p ()
  4443. "Is the end of the current subtree visible?"
  4444. (pos-visible-in-window-p
  4445. (save-excursion (org-end-of-subtree t) (point))))
  4446. (defun org-first-headline-recenter (&optional N)
  4447. "Move cursor to the first headline and recenter the headline.
  4448. Optional argument N means, put the headline into the Nth line of the window."
  4449. (goto-char (point-min))
  4450. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4451. (beginning-of-line)
  4452. (recenter (prefix-numeric-value N))))
  4453. ;;; Org-goto
  4454. (defvar org-goto-window-configuration nil)
  4455. (defvar org-goto-marker nil)
  4456. (defvar org-goto-map
  4457. (let ((map (make-sparse-keymap)))
  4458. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4459. (while (setq cmd (pop cmds))
  4460. (substitute-key-definition cmd cmd map global-map)))
  4461. (suppress-keymap map)
  4462. (org-defkey map "\C-m" 'org-goto-ret)
  4463. (org-defkey map [(return)] 'org-goto-ret)
  4464. (org-defkey map [(left)] 'org-goto-left)
  4465. (org-defkey map [(right)] 'org-goto-right)
  4466. (org-defkey map [(control ?g)] 'org-goto-quit)
  4467. (org-defkey map "\C-i" 'org-cycle)
  4468. (org-defkey map [(tab)] 'org-cycle)
  4469. (org-defkey map [(down)] 'outline-next-visible-heading)
  4470. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4471. (if org-goto-auto-isearch
  4472. (if (fboundp 'define-key-after)
  4473. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4474. nil)
  4475. (org-defkey map "q" 'org-goto-quit)
  4476. (org-defkey map "n" 'outline-next-visible-heading)
  4477. (org-defkey map "p" 'outline-previous-visible-heading)
  4478. (org-defkey map "f" 'outline-forward-same-level)
  4479. (org-defkey map "b" 'outline-backward-same-level)
  4480. (org-defkey map "u" 'outline-up-heading))
  4481. (org-defkey map "/" 'org-occur)
  4482. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4483. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4484. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4485. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4486. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4487. map))
  4488. (defconst org-goto-help
  4489. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4490. RET=jump to location [Q]uit and return to previous location
  4491. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4492. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4493. ;; FIXME: Docstring doe not mention both interfaces
  4494. (defun org-goto (&optional alternative-interface)
  4495. "Look up a different location in the current file, keeping current visibility.
  4496. When you want look-up or go to a different location in a document, the
  4497. fastest way is often to fold the entire buffer and then dive into the tree.
  4498. This method has the disadvantage, that the previous location will be folded,
  4499. which may not be what you want.
  4500. This command works around this by showing a copy of the current buffer
  4501. in an indirect buffer, in overview mode. You can dive into the tree in
  4502. that copy, use org-occur and incremental search to find a location.
  4503. When pressing RET or `Q', the command returns to the original buffer in
  4504. which the visibility is still unchanged. After RET is will also jump to
  4505. the location selected in the indirect buffer and expose the
  4506. the headline hierarchy above."
  4507. (interactive "P")
  4508. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4509. (org-refile-use-outline-path t)
  4510. (org-refile-target-verify-function nil)
  4511. (interface
  4512. (if (not alternative-interface)
  4513. org-goto-interface
  4514. (if (eq org-goto-interface 'outline)
  4515. 'outline-path-completion
  4516. 'outline)))
  4517. (org-goto-start-pos (point))
  4518. (selected-point
  4519. (if (eq interface 'outline)
  4520. (car (org-get-location (current-buffer) org-goto-help))
  4521. (nth 3 (org-refile-get-location "Goto: ")))))
  4522. (if selected-point
  4523. (progn
  4524. (org-mark-ring-push org-goto-start-pos)
  4525. (goto-char selected-point)
  4526. (if (or (org-invisible-p) (org-invisible-p2))
  4527. (org-show-context 'org-goto)))
  4528. (message "Quit"))))
  4529. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4530. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4531. (defvar org-goto-local-auto-isearch-map) ; defined below
  4532. (defun org-get-location (buf help)
  4533. "Let the user select a location in the Org-mode buffer BUF.
  4534. This function uses a recursive edit. It returns the selected position
  4535. or nil."
  4536. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4537. (isearch-hide-immediately nil)
  4538. (isearch-search-fun-function
  4539. (lambda () 'org-goto-local-search-headings))
  4540. (org-goto-selected-point org-goto-exit-command))
  4541. (save-excursion
  4542. (save-window-excursion
  4543. (delete-other-windows)
  4544. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4545. (switch-to-buffer
  4546. (condition-case nil
  4547. (make-indirect-buffer (current-buffer) "*org-goto*")
  4548. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4549. (with-output-to-temp-buffer "*Help*"
  4550. (princ help))
  4551. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4552. (setq buffer-read-only nil)
  4553. (let ((org-startup-truncated t)
  4554. (org-startup-folded nil)
  4555. (org-startup-align-all-tables nil))
  4556. (org-mode)
  4557. (org-overview))
  4558. (setq buffer-read-only t)
  4559. (if (and (boundp 'org-goto-start-pos)
  4560. (integer-or-marker-p org-goto-start-pos))
  4561. (let ((org-show-hierarchy-above t)
  4562. (org-show-siblings t)
  4563. (org-show-following-heading t))
  4564. (goto-char org-goto-start-pos)
  4565. (and (org-invisible-p) (org-show-context)))
  4566. (goto-char (point-min)))
  4567. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4568. (message "Select location and press RET")
  4569. (use-local-map org-goto-map)
  4570. (recursive-edit)
  4571. ))
  4572. (kill-buffer "*org-goto*")
  4573. (cons org-goto-selected-point org-goto-exit-command)))
  4574. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4575. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4576. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4577. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4578. (defun org-goto-local-search-headings (string bound noerror)
  4579. "Search and make sure that any matches are in headlines."
  4580. (catch 'return
  4581. (while (if isearch-forward
  4582. (search-forward string bound noerror)
  4583. (search-backward string bound noerror))
  4584. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4585. (and (member :headline context)
  4586. (not (member :tags context))))
  4587. (throw 'return (point))))))
  4588. (defun org-goto-local-auto-isearch ()
  4589. "Start isearch."
  4590. (interactive)
  4591. (goto-char (point-min))
  4592. (let ((keys (this-command-keys)))
  4593. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4594. (isearch-mode t)
  4595. (isearch-process-search-char (string-to-char keys)))))
  4596. (defun org-goto-ret (&optional arg)
  4597. "Finish `org-goto' by going to the new location."
  4598. (interactive "P")
  4599. (setq org-goto-selected-point (point)
  4600. org-goto-exit-command 'return)
  4601. (throw 'exit nil))
  4602. (defun org-goto-left ()
  4603. "Finish `org-goto' by going to the new location."
  4604. (interactive)
  4605. (if (org-on-heading-p)
  4606. (progn
  4607. (beginning-of-line 1)
  4608. (setq org-goto-selected-point (point)
  4609. org-goto-exit-command 'left)
  4610. (throw 'exit nil))
  4611. (error "Not on a heading")))
  4612. (defun org-goto-right ()
  4613. "Finish `org-goto' by going to the new location."
  4614. (interactive)
  4615. (if (org-on-heading-p)
  4616. (progn
  4617. (setq org-goto-selected-point (point)
  4618. org-goto-exit-command 'right)
  4619. (throw 'exit nil))
  4620. (error "Not on a heading")))
  4621. (defun org-goto-quit ()
  4622. "Finish `org-goto' without cursor motion."
  4623. (interactive)
  4624. (setq org-goto-selected-point nil)
  4625. (setq org-goto-exit-command 'quit)
  4626. (throw 'exit nil))
  4627. ;;; Indirect buffer display of subtrees
  4628. (defvar org-indirect-dedicated-frame nil
  4629. "This is the frame being used for indirect tree display.")
  4630. (defvar org-last-indirect-buffer nil)
  4631. (defun org-tree-to-indirect-buffer (&optional arg)
  4632. "Create indirect buffer and narrow it to current subtree.
  4633. With numerical prefix ARG, go up to this level and then take that tree.
  4634. If ARG is negative, go up that many levels.
  4635. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4636. indirect buffer previously made with this command, to avoid proliferation of
  4637. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4638. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4639. is kept so that you can work with several indirect buffers at the same time.
  4640. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4641. requests that a new frame be made for the new buffer, so that the dedicated
  4642. frame is not changed."
  4643. (interactive "P")
  4644. (let ((cbuf (current-buffer))
  4645. (cwin (selected-window))
  4646. (pos (point))
  4647. beg end level heading ibuf)
  4648. (save-excursion
  4649. (org-back-to-heading t)
  4650. (when (numberp arg)
  4651. (setq level (org-outline-level))
  4652. (if (< arg 0) (setq arg (+ level arg)))
  4653. (while (> (setq level (org-outline-level)) arg)
  4654. (outline-up-heading 1 t)))
  4655. (setq beg (point)
  4656. heading (org-get-heading))
  4657. (org-end-of-subtree t) (setq end (point)))
  4658. (if (and (buffer-live-p org-last-indirect-buffer)
  4659. (not (eq org-indirect-buffer-display 'new-frame))
  4660. (not arg))
  4661. (kill-buffer org-last-indirect-buffer))
  4662. (setq ibuf (org-get-indirect-buffer cbuf)
  4663. org-last-indirect-buffer ibuf)
  4664. (cond
  4665. ((or (eq org-indirect-buffer-display 'new-frame)
  4666. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4667. (select-frame (make-frame))
  4668. (delete-other-windows)
  4669. (switch-to-buffer ibuf)
  4670. (org-set-frame-title heading))
  4671. ((eq org-indirect-buffer-display 'dedicated-frame)
  4672. (raise-frame
  4673. (select-frame (or (and org-indirect-dedicated-frame
  4674. (frame-live-p org-indirect-dedicated-frame)
  4675. org-indirect-dedicated-frame)
  4676. (setq org-indirect-dedicated-frame (make-frame)))))
  4677. (delete-other-windows)
  4678. (switch-to-buffer ibuf)
  4679. (org-set-frame-title (concat "Indirect: " heading)))
  4680. ((eq org-indirect-buffer-display 'current-window)
  4681. (switch-to-buffer ibuf))
  4682. ((eq org-indirect-buffer-display 'other-window)
  4683. (pop-to-buffer ibuf))
  4684. (t (error "Invalid value.")))
  4685. (if (featurep 'xemacs)
  4686. (save-excursion (org-mode) (turn-on-font-lock)))
  4687. (narrow-to-region beg end)
  4688. (show-all)
  4689. (goto-char pos)
  4690. (and (window-live-p cwin) (select-window cwin))))
  4691. (defun org-get-indirect-buffer (&optional buffer)
  4692. (setq buffer (or buffer (current-buffer)))
  4693. (let ((n 1) (base (buffer-name buffer)) bname)
  4694. (while (buffer-live-p
  4695. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4696. (setq n (1+ n)))
  4697. (condition-case nil
  4698. (make-indirect-buffer buffer bname 'clone)
  4699. (error (make-indirect-buffer buffer bname)))))
  4700. (defun org-set-frame-title (title)
  4701. "Set the title of the current frame to the string TITLE."
  4702. ;; FIXME: how to name a single frame in XEmacs???
  4703. (unless (featurep 'xemacs)
  4704. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4705. ;;;; Structure editing
  4706. ;;; Inserting headlines
  4707. (defun org-previous-line-empty-p ()
  4708. (save-excursion
  4709. (and (not (bobp))
  4710. (or (beginning-of-line 0) t)
  4711. (save-match-data
  4712. (looking-at "[ \t]*$")))))
  4713. (defun org-insert-heading (&optional force-heading)
  4714. "Insert a new heading or item with same depth at point.
  4715. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4716. If point is at the beginning of a headline, insert a sibling before the
  4717. current headline. If point is not at the beginning, do not split the line,
  4718. but create the new headline after the current line."
  4719. (interactive "P")
  4720. (if (= (buffer-size) 0)
  4721. (insert "\n* ")
  4722. (when (or force-heading (not (org-insert-item)))
  4723. (let* ((empty-line-p nil)
  4724. (head (save-excursion
  4725. (condition-case nil
  4726. (progn
  4727. (org-back-to-heading)
  4728. (setq empty-line-p (org-previous-line-empty-p))
  4729. (match-string 0))
  4730. (error "*"))))
  4731. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4732. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4733. pos hide-previous previous-pos)
  4734. (cond
  4735. ((and (org-on-heading-p) (bolp)
  4736. (or (bobp)
  4737. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4738. ;; insert before the current line
  4739. (open-line (if blank 2 1)))
  4740. ((and (bolp)
  4741. (or (bobp)
  4742. (save-excursion
  4743. (backward-char 1) (not (org-invisible-p)))))
  4744. ;; insert right here
  4745. nil)
  4746. (t
  4747. ;; somewhere in the line
  4748. (save-excursion
  4749. (setq previous-pos (point-at-bol))
  4750. (end-of-line)
  4751. (setq hide-previous (org-invisible-p)))
  4752. (and org-insert-heading-respect-content (org-show-subtree))
  4753. (let ((split
  4754. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4755. (save-excursion
  4756. (let ((p (point)))
  4757. (goto-char (point-at-bol))
  4758. (and (looking-at org-complex-heading-regexp)
  4759. (> p (match-beginning 4)))))))
  4760. tags pos)
  4761. (cond
  4762. (org-insert-heading-respect-content
  4763. (org-end-of-subtree nil t)
  4764. (or (bolp) (newline))
  4765. (or (org-previous-line-empty-p)
  4766. (and blank (newline)))
  4767. (open-line 1))
  4768. ((org-on-heading-p)
  4769. (when hide-previous
  4770. (show-children)
  4771. (org-show-entry))
  4772. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4773. (setq tags (and (match-end 2) (match-string 2)))
  4774. (and (match-end 1)
  4775. (delete-region (match-beginning 1) (match-end 1)))
  4776. (setq pos (point-at-bol))
  4777. (or split (end-of-line 1))
  4778. (delete-horizontal-space)
  4779. (newline (if blank 2 1))
  4780. (when tags
  4781. (save-excursion
  4782. (goto-char pos)
  4783. (end-of-line 1)
  4784. (insert " " tags)
  4785. (org-set-tags nil 'align))))
  4786. (t
  4787. (or split (end-of-line 1))
  4788. (newline (if blank 2 1)))))))
  4789. (insert head) (just-one-space)
  4790. (setq pos (point))
  4791. (end-of-line 1)
  4792. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4793. (when (and org-insert-heading-respect-content hide-previous)
  4794. (save-excursion
  4795. (goto-char previous-pos)
  4796. (hide-subtree)))
  4797. (run-hooks 'org-insert-heading-hook)))))
  4798. (defun org-get-heading (&optional no-tags)
  4799. "Return the heading of the current entry, without the stars."
  4800. (save-excursion
  4801. (org-back-to-heading t)
  4802. (if (looking-at
  4803. (if no-tags
  4804. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4805. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4806. (match-string 1) "")))
  4807. (defun org-heading-components ()
  4808. "Return the components of the current heading.
  4809. This is a list with the following elements:
  4810. - the level as an integer
  4811. - the reduced level, different if `org-odd-levels-only' is set.
  4812. - the TODO keyword, or nil
  4813. - the priority character, like ?A, or nil if no priority is given
  4814. - the headline text itself, or the tags string if no headline text
  4815. - the tags string, or nil."
  4816. (save-excursion
  4817. (org-back-to-heading t)
  4818. (if (looking-at org-complex-heading-regexp)
  4819. (list (length (match-string 1))
  4820. (org-reduced-level (length (match-string 1)))
  4821. (org-match-string-no-properties 2)
  4822. (and (match-end 3) (aref (match-string 3) 2))
  4823. (org-match-string-no-properties 4)
  4824. (org-match-string-no-properties 5)))))
  4825. (defun org-get-entry ()
  4826. "Get the entry text, after heading, entire subtree."
  4827. (save-excursion
  4828. (org-back-to-heading t)
  4829. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  4830. (defun org-insert-heading-after-current ()
  4831. "Insert a new heading with same level as current, after current subtree."
  4832. (interactive)
  4833. (org-back-to-heading)
  4834. (org-insert-heading)
  4835. (org-move-subtree-down)
  4836. (end-of-line 1))
  4837. (defun org-insert-heading-respect-content ()
  4838. (interactive)
  4839. (let ((org-insert-heading-respect-content t))
  4840. (org-insert-heading t)))
  4841. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4842. (interactive "P")
  4843. (let ((org-insert-heading-respect-content t))
  4844. (org-insert-todo-heading force-state t)))
  4845. (defun org-insert-todo-heading (arg &optional force-heading)
  4846. "Insert a new heading with the same level and TODO state as current heading.
  4847. If the heading has no TODO state, or if the state is DONE, use the first
  4848. state (TODO by default). Also with prefix arg, force first state."
  4849. (interactive "P")
  4850. (when (or force-heading (not (org-insert-item 'checkbox)))
  4851. (org-insert-heading force-heading)
  4852. (save-excursion
  4853. (org-back-to-heading)
  4854. (outline-previous-heading)
  4855. (looking-at org-todo-line-regexp))
  4856. (let*
  4857. ((new-mark-x
  4858. (if (or arg
  4859. (not (match-beginning 2))
  4860. (member (match-string 2) org-done-keywords))
  4861. (car org-todo-keywords-1)
  4862. (match-string 2)))
  4863. (new-mark
  4864. (or
  4865. (run-hook-with-args-until-success
  4866. 'org-todo-get-default-hook new-mark-x nil)
  4867. new-mark-x)))
  4868. (beginning-of-line 1)
  4869. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4870. (insert new-mark " ")))
  4871. (when org-provide-todo-statistics
  4872. (org-update-parent-todo-statistics))))
  4873. (defun org-insert-subheading (arg)
  4874. "Insert a new subheading and demote it.
  4875. Works for outline headings and for plain lists alike."
  4876. (interactive "P")
  4877. (org-insert-heading arg)
  4878. (cond
  4879. ((org-on-heading-p) (org-do-demote))
  4880. ((org-at-item-p) (org-indent-item 1))))
  4881. (defun org-insert-todo-subheading (arg)
  4882. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4883. Works for outline headings and for plain lists alike."
  4884. (interactive "P")
  4885. (org-insert-todo-heading arg)
  4886. (cond
  4887. ((org-on-heading-p) (org-do-demote))
  4888. ((org-at-item-p) (org-indent-item 1))))
  4889. ;;; Promotion and Demotion
  4890. (defun org-promote-subtree ()
  4891. "Promote the entire subtree.
  4892. See also `org-promote'."
  4893. (interactive)
  4894. (save-excursion
  4895. (org-map-tree 'org-promote))
  4896. (org-fix-position-after-promote))
  4897. (defun org-demote-subtree ()
  4898. "Demote the entire subtree. See `org-demote'.
  4899. See also `org-promote'."
  4900. (interactive)
  4901. (save-excursion
  4902. (org-map-tree 'org-demote))
  4903. (org-fix-position-after-promote))
  4904. (defun org-do-promote ()
  4905. "Promote the current heading higher up the tree.
  4906. If the region is active in `transient-mark-mode', promote all headings
  4907. in the region."
  4908. (interactive)
  4909. (save-excursion
  4910. (if (org-region-active-p)
  4911. (org-map-region 'org-promote (region-beginning) (region-end))
  4912. (org-promote)))
  4913. (org-fix-position-after-promote))
  4914. (defun org-do-demote ()
  4915. "Demote the current heading lower down the tree.
  4916. If the region is active in `transient-mark-mode', demote all headings
  4917. in the region."
  4918. (interactive)
  4919. (save-excursion
  4920. (if (org-region-active-p)
  4921. (org-map-region 'org-demote (region-beginning) (region-end))
  4922. (org-demote)))
  4923. (org-fix-position-after-promote))
  4924. (defun org-fix-position-after-promote ()
  4925. "Make sure that after pro/demotion cursor position is right."
  4926. (let ((pos (point)))
  4927. (when (save-excursion
  4928. (beginning-of-line 1)
  4929. (looking-at org-todo-line-regexp)
  4930. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4931. (cond ((eobp) (insert " "))
  4932. ((eolp) (insert " "))
  4933. ((equal (char-after) ?\ ) (forward-char 1))))))
  4934. (defun org-reduced-level (l)
  4935. "Compute the effective level of a heading.
  4936. This takes into account the setting of `org-odd-levels-only'."
  4937. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4938. (defun org-get-valid-level (level &optional change)
  4939. "Rectify a level change under the influence of `org-odd-levels-only'
  4940. LEVEL is a current level, CHANGE is by how much the level should be
  4941. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4942. even level numbers will become the next higher odd number."
  4943. (if org-odd-levels-only
  4944. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4945. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4946. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4947. (max 1 (+ level (or change 0)))))
  4948. (if (boundp 'define-obsolete-function-alias)
  4949. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4950. (define-obsolete-function-alias 'org-get-legal-level
  4951. 'org-get-valid-level)
  4952. (define-obsolete-function-alias 'org-get-legal-level
  4953. 'org-get-valid-level "23.1")))
  4954. (defun org-promote ()
  4955. "Promote the current heading higher up the tree.
  4956. If the region is active in `transient-mark-mode', promote all headings
  4957. in the region."
  4958. (org-back-to-heading t)
  4959. (let* ((level (save-match-data (funcall outline-level)))
  4960. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4961. (diff (abs (- level (length up-head) -1))))
  4962. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4963. (replace-match up-head nil t)
  4964. ;; Fixup tag positioning
  4965. (and org-auto-align-tags (org-set-tags nil t))
  4966. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4967. (defun org-demote ()
  4968. "Demote the current heading lower down the tree.
  4969. If the region is active in `transient-mark-mode', demote all headings
  4970. in the region."
  4971. (org-back-to-heading t)
  4972. (let* ((level (save-match-data (funcall outline-level)))
  4973. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4974. (diff (abs (- level (length down-head) -1))))
  4975. (replace-match down-head nil t)
  4976. ;; Fixup tag positioning
  4977. (and org-auto-align-tags (org-set-tags nil t))
  4978. (if org-adapt-indentation (org-fixup-indentation diff))))
  4979. (defun org-map-tree (fun)
  4980. "Call FUN for every heading underneath the current one."
  4981. (org-back-to-heading)
  4982. (let ((level (funcall outline-level)))
  4983. (save-excursion
  4984. (funcall fun)
  4985. (while (and (progn
  4986. (outline-next-heading)
  4987. (> (funcall outline-level) level))
  4988. (not (eobp)))
  4989. (funcall fun)))))
  4990. (defun org-map-region (fun beg end)
  4991. "Call FUN for every heading between BEG and END."
  4992. (let ((org-ignore-region t))
  4993. (save-excursion
  4994. (setq end (copy-marker end))
  4995. (goto-char beg)
  4996. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4997. (< (point) end))
  4998. (funcall fun))
  4999. (while (and (progn
  5000. (outline-next-heading)
  5001. (< (point) end))
  5002. (not (eobp)))
  5003. (funcall fun)))))
  5004. (defun org-fixup-indentation (diff)
  5005. "Change the indentation in the current entry by DIFF
  5006. However, if any line in the current entry has no indentation, or if it
  5007. would end up with no indentation after the change, nothing at all is done."
  5008. (save-excursion
  5009. (let ((end (save-excursion (outline-next-heading)
  5010. (point-marker)))
  5011. (prohibit (if (> diff 0)
  5012. "^\\S-"
  5013. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5014. col)
  5015. (unless (save-excursion (end-of-line 1)
  5016. (re-search-forward prohibit end t))
  5017. (while (and (< (point) end)
  5018. (re-search-forward "^[ \t]+" end t))
  5019. (goto-char (match-end 0))
  5020. (setq col (current-column))
  5021. (if (< diff 0) (replace-match ""))
  5022. (org-indent-to-column (+ diff col))))
  5023. (move-marker end nil))))
  5024. (defun org-convert-to-odd-levels ()
  5025. "Convert an org-mode file with all levels allowed to one with odd levels.
  5026. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5027. level 5 etc."
  5028. (interactive)
  5029. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5030. (let ((org-odd-levels-only nil) n)
  5031. (save-excursion
  5032. (goto-char (point-min))
  5033. (while (re-search-forward "^\\*\\*+ " nil t)
  5034. (setq n (- (length (match-string 0)) 2))
  5035. (while (>= (setq n (1- n)) 0)
  5036. (org-demote))
  5037. (end-of-line 1))))))
  5038. (defun org-convert-to-oddeven-levels ()
  5039. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5040. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5041. section with an even level, conversion would destroy the structure of the file. An error
  5042. is signaled in this case."
  5043. (interactive)
  5044. (goto-char (point-min))
  5045. ;; First check if there are no even levels
  5046. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5047. (org-show-context t)
  5048. (error "Not all levels are odd in this file. Conversion not possible."))
  5049. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5050. (let ((org-odd-levels-only nil) n)
  5051. (save-excursion
  5052. (goto-char (point-min))
  5053. (while (re-search-forward "^\\*\\*+ " nil t)
  5054. (setq n (/ (1- (length (match-string 0))) 2))
  5055. (while (>= (setq n (1- n)) 0)
  5056. (org-promote))
  5057. (end-of-line 1))))))
  5058. (defun org-tr-level (n)
  5059. "Make N odd if required."
  5060. (if org-odd-levels-only (1+ (/ n 2)) n))
  5061. ;;; Vertical tree motion, cutting and pasting of subtrees
  5062. (defun org-move-subtree-up (&optional arg)
  5063. "Move the current subtree up past ARG headlines of the same level."
  5064. (interactive "p")
  5065. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5066. (defun org-move-subtree-down (&optional arg)
  5067. "Move the current subtree down past ARG headlines of the same level."
  5068. (interactive "p")
  5069. (setq arg (prefix-numeric-value arg))
  5070. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5071. 'outline-get-last-sibling))
  5072. (ins-point (make-marker))
  5073. (cnt (abs arg))
  5074. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5075. ;; Select the tree
  5076. (org-back-to-heading)
  5077. (setq beg0 (point))
  5078. (save-excursion
  5079. (setq ne-beg (org-back-over-empty-lines))
  5080. (setq beg (point)))
  5081. (save-match-data
  5082. (save-excursion (outline-end-of-heading)
  5083. (setq folded (org-invisible-p)))
  5084. (outline-end-of-subtree))
  5085. (outline-next-heading)
  5086. (setq ne-end (org-back-over-empty-lines))
  5087. (setq end (point))
  5088. (goto-char beg0)
  5089. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5090. ;; include less whitespace
  5091. (save-excursion
  5092. (goto-char beg)
  5093. (forward-line (- ne-beg ne-end))
  5094. (setq beg (point))))
  5095. ;; Find insertion point, with error handling
  5096. (while (> cnt 0)
  5097. (or (and (funcall movfunc) (looking-at outline-regexp))
  5098. (progn (goto-char beg0)
  5099. (error "Cannot move past superior level or buffer limit")))
  5100. (setq cnt (1- cnt)))
  5101. (if (> arg 0)
  5102. ;; Moving forward - still need to move over subtree
  5103. (progn (org-end-of-subtree t t)
  5104. (save-excursion
  5105. (org-back-over-empty-lines)
  5106. (or (bolp) (newline)))))
  5107. (setq ne-ins (org-back-over-empty-lines))
  5108. (move-marker ins-point (point))
  5109. (setq txt (buffer-substring beg end))
  5110. (org-save-markers-in-region beg end)
  5111. (delete-region beg end)
  5112. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5113. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5114. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5115. (let ((bbb (point)))
  5116. (insert-before-markers txt)
  5117. (org-reinstall-markers-in-region bbb)
  5118. (move-marker ins-point bbb))
  5119. (or (bolp) (insert "\n"))
  5120. (setq ins-end (point))
  5121. (goto-char ins-point)
  5122. (org-skip-whitespace)
  5123. (when (and (< arg 0)
  5124. (org-first-sibling-p)
  5125. (> ne-ins ne-beg))
  5126. ;; Move whitespace back to beginning
  5127. (save-excursion
  5128. (goto-char ins-end)
  5129. (let ((kill-whole-line t))
  5130. (kill-line (- ne-ins ne-beg)) (point)))
  5131. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5132. (move-marker ins-point nil)
  5133. (org-compact-display-after-subtree-move)
  5134. (org-show-empty-lines-in-parent)
  5135. (unless folded
  5136. (org-show-entry)
  5137. (show-children)
  5138. (org-cycle-hide-drawers 'children))))
  5139. (defvar org-subtree-clip ""
  5140. "Clipboard for cut and paste of subtrees.
  5141. This is actually only a copy of the kill, because we use the normal kill
  5142. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5143. (defvar org-subtree-clip-folded nil
  5144. "Was the last copied subtree folded?
  5145. This is used to fold the tree back after pasting.")
  5146. (defun org-cut-subtree (&optional n)
  5147. "Cut the current subtree into the clipboard.
  5148. With prefix arg N, cut this many sequential subtrees.
  5149. This is a short-hand for marking the subtree and then cutting it."
  5150. (interactive "p")
  5151. (org-copy-subtree n 'cut))
  5152. (defun org-copy-subtree (&optional n cut force-store-markers)
  5153. "Cut the current subtree into the clipboard.
  5154. With prefix arg N, cut this many sequential subtrees.
  5155. This is a short-hand for marking the subtree and then copying it.
  5156. If CUT is non-nil, actually cut the subtree.
  5157. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5158. of some markers in the region, even if CUT is non-nil. This is
  5159. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5160. (interactive "p")
  5161. (let (beg end folded (beg0 (point)))
  5162. (if (interactive-p)
  5163. (org-back-to-heading nil) ; take what looks like a subtree
  5164. (org-back-to-heading t)) ; take what is really there
  5165. (org-back-over-empty-lines)
  5166. (setq beg (point))
  5167. (skip-chars-forward " \t\r\n")
  5168. (save-match-data
  5169. (save-excursion (outline-end-of-heading)
  5170. (setq folded (org-invisible-p)))
  5171. (condition-case nil
  5172. (outline-forward-same-level (1- n))
  5173. (error nil))
  5174. (org-end-of-subtree t t))
  5175. (org-back-over-empty-lines)
  5176. (setq end (point))
  5177. (goto-char beg0)
  5178. (when (> end beg)
  5179. (setq org-subtree-clip-folded folded)
  5180. (when (or cut force-store-markers)
  5181. (org-save-markers-in-region beg end))
  5182. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5183. (setq org-subtree-clip (current-kill 0))
  5184. (message "%s: Subtree(s) with %d characters"
  5185. (if cut "Cut" "Copied")
  5186. (length org-subtree-clip)))))
  5187. (defun org-paste-subtree (&optional level tree for-yank)
  5188. "Paste the clipboard as a subtree, with modification of headline level.
  5189. The entire subtree is promoted or demoted in order to match a new headline
  5190. level.
  5191. If the cursor is at the beginning of a headline, the same level as
  5192. that headline is used to paste the tree
  5193. If not, the new level is derived from the *visible* headings
  5194. before and after the insertion point, and taken to be the inferior headline
  5195. level of the two. So if the previous visible heading is level 3 and the
  5196. next is level 4 (or vice versa), level 4 will be used for insertion.
  5197. This makes sure that the subtree remains an independent subtree and does
  5198. not swallow low level entries.
  5199. You can also force a different level, either by using a numeric prefix
  5200. argument, or by inserting the heading marker by hand. For example, if the
  5201. cursor is after \"*****\", then the tree will be shifted to level 5.
  5202. If optional TREE is given, use this text instead of the kill ring.
  5203. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5204. move back over whitespace before inserting, and move point to the end of
  5205. the inserted text when done."
  5206. (interactive "P")
  5207. (unless (org-kill-is-subtree-p tree)
  5208. (error "%s"
  5209. (substitute-command-keys
  5210. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5211. (let* ((visp (not (org-invisible-p)))
  5212. (txt (or tree (and kill-ring (current-kill 0))))
  5213. (^re (concat "^\\(" outline-regexp "\\)"))
  5214. (re (concat "\\(" outline-regexp "\\)"))
  5215. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5216. (old-level (if (string-match ^re txt)
  5217. (- (match-end 0) (match-beginning 0) 1)
  5218. -1))
  5219. (force-level (cond (level (prefix-numeric-value level))
  5220. ((and (looking-at "[ \t]*$")
  5221. (string-match
  5222. ^re_ (buffer-substring
  5223. (point-at-bol) (point))))
  5224. (- (match-end 1) (match-beginning 1)))
  5225. ((and (bolp)
  5226. (looking-at org-outline-regexp))
  5227. (- (match-end 0) (point) 1))
  5228. (t nil)))
  5229. (previous-level (save-excursion
  5230. (condition-case nil
  5231. (progn
  5232. (outline-previous-visible-heading 1)
  5233. (if (looking-at re)
  5234. (- (match-end 0) (match-beginning 0) 1)
  5235. 1))
  5236. (error 1))))
  5237. (next-level (save-excursion
  5238. (condition-case nil
  5239. (progn
  5240. (or (looking-at outline-regexp)
  5241. (outline-next-visible-heading 1))
  5242. (if (looking-at re)
  5243. (- (match-end 0) (match-beginning 0) 1)
  5244. 1))
  5245. (error 1))))
  5246. (new-level (or force-level (max previous-level next-level)))
  5247. (shift (if (or (= old-level -1)
  5248. (= new-level -1)
  5249. (= old-level new-level))
  5250. 0
  5251. (- new-level old-level)))
  5252. (delta (if (> shift 0) -1 1))
  5253. (func (if (> shift 0) 'org-demote 'org-promote))
  5254. (org-odd-levels-only nil)
  5255. beg end newend)
  5256. ;; Remove the forced level indicator
  5257. (if force-level
  5258. (delete-region (point-at-bol) (point)))
  5259. ;; Paste
  5260. (beginning-of-line 1)
  5261. (unless for-yank (org-back-over-empty-lines))
  5262. (setq beg (point))
  5263. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5264. (insert-before-markers txt)
  5265. (unless (string-match "\n\\'" txt) (insert "\n"))
  5266. (setq newend (point))
  5267. (org-reinstall-markers-in-region beg)
  5268. (setq end (point))
  5269. (goto-char beg)
  5270. (skip-chars-forward " \t\n\r")
  5271. (setq beg (point))
  5272. (if (and (org-invisible-p) visp)
  5273. (save-excursion (outline-show-heading)))
  5274. ;; Shift if necessary
  5275. (unless (= shift 0)
  5276. (save-restriction
  5277. (narrow-to-region beg end)
  5278. (while (not (= shift 0))
  5279. (org-map-region func (point-min) (point-max))
  5280. (setq shift (+ delta shift)))
  5281. (goto-char (point-min))
  5282. (setq newend (point-max))))
  5283. (when (or (interactive-p) for-yank)
  5284. (message "Clipboard pasted as level %d subtree" new-level))
  5285. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5286. kill-ring
  5287. (eq org-subtree-clip (current-kill 0))
  5288. org-subtree-clip-folded)
  5289. ;; The tree was folded before it was killed/copied
  5290. (hide-subtree))
  5291. (and for-yank (goto-char newend))))
  5292. (defun org-kill-is-subtree-p (&optional txt)
  5293. "Check if the current kill is an outline subtree, or a set of trees.
  5294. Returns nil if kill does not start with a headline, or if the first
  5295. headline level is not the largest headline level in the tree.
  5296. So this will actually accept several entries of equal levels as well,
  5297. which is OK for `org-paste-subtree'.
  5298. If optional TXT is given, check this string instead of the current kill."
  5299. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5300. (start-level (and kill
  5301. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5302. org-outline-regexp "\\)")
  5303. kill)
  5304. (- (match-end 2) (match-beginning 2) 1)))
  5305. (re (concat "^" org-outline-regexp))
  5306. (start (1+ (or (match-beginning 2) -1))))
  5307. (if (not start-level)
  5308. (progn
  5309. nil) ;; does not even start with a heading
  5310. (catch 'exit
  5311. (while (setq start (string-match re kill (1+ start)))
  5312. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5313. (throw 'exit nil)))
  5314. t))))
  5315. (defvar org-markers-to-move nil
  5316. "Markers that should be moved with a cut-and-paste operation.
  5317. Those markers are stored together with their positions relative to
  5318. the start of the region.")
  5319. (defun org-save-markers-in-region (beg end)
  5320. "Check markers in region.
  5321. If these markers are between BEG and END, record their position relative
  5322. to BEG, so that after moving the block of text, we can put the markers back
  5323. into place.
  5324. This function gets called just before an entry or tree gets cut from the
  5325. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5326. called immediately, to move the markers with the entries."
  5327. (setq org-markers-to-move nil)
  5328. (when (featurep 'org-clock)
  5329. (org-clock-save-markers-for-cut-and-paste beg end))
  5330. (when (featurep 'org-agenda)
  5331. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5332. (defun org-check-and-save-marker (marker beg end)
  5333. "Check if MARKER is between BEG and END.
  5334. If yes, remember the marker and the distance to BEG."
  5335. (when (and (marker-buffer marker)
  5336. (equal (marker-buffer marker) (current-buffer)))
  5337. (if (and (>= marker beg) (< marker end))
  5338. (push (cons marker (- marker beg)) org-markers-to-move))))
  5339. (defun org-reinstall-markers-in-region (beg)
  5340. "Move all remembered markers to their position relative to BEG."
  5341. (mapc (lambda (x)
  5342. (move-marker (car x) (+ beg (cdr x))))
  5343. org-markers-to-move)
  5344. (setq org-markers-to-move nil))
  5345. (defun org-narrow-to-subtree ()
  5346. "Narrow buffer to the current subtree."
  5347. (interactive)
  5348. (save-excursion
  5349. (save-match-data
  5350. (narrow-to-region
  5351. (progn (org-back-to-heading) (point))
  5352. (progn (org-end-of-subtree t) (point))))))
  5353. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5354. "Clone the task (subtree) at point N times.
  5355. The clones will be inserted as siblings.
  5356. In interactive use, the user will be prompted for the number of clones
  5357. to be produced, and for a time SHIFT, which may be a repeater as used
  5358. in time stamps, for example `+3d'.
  5359. When a valid repeater is given and the entry contains any time stamps,
  5360. the clones will become a sequence in time, with time stamps in the
  5361. subtree shifted for each clone produced. If SHIFT is nil or the
  5362. empty string, time stamps will be left alone.
  5363. If the original subtree did contain time stamps with a repeater,
  5364. the following will happen:
  5365. - the repeater will be removed in each clone
  5366. - an additional clone will be produced, with the current, unshifted
  5367. date(s) in the entry.
  5368. - the original entry will be placed *after* all the clones, with
  5369. repeater intact.
  5370. - the start days in the repeater in the original entry will be shifted
  5371. to past the last clone.
  5372. I this way you can spell out a number of instances of a repeating task,
  5373. and still retain the repeater to cover future instances of the task."
  5374. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5375. (let (beg end template task
  5376. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5377. (if (not (and (integerp n) (> n 0)))
  5378. (error "Invalid number of replications %s" n))
  5379. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5380. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5381. shift)))
  5382. (error "Invalid shift specification %s" shift))
  5383. (when doshift
  5384. (setq shift-n (string-to-number (match-string 1 shift))
  5385. shift-what (cdr (assoc (match-string 2 shift)
  5386. '(("d" . day) ("w" . week)
  5387. ("m" . month) ("y" . year))))))
  5388. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5389. (setq nmin 1 nmax n)
  5390. (org-back-to-heading t)
  5391. (setq beg (point))
  5392. (org-end-of-subtree t t)
  5393. (setq end (point))
  5394. (setq template (buffer-substring beg end))
  5395. (when (and doshift
  5396. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5397. (delete-region beg end)
  5398. (setq end beg)
  5399. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5400. (goto-char end)
  5401. (loop for n from nmin to nmax do
  5402. (if (not doshift)
  5403. (setq task template)
  5404. (with-temp-buffer
  5405. (insert template)
  5406. (org-mode)
  5407. (goto-char (point-min))
  5408. (while (re-search-forward org-ts-regexp nil t)
  5409. (org-timestamp-change (* n shift-n) shift-what))
  5410. (unless (= n n-no-remove)
  5411. (goto-char (point-min))
  5412. (while (re-search-forward org-ts-regexp nil t)
  5413. (save-excursion
  5414. (goto-char (match-beginning 0))
  5415. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5416. (delete-region (match-beginning 1) (match-end 1))))))
  5417. (setq task (buffer-string))))
  5418. (insert task))
  5419. (goto-char beg)))
  5420. ;;; Outline Sorting
  5421. (defun org-sort (with-case)
  5422. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5423. Optional argument WITH-CASE means sort case-sensitively.
  5424. With a double prefix argument, also remove duplicate entries."
  5425. (interactive "P")
  5426. (if (org-at-table-p)
  5427. (org-call-with-arg 'org-table-sort-lines with-case)
  5428. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5429. (defun org-sort-remove-invisible (s)
  5430. (remove-text-properties 0 (length s) org-rm-props s)
  5431. (while (string-match org-bracket-link-regexp s)
  5432. (setq s (replace-match (if (match-end 2)
  5433. (match-string 3 s)
  5434. (match-string 1 s)) t t s)))
  5435. s)
  5436. (defvar org-priority-regexp) ; defined later in the file
  5437. (defun org-sort-entries-or-items
  5438. (&optional with-case sorting-type getkey-func compare-func property)
  5439. "Sort entries on a certain level of an outline tree.
  5440. If there is an active region, the entries in the region are sorted.
  5441. Else, if the cursor is before the first entry, sort the top-level items.
  5442. Else, the children of the entry at point are sorted.
  5443. Sorting can be alphabetically, numerically, and by date/time as given by
  5444. the first time stamp in the entry. The command prompts for the sorting
  5445. type unless it has been given to the function through the SORTING-TYPE
  5446. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5447. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5448. called with point at the beginning of the record. It must return either
  5449. a string or a number that should serve as the sorting key for that record.
  5450. Comparing entries ignores case by default. However, with an optional argument
  5451. WITH-CASE, the sorting considers case as well."
  5452. (interactive "P")
  5453. (let ((case-func (if with-case 'identity 'downcase))
  5454. start beg end stars re re2
  5455. txt what tmp plain-list-p)
  5456. ;; Find beginning and end of region to sort
  5457. (cond
  5458. ((org-region-active-p)
  5459. ;; we will sort the region
  5460. (setq end (region-end)
  5461. what "region")
  5462. (goto-char (region-beginning))
  5463. (if (not (org-on-heading-p)) (outline-next-heading))
  5464. (setq start (point)))
  5465. ((org-at-item-p)
  5466. ;; we will sort this plain list
  5467. (org-beginning-of-item-list) (setq start (point))
  5468. (org-end-of-item-list) (setq end (point))
  5469. (goto-char start)
  5470. (setq plain-list-p t
  5471. what "plain list"))
  5472. ((or (org-on-heading-p)
  5473. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5474. ;; we will sort the children of the current headline
  5475. (org-back-to-heading)
  5476. (setq start (point)
  5477. end (progn (org-end-of-subtree t t)
  5478. (org-back-over-empty-lines)
  5479. (point))
  5480. what "children")
  5481. (goto-char start)
  5482. (show-subtree)
  5483. (outline-next-heading))
  5484. (t
  5485. ;; we will sort the top-level entries in this file
  5486. (goto-char (point-min))
  5487. (or (org-on-heading-p) (outline-next-heading))
  5488. (setq start (point) end (point-max) what "top-level")
  5489. (goto-char start)
  5490. (show-all)))
  5491. (setq beg (point))
  5492. (if (>= beg end) (error "Nothing to sort"))
  5493. (unless plain-list-p
  5494. (looking-at "\\(\\*+\\)")
  5495. (setq stars (match-string 1)
  5496. re (concat "^" (regexp-quote stars) " +")
  5497. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5498. txt (buffer-substring beg end))
  5499. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5500. (if (and (not (equal stars "*")) (string-match re2 txt))
  5501. (error "Region to sort contains a level above the first entry")))
  5502. (unless sorting-type
  5503. (message
  5504. (if plain-list-p
  5505. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5506. "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:")
  5507. what)
  5508. (setq sorting-type (read-char-exclusive))
  5509. (and (= (downcase sorting-type) ?f)
  5510. (setq getkey-func
  5511. (org-ido-completing-read "Sort using function: "
  5512. obarray 'fboundp t nil nil))
  5513. (setq getkey-func (intern getkey-func)))
  5514. (and (= (downcase sorting-type) ?r)
  5515. (setq property
  5516. (org-ido-completing-read "Property: "
  5517. (mapcar 'list (org-buffer-property-keys t))
  5518. nil t))))
  5519. (message "Sorting entries...")
  5520. (save-restriction
  5521. (narrow-to-region start end)
  5522. (let ((dcst (downcase sorting-type))
  5523. (case-fold-search nil)
  5524. (now (current-time)))
  5525. (sort-subr
  5526. (/= dcst sorting-type)
  5527. ;; This function moves to the beginning character of the "record" to
  5528. ;; be sorted.
  5529. (if plain-list-p
  5530. (lambda nil
  5531. (if (org-at-item-p) t (goto-char (point-max))))
  5532. (lambda nil
  5533. (if (re-search-forward re nil t)
  5534. (goto-char (match-beginning 0))
  5535. (goto-char (point-max)))))
  5536. ;; This function moves to the last character of the "record" being
  5537. ;; sorted.
  5538. (if plain-list-p
  5539. 'org-end-of-item
  5540. (lambda nil
  5541. (save-match-data
  5542. (condition-case nil
  5543. (outline-forward-same-level 1)
  5544. (error
  5545. (goto-char (point-max)))))))
  5546. ;; This function returns the value that gets sorted against.
  5547. (if plain-list-p
  5548. (lambda nil
  5549. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5550. (cond
  5551. ((= dcst ?n)
  5552. (string-to-number (buffer-substring (match-end 0)
  5553. (point-at-eol))))
  5554. ((= dcst ?a)
  5555. (buffer-substring (match-end 0) (point-at-eol)))
  5556. ((= dcst ?t)
  5557. (if (re-search-forward org-ts-regexp
  5558. (point-at-eol) t)
  5559. (org-time-string-to-time (match-string 0))
  5560. now))
  5561. ((= dcst ?f)
  5562. (if getkey-func
  5563. (progn
  5564. (setq tmp (funcall getkey-func))
  5565. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5566. tmp)
  5567. (error "Invalid key function `%s'" getkey-func)))
  5568. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5569. (lambda nil
  5570. (cond
  5571. ((= dcst ?n)
  5572. (if (looking-at org-complex-heading-regexp)
  5573. (string-to-number (match-string 4))
  5574. nil))
  5575. ((= dcst ?a)
  5576. (if (looking-at org-complex-heading-regexp)
  5577. (funcall case-func (match-string 4))
  5578. nil))
  5579. ((= dcst ?t)
  5580. (if (re-search-forward org-ts-regexp
  5581. (save-excursion
  5582. (forward-line 2)
  5583. (point)) t)
  5584. (org-time-string-to-time (match-string 0))
  5585. now))
  5586. ((= dcst ?p)
  5587. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5588. (string-to-char (match-string 2))
  5589. org-default-priority))
  5590. ((= dcst ?r)
  5591. (or (org-entry-get nil property) ""))
  5592. ((= dcst ?o)
  5593. (if (looking-at org-complex-heading-regexp)
  5594. (- 9999 (length (member (match-string 2)
  5595. org-todo-keywords-1)))))
  5596. ((= dcst ?f)
  5597. (if getkey-func
  5598. (progn
  5599. (setq tmp (funcall getkey-func))
  5600. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5601. tmp)
  5602. (error "Invalid key function `%s'" getkey-func)))
  5603. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5604. nil
  5605. (cond
  5606. ((= dcst ?a) 'string<)
  5607. ((= dcst ?t) 'time-less-p)
  5608. ((= dcst ?f) compare-func)
  5609. (t nil)))))
  5610. (message "Sorting entries...done")))
  5611. (defun org-do-sort (table what &optional with-case sorting-type)
  5612. "Sort TABLE of WHAT according to SORTING-TYPE.
  5613. The user will be prompted for the SORTING-TYPE if the call to this
  5614. function does not specify it. WHAT is only for the prompt, to indicate
  5615. what is being sorted. The sorting key will be extracted from
  5616. the car of the elements of the table.
  5617. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5618. (unless sorting-type
  5619. (message
  5620. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5621. what)
  5622. (setq sorting-type (read-char-exclusive)))
  5623. (let ((dcst (downcase sorting-type))
  5624. extractfun comparefun)
  5625. ;; Define the appropriate functions
  5626. (cond
  5627. ((= dcst ?n)
  5628. (setq extractfun 'string-to-number
  5629. comparefun (if (= dcst sorting-type) '< '>)))
  5630. ((= dcst ?a)
  5631. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5632. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5633. comparefun (if (= dcst sorting-type)
  5634. 'string<
  5635. (lambda (a b) (and (not (string< a b))
  5636. (not (string= a b)))))))
  5637. ((= dcst ?t)
  5638. (setq extractfun
  5639. (lambda (x)
  5640. (if (or (string-match org-ts-regexp x)
  5641. (string-match org-ts-regexp-both x))
  5642. (time-to-seconds
  5643. (org-time-string-to-time (match-string 0 x)))
  5644. 0))
  5645. comparefun (if (= dcst sorting-type) '< '>)))
  5646. (t (error "Invalid sorting type `%c'" sorting-type)))
  5647. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5648. table)
  5649. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5650. ;;; Editing source examples
  5651. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5652. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5653. (defvar org-edit-src-force-single-line nil)
  5654. (defvar org-edit-src-from-org-mode nil)
  5655. (defvar org-edit-src-picture nil)
  5656. (define-minor-mode org-exit-edit-mode
  5657. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5658. (defun org-edit-src-code ()
  5659. "Edit the source code example at point.
  5660. An indirect buffer is created, and that buffer is then narrowed to the
  5661. example at point and switched to the correct language mode. When done,
  5662. exit by killing the buffer with \\[org-edit-src-exit]."
  5663. (interactive)
  5664. (let ((line (org-current-line))
  5665. (case-fold-search t)
  5666. (msg (substitute-command-keys
  5667. "Edit, then exit with C-c ' (C-c and single quote)"))
  5668. (info (org-edit-src-find-region-and-lang))
  5669. (org-mode-p (eq major-mode 'org-mode))
  5670. beg end lang lang-f single lfmt)
  5671. (if (not info)
  5672. nil
  5673. (setq beg (nth 0 info)
  5674. end (nth 1 info)
  5675. lang (nth 2 info)
  5676. single (nth 3 info)
  5677. lfmt (nth 4 info)
  5678. lang-f (intern (concat lang "-mode")))
  5679. (unless (functionp lang-f)
  5680. (error "No such language mode: %s" lang-f))
  5681. (goto-line line)
  5682. (if (get-buffer "*Org Edit Src Example*")
  5683. (kill-buffer "*Org Edit Src Example*"))
  5684. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5685. "*Org Edit Src Example*"))
  5686. (narrow-to-region beg end)
  5687. (remove-text-properties beg end '(display nil invisible nil
  5688. intangible nil))
  5689. (let ((org-inhibit-startup t))
  5690. (funcall lang-f))
  5691. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5692. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5693. (when lfmt
  5694. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5695. (when org-mode-p
  5696. (goto-char (point-min))
  5697. (while (re-search-forward "^," nil t)
  5698. (replace-match "")))
  5699. (goto-line line)
  5700. (org-exit-edit-mode)
  5701. (org-set-local 'header-line-format msg)
  5702. (message "%s" msg)
  5703. t)))
  5704. (defun org-edit-fixed-width-region ()
  5705. "Edit the fixed-width ascii drawing at point.
  5706. This must be a region where each line starts with a colon followed by
  5707. a space character.
  5708. An indirect buffer is created, and that buffer is then narrowed to the
  5709. example at point and switched to artist-mode. When done,
  5710. exit by killing the buffer with \\[org-edit-src-exit]."
  5711. (interactive)
  5712. (let ((line (org-current-line))
  5713. (case-fold-search t)
  5714. (msg (substitute-command-keys
  5715. "Edit, then exit with C-c ' (C-c and single quote)"))
  5716. (org-mode-p (eq major-mode 'org-mode))
  5717. beg end)
  5718. (beginning-of-line 1)
  5719. (if (looking-at "[ \t]*[^:\n \t]")
  5720. nil
  5721. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5722. (setq beg (point) end beg)
  5723. (save-excursion
  5724. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5725. (setq beg (point-at-bol 2))
  5726. (setq beg (point))))
  5727. (save-excursion
  5728. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5729. (setq end (1- (match-beginning 0)))
  5730. (setq end (point))))
  5731. (goto-line line))
  5732. (if (get-buffer "*Org Edit Picture*")
  5733. (kill-buffer "*Org Edit Picture*"))
  5734. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5735. "*Org Edit Picture*"))
  5736. (narrow-to-region beg end)
  5737. (remove-text-properties beg end '(display nil invisible nil
  5738. intangible nil))
  5739. (when (fboundp 'font-lock-unfontify-region)
  5740. (font-lock-unfontify-region (point-min) (point-max)))
  5741. (cond
  5742. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5743. (fundamental-mode)
  5744. (artist-mode 1))
  5745. (t (funcall org-edit-fixed-width-region-mode)))
  5746. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5747. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5748. (set (make-local-variable 'org-edit-src-picture) t)
  5749. (goto-char (point-min))
  5750. (while (re-search-forward "^[ \t]*: ?" nil t)
  5751. (replace-match ""))
  5752. (goto-line line)
  5753. (org-exit-edit-mode)
  5754. (org-set-local 'header-line-format msg)
  5755. (message "%s" msg)
  5756. t)))
  5757. (defun org-edit-src-find-region-and-lang ()
  5758. "Find the region and language for a local edit.
  5759. Return a list with beginning and end of the region, a string representing
  5760. the language, a switch telling of the content should be in a single line."
  5761. (let ((re-list
  5762. (append
  5763. org-edit-src-region-extra
  5764. '(
  5765. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5766. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5767. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5768. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5769. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5770. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5771. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5772. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5773. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5774. ("^#\\+html:" "\n" "html" single-line)
  5775. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5776. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5777. ("^#\\+latex:" "\n" "latex" single-line)
  5778. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5779. ("^#\\+ascii:" "\n" "ascii" single-line)
  5780. )))
  5781. (pos (point))
  5782. re1 re2 single beg end lang lfmt match-re1)
  5783. (catch 'exit
  5784. (while (setq entry (pop re-list))
  5785. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5786. single (nth 3 entry))
  5787. (save-excursion
  5788. (if (or (looking-at re1)
  5789. (re-search-backward re1 nil t))
  5790. (progn
  5791. (setq match-re1 (match-string 0))
  5792. (setq beg (match-end 0)
  5793. lang (org-edit-src-get-lang lang)
  5794. lfmt (org-edit-src-get-label-format match-re1))
  5795. (if (and (re-search-forward re2 nil t)
  5796. (>= (match-end 0) pos))
  5797. (throw 'exit (list beg (match-beginning 0)
  5798. lang single lfmt))))
  5799. (if (or (looking-at re2)
  5800. (re-search-forward re2 nil t))
  5801. (progn
  5802. (setq end (match-beginning 0))
  5803. (if (and (re-search-backward re1 nil t)
  5804. (<= (match-beginning 0) pos))
  5805. (progn
  5806. (setq lfmt (org-edit-src-get-label-format
  5807. (match-string 0)))
  5808. (throw 'exit
  5809. (list (match-end 0) end
  5810. (org-edit-src-get-lang lang)
  5811. single lfmt))))))))))))
  5812. (defun org-edit-src-get-lang (lang)
  5813. "Extract the src language."
  5814. (let ((m (match-string 0)))
  5815. (cond
  5816. ((stringp lang) lang)
  5817. ((integerp lang) (match-string lang))
  5818. ((and (eq lang 'lang)
  5819. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5820. (match-string 1 m))
  5821. ((and (eq lang 'style)
  5822. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5823. (match-string 1 m))
  5824. (t "fundamental"))))
  5825. (defun org-edit-src-get-label-format (s)
  5826. "Extract the label format."
  5827. (save-match-data
  5828. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5829. (match-string 1 s))))
  5830. (defun org-edit-src-exit ()
  5831. "Exit special edit and protect problematic lines."
  5832. (interactive)
  5833. (unless (buffer-base-buffer (current-buffer))
  5834. (error "This is not an indirect buffer, something is wrong..."))
  5835. (unless (> (point-min) 1)
  5836. (error "This buffer is not narrowed, something is wrong..."))
  5837. (goto-char (point-min))
  5838. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5839. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5840. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5841. (goto-char (point-min))
  5842. (while (re-search-forward "\n" nil t)
  5843. (replace-match " "))
  5844. (goto-char (point-min))
  5845. (if (looking-at "\\s-*") (replace-match " "))
  5846. (if (re-search-forward "\\s-+\\'" nil t)
  5847. (replace-match "")))
  5848. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5849. (goto-char (point-min))
  5850. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5851. (replace-match ",\\1"))
  5852. (when font-lock-mode
  5853. (font-lock-unfontify-region (point-min) (point-max)))
  5854. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5855. (when (org-bound-and-true-p org-edit-src-picture)
  5856. (untabify (point-min) (point-max))
  5857. (goto-char (point-min))
  5858. (while (re-search-forward "^" nil t)
  5859. (replace-match ": "))
  5860. (when font-lock-mode
  5861. (font-lock-unfontify-region (point-min) (point-max)))
  5862. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5863. (kill-buffer (current-buffer))
  5864. (and (org-mode-p) (org-restart-font-lock)))
  5865. ;;; The orgstruct minor mode
  5866. ;; Define a minor mode which can be used in other modes in order to
  5867. ;; integrate the org-mode structure editing commands.
  5868. ;; This is really a hack, because the org-mode structure commands use
  5869. ;; keys which normally belong to the major mode. Here is how it
  5870. ;; works: The minor mode defines all the keys necessary to operate the
  5871. ;; structure commands, but wraps the commands into a function which
  5872. ;; tests if the cursor is currently at a headline or a plain list
  5873. ;; item. If that is the case, the structure command is used,
  5874. ;; temporarily setting many Org-mode variables like regular
  5875. ;; expressions for filling etc. However, when any of those keys is
  5876. ;; used at a different location, function uses `key-binding' to look
  5877. ;; up if the key has an associated command in another currently active
  5878. ;; keymap (minor modes, major mode, global), and executes that
  5879. ;; command. There might be problems if any of the keys is otherwise
  5880. ;; used as a prefix key.
  5881. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5882. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5883. ;; addresses this by checking explicitly for both bindings.
  5884. (defvar orgstruct-mode-map (make-sparse-keymap)
  5885. "Keymap for the minor `orgstruct-mode'.")
  5886. (defvar org-local-vars nil
  5887. "List of local variables, for use by `orgstruct-mode'")
  5888. ;;;###autoload
  5889. (define-minor-mode orgstruct-mode
  5890. "Toggle the minor more `orgstruct-mode'.
  5891. This mode is for using Org-mode structure commands in other modes.
  5892. The following key behave as if Org-mode was active, if the cursor
  5893. is on a headline, or on a plain list item (both in the definition
  5894. of Org-mode).
  5895. M-up Move entry/item up
  5896. M-down Move entry/item down
  5897. M-left Promote
  5898. M-right Demote
  5899. M-S-up Move entry/item up
  5900. M-S-down Move entry/item down
  5901. M-S-left Promote subtree
  5902. M-S-right Demote subtree
  5903. M-q Fill paragraph and items like in Org-mode
  5904. C-c ^ Sort entries
  5905. C-c - Cycle list bullet
  5906. TAB Cycle item visibility
  5907. M-RET Insert new heading/item
  5908. S-M-RET Insert new TODO heading / Checkbox item
  5909. C-c C-c Set tags / toggle checkbox"
  5910. nil " OrgStruct" nil
  5911. (org-load-modules-maybe)
  5912. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5913. ;;;###autoload
  5914. (defun turn-on-orgstruct ()
  5915. "Unconditionally turn on `orgstruct-mode'."
  5916. (orgstruct-mode 1))
  5917. (defun orgstruct++-mode (&optional arg)
  5918. "Toggle `orgstruct-mode', the enhanced version of it.
  5919. In addition to setting orgstruct-mode, this also exports all indentation and
  5920. autofilling variables from org-mode into the buffer. It will also
  5921. recognize item context in multiline items.
  5922. Note that turning off orgstruct-mode will *not* remove the
  5923. indentation/paragraph settings. This can only be done by refreshing the
  5924. major mode, for example with \[normal-mode]."
  5925. (interactive "P")
  5926. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5927. (if (< arg 1)
  5928. (orgstruct-mode -1)
  5929. (orgstruct-mode 1)
  5930. (let (var val)
  5931. (mapc
  5932. (lambda (x)
  5933. (when (string-match
  5934. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5935. (symbol-name (car x)))
  5936. (setq var (car x) val (nth 1 x))
  5937. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5938. org-local-vars)
  5939. (org-set-local 'orgstruct-is-++ t))))
  5940. (defvar orgstruct-is-++ nil
  5941. "Is orgstruct-mode in ++ version in the current-buffer?")
  5942. (make-variable-buffer-local 'orgstruct-is-++)
  5943. ;;;###autoload
  5944. (defun turn-on-orgstruct++ ()
  5945. "Unconditionally turn on `orgstruct++-mode'."
  5946. (orgstruct++-mode 1))
  5947. (defun orgstruct-error ()
  5948. "Error when there is no default binding for a structure key."
  5949. (interactive)
  5950. (error "This key has no function outside structure elements"))
  5951. (defun orgstruct-setup ()
  5952. "Setup orgstruct keymaps."
  5953. (let ((nfunc 0)
  5954. (bindings
  5955. (list
  5956. '([(meta up)] org-metaup)
  5957. '([(meta down)] org-metadown)
  5958. '([(meta left)] org-metaleft)
  5959. '([(meta right)] org-metaright)
  5960. '([(meta shift up)] org-shiftmetaup)
  5961. '([(meta shift down)] org-shiftmetadown)
  5962. '([(meta shift left)] org-shiftmetaleft)
  5963. '([(meta shift right)] org-shiftmetaright)
  5964. '([(shift up)] org-shiftup)
  5965. '([(shift down)] org-shiftdown)
  5966. '([(shift left)] org-shiftleft)
  5967. '([(shift right)] org-shiftright)
  5968. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5969. '("\M-q" fill-paragraph)
  5970. '("\C-c^" org-sort)
  5971. '("\C-c-" org-cycle-list-bullet)))
  5972. elt key fun cmd)
  5973. (while (setq elt (pop bindings))
  5974. (setq nfunc (1+ nfunc))
  5975. (setq key (org-key (car elt))
  5976. fun (nth 1 elt)
  5977. cmd (orgstruct-make-binding fun nfunc key))
  5978. (org-defkey orgstruct-mode-map key cmd))
  5979. ;; Special treatment needed for TAB and RET
  5980. (org-defkey orgstruct-mode-map [(tab)]
  5981. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5982. (org-defkey orgstruct-mode-map "\C-i"
  5983. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5984. (org-defkey orgstruct-mode-map "\M-\C-m"
  5985. (orgstruct-make-binding 'org-insert-heading 105
  5986. "\M-\C-m" [(meta return)]))
  5987. (org-defkey orgstruct-mode-map [(meta return)]
  5988. (orgstruct-make-binding 'org-insert-heading 106
  5989. [(meta return)] "\M-\C-m"))
  5990. (org-defkey orgstruct-mode-map [(shift meta return)]
  5991. (orgstruct-make-binding 'org-insert-todo-heading 107
  5992. [(meta return)] "\M-\C-m"))
  5993. (unless org-local-vars
  5994. (setq org-local-vars (org-get-local-variables)))
  5995. t))
  5996. (defun orgstruct-make-binding (fun n &rest keys)
  5997. "Create a function for binding in the structure minor mode.
  5998. FUN is the command to call inside a table. N is used to create a unique
  5999. command name. KEYS are keys that should be checked in for a command
  6000. to execute outside of tables."
  6001. (eval
  6002. (list 'defun
  6003. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6004. '(arg)
  6005. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6006. "Outside of structure, run the binding of `"
  6007. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6008. "'.")
  6009. '(interactive "p")
  6010. (list 'if
  6011. `(org-context-p 'headline 'item
  6012. (and orgstruct-is-++
  6013. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6014. 'item-body))
  6015. (list 'org-run-like-in-org-mode (list 'quote fun))
  6016. (list 'let '(orgstruct-mode)
  6017. (list 'call-interactively
  6018. (append '(or)
  6019. (mapcar (lambda (k)
  6020. (list 'key-binding k))
  6021. keys)
  6022. '('orgstruct-error))))))))
  6023. (defun org-context-p (&rest contexts)
  6024. "Check if local context is any of CONTEXTS.
  6025. Possible values in the list of contexts are `table', `headline', and `item'."
  6026. (let ((pos (point)))
  6027. (goto-char (point-at-bol))
  6028. (prog1 (or (and (memq 'table contexts)
  6029. (looking-at "[ \t]*|"))
  6030. (and (memq 'headline contexts)
  6031. ;;????????? (looking-at "\\*+"))
  6032. (looking-at outline-regexp))
  6033. (and (memq 'item contexts)
  6034. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6035. (and (memq 'item-body contexts)
  6036. (org-in-item-p)))
  6037. (goto-char pos))))
  6038. (defun org-get-local-variables ()
  6039. "Return a list of all local variables in an org-mode buffer."
  6040. (let (varlist)
  6041. (with-current-buffer (get-buffer-create "*Org tmp*")
  6042. (erase-buffer)
  6043. (org-mode)
  6044. (setq varlist (buffer-local-variables)))
  6045. (kill-buffer "*Org tmp*")
  6046. (delq nil
  6047. (mapcar
  6048. (lambda (x)
  6049. (setq x
  6050. (if (symbolp x)
  6051. (list x)
  6052. (list (car x) (list 'quote (cdr x)))))
  6053. (if (string-match
  6054. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6055. (symbol-name (car x)))
  6056. x nil))
  6057. varlist))))
  6058. ;;;###autoload
  6059. (defun org-run-like-in-org-mode (cmd)
  6060. "Run a command, pretending that the current buffer is in Org-mode.
  6061. This will temporarily bind local variables that are typically bound in
  6062. Org-mode to the values they have in Org-mode, and then interactively
  6063. call CMD."
  6064. (org-load-modules-maybe)
  6065. (unless org-local-vars
  6066. (setq org-local-vars (org-get-local-variables)))
  6067. (eval (list 'let org-local-vars
  6068. (list 'call-interactively (list 'quote cmd)))))
  6069. ;;;; Archiving
  6070. (defun org-get-category (&optional pos)
  6071. "Get the category applying to position POS."
  6072. (get-text-property (or pos (point)) 'org-category))
  6073. (defun org-refresh-category-properties ()
  6074. "Refresh category text properties in the buffer."
  6075. (let ((def-cat (cond
  6076. ((null org-category)
  6077. (if buffer-file-name
  6078. (file-name-sans-extension
  6079. (file-name-nondirectory buffer-file-name))
  6080. "???"))
  6081. ((symbolp org-category) (symbol-name org-category))
  6082. (t org-category)))
  6083. beg end cat pos optionp)
  6084. (org-unmodified
  6085. (save-excursion
  6086. (save-restriction
  6087. (widen)
  6088. (goto-char (point-min))
  6089. (put-text-property (point) (point-max) 'org-category def-cat)
  6090. (while (re-search-forward
  6091. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6092. (setq pos (match-end 0)
  6093. optionp (equal (char-after (match-beginning 0)) ?#)
  6094. cat (org-trim (match-string 2)))
  6095. (if optionp
  6096. (setq beg (point-at-bol) end (point-max))
  6097. (org-back-to-heading t)
  6098. (setq beg (point) end (org-end-of-subtree t t)))
  6099. (put-text-property beg end 'org-category cat)
  6100. (goto-char pos)))))))
  6101. ;;;; Link Stuff
  6102. ;;; Link abbreviations
  6103. (defun org-link-expand-abbrev (link)
  6104. "Apply replacements as defined in `org-link-abbrev-alist."
  6105. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6106. (let* ((key (match-string 1 link))
  6107. (as (or (assoc key org-link-abbrev-alist-local)
  6108. (assoc key org-link-abbrev-alist)))
  6109. (tag (and (match-end 2) (match-string 3 link)))
  6110. rpl)
  6111. (if (not as)
  6112. link
  6113. (setq rpl (cdr as))
  6114. (cond
  6115. ((symbolp rpl) (funcall rpl tag))
  6116. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6117. ((string-match "%h" rpl)
  6118. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6119. (t (concat rpl tag)))))
  6120. link))
  6121. ;;; Storing and inserting links
  6122. (defvar org-insert-link-history nil
  6123. "Minibuffer history for links inserted with `org-insert-link'.")
  6124. (defvar org-stored-links nil
  6125. "Contains the links stored with `org-store-link'.")
  6126. (defvar org-store-link-plist nil
  6127. "Plist with info about the most recently link created with `org-store-link'.")
  6128. (defvar org-link-protocols nil
  6129. "Link protocols added to Org-mode using `org-add-link-type'.")
  6130. (defvar org-store-link-functions nil
  6131. "List of functions that are called to create and store a link.
  6132. Each function will be called in turn until one returns a non-nil
  6133. value. Each function should check if it is responsible for creating
  6134. this link (for example by looking at the major mode).
  6135. If not, it must exit and return nil.
  6136. If yes, it should return a non-nil value after a calling
  6137. `org-store-link-props' with a list of properties and values.
  6138. Special properties are:
  6139. :type The link prefix. like \"http\". This must be given.
  6140. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6141. This is obligatory as well.
  6142. :description Optional default description for the second pair
  6143. of brackets in an Org-mode link. The user can still change
  6144. this when inserting this link into an Org-mode buffer.
  6145. In addition to these, any additional properties can be specified
  6146. and then used in remember templates.")
  6147. (defun org-add-link-type (type &optional follow export)
  6148. "Add TYPE to the list of `org-link-types'.
  6149. Re-compute all regular expressions depending on `org-link-types'
  6150. FOLLOW and EXPORT are two functions.
  6151. FOLLOW should take the link path as the single argument and do whatever
  6152. is necessary to follow the link, for example find a file or display
  6153. a mail message.
  6154. EXPORT should format the link path for export to one of the export formats.
  6155. It should be a function accepting three arguments:
  6156. path the path of the link, the text after the prefix (like \"http:\")
  6157. desc the description of the link, if any, nil if there was no description
  6158. format the export format, a symbol like `html' or `latex'.
  6159. The function may use the FORMAT information to return different values
  6160. depending on the format. The return value will be put literally into
  6161. the exported file.
  6162. Org-mode has a built-in default for exporting links. If you are happy with
  6163. this default, there is no need to define an export function for the link
  6164. type. For a simple example of an export function, see `org-bbdb.el'."
  6165. (add-to-list 'org-link-types type t)
  6166. (org-make-link-regexps)
  6167. (if (assoc type org-link-protocols)
  6168. (setcdr (assoc type org-link-protocols) (list follow export))
  6169. (push (list type follow export) org-link-protocols)))
  6170. ;;;###autoload
  6171. (defun org-store-link (arg)
  6172. "\\<org-mode-map>Store an org-link to the current location.
  6173. This link is added to `org-stored-links' and can later be inserted
  6174. into an org-buffer with \\[org-insert-link].
  6175. For some link types, a prefix arg is interpreted:
  6176. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6177. For file links, arg negates `org-context-in-file-links'."
  6178. (interactive "P")
  6179. (org-load-modules-maybe)
  6180. (setq org-store-link-plist nil) ; reset
  6181. (let (link cpltxt desc description search txt custom-id)
  6182. (cond
  6183. ((run-hook-with-args-until-success 'org-store-link-functions)
  6184. (setq link (plist-get org-store-link-plist :link)
  6185. desc (or (plist-get org-store-link-plist :description) link)))
  6186. ((equal (buffer-name) "*Org Edit Src Example*")
  6187. (let (label gc)
  6188. (while (or (not label)
  6189. (save-excursion
  6190. (save-restriction
  6191. (widen)
  6192. (goto-char (point-min))
  6193. (re-search-forward
  6194. (regexp-quote (format org-coderef-label-format label))
  6195. nil t))))
  6196. (when label (message "Label exists already") (sit-for 2))
  6197. (setq label (read-string "Code line label: " label)))
  6198. (end-of-line 1)
  6199. (setq link (format org-coderef-label-format label))
  6200. (setq gc (- 79 (length link)))
  6201. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6202. (insert link)
  6203. (setq link (concat "(" label ")") desc nil)))
  6204. ((eq major-mode 'calendar-mode)
  6205. (let ((cd (calendar-cursor-to-date)))
  6206. (setq link
  6207. (format-time-string
  6208. (car org-time-stamp-formats)
  6209. (apply 'encode-time
  6210. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6211. nil nil nil))))
  6212. (org-store-link-props :type "calendar" :date cd)))
  6213. ((eq major-mode 'w3-mode)
  6214. (setq cpltxt (if (and (buffer-name)
  6215. (not (string-match "Untitled" (buffer-name))))
  6216. (buffer-name)
  6217. (url-view-url t))
  6218. link (org-make-link (url-view-url t)))
  6219. (org-store-link-props :type "w3" :url (url-view-url t)))
  6220. ((eq major-mode 'w3m-mode)
  6221. (setq cpltxt (or w3m-current-title w3m-current-url)
  6222. link (org-make-link w3m-current-url))
  6223. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6224. ((setq search (run-hook-with-args-until-success
  6225. 'org-create-file-search-functions))
  6226. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6227. "::" search))
  6228. (setq cpltxt (or description link)))
  6229. ((eq major-mode 'image-mode)
  6230. (setq cpltxt (concat "file:"
  6231. (abbreviate-file-name buffer-file-name))
  6232. link (org-make-link cpltxt))
  6233. (org-store-link-props :type "image" :file buffer-file-name))
  6234. ((eq major-mode 'dired-mode)
  6235. ;; link to the file in the current line
  6236. (setq cpltxt (concat "file:"
  6237. (abbreviate-file-name
  6238. (expand-file-name
  6239. (dired-get-filename nil t))))
  6240. link (org-make-link cpltxt)))
  6241. ((and buffer-file-name (org-mode-p))
  6242. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  6243. (cond
  6244. ((org-in-regexp "<<\\(.*?\\)>>")
  6245. (setq cpltxt
  6246. (concat "file:"
  6247. (abbreviate-file-name buffer-file-name)
  6248. "::" (match-string 1))
  6249. link (org-make-link cpltxt)))
  6250. ((and (featurep 'org-id)
  6251. (or (eq org-link-to-org-use-id t)
  6252. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6253. (interactive-p))
  6254. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6255. (interactive-p)
  6256. (not custom-id))
  6257. (and org-link-to-org-use-id
  6258. (condition-case nil
  6259. (org-entry-get nil "ID")
  6260. (error nil)))))
  6261. ;; We can make a link using the ID.
  6262. (setq link (condition-case nil
  6263. (prog1 (org-id-store-link)
  6264. (setq desc (plist-get org-store-link-plist
  6265. :description)))
  6266. (error
  6267. ;; probably before first headline, link to file only
  6268. (concat "file:"
  6269. (abbreviate-file-name buffer-file-name))))))
  6270. (t
  6271. ;; Just link to current headline
  6272. (setq cpltxt (concat "file:"
  6273. (abbreviate-file-name buffer-file-name)))
  6274. ;; Add a context search string
  6275. (when (org-xor org-context-in-file-links arg)
  6276. (setq txt (cond
  6277. ((org-on-heading-p) nil)
  6278. ((org-region-active-p)
  6279. (buffer-substring (region-beginning) (region-end)))
  6280. (t nil)))
  6281. (when (or (null txt) (string-match "\\S-" txt))
  6282. (setq cpltxt
  6283. (concat cpltxt "::"
  6284. (condition-case nil
  6285. (org-make-org-heading-search-string txt)
  6286. (error "")))
  6287. desc "NONE")))
  6288. (if (string-match "::\\'" cpltxt)
  6289. (setq cpltxt (substring cpltxt 0 -2)))
  6290. (setq link (org-make-link cpltxt)))))
  6291. ((buffer-file-name (buffer-base-buffer))
  6292. ;; Just link to this file here.
  6293. (setq cpltxt (concat "file:"
  6294. (abbreviate-file-name
  6295. (buffer-file-name (buffer-base-buffer)))))
  6296. ;; Add a context string
  6297. (when (org-xor org-context-in-file-links arg)
  6298. (setq txt (if (org-region-active-p)
  6299. (buffer-substring (region-beginning) (region-end))
  6300. (buffer-substring (point-at-bol) (point-at-eol))))
  6301. ;; Only use search option if there is some text.
  6302. (when (string-match "\\S-" txt)
  6303. (setq cpltxt
  6304. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6305. desc "NONE")))
  6306. (setq link (org-make-link cpltxt)))
  6307. ((interactive-p)
  6308. (error "Cannot link to a buffer which is not visiting a file"))
  6309. (t (setq link nil)))
  6310. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6311. (setq link (or link cpltxt)
  6312. desc (or desc cpltxt))
  6313. (if (equal desc "NONE") (setq desc nil))
  6314. (if (and (interactive-p) link)
  6315. (progn
  6316. (setq org-stored-links
  6317. (cons (list link desc) org-stored-links))
  6318. (message "Stored: %s" (or desc link))
  6319. (when custom-id
  6320. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6321. "::#" custom-id))
  6322. (setq org-stored-links
  6323. (cons (list link desc) org-stored-links))))
  6324. (and link (org-make-link-string link desc)))))
  6325. (defun org-store-link-props (&rest plist)
  6326. "Store link properties, extract names and addresses."
  6327. (let (x adr)
  6328. (when (setq x (plist-get plist :from))
  6329. (setq adr (mail-extract-address-components x))
  6330. (setq plist (plist-put plist :fromname (car adr)))
  6331. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6332. (when (setq x (plist-get plist :to))
  6333. (setq adr (mail-extract-address-components x))
  6334. (setq plist (plist-put plist :toname (car adr)))
  6335. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6336. (let ((from (plist-get plist :from))
  6337. (to (plist-get plist :to)))
  6338. (when (and from to org-from-is-user-regexp)
  6339. (setq plist
  6340. (plist-put plist :fromto
  6341. (if (string-match org-from-is-user-regexp from)
  6342. (concat "to %t")
  6343. (concat "from %f"))))))
  6344. (setq org-store-link-plist plist))
  6345. (defun org-add-link-props (&rest plist)
  6346. "Add these properties to the link property list."
  6347. (let (key value)
  6348. (while plist
  6349. (setq key (pop plist) value (pop plist))
  6350. (setq org-store-link-plist
  6351. (plist-put org-store-link-plist key value)))))
  6352. (defun org-email-link-description (&optional fmt)
  6353. "Return the description part of an email link.
  6354. This takes information from `org-store-link-plist' and formats it
  6355. according to FMT (default from `org-email-link-description-format')."
  6356. (setq fmt (or fmt org-email-link-description-format))
  6357. (let* ((p org-store-link-plist)
  6358. (to (plist-get p :toaddress))
  6359. (from (plist-get p :fromaddress))
  6360. (table
  6361. (list
  6362. (cons "%c" (plist-get p :fromto))
  6363. (cons "%F" (plist-get p :from))
  6364. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6365. (cons "%T" (plist-get p :to))
  6366. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6367. (cons "%s" (plist-get p :subject))
  6368. (cons "%m" (plist-get p :message-id)))))
  6369. (when (string-match "%c" fmt)
  6370. ;; Check if the user wrote this message
  6371. (if (and org-from-is-user-regexp from to
  6372. (save-match-data (string-match org-from-is-user-regexp from)))
  6373. (setq fmt (replace-match "to %t" t t fmt))
  6374. (setq fmt (replace-match "from %f" t t fmt))))
  6375. (org-replace-escapes fmt table)))
  6376. (defun org-make-org-heading-search-string (&optional string heading)
  6377. "Make search string for STRING or current headline."
  6378. (interactive)
  6379. (let ((s (or string (org-get-heading))))
  6380. (unless (and string (not heading))
  6381. ;; We are using a headline, clean up garbage in there.
  6382. (if (string-match org-todo-regexp s)
  6383. (setq s (replace-match "" t t s)))
  6384. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6385. (setq s (replace-match "" t t s)))
  6386. (setq s (org-trim s))
  6387. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6388. org-comment-string "\\)") s)
  6389. (setq s (replace-match "" t t s)))
  6390. (while (string-match org-ts-regexp s)
  6391. (setq s (replace-match "" t t s))))
  6392. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6393. (setq s (replace-match " " t t s)))
  6394. (or string (setq s (concat "*" s))) ; Add * for headlines
  6395. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6396. (defun org-make-link (&rest strings)
  6397. "Concatenate STRINGS."
  6398. (apply 'concat strings))
  6399. (defun org-make-link-string (link &optional description)
  6400. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6401. (unless (string-match "\\S-" link)
  6402. (error "Empty link"))
  6403. (when (stringp description)
  6404. ;; Remove brackets from the description, they are fatal.
  6405. (while (string-match "\\[" description)
  6406. (setq description (replace-match "{" t t description)))
  6407. (while (string-match "\\]" description)
  6408. (setq description (replace-match "}" t t description))))
  6409. (when (equal (org-link-escape link) description)
  6410. ;; No description needed, it is identical
  6411. (setq description nil))
  6412. (when (and (not description)
  6413. (not (equal link (org-link-escape link))))
  6414. (setq description (org-extract-attributes link)))
  6415. (concat "[[" (org-link-escape link) "]"
  6416. (if description (concat "[" description "]") "")
  6417. "]"))
  6418. (defconst org-link-escape-chars
  6419. '((?\ . "%20")
  6420. (?\[ . "%5B")
  6421. (?\] . "%5D")
  6422. (?\340 . "%E0") ; `a
  6423. (?\342 . "%E2") ; ^a
  6424. (?\347 . "%E7") ; ,c
  6425. (?\350 . "%E8") ; `e
  6426. (?\351 . "%E9") ; 'e
  6427. (?\352 . "%EA") ; ^e
  6428. (?\356 . "%EE") ; ^i
  6429. (?\364 . "%F4") ; ^o
  6430. (?\371 . "%F9") ; `u
  6431. (?\373 . "%FB") ; ^u
  6432. (?\; . "%3B")
  6433. (?? . "%3F")
  6434. (?= . "%3D")
  6435. (?+ . "%2B")
  6436. )
  6437. "Association list of escapes for some characters problematic in links.
  6438. This is the list that is used for internal purposes.")
  6439. (defvar org-url-encoding-use-url-hexify nil)
  6440. (defconst org-link-escape-chars-browser
  6441. '((?\ . "%20")) ; 32 for the SPC char
  6442. "Association list of escapes for some characters problematic in links.
  6443. This is the list that is used before handing over to the browser.")
  6444. (defun org-link-escape (text &optional table)
  6445. "Escape characters in TEXT that are problematic for links."
  6446. (if org-url-encoding-use-url-hexify
  6447. (url-hexify-string text)
  6448. (setq table (or table org-link-escape-chars))
  6449. (when text
  6450. (let ((re (mapconcat (lambda (x) (regexp-quote
  6451. (char-to-string (car x))))
  6452. table "\\|")))
  6453. (while (string-match re text)
  6454. (setq text
  6455. (replace-match
  6456. (cdr (assoc (string-to-char (match-string 0 text))
  6457. table))
  6458. t t text)))
  6459. text))))
  6460. (defun org-link-unescape (text &optional table)
  6461. "Reverse the action of `org-link-escape'."
  6462. (if org-url-encoding-use-url-hexify
  6463. (url-unhex-string text)
  6464. (setq table (or table org-link-escape-chars))
  6465. (when text
  6466. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6467. table "\\|")))
  6468. (while (string-match re text)
  6469. (setq text
  6470. (replace-match
  6471. (char-to-string (car (rassoc (match-string 0 text) table)))
  6472. t t text)))
  6473. text))))
  6474. (defun org-xor (a b)
  6475. "Exclusive or."
  6476. (if a (not b) b))
  6477. (defun org-fixup-message-id-for-http (s)
  6478. "Replace special characters in a message id, so it can be used in an http query."
  6479. (while (string-match "<" s)
  6480. (setq s (replace-match "%3C" t t s)))
  6481. (while (string-match ">" s)
  6482. (setq s (replace-match "%3E" t t s)))
  6483. (while (string-match "@" s)
  6484. (setq s (replace-match "%40" t t s)))
  6485. s)
  6486. ;;;###autoload
  6487. (defun org-insert-link-global ()
  6488. "Insert a link like Org-mode does.
  6489. This command can be called in any mode to insert a link in Org-mode syntax."
  6490. (interactive)
  6491. (org-load-modules-maybe)
  6492. (org-run-like-in-org-mode 'org-insert-link))
  6493. (defun org-insert-link (&optional complete-file link-location)
  6494. "Insert a link. At the prompt, enter the link.
  6495. Completion can be used to insert any of the link protocol prefixes like
  6496. http or ftp in use.
  6497. The history can be used to select a link previously stored with
  6498. `org-store-link'. When the empty string is entered (i.e. if you just
  6499. press RET at the prompt), the link defaults to the most recently
  6500. stored link. As SPC triggers completion in the minibuffer, you need to
  6501. use M-SPC or C-q SPC to force the insertion of a space character.
  6502. You will also be prompted for a description, and if one is given, it will
  6503. be displayed in the buffer instead of the link.
  6504. If there is already a link at point, this command will allow you to edit link
  6505. and description parts.
  6506. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6507. be selected using completion. The path to the file will be relative to the
  6508. current directory if the file is in the current directory or a subdirectory.
  6509. Otherwise, the link will be the absolute path as completed in the minibuffer
  6510. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6511. option `org-link-file-path-type'.
  6512. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6513. the current directory or below.
  6514. With three \\[universal-argument] prefixes, negate the meaning of
  6515. `org-keep-stored-link-after-insertion'.
  6516. If `org-make-link-description-function' is non-nil, this function will be
  6517. called with the link target, and the result will be the default
  6518. link description.
  6519. If the LINK-LOCATION parameter is non-nil, this value will be
  6520. used as the link location instead of reading one interactively."
  6521. (interactive "P")
  6522. (let* ((wcf (current-window-configuration))
  6523. (region (if (org-region-active-p)
  6524. (buffer-substring (region-beginning) (region-end))))
  6525. (remove (and region (list (region-beginning) (region-end))))
  6526. (desc region)
  6527. tmphist ; byte-compile incorrectly complains about this
  6528. (link link-location)
  6529. entry file)
  6530. (cond
  6531. (link-location) ; specified by arg, just use it.
  6532. ((org-in-regexp org-bracket-link-regexp 1)
  6533. ;; We do have a link at point, and we are going to edit it.
  6534. (setq remove (list (match-beginning 0) (match-end 0)))
  6535. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6536. (setq link (read-string "Link: "
  6537. (org-link-unescape
  6538. (org-match-string-no-properties 1)))))
  6539. ((or (org-in-regexp org-angle-link-re)
  6540. (org-in-regexp org-plain-link-re))
  6541. ;; Convert to bracket link
  6542. (setq remove (list (match-beginning 0) (match-end 0))
  6543. link (read-string "Link: "
  6544. (org-remove-angle-brackets (match-string 0)))))
  6545. ((member complete-file '((4) (16)))
  6546. ;; Completing read for file names.
  6547. (setq file (read-file-name "File: "))
  6548. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6549. (pwd1 (file-name-as-directory (abbreviate-file-name
  6550. (expand-file-name ".")))))
  6551. (cond
  6552. ((equal complete-file '(16))
  6553. (setq link (org-make-link
  6554. "file:"
  6555. (abbreviate-file-name (expand-file-name file)))))
  6556. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6557. (setq link (org-make-link "file:" (match-string 1 file))))
  6558. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6559. (expand-file-name file))
  6560. (setq link (org-make-link
  6561. "file:" (match-string 1 (expand-file-name file)))))
  6562. (t (setq link (org-make-link "file:" file))))))
  6563. (t
  6564. ;; Read link, with completion for stored links.
  6565. (with-output-to-temp-buffer "*Org Links*"
  6566. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6567. (when org-stored-links
  6568. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6569. (princ (mapconcat
  6570. (lambda (x)
  6571. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6572. (reverse org-stored-links) "\n"))))
  6573. (let ((cw (selected-window)))
  6574. (select-window (get-buffer-window "*Org Links*"))
  6575. (setq truncate-lines t)
  6576. (org-fit-window-to-buffer)
  6577. (select-window cw))
  6578. ;; Fake a link history, containing the stored links.
  6579. (setq tmphist (append (mapcar 'car org-stored-links)
  6580. org-insert-link-history))
  6581. (unwind-protect
  6582. (setq link
  6583. (let ((org-completion-use-ido nil))
  6584. (org-completing-read
  6585. "Link: "
  6586. (append
  6587. (mapcar (lambda (x) (list (concat (car x) ":")))
  6588. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6589. (mapcar (lambda (x) (list (concat x ":")))
  6590. org-link-types))
  6591. nil nil nil
  6592. 'tmphist
  6593. (or (car (car org-stored-links))))))
  6594. (set-window-configuration wcf)
  6595. (kill-buffer "*Org Links*"))
  6596. (setq entry (assoc link org-stored-links))
  6597. (or entry (push link org-insert-link-history))
  6598. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6599. (not org-keep-stored-link-after-insertion))
  6600. (setq org-stored-links (delq (assoc link org-stored-links)
  6601. org-stored-links)))
  6602. (setq desc (or desc (nth 1 entry)))))
  6603. (if (string-match org-plain-link-re link)
  6604. ;; URL-like link, normalize the use of angular brackets.
  6605. (setq link (org-make-link (org-remove-angle-brackets link))))
  6606. ;; Check if we are linking to the current file with a search option
  6607. ;; If yes, simplify the link by using only the search option.
  6608. (when (and buffer-file-name
  6609. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6610. (let* ((path (match-string 1 link))
  6611. (case-fold-search nil)
  6612. (search (match-string 2 link)))
  6613. (save-match-data
  6614. (if (equal (file-truename buffer-file-name) (file-truename path))
  6615. ;; We are linking to this same file, with a search option
  6616. (setq link search)))))
  6617. ;; Check if we can/should use a relative path. If yes, simplify the link
  6618. (when (string-match "^file:\\(.*\\)" link)
  6619. (let* ((path (match-string 1 link))
  6620. (origpath path)
  6621. (case-fold-search nil))
  6622. (cond
  6623. ((or (eq org-link-file-path-type 'absolute)
  6624. (equal complete-file '(16)))
  6625. (setq path (abbreviate-file-name (expand-file-name path))))
  6626. ((eq org-link-file-path-type 'noabbrev)
  6627. (setq path (expand-file-name path)))
  6628. ((eq org-link-file-path-type 'relative)
  6629. (setq path (file-relative-name path)))
  6630. (t
  6631. (save-match-data
  6632. (if (string-match (concat "^" (regexp-quote
  6633. (file-name-as-directory
  6634. (expand-file-name "."))))
  6635. (expand-file-name path))
  6636. ;; We are linking a file with relative path name.
  6637. (setq path (substring (expand-file-name path)
  6638. (match-end 0)))
  6639. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6640. (setq link (concat "file:" path))
  6641. (if (equal desc origpath)
  6642. (setq desc path))))
  6643. (if org-make-link-description-function
  6644. (setq desc (funcall org-make-link-description-function link desc)))
  6645. (setq desc (read-string "Description: " desc))
  6646. (unless (string-match "\\S-" desc) (setq desc nil))
  6647. (if remove (apply 'delete-region remove))
  6648. (insert (org-make-link-string link desc))))
  6649. (defun org-completing-read (&rest args)
  6650. "Completing-read with SPACE being a normal character."
  6651. (let ((minibuffer-local-completion-map
  6652. (copy-keymap minibuffer-local-completion-map)))
  6653. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6654. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6655. (apply 'org-ido-completing-read args)))
  6656. (defun org-completing-read-no-ido (&rest args)
  6657. (let (org-completion-use-ido)
  6658. (apply 'org-completing-read args)))
  6659. (defun org-ido-completing-read (&rest args)
  6660. "Completing-read using `ido-mode' speedups if available"
  6661. (if (and org-completion-use-ido
  6662. (fboundp 'ido-completing-read)
  6663. (boundp 'ido-mode) ido-mode
  6664. (listp (second args)))
  6665. (let ((ido-enter-matching-directory nil))
  6666. (apply 'ido-completing-read (concat (car args))
  6667. (mapcar (lambda (x) (car x)) (nth 1 args))
  6668. (cddr args)))
  6669. (apply 'completing-read args)))
  6670. (defun org-extract-attributes (s)
  6671. "Extract the attributes cookie from a string and set as text property."
  6672. (let (a attr (start 0) key value)
  6673. (save-match-data
  6674. (when (string-match "{{\\([^}]+\\)}}$" s)
  6675. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6676. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6677. (setq key (match-string 1 a) value (match-string 2 a)
  6678. start (match-end 0)
  6679. attr (plist-put attr (intern key) value))))
  6680. (org-add-props s nil 'org-attr attr))
  6681. s))
  6682. (defun org-extract-attributes-from-string (tag)
  6683. (let (key value attr)
  6684. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6685. (setq key (match-string 1 tag) value (match-string 2 tag)
  6686. tag (replace-match "" t t tag)
  6687. attr (plist-put attr (intern key) value)))
  6688. (cons tag attr)))
  6689. (defun org-attributes-to-string (plist)
  6690. "Format a property list into an HTML attribute list."
  6691. (let ((s "") key value)
  6692. (while plist
  6693. (setq key (pop plist) value (pop plist))
  6694. (and value
  6695. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6696. s))
  6697. ;;; Opening/following a link
  6698. (defvar org-link-search-failed nil)
  6699. (defun org-next-link ()
  6700. "Move forward to the next link.
  6701. If the link is in hidden text, expose it."
  6702. (interactive)
  6703. (when (and org-link-search-failed (eq this-command last-command))
  6704. (goto-char (point-min))
  6705. (message "Link search wrapped back to beginning of buffer"))
  6706. (setq org-link-search-failed nil)
  6707. (let* ((pos (point))
  6708. (ct (org-context))
  6709. (a (assoc :link ct)))
  6710. (if a (goto-char (nth 2 a)))
  6711. (if (re-search-forward org-any-link-re nil t)
  6712. (progn
  6713. (goto-char (match-beginning 0))
  6714. (if (org-invisible-p) (org-show-context)))
  6715. (goto-char pos)
  6716. (setq org-link-search-failed t)
  6717. (error "No further link found"))))
  6718. (defun org-previous-link ()
  6719. "Move backward to the previous link.
  6720. If the link is in hidden text, expose it."
  6721. (interactive)
  6722. (when (and org-link-search-failed (eq this-command last-command))
  6723. (goto-char (point-max))
  6724. (message "Link search wrapped back to end of buffer"))
  6725. (setq org-link-search-failed nil)
  6726. (let* ((pos (point))
  6727. (ct (org-context))
  6728. (a (assoc :link ct)))
  6729. (if a (goto-char (nth 1 a)))
  6730. (if (re-search-backward org-any-link-re nil t)
  6731. (progn
  6732. (goto-char (match-beginning 0))
  6733. (if (org-invisible-p) (org-show-context)))
  6734. (goto-char pos)
  6735. (setq org-link-search-failed t)
  6736. (error "No further link found"))))
  6737. (defun org-translate-link (s)
  6738. "Translate a link string if a translation function has been defined."
  6739. (if (and org-link-translation-function
  6740. (fboundp org-link-translation-function)
  6741. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6742. (progn
  6743. (setq s (funcall org-link-translation-function
  6744. (match-string 1) (match-string 2)))
  6745. (concat (car s) ":" (cdr s)))
  6746. s))
  6747. (defun org-translate-link-from-planner (type path)
  6748. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6749. This is still an experimental function, your mileage may vary."
  6750. (cond
  6751. ((member type '("http" "https" "news" "ftp"))
  6752. ;; standard Internet links are the same.
  6753. nil)
  6754. ((and (equal type "irc") (string-match "^//" path))
  6755. ;; Planner has two / at the beginning of an irc link, we have 1.
  6756. ;; We should have zero, actually....
  6757. (setq path (substring path 1)))
  6758. ((and (equal type "lisp") (string-match "^/" path))
  6759. ;; Planner has a slash, we do not.
  6760. (setq type "elisp" path (substring path 1)))
  6761. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6762. ;; A typical message link. Planner has the id after the fina slash,
  6763. ;; we separate it with a hash mark
  6764. (setq path (concat (match-string 1 path) "#"
  6765. (org-remove-angle-brackets (match-string 2 path)))))
  6766. )
  6767. (cons type path))
  6768. (defun org-find-file-at-mouse (ev)
  6769. "Open file link or URL at mouse."
  6770. (interactive "e")
  6771. (mouse-set-point ev)
  6772. (org-open-at-point 'in-emacs))
  6773. (defun org-open-at-mouse (ev)
  6774. "Open file link or URL at mouse."
  6775. (interactive "e")
  6776. (mouse-set-point ev)
  6777. (if (eq major-mode 'org-agenda-mode)
  6778. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6779. (org-open-at-point))
  6780. (defvar org-window-config-before-follow-link nil
  6781. "The window configuration before following a link.
  6782. This is saved in case the need arises to restore it.")
  6783. (defvar org-open-link-marker (make-marker)
  6784. "Marker pointing to the location where `org-open-at-point; was called.")
  6785. ;;;###autoload
  6786. (defun org-open-at-point-global ()
  6787. "Follow a link like Org-mode does.
  6788. This command can be called in any mode to follow a link that has
  6789. Org-mode syntax."
  6790. (interactive)
  6791. (org-run-like-in-org-mode 'org-open-at-point))
  6792. ;;;###autoload
  6793. (defun org-open-link-from-string (s &optional arg)
  6794. "Open a link in the string S, as if it was in Org-mode."
  6795. (interactive "sLink: \nP")
  6796. (let ((reference-buffer (current-buffer)))
  6797. (with-temp-buffer
  6798. (let ((org-inhibit-startup t))
  6799. (org-mode)
  6800. (insert s)
  6801. (goto-char (point-min))
  6802. (org-open-at-point arg reference-buffer)))))
  6803. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6804. "Open link at or after point.
  6805. If there is no link at point, this function will search forward up to
  6806. the end of the current line.
  6807. Normally, files will be opened by an appropriate application. If the
  6808. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6809. With a double prefix argument, try to open outside of Emacs, in the
  6810. application the system uses for this file type."
  6811. (interactive "P")
  6812. (org-load-modules-maybe)
  6813. (move-marker org-open-link-marker (point))
  6814. (setq org-window-config-before-follow-link (current-window-configuration))
  6815. (org-remove-occur-highlights nil nil t)
  6816. (cond
  6817. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6818. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6819. (org-footnote-action))
  6820. (t
  6821. (let (type path link line search (pos (point)))
  6822. (catch 'match
  6823. (save-excursion
  6824. (skip-chars-forward "^]\n\r")
  6825. (when (org-in-regexp org-bracket-link-regexp)
  6826. (setq link (org-extract-attributes
  6827. (org-link-unescape (org-match-string-no-properties 1))))
  6828. (while (string-match " *\n *" link)
  6829. (setq link (replace-match " " t t link)))
  6830. (setq link (org-link-expand-abbrev link))
  6831. (cond
  6832. ((or (file-name-absolute-p link)
  6833. (string-match "^\\.\\.?/" link))
  6834. (setq type "file" path link))
  6835. ((string-match org-link-re-with-space3 link)
  6836. (setq type (match-string 1 link) path (match-string 2 link)))
  6837. (t (setq type "thisfile" path link)))
  6838. (throw 'match t)))
  6839. (when (get-text-property (point) 'org-linked-text)
  6840. (setq type "thisfile"
  6841. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6842. (1+ (point)) (point))
  6843. path (buffer-substring
  6844. (previous-single-property-change pos 'org-linked-text)
  6845. (next-single-property-change pos 'org-linked-text)))
  6846. (throw 'match t))
  6847. (save-excursion
  6848. (when (or (org-in-regexp org-angle-link-re)
  6849. (org-in-regexp org-plain-link-re))
  6850. (setq type (match-string 1) path (match-string 2))
  6851. (throw 'match t)))
  6852. (save-excursion
  6853. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6854. (setq type "tags"
  6855. path (match-string 1))
  6856. (while (string-match ":" path)
  6857. (setq path (replace-match "+" t t path)))
  6858. (throw 'match t)))
  6859. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6860. (setq type "tree-match"
  6861. path (match-string 1))
  6862. (throw 'match t)))
  6863. (unless path
  6864. (error "No link found"))
  6865. ;; switch back to reference buffer
  6866. ;; needed when if called in a temporary buffer through
  6867. ;; org-open-link-from-string
  6868. (and reference-buffer (switch-to-buffer reference-buffer))
  6869. ;; Remove any trailing spaces in path
  6870. (if (string-match " +\\'" path)
  6871. (setq path (replace-match "" t t path)))
  6872. (if (and org-link-translation-function
  6873. (fboundp org-link-translation-function))
  6874. ;; Check if we need to translate the link
  6875. (let ((tmp (funcall org-link-translation-function type path)))
  6876. (setq type (car tmp) path (cdr tmp))))
  6877. (cond
  6878. ((assoc type org-link-protocols)
  6879. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6880. ((equal type "mailto")
  6881. (let ((cmd (car org-link-mailto-program))
  6882. (args (cdr org-link-mailto-program)) args1
  6883. (address path) (subject "") a)
  6884. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6885. (setq address (match-string 1 path)
  6886. subject (org-link-escape (match-string 2 path))))
  6887. (while args
  6888. (cond
  6889. ((not (stringp (car args))) (push (pop args) args1))
  6890. (t (setq a (pop args))
  6891. (if (string-match "%a" a)
  6892. (setq a (replace-match address t t a)))
  6893. (if (string-match "%s" a)
  6894. (setq a (replace-match subject t t a)))
  6895. (push a args1))))
  6896. (apply cmd (nreverse args1))))
  6897. ((member type '("http" "https" "ftp" "news"))
  6898. (browse-url (concat type ":" (org-link-escape
  6899. path org-link-escape-chars-browser))))
  6900. ((member type '("message"))
  6901. (browse-url (concat type ":" path)))
  6902. ((string= type "tags")
  6903. (org-tags-view in-emacs path))
  6904. ((string= type "thisfile")
  6905. (if in-emacs
  6906. (switch-to-buffer-other-window
  6907. (org-get-buffer-for-internal-link (current-buffer)))
  6908. (org-mark-ring-push))
  6909. (let ((cmd `(org-link-search
  6910. ,path
  6911. ,(cond ((equal in-emacs '(4)) 'occur)
  6912. ((equal in-emacs '(16)) 'org-occur)
  6913. (t nil))
  6914. ,pos)))
  6915. (condition-case nil (eval cmd)
  6916. (error (progn (widen) (eval cmd))))))
  6917. ((string= type "tree-match")
  6918. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6919. ((string= type "file")
  6920. (if (string-match "::\\([0-9]+\\)\\'" path)
  6921. (setq line (string-to-number (match-string 1 path))
  6922. path (substring path 0 (match-beginning 0)))
  6923. (if (string-match "::\\(.+\\)\\'" path)
  6924. (setq search (match-string 1 path)
  6925. path (substring path 0 (match-beginning 0)))))
  6926. (if (string-match "[*?{]" (file-name-nondirectory path))
  6927. (dired path)
  6928. (org-open-file path in-emacs line search)))
  6929. ((string= type "news")
  6930. (require 'org-gnus)
  6931. (org-gnus-follow-link path))
  6932. ((string= type "shell")
  6933. (let ((cmd path))
  6934. (if (or (not org-confirm-shell-link-function)
  6935. (funcall org-confirm-shell-link-function
  6936. (format "Execute \"%s\" in shell? "
  6937. (org-add-props cmd nil
  6938. 'face 'org-warning))))
  6939. (progn
  6940. (message "Executing %s" cmd)
  6941. (shell-command cmd))
  6942. (error "Abort"))))
  6943. ((string= type "elisp")
  6944. (let ((cmd path))
  6945. (if (or (not org-confirm-elisp-link-function)
  6946. (funcall org-confirm-elisp-link-function
  6947. (format "Execute \"%s\" as elisp? "
  6948. (org-add-props cmd nil
  6949. 'face 'org-warning))))
  6950. (message "%s => %s" cmd
  6951. (if (equal (string-to-char cmd) ?\()
  6952. (eval (read cmd))
  6953. (call-interactively (read cmd))))
  6954. (error "Abort"))))
  6955. (t
  6956. (browse-url-at-point))))))
  6957. (move-marker org-open-link-marker nil)
  6958. (run-hook-with-args 'org-follow-link-hook))
  6959. ;;;; Time estimates
  6960. (defun org-get-effort (&optional pom)
  6961. "Get the effort estimate for the current entry."
  6962. (org-entry-get pom org-effort-property))
  6963. ;;; File search
  6964. (defvar org-create-file-search-functions nil
  6965. "List of functions to construct the right search string for a file link.
  6966. These functions are called in turn with point at the location to
  6967. which the link should point.
  6968. A function in the hook should first test if it would like to
  6969. handle this file type, for example by checking the major-mode or
  6970. the file extension. If it decides not to handle this file, it
  6971. should just return nil to give other functions a chance. If it
  6972. does handle the file, it must return the search string to be used
  6973. when following the link. The search string will be part of the
  6974. file link, given after a double colon, and `org-open-at-point'
  6975. will automatically search for it. If special measures must be
  6976. taken to make the search successful, another function should be
  6977. added to the companion hook `org-execute-file-search-functions',
  6978. which see.
  6979. A function in this hook may also use `setq' to set the variable
  6980. `description' to provide a suggestion for the descriptive text to
  6981. be used for this link when it gets inserted into an Org-mode
  6982. buffer with \\[org-insert-link].")
  6983. (defvar org-execute-file-search-functions nil
  6984. "List of functions to execute a file search triggered by a link.
  6985. Functions added to this hook must accept a single argument, the
  6986. search string that was part of the file link, the part after the
  6987. double colon. The function must first check if it would like to
  6988. handle this search, for example by checking the major-mode or the
  6989. file extension. If it decides not to handle this search, it
  6990. should just return nil to give other functions a chance. If it
  6991. does handle the search, it must return a non-nil value to keep
  6992. other functions from trying.
  6993. Each function can access the current prefix argument through the
  6994. variable `current-prefix-argument'. Note that a single prefix is
  6995. used to force opening a link in Emacs, so it may be good to only
  6996. use a numeric or double prefix to guide the search function.
  6997. In case this is needed, a function in this hook can also restore
  6998. the window configuration before `org-open-at-point' was called using:
  6999. (set-window-configuration org-window-config-before-follow-link)")
  7000. (defun org-link-search (s &optional type avoid-pos)
  7001. "Search for a link search option.
  7002. If S is surrounded by forward slashes, it is interpreted as a
  7003. regular expression. In org-mode files, this will create an `org-occur'
  7004. sparse tree. In ordinary files, `occur' will be used to list matches.
  7005. If the current buffer is in `dired-mode', grep will be used to search
  7006. in all files. If AVOID-POS is given, ignore matches near that position."
  7007. (let ((case-fold-search t)
  7008. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7009. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7010. (append '(("") (" ") ("\t") ("\n"))
  7011. org-emphasis-alist)
  7012. "\\|") "\\)"))
  7013. (pos (point))
  7014. (pre nil) (post nil)
  7015. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7016. (cond
  7017. ;; First check if there are any special
  7018. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7019. ;; Now try the builtin stuff
  7020. ((and (equal (string-to-char s0) ?#)
  7021. (> (length s0) 1)
  7022. (save-excursion
  7023. (goto-char (point-min))
  7024. (and
  7025. (re-search-forward
  7026. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7027. (setq type 'dedicated
  7028. pos (match-beginning 0))))
  7029. ;; There is an exact target for this
  7030. (goto-char pos)
  7031. (org-back-to-heading t)))
  7032. ((save-excursion
  7033. (goto-char (point-min))
  7034. (and
  7035. (re-search-forward
  7036. (concat "<<" (regexp-quote s0) ">>") nil t)
  7037. (setq type 'dedicated
  7038. pos (match-beginning 0))))
  7039. ;; There is an exact target for this
  7040. (goto-char pos))
  7041. ((and (string-match "^(\\(.*\\))$" s0)
  7042. (save-excursion
  7043. (goto-char (point-min))
  7044. (and
  7045. (re-search-forward
  7046. (concat "[^[]" (regexp-quote
  7047. (format org-coderef-label-format
  7048. (match-string 1 s0))))
  7049. nil t)
  7050. (setq type 'dedicated
  7051. pos (1+ (match-beginning 0))))))
  7052. ;; There is a coderef target for this
  7053. (goto-char pos))
  7054. ((string-match "^/\\(.*\\)/$" s)
  7055. ;; A regular expression
  7056. (cond
  7057. ((org-mode-p)
  7058. (org-occur (match-string 1 s)))
  7059. ;;((eq major-mode 'dired-mode)
  7060. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7061. (t (org-do-occur (match-string 1 s)))))
  7062. (t
  7063. ;; A normal search strings
  7064. (when (equal (string-to-char s) ?*)
  7065. ;; Anchor on headlines, post may include tags.
  7066. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7067. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7068. s (substring s 1)))
  7069. (remove-text-properties
  7070. 0 (length s)
  7071. '(face nil mouse-face nil keymap nil fontified nil) s)
  7072. ;; Make a series of regular expressions to find a match
  7073. (setq words (org-split-string s "[ \n\r\t]+")
  7074. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7075. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7076. "\\)" markers)
  7077. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7078. re2a (concat "[ \t\r\n]" re2a_)
  7079. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7080. re4 (concat "[^a-zA-Z_]" re4_)
  7081. re1 (concat pre re2 post)
  7082. re3 (concat pre (if pre re4_ re4) post)
  7083. re5 (concat pre ".*" re4)
  7084. re2 (concat pre re2)
  7085. re2a (concat pre (if pre re2a_ re2a))
  7086. re4 (concat pre (if pre re4_ re4))
  7087. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7088. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7089. re5 "\\)"
  7090. ))
  7091. (cond
  7092. ((eq type 'org-occur) (org-occur reall))
  7093. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7094. (t (goto-char (point-min))
  7095. (setq type 'fuzzy)
  7096. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7097. (org-search-not-self 1 re1 nil t)
  7098. (org-search-not-self 1 re2 nil t)
  7099. (org-search-not-self 1 re2a nil t)
  7100. (org-search-not-self 1 re3 nil t)
  7101. (org-search-not-self 1 re4 nil t)
  7102. (org-search-not-self 1 re5 nil t)
  7103. )
  7104. (goto-char (match-beginning 1))
  7105. (goto-char pos)
  7106. (error "No match")))))
  7107. (t
  7108. ;; Normal string-search
  7109. (goto-char (point-min))
  7110. (if (search-forward s nil t)
  7111. (goto-char (match-beginning 0))
  7112. (error "No match"))))
  7113. (and (org-mode-p) (org-show-context 'link-search))
  7114. type))
  7115. (defun org-search-not-self (group &rest args)
  7116. "Execute `re-search-forward', but only accept matches that do not
  7117. enclose the position of `org-open-link-marker'."
  7118. (let ((m org-open-link-marker))
  7119. (catch 'exit
  7120. (while (apply 're-search-forward args)
  7121. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7122. (goto-char (match-end group))
  7123. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7124. (> (match-beginning 0) (marker-position m))
  7125. (< (match-end 0) (marker-position m)))
  7126. (save-match-data
  7127. (or (not (org-in-regexp
  7128. org-bracket-link-analytic-regexp 1))
  7129. (not (match-end 4)) ; no description
  7130. (and (<= (match-beginning 4) (point))
  7131. (>= (match-end 4) (point))))))
  7132. (throw 'exit (point))))))))
  7133. (defun org-get-buffer-for-internal-link (buffer)
  7134. "Return a buffer to be used for displaying the link target of internal links."
  7135. (cond
  7136. ((not org-display-internal-link-with-indirect-buffer)
  7137. buffer)
  7138. ((string-match "(Clone)$" (buffer-name buffer))
  7139. (message "Buffer is already a clone, not making another one")
  7140. ;; we also do not modify visibility in this case
  7141. buffer)
  7142. (t ; make a new indirect buffer for displaying the link
  7143. (let* ((bn (buffer-name buffer))
  7144. (ibn (concat bn "(Clone)"))
  7145. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7146. (with-current-buffer ib (org-overview))
  7147. ib))))
  7148. (defun org-do-occur (regexp &optional cleanup)
  7149. "Call the Emacs command `occur'.
  7150. If CLEANUP is non-nil, remove the printout of the regular expression
  7151. in the *Occur* buffer. This is useful if the regex is long and not useful
  7152. to read."
  7153. (occur regexp)
  7154. (when cleanup
  7155. (let ((cwin (selected-window)) win beg end)
  7156. (when (setq win (get-buffer-window "*Occur*"))
  7157. (select-window win))
  7158. (goto-char (point-min))
  7159. (when (re-search-forward "match[a-z]+" nil t)
  7160. (setq beg (match-end 0))
  7161. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7162. (setq end (1- (match-beginning 0)))))
  7163. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7164. (goto-char (point-min))
  7165. (select-window cwin))))
  7166. ;;; The mark ring for links jumps
  7167. (defvar org-mark-ring nil
  7168. "Mark ring for positions before jumps in Org-mode.")
  7169. (defvar org-mark-ring-last-goto nil
  7170. "Last position in the mark ring used to go back.")
  7171. ;; Fill and close the ring
  7172. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7173. (loop for i from 1 to org-mark-ring-length do
  7174. (push (make-marker) org-mark-ring))
  7175. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7176. org-mark-ring)
  7177. (defun org-mark-ring-push (&optional pos buffer)
  7178. "Put the current position or POS into the mark ring and rotate it."
  7179. (interactive)
  7180. (setq pos (or pos (point)))
  7181. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7182. (move-marker (car org-mark-ring)
  7183. (or pos (point))
  7184. (or buffer (current-buffer)))
  7185. (message "%s"
  7186. (substitute-command-keys
  7187. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7188. (defun org-mark-ring-goto (&optional n)
  7189. "Jump to the previous position in the mark ring.
  7190. With prefix arg N, jump back that many stored positions. When
  7191. called several times in succession, walk through the entire ring.
  7192. Org-mode commands jumping to a different position in the current file,
  7193. or to another Org-mode file, automatically push the old position
  7194. onto the ring."
  7195. (interactive "p")
  7196. (let (p m)
  7197. (if (eq last-command this-command)
  7198. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7199. (setq p org-mark-ring))
  7200. (setq org-mark-ring-last-goto p)
  7201. (setq m (car p))
  7202. (switch-to-buffer (marker-buffer m))
  7203. (goto-char m)
  7204. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7205. (defun org-remove-angle-brackets (s)
  7206. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7207. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7208. s)
  7209. (defun org-add-angle-brackets (s)
  7210. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7211. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7212. s)
  7213. (defun org-remove-double-quotes (s)
  7214. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7215. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7216. s)
  7217. ;;; Following specific links
  7218. (defun org-follow-timestamp-link ()
  7219. (cond
  7220. ((org-at-date-range-p t)
  7221. (let ((org-agenda-start-on-weekday)
  7222. (t1 (match-string 1))
  7223. (t2 (match-string 2)))
  7224. (setq t1 (time-to-days (org-time-string-to-time t1))
  7225. t2 (time-to-days (org-time-string-to-time t2)))
  7226. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7227. ((org-at-timestamp-p t)
  7228. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7229. (substring (match-string 1) 0 10)))
  7230. 1))
  7231. (t (error "This should not happen"))))
  7232. ;;; Following file links
  7233. (defvar org-wait nil)
  7234. (defun org-open-file (path &optional in-emacs line search)
  7235. "Open the file at PATH.
  7236. First, this expands any special file name abbreviations. Then the
  7237. configuration variable `org-file-apps' is checked if it contains an
  7238. entry for this file type, and if yes, the corresponding command is launched.
  7239. If no application is found, Emacs simply visits the file.
  7240. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7241. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7242. and o use an external application to visit the file.
  7243. Optional LINE specifies a line to go to, optional SEARCH a string to
  7244. search for. If LINE or SEARCH is given, the file will always be
  7245. opened in Emacs.
  7246. If the file does not exist, an error is thrown."
  7247. (setq in-emacs (or in-emacs line search))
  7248. (let* ((file (if (equal path "")
  7249. buffer-file-name
  7250. (substitute-in-file-name (expand-file-name path))))
  7251. (apps (append org-file-apps (org-default-apps)))
  7252. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7253. (dirp (if remp nil (file-directory-p file)))
  7254. (file (if (and dirp org-open-directory-means-index-dot-org)
  7255. (concat (file-name-as-directory file) "index.org")
  7256. file))
  7257. (a-m-a-p (assq 'auto-mode apps))
  7258. (dfile (downcase file))
  7259. (old-buffer (current-buffer))
  7260. (old-pos (point))
  7261. (old-mode major-mode)
  7262. ext cmd)
  7263. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7264. (setq ext (match-string 1 dfile))
  7265. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7266. (setq ext (match-string 1 dfile))))
  7267. (cond
  7268. ((equal in-emacs '(16))
  7269. (setq cmd (cdr (assoc 'system apps))))
  7270. (in-emacs (setq cmd 'emacs))
  7271. (t
  7272. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7273. (and dirp (cdr (assoc 'directory apps)))
  7274. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7275. 'string-match)
  7276. (cdr (assoc ext apps))
  7277. (cdr (assoc t apps))))))
  7278. (when (eq cmd 'system)
  7279. (setq cmd (cdr (assoc 'system apps))))
  7280. (when (eq cmd 'default)
  7281. (setq cmd (cdr (assoc t apps))))
  7282. (when (eq cmd 'mailcap)
  7283. (require 'mailcap)
  7284. (mailcap-parse-mailcaps)
  7285. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7286. (command (mailcap-mime-info mime-type)))
  7287. (if (stringp command)
  7288. (setq cmd command)
  7289. (setq cmd 'emacs))))
  7290. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7291. (not (file-exists-p file))
  7292. (not org-open-non-existing-files))
  7293. (error "No such file: %s" file))
  7294. (cond
  7295. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7296. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7297. (while (string-match "['\"]%s['\"]" cmd)
  7298. (setq cmd (replace-match "%s" t t cmd)))
  7299. (while (string-match "%s" cmd)
  7300. (setq cmd (replace-match
  7301. (save-match-data
  7302. (shell-quote-argument
  7303. (convert-standard-filename file)))
  7304. t t cmd)))
  7305. (save-window-excursion
  7306. (start-process-shell-command cmd nil cmd)
  7307. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7308. ))
  7309. ((or (stringp cmd)
  7310. (eq cmd 'emacs))
  7311. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7312. (widen)
  7313. (if line (goto-line line)
  7314. (if search (org-link-search search))))
  7315. ((consp cmd)
  7316. (let ((file (convert-standard-filename file)))
  7317. (eval cmd)))
  7318. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7319. (and (org-mode-p) (eq old-mode 'org-mode)
  7320. (or (not (equal old-buffer (current-buffer)))
  7321. (not (equal old-pos (point))))
  7322. (org-mark-ring-push old-pos old-buffer))))
  7323. (defun org-default-apps ()
  7324. "Return the default applications for this operating system."
  7325. (cond
  7326. ((eq system-type 'darwin)
  7327. org-file-apps-defaults-macosx)
  7328. ((eq system-type 'windows-nt)
  7329. org-file-apps-defaults-windowsnt)
  7330. (t org-file-apps-defaults-gnu)))
  7331. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7332. "Convert extensions to regular expressions in the cars of LIST.
  7333. Also, weed out any non-string entries, because the return value is used
  7334. only for regexp matching.
  7335. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7336. point to the symbol `emacs', indicating that the file should
  7337. be opened in Emacs."
  7338. (append
  7339. (delq nil
  7340. (mapcar (lambda (x)
  7341. (if (not (stringp (car x)))
  7342. nil
  7343. (if (string-match "\\W" (car x))
  7344. x
  7345. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7346. list))
  7347. (if add-auto-mode
  7348. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7349. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7350. (defun org-file-remote-p (file)
  7351. "Test whether FILE specifies a location on a remote system.
  7352. Return non-nil if the location is indeed remote.
  7353. For example, the filename \"/user@host:/foo\" specifies a location
  7354. on the system \"/user@host:\"."
  7355. (cond ((fboundp 'file-remote-p)
  7356. (file-remote-p file))
  7357. ((fboundp 'tramp-handle-file-remote-p)
  7358. (tramp-handle-file-remote-p file))
  7359. ((and (boundp 'ange-ftp-name-format)
  7360. (string-match (car ange-ftp-name-format) file))
  7361. t)
  7362. (t nil)))
  7363. ;;;; Refiling
  7364. (defun org-get-org-file ()
  7365. "Read a filename, with default directory `org-directory'."
  7366. (let ((default (or org-default-notes-file remember-data-file)))
  7367. (read-file-name (format "File name [%s]: " default)
  7368. (file-name-as-directory org-directory)
  7369. default)))
  7370. (defun org-notes-order-reversed-p ()
  7371. "Check if the current file should receive notes in reversed order."
  7372. (cond
  7373. ((not org-reverse-note-order) nil)
  7374. ((eq t org-reverse-note-order) t)
  7375. ((not (listp org-reverse-note-order)) nil)
  7376. (t (catch 'exit
  7377. (let ((all org-reverse-note-order)
  7378. entry)
  7379. (while (setq entry (pop all))
  7380. (if (string-match (car entry) buffer-file-name)
  7381. (throw 'exit (cdr entry))))
  7382. nil)))))
  7383. (defvar org-refile-target-table nil
  7384. "The list of refile targets, created by `org-refile'.")
  7385. (defvar org-agenda-new-buffers nil
  7386. "Buffers created to visit agenda files.")
  7387. (defun org-get-refile-targets (&optional default-buffer)
  7388. "Produce a table with refile targets."
  7389. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7390. targets txt re files f desc descre fast-path-p level pos0)
  7391. (message "Getting targets...")
  7392. (with-current-buffer (or default-buffer (current-buffer))
  7393. (while (setq entry (pop entries))
  7394. (setq files (car entry) desc (cdr entry))
  7395. (setq fast-path-p nil)
  7396. (cond
  7397. ((null files) (setq files (list (current-buffer))))
  7398. ((eq files 'org-agenda-files)
  7399. (setq files (org-agenda-files 'unrestricted)))
  7400. ((and (symbolp files) (fboundp files))
  7401. (setq files (funcall files)))
  7402. ((and (symbolp files) (boundp files))
  7403. (setq files (symbol-value files))))
  7404. (if (stringp files) (setq files (list files)))
  7405. (cond
  7406. ((eq (car desc) :tag)
  7407. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7408. ((eq (car desc) :todo)
  7409. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7410. ((eq (car desc) :regexp)
  7411. (setq descre (cdr desc)))
  7412. ((eq (car desc) :level)
  7413. (setq descre (concat "^\\*\\{" (number-to-string
  7414. (if org-odd-levels-only
  7415. (1- (* 2 (cdr desc)))
  7416. (cdr desc)))
  7417. "\\}[ \t]")))
  7418. ((eq (car desc) :maxlevel)
  7419. (setq fast-path-p t)
  7420. (setq descre (concat "^\\*\\{1," (number-to-string
  7421. (if org-odd-levels-only
  7422. (1- (* 2 (cdr desc)))
  7423. (cdr desc)))
  7424. "\\}[ \t]")))
  7425. (t (error "Bad refiling target description %s" desc)))
  7426. (while (setq f (pop files))
  7427. (save-excursion
  7428. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7429. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7430. (setq f (expand-file-name f))
  7431. (if (eq org-refile-use-outline-path 'file)
  7432. (push (list (file-name-nondirectory f) f nil nil) targets))
  7433. (save-excursion
  7434. (save-restriction
  7435. (widen)
  7436. (goto-char (point-min))
  7437. (while (re-search-forward descre nil t)
  7438. (goto-char (setq pos0 (point-at-bol)))
  7439. (catch 'next
  7440. (when org-refile-target-verify-function
  7441. (save-match-data
  7442. (or (funcall org-refile-target-verify-function)
  7443. (throw 'next t))))
  7444. (when (looking-at org-complex-heading-regexp)
  7445. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7446. txt (org-link-display-format (match-string 4))
  7447. re (concat "^" (regexp-quote
  7448. (buffer-substring (match-beginning 1)
  7449. (match-end 4)))))
  7450. (if (match-end 5) (setq re (concat re "[ \t]+"
  7451. (regexp-quote
  7452. (match-string 5)))))
  7453. (setq re (concat re "[ \t]*$"))
  7454. (when org-refile-use-outline-path
  7455. (setq txt (mapconcat 'org-protect-slash
  7456. (append
  7457. (if (eq org-refile-use-outline-path 'file)
  7458. (list (file-name-nondirectory
  7459. (buffer-file-name (buffer-base-buffer))))
  7460. (if (eq org-refile-use-outline-path 'full-file-path)
  7461. (list (buffer-file-name (buffer-base-buffer)))))
  7462. (org-get-outline-path fast-path-p level txt)
  7463. (list txt))
  7464. "/")))
  7465. (push (list txt f re (point)) targets)))
  7466. (when (= (point) pos0)
  7467. ;; verification function has not moved point
  7468. (goto-char (point-at-eol))))))))))
  7469. (message "Getting targets...done")
  7470. (nreverse targets)))
  7471. (defun org-protect-slash (s)
  7472. (while (string-match "/" s)
  7473. (setq s (replace-match "\\" t t s)))
  7474. s)
  7475. (defvar org-olpa (make-vector 20 nil))
  7476. (defun org-get-outline-path (&optional fastp level heading)
  7477. "Return the outline path to the current entry, as a list."
  7478. (if fastp
  7479. (progn
  7480. (if (> level 19)
  7481. (error "Outline path failure, more than 19 levels."))
  7482. (loop for i from level upto 19 do
  7483. (aset org-olpa i nil))
  7484. (prog1
  7485. (delq nil (append org-olpa nil))
  7486. (aset org-olpa level heading)))
  7487. (let (rtn)
  7488. (save-excursion
  7489. (while (org-up-heading-safe)
  7490. (when (looking-at org-complex-heading-regexp)
  7491. (push (org-match-string-no-properties 4) rtn)))
  7492. rtn))))
  7493. (defvar org-refile-history nil
  7494. "History for refiling operations.")
  7495. (defun org-refile (&optional goto default-buffer)
  7496. "Move the entry at point to another heading.
  7497. The list of target headings is compiled using the information in
  7498. `org-refile-targets', which see. This list is created before each use
  7499. and will therefore always be up-to-date.
  7500. At the target location, the entry is filed as a subitem of the target heading.
  7501. Depending on `org-reverse-note-order', the new subitem will either be the
  7502. first or the last subitem.
  7503. If there is an active region, all entries in that region will be moved.
  7504. However, the region must fulfil the requirement that the first heading
  7505. is the first one sets the top-level of the moved text - at most siblings
  7506. below it are allowed.
  7507. With prefix arg GOTO, the command will only visit the target location,
  7508. not actually move anything.
  7509. With a double prefix `C-u C-u', go to the location where the last refiling
  7510. operation has put the subtree.
  7511. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7512. (interactive "P")
  7513. (let* ((cbuf (current-buffer))
  7514. (regionp (org-region-active-p))
  7515. (region-start (and regionp (region-beginning)))
  7516. (region-end (and regionp (region-end)))
  7517. (region-length (and regionp (- region-end region-start)))
  7518. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7519. pos it nbuf file re level reversed)
  7520. (when regionp (goto-char region-start)
  7521. (unless (org-kill-is-subtree-p
  7522. (buffer-substring region-start region-end))
  7523. (error "The region is not a (sequence of) subtree(s)")))
  7524. (if (equal goto '(16))
  7525. (org-refile-goto-last-stored)
  7526. (when (setq it (org-refile-get-location
  7527. (if goto "Goto: " "Refile to: ") default-buffer
  7528. org-refile-allow-creating-parent-nodes))
  7529. (setq file (nth 1 it)
  7530. re (nth 2 it)
  7531. pos (nth 3 it))
  7532. (if (and (not goto)
  7533. pos
  7534. (equal (buffer-file-name) file)
  7535. (if regionp
  7536. (and (>= pos region-start)
  7537. (<= pos region-end))
  7538. (and (>= pos (point))
  7539. (< pos (save-excursion
  7540. (org-end-of-subtree t t))))))
  7541. (error "Cannot refile to position inside the tree or region"))
  7542. (setq nbuf (or (find-buffer-visiting file)
  7543. (find-file-noselect file)))
  7544. (if goto
  7545. (progn
  7546. (switch-to-buffer nbuf)
  7547. (goto-char pos)
  7548. (org-show-context 'org-goto))
  7549. (if regionp
  7550. (progn
  7551. (kill-new (buffer-substring region-start region-end))
  7552. (org-save-markers-in-region region-start region-end))
  7553. (org-copy-subtree 1 nil t))
  7554. (save-excursion
  7555. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7556. (find-file-noselect file))))
  7557. (setq reversed (org-notes-order-reversed-p))
  7558. (save-excursion
  7559. (save-restriction
  7560. (widen)
  7561. (if pos
  7562. (progn
  7563. (goto-char pos)
  7564. (looking-at outline-regexp)
  7565. (setq level (org-get-valid-level (funcall outline-level) 1))
  7566. (goto-char
  7567. (if reversed
  7568. (or (outline-next-heading) (point-max))
  7569. (or (save-excursion (outline-get-next-sibling))
  7570. (org-end-of-subtree t t)
  7571. (point-max)))))
  7572. (setq level 1)
  7573. (if (not reversed)
  7574. (goto-char (point-max))
  7575. (goto-char (point-min))
  7576. (or (outline-next-heading) (goto-char (point-max)))))
  7577. (if (not (bolp)) (newline))
  7578. (bookmark-set "org-refile-last-stored")
  7579. (org-paste-subtree level))))
  7580. (if regionp
  7581. (delete-region (point) (+ (point) region-length))
  7582. (org-cut-subtree))
  7583. (setq org-markers-to-move nil)
  7584. (message "Refiled to \"%s\"" (car it))))))
  7585. (org-reveal))
  7586. (defun org-refile-goto-last-stored ()
  7587. "Go to the location where the last refile was stored."
  7588. (interactive)
  7589. (bookmark-jump "org-refile-last-stored")
  7590. (message "This is the location of the last refile"))
  7591. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7592. "Prompt the user for a refile location, using PROMPT."
  7593. (let ((org-refile-targets org-refile-targets)
  7594. (org-refile-use-outline-path org-refile-use-outline-path))
  7595. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7596. (unless org-refile-target-table
  7597. (error "No refile targets"))
  7598. (let* ((cbuf (current-buffer))
  7599. (partial-completion-mode nil)
  7600. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7601. (cfunc (if (and org-refile-use-outline-path
  7602. org-outline-path-complete-in-steps)
  7603. 'org-olpath-completing-read
  7604. 'org-ido-completing-read))
  7605. (extra (if org-refile-use-outline-path "/" ""))
  7606. (filename (and cfn (expand-file-name cfn)))
  7607. (tbl (mapcar
  7608. (lambda (x)
  7609. (if (and (not (member org-refile-use-outline-path
  7610. '(file full-file-path)))
  7611. (not (equal filename (nth 1 x))))
  7612. (cons (concat (car x) extra " ("
  7613. (file-name-nondirectory (nth 1 x)) ")")
  7614. (cdr x))
  7615. (cons (concat (car x) extra) (cdr x))))
  7616. org-refile-target-table))
  7617. (completion-ignore-case t)
  7618. pa answ parent-target child parent)
  7619. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7620. nil 'org-refile-history))
  7621. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7622. (if pa
  7623. pa
  7624. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7625. (setq parent (match-string 1 answ)
  7626. child (match-string 2 answ))
  7627. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7628. (when (and parent-target
  7629. (or (eq new-nodes t)
  7630. (and (eq new-nodes 'confirm)
  7631. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7632. (org-refile-new-child parent-target child))))))
  7633. (defun org-refile-new-child (parent-target child)
  7634. "Use refile target PARENT-TARGET to add new CHILD below it."
  7635. (unless parent-target
  7636. (error "Cannot find parent for new node"))
  7637. (let ((file (nth 1 parent-target))
  7638. (pos (nth 3 parent-target))
  7639. level)
  7640. (with-current-buffer (or (find-buffer-visiting file)
  7641. (find-file-noselect file))
  7642. (save-excursion
  7643. (save-restriction
  7644. (widen)
  7645. (if pos
  7646. (goto-char pos)
  7647. (goto-char (point-max))
  7648. (if (not (bolp)) (newline)))
  7649. (when (looking-at outline-regexp)
  7650. (setq level (funcall outline-level))
  7651. (org-end-of-subtree t t))
  7652. (org-back-over-empty-lines)
  7653. (insert "\n" (make-string
  7654. (if pos (org-get-valid-level level 1) 1) ?*)
  7655. " " child "\n")
  7656. (beginning-of-line 0)
  7657. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7658. (defun org-olpath-completing-read (prompt collection &rest args)
  7659. "Read an outline path like a file name."
  7660. (let ((thetable collection)
  7661. (org-completion-use-ido nil)) ; does not work with ido.
  7662. (apply
  7663. 'org-ido-completing-read prompt
  7664. (lambda (string predicate &optional flag)
  7665. (let (rtn r f (l (length string)))
  7666. (cond
  7667. ((eq flag nil)
  7668. ;; try completion
  7669. (try-completion string thetable))
  7670. ((eq flag t)
  7671. ;; all-completions
  7672. (setq rtn (all-completions string thetable predicate))
  7673. (mapcar
  7674. (lambda (x)
  7675. (setq r (substring x l))
  7676. (if (string-match " ([^)]*)$" x)
  7677. (setq f (match-string 0 x))
  7678. (setq f ""))
  7679. (if (string-match "/" r)
  7680. (concat string (substring r 0 (match-end 0)) f)
  7681. x))
  7682. rtn))
  7683. ((eq flag 'lambda)
  7684. ;; exact match?
  7685. (assoc string thetable)))
  7686. ))
  7687. args)))
  7688. ;;;; Dynamic blocks
  7689. (defun org-find-dblock (name)
  7690. "Find the first dynamic block with name NAME in the buffer.
  7691. If not found, stay at current position and return nil."
  7692. (let (pos)
  7693. (save-excursion
  7694. (goto-char (point-min))
  7695. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7696. nil t)
  7697. (match-beginning 0))))
  7698. (if pos (goto-char pos))
  7699. pos))
  7700. (defconst org-dblock-start-re
  7701. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7702. "Matches the startline of a dynamic block, with parameters.")
  7703. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7704. "Matches the end of a dynamic block.")
  7705. (defun org-create-dblock (plist)
  7706. "Create a dynamic block section, with parameters taken from PLIST.
  7707. PLIST must contain a :name entry which is used as name of the block."
  7708. (unless (bolp) (newline))
  7709. (let ((name (plist-get plist :name)))
  7710. (insert "#+BEGIN: " name)
  7711. (while plist
  7712. (if (eq (car plist) :name)
  7713. (setq plist (cddr plist))
  7714. (insert " " (prin1-to-string (pop plist)))))
  7715. (insert "\n\n#+END:\n")
  7716. (beginning-of-line -2)))
  7717. (defun org-prepare-dblock ()
  7718. "Prepare dynamic block for refresh.
  7719. This empties the block, puts the cursor at the insert position and returns
  7720. the property list including an extra property :name with the block name."
  7721. (unless (looking-at org-dblock-start-re)
  7722. (error "Not at a dynamic block"))
  7723. (let* ((begdel (1+ (match-end 0)))
  7724. (name (org-no-properties (match-string 1)))
  7725. (params (append (list :name name)
  7726. (read (concat "(" (match-string 3) ")")))))
  7727. (unless (re-search-forward org-dblock-end-re nil t)
  7728. (error "Dynamic block not terminated"))
  7729. (setq params
  7730. (append params
  7731. (list :content (buffer-substring
  7732. begdel (match-beginning 0)))))
  7733. (delete-region begdel (match-beginning 0))
  7734. (goto-char begdel)
  7735. (open-line 1)
  7736. params))
  7737. (defun org-map-dblocks (&optional command)
  7738. "Apply COMMAND to all dynamic blocks in the current buffer.
  7739. If COMMAND is not given, use `org-update-dblock'."
  7740. (let ((cmd (or command 'org-update-dblock))
  7741. pos)
  7742. (save-excursion
  7743. (goto-char (point-min))
  7744. (while (re-search-forward org-dblock-start-re nil t)
  7745. (goto-char (setq pos (match-beginning 0)))
  7746. (condition-case nil
  7747. (funcall cmd)
  7748. (error (message "Error during update of dynamic block")))
  7749. (goto-char pos)
  7750. (unless (re-search-forward org-dblock-end-re nil t)
  7751. (error "Dynamic block not terminated"))))))
  7752. (defun org-dblock-update (&optional arg)
  7753. "User command for updating dynamic blocks.
  7754. Update the dynamic block at point. With prefix ARG, update all dynamic
  7755. blocks in the buffer."
  7756. (interactive "P")
  7757. (if arg
  7758. (org-update-all-dblocks)
  7759. (or (looking-at org-dblock-start-re)
  7760. (org-beginning-of-dblock))
  7761. (org-update-dblock)))
  7762. (defun org-update-dblock ()
  7763. "Update the dynamic block at point
  7764. This means to empty the block, parse for parameters and then call
  7765. the correct writing function."
  7766. (save-window-excursion
  7767. (let* ((pos (point))
  7768. (line (org-current-line))
  7769. (params (org-prepare-dblock))
  7770. (name (plist-get params :name))
  7771. (cmd (intern (concat "org-dblock-write:" name))))
  7772. (message "Updating dynamic block `%s' at line %d..." name line)
  7773. (funcall cmd params)
  7774. (message "Updating dynamic block `%s' at line %d...done" name line)
  7775. (goto-char pos))))
  7776. (defun org-beginning-of-dblock ()
  7777. "Find the beginning of the dynamic block at point.
  7778. Error if there is no such block at point."
  7779. (let ((pos (point))
  7780. beg)
  7781. (end-of-line 1)
  7782. (if (and (re-search-backward org-dblock-start-re nil t)
  7783. (setq beg (match-beginning 0))
  7784. (re-search-forward org-dblock-end-re nil t)
  7785. (> (match-end 0) pos))
  7786. (goto-char beg)
  7787. (goto-char pos)
  7788. (error "Not in a dynamic block"))))
  7789. (defun org-update-all-dblocks ()
  7790. "Update all dynamic blocks in the buffer.
  7791. This function can be used in a hook."
  7792. (when (org-mode-p)
  7793. (org-map-dblocks 'org-update-dblock)))
  7794. ;;;; Completion
  7795. (defconst org-additional-option-like-keywords
  7796. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7797. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7798. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7799. "BEGIN_QUOTE" "END_QUOTE"
  7800. "BEGIN_VERSE" "END_VERSE"
  7801. "BEGIN_CENTER" "END_CENTER"
  7802. "BEGIN_SRC" "END_SRC"
  7803. "CATEGORY" "COLUMNS"
  7804. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7805. (defcustom org-structure-template-alist
  7806. '(
  7807. ("s" "#+begin_src ?\n\n#+end_src"
  7808. "<src lang=\"?\">\n\n</src>")
  7809. ("e" "#+begin_example\n?\n#+end_example"
  7810. "<example>\n?\n</example>")
  7811. ("q" "#+begin_quote\n?\n#+end_quote"
  7812. "<quote>\n?\n</quote>")
  7813. ("v" "#+begin_verse\n?\n#+end_verse"
  7814. "<verse>\n?\n/verse>")
  7815. ("c" "#+begin_center\n?\n#+end_center"
  7816. "<center>\n?\n/center>")
  7817. ("l" "#+begin_latex\n?\n#+end_latex"
  7818. "<literal style=\"latex\">\n?\n</literal>")
  7819. ("L" "#+latex: "
  7820. "<literal style=\"latex\">?</literal>")
  7821. ("h" "#+begin_html\n?\n#+end_html"
  7822. "<literal style=\"html\">\n?\n</literal>")
  7823. ("H" "#+html: "
  7824. "<literal style=\"html\">?</literal>")
  7825. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7826. ("A" "#+ascii: ")
  7827. ("i" "#+include %file ?"
  7828. "<include file=%file markup=\"?\">")
  7829. )
  7830. "Structure completion elements.
  7831. This is a list of abbreviation keys and values. The value gets inserted
  7832. it you type @samp{.} followed by the key and then the completion key,
  7833. usually `M-TAB'. %file will be replaced by a file name after prompting
  7834. for the file using completion.
  7835. There are two templates for each key, the first uses the original Org syntax,
  7836. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7837. the default when the /org-mtags.el/ module has been loaded. See also the
  7838. variable `org-mtags-prefer-muse-templates'.
  7839. This is an experimental feature, it is undecided if it is going to stay in."
  7840. :group 'org-completion
  7841. :type '(repeat
  7842. (string :tag "Key")
  7843. (string :tag "Template")
  7844. (string :tag "Muse Template")))
  7845. (defun org-try-structure-completion ()
  7846. "Try to complete a structure template before point.
  7847. This looks for strings like \"<e\" on an otherwise empty line and
  7848. expands them."
  7849. (let ((l (buffer-substring (point-at-bol) (point)))
  7850. a)
  7851. (when (and (looking-at "[ \t]*$")
  7852. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7853. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7854. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7855. (match-beginning 1)) a)
  7856. t)))
  7857. (defun org-complete-expand-structure-template (start cell)
  7858. "Expand a structure template."
  7859. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7860. (rpl (nth (if musep 2 1) cell)))
  7861. (delete-region start (point))
  7862. (when (string-match "\\`#\\+" rpl)
  7863. (cond
  7864. ((bolp))
  7865. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7866. (delete-region (point-at-bol) (point)))
  7867. (t (newline))))
  7868. (setq start (point))
  7869. (if (string-match "%file" rpl)
  7870. (setq rpl (replace-match
  7871. (concat
  7872. "\""
  7873. (save-match-data
  7874. (abbreviate-file-name (read-file-name "Include file: ")))
  7875. "\"")
  7876. t t rpl)))
  7877. (insert rpl)
  7878. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7879. (defun org-complete (&optional arg)
  7880. "Perform completion on word at point.
  7881. At the beginning of a headline, this completes TODO keywords as given in
  7882. `org-todo-keywords'.
  7883. If the current word is preceded by a backslash, completes the TeX symbols
  7884. that are supported for HTML support.
  7885. If the current word is preceded by \"#+\", completes special words for
  7886. setting file options.
  7887. In the line after \"#+STARTUP:, complete valid keywords.\"
  7888. At all other locations, this simply calls the value of
  7889. `org-completion-fallback-command'."
  7890. (interactive "P")
  7891. (org-without-partial-completion
  7892. (catch 'exit
  7893. (let* ((a nil)
  7894. (end (point))
  7895. (beg1 (save-excursion
  7896. (skip-chars-backward (org-re "[:alnum:]_@"))
  7897. (point)))
  7898. (beg (save-excursion
  7899. (skip-chars-backward "a-zA-Z0-9_:$")
  7900. (point)))
  7901. (confirm (lambda (x) (stringp (car x))))
  7902. (searchhead (equal (char-before beg) ?*))
  7903. (struct
  7904. (when (and (member (char-before beg1) '(?. ?<))
  7905. (setq a (assoc (buffer-substring beg1 (point))
  7906. org-structure-template-alist)))
  7907. (org-complete-expand-structure-template (1- beg1) a)
  7908. (throw 'exit t)))
  7909. (tag (and (equal (char-before beg1) ?:)
  7910. (equal (char-after (point-at-bol)) ?*)))
  7911. (prop (and (equal (char-before beg1) ?:)
  7912. (not (equal (char-after (point-at-bol)) ?*))))
  7913. (texp (equal (char-before beg) ?\\))
  7914. (link (equal (char-before beg) ?\[))
  7915. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7916. beg)
  7917. "#+"))
  7918. (startup (string-match "^#\\+STARTUP:.*"
  7919. (buffer-substring (point-at-bol) (point))))
  7920. (completion-ignore-case opt)
  7921. (type nil)
  7922. (tbl nil)
  7923. (table (cond
  7924. (opt
  7925. (setq type :opt)
  7926. (require 'org-exp)
  7927. (append
  7928. (mapcar
  7929. (lambda (x)
  7930. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7931. (cons (match-string 2 x) (match-string 1 x)))
  7932. (org-split-string (org-get-current-options) "\n"))
  7933. (mapcar 'list org-additional-option-like-keywords)))
  7934. (startup
  7935. (setq type :startup)
  7936. org-startup-options)
  7937. (link (append org-link-abbrev-alist-local
  7938. org-link-abbrev-alist))
  7939. (texp
  7940. (setq type :tex)
  7941. org-html-entities)
  7942. ((string-match "\\`\\*+[ \t]+\\'"
  7943. (buffer-substring (point-at-bol) beg))
  7944. (setq type :todo)
  7945. (mapcar 'list org-todo-keywords-1))
  7946. (searchhead
  7947. (setq type :searchhead)
  7948. (save-excursion
  7949. (goto-char (point-min))
  7950. (while (re-search-forward org-todo-line-regexp nil t)
  7951. (push (list
  7952. (org-make-org-heading-search-string
  7953. (match-string 3) t))
  7954. tbl)))
  7955. tbl)
  7956. (tag (setq type :tag beg beg1)
  7957. (or org-tag-alist (org-get-buffer-tags)))
  7958. (prop (setq type :prop beg beg1)
  7959. (mapcar 'list (org-buffer-property-keys nil t t)))
  7960. (t (progn
  7961. (call-interactively org-completion-fallback-command)
  7962. (throw 'exit nil)))))
  7963. (pattern (buffer-substring-no-properties beg end))
  7964. (completion (try-completion pattern table confirm)))
  7965. (cond ((eq completion t)
  7966. (if (not (assoc (upcase pattern) table))
  7967. (message "Already complete")
  7968. (if (and (equal type :opt)
  7969. (not (member (car (assoc (upcase pattern) table))
  7970. org-additional-option-like-keywords)))
  7971. (insert (substring (cdr (assoc (upcase pattern) table))
  7972. (length pattern)))
  7973. (if (memq type '(:tag :prop)) (insert ":")))))
  7974. ((null completion)
  7975. (message "Can't find completion for \"%s\"" pattern)
  7976. (ding))
  7977. ((not (string= pattern completion))
  7978. (delete-region beg end)
  7979. (if (string-match " +$" completion)
  7980. (setq completion (replace-match "" t t completion)))
  7981. (insert completion)
  7982. (if (get-buffer-window "*Completions*")
  7983. (delete-window (get-buffer-window "*Completions*")))
  7984. (if (assoc completion table)
  7985. (if (eq type :todo) (insert " ")
  7986. (if (memq type '(:tag :prop)) (insert ":"))))
  7987. (if (and (equal type :opt) (assoc completion table))
  7988. (message "%s" (substitute-command-keys
  7989. "Press \\[org-complete] again to insert example settings"))))
  7990. (t
  7991. (message "Making completion list...")
  7992. (let ((list (sort (all-completions pattern table confirm)
  7993. 'string<)))
  7994. (with-output-to-temp-buffer "*Completions*"
  7995. (condition-case nil
  7996. ;; Protection needed for XEmacs and emacs 21
  7997. (display-completion-list list pattern)
  7998. (error (display-completion-list list)))))
  7999. (message "Making completion list...%s" "done")))))))
  8000. ;;;; TODO, DEADLINE, Comments
  8001. (defun org-toggle-comment ()
  8002. "Change the COMMENT state of an entry."
  8003. (interactive)
  8004. (save-excursion
  8005. (org-back-to-heading)
  8006. (let (case-fold-search)
  8007. (if (looking-at (concat outline-regexp
  8008. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8009. (replace-match "" t t nil 1)
  8010. (if (looking-at outline-regexp)
  8011. (progn
  8012. (goto-char (match-end 0))
  8013. (insert org-comment-string " ")))))))
  8014. (defvar org-last-todo-state-is-todo nil
  8015. "This is non-nil when the last TODO state change led to a TODO state.
  8016. If the last change removed the TODO tag or switched to DONE, then
  8017. this is nil.")
  8018. (defvar org-setting-tags nil) ; dynamically skipped
  8019. (defun org-parse-local-options (string var)
  8020. "Parse STRING for startup setting relevant for variable VAR."
  8021. (let ((rtn (symbol-value var))
  8022. e opts)
  8023. (save-match-data
  8024. (if (or (not string) (not (string-match "\\S-" string)))
  8025. rtn
  8026. (setq opts (delq nil (mapcar (lambda (x)
  8027. (setq e (assoc x org-startup-options))
  8028. (if (eq (nth 1 e) var) e nil))
  8029. (org-split-string string "[ \t]+"))))
  8030. (if (not opts)
  8031. rtn
  8032. (setq rtn nil)
  8033. (while (setq e (pop opts))
  8034. (if (not (nth 3 e))
  8035. (setq rtn (nth 2 e))
  8036. (if (not (listp rtn)) (setq rtn nil))
  8037. (push (nth 2 e) rtn)))
  8038. rtn)))))
  8039. (defvar org-todo-setup-filter-hook nil
  8040. "Hook for functions that pre-filter todo specs.
  8041. Each function takes a todo spec and returns either `nil' or the spec
  8042. transformed into canonical form." )
  8043. (defvar org-todo-get-default-hook nil
  8044. "Hook for functions that get a default item for todo.
  8045. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8046. `nil' or a string to be used for the todo mark." )
  8047. (defvar org-agenda-headline-snapshot-before-repeat)
  8048. (defun org-todo (&optional arg)
  8049. "Change the TODO state of an item.
  8050. The state of an item is given by a keyword at the start of the heading,
  8051. like
  8052. *** TODO Write paper
  8053. *** DONE Call mom
  8054. The different keywords are specified in the variable `org-todo-keywords'.
  8055. By default the available states are \"TODO\" and \"DONE\".
  8056. So for this example: when the item starts with TODO, it is changed to DONE.
  8057. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8058. DONE are present, add TODO at the beginning of the heading.
  8059. With C-u prefix arg, use completion to determine the new state.
  8060. With numeric prefix arg, switch to that state.
  8061. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8062. With a tripple C-u prefix, circumvent any state blocking.
  8063. For calling through lisp, arg is also interpreted in the following way:
  8064. 'none -> empty state
  8065. \"\"(empty string) -> switch to empty state
  8066. 'done -> switch to DONE
  8067. 'nextset -> switch to the next set of keywords
  8068. 'previousset -> switch to the previous set of keywords
  8069. \"WAITING\" -> switch to the specified keyword, but only if it
  8070. really is a member of `org-todo-keywords'."
  8071. (interactive "P")
  8072. (if (equal arg '(16)) (setq arg 'nextset))
  8073. (let ((org-blocker-hook org-blocker-hook)
  8074. (case-fold-search nil))
  8075. (when (equal arg '(64))
  8076. (setq arg nil org-blocker-hook nil))
  8077. (when (and org-blocker-hook
  8078. (org-entry-get nil "NOBLOCKING"))
  8079. (setq org-blocker-hook nil))
  8080. (save-excursion
  8081. (catch 'exit
  8082. (org-back-to-heading)
  8083. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8084. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8085. (looking-at " *"))
  8086. (let* ((match-data (match-data))
  8087. (startpos (point-at-bol))
  8088. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8089. (org-log-done org-log-done)
  8090. (org-log-repeat org-log-repeat)
  8091. (org-todo-log-states org-todo-log-states)
  8092. (this (match-string 1))
  8093. (hl-pos (match-beginning 0))
  8094. (head (org-get-todo-sequence-head this))
  8095. (ass (assoc head org-todo-kwd-alist))
  8096. (interpret (nth 1 ass))
  8097. (done-word (nth 3 ass))
  8098. (final-done-word (nth 4 ass))
  8099. (last-state (or this ""))
  8100. (completion-ignore-case t)
  8101. (member (member this org-todo-keywords-1))
  8102. (tail (cdr member))
  8103. (state (cond
  8104. ((and org-todo-key-trigger
  8105. (or (and (equal arg '(4))
  8106. (eq org-use-fast-todo-selection 'prefix))
  8107. (and (not arg) org-use-fast-todo-selection
  8108. (not (eq org-use-fast-todo-selection
  8109. 'prefix)))))
  8110. ;; Use fast selection
  8111. (org-fast-todo-selection))
  8112. ((and (equal arg '(4))
  8113. (or (not org-use-fast-todo-selection)
  8114. (not org-todo-key-trigger)))
  8115. ;; Read a state with completion
  8116. (org-ido-completing-read
  8117. "State: " (mapcar (lambda(x) (list x))
  8118. org-todo-keywords-1)
  8119. nil t))
  8120. ((eq arg 'right)
  8121. (if this
  8122. (if tail (car tail) nil)
  8123. (car org-todo-keywords-1)))
  8124. ((eq arg 'left)
  8125. (if (equal member org-todo-keywords-1)
  8126. nil
  8127. (if this
  8128. (nth (- (length org-todo-keywords-1)
  8129. (length tail) 2)
  8130. org-todo-keywords-1)
  8131. (org-last org-todo-keywords-1))))
  8132. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8133. (setq arg nil))) ; hack to fall back to cycling
  8134. (arg
  8135. ;; user or caller requests a specific state
  8136. (cond
  8137. ((equal arg "") nil)
  8138. ((eq arg 'none) nil)
  8139. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8140. ((eq arg 'nextset)
  8141. (or (car (cdr (member head org-todo-heads)))
  8142. (car org-todo-heads)))
  8143. ((eq arg 'previousset)
  8144. (let ((org-todo-heads (reverse org-todo-heads)))
  8145. (or (car (cdr (member head org-todo-heads)))
  8146. (car org-todo-heads))))
  8147. ((car (member arg org-todo-keywords-1)))
  8148. ((nth (1- (prefix-numeric-value arg))
  8149. org-todo-keywords-1))))
  8150. ((null member) (or head (car org-todo-keywords-1)))
  8151. ((equal this final-done-word) nil) ;; -> make empty
  8152. ((null tail) nil) ;; -> first entry
  8153. ((memq interpret '(type priority))
  8154. (if (eq this-command last-command)
  8155. (car tail)
  8156. (if (> (length tail) 0)
  8157. (or done-word (car org-done-keywords))
  8158. nil)))
  8159. (t
  8160. (car tail))))
  8161. (state (or
  8162. (run-hook-with-args-until-success
  8163. 'org-todo-get-default-hook state last-state)
  8164. state))
  8165. (next (if state (concat " " state " ") " "))
  8166. (change-plist (list :type 'todo-state-change :from this :to state
  8167. :position startpos))
  8168. dolog now-done-p)
  8169. (when org-blocker-hook
  8170. (setq org-last-todo-state-is-todo
  8171. (not (member this org-done-keywords)))
  8172. (unless (save-excursion
  8173. (save-match-data
  8174. (run-hook-with-args-until-failure
  8175. 'org-blocker-hook change-plist)))
  8176. (if (interactive-p)
  8177. (error "TODO state change from %s to %s blocked" this state)
  8178. ;; fail silently
  8179. (message "TODO state change from %s to %s blocked" this state)
  8180. (throw 'exit nil))))
  8181. (store-match-data match-data)
  8182. (replace-match next t t)
  8183. (unless (pos-visible-in-window-p hl-pos)
  8184. (message "TODO state changed to %s" (org-trim next)))
  8185. (unless head
  8186. (setq head (org-get-todo-sequence-head state)
  8187. ass (assoc head org-todo-kwd-alist)
  8188. interpret (nth 1 ass)
  8189. done-word (nth 3 ass)
  8190. final-done-word (nth 4 ass)))
  8191. (when (memq arg '(nextset previousset))
  8192. (message "Keyword-Set %d/%d: %s"
  8193. (- (length org-todo-sets) -1
  8194. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8195. (length org-todo-sets)
  8196. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8197. (setq org-last-todo-state-is-todo
  8198. (not (member state org-done-keywords)))
  8199. (setq now-done-p (and (member state org-done-keywords)
  8200. (not (member this org-done-keywords))))
  8201. (and logging (org-local-logging logging))
  8202. (when (and (or org-todo-log-states org-log-done)
  8203. (not (memq arg '(nextset previousset))))
  8204. ;; we need to look at recording a time and note
  8205. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8206. (nth 2 (assoc this org-todo-log-states))))
  8207. (when (and state
  8208. (member state org-not-done-keywords)
  8209. (not (member this org-not-done-keywords)))
  8210. ;; This is now a todo state and was not one before
  8211. ;; If there was a CLOSED time stamp, get rid of it.
  8212. (org-add-planning-info nil nil 'closed))
  8213. (when (and now-done-p org-log-done)
  8214. ;; It is now done, and it was not done before
  8215. (org-add-planning-info 'closed (org-current-time))
  8216. (if (and (not dolog) (eq 'note org-log-done))
  8217. (org-add-log-setup 'done state this 'findpos 'note)))
  8218. (when (and state dolog)
  8219. ;; This is a non-nil state, and we need to log it
  8220. (org-add-log-setup 'state state this 'findpos dolog)))
  8221. ;; Fixup tag positioning
  8222. (org-todo-trigger-tag-changes state)
  8223. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8224. (when org-provide-todo-statistics
  8225. (org-update-parent-todo-statistics))
  8226. (run-hooks 'org-after-todo-state-change-hook)
  8227. (if (and arg (not (member state org-done-keywords)))
  8228. (setq head (org-get-todo-sequence-head state)))
  8229. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8230. ;; Do we need to trigger a repeat?
  8231. (when now-done-p
  8232. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8233. ;; This is for the agenda, take a snapshot of the headline.
  8234. (save-match-data
  8235. (setq org-agenda-headline-snapshot-before-repeat
  8236. (org-get-heading))))
  8237. (org-auto-repeat-maybe state))
  8238. ;; Fixup cursor location if close to the keyword
  8239. (if (and (outline-on-heading-p)
  8240. (not (bolp))
  8241. (save-excursion (beginning-of-line 1)
  8242. (looking-at org-todo-line-regexp))
  8243. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8244. (progn
  8245. (goto-char (or (match-end 2) (match-end 1)))
  8246. (and (looking-at " ") (just-one-space))))
  8247. (when org-trigger-hook
  8248. (save-excursion
  8249. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8250. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8251. "Block turning an entry into a TODO, using the hierarchy.
  8252. This checks whether the current task should be blocked from state
  8253. changes. Such blocking occurs when:
  8254. 1. The task has children which are not all in a completed state.
  8255. 2. A task has a parent with the property :ORDERED:, and there
  8256. are siblings prior to the current task with incomplete
  8257. status.
  8258. 3. The parent of the task is blocked because it has siblings that should
  8259. be done first, or is child of a block grandparent TODO entry."
  8260. (catch 'dont-block
  8261. ;; If this is not a todo state change, or if this entry is already DONE,
  8262. ;; do not block
  8263. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8264. (member (plist-get change-plist :from)
  8265. (cons 'done org-done-keywords))
  8266. (member (plist-get change-plist :to)
  8267. (cons 'todo org-not-done-keywords)))
  8268. (throw 'dont-block t))
  8269. ;; If this task has children, and any are undone, it's blocked
  8270. (save-excursion
  8271. (org-back-to-heading t)
  8272. (let ((this-level (funcall outline-level)))
  8273. (outline-next-heading)
  8274. (let ((child-level (funcall outline-level)))
  8275. (while (and (not (eobp))
  8276. (> child-level this-level))
  8277. ;; this todo has children, check whether they are all
  8278. ;; completed
  8279. (if (and (not (org-entry-is-done-p))
  8280. (org-entry-is-todo-p))
  8281. (throw 'dont-block nil))
  8282. (outline-next-heading)
  8283. (setq child-level (funcall outline-level))))))
  8284. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8285. ;; any previous siblings are undone, it's blocked
  8286. (save-excursion
  8287. (org-back-to-heading t)
  8288. (let* ((pos (point))
  8289. (parent-pos (and (org-up-heading-safe) (point))))
  8290. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8291. (when (and (org-entry-get (point) "ORDERED")
  8292. (forward-line 1)
  8293. (re-search-forward org-not-done-heading-regexp pos t))
  8294. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8295. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8296. (while t
  8297. (goto-char parent-pos)
  8298. (if (not (looking-at org-not-done-heading-regexp))
  8299. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8300. (setq pos (point))
  8301. (setq parent-pos (and (org-up-heading-safe) (point)))
  8302. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8303. (when (and (org-entry-get (point) "ORDERED")
  8304. (forward-line 1)
  8305. (re-search-forward org-not-done-heading-regexp pos t))
  8306. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8307. (defcustom org-track-ordered-property-with-tag nil
  8308. "Should the ORDERED property also be shown as a tag?
  8309. The ORDERED property decides if an entry should require subtasks to be
  8310. completed in sequence. Since a property is not very visible, setting
  8311. this option means that toggling the ORDERED property with the command
  8312. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8313. not relevant for the behavior, but it makes things more visible.
  8314. Note that toggling the tag with tags commands will not change the property
  8315. and therefore not influence behavior!
  8316. This can be t, meaning the tag ORDERED should be used, It can also be a
  8317. string to select a different tag for this task."
  8318. :group 'org-todo
  8319. :type '(choice
  8320. (const :tag "No tracking" nil)
  8321. (const :tag "Track with ORDERED tag" t)
  8322. (string :tag "Use other tag")))
  8323. (defun org-toggle-ordered-property ()
  8324. "Toggle the ORDERED property of the current entry.
  8325. For better visibility, you can track the value of this property with a tag.
  8326. See variable `org-track-ordered-property-with-tag'."
  8327. (interactive)
  8328. (let* ((t1 org-track-ordered-property-with-tag)
  8329. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8330. (save-excursion
  8331. (org-back-to-heading)
  8332. (if (org-entry-get nil "ORDERED")
  8333. (progn
  8334. (org-delete-property "ORDERED")
  8335. (and tag (org-toggle-tag tag 'off))
  8336. (message "Subtasks can be completed in arbitrary order"))
  8337. (org-entry-put nil "ORDERED" "t")
  8338. (and tag (org-toggle-tag tag 'on))
  8339. (message "Subtasks must be completed in sequence")))))
  8340. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8341. (defun org-block-todo-from-checkboxes (change-plist)
  8342. "Block turning an entry into a TODO, using checkboxes.
  8343. This checks whether the current task should be blocked from state
  8344. changes because there are uncheckd boxes in this entry."
  8345. (catch 'dont-block
  8346. ;; If this is not a todo state change, or if this entry is already DONE,
  8347. ;; do not block
  8348. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8349. (member (plist-get change-plist :from)
  8350. (cons 'done org-done-keywords))
  8351. (member (plist-get change-plist :to)
  8352. (cons 'todo org-not-done-keywords)))
  8353. (throw 'dont-block t))
  8354. ;; If this task has checkboxes that are not checked, it's blocked
  8355. (save-excursion
  8356. (org-back-to-heading t)
  8357. (let ((beg (point)) end)
  8358. (outline-next-heading)
  8359. (setq end (point))
  8360. (goto-char beg)
  8361. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8362. end t)
  8363. (progn
  8364. (if (boundp 'org-blocked-by-checkboxes)
  8365. (setq org-blocked-by-checkboxes t))
  8366. (throw 'dont-block nil)))))
  8367. t)) ; do not block
  8368. (defun org-update-parent-todo-statistics ()
  8369. "Update any statistics cookie in the parent of the current headline."
  8370. (interactive)
  8371. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8372. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8373. (catch 'exit
  8374. (save-excursion
  8375. (setq level (org-up-heading-safe))
  8376. (unless (and level
  8377. (not (equal (downcase
  8378. (or (org-entry-get
  8379. nil "COOKIE_DATA")
  8380. ""))
  8381. "checkbox")))
  8382. (throw 'exit nil))
  8383. (while (re-search-forward box-re (point-at-eol) t)
  8384. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8385. (setq is-percent (match-end 2))
  8386. (save-match-data
  8387. (unless (outline-next-heading) (throw 'exit nil))
  8388. (while (looking-at org-todo-line-regexp)
  8389. (setq kwd (match-string 2))
  8390. (and kwd (setq cnt-all (1+ cnt-all)))
  8391. (and (member kwd org-done-keywords)
  8392. (setq cnt-done (1+ cnt-done)))
  8393. (condition-case nil
  8394. (org-forward-same-level 1)
  8395. (error (end-of-line 1)))))
  8396. (replace-match
  8397. (if is-percent
  8398. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8399. (format "[%d/%d]" cnt-done cnt-all))))
  8400. (when cookie-present
  8401. (run-hook-with-args 'org-after-todo-statistics-hook
  8402. cnt-done (- cnt-all cnt-done)))))))
  8403. (defvar org-after-todo-statistics-hook nil
  8404. "Hook that is called after a TODO statistics cookie has been updated.
  8405. Each function is called with two arguments: the number of not-done entries
  8406. and the number of done entries.
  8407. For example, the following function, when added to this hook, will switch
  8408. an entry to DONE when all children are done, and back to TODO when new
  8409. entries are set to a TODO status. Note that this hook is only called
  8410. when there is a statistics cookie in the headline!
  8411. (defun org-summary-todo (n-done n-not-done)
  8412. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8413. (let (org-log-done org-log-states) ; turn off logging
  8414. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8415. ")
  8416. (defun org-todo-trigger-tag-changes (state)
  8417. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8418. (let ((l org-todo-state-tags-triggers)
  8419. changes)
  8420. (when (or (not state) (equal state ""))
  8421. (setq changes (append changes (cdr (assoc "" l)))))
  8422. (when (and (stringp state) (> (length state) 0))
  8423. (setq changes (append changes (cdr (assoc state l)))))
  8424. (when (member state org-not-done-keywords)
  8425. (setq changes (append changes (cdr (assoc 'todo l)))))
  8426. (when (member state org-done-keywords)
  8427. (setq changes (append changes (cdr (assoc 'done l)))))
  8428. (dolist (c changes)
  8429. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8430. (defun org-local-logging (value)
  8431. "Get logging settings from a property VALUE."
  8432. (let* (words w a)
  8433. ;; directly set the variables, they are already local.
  8434. (setq org-log-done nil
  8435. org-log-repeat nil
  8436. org-todo-log-states nil)
  8437. (setq words (org-split-string value))
  8438. (while (setq w (pop words))
  8439. (cond
  8440. ((setq a (assoc w org-startup-options))
  8441. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8442. (set (nth 1 a) (nth 2 a))))
  8443. ((setq a (org-extract-log-state-settings w))
  8444. (and (member (car a) org-todo-keywords-1)
  8445. (push a org-todo-log-states)))))))
  8446. (defun org-get-todo-sequence-head (kwd)
  8447. "Return the head of the TODO sequence to which KWD belongs.
  8448. If KWD is not set, check if there is a text property remembering the
  8449. right sequence."
  8450. (let (p)
  8451. (cond
  8452. ((not kwd)
  8453. (or (get-text-property (point-at-bol) 'org-todo-head)
  8454. (progn
  8455. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8456. nil (point-at-eol)))
  8457. (get-text-property p 'org-todo-head))))
  8458. ((not (member kwd org-todo-keywords-1))
  8459. (car org-todo-keywords-1))
  8460. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8461. (defun org-fast-todo-selection ()
  8462. "Fast TODO keyword selection with single keys.
  8463. Returns the new TODO keyword, or nil if no state change should occur."
  8464. (let* ((fulltable org-todo-key-alist)
  8465. (done-keywords org-done-keywords) ;; needed for the faces.
  8466. (maxlen (apply 'max (mapcar
  8467. (lambda (x)
  8468. (if (stringp (car x)) (string-width (car x)) 0))
  8469. fulltable)))
  8470. (expert nil)
  8471. (fwidth (+ maxlen 3 1 3))
  8472. (ncol (/ (- (window-width) 4) fwidth))
  8473. tg cnt e c tbl
  8474. groups ingroup)
  8475. (save-excursion
  8476. (save-window-excursion
  8477. (if expert
  8478. (set-buffer (get-buffer-create " *Org todo*"))
  8479. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8480. (erase-buffer)
  8481. (org-set-local 'org-done-keywords done-keywords)
  8482. (setq tbl fulltable cnt 0)
  8483. (while (setq e (pop tbl))
  8484. (cond
  8485. ((equal e '(:startgroup))
  8486. (push '() groups) (setq ingroup t)
  8487. (when (not (= cnt 0))
  8488. (setq cnt 0)
  8489. (insert "\n"))
  8490. (insert "{ "))
  8491. ((equal e '(:endgroup))
  8492. (setq ingroup nil cnt 0)
  8493. (insert "}\n"))
  8494. ((equal e '(:newline))
  8495. (when (not (= cnt 0))
  8496. (setq cnt 0)
  8497. (insert "\n")
  8498. (setq e (car tbl))
  8499. (while (equal (car tbl) '(:newline))
  8500. (insert "\n")
  8501. (setq tbl (cdr tbl)))))
  8502. (t
  8503. (setq tg (car e) c (cdr e))
  8504. (if ingroup (push tg (car groups)))
  8505. (setq tg (org-add-props tg nil 'face
  8506. (org-get-todo-face tg)))
  8507. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8508. (insert "[" c "] " tg (make-string
  8509. (- fwidth 4 (length tg)) ?\ ))
  8510. (when (= (setq cnt (1+ cnt)) ncol)
  8511. (insert "\n")
  8512. (if ingroup (insert " "))
  8513. (setq cnt 0)))))
  8514. (insert "\n")
  8515. (goto-char (point-min))
  8516. (if (not expert) (org-fit-window-to-buffer))
  8517. (message "[a-z..]:Set [SPC]:clear")
  8518. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8519. (cond
  8520. ((or (= c ?\C-g)
  8521. (and (= c ?q) (not (rassoc c fulltable))))
  8522. (setq quit-flag t))
  8523. ((= c ?\ ) nil)
  8524. ((setq e (rassoc c fulltable) tg (car e))
  8525. tg)
  8526. (t (setq quit-flag t)))))))
  8527. (defun org-entry-is-todo-p ()
  8528. (member (org-get-todo-state) org-not-done-keywords))
  8529. (defun org-entry-is-done-p ()
  8530. (member (org-get-todo-state) org-done-keywords))
  8531. (defun org-get-todo-state ()
  8532. (save-excursion
  8533. (org-back-to-heading t)
  8534. (and (looking-at org-todo-line-regexp)
  8535. (match-end 2)
  8536. (match-string 2))))
  8537. (defun org-at-date-range-p (&optional inactive-ok)
  8538. "Is the cursor inside a date range?"
  8539. (interactive)
  8540. (save-excursion
  8541. (catch 'exit
  8542. (let ((pos (point)))
  8543. (skip-chars-backward "^[<\r\n")
  8544. (skip-chars-backward "<[")
  8545. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8546. (>= (match-end 0) pos)
  8547. (throw 'exit t))
  8548. (skip-chars-backward "^<[\r\n")
  8549. (skip-chars-backward "<[")
  8550. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8551. (>= (match-end 0) pos)
  8552. (throw 'exit t)))
  8553. nil)))
  8554. (defun org-get-repeat ()
  8555. "Check if there is a deadline/schedule with repeater in this entry."
  8556. (save-match-data
  8557. (save-excursion
  8558. (org-back-to-heading t)
  8559. (if (re-search-forward
  8560. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8561. (match-string 1)))))
  8562. (defvar org-last-changed-timestamp)
  8563. (defvar org-last-inserted-timestamp)
  8564. (defvar org-log-post-message)
  8565. (defvar org-log-note-purpose)
  8566. (defvar org-log-note-how)
  8567. (defvar org-log-note-extra)
  8568. (defun org-auto-repeat-maybe (done-word)
  8569. "Check if the current headline contains a repeated deadline/schedule.
  8570. If yes, set TODO state back to what it was and change the base date
  8571. of repeating deadline/scheduled time stamps to new date.
  8572. This function is run automatically after each state change to a DONE state."
  8573. ;; last-state is dynamically scoped into this function
  8574. (let* ((repeat (org-get-repeat))
  8575. (aa (assoc last-state org-todo-kwd-alist))
  8576. (interpret (nth 1 aa))
  8577. (head (nth 2 aa))
  8578. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8579. (msg "Entry repeats: ")
  8580. (org-log-done nil)
  8581. (org-todo-log-states nil)
  8582. (nshiftmax 10) (nshift 0)
  8583. re type n what ts time)
  8584. (when repeat
  8585. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8586. (org-todo (if (eq interpret 'type) last-state head))
  8587. (when org-log-repeat
  8588. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8589. (memq 'org-add-log-note post-command-hook))
  8590. ;; OK, we are already setup for some record
  8591. (if (eq org-log-repeat 'note)
  8592. ;; make sure we take a note, not only a time stamp
  8593. (setq org-log-note-how 'note))
  8594. ;; Set up for taking a record
  8595. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8596. last-state
  8597. 'findpos org-log-repeat)))
  8598. (org-back-to-heading t)
  8599. (org-add-planning-info nil nil 'closed)
  8600. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8601. org-deadline-time-regexp "\\)\\|\\("
  8602. org-ts-regexp "\\)"))
  8603. (while (re-search-forward
  8604. re (save-excursion (outline-next-heading) (point)) t)
  8605. (setq type (if (match-end 1) org-scheduled-string
  8606. (if (match-end 3) org-deadline-string "Plain:"))
  8607. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8608. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8609. (setq n (string-to-number (match-string 2 ts))
  8610. what (match-string 3 ts))
  8611. (if (equal what "w") (setq n (* n 7) what "d"))
  8612. ;; Preparation, see if we need to modify the start date for the change
  8613. (when (match-end 1)
  8614. (setq time (save-match-data (org-time-string-to-time ts)))
  8615. (cond
  8616. ((equal (match-string 1 ts) ".")
  8617. ;; Shift starting date to today
  8618. (org-timestamp-change
  8619. (- (time-to-days (current-time)) (time-to-days time))
  8620. 'day))
  8621. ((equal (match-string 1 ts) "+")
  8622. (while (or (= nshift 0)
  8623. (<= (time-to-days time) (time-to-days (current-time))))
  8624. (when (= (incf nshift) nshiftmax)
  8625. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8626. (error "Abort")))
  8627. (org-timestamp-change n (cdr (assoc what whata)))
  8628. (org-at-timestamp-p t)
  8629. (setq ts (match-string 1))
  8630. (setq time (save-match-data (org-time-string-to-time ts))))
  8631. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8632. ;; rematch, so that we have everything in place for the real shift
  8633. (org-at-timestamp-p t)
  8634. (setq ts (match-string 1))
  8635. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8636. (org-timestamp-change n (cdr (assoc what whata)))
  8637. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8638. (setq org-log-post-message msg)
  8639. (message "%s" msg))))
  8640. (defun org-show-todo-tree (arg)
  8641. "Make a compact tree which shows all headlines marked with TODO.
  8642. The tree will show the lines where the regexp matches, and all higher
  8643. headlines above the match.
  8644. With a \\[universal-argument] prefix, also show the DONE entries.
  8645. With a numeric prefix N, construct a sparse tree for the Nth element
  8646. of `org-todo-keywords-1'."
  8647. (interactive "P")
  8648. (let ((case-fold-search nil)
  8649. (kwd-re
  8650. (cond ((null arg) org-not-done-regexp)
  8651. ((equal arg '(4))
  8652. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8653. (mapcar 'list org-todo-keywords-1))))
  8654. (concat "\\("
  8655. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8656. "\\)\\>")))
  8657. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8658. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8659. org-todo-keywords-1)))
  8660. (t (error "Invalid prefix argument: %s" arg)))))
  8661. (message "%d TODO entries found"
  8662. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8663. (defun org-deadline (&optional remove time)
  8664. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8665. With argument REMOVE, remove any deadline from the item.
  8666. When TIME is set, it should be an internal time specification, and the
  8667. scheduling will use the corresponding date."
  8668. (interactive "P")
  8669. (if remove
  8670. (progn
  8671. (org-remove-timestamp-with-keyword org-deadline-string)
  8672. (message "Item no longer has a deadline."))
  8673. (if (org-get-repeat)
  8674. (error "Cannot change deadline on task with repeater, please do that by hand")
  8675. (org-add-planning-info 'deadline time 'closed)
  8676. (message "Deadline on %s" org-last-inserted-timestamp))))
  8677. (defun org-schedule (&optional remove time)
  8678. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8679. With argument REMOVE, remove any scheduling date from the item.
  8680. When TIME is set, it should be an internal time specification, and the
  8681. scheduling will use the corresponding date."
  8682. (interactive "P")
  8683. (if remove
  8684. (progn
  8685. (org-remove-timestamp-with-keyword org-scheduled-string)
  8686. (message "Item is no longer scheduled."))
  8687. (if (org-get-repeat)
  8688. (error "Cannot reschedule task with repeater, please do that by hand")
  8689. (org-add-planning-info 'scheduled time 'closed)
  8690. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8691. (defun org-get-scheduled-time (pom &optional inherit)
  8692. "Get the scheduled time as a time tuple, of a format suitable
  8693. for calling org-schedule with, or if there is no scheduling,
  8694. returns nil."
  8695. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8696. (when time
  8697. (apply 'encode-time (org-parse-time-string time)))))
  8698. (defun org-get-deadline-time (pom &optional inherit)
  8699. "Get the deadine as a time tuple, of a format suitable for
  8700. calling org-deadlin with, or if there is no scheduling, returns
  8701. nil."
  8702. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8703. (when time
  8704. (apply 'encode-time (org-parse-time-string time)))))
  8705. (defun org-remove-timestamp-with-keyword (keyword)
  8706. "Remove all time stamps with KEYWORD in the current entry."
  8707. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8708. beg)
  8709. (save-excursion
  8710. (org-back-to-heading t)
  8711. (setq beg (point))
  8712. (org-end-of-subtree t t)
  8713. (while (re-search-backward re beg t)
  8714. (replace-match "")
  8715. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8716. (equal (char-before) ?\ ))
  8717. (backward-delete-char 1)
  8718. (if (string-match "^[ \t]*$" (buffer-substring
  8719. (point-at-bol) (point-at-eol)))
  8720. (delete-region (point-at-bol)
  8721. (min (point-max) (1+ (point-at-eol))))))))))
  8722. (defun org-add-planning-info (what &optional time &rest remove)
  8723. "Insert new timestamp with keyword in the line directly after the headline.
  8724. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8725. If non is given, the user is prompted for a date.
  8726. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8727. be removed."
  8728. (interactive)
  8729. (let (org-time-was-given org-end-time-was-given ts
  8730. end default-time default-input)
  8731. (catch 'exit
  8732. (when (and (not time) (memq what '(scheduled deadline)))
  8733. ;; Try to get a default date/time from existing timestamp
  8734. (save-excursion
  8735. (org-back-to-heading t)
  8736. (setq end (save-excursion (outline-next-heading) (point)))
  8737. (when (re-search-forward (if (eq what 'scheduled)
  8738. org-scheduled-time-regexp
  8739. org-deadline-time-regexp)
  8740. end t)
  8741. (setq ts (match-string 1)
  8742. default-time
  8743. (apply 'encode-time (org-parse-time-string ts))
  8744. default-input (and ts (org-get-compact-tod ts))))))
  8745. (when what
  8746. ;; If necessary, get the time from the user
  8747. (setq time (or time (org-read-date nil 'to-time nil nil
  8748. default-time default-input))))
  8749. (when (and org-insert-labeled-timestamps-at-point
  8750. (member what '(scheduled deadline)))
  8751. (insert
  8752. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8753. (org-insert-time-stamp time org-time-was-given
  8754. nil nil nil (list org-end-time-was-given))
  8755. (setq what nil))
  8756. (save-excursion
  8757. (save-restriction
  8758. (let (col list elt ts buffer-invisibility-spec)
  8759. (org-back-to-heading t)
  8760. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8761. (goto-char (match-end 1))
  8762. (setq col (current-column))
  8763. (goto-char (match-end 0))
  8764. (if (eobp) (insert "\n") (forward-char 1))
  8765. (when (and (not what)
  8766. (not (looking-at
  8767. (concat "[ \t]*"
  8768. org-keyword-time-not-clock-regexp))))
  8769. ;; Nothing to add, nothing to remove...... :-)
  8770. (throw 'exit nil))
  8771. (if (and (not (looking-at outline-regexp))
  8772. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8773. "[^\r\n]*"))
  8774. (not (equal (match-string 1) org-clock-string)))
  8775. (narrow-to-region (match-beginning 0) (match-end 0))
  8776. (insert-before-markers "\n")
  8777. (backward-char 1)
  8778. (narrow-to-region (point) (point))
  8779. (and org-adapt-indentation (org-indent-to-column col)))
  8780. ;; Check if we have to remove something.
  8781. (setq list (cons what remove))
  8782. (while list
  8783. (setq elt (pop list))
  8784. (goto-char (point-min))
  8785. (when (or (and (eq elt 'scheduled)
  8786. (re-search-forward org-scheduled-time-regexp nil t))
  8787. (and (eq elt 'deadline)
  8788. (re-search-forward org-deadline-time-regexp nil t))
  8789. (and (eq elt 'closed)
  8790. (re-search-forward org-closed-time-regexp nil t)))
  8791. (replace-match "")
  8792. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8793. (if (looking-at " +") (replace-match ""))))
  8794. (goto-char (point-max))
  8795. (when what
  8796. (insert
  8797. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8798. (cond ((eq what 'scheduled) org-scheduled-string)
  8799. ((eq what 'deadline) org-deadline-string)
  8800. ((eq what 'closed) org-closed-string))
  8801. " ")
  8802. (setq ts (org-insert-time-stamp
  8803. time
  8804. (or org-time-was-given
  8805. (and (eq what 'closed) org-log-done-with-time))
  8806. (eq what 'closed)
  8807. nil nil (list org-end-time-was-given)))
  8808. (end-of-line 1))
  8809. (goto-char (point-min))
  8810. (widen)
  8811. (if (and (looking-at "[ \t]+\n")
  8812. (equal (char-before) ?\n))
  8813. (delete-region (1- (point)) (point-at-eol)))
  8814. ts))))))
  8815. (defvar org-log-note-marker (make-marker))
  8816. (defvar org-log-note-purpose nil)
  8817. (defvar org-log-note-state nil)
  8818. (defvar org-log-note-previous-state nil)
  8819. (defvar org-log-note-how nil)
  8820. (defvar org-log-note-extra nil)
  8821. (defvar org-log-note-window-configuration nil)
  8822. (defvar org-log-note-return-to (make-marker))
  8823. (defvar org-log-post-message nil
  8824. "Message to be displayed after a log note has been stored.
  8825. The auto-repeater uses this.")
  8826. (defun org-add-note ()
  8827. "Add a note to the current entry.
  8828. This is done in the same way as adding a state change note."
  8829. (interactive)
  8830. (org-add-log-setup 'note nil nil 'findpos nil))
  8831. (defvar org-property-end-re)
  8832. (defun org-add-log-setup (&optional purpose state prev-state
  8833. findpos how &optional extra)
  8834. "Set up the post command hook to take a note.
  8835. If this is about to TODO state change, the new state is expected in STATE.
  8836. When FINDPOS is non-nil, find the correct position for the note in
  8837. the current entry. If not, assume that it can be inserted at point.
  8838. HOW is an indicator what kind of note should be created.
  8839. EXTRA is additional text that will be inserted into the notes buffer."
  8840. (let ((drawer (cond ((stringp org-log-into-drawer)
  8841. org-log-into-drawer)
  8842. (org-log-into-drawer "LOGBOOK")
  8843. (t nil))))
  8844. (save-restriction
  8845. (save-excursion
  8846. (when findpos
  8847. (org-back-to-heading t)
  8848. (narrow-to-region (point) (save-excursion
  8849. (outline-next-heading) (point)))
  8850. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8851. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8852. "[^\r\n]*\\)?"))
  8853. (goto-char (match-end 0))
  8854. (cond
  8855. (drawer
  8856. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8857. nil t)
  8858. (progn
  8859. (goto-char (match-end 0))
  8860. (or org-log-states-order-reversed
  8861. (and (re-search-forward org-property-end-re nil t)
  8862. (goto-char (1- (match-beginning 0))))))
  8863. (insert "\n:" drawer ":\n:END:")
  8864. (beginning-of-line 0)
  8865. (org-indent-line-function)
  8866. (beginning-of-line 2)
  8867. (org-indent-line-function)
  8868. (end-of-line 0)))
  8869. ((and org-log-state-notes-insert-after-drawers
  8870. (save-excursion
  8871. (forward-line) (looking-at org-drawer-regexp)))
  8872. (forward-line)
  8873. (while (looking-at org-drawer-regexp)
  8874. (goto-char (match-end 0))
  8875. (re-search-forward org-property-end-re (point-max) t)
  8876. (forward-line))
  8877. (forward-line -1)))
  8878. (unless org-log-states-order-reversed
  8879. (and (= (char-after) ?\n) (forward-char 1))
  8880. (org-skip-over-state-notes)
  8881. (skip-chars-backward " \t\n\r")))
  8882. (move-marker org-log-note-marker (point))
  8883. (setq org-log-note-purpose purpose
  8884. org-log-note-state state
  8885. org-log-note-previous-state prev-state
  8886. org-log-note-how how
  8887. org-log-note-extra extra)
  8888. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8889. (defun org-skip-over-state-notes ()
  8890. "Skip past the list of State notes in an entry."
  8891. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8892. (while (looking-at "[ \t]*- State")
  8893. (condition-case nil
  8894. (org-next-item)
  8895. (error (org-end-of-item)))))
  8896. (defun org-add-log-note (&optional purpose)
  8897. "Pop up a window for taking a note, and add this note later at point."
  8898. (remove-hook 'post-command-hook 'org-add-log-note)
  8899. (setq org-log-note-window-configuration (current-window-configuration))
  8900. (delete-other-windows)
  8901. (move-marker org-log-note-return-to (point))
  8902. (switch-to-buffer (marker-buffer org-log-note-marker))
  8903. (goto-char org-log-note-marker)
  8904. (org-switch-to-buffer-other-window "*Org Note*")
  8905. (erase-buffer)
  8906. (if (memq org-log-note-how '(time state))
  8907. (let (current-prefix-arg) (org-store-log-note))
  8908. (let ((org-inhibit-startup t)) (org-mode))
  8909. (insert (format "# Insert note for %s.
  8910. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8911. (cond
  8912. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8913. ((eq org-log-note-purpose 'done) "closed todo item")
  8914. ((eq org-log-note-purpose 'state)
  8915. (format "state change from \"%s\" to \"%s\""
  8916. (or org-log-note-previous-state "")
  8917. (or org-log-note-state "")))
  8918. ((eq org-log-note-purpose 'note)
  8919. "this entry")
  8920. (t (error "This should not happen")))))
  8921. (if org-log-note-extra (insert org-log-note-extra))
  8922. (org-set-local 'org-finish-function 'org-store-log-note)))
  8923. (defvar org-note-abort nil) ; dynamically scoped
  8924. (defun org-store-log-note ()
  8925. "Finish taking a log note, and insert it to where it belongs."
  8926. (let ((txt (buffer-string))
  8927. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8928. lines ind)
  8929. (kill-buffer (current-buffer))
  8930. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8931. (setq txt (replace-match "" t t txt)))
  8932. (if (string-match "\\s-+\\'" txt)
  8933. (setq txt (replace-match "" t t txt)))
  8934. (setq lines (org-split-string txt "\n"))
  8935. (when (and note (string-match "\\S-" note))
  8936. (setq note
  8937. (org-replace-escapes
  8938. note
  8939. (list (cons "%u" (user-login-name))
  8940. (cons "%U" user-full-name)
  8941. (cons "%t" (format-time-string
  8942. (org-time-stamp-format 'long 'inactive)
  8943. (current-time)))
  8944. (cons "%s" (if org-log-note-state
  8945. (concat "\"" org-log-note-state "\"")
  8946. ""))
  8947. (cons "%S" (if org-log-note-previous-state
  8948. (concat "\"" org-log-note-previous-state "\"")
  8949. "\"\"")))))
  8950. (if lines (setq note (concat note " \\\\")))
  8951. (push note lines))
  8952. (when (or current-prefix-arg org-note-abort)
  8953. (when org-log-into-drawer
  8954. (org-remove-empty-drawer-at
  8955. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8956. org-log-note-marker))
  8957. (setq lines nil))
  8958. (when lines
  8959. (save-excursion
  8960. (set-buffer (marker-buffer org-log-note-marker))
  8961. (save-excursion
  8962. (goto-char org-log-note-marker)
  8963. (move-marker org-log-note-marker nil)
  8964. (end-of-line 1)
  8965. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8966. (insert "- " (pop lines))
  8967. (org-indent-line-function)
  8968. (beginning-of-line 1)
  8969. (looking-at "[ \t]*")
  8970. (setq ind (concat (match-string 0) " "))
  8971. (end-of-line 1)
  8972. (while lines (insert "\n" ind (pop lines)))
  8973. (message "Note stored")
  8974. (org-back-to-heading t)
  8975. (org-cycle-hide-drawers 'children)))))
  8976. (set-window-configuration org-log-note-window-configuration)
  8977. (with-current-buffer (marker-buffer org-log-note-return-to)
  8978. (goto-char org-log-note-return-to))
  8979. (move-marker org-log-note-return-to nil)
  8980. (and org-log-post-message (message "%s" org-log-post-message)))
  8981. (defun org-remove-empty-drawer-at (drawer pos)
  8982. "Remove an emptyr DARWER drawer at position POS.
  8983. POS may also be a marker."
  8984. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8985. (save-excursion
  8986. (save-restriction
  8987. (widen)
  8988. (goto-char pos)
  8989. (if (org-in-regexp
  8990. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8991. (replace-match ""))))))
  8992. (defun org-sparse-tree (&optional arg)
  8993. "Create a sparse tree, prompt for the details.
  8994. This command can create sparse trees. You first need to select the type
  8995. of match used to create the tree:
  8996. t Show entries with a specific TODO keyword.
  8997. m Show entries selected by a tags/property match.
  8998. p Enter a property name and its value (both with completion on existing
  8999. names/values) and show entries with that property.
  9000. r Show entries matching a regular expression.
  9001. d Show deadlines due within `org-deadline-warning-days'.
  9002. b Show deadlines and scheduled items before a date.
  9003. a Show deadlines and scheduled items after a date."
  9004. (interactive "P")
  9005. (let (ans kwd value)
  9006. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9007. (setq ans (read-char-exclusive))
  9008. (cond
  9009. ((equal ans ?d)
  9010. (call-interactively 'org-check-deadlines))
  9011. ((equal ans ?b)
  9012. (call-interactively 'org-check-before-date))
  9013. ((equal ans ?a)
  9014. (call-interactively 'org-check-after-date))
  9015. ((equal ans ?t)
  9016. (org-show-todo-tree '(4)))
  9017. ((member ans '(?T ?m))
  9018. (call-interactively 'org-match-sparse-tree))
  9019. ((member ans '(?p ?P))
  9020. (setq kwd (org-ido-completing-read "Property: "
  9021. (mapcar 'list (org-buffer-property-keys))))
  9022. (setq value (org-ido-completing-read "Value: "
  9023. (mapcar 'list (org-property-values kwd))))
  9024. (unless (string-match "\\`{.*}\\'" value)
  9025. (setq value (concat "\"" value "\"")))
  9026. (org-match-sparse-tree arg (concat kwd "=" value)))
  9027. ((member ans '(?r ?R ?/))
  9028. (call-interactively 'org-occur))
  9029. (t (error "No such sparse tree command \"%c\"" ans)))))
  9030. (defvar org-occur-highlights nil
  9031. "List of overlays used for occur matches.")
  9032. (make-variable-buffer-local 'org-occur-highlights)
  9033. (defvar org-occur-parameters nil
  9034. "Parameters of the active org-occur calls.
  9035. This is a list, each call to org-occur pushes as cons cell,
  9036. containing the regular expression and the callback, onto the list.
  9037. The list can contain several entries if `org-occur' has been called
  9038. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9039. will only contain one set of parameters. When the highlights are
  9040. removed (for example with `C-c C-c', or with the next edit (depending
  9041. on `org-remove-highlights-with-change'), this variable is emptied
  9042. as well.")
  9043. (make-variable-buffer-local 'org-occur-parameters)
  9044. (defun org-occur (regexp &optional keep-previous callback)
  9045. "Make a compact tree which shows all matches of REGEXP.
  9046. The tree will show the lines where the regexp matches, and all higher
  9047. headlines above the match. It will also show the heading after the match,
  9048. to make sure editing the matching entry is easy.
  9049. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9050. call to `org-occur' will be kept, to allow stacking of calls to this
  9051. command.
  9052. If CALLBACK is non-nil, it is a function which is called to confirm
  9053. that the match should indeed be shown."
  9054. (interactive "sRegexp: \nP")
  9055. (unless keep-previous
  9056. (org-remove-occur-highlights nil nil t))
  9057. (push (cons regexp callback) org-occur-parameters)
  9058. (let ((cnt 0))
  9059. (save-excursion
  9060. (goto-char (point-min))
  9061. (if (or (not keep-previous) ; do not want to keep
  9062. (not org-occur-highlights)) ; no previous matches
  9063. ;; hide everything
  9064. (org-overview))
  9065. (while (re-search-forward regexp nil t)
  9066. (when (or (not callback)
  9067. (save-match-data (funcall callback)))
  9068. (setq cnt (1+ cnt))
  9069. (when org-highlight-sparse-tree-matches
  9070. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9071. (org-show-context 'occur-tree))))
  9072. (when org-remove-highlights-with-change
  9073. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9074. nil 'local))
  9075. (unless org-sparse-tree-open-archived-trees
  9076. (org-hide-archived-subtrees (point-min) (point-max)))
  9077. (run-hooks 'org-occur-hook)
  9078. (if (interactive-p)
  9079. (message "%d match(es) for regexp %s" cnt regexp))
  9080. cnt))
  9081. (defun org-show-context (&optional key)
  9082. "Make sure point and context and visible.
  9083. How much context is shown depends upon the variables
  9084. `org-show-hierarchy-above', `org-show-following-heading'. and
  9085. `org-show-siblings'."
  9086. (let ((heading-p (org-on-heading-p t))
  9087. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9088. (following-p (org-get-alist-option org-show-following-heading key))
  9089. (entry-p (org-get-alist-option org-show-entry-below key))
  9090. (siblings-p (org-get-alist-option org-show-siblings key)))
  9091. (catch 'exit
  9092. ;; Show heading or entry text
  9093. (if (and heading-p (not entry-p))
  9094. (org-flag-heading nil) ; only show the heading
  9095. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9096. (org-show-hidden-entry))) ; show entire entry
  9097. (when following-p
  9098. ;; Show next sibling, or heading below text
  9099. (save-excursion
  9100. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9101. (org-flag-heading nil))))
  9102. (when siblings-p (org-show-siblings))
  9103. (when hierarchy-p
  9104. ;; show all higher headings, possibly with siblings
  9105. (save-excursion
  9106. (while (and (condition-case nil
  9107. (progn (org-up-heading-all 1) t)
  9108. (error nil))
  9109. (not (bobp)))
  9110. (org-flag-heading nil)
  9111. (when siblings-p (org-show-siblings))))))))
  9112. (defun org-reveal (&optional siblings)
  9113. "Show current entry, hierarchy above it, and the following headline.
  9114. This can be used to show a consistent set of context around locations
  9115. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9116. not t for the search context.
  9117. With optional argument SIBLINGS, on each level of the hierarchy all
  9118. siblings are shown. This repairs the tree structure to what it would
  9119. look like when opened with hierarchical calls to `org-cycle'."
  9120. (interactive "P")
  9121. (let ((org-show-hierarchy-above t)
  9122. (org-show-following-heading t)
  9123. (org-show-siblings (if siblings t org-show-siblings)))
  9124. (org-show-context nil)))
  9125. (defun org-highlight-new-match (beg end)
  9126. "Highlight from BEG to END and mark the highlight is an occur headline."
  9127. (let ((ov (org-make-overlay beg end)))
  9128. (org-overlay-put ov 'face 'secondary-selection)
  9129. (push ov org-occur-highlights)))
  9130. (defun org-remove-occur-highlights (&optional beg end noremove)
  9131. "Remove the occur highlights from the buffer.
  9132. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9133. from the `before-change-functions' in the current buffer."
  9134. (interactive)
  9135. (unless org-inhibit-highlight-removal
  9136. (mapc 'org-delete-overlay org-occur-highlights)
  9137. (setq org-occur-highlights nil)
  9138. (setq org-occur-parameters nil)
  9139. (unless noremove
  9140. (remove-hook 'before-change-functions
  9141. 'org-remove-occur-highlights 'local))))
  9142. ;;;; Priorities
  9143. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9144. "Regular expression matching the priority indicator.")
  9145. (defvar org-remove-priority-next-time nil)
  9146. (defun org-priority-up ()
  9147. "Increase the priority of the current item."
  9148. (interactive)
  9149. (org-priority 'up))
  9150. (defun org-priority-down ()
  9151. "Decrease the priority of the current item."
  9152. (interactive)
  9153. (org-priority 'down))
  9154. (defun org-priority (&optional action)
  9155. "Change the priority of an item by ARG.
  9156. ACTION can be `set', `up', `down', or a character."
  9157. (interactive)
  9158. (setq action (or action 'set))
  9159. (let (current new news have remove)
  9160. (save-excursion
  9161. (org-back-to-heading t)
  9162. (if (looking-at org-priority-regexp)
  9163. (setq current (string-to-char (match-string 2))
  9164. have t)
  9165. (setq current org-default-priority))
  9166. (cond
  9167. ((or (eq action 'set)
  9168. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9169. (if (not (eq action 'set))
  9170. (setq new action)
  9171. (message "Priority %c-%c, SPC to remove: "
  9172. org-highest-priority org-lowest-priority)
  9173. (setq new (read-char-exclusive)))
  9174. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9175. (= (upcase org-lowest-priority) org-lowest-priority))
  9176. (setq new (upcase new)))
  9177. (cond ((equal new ?\ ) (setq remove t))
  9178. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9179. (error "Priority must be between `%c' and `%c'"
  9180. org-highest-priority org-lowest-priority))))
  9181. ((eq action 'up)
  9182. (if (and (not have) (eq last-command this-command))
  9183. (setq new org-lowest-priority)
  9184. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9185. org-default-priority (1- current)))))
  9186. ((eq action 'down)
  9187. (if (and (not have) (eq last-command this-command))
  9188. (setq new org-highest-priority)
  9189. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9190. org-default-priority (1+ current)))))
  9191. (t (error "Invalid action")))
  9192. (if (or (< (upcase new) org-highest-priority)
  9193. (> (upcase new) org-lowest-priority))
  9194. (setq remove t))
  9195. (setq news (format "%c" new))
  9196. (if have
  9197. (if remove
  9198. (replace-match "" t t nil 1)
  9199. (replace-match news t t nil 2))
  9200. (if remove
  9201. (error "No priority cookie found in line")
  9202. (let ((case-fold-search nil))
  9203. (looking-at org-todo-line-regexp))
  9204. (if (match-end 2)
  9205. (progn
  9206. (goto-char (match-end 2))
  9207. (insert " [#" news "]"))
  9208. (goto-char (match-beginning 3))
  9209. (insert "[#" news "] ")))))
  9210. (org-preserve-lc (org-set-tags nil 'align))
  9211. (if remove
  9212. (message "Priority removed")
  9213. (message "Priority of current item set to %s" news))))
  9214. (defun org-get-priority (s)
  9215. "Find priority cookie and return priority."
  9216. (save-match-data
  9217. (if (not (string-match org-priority-regexp s))
  9218. (* 1000 (- org-lowest-priority org-default-priority))
  9219. (* 1000 (- org-lowest-priority
  9220. (string-to-char (match-string 2 s)))))))
  9221. ;;;; Tags
  9222. (defvar org-agenda-archives-mode)
  9223. (defvar org-map-continue-from nil
  9224. "Position from where mapping should continue.
  9225. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9226. (defvar org-scanner-tags nil
  9227. "The current tag list while the tags scanner is running.")
  9228. (defvar org-trust-scanner-tags nil
  9229. "Should `org-get-tags-at' use the tags fro the scanner.
  9230. This is for internal dynamical scoping only.
  9231. When this is non-nil, the function `org-get-tags-at' will return the value
  9232. of `org-scanner-tags' instead of building the list by itself. This
  9233. can lead to large speed-ups when the tags scanner is used in a file with
  9234. many entries, and when the list of tags is retrieved, for example to
  9235. obtain a list of properties. Building the tags list for each entry in such
  9236. a file becomes an N^2 operation - but with this variable set, it scales
  9237. as N.")
  9238. (defun org-scan-tags (action matcher &optional todo-only)
  9239. "Scan headline tags with inheritance and produce output ACTION.
  9240. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9241. or `agenda' to produce an entry list for an agenda view. It can also be
  9242. a Lisp form or a function that should be called at each matched headline, in
  9243. this case the return value is a list of all return values from these calls.
  9244. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9245. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9246. only lines with a TODO keyword are included in the output."
  9247. (require 'org-agenda)
  9248. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9249. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9250. (org-re
  9251. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9252. (props (list 'face 'default
  9253. 'done-face 'org-agenda-done
  9254. 'undone-face 'default
  9255. 'mouse-face 'highlight
  9256. 'org-not-done-regexp org-not-done-regexp
  9257. 'org-todo-regexp org-todo-regexp
  9258. 'keymap org-agenda-keymap
  9259. 'help-echo
  9260. (format "mouse-2 or RET jump to org file %s"
  9261. (abbreviate-file-name
  9262. (or (buffer-file-name (buffer-base-buffer))
  9263. (buffer-name (buffer-base-buffer)))))))
  9264. (case-fold-search nil)
  9265. (org-map-continue-from nil)
  9266. lspos tags tags-list
  9267. (tags-alist (list (cons 0 org-file-tags)))
  9268. (llast 0) rtn rtn1 level category i txt
  9269. todo marker entry priority)
  9270. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9271. (setq action (list 'lambda nil action)))
  9272. (save-excursion
  9273. (goto-char (point-min))
  9274. (when (eq action 'sparse-tree)
  9275. (org-overview)
  9276. (org-remove-occur-highlights))
  9277. (while (re-search-forward re nil t)
  9278. (catch :skip
  9279. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9280. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9281. (goto-char (setq lspos (match-beginning 0)))
  9282. (setq level (org-reduced-level (funcall outline-level))
  9283. category (org-get-category))
  9284. (setq i llast llast level)
  9285. ;; remove tag lists from same and sublevels
  9286. (while (>= i level)
  9287. (when (setq entry (assoc i tags-alist))
  9288. (setq tags-alist (delete entry tags-alist)))
  9289. (setq i (1- i)))
  9290. ;; add the next tags
  9291. (when tags
  9292. (setq tags (org-split-string tags ":")
  9293. tags-alist
  9294. (cons (cons level tags) tags-alist)))
  9295. ;; compile tags for current headline
  9296. (setq tags-list
  9297. (if org-use-tag-inheritance
  9298. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9299. tags)
  9300. org-scanner-tags tags-list)
  9301. (when org-use-tag-inheritance
  9302. (setcdr (car tags-alist)
  9303. (mapcar (lambda (x)
  9304. (setq x (copy-sequence x))
  9305. (org-add-prop-inherited x))
  9306. (cdar tags-alist))))
  9307. (when (and tags org-use-tag-inheritance
  9308. (or (not (eq t org-use-tag-inheritance))
  9309. org-tags-exclude-from-inheritance))
  9310. ;; selective inheritance, remove uninherited ones
  9311. (setcdr (car tags-alist)
  9312. (org-remove-uniherited-tags (cdar tags-alist))))
  9313. (when (and (or (not todo-only)
  9314. (and (member todo org-not-done-keywords)
  9315. (or (not org-agenda-tags-todo-honor-ignore-options)
  9316. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9317. (let ((case-fold-search t)) (eval matcher))
  9318. (or
  9319. (not (member org-archive-tag tags-list))
  9320. ;; we have an archive tag, should we use this anyway?
  9321. (or (not org-agenda-skip-archived-trees)
  9322. (and (eq action 'agenda) org-agenda-archives-mode))))
  9323. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9324. ;; select this headline
  9325. (cond
  9326. ((eq action 'sparse-tree)
  9327. (and org-highlight-sparse-tree-matches
  9328. (org-get-heading) (match-end 0)
  9329. (org-highlight-new-match
  9330. (match-beginning 0) (match-beginning 1)))
  9331. (org-show-context 'tags-tree))
  9332. ((eq action 'agenda)
  9333. (setq txt (org-format-agenda-item
  9334. ""
  9335. (concat
  9336. (if (eq org-tags-match-list-sublevels 'indented)
  9337. (make-string (1- level) ?.) "")
  9338. (org-get-heading))
  9339. category
  9340. tags-list
  9341. )
  9342. priority (org-get-priority txt))
  9343. (goto-char lspos)
  9344. (setq marker (org-agenda-new-marker))
  9345. (org-add-props txt props
  9346. 'org-marker marker 'org-hd-marker marker 'org-category category
  9347. 'todo-state todo
  9348. 'priority priority 'type "tagsmatch")
  9349. (push txt rtn))
  9350. ((functionp action)
  9351. (setq org-map-continue-from nil)
  9352. (save-excursion
  9353. (setq rtn1 (funcall action))
  9354. (push rtn1 rtn)))
  9355. (t (error "Invalid action")))
  9356. ;; if we are to skip sublevels, jump to end of subtree
  9357. (unless org-tags-match-list-sublevels
  9358. (org-end-of-subtree t)
  9359. (backward-char 1))))
  9360. ;; Get the correct position from where to continue
  9361. (if org-map-continue-from
  9362. (goto-char org-map-continue-from)
  9363. (and (= (point) lspos) (end-of-line 1)))))
  9364. (when (and (eq action 'sparse-tree)
  9365. (not org-sparse-tree-open-archived-trees))
  9366. (org-hide-archived-subtrees (point-min) (point-max)))
  9367. (nreverse rtn)))
  9368. (defun org-remove-uniherited-tags (tags)
  9369. "Remove all tags that are not inherited from the list TAGS."
  9370. (cond
  9371. ((eq org-use-tag-inheritance t)
  9372. (if org-tags-exclude-from-inheritance
  9373. (org-delete-all org-tags-exclude-from-inheritance tags)
  9374. tags))
  9375. ((not org-use-tag-inheritance) nil)
  9376. ((stringp org-use-tag-inheritance)
  9377. (delq nil (mapcar
  9378. (lambda (x)
  9379. (if (and (string-match org-use-tag-inheritance x)
  9380. (not (member x org-tags-exclude-from-inheritance)))
  9381. x nil))
  9382. tags)))
  9383. ((listp org-use-tag-inheritance)
  9384. (delq nil (mapcar
  9385. (lambda (x)
  9386. (if (member x org-use-tag-inheritance) x nil))
  9387. tags)))))
  9388. (defvar todo-only) ;; dynamically scoped
  9389. (defun org-match-sparse-tree (&optional todo-only match)
  9390. "Create a sparse tree according to tags string MATCH.
  9391. MATCH can contain positive and negative selection of tags, like
  9392. \"+WORK+URGENT-WITHBOSS\".
  9393. If optional argument TODO-ONLY is non-nil, only select lines that are
  9394. also TODO lines."
  9395. (interactive "P")
  9396. (org-prepare-agenda-buffers (list (current-buffer)))
  9397. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9398. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9399. (defvar org-cached-props nil)
  9400. (defun org-cached-entry-get (pom property)
  9401. (if (or (eq t org-use-property-inheritance)
  9402. (and (stringp org-use-property-inheritance)
  9403. (string-match org-use-property-inheritance property))
  9404. (and (listp org-use-property-inheritance)
  9405. (member property org-use-property-inheritance)))
  9406. ;; Caching is not possible, check it directly
  9407. (org-entry-get pom property 'inherit)
  9408. ;; Get all properties, so that we can do complicated checks easily
  9409. (cdr (assoc property (or org-cached-props
  9410. (setq org-cached-props
  9411. (org-entry-properties pom)))))))
  9412. (defun org-global-tags-completion-table (&optional files)
  9413. "Return the list of all tags in all agenda buffer/files."
  9414. (save-excursion
  9415. (org-uniquify
  9416. (delq nil
  9417. (apply 'append
  9418. (mapcar
  9419. (lambda (file)
  9420. (set-buffer (find-file-noselect file))
  9421. (append (org-get-buffer-tags)
  9422. (mapcar (lambda (x) (if (stringp (car-safe x))
  9423. (list (car-safe x)) nil))
  9424. org-tag-alist)))
  9425. (if (and files (car files))
  9426. files
  9427. (org-agenda-files))))))))
  9428. (defun org-make-tags-matcher (match)
  9429. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9430. ;; todo-only is scoped dynamically into this function, and the function
  9431. ;; may change it if the matcher asks for it.
  9432. (unless match
  9433. ;; Get a new match request, with completion
  9434. (let ((org-last-tags-completion-table
  9435. (org-global-tags-completion-table)))
  9436. (setq match (org-completing-read-no-ido
  9437. "Match: " 'org-tags-completion-function nil nil nil
  9438. 'org-tags-history))))
  9439. ;; Parse the string and create a lisp form
  9440. (let ((match0 match)
  9441. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9442. minus tag mm
  9443. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9444. orterms term orlist re-p str-p level-p level-op time-p
  9445. prop-p pn pv po cat-p gv rest)
  9446. (if (string-match "/+" match)
  9447. ;; match contains also a todo-matching request
  9448. (progn
  9449. (setq tagsmatch (substring match 0 (match-beginning 0))
  9450. todomatch (substring match (match-end 0)))
  9451. (if (string-match "^!" todomatch)
  9452. (setq todo-only t todomatch (substring todomatch 1)))
  9453. (if (string-match "^\\s-*$" todomatch)
  9454. (setq todomatch nil)))
  9455. ;; only matching tags
  9456. (setq tagsmatch match todomatch nil))
  9457. ;; Make the tags matcher
  9458. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9459. (setq tagsmatcher t)
  9460. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9461. (while (setq term (pop orterms))
  9462. (while (and (equal (substring term -1) "\\") orterms)
  9463. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9464. (while (string-match re term)
  9465. (setq rest (substring term (match-end 0))
  9466. minus (and (match-end 1)
  9467. (equal (match-string 1 term) "-"))
  9468. tag (match-string 2 term)
  9469. re-p (equal (string-to-char tag) ?{)
  9470. level-p (match-end 4)
  9471. prop-p (match-end 5)
  9472. mm (cond
  9473. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9474. (level-p
  9475. (setq level-op (org-op-to-function (match-string 3 term)))
  9476. `(,level-op level ,(string-to-number
  9477. (match-string 4 term))))
  9478. (prop-p
  9479. (setq pn (match-string 5 term)
  9480. po (match-string 6 term)
  9481. pv (match-string 7 term)
  9482. cat-p (equal pn "CATEGORY")
  9483. re-p (equal (string-to-char pv) ?{)
  9484. str-p (equal (string-to-char pv) ?\")
  9485. time-p (save-match-data
  9486. (string-match "^\"[[<].*[]>]\"$" pv))
  9487. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9488. (if time-p (setq pv (org-matcher-time pv)))
  9489. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9490. (cond
  9491. ((equal pn "CATEGORY")
  9492. (setq gv '(get-text-property (point) 'org-category)))
  9493. ((equal pn "TODO")
  9494. (setq gv 'todo))
  9495. (t
  9496. (setq gv `(org-cached-entry-get nil ,pn))))
  9497. (if re-p
  9498. (if (eq po 'org<>)
  9499. `(not (string-match ,pv (or ,gv "")))
  9500. `(string-match ,pv (or ,gv "")))
  9501. (if str-p
  9502. `(,po (or ,gv "") ,pv)
  9503. `(,po (string-to-number (or ,gv ""))
  9504. ,(string-to-number pv) ))))
  9505. (t `(member ,tag tags-list)))
  9506. mm (if minus (list 'not mm) mm)
  9507. term rest)
  9508. (push mm tagsmatcher))
  9509. (push (if (> (length tagsmatcher) 1)
  9510. (cons 'and tagsmatcher)
  9511. (car tagsmatcher))
  9512. orlist)
  9513. (setq tagsmatcher nil))
  9514. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9515. (setq tagsmatcher
  9516. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9517. ;; Make the todo matcher
  9518. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9519. (setq todomatcher t)
  9520. (setq orterms (org-split-string todomatch "|") orlist nil)
  9521. (while (setq term (pop orterms))
  9522. (while (string-match re term)
  9523. (setq minus (and (match-end 1)
  9524. (equal (match-string 1 term) "-"))
  9525. kwd (match-string 2 term)
  9526. re-p (equal (string-to-char kwd) ?{)
  9527. term (substring term (match-end 0))
  9528. mm (if re-p
  9529. `(string-match ,(substring kwd 1 -1) todo)
  9530. (list 'equal 'todo kwd))
  9531. mm (if minus (list 'not mm) mm))
  9532. (push mm todomatcher))
  9533. (push (if (> (length todomatcher) 1)
  9534. (cons 'and todomatcher)
  9535. (car todomatcher))
  9536. orlist)
  9537. (setq todomatcher nil))
  9538. (setq todomatcher (if (> (length orlist) 1)
  9539. (cons 'or orlist) (car orlist))))
  9540. ;; Return the string and lisp forms of the matcher
  9541. (setq matcher (if todomatcher
  9542. (list 'and tagsmatcher todomatcher)
  9543. tagsmatcher))
  9544. (cons match0 matcher)))
  9545. (defun org-op-to-function (op &optional stringp)
  9546. "Turn an operator into the appropriate function."
  9547. (setq op
  9548. (cond
  9549. ((equal op "<" ) '(< string< org-time<))
  9550. ((equal op ">" ) '(> org-string> org-time>))
  9551. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9552. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9553. ((member op '("=" "==")) '(= string= org-time=))
  9554. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9555. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9556. (defun org<> (a b) (not (= a b)))
  9557. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9558. (defun org-string>= (a b) (not (string< a b)))
  9559. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9560. (defun org-string<> (a b) (not (string= a b)))
  9561. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9562. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9563. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9564. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9565. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9566. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9567. (defun org-2ft (s)
  9568. "Convert S to a floating point time.
  9569. If S is already a number, just return it. If it is a string, parse
  9570. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9571. (cond
  9572. ((numberp s) s)
  9573. ((stringp s)
  9574. (condition-case nil
  9575. (float-time (apply 'encode-time (org-parse-time-string s)))
  9576. (error 0.)))
  9577. (t 0.)))
  9578. (defun org-time-today ()
  9579. "Time in seconds today at 0:00.
  9580. Returns the float number of seconds since the beginning of the
  9581. epoch to the beginning of today (00:00)."
  9582. (float-time (apply 'encode-time
  9583. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9584. (defun org-matcher-time (s)
  9585. "Interpret a time comparison value."
  9586. (save-match-data
  9587. (cond
  9588. ((string= s "<now>") (float-time))
  9589. ((string= s "<today>") (org-time-today))
  9590. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9591. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9592. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9593. (+ (org-time-today)
  9594. (* (string-to-number (match-string 1 s))
  9595. (cdr (assoc (match-string 2 s)
  9596. '(("d" . 86400.0) ("w" . 604800.0)
  9597. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9598. (t (org-2ft s)))))
  9599. (defun org-match-any-p (re list)
  9600. "Does re match any element of list?"
  9601. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9602. (delq nil list))
  9603. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9604. (defvar org-tags-overlay (org-make-overlay 1 1))
  9605. (org-detach-overlay org-tags-overlay)
  9606. (defun org-get-local-tags-at (&optional pos)
  9607. "Get a list of tags defined in the current headline."
  9608. (org-get-tags-at pos 'local))
  9609. (defun org-get-local-tags ()
  9610. "Get a list of tags defined in the current headline."
  9611. (org-get-tags-at nil 'local))
  9612. (defun org-get-tags-at (&optional pos local)
  9613. "Get a list of all headline tags applicable at POS.
  9614. POS defaults to point. If tags are inherited, the list contains
  9615. the targets in the same sequence as the headlines appear, i.e.
  9616. the tags of the current headline come last.
  9617. When LOCAL is non-nil, only return tags from the current headline,
  9618. ignore inherited ones."
  9619. (interactive)
  9620. (if (and org-trust-scanner-tags
  9621. (or (not pos) (equal pos (point)))
  9622. (not local))
  9623. org-scanner-tags
  9624. (let (tags ltags lastpos parent)
  9625. (save-excursion
  9626. (save-restriction
  9627. (widen)
  9628. (goto-char (or pos (point)))
  9629. (save-match-data
  9630. (catch 'done
  9631. (condition-case nil
  9632. (progn
  9633. (org-back-to-heading t)
  9634. (while (not (equal lastpos (point)))
  9635. (setq lastpos (point))
  9636. (when (looking-at
  9637. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9638. (setq ltags (org-split-string
  9639. (org-match-string-no-properties 1) ":"))
  9640. (when parent
  9641. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9642. (setq tags (append
  9643. (if parent
  9644. (org-remove-uniherited-tags ltags)
  9645. ltags)
  9646. tags)))
  9647. (or org-use-tag-inheritance (throw 'done t))
  9648. (if local (throw 'done t))
  9649. (org-up-heading-all 1)
  9650. (setq parent t)))
  9651. (error nil)))))
  9652. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9653. (defun org-add-prop-inherited (s)
  9654. (add-text-properties 0 (length s) '(inherited t) s)
  9655. s)
  9656. (defun org-toggle-tag (tag &optional onoff)
  9657. "Toggle the tag TAG for the current line.
  9658. If ONOFF is `on' or `off', don't toggle but set to this state."
  9659. (let (res current)
  9660. (save-excursion
  9661. (org-back-to-heading t)
  9662. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9663. (point-at-eol) t)
  9664. (progn
  9665. (setq current (match-string 1))
  9666. (replace-match ""))
  9667. (setq current ""))
  9668. (setq current (nreverse (org-split-string current ":")))
  9669. (cond
  9670. ((eq onoff 'on)
  9671. (setq res t)
  9672. (or (member tag current) (push tag current)))
  9673. ((eq onoff 'off)
  9674. (or (not (member tag current)) (setq current (delete tag current))))
  9675. (t (if (member tag current)
  9676. (setq current (delete tag current))
  9677. (setq res t)
  9678. (push tag current))))
  9679. (end-of-line 1)
  9680. (if current
  9681. (progn
  9682. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9683. (org-set-tags nil t))
  9684. (delete-horizontal-space))
  9685. (run-hooks 'org-after-tags-change-hook))
  9686. res))
  9687. (defun org-align-tags-here (to-col)
  9688. ;; Assumes that this is a headline
  9689. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9690. (beginning-of-line 1)
  9691. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9692. (< pos (match-beginning 2)))
  9693. (progn
  9694. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9695. (goto-char (match-beginning 1))
  9696. (insert " ")
  9697. (delete-region (point) (1+ (match-beginning 2)))
  9698. (setq ncol (max (1+ (current-column))
  9699. (1+ col)
  9700. (if (> to-col 0)
  9701. to-col
  9702. (- (abs to-col) tags-l))))
  9703. (setq p (point))
  9704. (insert (make-string (- ncol (current-column)) ?\ ))
  9705. (setq ncol (current-column))
  9706. (when indent-tabs-mode (tabify p (point-at-eol)))
  9707. (org-move-to-column (min ncol col) t))
  9708. (goto-char pos))))
  9709. (defun org-set-tags-command (&optional arg just-align)
  9710. "Call the set-tags command for the current entry."
  9711. (interactive "P")
  9712. (if (org-on-heading-p)
  9713. (org-set-tags arg just-align)
  9714. (save-excursion
  9715. (org-back-to-heading t)
  9716. (org-set-tags arg just-align))))
  9717. (defun org-set-tags (&optional arg just-align)
  9718. "Set the tags for the current headline.
  9719. With prefix ARG, realign all tags in headings in the current buffer."
  9720. (interactive "P")
  9721. (let* ((re (concat "^" outline-regexp))
  9722. (current (org-get-tags-string))
  9723. (col (current-column))
  9724. (org-setting-tags t)
  9725. table current-tags inherited-tags ; computed below when needed
  9726. tags p0 c0 c1 rpl)
  9727. (if arg
  9728. (save-excursion
  9729. (goto-char (point-min))
  9730. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9731. (while (re-search-forward re nil t)
  9732. (org-set-tags nil t)
  9733. (end-of-line 1)))
  9734. (message "All tags realigned to column %d" org-tags-column))
  9735. (if just-align
  9736. (setq tags current)
  9737. ;; Get a new set of tags from the user
  9738. (save-excursion
  9739. (setq table (append org-tag-persistent-alist
  9740. (or org-tag-alist (org-get-buffer-tags)))
  9741. org-last-tags-completion-table table
  9742. current-tags (org-split-string current ":")
  9743. inherited-tags (nreverse
  9744. (nthcdr (length current-tags)
  9745. (nreverse (org-get-tags-at))))
  9746. tags
  9747. (if (or (eq t org-use-fast-tag-selection)
  9748. (and org-use-fast-tag-selection
  9749. (delq nil (mapcar 'cdr table))))
  9750. (org-fast-tag-selection
  9751. current-tags inherited-tags table
  9752. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9753. (let ((org-add-colon-after-tag-completion t))
  9754. (org-trim
  9755. (org-without-partial-completion
  9756. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9757. nil nil current 'org-tags-history)))))))
  9758. (while (string-match "[-+&]+" tags)
  9759. ;; No boolean logic, just a list
  9760. (setq tags (replace-match ":" t t tags))))
  9761. (if (string-match "\\`[\t ]*\\'" tags)
  9762. (setq tags "")
  9763. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9764. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9765. ;; Insert new tags at the correct column
  9766. (beginning-of-line 1)
  9767. (cond
  9768. ((and (equal current "") (equal tags "")))
  9769. ((re-search-forward
  9770. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9771. (point-at-eol) t)
  9772. (if (equal tags "")
  9773. (setq rpl "")
  9774. (goto-char (match-beginning 0))
  9775. (setq c0 (current-column) p0 (point)
  9776. c1 (max (1+ c0) (if (> org-tags-column 0)
  9777. org-tags-column
  9778. (- (- org-tags-column) (length tags))))
  9779. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9780. (replace-match rpl t t)
  9781. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9782. tags)
  9783. (t (error "Tags alignment failed")))
  9784. (org-move-to-column col)
  9785. (unless just-align
  9786. (run-hooks 'org-after-tags-change-hook)))))
  9787. (defun org-change-tag-in-region (beg end tag off)
  9788. "Add or remove TAG for each entry in the region.
  9789. This works in the agenda, and also in an org-mode buffer."
  9790. (interactive
  9791. (list (region-beginning) (region-end)
  9792. (let ((org-last-tags-completion-table
  9793. (if (org-mode-p)
  9794. (org-get-buffer-tags)
  9795. (org-global-tags-completion-table))))
  9796. (org-ido-completing-read
  9797. "Tag: " 'org-tags-completion-function nil nil nil
  9798. 'org-tags-history))
  9799. (progn
  9800. (message "[s]et or [r]emove? ")
  9801. (equal (read-char-exclusive) ?r))))
  9802. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9803. (let ((agendap (equal major-mode 'org-agenda-mode))
  9804. l1 l2 m buf pos newhead (cnt 0))
  9805. (goto-char end)
  9806. (setq l2 (1- (org-current-line)))
  9807. (goto-char beg)
  9808. (setq l1 (org-current-line))
  9809. (loop for l from l1 to l2 do
  9810. (goto-line l)
  9811. (setq m (get-text-property (point) 'org-hd-marker))
  9812. (when (or (and (org-mode-p) (org-on-heading-p))
  9813. (and agendap m))
  9814. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9815. pos (if agendap m (point)))
  9816. (with-current-buffer buf
  9817. (save-excursion
  9818. (save-restriction
  9819. (goto-char pos)
  9820. (setq cnt (1+ cnt))
  9821. (org-toggle-tag tag (if off 'off 'on))
  9822. (setq newhead (org-get-heading)))))
  9823. (and agendap (org-agenda-change-all-lines newhead m))))
  9824. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9825. (defun org-tags-completion-function (string predicate &optional flag)
  9826. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9827. (confirm (lambda (x) (stringp (car x)))))
  9828. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9829. (setq s1 (match-string 1 string)
  9830. s2 (match-string 2 string))
  9831. (setq s1 "" s2 string))
  9832. (cond
  9833. ((eq flag nil)
  9834. ;; try completion
  9835. (setq rtn (try-completion s2 ctable confirm))
  9836. (if (stringp rtn)
  9837. (setq rtn
  9838. (concat s1 s2 (substring rtn (length s2))
  9839. (if (and org-add-colon-after-tag-completion
  9840. (assoc rtn ctable))
  9841. ":" ""))))
  9842. rtn)
  9843. ((eq flag t)
  9844. ;; all-completions
  9845. (all-completions s2 ctable confirm)
  9846. )
  9847. ((eq flag 'lambda)
  9848. ;; exact match?
  9849. (assoc s2 ctable)))
  9850. ))
  9851. (defun org-fast-tag-insert (kwd tags face &optional end)
  9852. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9853. (insert (format "%-12s" (concat kwd ":"))
  9854. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9855. (or end "")))
  9856. (defun org-fast-tag-show-exit (flag)
  9857. (save-excursion
  9858. (goto-line 3)
  9859. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9860. (replace-match ""))
  9861. (when flag
  9862. (end-of-line 1)
  9863. (org-move-to-column (- (window-width) 19) t)
  9864. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9865. (defun org-set-current-tags-overlay (current prefix)
  9866. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9867. (if (featurep 'xemacs)
  9868. (org-overlay-display org-tags-overlay (concat prefix s)
  9869. 'secondary-selection)
  9870. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9871. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9872. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9873. "Fast tag selection with single keys.
  9874. CURRENT is the current list of tags in the headline, INHERITED is the
  9875. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9876. possibly with grouping information. TODO-TABLE is a similar table with
  9877. TODO keywords, should these have keys assigned to them.
  9878. If the keys are nil, a-z are automatically assigned.
  9879. Returns the new tags string, or nil to not change the current settings."
  9880. (let* ((fulltable (append table todo-table))
  9881. (maxlen (apply 'max (mapcar
  9882. (lambda (x)
  9883. (if (stringp (car x)) (string-width (car x)) 0))
  9884. fulltable)))
  9885. (buf (current-buffer))
  9886. (expert (eq org-fast-tag-selection-single-key 'expert))
  9887. (buffer-tags nil)
  9888. (fwidth (+ maxlen 3 1 3))
  9889. (ncol (/ (- (window-width) 4) fwidth))
  9890. (i-face 'org-done)
  9891. (c-face 'org-todo)
  9892. tg cnt e c char c1 c2 ntable tbl rtn
  9893. ov-start ov-end ov-prefix
  9894. (exit-after-next org-fast-tag-selection-single-key)
  9895. (done-keywords org-done-keywords)
  9896. groups ingroup)
  9897. (save-excursion
  9898. (beginning-of-line 1)
  9899. (if (looking-at
  9900. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9901. (setq ov-start (match-beginning 1)
  9902. ov-end (match-end 1)
  9903. ov-prefix "")
  9904. (setq ov-start (1- (point-at-eol))
  9905. ov-end (1+ ov-start))
  9906. (skip-chars-forward "^\n\r")
  9907. (setq ov-prefix
  9908. (concat
  9909. (buffer-substring (1- (point)) (point))
  9910. (if (> (current-column) org-tags-column)
  9911. " "
  9912. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9913. (org-move-overlay org-tags-overlay ov-start ov-end)
  9914. (save-window-excursion
  9915. (if expert
  9916. (set-buffer (get-buffer-create " *Org tags*"))
  9917. (delete-other-windows)
  9918. (split-window-vertically)
  9919. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9920. (erase-buffer)
  9921. (org-set-local 'org-done-keywords done-keywords)
  9922. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9923. (org-fast-tag-insert "Current" current c-face "\n\n")
  9924. (org-fast-tag-show-exit exit-after-next)
  9925. (org-set-current-tags-overlay current ov-prefix)
  9926. (setq tbl fulltable char ?a cnt 0)
  9927. (while (setq e (pop tbl))
  9928. (cond
  9929. ((equal e '(:startgroup))
  9930. (push '() groups) (setq ingroup t)
  9931. (when (not (= cnt 0))
  9932. (setq cnt 0)
  9933. (insert "\n"))
  9934. (insert "{ "))
  9935. ((equal e '(:endgroup))
  9936. (setq ingroup nil cnt 0)
  9937. (insert "}\n"))
  9938. ((equal e '(:newline))
  9939. (when (not (= cnt 0))
  9940. (setq cnt 0)
  9941. (insert "\n")
  9942. (setq e (car tbl))
  9943. (while (equal (car tbl) '(:newline))
  9944. (insert "\n")
  9945. (setq tbl (cdr tbl)))))
  9946. (t
  9947. (setq tg (car e) c2 nil)
  9948. (if (cdr e)
  9949. (setq c (cdr e))
  9950. ;; automatically assign a character.
  9951. (setq c1 (string-to-char
  9952. (downcase (substring
  9953. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9954. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9955. (while (or (rassoc char ntable) (rassoc char table))
  9956. (setq char (1+ char)))
  9957. (setq c2 c1))
  9958. (setq c (or c2 char)))
  9959. (if ingroup (push tg (car groups)))
  9960. (setq tg (org-add-props tg nil 'face
  9961. (cond
  9962. ((not (assoc tg table))
  9963. (org-get-todo-face tg))
  9964. ((member tg current) c-face)
  9965. ((member tg inherited) i-face)
  9966. (t nil))))
  9967. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9968. (insert "[" c "] " tg (make-string
  9969. (- fwidth 4 (length tg)) ?\ ))
  9970. (push (cons tg c) ntable)
  9971. (when (= (setq cnt (1+ cnt)) ncol)
  9972. (insert "\n")
  9973. (if ingroup (insert " "))
  9974. (setq cnt 0)))))
  9975. (setq ntable (nreverse ntable))
  9976. (insert "\n")
  9977. (goto-char (point-min))
  9978. (if (not expert) (org-fit-window-to-buffer))
  9979. (setq rtn
  9980. (catch 'exit
  9981. (while t
  9982. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9983. (if groups " [!] no groups" " [!]groups")
  9984. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9985. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9986. (cond
  9987. ((= c ?\r) (throw 'exit t))
  9988. ((= c ?!)
  9989. (setq groups (not groups))
  9990. (goto-char (point-min))
  9991. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9992. ((= c ?\C-c)
  9993. (if (not expert)
  9994. (org-fast-tag-show-exit
  9995. (setq exit-after-next (not exit-after-next)))
  9996. (setq expert nil)
  9997. (delete-other-windows)
  9998. (split-window-vertically)
  9999. (org-switch-to-buffer-other-window " *Org tags*")
  10000. (org-fit-window-to-buffer)))
  10001. ((or (= c ?\C-g)
  10002. (and (= c ?q) (not (rassoc c ntable))))
  10003. (org-detach-overlay org-tags-overlay)
  10004. (setq quit-flag t))
  10005. ((= c ?\ )
  10006. (setq current nil)
  10007. (if exit-after-next (setq exit-after-next 'now)))
  10008. ((= c ?\t)
  10009. (condition-case nil
  10010. (setq tg (org-ido-completing-read
  10011. "Tag: "
  10012. (or buffer-tags
  10013. (with-current-buffer buf
  10014. (org-get-buffer-tags)))))
  10015. (quit (setq tg "")))
  10016. (when (string-match "\\S-" tg)
  10017. (add-to-list 'buffer-tags (list tg))
  10018. (if (member tg current)
  10019. (setq current (delete tg current))
  10020. (push tg current)))
  10021. (if exit-after-next (setq exit-after-next 'now)))
  10022. ((setq e (rassoc c todo-table) tg (car e))
  10023. (with-current-buffer buf
  10024. (save-excursion (org-todo tg)))
  10025. (if exit-after-next (setq exit-after-next 'now)))
  10026. ((setq e (rassoc c ntable) tg (car e))
  10027. (if (member tg current)
  10028. (setq current (delete tg current))
  10029. (loop for g in groups do
  10030. (if (member tg g)
  10031. (mapc (lambda (x)
  10032. (setq current (delete x current)))
  10033. g)))
  10034. (push tg current))
  10035. (if exit-after-next (setq exit-after-next 'now))))
  10036. ;; Create a sorted list
  10037. (setq current
  10038. (sort current
  10039. (lambda (a b)
  10040. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10041. (if (eq exit-after-next 'now) (throw 'exit t))
  10042. (goto-char (point-min))
  10043. (beginning-of-line 2)
  10044. (delete-region (point) (point-at-eol))
  10045. (org-fast-tag-insert "Current" current c-face)
  10046. (org-set-current-tags-overlay current ov-prefix)
  10047. (while (re-search-forward
  10048. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10049. (setq tg (match-string 1))
  10050. (add-text-properties
  10051. (match-beginning 1) (match-end 1)
  10052. (list 'face
  10053. (cond
  10054. ((member tg current) c-face)
  10055. ((member tg inherited) i-face)
  10056. (t (get-text-property (match-beginning 1) 'face))))))
  10057. (goto-char (point-min)))))
  10058. (org-detach-overlay org-tags-overlay)
  10059. (if rtn
  10060. (mapconcat 'identity current ":")
  10061. nil))))
  10062. (defun org-get-tags-string ()
  10063. "Get the TAGS string in the current headline."
  10064. (unless (org-on-heading-p t)
  10065. (error "Not on a heading"))
  10066. (save-excursion
  10067. (beginning-of-line 1)
  10068. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10069. (org-match-string-no-properties 1)
  10070. "")))
  10071. (defun org-get-tags ()
  10072. "Get the list of tags specified in the current headline."
  10073. (org-split-string (org-get-tags-string) ":"))
  10074. (defun org-get-buffer-tags ()
  10075. "Get a table of all tags used in the buffer, for completion."
  10076. (let (tags)
  10077. (save-excursion
  10078. (goto-char (point-min))
  10079. (while (re-search-forward
  10080. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10081. (when (equal (char-after (point-at-bol 0)) ?*)
  10082. (mapc (lambda (x) (add-to-list 'tags x))
  10083. (org-split-string (org-match-string-no-properties 1) ":")))))
  10084. (mapcar 'list tags)))
  10085. ;;;; The mapping API
  10086. ;;;###autoload
  10087. (defun org-map-entries (func &optional match scope &rest skip)
  10088. "Call FUNC at each headline selected by MATCH in SCOPE.
  10089. FUNC is a function or a lisp form. The function will be called without
  10090. arguments, with the cursor positioned at the beginning of the headline.
  10091. The return values of all calls to the function will be collected and
  10092. returned as a list.
  10093. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10094. does not need to preserve point. After evaluaton, the cursor will be
  10095. moved to the end of the line (presumably of the headline of the
  10096. processed entry) and search continues from there. Under some
  10097. circumstances, this may not produce the wanted results. For example,
  10098. if you have removed (e.g. archived) the current (sub)tree it could
  10099. mean that the next entry will be skipped entirely. In such cases, you
  10100. can specify the position from where search should continue by making
  10101. FUNC set the variable `org-map-continue-from' to the desired buffer
  10102. position.
  10103. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10104. Only headlines that are matched by this query will be considered during
  10105. the iteration. When MATCH is nil or t, all headlines will be
  10106. visited by the iteration.
  10107. SCOPE determines the scope of this command. It can be any of:
  10108. nil The current buffer, respecting the restriction if any
  10109. tree The subtree started with the entry at point
  10110. file The current buffer, without restriction
  10111. file-with-archives
  10112. The current buffer, and any archives associated with it
  10113. agenda All agenda files
  10114. agenda-with-archives
  10115. All agenda files with any archive files associated with them
  10116. \(file1 file2 ...)
  10117. If this is a list, all files in the list will be scanned
  10118. The remaining args are treated as settings for the skipping facilities of
  10119. the scanner. The following items can be given here:
  10120. archive skip trees with the archive tag.
  10121. comment skip trees with the COMMENT keyword
  10122. function or Emacs Lisp form:
  10123. will be used as value for `org-agenda-skip-function', so whenever
  10124. the the function returns t, FUNC will not be called for that
  10125. entry and search will continue from the point where the
  10126. function leaves it.
  10127. If your function needs to retrieve the tags including inherited tags
  10128. at the *current* entry, you can use the value of the variable
  10129. `org-scanner-tags' which will be much faster than getting the value
  10130. with `org-get-tags-at'. If your function gets properties with
  10131. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10132. to t around the call to `org-entry-properties' to get the same speedup.
  10133. Note that if your function moves around to retrieve tags and properties at
  10134. a *different* entry, you cannot use these techniques."
  10135. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10136. (org-agenda-skip-archived-trees (memq 'archive skip))
  10137. (org-agenda-skip-comment-trees (memq 'comment skip))
  10138. (org-agenda-skip-function
  10139. (car (org-delete-all '(comment archive) skip)))
  10140. (org-tags-match-list-sublevels t)
  10141. matcher file res
  10142. org-todo-keywords-for-agenda
  10143. org-done-keywords-for-agenda
  10144. org-todo-keyword-alist-for-agenda
  10145. org-tag-alist-for-agenda)
  10146. (cond
  10147. ((eq match t) (setq matcher t))
  10148. ((eq match nil) (setq matcher t))
  10149. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10150. (save-excursion
  10151. (save-restriction
  10152. (when (eq scope 'tree)
  10153. (org-back-to-heading t)
  10154. (org-narrow-to-subtree)
  10155. (setq scope nil))
  10156. (if (not scope)
  10157. (progn
  10158. (org-prepare-agenda-buffers
  10159. (list (buffer-file-name (current-buffer))))
  10160. (setq res (org-scan-tags func matcher)))
  10161. ;; Get the right scope
  10162. (cond
  10163. ((and scope (listp scope) (symbolp (car scope)))
  10164. (setq scope (eval scope)))
  10165. ((eq scope 'agenda)
  10166. (setq scope (org-agenda-files t)))
  10167. ((eq scope 'agenda-with-archives)
  10168. (setq scope (org-agenda-files t))
  10169. (setq scope (org-add-archive-files scope)))
  10170. ((eq scope 'file)
  10171. (setq scope (list (buffer-file-name))))
  10172. ((eq scope 'file-with-archives)
  10173. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10174. (org-prepare-agenda-buffers scope)
  10175. (while (setq file (pop scope))
  10176. (with-current-buffer (org-find-base-buffer-visiting file)
  10177. (save-excursion
  10178. (save-restriction
  10179. (widen)
  10180. (goto-char (point-min))
  10181. (setq res (append res (org-scan-tags func matcher))))))))))
  10182. res))
  10183. ;;;; Properties
  10184. ;;; Setting and retrieving properties
  10185. (defconst org-special-properties
  10186. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10187. "TIMESTAMP" "TIMESTAMP_IA")
  10188. "The special properties valid in Org-mode.
  10189. These are properties that are not defined in the property drawer,
  10190. but in some other way.")
  10191. (defconst org-default-properties
  10192. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10193. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10194. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10195. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10196. "ORDERED" "NOBLOCKING" "COOKIE_DATA")
  10197. "Some properties that are used by Org-mode for various purposes.
  10198. Being in this list makes sure that they are offered for completion.")
  10199. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10200. "Regular expression matching the first line of a property drawer.")
  10201. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10202. "Regular expression matching the first line of a property drawer.")
  10203. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10204. "Regular expression matching the first line of a property drawer.")
  10205. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10206. "Regular expression matching the first line of a property drawer.")
  10207. (defconst org-property-drawer-re
  10208. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10209. org-property-end-re "\\)\n?")
  10210. "Matches an entire property drawer.")
  10211. (defconst org-clock-drawer-re
  10212. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10213. org-property-end-re "\\)\n?")
  10214. "Matches an entire clock drawer.")
  10215. (defun org-property-action ()
  10216. "Do an action on properties."
  10217. (interactive)
  10218. (let (c)
  10219. (org-at-property-p)
  10220. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10221. (setq c (read-char-exclusive))
  10222. (cond
  10223. ((equal c ?s)
  10224. (call-interactively 'org-set-property))
  10225. ((equal c ?d)
  10226. (call-interactively 'org-delete-property))
  10227. ((equal c ?D)
  10228. (call-interactively 'org-delete-property-globally))
  10229. ((equal c ?c)
  10230. (call-interactively 'org-compute-property-at-point))
  10231. (t (error "No such property action %c" c)))))
  10232. (defun org-at-property-p ()
  10233. "Is the cursor in a property line?"
  10234. ;; FIXME: Does not check if we are actually in the drawer.
  10235. ;; FIXME: also returns true on any drawers.....
  10236. ;; This is used by C-c C-c for property action.
  10237. (save-excursion
  10238. (beginning-of-line 1)
  10239. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10240. (defun org-get-property-block (&optional beg end force)
  10241. "Return the (beg . end) range of the body of the property drawer.
  10242. BEG and END can be beginning and end of subtree, if not given
  10243. they will be found.
  10244. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10245. (catch 'exit
  10246. (save-excursion
  10247. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10248. (end (or end (progn (outline-next-heading) (point)))))
  10249. (goto-char beg)
  10250. (if (re-search-forward org-property-start-re end t)
  10251. (setq beg (1+ (match-end 0)))
  10252. (if force
  10253. (save-excursion
  10254. (org-insert-property-drawer)
  10255. (setq end (progn (outline-next-heading) (point))))
  10256. (throw 'exit nil))
  10257. (goto-char beg)
  10258. (if (re-search-forward org-property-start-re end t)
  10259. (setq beg (1+ (match-end 0)))))
  10260. (if (re-search-forward org-property-end-re end t)
  10261. (setq end (match-beginning 0))
  10262. (or force (throw 'exit nil))
  10263. (goto-char beg)
  10264. (setq end beg)
  10265. (org-indent-line-function)
  10266. (insert ":END:\n"))
  10267. (cons beg end)))))
  10268. (defun org-entry-properties (&optional pom which)
  10269. "Get all properties of the entry at point-or-marker POM.
  10270. This includes the TODO keyword, the tags, time strings for deadline,
  10271. scheduled, and clocking, and any additional properties defined in the
  10272. entry. The return value is an alist, keys may occur multiple times
  10273. if the property key was used several times.
  10274. POM may also be nil, in which case the current entry is used.
  10275. If WHICH is nil or `all', get all properties. If WHICH is
  10276. `special' or `standard', only get that subclass."
  10277. (setq which (or which 'all))
  10278. (org-with-point-at pom
  10279. (let ((clockstr (substring org-clock-string 0 -1))
  10280. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10281. beg end range props sum-props key value string clocksum)
  10282. (save-excursion
  10283. (when (condition-case nil
  10284. (and (org-mode-p) (org-back-to-heading t))
  10285. (error nil))
  10286. (setq beg (point))
  10287. (setq sum-props (get-text-property (point) 'org-summaries))
  10288. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10289. (outline-next-heading)
  10290. (setq end (point))
  10291. (when (memq which '(all special))
  10292. ;; Get the special properties, like TODO and tags
  10293. (goto-char beg)
  10294. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10295. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10296. (when (looking-at org-priority-regexp)
  10297. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10298. (when (and (setq value (org-get-tags-string))
  10299. (string-match "\\S-" value))
  10300. (push (cons "TAGS" value) props))
  10301. (when (setq value (org-get-tags-at))
  10302. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10303. props))
  10304. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10305. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10306. string (if (equal key clockstr)
  10307. (org-no-properties
  10308. (org-trim
  10309. (buffer-substring
  10310. (match-beginning 3) (goto-char (point-at-eol)))))
  10311. (substring (org-match-string-no-properties 3) 1 -1)))
  10312. (unless key
  10313. (if (= (char-after (match-beginning 3)) ?\[)
  10314. (setq key "TIMESTAMP_IA")
  10315. (setq key "TIMESTAMP")))
  10316. (when (or (equal key clockstr) (not (assoc key props)))
  10317. (push (cons key string) props)))
  10318. )
  10319. (when (memq which '(all standard))
  10320. ;; Get the standard properties, like :PROP: ...
  10321. (setq range (org-get-property-block beg end))
  10322. (when range
  10323. (goto-char (car range))
  10324. (while (re-search-forward
  10325. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10326. (cdr range) t)
  10327. (setq key (org-match-string-no-properties 1)
  10328. value (org-trim (or (org-match-string-no-properties 2) "")))
  10329. (unless (member key excluded)
  10330. (push (cons key (or value "")) props)))))
  10331. (if clocksum
  10332. (push (cons "CLOCKSUM"
  10333. (org-columns-number-to-string (/ (float clocksum) 60.)
  10334. 'add_times))
  10335. props))
  10336. (unless (assoc "CATEGORY" props)
  10337. (setq value (or (org-get-category)
  10338. (progn (org-refresh-category-properties)
  10339. (org-get-category))))
  10340. (push (cons "CATEGORY" value) props))
  10341. (append sum-props (nreverse props)))))))
  10342. (defun org-entry-get (pom property &optional inherit)
  10343. "Get value of PROPERTY for entry at point-or-marker POM.
  10344. If INHERIT is non-nil and the entry does not have the property,
  10345. then also check higher levels of the hierarchy.
  10346. If INHERIT is the symbol `selective', use inheritance only if the setting
  10347. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10348. If the property is present but empty, the return value is the empty string.
  10349. If the property is not present at all, nil is returned."
  10350. (org-with-point-at pom
  10351. (if (and inherit (if (eq inherit 'selective)
  10352. (org-property-inherit-p property)
  10353. t))
  10354. (org-entry-get-with-inheritance property)
  10355. (if (member property org-special-properties)
  10356. ;; We need a special property. Use brute force, get all properties.
  10357. (cdr (assoc property (org-entry-properties nil 'special)))
  10358. (let ((range (org-get-property-block)))
  10359. (if (and range
  10360. (goto-char (car range))
  10361. (re-search-forward
  10362. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10363. (cdr range) t))
  10364. ;; Found the property, return it.
  10365. (if (match-end 1)
  10366. (org-match-string-no-properties 1)
  10367. "")))))))
  10368. (defun org-property-or-variable-value (var &optional inherit)
  10369. "Check if there is a property fixing the value of VAR.
  10370. If yes, return this value. If not, return the current value of the variable."
  10371. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10372. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10373. (read prop)
  10374. (symbol-value var))))
  10375. (defun org-entry-delete (pom property)
  10376. "Delete the property PROPERTY from entry at point-or-marker POM."
  10377. (org-with-point-at pom
  10378. (if (member property org-special-properties)
  10379. nil ; cannot delete these properties.
  10380. (let ((range (org-get-property-block)))
  10381. (if (and range
  10382. (goto-char (car range))
  10383. (re-search-forward
  10384. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10385. (cdr range) t))
  10386. (progn
  10387. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10388. t)
  10389. nil)))))
  10390. ;; Multi-values properties are properties that contain multiple values
  10391. ;; These values are assumed to be single words, separated by whitespace.
  10392. (defun org-entry-add-to-multivalued-property (pom property value)
  10393. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10394. (let* ((old (org-entry-get pom property))
  10395. (values (and old (org-split-string old "[ \t]"))))
  10396. (setq value (org-entry-protect-space value))
  10397. (unless (member value values)
  10398. (setq values (cons value values))
  10399. (org-entry-put pom property
  10400. (mapconcat 'identity values " ")))))
  10401. (defun org-entry-remove-from-multivalued-property (pom property value)
  10402. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10403. (let* ((old (org-entry-get pom property))
  10404. (values (and old (org-split-string old "[ \t]"))))
  10405. (setq value (org-entry-protect-space value))
  10406. (when (member value values)
  10407. (setq values (delete value values))
  10408. (org-entry-put pom property
  10409. (mapconcat 'identity values " ")))))
  10410. (defun org-entry-member-in-multivalued-property (pom property value)
  10411. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10412. (let* ((old (org-entry-get pom property))
  10413. (values (and old (org-split-string old "[ \t]"))))
  10414. (setq value (org-entry-protect-space value))
  10415. (member value values)))
  10416. (defun org-entry-get-multivalued-property (pom property)
  10417. "Return a list of values in a multivalued property."
  10418. (let* ((value (org-entry-get pom property))
  10419. (values (and value (org-split-string value "[ \t]"))))
  10420. (mapcar 'org-entry-restore-space values)))
  10421. (defun org-entry-put-multivalued-property (pom property &rest values)
  10422. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10423. VALUES should be a list of strings. Spaces will be protected."
  10424. (org-entry-put pom property
  10425. (mapconcat 'org-entry-protect-space values " "))
  10426. (let* ((value (org-entry-get pom property))
  10427. (values (and value (org-split-string value "[ \t]"))))
  10428. (mapcar 'org-entry-restore-space values)))
  10429. (defun org-entry-protect-space (s)
  10430. "Protect spaces and newline in string S."
  10431. (while (string-match " " s)
  10432. (setq s (replace-match "%20" t t s)))
  10433. (while (string-match "\n" s)
  10434. (setq s (replace-match "%0A" t t s)))
  10435. s)
  10436. (defun org-entry-restore-space (s)
  10437. "Restore spaces and newline in string S."
  10438. (while (string-match "%20" s)
  10439. (setq s (replace-match " " t t s)))
  10440. (while (string-match "%0A" s)
  10441. (setq s (replace-match "\n" t t s)))
  10442. s)
  10443. (defvar org-entry-property-inherited-from (make-marker)
  10444. "Marker pointing to the entry from where a property was inherited.
  10445. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10446. location of the entry where the inheritance search matched. If there was
  10447. no match, the marker will point nowhere.
  10448. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10449. is set.")
  10450. (defun org-entry-get-with-inheritance (property)
  10451. "Get entry property, and search higher levels if not present."
  10452. (move-marker org-entry-property-inherited-from nil)
  10453. (let (tmp)
  10454. (save-excursion
  10455. (save-restriction
  10456. (widen)
  10457. (catch 'ex
  10458. (while t
  10459. (when (setq tmp (org-entry-get nil property))
  10460. (org-back-to-heading t)
  10461. (move-marker org-entry-property-inherited-from (point))
  10462. (throw 'ex tmp))
  10463. (or (org-up-heading-safe) (throw 'ex nil)))))
  10464. (or tmp
  10465. (cdr (assoc property org-file-properties))
  10466. (cdr (assoc property org-global-properties))
  10467. (cdr (assoc property org-global-properties-fixed))))))
  10468. (defun org-entry-put (pom property value)
  10469. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10470. (org-with-point-at pom
  10471. (org-back-to-heading t)
  10472. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10473. range)
  10474. (cond
  10475. ((equal property "TODO")
  10476. (when (and (stringp value) (string-match "\\S-" value)
  10477. (not (member value org-todo-keywords-1)))
  10478. (error "\"%s\" is not a valid TODO state" value))
  10479. (if (or (not value)
  10480. (not (string-match "\\S-" value)))
  10481. (setq value 'none))
  10482. (org-todo value)
  10483. (org-set-tags nil 'align))
  10484. ((equal property "PRIORITY")
  10485. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10486. (string-to-char value) ?\ ))
  10487. (org-set-tags nil 'align))
  10488. ((equal property "SCHEDULED")
  10489. (if (re-search-forward org-scheduled-time-regexp end t)
  10490. (cond
  10491. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10492. ((eq value 'later) (org-timestamp-change 1 'day))
  10493. (t (call-interactively 'org-schedule)))
  10494. (call-interactively 'org-schedule)))
  10495. ((equal property "DEADLINE")
  10496. (if (re-search-forward org-deadline-time-regexp end t)
  10497. (cond
  10498. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10499. ((eq value 'later) (org-timestamp-change 1 'day))
  10500. (t (call-interactively 'org-deadline)))
  10501. (call-interactively 'org-deadline)))
  10502. ((member property org-special-properties)
  10503. (error "The %s property can not yet be set with `org-entry-put'"
  10504. property))
  10505. (t ; a non-special property
  10506. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10507. (setq range (org-get-property-block beg end 'force))
  10508. (goto-char (car range))
  10509. (if (re-search-forward
  10510. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10511. (progn
  10512. (delete-region (match-beginning 1) (match-end 1))
  10513. (goto-char (match-beginning 1)))
  10514. (goto-char (cdr range))
  10515. (insert "\n")
  10516. (backward-char 1)
  10517. (org-indent-line-function)
  10518. (insert ":" property ":"))
  10519. (and value (insert " " value))
  10520. (org-indent-line-function)))))))
  10521. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10522. "Get all property keys in the current buffer.
  10523. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10524. like tags and TODO state.
  10525. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10526. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10527. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10528. formats in the current buffer."
  10529. (let (rtn range cfmt s p)
  10530. (save-excursion
  10531. (save-restriction
  10532. (widen)
  10533. (goto-char (point-min))
  10534. (while (re-search-forward org-property-start-re nil t)
  10535. (setq range (org-get-property-block))
  10536. (goto-char (car range))
  10537. (while (re-search-forward
  10538. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10539. (cdr range) t)
  10540. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10541. (outline-next-heading))))
  10542. (when include-specials
  10543. (setq rtn (append org-special-properties rtn)))
  10544. (when include-defaults
  10545. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10546. (when include-columns
  10547. (save-excursion
  10548. (save-restriction
  10549. (widen)
  10550. (goto-char (point-min))
  10551. (while (re-search-forward
  10552. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10553. nil t)
  10554. (setq cfmt (match-string 2) s 0)
  10555. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10556. cfmt s)
  10557. (setq s (match-end 0)
  10558. p (match-string 1 cfmt))
  10559. (unless (or (equal p "ITEM")
  10560. (member p org-special-properties))
  10561. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10562. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10563. (defun org-property-values (key)
  10564. "Return a list of all values of property KEY."
  10565. (save-excursion
  10566. (save-restriction
  10567. (widen)
  10568. (goto-char (point-min))
  10569. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10570. values)
  10571. (while (re-search-forward re nil t)
  10572. (add-to-list 'values (org-trim (match-string 1))))
  10573. (delete "" values)))))
  10574. (defun org-insert-property-drawer ()
  10575. "Insert a property drawer into the current entry."
  10576. (interactive)
  10577. (org-back-to-heading t)
  10578. (looking-at outline-regexp)
  10579. (let ((indent (- (match-end 0)(match-beginning 0)))
  10580. (beg (point))
  10581. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10582. end hiddenp)
  10583. (outline-next-heading)
  10584. (setq end (point))
  10585. (goto-char beg)
  10586. (while (re-search-forward re end t))
  10587. (setq hiddenp (org-invisible-p))
  10588. (end-of-line 1)
  10589. (and (equal (char-after) ?\n) (forward-char 1))
  10590. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10591. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10592. ;; just skip this line
  10593. (beginning-of-line 2)
  10594. ;; Drawer start, find the end
  10595. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10596. (beginning-of-line 1)))
  10597. (org-skip-over-state-notes)
  10598. (skip-chars-backward " \t\n\r")
  10599. (if (eq (char-before) ?*) (forward-char 1))
  10600. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10601. (beginning-of-line 0)
  10602. (org-indent-to-column indent)
  10603. (beginning-of-line 2)
  10604. (org-indent-to-column indent)
  10605. (beginning-of-line 0)
  10606. (if hiddenp
  10607. (save-excursion
  10608. (org-back-to-heading t)
  10609. (hide-entry))
  10610. (org-flag-drawer t))))
  10611. (defun org-set-property (property value)
  10612. "In the current entry, set PROPERTY to VALUE.
  10613. When called interactively, this will prompt for a property name, offering
  10614. completion on existing and default properties. And then it will prompt
  10615. for a value, offering completion either on allowed values (via an inherited
  10616. xxx_ALL property) or on existing values in other instances of this property
  10617. in the current file."
  10618. (interactive
  10619. (let* ((completion-ignore-case t)
  10620. (keys (org-buffer-property-keys nil t t))
  10621. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10622. (prop (if (member prop0 keys)
  10623. prop0
  10624. (or (cdr (assoc (downcase prop0)
  10625. (mapcar (lambda (x) (cons (downcase x) x))
  10626. keys)))
  10627. prop0)))
  10628. (cur (org-entry-get nil prop))
  10629. (allowed (org-property-get-allowed-values nil prop 'table))
  10630. (existing (mapcar 'list (org-property-values prop)))
  10631. (val (if allowed
  10632. (org-completing-read "Value: " allowed nil 'req-match)
  10633. (let (org-completion-use-ido)
  10634. (org-completing-read
  10635. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10636. (concat "[" cur "]") "")
  10637. ": ")
  10638. existing nil nil "" nil cur)))))
  10639. (list prop (if (equal val "") cur val))))
  10640. (unless (equal (org-entry-get nil property) value)
  10641. (org-entry-put nil property value)))
  10642. (defun org-delete-property (property)
  10643. "In the current entry, delete PROPERTY."
  10644. (interactive
  10645. (let* ((completion-ignore-case t)
  10646. (prop (org-ido-completing-read
  10647. "Property: " (org-entry-properties nil 'standard))))
  10648. (list prop)))
  10649. (message "Property %s %s" property
  10650. (if (org-entry-delete nil property)
  10651. "deleted"
  10652. "was not present in the entry")))
  10653. (defun org-delete-property-globally (property)
  10654. "Remove PROPERTY globally, from all entries."
  10655. (interactive
  10656. (let* ((completion-ignore-case t)
  10657. (prop (org-ido-completing-read
  10658. "Globally remove property: "
  10659. (mapcar 'list (org-buffer-property-keys)))))
  10660. (list prop)))
  10661. (save-excursion
  10662. (save-restriction
  10663. (widen)
  10664. (goto-char (point-min))
  10665. (let ((cnt 0))
  10666. (while (re-search-forward
  10667. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10668. nil t)
  10669. (setq cnt (1+ cnt))
  10670. (replace-match ""))
  10671. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10672. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10673. (defun org-compute-property-at-point ()
  10674. "Compute the property at point.
  10675. This looks for an enclosing column format, extracts the operator and
  10676. then applies it to the property in the column format's scope."
  10677. (interactive)
  10678. (unless (org-at-property-p)
  10679. (error "Not at a property"))
  10680. (let ((prop (org-match-string-no-properties 2)))
  10681. (org-columns-get-format-and-top-level)
  10682. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10683. (error "No operator defined for property %s" prop))
  10684. (org-columns-compute prop)))
  10685. (defun org-property-get-allowed-values (pom property &optional table)
  10686. "Get allowed values for the property PROPERTY.
  10687. When TABLE is non-nil, return an alist that can directly be used for
  10688. completion."
  10689. (let (vals)
  10690. (cond
  10691. ((equal property "TODO")
  10692. (setq vals (org-with-point-at pom
  10693. (append org-todo-keywords-1 '("")))))
  10694. ((equal property "PRIORITY")
  10695. (let ((n org-lowest-priority))
  10696. (while (>= n org-highest-priority)
  10697. (push (char-to-string n) vals)
  10698. (setq n (1- n)))))
  10699. ((member property org-special-properties))
  10700. (t
  10701. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10702. (when (and vals (string-match "\\S-" vals))
  10703. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10704. (setq vals (mapcar (lambda (x)
  10705. (cond ((stringp x) x)
  10706. ((numberp x) (number-to-string x))
  10707. ((symbolp x) (symbol-name x))
  10708. (t "???")))
  10709. vals)))))
  10710. (if table (mapcar 'list vals) vals)))
  10711. (defun org-property-previous-allowed-value (&optional previous)
  10712. "Switch to the next allowed value for this property."
  10713. (interactive)
  10714. (org-property-next-allowed-value t))
  10715. (defun org-property-next-allowed-value (&optional previous)
  10716. "Switch to the next allowed value for this property."
  10717. (interactive)
  10718. (unless (org-at-property-p)
  10719. (error "Not at a property"))
  10720. (let* ((key (match-string 2))
  10721. (value (match-string 3))
  10722. (allowed (or (org-property-get-allowed-values (point) key)
  10723. (and (member value '("[ ]" "[-]" "[X]"))
  10724. '("[ ]" "[X]"))))
  10725. nval)
  10726. (unless allowed
  10727. (error "Allowed values for this property have not been defined"))
  10728. (if previous (setq allowed (reverse allowed)))
  10729. (if (member value allowed)
  10730. (setq nval (car (cdr (member value allowed)))))
  10731. (setq nval (or nval (car allowed)))
  10732. (if (equal nval value)
  10733. (error "Only one allowed value for this property"))
  10734. (org-at-property-p)
  10735. (replace-match (concat " :" key ": " nval) t t)
  10736. (org-indent-line-function)
  10737. (beginning-of-line 1)
  10738. (skip-chars-forward " \t")))
  10739. (defun org-find-entry-with-id (ident)
  10740. "Locate the entry that contains the ID property with exact value IDENT.
  10741. IDENT can be a string, a symbol or a number, this function will search for
  10742. the string representation of it.
  10743. Return the position where this entry starts, or nil if there is no such entry."
  10744. (interactive "sID: ")
  10745. (let ((id (cond
  10746. ((stringp ident) ident)
  10747. ((symbol-name ident) (symbol-name ident))
  10748. ((numberp ident) (number-to-string ident))
  10749. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10750. (case-fold-search nil))
  10751. (save-excursion
  10752. (save-restriction
  10753. (widen)
  10754. (goto-char (point-min))
  10755. (when (re-search-forward
  10756. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10757. nil t)
  10758. (org-back-to-heading)
  10759. (point))))))
  10760. ;;;; Timestamps
  10761. (defvar org-last-changed-timestamp nil)
  10762. (defvar org-last-inserted-timestamp nil
  10763. "The last time stamp inserted with `org-insert-time-stamp'.")
  10764. (defvar org-time-was-given) ; dynamically scoped parameter
  10765. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10766. (defvar org-ts-what) ; dynamically scoped parameter
  10767. (defun org-time-stamp (arg &optional inactive)
  10768. "Prompt for a date/time and insert a time stamp.
  10769. If the user specifies a time like HH:MM, or if this command is called
  10770. with a prefix argument, the time stamp will contain date and time.
  10771. Otherwise, only the date will be included. All parts of a date not
  10772. specified by the user will be filled in from the current date/time.
  10773. So if you press just return without typing anything, the time stamp
  10774. will represent the current date/time. If there is already a timestamp
  10775. at the cursor, it will be modified."
  10776. (interactive "P")
  10777. (let* ((ts nil)
  10778. (default-time
  10779. ;; Default time is either today, or, when entering a range,
  10780. ;; the range start.
  10781. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10782. (save-excursion
  10783. (re-search-backward
  10784. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10785. (- (point) 20) t)))
  10786. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10787. (current-time)))
  10788. (default-input (and ts (org-get-compact-tod ts)))
  10789. org-time-was-given org-end-time-was-given time)
  10790. (cond
  10791. ((and (org-at-timestamp-p t)
  10792. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10793. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10794. (insert "--")
  10795. (setq time (let ((this-command this-command))
  10796. (org-read-date arg 'totime nil nil
  10797. default-time default-input)))
  10798. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10799. ((org-at-timestamp-p t)
  10800. (setq time (let ((this-command this-command))
  10801. (org-read-date arg 'totime nil nil default-time default-input)))
  10802. (when (org-at-timestamp-p t) ; just to get the match data
  10803. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10804. (replace-match "")
  10805. (setq org-last-changed-timestamp
  10806. (org-insert-time-stamp
  10807. time (or org-time-was-given arg)
  10808. inactive nil nil (list org-end-time-was-given))))
  10809. (message "Timestamp updated"))
  10810. (t
  10811. (setq time (let ((this-command this-command))
  10812. (org-read-date arg 'totime nil nil default-time default-input)))
  10813. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10814. nil nil (list org-end-time-was-given))))))
  10815. ;; FIXME: can we use this for something else, like computing time differences?
  10816. (defun org-get-compact-tod (s)
  10817. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10818. (let* ((t1 (match-string 1 s))
  10819. (h1 (string-to-number (match-string 2 s)))
  10820. (m1 (string-to-number (match-string 3 s)))
  10821. (t2 (and (match-end 4) (match-string 5 s)))
  10822. (h2 (and t2 (string-to-number (match-string 6 s))))
  10823. (m2 (and t2 (string-to-number (match-string 7 s))))
  10824. dh dm)
  10825. (if (not t2)
  10826. t1
  10827. (setq dh (- h2 h1) dm (- m2 m1))
  10828. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10829. (concat t1 "+" (number-to-string dh)
  10830. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10831. (defun org-time-stamp-inactive (&optional arg)
  10832. "Insert an inactive time stamp.
  10833. An inactive time stamp is enclosed in square brackets instead of angle
  10834. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10835. does not link to the calendar and cannot be changed with the S-cursor keys.
  10836. So these are more for recording a certain time/date."
  10837. (interactive "P")
  10838. (org-time-stamp arg 'inactive))
  10839. (defvar org-date-ovl (org-make-overlay 1 1))
  10840. (org-overlay-put org-date-ovl 'face 'org-warning)
  10841. (org-detach-overlay org-date-ovl)
  10842. (defvar org-ans1) ; dynamically scoped parameter
  10843. (defvar org-ans2) ; dynamically scoped parameter
  10844. (defvar org-plain-time-of-day-regexp) ; defined below
  10845. (defvar org-overriding-default-time nil) ; dynamically scoped
  10846. (defvar org-read-date-overlay nil)
  10847. (defvar org-dcst nil) ; dynamically scoped
  10848. (defvar org-read-date-history nil)
  10849. (defun org-read-date (&optional with-time to-time from-string prompt
  10850. default-time default-input)
  10851. "Read a date, possibly a time, and make things smooth for the user.
  10852. The prompt will suggest to enter an ISO date, but you can also enter anything
  10853. which will at least partially be understood by `parse-time-string'.
  10854. Unrecognized parts of the date will default to the current day, month, year,
  10855. hour and minute. If this command is called to replace a timestamp at point,
  10856. of to enter the second timestamp of a range, the default time is taken from the
  10857. existing stamp. For example,
  10858. 3-2-5 --> 2003-02-05
  10859. feb 15 --> currentyear-02-15
  10860. sep 12 9 --> 2009-09-12
  10861. 12:45 --> today 12:45
  10862. 22 sept 0:34 --> currentyear-09-22 0:34
  10863. 12 --> currentyear-currentmonth-12
  10864. Fri --> nearest Friday (today or later)
  10865. etc.
  10866. Furthermore you can specify a relative date by giving, as the *first* thing
  10867. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10868. change in days weeks, months, years.
  10869. With a single plus or minus, the date is relative to today. With a double
  10870. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10871. +4d --> four days from today
  10872. +4 --> same as above
  10873. +2w --> two weeks from today
  10874. ++5 --> five days from default date
  10875. The function understands only English month and weekday abbreviations,
  10876. but this can be configured with the variables `parse-time-months' and
  10877. `parse-time-weekdays'.
  10878. While prompting, a calendar is popped up - you can also select the
  10879. date with the mouse (button 1). The calendar shows a period of three
  10880. months. To scroll it to other months, use the keys `>' and `<'.
  10881. If you don't like the calendar, turn it off with
  10882. \(setq org-read-date-popup-calendar nil)
  10883. With optional argument TO-TIME, the date will immediately be converted
  10884. to an internal time.
  10885. With an optional argument WITH-TIME, the prompt will suggest to also
  10886. insert a time. Note that when WITH-TIME is not set, you can still
  10887. enter a time, and this function will inform the calling routine about
  10888. this change. The calling routine may then choose to change the format
  10889. used to insert the time stamp into the buffer to include the time.
  10890. With optional argument FROM-STRING, read from this string instead from
  10891. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10892. the time/date that is used for everything that is not specified by the
  10893. user."
  10894. (require 'parse-time)
  10895. (let* ((org-time-stamp-rounding-minutes
  10896. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10897. (org-dcst org-display-custom-times)
  10898. (ct (org-current-time))
  10899. (def (or org-overriding-default-time default-time ct))
  10900. (defdecode (decode-time def))
  10901. (dummy (progn
  10902. (when (< (nth 2 defdecode) org-extend-today-until)
  10903. (setcar (nthcdr 2 defdecode) -1)
  10904. (setcar (nthcdr 1 defdecode) 59)
  10905. (setq def (apply 'encode-time defdecode)
  10906. defdecode (decode-time def)))))
  10907. (calendar-move-hook nil)
  10908. (calendar-view-diary-initially-flag nil)
  10909. (view-diary-entries-initially nil)
  10910. (calendar-view-holidays-initially-flag nil)
  10911. (view-calendar-holidays-initially nil)
  10912. (timestr (format-time-string
  10913. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10914. (prompt (concat (if prompt (concat prompt " ") "")
  10915. (format "Date+time [%s]: " timestr)))
  10916. ans (org-ans0 "") org-ans1 org-ans2 final)
  10917. (cond
  10918. (from-string (setq ans from-string))
  10919. (org-read-date-popup-calendar
  10920. (save-excursion
  10921. (save-window-excursion
  10922. (calendar)
  10923. (calendar-forward-day (- (time-to-days def)
  10924. (calendar-absolute-from-gregorian
  10925. (calendar-current-date))))
  10926. (org-eval-in-calendar nil t)
  10927. (let* ((old-map (current-local-map))
  10928. (map (copy-keymap calendar-mode-map))
  10929. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10930. (org-defkey map (kbd "RET") 'org-calendar-select)
  10931. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10932. 'org-calendar-select-mouse)
  10933. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10934. 'org-calendar-select-mouse)
  10935. (org-defkey minibuffer-local-map [(meta shift left)]
  10936. (lambda () (interactive)
  10937. (org-eval-in-calendar '(calendar-backward-month 1))))
  10938. (org-defkey minibuffer-local-map [(meta shift right)]
  10939. (lambda () (interactive)
  10940. (org-eval-in-calendar '(calendar-forward-month 1))))
  10941. (org-defkey minibuffer-local-map [(meta shift up)]
  10942. (lambda () (interactive)
  10943. (org-eval-in-calendar '(calendar-backward-year 1))))
  10944. (org-defkey minibuffer-local-map [(meta shift down)]
  10945. (lambda () (interactive)
  10946. (org-eval-in-calendar '(calendar-forward-year 1))))
  10947. (org-defkey minibuffer-local-map [(shift up)]
  10948. (lambda () (interactive)
  10949. (org-eval-in-calendar '(calendar-backward-week 1))))
  10950. (org-defkey minibuffer-local-map [(shift down)]
  10951. (lambda () (interactive)
  10952. (org-eval-in-calendar '(calendar-forward-week 1))))
  10953. (org-defkey minibuffer-local-map [(shift left)]
  10954. (lambda () (interactive)
  10955. (org-eval-in-calendar '(calendar-backward-day 1))))
  10956. (org-defkey minibuffer-local-map [(shift right)]
  10957. (lambda () (interactive)
  10958. (org-eval-in-calendar '(calendar-forward-day 1))))
  10959. (org-defkey minibuffer-local-map ">"
  10960. (lambda () (interactive)
  10961. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10962. (org-defkey minibuffer-local-map "<"
  10963. (lambda () (interactive)
  10964. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10965. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10966. (unwind-protect
  10967. (progn
  10968. (use-local-map map)
  10969. (add-hook 'post-command-hook 'org-read-date-display)
  10970. (setq org-ans0 (read-string prompt default-input
  10971. 'org-read-date-history nil))
  10972. ;; org-ans0: from prompt
  10973. ;; org-ans1: from mouse click
  10974. ;; org-ans2: from calendar motion
  10975. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10976. (remove-hook 'post-command-hook 'org-read-date-display)
  10977. (use-local-map old-map)
  10978. (when org-read-date-overlay
  10979. (org-delete-overlay org-read-date-overlay)
  10980. (setq org-read-date-overlay nil)))))))
  10981. (t ; Naked prompt only
  10982. (unwind-protect
  10983. (setq ans (read-string prompt default-input
  10984. 'org-read-date-history timestr))
  10985. (when org-read-date-overlay
  10986. (org-delete-overlay org-read-date-overlay)
  10987. (setq org-read-date-overlay nil)))))
  10988. (setq final (org-read-date-analyze ans def defdecode))
  10989. (if to-time
  10990. (apply 'encode-time final)
  10991. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10992. (format "%04d-%02d-%02d %02d:%02d"
  10993. (nth 5 final) (nth 4 final) (nth 3 final)
  10994. (nth 2 final) (nth 1 final))
  10995. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10996. (defvar def)
  10997. (defvar defdecode)
  10998. (defvar with-time)
  10999. (defun org-read-date-display ()
  11000. "Display the current date prompt interpretation in the minibuffer."
  11001. (when org-read-date-display-live
  11002. (when org-read-date-overlay
  11003. (org-delete-overlay org-read-date-overlay))
  11004. (let ((p (point)))
  11005. (end-of-line 1)
  11006. (while (not (equal (buffer-substring
  11007. (max (point-min) (- (point) 4)) (point))
  11008. " "))
  11009. (insert " "))
  11010. (goto-char p))
  11011. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11012. " " (or org-ans1 org-ans2)))
  11013. (org-end-time-was-given nil)
  11014. (f (org-read-date-analyze ans def defdecode))
  11015. (fmts (if org-dcst
  11016. org-time-stamp-custom-formats
  11017. org-time-stamp-formats))
  11018. (fmt (if (or with-time
  11019. (and (boundp 'org-time-was-given) org-time-was-given))
  11020. (cdr fmts)
  11021. (car fmts)))
  11022. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11023. (when (and org-end-time-was-given
  11024. (string-match org-plain-time-of-day-regexp txt))
  11025. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11026. org-end-time-was-given
  11027. (substring txt (match-end 0)))))
  11028. (setq org-read-date-overlay
  11029. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11030. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11031. (defun org-read-date-analyze (ans def defdecode)
  11032. "Analyse the combined answer of the date prompt."
  11033. ;; FIXME: cleanup and comment
  11034. (let (delta deltan deltaw deltadef year month day
  11035. hour minute second wday pm h2 m2 tl wday1
  11036. iso-year iso-weekday iso-week iso-year iso-date)
  11037. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11038. (setq ans "+0"))
  11039. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11040. (setq ans (replace-match "" t t ans)
  11041. deltan (car delta)
  11042. deltaw (nth 1 delta)
  11043. deltadef (nth 2 delta)))
  11044. ;; Check if there is an iso week date in there
  11045. ;; If yes, sore the info and postpone interpreting it until the rest
  11046. ;; of the parsing is done
  11047. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11048. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11049. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11050. iso-week (string-to-number (match-string 2 ans)))
  11051. (setq ans (replace-match "" t t ans)))
  11052. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11053. (when (string-match
  11054. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11055. (setq year (if (match-end 2)
  11056. (string-to-number (match-string 2 ans))
  11057. (string-to-number (format-time-string "%Y")))
  11058. month (string-to-number (match-string 3 ans))
  11059. day (string-to-number (match-string 4 ans)))
  11060. (if (< year 100) (setq year (+ 2000 year)))
  11061. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11062. t nil ans)))
  11063. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11064. ;; If there is a time with am/pm, and *no* time without it, we convert
  11065. ;; so that matching will be successful.
  11066. (loop for i from 1 to 2 do ; twice, for end time as well
  11067. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11068. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11069. (setq hour (string-to-number (match-string 1 ans))
  11070. minute (if (match-end 3)
  11071. (string-to-number (match-string 3 ans))
  11072. 0)
  11073. pm (equal ?p
  11074. (string-to-char (downcase (match-string 4 ans)))))
  11075. (if (and (= hour 12) (not pm))
  11076. (setq hour 0)
  11077. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11078. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11079. t t ans))))
  11080. ;; Check if a time range is given as a duration
  11081. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11082. (setq hour (string-to-number (match-string 1 ans))
  11083. h2 (+ hour (string-to-number (match-string 3 ans)))
  11084. minute (string-to-number (match-string 2 ans))
  11085. m2 (+ minute (if (match-end 5) (string-to-number
  11086. (match-string 5 ans))0)))
  11087. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11088. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11089. t t ans)))
  11090. ;; Check if there is a time range
  11091. (when (boundp 'org-end-time-was-given)
  11092. (setq org-time-was-given nil)
  11093. (when (and (string-match org-plain-time-of-day-regexp ans)
  11094. (match-end 8))
  11095. (setq org-end-time-was-given (match-string 8 ans))
  11096. (setq ans (concat (substring ans 0 (match-beginning 7))
  11097. (substring ans (match-end 7))))))
  11098. (setq tl (parse-time-string ans)
  11099. day (or (nth 3 tl) (nth 3 defdecode))
  11100. month (or (nth 4 tl)
  11101. (if (and org-read-date-prefer-future
  11102. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11103. (1+ (nth 4 defdecode))
  11104. (nth 4 defdecode)))
  11105. year (or (nth 5 tl)
  11106. (if (and org-read-date-prefer-future
  11107. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11108. (1+ (nth 5 defdecode))
  11109. (nth 5 defdecode)))
  11110. hour (or (nth 2 tl) (nth 2 defdecode))
  11111. minute (or (nth 1 tl) (nth 1 defdecode))
  11112. second (or (nth 0 tl) 0)
  11113. wday (nth 6 tl))
  11114. ;; Special date definitions below
  11115. (cond
  11116. (iso-week
  11117. ;; There was an iso week
  11118. (setq year (or iso-year year)
  11119. day (or iso-weekday wday 1)
  11120. wday nil ; to make sure that the trigger below does not match
  11121. iso-date (calendar-gregorian-from-absolute
  11122. (calendar-absolute-from-iso
  11123. (list iso-week day year))))
  11124. ; FIXME: Should we also push ISO weeks into the future?
  11125. ; (when (and org-read-date-prefer-future
  11126. ; (not iso-year)
  11127. ; (< (calendar-absolute-from-gregorian iso-date)
  11128. ; (time-to-days (current-time))))
  11129. ; (setq year (1+ year)
  11130. ; iso-date (calendar-gregorian-from-absolute
  11131. ; (calendar-absolute-from-iso
  11132. ; (list iso-week day year)))))
  11133. (setq month (car iso-date)
  11134. year (nth 2 iso-date)
  11135. day (nth 1 iso-date)))
  11136. (deltan
  11137. (unless deltadef
  11138. (let ((now (decode-time (current-time))))
  11139. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11140. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11141. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11142. ((equal deltaw "m") (setq month (+ month deltan)))
  11143. ((equal deltaw "y") (setq year (+ year deltan)))))
  11144. ((and wday (not (nth 3 tl)))
  11145. ;; Weekday was given, but no day, so pick that day in the week
  11146. ;; on or after the derived date.
  11147. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11148. (unless (equal wday wday1)
  11149. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11150. (if (and (boundp 'org-time-was-given)
  11151. (nth 2 tl))
  11152. (setq org-time-was-given t))
  11153. (if (< year 100) (setq year (+ 2000 year)))
  11154. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11155. (list second minute hour day month year)))
  11156. (defvar parse-time-weekdays)
  11157. (defun org-read-date-get-relative (s today default)
  11158. "Check string S for special relative date string.
  11159. TODAY and DEFAULT are internal times, for today and for a default.
  11160. Return shift list (N what def-flag)
  11161. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11162. N is the number of WHATs to shift.
  11163. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11164. the DEFAULT date rather than TODAY."
  11165. (when (and
  11166. (string-match
  11167. (concat
  11168. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11169. "\\([0-9]+\\)?"
  11170. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11171. "\\([ \t]\\|$\\)") s)
  11172. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11173. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11174. (string-to-char (substring (match-string 1 s) -1))
  11175. ?+))
  11176. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11177. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11178. (what (if (match-end 3) (match-string 3 s) "d"))
  11179. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11180. (date (if rel default today))
  11181. (wday (nth 6 (decode-time date)))
  11182. delta)
  11183. (if wday1
  11184. (progn
  11185. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11186. (if (= dir ?-) (setq delta (- delta 7)))
  11187. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11188. (list delta "d" rel))
  11189. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11190. (defun org-eval-in-calendar (form &optional keepdate)
  11191. "Eval FORM in the calendar window and return to current window.
  11192. Also, store the cursor date in variable org-ans2."
  11193. (let ((sw (selected-window)))
  11194. (select-window (get-buffer-window "*Calendar*"))
  11195. (eval form)
  11196. (when (and (not keepdate) (calendar-cursor-to-date))
  11197. (let* ((date (calendar-cursor-to-date))
  11198. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11199. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11200. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11201. (select-window sw)))
  11202. (defun org-calendar-select ()
  11203. "Return to `org-read-date' with the date currently selected.
  11204. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11205. (interactive)
  11206. (when (calendar-cursor-to-date)
  11207. (let* ((date (calendar-cursor-to-date))
  11208. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11209. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11210. (if (active-minibuffer-window) (exit-minibuffer))))
  11211. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11212. "Insert a date stamp for the date given by the internal TIME.
  11213. WITH-HM means, use the stamp format that includes the time of the day.
  11214. INACTIVE means use square brackets instead of angular ones, so that the
  11215. stamp will not contribute to the agenda.
  11216. PRE and POST are optional strings to be inserted before and after the
  11217. stamp.
  11218. The command returns the inserted time stamp."
  11219. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11220. stamp)
  11221. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11222. (insert-before-markers (or pre ""))
  11223. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11224. (when (listp extra)
  11225. (setq extra (car extra))
  11226. (if (and (stringp extra)
  11227. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11228. (setq extra (format "-%02d:%02d"
  11229. (string-to-number (match-string 1 extra))
  11230. (string-to-number (match-string 2 extra))))
  11231. (setq extra nil)))
  11232. (when extra
  11233. (backward-char 1)
  11234. (insert-before-markers extra)
  11235. (forward-char 1))
  11236. (insert-before-markers (or post ""))
  11237. (setq org-last-inserted-timestamp stamp)))
  11238. (defun org-toggle-time-stamp-overlays ()
  11239. "Toggle the use of custom time stamp formats."
  11240. (interactive)
  11241. (setq org-display-custom-times (not org-display-custom-times))
  11242. (unless org-display-custom-times
  11243. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11244. (while (setq p (next-single-property-change p 'display))
  11245. (if (and (get-text-property p 'display)
  11246. (eq (get-text-property p 'face) 'org-date))
  11247. (remove-text-properties
  11248. p (setq p (next-single-property-change p 'display))
  11249. '(display t))))
  11250. (set-buffer-modified-p bmp)))
  11251. (if (featurep 'xemacs)
  11252. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11253. (org-restart-font-lock)
  11254. (setq org-table-may-need-update t)
  11255. (if org-display-custom-times
  11256. (message "Time stamps are overlayed with custom format")
  11257. (message "Time stamp overlays removed")))
  11258. (defun org-display-custom-time (beg end)
  11259. "Overlay modified time stamp format over timestamp between BEG and END."
  11260. (let* ((ts (buffer-substring beg end))
  11261. t1 w1 with-hm tf time str w2 (off 0))
  11262. (save-match-data
  11263. (setq t1 (org-parse-time-string ts t))
  11264. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11265. (setq off (- (match-end 0) (match-beginning 0)))))
  11266. (setq end (- end off))
  11267. (setq w1 (- end beg)
  11268. with-hm (and (nth 1 t1) (nth 2 t1))
  11269. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11270. time (org-fix-decoded-time t1)
  11271. str (org-add-props
  11272. (format-time-string
  11273. (substring tf 1 -1) (apply 'encode-time time))
  11274. nil 'mouse-face 'highlight)
  11275. w2 (length str))
  11276. (if (not (= w2 w1))
  11277. (add-text-properties (1+ beg) (+ 2 beg)
  11278. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11279. (if (featurep 'xemacs)
  11280. (progn
  11281. (put-text-property beg end 'invisible t)
  11282. (put-text-property beg end 'end-glyph (make-glyph str)))
  11283. (put-text-property beg end 'display str))))
  11284. (defun org-translate-time (string)
  11285. "Translate all timestamps in STRING to custom format.
  11286. But do this only if the variable `org-display-custom-times' is set."
  11287. (when org-display-custom-times
  11288. (save-match-data
  11289. (let* ((start 0)
  11290. (re org-ts-regexp-both)
  11291. t1 with-hm inactive tf time str beg end)
  11292. (while (setq start (string-match re string start))
  11293. (setq beg (match-beginning 0)
  11294. end (match-end 0)
  11295. t1 (save-match-data
  11296. (org-parse-time-string (substring string beg end) t))
  11297. with-hm (and (nth 1 t1) (nth 2 t1))
  11298. inactive (equal (substring string beg (1+ beg)) "[")
  11299. tf (funcall (if with-hm 'cdr 'car)
  11300. org-time-stamp-custom-formats)
  11301. time (org-fix-decoded-time t1)
  11302. str (format-time-string
  11303. (concat
  11304. (if inactive "[" "<") (substring tf 1 -1)
  11305. (if inactive "]" ">"))
  11306. (apply 'encode-time time))
  11307. string (replace-match str t t string)
  11308. start (+ start (length str)))))))
  11309. string)
  11310. (defun org-fix-decoded-time (time)
  11311. "Set 0 instead of nil for the first 6 elements of time.
  11312. Don't touch the rest."
  11313. (let ((n 0))
  11314. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11315. (defun org-days-to-time (timestamp-string)
  11316. "Difference between TIMESTAMP-STRING and now in days."
  11317. (- (time-to-days (org-time-string-to-time timestamp-string))
  11318. (time-to-days (current-time))))
  11319. (defun org-deadline-close (timestamp-string &optional ndays)
  11320. "Is the time in TIMESTAMP-STRING close to the current date?"
  11321. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11322. (and (< (org-days-to-time timestamp-string) ndays)
  11323. (not (org-entry-is-done-p))))
  11324. (defun org-get-wdays (ts)
  11325. "Get the deadline lead time appropriate for timestring TS."
  11326. (cond
  11327. ((<= org-deadline-warning-days 0)
  11328. ;; 0 or negative, enforce this value no matter what
  11329. (- org-deadline-warning-days))
  11330. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11331. ;; lead time is specified.
  11332. (floor (* (string-to-number (match-string 1 ts))
  11333. (cdr (assoc (match-string 2 ts)
  11334. '(("d" . 1) ("w" . 7)
  11335. ("m" . 30.4) ("y" . 365.25)))))))
  11336. ;; go for the default.
  11337. (t org-deadline-warning-days)))
  11338. (defun org-calendar-select-mouse (ev)
  11339. "Return to `org-read-date' with the date currently selected.
  11340. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11341. (interactive "e")
  11342. (mouse-set-point ev)
  11343. (when (calendar-cursor-to-date)
  11344. (let* ((date (calendar-cursor-to-date))
  11345. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11346. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11347. (if (active-minibuffer-window) (exit-minibuffer))))
  11348. (defun org-check-deadlines (ndays)
  11349. "Check if there are any deadlines due or past due.
  11350. A deadline is considered due if it happens within `org-deadline-warning-days'
  11351. days from today's date. If the deadline appears in an entry marked DONE,
  11352. it is not shown. The prefix arg NDAYS can be used to test that many
  11353. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11354. (interactive "P")
  11355. (let* ((org-warn-days
  11356. (cond
  11357. ((equal ndays '(4)) 100000)
  11358. (ndays (prefix-numeric-value ndays))
  11359. (t (abs org-deadline-warning-days))))
  11360. (case-fold-search nil)
  11361. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11362. (callback
  11363. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11364. (message "%d deadlines past-due or due within %d days"
  11365. (org-occur regexp nil callback)
  11366. org-warn-days)))
  11367. (defun org-check-before-date (date)
  11368. "Check if there are deadlines or scheduled entries before DATE."
  11369. (interactive (list (org-read-date)))
  11370. (let ((case-fold-search nil)
  11371. (regexp (concat "\\<\\(" org-deadline-string
  11372. "\\|" org-scheduled-string
  11373. "\\) *<\\([^>]+\\)>"))
  11374. (callback
  11375. (lambda () (time-less-p
  11376. (org-time-string-to-time (match-string 2))
  11377. (org-time-string-to-time date)))))
  11378. (message "%d entries before %s"
  11379. (org-occur regexp nil callback) date)))
  11380. (defun org-check-after-date (date)
  11381. "Check if there are deadlines or scheduled entries after DATE."
  11382. (interactive (list (org-read-date)))
  11383. (let ((case-fold-search nil)
  11384. (regexp (concat "\\<\\(" org-deadline-string
  11385. "\\|" org-scheduled-string
  11386. "\\) *<\\([^>]+\\)>"))
  11387. (callback
  11388. (lambda () (not
  11389. (time-less-p
  11390. (org-time-string-to-time (match-string 2))
  11391. (org-time-string-to-time date))))))
  11392. (message "%d entries after %s"
  11393. (org-occur regexp nil callback) date)))
  11394. (defun org-evaluate-time-range (&optional to-buffer)
  11395. "Evaluate a time range by computing the difference between start and end.
  11396. Normally the result is just printed in the echo area, but with prefix arg
  11397. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11398. If the time range is actually in a table, the result is inserted into the
  11399. next column.
  11400. For time difference computation, a year is assumed to be exactly 365
  11401. days in order to avoid rounding problems."
  11402. (interactive "P")
  11403. (or
  11404. (org-clock-update-time-maybe)
  11405. (save-excursion
  11406. (unless (org-at-date-range-p t)
  11407. (goto-char (point-at-bol))
  11408. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11409. (if (not (org-at-date-range-p t))
  11410. (error "Not at a time-stamp range, and none found in current line")))
  11411. (let* ((ts1 (match-string 1))
  11412. (ts2 (match-string 2))
  11413. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11414. (match-end (match-end 0))
  11415. (time1 (org-time-string-to-time ts1))
  11416. (time2 (org-time-string-to-time ts2))
  11417. (t1 (time-to-seconds time1))
  11418. (t2 (time-to-seconds time2))
  11419. (diff (abs (- t2 t1)))
  11420. (negative (< (- t2 t1) 0))
  11421. ;; (ys (floor (* 365 24 60 60)))
  11422. (ds (* 24 60 60))
  11423. (hs (* 60 60))
  11424. (fy "%dy %dd %02d:%02d")
  11425. (fy1 "%dy %dd")
  11426. (fd "%dd %02d:%02d")
  11427. (fd1 "%dd")
  11428. (fh "%02d:%02d")
  11429. y d h m align)
  11430. (if havetime
  11431. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11432. y 0
  11433. d (floor (/ diff ds)) diff (mod diff ds)
  11434. h (floor (/ diff hs)) diff (mod diff hs)
  11435. m (floor (/ diff 60)))
  11436. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11437. y 0
  11438. d (floor (+ (/ diff ds) 0.5))
  11439. h 0 m 0))
  11440. (if (not to-buffer)
  11441. (message "%s" (org-make-tdiff-string y d h m))
  11442. (if (org-at-table-p)
  11443. (progn
  11444. (goto-char match-end)
  11445. (setq align t)
  11446. (and (looking-at " *|") (goto-char (match-end 0))))
  11447. (goto-char match-end))
  11448. (if (looking-at
  11449. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11450. (replace-match ""))
  11451. (if negative (insert " -"))
  11452. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11453. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11454. (insert " " (format fh h m))))
  11455. (if align (org-table-align))
  11456. (message "Time difference inserted")))))
  11457. (defun org-make-tdiff-string (y d h m)
  11458. (let ((fmt "")
  11459. (l nil))
  11460. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11461. l (push y l)))
  11462. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11463. l (push d l)))
  11464. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11465. l (push h l)))
  11466. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11467. l (push m l)))
  11468. (apply 'format fmt (nreverse l))))
  11469. (defun org-time-string-to-time (s)
  11470. (apply 'encode-time (org-parse-time-string s)))
  11471. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11472. "Convert a time stamp to an absolute day number.
  11473. If there is a specifyer for a cyclic time stamp, get the closest date to
  11474. DAYNR.
  11475. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11476. (cond
  11477. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11478. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11479. daynr
  11480. (+ daynr 1000)))
  11481. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11482. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11483. (time-to-days (current-time))) (match-string 0 s)
  11484. prefer show-all))
  11485. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11486. (defun org-days-to-iso-week (days)
  11487. "Return the iso week number."
  11488. (require 'cal-iso)
  11489. (car (calendar-iso-from-absolute days)))
  11490. (defun org-small-year-to-year (year)
  11491. "Convert 2-digit years into 4-digit years.
  11492. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11493. The year 2000 cannot be abbreviated. Any year larger than 99
  11494. is returned unchanged."
  11495. (if (< year 38)
  11496. (setq year (+ 2000 year))
  11497. (if (< year 100)
  11498. (setq year (+ 1900 year))))
  11499. year)
  11500. (defun org-time-from-absolute (d)
  11501. "Return the time corresponding to date D.
  11502. D may be an absolute day number, or a calendar-type list (month day year)."
  11503. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11504. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11505. (defun org-calendar-holiday ()
  11506. "List of holidays, for Diary display in Org-mode."
  11507. (require 'holidays)
  11508. (let ((hl (funcall
  11509. (if (fboundp 'calendar-check-holidays)
  11510. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11511. (if hl (mapconcat 'identity hl "; "))))
  11512. (defun org-diary-sexp-entry (sexp entry date)
  11513. "Process a SEXP diary ENTRY for DATE."
  11514. (require 'diary-lib)
  11515. (let ((result (if calendar-debug-sexp
  11516. (let ((stack-trace-on-error t))
  11517. (eval (car (read-from-string sexp))))
  11518. (condition-case nil
  11519. (eval (car (read-from-string sexp)))
  11520. (error
  11521. (beep)
  11522. (message "Bad sexp at line %d in %s: %s"
  11523. (org-current-line)
  11524. (buffer-file-name) sexp)
  11525. (sleep-for 2))))))
  11526. (cond ((stringp result) result)
  11527. ((and (consp result)
  11528. (stringp (cdr result))) (cdr result))
  11529. (result entry)
  11530. (t nil))))
  11531. (defun org-diary-to-ical-string (frombuf)
  11532. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11533. This uses the icalendar.el library."
  11534. (let* ((tmpdir (if (featurep 'xemacs)
  11535. (temp-directory)
  11536. temporary-file-directory))
  11537. (tmpfile (make-temp-name
  11538. (expand-file-name "orgics" tmpdir)))
  11539. buf rtn b e)
  11540. (save-excursion
  11541. (set-buffer frombuf)
  11542. (icalendar-export-region (point-min) (point-max) tmpfile)
  11543. (setq buf (find-buffer-visiting tmpfile))
  11544. (set-buffer buf)
  11545. (goto-char (point-min))
  11546. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11547. (setq b (match-beginning 0)))
  11548. (goto-char (point-max))
  11549. (if (re-search-backward "^END:VEVENT" nil t)
  11550. (setq e (match-end 0)))
  11551. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11552. (kill-buffer buf)
  11553. (delete-file tmpfile)
  11554. rtn))
  11555. (defun org-closest-date (start current change prefer show-all)
  11556. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11557. When PREFER is `past' return a date that is either CURRENT or past.
  11558. When PREFER is `future', return a date that is either CURRENT or future.
  11559. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11560. ;; Make the proper lists from the dates
  11561. (catch 'exit
  11562. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11563. dn dw sday cday n1 n2 n0
  11564. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11565. (setq start (org-date-to-gregorian start)
  11566. current (org-date-to-gregorian
  11567. (if show-all
  11568. current
  11569. (time-to-days (current-time))))
  11570. sday (calendar-absolute-from-gregorian start)
  11571. cday (calendar-absolute-from-gregorian current))
  11572. (if (<= cday sday) (throw 'exit sday))
  11573. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11574. (setq dn (string-to-number (match-string 1 change))
  11575. dw (cdr (assoc (match-string 2 change) a1)))
  11576. (error "Invalid change specifyer: %s" change))
  11577. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11578. (cond
  11579. ((eq dw 'day)
  11580. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11581. n2 (+ n1 dn)))
  11582. ((eq dw 'year)
  11583. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11584. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11585. (setq date1 (list m d y1)
  11586. n1 (calendar-absolute-from-gregorian date1)
  11587. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11588. n2 (calendar-absolute-from-gregorian date2)))
  11589. ((eq dw 'month)
  11590. ;; approx number of month between the two dates
  11591. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11592. ;; How often does dn fit in there?
  11593. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11594. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11595. m (+ m nm)
  11596. ny (floor (/ m 12))
  11597. y (+ y ny)
  11598. m (- m (* ny 12)))
  11599. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11600. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11601. (setq m2 (+ m dn) y2 y)
  11602. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11603. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11604. (while (<= n2 cday)
  11605. (setq n1 n2 m m2 y y2)
  11606. (setq m2 (+ m dn) y2 y)
  11607. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11608. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11609. ;; Make sure n1 is the earlier date
  11610. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11611. (if show-all
  11612. (cond
  11613. ((eq prefer 'past) n1)
  11614. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11615. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11616. (cond
  11617. ((eq prefer 'past) n1)
  11618. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11619. (t (if (= cday n1) n1 n2)))))))
  11620. (defun org-date-to-gregorian (date)
  11621. "Turn any specification of DATE into a gregorian date for the calendar."
  11622. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11623. ((and (listp date) (= (length date) 3)) date)
  11624. ((stringp date)
  11625. (setq date (org-parse-time-string date))
  11626. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11627. ((listp date)
  11628. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11629. (defun org-parse-time-string (s &optional nodefault)
  11630. "Parse the standard Org-mode time string.
  11631. This should be a lot faster than the normal `parse-time-string'.
  11632. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11633. hour and minute fields will be nil if not given."
  11634. (if (string-match org-ts-regexp0 s)
  11635. (list 0
  11636. (if (or (match-beginning 8) (not nodefault))
  11637. (string-to-number (or (match-string 8 s) "0")))
  11638. (if (or (match-beginning 7) (not nodefault))
  11639. (string-to-number (or (match-string 7 s) "0")))
  11640. (string-to-number (match-string 4 s))
  11641. (string-to-number (match-string 3 s))
  11642. (string-to-number (match-string 2 s))
  11643. nil nil nil)
  11644. (make-list 9 0)))
  11645. (defun org-timestamp-up (&optional arg)
  11646. "Increase the date item at the cursor by one.
  11647. If the cursor is on the year, change the year. If it is on the month or
  11648. the day, change that.
  11649. With prefix ARG, change by that many units."
  11650. (interactive "p")
  11651. (org-timestamp-change (prefix-numeric-value arg)))
  11652. (defun org-timestamp-down (&optional arg)
  11653. "Decrease the date item at the cursor by one.
  11654. If the cursor is on the year, change the year. If it is on the month or
  11655. the day, change that.
  11656. With prefix ARG, change by that many units."
  11657. (interactive "p")
  11658. (org-timestamp-change (- (prefix-numeric-value arg))))
  11659. (defun org-timestamp-up-day (&optional arg)
  11660. "Increase the date in the time stamp by one day.
  11661. With prefix ARG, change that many days."
  11662. (interactive "p")
  11663. (if (and (not (org-at-timestamp-p t))
  11664. (org-on-heading-p))
  11665. (org-todo 'up)
  11666. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11667. (defun org-timestamp-down-day (&optional arg)
  11668. "Decrease the date in the time stamp by one day.
  11669. With prefix ARG, change that many days."
  11670. (interactive "p")
  11671. (if (and (not (org-at-timestamp-p t))
  11672. (org-on-heading-p))
  11673. (org-todo 'down)
  11674. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11675. (defun org-at-timestamp-p (&optional inactive-ok)
  11676. "Determine if the cursor is in or at a timestamp."
  11677. (interactive)
  11678. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11679. (pos (point))
  11680. (ans (or (looking-at tsr)
  11681. (save-excursion
  11682. (skip-chars-backward "^[<\n\r\t")
  11683. (if (> (point) (point-min)) (backward-char 1))
  11684. (and (looking-at tsr)
  11685. (> (- (match-end 0) pos) -1))))))
  11686. (and ans
  11687. (boundp 'org-ts-what)
  11688. (setq org-ts-what
  11689. (cond
  11690. ((= pos (match-beginning 0)) 'bracket)
  11691. ((= pos (1- (match-end 0))) 'bracket)
  11692. ((org-pos-in-match-range pos 2) 'year)
  11693. ((org-pos-in-match-range pos 3) 'month)
  11694. ((org-pos-in-match-range pos 7) 'hour)
  11695. ((org-pos-in-match-range pos 8) 'minute)
  11696. ((or (org-pos-in-match-range pos 4)
  11697. (org-pos-in-match-range pos 5)) 'day)
  11698. ((and (> pos (or (match-end 8) (match-end 5)))
  11699. (< pos (match-end 0)))
  11700. (- pos (or (match-end 8) (match-end 5))))
  11701. (t 'day))))
  11702. ans))
  11703. (defun org-toggle-timestamp-type ()
  11704. "Toggle the type (<active> or [inactive]) of a time stamp."
  11705. (interactive)
  11706. (when (org-at-timestamp-p t)
  11707. (let ((beg (match-beginning 0)) (end (match-end 0))
  11708. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11709. (save-excursion
  11710. (goto-char beg)
  11711. (while (re-search-forward "[][<>]" end t)
  11712. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11713. t t)))
  11714. (message "Timestamp is now %sactive"
  11715. (if (equal (char-after beg) ?<) "" "in")))))
  11716. (defun org-timestamp-change (n &optional what)
  11717. "Change the date in the time stamp at point.
  11718. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11719. `year', `minute', `second'. If WHAT is not given, the cursor position
  11720. in the timestamp determines what will be changed."
  11721. (let ((pos (point))
  11722. with-hm inactive
  11723. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11724. org-ts-what
  11725. extra rem
  11726. ts time time0)
  11727. (if (not (org-at-timestamp-p t))
  11728. (error "Not at a timestamp"))
  11729. (if (and (not what) (eq org-ts-what 'bracket))
  11730. (org-toggle-timestamp-type)
  11731. (if (and (not what) (not (eq org-ts-what 'day))
  11732. org-display-custom-times
  11733. (get-text-property (point) 'display)
  11734. (not (get-text-property (1- (point)) 'display)))
  11735. (setq org-ts-what 'day))
  11736. (setq org-ts-what (or what org-ts-what)
  11737. inactive (= (char-after (match-beginning 0)) ?\[)
  11738. ts (match-string 0))
  11739. (replace-match "")
  11740. (if (string-match
  11741. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11742. ts)
  11743. (setq extra (match-string 1 ts)))
  11744. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11745. (setq with-hm t))
  11746. (setq time0 (org-parse-time-string ts))
  11747. (when (and (eq org-ts-what 'minute)
  11748. (eq current-prefix-arg nil))
  11749. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11750. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11751. (setcar (cdr time0) (+ (nth 1 time0)
  11752. (if (> n 0) (- rem) (- dm rem))))))
  11753. (setq time
  11754. (encode-time (or (car time0) 0)
  11755. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11756. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11757. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11758. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11759. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11760. (nthcdr 6 time0)))
  11761. (when (and (member org-ts-what '(hour minute))
  11762. extra
  11763. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11764. (setq extra (org-modify-ts-extra
  11765. extra
  11766. (if (eq org-ts-what 'hour) 2 5)
  11767. n dm)))
  11768. (when (integerp org-ts-what)
  11769. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11770. (if (eq what 'calendar)
  11771. (let ((cal-date (org-get-date-from-calendar)))
  11772. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11773. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11774. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11775. (setcar time0 (or (car time0) 0))
  11776. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11777. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11778. (setq time (apply 'encode-time time0))))
  11779. (setq org-last-changed-timestamp
  11780. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11781. (org-clock-update-time-maybe)
  11782. (goto-char pos)
  11783. ;; Try to recenter the calendar window, if any
  11784. (if (and org-calendar-follow-timestamp-change
  11785. (get-buffer-window "*Calendar*" t)
  11786. (memq org-ts-what '(day month year)))
  11787. (org-recenter-calendar (time-to-days time))))))
  11788. (defun org-modify-ts-extra (s pos n dm)
  11789. "Change the different parts of the lead-time and repeat fields in timestamp."
  11790. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11791. ng h m new rem)
  11792. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11793. (cond
  11794. ((or (org-pos-in-match-range pos 2)
  11795. (org-pos-in-match-range pos 3))
  11796. (setq m (string-to-number (match-string 3 s))
  11797. h (string-to-number (match-string 2 s)))
  11798. (if (org-pos-in-match-range pos 2)
  11799. (setq h (+ h n))
  11800. (setq n (* dm (org-no-warnings (signum n))))
  11801. (when (not (= 0 (setq rem (% m dm))))
  11802. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11803. (setq m (+ m n)))
  11804. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11805. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11806. (setq h (min 24 (max 0 h)))
  11807. (setq ng 1 new (format "-%02d:%02d" h m)))
  11808. ((org-pos-in-match-range pos 6)
  11809. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11810. ((org-pos-in-match-range pos 5)
  11811. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11812. ((org-pos-in-match-range pos 9)
  11813. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11814. ((org-pos-in-match-range pos 8)
  11815. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11816. (when ng
  11817. (setq s (concat
  11818. (substring s 0 (match-beginning ng))
  11819. new
  11820. (substring s (match-end ng))))))
  11821. s))
  11822. (defun org-recenter-calendar (date)
  11823. "If the calendar is visible, recenter it to DATE."
  11824. (let* ((win (selected-window))
  11825. (cwin (get-buffer-window "*Calendar*" t))
  11826. (calendar-move-hook nil))
  11827. (when cwin
  11828. (select-window cwin)
  11829. (calendar-goto-date (if (listp date) date
  11830. (calendar-gregorian-from-absolute date)))
  11831. (select-window win))))
  11832. (defun org-goto-calendar (&optional arg)
  11833. "Go to the Emacs calendar at the current date.
  11834. If there is a time stamp in the current line, go to that date.
  11835. A prefix ARG can be used to force the current date."
  11836. (interactive "P")
  11837. (let ((tsr org-ts-regexp) diff
  11838. (calendar-move-hook nil)
  11839. (calendar-view-holidays-initially-flag nil)
  11840. (view-calendar-holidays-initially nil)
  11841. (calendar-view-diary-initially-flag nil)
  11842. (view-diary-entries-initially nil))
  11843. (if (or (org-at-timestamp-p)
  11844. (save-excursion
  11845. (beginning-of-line 1)
  11846. (looking-at (concat ".*" tsr))))
  11847. (let ((d1 (time-to-days (current-time)))
  11848. (d2 (time-to-days
  11849. (org-time-string-to-time (match-string 1)))))
  11850. (setq diff (- d2 d1))))
  11851. (calendar)
  11852. (calendar-goto-today)
  11853. (if (and diff (not arg)) (calendar-forward-day diff))))
  11854. (defun org-get-date-from-calendar ()
  11855. "Return a list (month day year) of date at point in calendar."
  11856. (with-current-buffer "*Calendar*"
  11857. (save-match-data
  11858. (calendar-cursor-to-date))))
  11859. (defun org-date-from-calendar ()
  11860. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11861. If there is already a time stamp at the cursor position, update it."
  11862. (interactive)
  11863. (if (org-at-timestamp-p t)
  11864. (org-timestamp-change 0 'calendar)
  11865. (let ((cal-date (org-get-date-from-calendar)))
  11866. (org-insert-time-stamp
  11867. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11868. (defun org-minutes-to-hh:mm-string (m)
  11869. "Compute H:MM from a number of minutes."
  11870. (let ((h (/ m 60)))
  11871. (setq m (- m (* 60 h)))
  11872. (format org-time-clocksum-format h m)))
  11873. (defun org-hh:mm-string-to-minutes (s)
  11874. "Convert a string H:MM to a number of minutes."
  11875. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11876. (+ (* (string-to-number (match-string 1 s)) 60)
  11877. (string-to-number (match-string 2 s)))
  11878. 0))
  11879. ;;;; Files
  11880. (defun org-save-all-org-buffers ()
  11881. "Save all Org-mode buffers without user confirmation."
  11882. (interactive)
  11883. (message "Saving all Org-mode buffers...")
  11884. (save-some-buffers t 'org-mode-p)
  11885. (when (featurep 'org-id) (org-id-locations-save))
  11886. (message "Saving all Org-mode buffers... done"))
  11887. (defun org-revert-all-org-buffers ()
  11888. "Revert all Org-mode buffers.
  11889. Prompt for confirmation when there are unsaved changes.
  11890. Be sure you know what you are doing before letting this function
  11891. overwrite your changes.
  11892. This function is useful in a setup where one tracks org files
  11893. with a version control system, to revert on one machine after pulling
  11894. changes from another. I believe the procedure must be like this:
  11895. 1. M-x org-save-all-org-buffers
  11896. 2. Pull changes from the other machine, resolve conflicts
  11897. 3. M-x org-revert-all-org-buffers"
  11898. (interactive)
  11899. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11900. (error "Abort"))
  11901. (save-excursion
  11902. (save-window-excursion
  11903. (mapc
  11904. (lambda (b)
  11905. (when (and (with-current-buffer b (org-mode-p))
  11906. (with-current-buffer b buffer-file-name))
  11907. (switch-to-buffer b)
  11908. (revert-buffer t 'no-confirm)))
  11909. (buffer-list))
  11910. (when (and (featurep 'org-id) org-id-track-globally)
  11911. (org-id-locations-load)))))
  11912. ;;;; Agenda files
  11913. ;;;###autoload
  11914. (defun org-iswitchb (&optional arg)
  11915. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11916. With a prefix argument, restrict available to files.
  11917. With two prefix arguments, restrict available buffers to agenda files.
  11918. Due to some yet unresolved reason, the global function
  11919. `iswitchb-mode' needs to be active for this function to work."
  11920. (interactive "P")
  11921. (require 'iswitchb)
  11922. (let ((enabled iswitchb-mode) blist)
  11923. (or enabled (iswitchb-mode 1))
  11924. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11925. ((equal arg '(16)) (org-buffer-list 'agenda))
  11926. (t (org-buffer-list))))
  11927. (unwind-protect
  11928. (let ((iswitchb-make-buflist-hook
  11929. (lambda ()
  11930. (setq iswitchb-temp-buflist
  11931. (mapcar 'buffer-name blist)))))
  11932. (switch-to-buffer
  11933. (iswitchb-read-buffer
  11934. "Switch-to: " nil t))
  11935. (or enabled (iswitchb-mode -1))))))
  11936. ;;;###autoload
  11937. (defun org-ido-switchb (&optional arg)
  11938. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11939. With a prefix argument, restrict available to files.
  11940. With two prefix arguments, restrict available buffers to agenda files."
  11941. (interactive "P")
  11942. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11943. ((equal arg '(16)) (org-buffer-list 'agenda))
  11944. (t (org-buffer-list)))))
  11945. (switch-to-buffer
  11946. (org-ido-completing-read "Org buffer: "
  11947. (mapcar 'list (mapcar 'buffer-name blist))
  11948. nil t))))
  11949. (defun org-buffer-list (&optional predicate exclude-tmp)
  11950. "Return a list of Org buffers.
  11951. PREDICATE can be `export', `files' or `agenda'.
  11952. export restrict the list to Export buffers.
  11953. files restrict the list to buffers visiting Org files.
  11954. agenda restrict the list to buffers visiting agenda files.
  11955. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11956. (let* ((bfn nil)
  11957. (agenda-files (and (eq predicate 'agenda)
  11958. (mapcar 'file-truename (org-agenda-files t))))
  11959. (filter
  11960. (cond
  11961. ((eq predicate 'files)
  11962. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11963. ((eq predicate 'export)
  11964. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11965. ((eq predicate 'agenda)
  11966. (lambda (b)
  11967. (with-current-buffer b
  11968. (and (eq major-mode 'org-mode)
  11969. (setq bfn (buffer-file-name b))
  11970. (member (file-truename bfn) agenda-files)))))
  11971. (t (lambda (b) (with-current-buffer b
  11972. (or (eq major-mode 'org-mode)
  11973. (string-match "\*Org .*Export"
  11974. (buffer-name b)))))))))
  11975. (delq nil
  11976. (mapcar
  11977. (lambda(b)
  11978. (if (and (funcall filter b)
  11979. (or (not exclude-tmp)
  11980. (not (string-match "tmp" (buffer-name b)))))
  11981. b
  11982. nil))
  11983. (buffer-list)))))
  11984. (defun org-agenda-files (&optional unrestricted archives)
  11985. "Get the list of agenda files.
  11986. Optional UNRESTRICTED means return the full list even if a restriction
  11987. is currently in place.
  11988. When ARCHIVES is t, include all archive files hat are really being
  11989. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11990. `org-agenda-archives-mode' is t."
  11991. (let ((files
  11992. (cond
  11993. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11994. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11995. ((listp org-agenda-files) org-agenda-files)
  11996. (t (error "Invalid value of `org-agenda-files'")))))
  11997. (setq files (apply 'append
  11998. (mapcar (lambda (f)
  11999. (if (file-directory-p f)
  12000. (directory-files
  12001. f t org-agenda-file-regexp)
  12002. (list f)))
  12003. files)))
  12004. (when org-agenda-skip-unavailable-files
  12005. (setq files (delq nil
  12006. (mapcar (function
  12007. (lambda (file)
  12008. (and (file-readable-p file) file)))
  12009. files))))
  12010. (when (or (eq archives t)
  12011. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12012. (setq files (org-add-archive-files files)))
  12013. files))
  12014. (defun org-edit-agenda-file-list ()
  12015. "Edit the list of agenda files.
  12016. Depending on setup, this either uses customize to edit the variable
  12017. `org-agenda-files', or it visits the file that is holding the list. In the
  12018. latter case, the buffer is set up in a way that saving it automatically kills
  12019. the buffer and restores the previous window configuration."
  12020. (interactive)
  12021. (if (stringp org-agenda-files)
  12022. (let ((cw (current-window-configuration)))
  12023. (find-file org-agenda-files)
  12024. (org-set-local 'org-window-configuration cw)
  12025. (org-add-hook 'after-save-hook
  12026. (lambda ()
  12027. (set-window-configuration
  12028. (prog1 org-window-configuration
  12029. (kill-buffer (current-buffer))))
  12030. (org-install-agenda-files-menu)
  12031. (message "New agenda file list installed"))
  12032. nil 'local)
  12033. (message "%s" (substitute-command-keys
  12034. "Edit list and finish with \\[save-buffer]")))
  12035. (customize-variable 'org-agenda-files)))
  12036. (defun org-store-new-agenda-file-list (list)
  12037. "Set new value for the agenda file list and save it correctly."
  12038. (if (stringp org-agenda-files)
  12039. (let ((f org-agenda-files) b)
  12040. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12041. (with-temp-file f
  12042. (insert (mapconcat 'identity list "\n") "\n")))
  12043. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12044. (setq org-agenda-files list)
  12045. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12046. (defun org-read-agenda-file-list ()
  12047. "Read the list of agenda files from a file."
  12048. (when (file-directory-p org-agenda-files)
  12049. (error "`org-agenda-files' cannot be a single directory"))
  12050. (when (stringp org-agenda-files)
  12051. (with-temp-buffer
  12052. (insert-file-contents org-agenda-files)
  12053. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12054. ;;;###autoload
  12055. (defun org-cycle-agenda-files ()
  12056. "Cycle through the files in `org-agenda-files'.
  12057. If the current buffer visits an agenda file, find the next one in the list.
  12058. If the current buffer does not, find the first agenda file."
  12059. (interactive)
  12060. (let* ((fs (org-agenda-files t))
  12061. (files (append fs (list (car fs))))
  12062. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12063. file)
  12064. (unless files (error "No agenda files"))
  12065. (catch 'exit
  12066. (while (setq file (pop files))
  12067. (if (equal (file-truename file) tcf)
  12068. (when (car files)
  12069. (find-file (car files))
  12070. (throw 'exit t))))
  12071. (find-file (car fs)))
  12072. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12073. (defun org-agenda-file-to-front (&optional to-end)
  12074. "Move/add the current file to the top of the agenda file list.
  12075. If the file is not present in the list, it is added to the front. If it is
  12076. present, it is moved there. With optional argument TO-END, add/move to the
  12077. end of the list."
  12078. (interactive "P")
  12079. (let ((org-agenda-skip-unavailable-files nil)
  12080. (file-alist (mapcar (lambda (x)
  12081. (cons (file-truename x) x))
  12082. (org-agenda-files t)))
  12083. (ctf (file-truename buffer-file-name))
  12084. x had)
  12085. (setq x (assoc ctf file-alist) had x)
  12086. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12087. (if to-end
  12088. (setq file-alist (append (delq x file-alist) (list x)))
  12089. (setq file-alist (cons x (delq x file-alist))))
  12090. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12091. (org-install-agenda-files-menu)
  12092. (message "File %s to %s of agenda file list"
  12093. (if had "moved" "added") (if to-end "end" "front"))))
  12094. (defun org-remove-file (&optional file)
  12095. "Remove current file from the list of files in variable `org-agenda-files'.
  12096. These are the files which are being checked for agenda entries.
  12097. Optional argument FILE means, use this file instead of the current."
  12098. (interactive)
  12099. (let* ((org-agenda-skip-unavailable-files nil)
  12100. (file (or file buffer-file-name))
  12101. (true-file (file-truename file))
  12102. (afile (abbreviate-file-name file))
  12103. (files (delq nil (mapcar
  12104. (lambda (x)
  12105. (if (equal true-file
  12106. (file-truename x))
  12107. nil x))
  12108. (org-agenda-files t)))))
  12109. (if (not (= (length files) (length (org-agenda-files t))))
  12110. (progn
  12111. (org-store-new-agenda-file-list files)
  12112. (org-install-agenda-files-menu)
  12113. (message "Removed file: %s" afile))
  12114. (message "File was not in list: %s (not removed)" afile))))
  12115. (defun org-file-menu-entry (file)
  12116. (vector file (list 'find-file file) t))
  12117. (defun org-check-agenda-file (file)
  12118. "Make sure FILE exists. If not, ask user what to do."
  12119. (when (not (file-exists-p file))
  12120. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12121. (abbreviate-file-name file))
  12122. (let ((r (downcase (read-char-exclusive))))
  12123. (cond
  12124. ((equal r ?r)
  12125. (org-remove-file file)
  12126. (throw 'nextfile t))
  12127. (t (error "Abort"))))))
  12128. (defun org-get-agenda-file-buffer (file)
  12129. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12130. it to the list of buffers which might be released later."
  12131. (let ((buf (org-find-base-buffer-visiting file)))
  12132. (if buf
  12133. buf ; just return it
  12134. ;; Make a new buffer and remember it
  12135. (setq buf (find-file-noselect file))
  12136. (if buf (push buf org-agenda-new-buffers))
  12137. buf)))
  12138. (defun org-release-buffers (blist)
  12139. "Release all buffers in list, asking the user for confirmation when needed.
  12140. When a buffer is unmodified, it is just killed. When modified, it is saved
  12141. \(if the user agrees) and then killed."
  12142. (let (buf file)
  12143. (while (setq buf (pop blist))
  12144. (setq file (buffer-file-name buf))
  12145. (when (and (buffer-modified-p buf)
  12146. file
  12147. (y-or-n-p (format "Save file %s? " file)))
  12148. (with-current-buffer buf (save-buffer)))
  12149. (kill-buffer buf))))
  12150. (defun org-prepare-agenda-buffers (files)
  12151. "Create buffers for all agenda files, protect archived trees and comments."
  12152. (interactive)
  12153. (let ((pa '(:org-archived t))
  12154. (pc '(:org-comment t))
  12155. (pall '(:org-archived t :org-comment t))
  12156. (inhibit-read-only t)
  12157. (rea (concat ":" org-archive-tag ":"))
  12158. bmp file re)
  12159. (save-excursion
  12160. (save-restriction
  12161. (while (setq file (pop files))
  12162. (if (bufferp file)
  12163. (set-buffer file)
  12164. (org-check-agenda-file file)
  12165. (set-buffer (org-get-agenda-file-buffer file)))
  12166. (widen)
  12167. (setq bmp (buffer-modified-p))
  12168. (org-refresh-category-properties)
  12169. (setq org-todo-keywords-for-agenda
  12170. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12171. (setq org-done-keywords-for-agenda
  12172. (append org-done-keywords-for-agenda org-done-keywords))
  12173. (setq org-todo-keyword-alist-for-agenda
  12174. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12175. (setq org-tag-alist-for-agenda
  12176. (append org-tag-alist-for-agenda org-tag-alist))
  12177. (save-excursion
  12178. (remove-text-properties (point-min) (point-max) pall)
  12179. (when org-agenda-skip-archived-trees
  12180. (goto-char (point-min))
  12181. (while (re-search-forward rea nil t)
  12182. (if (org-on-heading-p t)
  12183. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12184. (goto-char (point-min))
  12185. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12186. (while (re-search-forward re nil t)
  12187. (add-text-properties
  12188. (match-beginning 0) (org-end-of-subtree t) pc)))
  12189. (set-buffer-modified-p bmp))))
  12190. (setq org-todo-keyword-alist-for-agenda
  12191. (org-uniquify org-todo-keyword-alist-for-agenda)
  12192. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12193. ;;;; Embedded LaTeX
  12194. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12195. "Keymap for the minor `org-cdlatex-mode'.")
  12196. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12197. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12198. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12199. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12200. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12201. (defvar org-cdlatex-texmathp-advice-is-done nil
  12202. "Flag remembering if we have applied the advice to texmathp already.")
  12203. (define-minor-mode org-cdlatex-mode
  12204. "Toggle the minor `org-cdlatex-mode'.
  12205. This mode supports entering LaTeX environment and math in LaTeX fragments
  12206. in Org-mode.
  12207. \\{org-cdlatex-mode-map}"
  12208. nil " OCDL" nil
  12209. (when org-cdlatex-mode (require 'cdlatex))
  12210. (unless org-cdlatex-texmathp-advice-is-done
  12211. (setq org-cdlatex-texmathp-advice-is-done t)
  12212. (defadvice texmathp (around org-math-always-on activate)
  12213. "Always return t in org-mode buffers.
  12214. This is because we want to insert math symbols without dollars even outside
  12215. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12216. an embedded LaTeX fragment, let texmathp do its job.
  12217. \\[org-cdlatex-mode-map]"
  12218. (interactive)
  12219. (let (p)
  12220. (cond
  12221. ((not (org-mode-p)) ad-do-it)
  12222. ((eq this-command 'cdlatex-math-symbol)
  12223. (setq ad-return-value t
  12224. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12225. (t
  12226. (let ((p (org-inside-LaTeX-fragment-p)))
  12227. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12228. (setq ad-return-value t
  12229. texmathp-why '("Org-mode embedded math" . 0))
  12230. (if p ad-do-it)))))))))
  12231. (defun turn-on-org-cdlatex ()
  12232. "Unconditionally turn on `org-cdlatex-mode'."
  12233. (org-cdlatex-mode 1))
  12234. (defun org-inside-LaTeX-fragment-p ()
  12235. "Test if point is inside a LaTeX fragment.
  12236. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12237. sequence appearing also before point.
  12238. Even though the matchers for math are configurable, this function assumes
  12239. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12240. delimiters are skipped when they have been removed by customization.
  12241. The return value is nil, or a cons cell with the delimiter and
  12242. and the position of this delimiter.
  12243. This function does a reasonably good job, but can locally be fooled by
  12244. for example currency specifications. For example it will assume being in
  12245. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12246. fragments that are properly closed, but during editing, we have to live
  12247. with the uncertainty caused by missing closing delimiters. This function
  12248. looks only before point, not after."
  12249. (catch 'exit
  12250. (let ((pos (point))
  12251. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12252. (lim (progn
  12253. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12254. (point)))
  12255. dd-on str (start 0) m re)
  12256. (goto-char pos)
  12257. (when dodollar
  12258. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12259. re (nth 1 (assoc "$" org-latex-regexps)))
  12260. (while (string-match re str start)
  12261. (cond
  12262. ((= (match-end 0) (length str))
  12263. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12264. ((= (match-end 0) (- (length str) 5))
  12265. (throw 'exit nil))
  12266. (t (setq start (match-end 0))))))
  12267. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12268. (goto-char pos)
  12269. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12270. (and (match-beginning 2) (throw 'exit nil))
  12271. ;; count $$
  12272. (while (re-search-backward "\\$\\$" lim t)
  12273. (setq dd-on (not dd-on)))
  12274. (goto-char pos)
  12275. (if dd-on (cons "$$" m))))))
  12276. (defun org-try-cdlatex-tab ()
  12277. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12278. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12279. - inside a LaTeX fragment, or
  12280. - after the first word in a line, where an abbreviation expansion could
  12281. insert a LaTeX environment."
  12282. (when org-cdlatex-mode
  12283. (cond
  12284. ((save-excursion
  12285. (skip-chars-backward "a-zA-Z0-9*")
  12286. (skip-chars-backward " \t")
  12287. (bolp))
  12288. (cdlatex-tab) t)
  12289. ((org-inside-LaTeX-fragment-p)
  12290. (cdlatex-tab) t)
  12291. (t nil))))
  12292. (defun org-cdlatex-underscore-caret (&optional arg)
  12293. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12294. Revert to the normal definition outside of these fragments."
  12295. (interactive "P")
  12296. (if (org-inside-LaTeX-fragment-p)
  12297. (call-interactively 'cdlatex-sub-superscript)
  12298. (let (org-cdlatex-mode)
  12299. (call-interactively (key-binding (vector last-input-event))))))
  12300. (defun org-cdlatex-math-modify (&optional arg)
  12301. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12302. Revert to the normal definition outside of these fragments."
  12303. (interactive "P")
  12304. (if (org-inside-LaTeX-fragment-p)
  12305. (call-interactively 'cdlatex-math-modify)
  12306. (let (org-cdlatex-mode)
  12307. (call-interactively (key-binding (vector last-input-event))))))
  12308. (defvar org-latex-fragment-image-overlays nil
  12309. "List of overlays carrying the images of latex fragments.")
  12310. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12311. (defun org-remove-latex-fragment-image-overlays ()
  12312. "Remove all overlays with LaTeX fragment images in current buffer."
  12313. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12314. (setq org-latex-fragment-image-overlays nil))
  12315. (defun org-preview-latex-fragment (&optional subtree)
  12316. "Preview the LaTeX fragment at point, or all locally or globally.
  12317. If the cursor is in a LaTeX fragment, create the image and overlay
  12318. it over the source code. If there is no fragment at point, display
  12319. all fragments in the current text, from one headline to the next. With
  12320. prefix SUBTREE, display all fragments in the current subtree. With a
  12321. double prefix `C-u C-u', or when the cursor is before the first headline,
  12322. display all fragments in the buffer.
  12323. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12324. (interactive "P")
  12325. (org-remove-latex-fragment-image-overlays)
  12326. (save-excursion
  12327. (save-restriction
  12328. (let (beg end at msg)
  12329. (cond
  12330. ((or (equal subtree '(16))
  12331. (not (save-excursion
  12332. (re-search-backward (concat "^" outline-regexp) nil t))))
  12333. (setq beg (point-min) end (point-max)
  12334. msg "Creating images for buffer...%s"))
  12335. ((equal subtree '(4))
  12336. (org-back-to-heading)
  12337. (setq beg (point) end (org-end-of-subtree t)
  12338. msg "Creating images for subtree...%s"))
  12339. (t
  12340. (if (setq at (org-inside-LaTeX-fragment-p))
  12341. (goto-char (max (point-min) (- (cdr at) 2)))
  12342. (org-back-to-heading))
  12343. (setq beg (point) end (progn (outline-next-heading) (point))
  12344. msg (if at "Creating image...%s"
  12345. "Creating images for entry...%s"))))
  12346. (message msg "")
  12347. (narrow-to-region beg end)
  12348. (goto-char beg)
  12349. (org-format-latex
  12350. (concat "ltxpng/" (file-name-sans-extension
  12351. (file-name-nondirectory
  12352. buffer-file-name)))
  12353. default-directory 'overlays msg at 'forbuffer)
  12354. (message msg "done. Use `C-c C-c' to remove images.")))))
  12355. (defvar org-latex-regexps
  12356. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12357. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12358. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12359. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12360. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12361. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12362. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12363. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12364. "Regular expressions for matching embedded LaTeX.")
  12365. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12366. "Replace LaTeX fragments with links to an image, and produce images."
  12367. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12368. (let* ((prefixnodir (file-name-nondirectory prefix))
  12369. (absprefix (expand-file-name prefix dir))
  12370. (todir (file-name-directory absprefix))
  12371. (opt org-format-latex-options)
  12372. (matchers (plist-get opt :matchers))
  12373. (re-list org-latex-regexps)
  12374. (cnt 0) txt link beg end re e checkdir
  12375. m n block linkfile movefile ov)
  12376. ;; Check if there are old images files with this prefix, and remove them
  12377. (when (file-directory-p todir)
  12378. (mapc 'delete-file
  12379. (directory-files
  12380. todir 'full
  12381. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12382. ;; Check the different regular expressions
  12383. (while (setq e (pop re-list))
  12384. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12385. block (if (nth 3 e) "\n\n" ""))
  12386. (when (member m matchers)
  12387. (goto-char (point-min))
  12388. (while (re-search-forward re nil t)
  12389. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12390. (not (get-text-property (match-beginning n)
  12391. 'org-protected)))
  12392. (setq txt (match-string n)
  12393. beg (match-beginning n) end (match-end n)
  12394. cnt (1+ cnt)
  12395. linkfile (format "%s_%04d.png" prefix cnt)
  12396. movefile (format "%s_%04d.png" absprefix cnt)
  12397. link (concat block "[[file:" linkfile "]]" block))
  12398. (if msg (message msg cnt))
  12399. (goto-char beg)
  12400. (unless checkdir ; make sure the directory exists
  12401. (setq checkdir t)
  12402. (or (file-directory-p todir) (make-directory todir)))
  12403. (org-create-formula-image
  12404. txt movefile opt forbuffer)
  12405. (if overlays
  12406. (progn
  12407. (setq ov (org-make-overlay beg end))
  12408. (if (featurep 'xemacs)
  12409. (progn
  12410. (org-overlay-put ov 'invisible t)
  12411. (org-overlay-put
  12412. ov 'end-glyph
  12413. (make-glyph (vector 'png :file movefile))))
  12414. (org-overlay-put
  12415. ov 'display
  12416. (list 'image :type 'png :file movefile :ascent 'center)))
  12417. (push ov org-latex-fragment-image-overlays)
  12418. (goto-char end))
  12419. (delete-region beg end)
  12420. (insert link))))))))
  12421. ;; This function borrows from Ganesh Swami's latex2png.el
  12422. (defun org-create-formula-image (string tofile options buffer)
  12423. (let* ((tmpdir (if (featurep 'xemacs)
  12424. (temp-directory)
  12425. temporary-file-directory))
  12426. (texfilebase (make-temp-name
  12427. (expand-file-name "orgtex" tmpdir)))
  12428. (texfile (concat texfilebase ".tex"))
  12429. (dvifile (concat texfilebase ".dvi"))
  12430. (pngfile (concat texfilebase ".png"))
  12431. (fnh (if (featurep 'xemacs)
  12432. (font-height (get-face-font 'default))
  12433. (face-attribute 'default :height nil)))
  12434. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12435. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12436. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12437. "Black"))
  12438. (bg (or (plist-get options (if buffer :background :html-background))
  12439. "Transparent")))
  12440. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12441. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12442. (with-temp-file texfile
  12443. (insert org-format-latex-header
  12444. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12445. (let ((dir default-directory))
  12446. (condition-case nil
  12447. (progn
  12448. (cd tmpdir)
  12449. (call-process "latex" nil nil nil texfile))
  12450. (error nil))
  12451. (cd dir))
  12452. (if (not (file-exists-p dvifile))
  12453. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12454. (condition-case nil
  12455. (call-process "dvipng" nil nil nil
  12456. "-E" "-fg" fg "-bg" bg
  12457. "-D" dpi
  12458. ;;"-x" scale "-y" scale
  12459. "-T" "tight"
  12460. "-o" pngfile
  12461. dvifile)
  12462. (error nil))
  12463. (if (not (file-exists-p pngfile))
  12464. (progn (message "Failed to create png file from %s" texfile) nil)
  12465. ;; Use the requested file name and clean up
  12466. (copy-file pngfile tofile 'replace)
  12467. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12468. (delete-file (concat texfilebase e)))
  12469. pngfile))))
  12470. (defun org-dvipng-color (attr)
  12471. "Return an rgb color specification for dvipng."
  12472. (apply 'format "rgb %s %s %s"
  12473. (mapcar 'org-normalize-color
  12474. (color-values (face-attribute 'default attr nil)))))
  12475. (defun org-normalize-color (value)
  12476. "Return string to be used as color value for an RGB component."
  12477. (format "%g" (/ value 65535.0)))
  12478. ;;;; Key bindings
  12479. ;; Make `C-c C-x' a prefix key
  12480. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12481. ;; TAB key with modifiers
  12482. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12483. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12484. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12485. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12486. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12487. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12488. ;; The following line is necessary under Suse GNU/Linux
  12489. (unless (featurep 'xemacs)
  12490. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12491. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12492. (define-key org-mode-map [backtab] 'org-shifttab)
  12493. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12494. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12495. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12496. ;; Cursor keys with modifiers
  12497. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12498. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12499. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12500. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12501. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12502. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12503. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12504. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12505. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12506. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12507. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12508. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12509. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12510. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12511. ;;; Extra keys for tty access.
  12512. ;; We only set them when really needed because otherwise the
  12513. ;; menus don't show the simple keys
  12514. (when (or org-use-extra-keys
  12515. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12516. (not window-system))
  12517. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12518. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12519. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12520. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12521. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12522. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12523. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12524. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12525. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12526. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12527. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12528. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12529. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12530. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12531. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12532. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12533. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12534. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12535. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12536. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12537. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12538. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12539. ;; All the other keys
  12540. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12541. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12542. (if (boundp 'narrow-map)
  12543. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12544. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12545. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12546. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12547. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12548. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12549. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12550. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12551. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12552. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12553. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12554. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12555. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12556. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12557. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12558. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12559. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12560. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12561. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12562. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12563. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12564. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12565. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12566. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12567. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12568. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12569. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12570. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12571. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12572. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12573. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12574. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12575. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12576. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12577. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12578. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12579. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12580. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12581. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12582. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12583. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12584. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12585. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12586. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12587. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12588. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12589. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12590. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12591. (org-defkey org-mode-map "\C-m" 'org-return)
  12592. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12593. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12594. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12595. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12596. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12597. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12598. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12599. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12600. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12601. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12602. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12603. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12604. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12605. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12606. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12607. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12608. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12609. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12610. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12611. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12612. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12613. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12614. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12615. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12616. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12617. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12618. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12619. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12620. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12621. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12622. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12623. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12624. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12625. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12626. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12627. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12628. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12629. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12630. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12631. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12632. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12633. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12634. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12635. (when (featurep 'xemacs)
  12636. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12637. (defvar org-self-insert-command-undo-counter 0)
  12638. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12639. (defun org-self-insert-command (N)
  12640. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12641. If the cursor is in a table looking at whitespace, the whitespace is
  12642. overwritten, and the table is not marked as requiring realignment."
  12643. (interactive "p")
  12644. (if (and
  12645. (org-table-p)
  12646. (progn
  12647. ;; check if we blank the field, and if that triggers align
  12648. (and (featurep 'org-table) org-table-auto-blank-field
  12649. (member last-command
  12650. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12651. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12652. ;; got extra space, this field does not determine column width
  12653. (let (org-table-may-need-update) (org-table-blank-field))
  12654. ;; no extra space, this field may determine column width
  12655. (org-table-blank-field)))
  12656. t)
  12657. (eq N 1)
  12658. (looking-at "[^|\n]* |"))
  12659. (let (org-table-may-need-update)
  12660. (goto-char (1- (match-end 0)))
  12661. (delete-backward-char 1)
  12662. (goto-char (match-beginning 0))
  12663. (self-insert-command N))
  12664. (setq org-table-may-need-update t)
  12665. (self-insert-command N)
  12666. (org-fix-tags-on-the-fly)
  12667. (if org-self-insert-cluster-for-undo
  12668. (if (not (eq last-command 'org-self-insert-command))
  12669. (setq org-self-insert-command-undo-counter 1)
  12670. (if (>= org-self-insert-command-undo-counter 20)
  12671. (setq org-self-insert-command-undo-counter 1)
  12672. (and (> org-self-insert-command-undo-counter 0)
  12673. buffer-undo-list
  12674. (not (cadr buffer-undo-list)) ; remove nil entry
  12675. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12676. (setq org-self-insert-command-undo-counter
  12677. (1+ org-self-insert-command-undo-counter)))))))
  12678. (defun org-fix-tags-on-the-fly ()
  12679. (when (and (equal (char-after (point-at-bol)) ?*)
  12680. (org-on-heading-p))
  12681. (org-align-tags-here org-tags-column)))
  12682. (defun org-delete-backward-char (N)
  12683. "Like `delete-backward-char', insert whitespace at field end in tables.
  12684. When deleting backwards, in tables this function will insert whitespace in
  12685. front of the next \"|\" separator, to keep the table aligned. The table will
  12686. still be marked for re-alignment if the field did fill the entire column,
  12687. because, in this case the deletion might narrow the column."
  12688. (interactive "p")
  12689. (if (and (org-table-p)
  12690. (eq N 1)
  12691. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12692. (looking-at ".*?|"))
  12693. (let ((pos (point))
  12694. (noalign (looking-at "[^|\n\r]* |"))
  12695. (c org-table-may-need-update))
  12696. (backward-delete-char N)
  12697. (skip-chars-forward "^|")
  12698. (insert " ")
  12699. (goto-char (1- pos))
  12700. ;; noalign: if there were two spaces at the end, this field
  12701. ;; does not determine the width of the column.
  12702. (if noalign (setq org-table-may-need-update c)))
  12703. (backward-delete-char N)
  12704. (org-fix-tags-on-the-fly)))
  12705. (defun org-delete-char (N)
  12706. "Like `delete-char', but insert whitespace at field end in tables.
  12707. When deleting characters, in tables this function will insert whitespace in
  12708. front of the next \"|\" separator, to keep the table aligned. The table will
  12709. still be marked for re-alignment if the field did fill the entire column,
  12710. because, in this case the deletion might narrow the column."
  12711. (interactive "p")
  12712. (if (and (org-table-p)
  12713. (not (bolp))
  12714. (not (= (char-after) ?|))
  12715. (eq N 1))
  12716. (if (looking-at ".*?|")
  12717. (let ((pos (point))
  12718. (noalign (looking-at "[^|\n\r]* |"))
  12719. (c org-table-may-need-update))
  12720. (replace-match (concat
  12721. (substring (match-string 0) 1 -1)
  12722. " |"))
  12723. (goto-char pos)
  12724. ;; noalign: if there were two spaces at the end, this field
  12725. ;; does not determine the width of the column.
  12726. (if noalign (setq org-table-may-need-update c)))
  12727. (delete-char N))
  12728. (delete-char N)
  12729. (org-fix-tags-on-the-fly)))
  12730. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12731. (put 'org-self-insert-command 'delete-selection t)
  12732. (put 'orgtbl-self-insert-command 'delete-selection t)
  12733. (put 'org-delete-char 'delete-selection 'supersede)
  12734. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12735. (put 'org-yank 'delete-selection 'yank)
  12736. ;; Make `flyspell-mode' delay after some commands
  12737. (put 'org-self-insert-command 'flyspell-delayed t)
  12738. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12739. (put 'org-delete-char 'flyspell-delayed t)
  12740. (put 'org-delete-backward-char 'flyspell-delayed t)
  12741. ;; Make pabbrev-mode expand after org-mode commands
  12742. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12743. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12744. ;; How to do this: Measure non-white length of current string
  12745. ;; If equal to column width, we should realign.
  12746. (defun org-remap (map &rest commands)
  12747. "In MAP, remap the functions given in COMMANDS.
  12748. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12749. (let (new old)
  12750. (while commands
  12751. (setq old (pop commands) new (pop commands))
  12752. (if (fboundp 'command-remapping)
  12753. (org-defkey map (vector 'remap old) new)
  12754. (substitute-key-definition old new map global-map)))))
  12755. (when (eq org-enable-table-editor 'optimized)
  12756. ;; If the user wants maximum table support, we need to hijack
  12757. ;; some standard editing functions
  12758. (org-remap org-mode-map
  12759. 'self-insert-command 'org-self-insert-command
  12760. 'delete-char 'org-delete-char
  12761. 'delete-backward-char 'org-delete-backward-char)
  12762. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12763. (defvar org-ctrl-c-ctrl-c-hook nil
  12764. "Hook for functions attaching themselves to `C-c C-c'.
  12765. This can be used to add additional functionality to the C-c C-c key which
  12766. executes context-dependent commands.
  12767. Each function will be called with no arguments. The function must check
  12768. if the context is appropriate for it to act. If yes, it should do its
  12769. thing and then return a non-nil value. If the context is wrong,
  12770. just do nothing and return nil.")
  12771. (defvar org-tab-first-hook nil
  12772. "Hook for functions to attach themselves to TAB.
  12773. See `org-ctrl-c-ctrl-c-hook' for more information.
  12774. This hook runs as the first action when TAB is pressed, even before
  12775. `org-cycle' messes around with the `outline-regexp' to cater for
  12776. inline tasks and plain list item folding.
  12777. If any function in this hook returns t, not other actions like table
  12778. field motion visibility cycling will be done.")
  12779. (defvar org-tab-after-check-for-table-hook nil
  12780. "Hook for functions to attach themselves to TAB.
  12781. See `org-ctrl-c-ctrl-c-hook' for more information.
  12782. This hook runs after it has been established that the cursor is not in a
  12783. table, but before checking if the cursor is in a headline or if global cycling
  12784. should be done.
  12785. If any function in this hook returns t, not other actions like visibility
  12786. cycling will be done.")
  12787. (defvar org-tab-after-check-for-cycling-hook nil
  12788. "Hook for functions to attach themselves to TAB.
  12789. See `org-ctrl-c-ctrl-c-hook' for more information.
  12790. This hook runs after it has been established that not table field motion and
  12791. not visibility should be done because of current context. This is probably
  12792. the place where a package like yasnippets can hook in.")
  12793. (defvar org-metaleft-hook nil
  12794. "Hook for functions attaching themselves to `M-left'.
  12795. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12796. (defvar org-metaright-hook nil
  12797. "Hook for functions attaching themselves to `M-right'.
  12798. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12799. (defvar org-metaup-hook nil
  12800. "Hook for functions attaching themselves to `M-up'.
  12801. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12802. (defvar org-metadown-hook nil
  12803. "Hook for functions attaching themselves to `M-down'.
  12804. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12805. (defvar org-shiftmetaleft-hook nil
  12806. "Hook for functions attaching themselves to `M-S-left'.
  12807. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12808. (defvar org-shiftmetaright-hook nil
  12809. "Hook for functions attaching themselves to `M-S-right'.
  12810. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12811. (defvar org-shiftmetaup-hook nil
  12812. "Hook for functions attaching themselves to `M-S-up'.
  12813. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12814. (defvar org-shiftmetadown-hook nil
  12815. "Hook for functions attaching themselves to `M-S-down'.
  12816. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12817. (defvar org-metareturn-hook nil
  12818. "Hook for functions attaching themselves to `M-RET'.
  12819. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12820. (defun org-modifier-cursor-error ()
  12821. "Throw an error, a modified cursor command was applied in wrong context."
  12822. (error "This command is active in special context like tables, headlines or items"))
  12823. (defun org-shiftselect-error ()
  12824. "Throw an error because Shift-Cursor command was applied in wrong context."
  12825. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12826. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12827. (error "This command works only in special context like headlines or timestamps.")))
  12828. (defun org-call-for-shift-select (cmd)
  12829. (let ((this-command-keys-shift-translated t))
  12830. (call-interactively cmd)))
  12831. (defun org-shifttab (&optional arg)
  12832. "Global visibility cycling or move to previous table field.
  12833. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12834. on context.
  12835. See the individual commands for more information."
  12836. (interactive "P")
  12837. (cond
  12838. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12839. ((integerp arg)
  12840. (message "Content view to level: %d" arg)
  12841. (org-content (prefix-numeric-value arg))
  12842. (setq org-cycle-global-status 'overview))
  12843. (t (call-interactively 'org-global-cycle))))
  12844. (defun org-shiftmetaleft ()
  12845. "Promote subtree or delete table column.
  12846. Calls `org-promote-subtree', `org-outdent-item',
  12847. or `org-table-delete-column', depending on context.
  12848. See the individual commands for more information."
  12849. (interactive)
  12850. (cond
  12851. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12852. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12853. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12854. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12855. (t (org-modifier-cursor-error))))
  12856. (defun org-shiftmetaright ()
  12857. "Demote subtree or insert table column.
  12858. Calls `org-demote-subtree', `org-indent-item',
  12859. or `org-table-insert-column', depending on context.
  12860. See the individual commands for more information."
  12861. (interactive)
  12862. (cond
  12863. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12864. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12865. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12866. ((org-at-item-p) (call-interactively 'org-indent-item))
  12867. (t (org-modifier-cursor-error))))
  12868. (defun org-shiftmetaup (&optional arg)
  12869. "Move subtree up or kill table row.
  12870. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12871. `org-move-item-up' depending on context. See the individual commands
  12872. for more information."
  12873. (interactive "P")
  12874. (cond
  12875. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12876. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12877. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12878. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12879. (t (org-modifier-cursor-error))))
  12880. (defun org-shiftmetadown (&optional arg)
  12881. "Move subtree down or insert table row.
  12882. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12883. `org-move-item-down', depending on context. See the individual
  12884. commands for more information."
  12885. (interactive "P")
  12886. (cond
  12887. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12888. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12889. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12890. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12891. (t (org-modifier-cursor-error))))
  12892. (defun org-metaleft (&optional arg)
  12893. "Promote heading or move table column to left.
  12894. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12895. With no specific context, calls the Emacs default `backward-word'.
  12896. See the individual commands for more information."
  12897. (interactive "P")
  12898. (cond
  12899. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12900. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12901. ((or (org-on-heading-p)
  12902. (and (org-region-active-p)
  12903. (save-excursion
  12904. (goto-char (region-beginning))
  12905. (org-on-heading-p))))
  12906. (call-interactively 'org-do-promote))
  12907. ((or (org-at-item-p)
  12908. (and (org-region-active-p)
  12909. (save-excursion
  12910. (goto-char (region-beginning))
  12911. (org-at-item-p))))
  12912. (call-interactively 'org-outdent-item))
  12913. (t (call-interactively 'backward-word))))
  12914. (defun org-metaright (&optional arg)
  12915. "Demote subtree or move table column to right.
  12916. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12917. With no specific context, calls the Emacs default `forward-word'.
  12918. See the individual commands for more information."
  12919. (interactive "P")
  12920. (cond
  12921. ((run-hook-with-args-until-success 'org-metaright-hook))
  12922. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12923. ((or (org-on-heading-p)
  12924. (and (org-region-active-p)
  12925. (save-excursion
  12926. (goto-char (region-beginning))
  12927. (org-on-heading-p))))
  12928. (call-interactively 'org-do-demote))
  12929. ((or (org-at-item-p)
  12930. (and (org-region-active-p)
  12931. (save-excursion
  12932. (goto-char (region-beginning))
  12933. (org-at-item-p))))
  12934. (call-interactively 'org-indent-item))
  12935. (t (call-interactively 'forward-word))))
  12936. (defun org-metaup (&optional arg)
  12937. "Move subtree up or move table row up.
  12938. Calls `org-move-subtree-up' or `org-table-move-row' or
  12939. `org-move-item-up', depending on context. See the individual commands
  12940. for more information."
  12941. (interactive "P")
  12942. (cond
  12943. ((run-hook-with-args-until-success 'org-metaup-hook))
  12944. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12945. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12946. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12947. (t (transpose-lines 1) (beginning-of-line -1))))
  12948. (defun org-metadown (&optional arg)
  12949. "Move subtree down or move table row down.
  12950. Calls `org-move-subtree-down' or `org-table-move-row' or
  12951. `org-move-item-down', depending on context. See the individual
  12952. commands for more information."
  12953. (interactive "P")
  12954. (cond
  12955. ((run-hook-with-args-until-success 'org-metadown-hook))
  12956. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12957. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12958. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12959. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12960. (defun org-shiftup (&optional arg)
  12961. "Increase item in timestamp or increase priority of current headline.
  12962. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12963. depending on context. See the individual commands for more information."
  12964. (interactive "P")
  12965. (cond
  12966. ((and org-support-shift-select (org-region-active-p))
  12967. (org-call-for-shift-select 'previous-line))
  12968. ((org-at-timestamp-p t)
  12969. (call-interactively (if org-edit-timestamp-down-means-later
  12970. 'org-timestamp-down 'org-timestamp-up)))
  12971. ((and (not (eq org-support-shift-select 'always))
  12972. (org-on-heading-p))
  12973. (call-interactively 'org-priority-up))
  12974. ((and (not org-support-shift-select) (org-at-item-p))
  12975. (call-interactively 'org-previous-item))
  12976. ((org-clocktable-try-shift 'up arg))
  12977. (org-support-shift-select
  12978. (org-call-for-shift-select 'previous-line))
  12979. (t (org-shiftselect-error))))
  12980. (defun org-shiftdown (&optional arg)
  12981. "Decrease item in timestamp or decrease priority of current headline.
  12982. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12983. depending on context. See the individual commands for more information."
  12984. (interactive "P")
  12985. (cond
  12986. ((and org-support-shift-select (org-region-active-p))
  12987. (org-call-for-shift-select 'next-line))
  12988. ((org-at-timestamp-p t)
  12989. (call-interactively (if org-edit-timestamp-down-means-later
  12990. 'org-timestamp-up 'org-timestamp-down)))
  12991. ((and (not (eq org-support-shift-select 'always))
  12992. (org-on-heading-p))
  12993. (call-interactively 'org-priority-down))
  12994. ((and (not org-support-shift-select) (org-at-item-p))
  12995. (call-interactively 'org-next-item))
  12996. ((org-clocktable-try-shift 'down arg))
  12997. (org-support-shift-select
  12998. (org-call-for-shift-select 'next-line))
  12999. (t (org-shiftselect-error))))
  13000. (defun org-shiftright (&optional arg)
  13001. "Cycle the thing at point or in the current line, depending on context.
  13002. Depending on context, this does one of the following:
  13003. - switch a timestamp at point one day into the future
  13004. - on a headline, switch to the next TODO keyword.
  13005. - on an item, switch entire list to the next bullet type
  13006. - on a property line, switch to the next allowed value
  13007. - on a clocktable definition line, move time block into the future"
  13008. (interactive "P")
  13009. (cond
  13010. ((and org-support-shift-select (org-region-active-p))
  13011. (org-call-for-shift-select 'forward-char))
  13012. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13013. ((and (not (eq org-support-shift-select 'always))
  13014. (org-on-heading-p))
  13015. (org-call-with-arg 'org-todo 'right))
  13016. ((or (and org-support-shift-select
  13017. (not (eq org-support-shift-select 'always))
  13018. (org-at-item-bullet-p))
  13019. (and (not org-support-shift-select) (org-at-item-p)))
  13020. (org-call-with-arg 'org-cycle-list-bullet nil))
  13021. ((and (not (eq org-support-shift-select 'always))
  13022. (org-at-property-p))
  13023. (call-interactively 'org-property-next-allowed-value))
  13024. ((org-clocktable-try-shift 'right arg))
  13025. (org-support-shift-select
  13026. (org-call-for-shift-select 'forward-char))
  13027. (t (org-shiftselect-error))))
  13028. (defun org-shiftleft (&optional arg)
  13029. "Cycle the thing at point or in the current line, depending on context.
  13030. Depending on context, this does one of the following:
  13031. - switch a timestamp at point one day into the past
  13032. - on a headline, switch to the previous TODO keyword.
  13033. - on an item, switch entire list to the previous bullet type
  13034. - on a property line, switch to the previous allowed value
  13035. - on a clocktable definition line, move time block into the past"
  13036. (interactive "P")
  13037. (cond
  13038. ((and org-support-shift-select (org-region-active-p))
  13039. (org-call-for-shift-select 'backward-char))
  13040. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13041. ((and (not (eq org-support-shift-select 'always))
  13042. (org-on-heading-p))
  13043. (org-call-with-arg 'org-todo 'left))
  13044. ((or (and org-support-shift-select
  13045. (not (eq org-support-shift-select 'always))
  13046. (org-at-item-bullet-p))
  13047. (and (not org-support-shift-select) (org-at-item-p)))
  13048. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13049. ((and (not (eq org-support-shift-select 'always))
  13050. (org-at-property-p))
  13051. (call-interactively 'org-property-previous-allowed-value))
  13052. ((org-clocktable-try-shift 'left arg))
  13053. (org-support-shift-select
  13054. (org-call-for-shift-select 'backward-char))
  13055. (t (org-shiftselect-error))))
  13056. (defun org-shiftcontrolright ()
  13057. "Switch to next TODO set."
  13058. (interactive)
  13059. (cond
  13060. ((and org-support-shift-select (org-region-active-p))
  13061. (org-call-for-shift-select 'forward-word))
  13062. ((and (not (eq org-support-shift-select 'always))
  13063. (org-on-heading-p))
  13064. (org-call-with-arg 'org-todo 'nextset))
  13065. (org-support-shift-select
  13066. (org-call-for-shift-select 'forward-word))
  13067. (t (org-shiftselect-error))))
  13068. (defun org-shiftcontrolleft ()
  13069. "Switch to previous TODO set."
  13070. (interactive)
  13071. (cond
  13072. ((and org-support-shift-select (org-region-active-p))
  13073. (org-call-for-shift-select 'backward-word))
  13074. ((and (not (eq org-support-shift-select 'always))
  13075. (org-on-heading-p))
  13076. (org-call-with-arg 'org-todo 'previousset))
  13077. (org-support-shift-select
  13078. (org-call-for-shift-select 'backward-word))
  13079. (t (org-shiftselect-error))))
  13080. (defun org-ctrl-c-ret ()
  13081. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13082. (interactive)
  13083. (cond
  13084. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13085. (t (call-interactively 'org-insert-heading))))
  13086. (defun org-copy-special ()
  13087. "Copy region in table or copy current subtree.
  13088. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13089. See the individual commands for more information."
  13090. (interactive)
  13091. (call-interactively
  13092. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13093. (defun org-cut-special ()
  13094. "Cut region in table or cut current subtree.
  13095. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13096. See the individual commands for more information."
  13097. (interactive)
  13098. (call-interactively
  13099. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13100. (defun org-paste-special (arg)
  13101. "Paste rectangular region into table, or past subtree relative to level.
  13102. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13103. See the individual commands for more information."
  13104. (interactive "P")
  13105. (if (org-at-table-p)
  13106. (org-table-paste-rectangle)
  13107. (org-paste-subtree arg)))
  13108. (defun org-edit-special ()
  13109. "Call a special editor for the stuff at point.
  13110. When at a table, call the formula editor with `org-table-edit-formulas'.
  13111. When at the first line of an src example, call `org-edit-src-code'.
  13112. When in an #+include line, visit the include file. Otherwise call
  13113. `ffap' to visit the file at point."
  13114. (interactive)
  13115. (cond
  13116. ((org-at-table-p)
  13117. (call-interactively 'org-table-edit-formulas))
  13118. ((save-excursion
  13119. (beginning-of-line 1)
  13120. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13121. (find-file (org-trim (match-string 1))))
  13122. ((org-edit-src-code))
  13123. ((org-edit-fixed-width-region))
  13124. (t (call-interactively 'ffap))))
  13125. (defun org-ctrl-c-ctrl-c (&optional arg)
  13126. "Set tags in headline, or update according to changed information at point.
  13127. This command does many different things, depending on context:
  13128. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13129. this is what we do.
  13130. - If the cursor is in a headline, prompt for tags and insert them
  13131. into the current line, aligned to `org-tags-column'. When called
  13132. with prefix arg, realign all tags in the current buffer.
  13133. - If the cursor is in one of the special #+KEYWORD lines, this
  13134. triggers scanning the buffer for these lines and updating the
  13135. information.
  13136. - If the cursor is inside a table, realign the table. This command
  13137. works even if the automatic table editor has been turned off.
  13138. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13139. the entire table.
  13140. - If the cursor is at a footnote reference or definition, jump to
  13141. the corresponding definition or references, respectively.
  13142. - If the cursor is a the beginning of a dynamic block, update it.
  13143. - If the cursor is inside a table created by the table.el package,
  13144. activate that table.
  13145. - If the current buffer is a remember buffer, close note and file
  13146. it. A prefix argument of 1 files to the default location
  13147. without further interaction. A prefix argument of 2 files to
  13148. the currently clocking task.
  13149. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13150. links in this buffer.
  13151. - If the cursor is on a numbered item in a plain list, renumber the
  13152. ordered list.
  13153. - If the cursor is on a checkbox, toggle it."
  13154. (interactive "P")
  13155. (let ((org-enable-table-editor t))
  13156. (cond
  13157. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13158. org-occur-highlights
  13159. org-latex-fragment-image-overlays)
  13160. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13161. (org-remove-occur-highlights)
  13162. (org-remove-latex-fragment-image-overlays)
  13163. (message "Temporary highlights/overlays removed from current buffer"))
  13164. ((and (local-variable-p 'org-finish-function (current-buffer))
  13165. (fboundp org-finish-function))
  13166. (funcall org-finish-function))
  13167. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13168. ((org-at-property-p)
  13169. (call-interactively 'org-property-action))
  13170. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13171. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13172. ((org-at-table.el-p)
  13173. (require 'table)
  13174. (beginning-of-line 1)
  13175. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13176. (call-interactively 'table-recognize-table))
  13177. ((org-at-table-p)
  13178. (org-table-maybe-eval-formula)
  13179. (if arg
  13180. (call-interactively 'org-table-recalculate)
  13181. (org-table-maybe-recalculate-line))
  13182. (call-interactively 'org-table-align))
  13183. ((or (org-footnote-at-reference-p)
  13184. (org-footnote-at-definition-p))
  13185. (call-interactively 'org-footnote-action))
  13186. ((org-at-item-checkbox-p)
  13187. (call-interactively 'org-toggle-checkbox))
  13188. ((org-at-item-p)
  13189. (if arg
  13190. (call-interactively 'org-toggle-checkbox)
  13191. (call-interactively 'org-maybe-renumber-ordered-list)))
  13192. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13193. ;; Dynamic block
  13194. (beginning-of-line 1)
  13195. (save-excursion (org-update-dblock)))
  13196. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13197. (cond
  13198. ((equal (match-string 1) "TBLFM")
  13199. ;; Recalculate the table before this line
  13200. (save-excursion
  13201. (beginning-of-line 1)
  13202. (skip-chars-backward " \r\n\t")
  13203. (if (org-at-table-p)
  13204. (org-call-with-arg 'org-table-recalculate t))))
  13205. (t
  13206. ; (org-set-regexps-and-options)
  13207. ; (org-restart-font-lock)
  13208. (let ((org-inhibit-startup t)) (org-mode-restart))
  13209. (message "Local setup has been refreshed"))))
  13210. (t (error "C-c C-c can do nothing useful at this location.")))))
  13211. (defun org-mode-restart ()
  13212. "Restart Org-mode, to scan again for special lines.
  13213. Also updates the keyword regular expressions."
  13214. (interactive)
  13215. (org-mode)
  13216. (message "Org-mode restarted"))
  13217. (defun org-kill-note-or-show-branches ()
  13218. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13219. (interactive)
  13220. (if (not org-finish-function)
  13221. (call-interactively 'show-branches)
  13222. (let ((org-note-abort t))
  13223. (funcall org-finish-function))))
  13224. (defun org-return (&optional indent)
  13225. "Goto next table row or insert a newline.
  13226. Calls `org-table-next-row' or `newline', depending on context.
  13227. See the individual commands for more information."
  13228. (interactive)
  13229. (cond
  13230. ((bobp) (if indent (newline-and-indent) (newline)))
  13231. ((org-at-table-p)
  13232. (org-table-justify-field-maybe)
  13233. (call-interactively 'org-table-next-row))
  13234. ((and org-return-follows-link
  13235. (eq (get-text-property (point) 'face) 'org-link))
  13236. (call-interactively 'org-open-at-point))
  13237. ((and (org-at-heading-p)
  13238. (looking-at
  13239. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13240. (org-show-entry)
  13241. (end-of-line 1)
  13242. (newline))
  13243. (t (if indent (newline-and-indent) (newline)))))
  13244. (defun org-return-indent ()
  13245. "Goto next table row or insert a newline and indent.
  13246. Calls `org-table-next-row' or `newline-and-indent', depending on
  13247. context. See the individual commands for more information."
  13248. (interactive)
  13249. (org-return t))
  13250. (defun org-ctrl-c-star ()
  13251. "Compute table, or change heading status of lines.
  13252. Calls `org-table-recalculate' or `org-toggle-heading',
  13253. depending on context."
  13254. (interactive)
  13255. (cond
  13256. ((org-at-table-p)
  13257. (call-interactively 'org-table-recalculate))
  13258. (t
  13259. ;; Convert all lines in region to list items
  13260. (call-interactively 'org-toggle-heading))))
  13261. (defun org-ctrl-c-minus ()
  13262. "Insert separator line in table or modify bullet status of line.
  13263. Also turns a plain line or a region of lines into list items.
  13264. Calls `org-table-insert-hline', `org-toggle-item', or
  13265. `org-cycle-list-bullet', depending on context."
  13266. (interactive)
  13267. (cond
  13268. ((org-at-table-p)
  13269. (call-interactively 'org-table-insert-hline))
  13270. ((org-region-active-p)
  13271. (call-interactively 'org-toggle-item))
  13272. ((org-in-item-p)
  13273. (call-interactively 'org-cycle-list-bullet))
  13274. (t
  13275. (call-interactively 'org-toggle-item))))
  13276. (defun org-toggle-item ()
  13277. "Convert headings or normal lines to items, items to normal lines.
  13278. If there is no active region, only the current line is considered.
  13279. If the first line in the region is a headline, convert all headlines to items.
  13280. If the first line in the region is an item, convert all items to normal lines.
  13281. If the first line is normal text, add an item bullet to each line."
  13282. (interactive)
  13283. (let (l2 l beg end)
  13284. (if (org-region-active-p)
  13285. (setq beg (region-beginning) end (region-end))
  13286. (setq beg (point-at-bol)
  13287. end (min (1+ (point-at-eol)) (point-max))))
  13288. (save-excursion
  13289. (goto-char end)
  13290. (setq l2 (org-current-line))
  13291. (goto-char beg)
  13292. (beginning-of-line 1)
  13293. (setq l (1- (org-current-line)))
  13294. (if (org-at-item-p)
  13295. ;; We already have items, de-itemize
  13296. (while (< (setq l (1+ l)) l2)
  13297. (when (org-at-item-p)
  13298. (goto-char (match-beginning 2))
  13299. (delete-region (match-beginning 2) (match-end 2))
  13300. (and (looking-at "[ \t]+") (replace-match "")))
  13301. (beginning-of-line 2))
  13302. (if (org-on-heading-p)
  13303. ;; Headings, convert to items
  13304. (while (< (setq l (1+ l)) l2)
  13305. (if (looking-at org-outline-regexp)
  13306. (replace-match "- " t t))
  13307. (beginning-of-line 2))
  13308. ;; normal lines, turn them into items
  13309. (while (< (setq l (1+ l)) l2)
  13310. (unless (org-at-item-p)
  13311. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13312. (replace-match "\\1- \\2")))
  13313. (beginning-of-line 2)))))))
  13314. (defun org-toggle-heading (&optional nstars)
  13315. "Convert headings to normal text, or items or text to headings.
  13316. If there is no active region, only the current line is considered.
  13317. If the first line is a heading, remove the stars from all headlines
  13318. in the region.
  13319. If the first line is a plain list item, turn all plain list items into
  13320. headings.
  13321. If the first line is a normal line, turn each and every line in the region
  13322. into a heading.
  13323. When converting a line into a heading, the number of stars is chosen
  13324. such that the lines become children of the current entry. However, when
  13325. a prefix argument is given, its value determines the number of stars to add."
  13326. (interactive "P")
  13327. (let (l2 l itemp beg end)
  13328. (if (org-region-active-p)
  13329. (setq beg (region-beginning) end (region-end))
  13330. (setq beg (point-at-bol)
  13331. end (min (1+ (point-at-eol)) (point-max))))
  13332. (save-excursion
  13333. (goto-char end)
  13334. (setq l2 (org-current-line))
  13335. (goto-char beg)
  13336. (beginning-of-line 1)
  13337. (setq l (1- (org-current-line)))
  13338. (if (org-on-heading-p)
  13339. ;; We already have headlines, de-star them
  13340. (while (< (setq l (1+ l)) l2)
  13341. (when (org-on-heading-p t)
  13342. (and (looking-at outline-regexp) (replace-match "")))
  13343. (beginning-of-line 2))
  13344. (setq itemp (org-at-item-p))
  13345. (let* ((stars
  13346. (if nstars
  13347. (make-string (prefix-numeric-value current-prefix-arg)
  13348. ?*)
  13349. (save-excursion
  13350. (re-search-backward org-complex-heading-regexp nil t)
  13351. (or (match-string 1) ""))))
  13352. (add-stars (cond (nstars "")
  13353. ((equal stars "") "*")
  13354. (org-odd-levels-only "**")
  13355. (t "*")))
  13356. (rpl (concat stars add-stars " ")))
  13357. (while (< (setq l (1+ l)) l2)
  13358. (if itemp
  13359. (and (org-at-item-p) (replace-match rpl t t))
  13360. (unless (org-on-heading-p)
  13361. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13362. (replace-match (concat rpl (match-string 2))))))
  13363. (beginning-of-line 2)))))))
  13364. (defun org-meta-return (&optional arg)
  13365. "Insert a new heading or wrap a region in a table.
  13366. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13367. See the individual commands for more information."
  13368. (interactive "P")
  13369. (cond
  13370. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13371. ((org-at-table-p)
  13372. (call-interactively 'org-table-wrap-region))
  13373. (t (call-interactively 'org-insert-heading))))
  13374. ;;; Menu entries
  13375. ;; Define the Org-mode menus
  13376. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13377. '("Tbl"
  13378. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13379. ["Next Field" org-cycle (org-at-table-p)]
  13380. ["Previous Field" org-shifttab (org-at-table-p)]
  13381. ["Next Row" org-return (org-at-table-p)]
  13382. "--"
  13383. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13384. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13385. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13386. "--"
  13387. ("Column"
  13388. ["Move Column Left" org-metaleft (org-at-table-p)]
  13389. ["Move Column Right" org-metaright (org-at-table-p)]
  13390. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13391. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13392. ("Row"
  13393. ["Move Row Up" org-metaup (org-at-table-p)]
  13394. ["Move Row Down" org-metadown (org-at-table-p)]
  13395. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13396. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13397. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13398. "--"
  13399. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13400. ("Rectangle"
  13401. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13402. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13403. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13404. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13405. "--"
  13406. ("Calculate"
  13407. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13408. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13409. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13410. "--"
  13411. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13412. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13413. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13414. "--"
  13415. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13416. "--"
  13417. ["Sum Column/Rectangle" org-table-sum
  13418. (or (org-at-table-p) (org-region-active-p))]
  13419. ["Which Column?" org-table-current-column (org-at-table-p)])
  13420. ["Debug Formulas"
  13421. org-table-toggle-formula-debugger
  13422. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13423. ["Show Col/Row Numbers"
  13424. org-table-toggle-coordinate-overlays
  13425. :style toggle
  13426. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13427. "--"
  13428. ["Create" org-table-create (and (not (org-at-table-p))
  13429. org-enable-table-editor)]
  13430. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13431. ["Import from File" org-table-import (not (org-at-table-p))]
  13432. ["Export to File" org-table-export (org-at-table-p)]
  13433. "--"
  13434. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13435. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13436. '("Org"
  13437. ("Show/Hide"
  13438. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13439. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13440. ["Sparse Tree..." org-sparse-tree t]
  13441. ["Reveal Context" org-reveal t]
  13442. ["Show All" show-all t]
  13443. "--"
  13444. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13445. "--"
  13446. ["New Heading" org-insert-heading t]
  13447. ("Navigate Headings"
  13448. ["Up" outline-up-heading t]
  13449. ["Next" outline-next-visible-heading t]
  13450. ["Previous" outline-previous-visible-heading t]
  13451. ["Next Same Level" outline-forward-same-level t]
  13452. ["Previous Same Level" outline-backward-same-level t]
  13453. "--"
  13454. ["Jump" org-goto t])
  13455. ("Edit Structure"
  13456. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13457. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13458. "--"
  13459. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13460. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13461. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13462. "--"
  13463. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13464. "--"
  13465. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13466. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13467. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13468. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13469. "--"
  13470. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13471. "--"
  13472. ["Convert to odd levels" org-convert-to-odd-levels t]
  13473. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13474. ("Editing"
  13475. ["Emphasis..." org-emphasize t]
  13476. ["Edit Source Example" org-edit-special t]
  13477. "--"
  13478. ["Footnote new/jump" org-footnote-action t]
  13479. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13480. ("Archive"
  13481. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13482. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13483. ; :active t :keys "C-u C-c C-x C-a"]
  13484. ["Sparse trees open ARCHIVE trees"
  13485. (setq org-sparse-tree-open-archived-trees
  13486. (not org-sparse-tree-open-archived-trees))
  13487. :style toggle :selected org-sparse-tree-open-archived-trees]
  13488. ["Cycling opens ARCHIVE trees"
  13489. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13490. :style toggle :selected org-cycle-open-archived-trees]
  13491. "--"
  13492. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13493. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13494. ; ["Check and Move Children" (org-archive-subtree '(4))
  13495. ; :active t :keys "C-u C-c C-x C-s"]
  13496. )
  13497. "--"
  13498. ("Hyperlinks"
  13499. ["Store Link (Global)" org-store-link t]
  13500. ["Insert Link" org-insert-link t]
  13501. ["Follow Link" org-open-at-point t]
  13502. "--"
  13503. ["Next link" org-next-link t]
  13504. ["Previous link" org-previous-link t]
  13505. "--"
  13506. ["Descriptive Links"
  13507. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13508. :style radio
  13509. :selected (member '(org-link) buffer-invisibility-spec)]
  13510. ["Literal Links"
  13511. (progn
  13512. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13513. :style radio
  13514. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13515. "--"
  13516. ("TODO Lists"
  13517. ["TODO/DONE/-" org-todo t]
  13518. ("Select keyword"
  13519. ["Next keyword" org-shiftright (org-on-heading-p)]
  13520. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13521. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13522. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13523. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13524. ["Show TODO Tree" org-show-todo-tree t]
  13525. ["Global TODO list" org-todo-list t]
  13526. "--"
  13527. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13528. :selected org-enforce-todo-dependencies :style toggle :active t]
  13529. "Settings for tree at point"
  13530. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13531. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13532. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13533. ["Do Children parallel" org-toggle-ordered-property :style radio
  13534. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13535. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13536. "--"
  13537. ["Set Priority" org-priority t]
  13538. ["Priority Up" org-shiftup t]
  13539. ["Priority Down" org-shiftdown t]
  13540. "--"
  13541. ["Get news from all feeds" org-feed-update-all t]
  13542. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13543. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13544. ("TAGS and Properties"
  13545. ["Set Tags" org-set-tags-command t]
  13546. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13547. "--"
  13548. ["Set property" org-set-property t]
  13549. ["Column view of properties" org-columns t]
  13550. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13551. ("Dates and Scheduling"
  13552. ["Timestamp" org-time-stamp t]
  13553. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13554. ("Change Date"
  13555. ["1 Day Later" org-shiftright t]
  13556. ["1 Day Earlier" org-shiftleft t]
  13557. ["1 ... Later" org-shiftup t]
  13558. ["1 ... Earlier" org-shiftdown t])
  13559. ["Compute Time Range" org-evaluate-time-range t]
  13560. ["Schedule Item" org-schedule t]
  13561. ["Deadline" org-deadline t]
  13562. "--"
  13563. ["Custom time format" org-toggle-time-stamp-overlays
  13564. :style radio :selected org-display-custom-times]
  13565. "--"
  13566. ["Goto Calendar" org-goto-calendar t]
  13567. ["Date from Calendar" org-date-from-calendar t]
  13568. "--"
  13569. ["Start/Restart Timer" org-timer-start t]
  13570. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13571. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13572. ["Insert Timer String" org-timer t]
  13573. ["Insert Timer Item" org-timer-item t])
  13574. ("Logging work"
  13575. ["Clock in" org-clock-in t]
  13576. ["Clock out" org-clock-out t]
  13577. ["Clock cancel" org-clock-cancel t]
  13578. ["Goto running clock" org-clock-goto t]
  13579. ["Display times" org-clock-display t]
  13580. ["Create clock table" org-clock-report t]
  13581. "--"
  13582. ["Record DONE time"
  13583. (progn (setq org-log-done (not org-log-done))
  13584. (message "Switching to %s will %s record a timestamp"
  13585. (car org-done-keywords)
  13586. (if org-log-done "automatically" "not")))
  13587. :style toggle :selected org-log-done])
  13588. "--"
  13589. ["Agenda Command..." org-agenda t]
  13590. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13591. ("File List for Agenda")
  13592. ("Special views current file"
  13593. ["TODO Tree" org-show-todo-tree t]
  13594. ["Check Deadlines" org-check-deadlines t]
  13595. ["Timeline" org-timeline t]
  13596. ["Tags/Property tree" org-match-sparse-tree t])
  13597. "--"
  13598. ["Export/Publish..." org-export t]
  13599. ("LaTeX"
  13600. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13601. :selected org-cdlatex-mode]
  13602. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13603. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13604. ["Modify math symbol" org-cdlatex-math-modify
  13605. (org-inside-LaTeX-fragment-p)]
  13606. ["Insert citation" org-reftex-citation t]
  13607. "--"
  13608. ["Export LaTeX fragments as images"
  13609. (if (featurep 'org-exp)
  13610. (setq org-export-with-LaTeX-fragments
  13611. (not org-export-with-LaTeX-fragments))
  13612. (require 'org-exp))
  13613. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13614. org-export-with-LaTeX-fragments)])
  13615. "--"
  13616. ("Documentation"
  13617. ["Show Version" org-version t]
  13618. ["Info Documentation" org-info t])
  13619. ("Customize"
  13620. ["Browse Org Group" org-customize t]
  13621. "--"
  13622. ["Expand This Menu" org-create-customize-menu
  13623. (fboundp 'customize-menu-create)])
  13624. "--"
  13625. ("Refresh/Reload"
  13626. ["Refresh setup current buffer" org-mode-restart t]
  13627. ["Reload Org (after update)" org-reload t]
  13628. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13629. ))
  13630. (defun org-info (&optional node)
  13631. "Read documentation for Org-mode in the info system.
  13632. With optional NODE, go directly to that node."
  13633. (interactive)
  13634. (info (format "(org)%s" (or node ""))))
  13635. (defun org-install-agenda-files-menu ()
  13636. (let ((bl (buffer-list)))
  13637. (save-excursion
  13638. (while bl
  13639. (set-buffer (pop bl))
  13640. (if (org-mode-p) (setq bl nil)))
  13641. (when (org-mode-p)
  13642. (easy-menu-change
  13643. '("Org") "File List for Agenda"
  13644. (append
  13645. (list
  13646. ["Edit File List" (org-edit-agenda-file-list) t]
  13647. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13648. ["Remove Current File from List" org-remove-file t]
  13649. ["Cycle through agenda files" org-cycle-agenda-files t]
  13650. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13651. "--")
  13652. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13653. ;;;; Documentation
  13654. ;;;###autoload
  13655. (defun org-require-autoloaded-modules ()
  13656. (interactive)
  13657. (mapc 'require
  13658. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13659. org-docbook org-exp org-html org-icalendar
  13660. org-id org-latex
  13661. org-publish org-remember org-table
  13662. org-timer org-xoxo)))
  13663. ;;;###autoload
  13664. (defun org-reload (&optional uncompiled)
  13665. "Reload all org lisp files.
  13666. With prefix arg UNCOMPILED, load the uncompiled versions."
  13667. (interactive "P")
  13668. (require 'find-func)
  13669. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13670. (dir-org (file-name-directory (org-find-library-name "org")))
  13671. (dir-org-contrib (ignore-errors
  13672. (file-name-directory
  13673. (org-find-library-name "org-contribdir"))))
  13674. (files
  13675. (append (directory-files dir-org t file-re)
  13676. (and dir-org-contrib
  13677. (directory-files dir-org-contrib t file-re))))
  13678. (remove-re (concat (if (featurep 'xemacs)
  13679. "org-colview" "org-colview-xemacs")
  13680. "\\'")))
  13681. (setq files (mapcar 'file-name-sans-extension files))
  13682. (setq files (mapcar
  13683. (lambda (x) (if (string-match remove-re x) nil x))
  13684. files))
  13685. (setq files (delq nil files))
  13686. (mapc
  13687. (lambda (f)
  13688. (when (featurep (intern (file-name-nondirectory f)))
  13689. (if (and (not uncompiled)
  13690. (file-exists-p (concat f ".elc")))
  13691. (load (concat f ".elc") nil nil t)
  13692. (load (concat f ".el") nil nil t))))
  13693. files))
  13694. (org-version))
  13695. ;;;###autoload
  13696. (defun org-customize ()
  13697. "Call the customize function with org as argument."
  13698. (interactive)
  13699. (org-load-modules-maybe)
  13700. (org-require-autoloaded-modules)
  13701. (customize-browse 'org))
  13702. (defun org-create-customize-menu ()
  13703. "Create a full customization menu for Org-mode, insert it into the menu."
  13704. (interactive)
  13705. (org-load-modules-maybe)
  13706. (org-require-autoloaded-modules)
  13707. (if (fboundp 'customize-menu-create)
  13708. (progn
  13709. (easy-menu-change
  13710. '("Org") "Customize"
  13711. `(["Browse Org group" org-customize t]
  13712. "--"
  13713. ,(customize-menu-create 'org)
  13714. ["Set" Custom-set t]
  13715. ["Save" Custom-save t]
  13716. ["Reset to Current" Custom-reset-current t]
  13717. ["Reset to Saved" Custom-reset-saved t]
  13718. ["Reset to Standard Settings" Custom-reset-standard t]))
  13719. (message "\"Org\"-menu now contains full customization menu"))
  13720. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13721. ;;;; Miscellaneous stuff
  13722. ;;; Generally useful functions
  13723. (defun org-find-text-property-in-string (prop s)
  13724. "Return the first non-nil value of property PROP in string S."
  13725. (or (get-text-property 0 prop s)
  13726. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13727. prop s)))
  13728. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13729. "Display the given MESSAGE as a warning."
  13730. (if (fboundp 'display-warning)
  13731. (display-warning 'org message
  13732. (if (featurep 'xemacs)
  13733. 'warning
  13734. :warning))
  13735. (let ((buf (get-buffer-create "*Org warnings*")))
  13736. (with-current-buffer buf
  13737. (goto-char (point-max))
  13738. (insert "Warning (Org): " message)
  13739. (unless (bolp)
  13740. (newline)))
  13741. (display-buffer buf)
  13742. (sit-for 0))))
  13743. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13744. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13745. (if (and marker (marker-buffer marker)
  13746. (buffer-live-p (marker-buffer marker)))
  13747. (progn
  13748. (switch-to-buffer (marker-buffer marker))
  13749. (if (or (> marker (point-max)) (< marker (point-min)))
  13750. (widen))
  13751. (goto-char marker)
  13752. (org-show-context 'org-goto))
  13753. (if bookmark
  13754. (bookmark-jump bookmark)
  13755. (error "Cannot find location"))))
  13756. (defun org-quote-csv-field (s)
  13757. "Quote field for inclusion in CSV material."
  13758. (if (string-match "[\",]" s)
  13759. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13760. s))
  13761. (defun org-plist-delete (plist property)
  13762. "Delete PROPERTY from PLIST.
  13763. This is in contrast to merely setting it to 0."
  13764. (let (p)
  13765. (while plist
  13766. (if (not (eq property (car plist)))
  13767. (setq p (plist-put p (car plist) (nth 1 plist))))
  13768. (setq plist (cddr plist)))
  13769. p))
  13770. (defun org-force-self-insert (N)
  13771. "Needed to enforce self-insert under remapping."
  13772. (interactive "p")
  13773. (self-insert-command N))
  13774. (defun org-string-width (s)
  13775. "Compute width of string, ignoring invisible characters.
  13776. This ignores character with invisibility property `org-link', and also
  13777. characters with property `org-cwidth', because these will become invisible
  13778. upon the next fontification round."
  13779. (let (b l)
  13780. (when (or (eq t buffer-invisibility-spec)
  13781. (assq 'org-link buffer-invisibility-spec))
  13782. (while (setq b (text-property-any 0 (length s)
  13783. 'invisible 'org-link s))
  13784. (setq s (concat (substring s 0 b)
  13785. (substring s (or (next-single-property-change
  13786. b 'invisible s) (length s)))))))
  13787. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13788. (setq s (concat (substring s 0 b)
  13789. (substring s (or (next-single-property-change
  13790. b 'org-cwidth s) (length s))))))
  13791. (setq l (string-width s) b -1)
  13792. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13793. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13794. l))
  13795. (defun org-get-indentation (&optional line)
  13796. "Get the indentation of the current line, interpreting tabs.
  13797. When LINE is given, assume it represents a line and compute its indentation."
  13798. (if line
  13799. (if (string-match "^ *" (org-remove-tabs line))
  13800. (match-end 0))
  13801. (save-excursion
  13802. (beginning-of-line 1)
  13803. (skip-chars-forward " \t")
  13804. (current-column))))
  13805. (defun org-remove-tabs (s &optional width)
  13806. "Replace tabulators in S with spaces.
  13807. Assumes that s is a single line, starting in column 0."
  13808. (setq width (or width tab-width))
  13809. (while (string-match "\t" s)
  13810. (setq s (replace-match
  13811. (make-string
  13812. (- (* width (/ (+ (match-beginning 0) width) width))
  13813. (match-beginning 0)) ?\ )
  13814. t t s)))
  13815. s)
  13816. (defun org-fix-indentation (line ind)
  13817. "Fix indentation in LINE.
  13818. IND is a cons cell with target and minimum indentation.
  13819. If the current indentation in LINE is smaller than the minimum,
  13820. leave it alone. If it is larger than ind, set it to the target."
  13821. (let* ((l (org-remove-tabs line))
  13822. (i (org-get-indentation l))
  13823. (i1 (car ind)) (i2 (cdr ind)))
  13824. (if (>= i i2) (setq l (substring line i2)))
  13825. (if (> i1 0)
  13826. (concat (make-string i1 ?\ ) l)
  13827. l)))
  13828. (defun org-base-buffer (buffer)
  13829. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13830. (if (not buffer)
  13831. buffer
  13832. (or (buffer-base-buffer buffer)
  13833. buffer)))
  13834. (defun org-trim (s)
  13835. "Remove whitespace at beginning and end of string."
  13836. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13837. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13838. s)
  13839. (defun org-wrap (string &optional width lines)
  13840. "Wrap string to either a number of lines, or a width in characters.
  13841. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13842. that costs. If there is a word longer than WIDTH, the text is actually
  13843. wrapped to the length of that word.
  13844. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13845. many lines, whatever width that takes.
  13846. The return value is a list of lines, without newlines at the end."
  13847. (let* ((words (org-split-string string "[ \t\n]+"))
  13848. (maxword (apply 'max (mapcar 'org-string-width words)))
  13849. w ll)
  13850. (cond (width
  13851. (org-do-wrap words (max maxword width)))
  13852. (lines
  13853. (setq w maxword)
  13854. (setq ll (org-do-wrap words maxword))
  13855. (if (<= (length ll) lines)
  13856. ll
  13857. (setq ll words)
  13858. (while (> (length ll) lines)
  13859. (setq w (1+ w))
  13860. (setq ll (org-do-wrap words w)))
  13861. ll))
  13862. (t (error "Cannot wrap this")))))
  13863. (defun org-do-wrap (words width)
  13864. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13865. (let (lines line)
  13866. (while words
  13867. (setq line (pop words))
  13868. (while (and words (< (+ (length line) (length (car words))) width))
  13869. (setq line (concat line " " (pop words))))
  13870. (setq lines (push line lines)))
  13871. (nreverse lines)))
  13872. (defun org-split-string (string &optional separators)
  13873. "Splits STRING into substrings at SEPARATORS.
  13874. No empty strings are returned if there are matches at the beginning
  13875. and end of string."
  13876. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13877. (start 0)
  13878. notfirst
  13879. (list nil))
  13880. (while (and (string-match rexp string
  13881. (if (and notfirst
  13882. (= start (match-beginning 0))
  13883. (< start (length string)))
  13884. (1+ start) start))
  13885. (< (match-beginning 0) (length string)))
  13886. (setq notfirst t)
  13887. (or (eq (match-beginning 0) 0)
  13888. (and (eq (match-beginning 0) (match-end 0))
  13889. (eq (match-beginning 0) start))
  13890. (setq list
  13891. (cons (substring string start (match-beginning 0))
  13892. list)))
  13893. (setq start (match-end 0)))
  13894. (or (eq start (length string))
  13895. (setq list
  13896. (cons (substring string start)
  13897. list)))
  13898. (nreverse list)))
  13899. (defun org-uuidgen-p (s)
  13900. "Is S an ID created by UUIDGEN?"
  13901. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13902. (defun org-context ()
  13903. "Return a list of contexts of the current cursor position.
  13904. If several contexts apply, all are returned.
  13905. Each context entry is a list with a symbol naming the context, and
  13906. two positions indicating start and end of the context. Possible
  13907. contexts are:
  13908. :headline anywhere in a headline
  13909. :headline-stars on the leading stars in a headline
  13910. :todo-keyword on a TODO keyword (including DONE) in a headline
  13911. :tags on the TAGS in a headline
  13912. :priority on the priority cookie in a headline
  13913. :item on the first line of a plain list item
  13914. :item-bullet on the bullet/number of a plain list item
  13915. :checkbox on the checkbox in a plain list item
  13916. :table in an org-mode table
  13917. :table-special on a special filed in a table
  13918. :table-table in a table.el table
  13919. :link on a hyperlink
  13920. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13921. :target on a <<target>>
  13922. :radio-target on a <<<radio-target>>>
  13923. :latex-fragment on a LaTeX fragment
  13924. :latex-preview on a LaTeX fragment with overlayed preview image
  13925. This function expects the position to be visible because it uses font-lock
  13926. faces as a help to recognize the following contexts: :table-special, :link,
  13927. and :keyword."
  13928. (let* ((f (get-text-property (point) 'face))
  13929. (faces (if (listp f) f (list f)))
  13930. (p (point)) clist o)
  13931. ;; First the large context
  13932. (cond
  13933. ((org-on-heading-p t)
  13934. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13935. (when (progn
  13936. (beginning-of-line 1)
  13937. (looking-at org-todo-line-tags-regexp))
  13938. (push (org-point-in-group p 1 :headline-stars) clist)
  13939. (push (org-point-in-group p 2 :todo-keyword) clist)
  13940. (push (org-point-in-group p 4 :tags) clist))
  13941. (goto-char p)
  13942. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13943. (if (looking-at "\\[#[A-Z0-9]\\]")
  13944. (push (org-point-in-group p 0 :priority) clist)))
  13945. ((org-at-item-p)
  13946. (push (org-point-in-group p 2 :item-bullet) clist)
  13947. (push (list :item (point-at-bol)
  13948. (save-excursion (org-end-of-item) (point)))
  13949. clist)
  13950. (and (org-at-item-checkbox-p)
  13951. (push (org-point-in-group p 0 :checkbox) clist)))
  13952. ((org-at-table-p)
  13953. (push (list :table (org-table-begin) (org-table-end)) clist)
  13954. (if (memq 'org-formula faces)
  13955. (push (list :table-special
  13956. (previous-single-property-change p 'face)
  13957. (next-single-property-change p 'face)) clist)))
  13958. ((org-at-table-p 'any)
  13959. (push (list :table-table) clist)))
  13960. (goto-char p)
  13961. ;; Now the small context
  13962. (cond
  13963. ((org-at-timestamp-p)
  13964. (push (org-point-in-group p 0 :timestamp) clist))
  13965. ((memq 'org-link faces)
  13966. (push (list :link
  13967. (previous-single-property-change p 'face)
  13968. (next-single-property-change p 'face)) clist))
  13969. ((memq 'org-special-keyword faces)
  13970. (push (list :keyword
  13971. (previous-single-property-change p 'face)
  13972. (next-single-property-change p 'face)) clist))
  13973. ((org-on-target-p)
  13974. (push (org-point-in-group p 0 :target) clist)
  13975. (goto-char (1- (match-beginning 0)))
  13976. (if (looking-at org-radio-target-regexp)
  13977. (push (org-point-in-group p 0 :radio-target) clist))
  13978. (goto-char p))
  13979. ((setq o (car (delq nil
  13980. (mapcar
  13981. (lambda (x)
  13982. (if (memq x org-latex-fragment-image-overlays) x))
  13983. (org-overlays-at (point))))))
  13984. (push (list :latex-fragment
  13985. (org-overlay-start o) (org-overlay-end o)) clist)
  13986. (push (list :latex-preview
  13987. (org-overlay-start o) (org-overlay-end o)) clist))
  13988. ((org-inside-LaTeX-fragment-p)
  13989. ;; FIXME: positions wrong.
  13990. (push (list :latex-fragment (point) (point)) clist)))
  13991. (setq clist (nreverse (delq nil clist)))
  13992. clist))
  13993. ;; FIXME: Compare with at-regexp-p Do we need both?
  13994. (defun org-in-regexp (re &optional nlines visually)
  13995. "Check if point is inside a match of regexp.
  13996. Normally only the current line is checked, but you can include NLINES extra
  13997. lines both before and after point into the search.
  13998. If VISUALLY is set, require that the cursor is not after the match but
  13999. really on, so that the block visually is on the match."
  14000. (catch 'exit
  14001. (let ((pos (point))
  14002. (eol (point-at-eol (+ 1 (or nlines 0))))
  14003. (inc (if visually 1 0)))
  14004. (save-excursion
  14005. (beginning-of-line (- 1 (or nlines 0)))
  14006. (while (re-search-forward re eol t)
  14007. (if (and (<= (match-beginning 0) pos)
  14008. (>= (+ inc (match-end 0)) pos))
  14009. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14010. (defun org-at-regexp-p (regexp)
  14011. "Is point inside a match of REGEXP in the current line?"
  14012. (catch 'exit
  14013. (save-excursion
  14014. (let ((pos (point)) (end (point-at-eol)))
  14015. (beginning-of-line 1)
  14016. (while (re-search-forward regexp end t)
  14017. (if (and (<= (match-beginning 0) pos)
  14018. (>= (match-end 0) pos))
  14019. (throw 'exit t)))
  14020. nil))))
  14021. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14022. "Call `multi-occur' with buffers for all agenda files."
  14023. (interactive "sOrg-files matching: \np")
  14024. (let* ((files (org-agenda-files))
  14025. (tnames (mapcar 'file-truename files))
  14026. (extra org-agenda-text-search-extra-files)
  14027. f)
  14028. (when (eq (car extra) 'agenda-archives)
  14029. (setq extra (cdr extra))
  14030. (setq files (org-add-archive-files files)))
  14031. (while (setq f (pop extra))
  14032. (unless (member (file-truename f) tnames)
  14033. (add-to-list 'files f 'append)
  14034. (add-to-list 'tnames (file-truename f) 'append)))
  14035. (multi-occur
  14036. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14037. regexp)))
  14038. (if (boundp 'occur-mode-find-occurrence-hook)
  14039. ;; Emacs 23
  14040. (add-hook 'occur-mode-find-occurrence-hook
  14041. (lambda ()
  14042. (when (org-mode-p)
  14043. (org-reveal))))
  14044. ;; Emacs 22
  14045. (defadvice occur-mode-goto-occurrence
  14046. (after org-occur-reveal activate)
  14047. (and (org-mode-p) (org-reveal)))
  14048. (defadvice occur-mode-goto-occurrence-other-window
  14049. (after org-occur-reveal activate)
  14050. (and (org-mode-p) (org-reveal)))
  14051. (defadvice occur-mode-display-occurrence
  14052. (after org-occur-reveal activate)
  14053. (when (org-mode-p)
  14054. (let ((pos (occur-mode-find-occurrence)))
  14055. (with-current-buffer (marker-buffer pos)
  14056. (save-excursion
  14057. (goto-char pos)
  14058. (org-reveal)))))))
  14059. (defun org-uniquify (list)
  14060. "Remove duplicate elements from LIST."
  14061. (let (res)
  14062. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14063. res))
  14064. (defun org-delete-all (elts list)
  14065. "Remove all elements in ELTS from LIST."
  14066. (while elts
  14067. (setq list (delete (pop elts) list)))
  14068. list)
  14069. (defun org-back-over-empty-lines ()
  14070. "Move backwards over whitespace, to the beginning of the first empty line.
  14071. Returns the number of empty lines passed."
  14072. (let ((pos (point)))
  14073. (skip-chars-backward " \t\n\r")
  14074. (beginning-of-line 2)
  14075. (goto-char (min (point) pos))
  14076. (count-lines (point) pos)))
  14077. (defun org-skip-whitespace ()
  14078. (skip-chars-forward " \t\n\r"))
  14079. (defun org-point-in-group (point group &optional context)
  14080. "Check if POINT is in match-group GROUP.
  14081. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14082. match. If the match group does ot exist or point is not inside it,
  14083. return nil."
  14084. (and (match-beginning group)
  14085. (>= point (match-beginning group))
  14086. (<= point (match-end group))
  14087. (if context
  14088. (list context (match-beginning group) (match-end group))
  14089. t)))
  14090. (defun org-switch-to-buffer-other-window (&rest args)
  14091. "Switch to buffer in a second window on the current frame.
  14092. In particular, do not allow pop-up frames."
  14093. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14094. special-display-function)
  14095. (apply 'switch-to-buffer-other-window args)))
  14096. (defun org-combine-plists (&rest plists)
  14097. "Create a single property list from all plists in PLISTS.
  14098. The process starts by copying the first list, and then setting properties
  14099. from the other lists. Settings in the last list are the most significant
  14100. ones and overrule settings in the other lists."
  14101. (let ((rtn (copy-sequence (pop plists)))
  14102. p v ls)
  14103. (while plists
  14104. (setq ls (pop plists))
  14105. (while ls
  14106. (setq p (pop ls) v (pop ls))
  14107. (setq rtn (plist-put rtn p v))))
  14108. rtn))
  14109. (defun org-move-line-down (arg)
  14110. "Move the current line down. With prefix argument, move it past ARG lines."
  14111. (interactive "p")
  14112. (let ((col (current-column))
  14113. beg end pos)
  14114. (beginning-of-line 1) (setq beg (point))
  14115. (beginning-of-line 2) (setq end (point))
  14116. (beginning-of-line (+ 1 arg))
  14117. (setq pos (move-marker (make-marker) (point)))
  14118. (insert (delete-and-extract-region beg end))
  14119. (goto-char pos)
  14120. (org-move-to-column col)))
  14121. (defun org-move-line-up (arg)
  14122. "Move the current line up. With prefix argument, move it past ARG lines."
  14123. (interactive "p")
  14124. (let ((col (current-column))
  14125. beg end pos)
  14126. (beginning-of-line 1) (setq beg (point))
  14127. (beginning-of-line 2) (setq end (point))
  14128. (beginning-of-line (- arg))
  14129. (setq pos (move-marker (make-marker) (point)))
  14130. (insert (delete-and-extract-region beg end))
  14131. (goto-char pos)
  14132. (org-move-to-column col)))
  14133. (defun org-replace-escapes (string table)
  14134. "Replace %-escapes in STRING with values in TABLE.
  14135. TABLE is an association list with keys like \"%a\" and string values.
  14136. The sequences in STRING may contain normal field width and padding information,
  14137. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14138. so values can contain further %-escapes if they are define later in TABLE."
  14139. (let ((case-fold-search nil)
  14140. e re rpl)
  14141. (while (setq e (pop table))
  14142. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14143. (while (string-match re string)
  14144. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14145. (cdr e)))
  14146. (setq string (replace-match rpl t t string))))
  14147. string))
  14148. (defun org-sublist (list start end)
  14149. "Return a section of LIST, from START to END.
  14150. Counting starts at 1."
  14151. (let (rtn (c start))
  14152. (setq list (nthcdr (1- start) list))
  14153. (while (and list (<= c end))
  14154. (push (pop list) rtn)
  14155. (setq c (1+ c)))
  14156. (nreverse rtn)))
  14157. (defun org-find-base-buffer-visiting (file)
  14158. "Like `find-buffer-visiting' but alway return the base buffer and
  14159. not an indirect buffer."
  14160. (let ((buf (find-buffer-visiting file)))
  14161. (if buf
  14162. (or (buffer-base-buffer buf) buf)
  14163. nil)))
  14164. (defun org-image-file-name-regexp (&optional extensions)
  14165. "Return regexp matching the file names of images.
  14166. If EXTENSIONS is given, only match these."
  14167. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14168. (image-file-name-regexp)
  14169. (let ((image-file-name-extensions
  14170. (or extensions
  14171. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14172. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14173. (concat "\\."
  14174. (regexp-opt (nconc (mapcar 'upcase
  14175. image-file-name-extensions)
  14176. image-file-name-extensions)
  14177. t)
  14178. "\\'"))))
  14179. (defun org-file-image-p (file &optional extensions)
  14180. "Return non-nil if FILE is an image."
  14181. (save-match-data
  14182. (string-match (org-image-file-name-regexp extensions) file)))
  14183. (defun org-get-cursor-date ()
  14184. "Return the date at cursor in as a time.
  14185. This works in the calendar and in the agenda, anywhere else it just
  14186. returns the current time."
  14187. (let (date day defd)
  14188. (cond
  14189. ((eq major-mode 'calendar-mode)
  14190. (setq date (calendar-cursor-to-date)
  14191. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14192. ((eq major-mode 'org-agenda-mode)
  14193. (setq day (get-text-property (point) 'day))
  14194. (if day
  14195. (setq date (calendar-gregorian-from-absolute day)
  14196. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14197. (nth 2 date))))))
  14198. (or defd (current-time))))
  14199. (defvar org-agenda-action-marker (make-marker)
  14200. "Marker pointing to the entry for the next agenda action.")
  14201. (defun org-mark-entry-for-agenda-action ()
  14202. "Mark the current entry as target of an agenda action.
  14203. Agenda actions are actions executed from the agenda with the key `k',
  14204. which make use of the date at the cursor."
  14205. (interactive)
  14206. (move-marker org-agenda-action-marker
  14207. (save-excursion (org-back-to-heading t) (point))
  14208. (current-buffer))
  14209. (message
  14210. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14211. ;;; Paragraph filling stuff.
  14212. ;; We want this to be just right, so use the full arsenal.
  14213. (defun org-indent-line-function ()
  14214. "Indent line like previous, but further if previous was headline or item."
  14215. (interactive)
  14216. (let* ((pos (point))
  14217. (itemp (org-at-item-p))
  14218. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14219. column bpos bcol tpos tcol bullet btype bullet-type)
  14220. ;; Find the previous relevant line
  14221. (beginning-of-line 1)
  14222. (cond
  14223. ((looking-at "#") (setq column 0))
  14224. ((looking-at "\\*+ ") (setq column 0))
  14225. ((and (looking-at "[ \t]*:END:")
  14226. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14227. (save-excursion
  14228. (goto-char (1- (match-beginning 1)))
  14229. (setq column (current-column))))
  14230. (t
  14231. (beginning-of-line 0)
  14232. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14233. (not (looking-at "[ \t]*:END:"))
  14234. (not (looking-at org-drawer-regexp)))
  14235. (beginning-of-line 0))
  14236. (cond
  14237. ((looking-at "\\*+[ \t]+")
  14238. (if (not org-adapt-indentation)
  14239. (setq column 0)
  14240. (goto-char (match-end 0))
  14241. (setq column (current-column))))
  14242. ((looking-at org-drawer-regexp)
  14243. (goto-char (1- (match-beginning 1)))
  14244. (setq column (current-column)))
  14245. ((looking-at "\\([ \t]*\\):END:")
  14246. (goto-char (match-end 1))
  14247. (setq column (current-column)))
  14248. ((org-in-item-p)
  14249. (org-beginning-of-item)
  14250. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14251. (setq bpos (match-beginning 1) tpos (match-end 0)
  14252. bcol (progn (goto-char bpos) (current-column))
  14253. tcol (progn (goto-char tpos) (current-column))
  14254. bullet (match-string 1)
  14255. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14256. (if (> tcol (+ bcol org-description-max-indent))
  14257. (setq tcol (+ bcol 5)))
  14258. (if (not itemp)
  14259. (setq column tcol)
  14260. (goto-char pos)
  14261. (beginning-of-line 1)
  14262. (if (looking-at "\\S-")
  14263. (progn
  14264. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14265. (setq bullet (match-string 1)
  14266. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14267. (setq column (if (equal btype bullet-type) bcol tcol)))
  14268. (setq column (org-get-indentation)))))
  14269. (t (setq column (org-get-indentation))))))
  14270. (goto-char pos)
  14271. (if (<= (current-column) (current-indentation))
  14272. (org-indent-line-to column)
  14273. (save-excursion (org-indent-line-to column)))
  14274. (setq column (current-column))
  14275. (beginning-of-line 1)
  14276. (if (looking-at
  14277. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14278. (replace-match (concat "\\1" (format org-property-format
  14279. (match-string 2) (match-string 3)))
  14280. t nil))
  14281. (org-move-to-column column)))
  14282. (defun org-set-autofill-regexps ()
  14283. (interactive)
  14284. ;; In the paragraph separator we include headlines, because filling
  14285. ;; text in a line directly attached to a headline would otherwise
  14286. ;; fill the headline as well.
  14287. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14288. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14289. ;; The paragraph starter includes hand-formatted lists.
  14290. (org-set-local 'paragraph-start
  14291. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14292. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14293. ;; But only if the user has not turned off tables or fixed-width regions
  14294. (org-set-local
  14295. 'auto-fill-inhibit-regexp
  14296. (concat "\\*+ \\|#\\+"
  14297. "\\|[ \t]*" org-keyword-time-regexp
  14298. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14299. (concat
  14300. "\\|[ \t]*["
  14301. (if org-enable-table-editor "|" "")
  14302. (if org-enable-fixed-width-editor ":" "")
  14303. "]"))))
  14304. ;; We use our own fill-paragraph function, to make sure that tables
  14305. ;; and fixed-width regions are not wrapped. That function will pass
  14306. ;; through to `fill-paragraph' when appropriate.
  14307. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14308. ; Adaptive filling: To get full control, first make sure that
  14309. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14310. (org-set-local 'adaptive-fill-regexp "\000")
  14311. (org-set-local 'adaptive-fill-function
  14312. 'org-adaptive-fill-function)
  14313. (org-set-local
  14314. 'align-mode-rules-list
  14315. '((org-in-buffer-settings
  14316. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14317. (modes . '(org-mode))))))
  14318. (defun org-fill-paragraph (&optional justify)
  14319. "Re-align a table, pass through to fill-paragraph if no table."
  14320. (let ((table-p (org-at-table-p))
  14321. (table.el-p (org-at-table.el-p)))
  14322. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14323. (save-excursion (goto-char (point-at-bol))
  14324. (looking-at outline-regexp)))
  14325. t) ; skip headlines
  14326. (table.el-p t) ; skip table.el tables
  14327. (table-p (org-table-align) t) ; align org-mode tables
  14328. (t nil)))) ; call paragraph-fill
  14329. ;; For reference, this is the default value of adaptive-fill-regexp
  14330. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14331. (defun org-adaptive-fill-function ()
  14332. "Return a fill prefix for org-mode files.
  14333. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14334. work correctly."
  14335. (cond ((looking-at "#[ \t]+")
  14336. (match-string 0))
  14337. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14338. (save-excursion
  14339. (if (> (match-end 1) (+ (match-beginning 1)
  14340. org-description-max-indent))
  14341. (goto-char (+ (match-beginning 1) 5))
  14342. (goto-char (match-end 0)))
  14343. (make-string (current-column) ?\ )))
  14344. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14345. (save-excursion
  14346. (goto-char (match-end 0))
  14347. (make-string (current-column) ?\ )))
  14348. (t nil)))
  14349. ;;; Other stuff.
  14350. (defun org-toggle-fixed-width-section (arg)
  14351. "Toggle the fixed-width export.
  14352. If there is no active region, the QUOTE keyword at the current headline is
  14353. inserted or removed. When present, it causes the text between this headline
  14354. and the next to be exported as fixed-width text, and unmodified.
  14355. If there is an active region, this command adds or removes a colon as the
  14356. first character of this line. If the first character of a line is a colon,
  14357. this line is also exported in fixed-width font."
  14358. (interactive "P")
  14359. (let* ((cc 0)
  14360. (regionp (org-region-active-p))
  14361. (beg (if regionp (region-beginning) (point)))
  14362. (end (if regionp (region-end)))
  14363. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14364. (case-fold-search nil)
  14365. (re "[ \t]*\\(: \\)")
  14366. off)
  14367. (if regionp
  14368. (save-excursion
  14369. (goto-char beg)
  14370. (setq cc (current-column))
  14371. (beginning-of-line 1)
  14372. (setq off (looking-at re))
  14373. (while (> nlines 0)
  14374. (setq nlines (1- nlines))
  14375. (beginning-of-line 1)
  14376. (cond
  14377. (arg
  14378. (org-move-to-column cc t)
  14379. (insert ": \n")
  14380. (forward-line -1))
  14381. ((and off (looking-at re))
  14382. (replace-match "" t t nil 1))
  14383. ((not off) (org-move-to-column cc t) (insert ": ")))
  14384. (forward-line 1)))
  14385. (save-excursion
  14386. (org-back-to-heading)
  14387. (if (looking-at (concat outline-regexp
  14388. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14389. (replace-match "" t t nil 1)
  14390. (if (looking-at outline-regexp)
  14391. (progn
  14392. (goto-char (match-end 0))
  14393. (insert org-quote-string " "))))))))
  14394. (defun org-reftex-citation ()
  14395. "Use reftex-citation to insert a citation into the buffer.
  14396. This looks for a line like
  14397. #+BIBLIOGRAPHY: foo plain option:-d
  14398. and derives from it that foo.bib is the bbliography file relevant
  14399. for this document. It then installs the necessary environment for RefTeX
  14400. to work in this buffer and calls `reftex-citation' to insert a citation
  14401. into the buffer.
  14402. Export of such citations to both LaTeX and HTML is handled by the contributed
  14403. package org-exp-bibtex by Taru Karttunen."
  14404. (interactive)
  14405. (let ((reftex-docstruct-symbol 'rds)
  14406. (reftex-cite-format "\\cite{%l}")
  14407. rds bib)
  14408. (save-excursion
  14409. (save-restriction
  14410. (widen)
  14411. (let ((case-fold-search t)
  14412. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14413. (if (not (save-excursion
  14414. (or (re-search-forward re nil t)
  14415. (re-search-backward re nil t))))
  14416. (error "No bibliography defined in file")
  14417. (setq bib (concat (match-string 1) ".bib")
  14418. rds (list (list 'bib bib)))))))
  14419. (call-interactively 'reftex-citation)))
  14420. ;;;; Functions extending outline functionality
  14421. (defun org-beginning-of-line (&optional arg)
  14422. "Go to the beginning of the current line. If that is invisible, continue
  14423. to a visible line beginning. This makes the function of C-a more intuitive.
  14424. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14425. first attempt, and only move to after the tags when the cursor is already
  14426. beyond the end of the headline."
  14427. (interactive "P")
  14428. (let ((pos (point))
  14429. (special (if (consp org-special-ctrl-a/e)
  14430. (car org-special-ctrl-a/e)
  14431. org-special-ctrl-a/e))
  14432. refpos)
  14433. (beginning-of-line 1)
  14434. (if (and arg (fboundp 'move-beginning-of-line))
  14435. (call-interactively 'move-beginning-of-line)
  14436. (if (bobp)
  14437. nil
  14438. (backward-char 1)
  14439. (if (org-invisible-p)
  14440. (while (and (not (bobp)) (org-invisible-p))
  14441. (backward-char 1)
  14442. (beginning-of-line 1))
  14443. (forward-char 1))))
  14444. (when special
  14445. (cond
  14446. ((and (looking-at org-complex-heading-regexp)
  14447. (= (char-after (match-end 1)) ?\ ))
  14448. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14449. (point-at-eol)))
  14450. (goto-char
  14451. (if (eq special t)
  14452. (cond ((> pos refpos) refpos)
  14453. ((= pos (point)) refpos)
  14454. (t (point)))
  14455. (cond ((> pos (point)) (point))
  14456. ((not (eq last-command this-command)) (point))
  14457. (t refpos)))))
  14458. ((org-at-item-p)
  14459. (goto-char
  14460. (if (eq special t)
  14461. (cond ((> pos (match-end 4)) (match-end 4))
  14462. ((= pos (point)) (match-end 4))
  14463. (t (point)))
  14464. (cond ((> pos (point)) (point))
  14465. ((not (eq last-command this-command)) (point))
  14466. (t (match-end 4))))))))
  14467. (org-no-warnings
  14468. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14469. (defun org-end-of-line (&optional arg)
  14470. "Go to the end of the line.
  14471. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14472. first attempt, and only move to after the tags when the cursor is already
  14473. beyond the end of the headline."
  14474. (interactive "P")
  14475. (let ((special (if (consp org-special-ctrl-a/e)
  14476. (cdr org-special-ctrl-a/e)
  14477. org-special-ctrl-a/e)))
  14478. (if (or (not special)
  14479. (not (org-on-heading-p))
  14480. arg)
  14481. (call-interactively (if (fboundp 'move-end-of-line)
  14482. 'move-end-of-line
  14483. 'end-of-line))
  14484. (let ((pos (point)))
  14485. (beginning-of-line 1)
  14486. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14487. (if (eq special t)
  14488. (if (or (< pos (match-beginning 1))
  14489. (= pos (match-end 0)))
  14490. (goto-char (match-beginning 1))
  14491. (goto-char (match-end 0)))
  14492. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14493. (goto-char (match-end 0))
  14494. (goto-char (match-beginning 1))))
  14495. (call-interactively (if (fboundp 'move-end-of-line)
  14496. 'move-end-of-line
  14497. 'end-of-line)))))
  14498. (org-no-warnings
  14499. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14500. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14501. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14502. (defun org-backward-sentence (&optional arg)
  14503. "Go to beginning of sentence, or beginning of table field.
  14504. This will call `backward-sentence' or `org-table-beginning-of-field',
  14505. depending on context."
  14506. (interactive "P")
  14507. (cond
  14508. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14509. (t (call-interactively 'backward-sentence))))
  14510. (defun org-forward-sentence (&optional arg)
  14511. "Go to end of sentence, or end of table field.
  14512. This will call `forward-sentence' or `org-table-end-of-field',
  14513. depending on context."
  14514. (interactive "P")
  14515. (cond
  14516. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14517. (t (call-interactively 'forward-sentence))))
  14518. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14519. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14520. (defun org-kill-line (&optional arg)
  14521. "Kill line, to tags or end of line."
  14522. (interactive "P")
  14523. (cond
  14524. ((or (not org-special-ctrl-k)
  14525. (bolp)
  14526. (not (org-on-heading-p)))
  14527. (call-interactively 'kill-line))
  14528. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14529. (kill-region (point) (match-beginning 1))
  14530. (org-set-tags nil t))
  14531. (t (kill-region (point) (point-at-eol)))))
  14532. (define-key org-mode-map "\C-k" 'org-kill-line)
  14533. (defun org-yank (&optional arg)
  14534. "Yank. If the kill is a subtree, treat it specially.
  14535. This command will look at the current kill and check if is a single
  14536. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14537. cursor is at the beginning of a line or after the stars of a currently
  14538. empty headline, then the yank is handled specially. How exactly depends
  14539. on the value of the following variables, both set by default.
  14540. org-yank-folded-subtrees
  14541. When set, the subtree(s) will be folded after insertion, but only
  14542. if doing so would now swallow text after the yanked text.
  14543. org-yank-adjusted-subtrees
  14544. When set, the subtree will be promoted or demoted in order to
  14545. fit into the local outline tree structure, which means that the level
  14546. will be adjusted so that it becomes the smaller one of the two
  14547. *visible* surrounding headings.
  14548. Any prefix to this command will cause `yank' to be called directly with
  14549. no special treatment. In particular, a simple `C-u' prefix will just
  14550. plainly yank the text as it is.
  14551. \[1] Basically, the test checks if the first non-white line is a heading
  14552. and if there are no other headings with fewer stars."
  14553. (interactive "P")
  14554. (setq this-command 'yank)
  14555. (if arg
  14556. (call-interactively 'yank)
  14557. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14558. (and (org-kill-is-subtree-p)
  14559. (or (bolp)
  14560. (and (looking-at "[ \t]*$")
  14561. (string-match
  14562. "\\`\\*+\\'"
  14563. (buffer-substring (point-at-bol) (point)))))))
  14564. swallowp)
  14565. (cond
  14566. ((and subtreep org-yank-folded-subtrees)
  14567. (let ((beg (point))
  14568. end)
  14569. (if (and subtreep org-yank-adjusted-subtrees)
  14570. (org-paste-subtree nil nil 'for-yank)
  14571. (call-interactively 'yank))
  14572. (setq end (point))
  14573. (goto-char beg)
  14574. (when (and (bolp) subtreep
  14575. (not (setq swallowp
  14576. (org-yank-folding-would-swallow-text beg end))))
  14577. (or (looking-at outline-regexp)
  14578. (re-search-forward (concat "^" outline-regexp) end t))
  14579. (while (and (< (point) end) (looking-at outline-regexp))
  14580. (hide-subtree)
  14581. (org-cycle-show-empty-lines 'folded)
  14582. (condition-case nil
  14583. (outline-forward-same-level 1)
  14584. (error (goto-char end)))))
  14585. (when swallowp
  14586. (message
  14587. "Yanked text not folded because that would swallow text"))
  14588. (goto-char end)
  14589. (skip-chars-forward " \t\n\r")
  14590. (beginning-of-line 1)
  14591. (push-mark beg 'nomsg)))
  14592. ((and subtreep org-yank-adjusted-subtrees)
  14593. (let ((beg (point-at-bol)))
  14594. (org-paste-subtree nil nil 'for-yank)
  14595. (push-mark beg 'nomsg)))
  14596. (t
  14597. (call-interactively 'yank))))))
  14598. (defun org-yank-folding-would-swallow-text (beg end)
  14599. "Would hide-subtree at BEG swallow any text after END?"
  14600. (let (level)
  14601. (save-excursion
  14602. (goto-char beg)
  14603. (when (or (looking-at outline-regexp)
  14604. (re-search-forward (concat "^" outline-regexp) end t))
  14605. (setq level (org-outline-level)))
  14606. (goto-char end)
  14607. (skip-chars-forward " \t\r\n\v\f")
  14608. (if (or (eobp)
  14609. (and (bolp) (looking-at org-outline-regexp)
  14610. (<= (org-outline-level) level)))
  14611. nil ; Nothing would be swallowed
  14612. t)))) ; something would swallow
  14613. (define-key org-mode-map "\C-y" 'org-yank)
  14614. (defun org-invisible-p ()
  14615. "Check if point is at a character currently not visible."
  14616. ;; Early versions of noutline don't have `outline-invisible-p'.
  14617. (if (fboundp 'outline-invisible-p)
  14618. (outline-invisible-p)
  14619. (get-char-property (point) 'invisible)))
  14620. (defun org-invisible-p2 ()
  14621. "Check if point is at a character currently not visible."
  14622. (save-excursion
  14623. (if (and (eolp) (not (bobp))) (backward-char 1))
  14624. ;; Early versions of noutline don't have `outline-invisible-p'.
  14625. (if (fboundp 'outline-invisible-p)
  14626. (outline-invisible-p)
  14627. (get-char-property (point) 'invisible))))
  14628. (defun org-back-to-heading (&optional invisible-ok)
  14629. "Call `outline-back-to-heading', but provide a better error message."
  14630. (condition-case nil
  14631. (outline-back-to-heading invisible-ok)
  14632. (error (error "Before first headline at position %d in buffer %s"
  14633. (point) (current-buffer)))))
  14634. (defun org-before-first-heading-p ()
  14635. "Before first heading?"
  14636. (save-excursion
  14637. (null (re-search-backward "^\\*+ " nil t))))
  14638. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14639. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14640. (defun org-at-heading-or-item-p ()
  14641. (or (org-on-heading-p) (org-at-item-p)))
  14642. (defun org-on-target-p ()
  14643. (or (org-in-regexp org-radio-target-regexp)
  14644. (org-in-regexp org-target-regexp)))
  14645. (defun org-up-heading-all (arg)
  14646. "Move to the heading line of which the present line is a subheading.
  14647. This function considers both visible and invisible heading lines.
  14648. With argument, move up ARG levels."
  14649. (if (fboundp 'outline-up-heading-all)
  14650. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14651. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14652. (defun org-up-heading-safe ()
  14653. "Move to the heading line of which the present line is a subheading.
  14654. This version will not throw an error. It will return the level of the
  14655. headline found, or nil if no higher level is found.
  14656. Also, this function will be a lot faster than `outline-up-heading',
  14657. because it relies on stars being the outline starters. This can really
  14658. make a significant difference in outlines with very many siblings."
  14659. (let (start-level re)
  14660. (org-back-to-heading t)
  14661. (setq start-level (funcall outline-level))
  14662. (if (equal start-level 1)
  14663. nil
  14664. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14665. (if (re-search-backward re nil t)
  14666. (funcall outline-level)))))
  14667. (defun org-first-sibling-p ()
  14668. "Is this heading the first child of its parents?"
  14669. (interactive)
  14670. (let ((re (concat "^" outline-regexp))
  14671. level l)
  14672. (unless (org-at-heading-p t)
  14673. (error "Not at a heading"))
  14674. (setq level (funcall outline-level))
  14675. (save-excursion
  14676. (if (not (re-search-backward re nil t))
  14677. t
  14678. (setq l (funcall outline-level))
  14679. (< l level)))))
  14680. (defun org-goto-sibling (&optional previous)
  14681. "Goto the next sibling, even if it is invisible.
  14682. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14683. when a sibling was found. When none is found, return nil and don't
  14684. move point."
  14685. (let ((fun (if previous 're-search-backward 're-search-forward))
  14686. (pos (point))
  14687. (re (concat "^" outline-regexp))
  14688. level l)
  14689. (when (condition-case nil (org-back-to-heading t) (error nil))
  14690. (setq level (funcall outline-level))
  14691. (catch 'exit
  14692. (or previous (forward-char 1))
  14693. (while (funcall fun re nil t)
  14694. (setq l (funcall outline-level))
  14695. (when (< l level) (goto-char pos) (throw 'exit nil))
  14696. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14697. (goto-char pos)
  14698. nil))))
  14699. (defun org-show-siblings ()
  14700. "Show all siblings of the current headline."
  14701. (save-excursion
  14702. (while (org-goto-sibling) (org-flag-heading nil)))
  14703. (save-excursion
  14704. (while (org-goto-sibling 'previous)
  14705. (org-flag-heading nil))))
  14706. (defun org-show-hidden-entry ()
  14707. "Show an entry where even the heading is hidden."
  14708. (save-excursion
  14709. (org-show-entry)))
  14710. (defun org-flag-heading (flag &optional entry)
  14711. "Flag the current heading. FLAG non-nil means make invisible.
  14712. When ENTRY is non-nil, show the entire entry."
  14713. (save-excursion
  14714. (org-back-to-heading t)
  14715. ;; Check if we should show the entire entry
  14716. (if entry
  14717. (progn
  14718. (org-show-entry)
  14719. (save-excursion
  14720. (and (outline-next-heading)
  14721. (org-flag-heading nil))))
  14722. (outline-flag-region (max (point-min) (1- (point)))
  14723. (save-excursion (outline-end-of-heading) (point))
  14724. flag))))
  14725. (defun org-forward-same-level (arg)
  14726. "Move forward to the ARG'th subheading at same level as this one.
  14727. Stop at the first and last subheadings of a superior heading.
  14728. This is like outline-forward-same-level, but invisible headings are ok."
  14729. (interactive "p")
  14730. (org-back-to-heading t)
  14731. (while (> arg 0)
  14732. (let ((point-to-move-to (save-excursion
  14733. (org-get-next-sibling))))
  14734. (if point-to-move-to
  14735. (progn
  14736. (goto-char point-to-move-to)
  14737. (setq arg (1- arg)))
  14738. (progn
  14739. (setq arg 0)
  14740. (error "No following same-level heading"))))))
  14741. (defun org-get-next-sibling ()
  14742. "Move to next heading of the same level, and return point.
  14743. If there is no such heading, return nil.
  14744. This is like outline-next-sibling, but invisible headings are ok."
  14745. (let ((level (funcall outline-level)))
  14746. (outline-next-heading)
  14747. (while (and (not (eobp)) (> (funcall outline-level) level))
  14748. (outline-next-heading))
  14749. (if (or (eobp) (< (funcall outline-level) level))
  14750. nil
  14751. (point))))
  14752. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14753. ;; This is an exact copy of the original function, but it uses
  14754. ;; `org-back-to-heading', to make it work also in invisible
  14755. ;; trees. And is uses an invisible-OK argument.
  14756. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14757. (org-back-to-heading invisible-OK)
  14758. (let ((first t)
  14759. (level (funcall outline-level)))
  14760. (while (and (not (eobp))
  14761. (or first (> (funcall outline-level) level)))
  14762. (setq first nil)
  14763. (outline-next-heading))
  14764. (unless to-heading
  14765. (if (memq (preceding-char) '(?\n ?\^M))
  14766. (progn
  14767. ;; Go to end of line before heading
  14768. (forward-char -1)
  14769. (if (memq (preceding-char) '(?\n ?\^M))
  14770. ;; leave blank line before heading
  14771. (forward-char -1))))))
  14772. (point))
  14773. (defun org-show-subtree ()
  14774. "Show everything after this heading at deeper levels."
  14775. (outline-flag-region
  14776. (point)
  14777. (save-excursion
  14778. (outline-end-of-subtree) (outline-next-heading) (point))
  14779. nil))
  14780. (defun org-show-entry ()
  14781. "Show the body directly following this heading.
  14782. Show the heading too, if it is currently invisible."
  14783. (interactive)
  14784. (save-excursion
  14785. (condition-case nil
  14786. (progn
  14787. (org-back-to-heading t)
  14788. (outline-flag-region
  14789. (max (point-min) (1- (point)))
  14790. (save-excursion
  14791. (if (re-search-forward
  14792. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14793. (match-beginning 1)
  14794. (point-max)))
  14795. nil))
  14796. (error nil))))
  14797. (defun org-make-options-regexp (kwds &optional extra)
  14798. "Make a regular expression for keyword lines."
  14799. (concat
  14800. "^"
  14801. "#?[ \t]*\\+\\("
  14802. (mapconcat 'regexp-quote kwds "\\|")
  14803. (if extra (concat "\\|" extra))
  14804. "\\):[ \t]*"
  14805. "\\(.+\\)"))
  14806. ;; Make isearch reveal the necessary context
  14807. (defun org-isearch-end ()
  14808. "Reveal context after isearch exits."
  14809. (when isearch-success ; only if search was successful
  14810. (if (featurep 'xemacs)
  14811. ;; Under XEmacs, the hook is run in the correct place,
  14812. ;; we directly show the context.
  14813. (org-show-context 'isearch)
  14814. ;; In Emacs the hook runs *before* restoring the overlays.
  14815. ;; So we have to use a one-time post-command-hook to do this.
  14816. ;; (Emacs 22 has a special variable, see function `org-mode')
  14817. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14818. isearch-mode-end-hook-quit)
  14819. ;; Only when the isearch was not quitted.
  14820. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14821. 'append 'local)))))
  14822. (defun org-isearch-post-command ()
  14823. "Remove self from hook, and show context."
  14824. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14825. (org-show-context 'isearch))
  14826. ;;;; Integration with and fixes for other packages
  14827. ;;; Imenu support
  14828. (defvar org-imenu-markers nil
  14829. "All markers currently used by Imenu.")
  14830. (make-variable-buffer-local 'org-imenu-markers)
  14831. (defun org-imenu-new-marker (&optional pos)
  14832. "Return a new marker for use by Imenu, and remember the marker."
  14833. (let ((m (make-marker)))
  14834. (move-marker m (or pos (point)))
  14835. (push m org-imenu-markers)
  14836. m))
  14837. (defun org-imenu-get-tree ()
  14838. "Produce the index for Imenu."
  14839. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14840. (setq org-imenu-markers nil)
  14841. (let* ((n org-imenu-depth)
  14842. (re (concat "^" outline-regexp))
  14843. (subs (make-vector (1+ n) nil))
  14844. (last-level 0)
  14845. m level head)
  14846. (save-excursion
  14847. (save-restriction
  14848. (widen)
  14849. (goto-char (point-max))
  14850. (while (re-search-backward re nil t)
  14851. (setq level (org-reduced-level (funcall outline-level)))
  14852. (when (<= level n)
  14853. (looking-at org-complex-heading-regexp)
  14854. (setq head (org-link-display-format
  14855. (org-match-string-no-properties 4))
  14856. m (org-imenu-new-marker))
  14857. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14858. (if (>= level last-level)
  14859. (push (cons head m) (aref subs level))
  14860. (push (cons head (aref subs (1+ level))) (aref subs level))
  14861. (loop for i from (1+ level) to n do (aset subs i nil)))
  14862. (setq last-level level)))))
  14863. (aref subs 1)))
  14864. (eval-after-load "imenu"
  14865. '(progn
  14866. (add-hook 'imenu-after-jump-hook
  14867. (lambda ()
  14868. (if (eq major-mode 'org-mode)
  14869. (org-show-context 'org-goto))))))
  14870. (defun org-link-display-format (link)
  14871. "Replace a link with either the description, or the link target
  14872. if no description is present"
  14873. (save-match-data
  14874. (if (string-match org-bracket-link-analytic-regexp link)
  14875. (replace-match (or (match-string 5 link)
  14876. (concat (match-string 1 link)
  14877. (match-string 3 link)))
  14878. nil nil link)
  14879. link)))
  14880. ;; Speedbar support
  14881. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14882. "Overlay marking the agenda restriction line in speedbar.")
  14883. (org-overlay-put org-speedbar-restriction-lock-overlay
  14884. 'face 'org-agenda-restriction-lock)
  14885. (org-overlay-put org-speedbar-restriction-lock-overlay
  14886. 'help-echo "Agendas are currently limited to this item.")
  14887. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14888. (defun org-speedbar-set-agenda-restriction ()
  14889. "Restrict future agenda commands to the location at point in speedbar.
  14890. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14891. (interactive)
  14892. (require 'org-agenda)
  14893. (let (p m tp np dir txt)
  14894. (cond
  14895. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14896. 'org-imenu t))
  14897. (setq m (get-text-property p 'org-imenu-marker))
  14898. (save-excursion
  14899. (save-restriction
  14900. (set-buffer (marker-buffer m))
  14901. (goto-char m)
  14902. (org-agenda-set-restriction-lock 'subtree))))
  14903. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14904. 'speedbar-function 'speedbar-find-file))
  14905. (setq tp (previous-single-property-change
  14906. (1+ p) 'speedbar-function)
  14907. np (next-single-property-change
  14908. tp 'speedbar-function)
  14909. dir (speedbar-line-directory)
  14910. txt (buffer-substring-no-properties (or tp (point-min))
  14911. (or np (point-max))))
  14912. (save-excursion
  14913. (save-restriction
  14914. (set-buffer (find-file-noselect
  14915. (let ((default-directory dir))
  14916. (expand-file-name txt))))
  14917. (unless (org-mode-p)
  14918. (error "Cannot restrict to non-Org-mode file"))
  14919. (org-agenda-set-restriction-lock 'file))))
  14920. (t (error "Don't know how to restrict Org-mode's agenda")))
  14921. (org-move-overlay org-speedbar-restriction-lock-overlay
  14922. (point-at-bol) (point-at-eol))
  14923. (setq current-prefix-arg nil)
  14924. (org-agenda-maybe-redo)))
  14925. (eval-after-load "speedbar"
  14926. '(progn
  14927. (speedbar-add-supported-extension ".org")
  14928. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14929. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14930. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14931. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14932. (add-hook 'speedbar-visiting-tag-hook
  14933. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14934. ;;; Fixes and Hacks for problems with other packages
  14935. ;; Make flyspell not check words in links, to not mess up our keymap
  14936. (defun org-mode-flyspell-verify ()
  14937. "Don't let flyspell put overlays at active buttons."
  14938. (not (get-text-property (point) 'keymap)))
  14939. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14940. (eval-after-load "bookmark"
  14941. '(if (boundp 'bookmark-after-jump-hook)
  14942. ;; We can use the hook
  14943. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14944. ;; Hook not available, use advice
  14945. (defadvice bookmark-jump (after org-make-visible activate)
  14946. "Make the position visible."
  14947. (org-bookmark-jump-unhide))))
  14948. ;; Make sure saveplace show the location if it was hidden
  14949. (eval-after-load "saveplace"
  14950. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14951. "Make the position visible."
  14952. (org-bookmark-jump-unhide)))
  14953. (defun org-bookmark-jump-unhide ()
  14954. "Unhide the current position, to show the bookmark location."
  14955. (and (org-mode-p)
  14956. (or (org-invisible-p)
  14957. (save-excursion (goto-char (max (point-min) (1- (point))))
  14958. (org-invisible-p)))
  14959. (org-show-context 'bookmark-jump)))
  14960. ;; Make session.el ignore our circular variable
  14961. (eval-after-load "session"
  14962. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14963. ;;;; Experimental code
  14964. (defun org-closed-in-range ()
  14965. "Sparse tree of items closed in a certain time range.
  14966. Still experimental, may disappear in the future."
  14967. (interactive)
  14968. ;; Get the time interval from the user.
  14969. (let* ((time1 (time-to-seconds
  14970. (org-read-date nil 'to-time nil "Starting date: ")))
  14971. (time2 (time-to-seconds
  14972. (org-read-date nil 'to-time nil "End date:")))
  14973. ;; callback function
  14974. (callback (lambda ()
  14975. (let ((time
  14976. (time-to-seconds
  14977. (apply 'encode-time
  14978. (org-parse-time-string
  14979. (match-string 1))))))
  14980. ;; check if time in interval
  14981. (and (>= time time1) (<= time time2))))))
  14982. ;; make tree, check each match with the callback
  14983. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14984. ;;;; Finish up
  14985. (provide 'org)
  14986. (run-hooks 'org-load-hook)
  14987. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14988. ;;; org.el ends here